


import copy

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


_SCREAMING_SNAKE_CASE    :				str       =					logging.get_logger(__name__)

class 						A__      (					snake_case__			):







    """simple docstring"""

    __magic_name__    						=	'speech-encoder-decoder'
    __magic_name__    						=	True
    def __init__( self    ,      **__snake_case					):
         super().__init__(**__snake_case					)
         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}'''					)

         snake_case					=       kwargs.pop('''encoder'''					)
         snake_case					=       encoder_config.pop('''model_type'''					)
         snake_case					=       kwargs.pop('''decoder'''					)
         snake_case					=       decoder_config.pop('''model_type'''					)

         snake_case					=       AutoConfig.for_model(__snake_case    ,      **__snake_case					)
         snake_case					=       AutoConfig.for_model(__snake_case    ,      **__snake_case					)
         snake_case					=       True
    @classmethod
    def 	a_    ( cls    ,      __snake_case    ,      __snake_case    ,      **__snake_case					):
         logger.info('''Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''					)
         snake_case					=       True
         snake_case					=       True

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




    def 	a_    ( self					):
         snake_case					=       copy.deepcopy(self.__dict__					)
         snake_case					=       self.encoder.to_dict()
         snake_case					=       self.decoder.to_dict()
         snake_case					=       self.__class__.model_type
         return output








import torch

from accelerate import Accelerator, DistributedType

class 						A__      :







    """simple docstring"""

    def __enter__( self					):
         if self.enabled:
              snake_case					=       self.model.no_sync()
              self.model_sync_obj.__enter__()

         return self
    def __exit__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
         if self.enabled:
              # Average all models on exit
              self._sync_and_avg_model_params()
              self.model_sync_obj.__exit__(__snake_case    ,      __snake_case    ,      __snake_case					)
    def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      __snake_case = True					):
         if accelerator.distributed_type not in [
             DistributedType.NO,
             DistributedType.MULTI_CPU,
             DistributedType.MULTI_GPU,
         ]:
              raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'''					)
         snake_case					=       enabled and accelerator.distributed_type != DistributedType.NO
         snake_case					=       0
         if self.enabled:
              snake_case					=       accelerator
              snake_case					=       model
              snake_case					=       local_sgd_steps
    def 	a_    ( self					):
         self.num_steps += 1
         if not self.enabled:
              return

         if self.num_steps % self.local_sgd_steps == 0:
              self._sync_and_avg_model_params()




    def 	a_    ( self					):
         self.accelerator.wait_for_everyone()
         with self.accelerator.autocast():
              for param in self.model.parameters():
                   snake_case					=       self.accelerator.reduce(param.data    ,      reduction='''mean'''					)




