






"""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_       (   UpperCAmelCase_  )						->Tuple:





  """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_  ,			UpperCAmelCase_ = True  )						->str:





  """simple docstring"""


  __UpperCAmelCase    :						int       =						(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

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

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

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

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

  if is_compiled:
    __UpperCAmelCase    :						str       =						model
    __UpperCAmelCase    :						Any       =						compiled_model

  return model


def       lowerCamelCase_       (   )						->str:





  """simple docstring"""


  PartialState().wait_for_everyone()


def       lowerCamelCase_       (   UpperCAmelCase_  ,			UpperCAmelCase_  )						->Tuple:





  """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_  )						->str:





  """simple docstring"""


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

  yield

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


def       lowerCamelCase_       (   UpperCAmelCase_  )						->Union[str, Any]:





  """simple docstring"""


  if not hasattr(UpperCAmelCase_  ,			'''__qualname__'''  ) and not hasattr(UpperCAmelCase_  ,			'''__name__'''  ):
    __UpperCAmelCase    :						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_  ,			UpperCAmelCase_  )						->Any:





  """simple docstring"""


  for key, value in source.items():
    if isinstance(UpperCAmelCase_  ,			UpperCAmelCase_  ):
      __UpperCAmelCase    :						Union[str, Any]       =						destination.setdefault(UpperCAmelCase_  ,			{}  )
      merge_dicts(UpperCAmelCase_  ,			UpperCAmelCase_  )
    else:
      __UpperCAmelCase    :						str       =						value

  return destination







def       lowerCamelCase_       (   UpperCAmelCase_ = None  )						->bool:





  """simple docstring"""


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






"""simple docstring"""





# flake8: noqa
# Lint as: python3
lowercase__				:Tuple				   =       [
    'VerificationMode',
    'Version',
    'disable_progress_bar',
    'enable_progress_bar',
    'is_progress_bar_enabled',
    'experimental',
]

from .info_utils import VerificationMode
from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled
from .version import Version
from .experimental import experimental