


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     UpperCamelCase(  unittest.TestCase   ):


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Union[str, Any]						)			->	Tuple:




											'''simple docstring'''

											__snake_case				=		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     ,    )

											__snake_case				=		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

											__snake_case				=		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

											__snake_case				=		tf_top_k_top_p_filtering(SCREAMING_SNAKE_CASE     ,    top_k=1_0     ,    top_p=0.6     ,    min_tokens_to_keep=4						)

											__snake_case				=		output[output != -float("inf"						)]
											__snake_case				=		tf.cast(
											    tf.where(tf.not_equal(SCREAMING_SNAKE_CASE     ,    tf.constant(-float("inf"						)     ,    dtype=tf.floataa						)						)						)     ,    dtype=tf.intaa     ,    )

											tf.debugging.assert_near(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE     ,    rtol=1e-1_2						)
											tf.debugging.assert_equal(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)






@require_tf
class     UpperCamelCase(  unittest.TestCase     ,							_a   ):
					# setting framework_dependent_parameters needs to be gated, just like its contents' imports
					if is_tf_available():
										snake_case_		:				List[Any]										=	{
										    """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 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[int]						)			->	Optional[int]:




											'''simple docstring'''

											__snake_case				=		TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"						)
											__snake_case				=		2
											__snake_case				=		2






											class     UpperCamelCase(  tf.Module   ):


																def __init__(						self	:		Union[str, Any]     ,    SCREAMING_SNAKE_CASE	:		Any						)			->	Dict:




																						'''simple docstring'''

																						super(SCREAMING_SNAKE_CASE     ,    self						).__init__()
																						__snake_case				=		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=SCREAMING_SNAKE_CASE     ,    )
																def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		List[Any]     ,    SCREAMING_SNAKE_CASE	:		Optional[Any]						)			->	Optional[Any]:




																						'''simple docstring'''

																						__snake_case				=		self.model.generate(
																						    input_ids=SCREAMING_SNAKE_CASE     ,    attention_mask=SCREAMING_SNAKE_CASE     ,    max_new_tokens=SCREAMING_SNAKE_CASE     ,    return_dict_in_generate=SCREAMING_SNAKE_CASE     ,    )
																						return {"sequences": outputs["sequences"]}




											__snake_case				=		[[2, 0], [1_0_2, 1_0_3]]
											__snake_case				=		[[1, 0], [1, 1]]
											__snake_case				=		DummyModel(model=SCREAMING_SNAKE_CASE						)
											with tempfile.TemporaryDirectory() as tmp_dir:
																	tf.saved_model.save(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE     ,    signatures={"serving_default": dummy_model.serving}						)
																	__snake_case				=		tf.saved_model.load(SCREAMING_SNAKE_CASE						).signatures["serving_default"]
																	for batch_size in range(1     ,    len(SCREAMING_SNAKE_CASE						) + 1						):
																							__snake_case				=		{
																							    "input_ids": tf.constant(dummy_input_ids[:batch_size]						),
																							    "attention_mask": tf.constant(dummy_attention_masks[:batch_size]						),
																							}
																							__snake_case				=		serving_func(**SCREAMING_SNAKE_CASE						)["sequences"]
																							__snake_case				=		test_model.generate(**SCREAMING_SNAKE_CASE     ,    max_new_tokens=SCREAMING_SNAKE_CASE						)
																							tf.debugging.assert_equal(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)


					@slow
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict						)			->	Optional[Any]:




											'''simple docstring'''

											__snake_case				=		TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"						)
											__snake_case				=		1
											__snake_case				=		2






											class     UpperCamelCase(  tf.Module   ):


																def __init__(						self	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		str						)			->	Optional[Any]:




																						'''simple docstring'''

																						super(SCREAMING_SNAKE_CASE     ,    self						).__init__()
																						__snake_case				=		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=SCREAMING_SNAKE_CASE     ,    )
																def 							SCREAMING_SNAKE_CASE_  (						self	:		int     ,    SCREAMING_SNAKE_CASE	:		int     ,    SCREAMING_SNAKE_CASE	:		Optional[Any]						)			->	List[Any]:




																						'''simple docstring'''

																						__snake_case				=		self.model.generate(
																						    input_ids=SCREAMING_SNAKE_CASE     ,    attention_mask=SCREAMING_SNAKE_CASE     ,    max_new_tokens=SCREAMING_SNAKE_CASE     ,    return_dict_in_generate=SCREAMING_SNAKE_CASE     ,    )
																						return {"sequences": outputs["sequences"]}




											__snake_case				=		[[2], [1_0_2, 1_0_3]]
											__snake_case				=		[[1], [1, 1]]
											__snake_case				=		DummyModel(model=SCREAMING_SNAKE_CASE						)
											with tempfile.TemporaryDirectory() as tmp_dir:
																	tf.saved_model.save(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE     ,    signatures={"serving_default": dummy_model.serving}						)
																	__snake_case				=		tf.saved_model.load(SCREAMING_SNAKE_CASE						).signatures["serving_default"]
																	for input_row in range(len(SCREAMING_SNAKE_CASE						)						):
																							__snake_case				=		{
																							    "input_ids": tf.constant([dummy_input_ids[input_row]]						),
																							    "attention_mask": tf.constant([dummy_attention_masks[input_row]]						),
																							}
																							__snake_case				=		serving_func(**SCREAMING_SNAKE_CASE						)["sequences"]
																							__snake_case				=		test_model.generate(**SCREAMING_SNAKE_CASE     ,    max_new_tokens=SCREAMING_SNAKE_CASE						)
																							tf.debugging.assert_equal(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)


					@slow
					@require_tensorflow_text
					def 							SCREAMING_SNAKE_CASE_  (						self	:		str						)			->	Union[str, Any]:




											'''simple docstring'''

											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=SCREAMING_SNAKE_CASE						)






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


																						def __init__(						self	:		Any						)			->	Optional[int]:




																												'''simple docstring'''

																												super().__init__()
																												__snake_case				=		text.SentencepieceTokenizer(
																												    model=tf.io.gfile.GFile(os.path.join(SCREAMING_SNAKE_CASE     ,    "spiece.model"						)     ,    "rb"						).read()						)
																												__snake_case				=		TFAutoModelForSeqaSeqLM.from_pretrained("hf-internal-testing/tiny-random-t5"						)






																						def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		List[Any]     ,    *SCREAMING_SNAKE_CASE	:		int     ,    **SCREAMING_SNAKE_CASE	:		str						)			->	List[Any]:




																												'''simple docstring'''

																												__snake_case				=		self.tokenizer.tokenize(SCREAMING_SNAKE_CASE						)
																												__snake_case		,					__snake_case				=		text.pad_model_inputs(
																												    SCREAMING_SNAKE_CASE     ,    max_seq_length=6_4     ,    pad_value=self.model.config.pad_token_id						)
																												__snake_case				=		self.model.generate(input_ids=SCREAMING_SNAKE_CASE     ,    attention_mask=SCREAMING_SNAKE_CASE						)
																												return self.tokenizer.detokenize(SCREAMING_SNAKE_CASE						)




																	__snake_case				=		CompleteSentenceTransformer()
																	__snake_case				=		tf.keras.layers.Input(shape=(1,)     ,    dtype=tf.string     ,    name="inputs"						)
																	__snake_case				=		complete_model(SCREAMING_SNAKE_CASE						)
																	__snake_case				=		tf.keras.Model(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)
																	keras_model.save(SCREAMING_SNAKE_CASE						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any						)			->	Dict:




											'''simple docstring'''

											__snake_case				=		{
											    "do_sample": True,
											    "num_beams": 1,
											    "top_p": 0.7,
											    "top_k": 1_0,
											    "temperature": 0.7,
											}
											__snake_case				=		1_4

											__snake_case				=		AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2"						)
											__snake_case				=		"Hello, my dog is cute and"
											__snake_case				=		tokenizer(SCREAMING_SNAKE_CASE     ,    return_tensors="tf"						)
											__snake_case				=		TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"						)

											__snake_case				=		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						)
																	__snake_case				=		model.generate(**SCREAMING_SNAKE_CASE     ,    eos_token_id=SCREAMING_SNAKE_CASE     ,    **SCREAMING_SNAKE_CASE						)
											self.assertTrue(expectation == len(generated_tokens[0]						)						)

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






					def 							SCREAMING_SNAKE_CASE_  (						self	:		int						)			->	int:




											'''simple docstring'''

											__snake_case				=		AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bart"						)
											__snake_case				=		"Hugging Face is a technology company based in New York and Paris."
											__snake_case				=		bart_tokenizer(SCREAMING_SNAKE_CASE     ,    return_tensors="tf"						).input_ids
											__snake_case				=		TFBartForConditionalGeneration.from_pretrained("hf-internal-testing/tiny-random-bart"						)
											__snake_case				=		bart_model.generate(SCREAMING_SNAKE_CASE						).numpy()






											class     UpperCamelCase(  _a   ):


																def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		Tuple=None     ,    **SCREAMING_SNAKE_CASE	:		List[str]						)			->	Tuple:




																						'''simple docstring'''

																						return super().call(SCREAMING_SNAKE_CASE     ,    **SCREAMING_SNAKE_CASE						)




											__snake_case				=		FakeBart.from_pretrained("hf-internal-testing/tiny-random-bart"						)
											__snake_case				=		bart_model.generate(SCREAMING_SNAKE_CASE     ,    foo="bar"						).numpy()
											self.assertTrue(np.array_equal(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)						)






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


																def 							SCREAMING_SNAKE_CASE_  (						self	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		Optional[Any]     ,    **SCREAMING_SNAKE_CASE	:		Any						)			->	List[str]:




																						'''simple docstring'''

																						return super().call(SCREAMING_SNAKE_CASE     ,    **SCREAMING_SNAKE_CASE						)




											__snake_case				=		FakeEncoder(bart_model.config     ,    bart_model.model.shared						)
											__snake_case				=		fake_encoder

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









def 	_lowerCAmelCase       (    _lowerCAmelCase = "The quick brown fox jumps over the lazy dog"      ,	)						->	bool:


						'''simple docstring'''






						__snake_case				=		set()

						# Replace all the whitespace in our sentence
						__snake_case				=		input_str.replace(" "      ,	"" )
						for alpha in input_str:
												if "a" <= alpha.lower() <= "z":
																		frequency.add(alpha.lower() )
						return len(_lowerCAmelCase ) == 26





def 	_lowerCAmelCase       (    _lowerCAmelCase = "The quick brown fox jumps over the lazy dog"      ,	)						->	bool:


						'''simple docstring'''






						__snake_case				=		[False] * 26
						for char in input_str:
												if char.islower():
																		__snake_case				=		True
												elif char.isupper():
																		__snake_case				=		True
						return all(_lowerCAmelCase )





def 	_lowerCAmelCase       (    _lowerCAmelCase = "The quick brown fox jumps over the lazy dog"      ,	)						->	bool:


						'''simple docstring'''






						return len({char for char in input_str.lower() if char.isalpha()} ) == 26





def 	_lowerCAmelCase       (    )						->	None:


						'''simple docstring'''






						from timeit import timeit

						__snake_case				=		"from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest"
						print(timeit("is_pangram()"      ,	setup=_lowerCAmelCase ) )
						print(timeit("is_pangram_faster()"      ,	setup=_lowerCAmelCase ) )
						print(timeit("is_pangram_fastest()"      ,	setup=_lowerCAmelCase ) )
						# 5.348480500048026, 2.6477354579837993, 1.8470395830227062
						# 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	benchmark()





