




'''simple docstring'''




import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
							import transformer_engine.pytorch as te




def 						snake_case__    ( lowerCamelCase__				:   Union[str, Any]			,		lowerCamelCase__				:   int=True			,		lowerCamelCase__				:   int=True			,		lowerCamelCase__				:   Tuple=True			)   ->   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(lowerCamelCase__			,		nn.Linear			) and to_transformer_engine and _convert_linear:
															# Return early if the linear layer weights are not multiples of 16
															if any(p % 1_6 != 0 for p in module.weight.shape			):
																				return
															A_ :     Optional[Any] =							module.bias is not None
															A_ :     str =							te.Linear(
															    module.in_features			,		module.out_features			,		bias=lowerCamelCase__			,		params_dtype=module.weight.dtype			)
															A_ :     Union[str, Any] =							module.weight.data.clone()
															if has_bias:
																				A_ :     Optional[int] =							module.bias.data.clone()

															setattr(lowerCamelCase__			,		lowerCamelCase__			,		lowerCamelCase__			)
										elif isinstance(lowerCamelCase__			,		nn.LayerNorm			) and to_transformer_engine and _convert_ln:
															A_ :     Union[str, Any] =							te.LayerNorm(module.normalized_shape[0]			,		eps=module.eps			,		params_dtype=module.weight.dtype			)
															A_ :     List[str] =							module.weight.data.clone()
															A_ :     str =							module.bias.data.clone()

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

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

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





def 						snake_case__    ( lowerCamelCase__				:   str			)   ->   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(lowerCamelCase__			,		(te.LayerNorm, te.Linear)			):
															return True
					return False









'''simple docstring'''




def 						snake_case__    ( lowerCamelCase__				:   float			)   ->   float:
					return -num if num < 0 else num




def 						snake_case__    ( lowerCamelCase__				:   list[int]			)   ->   int:
					if len(lowerCamelCase__			) == 0:
										raise ValueError('''abs_min() arg is an empty sequence'''			)
					A_ :     Any =							x[0]
					for i in x:
										if abs_val(lowerCamelCase__			) < abs_val(lowerCamelCase__			):
															A_ :     Tuple =							i
					return j




def 						snake_case__    ( lowerCamelCase__				:   list[int]			)   ->   int:
					if len(lowerCamelCase__			) == 0:
										raise ValueError('''abs_max() arg is an empty sequence'''			)
					A_ :     List[str] =							x[0]
					for i in x:
										if abs(lowerCamelCase__			) > abs(lowerCamelCase__			):
															A_ :     Any =							i
					return j




def 						snake_case__    ( lowerCamelCase__				:   list[int]			)   ->   int:
					if len(lowerCamelCase__			) == 0:
										raise ValueError('''abs_max_sort() arg is an empty sequence'''			)
					return sorted(lowerCamelCase__			,		key=lowerCamelCase__			)[-1]




def 						snake_case__    ( )   ->   Optional[Any]:
					assert abs_val(0			) == 0
					assert abs_val(3_4			) == 3_4
					assert abs_val(-1_0_0_0_0_0_0_0_0_0_0_0			) == 1_0_0_0_0_0_0_0_0_0_0_0

					A_ :     Dict =							[-3, -1, 2, -1_1]
					assert abs_max(lowerCamelCase__			) == -1_1
					assert abs_max_sort(lowerCamelCase__			) == -1_1
					assert abs_min(lowerCamelCase__			) == -1


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							test_abs_val()
							print(abs_val(-34))  # --> 34



