





"""simple docstring"""


import tensorflow as tf

from ...tf_utils import shape_list






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

			"""simple docstring"""







			def __init__(      self						,   _A						,   _A						,   _A						,   _A						,   _A=1						,   _A=False						,   **_A    ):






									'''simple docstring'''







									super().__init__(**_A    )

									UpperCamelCase				:		List[str]				   =			vocab_size
									UpperCamelCase				:		Optional[Any]				   =			d_embed
									UpperCamelCase				:		List[Any]				   =			d_proj

									UpperCamelCase				:		List[Any]				   =			cutoffs + [vocab_size]
									UpperCamelCase				:		Any				   =			[0] + self.cutoffs
									UpperCamelCase				:		Union[str, Any]				   =			div_val

									UpperCamelCase				:		Dict				   =			self.cutoffs[0]
									UpperCamelCase				:		Any				   =			len(self.cutoffs    ) - 1
									UpperCamelCase				:		Optional[Any]				   =			self.shortlist_size + self.n_clusters
									UpperCamelCase				:		int				   =			keep_order

									UpperCamelCase				:		Any				   =			[]
									UpperCamelCase				:		Optional[int]				   =			[]







			def   _a      (      self						,   _A    ):






									'''simple docstring'''







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

																					UpperCamelCase				:		Any				   =			self.add_weight(
																					    shape=(d_emb_i, self.d_proj)						,   initializer="""zeros"""						,   trainable=_A						,   name=f"""out_projs_._{i}"""    )
																					self.out_projs.append(_A    )
																					UpperCamelCase				:		int				   =			self.add_weight(
																					    shape=(r_idx - l_idx, d_emb_i)						,   initializer="""zeros"""						,   trainable=_A						,   name=f"""out_layers_._{i}_._weight"""						,   )
																					UpperCamelCase				:		Optional[Any]				   =			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						,   _A						,   _A						,   _A=None    ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			x
									if proj is not None:
															UpperCamelCase				:		List[str]				   =			tf.einsum("""ibd,ed->ibe"""						,   _A						,   _A    )
									return tf.einsum("""ibd,nd->ibn"""						,   _A						,   _A    ) + b







			@staticmethod
			def   _a      (      _A						,   _A    ):






									'''simple docstring'''







									UpperCamelCase				:		Optional[Any]				   =			shape_list(_A    )
									UpperCamelCase				:		Any				   =			tf.range(lp_size[0]						,   dtype=target.dtype    )
									UpperCamelCase				:		str				   =			tf.stack([r, target]						,   1    )
									return tf.gather_nd(_A						,   _A    )







			def   _a      (      self						,   _A						,   _A						,   _A=True						,   _A=False    ):






									'''simple docstring'''







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

																					if self.div_val == 1:
																											UpperCamelCase				:		str				   =			self.out_layers[0][0][l_idx:r_idx]
																											UpperCamelCase				:		Union[str, Any]				   =			self.out_layers[0][1][l_idx:r_idx]
																					else:
																											UpperCamelCase				:		Tuple				   =			self.out_layers[i][0]
																											UpperCamelCase				:		Optional[Any]				   =			self.out_layers[i][1]

																					if i == 0:
																											UpperCamelCase				:		str				   =			tf.concat([cur_W, self.cluster_weight]						,   0    )
																											UpperCamelCase				:		List[Any]				   =			tf.concat([cur_b, self.cluster_bias]						,   0    )

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

									if target is not None:
															if return_mean:
																					UpperCamelCase				:		Union[str, Any]				   =			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







'''simple docstring'''
from __future__ import annotations

from decimal import Decimal

from numpy import array


def      a (     __a  )    ->      list[list[float]]:



					'''simple docstring'''






					UpperCamelCase__  :int				   =					Decimal

					# Check if the provided matrix has 2 rows and 2 columns
					# since this implementation only works for 2x2 matrices
					if len(__a  ) == 2 and len(matrix[0]  ) == 2 and len(matrix[1]  ) == 2:
										# Calculate the determinant of the matrix
										UpperCamelCase__  :str				   =					float(
										    d(matrix[0][0]  ) * d(matrix[1][1]  ) - d(matrix[1][0]  ) * d(matrix[0][1]  )  )
										if determinant == 0:
															raise ValueError('''This matrix has no inverse.'''  )

										# Creates a copy of the matrix with swapped positions of the elements
										UpperCamelCase__  :List[Any]				   =					[[0.0, 0.0], [0.0, 0.0]]
										UpperCamelCase__   ,							UpperCamelCase__  :Union[str, Any]				   =					matrix[1][1], matrix[0][0]
										UpperCamelCase__   ,							UpperCamelCase__  :Dict				   =					-matrix[1][0], -matrix[0][1]

										# Calculate the inverse of the matrix
										return [
										    [(float(d(__a  )  ) / determinant) or 0.0 for n in row] for row in swapped_matrix
										]
					elif (
					    len(__a  ) == 3
					    and len(matrix[0]  ) == 3
					    and len(matrix[1]  ) == 3
					    and len(matrix[2]  ) == 3
					):
										# Calculate the determinant of the matrix using Sarrus rule
										UpperCamelCase__  :int				   =					float(
										    (
										        (d(matrix[0][0]  ) * d(matrix[1][1]  ) * d(matrix[2][2]  ))
										        + (d(matrix[0][1]  ) * d(matrix[1][2]  ) * d(matrix[2][0]  ))
										        + (d(matrix[0][2]  ) * d(matrix[1][0]  ) * d(matrix[2][1]  ))
										    )
										    - (
										        (d(matrix[0][2]  ) * d(matrix[1][1]  ) * d(matrix[2][0]  ))
										        + (d(matrix[0][1]  ) * d(matrix[1][0]  ) * d(matrix[2][2]  ))
										        + (d(matrix[0][0]  ) * d(matrix[1][2]  ) * d(matrix[2][1]  ))
										    )  )
										if determinant == 0:
															raise ValueError('''This matrix has no inverse.'''  )

										# Creating cofactor matrix
										UpperCamelCase__  :List[Any]				   =					[
										    [d(0.0  ), d(0.0  ), d(0.0  )],
										    [d(0.0  ), d(0.0  ), d(0.0  )],
										    [d(0.0  ), d(0.0  ), d(0.0  )],
										]
										UpperCamelCase__  :Tuple				   =					(d(matrix[1][1]  ) * d(matrix[2][2]  )) - (
										    d(matrix[1][2]  ) * d(matrix[2][1]  )
										)
										UpperCamelCase__  :List[str]				   =					-(
										    (d(matrix[1][0]  ) * d(matrix[2][2]  )) - (d(matrix[1][2]  ) * d(matrix[2][0]  ))
										)
										UpperCamelCase__  :Union[str, Any]				   =					(d(matrix[1][0]  ) * d(matrix[2][1]  )) - (
										    d(matrix[1][1]  ) * d(matrix[2][0]  )
										)
										UpperCamelCase__  :List[Any]				   =					-(
										    (d(matrix[0][1]  ) * d(matrix[2][2]  )) - (d(matrix[0][2]  ) * d(matrix[2][1]  ))
										)
										UpperCamelCase__  :Optional[int]				   =					(d(matrix[0][0]  ) * d(matrix[2][2]  )) - (
										    d(matrix[0][2]  ) * d(matrix[2][0]  )
										)
										UpperCamelCase__  :List[Any]				   =					-(
										    (d(matrix[0][0]  ) * d(matrix[2][1]  )) - (d(matrix[0][1]  ) * d(matrix[2][0]  ))
										)
										UpperCamelCase__  :List[Any]				   =					(d(matrix[0][1]  ) * d(matrix[1][2]  )) - (
										    d(matrix[0][2]  ) * d(matrix[1][1]  )
										)
										UpperCamelCase__  :List[str]				   =					-(
										    (d(matrix[0][0]  ) * d(matrix[1][2]  )) - (d(matrix[0][2]  ) * d(matrix[1][0]  ))
										)
										UpperCamelCase__  :Dict				   =					(d(matrix[0][0]  ) * d(matrix[1][1]  )) - (
										    d(matrix[0][1]  ) * d(matrix[1][0]  )
										)

										# Transpose the cofactor matrix (Adjoint matrix)
										UpperCamelCase__  :Optional[Any]				   =					array(__a  )
										for i in range(3  ):
															for j in range(3  ):
																				UpperCamelCase__  :Optional[Any]				   =					cofactor_matrix[j][i]

        # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
										UpperCamelCase__  :Any				   =					array(__a  )
										for i in range(3  ):
															for j in range(3  ):
																				inverse_matrix[i][j] /= d(__a  )

        # Calculate the inverse of the matrix
										return [[float(d(__a  )  ) or 0.0 for n in row] for row in inverse_matrix]
					raise ValueError('''Please provide a matrix of size 2x2 or 3x3.'''  )