
import tensorflow as tf

from ...tf_utils import shape_list




class 						lowercase__  (						tf.keras.layers.Layer  ):
	def __init__(			self							:   Tuple       ,   UpperCAmelCase_							:   Optional[int]       ,   UpperCAmelCase_							:   Dict       ,   UpperCAmelCase_							:   List[Any]       ,   UpperCAmelCase_							:   Dict       ,   UpperCAmelCase_							:   Optional[int]=1       ,   UpperCAmelCase_							:   Tuple=False       ,   **UpperCAmelCase_							:   Dict							):
						super().__init__(**__lowerCamelCase							)

						SCREAMING_SNAKE_CASE__													= vocab_size
						SCREAMING_SNAKE_CASE__													= d_embed
						SCREAMING_SNAKE_CASE__													= d_proj

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

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

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

																SCREAMING_SNAKE_CASE__													= self.add_weight(
																    shape=(d_emb_i, self.d_proj)       ,   initializer='zeros'       ,   trainable=__lowerCamelCase       ,   name=F'out_projs_._{i}'							)
																self.out_projs.append(__lowerCamelCase							)
																SCREAMING_SNAKE_CASE__													= self.add_weight(
																    shape=(r_idx - l_idx, d_emb_i)       ,   initializer='zeros'       ,   trainable=__lowerCamelCase       ,   name=F'out_layers_._{i}_._weight'       ,   )
																SCREAMING_SNAKE_CASE__													= 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      A_				(			UpperCAmelCase_							:   List[Any]       ,   UpperCAmelCase_							:   Dict       ,   UpperCAmelCase_							:   Optional[int]       ,   UpperCAmelCase_							:   Optional[Any]=None							):
						SCREAMING_SNAKE_CASE__													= x
						if proj is not None:
											SCREAMING_SNAKE_CASE__													= tf.einsum('ibd,ed->ibe'       ,   __lowerCamelCase       ,   __lowerCamelCase							)
						return tf.einsum('ibd,nd->ibn'       ,   __lowerCamelCase       ,   __lowerCamelCase							) + b
	@staticmethod
	def      A_				(			UpperCAmelCase_							:   List[Any]       ,   UpperCAmelCase_							:   List[Any]							):
						SCREAMING_SNAKE_CASE__													= shape_list(__lowerCamelCase							)
						SCREAMING_SNAKE_CASE__													= tf.range(lp_size[0]       ,   dtype=target.dtype							)
						SCREAMING_SNAKE_CASE__													= tf.stack([r, target]       ,   1							)
						return tf.gather_nd(__lowerCamelCase       ,   __lowerCamelCase							)





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

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

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

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

						if target is not None:
											if return_mean:
																SCREAMING_SNAKE_CASE__													= 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













import re
import string

import numpy as np

import datasets


UpperCAmelCase_ :						Dict      =      '''
Returns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.
'''

UpperCAmelCase_ :						Any      =      '''
Args:
    predictions: List of predicted texts.
    references: List of reference texts.
    regexes_to_ignore: List, defaults to None. Regex expressions of characters to
        ignore when calculating the exact matches. Note: these regexes are removed
        from the input data before the changes based on the options below (e.g. ignore_case,
        ignore_punctuation, ignore_numbers) are applied.
    ignore_case: Boolean, defaults to False. If true, turns everything
        to lowercase so that capitalization differences are ignored.
    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
Returns:
    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.
Examples:
    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results["exact_match"], 1))
    25.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results["exact_match"], 1))
    50.0


    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results["exact_match"], 1))
    75.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)
    >>> print(round(results["exact_match"], 1))
    100.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]
    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results["exact_match"], 1))
    33.3

'''

UpperCAmelCase_ :						Tuple      =      '''
'''

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,   _KWARGS_DESCRIPTION      )
class     _SCREAMING_SNAKE_CASE       (			datasets.Metric      ):





	def 						_A (		self				:					Optional[int]	):
		return datasets.MetricInfo(
		    description=_DESCRIPTION ,  citation=_CITATION ,  inputs_description=_KWARGS_DESCRIPTION ,  features=datasets.Features(
		        {
		            """predictions""": datasets.Value("""string""" ,  id="""sequence"""	),
		            """references""": datasets.Value("""string""" ,  id="""sequence"""	),
		        }	) ,  reference_urls=[] ,  )





	def 						_A (		self				:					Optional[int] ,  __lowerCamelCase				:					Union[str, Any] ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					List[str]=None ,  __lowerCamelCase				:					Optional[int]=False ,  __lowerCamelCase				:					int=False ,  __lowerCamelCase				:					Optional[int]=False ,  ):
		if regexes_to_ignore is not None:
			for s in regexes_to_ignore:
				UpperCamelCase     :str          =	np.array([re.sub(__lowerCamelCase ,  """""" ,  __lowerCamelCase	) for x in predictions]	)
				UpperCamelCase     :Tuple          =	np.array([re.sub(__lowerCamelCase ,  """""" ,  __lowerCamelCase	) for x in references]	)
		else:
			UpperCamelCase     :Any          =	np.asarray(__lowerCamelCase	)
			UpperCamelCase     :str          =	np.asarray(__lowerCamelCase	)

		if ignore_case:
			UpperCamelCase     :Tuple          =	np.char.lower(__lowerCamelCase	)
			UpperCamelCase     :Any          =	np.char.lower(__lowerCamelCase	)

		if ignore_punctuation:
			UpperCamelCase     :Optional[int]          =	string.punctuation.maketrans("""""" ,  """""" ,  string.punctuation	)
			UpperCamelCase     :Optional[Any]          =	np.char.translate(__lowerCamelCase ,  table=__lowerCamelCase	)
			UpperCamelCase     :List[str]          =	np.char.translate(__lowerCamelCase ,  table=__lowerCamelCase	)

		if ignore_numbers:
			UpperCamelCase     :Tuple          =	string.digits.maketrans("""""" ,  """""" ,  string.digits	)
			UpperCamelCase     :Dict          =	np.char.translate(__lowerCamelCase ,  table=__lowerCamelCase	)
			UpperCamelCase     :Tuple          =	np.char.translate(__lowerCamelCase ,  table=__lowerCamelCase	)

		UpperCamelCase     :int          =	predictions == references

		return {"exact_match": np.mean(__lowerCamelCase	) * 100}
