import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
					import transformer_engine.pytorch as te


def   __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__=True  ,		SCREAMING_SNAKE_CASE__=True  ,		SCREAMING_SNAKE_CASE__=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(SCREAMING_SNAKE_CASE__  ,		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=SCREAMING_SNAKE_CASE__  ,		params_dtype=module.weight.dtype	)
																		snake_case_       =     module.weight.data.clone()
																		if has_bias:
																								snake_case_       =     module.bias.data.clone()

																		setattr(SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__	)
												elif isinstance(SCREAMING_SNAKE_CASE__  ,		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(SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__	)
												elif isinstance(SCREAMING_SNAKE_CASE__  ,		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=SCREAMING_SNAKE_CASE__  ,		params_dtype=module.weight.dtype	)
																		snake_case_       =     module.weight.data.clone()
																		if has_bias:
																								snake_case_       =     module.bias.data.clone()

																		setattr(SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__	)
												elif isinstance(SCREAMING_SNAKE_CASE__  ,		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(SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__  ,		SCREAMING_SNAKE_CASE__	)
												else:
																		convert_model(
																		    SCREAMING_SNAKE_CASE__  ,		to_transformer_engine=SCREAMING_SNAKE_CASE__  ,		_convert_linear=_convert_linear  ,		_convert_ln=_convert_ln  ,		)


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


def   __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__	):
						snake_case_       =     5381
						for x in s:
												snake_case_       =     ((hash_value << 5) + hash_value) + ord(SCREAMING_SNAKE_CASE__	)
						return hash_value & 0xFF_FF_FF_FF