
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__				:							Optional[Any]    ,  lowerCAmelCase__				:							Any    ,  lowerCAmelCase__				:							List[Any]=None    ,  lowerCAmelCase__				:							List[str]=None    ):
 if attention_mask is None:
  __a					:    str													=    tf.cast(tf.math.not_equal(_UpperCAmelCase    ,  config.pad_token_id    )    ,  tf.inta    )
 return {"input_ids": input_ids, "attention_mask": attention_mask}



@require_tf
class 						UpperCamelCase__    :
   _SCREAMING_SNAKE_CASE :				Any      			= OPTConfig
   _SCREAMING_SNAKE_CASE :				Dict      			= {}
   _SCREAMING_SNAKE_CASE :				Optional[int]      			= "gelu"
   def __init__(self					:   Any   ,							snake_case_					:   Dict   ,							snake_case_					:   int=1_3   ,							snake_case_					:   Dict=7   ,							snake_case_					:   Union[str, Any]=True   ,							snake_case_					:   int=False   ,							snake_case_					:   Optional[Any]=9_9   ,							snake_case_					:   str=1_6   ,							snake_case_					:   Optional[int]=2   ,							snake_case_					:   Tuple=4   ,							snake_case_					:   Dict=4   ,							snake_case_					:   Optional[int]="gelu"   ,							snake_case_					:   str=0.1   ,							snake_case_					:   Union[str, Any]=0.1   ,							snake_case_					:   List[str]=2_0   ,							snake_case_					:   str=2   ,							snake_case_					:   Union[str, Any]=1   ,							snake_case_					:   Tuple=0   ,							snake_case_					:   List[Any]=1_6   ,							snake_case_					:   List[str]=1_6   ,							):
    __a					:    Optional[Any]													=    parent
    __a					:    List[str]													=    batch_size
    __a					:    Union[str, Any]													=    seq_length
    __a					:    Optional[int]													=    is_training
    __a					:    Any													=    use_labels
    __a					:    int													=    vocab_size
    __a					:    Optional[Any]													=    hidden_size
    __a					:    int													=    num_hidden_layers
    __a					:    Optional[Any]													=    num_attention_heads
    __a					:    Dict													=    intermediate_size
    __a					:    List[str]													=    hidden_act
    __a					:    Optional[int]													=    hidden_dropout_prob
    __a					:    Union[str, Any]													=    attention_probs_dropout_prob
    __a					:    List[Any]													=    max_position_embeddings
    __a					:    Any													=    eos_token_id
    __a					:    Optional[Any]													=    pad_token_id
    __a					:    int													=    bos_token_id
    __a					:    Tuple													=    embed_dim
    __a					:    List[Any]													=    word_embed_proj_dim
    __a					:    Optional[int]													=    False
   def  lowerCAmelCase     (self					:   Optional[int]  ):
    __a					:    Union[str, Any]													=    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[str]													=    tf.concat([input_ids, eos_tensor]   ,							axis=1  )

    __a					:    Tuple													=    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=__a   ,							**self.config_updates   ,							)
    __a					:    str													=    prepare_opt_inputs_dict(__a   ,							__a  )
    return config, inputs_dict







   def  lowerCAmelCase     (self					:   Union[str, Any]   ,							snake_case_					:   List[Any]   ,							snake_case_					:   List[Any]  ):
    __a					:    List[str]													=    TFOPTModel(config=__a  )
    __a					:    List[str]													=    inputs_dict['input_ids']

    __a					:    Union[str, Any]													=    input_ids[:1, :]
    __a					:    Any													=    inputs_dict['attention_mask'][:1, :]
    __a					:    List[str]													=    1

    # first forward pass
    __a					:    Any													=    model(__a   ,							attention_mask=__a   ,							use_cache=__a  )

    __a					:    Any													=    outputs.to_tuple()

    # create hypothetical next token and extent to next_input_ids
    __a					:    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					:    List[str]													=    tf.concat([input_ids, next_tokens]   ,							axis=-1  )
    __a					:    Tuple													=    tf.concat([attention_mask, next_attn_mask]   ,							axis=-1  )

    __a					:    Dict													=    model(__a   ,							attention_mask=__a  )[0]
    __a					:    Dict													=    model(__a   ,							attention_mask=__a   ,							past_key_values=__a  )[0]

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

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

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





@require_tf
class 						UpperCamelCase__    (			SCREAMING_SNAKE_CASE_       ,SCREAMING_SNAKE_CASE_       ,unittest.TestCase       ):
   _SCREAMING_SNAKE_CASE :				str      			= (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
   _SCREAMING_SNAKE_CASE :				Optional[int]      			= (TFOPTForCausalLM,) if is_tf_available() else ()
   _SCREAMING_SNAKE_CASE :				Any      			= (
       {"feature-extraction": TFOPTModel, "text-generation": TFOPTForCausalLM} if is_tf_available() else {}
   )
   _SCREAMING_SNAKE_CASE :				List[Any]      			= False
   _SCREAMING_SNAKE_CASE :				str      			= False
   _SCREAMING_SNAKE_CASE :				Tuple      			= False
   _SCREAMING_SNAKE_CASE :				List[str]      			= 10
   def  lowerCAmelCase     (self					:   Union[str, Any]  ):
    __a					:    int													=    TFOPTModelTester(self  )
    __a					:    Any													=    ConfigTester(self   ,							config_class=__a  )
   def  lowerCAmelCase     (self					:   List[Any]  ):
    self.config_tester.run_common_tests()
   def  lowerCAmelCase     (self					:   List[Any]  ):
    __a					:    Union[str, Any]													=    self.model_tester.prepare_config_and_inputs_for_common()
    self.model_tester.check_decoder_model_past_large_inputs(*__a  )







   def  lowerCAmelCase     (self					:   Union[str, Any]  ):
    __a					:    str													=    self.model_tester.prepare_config_and_inputs_for_common()

    def _get_word_embedding_weight(snake_case_					:   int   ,							snake_case_					:   int  ):
     if hasattr(__a   ,							'''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(__a   ,							'''weight'''  ):
       return embedding_layer.weight
      else:
       return None

    for model_class in self.all_model_classes:
     for size in [config.vocab_size - 1_0, config.vocab_size + 1_0]:
      # build the embeddings
      __a					:    Union[str, Any]													=    model_class(config=__a  )
      __a					:    Any													=    _get_word_embedding_weight(__a   ,							model.get_input_embeddings()  )
      __a					:    Dict													=    _get_word_embedding_weight(__a   ,							model.get_output_embeddings()  )

      # reshape the embeddings
      model.resize_token_embeddings(__a  )
      __a					:    Optional[int]													=    _get_word_embedding_weight(__a   ,							model.get_input_embeddings()  )
      __a					:    Optional[Any]													=    _get_word_embedding_weight(__a   ,							model.get_output_embeddings()  )

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

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

      # check that weights remain the same after resizing
      __a					:    List[str]													=    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:
        __a					:    Optional[Any]													=    False
      self.assertTrue(__a  )

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

       __a					:    Optional[int]													=    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:
         __a					:    Any													=    False
       self.assertTrue(__a  )





def   __UpperCamelCase	(							lowerCAmelCase__				:							Union[str, Any]    ):
 return tf.constant(_UpperCAmelCase    ,  dtype=tf.intaa    )



@require_tf
class 						UpperCamelCase__    (			unittest.TestCase       ):
   _SCREAMING_SNAKE_CASE :				Union[str, Any]      			= 99
   def  lowerCAmelCase     (self					:   Union[str, Any]  ):
    __a					:    Union[str, Any]													=    tf.ones((4, 1)   ,							dtype=tf.intaa  ) * 2
    __a					:    Optional[int]													=    tf.concat([ids_tensor((4, 6)   ,							self.vocab_size - 3  ) + 3, eos_column_vector]   ,							axis=1  )
    __a					:    List[str]													=    input_ids.shape[0]
    __a					:    int													=    OPTConfig(
        vocab_size=self.vocab_size   ,							hidden_size=2_4   ,							num_hidden_layers=2   ,							num_attention_heads=2   ,							ffn_dim=3_2   ,							max_position_embeddings=4_8   ,							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  lowerCAmelCase     (self					:   List[str]  ):
    __a					:    List[str]													=    TFOPTModel.from_pretrained('''facebook/opt-350m'''  )
    __a					:    Any													=    _long_tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]  )
    __a					:    Dict													=    tf.not_equal(__a   ,							model.config.pad_token_id  )
    with tf.GradientTape():
     __a					:    Optional[Any]													=    model(input_ids=__a   ,							attention_mask=__a  ).last_hidden_state
    __a					:    str													=    (1, 1_1, 5_1_2)
    self.assertEqual(output.shape   ,							__a  )
    __a					:    Dict													=    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]   ,							__a   ,							atol=4E-3  )  )

    __a					:    List[str]													=    tf.function(__a   ,							jit_compile=__a  )
    __a					:    int													=    xla_generate(__a   ,							__a  )[0]
    self.assertTrue(np.allclose(output[:, :3, :3]   ,							__a   ,							atol=4E-2  )  )





@require_tf
@slow
class 						UpperCamelCase__    (			unittest.TestCase       ):
   def  lowerCAmelCase     (self					:   Optional[int]  ):
    super().setUp()
    __a					:    str													=    'facebook/opt-350m'







   def  lowerCAmelCase     (self					:   List[str]  ):
    __a					:    Union[str, Any]													=    TFOPTForCausalLM.from_pretrained(self.path_model  )
    __a					:    Dict													=    GPTaTokenizer.from_pretrained(self.path_model  )

    __a					:    List[Any]													=    [
        '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
    __a					:    Any													=    tokenizer(__a   ,							return_tensors='''tf'''   ,							padding=__a   ,							add_special_tokens=__a  )
    __a					:    Optional[int]													=    tf.math.reduce_mean(model(inputs.input_ids   ,							attention_mask=inputs.attention_mask  )[0]   ,							axis=-1  )
    __a					:    Optional[int]													=    tf.constant(
        [
            [1.3851, -1_3.8_9_2_3, -1_0.5_2_2_9, -1_0.7_5_3_3, -0.2309, -1_0.2_3_8_4, -0.5365, -9.0947, -5.1670],
            [-4.7073, -1_0.6_2_7_6, -3.9415, -2_1.5_2_4_2, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822],
            [0.6247, -3.4229, -8.9179, -1.4297, -1_4.1_6_5_0, 1.4146, -9.0218, -0.2703, -0.2703],
            [6.4783, -1.9913, -1_0.7_9_2_6, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477],
        ]  )
    self.assertTrue(np.allclose(__a   ,							__a   ,							atol=1E-4  )  )

    __a					:    List[Any]													=    tf.function(__a   ,							jit_compile=__a  )
    __a					:    List[str]													=    tf.math.reduce_mean(xla_generate(inputs.input_ids   ,							attention_mask=inputs.attention_mask  )[0]   ,							axis=-1  )
    self.assertTrue(np.allclose(__a   ,							__a   ,							atol=1E-4  )  )





@require_tf
@slow
class 						UpperCamelCase__    (			unittest.TestCase       ):
   @property
   def  lowerCAmelCase     (self					:   Optional[int]  ):
    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  lowerCAmelCase     (self					:   str  ):
    __a					:    Any													=    'facebook/opt-125m'

    __a					:    List[str]													=    [
        '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',
    ]

    __a					:    Optional[Any]													=    []
    __a					:    Dict													=    GPTaTokenizer.from_pretrained(__a  )
    __a					:    Union[str, Any]													=    TFOPTForCausalLM.from_pretrained(__a  )

    for prompt in self.prompts:
     __a					:    Tuple													=    tokenizer(__a   ,							return_tensors='''tf'''  ).input_ids

     __a					:    Optional[Any]													=    model.generate(__a   ,							max_length=1_0  )

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

    self.assertListEqual(__a   ,							__a  )
   def  lowerCAmelCase     (self					:   List[Any]  ):
    __a					:    str													=    'facebook/opt-350m'

    __a					:    Optional[int]													=    GPTaTokenizer.from_pretrained(__a  )
    __a					:    List[str]													=    TFOPTForCausalLM.from_pretrained(__a  )

    __a					:    Optional[int]													=    'left'

    # use different length sentences to test batching
    __a					:    Union[str, Any]													=    [
        'Hello, my dog is a little',
        'Today, I',
    ]

    __a					:    Dict													=    tokenizer(__a   ,							return_tensors='''tf'''   ,							padding=__a  )
    __a					:    Optional[Any]													=    inputs['input_ids']

    __a					:    Union[str, Any]													=    model.generate(input_ids=__a   ,							attention_mask=inputs['''attention_mask''']  )

    __a					:    Union[str, Any]													=    tokenizer(sentences[0]   ,							return_tensors='''tf'''  ).input_ids
    __a					:    Optional[int]													=    model.generate(input_ids=__a  )

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

    __a					:    int													=    tokenizer.batch_decode(__a   ,							skip_special_tokens=__a  )
    __a					:    List[str]													=    tokenizer.decode(output_non_padded[0]   ,							skip_special_tokens=__a  )
    __a					:    List[Any]													=    tokenizer.decode(output_padded[0]   ,							skip_special_tokens=__a  )

    __a					:    Optional[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(__a   ,							__a  )
    self.assertListEqual(__a   ,							[non_padded_sentence, padded_sentence]  )







   def  lowerCAmelCase     (self					:   List[Any]  ):
    __a					:    Dict													=    'facebook/opt-350m'

    __a					:    Union[str, Any]													=    [
        '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',
    ]

    __a					:    int													=    []
    __a					:    Tuple													=    GPTaTokenizer.from_pretrained(__a  )
    __a					:    List[str]													=    TFOPTForCausalLM.from_pretrained(__a  )

    for prompt in self.prompts:
     __a					:    List[Any]													=    tokenizer(__a   ,							return_tensors='''tf'''  ).input_ids

     __a					:    int													=    model.generate(__a   ,							max_length=1_0  )

     __a					:    List[Any]													=    tokenizer.batch_decode(__a   ,							skip_special_tokens=__a  )
     predicted_outputs += generated_string

    self.assertListEqual(__a   ,							__a  )





'''simple docstring'''





import json
import os
import unittest

from transformers.models.blenderbot_small.tokenization_blenderbot_small import (
    VOCAB_FILES_NAMES,
    BlenderbotSmallTokenizer,
)

from ...test_tokenization_common import TokenizerTesterMixin



class  snake_case__						(    SCREAMING_SNAKE_CASE_  ,     unittest.TestCase				):
 A__				= BlenderbotSmallTokenizer
 A__				= False
 def A_   (   self						:							Any  )      ->   str:







       '''simple docstring'''
       super().setUp()

       __snake_case :   Any													=    ['__start__', 'adapt', 'act', 'ap@@', 'te', '__end__', '__unk__']
       __snake_case :   str													=    dict(zip(__a      ,     range(len(__a  )  )  )  )

       __snake_case :   List[Any]													=    ['#version: 0.2', 'a p', 't e</w>', 'ap t</w>', 'a d', 'ad apt</w>', 'a c', 'ac t</w>', '']
       __snake_case :   Tuple													=    {'unk_token': '__unk__', 'bos_token': '__start__', 'eos_token': '__end__'}

       __snake_case :   str													=    os.path.join(self.tmpdirname      ,     VOCAB_FILES_NAMES['vocab_file']  )
       __snake_case :   Union[str, Any]													=    os.path.join(self.tmpdirname      ,     VOCAB_FILES_NAMES['merges_file']  )
       with open(self.vocab_file      ,     'w'      ,     encoding='utf-8'  ) as fp:
             fp.write(json.dumps(__a  ) + '\n'  )
       with open(self.merges_file      ,     'w'      ,     encoding='utf-8'  ) as fp:
             fp.write('\n'.join(__a  )  )
 def A_   (   self						:							Tuple      ,     **__a						:							Optional[int]  )      ->   List[str]:







       '''simple docstring'''
       kwargs.update(self.special_tokens_map  )
       return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname      ,     **__a  )
 def A_   (   self						:							int      ,     __a						:							int  )      ->   List[Any]:







       '''simple docstring'''
       __snake_case :   List[str]													=    'adapt act apte'
       __snake_case :   List[str]													=    'adapt act apte'
       return input_text, output_text
 def A_   (   self						:							Dict  )      ->   str:







       '''simple docstring'''
       __snake_case :   Dict													=    BlenderbotSmallTokenizer(self.vocab_file      ,     self.merges_file      ,     **self.special_tokens_map  )
       __snake_case :   List[str]													=    'adapt act apte'
       __snake_case :   List[Any]													=    ['adapt', 'act', 'ap@@', 'te']
       __snake_case :   Optional[int]													=    tokenizer.tokenize(__a  )
       self.assertListEqual(__a      ,     __a  )

       __snake_case :   List[str]													=    [tokenizer.bos_token] + tokens + [tokenizer.eos_token]

       __snake_case :   List[str]													=    [0, 1, 2, 3, 4, 5]
       self.assertListEqual(tokenizer.convert_tokens_to_ids(__a  )      ,     __a  )
 def A_   (   self						:							int  )      ->   str:







       '''simple docstring'''
       __snake_case :   int													=    BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M'  )
       assert tok('sam'  ).input_ids == [1384]
       __snake_case :   List[str]													=    'I am a small frog.'
       __snake_case :   List[Any]													=    tok([src_text]      ,     padding=__a      ,     truncation=__a  )['input_ids']
       __snake_case :   int													=    tok.batch_decode(__a      ,     skip_special_tokens=__a      ,     clean_up_tokenization_spaces=__a  )[0]
       assert src_text != decoded  # I wish it did!
       assert decoded == "i am a small frog ."





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







       '''simple docstring'''
       __snake_case :   Tuple													=    BlenderbotSmallTokenizer.from_pretrained('facebook/blenderbot-90M'  )
       __snake_case :   Optional[Any]													=    'I am a small frog .'
       __snake_case :   List[str]													=    '.'
       __snake_case :   List[str]													=    tok(__a  )['input_ids']
       __snake_case :   str													=    tok(__a  )['input_ids']

       assert encoded[-1] == encoded_dot[0]



