
'''simple docstring'''
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__      ,    SCREAMING_SNAKE_CASE__=True      ,    SCREAMING_SNAKE_CASE__=True      ,    SCREAMING_SNAKE_CASE__=True       )   ->				Optional[int]:


		'''simple docstring'''


		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   :    List[str]			       =		module.bias is not None
						snake_case   :    Union[str, Any]			       =		te.Linear(
						    module.in_features      ,    module.out_features      ,    bias=SCREAMING_SNAKE_CASE__      ,    params_dtype=module.weight.dtype       )
						snake_case   :    Union[str, Any]			       =		module.weight.data.clone()
						if has_bias:
								snake_case   :    Tuple			       =		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   :    List[Any]			       =		te.LayerNorm(module.normalized_shape[0]      ,    eps=module.eps      ,    params_dtype=module.weight.dtype       )
						snake_case   :    Optional[Any]			       =		module.weight.data.clone()
						snake_case   :    Any			       =		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   :    Optional[Any]			       =		module.bias is not None
						snake_case   :    Optional[Any]			       =		nn.Linear(
						    module.in_features      ,    module.out_features      ,    bias=SCREAMING_SNAKE_CASE__      ,    params_dtype=module.weight.dtype       )
						snake_case   :    Optional[int]			       =		module.weight.data.clone()
						if has_bias:
								snake_case   :    Dict			       =		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   :    List[Any]			       =		nn.LayerNorm(module.normalized_shape[0]      ,    eps=module.eps      ,    params_dtype=module.weight.dtype       )
						snake_case   :    str			       =		module.weight.data.clone()
						snake_case   :    Any			       =		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__       )   ->				List[Any]:


		'''simple docstring'''


		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




'''simple docstring'''
from __future__ import annotations

import sys




def 					_UpperCamelCase		(    SCREAMING_SNAKE_CASE__      ,    SCREAMING_SNAKE_CASE__       )   ->				tuple[int, int]:


		'''simple docstring'''


		if abs(SCREAMING_SNAKE_CASE__       ) == 1:
				return a, 0
		elif abs(SCREAMING_SNAKE_CASE__       ) == 1:
				return 0, b

		snake_case       ,snake_case   :    Tuple			       =		a, b
		snake_case       ,snake_case   :    List[str]			       =		1, 0
		snake_case       ,snake_case   :    Optional[Any]			       =		0, 1

		while remainder != 0:
				snake_case   :    int			       =		old_remainder // remainder
				snake_case       ,snake_case   :    Tuple			       =		remainder, old_remainder - quotient * remainder
				snake_case       ,snake_case   :    Optional[Any]			       =		coeff_a, old_coeff_a - quotient * coeff_a
				snake_case       ,snake_case   :    Dict			       =		coeff_b, old_coeff_b - quotient * coeff_b

		# sign correction for negative numbers
		if a < 0:
				snake_case   :    Optional[Any]			       =		-old_coeff_a
		if b < 0:
				snake_case   :    int			       =		-old_coeff_b

		return old_coeff_a, old_coeff_b




def 					_UpperCamelCase		(    )   ->				str:


		'''simple docstring'''


		if len(sys.argv       ) < 3:
				print('''2 integer arguments required'''       )
				return 1
		snake_case   :    str			       =		int(sys.argv[1]       )
		snake_case   :    List[str]			       =		int(sys.argv[2]       )
		print(extended_euclidean_algorithm(SCREAMING_SNAKE_CASE__      ,    SCREAMING_SNAKE_CASE__       )       )
		return 0


if __name__ == "__main__":
						raise SystemExit(main())


