






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







def      snake_case		(							UpperCAmelCase	:       Any,						UpperCAmelCase	:       bool = True   ):
				A	 =      (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

				A	 =      is_compiled_module(UpperCAmelCase   )
				if is_compiled:
								A	 =      model
								A	 =      model._orig_mod

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

				while isinstance(UpperCAmelCase,						UpperCAmelCase   ):
								A	 =      model.module

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

				if is_compiled:
								A	 =      model
								A	 =      compiled_model

				return model







def      snake_case		(							):
				PartialState().wait_for_everyone()







def      snake_case		(							UpperCAmelCase	:       str,						UpperCAmelCase	:       Optional[int]   ):
				if PartialState().distributed_type == DistributedType.TPU:
								xm.save(UpperCAmelCase,						UpperCAmelCase   )
				elif PartialState().local_process_index == 0:
								torch.save(UpperCAmelCase,						UpperCAmelCase   )







@contextmanager
def      snake_case		(							**UpperCAmelCase	:       Optional[Any]   ):
				for key, value in kwargs.items():
								A	 =      str(UpperCAmelCase   )

				yield

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







def      snake_case		(							UpperCAmelCase	:       Tuple   ):
				if not hasattr(UpperCAmelCase,						'__qualname__'   ) and not hasattr(UpperCAmelCase,						'__name__'   ):
								A	 =      getattr(UpperCAmelCase,						'__class__',						UpperCAmelCase   )
				if hasattr(UpperCAmelCase,						'__qualname__'   ):
								return obj.__qualname__
				if hasattr(UpperCAmelCase,						'__name__'   ):
								return obj.__name__
				return str(UpperCAmelCase   )







def      snake_case		(							UpperCAmelCase	:       str,						UpperCAmelCase	:       List[str]   ):
				for key, value in source.items():
								if isinstance(UpperCAmelCase,						UpperCAmelCase   ):
												A	 =      destination.setdefault(UpperCAmelCase,						{}   )
												merge_dicts(UpperCAmelCase,						UpperCAmelCase   )
								else:
												A	 =      value

				return destination







def      snake_case		(							UpperCAmelCase	:       int = None   ):
				if port is None:
								A	 =      2_95_00
				with socket.socket(socket.AF_INET,						socket.SOCK_STREAM   ) as s:
								return s.connect_ex(('localhost', port)   ) == 0













import os


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_doctest_list.py
lowerCAmelCase_           = '.'


if __name__ == "__main__":
					lowerCAmelCase_           = os.path.join(REPO_PATH, 'utils/documentation_tests.txt')
					lowerCAmelCase_           = []
					lowerCAmelCase_           = []
					with open(doctest_file_path) as fp:
										for line in fp:
															lowerCAmelCase_           = line.strip()
															lowerCAmelCase_           = os.path.join(REPO_PATH, line)
															if not (os.path.isfile(path) or os.path.isdir(path)):
																				non_existent_paths.append(line)
															all_paths.append(path)
					if len(non_existent_paths) > 0:
										lowerCAmelCase_           = '\n'.join(non_existent_paths)
										raise ValueError(f'''`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}''')
					if all_paths != sorted(all_paths):
										raise ValueError('Files in `utils/documentation_tests.txt` are not in alphabetical order.')





