






"""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_(			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
            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_(			SCREAMING_SNAKE_CASE       )       ->						Tuple:
    """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"""





def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE       )       ->						int:
    """simple docstring"""

    if not isinstance(SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE       ):
        raise ValueError('double_factorial() only accepts integral values'       )
    if num < 0:
        raise ValueError('double_factorial() not defined for negative values'       )
    UpperCamelCase__								=     1
    for i in range(SCREAMING_SNAKE_CASE			,						0			,						-2       ):
        value *= i
    return value


if __name__ == "__main__":
  import doctest

  doctest.testmod()




