



"""simple docstring"""


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



						"""simple docstring"""
						__UpperCamelCase						=							BlenderbotConfig
						__UpperCamelCase						=							{}
						__UpperCamelCase						=							"""gelu"""






						def __init__(					self :Optional[int]   ,  lowercase_ :Optional[Any]   ,  lowercase_ :Any=13   ,  lowercase_ :Optional[int]=7   ,  lowercase_ :Union[str, Any]=True   ,  lowercase_ :Union[str, Any]=False   ,  lowercase_ :List[Any]=99   ,  lowercase_ :str=32   ,  lowercase_ :Union[str, Any]=2   ,  lowercase_ :Optional[Any]=4   ,  lowercase_ :Any=37   ,  lowercase_ :Optional[Any]=0.1   ,  lowercase_ :Any=0.1   ,  lowercase_ :Optional[Any]=20   ,  lowercase_ :Dict=2   ,  lowercase_ :Optional[int]=1   ,  lowercase_ :Dict=0   ,  )				->			Dict:
												UpperCAmelCase             = parent
												UpperCAmelCase             = batch_size
												UpperCAmelCase             = seq_length
												UpperCAmelCase             = is_training
												UpperCAmelCase             = use_labels
												UpperCAmelCase             = vocab_size
												UpperCAmelCase             = hidden_size
												UpperCAmelCase             = num_hidden_layers
												UpperCAmelCase             = num_attention_heads
												UpperCAmelCase             = intermediate_size

												UpperCAmelCase             = hidden_dropout_prob
												UpperCAmelCase             = attention_probs_dropout_prob
												UpperCAmelCase             = max_position_embeddings
												UpperCAmelCase             = eos_token_id
												UpperCAmelCase             = pad_token_id
												UpperCAmelCase             = bos_token_id






						def 	UpperCAmelCase__     (					self :str					)				->			List[Any]:
												UpperCAmelCase             = ids_tensor([self.batch_size, self.seq_length - 1]   ,  self.vocab_size					)
												UpperCAmelCase             = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size					)   ,  1					)
												UpperCAmelCase             = tf.concat([input_ids, eos_tensor]   ,  axis=1					)

												UpperCAmelCase             = ids_tensor([self.batch_size, self.seq_length]   ,  self.vocab_size					)

												UpperCAmelCase             = 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             = prepare_blenderbot_inputs_dict(lowercase_   ,  lowercase_   ,  lowercase_					)
												return config, inputs_dict






						def 	UpperCAmelCase__     (					self :str   ,  lowercase_ :str   ,  lowercase_ :List[str]					)				->			Optional[int]:
												UpperCAmelCase             = TFBlenderbotModel(config=lowercase_					).get_decoder()
												UpperCAmelCase             = inputs_dict['input_ids']

												UpperCAmelCase             = input_ids[:1, :]
												UpperCAmelCase             = inputs_dict['attention_mask'][:1, :]
												UpperCAmelCase             = inputs_dict['head_mask']
												UpperCAmelCase             = 1

												# first forward pass
												UpperCAmelCase             = model(lowercase_   ,  attention_mask=lowercase_   ,  head_mask=lowercase_   ,  use_cache=lowercase_					)

												UpperCAmelCase      ,    UpperCAmelCase             = outputs.to_tuple()

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

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

												UpperCAmelCase             = model(lowercase_   ,  attention_mask=lowercase_					)[0]
												UpperCAmelCase             = model(lowercase_   ,  attention_mask=lowercase_   ,  past_key_values=lowercase_					)[0]

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

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

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


def  _lowerCAmelCase      (   lowercase_		,	lowercase_		,	lowercase_		,	lowercase_=None		,	lowercase_=None		,	lowercase_=None		,	lowercase_=None		,	lowercase_=None		,	):
						if attention_mask is None:
												UpperCAmelCase             = tf.cast(tf.math.not_equal(lowercase_		,	config.pad_token_id						)		,	tf.inta						)
						if decoder_attention_mask is None:
												UpperCAmelCase             = 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             = tf.ones((config.encoder_layers, config.encoder_attention_heads)						)
						if decoder_head_mask is None:
												UpperCAmelCase             = tf.ones((config.decoder_layers, config.decoder_attention_heads)						)
						if cross_attn_head_mask is None:
												UpperCAmelCase             = 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_			(     SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_	,   unittest.TestCase				):



						"""simple docstring"""
						__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 	UpperCAmelCase__     (					self :int					)				->			List[Any]:
												UpperCAmelCase             = TFBlenderbotModelTester(self					)
												UpperCAmelCase             = ConfigTester(self   ,  config_class=lowercase_					)






						def 	UpperCAmelCase__     (					self :Tuple					)				->			Any:
												self.config_tester.run_common_tests()






						def 	UpperCAmelCase__     (					self :List[Any]					)				->			List[str]:
												UpperCAmelCase             = self.model_tester.prepare_config_and_inputs_for_common()
												self.model_tester.check_decoder_model_past_large_inputs(*lowercase_					)







@require_tokenizers
@require_tf
class       A_			(     unittest.TestCase				):



						"""simple docstring"""
						__UpperCamelCase						=							["""My friends are cool but they eat too many carbs."""]
						__UpperCamelCase						=							"""facebook/blenderbot-400M-distill"""






						@cached_property
						def 	UpperCAmelCase__     (					self :Tuple					)				->			Optional[Any]:
												return BlenderbotTokenizer.from_pretrained(self.model_name					)






						@cached_property
						def 	UpperCAmelCase__     (					self :Optional[int]					)				->			Dict:
												UpperCAmelCase             = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name					)
												return model






						@slow
						def 	UpperCAmelCase__     (					self :int					)				->			Optional[int]:
												UpperCAmelCase             = self.tokenizer(self.src_text   ,  return_tensors='tf'					)
												UpperCAmelCase             = self.model.generate(
												    model_inputs.input_ids   ,  )
												UpperCAmelCase             = self.tokenizer.batch_decode(generated_ids.numpy()   ,  skip_special_tokens=lowercase_					)[0]
												assert (
												    generated_words
												    == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
												)










from typing import Optional

from .. import Features, NamedSplit
from ..packaged_modules.text.text import Text
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader

class     _A       (	__UpperCAmelCase    ):



       def __init__(		self							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     NestedDataStructureLike[PathLike]     ,		__SCREAMING_SNAKE_CASE							:     Optional[NamedSplit] = None     ,		__SCREAMING_SNAKE_CASE							:     Optional[Features] = None     ,		__SCREAMING_SNAKE_CASE							:     str = None     ,		__SCREAMING_SNAKE_CASE							:     bool = False     ,		__SCREAMING_SNAKE_CASE							:     bool = False     ,		__SCREAMING_SNAKE_CASE							:     Optional[int] = None     ,		**__SCREAMING_SNAKE_CASE							:     List[str]     ,		):


           '''simple docstring'''

           super().__init__(
               __SCREAMING_SNAKE_CASE     ,		split=__SCREAMING_SNAKE_CASE     ,		features=__SCREAMING_SNAKE_CASE     ,		cache_dir=__SCREAMING_SNAKE_CASE     ,		keep_in_memory=__SCREAMING_SNAKE_CASE     ,		streaming=__SCREAMING_SNAKE_CASE     ,		num_proc=__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE     ,		)
           __a						       =				path_or_paths if isinstance(__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE) else {self.split: path_or_paths}
           __a						       =				Text(
               cache_dir=__SCREAMING_SNAKE_CASE     ,		data_files=__SCREAMING_SNAKE_CASE     ,		features=__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE     ,		)





       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           if self.streaming:
               __a						       =				self.builder.as_streaming_dataset(split=self.split)
           # Build regular (map-style) dataset
           else:
               __a						       =				None
               __a						       =				None
               __a						       =				None
               __a						       =				None

               self.builder.download_and_prepare(
                   download_config=__SCREAMING_SNAKE_CASE     ,		download_mode=__SCREAMING_SNAKE_CASE     ,		verification_mode=__SCREAMING_SNAKE_CASE     ,		base_path=__SCREAMING_SNAKE_CASE     ,		num_proc=self.num_proc     ,		)
               __a						       =				self.builder.as_dataset(
                   split=self.split     ,		verification_mode=__SCREAMING_SNAKE_CASE     ,		in_memory=self.keep_in_memory)
           return dataset





