






import tensorflow as tf

from ...tf_utils import shape_list





class 	snake_case__ (							tf.keras.layers.Layer				):
	def __init__(    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__=1      ,       lowerCAmelCase__=False      ,       **lowerCAmelCase__			)							->				str:
							super().__init__(**lowerCAmelCase__			)

							__magic_name__      :       Union[str, Any]       						= vocab_size
							__magic_name__      :       Any       						= d_embed
							__magic_name__      :       List[str]       						= d_proj

							__magic_name__      :       List[str]       						= cutoffs + [vocab_size]
							__magic_name__      :       Any       						= [0] + self.cutoffs
							__magic_name__      :       List[Any]       						= div_val

							__magic_name__      :       List[Any]       						= self.cutoffs[0]
							__magic_name__      :       List[str]       						= len(self.cutoffs			) - 1
							__magic_name__      :       int       						= self.shortlist_size + self.n_clusters
							__magic_name__      :       List[str]       						= keep_order

							__magic_name__      :       int       						= []
							__magic_name__      :       List[Any]       						= []
	def       __magic_name__    (    self      ,       lowerCAmelCase__			)							->				Optional[Any]:
							if self.n_clusters > 0:
													__magic_name__      :       Union[str, Any]       						= self.add_weight(
													    shape=(self.n_clusters, self.d_embed)      ,       initializer="""zeros"""      ,       trainable=lowerCAmelCase__      ,       name="""cluster_weight"""			)
													__magic_name__      :       List[Any]       						= 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:
																									__magic_name__      :       int       						= 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__			)
																			__magic_name__      :       str       						= self.add_weight(
																			    shape=(self.vocab_size, self.d_embed)      ,       initializer="""zeros"""      ,       trainable=lowerCAmelCase__      ,       name=F'out_layers_._{i}_._weight'      ,       )
																			__magic_name__      :       Tuple       						= 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			)			):
																			__magic_name__						,__magic_name__      :       List[str]       						= self.cutoff_ends[i], self.cutoff_ends[i + 1]
																			__magic_name__      :       Optional[Any]       						= self.d_embed // (self.div_val**i)

																			__magic_name__      :       Union[str, Any]       						= self.add_weight(
																			    shape=(d_emb_i, self.d_proj)      ,       initializer="""zeros"""      ,       trainable=lowerCAmelCase__      ,       name=F'out_projs_._{i}'			)
																			self.out_projs.append(lowerCAmelCase__			)
																			__magic_name__      :       List[str]       						= self.add_weight(
																			    shape=(r_idx - l_idx, d_emb_i)      ,       initializer="""zeros"""      ,       trainable=lowerCAmelCase__      ,       name=F'out_layers_._{i}_._weight'      ,       )
																			__magic_name__      :       str       						= 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       __magic_name__    (    lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__=None			)							->				Tuple:
							__magic_name__      :       Dict       						= x
							if proj is not None:
													__magic_name__      :       Optional[Any]       						= tf.einsum("""ibd,ed->ibe"""      ,       lowerCAmelCase__      ,       lowerCAmelCase__			)
							return tf.einsum("""ibd,nd->ibn"""      ,       lowerCAmelCase__      ,       lowerCAmelCase__			) + b
	@staticmethod
	def       __magic_name__    (    lowerCAmelCase__      ,       lowerCAmelCase__			)							->				List[str]:
							__magic_name__      :       Tuple       						= shape_list(lowerCAmelCase__			)
							__magic_name__      :       Tuple       						= tf.range(lp_size[0]      ,       dtype=target.dtype			)
							__magic_name__      :       Optional[Any]       						= tf.stack([r, target]      ,       1			)
							return tf.gather_nd(lowerCAmelCase__      ,       lowerCAmelCase__			)







	def       __magic_name__    (    self      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__=True      ,       lowerCAmelCase__=False			)							->				str:
							__magic_name__      :       Any       						= 0
							if self.n_clusters == 0:
													__magic_name__      :       List[Any]       						= self._logit(lowerCAmelCase__      ,       self.out_layers[0][0]      ,       self.out_layers[0][1]      ,       self.out_projs[0]			)
													if target is not None:
																			__magic_name__      :       str       						= tf.nn.sparse_softmax_cross_entropy_with_logits(labels=lowerCAmelCase__      ,       logits=lowerCAmelCase__			)
													__magic_name__      :       Optional[int]       						= tf.nn.log_softmax(lowerCAmelCase__      ,       axis=-1			)
							else:
													__magic_name__      :       Optional[Any]       						= shape_list(lowerCAmelCase__			)
													__magic_name__      :       List[str]       						= []
													__magic_name__      :       Any       						= tf.zeros(hidden_sizes[:2]			)
													for i in range(len(self.cutoffs			)			):
																			__magic_name__						,__magic_name__      :       List[Any]       						= self.cutoff_ends[i], self.cutoff_ends[i + 1]
																			if target is not None:
																									__magic_name__      :       Any       						= (target >= l_idx) & (target < r_idx)
																									__magic_name__      :       Optional[Any]       						= tf.where(lowerCAmelCase__			)
																									__magic_name__      :       Tuple       						= tf.boolean_mask(lowerCAmelCase__      ,       lowerCAmelCase__			) - l_idx

																			if self.div_val == 1:
																									__magic_name__      :       Any       						= self.out_layers[0][0][l_idx:r_idx]
																									__magic_name__      :       List[str]       						= self.out_layers[0][1][l_idx:r_idx]
																			else:
																									__magic_name__      :       Union[str, Any]       						= self.out_layers[i][0]
																									__magic_name__      :       int       						= self.out_layers[i][1]

																			if i == 0:
																									__magic_name__      :       Union[str, Any]       						= tf.concat([cur_W, self.cluster_weight]      ,       0			)
																									__magic_name__      :       List[Any]       						= tf.concat([cur_b, self.cluster_bias]      ,       0			)

																									__magic_name__      :       Any       						= self._logit(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       self.out_projs[0]			)
																									__magic_name__      :       Optional[Any]       						= tf.nn.log_softmax(lowerCAmelCase__			)
																									out.append(head_logprob[..., : self.cutoffs[0]]			)
																									if target is not None:
																															__magic_name__      :       Dict       						= tf.boolean_mask(lowerCAmelCase__      ,       lowerCAmelCase__			)
																															__magic_name__      :       Tuple       						= self._gather_logprob(lowerCAmelCase__      ,       lowerCAmelCase__			)
																			else:
																									__magic_name__      :       Dict       						= self._logit(lowerCAmelCase__      ,       lowerCAmelCase__      ,       lowerCAmelCase__      ,       self.out_projs[i]			)
																									__magic_name__      :       List[str]       						= tf.nn.log_softmax(lowerCAmelCase__			)
																									__magic_name__      :       Dict       						= self.cutoffs[0] + i - 1  # No probability for the head cluster
																									__magic_name__      :       Optional[int]       						= head_logprob[..., cluster_prob_idx, None] + tail_logprob
																									out.append(lowerCAmelCase__			)
																									if target is not None:
																															__magic_name__      :       int       						= tf.boolean_mask(lowerCAmelCase__      ,       lowerCAmelCase__			)
																															__magic_name__      :       List[Any]       						= tf.boolean_mask(lowerCAmelCase__      ,       lowerCAmelCase__			)
																															__magic_name__      :       Dict       						= 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__			)			)
													__magic_name__      :       List[str]       						= tf.concat(lowerCAmelCase__      ,       axis=-1			)

							if target is not None:
													if return_mean:
																			__magic_name__      :       Dict       						= 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








import os
from pathlib import Path

def    UpperCamelCase						(	):






						"""simple docstring"""







						from torch.utils.cpp_extension import load

						__magic_name__      :       Dict       						= Path(_A			).resolve().parent.parent.parent / """kernels""" / """deformable_detr"""
						__magic_name__      :       Optional[int]       						= [
						    root / filename
						    for filename in [
						        """vision.cpp""",
						        os.path.join("""cpu""",   """ms_deform_attn_cpu.cpp"""			),
						        os.path.join("""cuda""",   """ms_deform_attn_cuda.cu"""			),
						    ]
						]

						load(
						    """MultiScaleDeformableAttention""",   _A,   with_cuda=_A,   extra_include_paths=[str(_A			)],   extra_cflags=["""-DWITH_CUDA=1"""],   extra_cuda_cflags=[
						        """-DCUDA_HAS_FP16=1""",
						        """-D__CUDA_NO_HALF_OPERATORS__""",
						        """-D__CUDA_NO_HALF_CONVERSIONS__""",
						        """-D__CUDA_NO_HALF2_OPERATORS__""",
						    ],   )

						import MultiScaleDeformableAttention as MSDA

						return MSDA
