'''simple docstring'''




import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
						import transformer_engine.pytorch as te




def 				lowerCamelCase      ( UpperCAmelCase__			:   Optional[Any] , UpperCAmelCase__			:   Union[str, Any]=True , UpperCAmelCase__			:   Union[str, Any]=True , UpperCAmelCase__			:   Optional[Any]=True					)     -> List[str]:
							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(UpperCAmelCase__ , 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
																					lowercase_   :     List[str]									=			module.bias is not None
																					lowercase_   :     Union[str, Any]									=			te.Linear(
																					    module.in_features , module.out_features , bias=UpperCAmelCase__ , params_dtype=module.weight.dtype					)
																					lowercase_   :     str									=			module.weight.data.clone()
																					if has_bias:
																												lowercase_   :     Optional[Any]									=			module.bias.data.clone()

																					setattr(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__					)
														elif isinstance(UpperCAmelCase__ , nn.LayerNorm					) and to_transformer_engine and _convert_ln:
																					lowercase_   :     Optional[int]									=			te.LayerNorm(module.normalized_shape[0] , eps=module.eps , params_dtype=module.weight.dtype					)
																					lowercase_   :     str									=			module.weight.data.clone()
																					lowercase_   :     int									=			module.bias.data.clone()

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

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

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




def 				lowerCamelCase      ( UpperCAmelCase__			:   Optional[Any]					)     -> Dict:
							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(UpperCAmelCase__ , (te.LayerNorm, te.Linear)					):
																					return True
							return False






'''simple docstring'''




from transformers import HfArgumentParser, PyTorchBenchmark, PyTorchBenchmarkArguments




def 				lowerCamelCase      ( )     -> List[Any]:
							lowercase_   :     Tuple									=			HfArgumentParser(UpperCAmelCase__					)
							try:
														lowercase_   :     List[Any]									=			parser.parse_args_into_dataclasses()[0]
							except ValueError as e:
														lowercase_   :     int									=			"""Arg --no_{0} is no longer used, please use --no-{0} instead."""
														lowercase_   :     Any									=			""" """.join(str(UpperCAmelCase__					).split(""" """					)[:-1]					)
														lowercase_   :     Optional[Any]									=			""""""
														lowercase_   :     List[str]									=			eval(str(UpperCAmelCase__					).split(""" """					)[-1]					)
														lowercase_   :     Dict									=			[]
														for arg in depreciated_args:
																					# arg[2:] removes '--'
																					if arg[2:] in PyTorchBenchmarkArguments.deprecated_args:
																												# arg[5:] removes '--no_'
																												full_error_msg += arg_error_msg.format(arg[5:]					)
																					else:
																												wrong_args.append(UpperCAmelCase__					)
														if len(UpperCAmelCase__					) > 0:
																					lowercase_   :     Dict									=			full_error_msg + begin_error_msg + str(UpperCAmelCase__					)
														raise ValueError(UpperCAmelCase__					)

							lowercase_   :     Optional[int]									=			PyTorchBenchmark(args=UpperCAmelCase__					)
							benchmark.run()


if __name__ == "__main__":
						main()





