





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    __snake_case  (    _lowerCAmelCase       :       Tuple		)       ->  str:
   if is_torch_version("<" , "2.0.0"		) or not hasattr(lowerCAmelCase__ , "_dynamo"		):
      return False
   return isinstance(lowerCAmelCase__ , torch._dynamo.eval_frame.OptimizedModule		)

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

   A_    : Union[str, Any]								=	is_compiled_module(lowerCAmelCase__		)
   if is_compiled:
      A_    : str								=	model
      A_    : Tuple								=	model._orig_mod

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

   while isinstance(lowerCAmelCase__ , lowerCAmelCase__		):
      A_    : Dict								=	model.module

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

   if is_compiled:
      A_    : List[Any]								=	model
      A_    : int								=	compiled_model

   return model

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

def    __snake_case  (    _lowerCAmelCase       :       Optional[int] , _lowerCAmelCase       :       Tuple		)       ->  int:
   if PartialState().distributed_type == DistributedType.TPU:
      xm.save(lowerCAmelCase__ , lowerCAmelCase__		)
   elif PartialState().local_process_index == 0:
      torch.save(lowerCAmelCase__ , lowerCAmelCase__		)

@contextmanager
def    __snake_case  (    **_lowerCAmelCase       :       List[str]		)       ->  List[str]:
   for key, value in kwargs.items():
      A_    : int								=	str(lowerCAmelCase__		)

   yield

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

def    __snake_case  (    _lowerCAmelCase       :       Any		)       ->  Optional[int]:
   if not hasattr(lowerCAmelCase__ , "__qualname__"		) and not hasattr(lowerCAmelCase__ , "__name__"		):
      A_    : Union[str, Any]								=	getattr(lowerCAmelCase__ , "__class__" , lowerCAmelCase__		)
   if hasattr(lowerCAmelCase__ , "__qualname__"		):
      return obj.__qualname__
   if hasattr(lowerCAmelCase__ , "__name__"		):
      return obj.__name__
   return str(lowerCAmelCase__		)

def    __snake_case  (    _lowerCAmelCase       :       Any , _lowerCAmelCase       :       Union[str, Any]		)       ->  str:
   for key, value in source.items():
      if isinstance(lowerCAmelCase__ , lowerCAmelCase__		):
         A_    : Union[str, Any]								=	destination.setdefault(lowerCAmelCase__ , {}		)
         merge_dicts(lowerCAmelCase__ , lowerCAmelCase__		)
      else:
         A_    : List[str]								=	value

   return destination





def    __snake_case  (    _lowerCAmelCase       :       int = None		)       ->  bool:
   if port is None:
      A_    : str								=	29500
   with socket.socket(socket.AF_INET , socket.SOCK_STREAM		) as s:
      return s.connect_ex(("localhost", port)		) == 0




"""simple docstring"""




lowercase_			     =  [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
lowercase_			     =  [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
lowercase_			     =  {
    0: "Sunday",
    1: "Monday",
    2: "Tuesday",
    3: "Wednesday",
    4: "Thursday",
    5: "Friday",
    6: "Saturday",
}

def        lowercase							(		lowerCAmelCase__ :     int    ,					lowerCAmelCase__ :     int    ,					lowerCAmelCase__ :     int			) ->      str:
		assert len(str(lowerCAmelCase__			)			) > 2, "year should be in YYYY format"
		assert 1 <= month <= 12, "month should be between 1 to 12"
		assert 1 <= day <= 31, "day should be between 1 to 31"

		# Doomsday algorithm:
		__a           =			year // 100
		__a           =			(5 * (century % 4) + 2) % 7
		__a           =			year % 100
		__a           =			centurian % 12
		__a           =			(
		    (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor
		) % 7
		__a           =			(
		    DOOMSDAY_NOT_LEAP[month - 1]
		    if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0)
		    else DOOMSDAY_LEAP[month - 1]
		)
		__a           =			(dooms_day + day - day_anchor) % 7
		return WEEK_DAY_NAMES[week_day]


if __name__ == "__main__":
				import doctest

				doctest.testmod()






