

"""simple docstring"""

import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
    import transformer_engine.pytorch as te



def 			SCREAMING_SNAKE_CASE       (   lowercase__				,				lowercase__=True				,				lowercase__=True				,				lowercase__=True      ) ->	int:
 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(lowercase__				,				nn.Linear      ) and to_transformer_engine and _convert_linear:
   # Return early if the linear layer weights are not multiples of 16
   if any(p % 1_6 != 0 for p in module.weight.shape      ):
    return
   lowerCAmelCase__      :							Optional[Any]    =							module.bias is not None
   lowerCAmelCase__      :							str    =							te.Linear(
       module.in_features				,				module.out_features				,				bias=lowercase__				,				params_dtype=module.weight.dtype      )
   lowerCAmelCase__      :							Tuple    =							module.weight.data.clone()
   if has_bias:
    lowerCAmelCase__      :							Tuple    =							module.bias.data.clone()

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

   setattr(lowercase__				,				lowercase__				,				lowercase__      )
  elif isinstance(lowercase__				,				te.Linear      ) and not to_transformer_engine and _convert_linear:
   lowerCAmelCase__      :							Any    =							module.bias is not None
   lowerCAmelCase__      :							List[Any]    =							nn.Linear(
       module.in_features				,				module.out_features				,				bias=lowercase__				,				params_dtype=module.weight.dtype      )
   lowerCAmelCase__      :							Optional[Any]    =							module.weight.data.clone()
   if has_bias:
    lowerCAmelCase__      :							int    =							module.bias.data.clone()

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

   setattr(lowercase__				,				lowercase__				,				lowercase__      )
  else:
   convert_model(
       lowercase__				,				to_transformer_engine=lowercase__				,				_convert_linear=_convert_linear				,				_convert_ln=_convert_ln				,				)






def 			SCREAMING_SNAKE_CASE       (   lowercase__      ) ->	Union[str, 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(lowercase__				,				(te.LayerNorm, te.Linear)      ):
   return True
 return False









"""simple docstring"""

def 			SCREAMING_SNAKE_CASE       (   lowercase__      ) ->	int:
 return 1 if input_a == 0 else 0



def 			SCREAMING_SNAKE_CASE       (   ) ->	None:
 assert not_gate(0      ) == 1
 assert not_gate(1      ) == 0


if __name__ == "__main__":
    print(not_gate(0))
    print(not_gate(1))






