



import tensorflow as tf

from ...tf_utils import shape_list







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







						"""simple docstring"""




						def __init__(       self ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase=1 ,       __lowercase=False ,       **__lowercase  ) ->				str:
									"""simple docstring"""




									super().__init__(**__lowerCamelCase  )

									a__       :						Tuple							=  vocab_size
									a__       :						Tuple							=  d_embed
									a__       :						Any							=  d_proj

									a__       :						Optional[int]							=  cutoffs + [vocab_size]
									a__       :						Optional[int]							=  [0] + self.cutoffs
									a__       :						Union[str, Any]							=  div_val

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

									a__       :						Dict							=  []
									a__       :						Dict							=  []

						def 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase  ) ->				str:
									"""simple docstring"""




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

															a__       :						Tuple							=  self.add_weight(
															    shape=(d_emb_i, self.d_proj) ,       initializer="""zeros""" ,       trainable=__lowerCamelCase ,       name=F'''out_projs_._{i}'''  )
															self.out_projs.append(__lowerCamelCase  )
															a__       :						Optional[int]							=  self.add_weight(
															    shape=(r_idx - l_idx, d_emb_i) ,       initializer="""zeros""" ,       trainable=__lowerCamelCase ,       name=F'''out_layers_._{i}_._weight''' ,       )
															a__       :						Union[str, Any]							=  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 							SCREAMING_SNAKE_CASE__(       __lowercase ,       __lowercase ,       __lowercase ,       __lowercase=None  ) ->				Union[str, Any]:
									"""simple docstring"""




									a__       :						Optional[Any]							=  x
									if proj is not None:
												a__       :						Tuple							=  tf.einsum("""ibd,ed->ibe""" ,       __lowerCamelCase ,       __lowerCamelCase  )
									return tf.einsum("""ibd,nd->ibn""" ,       __lowerCamelCase ,       __lowerCamelCase  ) + b

						@staticmethod
						def 							SCREAMING_SNAKE_CASE__(       __lowercase ,       __lowercase  ) ->				List[Any]:
									"""simple docstring"""




									a__       :						Dict							=  shape_list(__lowerCamelCase  )
									a__       :						Dict							=  tf.range(lp_size[0] ,       dtype=target.dtype  )
									a__       :						Optional[Any]							=  tf.stack([r, target] ,       1  )
									return tf.gather_nd(__lowerCamelCase ,       __lowerCamelCase  )

						def 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase=True ,       __lowercase=False  ) ->				Optional[Any]:
									"""simple docstring"""




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

															if self.div_val == 1:
																		a__       :						Optional[Any]							=  self.out_layers[0][0][l_idx:r_idx]
																		a__       :						Optional[int]							=  self.out_layers[0][1][l_idx:r_idx]
															else:
																		a__       :						Optional[int]							=  self.out_layers[i][0]
																		a__       :						int							=  self.out_layers[i][1]

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

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

									if target is not None:
												if return_mean:
															a__       :						List[Any]							=  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









from __future__ import annotations

def      lowerCAmelCase_					(  _lowercase    :       float						,       _lowercase    :       float						,       _lowercase    :       float						,       )							->	tuple[str, float]:







			"""simple docstring"""
			if (stress, tangential_force, area).count(0) != 1:
						raise ValueError("""You cannot supply more or less than 2 values""")
			elif stress < 0:
						raise ValueError("""Stress cannot be negative""")
			elif tangential_force < 0:
						raise ValueError("""Tangential Force cannot be negative""")
			elif area < 0:
						raise ValueError("""Area cannot be negative""")
			elif stress == 0:
						return (
						    "stress",
						    tangential_force / area,
						)
			elif tangential_force == 0:
						return (
						    "tangential_force",
						    stress * area,
						)
			else:
						return (
						    "area",
						    tangential_force / stress,
						)


if __name__ == "__main__":
		import doctest

		doctest.testmod()




