






"""simple docstring"""

import tensorflow as tf

from ...tf_utils import shape_list

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







				def __init__(							self							:						int						,    __lowerCamelCase							:						Optional[Any]						,    __lowerCamelCase							:						Any						,    __lowerCamelCase							:						int						,    __lowerCamelCase							:						List[str]						,    __lowerCamelCase							:						Optional[Any]=1						,    __lowerCamelCase							:						Optional[int]=False						,    **__lowerCamelCase							:						Dict	)				->					Dict:
										super().__init__(**__lowerCamelCase	)

										__magic_name__            =     vocab_size
										__magic_name__            =     d_embed
										__magic_name__            =     d_proj

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

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

										__magic_name__            =     []
										__magic_name__            =     []







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

																						__magic_name__            =     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__            =     self.add_weight(
																						    shape=(r_idx - l_idx, d_emb_i)						,    initializer="zeros"						,    trainable=__lowerCamelCase						,    name=f'''out_layers_._{i}_._weight'''						,    )
																						__magic_name__            =     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     _snake_case					(							__lowerCamelCase							:						Tuple						,    __lowerCamelCase							:						int						,    __lowerCamelCase							:						List[Any]						,    __lowerCamelCase							:						List[Any]=None	)				->					List[str]:
										__magic_name__            =     x
										if proj is not None:
																__magic_name__            =     tf.einsum("ibd,ed->ibe"						,    __lowerCamelCase						,    __lowerCamelCase	)
										return tf.einsum("ibd,nd->ibn"						,    __lowerCamelCase						,    __lowerCamelCase	) + b







				@staticmethod
				def     _snake_case					(							__lowerCamelCase							:						List[str]						,    __lowerCamelCase							:						str	)				->					Dict:
										__magic_name__            =     shape_list(__lowerCamelCase	)
										__magic_name__            =     tf.range(lp_size[0]						,    dtype=target.dtype	)
										__magic_name__            =     tf.stack([r, target]						,    1	)
										return tf.gather_nd(__lowerCamelCase						,    __lowerCamelCase	)







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

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

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

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

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











"""simple docstring"""

def _lowerCAmelCase	(  __lowerCamelCase:int   ):

						'''simple docstring'''






						__magic_name__            =     n ** (1 / 3)
						return (val * val * val) == n


if __name__ == "__main__":
			print(perfect_cube(27))
			print(perfect_cube(4))



