




'''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 						SCREAMING_SNAKE_CASE       (      a_	:							str			):
  if is_torch_version('<'				,					'2.0.0'			) or not hasattr(a_				,					'_dynamo'			):
    return False
  return isinstance(a_				,					torch._dynamo.eval_frame.OptimizedModule			)


def 						SCREAMING_SNAKE_CASE       (      a_	:							str				,					a_	:							bool = True			):
  __a			=					(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  __a			=					is_compiled_module(a_			)
  if is_compiled:
    __a			=					model
    __a			=					model._orig_mod

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

  while isinstance(a_				,					a_			):
    __a			=					model.module

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

  if is_compiled:
    __a			=					model
    __a			=					compiled_model

  return model


def 						SCREAMING_SNAKE_CASE       (      ):
  PartialState().wait_for_everyone()


def 						SCREAMING_SNAKE_CASE       (      a_	:							Optional[int]				,					a_	:							int			):
  if PartialState().distributed_type == DistributedType.TPU:
    xm.save(a_				,					a_			)
  elif PartialState().local_process_index == 0:
    torch.save(a_				,					a_			)


@contextmanager
def 						SCREAMING_SNAKE_CASE       (      **a_	:							Dict			):
  for key, value in kwargs.items():
    __a			=					str(a_			)

  yield

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


def 						SCREAMING_SNAKE_CASE       (      a_	:							Tuple			):
  if not hasattr(a_				,					'__qualname__'			) and not hasattr(a_				,					'__name__'			):
    __a			=					getattr(a_				,					'__class__'				,					a_			)
  if hasattr(a_				,					'__qualname__'			):
    return obj.__qualname__
  if hasattr(a_				,					'__name__'			):
    return obj.__name__
  return str(a_			)


def 						SCREAMING_SNAKE_CASE       (      a_	:							Dict				,					a_	:							List[Any]			):
  for key, value in source.items():
    if isinstance(a_				,					a_			):
      __a			=					destination.setdefault(a_				,					{}			)
      merge_dicts(a_				,					a_			)
    else:
      __a			=					value

  return destination


def 						SCREAMING_SNAKE_CASE       (      a_	:							int = None			):
  if port is None:
    __a			=					2_9500
  with socket.socket(socket.AF_INET				,					socket.SOCK_STREAM			) as s:
    return s.connect_ex(('localhost', port)			) == 0









'''simple docstring'''


def 						SCREAMING_SNAKE_CASE       (      a_	:							int				,					a_	:							int			):
  return int((input_a, input_a).count(0			) != 0			)


def 						SCREAMING_SNAKE_CASE       (      ):
  assert nand_gate(0				,					0			) == 1
  assert nand_gate(0				,					1			) == 1
  assert nand_gate(1				,					0			) == 1
  assert nand_gate(1				,					1			) == 0


if __name__ == "__main__":
  print(nand_gate(0, 0))
  print(nand_gate(0, 1))
  print(nand_gate(1, 0))
  print(nand_gate(1, 1))



