'''simple docstring'''





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      (						A_   : int					)    ->  Any:


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




def       __lowerCAmelCase      (						A_   : Optional[Any]						,	A_   : bool = True					)    ->  Optional[int]:
  __UpperCAmelCase						    =       (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  __UpperCAmelCase						    =       is_compiled_module(__snake_case					)
  if is_compiled:
    __UpperCAmelCase						    =       model
    __UpperCAmelCase						    =       model._orig_mod

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

  while isinstance(__snake_case						,	__snake_case					):
    __UpperCAmelCase						    =       model.module

  if not keep_fpaa_wrapper:
    __UpperCAmelCase						    =       getattr(__snake_case						,	"forward"					)
    __UpperCAmelCase						    =       model.__dict__.pop("_original_forward"						,	__snake_case					)
    if original_forward is not None:
      while hasattr(__snake_case						,	"__wrapped__"					):
        __UpperCAmelCase						    =       forward.__wrapped__
        if forward == original_forward:
          break
      __UpperCAmelCase						    =       forward
    if getattr(__snake_case						,	"_converted_to_transformer_engine"						,	__snake_case					):
      convert_model(__snake_case						,	to_transformer_engine=__snake_case					)

  if is_compiled:
    __UpperCAmelCase						    =       model
    __UpperCAmelCase						    =       compiled_model

  return model




def       __lowerCAmelCase      (						)    ->  Optional[int]:
  PartialState().wait_for_everyone()




def       __lowerCAmelCase      (						A_   : List[str]						,	A_   : Tuple					)    ->  List[str]:


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




@contextmanager
def       __lowerCAmelCase      (						**A_   : str					)    ->  List[Any]:


  for key, value in kwargs.items():
    __UpperCAmelCase						    =       str(__snake_case					)

  yield

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




def       __lowerCAmelCase      (						A_   : Dict					)    ->  List[str]:


  if not hasattr(__snake_case						,	"__qualname__"					) and not hasattr(__snake_case						,	"__name__"					):
    __UpperCAmelCase						    =       getattr(__snake_case						,	"__class__"						,	__snake_case					)
  if hasattr(__snake_case						,	"__qualname__"					):
    return obj.__qualname__
  if hasattr(__snake_case						,	"__name__"					):
    return obj.__name__
  return str(__snake_case					)




def       __lowerCAmelCase      (						A_   : Union[str, Any]						,	A_   : List[Any]					)    ->  Union[str, Any]:


  for key, value in source.items():
    if isinstance(__snake_case						,	__snake_case					):
      __UpperCAmelCase						    =       destination.setdefault(__snake_case						,	{}					)
      merge_dicts(__snake_case						,	__snake_case					)
    else:
      __UpperCAmelCase						    =       value

  return destination





def       __lowerCAmelCase      (						A_   : int = None					)    ->  bool:


  if port is None:
    __UpperCAmelCase						    =       2_95_00
  with socket.socket(socket.AF_INET						,	socket.SOCK_STREAM					) as s:
    return s.connect_ex(("localhost", port)					) == 0




from ..utils import DummyObject, requires_backends


class 			UpperCAmelCase__    (		metaclass=snake_case					):



  """simple docstring"""






  lowerCAmelCase__		:	List[str]    		= ['transformers', 'torch', 'note_seq']






  def __init__(				self:			List[str]				,				*__lowerCAmelCase:			Optional[int]				,				**__lowerCAmelCase:			List[Any]   ) -> Optional[int]:




    '''simple docstring'''




    requires_backends(self				,				["transformers", "torch", "note_seq"]   )






  @classmethod
  def 	_UpperCAmelCase				(				cls:			Optional[int]				,				*__lowerCAmelCase:			Any				,				**__lowerCAmelCase:			List[str]   ) -> Dict:




    '''simple docstring'''




    requires_backends(cls				,				["transformers", "torch", "note_seq"]   )






  @classmethod
  def 	_UpperCAmelCase				(				cls:			Union[str, Any]				,				*__lowerCAmelCase:			Optional[Any]				,				**__lowerCAmelCase:			Optional[Any]   ) -> Any:




    '''simple docstring'''




    requires_backends(cls				,				["transformers", "torch", "note_seq"]   )



