

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     :							Any			,snake_case     :							str			,snake_case     :							int=13			,snake_case     :							Union[str, Any]=7			,snake_case     :							Optional[Any]=True			,snake_case     :							Optional[int]=False			,snake_case     :							Optional[int]=99			,snake_case     :							str=32			,snake_case     :							Dict=2			,snake_case     :							int=4			,snake_case     :							List[Any]=37			,snake_case     :							Union[str, Any]=0.1			,snake_case     :							Optional[Any]=0.1			,snake_case     :							Optional[int]=20			,snake_case     :							int=2			,snake_case     :							Optional[Any]=1			,snake_case     :							List[str]=0			,):
           SCREAMING_SNAKE_CASE      						=parent
           SCREAMING_SNAKE_CASE      						=batch_size
           SCREAMING_SNAKE_CASE      						=seq_length
           SCREAMING_SNAKE_CASE      						=is_training
           SCREAMING_SNAKE_CASE      						=use_labels
           SCREAMING_SNAKE_CASE      						=vocab_size
           SCREAMING_SNAKE_CASE      						=hidden_size
           SCREAMING_SNAKE_CASE      						=num_hidden_layers
           SCREAMING_SNAKE_CASE      						=num_attention_heads
           SCREAMING_SNAKE_CASE      						=intermediate_size

           SCREAMING_SNAKE_CASE      						=hidden_dropout_prob
           SCREAMING_SNAKE_CASE      						=attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE      						=max_position_embeddings
           SCREAMING_SNAKE_CASE      						=eos_token_id
           SCREAMING_SNAKE_CASE      						=pad_token_id
           SCREAMING_SNAKE_CASE      						=bos_token_id
      def        _lowerCAmelCase     (      self     :							Tuple				):
           SCREAMING_SNAKE_CASE      						=ids_tensor([self.batch_size, self.seq_length - 1]			,self.vocab_size				)
           SCREAMING_SNAKE_CASE      						=tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size				)			,1				)
           SCREAMING_SNAKE_CASE      						=tf.concat([input_ids, eos_tensor]			,axis=1				)

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

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


      def        _lowerCAmelCase     (      self     :							List[str]			,snake_case     :							int			,snake_case     :							Optional[int]				):
           SCREAMING_SNAKE_CASE      						=TFBlenderbotModel(config=__snake_case				).get_decoder()
           SCREAMING_SNAKE_CASE      						=inputs_dict['''input_ids''']

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

           # first forward pass
           SCREAMING_SNAKE_CASE      						=model(__snake_case			,attention_mask=__snake_case			,head_mask=__snake_case			,use_cache=__snake_case				)

           SCREAMING_SNAKE_CASE      						=outputs.to_tuple()

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

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

           SCREAMING_SNAKE_CASE      						=model(__snake_case			,attention_mask=__snake_case				)[0]
           SCREAMING_SNAKE_CASE      						=model(__snake_case			,attention_mask=__snake_case			,past_key_values=__snake_case				)[0]

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

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

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







def 			snake_case__				(   lowerCAmelCase_,    lowerCAmelCase_,    lowerCAmelCase_,    lowerCAmelCase_=None,    lowerCAmelCase_=None,    lowerCAmelCase_=None,    lowerCAmelCase_=None,    lowerCAmelCase_=None,    ):





     """simple docstring"""







     if attention_mask is None:
          SCREAMING_SNAKE_CASE      						=tf.cast(tf.math.not_equal(_lowerCAmelCase,    config.pad_token_id			),    tf.inta			)
     if decoder_attention_mask is None:
          SCREAMING_SNAKE_CASE      						=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:
          SCREAMING_SNAKE_CASE      						=tf.ones((config.encoder_layers, config.encoder_attention_heads)			)
     if decoder_head_mask is None:
          SCREAMING_SNAKE_CASE      						=tf.ones((config.decoder_layers, config.decoder_attention_heads)			)
     if cross_attn_head_mask is None:
          SCREAMING_SNAKE_CASE      						=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_       (							A__ ,			A__ ,			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        _lowerCAmelCase     (      self     :							str				):
           SCREAMING_SNAKE_CASE      						=TFBlenderbotModelTester(self				)
           SCREAMING_SNAKE_CASE      						=ConfigTester(self			,config_class=__snake_case				)
      def        _lowerCAmelCase     (      self     :							int				):
           self.config_tester.run_common_tests()


      def        _lowerCAmelCase     (      self     :							Any				):
           SCREAMING_SNAKE_CASE      						=self.model_tester.prepare_config_and_inputs_for_common()
           self.model_tester.check_decoder_model_past_large_inputs(*__snake_case				)







@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        _lowerCAmelCase     (      self     :							Union[str, Any]				):
           return BlenderbotTokenizer.from_pretrained(self.model_name				)
      @cached_property
      def        _lowerCAmelCase     (      self     :							Union[str, Any]				):
           SCREAMING_SNAKE_CASE      						=TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name				)
           return model


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






'''simple docstring'''






import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin

class 				SCREAMING_SNAKE_CASE(							A__			):







	"""simple docstring"""






	lowerCamelCase__			 =       """MCTCTFeatureExtractor"""
	lowerCamelCase__			 =       """AutoTokenizer"""







	def __init__(						self			:     Dict		,			__snake_case			:     Optional[int]		,			__snake_case			:     List[str]		)			->		str:
								super().__init__(__snake_case		,			__snake_case		)
								UpperCAmelCase :       List[Any] 							=		self.feature_extractor
								UpperCAmelCase :       Union[str, Any] 							=		False







	def __call__(						self			:     Any		,			*__snake_case			:     List[str]		,			**__snake_case			:     Any		)			->		List[Any]:
								# For backward compatibility
								if self._in_target_context_manager:
															return self.current_processor(*__snake_case		,			**__snake_case		)

								if "raw_speech" in kwargs:
															warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''		)
															UpperCAmelCase :       int 							=		kwargs.pop('''raw_speech'''		)
								else:
															UpperCAmelCase :       Union[str, Any] 							=		kwargs.pop('''audio'''		,			__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		kwargs.pop('''sampling_rate'''		,			__snake_case		)
								UpperCAmelCase :       Dict 							=		kwargs.pop('''text'''		,			__snake_case		)
								if len(__snake_case		) > 0:
															UpperCAmelCase :       Any 							=		args[0]
															UpperCAmelCase :       Optional[int] 							=		args[1:]

								if audio is None and text is None:
															raise ValueError('''You need to specify either an `audio` or `text` input to process.'''		)

								if audio is not None:
															UpperCAmelCase :       List[str] 							=		self.feature_extractor(__snake_case		,			*__snake_case		,			sampling_rate=__snake_case		,			**__snake_case		)
								if text is not None:
															UpperCAmelCase :       int 							=		self.tokenizer(__snake_case		,			**__snake_case		)

								if text is None:
															return inputs
								elif audio is None:
															return encodings
								else:
															UpperCAmelCase :       str 							=		encodings['''input_ids''']
															return inputs







	def      A	(						self			:     List[Any]		,			*__snake_case			:     List[Any]		,			**__snake_case			:     List[Any]		)			->		str:
								return self.tokenizer.batch_decode(*__snake_case		,			**__snake_case		)







	def      A	(						self			:     List[Any]		,			*__snake_case			:     int		,			**__snake_case			:     Optional[int]		)			->		Any:
								# For backward compatibility
								if self._in_target_context_manager:
															return self.current_processor.pad(*__snake_case		,			**__snake_case		)

								UpperCAmelCase :       List[Any] 							=		kwargs.pop('''input_features'''		,			__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		kwargs.pop('''labels'''		,			__snake_case		)
								if len(__snake_case		) > 0:
															UpperCAmelCase :       List[str] 							=		args[0]
															UpperCAmelCase :       List[Any] 							=		args[1:]

								if input_features is not None:
															UpperCAmelCase :       Tuple 							=		self.feature_extractor.pad(__snake_case		,			*__snake_case		,			**__snake_case		)
								if labels is not None:
															UpperCAmelCase :       Optional[int] 							=		self.tokenizer.pad(__snake_case		,			**__snake_case		)

								if labels is None:
															return input_features
								elif input_features is None:
															return labels
								else:
															UpperCAmelCase :       List[str] 							=		labels['''input_ids''']
															return input_features







	def      A	(						self			:     Union[str, Any]		,			*__snake_case			:     Optional[Any]		,			**__snake_case			:     Optional[int]		)			->		Optional[Any]:
								return self.tokenizer.decode(*__snake_case		,			**__snake_case		)







	@contextmanager
	def      A	(						self			:     Any		)			->		Optional[int]:
								warnings.warn(
								    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
								    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
								    '''your audio inputs, or in a separate call.'''		)
								UpperCAmelCase :       Dict 							=		True
								UpperCAmelCase :       List[Any] 							=		self.tokenizer
								yield
								UpperCAmelCase :       Tuple 							=		self.feature_extractor
								UpperCAmelCase :       List[Any] 							=		False





