



'''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_      :	int					, UpperCAmelCase_      :	Union[str, Any]=True					, UpperCAmelCase_      :	Any=True					, UpperCAmelCase_      :	Tuple=True						) ->							Optional[Any]:
							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
																					lowerCAmelCase__  =   module.bias is not None
																					lowerCAmelCase__  =   te.Linear(
																					    module.in_features					, module.out_features					, bias=UpperCAmelCase_					, params_dtype=module.weight.dtype						)
																					lowerCAmelCase__  =   module.weight.data.clone()
																					if has_bias:
																												lowerCAmelCase__  =   module.bias.data.clone()

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

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

																					setattr(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)
														elif isinstance(UpperCAmelCase_					, te.LayerNorm						) and not to_transformer_engine and _convert_ln:
																					lowerCAmelCase__  =   nn.LayerNorm(module.normalized_shape[0]					, eps=module.eps					, params_dtype=module.weight.dtype						)
																					lowerCAmelCase__  =   module.weight.data.clone()
																					lowerCAmelCase__  =   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_      :	Tuple						) ->							Any:
							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'''







def 			_lowerCAmelCase(      UpperCAmelCase_      :	str						) ->							str:
							lowerCAmelCase__  =   word.upper()
							lowerCAmelCase__  =   0
							lowerCAmelCase__  =   {}
							lowerCAmelCase__  =   []

							for letter in word:
														if letter not in letter_nums:
																					lowerCAmelCase__  =   str(UpperCAmelCase_						)
																					next_num += 1
														word_pattern.append(letter_nums[letter]						)
							return ".".join(UpperCAmelCase_						)


if __name__ == "__main__":
		import pprint
		import time

		_UpperCamelCase  						=			time.time()
		with open("""dictionary.txt""") as in_file:
				_UpperCamelCase  						=			in_file.read().splitlines()

		_UpperCamelCase  						=			{}
		for word in word_list:
				_UpperCamelCase  						=			get_word_pattern(word)
				if pattern in all_patterns:
						all_patterns[pattern].append(word)
				else:
						_UpperCamelCase  						=			[word]

		with open("""word_patterns.txt""", """w""") as out_file:
				out_file.write(pprint.pformat(all_patterns))

		_UpperCamelCase  						=			round(time.time() - start_time, 2)
		print(f'Done!  {len(all_patterns):,} word patterns found in {total_time} seconds.')
		# Done!  9,581 word patterns found in 0.58 seconds.





