




from __future__ import annotations

import unittest

from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available
from transformers.testing_utils import require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel




@require_tf
class 		__A   :
						UpperCamelCase      =       BlenderbotConfig
						UpperCamelCase      =       {}
						UpperCamelCase      =       """gelu"""

						def __init__(		self :Union[str, Any]     ,			__snake_case :Union[str, Any]     ,			__snake_case :Union[str, Any]=13     ,			__snake_case :Optional[Any]=7     ,			__snake_case :Optional[int]=True     ,			__snake_case :Optional[Any]=False     ,			__snake_case :Dict=99     ,			__snake_case :List[str]=32     ,			__snake_case :List[str]=2     ,			__snake_case :List[str]=4     ,			__snake_case :List[str]=37     ,			__snake_case :Any=0.1     ,			__snake_case :List[str]=0.1     ,			__snake_case :Union[str, Any]=20     ,			__snake_case :int=2     ,			__snake_case :Dict=1     ,			__snake_case :Any=0     ,			):
										'''simple docstring'''

										__magic_name__						:						Dict										=parent
										__magic_name__						:						Dict										=batch_size
										__magic_name__						:						Dict										=seq_length
										__magic_name__						:						Union[str, Any]										=is_training
										__magic_name__						:						int										=use_labels
										__magic_name__						:						str										=vocab_size
										__magic_name__						:						Optional[int]										=hidden_size
										__magic_name__						:						List[Any]										=num_hidden_layers
										__magic_name__						:						str										=num_attention_heads
										__magic_name__						:						Dict										=intermediate_size

										__magic_name__						:						int										=hidden_dropout_prob
										__magic_name__						:						Tuple										=attention_probs_dropout_prob
										__magic_name__						:						Union[str, Any]										=max_position_embeddings
										__magic_name__						:						int										=eos_token_id
										__magic_name__						:						Optional[int]										=pad_token_id
										__magic_name__						:						Optional[Any]										=bos_token_id

						def      A__    (		self :Dict			):
										'''simple docstring'''

										__magic_name__						:						int										=ids_tensor([self.batch_size, self.seq_length - 1]     ,			self.vocab_size			)
										__magic_name__						:						Tuple										=tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size			)     ,			1			)
										__magic_name__						:						Tuple										=tf.concat([input_ids, eos_tensor]     ,			axis=1			)

										__magic_name__						:						Optional[Any]										=ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size			)

										__magic_name__						:						Dict										=self.config_cls(
										    vocab_size=self.vocab_size     ,			d_model=self.hidden_size     ,			encoder_layers=self.num_hidden_layers     ,			decoder_layers=self.num_hidden_layers     ,			encoder_attention_heads=self.num_attention_heads     ,			decoder_attention_heads=self.num_attention_heads     ,			encoder_ffn_dim=self.intermediate_size     ,			decoder_ffn_dim=self.intermediate_size     ,			dropout=self.hidden_dropout_prob     ,			attention_dropout=self.attention_probs_dropout_prob     ,			max_position_embeddings=self.max_position_embeddings     ,			eos_token_ids=[2]     ,			bos_token_id=self.bos_token_id     ,			pad_token_id=self.pad_token_id     ,			decoder_start_token_id=self.pad_token_id     ,			**self.config_updates     ,			)
										__magic_name__						:						List[str]										=prepare_blenderbot_inputs_dict(__snake_case     ,			__snake_case     ,			__snake_case			)
										return config, inputs_dict


						def      A__    (		self :List[str]     ,			__snake_case :Optional[Any]     ,			__snake_case :Dict			):
										'''simple docstring'''

										__magic_name__						:						List[str]										=TFBlenderbotModel(config=__snake_case			).get_decoder()
										__magic_name__						:						Union[str, Any]										=inputs_dict["""input_ids"""]

										__magic_name__						:						Any										=input_ids[:1, :]
										__magic_name__						:						List[str]										=inputs_dict["""attention_mask"""][:1, :]
										__magic_name__						:						Dict										=inputs_dict["""head_mask"""]
										__magic_name__						:						Optional[Any]										=1

										# first forward pass
										__magic_name__						:						Tuple										=model(__snake_case     ,			attention_mask=__snake_case     ,			head_mask=__snake_case     ,			use_cache=__snake_case			)

										__magic_name__					,			__magic_name__						:						List[str]										=outputs.to_tuple()

										# create hypothetical next token and extent to next_input_ids
										__magic_name__						:						Any										=ids_tensor((self.batch_size, 3)     ,			config.vocab_size			)
										__magic_name__						:						int										=tf.cast(ids_tensor((self.batch_size, 3)     ,			2			)     ,			tf.inta			)

										# append to next input_ids and
										__magic_name__						:						Any										=tf.concat([input_ids, next_tokens]     ,			axis=-1			)
										__magic_name__						:						Optional[Any]										=tf.concat([attention_mask, next_attn_mask]     ,			axis=-1			)

										__magic_name__						:						Optional[int]										=model(__snake_case     ,			attention_mask=__snake_case			)[0]
										__magic_name__						:						Dict										=model(__snake_case     ,			attention_mask=__snake_case     ,			past_key_values=__snake_case			)[0]

										self.parent.assertEqual(next_tokens.shape[1]     ,			output_from_past.shape[1]			)

										# select random slice
										__magic_name__						:						Union[str, Any]										=int(ids_tensor((1,)     ,			output_from_past.shape[-1]			)			)
										__magic_name__						:						Any										=output_from_no_past[:, -3:, random_slice_idx]
										__magic_name__						:						Union[str, Any]										=output_from_past[:, :, random_slice_idx]

										# test that outputs are equal for slice
										tf.debugging.assert_near(__snake_case     ,			__snake_case     ,			rtol=1E-3			)




def 		lowerCAmelCase_ (						lowerCamelCase						,  lowerCamelCase						,  lowerCamelCase						,  lowerCamelCase=None						,  lowerCamelCase=None						,  lowerCamelCase=None						,  lowerCamelCase=None						,  lowerCamelCase=None						,  ):
				if attention_mask is None:
								__magic_name__						:						Optional[int]										=tf.cast(tf.math.not_equal(lowerCamelCase						,  config.pad_token_id )						,  tf.inta )
				if decoder_attention_mask is None:
								__magic_name__						:						List[Any]										=tf.concat(
								    [
								        tf.ones(decoder_input_ids[:, :1].shape						,  dtype=tf.inta ),
								        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]						,  config.pad_token_id )						,  tf.inta ),
								    ]						,  axis=-1						,  )
				if head_mask is None:
								__magic_name__						:						Optional[Any]										=tf.ones((config.encoder_layers, config.encoder_attention_heads) )
				if decoder_head_mask is None:
								__magic_name__						:						Tuple										=tf.ones((config.decoder_layers, config.decoder_attention_heads) )
				if cross_attn_head_mask is None:
								__magic_name__						:						Any										=tf.ones((config.decoder_layers, config.decoder_attention_heads) )
				return {
				    "input_ids": input_ids,
				    "decoder_input_ids": decoder_input_ids,
				    "attention_mask": attention_mask,
				    "decoder_attention_mask": decoder_attention_mask,
				    "head_mask": head_mask,
				    "decoder_head_mask": decoder_head_mask,
				    "cross_attn_head_mask": cross_attn_head_mask,
				}




@require_tf
class 		__A   (      UpperCamelCase__       ,    UpperCamelCase__       ,    unittest.TestCase      ):
						UpperCamelCase      =       (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
						UpperCamelCase      =       (TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
						UpperCamelCase      =       (
						    {
						        """conversational""": TFBlenderbotForConditionalGeneration,
						        """feature-extraction""": TFBlenderbotModel,
						        """summarization""": TFBlenderbotForConditionalGeneration,
						        """text2text-generation""": TFBlenderbotForConditionalGeneration,
						        """translation""": TFBlenderbotForConditionalGeneration,
						    }
						    if is_tf_available()
						    else {}
						)
						UpperCamelCase      =       True
						UpperCamelCase      =       False
						UpperCamelCase      =       False

						def      A__    (		self :Union[str, Any]			):
										'''simple docstring'''

										__magic_name__						:						str										=TFBlenderbotModelTester(self			)
										__magic_name__						:						Dict										=ConfigTester(self     ,			config_class=__snake_case			)

						def      A__    (		self :Union[str, Any]			):
										'''simple docstring'''

										self.config_tester.run_common_tests()


						def      A__    (		self :List[str]			):
										'''simple docstring'''

										__magic_name__						:						Optional[int]										=self.model_tester.prepare_config_and_inputs_for_common()
										self.model_tester.check_decoder_model_past_large_inputs(*__snake_case			)




@require_tokenizers
@require_tf
class 		__A   (      unittest.TestCase      ):
						UpperCamelCase      =       ["""My friends are cool but they eat too many carbs."""]
						UpperCamelCase      =       """facebook/blenderbot-400M-distill"""

						@cached_property
						def      A__    (		self :List[str]			):
										'''simple docstring'''

										return BlenderbotTokenizer.from_pretrained(self.model_name			)

						@cached_property
						def      A__    (		self :Optional[Any]			):
										'''simple docstring'''

										__magic_name__						:						Tuple										=TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name			)
										return model


						@slow
						def      A__    (		self :Union[str, Any]			):
										'''simple docstring'''

										__magic_name__						:						int										=self.tokenizer(self.src_text     ,			return_tensors="""tf"""			)
										__magic_name__						:						Optional[int]										=self.model.generate(
										    model_inputs.input_ids     ,			)
										__magic_name__						:						Union[str, Any]										=self.tokenizer.batch_decode(generated_ids.numpy()     ,			skip_special_tokens=__snake_case			)[0]
										assert (
										    generated_words
										    == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
										)






import unittest

from transformers import AlbertTokenizer, AlbertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


__A							=get_tests_dir('''fixtures/spiece.model''')




@require_sentencepiece
@require_tokenizers
class        _SCREAMING_SNAKE_CASE	( snake_case_     ,			unittest.TestCase		):
	lowerCAmelCase__   =					AlbertTokenizer
	lowerCAmelCase__   =					AlbertTokenizerFast
	lowerCAmelCase__   =					True
	lowerCAmelCase__   =					True
	lowerCAmelCase__   =					True



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					super().setUp()

					# We have a SentencePiece fixture for testing
					lowerCamelCase_         =     AlbertTokenizer(lowercase					)
					tokenizer.save_pretrained(self.tmpdirname					)



	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase					)							->			str:
					lowerCamelCase_         =     "this is a test"
					lowerCamelCase_         =     "this is a test"
					return input_text, output_text



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Dict:
					lowerCamelCase_         =     "<pad>"
					lowerCamelCase_         =     0

					self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowercase					)		,    lowercase					)
					self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowercase					)		,    lowercase					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					lowerCamelCase_         =     list(self.get_tokenizer().get_vocab().keys()					)

					self.assertEqual(vocab_keys[0]		,    "<pad>"					)
					self.assertEqual(vocab_keys[1]		,    "<unk>"					)
					self.assertEqual(vocab_keys[-1]		,    "▁eloquent"					)
					self.assertEqual(len(lowercase					)		,    30000					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Union[str, Any]:
					self.assertEqual(self.get_tokenizer().vocab_size		,    30000					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Union[str, Any]:
					if not self.test_rust_tokenizer:
									return

					lowerCamelCase_         =     self.get_tokenizer()
					lowerCamelCase_         =     self.get_rust_tokenizer()

					lowerCamelCase_         =     "I was born in 92000, and this is falsé."

					lowerCamelCase_         =     tokenizer.tokenize(lowercase					)
					lowerCamelCase_         =     rust_tokenizer.tokenize(lowercase					)
					self.assertListEqual(lowercase		,    lowercase					)

					lowerCamelCase_         =     tokenizer.encode(lowercase		,    add_special_tokens=lowercase					)
					lowerCamelCase_         =     rust_tokenizer.encode(lowercase		,    add_special_tokens=lowercase					)
					self.assertListEqual(lowercase		,    lowercase					)

					lowerCamelCase_         =     self.get_rust_tokenizer()
					lowerCamelCase_         =     tokenizer.encode(lowercase					)
					lowerCamelCase_         =     rust_tokenizer.encode(lowercase					)
					self.assertListEqual(lowercase		,    lowercase					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[Any]:
					lowerCamelCase_         =     AlbertTokenizer(lowercase		,    keep_accents=lowercase					)

					lowerCamelCase_         =     tokenizer.tokenize("This is a test"					)
					self.assertListEqual(lowercase		,    ["▁this", "▁is", "▁a", "▁test"]					)

					self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase					)		,    [48, 25, 21, 1289]					)

					lowerCamelCase_         =     tokenizer.tokenize("I was born in 92000, and this is falsé."					)
					self.assertListEqual(
					    lowercase		,    ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "é", "."]					)
					lowerCamelCase_         =     tokenizer.convert_tokens_to_ids(lowercase					)
					self.assertListEqual(lowercase		,    [31, 23, 386, 19, 561, 3050, 15, 17, 48, 25, 8256, 18, 1, 9]					)

					lowerCamelCase_         =     tokenizer.convert_ids_to_tokens(lowercase					)
					self.assertListEqual(
					    lowercase		,    ["▁i", "▁was", "▁born", "▁in", "▁9", "2000", ",", "▁and", "▁this", "▁is", "▁fal", "s", "<unk>", "."]		,    )



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					lowerCamelCase_         =     AlbertTokenizer(lowercase					)

					lowerCamelCase_         =     tokenizer.encode("sequence builders"					)
					lowerCamelCase_         =     tokenizer.encode("multi-sequence build"					)

					lowerCamelCase_         =     tokenizer.build_inputs_with_special_tokens(lowercase					)
					lowerCamelCase_         =     tokenizer.build_inputs_with_special_tokens(lowercase		,    lowercase					)

					assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
					assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
					    tokenizer.sep_token_id
					]



	@slow
	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[Any]:
					# fmt: off
					lowerCamelCase_         =     {"attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "input_ids": [[2, 21970, 13, 5, 6092, 167, 28, 7103, 2153, 673, 8, 7028, 12051, 18, 17, 7103, 2153, 673, 8, 3515, 18684, 8, 4461, 6, 1927, 297, 8, 12060, 2607, 18, 13, 5, 4461, 15, 10538, 38, 8, 135, 15, 822, 58, 15, 993, 10363, 15, 1460, 8005, 4461, 15, 993, 255, 2328, 9, 9, 9, 6, 26, 1112, 816, 3260, 13, 5, 103, 2377, 6, 17, 1112, 816, 2782, 13, 5, 103, 10641, 6, 29, 84, 2512, 2430, 782, 18684, 2761, 19, 808, 2430, 2556, 17, 855, 1480, 9477, 4091, 128, 11712, 15, 7103, 2153, 673, 17, 24883, 9990, 9, 3], [2, 11502, 25, 1006, 20, 782, 8, 11809, 855, 1732, 19393, 18667, 37, 367, 21018, 69, 1854, 34, 11860, 19124, 27, 156, 225, 17, 193, 4141, 19, 65, 9124, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
					# fmt: on

					self.tokenizer_integration_test_util(
					    expected_encoding=lowercase		,    model_name="albert-base-v2"		,    revision="6b6560eaf5ff2e250b00c50f380c5389a9c2d82e"		,    )


