



"""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    __lowercase     :







						"""simple docstring"""







						_A		:							int							       =		BlenderbotConfig
						_A		:							str							       =		{}
						_A		:							List[Any]							       =		"""gelu"""







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

											snake_case_					:		Any 					=					hidden_dropout_prob
											snake_case_					:		List[Any] 					=					attention_probs_dropout_prob
											snake_case_					:		Dict 					=					max_position_embeddings
											snake_case_					:		Optional[Any] 					=					eos_token_id
											snake_case_					:		Optional[Any] 					=					pad_token_id
											snake_case_					:		Dict 					=					bos_token_id







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Any 					=					ids_tensor([self.batch_size, self.seq_length - 1]   ,		self.vocab_size       )
											snake_case_					:		List[Any] 					=					tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size       )   ,		1       )
											snake_case_					:		Optional[int] 					=					tf.concat([input_ids, eos_tensor]   ,		axis=1       )

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

											snake_case_					:		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   ,		)
											snake_case_					:		List[str] 					=					prepare_blenderbot_inputs_dict(lowercase__   ,		lowercase__   ,		lowercase__       )
											return config, inputs_dict







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__       ):
											snake_case_					:		str 					=					TFBlenderbotModel(config=lowercase__       ).get_decoder()
											snake_case_					:		Dict 					=					inputs_dict["""input_ids"""]

											snake_case_					:		Optional[Any] 					=					input_ids[:1, :]
											snake_case_					:		Dict 					=					inputs_dict["""attention_mask"""][:1, :]
											snake_case_					:		Dict 					=					inputs_dict["""head_mask"""]
											snake_case_					:		Dict 					=					1

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

											snake_case_					,			snake_case_					:		Any 					=					outputs.to_tuple()

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

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

											snake_case_					:		List[Any] 					=					model(lowercase__   ,		attention_mask=lowercase__       )[0]
											snake_case_					:		int 					=					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
											snake_case_					:		str 					=					int(ids_tensor((1,)   ,		output_from_past.shape[-1]       )       )
											snake_case_					:		List[Any] 					=					output_from_no_past[:, -3:, random_slice_idx]
											snake_case_					:		str 					=					output_from_past[:, :, random_slice_idx]

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







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Union[str, Any]				,    SCREAMING_SNAKE_CASE__ :  Optional[Any]				,    SCREAMING_SNAKE_CASE__ :  Any				,    SCREAMING_SNAKE_CASE__ :  List[str]=None				,    SCREAMING_SNAKE_CASE__ :  Optional[Any]=None				,    SCREAMING_SNAKE_CASE__ :  Optional[Any]=None				,    SCREAMING_SNAKE_CASE__ :  List[str]=None				,    SCREAMING_SNAKE_CASE__ :  Optional[int]=None				,    ):



					"""simple docstring"""





					if attention_mask is None:
										snake_case_					:		List[Any] 					=					tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE__				,    config.pad_token_id			)				,    tf.inta			)
					if decoder_attention_mask is None:
										snake_case_					:		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:
										snake_case_					:		Dict 					=					tf.ones((config.encoder_layers, config.encoder_attention_heads)			)
					if decoder_head_mask is None:
										snake_case_					:		List[Any] 					=					tf.ones((config.decoder_layers, config.decoder_attention_heads)			)
					if cross_attn_head_mask is None:
										snake_case_					:		List[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    __lowercase     (					_UpperCAmelCase					,    _UpperCAmelCase					,    unittest.TestCase):







						"""simple docstring"""







						_A		:							int							       =		(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
						_A		:							Optional[Any]							       =		(TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
						_A		:							Optional[Any]							       =		(
						    {
						        """conversational""": TFBlenderbotForConditionalGeneration,
						        """feature-extraction""": TFBlenderbotModel,
						        """summarization""": TFBlenderbotForConditionalGeneration,
						        """text2text-generation""": TFBlenderbotForConditionalGeneration,
						        """translation""": TFBlenderbotForConditionalGeneration,
						    }
						    if is_tf_available()
						    else {}
						)
						_A		:							str							       =		True
						_A		:							Optional[Any]							       =		False
						_A		:							Any							       =		False







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Any 					=					TFBlenderbotModelTester(self       )
											snake_case_					:		str 					=					ConfigTester(self   ,		config_class=lowercase__       )







						def 						__UpperCamelCase							(self       ):
											self.config_tester.run_common_tests()







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		List[Any] 					=					self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.check_decoder_model_past_large_inputs(*lowercase__       )







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







						"""simple docstring"""







						_A		:							Dict							       =		["""My friends are cool but they eat too many carbs."""]
						_A		:							int							       =		"""facebook/blenderbot-400M-distill"""







						@cached_property
						def 						__UpperCamelCase							(self       ):
											return BlenderbotTokenizer.from_pretrained(self.model_name       )







						@cached_property
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Any 					=					TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name       )
											return model







						@slow
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Tuple 					=					self.tokenizer(self.src_text   ,		return_tensors="""tf"""       )
											snake_case_					:		Tuple 					=					self.model.generate(
											    model_inputs.input_ids   ,		)
											snake_case_					:		List[str] 					=					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?"
											)









"""simple docstring"""

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_			   =					logging.get_logger(__name__)

a_			   =					{
    '''facebook/dpr-ctx_encoder-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-question_encoder-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-reader-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-ctx_encoder-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-question_encoder-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-reader-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json'''
    ),
}







class    __lowercase     (					_UpperCAmelCase):







						"""simple docstring"""







						_A		:							Optional[int]							       =		"""dpr"""







						def __init__(self   ,		lowercase__=3_05_22   ,		lowercase__=7_68   ,		lowercase__=12   ,		lowercase__=12   ,		lowercase__=30_72   ,		lowercase__="gelu"   ,		lowercase__=0.1   ,		lowercase__=0.1   ,		lowercase__=5_12   ,		lowercase__=2   ,		lowercase__=0.02   ,		lowercase__=1e-12   ,		lowercase__=0   ,		lowercase__="absolute"   ,		lowercase__ = 0   ,		**lowercase__   ,		):
											super().__init__(pad_token_id=lowercase__   ,		**lowercase__       )

											snake_case_					:		List[Any] 					=					vocab_size
											snake_case_					:		List[str] 					=					hidden_size
											snake_case_					:		Tuple 					=					num_hidden_layers
											snake_case_					:		List[Any] 					=					num_attention_heads
											snake_case_					:		int 					=					hidden_act
											snake_case_					:		Dict 					=					intermediate_size
											snake_case_					:		int 					=					hidden_dropout_prob
											snake_case_					:		Any 					=					attention_probs_dropout_prob
											snake_case_					:		str 					=					max_position_embeddings
											snake_case_					:		List[str] 					=					type_vocab_size
											snake_case_					:		List[str] 					=					initializer_range
											snake_case_					:		Optional[int] 					=					layer_norm_eps
											snake_case_					:		Union[str, Any] 					=					projection_dim
											snake_case_					:		str 					=					position_embedding_type




