
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 	_SCREAMING_SNAKE_CASE					(		_lowerCamelCase :			int)					->    Union[str, Any]:






							'''simple docstring'''





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




def 	_SCREAMING_SNAKE_CASE					(		_lowerCamelCase :			Tuple						,  _lowerCamelCase :			int = True)					->    Optional[Any]:






							'''simple docstring'''





							__UpperCamelCase	:   Dict			   =					(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

							__UpperCamelCase	:   Optional[int]			   =					is_compiled_module(__snake_case)
							if is_compiled:
														__UpperCamelCase	:   Optional[int]			   =					model
														__UpperCamelCase	:   Dict			   =					model._orig_mod

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

							while isinstance(__snake_case						,  __snake_case):
														__UpperCamelCase	:   Dict			   =					model.module

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

							if is_compiled:
														__UpperCamelCase	:   Tuple			   =					model
														__UpperCamelCase	:   int			   =					compiled_model

							return model




def 	_SCREAMING_SNAKE_CASE					(		)					->    Tuple:






							'''simple docstring'''





							PartialState().wait_for_everyone()




def 	_SCREAMING_SNAKE_CASE					(		_lowerCamelCase :			Any						,  _lowerCamelCase :			List[str])					->    Any:






							'''simple docstring'''





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




@contextmanager
def 	_SCREAMING_SNAKE_CASE					(		**_lowerCamelCase :			str)					->    int:






							'''simple docstring'''





							for key, value in kwargs.items():
														__UpperCamelCase	:   List[str]			   =					str(__snake_case)

							yield

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




def 	_SCREAMING_SNAKE_CASE					(		_lowerCamelCase :			Dict)					->    List[str]:






							'''simple docstring'''





							if not hasattr(__snake_case						,  "__qualname__") and not hasattr(__snake_case						,  "__name__"):
														__UpperCamelCase	:   Dict			   =					getattr(__snake_case						,  "__class__"						,  __snake_case)
							if hasattr(__snake_case						,  "__qualname__"):
														return obj.__qualname__
							if hasattr(__snake_case						,  "__name__"):
														return obj.__name__
							return str(__snake_case)




def 	_SCREAMING_SNAKE_CASE					(		_lowerCamelCase :			Any						,  _lowerCamelCase :			int)					->    Tuple:






							'''simple docstring'''





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

							return destination




def 	_SCREAMING_SNAKE_CASE					(		_lowerCamelCase :			Optional[Any] = None)					->    bool:






							'''simple docstring'''





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






def lowerCAmelCase__(__snake_case       ,__snake_case							)				->      float:







	'''simple docstring'''







	if mass < 0:
		raise ValueError('''The mass of a body cannot be negative'''							)
	return 0.5 * mass * abs(__snake_case							) * abs(__snake_case							)


if __name__ == "__main__":
			import doctest

			doctest.testmod(verbose=True)



