






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




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	tf.convert_to_tensor(
												    [
												        [
												            8.2_22_09_91,  # 3rd highest value; idx. 0
												            -0.5_62_00_44,
												            5.23_22_97_52,
												            4.0_38_63_93,
												            -6.8_79_83_78,
												            -0.54_78_58_02,
												            -3.2_01_21_53,
												            2.92_77_71_76,
												            1.88_17_19_53,
												            7.35_34_12_76,  # 5th highest value; idx. 9
												            8.43_20_78_33,  # 2nd highest value; idx. 10
												            -9.85_71_18_36,
												            -5.96_20_92_36,
												            -1.13_03_91_61,
												            -7.1_11_52_94,
												            -0.8_36_96_33,
												            -5.3_18_64_08,
												            7.06_42_74_07,
												            0.81_36_93_44,
												            -0.82_02_38_17,
												            -5.9_17_97_96,
												            0.58_81_34_43,
												            -6.99_77_84_38,
												            4.71_55_11_89,
												            -0.18_77_16_37,
												            7.44_02_07_59,  # 4th highest value; idx. 25
												            9.38_45_09_87,  # 1st highest value; idx. 26
												            2.12_66_29_41,
												            -9.32_56_20_38,
												            2.35_65_25_22,
												        ],  # cummulative prob of 5 highest values <= 0.6
												        [
												            0.58_42_55_18,
												            4.53_13_92_38,
												            -5.57_51_04_64,
												            -6.28_03_06_99,
												            -7.19_52_95_03,
												            -4.02_12_25_51,
												            1.39_33_70_37,
												            -6.06_70_70_57,
												            1.59_48_05_17,
												            -9.64_31_19,
												            0.03_90_77_99,
												            0.67_23_17_62,
												            -8.88_20_67_26,
												            6.27_11_59_22,  # 4th highest value; idx. 13
												            2.28_52_07_23,
												            4.82_76_75_06,
												            4.30_42_13_68,
												            8.8_27_53_13,  # 2nd highest value; idx. 17
												            5.44_02_99_58,  # 5th highest value; idx. 18
												            -4.4_73_57_94,
												            7.38_57_95_36,  # 3rd highest value; idx. 20
												            -2.91_05_16_63,
												            2.61_94_60_77,
												            -2.5_67_47_62,
												            -9.48_95_93_02,
												            -4.02_92_26_45,
												            -1.35_41_69_18,
												            9.67_70_23_23,  # 1st highest value; idx. 27
												            -5.89_47_85_53,
												            1.85_37_04_67,
												        ],  # cummulative prob of 5 highest values <= 0.6
												    ]	,  dtype=tf.floataa	,  )

												lowercase__    :				List[str]=	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

												lowercase__    :				int=	tf.convert_to_tensor(
												    [8.22_20_99, 7.3_53_41_26, 8.43_20_78, 7.4_40_20_75, 9.3_84_51, 6.27_11_59, 8.82_75_31, 5.4_40_29_95, 7.3_85_79_56, 9.67_70_23]	,  dtype=tf.floataa	,  )  # expected non filtered values as noted above

												lowercase__    :				Optional[Any]=	tf_top_k_top_p_filtering(snake_case__	,  top_k=10	,  top_p=0.6	,  min_tokens_to_keep=4      )

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

												tf.debugging.assert_near(snake_case__	,  snake_case__	,  rtol=1e-12      )
												tf.debugging.assert_equal(snake_case__	,  snake_case__      )


@require_tf
class    __UpperCAmelCase(       unittest.TestCase  ,  SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						if is_tf_available():
												__lowerCamelCase    	= {
												    "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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# TF-only test: tf.saved_model export
												lowercase__    :				Any=	TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"      )
												lowercase__    :				Optional[int]=	2
												lowercase__    :				str=	2


												class    __UpperCAmelCase(       tf.Module							):




																		"""simple docstring"""




																		def __init__(       self	,  snake_case__      ):
																								'''simple docstring'''
																								super(snake_case__	,  self      ).__init__()
																								lowercase__    :				Tuple=	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=snake_case__	,  )
																		def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__      ):
																								'''simple docstring'''
																								lowercase__    :				str=	self.model.generate(
																								    input_ids=snake_case__	,  attention_mask=snake_case__	,  max_new_tokens=snake_case__	,  return_dict_in_generate=snake_case__	,  )
																								return {"sequences": outputs["sequences"]}


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




						@slow
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# TF-only test: tf.saved_model export
												lowercase__    :				Union[str, Any]=	TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"      )
												lowercase__    :				int=	1
												lowercase__    :				Tuple=	2


												class    __UpperCAmelCase(       tf.Module							):




																		"""simple docstring"""




																		def __init__(       self	,  snake_case__      ):
																								'''simple docstring'''
																								super(snake_case__	,  self      ).__init__()
																								lowercase__    :				Any=	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=snake_case__	,  )
																		def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__      ):
																								'''simple docstring'''
																								lowercase__    :				Any=	self.model.generate(
																								    input_ids=snake_case__	,  attention_mask=snake_case__	,  max_new_tokens=snake_case__	,  return_dict_in_generate=snake_case__	,  )
																								return {"sequences": outputs["sequences"]}


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




						@slow
						@require_tensorflow_text
						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# 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=snake_case__      )


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




																								"""simple docstring"""




																								def __init__(       self      ):
																														'''simple docstring'''
																														super().__init__()
																														lowercase__    :				str=	text.SentencepieceTokenizer(
																														    model=tf.io.gfile.GFile(os.path.join(snake_case__	,  "spiece.model"      )	,  "rb"      ).read()      )
																														lowercase__    :				List[str]=	TFAutoModelForSeqaSeqLM.from_pretrained("hf-internal-testing/tiny-random-t5"      )




																								def 		UpperCAmelCase_	(       self	,  snake_case__	,  *snake_case__	,  **snake_case__      ):
																														'''simple docstring'''
																														lowercase__    :				str=	self.tokenizer.tokenize(snake_case__      )
																														lowercase__,     lowercase__    :				Optional[Any]=	text.pad_model_inputs(
																														    snake_case__	,  max_seq_length=64	,  pad_value=self.model.config.pad_token_id      )
																														lowercase__    :				Dict=	self.model.generate(input_ids=snake_case__	,  attention_mask=snake_case__      )
																														return self.tokenizer.detokenize(snake_case__      )


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




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# Has PT equivalent: this test relies on random sampling
												lowercase__    :				str=	{
												    "do_sample": True,
												    "num_beams": 1,
												    "top_p": 0.7,
												    "top_k": 10,
												    "temperature": 0.7,
												}
												lowercase__    :				Optional[int]=	14

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

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

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




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


												class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




																		"""simple docstring"""




																		def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__=None	,  **snake_case__      ):
																								'''simple docstring'''
																								return super().call(snake_case__	,  **snake_case__      )


												lowercase__    :				Dict=	FakeBart.from_pretrained("hf-internal-testing/tiny-random-bart"      )
												lowercase__    :				int=	bart_model.generate(snake_case__	,  foo="bar"      ).numpy()
												self.assertTrue(np.array_equal(snake_case__	,  snake_case__      )      )


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




																		"""simple docstring"""




																		def 		UpperCAmelCase_	(       self	,  snake_case__	,  **snake_case__      ):
																								'''simple docstring'''
																								return super().call(snake_case__	,  **snake_case__      )


												lowercase__    :				Tuple=	FakeEncoder(bart_model.config	,  bart_model.model.shared      )
												lowercase__    :				Dict=	fake_encoder

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














"""simple docstring"""







from __future__ import annotations

from collections.abc import Iterator
from typing import Any


class    __UpperCAmelCase:




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Any=	data
												lowercase__    :				Node | None=	None


class    __UpperCAmelCase:




						"""simple docstring"""




						def __init__(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	None
												lowercase__    :				int=	None




						def __iter__(       self      ):
												'''simple docstring'''
												lowercase__    :				Any=	self.head
												while self.head:
																		yield node.data
																		lowercase__    :				str=	node.next
																		if node == self.head:
																								break




						def __len__(       self      ):
												'''simple docstring'''
												return sum(1 for _ in self      )




						def __repr__(       self      ):
												'''simple docstring'''
												return "->".join(str(snake_case__      ) for item in iter(self      )      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												self.insert_nth(len(self      )	,  snake_case__      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												self.insert_nth(0	,  snake_case__      )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												if index < 0 or index > len(self      ):
																		raise IndexError("list index out of range."      )
												lowercase__    :				Optional[Any]=	Node(snake_case__      )
												if self.head is None:
																		lowercase__    :				Tuple=	new_node  # first node points itself
																		lowercase__    :				str=	new_node
												elif index == 0:  # insert at head
																		lowercase__    :				Dict=	self.head
																		lowercase__    :				int=	new_node
												else:
																		lowercase__    :				Dict=	self.head
																		for _ in range(index - 1      ):
																								lowercase__    :				Any=	temp.next
																		lowercase__    :				str=	temp.next
																		lowercase__    :				Dict=	new_node
																		if index == len(self      ) - 1:  # insert at tail
																								lowercase__    :				int=	new_node




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return self.delete_nth(0      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return self.delete_nth(len(self      ) - 1      )




						def 		UpperCAmelCase_	(       self	,  snake_case__ = 0      ):
												'''simple docstring'''
												if not 0 <= index < len(self      ):
																		raise IndexError("list index out of range."      )
												lowercase__    :				int=	self.head
												if self.head == self.tail:  # just one node
																		lowercase__    :				Union[str, Any]=	None
												elif index == 0:  # delete head node
																		lowercase__    :				Tuple=	self.tail.next.next
																		lowercase__    :				Union[str, Any]=	self.head.next
												else:
																		lowercase__    :				Union[str, Any]=	self.head
																		for _ in range(index - 1      ):
																								lowercase__    :				Dict=	temp.next
																		lowercase__    :				List[str]=	temp.next
																		lowercase__    :				Optional[Any]=	temp.next.next
																		if index == len(self      ) - 1:  # delete at tail
																								lowercase__    :				Optional[Any]=	temp
												return delete_node.data




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return len(self      ) == 0





def 						lowercase__()    ->None:




						"""simple docstring"""



						lowercase__    :				Dict=	CircularLinkedList()
						assert len(A						) == 0
						assert circular_linked_list.is_empty() is True
						assert str(A						) == ""

						try:
												circular_linked_list.delete_front()
												raise AssertionError  # This should not happen
						except IndexError:
												assert True  # This should happen

						try:
												circular_linked_list.delete_tail()
												raise AssertionError  # This should not happen
						except IndexError:
												assert True  # This should happen

						try:
												circular_linked_list.delete_nth(-1						)
												raise AssertionError
						except IndexError:
												assert True

						try:
												circular_linked_list.delete_nth(0						)
												raise AssertionError
						except IndexError:
												assert True

						assert circular_linked_list.is_empty() is True
						for i in range(5						):
												assert len(A						) == i
												circular_linked_list.insert_nth(A ,		i + 1						)
						assert str(A						) == "->".join(str(A						) for i in range(1 ,		6						)						)

						circular_linked_list.insert_tail(6						)
						assert str(A						) == "->".join(str(A						) for i in range(1 ,		7						)						)
						circular_linked_list.insert_head(0						)
						assert str(A						) == "->".join(str(A						) for i in range(0 ,		7						)						)

						assert circular_linked_list.delete_front() == 0
						assert circular_linked_list.delete_tail() == 6
						assert str(A						) == "->".join(str(A						) for i in range(1 ,		6						)						)
						assert circular_linked_list.delete_nth(2						) == 3

						circular_linked_list.insert_nth(2 ,		3						)
						assert str(A						) == "->".join(str(A						) for i in range(1 ,		6						)						)

						assert circular_linked_list.is_empty() is False


if __name__ == "__main__":
			import doctest

			doctest.testmod()






