
'''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 A				(__lowerCamelCase				:Tuple		):
						if is_torch_version("""<"""			,     """2.0.0"""		) or not hasattr(lowerCamelCase__			,     """_dynamo"""		):
												return False
						return isinstance(lowerCamelCase__			,     torch._dynamo.eval_frame.OptimizedModule		)


def A				(__lowerCamelCase				:List[Any]			,     __lowerCamelCase				:int = True		):
						_lowerCAmelCase  						=			(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

						_lowerCAmelCase  						=			is_compiled_module(lowerCamelCase__		)
						if is_compiled:
												_lowerCAmelCase  						=			model
												_lowerCAmelCase  						=			model._orig_mod

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

						while isinstance(lowerCamelCase__			,     lowerCamelCase__		):
												_lowerCAmelCase  						=			model.module

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

						if is_compiled:
												_lowerCAmelCase  						=			model
												_lowerCAmelCase  						=			compiled_model

						return model


def A				():
						PartialState().wait_for_everyone()


def A				(__lowerCamelCase				:List[Any]			,     __lowerCamelCase				:Dict		):
						if PartialState().distributed_type == DistributedType.TPU:
												xm.save(lowerCamelCase__			,     lowerCamelCase__		)
						elif PartialState().local_process_index == 0:
												torch.save(lowerCamelCase__			,     lowerCamelCase__		)


@contextmanager
def A				(**__lowerCamelCase				:Union[str, Any]		):
						for key, value in kwargs.items():
												_lowerCAmelCase  						=			str(lowerCamelCase__		)

						yield

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


def A				(__lowerCamelCase				:List[str]		):
						if not hasattr(lowerCamelCase__			,     """__qualname__"""		) and not hasattr(lowerCamelCase__			,     """__name__"""		):
												_lowerCAmelCase  						=			getattr(lowerCamelCase__			,     """__class__"""			,     lowerCamelCase__		)
						if hasattr(lowerCamelCase__			,     """__qualname__"""		):
												return obj.__qualname__
						if hasattr(lowerCamelCase__			,     """__name__"""		):
												return obj.__name__
						return str(lowerCamelCase__		)


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

						return destination





def A				(__lowerCamelCase				:Optional[int] = None		):
						if port is None:
												_lowerCAmelCase  						=			29500
						with socket.socket(socket.AF_INET			,     socket.SOCK_STREAM		) as s:
												return s.connect_ex(("""localhost""", port)		) == 0


'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


_lowercase    =		{"""configuration_deit""": ["""DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DeiTConfig""", """DeiTOnnxConfig"""]}

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowercase    =		["""DeiTFeatureExtractor"""]
						_lowercase    =		["""DeiTImageProcessor"""]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowercase    =		[
						    """DEIT_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """DeiTForImageClassification""",
						    """DeiTForImageClassificationWithTeacher""",
						    """DeiTForMaskedImageModeling""",
						    """DeiTModel""",
						    """DeiTPreTrainedModel""",
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowercase    =		[
						    """TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """TFDeiTForImageClassification""",
						    """TFDeiTForImageClassificationWithTeacher""",
						    """TFDeiTForMaskedImageModeling""",
						    """TFDeiTModel""",
						    """TFDeiTPreTrainedModel""",
						]


if TYPE_CHECKING:
						from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_deit import DeiTFeatureExtractor
												from .image_processing_deit import DeiTImageProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_deit import (
												    DEIT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    DeiTForImageClassification,
												    DeiTForImageClassificationWithTeacher,
												    DeiTForMaskedImageModeling,
												    DeiTModel,
												    DeiTPreTrainedModel,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_deit import (
												    TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFDeiTForImageClassification,
												    TFDeiTForImageClassificationWithTeacher,
												    TFDeiTForMaskedImageModeling,
												    TFDeiTModel,
												    TFDeiTPreTrainedModel,
												)


else:
						import sys

						_lowercase    =		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
