

import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
     from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
     import torch_xla.core.xla_model as xm

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Union[str, Any]      ):



     '''simple docstring'''





     if is_torch_version('<'     ,					'2.0.0'      ) or not hasattr(SCREAMING_SNAKE_CASE_     ,					'_dynamo'      ):
          return False
     return isinstance(SCREAMING_SNAKE_CASE_     ,					torch._dynamo.eval_frame.OptimizedModule      )

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   List[str]     ,					SCREAMING_SNAKE_CASE_		:   bool = True      ):



     '''simple docstring'''





     lowercase__   :       Dict				=	(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

     lowercase__   :       List[str]				=	is_compiled_module(SCREAMING_SNAKE_CASE_      )
     if is_compiled:
          lowercase__   :       Optional[Any]				=	model
          lowercase__   :       int				=	model._orig_mod

     if is_deepspeed_available():
          options += (DeepSpeedEngine,)

     while isinstance(SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_      ):
          lowercase__   :       Union[str, Any]				=	model.module

     if not keep_fpaa_wrapper:
          lowercase__   :       Dict				=	getattr(SCREAMING_SNAKE_CASE_     ,					'forward'      )
          lowercase__   :       str				=	model.__dict__.pop('_original_forward'     ,					SCREAMING_SNAKE_CASE_      )
          if original_forward is not None:
               while hasattr(SCREAMING_SNAKE_CASE_     ,					'__wrapped__'      ):
                    lowercase__   :       Any				=	forward.__wrapped__
                    if forward == original_forward:
                         break
               lowercase__   :       int				=	forward
          if getattr(SCREAMING_SNAKE_CASE_     ,					'_converted_to_transformer_engine'     ,					SCREAMING_SNAKE_CASE_      ):
               convert_model(SCREAMING_SNAKE_CASE_     ,					to_transformer_engine=SCREAMING_SNAKE_CASE_      )

     if is_compiled:
          lowercase__   :       Optional[int]				=	model
          lowercase__   :       Optional[Any]				=	compiled_model

     return model

def      snake_case__      (   ):



     '''simple docstring'''





     PartialState().wait_for_everyone()

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Dict     ,					SCREAMING_SNAKE_CASE_		:   List[str]      ):



     '''simple docstring'''





     if PartialState().distributed_type == DistributedType.TPU:
          xm.save(SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_      )
     elif PartialState().local_process_index == 0:
          torch.save(SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_      )

@contextmanager
def      snake_case__      (   **SCREAMING_SNAKE_CASE_		:   Tuple      ):



     '''simple docstring'''





     for key, value in kwargs.items():
          lowercase__   :       Tuple				=	str(SCREAMING_SNAKE_CASE_      )

     yield

     for key in kwargs:
          if key.upper() in os.environ:
               del os.environ[key.upper()]

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Tuple      ):



     '''simple docstring'''





     if not hasattr(SCREAMING_SNAKE_CASE_     ,					'__qualname__'      ) and not hasattr(SCREAMING_SNAKE_CASE_     ,					'__name__'      ):
          lowercase__   :       Optional[int]				=	getattr(SCREAMING_SNAKE_CASE_     ,					'__class__'     ,					SCREAMING_SNAKE_CASE_      )
     if hasattr(SCREAMING_SNAKE_CASE_     ,					'__qualname__'      ):
          return obj.__qualname__
     if hasattr(SCREAMING_SNAKE_CASE_     ,					'__name__'      ):
          return obj.__name__
     return str(SCREAMING_SNAKE_CASE_      )

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   Tuple     ,					SCREAMING_SNAKE_CASE_		:   Union[str, Any]      ):



     '''simple docstring'''





     for key, value in source.items():
          if isinstance(SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_      ):
               lowercase__   :       List[str]				=	destination.setdefault(SCREAMING_SNAKE_CASE_     ,					{}      )
               merge_dicts(SCREAMING_SNAKE_CASE_     ,					SCREAMING_SNAKE_CASE_      )
          else:
               lowercase__   :       Tuple				=	value

     return destination

def      snake_case__      (   SCREAMING_SNAKE_CASE_		:   int = None      ):



     '''simple docstring'''





     if port is None:
          lowercase__   :       int				=	29_500
     with socket.socket(socket.AF_INET     ,					socket.SOCK_STREAM      ) as s:
          return s.connect_ex(('localhost', port)      ) == 0




from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case_        =			logging.get_logger(__name__)

snake_case_        =			{
    '''facebook/s2t-wav2vec2-large-en-de''': (
        '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json'''
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}


class 				SCREAMING_SNAKE_CASE__ (__snake_case					):
    __lowerCamelCase						:	List[Any]														=					"""speech_to_text_2"""
    __lowerCamelCase						:	str														=					["""past_key_values"""]
    __lowerCamelCase						:	List[Any]														=					{"""num_attention_heads""": """decoder_attention_heads""", """hidden_size""": """d_model"""}





    def __init__(				self						,       a=1_0000						,       a=6						,       a=2048						,       a=4						,       a=0.0						,       a=True						,       a="relu"						,       a=256						,       a=0.1						,       a=0.0						,       a=0.0						,       a=0.02						,       a=2						,       a=True						,       a=1						,       a=0						,       a=2						,       a=1024						,       **a						,       ):
         lowercase__   :       Optional[int]				=	vocab_size
         lowercase__   :       List[str]				=	d_model
         lowercase__   :       int				=	decoder_ffn_dim
         lowercase__   :       Optional[Any]				=	decoder_layers
         lowercase__   :       int				=	decoder_attention_heads
         lowercase__   :       Dict				=	dropout
         lowercase__   :       Optional[int]				=	attention_dropout
         lowercase__   :       Optional[int]				=	activation_dropout
         lowercase__   :       Optional[int]				=	activation_function
         lowercase__   :       Dict				=	init_std
         lowercase__   :       List[Any]				=	decoder_layerdrop
         lowercase__   :       int				=	use_cache
         lowercase__   :       Any				=	decoder_layers
         lowercase__   :       Optional[int]				=	scale_embedding  # scale factor will be sqrt(d_model) if True
         lowercase__   :       Tuple				=	max_target_positions

         super().__init__(
             pad_token_id=a						,       bos_token_id=a						,       eos_token_id=a						,       decoder_start_token_id=a						,       **a						,       )

