'''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 :
    a__			:		Optional[int]			  =    BlenderbotConfig
    a__			:		List[str]			  =    {}
    a__			:		Optional[Any]			  =    """gelu"""



    def __init__(self       :       Tuple  ,     __a       :       int  ,     __a       :       List[str]=13  ,     __a       :       Union[str, Any]=7  ,     __a       :       List[str]=True  ,     __a       :       str=False  ,     __a       :       Union[str, Any]=99  ,     __a       :       Union[str, Any]=32  ,     __a       :       List[str]=2  ,     __a       :       List[Any]=4  ,     __a       :       List[str]=37  ,     __a       :       Optional[Any]=0.1  ,     __a       :       str=0.1  ,     __a       :       Dict=20  ,     __a       :       str=2  ,     __a       :       List[str]=1  ,     __a       :       Tuple=0  ,     ):
         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    _lowercase		(self       :       Tuple					):
         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(__a  ,     __a  ,     __a					)
         return config, inputs_dict






    def    _lowercase		(self       :       Optional[int]  ,     __a       :       str  ,     __a       :       Tuple					):
         UpperCAmelCase_			       =  TFBlenderbotModel(config=__a					).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(__a  ,     attention_mask=__a  ,     head_mask=__a  ,     use_cache=__a					)

         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(__a  ,     attention_mask=__a					)[0]
         UpperCAmelCase_			       =  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_			       =  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(__a  ,     __a  ,     rtol=1E-3					)





def       lowerCAmelCase_							(   snake_case_			:					List[Any]   ,       snake_case_			:					str   ,       snake_case_			:					Any   ,       snake_case_			:					Tuple=None   ,       snake_case_			:					Tuple=None   ,       snake_case_			:					Union[str, Any]=None   ,       snake_case_			:					Optional[Any]=None   ,       snake_case_			:					Any=None   ,       )       -> List[Any]:


     '''simple docstring'''




     if attention_mask is None:
          UpperCAmelCase_			       =  tf.cast(tf.math.not_equal(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 			__A (					UpperCamelCase__						,      UpperCamelCase__						,      unittest.TestCase      ):
    a__			:		List[Any]			  =    (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
    a__			:		int			  =    (TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
    a__			:		Any			  =    (
        {
            """conversational""": TFBlenderbotForConditionalGeneration,
            """feature-extraction""": TFBlenderbotModel,
            """summarization""": TFBlenderbotForConditionalGeneration,
            """text2text-generation""": TFBlenderbotForConditionalGeneration,
            """translation""": TFBlenderbotForConditionalGeneration,
        }
        if is_tf_available()
        else {}
    )
    a__			:		Dict			  =    True
    a__			:		Union[str, Any]			  =    False
    a__			:		Union[str, Any]			  =    False



    def    _lowercase		(self       :       Optional[Any]					):
         UpperCAmelCase_			       =  TFBlenderbotModelTester(self					)
         UpperCAmelCase_			       =  ConfigTester(self  ,     config_class=__a					)



    def    _lowercase		(self       :       Any					):
         self.config_tester.run_common_tests()






    def    _lowercase		(self       :       Dict					):
         UpperCAmelCase_			       =  self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.check_decoder_model_past_large_inputs(*__a					)




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



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



    @cached_property
    def    _lowercase		(self       :       Union[str, Any]					):
         UpperCAmelCase_			       =  TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name					)
         return model






    @slow
    def    _lowercase		(self       :       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=__a					)[0]
         assert (
             generated_words
             == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
         )






"""simple docstring"""





import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
		from transformers.trainer import DEFAULT_CALLBACKS

		from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel



class __lowerCAmelCase					(       _lowercase   ):




			"""simple docstring"""







			def __init__(					self    :       Optional[int]						)   ->			str:







								"""simple docstring"""
								A_								=    []




			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       List[str]							, _snake_case    :       Optional[int]							, _snake_case    :       Optional[int]							, **_snake_case    :       str						)   ->			int:







								"""simple docstring"""
								self.events.append("on_init_end"						)




			def   lowerCamelCase__  (					self    :       Dict							, _snake_case    :       Union[str, Any]							, _snake_case    :       List[Any]							, _snake_case    :       Tuple							, **_snake_case    :       Tuple						)   ->			List[str]:







								"""simple docstring"""
								self.events.append("on_train_begin"						)




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Any							, _snake_case    :       Optional[Any]							, _snake_case    :       Tuple							, **_snake_case    :       Optional[int]						)   ->			str:







								"""simple docstring"""
								self.events.append("on_train_end"						)




			def   lowerCamelCase__  (					self    :       Dict							, _snake_case    :       str							, _snake_case    :       List[Any]							, _snake_case    :       str							, **_snake_case    :       Optional[int]						)   ->			Dict:







								"""simple docstring"""
								self.events.append("on_epoch_begin"						)




			def   lowerCamelCase__  (					self    :       Optional[int]							, _snake_case    :       Optional[int]							, _snake_case    :       Any							, _snake_case    :       Optional[Any]							, **_snake_case    :       Optional[Any]						)   ->			str:







								"""simple docstring"""
								self.events.append("on_epoch_end"						)




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       List[str]							, _snake_case    :       str							, _snake_case    :       Optional[Any]							, **_snake_case    :       Optional[int]						)   ->			List[str]:







								"""simple docstring"""
								self.events.append("on_step_begin"						)




			def   lowerCamelCase__  (					self    :       Optional[Any]							, _snake_case    :       Union[str, Any]							, _snake_case    :       Optional[Any]							, _snake_case    :       Any							, **_snake_case    :       Union[str, Any]						)   ->			List[str]:







								"""simple docstring"""
								self.events.append("on_step_end"						)




			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       Dict							, _snake_case    :       Dict							, _snake_case    :       Union[str, Any]							, **_snake_case    :       List[str]						)   ->			Optional[int]:







								"""simple docstring"""
								self.events.append("on_evaluate"						)




			def   lowerCamelCase__  (					self    :       Optional[int]							, _snake_case    :       Optional[Any]							, _snake_case    :       int							, _snake_case    :       int							, **_snake_case    :       List[Any]						)   ->			List[str]:







								"""simple docstring"""
								self.events.append("on_predict"						)




			def   lowerCamelCase__  (					self    :       List[str]							, _snake_case    :       Union[str, Any]							, _snake_case    :       List[str]							, _snake_case    :       Optional[Any]							, **_snake_case    :       List[Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								self.events.append("on_save"						)




			def   lowerCamelCase__  (					self    :       Dict							, _snake_case    :       Dict							, _snake_case    :       List[Any]							, _snake_case    :       Optional[int]							, **_snake_case    :       Dict						)   ->			str:







								"""simple docstring"""
								self.events.append("on_log"						)






			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       Optional[Any]							, _snake_case    :       str							, _snake_case    :       int							, **_snake_case    :       List[Any]						)   ->			int:







								"""simple docstring"""
								self.events.append("on_prediction_step"						)





@require_torch
class __lowerCAmelCase					(       unittest.TestCase   ):




			"""simple docstring"""







			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			Dict:







								"""simple docstring"""
								A_								=    tempfile.mkdtemp()




			def   lowerCamelCase__  (					self    :       str						)   ->			Tuple:







								"""simple docstring"""
								shutil.rmtree(self.output_dir						)




			def   lowerCamelCase__  (					self    :       List[str]							, _snake_case    :       Any=0							, _snake_case    :       Optional[int]=0							, _snake_case    :       int=64							, _snake_case    :       Optional[int]=64							, _snake_case    :       List[Any]=None							, _snake_case    :       List[Any]=False							, **_snake_case    :       Optional[int]						)   ->			List[Any]:







								"""simple docstring"""
								# disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure
								# its set to False since the tests later on depend on its value.
								A_								=    RegressionDataset(length=_snake_case						)
								A_								=    RegressionDataset(length=_snake_case						)
								A_								=    RegressionModelConfig(a=_snake_case							, b=_snake_case						)
								A_								=    RegressionPreTrainedModel(_snake_case						)

								A_								=    TrainingArguments(self.output_dir							, disable_tqdm=_snake_case							, report_to=[]							, **_snake_case						)
								return Trainer(
								    _snake_case							, _snake_case							, train_dataset=_snake_case							, eval_dataset=_snake_case							, callbacks=_snake_case							, )




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Optional[Any]							, _snake_case    :       List[Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								self.assertEqual(len(_snake_case						)							, len(_snake_case						)						)

								# Order doesn't matter
								A_								=    sorted(_snake_case							, key=lambda _snake_case						: cb.__name__ if isinstance(_snake_case							, _snake_case						) else cb.__class__.__name__						)
								A_								=    sorted(_snake_case							, key=lambda _snake_case						: cb.__name__ if isinstance(_snake_case							, _snake_case						) else cb.__class__.__name__						)

								for cba, cba in zip(_snake_case							, _snake_case						):
													if isinstance(_snake_case							, _snake_case						) and isinstance(_snake_case							, _snake_case						):
																		self.assertEqual(_snake_case							, _snake_case						)
													elif isinstance(_snake_case							, _snake_case						) and not isinstance(_snake_case							, _snake_case						):
																		self.assertEqual(_snake_case							, cba.__class__						)
													elif not isinstance(_snake_case							, _snake_case						) and isinstance(_snake_case							, _snake_case						):
																		self.assertEqual(cba.__class__							, _snake_case						)
													else:
																		self.assertEqual(_snake_case							, _snake_case						)




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Union[str, Any]						)   ->			Optional[Any]:







								"""simple docstring"""
								A_								=    ["on_init_end", "on_train_begin"]
								A_								=    0
								A_								=    len(trainer.get_eval_dataloader()						)
								A_								=    ["on_prediction_step"] * len(trainer.get_eval_dataloader()						) + ["on_log", "on_evaluate"]
								for _ in range(trainer.state.num_train_epochs						):
													expected_events.append("on_epoch_begin"						)
													for _ in range(_snake_case						):
																		step += 1
																		expected_events += ["on_step_begin", "on_step_end"]
																		if step % trainer.args.logging_steps == 0:
																							expected_events.append("on_log"						)
																		if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
																							expected_events += evaluation_events.copy()
																		if step % trainer.args.save_steps == 0:
																							expected_events.append("on_save"						)
													expected_events.append("on_epoch_end"						)
													if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
																		expected_events += evaluation_events.copy()
								expected_events += ["on_log", "on_train_end"]
								return expected_events




			def   lowerCamelCase__  (					self    :       List[Any]						)   ->			int:







								"""simple docstring"""
								A_								=    self.get_trainer()
								A_								=    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								# Callbacks passed at init are added to the default callbacks
								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]						)
								expected_callbacks.append(_snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
								A_								=    self.get_trainer(disable_tqdm=_snake_case						)
								A_								=    DEFAULT_CALLBACKS.copy() + [PrinterCallback]
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)




			def   lowerCamelCase__  (					self    :       Optional[Any]						)   ->			List[Any]:







								"""simple docstring"""
								A_								=    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
								A_								=    self.get_trainer()

								# We can add, pop, or remove by class name
								trainer.remove_callback(_snake_case						)
								expected_callbacks.remove(_snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								A_								=    self.get_trainer()
								A_								=    trainer.pop_callback(_snake_case						)
								self.assertEqual(cb.__class__							, _snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								trainer.add_callback(_snake_case						)
								expected_callbacks.insert(0							, _snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								# We can also add, pop, or remove by instance
								A_								=    self.get_trainer()
								A_								=    trainer.callback_handler.callbacks[0]
								trainer.remove_callback(_snake_case						)
								expected_callbacks.remove(_snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								A_								=    self.get_trainer()
								A_								=    trainer.callback_handler.callbacks[0]
								A_								=    trainer.pop_callback(_snake_case						)
								self.assertEqual(_snake_case							, _snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								trainer.add_callback(_snake_case						)
								expected_callbacks.insert(0							, _snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)






			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								import warnings

								# XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
								warnings.simplefilter(action="ignore"							, category=_snake_case						)

								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								# Independent log/save/eval
								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]							, logging_steps=5						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]							, save_steps=5						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]							, eval_steps=5							, evaluation_strategy="steps"						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]							, evaluation_strategy="epoch"						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								# A bit of everything
								A_								=    self.get_trainer(
								    callbacks=[MyTestTrainerCallback]							, logging_steps=3							, save_steps=10							, eval_steps=5							, evaluation_strategy="steps"							, )
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								# warning should be emitted for duplicated callbacks
								with patch("transformers.trainer_callback.logger.warning"						) as warn_mock:
													A_								=    self.get_trainer(
													    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]							, )
													assert str(_snake_case						) in warn_mock.call_args[0][0]




