






from __future__ import annotations

import unittest

from transformers import AutoTokenizer, MBartConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, 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, TFMBartForConditionalGeneration, TFMBartModel


@require_tf
class 							__UpperCamelCase     :
	__snake_case						:Optional[Any] =							MBartConfig
	__snake_case						:str =							{}
	__snake_case						:str =							'gelu'







	def __init__(	self   :				Dict							,					_lowerCAmelCase   :				Dict							,					_lowerCAmelCase   :				List[str]=13							,					_lowerCAmelCase   :				Union[str, Any]=7							,					_lowerCAmelCase   :				List[str]=True							,					_lowerCAmelCase   :				List[Any]=False							,					_lowerCAmelCase   :				Tuple=99							,					_lowerCAmelCase   :				Dict=32							,					_lowerCAmelCase   :				List[Any]=2							,					_lowerCAmelCase   :				Optional[int]=4							,					_lowerCAmelCase   :				str=37							,					_lowerCAmelCase   :				Dict=0.1							,					_lowerCAmelCase   :				Tuple=0.1							,					_lowerCAmelCase   :				Optional[Any]=20							,					_lowerCAmelCase   :				Optional[int]=2							,					_lowerCAmelCase   :				int=1							,					_lowerCAmelCase   :				List[str]=0							,					)		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	parent
		__lowercase   							=	batch_size
		__lowercase   							=	seq_length
		__lowercase   							=	is_training
		__lowercase   							=	use_labels
		__lowercase   							=	vocab_size
		__lowercase   							=	hidden_size
		__lowercase   							=	num_hidden_layers
		__lowercase   							=	num_attention_heads
		__lowercase   							=	intermediate_size
		__lowercase   							=	hidden_dropout_prob
		__lowercase   							=	attention_probs_dropout_prob
		__lowercase   							=	max_position_embeddings
		__lowercase   							=	eos_token_id
		__lowercase   							=	pad_token_id
		__lowercase   							=	bos_token_id







	def 			_a (	self   :				int      )		->			Dict:
		"""simple docstring"""







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

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

		__lowercase   							=	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							,					)
		__lowercase   							=	prepare_mbart_inputs_dict(_lowerCAmelCase							,					_lowerCAmelCase							,					_lowerCAmelCase      )
		return config, inputs_dict







	def 			_a (	self   :				int							,					_lowerCAmelCase   :				Union[str, Any]							,					_lowerCAmelCase   :				str      )		->			int:
		"""simple docstring"""







		__lowercase   							=	TFMBartModel(config=_lowerCAmelCase      ).get_decoder()
		__lowercase   							=	inputs_dict["""input_ids"""]

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

		# first forward pass
		__lowercase   							=	model(_lowerCAmelCase							,					attention_mask=_lowerCAmelCase							,					head_mask=_lowerCAmelCase							,					use_cache=_lowerCAmelCase      )

		__lowercase							,    __lowercase   							=	outputs.to_tuple()
		__lowercase   							=	past_key_values[1]





def 					snake_case	(  lowerCamelCase					,	lowerCamelCase					,	lowerCamelCase					,	lowerCamelCase=None					,	lowerCamelCase=None					,	lowerCamelCase=None					,	lowerCamelCase=None					,	lowerCamelCase=None					,	):




	'''simple docstring'''







	if attention_mask is None:
		__lowercase   							=	tf.cast(tf.math.not_equal(lowerCamelCase					,	config.pad_token_id       )					,	tf.inta       )
	if decoder_attention_mask is None:
		__lowercase   							=	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:
		__lowercase   							=	tf.ones((config.encoder_layers, config.encoder_attention_heads)       )
	if decoder_head_mask is None:
		__lowercase   							=	tf.ones((config.decoder_layers, config.decoder_attention_heads)       )
	if cross_attn_head_mask is None:
		__lowercase   							=	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     (  _lowerCAmelCase ,      _lowerCAmelCase ,      unittest.TestCase ):
	__snake_case						:List[Any] =							(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
	__snake_case						:Optional[int] =							(TFMBartForConditionalGeneration,) if is_tf_available() else ()
	__snake_case						:Any =							(
	    {
	        'conversational': TFMBartForConditionalGeneration,
	        'feature-extraction': TFMBartModel,
	        'summarization': TFMBartForConditionalGeneration,
	        'text2text-generation': TFMBartForConditionalGeneration,
	        'translation': TFMBartForConditionalGeneration,
	    }
	    if is_tf_available()
	    else {}
	)
	__snake_case						:str =							True
	__snake_case						:List[str] =							False
	__snake_case						:Tuple =							False







	def 			_a (	self   :				Optional[int]							,					_lowerCAmelCase   :				Any							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				List[str]      )		->			Tuple:
		"""simple docstring"""







		if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
			# Exception encountered when calling layer '...'
			return True

		return False







	def 			_a (	self   :				str      )		->			Optional[Any]:
		"""simple docstring"""







		__lowercase   							=	TFMBartModelTester(self      )
		__lowercase   							=	ConfigTester(self							,					config_class=_lowerCAmelCase      )







	def 			_a (	self   :				List[str]      )		->			Optional[int]:
		"""simple docstring"""







		self.config_tester.run_common_tests()







	def 			_a (	self   :				Any      )		->			Tuple:
		"""simple docstring"""







		__lowercase   							=	self.model_tester.prepare_config_and_inputs_for_common()
		self.model_tester.check_decoder_model_past_large_inputs(*_lowerCAmelCase      )


@require_sentencepiece
@require_tokenizers
@require_tf
class 							__UpperCamelCase     (  unittest.TestCase ):
	__snake_case						:Union[str, Any] =							[
	    ' UN Chief Says There Is No Military Solution in Syria',
	]
	__snake_case						:Optional[Any] =							[
	    'Şeful ONU declară că nu există o soluţie militară în Siria',
	]
	__snake_case						:List[Any] =							'facebook/mbart-large-en-ro'







	@cached_property
	def 			_a (	self   :				List[Any]      )		->			Any:
		"""simple docstring"""







		return AutoTokenizer.from_pretrained(self.model_name      )







	@cached_property
	def 			_a (	self   :				Union[str, Any]      )		->			Any:
		"""simple docstring"""







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







	def 			_a (	self   :				str							,					**_lowerCAmelCase   :				Tuple      )		->			int:
		"""simple docstring"""







		__lowercase   							=	self.translate_src_text(**_lowerCAmelCase      )
		self.assertListEqual(self.expected_text							,					_lowerCAmelCase      )







	def 			_a (	self   :				Dict							,					**_lowerCAmelCase   :				str      )		->			Any:
		"""simple docstring"""







		__lowercase   							=	self.tokenizer(self.src_text							,					**_lowerCAmelCase							,					return_tensors="""tf"""      )
		__lowercase   							=	self.model.generate(
		    model_inputs.input_ids							,					attention_mask=model_inputs.attention_mask							,					num_beams=2      )
		__lowercase   							=	self.tokenizer.batch_decode(_lowerCAmelCase							,					skip_special_tokens=_lowerCAmelCase      )
		return generated_words







	@slow
	def 			_a (	self   :				List[str]      )		->			List[Any]:
		"""simple docstring"""







		self._assert_generated_batch_equal_expected()





import functools
import operator

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


snake_case							:       Optional[int]										=	logging.get_logger(__name__)

snake_case							:       Optional[int]										=	{
    '''microsoft/unispeech-large-1500h-cv''': (
        '''https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json'''
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}







class 						_snake_case    ( _snake_case				):
 SCREAMING_SNAKE_CASE__ =			'unispeech'




 def __init__(						self   ,    _lowerCamelCase=32   ,    _lowerCamelCase=768   ,    _lowerCamelCase=12   ,    _lowerCamelCase=12   ,    _lowerCamelCase=3072   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.0   ,    _lowerCamelCase=0.0   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.02   ,    _lowerCamelCase=1e-5   ,    _lowerCamelCase="group"   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=(512, 512, 512, 512, 512, 512, 512)   ,    _lowerCamelCase=(5, 2, 2, 2, 2, 2, 2)   ,    _lowerCamelCase=(10, 3, 3, 3, 3, 2, 2)   ,    _lowerCamelCase=False   ,    _lowerCamelCase=128   ,    _lowerCamelCase=16   ,    _lowerCamelCase=False   ,    _lowerCamelCase=True   ,    _lowerCamelCase=0.05   ,    _lowerCamelCase=10   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.0   ,    _lowerCamelCase=10   ,    _lowerCamelCase=0   ,    _lowerCamelCase=320   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=100   ,    _lowerCamelCase=256   ,    _lowerCamelCase=256   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase="mean"   ,    _lowerCamelCase=False   ,    _lowerCamelCase=False   ,    _lowerCamelCase=256   ,    _lowerCamelCase=80   ,    _lowerCamelCase=0   ,    _lowerCamelCase=1   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.5   ,    **_lowerCamelCase   ,    ):
       super().__init__(**_lowerCamelCase   ,    pad_token_id=_lowerCamelCase   ,    bos_token_id=_lowerCamelCase   ,    eos_token_id=_lowerCamelCase       )
       a		:Any									=			hidden_size
       a		:str									=			feat_extract_norm
       a		:List[Any]									=			feat_extract_activation
       a		:Tuple									=			list(_lowerCamelCase       )
       a		:Any									=			list(_lowerCamelCase       )
       a		:List[Any]									=			list(_lowerCamelCase       )
       a		:Union[str, Any]									=			conv_bias
       a		:str									=			num_conv_pos_embeddings
       a		:str									=			num_conv_pos_embedding_groups
       a		:Tuple									=			len(self.conv_dim       )
       a		:int									=			num_hidden_layers
       a		:Any									=			intermediate_size
       a		:Optional[Any]									=			hidden_act
       a		:Tuple									=			num_attention_heads
       a		:Any									=			hidden_dropout
       a		:Any									=			attention_dropout
       a		:Optional[Any]									=			activation_dropout
       a		:Optional[Any]									=			feat_proj_dropout
       a		:Any									=			final_dropout
       a		:int									=			layerdrop
       a		:int									=			layer_norm_eps
       a		:Dict									=			initializer_range
       a		:Dict									=			num_ctc_classes
       a		:Optional[Any]									=			vocab_size
       a		:str									=			do_stable_layer_norm
       a		:Tuple									=			use_weighted_layer_sum
       a		:Any									=			classifier_proj_size

       if (
           (len(self.conv_stride       ) != self.num_feat_extract_layers)
           or (len(self.conv_kernel       ) != self.num_feat_extract_layers)
           or (len(self.conv_dim       ) != self.num_feat_extract_layers)
       ):
             raise ValueError(
                 '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
                 ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
                 F''' {len(self.conv_dim       )}`, `len(config.conv_stride) = {len(self.conv_stride       )}`,'''
                 F''' `len(config.conv_kernel) = {len(self.conv_kernel       )}`.'''       )

       # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
       a		:List[Any]									=			apply_spec_augment
       a		:Any									=			mask_time_prob
       a		:Union[str, Any]									=			mask_time_length
       a		:str									=			mask_time_min_masks
       a		:Tuple									=			mask_feature_prob
       a		:Dict									=			mask_feature_length
       a		:int									=			mask_feature_min_masks

       # parameters for pretraining with codevector quantized representations
       a		:Union[str, Any]									=			num_codevectors_per_group
       a		:Dict									=			num_codevector_groups
       a		:List[Any]									=			contrastive_logits_temperature
       a		:Union[str, Any]									=			feat_quantizer_dropout
       a		:Optional[Any]									=			num_negatives
       a		:Tuple									=			codevector_dim
       a		:Optional[Any]									=			proj_codevector_dim
       a		:Union[str, Any]									=			diversity_loss_weight

       # ctc loss
       a		:List[Any]									=			ctc_loss_reduction
       a		:Union[str, Any]									=			ctc_zero_infinity

       # pretraining loss
       a		:int									=			replace_prob




 @property
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       return functools.reduce(operator.mul   ,    self.conv_stride   ,    1       )





