



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 				lowerCAmelCase_		:
       UpperCAmelCase__				:					Tuple        =  BlenderbotConfig
       UpperCAmelCase__				:					int        =  {}
       UpperCAmelCase__				:					Optional[int]        =  "gelu"
       def __init__(			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=13,			SCREAMING_SNAKE_CASE_=7,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=False,			SCREAMING_SNAKE_CASE_=99,			SCREAMING_SNAKE_CASE_=32,			SCREAMING_SNAKE_CASE_=2,			SCREAMING_SNAKE_CASE_=4,			SCREAMING_SNAKE_CASE_=37,			SCREAMING_SNAKE_CASE_=0.1,			SCREAMING_SNAKE_CASE_=0.1,			SCREAMING_SNAKE_CASE_=20,			SCREAMING_SNAKE_CASE_=2,			SCREAMING_SNAKE_CASE_=1,			SCREAMING_SNAKE_CASE_=0,			)     ->		Optional[Any]:
          UpperCamelCase    :  List[str]							=	parent
          UpperCamelCase    :  Any							=	batch_size
          UpperCamelCase    :  Optional[Any]							=	seq_length
          UpperCamelCase    :  str							=	is_training
          UpperCamelCase    :  List[str]							=	use_labels
          UpperCamelCase    :  Any							=	vocab_size
          UpperCamelCase    :  Optional[int]							=	hidden_size
          UpperCamelCase    :  Optional[Any]							=	num_hidden_layers
          UpperCamelCase    :  Union[str, Any]							=	num_attention_heads
          UpperCamelCase    :  Optional[int]							=	intermediate_size

          UpperCamelCase    :  Optional[int]							=	hidden_dropout_prob
          UpperCamelCase    :  Dict							=	attention_probs_dropout_prob
          UpperCamelCase    :  Union[str, Any]							=	max_position_embeddings
          UpperCamelCase    :  Any							=	eos_token_id
          UpperCamelCase    :  Optional[int]							=	pad_token_id
          UpperCamelCase    :  Union[str, Any]							=	bos_token_id
       def 					snake_case_       (			self       )     ->		List[Any]:
          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    :  str							=	tf.concat([input_ids, eos_tensor],			axis=1       )

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

          UpperCamelCase    :  List[Any]							=	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    :  Optional[int]							=	prepare_blenderbot_inputs_dict(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )
          return config, inputs_dict






       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		Tuple:
          UpperCamelCase    :  Optional[Any]							=	TFBlenderbotModel(config=SCREAMING_SNAKE_CASE_       ).get_decoder()
          UpperCamelCase    :  Any							=	inputs_dict['input_ids']

          UpperCamelCase    :  List[str]							=	input_ids[:1, :]
          UpperCamelCase    :  List[str]							=	inputs_dict['attention_mask'][:1, :]
          UpperCamelCase    :  Optional[int]							=	inputs_dict['head_mask']
          UpperCamelCase    :  List[str]							=	1

          # first forward pass
          UpperCamelCase    :  Optional[int]							=	model(SCREAMING_SNAKE_CASE_,			attention_mask=SCREAMING_SNAKE_CASE_,			head_mask=SCREAMING_SNAKE_CASE_,			use_cache=SCREAMING_SNAKE_CASE_       )

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

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

          # append to next input_ids and
          UpperCamelCase    :  Union[str, Any]							=	tf.concat([input_ids, next_tokens],			axis=-1       )
          UpperCamelCase    :  Union[str, Any]							=	tf.concat([attention_mask, next_attn_mask],			axis=-1       )

          UpperCamelCase    :  Optional[Any]							=	model(SCREAMING_SNAKE_CASE_,			attention_mask=SCREAMING_SNAKE_CASE_       )[0]
          UpperCamelCase    :  Optional[Any]							=	model(SCREAMING_SNAKE_CASE_,			attention_mask=SCREAMING_SNAKE_CASE_,			past_key_values=SCREAMING_SNAKE_CASE_       )[0]

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

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

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






def 		UpperCamelCase	(   snake_case__       :    List[Any]       ,     snake_case__       :    Tuple       ,     snake_case__       :    Union[str, Any]       ,     snake_case__       :    List[Any]=None       ,     snake_case__       :    Any=None       ,     snake_case__       :    Tuple=None       ,     snake_case__       :    Union[str, Any]=None       ,     snake_case__       :    Dict=None       ,     )	->     Optional[int]:
   if attention_mask is None:
      UpperCamelCase    :  int							=	tf.cast(tf.math.not_equal(snake_case__       ,     config.pad_token_id	)       ,     tf.inta	)
   if decoder_attention_mask is None:
      UpperCamelCase    :  str							=	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    :  Optional[int]							=	tf.ones((config.encoder_layers, config.encoder_attention_heads)	)
   if decoder_head_mask is None:
      UpperCamelCase    :  List[Any]							=	tf.ones((config.decoder_layers, config.decoder_attention_heads)	)
   if cross_attn_head_mask is None:
      UpperCamelCase    :  Optional[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 				lowerCAmelCase_		(  a__       ,    a__       ,    unittest.TestCase							):
       UpperCAmelCase__				:					Optional[int]        =  (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
       UpperCAmelCase__				:					int        =  (TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
       UpperCAmelCase__				:					Optional[int]        =  (
           {
               "conversational": TFBlenderbotForConditionalGeneration,
               "feature-extraction": TFBlenderbotModel,
               "summarization": TFBlenderbotForConditionalGeneration,
               "text2text-generation": TFBlenderbotForConditionalGeneration,
               "translation": TFBlenderbotForConditionalGeneration,
           }
           if is_tf_available()
           else {}
       )
       UpperCAmelCase__				:					Union[str, Any]        =  True
       UpperCAmelCase__				:					List[str]        =  False
       UpperCAmelCase__				:					Optional[Any]        =  False
       def 					snake_case_       (			self       )     ->		Optional[Any]:
          UpperCamelCase    :  str							=	TFBlenderbotModelTester(self       )
          UpperCamelCase    :  int							=	ConfigTester(self,			config_class=SCREAMING_SNAKE_CASE_       )
       def 					snake_case_       (			self       )     ->		int:
          self.config_tester.run_common_tests()






       def 					snake_case_       (			self       )     ->		Optional[int]:
          UpperCamelCase    :  List[Any]							=	self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE_       )






@require_tokenizers
@require_tf
class 				lowerCAmelCase_		(  unittest.TestCase							):
       UpperCAmelCase__				:					List[str]        =  ["My friends are cool but they eat too many carbs."]
       UpperCAmelCase__				:					Tuple        =  "facebook/blenderbot-400M-distill"
       @cached_property
       def 					snake_case_       (			self       )     ->		List[str]:
          return BlenderbotTokenizer.from_pretrained(self.model_name       )
       @cached_property
       def 					snake_case_       (			self       )     ->		Union[str, Any]:
          UpperCamelCase    :  Dict							=	TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name       )
          return model






       @slow
       def 					snake_case_       (			self       )     ->		List[Any]:
          UpperCamelCase    :  List[Any]							=	self.tokenizer(self.src_text,			return_tensors='tf'       )
          UpperCamelCase    :  Union[str, Any]							=	self.model.generate(
              model_inputs.input_ids,			)
          UpperCamelCase    :  Union[str, Any]							=	self.tokenizer.batch_decode(generated_ids.numpy(),			skip_special_tokens=SCREAMING_SNAKE_CASE_       )[0]
          assert (
              generated_words
              == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
          )








def 		UpperCamelCase	(   snake_case__       :    int       ,     snake_case__       :    int	)	->     int:
   return int(input_a == input_a == 0	)
def 		UpperCamelCase	(   )	->     None:
   print('Truth Table of NOR Gate:'	)
   print('|   Input 1   |  Input 2  | Output |'	)
   print(F"""|      0      |    0      |  {nor_gate(0       ,     0	)}     |"""	)
   print(F"""|      0      |    1      |  {nor_gate(0       ,     1	)}     |"""	)
   print(F"""|      1      |    0      |  {nor_gate(1       ,     0	)}     |"""	)
   print(F"""|      1      |    1      |  {nor_gate(1       ,     1	)}     |"""	)


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    main()



