



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 						__UpperCamelCase    :
	SCREAMING_SNAKE_CASE__    				=     BlenderbotConfig
	SCREAMING_SNAKE_CASE__    				=     {}
	SCREAMING_SNAKE_CASE__    				=     """gelu"""




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


		'''simple docstring'''







		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       __A					(		self	:						Any							):


		'''simple docstring'''







		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       __A					(		self	:						Dict		,	lowerCAmelCase	:						int		,	lowerCAmelCase	:						Dict							):


		'''simple docstring'''







		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_       				=			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      (						A       ,     A       ,     A       ,     A=None       ,     A=None       ,     A=None       ,     A=None       ,     A=None       ,     ):





	if attention_mask is None:
		UpperCAmelCase_       				=			tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE__       ,     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 						__UpperCamelCase    (					_UpperCAmelCase     ,     _UpperCAmelCase     ,     unittest.TestCase						):
	SCREAMING_SNAKE_CASE__    				=     (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
	SCREAMING_SNAKE_CASE__    				=     (TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
	SCREAMING_SNAKE_CASE__    				=     (
	    {
	        """conversational""": TFBlenderbotForConditionalGeneration,
	        """feature-extraction""": TFBlenderbotModel,
	        """summarization""": TFBlenderbotForConditionalGeneration,
	        """text2text-generation""": TFBlenderbotForConditionalGeneration,
	        """translation""": TFBlenderbotForConditionalGeneration,
	    }
	    if is_tf_available()
	    else {}
	)
	SCREAMING_SNAKE_CASE__    				=     True
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     False




	def       __A					(		self	:						List[str]							):


		'''simple docstring'''







		UpperCAmelCase_       				=			TFBlenderbotModelTester(self							)
		UpperCAmelCase_       				=			ConfigTester(self		,	config_class=lowercase__							)




	def       __A					(		self	:						List[Any]							):


		'''simple docstring'''







		self.config_tester.run_common_tests()




	def       __A					(		self	:						List[Any]							):


		'''simple docstring'''







		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 						__UpperCamelCase    (					unittest.TestCase						):
	SCREAMING_SNAKE_CASE__    				=     ["""My friends are cool but they eat too many carbs."""]
	SCREAMING_SNAKE_CASE__    				=     """facebook/blenderbot-400M-distill"""




	@cached_property
	def       __A					(		self	:						Union[str, Any]							):


		'''simple docstring'''







		return BlenderbotTokenizer.from_pretrained(self.model_name							)




	@cached_property
	def       __A					(		self	:						str							):


		'''simple docstring'''







		UpperCAmelCase_       				=			TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name							)
		return model




	@slow
	def       __A					(		self	:						List[Any]							):


		'''simple docstring'''







		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 sklearn.metrics import mean_squared_error

import datasets


_a:    Any 							=			"""\
@article{scikit-learn,
 title={Scikit-learn: Machine Learning in {P}ython},
 author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
 journal={Journal of Machine Learning Research},
 volume={12},
 pages={2825--2830},
 year={2011}
}
"""

_a:    List[Any] 							=			"""\
Mean Squared Error(MSE) is the average of the square of difference between the predicted
and actual values.
"""


_a:    List[str] 							=			"""
Args:
    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Estimated target values.
    references: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Ground truth (correct) target values.
    sample_weight: array-like of shape (n_samples,), default=None
        Sample weights.
    multioutput: {\"raw_values\", \"uniform_average\"} or array-like of shape (n_outputs,), default=\"uniform_average\"
        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.

        \"raw_values\" : Returns a full set of errors in case of multioutput input.

        \"uniform_average\" : Errors of all outputs are averaged with uniform weight.

    squared : bool, default=True
        If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.

Returns:
    mse : mean squared error.
Examples:

    >>> mse_metric = datasets.load_metric(\"mse\")
    >>> predictions = [2.5, 0.0, 2, 8]
    >>> references = [3, -0.5, 2, 7]
    >>> results = mse_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'mse': 0.375}
    >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)
    >>> print(rmse_result)
    {'mse': 0.6123724356957945}

    If you're using multi-dimensional lists, then set the config as follows :

    >>> mse_metric = datasets.load_metric(\"mse\", \"multilist\")
    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]
    >>> references = [[0, 2], [-1, 2], [8, -5]]
    >>> results = mse_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'mse': 0.7083333333333334}
    >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput='raw_values')
    >>> print(results) # doctest: +NORMALIZE_WHITESPACE
    {'mse': array([0.41666667, 1. ])}
"""

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION     ,     _KWARGS_DESCRIPTION						)
class 						__UpperCamelCase    (					datasets.Metric						):




	def       __A					(		self	:						List[str]							):


		'''simple docstring'''







		return datasets.MetricInfo(
		    description=_DESCRIPTION		,	citation=_CITATION		,	inputs_description=_KWARGS_DESCRIPTION		,	features=datasets.Features(self._get_feature_types()							)		,	reference_urls=[
		        "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html"
		    ]		,	)




	def       __A					(		self	:						Optional[Any]							):


		'''simple docstring'''







		if self.config_name == "multilist":
			return {
			    "predictions": datasets.Sequence(datasets.Value("float"							)							),
			    "references": datasets.Sequence(datasets.Value("float"							)							),
			}
		else:
			return {
			    "predictions": datasets.Value("float"							),
			    "references": datasets.Value("float"							),
			}




	def       __A					(		self	:						Tuple		,	lowerCAmelCase	:						int		,	lowerCAmelCase	:						Dict		,	lowerCAmelCase	:						str=None		,	lowerCAmelCase	:						str="uniform_average"		,	lowerCAmelCase	:						Any=True							):


		'''simple docstring'''







		UpperCAmelCase_       				=			mean_squared_error(
		    lowerCAmelCase		,	lowerCAmelCase		,	sample_weight=lowerCAmelCase		,	multioutput=lowerCAmelCase		,	squared=lowerCAmelCase							)

		return {"mse": mse}