






import tensorflow as tf

from ...tf_utils import shape_list


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






  """simple docstring"""





  def __init__(  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase=1		,		_UpperCAmelCase=False		,		**_UpperCAmelCase			)      ->			str:
     super().__init__(**_UpperCAmelCase			)

     UpperCamelCase_						=  vocab_size
     UpperCamelCase_						=  d_embed
     UpperCamelCase_						=  d_proj

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

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

     UpperCamelCase_						=  []
     UpperCamelCase_						=  []




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase			)      ->			Optional[int]:
     if self.n_clusters > 0:
        UpperCamelCase_						=  self.add_weight(
            shape=(self.n_clusters, self.d_embed)		,		initializer='zeros'		,		trainable=_UpperCAmelCase		,		name='cluster_weight'			)
        UpperCamelCase_						=  self.add_weight(
            shape=(self.n_clusters,)		,		initializer='zeros'		,		trainable=_UpperCAmelCase		,		name='cluster_bias'			)

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

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




  @staticmethod
  def  _UpperCAmelCase  (  _UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase=None			)      ->			Dict:
     UpperCamelCase_						=  x
     if proj is not None:
        UpperCamelCase_						=  tf.einsum('ibd,ed->ibe'		,		_UpperCAmelCase		,		_UpperCAmelCase			)
     return tf.einsum('ibd,nd->ibn'		,		_UpperCAmelCase		,		_UpperCAmelCase			) + b




  @staticmethod
  def  _UpperCAmelCase  (  _UpperCAmelCase		,		_UpperCAmelCase			)      ->			Union[str, Any]:
     UpperCamelCase_						=  shape_list(_UpperCAmelCase			)
     UpperCamelCase_						=  tf.range(lp_size[0]		,		dtype=target.dtype			)
     UpperCamelCase_						=  tf.stack([r, target]		,		1			)
     return tf.gather_nd(_UpperCAmelCase		,		_UpperCAmelCase			)




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

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

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

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

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

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

     return out













import requests






def      _snake_case     (__lowercase     ,   __lowercase):
   UpperCamelCase_						=  {'Content-Type': 'application/json'}
   UpperCamelCase_						=  requests.post(__lowercase     ,   json={'text': message_body}     ,   headers=__lowercase)
   if response.status_code != 200:
      UpperCamelCase_						=  (
          'Request to slack returned an error '
          f"""{response.status_code}, the response is:\n{response.text}"""
      )
      raise ValueError(__lowercase)


if __name__ == "__main__":
 # Set the slack url to the one provided by Slack when you create the webhook at
 # https://my.slack.com/services/new/incoming-webhook/
 send_slack_message("""<YOUR MESSAGE BODY>""", """<SLACK CHANNEL URL>""")





