





'''simple docstring'''






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							):



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Any       						= 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		,   )

			lowerCAmelCase  :     Dict       						= tf.convert_to_tensor(
			    [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]]		,   dtype=tf.intaa		,   )  # expected non filtered idx as noted above

			lowerCAmelCase  :     Any       						= 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

			lowerCAmelCase  :     Optional[int]       						= tf_top_k_top_p_filtering(a_		,   top_k=10		,   top_p=0.6		,   min_tokens_to_keep=4						)

			lowerCAmelCase  :     List[str]       						= output[output != -float("inf"						)]
			lowerCAmelCase  :     Dict       						= 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   , _A							):
	# setting framework_dependent_parameters needs to be gated, just like its contents' imports
	if is_tf_available():
		snake_case_   = {
		    "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						):
			# TF-only test: tf.saved_model export
			lowerCAmelCase  :     Any       						= TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"						)
			lowerCAmelCase  :     Any       						= 2
			lowerCAmelCase  :     Optional[int]       						= 2
			class 		lowerCamelCase	(   tf.Module							):



				def __init__( self		,   a_						):
						super(a_		,   self						).__init__()
						lowerCAmelCase  :     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		,   a_		,   a_						):
						lowerCAmelCase  :     Optional[Any]       						= self.model.generate(
						    input_ids=a_		,   attention_mask=a_		,   max_new_tokens=a_		,   return_dict_in_generate=a_		,   )
						return {"sequences": outputs["sequences"]}






			lowerCAmelCase  :     Optional[int]       						= [[2, 0], [102, 103]]
			lowerCAmelCase  :     str       						= [[1, 0], [1, 1]]
			lowerCAmelCase  :     Dict       						= DummyModel(model=a_						)
			with tempfile.TemporaryDirectory() as tmp_dir:
					tf.saved_model.save(a_		,   a_		,   signatures={"serving_default": dummy_model.serving}						)
					lowerCAmelCase  :     List[Any]       						= tf.saved_model.load(a_						).signatures["serving_default"]
					for batch_size in range(1		,   len(a_						) + 1						):
							lowerCAmelCase  :     Optional[Any]       						= {
							    "input_ids": tf.constant(dummy_input_ids[:batch_size]						),
							    "attention_mask": tf.constant(dummy_attention_masks[:batch_size]						),
							}
							lowerCAmelCase  :     Optional[Any]       						= serving_func(**a_						)["sequences"]
							lowerCAmelCase  :     Optional[int]       						= test_model.generate(**a_		,   max_new_tokens=a_						)
							tf.debugging.assert_equal(a_		,   a_						)



	@slow
	def 	_lowerCamelCase     ( self						):
			# TF-only test: tf.saved_model export
			lowerCAmelCase  :     Dict       						= TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"						)
			lowerCAmelCase  :     Tuple       						= 1
			lowerCAmelCase  :     Tuple       						= 2
			class 		lowerCamelCase	(   tf.Module							):



				def __init__( self		,   a_						):
						super(a_		,   self						).__init__()
						lowerCAmelCase  :     Dict       						= 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		,   a_		,   a_						):
						lowerCAmelCase  :     str       						= self.model.generate(
						    input_ids=a_		,   attention_mask=a_		,   max_new_tokens=a_		,   return_dict_in_generate=a_		,   )
						return {"sequences": outputs["sequences"]}






			lowerCAmelCase  :     List[Any]       						= [[2], [102, 103]]
			lowerCAmelCase  :     Union[str, Any]       						= [[1], [1, 1]]
			lowerCAmelCase  :     int       						= DummyModel(model=a_						)
			with tempfile.TemporaryDirectory() as tmp_dir:
					tf.saved_model.save(a_		,   a_		,   signatures={"serving_default": dummy_model.serving}						)
					lowerCAmelCase  :     Optional[int]       						= tf.saved_model.load(a_						).signatures["serving_default"]
					for input_row in range(len(a_						)						):
							lowerCAmelCase  :     Dict       						= {
							    "input_ids": tf.constant([dummy_input_ids[input_row]]						),
							    "attention_mask": tf.constant([dummy_attention_masks[input_row]]						),
							}
							lowerCAmelCase  :     Optional[int]       						= serving_func(**a_						)["sequences"]
							lowerCAmelCase  :     Tuple       						= test_model.generate(**a_		,   max_new_tokens=a_						)
							tf.debugging.assert_equal(a_		,   a_						)



	@slow
	@require_tensorflow_text
	def 	_lowerCamelCase     ( self						):
			# 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							):



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





						def 	_lowerCamelCase     ( self		,   a_		,   *a_		,   **a_						):
								lowerCAmelCase  :     List[str]       						= self.tokenizer.tokenize(a_						)
								lowerCAmelCase						,		lowerCAmelCase  :     Tuple       						= text.pad_model_inputs(
								    a_		,   max_seq_length=64		,   pad_value=self.model.config.pad_token_id						)
								lowerCAmelCase  :     Dict       						= self.model.generate(input_ids=a_		,   attention_mask=a_						)
								return self.tokenizer.detokenize(a_						)






					lowerCAmelCase  :     str       						= CompleteSentenceTransformer()
					lowerCAmelCase  :     Union[str, Any]       						= tf.keras.layers.Input(shape=(1,)		,   dtype=tf.string		,   name="inputs"						)
					lowerCAmelCase  :     int       						= complete_model(a_						)
					lowerCAmelCase  :     List[Any]       						= tf.keras.Model(a_		,   a_						)
					keras_model.save(a_						)



	def 	_lowerCamelCase     ( self						):
			# Has PT equivalent: this test relies on random sampling
			lowerCAmelCase  :     Tuple       						= {
			    "do_sample": True,
			    "num_beams": 1,
			    "top_p": 0.7,
			    "top_k": 10,
			    "temperature": 0.7,
			}
			lowerCAmelCase  :     Tuple       						= 14

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

			lowerCAmelCase  :     str       						= 638
			# forces the generation to happen on CPU, to avoid GPU-related quirks
			with tf.device(":/CPU:0"						):
					tf.random.set_seed(0						)
					lowerCAmelCase  :     Optional[int]       						= model.generate(**a_		,   eos_token_id=a_		,   **a_						)
			self.assertTrue(expectation == len(generated_tokens[0]						)						)

			lowerCAmelCase  :     List[str]       						= [638, 198]
			with tf.device(":/CPU:0"						):
					tf.random.set_seed(0						)
					lowerCAmelCase  :     int       						= model.generate(**a_		,   eos_token_id=a_		,   **a_						)
			self.assertTrue(expectation == len(generated_tokens[0]						)						)





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



				def 	_lowerCamelCase     ( self		,   a_		,   a_=None		,   **a_						):
						return super().call(a_		,   **a_						)






			lowerCAmelCase  :     List[str]       						= FakeBart.from_pretrained("hf-internal-testing/tiny-random-bart"						)
			lowerCAmelCase  :     Tuple       						= bart_model.generate(a_		,   foo="bar"						).numpy()
			self.assertTrue(np.array_equal(a_		,   a_						)						)
			class 		lowerCamelCase	(   bart_model.model.encoder.__class__							):



				def 	_lowerCamelCase     ( self		,   a_		,   **a_						):
						return super().call(a_		,   **a_						)






			lowerCAmelCase  :     Optional[int]       						= FakeEncoder(bart_model.config		,   bart_model.model.shared						)
			lowerCAmelCase  :     str       						= fake_encoder

			# Normal generation still works (the output will be different because the encoder weights are different)
			lowerCAmelCase  :     List[str]       						= 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"						)










'''simple docstring'''






import math

import tensorflow as tf
from packaging import version






def   __A    (				a_    :	List[Any]						):
		lowerCAmelCase  :     Any       						= tf.convert_to_tensor(a_						)
		lowerCAmelCase  :     List[Any]       						= 0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0						)   ,x.dtype						)						))

		return x * cdf






def   __A    (				a_    :	int						):
		lowerCAmelCase  :     Dict       						= tf.convert_to_tensor(a_						)
		lowerCAmelCase  :     int       						= tf.cast(math.pi   ,x.dtype						)
		lowerCAmelCase  :     Dict       						= tf.cast(0.0_4_4_7_1_5   ,x.dtype						)
		lowerCAmelCase  :     Dict       						= 0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi						) * (x + coeff * tf.pow(a_   ,3						))						))

		return x * cdf






def   __A    (				a_    :	Union[str, Any]						):
		lowerCAmelCase  :     Any       						= tf.convert_to_tensor(a_						)

		return x * tf.tanh(tf.math.softplus(a_						)						)






def   __A    (				a_    :	List[str]						):
		lowerCAmelCase  :     Dict       						= tf.convert_to_tensor(a_						)
		lowerCAmelCase  :     Any       						= tf.cast(0.0_4_4_7_1_5   ,x.dtype						)
		lowerCAmelCase  :     Optional[int]       						= tf.cast(0.7_9_7_8_8_4_5_6_0_8   ,x.dtype						)

		return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)						))






def   __A    (				a_    :	Union[str, Any]						):
		lowerCAmelCase  :     Optional[int]       						= tf.convert_to_tensor(a_						)
		lowerCAmelCase  :     List[Any]       						= tf.cast(1.7_0_2   ,x.dtype						)
		return x * tf.math.sigmoid(coeff * x						)






def   __A    (				a_    :	List[Any]						):
		return tf.clip_by_value(_gelu(a_						)   ,-1_0   ,1_0						)






def   __A    (				a_    :	List[Any]   ,a_    :	List[Any]=-1						):
		lowerCAmelCase						,		lowerCAmelCase  :     Optional[int]       						= tf.split(a_   ,2   ,axis=a_						)
		return a * tf.math.sigmoid(a_						)


if version.parse(tf.version.VERSION) >= version.parse("""2.4"""):






					def   __A    (				a_    :	Optional[Any]						):
							return tf.keras.activations.gelu(a_   ,approximate=a_						)

					lowerCAmelCase									=   tf.keras.activations.gelu

					lowerCAmelCase									=   approximate_gelu_wrap
else:
					lowerCAmelCase									=   _gelu
					lowerCAmelCase									=   _gelu_new


lowerCAmelCase									=   {
    """gelu""": gelu,
    """gelu_10""": gelu_aa,
    """gelu_fast""": gelu_fast,
    """gelu_new""": gelu_new,
    """glu""": glu,
    """mish""": mish,
    """quick_gelu""": quick_gelu,
    """relu""": tf.keras.activations.relu,
    """sigmoid""": tf.keras.activations.sigmoid,
    """silu""": tf.keras.activations.swish,
    """swish""": tf.keras.activations.swish,
    """tanh""": tf.keras.activations.tanh,
}






def   __A    (				a_    :	int						):
		if activation_string in ACTaFN:
				return ACTaFN[activation_string]
		else:
				raise KeyError(f'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}'''						)



