




import tensorflow as tf

from ...tf_utils import shape_list







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



 '''simple docstring'''


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

       """simple docstring"""






       super().__init__(**A_  )

       lowerCamelCase_							=   vocab_size
       lowerCamelCase_							=   d_embed
       lowerCamelCase_							=   d_proj

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

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

       lowerCamelCase_							=   []
       lowerCamelCase_							=   []

 def     a__			(    self :  Any						,							A_ :  Dict  )       ->     List[Any]:

       """simple docstring"""






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

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

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

 @staticmethod
 def     a__			(    A_ :  Any						,							A_ :  Any						,							A_ :  Optional[int]						,							A_ :  Optional[int]=None  )       ->     List[Any]:

       """simple docstring"""






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

 @staticmethod
 def     a__			(    A_ :  Any						,							A_ :  Any  )       ->     str:

       """simple docstring"""






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





 def     a__			(    self :  Optional[int]						,							A_ :  Dict						,							A_ :  Any						,							A_ :  Any=True						,							A_ :  int=False  )       ->     List[Any]:

       """simple docstring"""






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

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

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

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

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

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

       return out









from ..utils import DummyObject, requires_backends







class   A(						metaclass=UpperCamelCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						['''transformers''', '''torch''', '''note_seq''']

 def __init__(    self :  Any						,							*A_ :  Any						,							**A_ :  Tuple  )       ->     List[Any]:

       """simple docstring"""






       requires_backends(self						,							['transformers', 'torch', 'note_seq']  )

 @classmethod
 def     a__			(    cls :  Tuple						,							*A_ :  Dict						,							**A_ :  Optional[Any]  )       ->     Union[str, Any]:

       """simple docstring"""






       requires_backends(cls						,							['transformers', 'torch', 'note_seq']  )





 @classmethod
 def     a__			(    cls :  int						,							*A_ :  Dict						,							**A_ :  List[str]  )       ->     Tuple:

       """simple docstring"""






       requires_backends(cls						,							['transformers', 'torch', 'note_seq']  )



