





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  (	UpperCamelCase__   :   Tuple					)      ->    Union[str, Any]:

						"""simple docstring"""





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






def 		lowerCAmelCase  (	UpperCamelCase__   :   List[str]       ,			UpperCamelCase__   :   bool = True					)      ->    Tuple:

						"""simple docstring"""





						__SCREAMING_SNAKE_CASE:				Optional[int]           =   (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

						__SCREAMING_SNAKE_CASE:				List[Any]           =   is_compiled_module(_A					)
						if is_compiled:
												__SCREAMING_SNAKE_CASE:				Optional[int]           =   model
												__SCREAMING_SNAKE_CASE:				List[str]           =   model._orig_mod

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

						while isinstance(_A       ,			_A					):
												__SCREAMING_SNAKE_CASE:				Optional[Any]           =   model.module

						if not keep_fpaa_wrapper:
												__SCREAMING_SNAKE_CASE:				Optional[Any]           =   getattr(_A       ,			'''forward'''					)
												__SCREAMING_SNAKE_CASE:				List[Any]           =   model.__dict__.pop('''_original_forward'''       ,			_A					)
												if original_forward is not None:
																		while hasattr(_A       ,			'''__wrapped__'''					):
																								__SCREAMING_SNAKE_CASE:				List[Any]           =   forward.__wrapped__
																								if forward == original_forward:
																														break
																		__SCREAMING_SNAKE_CASE:				Dict           =   forward
												if getattr(_A       ,			'''_converted_to_transformer_engine'''       ,			_A					):
																		convert_model(_A       ,			to_transformer_engine=_A					)

						if is_compiled:
												__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   model
												__SCREAMING_SNAKE_CASE:				Optional[Any]           =   compiled_model

						return model






def 		lowerCAmelCase  (	)      ->    int:

						"""simple docstring"""





						PartialState().wait_for_everyone()






def 		lowerCAmelCase  (	UpperCamelCase__   :   Any       ,			UpperCamelCase__   :   str					)      ->    str:

						"""simple docstring"""





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






@contextmanager
def 		lowerCAmelCase  (	**UpperCamelCase__   :   List[str]					)      ->    Dict:

						"""simple docstring"""





						for key, value in kwargs.items():
												__SCREAMING_SNAKE_CASE:				Tuple           =   str(_A					)

						yield

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






def 		lowerCAmelCase  (	UpperCamelCase__   :   List[Any]					)      ->    Tuple:

						"""simple docstring"""





						if not hasattr(_A       ,			'''__qualname__'''					) and not hasattr(_A       ,			'''__name__'''					):
												__SCREAMING_SNAKE_CASE:				List[str]           =   getattr(_A       ,			'''__class__'''       ,			_A					)
						if hasattr(_A       ,			'''__qualname__'''					):
												return obj.__qualname__
						if hasattr(_A       ,			'''__name__'''					):
												return obj.__name__
						return str(_A					)






def 		lowerCAmelCase  (	UpperCamelCase__   :   List[str]       ,			UpperCamelCase__   :   Optional[int]					)      ->    List[str]:

						"""simple docstring"""





						for key, value in source.items():
												if isinstance(_A       ,			_A					):
																		__SCREAMING_SNAKE_CASE:				Any           =   destination.setdefault(_A       ,			{}					)
																		merge_dicts(_A       ,			_A					)
												else:
																		__SCREAMING_SNAKE_CASE:				List[str]           =   value

						return destination






def 		lowerCAmelCase  (	UpperCamelCase__   :   int = None					)      ->    bool:

						"""simple docstring"""





						if port is None:
												__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   29_500
						with socket.socket(socket.AF_INET       ,			socket.SOCK_STREAM					) as s:
												return s.connect_ex(('''localhost''', port)					) == 0




'''simple docstring'''




import os
from distutils.util import strtobool






def 							snake_case__				(     _A:   int ,  _A:   List[Any]					)       ->      Union[str, Any]:

		'''simple docstring'''


		for e in env_keys:
				lowerCAmelCase     =    int(os.environ.get(_A ,  -1					)					)
				if val >= 0:
						return val
		return default






def 							snake_case__				(     _A:   str ,  _A:   List[Any]=False					)       ->      int:

		'''simple docstring'''


		lowerCAmelCase     =    os.environ.get(_A ,  str(_A					)					)
		return strtobool(_A					) == 1  # As its name indicates `strtobool` actually returns an int...






def 							snake_case__				(     _A:   Dict ,  _A:   List[Any]="no"					)       ->      str:

		'''simple docstring'''


		lowerCAmelCase     =    os.environ.get(_A ,  str(_A					)					)
		return value




