


from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_snake_case  :	Any			      =       logging.get_logger(__name__)

_snake_case  :	str			      =       {
    "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json",
}

class        a	(__SCREAMING_SNAKE_CASE  ,       __SCREAMING_SNAKE_CASE   ):




							"""simple docstring"""


							__UpperCAmelCase					:							Tuple  						=						"resnet"
							__UpperCAmelCase					:							Optional[Any]  						=						["basic", "bottleneck"]

							def __init__(							self :						Optional[int]					,			lowerCamelCase :						List[Any]=3					,			lowerCamelCase :						Union[str, Any]=64					,			lowerCamelCase :						List[Any]=[256, 512, 1024, 2048]					,			lowerCamelCase :						List[Any]=[3, 4, 6, 3]					,			lowerCamelCase :						int="bottleneck"					,			lowerCamelCase :						Optional[int]="relu"					,			lowerCamelCase :						str=False					,			lowerCamelCase :						int=None					,			lowerCamelCase :						Optional[Any]=None					,			**lowerCamelCase :						str					,			)			->					Any:
														super().__init__(**__snake_case       )
														if layer_type not in self.layer_types:
																					raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types       )}'       )
														__snake_case	:    List[str]											=     num_channels
														__snake_case	:    Dict											=     embedding_size
														__snake_case	:    Any											=     hidden_sizes
														__snake_case	:    Union[str, Any]											=     depths
														__snake_case	:    str											=     layer_type
														__snake_case	:    Any											=     hidden_act
														__snake_case	:    Optional[int]											=     downsample_in_first_stage
														__snake_case	:    List[Any]											=     ['''stem'''] + [F'stage{idx}' for idx in range(1					,			len(__snake_case       ) + 1       )]
														__snake_case	:    str											=     get_aligned_output_features_output_indices(
														    out_features=__snake_case					,			out_indices=__snake_case					,			stage_names=self.stage_names       )

class        a	(__SCREAMING_SNAKE_CASE   ):




							"""simple docstring"""


							__UpperCAmelCase					:							List[Any]  						=						version.parse("1.11"   )

							@property
							def 					__snake_case      (							self :						List[Any]       )			->					Optional[int]:
														return OrderedDict(
														    [
														        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
														    ]       )






							@property
							def 					__snake_case      (							self :						str       )			->					Dict:
														return 1E-3





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







def        lowerCAmelCase_  (      __lowerCamelCase							,			__lowerCamelCase = True			):
							__snake_case	:    Optional[Any]											=     (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

							__snake_case	:    Tuple											=     is_compiled_module(__lowerCamelCase			)
							if is_compiled:
														__snake_case	:    Tuple											=     model
														__snake_case	:    List[str]											=     model._orig_mod

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

							while isinstance(__lowerCamelCase							,			__lowerCamelCase			):
														__snake_case	:    List[Any]											=     model.module

							if not keep_fpaa_wrapper:
														__snake_case	:    Tuple											=     getattr(__lowerCamelCase							,			"forward"			)
														__snake_case	:    int											=     model.__dict__.pop("_original_forward"							,			__lowerCamelCase			)
														if original_forward is not None:
																					while hasattr(__lowerCamelCase							,			"__wrapped__"			):
																												__snake_case	:    str											=     forward.__wrapped__
																												if forward == original_forward:
																																			break
																					__snake_case	:    Dict											=     forward
														if getattr(__lowerCamelCase							,			"_converted_to_transformer_engine"							,			__lowerCamelCase			):
																					convert_model(__lowerCamelCase							,			to_transformer_engine=__lowerCamelCase			)

							if is_compiled:
														__snake_case	:    Tuple											=     model
														__snake_case	:    str											=     compiled_model

							return model







def        lowerCAmelCase_  (      ):
							PartialState().wait_for_everyone()







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







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

							yield

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







def        lowerCAmelCase_  (      __lowerCamelCase			):
							if not hasattr(__lowerCamelCase							,			"__qualname__"			) and not hasattr(__lowerCamelCase							,			"__name__"			):
														__snake_case	:    Dict											=     getattr(__lowerCamelCase							,			"__class__"							,			__lowerCamelCase			)
							if hasattr(__lowerCamelCase							,			"__qualname__"			):
														return obj.__qualname__
							if hasattr(__lowerCamelCase							,			"__name__"			):
														return obj.__name__
							return str(__lowerCamelCase			)







def        lowerCAmelCase_  (      __lowerCamelCase							,			__lowerCamelCase			):
							for key, value in source.items():
														if isinstance(__lowerCamelCase							,			__lowerCamelCase			):
																					__snake_case	:    Tuple											=     destination.setdefault(__lowerCamelCase							,			{}			)
																					merge_dicts(__lowerCamelCase							,			__lowerCamelCase			)
														else:
																					__snake_case	:    Union[str, Any]											=     value

							return destination







def        lowerCAmelCase_  (      __lowerCamelCase = None			):
							if port is None:
														__snake_case	:    List[str]											=     2_9_5_0_0
							with socket.socket(socket.AF_INET							,			socket.SOCK_STREAM			) as s:
														return s.connect_ex(("localhost", port)			) == 0

