'''simple docstring'''





import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
   import transformer_engine.pytorch as te





def      __lowercase  (		lowerCamelCase      :  Any						,						lowerCamelCase      :  Optional[int]=True						,						lowerCamelCase      :  Union[str, Any]=True						,						lowerCamelCase      :  Any=True			):
 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
   UpperCamelCase_      :		Optional[Any]				=      module.bias is not None
   UpperCamelCase_      :		Union[str, Any]				=      te.Linear(
       module.in_features						,						module.out_features						,						bias=lowerCamelCase						,						params_dtype=module.weight.dtype			)
   UpperCamelCase_      :		Tuple				=      module.weight.data.clone()
   if has_bias:
    UpperCamelCase_      :		Union[str, Any]				=      module.bias.data.clone()

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

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

   setattr(lowerCamelCase						,						lowerCamelCase						,						lowerCamelCase			)
  elif isinstance(lowerCamelCase						,						te.LayerNorm			) and not to_transformer_engine and _convert_ln:
   UpperCamelCase_      :		Dict				=      nn.LayerNorm(module.normalized_shape[0]						,						eps=module.eps						,						params_dtype=module.weight.dtype			)
   UpperCamelCase_      :		Dict				=      module.weight.data.clone()
   UpperCamelCase_      :		List[str]				=      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      __lowercase  (		lowerCamelCase      :  Optional[int]			):
 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




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


a_					      = {
    'configuration_marian': ['MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MarianConfig', 'MarianOnnxConfig'],
}

try:
   if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   a_					      = ['MarianTokenizer']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   a_					      = [
       'MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST',
       'MarianForCausalLM',
       'MarianModel',
       'MarianMTModel',
       'MarianPreTrainedModel',
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   a_					      = ['TFMarianModel', 'TFMarianMTModel', 'TFMarianPreTrainedModel']

try:
   if not is_flax_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   a_					      = ['FlaxMarianModel', 'FlaxMarianMTModel', 'FlaxMarianPreTrainedModel']

if TYPE_CHECKING:
   from .configuration_marian import MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP, MarianConfig, MarianOnnxConfig

   try:
      if not is_sentencepiece_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_marian import MarianTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_marian import (
          MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST,
          MarianForCausalLM,
          MarianModel,
          MarianMTModel,
          MarianPreTrainedModel,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_marian import TFMarianModel, TFMarianMTModel, TFMarianPreTrainedModel

   try:
      if not is_flax_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_flax_marian import FlaxMarianModel, FlaxMarianMTModel, FlaxMarianPreTrainedModel

else:
   import sys

   a_					      = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



