






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






def 			__UpperCamelCase	(		snake_case__   ,   snake_case__ = True					):
	A_		:						Any	    =   (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

	A_		:						str	    =   is_compiled_module(snake_case__					)
	if is_compiled:
		A_		:						Dict	    =   model
		A_		:						int	    =   model._orig_mod

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

	while isinstance(snake_case__   ,   snake_case__					):
		A_		:						Tuple	    =   model.module

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

	if is_compiled:
		A_		:						Any	    =   model
		A_		:						Optional[Any]	    =   compiled_model

	return model






def 			__UpperCamelCase	(		):
	PartialState().wait_for_everyone()






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






@contextmanager
def 			__UpperCamelCase	(		**snake_case__					):
	for key, value in kwargs.items():
		A_		:						Optional[Any]	    =   str(snake_case__					)

	yield

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






def 			__UpperCamelCase	(		snake_case__					):
	if not hasattr(snake_case__   ,   """__qualname__"""					) and not hasattr(snake_case__   ,   """__name__"""					):
		A_		:						Dict	    =   getattr(snake_case__   ,   """__class__"""   ,   snake_case__					)
	if hasattr(snake_case__   ,   """__qualname__"""					):
		return obj.__qualname__
	if hasattr(snake_case__   ,   """__name__"""					):
		return obj.__name__
	return str(snake_case__					)






def 			__UpperCamelCase	(		snake_case__   ,   snake_case__					):
	for key, value in source.items():
		if isinstance(snake_case__   ,   snake_case__					):
			A_		:						Dict	    =   destination.setdefault(snake_case__   ,   {}					)
			merge_dicts(snake_case__   ,   snake_case__					)
		else:
			A_		:						Optional[int]	    =   value

	return destination






def 			__UpperCamelCase	(		snake_case__ = None					):
	if port is None:
		A_		:						Any	    =   29_500
	with socket.socket(socket.AF_INET   ,   socket.SOCK_STREAM					) as s:
		return s.connect_ex(("""localhost""", port)					) == 0









"""simple docstring"""
import copy

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


_lowerCAmelCase         =							logging.get_logger(__name__)

_lowerCAmelCase         =							{
    "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json",
}



class      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE						):





	"""simple docstring"""




	_A       :				List[str]    =				"""deta"""
	_A       :				Tuple    =				{
	    """hidden_size""": """d_model""",
	    """num_attention_heads""": """encoder_attention_heads""",
	}





	def __init__(self     ,     lowerCAmelCase_=None     ,     lowerCAmelCase_=900     ,     lowerCAmelCase_=2048     ,     lowerCAmelCase_=6     ,     lowerCAmelCase_=2048     ,     lowerCAmelCase_=8     ,     lowerCAmelCase_=6     ,     lowerCAmelCase_=1024     ,     lowerCAmelCase_=8     ,     lowerCAmelCase_=0.0     ,     lowerCAmelCase_=True     ,     lowerCAmelCase_="relu"     ,     lowerCAmelCase_=256     ,     lowerCAmelCase_=0.1     ,     lowerCAmelCase_=0.0     ,     lowerCAmelCase_=0.0     ,     lowerCAmelCase_=0.02     ,     lowerCAmelCase_=1.0     ,     lowerCAmelCase_=True     ,     lowerCAmelCase_=False     ,     lowerCAmelCase_="sine"     ,     lowerCAmelCase_=5     ,     lowerCAmelCase_=4     ,     lowerCAmelCase_=4     ,     lowerCAmelCase_=True     ,     lowerCAmelCase_=300     ,     lowerCAmelCase_=True     ,     lowerCAmelCase_=True     ,     lowerCAmelCase_=1     ,     lowerCAmelCase_=5     ,     lowerCAmelCase_=2     ,     lowerCAmelCase_=1     ,     lowerCAmelCase_=1     ,     lowerCAmelCase_=5     ,     lowerCAmelCase_=2     ,     lowerCAmelCase_=0.1     ,     lowerCAmelCase_=0.25     ,     **lowerCAmelCase_     ,     ):
		if backbone_config is None:
			logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""							)
			A_		:						Any	    =   CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""]							)
		else:
			if isinstance(lowerCAmelCase_     ,     lowerCAmelCase_							):
				A_		:						List[str]	    =   backbone_config.pop("""model_type"""							)
				A_		:						List[str]	    =   CONFIG_MAPPING[backbone_model_type]
				A_		:						Tuple	    =   config_class.from_dict(lowerCAmelCase_							)

		A_		:						List[Any]	    =   backbone_config
		A_		:						List[str]	    =   num_queries
		A_		:						List[Any]	    =   max_position_embeddings
		A_		:						int	    =   d_model
		A_		:						Optional[Any]	    =   encoder_ffn_dim
		A_		:						Optional[Any]	    =   encoder_layers
		A_		:						List[Any]	    =   encoder_attention_heads
		A_		:						int	    =   decoder_ffn_dim
		A_		:						Dict	    =   decoder_layers
		A_		:						Dict	    =   decoder_attention_heads
		A_		:						Optional[Any]	    =   dropout
		A_		:						str	    =   attention_dropout
		A_		:						Any	    =   activation_dropout
		A_		:						Union[str, Any]	    =   activation_function
		A_		:						int	    =   init_std
		A_		:						List[Any]	    =   init_xavier_std
		A_		:						List[Any]	    =   encoder_layerdrop
		A_		:						Any	    =   auxiliary_loss
		A_		:						Tuple	    =   position_embedding_type
		# deformable attributes
		A_		:						List[str]	    =   num_feature_levels
		A_		:						Tuple	    =   encoder_n_points
		A_		:						str	    =   decoder_n_points
		A_		:						List[Any]	    =   two_stage
		A_		:						Optional[Any]	    =   two_stage_num_proposals
		A_		:						Union[str, Any]	    =   with_box_refine
		A_		:						Optional[int]	    =   assign_first_stage
		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
		A_		:						List[Any]	    =   class_cost
		A_		:						str	    =   bbox_cost
		A_		:						Tuple	    =   giou_cost
		# Loss coefficients
		A_		:						int	    =   mask_loss_coefficient
		A_		:						Union[str, Any]	    =   dice_loss_coefficient
		A_		:						Union[str, Any]	    =   bbox_loss_coefficient
		A_		:						Optional[Any]	    =   giou_loss_coefficient
		A_		:						List[str]	    =   eos_coefficient
		A_		:						Optional[Any]	    =   focal_alpha
		super().__init__(is_encoder_decoder=lowerCAmelCase_     ,     **lowerCAmelCase_							)





	@property
	def    lowerCamelCase(self							):
		return self.encoder_attention_heads





	@property
	def    lowerCamelCase(self							):
		return self.d_model





	def    lowerCamelCase(self							):
		A_		:						str	    =   copy.deepcopy(self.__dict__							)
		A_		:						int	    =   self.backbone_config.to_dict()
		A_		:						Dict	    =   self.__class__.model_type
		return output

