



'''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_       :
    """simple docstring"""







    lowercase					=		MBartConfig
    lowercase					=		{}
    lowercase					=		"gelu"




    def __init__(    self					:     Dict ,     snake_case_					:     str ,     snake_case_					:     Tuple=13 ,     snake_case_					:     Optional[Any]=7 ,     snake_case_					:     List[Any]=True ,     snake_case_					:     List[str]=False ,     snake_case_					:     Any=99 ,     snake_case_					:     str=32 ,     snake_case_					:     int=2 ,     snake_case_					:     Optional[Any]=4 ,     snake_case_					:     Tuple=37 ,     snake_case_					:     Tuple=0.1 ,     snake_case_					:     Any=0.1 ,     snake_case_					:     Dict=20 ,     snake_case_					:     Optional[int]=2 ,     snake_case_					:     List[Any]=1 ,     snake_case_					:     Dict=0 ,     ):
          snake_case__  :    int    						=      parent
          snake_case__  :    Optional[Any]    						=      batch_size
          snake_case__  :    Optional[Any]    						=      seq_length
          snake_case__  :    Union[str, Any]    						=      is_training
          snake_case__  :    Optional[int]    						=      use_labels
          snake_case__  :    str    						=      vocab_size
          snake_case__  :    Optional[int]    						=      hidden_size
          snake_case__  :    str    						=      num_hidden_layers
          snake_case__  :    Optional[int]    						=      num_attention_heads
          snake_case__  :    int    						=      intermediate_size
          snake_case__  :    List[Any]    						=      hidden_dropout_prob
          snake_case__  :    Union[str, Any]    						=      attention_probs_dropout_prob
          snake_case__  :    Tuple    						=      max_position_embeddings
          snake_case__  :    List[Any]    						=      eos_token_id
          snake_case__  :    Dict    						=      pad_token_id
          snake_case__  :    Tuple    						=      bos_token_id




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

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

          snake_case__  :    Union[str, Any]    						=      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 ,     )
          snake_case__  :    List[Any]    						=      prepare_mbart_inputs_dict(snake_case_ ,     snake_case_ ,     snake_case_						)
          return config, inputs_dict




    def lowerCamelCase       (    self					:     int ,     snake_case_					:     Dict ,     snake_case_					:     str						):
          snake_case__  :    Union[str, Any]    						=      TFMBartModel(config=snake_case_						).get_decoder()
          snake_case__  :    Any    						=      inputs_dict["""input_ids"""]

          snake_case__  :    Union[str, Any]    						=      input_ids[:1, :]
          snake_case__  :    Any    						=      inputs_dict["""attention_mask"""][:1, :]
          snake_case__  :    Optional[Any]    						=      inputs_dict["""head_mask"""]
          snake_case__  :    Optional[int]    						=      1

          # first forward pass
          snake_case__  :    Dict    						=      model(snake_case_ ,     attention_mask=snake_case_ ,     head_mask=snake_case_ ,     use_cache=snake_case_						)

          snake_case__						,				snake_case__  :    List[Any]    						=      outputs.to_tuple()
          snake_case__  :    Tuple    						=      past_key_values[1]





def 						__snake_case(				_lowerCAmelCase     ,   _lowerCAmelCase     ,   _lowerCAmelCase     ,   _lowerCAmelCase=None     ,   _lowerCAmelCase=None     ,   _lowerCAmelCase=None     ,   _lowerCAmelCase=None     ,   _lowerCAmelCase=None     ,   )		-> str:
      if attention_mask is None:
            snake_case__  :    Union[str, Any]    						=      tf.cast(tf.math.not_equal(_lowerCAmelCase     ,   config.pad_token_id		)     ,   tf.inta		)
      if decoder_attention_mask is None:
            snake_case__  :    Dict    						=      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:
            snake_case__  :    Dict    						=      tf.ones((config.encoder_layers, config.encoder_attention_heads)		)
      if decoder_head_mask is None:
            snake_case__  :    int    						=      tf.ones((config.decoder_layers, config.decoder_attention_heads)		)
      if cross_attn_head_mask is None:
            snake_case__  :    Union[str, Any]    						=      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_       (       _a		,		_a		,		unittest.TestCase	):
    """simple docstring"""







    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 lowerCamelCase       (    self					:     str ,     snake_case_					:     List[str] ,     snake_case_					:     Optional[Any] ,     snake_case_					:     Tuple ,     snake_case_					:     Any ,     snake_case_					:     List[str]						):
          if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
                # Exception encountered when calling layer '...'
                return True

          return False




    def lowerCamelCase       (    self					:     str						):
          snake_case__  :    int    						=      TFMBartModelTester(self						)
          snake_case__  :    Optional[int]    						=      ConfigTester(self ,     config_class=snake_case_						)




    def lowerCamelCase       (    self					:     Optional[int]						):
          self.config_tester.run_common_tests()




    def lowerCamelCase       (    self					:     List[str]						):
          snake_case__  :    Optional[Any]    						=      self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.check_decoder_model_past_large_inputs(*snake_case_						)







@require_sentencepiece
@require_tokenizers
@require_tf
class     UpperCAmelCase_       (       unittest.TestCase	):
    """simple docstring"""







    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 lowerCamelCase       (    self					:     Optional[Any]						):
          return AutoTokenizer.from_pretrained(self.model_name						)




    @cached_property
    def lowerCamelCase       (    self					:     List[Any]						):
          snake_case__  :    Any    						=      TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name						)
          return model




    def lowerCamelCase       (    self					:     Union[str, Any] ,     **snake_case_					:     int						):
          snake_case__  :    Tuple    						=      self.translate_src_text(**snake_case_						)
          self.assertListEqual(self.expected_text ,     snake_case_						)




    def lowerCamelCase       (    self					:     List[str] ,     **snake_case_					:     Any						):
          snake_case__  :    Tuple    						=      self.tokenizer(self.src_text ,     **snake_case_ ,     return_tensors="""tf"""						)
          snake_case__  :    List[str]    						=      self.model.generate(
              model_inputs.input_ids ,     attention_mask=model_inputs.attention_mask ,     num_beams=2						)
          snake_case__  :    str    						=      self.tokenizer.batch_decode(snake_case_ ,     skip_special_tokens=snake_case_						)
          return generated_words




    @slow
    def lowerCamelCase       (    self					:     int						):
          self._assert_generated_batch_equal_expected()






"""simple docstring"""




import operator as op


lowerCAmelCase							:			Dict     							=							"""scaler.pt"""
lowerCAmelCase							:			Tuple     							=							"""pytorch_model"""
lowerCAmelCase							:			Union[str, Any]     							=							"""random_states"""
lowerCAmelCase							:			Union[str, Any]     							=							"""optimizer"""
lowerCAmelCase							:			Dict     							=							"""scheduler"""
lowerCAmelCase							:			int     							=							"""pytorch_model.bin"""
lowerCAmelCase							:			str     							=							"""pytorch_model.bin.index.json"""
lowerCAmelCase							:			Union[str, Any]     							=							"""model.safetensors"""
lowerCAmelCase							:			List[Any]     							=							"""model.safetensors.index.json"""
lowerCAmelCase							:			List[Any]     							=							"""1.10.2"""
lowerCAmelCase							:			Any     							=							"""py38"""
lowerCAmelCase							:			Optional[int]     							=							"""4.17.0"""
lowerCAmelCase							:			str     							=							["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""]
lowerCAmelCase							:			Tuple     							=							["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""]
lowerCAmelCase							:			List[Any]     							=							["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""]
lowerCAmelCase							:			List[str]     							=							["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""]
lowerCAmelCase							:			List[str]     							=							["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""]
lowerCAmelCase							:			Any     							=							"""2.0.1"""
lowerCAmelCase							:			List[Any]     							=							["""pdsh""", """standard""", """openmpi""", """mvapich"""]
lowerCAmelCase							:			Union[str, Any]     							=							["""default""", """reduce-overhead""", """max-autotune"""]

lowerCAmelCase							:			Optional[int]     							=							{""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt}

# These are the args for `torch.distributed.launch` for pytorch < 1.9
lowerCAmelCase							:			Union[str, Any]     							=							[
    """nnodes""",
    """nproc_per_node""",
    """rdzv_backend""",
    """rdzv_endpoint""",
    """rdzv_id""",
    """rdzv_conf""",
    """standalone""",
    """max_restarts""",
    """monitor_interval""",
    """start_method""",
    """role""",
    """module""",
    """m""",
    """no_python""",
    """run_path""",
    """log_dir""",
    """r""",
    """redirects""",
    """t""",
    """tee""",
    """node_rank""",
    """master_addr""",
    """master_port""",
]

lowerCAmelCase							:			List[str]     							=							["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""]
lowerCAmelCase							:			Optional[Any]     							=							["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]



