

"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, 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, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel







@require_tf
class   __lowercase      :
  snake_case_												=							BlenderbotSmallConfig
  snake_case_												=							{}
  snake_case_												=							"""gelu"""

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






   '''simple docstring'''
   UpperCAmelCase__       :	Dict        =       parent
   UpperCAmelCase__       :	Union[str, Any]        =       batch_size
   UpperCAmelCase__       :	Dict        =       seq_length
   UpperCAmelCase__       :	List[Any]        =       is_training
   UpperCAmelCase__       :	Optional[int]        =       use_labels
   UpperCAmelCase__       :	Tuple        =       vocab_size
   UpperCAmelCase__       :	List[str]        =       hidden_size
   UpperCAmelCase__       :	int        =       num_hidden_layers
   UpperCAmelCase__       :	int        =       num_attention_heads
   UpperCAmelCase__       :	Optional[int]        =       intermediate_size

   UpperCAmelCase__       :	Optional[Any]        =       hidden_dropout_prob
   UpperCAmelCase__       :	List[str]        =       attention_probs_dropout_prob
   UpperCAmelCase__       :	Optional[int]        =       max_position_embeddings
   UpperCAmelCase__       :	Tuple        =       eos_token_id
   UpperCAmelCase__       :	Optional[Any]        =       pad_token_id
   UpperCAmelCase__       :	Any        =       bos_token_id

  def  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       ids_tensor([self.batch_size, self.seq_length - 1]       ,self.vocab_size     )
   UpperCAmelCase__       :	int        =       tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size     )       ,1     )
   UpperCAmelCase__       :	List[str]        =       tf.concat([input_ids, eos_tensor]       ,axis=1     )

   UpperCAmelCase__       :	int        =       ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size     )

   UpperCAmelCase__       :	Optional[int]        =       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       ,)
   UpperCAmelCase__       :	Union[str, Any]        =       prepare_blenderbot_small_inputs_dict(A       ,A       ,A     )
   return config, inputs_dict




  def  __lowercase							(	self		:      int       ,A		:      int       ,A		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	int        =       TFBlenderbotSmallModel(config=A     ).get_decoder()
   UpperCAmelCase__       :	int        =       inputs_dict["""input_ids"""]

   UpperCAmelCase__       :	Tuple        =       input_ids[:1, :]
   UpperCAmelCase__       :	int        =       inputs_dict["""attention_mask"""][:1, :]
   UpperCAmelCase__       :	List[str]        =       inputs_dict["""head_mask"""]
   UpperCAmelCase__       :	int        =       1

   # first forward pass
   UpperCAmelCase__       :	List[str]        =       model(A       ,attention_mask=A       ,head_mask=A       ,use_cache=A     )

   UpperCAmelCase__ ,   UpperCAmelCase__       :	List[Any]        =       outputs.to_tuple()

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

   # append to next input_ids and
   UpperCAmelCase__       :	List[str]        =       tf.concat([input_ids, next_tokens]       ,axis=-1     )
   UpperCAmelCase__       :	Optional[int]        =       tf.concat([attention_mask, next_attn_mask]       ,axis=-1     )

   UpperCAmelCase__       :	Dict        =       model(A       ,attention_mask=A     )[0]
   UpperCAmelCase__       :	Dict        =       model(A       ,attention_mask=A       ,past_key_values=A     )[0]

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

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

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







def 	lowerCAmelCase    (       __UpperCamelCase				,     __UpperCamelCase				,     __UpperCamelCase				,     __UpperCamelCase=None				,     __UpperCamelCase=None				,     __UpperCamelCase=None				,     __UpperCamelCase=None				,     __UpperCamelCase=None				,     ):
 '''simple docstring'''





 if attention_mask is None:
  UpperCAmelCase__       :	Optional[int]        =       tf.cast(tf.math.not_equal(__UpperCamelCase				,     config.pad_token_id   )				,     tf.inta   )
 if decoder_attention_mask is None:
  UpperCAmelCase__       :	Optional[int]        =       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:
  UpperCAmelCase__       :	List[Any]        =       tf.ones((config.encoder_layers, config.encoder_attention_heads)   )
 if decoder_head_mask is None:
  UpperCAmelCase__       :	Dict        =       tf.ones((config.decoder_layers, config.decoder_attention_heads)   )
 if cross_attn_head_mask is None:
  UpperCAmelCase__       :	Tuple        =       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   __lowercase      (     __lowerCamelCase						,      __lowerCamelCase						,      unittest.TestCase       ):
  snake_case_												=							(
      (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else ()
  )
  snake_case_												=							(TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else ()
  snake_case_												=							(
      {
          """conversational""": TFBlenderbotSmallForConditionalGeneration,
          """feature-extraction""": TFBlenderbotSmallModel,
          """summarization""": TFBlenderbotSmallForConditionalGeneration,
          """text2text-generation""": TFBlenderbotSmallForConditionalGeneration,
          """translation""": TFBlenderbotSmallForConditionalGeneration,
      }
      if is_tf_available()
      else {}
  )
  snake_case_												=							True
  snake_case_												=							False
  snake_case_												=							False

  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       TFBlenderbotSmallModelTester(self     )
   UpperCAmelCase__       :	Tuple        =       ConfigTester(self       ,config_class=A     )

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   self.config_tester.run_common_tests()




  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Dict        =       self.model_tester.prepare_config_and_inputs_for_common()
   self.model_tester.check_decoder_model_past_large_inputs(*A     )







@require_tokenizers
@require_tf
class   __lowercase      (     unittest.TestCase       ):
  snake_case_												=							[
      """Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like """
      """  i'm going to throw up.\nand why is that?"""
  ]
  snake_case_												=							"""facebook/blenderbot_small-90M"""

  @cached_property
  def  __lowercase							(	self		:      Tuple     ):






   '''simple docstring'''
   # use "old" tokenizer here because of bug when downloading new tokenizer
   return BlenderbotSmallTokenizer.from_pretrained("""facebook/blenderbot-90M"""     )

  @cached_property
  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name     )
   return model




  @slow
  def  __lowercase							(	self		:      int     ):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       self.tokenizer(self.src_text       ,return_tensors="""tf"""     )
   UpperCAmelCase__       :	List[Any]        =       self.model.generate(
       model_inputs.input_ids       ,attention_mask=model_inputs.attention_mask       ,num_beams=2       ,use_cache=A       ,)
   UpperCAmelCase__       :	List[str]        =       self.tokenizer.batch_decode(generated_ids.numpy()       ,skip_special_tokens=A     )[0]
   assert generated_words in (
       "i don't know. i just feel like i'm going to throw up. it's not fun.",
       "i'm not sure. i just feel like i've been feeling like i have to be in a certain place",
       "i'm not sure. i just feel like i've been in a bad situation.",
   )







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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 14, 2231, 886, 2385, 17659, 84, 14, 16792, 1952, 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, 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]], "token_type_ids": [[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, 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, 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, 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"		,    )


