


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      __lowerCamelCase		(				lowerCamelCase__						:		List[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      __lowerCamelCase		(				lowerCamelCase__						:		List[Any]   ,				lowerCamelCase__						:		int = True	):





			'''simple docstring'''




			lowerCamelCase  						=   (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

			lowerCamelCase  						=   is_compiled_module(SCREAMING_SNAKE_CASE__	)
			if is_compiled:
						lowerCamelCase  						=   model
						lowerCamelCase  						=   model._orig_mod

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

			while isinstance(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__	):
						lowerCamelCase  						=   model.module

			if not keep_fpaa_wrapper:
						lowerCamelCase  						=   getattr(SCREAMING_SNAKE_CASE__   ,				"""forward"""	)
						lowerCamelCase  						=   model.__dict__.pop("""_original_forward"""   ,				SCREAMING_SNAKE_CASE__	)
						if original_forward is not None:
									while hasattr(SCREAMING_SNAKE_CASE__   ,				"""__wrapped__"""	):
												lowerCamelCase  						=   forward.__wrapped__
												if forward == original_forward:
															break
									lowerCamelCase  						=   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:
						lowerCamelCase  						=   model
						lowerCamelCase  						=   compiled_model

			return model






def      __lowerCamelCase		(				):





			'''simple docstring'''




			PartialState().wait_for_everyone()






def      __lowerCamelCase		(				lowerCamelCase__						:		int   ,				lowerCamelCase__						:		Tuple	):





			'''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      __lowerCamelCase		(				**lowerCamelCase__						:		Optional[int]	):





			'''simple docstring'''




			for key, value in kwargs.items():
						lowerCamelCase  						=   str(SCREAMING_SNAKE_CASE__	)

			yield

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






def      __lowerCamelCase		(				lowerCamelCase__						:		List[str]	):





			'''simple docstring'''




			if not hasattr(SCREAMING_SNAKE_CASE__   ,				"""__qualname__"""	) and not hasattr(SCREAMING_SNAKE_CASE__   ,				"""__name__"""	):
						lowerCamelCase  						=   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      __lowerCamelCase		(				lowerCamelCase__						:		Optional[Any]   ,				lowerCamelCase__						:		List[Any]	):





			'''simple docstring'''




			for key, value in source.items():
						if isinstance(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__	):
									lowerCamelCase  						=   destination.setdefault(SCREAMING_SNAKE_CASE__   ,				{}	)
									merge_dicts(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__	)
						else:
									lowerCamelCase  						=   value

			return destination






def      __lowerCamelCase		(				lowerCamelCase__						:		int = None	):





			'''simple docstring'''




			if port is None:
						lowerCamelCase  						=   29500
			with socket.socket(socket.AF_INET   ,				socket.SOCK_STREAM	) as s:
						return s.connect_ex(("""localhost""", port)	) == 0





'''simple docstring'''


import os
import sys
from contextlib import contextmanager


# Windows only
if os.name == "nt":
				import ctypes
				import msvcrt  # noqa





				class     lowercase__  (						ctypes.Structure					):





											'''simple docstring'''


											A_							:      Optional[Any]      	=  [("""size""", ctypes.c_int), ("""visible""", ctypes.c_byte)]







def 			snake_case_   (  ):







				"""simple docstring"""





				if os.name == "nt":
								_SCREAMING_SNAKE_CASE :       Tuple     		=      CursorInfo()
								_SCREAMING_SNAKE_CASE :       Tuple     		=      ctypes.windll.kernelaa.GetStdHandle(-11     )
								ctypes.windll.kernelaa.GetConsoleCursorInfo(SCREAMING_SNAKE_CASE__							,      ctypes.byref(SCREAMING_SNAKE_CASE__     )     )
								_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      False
								ctypes.windll.kernelaa.SetConsoleCursorInfo(SCREAMING_SNAKE_CASE__							,      ctypes.byref(SCREAMING_SNAKE_CASE__     )     )
				elif os.name == "posix":
								sys.stdout.write("""\033[?25l"""     )
								sys.stdout.flush()







def 			snake_case_   (  ):







				"""simple docstring"""





				if os.name == "nt":
								_SCREAMING_SNAKE_CASE :       int     		=      CursorInfo()
								_SCREAMING_SNAKE_CASE :       List[str]     		=      ctypes.windll.kernelaa.GetStdHandle(-11     )
								ctypes.windll.kernelaa.GetConsoleCursorInfo(SCREAMING_SNAKE_CASE__							,      ctypes.byref(SCREAMING_SNAKE_CASE__     )     )
								_SCREAMING_SNAKE_CASE :       Tuple     		=      True
								ctypes.windll.kernelaa.SetConsoleCursorInfo(SCREAMING_SNAKE_CASE__							,      ctypes.byref(SCREAMING_SNAKE_CASE__     )     )
				elif os.name == "posix":
								sys.stdout.write("""\033[?25h"""     )
								sys.stdout.flush()







@contextmanager
def 			snake_case_   (  ):







				"""simple docstring"""





				try:
								hide_cursor()
								yield
				finally:
								show_cursor()




