



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  (					a__  )							->					str:




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






def       __lowercase  (					a__				,  a__ = True  )							->					List[str]:
							__SCREAMING_SNAKE_CASE										=   (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

							__SCREAMING_SNAKE_CASE										=   is_compiled_module(_UpperCamelCase  )
							if is_compiled:
														__SCREAMING_SNAKE_CASE										=   model
														__SCREAMING_SNAKE_CASE										=   model._orig_mod

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

							while isinstance(_UpperCamelCase				,  _UpperCamelCase  ):
														__SCREAMING_SNAKE_CASE										=   model.module

							if not keep_fpaa_wrapper:
														__SCREAMING_SNAKE_CASE										=   getattr(_UpperCamelCase				,  'forward'  )
														__SCREAMING_SNAKE_CASE										=   model.__dict__.pop('_original_forward'				,  _UpperCamelCase  )
														if original_forward is not None:
																					while hasattr(_UpperCamelCase				,  '__wrapped__'  ):
																												__SCREAMING_SNAKE_CASE										=   forward.__wrapped__
																												if forward == original_forward:
																																			break
																					__SCREAMING_SNAKE_CASE										=   forward
														if getattr(_UpperCamelCase				,  '_converted_to_transformer_engine'				,  _UpperCamelCase  ):
																					convert_model(_UpperCamelCase				,  to_transformer_engine=_UpperCamelCase  )

							if is_compiled:
														__SCREAMING_SNAKE_CASE										=   model
														__SCREAMING_SNAKE_CASE										=   compiled_model

							return model






def       __lowercase  (					)							->					str:
							PartialState().wait_for_everyone()






def       __lowercase  (					a__				,  a__  )							->					str:




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






@contextmanager
def       __lowercase  (					**a__  )							->					Dict:




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

							yield

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






def       __lowercase  (					a__  )							->					Optional[Any]:




							if not hasattr(_UpperCamelCase				,  '__qualname__'  ) and not hasattr(_UpperCamelCase				,  '__name__'  ):
														__SCREAMING_SNAKE_CASE										=   getattr(_UpperCamelCase				,  '__class__'				,  _UpperCamelCase  )
							if hasattr(_UpperCamelCase				,  '__qualname__'  ):
														return obj.__qualname__
							if hasattr(_UpperCamelCase				,  '__name__'  ):
														return obj.__name__
							return str(_UpperCamelCase  )






def       __lowercase  (					a__				,  a__  )							->					List[str]:




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

							return destination






def       __lowercase  (					a__ = None  )							->					Optional[int]:




							if port is None:
														__SCREAMING_SNAKE_CASE										=   2_95_00
							with socket.socket(socket.AF_INET				,  socket.SOCK_STREAM  ) as s:
														return s.connect_ex(('localhost', port)  ) == 0










"""simple docstring"""


import heapq as hq
import math
from collections.abc import Iterator
class     _UpperCamelCase    :
							'''simple docstring'''







							def __init__(	self       ,  __a      ):
										__lowerCAmelCase							       =     str(id_      )
										__lowerCAmelCase							       =     None
										__lowerCAmelCase							       =     None
										__lowerCAmelCase							       =     []
										__lowerCAmelCase							       =     {}  # {vertex:distance}

							def __lt__(	self       ,  __a      ):
										return self.key < other.key

							def __repr__(	self      ):
										return self.id

							def      snake_case							(	self       ,  __a      ):
										self.neighbors.append(__a      )



							def      snake_case							(	self       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     weight


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			graph[a - 1].add_neighbor(graph[b - 1] )
			graph[b - 1].add_neighbor(graph[a - 1] )
			# add the edges:
			graph[a - 1].add_edge(graph[b - 1]       ,					_UpperCamelCase )
			graph[b - 1].add_edge(graph[a - 1]       ,					_UpperCamelCase )


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			__lowerCAmelCase							       =     []
			for u in graph:
						__lowerCAmelCase							       =     math.inf
						__lowerCAmelCase							       =     None
			__lowerCAmelCase							       =     0
			__lowerCAmelCase							       =     graph[:]
			while q:
						__lowerCAmelCase							       =     min(_UpperCamelCase )
						q.remove(_UpperCamelCase )
						for v in u.neighbors:
									if (v in q) and (u.edges[v.id] < v.key):
												__lowerCAmelCase							       =     u
												__lowerCAmelCase							       =     u.edges[v.id]
			for i in range(1       ,					len(_UpperCamelCase ) ):
						a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) )
			return a


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			for u in graph:
						__lowerCAmelCase							       =     math.inf
						__lowerCAmelCase							       =     None
			__lowerCAmelCase							       =     0

			__lowerCAmelCase							       =     list(_UpperCamelCase )
			hq.heapify(_UpperCamelCase )

			while h:
						__lowerCAmelCase							       =     hq.heappop(_UpperCamelCase )
						for v in u.neighbors:
									if (v in h) and (u.edges[v.id] < v.key):
												__lowerCAmelCase							       =     u
												__lowerCAmelCase							       =     u.edges[v.id]
												hq.heapify(_UpperCamelCase )

			for i in range(1       ,					len(_UpperCamelCase ) ):
						yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1)


def _lowerCamelCase      (						):

			'''simple docstring'''






if __name__ == "__main__":
	import doctest

	doctest.testmod()




