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__       (						__UpperCamelCase ):
      if is_torch_version("<"   ,					"2.0.0" ) or not hasattr(__UpperCAmelCase   ,					"_dynamo" ):
            return False
      return isinstance(__UpperCAmelCase   ,					torch._dynamo.eval_frame.OptimizedModule )


def    a__       (						__UpperCamelCase   ,					__UpperCamelCase = True ):
      SCREAMING_SNAKE_CASE_      =     (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

      SCREAMING_SNAKE_CASE_      =     is_compiled_module(__UpperCAmelCase )
      if is_compiled:
            SCREAMING_SNAKE_CASE_      =     model
            SCREAMING_SNAKE_CASE_      =     model._orig_mod

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

      while isinstance(__UpperCAmelCase   ,					__UpperCAmelCase ):
            SCREAMING_SNAKE_CASE_      =     model.module

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

      if is_compiled:
            SCREAMING_SNAKE_CASE_      =     model
            SCREAMING_SNAKE_CASE_      =     compiled_model

      return model


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


def    a__       (						__UpperCamelCase   ,					__UpperCamelCase ):
      if PartialState().distributed_type == DistributedType.TPU:
            xm.save(__UpperCAmelCase   ,					__UpperCAmelCase )
      elif PartialState().local_process_index == 0:
            torch.save(__UpperCAmelCase   ,					__UpperCAmelCase )


@contextmanager
def    a__       (						**__UpperCamelCase ):
      for key, value in kwargs.items():
            SCREAMING_SNAKE_CASE_      =     str(__UpperCAmelCase )

      yield

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


def    a__       (						__UpperCamelCase ):
      if not hasattr(__UpperCAmelCase   ,					"__qualname__" ) and not hasattr(__UpperCAmelCase   ,					"__name__" ):
            SCREAMING_SNAKE_CASE_      =     getattr(__UpperCAmelCase   ,					"__class__"   ,					__UpperCAmelCase )
      if hasattr(__UpperCAmelCase   ,					"__qualname__" ):
            return obj.__qualname__
      if hasattr(__UpperCAmelCase   ,					"__name__" ):
            return obj.__name__
      return str(__UpperCAmelCase )


def    a__       (						__UpperCamelCase   ,					__UpperCamelCase ):
      for key, value in source.items():
            if isinstance(__UpperCAmelCase   ,					__UpperCAmelCase ):
                  SCREAMING_SNAKE_CASE_      =     destination.setdefault(__UpperCAmelCase   ,					{} )
                  merge_dicts(__UpperCAmelCase   ,					__UpperCAmelCase )
            else:
                  SCREAMING_SNAKE_CASE_      =     value

      return destination





def    a__       (						__UpperCamelCase = None ):
      if port is None:
            SCREAMING_SNAKE_CASE_      =     2_9_5_0_0
      with socket.socket(socket.AF_INET   ,					socket.SOCK_STREAM ) as s:
            return s.connect_ex(("localhost", port) ) == 0









from __future__ import annotations




def 		lowerCAmelCase_    (  __UpperCAmelCase:			int		,   __UpperCAmelCase:			int      )      ->	list[list[int]]:
    UpperCamelCase__							:				list[list[int]]          =      []
    create_all_state(1		,   __UpperCAmelCase		,   __UpperCAmelCase		,   []		,   __UpperCAmelCase      )
    return result




def 		lowerCAmelCase_    (  __UpperCAmelCase:			int		,   __UpperCAmelCase:			int		,   __UpperCAmelCase:			int		,   __UpperCAmelCase:			list[int]		,   __UpperCAmelCase:			list[list[int]]		,   )      ->	None:
    if level == 0:
        total_list.append(current_list[:]      )
        return

    for i in range(__UpperCAmelCase		,   total_number - level + 2      ):
        current_list.append(__UpperCAmelCase      )
        create_all_state(i + 1		,   __UpperCAmelCase		,   level - 1		,   __UpperCAmelCase		,   __UpperCAmelCase      )
        current_list.pop()




def 		lowerCAmelCase_    (  __UpperCAmelCase:			list[list[int]]      )      ->	None:
    for i in total_list:
        print(*__UpperCAmelCase      )


if __name__ == "__main__":
  UpperCAmelCase_     							= 4
  UpperCAmelCase_     							= 2
  UpperCAmelCase_     							= generate_all_combinations(n, k)
  print_all_state(total_list)

