
'''simple docstring'''







import copy

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


lowercase    :							List[str]      =					logging.get_logger(__name__)

lowercase    :							str      =					{
    "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 						__UpperCAmelCase      (					_lowerCamelCase						):
							__lowercase      		=       """deformable_detr"""
							__lowercase      		=       {
							    """hidden_size""": """d_model""",
							    """num_attention_heads""": """encoder_attention_heads""",
							}


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





								"""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.'  )
										_snake_case   						=		CONFIG_MAPPING['resnet'](out_features=['stage4']  )
									elif isinstance(lowerCAmelCase_    ,   lowerCAmelCase_  ):
										_snake_case   						=		backbone_config.get('model_type'  )
										_snake_case   						=		CONFIG_MAPPING[backbone_model_type]
										_snake_case   						=		config_class.from_dict(lowerCAmelCase_  )
								_snake_case   						=		use_timm_backbone
								_snake_case   						=		backbone_config
								_snake_case   						=		num_channels
								_snake_case   						=		num_queries
								_snake_case   						=		max_position_embeddings
								_snake_case   						=		d_model
								_snake_case   						=		encoder_ffn_dim
								_snake_case   						=		encoder_layers
								_snake_case   						=		encoder_attention_heads
								_snake_case   						=		decoder_ffn_dim
								_snake_case   						=		decoder_layers
								_snake_case   						=		decoder_attention_heads
								_snake_case   						=		dropout
								_snake_case   						=		attention_dropout
								_snake_case   						=		activation_dropout
								_snake_case   						=		activation_function
								_snake_case   						=		init_std
								_snake_case   						=		init_xavier_std
								_snake_case   						=		encoder_layerdrop
								_snake_case   						=		auxiliary_loss
								_snake_case   						=		position_embedding_type
								_snake_case   						=		backbone
								_snake_case   						=		use_pretrained_backbone
								_snake_case   						=		dilation
								# deformable attributes
								_snake_case   						=		num_feature_levels
								_snake_case   						=		encoder_n_points
								_snake_case   						=		decoder_n_points
								_snake_case   						=		two_stage
								_snake_case   						=		two_stage_num_proposals
								_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
								_snake_case   						=		class_cost
								_snake_case   						=		bbox_cost
								_snake_case   						=		giou_cost
								# Loss coefficients
								_snake_case   						=		mask_loss_coefficient
								_snake_case   						=		dice_loss_coefficient
								_snake_case   						=		bbox_loss_coefficient
								_snake_case   						=		giou_loss_coefficient
								_snake_case   						=		eos_coefficient
								_snake_case   						=		focal_alpha
								_snake_case   						=		disable_custom_kernels
								super().__init__(is_encoder_decoder=lowerCAmelCase_    ,   **lowerCAmelCase_  )


							@property
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								return self.encoder_attention_heads


							@property
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								return self.d_model




							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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



'''simple docstring'''







import tensorflow as tf

from ...tf_utils import shape_list


class 						__UpperCAmelCase      (					tf.keras.layers.Layer						):


							def __init__(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_=1    ,   lowerCAmelCase_=False    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								super().__init__(**lowerCAmelCase_  )

								_snake_case   						=		vocab_size
								_snake_case   						=		d_embed
								_snake_case   						=		d_proj

								_snake_case   						=		cutoffs + [vocab_size]
								_snake_case   						=		[0] + self.cutoffs
								_snake_case   						=		div_val

								_snake_case   						=		self.cutoffs[0]
								_snake_case   						=		len(self.cutoffs  ) - 1
								_snake_case   						=		self.shortlist_size + self.n_clusters
								_snake_case   						=		keep_order

								_snake_case   						=		[]
								_snake_case   						=		[]


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								if self.n_clusters > 0:
									_snake_case   						=		self.add_weight(
									    shape=(self.n_clusters, self.d_embed)    ,   initializer='zeros'    ,   trainable=lowerCAmelCase_    ,   name='cluster_weight'  )
									_snake_case   						=		self.add_weight(
									    shape=(self.n_clusters,)    ,   initializer='zeros'    ,   trainable=lowerCAmelCase_    ,   name='cluster_bias'  )

								if self.div_val == 1:
									for i in range(len(self.cutoffs  )  ):
										if self.d_proj != self.d_embed:
											_snake_case   						=		self.add_weight(
											    shape=(self.d_embed, self.d_proj)    ,   initializer='zeros'    ,   trainable=lowerCAmelCase_    ,   name=F'out_projs_._{i}'    ,   )
											self.out_projs.append(lowerCAmelCase_  )
										else:
											self.out_projs.append(lowerCAmelCase_  )
										_snake_case   						=		self.add_weight(
										    shape=(self.vocab_size, self.d_embed)    ,   initializer='zeros'    ,   trainable=lowerCAmelCase_    ,   name=F'out_layers_._{i}_._weight'    ,   )
										_snake_case   						=		self.add_weight(
										    shape=(self.vocab_size,)    ,   initializer='zeros'    ,   trainable=lowerCAmelCase_    ,   name=F'out_layers_._{i}_._bias'    ,   )
										self.out_layers.append((weight, bias)  )
								else:
									for i in range(len(self.cutoffs  )  ):
										_snake_case						, _snake_case   						=		self.cutoff_ends[i], self.cutoff_ends[i + 1]
										_snake_case   						=		self.d_embed // (self.div_val**i)

										_snake_case   						=		self.add_weight(
										    shape=(d_emb_i, self.d_proj)    ,   initializer='zeros'    ,   trainable=lowerCAmelCase_    ,   name=F'out_projs_._{i}'  )
										self.out_projs.append(lowerCAmelCase_  )
										_snake_case   						=		self.add_weight(
										    shape=(r_idx - l_idx, d_emb_i)    ,   initializer='zeros'    ,   trainable=lowerCAmelCase_    ,   name=F'out_layers_._{i}_._weight'    ,   )
										_snake_case   						=		self.add_weight(
										    shape=(r_idx - l_idx,)    ,   initializer='zeros'    ,   trainable=lowerCAmelCase_    ,   name=F'out_layers_._{i}_._bias'    ,   )
										self.out_layers.append((weight, bias)  )
								super().build(lowerCAmelCase_  )


							@staticmethod
							def  lowerCamelCase				(       lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_=None  ):





								"""simple docstring"""






								_snake_case   						=		x
								if proj is not None:
									_snake_case   						=		tf.einsum('ibd,ed->ibe'    ,   lowerCAmelCase_    ,   lowerCAmelCase_  )
								return tf.einsum('ibd,nd->ibn'    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ) + b


							@staticmethod
							def  lowerCamelCase				(       lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		shape_list(lowerCAmelCase_  )
								_snake_case   						=		tf.range(lp_size[0]    ,   dtype=target.dtype  )
								_snake_case   						=		tf.stack([r, target]    ,   1  )
								return tf.gather_nd(lowerCAmelCase_    ,   lowerCAmelCase_  )




							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=False  ):





								"""simple docstring"""






								_snake_case   						=		0
								if self.n_clusters == 0:
									_snake_case   						=		self._logit(lowerCAmelCase_    ,   self.out_layers[0][0]    ,   self.out_layers[0][1]    ,   self.out_projs[0]  )
									if target is not None:
										_snake_case   						=		tf.nn.sparse_softmax_cross_entropy_with_logits(labels=lowerCAmelCase_    ,   logits=lowerCAmelCase_  )
									_snake_case   						=		tf.nn.log_softmax(lowerCAmelCase_    ,   axis=-1  )
								else:
									_snake_case   						=		shape_list(lowerCAmelCase_  )
									_snake_case   						=		[]
									_snake_case   						=		tf.zeros(hidden_sizes[:2]  )
									for i in range(len(self.cutoffs  )  ):
										_snake_case						, _snake_case   						=		self.cutoff_ends[i], self.cutoff_ends[i + 1]
										if target is not None:
											_snake_case   						=		(target >= l_idx) & (target < r_idx)
											_snake_case   						=		tf.where(lowerCAmelCase_  )
											_snake_case   						=		tf.boolean_mask(lowerCAmelCase_    ,   lowerCAmelCase_  ) - l_idx

										if self.div_val == 1:
											_snake_case   						=		self.out_layers[0][0][l_idx:r_idx]
											_snake_case   						=		self.out_layers[0][1][l_idx:r_idx]
										else:
											_snake_case   						=		self.out_layers[i][0]
											_snake_case   						=		self.out_layers[i][1]

										if i == 0:
											_snake_case   						=		tf.concat([cur_W, self.cluster_weight]    ,   0  )
											_snake_case   						=		tf.concat([cur_b, self.cluster_bias]    ,   0  )

											_snake_case   						=		self._logit(lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   self.out_projs[0]  )
											_snake_case   						=		tf.nn.log_softmax(lowerCAmelCase_  )
											out.append(head_logprob[..., : self.cutoffs[0]]  )
											if target is not None:
												_snake_case   						=		tf.boolean_mask(lowerCAmelCase_    ,   lowerCAmelCase_  )
												_snake_case   						=		self._gather_logprob(lowerCAmelCase_    ,   lowerCAmelCase_  )
										else:
											_snake_case   						=		self._logit(lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   self.out_projs[i]  )
											_snake_case   						=		tf.nn.log_softmax(lowerCAmelCase_  )
											_snake_case   						=		self.cutoffs[0] + i - 1  # No probability for the head cluster
											_snake_case   						=		head_logprob[..., cluster_prob_idx, None] + tail_logprob
											out.append(lowerCAmelCase_  )
											if target is not None:
												_snake_case   						=		tf.boolean_mask(lowerCAmelCase_    ,   lowerCAmelCase_  )
												_snake_case   						=		tf.boolean_mask(lowerCAmelCase_    ,   lowerCAmelCase_  )
												_snake_case   						=		self._gather_logprob(lowerCAmelCase_    ,   lowerCAmelCase_  )
												cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1]
										if target is not None:
											loss += tf.scatter_nd(lowerCAmelCase_    ,   -cur_logprob    ,   shape_list(lowerCAmelCase_  )  )
									_snake_case   						=		tf.concat(lowerCAmelCase_    ,   axis=-1  )

								if target is not None:
									if return_mean:
										_snake_case   						=		tf.reduce_mean(lowerCAmelCase_  )
									# Add the training-time loss value to the layer using `self.add_loss()`.
									self.add_loss(lowerCAmelCase_  )

									# Log the loss as a metric (we could log arbitrary metrics,
									# including different metrics for training and inference.
									self.add_metric(lowerCAmelCase_    ,   name=self.name    ,   aggregation='mean' if return_mean else ''  )

								return out

