'''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 							lowerCamelCase	(      UpperCAmelCase__       :					List[str] )			->      List[Any]:
							if is_torch_version("""<"""      ,      """2.0.0""" ) or not hasattr(UpperCAmelCase__      ,      """_dynamo""" ):
														return False
							return isinstance(UpperCAmelCase__      ,      torch._dynamo.eval_frame.OptimizedModule )




def 							lowerCamelCase	(      UpperCAmelCase__       :					Tuple      ,      UpperCAmelCase__       :					bool = True )			->      Optional[int]:
							lowercase_					:			Optional[int]										=       (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

							lowercase_					:			Optional[int]										=       is_compiled_module(UpperCAmelCase__ )
							if is_compiled:
														lowercase_					:			Union[str, Any]										=       model
														lowercase_					:			Dict										=       model._orig_mod

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

							while isinstance(UpperCAmelCase__      ,      UpperCAmelCase__ ):
														lowercase_					:			Dict										=       model.module

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

							if is_compiled:
														lowercase_					:			List[str]										=       model
														lowercase_					:			Dict										=       compiled_model

							return model




def 							lowerCamelCase	(      )			->      List[Any]:
							PartialState().wait_for_everyone()




def 							lowerCamelCase	(      UpperCAmelCase__       :					Optional[Any]      ,      UpperCAmelCase__       :					List[str] )			->      Union[str, Any]:
							if PartialState().distributed_type == DistributedType.TPU:
														xm.save(UpperCAmelCase__      ,      UpperCAmelCase__ )
							elif PartialState().local_process_index == 0:
														torch.save(UpperCAmelCase__      ,      UpperCAmelCase__ )




@contextmanager
def 							lowerCamelCase	(      **UpperCAmelCase__       :					str )			->      Optional[int]:
							for key, value in kwargs.items():
														lowercase_					:			List[Any]										=       str(UpperCAmelCase__ )

							yield

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




def 							lowerCamelCase	(      UpperCAmelCase__       :					Any )			->      int:
							if not hasattr(UpperCAmelCase__      ,      """__qualname__""" ) and not hasattr(UpperCAmelCase__      ,      """__name__""" ):
														lowercase_					:			List[Any]										=       getattr(UpperCAmelCase__      ,      """__class__"""      ,      UpperCAmelCase__ )
							if hasattr(UpperCAmelCase__      ,      """__qualname__""" ):
														return obj.__qualname__
							if hasattr(UpperCAmelCase__      ,      """__name__""" ):
														return obj.__name__
							return str(UpperCAmelCase__ )




def 							lowerCamelCase	(      UpperCAmelCase__       :					List[Any]      ,      UpperCAmelCase__       :					List[str] )			->      Dict:
							for key, value in source.items():
														if isinstance(UpperCAmelCase__      ,      UpperCAmelCase__ ):
																					lowercase_					:			str										=       destination.setdefault(UpperCAmelCase__      ,      {} )
																					merge_dicts(UpperCAmelCase__      ,      UpperCAmelCase__ )
														else:
																					lowercase_					:			Optional[int]										=       value

							return destination




def 							lowerCamelCase	(      UpperCAmelCase__       :					int = None )			->      bool:
							if port is None:
														lowercase_					:			Tuple										=       29500
							with socket.socket(socket.AF_INET      ,      socket.SOCK_STREAM ) as s:
														return s.connect_ex(("""localhost""", port) ) == 0






'''simple docstring'''




def 							lowerCamelCase	(      UpperCAmelCase__       :					int      ,      UpperCAmelCase__       :					int )			->      bool:
							return numa ^ numa < 0


if __name__ == "__main__":
						import doctest

						doctest.testmod()





