
from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

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 GPTaTokenizer, TFOPTForCausalLM, TFOPTModel


def 			UpperCamelCase					(    __lowerCamelCase     : Tuple				,					__lowerCamelCase     : Optional[int]				,					__lowerCamelCase     : int=None				,					__lowerCamelCase     : Optional[Any]=None    ):
       if attention_mask is None:
              snake_case       :   Optional[Any]							=      tf.cast(tf.math.not_equal(__lowerCamelCase				,					config.pad_token_id    )				,					tf.inta    )
       return {"input_ids": input_ids, "attention_mask": attention_mask}






@require_tf
class   UpperCAmelCase		:
   A__					:		List[str]						     =     OPTConfig
   A__					:		Optional[int]						     =     {}
   A__					:		List[Any]						     =     "gelu"



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


          '''simple docstring'''


          snake_case       :   Optional[int]							=      parent
          snake_case       :   int							=      batch_size
          snake_case       :   List[str]							=      seq_length
          snake_case       :   str							=      is_training
          snake_case       :   List[Any]							=      use_labels
          snake_case       :   Dict							=      vocab_size
          snake_case       :   List[Any]							=      hidden_size
          snake_case       :   Union[str, Any]							=      num_hidden_layers
          snake_case       :   str							=      num_attention_heads
          snake_case       :   Optional[int]							=      intermediate_size
          snake_case       :   str							=      hidden_act
          snake_case       :   int							=      hidden_dropout_prob
          snake_case       :   Optional[Any]							=      attention_probs_dropout_prob
          snake_case       :   Optional[int]							=      max_position_embeddings
          snake_case       :   List[str]							=      eos_token_id
          snake_case       :   int							=      pad_token_id
          snake_case       :   Optional[Any]							=      bos_token_id
          snake_case       :   List[str]							=      embed_dim
          snake_case       :   List[str]							=      word_embed_proj_dim
          snake_case       :   Optional[Any]							=      False



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


          '''simple docstring'''


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

          snake_case       :   Dict							=      self.config_cls(
              vocab_size=self.vocab_size    ,						hidden_size=self.hidden_size    ,						num_hidden_layers=self.num_hidden_layers    ,						num_attention_heads=self.num_attention_heads    ,						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_id=self.eos_token_id    ,						bos_token_id=self.bos_token_id    ,						pad_token_id=self.pad_token_id    ,						embed_dim=self.embed_dim    ,						word_embed_proj_dim=self.word_embed_proj_dim    ,						is_encoder_decoder=snake_case__    ,						**self.config_updates    ,						)
          snake_case       :   Dict							=      prepare_opt_inputs_dict(snake_case__    ,						snake_case__ )
          return config, inputs_dict



   def 				_SCREAMING_SNAKE_CASE   (self		:					Dict    ,						snake_case__		:					List[str]    ,						snake_case__		:					List[str] )		->	Dict:


          '''simple docstring'''


          snake_case       :   Any							=      TFOPTModel(config=snake_case__ )
          snake_case       :   Optional[int]							=      inputs_dict["input_ids"]

          snake_case       :   Union[str, Any]							=      input_ids[:1, :]
          snake_case       :   List[str]							=      inputs_dict["attention_mask"][:1, :]
          snake_case       :   Tuple							=      1

          # first forward pass
          snake_case       :   int							=      model(snake_case__    ,						attention_mask=snake_case__    ,						use_cache=snake_case__ )

          snake_case				,					snake_case       :   Dict							=      outputs.to_tuple()

          # create hypothetical next token and extent to next_input_ids
          snake_case       :   Dict							=      ids_tensor((self.batch_size, 3)    ,						config.vocab_size )
          snake_case       :   Any							=      tf.cast(ids_tensor((self.batch_size, 3)    ,						2 )    ,						tf.inta )

          # append to next input_ids and
          snake_case       :   Optional[Any]							=      tf.concat([input_ids, next_tokens]    ,						axis=-1 )
          snake_case       :   Union[str, Any]							=      tf.concat([attention_mask, next_attn_mask]    ,						axis=-1 )

          snake_case       :   int							=      model(snake_case__    ,						attention_mask=snake_case__ )[0]
          snake_case       :   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
          snake_case       :   Dict							=      int(ids_tensor((1,)    ,						output_from_past.shape[-1] ) )
          snake_case       :   List[str]							=      output_from_no_past[:, -3:, random_slice_idx]
          snake_case       :   List[Any]							=      output_from_past[:, :, random_slice_idx]

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






@require_tf
class   UpperCAmelCase		(						A_       ,A_       ,unittest.TestCase ):
   A__					:		Optional[int]						     =     (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
   A__					:		Any						     =     (TFOPTForCausalLM,) if is_tf_available() else ()
   A__					:		str						     =     (
       {"feature-extraction": TFOPTModel, "text-generation": TFOPTForCausalLM} if is_tf_available() else {}
   )
   A__					:		int						     =     False
   A__					:		Optional[int]						     =     False
   A__					:		Optional[int]						     =     False
   A__					:		List[Any]						     =     10



   def 				_SCREAMING_SNAKE_CASE   (self		:					Any )		->	Any:


          '''simple docstring'''


          snake_case       :   Dict							=      TFOPTModelTester(self )
          snake_case       :   Any							=      ConfigTester(self    ,						config_class=snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					int )		->	List[Any]:


          '''simple docstring'''


          self.config_tester.run_common_tests()



   def 				_SCREAMING_SNAKE_CASE   (self		:					str )		->	List[str]:


          '''simple docstring'''


          snake_case       :   Tuple							=      self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Any )		->	str:


          '''simple docstring'''


          snake_case				,					snake_case       :   int							=      self.model_tester.prepare_config_and_inputs_for_common()

          def _get_word_embedding_weight(snake_case__		:					str    ,						snake_case__		:					Union[str, Any] ):
                 if hasattr(snake_case__    ,						"weight" ):
                        return embedding_layer.weight
                 else:
                        # Here we build the word embeddings weights if not exists.
                        # And then we retry to get the attribute once built.
                        model.build()
                        if hasattr(snake_case__    ,						"weight" ):
                               return embedding_layer.weight
                        else:
                               return None

          for model_class in self.all_model_classes:
                 for size in [config.vocab_size - 10, config.vocab_size + 10]:
                        # build the embeddings
                        snake_case       :   List[str]							=      model_class(config=snake_case__ )
                        snake_case       :   int							=      _get_word_embedding_weight(snake_case__    ,						model.get_input_embeddings() )
                        snake_case       :   List[str]							=      _get_word_embedding_weight(snake_case__    ,						model.get_output_embeddings() )

                        # reshape the embeddings
                        model.resize_token_embeddings(snake_case__ )
                        snake_case       :   Tuple							=      _get_word_embedding_weight(snake_case__    ,						model.get_input_embeddings() )
                        snake_case       :   Tuple							=      _get_word_embedding_weight(snake_case__    ,						model.get_output_embeddings() )

                        # check that the resized embeddings size matches the desired size.
                        snake_case       :   List[str]							=      size if size is not None else config.vocab_size

                        self.assertEqual(new_input_embeddings.shape[0]    ,						snake_case__ )

                        # check that weights remain the same after resizing
                        snake_case       :   Optional[int]							=      True
                        for pa, pa in zip(old_input_embeddings.value()    ,						new_input_embeddings.value() ):
                               if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0:
                                      snake_case       :   int							=      False
                        self.assertTrue(snake_case__ )

                        if old_output_embeddings is not None and new_output_embeddings is not None:
                               self.assertEqual(new_output_embeddings.shape[0]    ,						snake_case__ )

                               snake_case       :   Optional[Any]							=      True
                               for pa, pa in zip(old_output_embeddings.value()    ,						new_output_embeddings.value() ):
                                      if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0:
                                             snake_case       :   Union[str, Any]							=      False
                               self.assertTrue(snake_case__ )



def 			UpperCamelCase					(    __lowerCamelCase     : List[str]    ):
       return tf.constant(__lowerCamelCase				,					dtype=tf.intaa    )






@require_tf
class   UpperCAmelCase		(						unittest.TestCase ):
   A__					:		str						     =     99



   def 				_SCREAMING_SNAKE_CASE   (self		:					List[str] )		->	List[Any]:


          '''simple docstring'''


          snake_case       :   Tuple							=      tf.ones((4, 1)    ,						dtype=tf.intaa ) * 2
          snake_case       :   Dict							=      tf.concat([ids_tensor((4, 6)    ,						self.vocab_size - 3 ) + 3, eos_column_vector]    ,						axis=1 )
          snake_case       :   Optional[int]							=      input_ids.shape[0]
          snake_case       :   List[str]							=      OPTConfig(
              vocab_size=self.vocab_size    ,						hidden_size=24    ,						num_hidden_layers=2    ,						num_attention_heads=2    ,						ffn_dim=32    ,						max_position_embeddings=48    ,						eos_token_id=2    ,						pad_token_id=1    ,						bos_token_id=0    ,						)
          return config, input_ids, batch_size






@require_sentencepiece
@require_tf
class   UpperCAmelCase		(						unittest.TestCase ):



   @slow
   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[Any] )		->	Any:


          '''simple docstring'''


          snake_case       :   List[Any]							=      TFOPTModel.from_pretrained("facebook/opt-350m" )
          snake_case       :   int							=      _long_tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] )
          snake_case       :   List[Any]							=      tf.not_equal(snake_case__    ,						model.config.pad_token_id )
          with tf.GradientTape():
                 snake_case       :   List[Any]							=      model(input_ids=snake_case__    ,						attention_mask=snake_case__ ).last_hidden_state
          snake_case       :   List[Any]							=      (1, 11, 5_12)
          self.assertEqual(output.shape    ,						snake_case__ )
          snake_case       :   List[Any]							=      tf.constant(
              [[-0.2873, -1.9218, -0.3033], [-1.2710, -0.1338, -0.1902], [0.4095, 0.1214, -1.3121]] )
          self.assertTrue(np.allclose(output[:, :3, :3]    ,						snake_case__    ,						atol=4e-3 ) )

          snake_case       :   Optional[int]							=      tf.function(snake_case__    ,						jit_compile=snake_case__ )
          snake_case       :   Optional[int]							=      xla_generate(snake_case__    ,						snake_case__ )[0]
          self.assertTrue(np.allclose(output[:, :3, :3]    ,						snake_case__    ,						atol=4e-2 ) )






@require_tf
@slow
class   UpperCAmelCase		(						unittest.TestCase ):



   def 				_SCREAMING_SNAKE_CASE   (self		:					str )		->	Optional[Any]:


          '''simple docstring'''


          super().setUp()
          snake_case       :   Optional[int]							=      "facebook/opt-350m"



   def 				_SCREAMING_SNAKE_CASE   (self		:					Tuple )		->	Optional[int]:


          '''simple docstring'''


          snake_case       :   Any							=      TFOPTForCausalLM.from_pretrained(self.path_model )
          snake_case       :   Tuple							=      GPTaTokenizer.from_pretrained(self.path_model )

          snake_case       :   Optional[int]							=      [
              "Today is a beautiful day and I want to",
              "In the city of",
              "Paris is the capital of France and",
              "Computers and mobile phones have taken",
          ]
          # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
          snake_case       :   str							=      tokenizer(snake_case__    ,						return_tensors="tf"    ,						padding=snake_case__    ,						add_special_tokens=snake_case__ )
          snake_case       :   Dict							=      tf.math.reduce_mean(model(inputs.input_ids    ,						attention_mask=inputs.attention_mask )[0]    ,						axis=-1 )
          snake_case       :   str							=      tf.constant(
              [
                  [1.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670],
                  [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822],
                  [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703],
                  [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477],
              ] )
          self.assertTrue(np.allclose(snake_case__    ,						snake_case__    ,						atol=1e-4 ) )

          snake_case       :   Optional[int]							=      tf.function(snake_case__    ,						jit_compile=snake_case__ )
          snake_case       :   Dict							=      tf.math.reduce_mean(xla_generate(inputs.input_ids    ,						attention_mask=inputs.attention_mask )[0]    ,						axis=-1 )
          self.assertTrue(np.allclose(snake_case__    ,						snake_case__    ,						atol=1e-4 ) )






@require_tf
@slow
class   UpperCAmelCase		(						unittest.TestCase ):



   @property
   def 				_SCREAMING_SNAKE_CASE   (self		:					Any )		->	Dict:


          '''simple docstring'''


          return [
              "Today is a beautiful day and I want",
              "In the city of",
              "Paris is the capital of France and",
              "Computers and mobile phones have taken",
          ]



   def 				_SCREAMING_SNAKE_CASE   (self		:					List[str] )		->	Any:


          '''simple docstring'''


          snake_case       :   Any							=      "facebook/opt-125m"

          snake_case       :   List[Any]							=      [
              "Today is a beautiful day and I want to",
              "In the city of New York, the city",
              "Paris is the capital of France and the capital",
              "Computers and mobile phones have taken over the",
          ]

          snake_case       :   str							=      []
          snake_case       :   Any							=      GPTaTokenizer.from_pretrained(snake_case__ )
          snake_case       :   Optional[Any]							=      TFOPTForCausalLM.from_pretrained(snake_case__ )

          for prompt in self.prompts:
                 snake_case       :   str							=      tokenizer(snake_case__    ,						return_tensors="tf" ).input_ids

                 snake_case       :   Optional[Any]							=      model.generate(snake_case__    ,						max_length=10 )

                 snake_case       :   Any							=      tokenizer.batch_decode(snake_case__    ,						skip_special_tokens=snake_case__ )
                 predicted_outputs += generated_string

          self.assertListEqual(snake_case__    ,						snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Tuple )		->	str:


          '''simple docstring'''


          snake_case       :   Tuple							=      "facebook/opt-350m"

          snake_case       :   Union[str, Any]							=      GPTaTokenizer.from_pretrained(snake_case__ )
          snake_case       :   List[Any]							=      TFOPTForCausalLM.from_pretrained(snake_case__ )

          snake_case       :   Tuple							=      "left"

          # use different length sentences to test batching
          snake_case       :   List[Any]							=      [
              "Hello, my dog is a little",
              "Today, I",
          ]

          snake_case       :   int							=      tokenizer(snake_case__    ,						return_tensors="tf"    ,						padding=snake_case__ )
          snake_case       :   int							=      inputs["input_ids"]

          snake_case       :   int							=      model.generate(input_ids=snake_case__    ,						attention_mask=inputs["attention_mask"] )

          snake_case       :   List[Any]							=      tokenizer(sentences[0]    ,						return_tensors="tf" ).input_ids
          snake_case       :   Dict							=      model.generate(input_ids=snake_case__ )

          snake_case       :   Union[str, Any]							=      inputs_non_padded.shape[-1] - tf.math.reduce_sum(
              tf.cast(inputs["attention_mask"][-1]    ,						tf.intaa ) )
          snake_case       :   Optional[int]							=      tokenizer(sentences[1]    ,						return_tensors="tf" ).input_ids
          snake_case       :   Any							=      model.generate(input_ids=snake_case__    ,						max_length=model.config.max_length - num_paddings )

          snake_case       :   Tuple							=      tokenizer.batch_decode(snake_case__    ,						skip_special_tokens=snake_case__ )
          snake_case       :   Dict							=      tokenizer.decode(output_non_padded[0]    ,						skip_special_tokens=snake_case__ )
          snake_case       :   Optional[Any]							=      tokenizer.decode(output_padded[0]    ,						skip_special_tokens=snake_case__ )

          snake_case       :   Any							=      [
              "Hello, my dog is a little bit of a dork.\nI'm a little bit",
              "Today, I was in the middle of a conversation with a friend about the",
          ]
          self.assertListEqual(snake_case__    ,						snake_case__ )
          self.assertListEqual(snake_case__    ,						[non_padded_sentence, padded_sentence] )



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


          '''simple docstring'''


          snake_case       :   Any							=      "facebook/opt-350m"

          snake_case       :   Optional[int]							=      [
              "Today is a beautiful day and I want to",
              "In the city of San Francisco, the city",
              "Paris is the capital of France and the capital",
              "Computers and mobile phones have taken over the",
          ]

          snake_case       :   int							=      []
          snake_case       :   int							=      GPTaTokenizer.from_pretrained(snake_case__ )
          snake_case       :   Any							=      TFOPTForCausalLM.from_pretrained(snake_case__ )

          for prompt in self.prompts:
                 snake_case       :   Optional[Any]							=      tokenizer(snake_case__    ,						return_tensors="tf" ).input_ids

                 snake_case       :   int							=      model.generate(snake_case__    ,						max_length=10 )

                 snake_case       :   Union[str, Any]							=      tokenizer.batch_decode(snake_case__    ,						skip_special_tokens=snake_case__ )
                 predicted_outputs += generated_string

          self.assertListEqual(snake_case__    ,						snake_case__ )







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


if is_sentencepiece_available():
   from ..ta.tokenization_ta import TaTokenizer
else:
   from ...utils.dummy_sentencepiece_objects import TaTokenizer

__lowerCamelCase							=       TaTokenizer

if is_tokenizers_available():
   from ..ta.tokenization_ta_fast import TaTokenizerFast
else:
   from ...utils.dummy_tokenizers_objects import TaTokenizerFast

__lowerCamelCase							=       TaTokenizerFast

__lowerCamelCase							=       {"""configuration_mt5""": ["""MT5Config""", """MT5OnnxConfig"""]}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase							=       [
       """MT5EncoderModel""",
       """MT5ForConditionalGeneration""",
       """MT5ForQuestionAnswering""",
       """MT5Model""",
       """MT5PreTrainedModel""",
       """MT5Stack""",
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase							=       ["""TFMT5EncoderModel""", """TFMT5ForConditionalGeneration""", """TFMT5Model"""]

try:
   if not is_flax_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase							=       ["""FlaxMT5EncoderModel""", """FlaxMT5ForConditionalGeneration""", """FlaxMT5Model"""]


if TYPE_CHECKING:
   from .configuration_mta import MTaConfig, MTaOnnxConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_mta import (
          MTaEncoderModel,
          MTaForConditionalGeneration,
          MTaForQuestionAnswering,
          MTaModel,
          MTaPreTrainedModel,
          MTaStack,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel

   try:
      if not is_flax_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel

else:
   import sys

   __lowerCamelCase							=       _LazyModule(
       __name__,
       globals()["""__file__"""],
       _import_structure,
       extra_objects={"""MT5Tokenizer""": MTaTokenizer, """MT5TokenizerFast""": MTaTokenizerFast},
       module_spec=__spec__,
   )





