


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__    (		snake_case__					:							Dict     ):
  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    a__    (		snake_case__					:							List[str]						,   snake_case__					:							bool = True     ):
  _UpperCAmelCase		:							str							=     (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  _UpperCAmelCase		:							Any							=     is_compiled_module(snake_case__     )
  if is_compiled:
    _UpperCAmelCase		:							int							=     model
    _UpperCAmelCase		:							Optional[int]							=     model._orig_mod

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

  while isinstance(snake_case__						,   snake_case__     ):
    _UpperCAmelCase		:							Dict							=     model.module

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

  if is_compiled:
    _UpperCAmelCase		:							Optional[Any]							=     model
    _UpperCAmelCase		:							List[str]							=     compiled_model

  return model




def    a__    (		):
  PartialState().wait_for_everyone()




def    a__    (		snake_case__					:							List[Any]						,   snake_case__					:							Tuple     ):
  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    a__    (		**snake_case__					:							Union[str, Any]     ):
  for key, value in kwargs.items():
    _UpperCAmelCase		:							List[Any]							=     str(snake_case__     )

  yield

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




def    a__    (		snake_case__					:							Dict     ):
  if not hasattr(snake_case__						,   """__qualname__"""     ) and not hasattr(snake_case__						,   """__name__"""     ):
    _UpperCAmelCase		:							Optional[int]							=     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    a__    (		snake_case__					:							Union[str, Any]						,   snake_case__					:							Tuple     ):
  for key, value in source.items():
    if isinstance(snake_case__						,   snake_case__     ):
      _UpperCAmelCase		:							Tuple							=     destination.setdefault(snake_case__						,   {}     )
      merge_dicts(snake_case__						,   snake_case__     )
    else:
      _UpperCAmelCase		:							Tuple							=     value

  return destination





def    a__    (		snake_case__					:							int = None     ):
  if port is None:
    _UpperCAmelCase		:							Optional[int]							=     2_9500
  with socket.socket(socket.AF_INET						,   socket.SOCK_STREAM     ) as s:
    return s.connect_ex(("""localhost""", port)     ) == 0









def    a__    (		snake_case__					:							int						,   snake_case__					:							int     ):
  return x if y == 0 else greatest_common_divisor(snake_case__						,   x % y     )




def    a__    (		snake_case__					:							int						,   snake_case__					:							int     ):
  return (x * y) // greatest_common_divisor(snake_case__						,   snake_case__     )




def    a__    (		snake_case__					:							int = 20     ):
  _UpperCAmelCase		:							Union[str, Any]							=     1
  for i in range(1						,   n + 1     ):
    _UpperCAmelCase		:							Dict							=     lcm(snake_case__						,   snake_case__     )
  return g


if __name__ == "__main__":
 print(F'{solution() = }')





