





"""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      lowercase__ (					lowerCAmelCase__   :			str			)    ->			str:



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






def      lowercase__ (					lowerCAmelCase__   :			List[str]							,			lowerCAmelCase__   :			bool = True			)    ->			Tuple:



			'''simple docstring'''
			a__  :	List[Any]		 =			(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

			a__  :	Optional[Any]		 =			is_compiled_module(lowerCAmelCase__			)
			if is_compiled:
						a__  :	Tuple		 =			model
						a__  :	Tuple		 =			model._orig_mod

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

			while isinstance(lowerCAmelCase__							,			lowerCAmelCase__			):
						a__  :	Optional[Any]		 =			model.module

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

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

			return model






def      lowercase__ (					)    ->			Optional[int]:



			'''simple docstring'''
			PartialState().wait_for_everyone()






def      lowercase__ (					lowerCAmelCase__   :			Optional[int]							,			lowerCAmelCase__   :			Union[str, Any]			)    ->			Optional[int]:



			'''simple docstring'''
			if PartialState().distributed_type == DistributedType.TPU:
						xm.save(lowerCAmelCase__							,			lowerCAmelCase__			)
			elif PartialState().local_process_index == 0:
						torch.save(lowerCAmelCase__							,			lowerCAmelCase__			)






@contextmanager
def      lowercase__ (					**lowerCAmelCase__   :			List[Any]			)    ->			Optional[int]:



			'''simple docstring'''
			for key, value in kwargs.items():
						a__  :	List[str]		 =			str(lowerCAmelCase__			)

			yield

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






def      lowercase__ (					lowerCAmelCase__   :			Optional[int]			)    ->			Optional[Any]:



			'''simple docstring'''
			if not hasattr(lowerCAmelCase__							,			"__qualname__"			) and not hasattr(lowerCAmelCase__							,			"__name__"			):
						a__  :	Optional[Any]		 =			getattr(lowerCAmelCase__							,			"__class__"							,			lowerCAmelCase__			)
			if hasattr(lowerCAmelCase__							,			"__qualname__"			):
						return obj.__qualname__
			if hasattr(lowerCAmelCase__							,			"__name__"			):
						return obj.__name__
			return str(lowerCAmelCase__			)






def      lowercase__ (					lowerCAmelCase__   :			Any							,			lowerCAmelCase__   :			str			)    ->			Optional[int]:



			'''simple docstring'''
			for key, value in source.items():
						if isinstance(lowerCAmelCase__							,			lowerCAmelCase__			):
									a__  :	str		 =			destination.setdefault(lowerCAmelCase__							,			{}			)
									merge_dicts(lowerCAmelCase__							,			lowerCAmelCase__			)
						else:
									a__  :	List[str]		 =			value

			return destination






def      lowercase__ (					lowerCAmelCase__   :			int = None			)    ->			bool:



			'''simple docstring'''
			if port is None:
						a__  :	Dict		 =			2_9_5_0_0
			with socket.socket(socket.AF_INET							,			socket.SOCK_STREAM			) as s:
						return s.connect_ex(("localhost", port)			) == 0






"""simple docstring"""






import functools






def      lowercase__ (					lowerCAmelCase__   :			str							,			lowerCAmelCase__   :			str			)    ->			int:



			'''simple docstring'''
			a__  :	Any		 =			len(lowerCAmelCase__			)
			a__  :	Optional[int]		 =			len(lowerCAmelCase__			)

			@functools.cache
			def min_distance(lowerCAmelCase__   :			int							,			lowerCAmelCase__   :			int			) -> int:
						# if first word index is overflow - delete all from the second word
						if indexa >= len_worda:
									return len_worda - indexa
						# if second word index is overflow - delete all from the first word
						if indexa >= len_worda:
									return len_worda - indexa
						a__  :	List[Any]		 =			int(worda[indexa] != worda[indexa]			)  # current letters not identical
						return min(
						    1 + min_distance(indexa + 1							,			lowerCAmelCase__			)							,			1 + min_distance(lowerCAmelCase__							,			indexa + 1			)							,			diff + min_distance(indexa + 1							,			indexa + 1			)							,			)

			return min_distance(0							,			0			)


if __name__ == "__main__":
		import doctest

		doctest.testmod()