
"""simple docstring"""




import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


lowerCAmelCase__							=				logging.get_logger(__name__)

lowerCAmelCase__							=				{
    'SenseTime/deformable-detr': 'https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json',
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}





class  snake_case     (							__lowercase   ):
							UpperCAmelCase__						     =      '''deformable_detr'''
							UpperCAmelCase__						     =      {
							    '''hidden_size''': '''d_model''',
							    '''num_attention_heads''': '''encoder_attention_heads''',
							}

							def __init__(self       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=None       ,       SCREAMING_SNAKE_CASE_=3       ,       SCREAMING_SNAKE_CASE_=3_00       ,       SCREAMING_SNAKE_CASE_=10_24       ,       SCREAMING_SNAKE_CASE_=6       ,       SCREAMING_SNAKE_CASE_=10_24       ,       SCREAMING_SNAKE_CASE_=8       ,       SCREAMING_SNAKE_CASE_=6       ,       SCREAMING_SNAKE_CASE_=10_24       ,       SCREAMING_SNAKE_CASE_=8       ,       SCREAMING_SNAKE_CASE_=0.0       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_="relu"       ,       SCREAMING_SNAKE_CASE_=2_56       ,       SCREAMING_SNAKE_CASE_=0.1       ,       SCREAMING_SNAKE_CASE_=0.0       ,       SCREAMING_SNAKE_CASE_=0.0       ,       SCREAMING_SNAKE_CASE_=0.02       ,       SCREAMING_SNAKE_CASE_=1.0       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=False       ,       SCREAMING_SNAKE_CASE_="sine"       ,       SCREAMING_SNAKE_CASE_="resnet50"       ,       SCREAMING_SNAKE_CASE_=True       ,       SCREAMING_SNAKE_CASE_=False       ,       SCREAMING_SNAKE_CASE_=4       ,       SCREAMING_SNAKE_CASE_=4       ,       SCREAMING_SNAKE_CASE_=4       ,       SCREAMING_SNAKE_CASE_=False       ,       SCREAMING_SNAKE_CASE_=3_00       ,       SCREAMING_SNAKE_CASE_=False       ,       SCREAMING_SNAKE_CASE_=1       ,       SCREAMING_SNAKE_CASE_=5       ,       SCREAMING_SNAKE_CASE_=2       ,       SCREAMING_SNAKE_CASE_=1       ,       SCREAMING_SNAKE_CASE_=1       ,       SCREAMING_SNAKE_CASE_=5       ,       SCREAMING_SNAKE_CASE_=2       ,       SCREAMING_SNAKE_CASE_=0.1       ,       SCREAMING_SNAKE_CASE_=0.25       ,       SCREAMING_SNAKE_CASE_=False       ,       **SCREAMING_SNAKE_CASE_       ,       ):

									"""simple docstring"""







									if backbone_config is not None and use_timm_backbone:
											raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.'''    )

									if not use_timm_backbone:
											if backbone_config is None:
													logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'''    )
													SCREAMING_SNAKE_CASE_						 = CONFIG_MAPPING['''resnet'''](out_features=['''stage4''']    )
											elif isinstance(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    ):
													SCREAMING_SNAKE_CASE_						 = backbone_config.get('''model_type'''    )
													SCREAMING_SNAKE_CASE_						 = CONFIG_MAPPING[backbone_model_type]
													SCREAMING_SNAKE_CASE_						 = config_class.from_dict(SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = use_timm_backbone
									SCREAMING_SNAKE_CASE_						 = backbone_config
									SCREAMING_SNAKE_CASE_						 = num_channels
									SCREAMING_SNAKE_CASE_						 = num_queries
									SCREAMING_SNAKE_CASE_						 = max_position_embeddings
									SCREAMING_SNAKE_CASE_						 = d_model
									SCREAMING_SNAKE_CASE_						 = encoder_ffn_dim
									SCREAMING_SNAKE_CASE_						 = encoder_layers
									SCREAMING_SNAKE_CASE_						 = encoder_attention_heads
									SCREAMING_SNAKE_CASE_						 = decoder_ffn_dim
									SCREAMING_SNAKE_CASE_						 = decoder_layers
									SCREAMING_SNAKE_CASE_						 = decoder_attention_heads
									SCREAMING_SNAKE_CASE_						 = dropout
									SCREAMING_SNAKE_CASE_						 = attention_dropout
									SCREAMING_SNAKE_CASE_						 = activation_dropout
									SCREAMING_SNAKE_CASE_						 = activation_function
									SCREAMING_SNAKE_CASE_						 = init_std
									SCREAMING_SNAKE_CASE_						 = init_xavier_std
									SCREAMING_SNAKE_CASE_						 = encoder_layerdrop
									SCREAMING_SNAKE_CASE_						 = auxiliary_loss
									SCREAMING_SNAKE_CASE_						 = position_embedding_type
									SCREAMING_SNAKE_CASE_						 = backbone
									SCREAMING_SNAKE_CASE_						 = use_pretrained_backbone
									SCREAMING_SNAKE_CASE_						 = dilation
									# deformable attributes
									SCREAMING_SNAKE_CASE_						 = num_feature_levels
									SCREAMING_SNAKE_CASE_						 = encoder_n_points
									SCREAMING_SNAKE_CASE_						 = decoder_n_points
									SCREAMING_SNAKE_CASE_						 = two_stage
									SCREAMING_SNAKE_CASE_						 = two_stage_num_proposals
									SCREAMING_SNAKE_CASE_						 = with_box_refine
									if two_stage is True and with_box_refine is False:
											raise ValueError('''If two_stage is True, with_box_refine must be True.'''    )
									# Hungarian matcher
									SCREAMING_SNAKE_CASE_						 = class_cost
									SCREAMING_SNAKE_CASE_						 = bbox_cost
									SCREAMING_SNAKE_CASE_						 = giou_cost
									# Loss coefficients
									SCREAMING_SNAKE_CASE_						 = mask_loss_coefficient
									SCREAMING_SNAKE_CASE_						 = dice_loss_coefficient
									SCREAMING_SNAKE_CASE_						 = bbox_loss_coefficient
									SCREAMING_SNAKE_CASE_						 = giou_loss_coefficient
									SCREAMING_SNAKE_CASE_						 = eos_coefficient
									SCREAMING_SNAKE_CASE_						 = focal_alpha
									SCREAMING_SNAKE_CASE_						 = disable_custom_kernels
									super().__init__(is_encoder_decoder=SCREAMING_SNAKE_CASE_       ,       **SCREAMING_SNAKE_CASE_    )

							@property
							def _lowercase					(self    ):

									"""simple docstring"""







									return self.encoder_attention_heads

							@property
							def _lowercase					(self    ):

									"""simple docstring"""







									return self.d_model







							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = copy.deepcopy(self.__dict__    )
									if self.backbone_config is not None:
											SCREAMING_SNAKE_CASE_						 = self.backbone_config.to_dict()
									SCREAMING_SNAKE_CASE_						 = self.__class__.model_type
									return output

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



def        _lowerCamelCase ( __a,							__a = True    ):
		SCREAMING_SNAKE_CASE_						 = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

		SCREAMING_SNAKE_CASE_						 = is_compiled_module(__a    )
		if is_compiled:
				SCREAMING_SNAKE_CASE_						 = model
				SCREAMING_SNAKE_CASE_						 = model._orig_mod

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

		while isinstance(__a,							__a    ):
				SCREAMING_SNAKE_CASE_						 = model.module

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

		if is_compiled:
				SCREAMING_SNAKE_CASE_						 = model
				SCREAMING_SNAKE_CASE_						 = compiled_model

		return model



def        _lowerCamelCase ( ):
		PartialState().wait_for_everyone()



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



@contextmanager
def        _lowerCamelCase ( **__a    ):
		for key, value in kwargs.items():
				SCREAMING_SNAKE_CASE_						 = str(__a    )

		yield

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



def        _lowerCamelCase ( __a    ):
		if not hasattr(__a,							'''__qualname__'''    ) and not hasattr(__a,							'''__name__'''    ):
				SCREAMING_SNAKE_CASE_						 = getattr(__a,							'''__class__''',							__a    )
		if hasattr(__a,							'''__qualname__'''    ):
				return obj.__qualname__
		if hasattr(__a,							'''__name__'''    ):
				return obj.__name__
		return str(__a    )



def        _lowerCamelCase ( __a,							__a    ):
		for key, value in source.items():
				if isinstance(__a,							__a    ):
						SCREAMING_SNAKE_CASE_						 = destination.setdefault(__a,							{}    )
						merge_dicts(__a,							__a    )
				else:
						SCREAMING_SNAKE_CASE_						 = value

		return destination



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