






from __future__ import annotations

import os
import tempfile
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import is_tensorflow_text_available, is_tf_available
from transformers.testing_utils import require_tensorflow_text, require_tf, slow

from ..test_modeling_tf_common import floats_tensor
from .test_framework_agnostic import GenerationIntegrationTestsMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import (
		    AutoTokenizer,
		    TFAutoModelForCausalLM,
		    TFAutoModelForSeqaSeqLM,
		    TFAutoModelForSpeechSeqaSeq,
		    TFAutoModelForVisionaSeq,
		    TFBartForConditionalGeneration,
		    TFLogitsProcessorList,
		    TFMinLengthLogitsProcessor,
		    tf_top_k_top_p_filtering,
		)

if is_tensorflow_text_available():
		import tensorflow_text as text




@require_tf
class 				lowerCamelCase__							(       unittest.TestCase):




	'''simple docstring'''




	def      _lowerCamelCase       (      self	:Tuple		)	->				int:
								__UpperCamelCase			:     int         =      tf.convert_to_tensor(
								    [
								        [
								            8.2220991,  # 3rd highest value; idx. 0
								            -0.5620044,
								            5.23229752,
								            4.0386393,
								            -6.8798378,
								            -0.54785802,
								            -3.2012153,
								            2.92777176,
								            1.88171953,
								            7.35341276,  # 5th highest value; idx. 9
								            8.43207833,  # 2nd highest value; idx. 10
								            -9.85711836,
								            -5.96209236,
								            -1.13039161,
								            -7.1115294,
								            -0.8369633,
								            -5.3186408,
								            7.06427407,
								            0.81369344,
								            -0.82023817,
								            -5.9179796,
								            0.58813443,
								            -6.99778438,
								            4.71551189,
								            -0.18771637,
								            7.44020759,  # 4th highest value; idx. 25
								            9.38450987,  # 1st highest value; idx. 26
								            2.12662941,
								            -9.32562038,
								            2.35652522,
								        ],  # cummulative prob of 5 highest values <= 0.6
								        [
								            0.58425518,
								            4.53139238,
								            -5.57510464,
								            -6.28030699,
								            -7.19529503,
								            -4.02122551,
								            1.39337037,
								            -6.06707057,
								            1.59480517,
								            -9.643119,
								            0.03907799,
								            0.67231762,
								            -8.88206726,
								            6.27115922,  # 4th highest value; idx. 13
								            2.28520723,
								            4.82767506,
								            4.30421368,
								            8.8275313,  # 2nd highest value; idx. 17
								            5.44029958,  # 5th highest value; idx. 18
								            -4.4735794,
								            7.38579536,  # 3rd highest value; idx. 20
								            -2.91051663,
								            2.61946077,
								            -2.5674762,
								            -9.48959302,
								            -4.02922645,
								            -1.35416918,
								            9.67702323,  # 1st highest value; idx. 27
								            -5.89478553,
								            1.85370467,
								        ],  # cummulative prob of 5 highest values <= 0.6
								    ]  ,     dtype=tf.floataa  ,     )

								__UpperCamelCase			:     Tuple         =      tf.convert_to_tensor(
								    [[0, 0], [0, 9], [0, 1_0], [0, 2_5], [0, 2_6], [1, 1_3], [1, 1_7], [1, 1_8], [1, 2_0], [1, 2_7]]  ,     dtype=tf.intaa  ,     )  # expected non filtered idx as noted above

								__UpperCamelCase			:     str         =      tf.convert_to_tensor(
								    [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023]  ,     dtype=tf.floataa  ,     )  # expected non filtered values as noted above

								__UpperCamelCase			:     Tuple         =      tf_top_k_top_p_filtering(a  ,     top_k=1_0  ,     top_p=0.6  ,     min_tokens_to_keep=4		)

								__UpperCamelCase			:     Dict         =      output[output != -float("inf"		)]
								__UpperCamelCase			:     int         =      tf.cast(
								    tf.where(tf.not_equal(a  ,     tf.constant(-float("inf"		)  ,     dtype=tf.floataa		)		)		)  ,     dtype=tf.intaa  ,     )

								tf.debugging.assert_near(a  ,     a  ,     rtol=1E-1_2		)
								tf.debugging.assert_equal(a  ,     a		)




@require_tf
class 				lowerCamelCase__							(       unittest.TestCase  ,		__lowercase):




	'''simple docstring'''




	if is_tf_available():
		_A			=  {
		    'AutoModelForCausalLM': TFAutoModelForCausalLM,
		    'AutoModelForSpeechSeq2Seq': TFAutoModelForSpeechSeqaSeq,
		    'AutoModelForSeq2SeqLM': TFAutoModelForSeqaSeqLM,
		    'AutoModelForVision2Seq': TFAutoModelForVisionaSeq,
		    'LogitsProcessorList': TFLogitsProcessorList,
		    'MinLengthLogitsProcessor': TFMinLengthLogitsProcessor,
		    'create_tensor_fn': tf.convert_to_tensor,
		    'floats_tensor': floats_tensor,
		    'return_tensors': 'tf',
		}







	@slow
	def      _lowerCamelCase       (      self	:Tuple		)	->				List[str]:
								# TF-only test: tf.saved_model export
								__UpperCamelCase			:     Any         =      TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"		)
								__UpperCamelCase			:     Union[str, Any]         =      2
								__UpperCamelCase			:     Optional[Any]         =      2




								class 				lowerCamelCase__							(       tf.Module):




									'''simple docstring'''




									def __init__(      self	:List[str]  ,     a	:List[Any]		)	->				Optional[Any]:
																super(a  ,     self		).__init__()
																__UpperCamelCase			:     Optional[int]         =      model







									@tf.function(
									    input_signature=(
									        tf.TensorSpec((None, input_length)  ,     tf.intaa  ,     name="input_ids"		),
									        tf.TensorSpec((None, input_length)  ,     tf.intaa  ,     name="attention_mask"		),
									    )  ,     jit_compile=a  ,     )
									def      _lowerCamelCase       (      self	:Any  ,     a	:List[Any]  ,     a	:Dict		)	->				Optional[Any]:
																__UpperCamelCase			:     str         =      self.model.generate(
																    input_ids=a  ,     attention_mask=a  ,     max_new_tokens=a  ,     return_dict_in_generate=a  ,     )
																return {"sequences": outputs["sequences"]}
								__UpperCamelCase			:     Any         =      [[2, 0], [1_0_2, 1_0_3]]
								__UpperCamelCase			:     Union[str, Any]         =      [[1, 0], [1, 1]]
								__UpperCamelCase			:     Union[str, Any]         =      DummyModel(model=a		)
								with tempfile.TemporaryDirectory() as tmp_dir:
															tf.saved_model.save(a  ,     a  ,     signatures={"serving_default": dummy_model.serving}		)
															__UpperCamelCase			:     Optional[Any]         =      tf.saved_model.load(a		).signatures["serving_default"]
															for batch_size in range(1  ,     len(a		) + 1		):
																						__UpperCamelCase			:     Optional[Any]         =      {
																						    "input_ids": tf.constant(dummy_input_ids[:batch_size]		),
																						    "attention_mask": tf.constant(dummy_attention_masks[:batch_size]		),
																						}
																						__UpperCamelCase			:     Dict         =      serving_func(**a		)["sequences"]
																						__UpperCamelCase			:     Any         =      test_model.generate(**a  ,     max_new_tokens=a		)
																						tf.debugging.assert_equal(a  ,     a		)







	@slow
	def      _lowerCamelCase       (      self	:int		)	->				int:
								# TF-only test: tf.saved_model export
								__UpperCamelCase			:     Tuple         =      TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"		)
								__UpperCamelCase			:     List[Any]         =      1
								__UpperCamelCase			:     int         =      2




								class 				lowerCamelCase__							(       tf.Module):




									'''simple docstring'''




									def __init__(      self	:Tuple  ,     a	:Tuple		)	->				str:
																super(a  ,     self		).__init__()
																__UpperCamelCase			:     int         =      model







									@tf.function(
									    input_signature=(
									        tf.TensorSpec((batch_size, None)  ,     tf.intaa  ,     name="input_ids"		),
									        tf.TensorSpec((batch_size, None)  ,     tf.intaa  ,     name="attention_mask"		),
									    )  ,     jit_compile=a  ,     )
									def      _lowerCamelCase       (      self	:str  ,     a	:Union[str, Any]  ,     a	:List[str]		)	->				int:
																__UpperCamelCase			:     Dict         =      self.model.generate(
																    input_ids=a  ,     attention_mask=a  ,     max_new_tokens=a  ,     return_dict_in_generate=a  ,     )
																return {"sequences": outputs["sequences"]}
								__UpperCamelCase			:     List[Any]         =      [[2], [1_0_2, 1_0_3]]
								__UpperCamelCase			:     Optional[Any]         =      [[1], [1, 1]]
								__UpperCamelCase			:     Any         =      DummyModel(model=a		)
								with tempfile.TemporaryDirectory() as tmp_dir:
															tf.saved_model.save(a  ,     a  ,     signatures={"serving_default": dummy_model.serving}		)
															__UpperCamelCase			:     Any         =      tf.saved_model.load(a		).signatures["serving_default"]
															for input_row in range(len(a		)		):
																						__UpperCamelCase			:     List[str]         =      {
																						    "input_ids": tf.constant([dummy_input_ids[input_row]]		),
																						    "attention_mask": tf.constant([dummy_attention_masks[input_row]]		),
																						}
																						__UpperCamelCase			:     int         =      serving_func(**a		)["sequences"]
																						__UpperCamelCase			:     str         =      test_model.generate(**a  ,     max_new_tokens=a		)
																						tf.debugging.assert_equal(a  ,     a		)







	@slow
	@require_tensorflow_text
	def      _lowerCamelCase       (      self	:Dict		)	->				Any:
								# TF-only test: tf.saved_model export
								with tempfile.TemporaryDirectory() as tmp_dir:
															# file needed to load the TF tokenizer
															hf_hub_download(repo_id="google/flan-t5-small"  ,     filename="spiece.model"  ,     local_dir=a		)




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




																'''simple docstring'''




																def __init__(      self	:Dict		)	->				Dict:
																							super().__init__()
																							__UpperCamelCase			:     Dict         =      text.SentencepieceTokenizer(
																							    model=tf.io.gfile.GFile(os.path.join(a  ,     "spiece.model"		)  ,     "rb"		).read()		)
																							__UpperCamelCase			:     Union[str, Any]         =      TFAutoModelForSeqaSeqLM.from_pretrained("hf-internal-testing/tiny-random-t5"		)







																def      _lowerCamelCase       (      self	:List[Any]  ,     a	:List[Any]  ,     *a	:Optional[Any]  ,     **a	:Tuple		)	->				str:
																							__UpperCamelCase			:     List[str]         =      self.tokenizer.tokenize(a		)
																							__UpperCamelCase     ,		__UpperCamelCase			:     Any         =      text.pad_model_inputs(
																							    a  ,     max_seq_length=6_4  ,     pad_value=self.model.config.pad_token_id		)
																							__UpperCamelCase			:     Tuple         =      self.model.generate(input_ids=a  ,     attention_mask=a		)
																							return self.tokenizer.detokenize(a		)
															__UpperCamelCase			:     str         =      CompleteSentenceTransformer()
															__UpperCamelCase			:     int         =      tf.keras.layers.Input(shape=(1,)  ,     dtype=tf.string  ,     name="inputs"		)
															__UpperCamelCase			:     Dict         =      complete_model(a		)
															__UpperCamelCase			:     Tuple         =      tf.keras.Model(a  ,     a		)
															keras_model.save(a		)







	def      _lowerCamelCase       (      self	:Tuple		)	->				List[Any]:
								# Has PT equivalent: this test relies on random sampling
								__UpperCamelCase			:     Dict         =      {
								    "do_sample": True,
								    "num_beams": 1,
								    "top_p": 0.7,
								    "top_k": 1_0,
								    "temperature": 0.7,
								}
								__UpperCamelCase			:     str         =      1_4

								__UpperCamelCase			:     List[Any]         =      AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2"		)
								__UpperCamelCase			:     Union[str, Any]         =      "Hello, my dog is cute and"
								__UpperCamelCase			:     int         =      tokenizer(a  ,     return_tensors="tf"		)
								__UpperCamelCase			:     Optional[int]         =      TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"		)

								__UpperCamelCase			:     Tuple         =      6_3_8
								# forces the generation to happen on CPU, to avoid GPU-related quirks
								with tf.device(":/CPU:0"		):
															tf.random.set_seed(0		)
															__UpperCamelCase			:     Dict         =      model.generate(**a  ,     eos_token_id=a  ,     **a		)
								self.assertTrue(expectation == len(generated_tokens[0]		)		)

								__UpperCamelCase			:     Dict         =      [6_3_8, 1_9_8]
								with tf.device(":/CPU:0"		):
															tf.random.set_seed(0		)
															__UpperCamelCase			:     List[str]         =      model.generate(**a  ,     eos_token_id=a  ,     **a		)
								self.assertTrue(expectation == len(generated_tokens[0]		)		)







	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				Dict:
								# Has PT equivalent: ample use of framework-specific code
								__UpperCamelCase			:     Optional[Any]         =      AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bart"		)
								__UpperCamelCase			:     Union[str, Any]         =      "Hugging Face is a technology company based in New York and Paris."
								__UpperCamelCase			:     Optional[Any]         =      bart_tokenizer(a  ,     return_tensors="tf"		).input_ids
								__UpperCamelCase			:     Dict         =      TFBartForConditionalGeneration.from_pretrained("hf-internal-testing/tiny-random-bart"		)
								__UpperCamelCase			:     Tuple         =      bart_model.generate(a		).numpy()




								class 				lowerCamelCase__							(       __lowercase):




									'''simple docstring'''




									def      _lowerCamelCase       (      self	:Dict  ,     a	:str  ,     a	:Any=None  ,     **a	:str		)	->				str:
																return super().call(a  ,     **a		)
								__UpperCamelCase			:     List[Any]         =      FakeBart.from_pretrained("hf-internal-testing/tiny-random-bart"		)
								__UpperCamelCase			:     List[str]         =      bart_model.generate(a  ,     foo="bar"		).numpy()
								self.assertTrue(np.array_equal(a  ,     a		)		)




								class 				lowerCamelCase__							(       bart_model.model.encoder.__class__):




									'''simple docstring'''




									def      _lowerCamelCase       (      self	:List[Any]  ,     a	:Union[str, Any]  ,     **a	:str		)	->				int:
																return super().call(a  ,     **a		)
								__UpperCamelCase			:     Optional[Any]         =      FakeEncoder(bart_model.config  ,     bart_model.model.shared		)
								__UpperCamelCase			:     Union[str, Any]         =      fake_encoder

								# Normal generation still works (the output will be different because the encoder weights are different)
								__UpperCamelCase			:     Union[str, Any]         =      bart_model.generate(a		).numpy()
								with self.assertRaises(a		):
															# FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo"
															bart_model.generate(a  ,     foo="bar"		)

import re
import string

import numpy as np

import datasets


lowercase						:      List[str]									= '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n'

lowercase						:      List[str]									= '\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results["exact_match"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    33.3\n\n'

lowercase						:      List[str]									= '\n'




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




	'''simple docstring'''




	def      _lowerCamelCase       (      self	:Dict		)	->				Optional[Any]:
								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      _lowerCamelCase       (      self	:int  ,     a	:Optional[Any]  ,     a	:Dict  ,     a	:Optional[int]=None  ,     a	:int=False  ,     a	:Tuple=False  ,     a	:Optional[int]=False  ,     )	->				Any:
								if regexes_to_ignore is not None:
															for s in regexes_to_ignore:
																						__UpperCamelCase			:     List[Any]         =      np.array([re.sub(a  ,     ""  ,     a		) for x in predictions]		)
																						__UpperCamelCase			:     Optional[Any]         =      np.array([re.sub(a  ,     ""  ,     a		) for x in references]		)
								else:
															__UpperCamelCase			:     Optional[int]         =      np.asarray(a		)
															__UpperCamelCase			:     List[str]         =      np.asarray(a		)

								if ignore_case:
															__UpperCamelCase			:     Optional[int]         =      np.char.lower(a		)
															__UpperCamelCase			:     str         =      np.char.lower(a		)

								if ignore_punctuation:
															__UpperCamelCase			:     Tuple         =      string.punctuation.maketrans(""  ,     ""  ,     string.punctuation		)
															__UpperCamelCase			:     int         =      np.char.translate(a  ,     table=a		)
															__UpperCamelCase			:     str         =      np.char.translate(a  ,     table=a		)

								if ignore_numbers:
															__UpperCamelCase			:     List[str]         =      string.digits.maketrans(""  ,     ""  ,     string.digits		)
															__UpperCamelCase			:     Tuple         =      np.char.translate(a  ,     table=a		)
															__UpperCamelCase			:     Union[str, Any]         =      np.char.translate(a  ,     table=a		)

								__UpperCamelCase			:     List[Any]         =      predictions == references

								return {"exact_match": np.mean(a		) * 1_0_0}