
"""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 						_snake_case       (	lowercase__		:  Tuple		)  ->				Union[str, Any]:


			'''simple docstring'''





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






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


			'''simple docstring'''





			lowerCAmelCase_    :List[str]			     =  (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

			lowerCAmelCase_    :List[Any]			     =  is_compiled_module(lowercase__		)
			if is_compiled:
						lowerCAmelCase_    :Optional[int]			     =  model
						lowerCAmelCase_    :Optional[int]			     =  model._orig_mod

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

			while isinstance(lowercase__      ,     lowercase__		):
						lowerCAmelCase_    :List[str]			     =  model.module

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

			if is_compiled:
						lowerCAmelCase_    :List[Any]			     =  model
						lowerCAmelCase_    :Optional[int]			     =  compiled_model

			return model






def 						_snake_case       (	)  ->				Union[str, Any]:


			'''simple docstring'''





			PartialState().wait_for_everyone()






def 						_snake_case       (	lowercase__		:  List[str]      ,     lowercase__		:  Dict		)  ->				Union[str, Any]:


			'''simple docstring'''





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






@contextmanager
def 						_snake_case       (	**lowercase__		:  Dict		)  ->				List[str]:


			'''simple docstring'''





			for key, value in kwargs.items():
						lowerCAmelCase_    :Tuple			     =  str(lowercase__		)

			yield

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






def 						_snake_case       (	lowercase__		:  List[str]		)  ->				Any:


			'''simple docstring'''





			if not hasattr(lowercase__      ,     """__qualname__"""		) and not hasattr(lowercase__      ,     """__name__"""		):
						lowerCAmelCase_    :str			     =  getattr(lowercase__      ,     """__class__"""      ,     lowercase__		)
			if hasattr(lowercase__      ,     """__qualname__"""		):
						return obj.__qualname__
			if hasattr(lowercase__      ,     """__name__"""		):
						return obj.__name__
			return str(lowercase__		)






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


			'''simple docstring'''





			for key, value in source.items():
						if isinstance(lowercase__      ,     lowercase__		):
									lowerCAmelCase_    :Tuple			     =  destination.setdefault(lowercase__      ,     {}		)
									merge_dicts(lowercase__      ,     lowercase__		)
						else:
									lowerCAmelCase_    :int			     =  value

			return destination






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


			'''simple docstring'''





			if port is None:
						lowerCAmelCase_    :Optional[Any]			     =  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 re
from pathlib import Path
from unittest import TestCase

import pytest



@pytest.mark.integration
class 			A__   (  __SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		str)				->       Dict:

				"""simple docstring"""

				with open(_SCREAMING_SNAKE_CASE       ,			encoding="utf-8") as input_file:
							__lowerCAmelCase      :  List[str]												=       re.compile(r"(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)")
							__lowerCAmelCase      :  List[Any]												=       input_file.read()
							__lowerCAmelCase      :  Any												=       regexp.search(_SCREAMING_SNAKE_CASE)

				return match

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

				"""simple docstring"""

				with open(_SCREAMING_SNAKE_CASE       ,			encoding="utf-8") as input_file:
							__lowerCAmelCase      :  Any												=       re.compile(r"#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()"       ,			re.DOTALL)
							__lowerCAmelCase      :  Optional[int]												=       input_file.read()
							# use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
							__lowerCAmelCase      :  int												=       regexp.finditer(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  Union[str, Any]												=       [match for match in matches if match is not None and match.group(1) is not None]
				return matches[0] if matches else None

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

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[Any]												=       Path("./datasets")
				__lowerCAmelCase      :  Optional[int]												=       list(dataset_paths.absolute().glob("**/*.py"))

				for dataset in dataset_files:
							if self._no_encoding_on_file_open(str(_SCREAMING_SNAKE_CASE)):
										raise AssertionError(F"""open(...) must use utf-8 encoding in {dataset}""")







	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any])				->       Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase      :  Dict												=       Path("./datasets")
				__lowerCAmelCase      :  Union[str, Any]												=       list(dataset_paths.absolute().glob("**/*.py"))

				for dataset in dataset_files:
							if self._no_print_statements(str(_SCREAMING_SNAKE_CASE)):
										raise AssertionError(F"""print statement found in {dataset}. Use datasets.logger/logging instead.""")