





"""simple docstring"""




from __future__ import annotations

import unittest
import warnings

from transformers import AutoTokenizer, MarianConfig, MarianTokenizer, TranslationPipeline, 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, TFMarianModel, TFMarianMTModel

@require_tf
class 			_UpperCAmelCase  :



      '''simple docstring'''
      lowercase_				:			int        =       MarianConfig
      lowercase_				:			List[Any]        =       {}
      lowercase_				:			Optional[int]        =       """gelu"""

      def __init__(					self ,				snake_case_ ,				snake_case_=1_3 ,				snake_case_=7 ,				snake_case_=True ,				snake_case_=False ,				snake_case_=9_9 ,				snake_case_=3_2 ,				snake_case_=2 ,				snake_case_=4 ,				snake_case_=3_7 ,				snake_case_=0.1 ,				snake_case_=0.1 ,				snake_case_=2_0 ,				snake_case_=2 ,				snake_case_=1 ,				snake_case_=0 ,				):

        """simple docstring"""

        A_  :	int    						=  parent
        A_  :	Dict    						=  batch_size
        A_  :	Optional[int]    						=  seq_length
        A_  :	Dict    						=  is_training
        A_  :	Tuple    						=  use_labels
        A_  :	List[Any]    						=  vocab_size
        A_  :	Dict    						=  hidden_size
        A_  :	Tuple    						=  num_hidden_layers
        A_  :	Union[str, Any]    						=  num_attention_heads
        A_  :	Dict    						=  intermediate_size

        A_  :	int    						=  hidden_dropout_prob
        A_  :	Union[str, Any]    						=  attention_probs_dropout_prob
        A_  :	List[str]    						=  max_position_embeddings
        A_  :	Optional[Any]    						=  eos_token_id
        A_  :	str    						=  pad_token_id
        A_  :	int    						=  bos_token_id

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

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

        A_  :	Union[str, Any]    						=  ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size  )

        A_  :	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 ,				)
        A_  :	Dict    						=  prepare_marian_inputs_dict(snake_case_ ,				snake_case_ ,				snake_case_  )
        return config, inputs_dict

      def  lowerCamelCase_      (					self ,				snake_case_ ,				snake_case_  ):

        """simple docstring"""

        A_  :	List[Any]    						=  TFMarianModel(config=snake_case_  ).get_decoder()
        A_  :	List[Any]    						=  inputs_dict['input_ids']

        A_  :	Tuple    						=  input_ids[:1, :]
        A_  :	Any    						=  inputs_dict['attention_mask'][:1, :]
        A_  :	List[Any]    						=  inputs_dict['head_mask']
        A_  :	Dict    						=  1

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

        A_  :	Union[str, Any]    						=  outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        A_  :	List[Any]    						=  ids_tensor((self.batch_size, 3) ,				config.vocab_size  )
        A_  :	int    						=  tf.cast(ids_tensor((self.batch_size, 3) ,				2  ) ,				tf.inta  )

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

        A_  :	Optional[Any]    						=  model(snake_case_ ,				attention_mask=snake_case_  )[0]
        A_  :	Union[str, Any]    						=  model(snake_case_ ,				attention_mask=snake_case_ ,				past_key_values=snake_case_  )[0]

        self.parent.assertEqual(next_tokens.shape[1] ,				output_from_past.shape[1]  )

        # select random slice
        A_  :	Dict    						=  int(ids_tensor((1,) ,				output_from_past.shape[-1]  )  )
        A_  :	str    						=  output_from_no_past[:, -3:, random_slice_idx]
        A_  :	Tuple    						=  output_from_past[:, :, random_slice_idx]

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





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase=None			, _UpperCAmelCase=None			, _UpperCAmelCase=None			, _UpperCAmelCase=None			, _UpperCAmelCase=None			, ):






  """simple docstring"""





  if attention_mask is None:
    A_  :	List[str]    						=  tf.cast(tf.math.not_equal(_UpperCAmelCase			, config.pad_token_id )			, tf.inta )
  if decoder_attention_mask is None:
    A_  :	Any    						=  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:
    A_  :	Optional[int]    						=  tf.ones((config.encoder_layers, config.encoder_attention_heads) )
  if decoder_head_mask is None:
    A_  :	str    						=  tf.ones((config.decoder_layers, config.decoder_attention_heads) )
  if cross_attn_head_mask is None:
    A_  :	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  (       UpperCAmelCase__      ,       UpperCAmelCase__      ,       unittest.TestCase		):



      '''simple docstring'''
      lowercase_				:			Union[str, Any]        =       (TFMarianMTModel, TFMarianModel) if is_tf_available() else ()
      lowercase_				:			List[Any]        =       (TFMarianMTModel,) if is_tf_available() else ()
      lowercase_				:			str        =       (
          {
              """conversational""": TFMarianMTModel,
              """feature-extraction""": TFMarianModel,
              """summarization""": TFMarianMTModel,
              """text2text-generation""": TFMarianMTModel,
              """translation""": TFMarianMTModel,
          }
          if is_tf_available()
          else {}
      )
      lowercase_				:			Union[str, Any]        =       True
      lowercase_				:			List[Any]        =       False
      lowercase_				:			List[str]        =       False

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Optional[Any]    						=  TFMarianModelTester(self  )
        A_  :	List[str]    						=  ConfigTester(self ,				config_class=snake_case_  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        self.config_tester.run_common_tests()

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	str    						=  self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.check_decoder_model_past_large_inputs(*snake_case_  )


@require_tf
class 			_UpperCAmelCase  (       unittest.TestCase		):



      '''simple docstring'''
      lowercase_				:			Union[str, Any]        =       1_000  # show more chars for failing integration tests

      @classmethod
      def  lowerCamelCase_      (					cls  ):

        """simple docstring"""

        A_  :	Any    						=  F"""Helsinki-NLP/opus-mt-{cls.src}-{cls.tgt}"""
        return cls

      @cached_property
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        return AutoTokenizer.from_pretrained(self.model_name  )

      @property
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        return self.tokenizer.eos_token_id

      @cached_property
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        warnings.simplefilter('error'  )
        A_  :	TFMarianMTModel    						=  TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name  )
        assert isinstance(snake_case_ ,				snake_case_  )
        A_  :	List[Any]    						=  model.config
        self.assertListEqual(c.bad_words_ids ,				[[c.pad_token_id]]  )
        self.assertEqual(c.max_length ,				5_1_2  )
        self.assertEqual(c.decoder_start_token_id ,				c.pad_token_id  )
        return model

      def  lowerCamelCase_      (					self ,				**snake_case_  ):

        """simple docstring"""

        A_  :	int    						=  self.translate_src_text(**snake_case_  )
        self.assertListEqual(self.expected_text ,				snake_case_  )

      def  lowerCamelCase_      (					self ,				**snake_case_  ):

        """simple docstring"""

        A_  :	Any    						=  self.tokenizer(self.src_text ,				**snake_case_ ,				padding=snake_case_ ,				return_tensors='tf'  )
        A_  :	Optional[Any]    						=  self.model.generate(
            model_inputs.input_ids ,				attention_mask=model_inputs.attention_mask ,				num_beams=2 ,				max_length=1_2_8  )
        A_  :	Optional[int]    						=  self.tokenizer.batch_decode(generated_ids.numpy() ,				skip_special_tokens=snake_case_  )
        return generated_words


@require_sentencepiece
@require_tokenizers
@require_tf
class 			_UpperCAmelCase  (       UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_				:			str        =       """mt"""
      lowercase_				:			List[Any]        =       """en"""
      lowercase_				:			List[Any]        =       ["""Billi messu b'mod ġentili, Ġesù fejjaq raġel li kien milqut bil - marda kerha tal - ġdiem."""]
      lowercase_				:			Tuple        =       ["""Touching gently, Jesus healed a man who was affected by the sad disease of leprosy."""]

      @unittest.skip('Skipping until #12647 is resolved.'  )
      @slow
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        self._assert_generated_batch_equal_expected()


@require_sentencepiece
@require_tokenizers
@require_tf
class 			_UpperCAmelCase  (       UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_				:			List[str]        =       """en"""
      lowercase_				:			List[Any]        =       """zh"""
      lowercase_				:			List[Any]        =       ["""My name is Wolfgang and I live in Berlin"""]
      lowercase_				:			Dict        =       ["""我叫沃尔夫冈 我住在柏林"""]

      @unittest.skip('Skipping until #12647 is resolved.'  )
      @slow
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        self._assert_generated_batch_equal_expected()


@require_sentencepiece
@require_tokenizers
@require_tf
class 			_UpperCAmelCase  (       UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_				:			int        =       """en"""
      lowercase_				:			List[str]        =       """ROMANCE"""
      lowercase_				:			str        =       [
          """>>fr<< Don't spend so much time watching TV.""",
          """>>pt<< Your message has been sent.""",
          """>>es<< He's two years older than me.""",
      ]
      lowercase_				:			Dict        =       [
          """Ne passez pas autant de temps à regarder la télé.""",
          """A sua mensagem foi enviada.""",
          """Es dos años más viejo que yo.""",
      ]

      @unittest.skip('Skipping until #12647 is resolved.'  )
      @slow
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        self._assert_generated_batch_equal_expected()

      @unittest.skip('Skipping until #12647 is resolved.'  )
      @slow
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Dict    						=  TranslationPipeline(self.model ,				self.tokenizer ,				framework='tf'  )
        A_  :	int    						=  pipeline(self.src_text  )
        self.assertEqual(self.expected_text ,				[x['translation_text'] for x in output]  )







"""simple docstring"""




from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


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

lowerCamelCase_					:						Optional[Any]      = {
    'gpt2': 'https://huggingface.co/gpt2/resolve/main/config.json',
    'gpt2-medium': 'https://huggingface.co/gpt2-medium/resolve/main/config.json',
    'gpt2-large': 'https://huggingface.co/gpt2-large/resolve/main/config.json',
    'gpt2-xl': 'https://huggingface.co/gpt2-xl/resolve/main/config.json',
    'distilgpt2': 'https://huggingface.co/distilgpt2/resolve/main/config.json',
}

class 			_UpperCAmelCase  (       UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_				:			Optional[Any]        =       """gpt2"""
      lowercase_				:			Optional[int]        =       ["""past_key_values"""]
      lowercase_				:			Dict        =       {
          """hidden_size""": """n_embd""",
          """max_position_embeddings""": """n_positions""",
          """num_attention_heads""": """n_head""",
          """num_hidden_layers""": """n_layer""",
      }

      def __init__(					self ,				snake_case_=5_0_2_5_7 ,				snake_case_=1_0_2_4 ,				snake_case_=7_6_8 ,				snake_case_=1_2 ,				snake_case_=1_2 ,				snake_case_=None ,				snake_case_="gelu_new" ,				snake_case_=0.1 ,				snake_case_=0.1 ,				snake_case_=0.1 ,				snake_case_=1E-5 ,				snake_case_=0.02 ,				snake_case_="cls_index" ,				snake_case_=True ,				snake_case_=None ,				snake_case_=True ,				snake_case_=0.1 ,				snake_case_=True ,				snake_case_=True ,				snake_case_=5_0_2_5_6 ,				snake_case_=5_0_2_5_6 ,				snake_case_=False ,				snake_case_=False ,				**snake_case_ ,				):

        """simple docstring"""

        A_  :	Union[str, Any]    						=  vocab_size
        A_  :	List[Any]    						=  n_positions
        A_  :	List[Any]    						=  n_embd
        A_  :	Tuple    						=  n_layer
        A_  :	List[Any]    						=  n_head
        A_  :	List[str]    						=  n_inner
        A_  :	str    						=  activation_function
        A_  :	Optional[Any]    						=  resid_pdrop
        A_  :	int    						=  embd_pdrop
        A_  :	Tuple    						=  attn_pdrop
        A_  :	str    						=  layer_norm_epsilon
        A_  :	Any    						=  initializer_range
        A_  :	Union[str, Any]    						=  summary_type
        A_  :	Optional[Any]    						=  summary_use_proj
        A_  :	Dict    						=  summary_activation
        A_  :	Optional[int]    						=  summary_first_dropout
        A_  :	Optional[int]    						=  summary_proj_to_labels
        A_  :	Any    						=  scale_attn_weights
        A_  :	Optional[Any]    						=  use_cache
        A_  :	Dict    						=  scale_attn_by_inverse_layer_idx
        A_  :	Any    						=  reorder_and_upcast_attn

        A_  :	Optional[Any]    						=  bos_token_id
        A_  :	List[Any]    						=  eos_token_id

        super().__init__(bos_token_id=snake_case_ ,				eos_token_id=snake_case_ ,				**snake_case_  )


class 			_UpperCAmelCase  (       UpperCAmelCase__		):



      '''simple docstring'''

      def __init__(					self ,				snake_case_ ,				snake_case_ = "default" ,				snake_case_ = None ,				snake_case_ = False ,				):

        """simple docstring"""

        super().__init__(snake_case_ ,				task=snake_case_ ,				patching_specs=snake_case_ ,				use_past=snake_case_  )
        if not getattr(self._config ,				'pad_token_id' ,				snake_case_  ):
          # TODO: how to do that better?
          A_  :	Tuple    						=  0

      @property
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Dict    						=  OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}  )
        if self.use_past:
          self.fill_with_past_key_values_(snake_case_ ,				direction='inputs'  )
          A_  :	List[str]    						=  {0: 'batch', 1: 'past_sequence + sequence'}
        else:
          A_  :	Tuple    						=  {0: 'batch', 1: 'sequence'}

        return common_inputs

      @property
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        return self._config.n_layer

      @property
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        return self._config.n_head

      def  lowerCamelCase_      (					self ,				snake_case_ ,				snake_case_ = -1 ,				snake_case_ = -1 ,				snake_case_ = False ,				snake_case_ = None ,				):

        """simple docstring"""

        A_  :	int    						=  super(snake_case_ ,				self  ).generate_dummy_inputs(
            snake_case_ ,				batch_size=snake_case_ ,				seq_length=snake_case_ ,				is_pair=snake_case_ ,				framework=snake_case_  )

        # We need to order the input in the way they appears in the forward()
        A_  :	Optional[Any]    						=  OrderedDict({'input_ids': common_inputs['input_ids']}  )

        # Need to add the past_keys
        if self.use_past:
          if not is_torch_available():
            raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'  )
          else:
            import torch

            A_						,	A_  :	List[str]    						=  common_inputs['input_ids'].shape
            # Not using the same length for past_key_values
            A_  :	Optional[Any]    						=  seqlen + 2
            A_  :	Any    						=  (
                batch,
                self.num_attention_heads,
                past_key_values_length,
                self._config.hidden_size // self.num_attention_heads,
            )
            A_  :	Any    						=  [
                (torch.zeros(snake_case_  ), torch.zeros(snake_case_  )) for _ in range(self.num_layers  )
            ]

        A_  :	List[Any]    						=  common_inputs['attention_mask']
        if self.use_past:
          A_  :	List[Any]    						=  ordered_inputs['attention_mask'].dtype
          A_  :	Any    						=  torch.cat(
              [ordered_inputs['attention_mask'], torch.ones(snake_case_ ,				snake_case_ ,				dtype=snake_case_  )] ,				dim=1  )

        return ordered_inputs

      @property
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        return 1_3