




"""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 					UpperCAmelCase__       (snake_case__							:					int    ):
  """simple docstring"""

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


def 					UpperCAmelCase__       (snake_case__							:					Tuple		,   snake_case__							:					int = True    ):
  """simple docstring"""

  _snake_case							:		Any								=  (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  _snake_case							:		Optional[int]								=  is_compiled_module(__A    )
  if is_compiled:
    _snake_case							:		Any								=  model
    _snake_case							:		Optional[int]								=  model._orig_mod

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

  while isinstance(__A		,   __A    ):
    _snake_case							:		Optional[int]								=  model.module

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

  if is_compiled:
    _snake_case							:		List[Any]								=  model
    _snake_case							:		Optional[int]								=  compiled_model

  return model


def 					UpperCAmelCase__       ():
  """simple docstring"""

  PartialState().wait_for_everyone()


def 					UpperCAmelCase__       (snake_case__							:					Optional[Any]		,   snake_case__							:					Union[str, Any]    ):
  """simple docstring"""

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


@contextmanager
def 					UpperCAmelCase__       (**snake_case__							:					Tuple    ):
  """simple docstring"""

  for key, value in kwargs.items():
    _snake_case							:		Tuple								=  str(__A    )

  yield

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


def 					UpperCAmelCase__       (snake_case__							:					str    ):
  """simple docstring"""

  if not hasattr(__A		,   """__qualname__"""    ) and not hasattr(__A		,   """__name__"""    ):
    _snake_case							:		List[Any]								=  getattr(__A		,   """__class__"""		,   __A    )
  if hasattr(__A		,   """__qualname__"""    ):
    return obj.__qualname__
  if hasattr(__A		,   """__name__"""    ):
    return obj.__name__
  return str(__A    )


def 					UpperCAmelCase__       (snake_case__							:					Optional[Any]		,   snake_case__							:					str    ):
  """simple docstring"""

  for key, value in source.items():
    if isinstance(__A		,   __A    ):
      _snake_case							:		Any								=  destination.setdefault(__A		,   {}    )
      merge_dicts(__A		,   __A    )
    else:
      _snake_case							:		Optional[Any]								=  value

  return destination






def 					UpperCAmelCase__       (snake_case__							:					int = None    ):
  """simple docstring"""

  if port is None:
    _snake_case							:		List[str]								=  2_95_00
  with socket.socket(socket.AF_INET		,   socket.SOCK_STREAM    ) as s:
    return s.connect_ex(("""localhost""", port)    ) == 0



import math




def    lowerCAmelCase_ (     __A							)     ->				bool:







      '''simple docstring'''


      return math.sqrt(__A							) * math.sqrt(__A							) == num




def    lowerCAmelCase_ (     __A							)     ->				bool:







      '''simple docstring'''


      UpperCAmelCase__								=  0
      UpperCAmelCase__								=  n
      while left <= right:
            UpperCAmelCase__								=  (left + right) // 2
            if mid**2 == n:
                  return True
            elif mid**2 > n:
                  UpperCAmelCase__								=  mid - 1
            else:
                  UpperCAmelCase__								=  mid + 1
      return False


if __name__ == "__main__":
      import doctest

      doctest.testmod()



