




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






				'''simple docstring'''
				__UpperCamelCase         =		BlenderbotConfig
				__UpperCamelCase         =		{}
				__UpperCamelCase         =		"gelu"





				def __init__(			self						,					_a						,					_a=13						,					_a=7						,					_a=True						,					_a=False						,					_a=99						,					_a=32						,					_a=2						,					_a=4						,					_a=37						,					_a=0.1						,					_a=0.1						,					_a=20						,					_a=2						,					_a=1						,					_a=0						,					):





							"""simple docstring"""
							lowerCamelCase      =   parent
							lowerCamelCase      =   batch_size
							lowerCamelCase      =   seq_length
							lowerCamelCase      =   is_training
							lowerCamelCase      =   use_labels
							lowerCamelCase      =   vocab_size
							lowerCamelCase      =   hidden_size
							lowerCamelCase      =   num_hidden_layers
							lowerCamelCase      =   num_attention_heads
							lowerCamelCase      =   intermediate_size

							lowerCamelCase      =   hidden_dropout_prob
							lowerCamelCase      =   attention_probs_dropout_prob
							lowerCamelCase      =   max_position_embeddings
							lowerCamelCase      =   eos_token_id
							lowerCamelCase      =   pad_token_id
							lowerCamelCase      =   bos_token_id





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   ids_tensor([self.batch_size, self.seq_length - 1]						,					self.vocab_size					)
							lowerCamelCase      =   tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size					)						,					1					)
							lowerCamelCase      =   tf.concat([input_ids, eos_tensor]						,					axis=1					)

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

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





				def   _lowerCAmelCase							(			self						,					_a						,					_a					):





							"""simple docstring"""
							lowerCamelCase      =   TFBlenderbotModel(config=_a					).get_decoder()
							lowerCamelCase      =   inputs_dict["""input_ids"""]

							lowerCamelCase      =   input_ids[:1, :]
							lowerCamelCase      =   inputs_dict["""attention_mask"""][:1, :]
							lowerCamelCase      =   inputs_dict["""head_mask"""]
							lowerCamelCase      =   1

							# first forward pass
							lowerCamelCase      =   model(_a						,					attention_mask=_a						,					head_mask=_a						,					use_cache=_a					)

							lowerCamelCase ,  lowerCamelCase      =   outputs.to_tuple()

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

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

							lowerCamelCase      =   model(_a						,					attention_mask=_a					)[0]
							lowerCamelCase      =   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
							lowerCamelCase      =   int(ids_tensor((1,)						,					output_from_past.shape[-1]					)					)
							lowerCamelCase      =   output_from_no_past[:, -3:, random_slice_idx]
							lowerCamelCase      =   output_from_past[:, :, random_slice_idx]

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




def        a__					(							snake_case__			,					snake_case__			,					snake_case__			,					snake_case__=None			,					snake_case__=None			,					snake_case__=None			,					snake_case__=None			,					snake_case__=None			,					)		->   Optional[int]:
			if attention_mask is None:
						lowerCamelCase      =   tf.cast(tf.math.not_equal(snake_case__			,					config.pad_token_id							)			,					tf.inta							)
			if decoder_attention_mask is None:
						lowerCamelCase      =   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:
						lowerCamelCase      =   tf.ones((config.encoder_layers, config.encoder_attention_heads)							)
			if decoder_head_mask is None:
						lowerCamelCase      =   tf.ones((config.decoder_layers, config.decoder_attention_heads)							)
			if cross_attn_head_mask is None:
						lowerCamelCase      =   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        __magic_name__				(			UpperCAmelCase__		,   UpperCAmelCase__		,   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					):





							"""simple docstring"""
							lowerCamelCase      =   TFBlenderbotModelTester(self					)
							lowerCamelCase      =   ConfigTester(self						,					config_class=_a					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							self.config_tester.run_common_tests()





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.model_tester.prepare_config_and_inputs_for_common()
							self.model_tester.check_decoder_model_past_large_inputs(*_a					)






@require_tokenizers
@require_tf
class        __magic_name__				(			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					):





							"""simple docstring"""
							return BlenderbotTokenizer.from_pretrained(self.model_name					)





				@cached_property
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name					)
							return model





				@slow
				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   self.tokenizer(self.src_text						,					return_tensors="""tf"""					)
							lowerCamelCase      =   self.model.generate(
							    model_inputs.input_ids						,					)
							lowerCamelCase      =   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"""




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


lowerCAmelCase				:     Union[str, Any]					=				logging.get_logger(__name__)

lowerCAmelCase				:     Union[str, Any]					=				{
    """speechbrain/m-ctc-t-large""": """https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json""",
    # See all M-CTC-T models at https://huggingface.co/models?filter=mctct
}






class        __magic_name__				(			UpperCAmelCase__							):






				'''simple docstring'''
				__UpperCamelCase         =		"mctct"





				def __init__(			self						,					_a=8_065						,					_a=1_536						,					_a=36						,					_a=6_144						,					_a=4						,					_a=384						,					_a=920						,					_a=1e-5						,					_a=0.3						,					_a="relu"						,					_a=0.02						,					_a=0.3						,					_a=0.3						,					_a=1						,					_a=0						,					_a=2						,					_a=1						,					_a=0.3						,					_a=1						,					_a=(7,)						,					_a=(3,)						,					_a=80						,					_a=1						,					_a=None						,					_a="sum"						,					_a=False						,					**_a						,					):





							"""simple docstring"""
							super().__init__(**_a						,					pad_token_id=_a						,					bos_token_id=_a						,					eos_token_id=_a					)
							lowerCamelCase      =   vocab_size
							lowerCamelCase      =   hidden_size
							lowerCamelCase      =   num_hidden_layers
							lowerCamelCase      =   intermediate_size
							lowerCamelCase      =   num_attention_heads
							lowerCamelCase      =   attention_head_dim
							lowerCamelCase      =   max_position_embeddings
							lowerCamelCase      =   layer_norm_eps
							lowerCamelCase      =   layerdrop
							lowerCamelCase      =   hidden_act
							lowerCamelCase      =   initializer_range
							lowerCamelCase      =   hidden_dropout_prob
							lowerCamelCase      =   attention_probs_dropout_prob
							lowerCamelCase      =   pad_token_id
							lowerCamelCase      =   bos_token_id
							lowerCamelCase      =   eos_token_id
							lowerCamelCase      =   conv_glu_dim
							lowerCamelCase      =   conv_dropout
							lowerCamelCase      =   num_conv_layers
							lowerCamelCase      =   input_feat_per_channel
							lowerCamelCase      =   input_channels
							lowerCamelCase      =   conv_channels
							lowerCamelCase      =   ctc_loss_reduction
							lowerCamelCase      =   ctc_zero_infinity

							# prevents config testing fail with exporting to json
							lowerCamelCase      =   list(_a					)
							lowerCamelCase      =   list(_a					)

							if len(self.conv_kernel					) != self.num_conv_layers:
										raise ValueError(
										    """Configuration for convolutional module is incorrect. """
										    """It is required that `len(config.conv_kernel)` == `config.num_conv_layers` """
										    f'but is `len(config.conv_kernel) = {len(self.conv_kernel					)}`, '
										    f'`config.num_conv_layers = {self.num_conv_layers}`.'					)



