





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 						lowerCamelCase__     (     UpperCamelCase__			:       Tuple						)			-> Union[str, Any]:



					'''simple docstring'''




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





def 						lowerCamelCase__     (     UpperCamelCase__			:       str       ,    UpperCamelCase__			:       bool = True						)			-> int:



					'''simple docstring'''




					_snake_case									=				(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

					_snake_case									=				is_compiled_module(UpperCamelCase__						)
					if is_compiled:
										_snake_case									=				model
										_snake_case									=				model._orig_mod

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

					while isinstance(UpperCamelCase__       ,    UpperCamelCase__						):
										_snake_case									=				model.module

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

					if is_compiled:
										_snake_case									=				model
										_snake_case									=				compiled_model

					return model





def 						lowerCamelCase__     (     )			-> List[Any]:



					'''simple docstring'''




					PartialState().wait_for_everyone()





def 						lowerCamelCase__     (     UpperCamelCase__			:       Dict       ,    UpperCamelCase__			:       Any						)			-> Optional[Any]:



					'''simple docstring'''




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





@contextmanager
def 						lowerCamelCase__     (     **UpperCamelCase__			:       List[Any]						)			-> Any:



					'''simple docstring'''




					for key, value in kwargs.items():
										_snake_case									=				str(UpperCamelCase__						)

					yield

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





def 						lowerCamelCase__     (     UpperCamelCase__			:       Union[str, Any]						)			-> List[str]:



					'''simple docstring'''




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





def 						lowerCamelCase__     (     UpperCamelCase__			:       str       ,    UpperCamelCase__			:       int						)			-> int:



					'''simple docstring'''




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

					return destination





def 						lowerCamelCase__     (     UpperCamelCase__			:       int = None						)			-> bool:



					'''simple docstring'''




					if port is None:
										_snake_case									=				29_500
					with socket.socket(socket.AF_INET       ,    socket.SOCK_STREAM						) as s:
										return s.connect_ex(('localhost', port)						) == 0










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCAmelCase_      							=     {"""configuration_swin""": ["""SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SwinConfig""", """SwinOnnxConfig"""]}


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					UpperCAmelCase_      							=     [
					    """SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """SwinForImageClassification""",
					    """SwinForMaskedImageModeling""",
					    """SwinModel""",
					    """SwinPreTrainedModel""",
					    """SwinBackbone""",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					UpperCAmelCase_      							=     [
					    """TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """TFSwinForImageClassification""",
					    """TFSwinForMaskedImageModeling""",
					    """TFSwinModel""",
					    """TFSwinPreTrainedModel""",
					]

if TYPE_CHECKING:
					from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_swin import (
										    SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
										    SwinBackbone,
										    SwinForImageClassification,
										    SwinForMaskedImageModeling,
										    SwinModel,
										    SwinPreTrainedModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_swin import (
										    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFSwinForImageClassification,
										    TFSwinForMaskedImageModeling,
										    TFSwinModel,
										    TFSwinPreTrainedModel,
										)

else:
					import sys

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



