




import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
  import transformer_engine.pytorch as te



def     lowerCAmelCase		(		_lowerCAmelCase				: Tuple    ,				_lowerCAmelCase				: Optional[int]=True    ,				_lowerCAmelCase				: List[str]=True    ,				_lowerCAmelCase				: Optional[int]=True						):




 """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
   UpperCAmelCase__							=    module.bias is not None
   UpperCAmelCase__							=    te.Linear(
       module.in_features    ,				module.out_features    ,				bias=SCREAMING_SNAKE_CASE_    ,				params_dtype=module.weight.dtype						)
   UpperCAmelCase__							=    module.weight.data.clone()
   if has_bias:
    UpperCAmelCase__							=    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:
   UpperCAmelCase__							=    te.LayerNorm(module.normalized_shape[0]    ,				eps=module.eps    ,				params_dtype=module.weight.dtype						)
   UpperCAmelCase__							=    module.weight.data.clone()
   UpperCAmelCase__							=    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:
   UpperCAmelCase__							=    module.bias is not None
   UpperCAmelCase__							=    nn.Linear(
       module.in_features    ,				module.out_features    ,				bias=SCREAMING_SNAKE_CASE_    ,				params_dtype=module.weight.dtype						)
   UpperCAmelCase__							=    module.weight.data.clone()
   if has_bias:
    UpperCAmelCase__							=    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:
   UpperCAmelCase__							=    nn.LayerNorm(module.normalized_shape[0]    ,				eps=module.eps    ,				params_dtype=module.weight.dtype						)
   UpperCAmelCase__							=    module.weight.data.clone()
   UpperCAmelCase__							=    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     lowerCAmelCase		(		_lowerCAmelCase				: int						):




 """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












import pytest

from datasets.utils.version import Version


@pytest.mark.parametrize(
    '''other, expected_equality'''				,							[
        (Version('''1.0.0'''		), True),
        ('''1.0.0''', True),
        (Version('''2.0.0'''		), False),
        ('''2.0.0''', False),
        ('''1''', False),
        ('''a''', False),
        (1, False),
        (None, False),
    ]				,							)
def        A__						(							SCREAMING_SNAKE_CASE_				,							SCREAMING_SNAKE_CASE_		) ->  Optional[int]:
		lowerCamelCase :			str    		=Version('''1.0.0'''		)
		assert (version == other) is expected_equality
		assert (version != other) is not expected_equality
		assert (hash(SCREAMING_SNAKE_CASE_		) == hash(SCREAMING_SNAKE_CASE_		)) is expected_equality

