



"""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 		__snake_case		( SCREAMING_SNAKE_CASE__      :		int							)    ->  Union[str, Any]:





     '''simple docstring'''

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

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Optional[Any]		,    SCREAMING_SNAKE_CASE__      :		int = True							)    ->  str:





     '''simple docstring'''

     _UpperCAmelCase     :      List[Any]														=       (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

     _UpperCAmelCase     :      int														=       is_compiled_module(SCREAMING_SNAKE_CASE_							)
     if is_compiled:
          _UpperCAmelCase     :      Dict														=       model
          _UpperCAmelCase     :      List[Any]														=       model._orig_mod

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

     while isinstance(SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_							):
          _UpperCAmelCase     :      List[Any]														=       model.module

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

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

     return model

def 		__snake_case		( )    ->  Dict:





     '''simple docstring'''

     PartialState().wait_for_everyone()

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		str		,    SCREAMING_SNAKE_CASE__      :		Dict							)    ->  List[Any]:





     '''simple docstring'''

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

@contextmanager
def 		__snake_case		( **SCREAMING_SNAKE_CASE__      :		Any							)    ->  str:





     '''simple docstring'''

     for key, value in kwargs.items():
          _UpperCAmelCase     :      Dict														=       str(SCREAMING_SNAKE_CASE_							)

     yield

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

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Union[str, Any]							)    ->  int:





     '''simple docstring'''

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

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Dict		,    SCREAMING_SNAKE_CASE__      :		Dict							)    ->  Union[str, Any]:





     '''simple docstring'''

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

     return destination

def 		__snake_case		( SCREAMING_SNAKE_CASE__      :		Union[str, Any] = None							)    ->  bool:





     '''simple docstring'''

     if port is None:
          _UpperCAmelCase     :      Optional[int]														=       29_500
     with socket.socket(socket.AF_INET		,    socket.SOCK_STREAM							) as s:
          return s.connect_ex(("localhost", port)							) == 0



def 			lowerCAmelCase(      SCREAMING_SNAKE_CASE_			)->						list:



    """simple docstring"""






    if bit_count < 0:
        raise ValueError("The given input must be positive"			)

    # get the generated string sequence
    UpperCamelCase_			  =						gray_code_sequence_string(SCREAMING_SNAKE_CASE_			)
    #
    # convert them to integers
    for i in range(len(SCREAMING_SNAKE_CASE_			)			):
        UpperCamelCase_			  =						int(sequence[i]			,						2			)

    return sequence




def 			lowerCAmelCase(      SCREAMING_SNAKE_CASE_			)->						list:



    """simple docstring"""






    if bit_count == 0:
        return ["0"]

    if bit_count == 1:
        return ["0", "1"]

    UpperCamelCase_			  =						1 << bit_count  # defines the length of the sequence
    # 1<< n is equivalent to 2^n

    # recursive answer will generate answer for n-1 bits
    UpperCamelCase_			  =						gray_code_sequence_string(bit_count - 1			)

    UpperCamelCase_			  =						[]

    # append 0 to first half of the smaller sequence generated
    for i in range(seq_len // 2			):
        UpperCamelCase_			  =						"0" + smaller_sequence[i]
        sequence.append(SCREAMING_SNAKE_CASE_			)

    # append 1 to second half ... start from the end of the list
    for i in reversed(range(seq_len // 2			)			):
        UpperCamelCase_			  =						"1" + smaller_sequence[i]
        sequence.append(SCREAMING_SNAKE_CASE_			)

    return sequence


if __name__ == "__main__":
 import doctest

 doctest.testmod()
