




'''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 (_lowerCamelCase							:     Union[str, Any]   ,	_lowerCamelCase							:     Tuple=True   ,	_lowerCamelCase							:     int=True   ,	_lowerCamelCase							:     List[Any]=True	)->   List[Any]:




     '''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(_lowerCamelCase   ,	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    					=  module.bias is not None
               __snake_case    					=  te.Linear(
                   module.in_features   ,	module.out_features   ,	bias=_lowerCamelCase   ,	params_dtype=module.weight.dtype	)
               __snake_case    					=  module.weight.data.clone()
               if has_bias:
                    __snake_case    					=  module.bias.data.clone()

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

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

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

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







def 			_UpperCamelCase (_lowerCamelCase							:     List[Any]	)->   Dict:




     '''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(_lowerCamelCase   ,	(te.LayerNorm, te.Linear)	):
               return True
     return False












'''simple docstring'''



from transformers import ProcessorMixin





class     lowerCAmelCase   ( __lowerCAmelCase):
       __lowercase  :      Union[str, Any]        =			'''AutoFeatureExtractor'''
       __lowercase  :      List[Any]        =			'''AutoTokenizer'''






