

"""simple docstring"""







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	:
   lowercase							=						MBartConfig
   lowercase							=						{}
   lowercase							=						"gelu"
   def __init__(    self  ,__UpperCamelCase  ,__UpperCamelCase=13  ,__UpperCamelCase=7  ,__UpperCamelCase=True  ,__UpperCamelCase=False  ,__UpperCamelCase=99  ,__UpperCamelCase=32  ,__UpperCamelCase=2  ,__UpperCamelCase=4  ,__UpperCamelCase=37  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.1  ,__UpperCamelCase=20  ,__UpperCamelCase=2  ,__UpperCamelCase=1  ,__UpperCamelCase=0  ,)			->   Optional[int]:


        '''simple docstring'''


        lowercase_			:       List[str]									=			parent
        lowercase_			:       List[Any]									=			batch_size
        lowercase_			:       List[str]									=			seq_length
        lowercase_			:       Optional[int]									=			is_training
        lowercase_			:       Tuple									=			use_labels
        lowercase_			:       Any									=			vocab_size
        lowercase_			:       List[str]									=			hidden_size
        lowercase_			:       Tuple									=			num_hidden_layers
        lowercase_			:       Optional[Any]									=			num_attention_heads
        lowercase_			:       int									=			intermediate_size
        lowercase_			:       str									=			hidden_dropout_prob
        lowercase_			:       Tuple									=			attention_probs_dropout_prob
        lowercase_			:       Union[str, Any]									=			max_position_embeddings
        lowercase_			:       List[str]									=			eos_token_id
        lowercase_			:       Dict									=			pad_token_id
        lowercase_			:       Optional[int]									=			bos_token_id
   def     _UpperCAmelCase     (    self    )			->   Dict:


        '''simple docstring'''


        lowercase_			:       Union[str, Any]									=			ids_tensor([self.batch_size, self.seq_length - 1]  ,self.vocab_size    )
        lowercase_			:       Dict									=			tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size    )  ,1    )
        lowercase_			:       Tuple									=			tf.concat([input_ids, eos_tensor]  ,axis=1    )

        lowercase_			:       Optional[int]									=			ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size    )

        lowercase_			:       str									=			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_			:       int									=			prepare_mbart_inputs_dict(_lowerCamelCase  ,_lowerCamelCase  ,_lowerCamelCase    )
        return config, inputs_dict







   def     _UpperCAmelCase     (    self  ,__UpperCamelCase  ,__UpperCamelCase    )			->   List[str]:


        '''simple docstring'''


        lowercase_			:       Tuple									=			TFMBartModel(config=_lowerCamelCase    ).get_decoder()
        lowercase_			:       List[str]									=			inputs_dict['input_ids']

        lowercase_			:       Union[str, Any]									=			input_ids[:1, :]
        lowercase_			:       Tuple									=			inputs_dict['attention_mask'][:1, :]
        lowercase_			:       List[str]									=			inputs_dict['head_mask']
        lowercase_			:       List[str]									=			1

        # first forward pass
        lowercase_			:       List[Any]									=			model(_lowerCamelCase  ,attention_mask=_lowerCamelCase  ,head_mask=_lowerCamelCase  ,use_cache=_lowerCamelCase    )

        lowercase_							,						lowercase_			:       Optional[Any]									=			outputs.to_tuple()
        lowercase_			:       Tuple									=			past_key_values[1]






def     lowercase__( __SCREAMING_SNAKE_CASE							:  Optional[Any]  ,    __SCREAMING_SNAKE_CASE							:  Union[str, Any]  ,    __SCREAMING_SNAKE_CASE							:  Any  ,    __SCREAMING_SNAKE_CASE							:  Optional[Any]=None  ,    __SCREAMING_SNAKE_CASE							:  str=None  ,    __SCREAMING_SNAKE_CASE							:  int=None  ,    __SCREAMING_SNAKE_CASE							:  Optional[Any]=None  ,    __SCREAMING_SNAKE_CASE							:  List[Any]=None  ,    ):
     if attention_mask is None:
          lowercase_			:       Any									=			tf.cast(tf.math.not_equal(_lowerCAmelCase  ,    config.pad_token_id   )  ,    tf.inta   )
     if decoder_attention_mask is None:
          lowercase_			:       Optional[int]									=			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_			:       List[Any]									=			tf.ones((config.encoder_layers, config.encoder_attention_heads)   )
     if decoder_head_mask is None:
          lowercase_			:       Optional[Any]									=			tf.ones((config.decoder_layers, config.decoder_attention_heads)   )
     if cross_attn_head_mask is None:
          lowercase_			:       Dict									=			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	(    __UpperCAmelCase		,   __UpperCAmelCase		,   unittest.TestCase						):
   lowercase							=						(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
   lowercase							=						(TFMBartForConditionalGeneration,) if is_tf_available() else ()
   lowercase							=						(
       {
           "conversational": TFMBartForConditionalGeneration,
           "feature-extraction": TFMBartModel,
           "summarization": TFMBartForConditionalGeneration,
           "text2text-generation": TFMBartForConditionalGeneration,
           "translation": TFMBartForConditionalGeneration,
       }
       if is_tf_available()
       else {}
   )
   lowercase							=						True
   lowercase							=						False
   lowercase							=						False
   def     _UpperCAmelCase     (    self  ,__UpperCamelCase  ,__UpperCamelCase  ,__UpperCamelCase  ,__UpperCamelCase  ,__UpperCamelCase    )			->   Union[str, Any]:


        '''simple docstring'''


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

        return False
   def     _UpperCAmelCase     (    self    )			->   int:


        '''simple docstring'''


        lowercase_			:       int									=			TFMBartModelTester(self    )
        lowercase_			:       Tuple									=			ConfigTester(self  ,config_class=_lowerCamelCase    )
   def     _UpperCAmelCase     (    self    )			->   List[Any]:


        '''simple docstring'''


        self.config_tester.run_common_tests()







   def     _UpperCAmelCase     (    self    )			->   Dict:


        '''simple docstring'''


        lowercase_			:       List[Any]									=			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						):
   lowercase							=						[
       " UN Chief Says There Is No Military Solution in Syria",
   ]
   lowercase							=						[
       "Şeful ONU declară că nu există o soluţie militară în Siria",
   ]
   lowercase							=						"facebook/mbart-large-en-ro"
   @cached_property
   def     _UpperCAmelCase     (    self    )			->   Union[str, Any]:


        '''simple docstring'''


        return AutoTokenizer.from_pretrained(self.model_name    )
   @cached_property
   def     _UpperCAmelCase     (    self    )			->   Dict:


        '''simple docstring'''


        lowercase_			:       Optional[int]									=			TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name    )
        return model
   def     _UpperCAmelCase     (    self  ,**__UpperCamelCase    )			->   List[Any]:


        '''simple docstring'''


        lowercase_			:       List[str]									=			self.translate_src_text(**_lowerCamelCase    )
        self.assertListEqual(self.expected_text  ,_lowerCamelCase    )
   def     _UpperCAmelCase     (    self  ,**__UpperCamelCase    )			->   int:


        '''simple docstring'''


        lowercase_			:       Union[str, Any]									=			self.tokenizer(self.src_text  ,**_lowerCamelCase  ,return_tensors='tf'    )
        lowercase_			:       Optional[int]									=			self.model.generate(
            model_inputs.input_ids  ,attention_mask=model_inputs.attention_mask  ,num_beams=2    )
        lowercase_			:       str									=			self.tokenizer.batch_decode(_lowerCamelCase  ,skip_special_tokens=_lowerCamelCase    )
        return generated_words







   @slow
   def     _UpperCAmelCase     (    self    )			->   Union[str, Any]:


        '''simple docstring'''


        self._assert_generated_batch_equal_expected()


"""simple docstring"""







import functools
import operator

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


__SCREAMING_SNAKE_CASE								=logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE								={
    "facebook/wav2vec2-base-960h": "https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json",
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}







class  UpperCamelCase	(    lowercase_						):
   lowercase							=						'wav2vec2'
   def __init__(    self  ,__UpperCamelCase=32  ,__UpperCamelCase=768  ,__UpperCamelCase=12  ,__UpperCamelCase=12  ,__UpperCamelCase=3072  ,__UpperCamelCase="gelu"  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.0  ,__UpperCamelCase=0.0  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.02  ,__UpperCamelCase=1e-5  ,__UpperCamelCase="group"  ,__UpperCamelCase="gelu"  ,__UpperCamelCase=(512, 512, 512, 512, 512, 512, 512)  ,__UpperCamelCase=(5, 2, 2, 2, 2, 2, 2)  ,__UpperCamelCase=(10, 3, 3, 3, 3, 2, 2)  ,__UpperCamelCase=False  ,__UpperCamelCase=128  ,__UpperCamelCase=16  ,__UpperCamelCase=False  ,__UpperCamelCase=True  ,__UpperCamelCase=0.05  ,__UpperCamelCase=10  ,__UpperCamelCase=2  ,__UpperCamelCase=0.0  ,__UpperCamelCase=10  ,__UpperCamelCase=0  ,__UpperCamelCase=320  ,__UpperCamelCase=2  ,__UpperCamelCase=0.1  ,__UpperCamelCase=100  ,__UpperCamelCase=256  ,__UpperCamelCase=256  ,__UpperCamelCase=0.1  ,__UpperCamelCase="sum"  ,__UpperCamelCase=False  ,__UpperCamelCase=False  ,__UpperCamelCase=256  ,__UpperCamelCase=(512, 512, 512, 512, 1500)  ,__UpperCamelCase=(5, 3, 3, 1, 1)  ,__UpperCamelCase=(1, 2, 3, 1, 1)  ,__UpperCamelCase=512  ,__UpperCamelCase=0  ,__UpperCamelCase=1  ,__UpperCamelCase=2  ,__UpperCamelCase=False  ,__UpperCamelCase=3  ,__UpperCamelCase=2  ,__UpperCamelCase=3  ,__UpperCamelCase=None  ,__UpperCamelCase=None  ,**__UpperCamelCase  ,)			->   Optional[Any]:


        '''simple docstring'''


        super().__init__(**__UpperCamelCase  ,pad_token_id=__UpperCamelCase  ,bos_token_id=__UpperCamelCase  ,eos_token_id=__UpperCamelCase    )
        lowercase_			:       Optional[Any]									=			hidden_size
        lowercase_			:       Tuple									=			feat_extract_norm
        lowercase_			:       Dict									=			feat_extract_activation
        lowercase_			:       List[str]									=			list(__UpperCamelCase    )
        lowercase_			:       str									=			list(__UpperCamelCase    )
        lowercase_			:       Dict									=			list(__UpperCamelCase    )
        lowercase_			:       Optional[Any]									=			conv_bias
        lowercase_			:       Dict									=			num_conv_pos_embeddings
        lowercase_			:       List[str]									=			num_conv_pos_embedding_groups
        lowercase_			:       Optional[Any]									=			len(self.conv_dim    )
        lowercase_			:       Any									=			num_hidden_layers
        lowercase_			:       List[Any]									=			intermediate_size
        lowercase_			:       List[Any]									=			hidden_act
        lowercase_			:       Optional[int]									=			num_attention_heads
        lowercase_			:       int									=			hidden_dropout
        lowercase_			:       Dict									=			attention_dropout
        lowercase_			:       Union[str, Any]									=			activation_dropout
        lowercase_			:       Tuple									=			feat_proj_dropout
        lowercase_			:       List[str]									=			final_dropout
        lowercase_			:       Union[str, Any]									=			layerdrop
        lowercase_			:       List[str]									=			layer_norm_eps
        lowercase_			:       Optional[int]									=			initializer_range
        lowercase_			:       List[Any]									=			vocab_size
        lowercase_			:       Optional[int]									=			do_stable_layer_norm
        lowercase_			:       Union[str, Any]									=			use_weighted_layer_sum

        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
        lowercase_			:       Dict									=			apply_spec_augment
        lowercase_			:       Optional[int]									=			mask_time_prob
        lowercase_			:       Union[str, Any]									=			mask_time_length
        lowercase_			:       List[str]									=			mask_time_min_masks
        lowercase_			:       List[str]									=			mask_feature_prob
        lowercase_			:       Any									=			mask_feature_length
        lowercase_			:       List[Any]									=			mask_feature_min_masks

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

        # ctc loss
        lowercase_			:       Tuple									=			ctc_loss_reduction
        lowercase_			:       int									=			ctc_zero_infinity

        # adapter
        lowercase_			:       int									=			add_adapter
        lowercase_			:       Dict									=			adapter_kernel_size
        lowercase_			:       List[str]									=			adapter_stride
        lowercase_			:       Dict									=			num_adapter_layers
        lowercase_			:       Dict									=			output_hidden_size or hidden_size
        lowercase_			:       Optional[Any]									=			adapter_attn_dim

        # SequenceClassification-specific parameter. Feel free to ignore for other classes.
        lowercase_			:       Dict									=			classifier_proj_size

        # XVector-specific parameters. Feel free to ignore for other classes.
        lowercase_			:       Any									=			list(__UpperCamelCase    )
        lowercase_			:       str									=			list(__UpperCamelCase    )
        lowercase_			:       Any									=			list(__UpperCamelCase    )
        lowercase_			:       Tuple									=			xvector_output_dim







   @property
   def     _UpperCAmelCase     (    self    )			->   str:


        '''simple docstring'''


        return functools.reduce(operator.mul  ,self.conv_stride  ,1    )

