






'''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 				lowercase       :


    '''simple docstring'''

    lowerCAmelCase__      				=							MBartConfig
    lowerCAmelCase__      				=							{}
    lowerCAmelCase__      				=							"gelu"

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

     '''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    a__				(					self     :     Union[str, Any]     )							->							Any:

     '''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_mbart_inputs_dict(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase     )
     return config, inputs_dict




    def    a__				(					self     :     Any				,     __lowerCamelCase     :     Optional[int]				,     __lowerCamelCase     :     str     )							->							Dict:

     '''simple docstring'''




     lowerCamelCase__  =     TFMBartModel(config=__lowerCamelCase     ).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(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     head_mask=__lowerCamelCase				,     use_cache=__lowerCamelCase     )

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






def    lowerCamelCase_     (    lowercase__				,     lowercase__				,     lowercase__				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     lowercase__=None				,     ):
 if attention_mask is None:
  lowerCamelCase__  =     tf.cast(tf.math.not_equal(lowercase__				,     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 				lowercase       (							_lowerCamelCase ,       _lowerCamelCase ,       unittest.TestCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
    lowerCAmelCase__      				=							(TFMBartForConditionalGeneration,) if is_tf_available() else ()
    lowerCAmelCase__      				=							(
        {
            "conversational": TFMBartForConditionalGeneration,
            "feature-extraction": TFMBartModel,
            "summarization": TFMBartForConditionalGeneration,
            "text2text-generation": TFMBartForConditionalGeneration,
            "translation": TFMBartForConditionalGeneration,
        }
        if is_tf_available()
        else {}
    )
    lowerCAmelCase__      				=							True
    lowerCAmelCase__      				=							False
    lowerCAmelCase__      				=							False

    def    a__				(					self     :     Dict				,     __lowerCamelCase     :     Optional[int]				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     str				,     __lowerCamelCase     :     Any     )							->							List[str]:

     '''simple docstring'''




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

     return False

    def    a__				(					self     :     str     )							->							str:

     '''simple docstring'''




     lowerCamelCase__  =     TFMBartModelTester(self     )
     lowerCamelCase__  =     ConfigTester(self				,     config_class=__lowerCamelCase     )

    def    a__				(					self     :     Union[str, Any]     )							->							Union[str, Any]:

     '''simple docstring'''




     self.config_tester.run_common_tests()




    def    a__				(					self     :     Tuple     )							->							List[str]:

     '''simple docstring'''




     lowerCamelCase__  =     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 				lowercase       (							unittest.TestCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							[
        " UN Chief Says There Is No Military Solution in Syria",
    ]
    lowerCAmelCase__      				=							[
        "Şeful ONU declară că nu există o soluţie militară în Siria",
    ]
    lowerCAmelCase__      				=							"facebook/mbart-large-en-ro"

    @cached_property
    def    a__				(					self     :     Dict     )							->							int:

     '''simple docstring'''




     return AutoTokenizer.from_pretrained(self.model_name     )

    @cached_property
    def    a__				(					self     :     Dict     )							->							Optional[int]:

     '''simple docstring'''




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

    def    a__				(					self     :     str				,     **__lowerCamelCase     :     Dict     )							->							Any:

     '''simple docstring'''




     lowerCamelCase__  =     self.translate_src_text(**__lowerCamelCase     )
     self.assertListEqual(self.expected_text				,     __lowerCamelCase     )

    def    a__				(					self     :     Any				,     **__lowerCamelCase     :     str     )							->							Optional[int]:

     '''simple docstring'''




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




    @slow
    def    a__				(					self     :     Tuple     )							->							Tuple:

     '''simple docstring'''




     self._assert_generated_batch_equal_expected()









'''simple docstring'''





import tensorflow as tf

from ...tf_utils import shape_list
class 				lowercase       (							tf.keras.layers.Layer      ):


    '''simple docstring'''

    def __init__(					self     :     Union[str, Any]				,     __lowerCamelCase     :     str				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Dict				,     __lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     int=1				,     __lowerCamelCase     :     Tuple=False				,     **__lowerCamelCase     :     Dict     )							->							str:

     '''simple docstring'''




     super().__init__(**__lowerCamelCase     )

     lowerCamelCase__  =     vocab_size
     lowerCamelCase__  =     d_embed
     lowerCamelCase__  =     d_proj

     lowerCamelCase__  =     cutoffs + [vocab_size]
     lowerCamelCase__  =     [0] + self.cutoffs
     lowerCamelCase__  =     div_val

     lowerCamelCase__  =     self.cutoffs[0]
     lowerCamelCase__  =     len(self.cutoffs     ) - 1
     lowerCamelCase__  =     self.shortlist_size + self.n_clusters
     lowerCamelCase__  =     keep_order

     lowerCamelCase__  =     []
     lowerCamelCase__  =     []

    def    a__				(					self     :     Optional[int]				,     __lowerCamelCase     :     str     )							->							List[str]:

     '''simple docstring'''




     if self.n_clusters > 0:
      lowerCamelCase__  =     self.add_weight(
          shape=(self.n_clusters, self.d_embed)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name="cluster_weight"     )
      lowerCamelCase__  =     self.add_weight(
          shape=(self.n_clusters,)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name="cluster_bias"     )

     if self.div_val == 1:
      for i in range(len(self.cutoffs     )     ):
       if self.d_proj != self.d_embed:
        lowerCamelCase__  =     self.add_weight(
            shape=(self.d_embed, self.d_proj)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_projs_._{i}'''				,     )
        self.out_projs.append(__lowerCamelCase     )
       else:
        self.out_projs.append(__lowerCamelCase     )
       lowerCamelCase__  =     self.add_weight(
           shape=(self.vocab_size, self.d_embed)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_layers_._{i}_._weight'''				,     )
       lowerCamelCase__  =     self.add_weight(
           shape=(self.vocab_size,)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_layers_._{i}_._bias'''				,     )
       self.out_layers.append((weight, bias)     )
     else:
      for i in range(len(self.cutoffs     )     ):
       lowerCamelCase__  ,	lowerCamelCase__  =     self.cutoff_ends[i], self.cutoff_ends[i + 1]
       lowerCamelCase__  =     self.d_embed // (self.div_val**i)

       lowerCamelCase__  =     self.add_weight(
           shape=(d_emb_i, self.d_proj)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_projs_._{i}'''     )
       self.out_projs.append(__lowerCamelCase     )
       lowerCamelCase__  =     self.add_weight(
           shape=(r_idx - l_idx, d_emb_i)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_layers_._{i}_._weight'''				,     )
       lowerCamelCase__  =     self.add_weight(
           shape=(r_idx - l_idx,)				,     initializer="zeros"				,     trainable=__lowerCamelCase				,     name=f'''out_layers_._{i}_._bias'''				,     )
       self.out_layers.append((weight, bias)     )
     super().build(__lowerCamelCase     )

    @staticmethod
    def    a__				(					__lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     List[str]=None     )							->							str:

     '''simple docstring'''




     lowerCamelCase__  =     x
     if proj is not None:
      lowerCamelCase__  =     tf.einsum("ibd,ed->ibe"				,     __lowerCamelCase				,     __lowerCamelCase     )
     return tf.einsum("ibd,nd->ibn"				,     __lowerCamelCase				,     __lowerCamelCase     ) + b

    @staticmethod
    def    a__				(					__lowerCamelCase     :     List[str]				,     __lowerCamelCase     :     Union[str, Any]     )							->							Union[str, Any]:

     '''simple docstring'''




     lowerCamelCase__  =     shape_list(__lowerCamelCase     )
     lowerCamelCase__  =     tf.range(lp_size[0]				,     dtype=target.dtype     )
     lowerCamelCase__  =     tf.stack([r, target]				,     1     )
     return tf.gather_nd(__lowerCamelCase				,     __lowerCamelCase     )




    def    a__				(					self     :     Any				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     str=True				,     __lowerCamelCase     :     Tuple=False     )							->							int:

     '''simple docstring'''




     lowerCamelCase__  =     0
     if self.n_clusters == 0:
      lowerCamelCase__  =     self._logit(__lowerCamelCase				,     self.out_layers[0][0]				,     self.out_layers[0][1]				,     self.out_projs[0]     )
      if target is not None:
       lowerCamelCase__  =     tf.nn.sparse_softmax_cross_entropy_with_logits(labels=__lowerCamelCase				,     logits=__lowerCamelCase     )
      lowerCamelCase__  =     tf.nn.log_softmax(__lowerCamelCase				,     axis=-1     )
     else:
      lowerCamelCase__  =     shape_list(__lowerCamelCase     )
      lowerCamelCase__  =     []
      lowerCamelCase__  =     tf.zeros(hidden_sizes[:2]     )
      for i in range(len(self.cutoffs     )     ):
       lowerCamelCase__  ,	lowerCamelCase__  =     self.cutoff_ends[i], self.cutoff_ends[i + 1]
       if target is not None:
        lowerCamelCase__  =     (target >= l_idx) & (target < r_idx)
        lowerCamelCase__  =     tf.where(__lowerCamelCase     )
        lowerCamelCase__  =     tf.boolean_mask(__lowerCamelCase				,     __lowerCamelCase     ) - l_idx

       if self.div_val == 1:
        lowerCamelCase__  =     self.out_layers[0][0][l_idx:r_idx]
        lowerCamelCase__  =     self.out_layers[0][1][l_idx:r_idx]
       else:
        lowerCamelCase__  =     self.out_layers[i][0]
        lowerCamelCase__  =     self.out_layers[i][1]

       if i == 0:
        lowerCamelCase__  =     tf.concat([cur_W, self.cluster_weight]				,     0     )
        lowerCamelCase__  =     tf.concat([cur_b, self.cluster_bias]				,     0     )

        lowerCamelCase__  =     self._logit(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase				,     self.out_projs[0]     )
        lowerCamelCase__  =     tf.nn.log_softmax(__lowerCamelCase     )
        out.append(head_logprob[..., : self.cutoffs[0]]     )
        if target is not None:
         lowerCamelCase__  =     tf.boolean_mask(__lowerCamelCase				,     __lowerCamelCase     )
         lowerCamelCase__  =     self._gather_logprob(__lowerCamelCase				,     __lowerCamelCase     )
       else:
        lowerCamelCase__  =     self._logit(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase				,     self.out_projs[i]     )
        lowerCamelCase__  =     tf.nn.log_softmax(__lowerCamelCase     )
        lowerCamelCase__  =     self.cutoffs[0] + i - 1  # No probability for the head cluster
        lowerCamelCase__  =     head_logprob[..., cluster_prob_idx, None] + tail_logprob
        out.append(__lowerCamelCase     )
        if target is not None:
         lowerCamelCase__  =     tf.boolean_mask(__lowerCamelCase				,     __lowerCamelCase     )
         lowerCamelCase__  =     tf.boolean_mask(__lowerCamelCase				,     __lowerCamelCase     )
         lowerCamelCase__  =     self._gather_logprob(__lowerCamelCase				,     __lowerCamelCase     )
         cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1]
       if target is not None:
        loss += tf.scatter_nd(__lowerCamelCase				,     -cur_logprob				,     shape_list(__lowerCamelCase     )     )
      lowerCamelCase__  =     tf.concat(__lowerCamelCase				,     axis=-1     )

     if target is not None:
      if return_mean:
       lowerCamelCase__  =     tf.reduce_mean(__lowerCamelCase     )
      # Add the training-time loss value to the layer using `self.add_loss()`.
      self.add_loss(__lowerCamelCase     )

      # Log the loss as a metric (we could log arbitrary metrics,
      # including different metrics for training and inference.
      self.add_metric(__lowerCamelCase				,     name=self.name				,     aggregation="mean" if return_mean else ""     )

     return out

