
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     _a					(						lowerCamelCase							):
 if is_torch_version("""<""",	"""2.0.0"""							) or not hasattr(lowerCamelCase,	"""_dynamo"""							):
  return False
 return isinstance(lowerCamelCase,	torch._dynamo.eval_frame.OptimizedModule							)





def     _a					(						lowerCamelCase,	lowerCamelCase = True							):
 lowerCamelCase		:	Optional[int]							=      (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

 lowerCamelCase		:	Optional[int]							=      is_compiled_module(lowerCamelCase							)
 if is_compiled:
  lowerCamelCase		:	Union[str, Any]							=      model
  lowerCamelCase		:	Tuple							=      model._orig_mod

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

 while isinstance(lowerCamelCase,	lowerCamelCase							):
  lowerCamelCase		:	str							=      model.module

 if not keep_fpaa_wrapper:
  lowerCamelCase		:	int							=      getattr(lowerCamelCase,	"""forward"""							)
  lowerCamelCase		:	Optional[Any]							=      model.__dict__.pop("""_original_forward""",	lowerCamelCase							)
  if original_forward is not None:
   while hasattr(lowerCamelCase,	"""__wrapped__"""							):
    lowerCamelCase		:	Any							=      forward.__wrapped__
    if forward == original_forward:
     break
   lowerCamelCase		:	Optional[Any]							=      forward
  if getattr(lowerCamelCase,	"""_converted_to_transformer_engine""",	lowerCamelCase							):
   convert_model(lowerCamelCase,	to_transformer_engine=lowerCamelCase							)

 if is_compiled:
  lowerCamelCase		:	Any							=      model
  lowerCamelCase		:	List[Any]							=      compiled_model

 return model





def     _a					(						):
 PartialState().wait_for_everyone()





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





@contextmanager
def     _a					(						**lowerCamelCase							):
 for key, value in kwargs.items():
  lowerCamelCase		:	Optional[Any]							=      str(lowerCamelCase							)

 yield

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





def     _a					(						lowerCamelCase							):
 if not hasattr(lowerCamelCase,	"""__qualname__"""							) and not hasattr(lowerCamelCase,	"""__name__"""							):
  lowerCamelCase		:	List[str]							=      getattr(lowerCamelCase,	"""__class__""",	lowerCamelCase							)
 if hasattr(lowerCamelCase,	"""__qualname__"""							):
  return obj.__qualname__
 if hasattr(lowerCamelCase,	"""__name__"""							):
  return obj.__name__
 return str(lowerCamelCase							)





def     _a					(						lowerCamelCase,	lowerCamelCase							):
 for key, value in source.items():
  if isinstance(lowerCamelCase,	lowerCamelCase							):
   lowerCamelCase		:	int							=      destination.setdefault(lowerCamelCase,	{}							)
   merge_dicts(lowerCamelCase,	lowerCamelCase							)
  else:
   lowerCamelCase		:	Any							=      value

 return destination





def     _a					(						lowerCamelCase = None							):
 if port is None:
  lowerCamelCase		:	List[Any]							=      2_9500
 with socket.socket(socket.AF_INET,	socket.SOCK_STREAM							) as s:
  return s.connect_ex(("""localhost""", port)							) == 0






import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Audio, Features, Value
from .base import TaskTemplate





@dataclass(frozen=__SCREAMING_SNAKE_CASE)
class 							A__		(	__SCREAMING_SNAKE_CASE):
      _UpperCAmelCase				:    str           =			field(default="""automatic-speech-recognition"""  ,   metadata={"""include_in_asdict_even_if_is_default""": True})
      _UpperCAmelCase				:    ClassVar[Features]           =			Features({"""audio""": Audio()})
      _UpperCAmelCase				:    ClassVar[Features]           =			Features({"""transcription""": Value("""string""")})
      _UpperCAmelCase				:    str           =			"audio"
      _UpperCAmelCase				:    str           =			"transcription"
      def 						UpperCamelCase__							(      self    ,  __magic_name__      ):
       if self.audio_column not in features:
        raise ValueError(F'''Column {self.audio_column} is not present in features.'''      )
       if not isinstance(features[self.audio_column]    ,  __magic_name__      ):
        raise ValueError(F'''Column {self.audio_column} is not an Audio type.'''      )
       lowerCamelCase		:	Optional[Any]							=      copy.deepcopy(self      )
       lowerCamelCase		:	List[Any]							=      self.input_schema.copy()
       lowerCamelCase		:	Tuple							=      features[self.audio_column]
       lowerCamelCase		:	int							=      input_schema
       return task_template






      @property
      def 						UpperCamelCase__							(      self      ):
       return {self.audio_column: "audio", self.transcription_column: "transcription"}




