



'''simple docstring'''





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   a_   (		_UpperCAmelCase    :      Optional[Any] ,_UpperCAmelCase    :      Any ,_UpperCAmelCase    :      List[Any]=None ,_UpperCAmelCase    :      List[str]=None				)    ->  int:
      if attention_mask is None:
            __snake_case :   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  snake_case__						:
 A__				= OPTConfig
 A__				= {}
 A__				= '''gelu'''
 def __init__(   self						:							Any      ,     __a						:							Dict      ,     __a						:							int=13      ,     __a						:							Dict=7      ,     __a						:							Union[str, Any]=True      ,     __a						:							int=False      ,     __a						:							Optional[Any]=99      ,     __a						:							str=16      ,     __a						:							Optional[int]=2      ,     __a						:							Tuple=4      ,     __a						:							Dict=4      ,     __a						:							Optional[int]="gelu"      ,     __a						:							str=0.1      ,     __a						:							Union[str, Any]=0.1      ,     __a						:							List[str]=20      ,     __a						:							str=2      ,     __a						:							Union[str, Any]=1      ,     __a						:							Tuple=0      ,     __a						:							List[Any]=16      ,     __a						:							List[str]=16      ,     )      ->   Optional[int]:







       '''simple docstring'''
       __snake_case :   Optional[Any]													=    parent
       __snake_case :   List[str]													=    batch_size
       __snake_case :   Union[str, Any]													=    seq_length
       __snake_case :   Optional[int]													=    is_training
       __snake_case :   Any													=    use_labels
       __snake_case :   int													=    vocab_size
       __snake_case :   Optional[Any]													=    hidden_size
       __snake_case :   int													=    num_hidden_layers
       __snake_case :   Optional[Any]													=    num_attention_heads
       __snake_case :   Dict													=    intermediate_size
       __snake_case :   List[str]													=    hidden_act
       __snake_case :   Optional[int]													=    hidden_dropout_prob
       __snake_case :   Union[str, Any]													=    attention_probs_dropout_prob
       __snake_case :   List[Any]													=    max_position_embeddings
       __snake_case :   Any													=    eos_token_id
       __snake_case :   Optional[Any]													=    pad_token_id
       __snake_case :   int													=    bos_token_id
       __snake_case :   Tuple													=    embed_dim
       __snake_case :   List[Any]													=    word_embed_proj_dim
       __snake_case :   Optional[int]													=    False
 def A_   (   self						:							Optional[int]  )      ->   List[Any]:







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

       __snake_case :   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      ,     )
       __snake_case :   str													=    prepare_opt_inputs_dict(__a      ,     __a  )
       return config, inputs_dict





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







       '''simple docstring'''
       __snake_case :   List[str]													=    TFOPTModel(config=__a  )
       __snake_case :   List[str]													=    inputs_dict['input_ids']

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

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

       __snake_case							,     __snake_case :   Any													=    outputs.to_tuple()

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

       # append to next input_ids and
       __snake_case :   List[str]													=    tf.concat([input_ids, next_tokens]      ,     axis=-1  )
       __snake_case :   Tuple													=    tf.concat([attention_mask, next_attn_mask]      ,     axis=-1  )

       __snake_case :   Dict													=    model(__a      ,     attention_mask=__a  )[0]
       __snake_case :   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
       __snake_case :   Tuple													=    int(ids_tensor((1,)      ,     output_from_past.shape[-1]  )  )
       __snake_case :   Dict													=    output_from_no_past[:, -3:, random_slice_idx]
       __snake_case :   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  snake_case__						(    SCREAMING_SNAKE_CASE_  ,     SCREAMING_SNAKE_CASE_  ,     unittest.TestCase				):
 A__				= (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
 A__				= (TFOPTForCausalLM,) if is_tf_available() else ()
 A__				= (
     {'''feature-extraction''': TFOPTModel, '''text-generation''': TFOPTForCausalLM} if is_tf_available() else {}
 )
 A__				= False
 A__				= False
 A__				= False
 A__				= 10
 def A_   (   self						:							Union[str, Any]  )      ->   int:







       '''simple docstring'''
       __snake_case :   int													=    TFOPTModelTester(self  )
       __snake_case :   Any													=    ConfigTester(self      ,     config_class=__a  )
 def A_   (   self						:							List[Any]  )      ->   List[str]:







       '''simple docstring'''
       self.config_tester.run_common_tests()
 def A_   (   self						:							List[Any]  )      ->   int:







       '''simple docstring'''
       __snake_case :   Union[str, Any]													=    self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.check_decoder_model_past_large_inputs(*__a  )





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







       '''simple docstring'''
       __snake_case							,     __snake_case :   str													=    self.model_tester.prepare_config_and_inputs_for_common()

       def _get_word_embedding_weight(__a						:							int      ,     __a						:							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 - 10, config.vocab_size + 10]:
                   # build the embeddings
                   __snake_case :   Union[str, Any]													=    model_class(config=__a  )
                   __snake_case :   Any													=    _get_word_embedding_weight(__a      ,     model.get_input_embeddings()  )
                   __snake_case :   Dict													=    _get_word_embedding_weight(__a      ,     model.get_output_embeddings()  )

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

                   # check that the resized embeddings size matches the desired size.
                   __snake_case :   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
                   __snake_case :   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:
                               __snake_case :   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  )

                         __snake_case :   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:
                                     __snake_case :   Any													=    False
                         self.assertTrue(__a  )





def   a_   (		_UpperCAmelCase    :      Union[str, Any]				)    ->  Tuple:
      return tf.constant(_UpperCAmelCase ,dtype=tf.intaa				)



@require_tf
class  snake_case__						(    unittest.TestCase				):
 A__				= 99
 def A_   (   self						:							Union[str, Any]  )      ->   str:







       '''simple docstring'''
       __snake_case :   Union[str, Any]													=    tf.ones((4, 1)      ,     dtype=tf.intaa  ) * 2
       __snake_case :   Optional[int]													=    tf.concat([ids_tensor((4, 6)      ,     self.vocab_size - 3  ) + 3, eos_column_vector]      ,     axis=1  )
       __snake_case :   List[str]													=    input_ids.shape[0]
       __snake_case :   int													=    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  snake_case__						(    unittest.TestCase				):
 @slow
 def A_   (   self						:							List[str]  )      ->   Optional[Any]:







       '''simple docstring'''
       __snake_case :   List[str]													=    TFOPTModel.from_pretrained('facebook/opt-350m'  )
       __snake_case :   Any													=    _long_tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]  )
       __snake_case :   Dict													=    tf.not_equal(__a      ,     model.config.pad_token_id  )
       with tf.GradientTape():
             __snake_case :   Optional[Any]													=    model(input_ids=__a      ,     attention_mask=__a  ).last_hidden_state
       __snake_case :   str													=    (1, 11, 512)
       self.assertEqual(output.shape      ,     __a  )
       __snake_case :   Dict													=    tf.constant(
           [[-0.2_8_7_3, -1.9_2_1_8, -0.3_0_3_3], [-1.2_7_1_0, -0.1_3_3_8, -0.1_9_0_2], [0.4_0_9_5, 0.1_2_1_4, -1.3_1_2_1]]  )
       self.assertTrue(np.allclose(output[:, :3, :3]      ,     __a      ,     atol=4e-3  )  )

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



@require_tf
@slow
class  snake_case__						(    unittest.TestCase				):
 def A_   (   self						:							Optional[int]  )      ->   List[Any]:







       '''simple docstring'''
       super().setUp()
       __snake_case :   str													=    'facebook/opt-350m'





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







       '''simple docstring'''
       __snake_case :   Union[str, Any]													=    TFOPTForCausalLM.from_pretrained(self.path_model  )
       __snake_case :   Dict													=    GPTaTokenizer.from_pretrained(self.path_model  )

       __snake_case :   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
       __snake_case :   Any													=    tokenizer(__a      ,     return_tensors='tf'      ,     padding=__a      ,     add_special_tokens=__a  )
       __snake_case :   Optional[int]													=    tf.math.reduce_mean(model(inputs.input_ids      ,     attention_mask=inputs.attention_mask  )[0]      ,     axis=-1  )
       __snake_case :   Optional[int]													=    tf.constant(
           [
               [1.3_8_5_1, -1_3.8_9_2_3, -1_0.5_2_2_9, -1_0.7_5_3_3, -0.2_3_0_9, -1_0.2_3_8_4, -0.5_3_6_5, -9.0_9_4_7, -5.1_6_7_0],
               [-4.7_0_7_3, -1_0.6_2_7_6, -3.9_4_1_5, -2_1.5_2_4_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2],
               [0.6_2_4_7, -3.4_2_2_9, -8.9_1_7_9, -1.4_2_9_7, -1_4.1_6_5_0, 1.4_1_4_6, -9.0_2_1_8, -0.2_7_0_3, -0.2_7_0_3],
               [6.4_7_8_3, -1.9_9_1_3, -1_0.7_9_2_6, -2.3_3_3_6, 1.5_0_9_2, -0.9_9_7_4, -6.8_2_1_3, 1.3_4_7_7, 1.3_4_7_7],
           ]  )
       self.assertTrue(np.allclose(__a      ,     __a      ,     atol=1e-4  )  )

       __snake_case :   List[Any]													=    tf.function(__a      ,     jit_compile=__a  )
       __snake_case :   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  snake_case__						(    unittest.TestCase				):
 @property
 def A_   (   self						:							Optional[int]  )      ->   List[str]:







       '''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 A_   (   self						:							str  )      ->   int:







       '''simple docstring'''
       __snake_case :   Any													=    'facebook/opt-125m'

       __snake_case :   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',
       ]

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

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

             __snake_case :   Optional[Any]													=    model.generate(__a      ,     max_length=10  )

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

       self.assertListEqual(__a      ,     __a  )
 def A_   (   self						:							List[Any]  )      ->   Dict:







       '''simple docstring'''
       __snake_case :   str													=    'facebook/opt-350m'

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

       __snake_case :   Optional[int]													=    'left'

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

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

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

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

       __snake_case :   int													=    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=__a      ,     max_length=model.config.max_length - num_paddings  )

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

       __snake_case :   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 A_   (   self						:							List[Any]  )      ->   List[str]:







       '''simple docstring'''
       __snake_case :   Dict													=    'facebook/opt-350m'

       __snake_case :   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',
       ]

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

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

             __snake_case :   int													=    model.generate(__a      ,     max_length=10  )

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

       self.assertListEqual(__a      ,     __a  )








'''simple docstring'''





from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A__    :   List[str]           =					{
    '''configuration_autoformer''': [
        '''AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''AutoformerConfig''',
    ],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    A__    :   Optional[Any]           =					[
        '''AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
        '''AutoformerForPrediction''',
        '''AutoformerModel''',
        '''AutoformerPreTrainedModel''',
    ]


if TYPE_CHECKING:
    from .configuration_autoformer import (
        AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
        AutoformerConfig,
    )

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_autoformer import (
            AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            AutoformerForPrediction,
            AutoformerModel,
            AutoformerPreTrainedModel,
        )

else:
    import sys

    A__    :   Optional[int]           =					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



