


'''simple docstring'''






import tensorflow as tf

from ...tf_utils import shape_list






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






       def __init__(      self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case=1  ,							snake_case=False  ,							**snake_case)						->  Optional[Any]:




          '''simple docstring'''
          super().__init__(**snake_case)

          _UpperCAmelCase       :       Dict							   =vocab_size
          _UpperCAmelCase       :       Any							   =d_embed
          _UpperCAmelCase       :       Optional[Any]							   =d_proj

          _UpperCAmelCase       :       List[str]							   =cutoffs + [vocab_size]
          _UpperCAmelCase       :       Dict							   =[0] + self.cutoffs
          _UpperCAmelCase       :       Optional[int]							   =div_val

          _UpperCAmelCase       :       List[Any]							   =self.cutoffs[0]
          _UpperCAmelCase       :       Any							   =len(self.cutoffs) - 1
          _UpperCAmelCase       :       Tuple							   =self.shortlist_size + self.n_clusters
          _UpperCAmelCase       :       Union[str, Any]							   =keep_order

          _UpperCAmelCase       :       List[Any]							   =[]
          _UpperCAmelCase       :       Optional[int]							   =[]






       def       lowerCAmelCase		(      self  ,							snake_case)						->  Tuple:




          '''simple docstring'''
          if self.n_clusters > 0:
             _UpperCAmelCase       :       List[Any]							   =self.add_weight(
                 shape=(self.n_clusters, self.d_embed)  ,							initializer='zeros'  ,							trainable=snake_case  ,							name='cluster_weight')
             _UpperCAmelCase       :       Optional[int]							   =self.add_weight(
                 shape=(self.n_clusters,)  ,							initializer='zeros'  ,							trainable=snake_case  ,							name='cluster_bias')

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

                _UpperCAmelCase       :       Optional[int]							   =self.add_weight(
                    shape=(d_emb_i, self.d_proj)  ,							initializer='zeros'  ,							trainable=snake_case  ,							name=f"out_projs_._{i}")
                self.out_projs.append(snake_case)
                _UpperCAmelCase       :       Union[str, Any]							   =self.add_weight(
                    shape=(r_idx - l_idx, d_emb_i)  ,							initializer='zeros'  ,							trainable=snake_case  ,							name=f"out_layers_._{i}_._weight"  ,							)
                _UpperCAmelCase       :       str							   =self.add_weight(
                    shape=(r_idx - l_idx,)  ,							initializer='zeros'  ,							trainable=snake_case  ,							name=f"out_layers_._{i}_._bias"  ,							)
                self.out_layers.append((weight, bias))
          super().build(snake_case)






       @staticmethod
       def       lowerCAmelCase		(      snake_case  ,							snake_case  ,							snake_case  ,							snake_case=None)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       Tuple							   =x
          if proj is not None:
             _UpperCAmelCase       :       Dict							   =tf.einsum('ibd,ed->ibe'  ,							snake_case  ,							snake_case)
          return tf.einsum('ibd,nd->ibn'  ,							snake_case  ,							snake_case) + b






       @staticmethod
       def       lowerCAmelCase		(      snake_case  ,							snake_case)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =shape_list(snake_case)
          _UpperCAmelCase       :       Union[str, Any]							   =tf.range(lp_size[0]  ,							dtype=target.dtype)
          _UpperCAmelCase       :       List[Any]							   =tf.stack([r, target]  ,							1)
          return tf.gather_nd(snake_case  ,							snake_case)






       def       lowerCAmelCase		(      self  ,							snake_case  ,							snake_case  ,							snake_case=True  ,							snake_case=False)						->  Optional[int]:




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

                if self.div_val == 1:
                   _UpperCAmelCase       :       Tuple							   =self.out_layers[0][0][l_idx:r_idx]
                   _UpperCAmelCase       :       str							   =self.out_layers[0][1][l_idx:r_idx]
                else:
                   _UpperCAmelCase       :       Optional[int]							   =self.out_layers[i][0]
                   _UpperCAmelCase       :       str							   =self.out_layers[i][1]

                if i == 0:
                   _UpperCAmelCase       :       Tuple							   =tf.concat([cur_W, self.cluster_weight]  ,							0)
                   _UpperCAmelCase       :       Optional[Any]							   =tf.concat([cur_b, self.cluster_bias]  ,							0)

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

          if target is not None:
             if return_mean:
                _UpperCAmelCase       :       Optional[int]							   =tf.reduce_mean(snake_case)
             # Add the training-time loss value to the layer using `self.add_loss()`.
             self.add_loss(snake_case)

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

          return out





'''simple docstring'''






from string import ascii_uppercase

lowercase							={str(ord(c) - 55): c for c in ascii_uppercase}





def lowerCamelCase__      (						__lowerCamelCase	:							int	,   __lowerCamelCase	:							int							):







   '''simple docstring'''




   if isinstance(__lowerCamelCase	,   __lowerCamelCase							):
      raise TypeError('int() can\'t convert non-string with explicit base'							)
   if num < 0:
      raise ValueError('parameter must be positive int'							)
   if isinstance(__lowerCamelCase	,   __lowerCamelCase							):
      raise TypeError('\'str\' object cannot be interpreted as an integer'							)
   if isinstance(__lowerCamelCase	,   __lowerCamelCase							):
      raise TypeError('\'float\' object cannot be interpreted as an integer'							)
   if base in (0, 1):
      raise ValueError('base must be >= 2'							)
   if base > 3_6:
      raise ValueError('base must be <= 36'							)
   _UpperCAmelCase       :       Union[str, Any]							   =''
   _UpperCAmelCase       :       Optional[int]							   =0
   _UpperCAmelCase       :       str							   =0
   while div != 1:
      _UpperCAmelCase   ,	_UpperCAmelCase       :       int							   =divmod(__lowerCamelCase	,   __lowerCamelCase							)
      if base >= 1_1 and 9 < mod < 3_6:
         _UpperCAmelCase       :       str							   =ALPHABET_VALUES[str(__lowerCamelCase							)]
      else:
         _UpperCAmelCase       :       Any							   =str(__lowerCamelCase							)
      new_value += actual_value
      _UpperCAmelCase       :       Union[str, Any]							   =num // base
      _UpperCAmelCase       :       Dict							   =div
      if div == 0:
         return str(new_value[::-1]							)
      elif div == 1:
         new_value += str(__lowerCamelCase							)
         return str(new_value[::-1]							)

   return new_value[::-1]


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 for base in range(2, 37):
  for num in range(1000):
   assert int(decimal_to_any(num, base), base) == num, (
       num,
       base,
       decimal_to_any(num, base),
       int(decimal_to_any(num, base), base),
   )

