






"""simple docstring"""
import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


A_     :					Any							=     logging.get_logger(__name__)



class 						a_ (      snake_case_					):







      '''simple docstring'''

      lowerCamelCase__			:							str					=      'speech-encoder-decoder'
      lowerCamelCase__			:							str					=      True




      def __init__(self,	**lowerCamelCase_   ):

       '''simple docstring'''



       super().__init__(**lowerCamelCase_   )
       if "encoder" not in kwargs or "decoder" not in kwargs:
        raise ValueError(
            f'''A configuraton of type {self.model_type} cannot be instantiated because not both `encoder` and'''
            f''' `decoder` sub-configurations are passed, but only {kwargs}'''   )

       lowerCamelCase__	:     Tuple								=		kwargs.pop('encoder'   )
       lowerCamelCase__	:     str								=		encoder_config.pop('model_type'   )
       lowerCamelCase__	:     List[Any]								=		kwargs.pop('decoder'   )
       lowerCamelCase__	:     Any								=		decoder_config.pop('model_type'   )

       lowerCamelCase__	:     Optional[int]								=		AutoConfig.for_model(lowerCamelCase_,	**lowerCamelCase_   )
       lowerCamelCase__	:     List[str]								=		AutoConfig.for_model(lowerCamelCase_,	**lowerCamelCase_   )
       lowerCamelCase__	:     Optional[int]								=		True




      @classmethod
      def      a__				(cls,	lowerCamelCase_,	lowerCamelCase_,	**lowerCamelCase_   ):

       '''simple docstring'''



       logger.info('Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'   )
       lowerCamelCase__	:     List[Any]								=		True
       lowerCamelCase__	:     str								=		True

       return cls(encoder=encoder_config.to_dict(),	decoder=decoder_config.to_dict(),	**lowerCamelCase_   )





      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		copy.deepcopy(self.__dict__   )
       lowerCamelCase__	:     int								=		self.encoder.to_dict()
       lowerCamelCase__	:     List[Any]								=		self.decoder.to_dict()
       lowerCamelCase__	:     Optional[int]								=		self.__class__.model_type
       return output









"""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_    (		_lowerCamelCase     ,  _lowerCamelCase=True     ,  _lowerCamelCase=True     ,  _lowerCamelCase=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
   lowerCamelCase__	:     List[Any]								=		module.bias is not None
   lowerCamelCase__	:     Tuple								=		te.Linear(
       module.in_features     ,  module.out_features     ,  bias=_lowerCamelCase     ,  params_dtype=module.weight.dtype							)
   lowerCamelCase__	:     Optional[Any]								=		module.weight.data.clone()
   if has_bias:
    lowerCamelCase__	:     List[str]								=		module.bias.data.clone()

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

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

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

