
import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
		import transformer_engine.pytorch as te




def   UpperCamelCase__     (    SCREAMING_SNAKE_CASE_  :  Union[str, Any]		,      SCREAMING_SNAKE_CASE_  :  Optional[int]=True		,      SCREAMING_SNAKE_CASE_  :  List[str]=True		,      SCREAMING_SNAKE_CASE_  :  int=True					)     ->    Dict:
						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
																		_lowercase									=				module.bias is not None
																		_lowercase									=				te.Linear(
																		    module.in_features		,      module.out_features		,      bias=SCREAMING_SNAKE_CASE_		,      params_dtype=module.weight.dtype					)
																		_lowercase									=				module.weight.data.clone()
																		if has_bias:
																								_lowercase									=				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:
																		_lowercase									=				te.LayerNorm(module.normalized_shape[0]		,      eps=module.eps		,      params_dtype=module.weight.dtype					)
																		_lowercase									=				module.weight.data.clone()
																		_lowercase									=				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:
																		_lowercase									=				module.bias is not None
																		_lowercase									=				nn.Linear(
																		    module.in_features		,      module.out_features		,      bias=SCREAMING_SNAKE_CASE_		,      params_dtype=module.weight.dtype					)
																		_lowercase									=				module.weight.data.clone()
																		if has_bias:
																								_lowercase									=				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:
																		_lowercase									=				nn.LayerNorm(module.normalized_shape[0]		,      eps=module.eps		,      params_dtype=module.weight.dtype					)
																		_lowercase									=				module.weight.data.clone()
																		_lowercase									=				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   UpperCamelCase__     (    SCREAMING_SNAKE_CASE_  :  str					)     ->    Optional[int]:
						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   UpperCamelCase__     (    SCREAMING_SNAKE_CASE_  :  int					)     ->    int:
						return n * (2 * n - 1)




def   UpperCamelCase__     (    SCREAMING_SNAKE_CASE_  :  int					)     ->    bool:
						_lowercase									=				(1 + 24 * n) ** 0.5
						return ((1 + root) / 6) % 1 == 0




def   UpperCamelCase__     (    SCREAMING_SNAKE_CASE_  :  int = 1_44					)     ->    int:
						_lowercase									=				start
						_lowercase									=				hexagonal_num(SCREAMING_SNAKE_CASE_					)
						while not is_pentagonal(SCREAMING_SNAKE_CASE_					):
												n += 1
												_lowercase									=				hexagonal_num(SCREAMING_SNAKE_CASE_					)
						return num


if __name__ == "__main__":
		print(F'{solution()} = ')