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



	"""simple docstring"""




	def 		_lowercase    (			self    :     Optional[int]			)			->	Dict:
					lowercase_	    =   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_	    =   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

					lowercase_	    =   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_	    =   tf_top_k_top_p_filtering(__SCREAMING_SNAKE_CASE			,				top_k=1_0			,				top_p=0.6			,				min_tokens_to_keep=4			)

					lowercase_	    =   output[output != -float('''inf'''			)]
					lowercase_	    =   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-12			)
					tf.debugging.assert_equal(__SCREAMING_SNAKE_CASE			,				__SCREAMING_SNAKE_CASE			)

@require_tf
class 					lowercase__(							unittest.TestCase					,		__lowerCAmelCase				):



	"""simple docstring"""




	if is_tf_available():
		a     :Tuple       	=      {
		    '''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 		_lowercase    (			self    :     Tuple			)			->	Union[str, Any]:
					lowercase_	    =   TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''			)
					lowercase_	    =   2
					lowercase_	    =   2
					class 					lowercase__(							tf.Module				):



						"""simple docstring"""




						def __init__(			self    :     Dict			,				SCREAMING_SNAKE_CASE_    :     str			)			->	Any:
										super(__SCREAMING_SNAKE_CASE			,				self			).__init__()
										lowercase_	    =   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 		_lowercase    (			self    :     Optional[Any]			,				SCREAMING_SNAKE_CASE_    :     List[Any]			,				SCREAMING_SNAKE_CASE_    :     List[Any]			)			->	Tuple:
										lowercase_	    =   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"]}

					lowercase_	    =   [[2, 0], [1_0_2, 1_0_3]]
					lowercase_	    =   [[1, 0], [1, 1]]
					lowercase_	    =   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}			)
									lowercase_	    =   tf.saved_model.load(__SCREAMING_SNAKE_CASE			).signatures['''serving_default''']
									for batch_size in range(1			,				len(__SCREAMING_SNAKE_CASE			) + 1			):
													lowercase_	    =   {
													    '''input_ids''': tf.constant(dummy_input_ids[:batch_size]			),
													    '''attention_mask''': tf.constant(dummy_attention_masks[:batch_size]			),
													}
													lowercase_	    =   serving_func(**__SCREAMING_SNAKE_CASE			)['''sequences''']
													lowercase_	    =   test_model.generate(**__SCREAMING_SNAKE_CASE			,				max_new_tokens=__SCREAMING_SNAKE_CASE			)
													tf.debugging.assert_equal(__SCREAMING_SNAKE_CASE			,				__SCREAMING_SNAKE_CASE			)
	@slow
	def 		_lowercase    (			self    :     Dict			)			->	int:
					lowercase_	    =   TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''			)
					lowercase_	    =   1
					lowercase_	    =   2
					class 					lowercase__(							tf.Module				):



						"""simple docstring"""




						def __init__(			self    :     int			,				SCREAMING_SNAKE_CASE_    :     Optional[int]			)			->	str:
										super(__SCREAMING_SNAKE_CASE			,				self			).__init__()
										lowercase_	    =   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 		_lowercase    (			self    :     int			,				SCREAMING_SNAKE_CASE_    :     int			,				SCREAMING_SNAKE_CASE_    :     List[str]			)			->	int:
										lowercase_	    =   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"]}

					lowercase_	    =   [[2], [1_0_2, 1_0_3]]
					lowercase_	    =   [[1], [1, 1]]
					lowercase_	    =   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}			)
									lowercase_	    =   tf.saved_model.load(__SCREAMING_SNAKE_CASE			).signatures['''serving_default''']
									for input_row in range(len(__SCREAMING_SNAKE_CASE			)			):
													lowercase_	    =   {
													    '''input_ids''': tf.constant([dummy_input_ids[input_row]]			),
													    '''attention_mask''': tf.constant([dummy_attention_masks[input_row]]			),
													}
													lowercase_	    =   serving_func(**__SCREAMING_SNAKE_CASE			)['''sequences''']
													lowercase_	    =   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 		_lowercase    (			self    :     str			)			->	Optional[Any]:




					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 					lowercase__(							tf.keras.layers.Layer				):



										"""simple docstring"""




										def __init__(			self    :     int			)			->	Optional[Any]:
														super().__init__()
														lowercase_	    =   text.SentencepieceTokenizer(
														    model=tf.io.gfile.GFile(os.path.join(__SCREAMING_SNAKE_CASE			,				'''spiece.model'''			)			,				'''rb'''			).read()			)
														lowercase_	    =   TFAutoModelForSeqaSeqLM.from_pretrained('''hf-internal-testing/tiny-random-t5'''			)




										def 		_lowercase    (			self    :     int			,				SCREAMING_SNAKE_CASE_    :     Optional[int]			,				*SCREAMING_SNAKE_CASE_    :     List[Any]			,				**SCREAMING_SNAKE_CASE_    :     Any			)			->	str:
														lowercase_	    =   self.tokenizer.tokenize(__SCREAMING_SNAKE_CASE			)
														lowercase_    ,      lowercase_	    =   text.pad_model_inputs(
														    __SCREAMING_SNAKE_CASE			,				max_seq_length=6_4			,				pad_value=self.model.config.pad_token_id			)
														lowercase_	    =   self.model.generate(input_ids=__SCREAMING_SNAKE_CASE			,				attention_mask=__SCREAMING_SNAKE_CASE			)
														return self.tokenizer.detokenize(__SCREAMING_SNAKE_CASE			)

									lowercase_	    =   CompleteSentenceTransformer()
									lowercase_	    =   tf.keras.layers.Input(shape=(1,)			,				dtype=tf.string			,				name='''inputs'''			)
									lowercase_	    =   complete_model(__SCREAMING_SNAKE_CASE			)
									lowercase_	    =   tf.keras.Model(__SCREAMING_SNAKE_CASE			,				__SCREAMING_SNAKE_CASE			)
									keras_model.save(__SCREAMING_SNAKE_CASE			)
	def 		_lowercase    (			self    :     Union[str, Any]			)			->	Any:
					lowercase_	    =   {
					    '''do_sample''': True,
					    '''num_beams''': 1,
					    '''top_p''': 0.7,
					    '''top_k''': 1_0,
					    '''temperature''': 0.7,
					}
					lowercase_	    =   1_4

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

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

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




	def 		_lowercase    (			self    :     Dict			)			->	List[str]:
					lowercase_	    =   AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bart'''			)
					lowercase_	    =   '''Hugging Face is a technology company based in New York and Paris.'''
					lowercase_	    =   bart_tokenizer(__SCREAMING_SNAKE_CASE			,				return_tensors='''tf'''			).input_ids
					lowercase_	    =   TFBartForConditionalGeneration.from_pretrained('''hf-internal-testing/tiny-random-bart'''			)
					lowercase_	    =   bart_model.generate(__SCREAMING_SNAKE_CASE			).numpy()
					class 					lowercase__(							__lowerCAmelCase				):



						"""simple docstring"""




						def 		_lowercase    (			self    :     Optional[Any]			,				SCREAMING_SNAKE_CASE_    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     int=None			,				**SCREAMING_SNAKE_CASE_    :     Any			)			->	List[Any]:
										return super().call(__SCREAMING_SNAKE_CASE			,				**__SCREAMING_SNAKE_CASE			)

					lowercase_	    =   FakeBart.from_pretrained('''hf-internal-testing/tiny-random-bart'''			)
					lowercase_	    =   bart_model.generate(__SCREAMING_SNAKE_CASE			,				foo='''bar'''			).numpy()
					self.assertTrue(np.array_equal(__SCREAMING_SNAKE_CASE			,				__SCREAMING_SNAKE_CASE			)			)
					class 					lowercase__(							bart_model.model.encoder.__class__				):



						"""simple docstring"""




						def 		_lowercase    (			self    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     List[str]			,				**SCREAMING_SNAKE_CASE_    :     List[Any]			)			->	int:
										return super().call(__SCREAMING_SNAKE_CASE			,				**__SCREAMING_SNAKE_CASE			)

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

					# Normal generation still works (the output will be different because the encoder weights are different)
					lowercase_	    =   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'''			)










'''simple docstring'''



import inspect
import warnings
from typing import Any, Dict, Optional, Union

from packaging import version







def       _UpperCamelCase			(*_lowerCamelCase				:  str ,						_lowerCamelCase				:  Optional[Union[Dict, Any]] = None ,						_lowerCamelCase				:  List[Any]=True ,						_lowerCamelCase				:  str=2						)-> str:




     '''simple docstring'''





     from .. import __version__

     __snake_case            =	take_from
     __snake_case            =	()
     if not isinstance(args[0] ,						_lowerCamelCase						):
          __snake_case            =	(args,)

     for attribute, version_name, message in args:
          if version.parse(version.parse(_lowerCamelCase						).base_version						) >= version.parse(_lowerCamelCase						):
               raise ValueError(
                   f'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\''''
                   f''' version {__version__} is >= {version_name}'''						)

          __snake_case            =	None
          if isinstance(_lowerCamelCase ,						_lowerCamelCase						) and attribute in deprecated_kwargs:
               values += (deprecated_kwargs.pop(_lowerCamelCase						),)
               __snake_case            =	f'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.'''
          elif hasattr(_lowerCamelCase ,						_lowerCamelCase						):
               values += (getattr(_lowerCamelCase ,						_lowerCamelCase						),)
               __snake_case            =	f'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.'''
          elif deprecated_kwargs is None:
               __snake_case            =	f'''`{attribute}` is deprecated and will be removed in version {version_name}.'''

          if warning is not None:
               __snake_case            =	warning + ''' ''' if standard_warn else ''''''
               warnings.warn(warning + message ,						_lowerCamelCase ,						stacklevel=_lowerCamelCase						)

     if isinstance(_lowerCamelCase ,						_lowerCamelCase						) and len(_lowerCamelCase						) > 0:
          __snake_case            =	inspect.getouterframes(inspect.currentframe()						)[1]
          __snake_case            =	call_frame.filename
          __snake_case            =	call_frame.lineno
          __snake_case            =	call_frame.function
          __snake_case       ,     __snake_case            =	next(iter(deprecated_kwargs.items()						)						)
          raise TypeError(f'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`'''						)

     if len(_lowerCamelCase						) == 0:
          return
     elif len(_lowerCamelCase						) == 1:
          return values[0]
     return values






