


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 				__lowerCamelCase						( UpperCAmelCase_						:		Optional[Any]						):




      """simple docstring"""





      if is_torch_version('''<''' ,  '''2.0.0'''						) or not hasattr(UpperCAmelCase_ ,  '''_dynamo'''						):
            return False
      return isinstance(UpperCAmelCase_ ,  torch._dynamo.eval_frame.OptimizedModule						)
def 				__lowerCamelCase						( UpperCAmelCase_						:		Dict ,  UpperCAmelCase_						:		bool = True						):




      """simple docstring"""





      a						:List[Any] 							=       (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

      a						:List[str] 							=       is_compiled_module(UpperCAmelCase_						)
      if is_compiled:
            a						:Tuple 							=       model
            a						:Optional[int] 							=       model._orig_mod

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

      while isinstance(UpperCAmelCase_ ,  UpperCAmelCase_						):
            a						:Any 							=       model.module

      if not keep_fpaa_wrapper:
            a						:Union[str, Any] 							=       getattr(UpperCAmelCase_ ,  '''forward'''						)
            a						:str 							=       model.__dict__.pop('''_original_forward''' ,  UpperCAmelCase_						)
            if original_forward is not None:
                  while hasattr(UpperCAmelCase_ ,  '''__wrapped__'''						):
                        a						:Tuple 							=       forward.__wrapped__
                        if forward == original_forward:
                              break
                  a						:Union[str, Any] 							=       forward
            if getattr(UpperCAmelCase_ ,  '''_converted_to_transformer_engine''' ,  UpperCAmelCase_						):
                  convert_model(UpperCAmelCase_ ,  to_transformer_engine=UpperCAmelCase_						)

      if is_compiled:
            a						:List[Any] 							=       model
            a						:int 							=       compiled_model

      return model
def 				__lowerCamelCase						( ):




      """simple docstring"""





      PartialState().wait_for_everyone()
def 				__lowerCamelCase						( UpperCAmelCase_						:		str ,  UpperCAmelCase_						:		int						):




      """simple docstring"""





      if PartialState().distributed_type == DistributedType.TPU:
            xm.save(UpperCAmelCase_ ,  UpperCAmelCase_						)
      elif PartialState().local_process_index == 0:
            torch.save(UpperCAmelCase_ ,  UpperCAmelCase_						)
@contextmanager
def 				__lowerCamelCase						( **UpperCAmelCase_						:		Union[str, Any]						):




      """simple docstring"""





      for key, value in kwargs.items():
            a						:Union[str, Any] 							=       str(UpperCAmelCase_						)

      yield

      for key in kwargs:
            if key.upper() in os.environ:
                  del os.environ[key.upper()]
def 				__lowerCamelCase						( UpperCAmelCase_						:		Dict						):




      """simple docstring"""





      if not hasattr(UpperCAmelCase_ ,  '''__qualname__'''						) and not hasattr(UpperCAmelCase_ ,  '''__name__'''						):
            a						:List[str] 							=       getattr(UpperCAmelCase_ ,  '''__class__''' ,  UpperCAmelCase_						)
      if hasattr(UpperCAmelCase_ ,  '''__qualname__'''						):
            return obj.__qualname__
      if hasattr(UpperCAmelCase_ ,  '''__name__'''						):
            return obj.__name__
      return str(UpperCAmelCase_						)
def 				__lowerCamelCase						( UpperCAmelCase_						:		Optional[Any] ,  UpperCAmelCase_						:		Tuple						):




      """simple docstring"""





      for key, value in source.items():
            if isinstance(UpperCAmelCase_ ,  UpperCAmelCase_						):
                  a						:Tuple 							=       destination.setdefault(UpperCAmelCase_ ,  {}						)
                  merge_dicts(UpperCAmelCase_ ,  UpperCAmelCase_						)
            else:
                  a						:Optional[int] 							=       value

      return destination






def 				__lowerCamelCase						( UpperCAmelCase_						:		int = None						):




      """simple docstring"""





      if port is None:
            a						:Any 							=       2_9500
      with socket.socket(socket.AF_INET ,  socket.SOCK_STREAM						) as s:
            return s.connect_ex(('''localhost''', port)						) == 0









snake_case				:    str       				=     '''
# Transformers installation
! pip install transformers datasets
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

snake_case				:    List[Any]       				=     [{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
snake_case				:    int       				=     {
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}





