






import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


lowercase  							= 500000

lowercase							,	lowercase  							= os.path.split(__file__)
lowercase  							= os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json"))





@get_duration
def 						__UpperCAmelCase      (					a_):
							snake_case_           =							dataset.select(range(0							,					len(a_)							,					2))





@get_duration
def 						__UpperCAmelCase      (					a_):
							snake_case_           =							dataset.sort('numbers')





@get_duration
def 						__UpperCAmelCase      (					a_):
							snake_case_           =							dataset.shuffle()





@get_duration
def 						__UpperCAmelCase      (					a_):
							snake_case_           =							dataset.train_test_split(0.1)





@get_duration
def 						__UpperCAmelCase      (					a_							,					a_=10):
							for shard_id in range(a_):
														snake_case_           =							dataset.shard(a_							,					a_)





def 						__UpperCAmelCase      (					):
							snake_case_           =							{'num examples': SPEED_TEST_N_EXAMPLES}
							snake_case_           =							(select, sort, shuffle, train_test_split, shard)
							with tempfile.TemporaryDirectory() as tmp_dir:
														print('generating dataset')
														snake_case_           =							datasets.Features({'text': datasets.Value('string'), 'numbers': datasets.Value('float32')})
														snake_case_           =							generate_example_dataset(
														    os.path.join(a_							,					'dataset.arrow')							,					a_							,					num_examples=a_)
														print('Functions')
														for func in functions:
																					print(func.__name__)
																					snake_case_           =							func(a_)

							with open(a_							,					'wb') as f:
														f.write(json.dumps(a_).encode('utf-8'))


if __name__ == "__main__":  # useful to run the profiler
	benchmark_indices_mapping()













import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
	import transformer_engine.pytorch as te





def 						__UpperCAmelCase      (					a_							,					a_=True							,					a_=True							,					a_=True):
							if not is_fpa_available():
														raise ImportError('Using `convert_model` requires transformer_engine to be installed.')
							for name, module in model.named_children():
														if isinstance(a_							,					nn.Linear) and to_transformer_engine and _convert_linear:
																					# Return early if the linear layer weights are not multiples of 16
																					if any(p % 16 != 0 for p in module.weight.shape):
																												return
																					snake_case_           =							module.bias is not None
																					snake_case_           =							te.Linear(
																					    module.in_features							,					module.out_features							,					bias=a_							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					if has_bias:
																												snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					nn.LayerNorm) and to_transformer_engine and _convert_ln:
																					snake_case_           =							te.LayerNorm(module.normalized_shape[0]							,					eps=module.eps							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					te.Linear) and not to_transformer_engine and _convert_linear:
																					snake_case_           =							module.bias is not None
																					snake_case_           =							nn.Linear(
																					    module.in_features							,					module.out_features							,					bias=a_							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					if has_bias:
																												snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					te.LayerNorm) and not to_transformer_engine and _convert_ln:
																					snake_case_           =							nn.LayerNorm(module.normalized_shape[0]							,					eps=module.eps							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														else:
																					convert_model(
																					    a_							,					to_transformer_engine=a_							,					_convert_linear=_convert_linear							,					_convert_ln=_convert_ln							,					)





def 						__UpperCAmelCase      (					a_):
							if not is_fpa_available():
														raise ImportError('Using `has_transformer_engine_layers` requires transformer_engine to be installed.')
							for m in model.modules():
														if isinstance(a_							,					(te.LayerNorm, te.Linear)):
																					return True
							return False





