


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    __lowerCamelCase   (      A__						: int						,						A__						: Union[str, Any]						,						A__						: Union[str, Any]=None						,						A__						: List[Any]=None					)     ->    int:
   if attention_mask is None:
      lowerCamelCase_						:   str		= tf.cast(tf.math.not_equal(A__						,						config.pad_token_id					)						,						tf.inta					)
   return {"input_ids": input_ids, "attention_mask": attention_mask}





@require_tf
class   SCREAMING_SNAKE_CASE_			:




       '''simple docstring'''



       _a   							=							OPTConfig
       _a   							=							{}
       _a   							=							"gelu"



       def __init__(						self				:				int		,				__a				:				Optional[Any]		,				__a				:				Any=13		,				__a				:				str=7		,				__a				:				List[Any]=True		,				__a				:				str=False		,				__a				:				int=99		,				__a				:				str=16		,				__a				:				int=2		,				__a				:				Tuple=4		,				__a				:				int=4		,				__a				:				Dict="gelu"		,				__a				:				Optional[int]=0.1		,				__a				:				Tuple=0.1		,				__a				:				Optional[Any]=20		,				__a				:				str=2		,				__a				:				Dict=1		,				__a				:				List[str]=0		,				__a				:				List[Any]=16		,				__a				:				str=16		,				)					->Dict:
          lowerCamelCase_						:   Dict		= parent
          lowerCamelCase_						:   List[str]		= batch_size
          lowerCamelCase_						:   Tuple		= seq_length
          lowerCamelCase_						:   Optional[int]		= is_training
          lowerCamelCase_						:   str		= use_labels
          lowerCamelCase_						:   Dict		= vocab_size
          lowerCamelCase_						:   Optional[int]		= hidden_size
          lowerCamelCase_						:   List[str]		= num_hidden_layers
          lowerCamelCase_						:   Dict		= num_attention_heads
          lowerCamelCase_						:   Optional[int]		= intermediate_size
          lowerCamelCase_						:   Union[str, Any]		= hidden_act
          lowerCamelCase_						:   Union[str, Any]		= hidden_dropout_prob
          lowerCamelCase_						:   List[Any]		= attention_probs_dropout_prob
          lowerCamelCase_						:   Dict		= max_position_embeddings
          lowerCamelCase_						:   List[str]		= eos_token_id
          lowerCamelCase_						:   str		= pad_token_id
          lowerCamelCase_						:   Dict		= bos_token_id
          lowerCamelCase_						:   int		= embed_dim
          lowerCamelCase_						:   Optional[Any]		= word_embed_proj_dim
          lowerCamelCase_						:   Dict		= False



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->Optional[Any]:
          lowerCamelCase_						:   Dict		= ids_tensor([self.batch_size, self.seq_length - 1]		,				self.vocab_size							)
          lowerCamelCase_						:   Tuple		= tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size							)		,				1							)
          lowerCamelCase_						:   List[Any]		= tf.concat([input_ids, eos_tensor]		,				axis=1							)

          lowerCamelCase_						:   int		= 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		,				)
          lowerCamelCase_						:   Optional[Any]		= prepare_opt_inputs_dict(__a		,				__a							)
          return config, inputs_dict




       def 				_lowerCAmelCase							(						self				:				int		,				__a				:				Optional[int]		,				__a				:				List[str]							)					->Optional[int]:
          lowerCamelCase_						:   Any		= TFOPTModel(config=__a							)
          lowerCamelCase_						:   str		= inputs_dict["""input_ids"""]

          lowerCamelCase_						:   int		= input_ids[:1, :]
          lowerCamelCase_						:   Dict		= inputs_dict["""attention_mask"""][:1, :]
          lowerCamelCase_						:   Any		= 1

          # first forward pass
          lowerCamelCase_						:   List[Any]		= model(__a		,				attention_mask=__a		,				use_cache=__a							)

          lowerCamelCase_,	lowerCamelCase_						:   Any		= outputs.to_tuple()

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

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

          lowerCamelCase_						:   Optional[int]		= model(__a		,				attention_mask=__a							)[0]
          lowerCamelCase_						:   Any		= 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
          lowerCamelCase_						:   Optional[Any]		= int(ids_tensor((1,)		,				output_from_past.shape[-1]							)							)
          lowerCamelCase_						:   Optional[Any]		= output_from_no_past[:, -3:, random_slice_idx]
          lowerCamelCase_						:   Tuple		= 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   SCREAMING_SNAKE_CASE_			(a__						,       a__						,       unittest.TestCase						):




       '''simple docstring'''



       _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 				_lowerCAmelCase							(						self				:				List[Any]							)					->Optional[Any]:
          lowerCamelCase_						:   str		= TFOPTModelTester(self							)
          lowerCamelCase_						:   Optional[Any]		= ConfigTester(self		,				config_class=__a							)



       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->Tuple:
          self.config_tester.run_common_tests()



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->List[str]:
          lowerCamelCase_						:   List[Any]		= self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.check_decoder_model_past_large_inputs(*__a							)




       def 				_lowerCAmelCase							(						self				:				Optional[Any]							)					->Any:
          lowerCamelCase_,	lowerCamelCase_						:   Tuple		= self.model_tester.prepare_config_and_inputs_for_common()

          def _get_word_embedding_weight(__a				:				List[Any]		,				__a				:				List[str]							):
             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
                lowerCamelCase_						:   Tuple		= model_class(config=__a							)
                lowerCamelCase_						:   Optional[Any]		= _get_word_embedding_weight(__a		,				model.get_input_embeddings()							)
                lowerCamelCase_						:   Dict		= _get_word_embedding_weight(__a		,				model.get_output_embeddings()							)

                # reshape the embeddings
                model.resize_token_embeddings(__a							)
                lowerCamelCase_						:   List[str]		= _get_word_embedding_weight(__a		,				model.get_input_embeddings()							)
                lowerCamelCase_						:   int		= _get_word_embedding_weight(__a		,				model.get_output_embeddings()							)

                # check that the resized embeddings size matches the desired size.
                lowerCamelCase_						:   List[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
                lowerCamelCase_						:   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:
                      lowerCamelCase_						:   Optional[int]		= 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							)

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







def    __lowerCamelCase   (      A__						: List[Any]					)     ->    int:
   return tf.constant(A__						,						dtype=tf.intaa					)





@require_tf
class   SCREAMING_SNAKE_CASE_			(unittest.TestCase						):




       '''simple docstring'''



       _a   							=							99



       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->Any:
          lowerCamelCase_						:   List[str]		= tf.ones((4, 1)		,				dtype=tf.intaa							) * 2
          lowerCamelCase_						:   List[str]		= tf.concat([ids_tensor((4, 6)		,				self.vocab_size - 3							) + 3, eos_column_vector]		,				axis=1							)
          lowerCamelCase_						:   Optional[int]		= input_ids.shape[0]
          lowerCamelCase_						:   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   SCREAMING_SNAKE_CASE_			(unittest.TestCase						):




       '''simple docstring'''



       @slow
       def 				_lowerCAmelCase							(						self				:				Dict							)					->Tuple:
          lowerCamelCase_						:   Dict		= TFOPTModel.from_pretrained("""facebook/opt-350m"""							)
          lowerCamelCase_						:   Optional[Any]		= _long_tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]							)
          lowerCamelCase_						:   Union[str, Any]		= tf.not_equal(__a		,				model.config.pad_token_id							)
          with tf.GradientTape():
             lowerCamelCase_						:   List[str]		= model(input_ids=__a		,				attention_mask=__a							).last_hidden_state
          lowerCamelCase_						:   Union[str, Any]		= (1, 11, 512)
          self.assertEqual(output.shape		,				__a							)
          lowerCamelCase_						:   Tuple		= tf.constant(
              [[-0.2_873, -1.9_218, -0.3_033], [-1.2_710, -0.1_338, -0.1_902], [0.4_095, 0.1_214, -1.3_121]]							)
          self.assertTrue(np.allclose(output[:, :3, :3]		,				__a		,				atol=4e-3							)							)

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







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




       '''simple docstring'''



       def 				_lowerCAmelCase							(						self				:				Optional[int]							)					->Tuple:
          super().setUp()
          lowerCamelCase_						:   Optional[Any]		= """facebook/opt-350m"""




       def 				_lowerCAmelCase							(						self				:				str							)					->Tuple:
          lowerCamelCase_						:   int		= TFOPTForCausalLM.from_pretrained(self.path_model							)
          lowerCamelCase_						:   str		= GPTaTokenizer.from_pretrained(self.path_model							)

          lowerCamelCase_						:   Dict		= [
              """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
          lowerCamelCase_						:   Any		= tokenizer(__a		,				return_tensors="""tf"""		,				padding=__a		,				add_special_tokens=__a							)
          lowerCamelCase_						:   Optional[Any]		= tf.math.reduce_mean(model(inputs.input_ids		,				attention_mask=inputs.attention_mask							)[0]		,				axis=-1							)
          lowerCamelCase_						:   Dict		= tf.constant(
              [
                  [1.3_851, -13.8_923, -10.5_229, -10.7_533, -0.2_309, -10.2_384, -0.5_365, -9.0_947, -5.1_670],
                  [-4.7_073, -10.6_276, -3.9_415, -21.5_242, -0.2_822, -0.2_822, -0.2_822, -0.2_822, -0.2_822],
                  [0.6_247, -3.4_229, -8.9_179, -1.4_297, -14.1_650, 1.4_146, -9.0_218, -0.2_703, -0.2_703],
                  [6.4_783, -1.9_913, -10.7_926, -2.3_336, 1.5_092, -0.9_974, -6.8_213, 1.3_477, 1.3_477],
              ]							)
          self.assertTrue(np.allclose(__a		,				__a		,				atol=1e-4							)							)

          lowerCamelCase_						:   int		= tf.function(__a		,				jit_compile=__a							)
          lowerCamelCase_						:   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   SCREAMING_SNAKE_CASE_			(unittest.TestCase						):




       '''simple docstring'''



       @property
       def 				_lowerCAmelCase							(						self				:				Any							)					->Union[str, Any]:
          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				:				Dict							)					->Optional[int]:
          lowerCamelCase_						:   int		= """facebook/opt-125m"""

          lowerCamelCase_						:   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""",
          ]

          lowerCamelCase_						:   Any		= []
          lowerCamelCase_						:   Tuple		= GPTaTokenizer.from_pretrained(__a							)
          lowerCamelCase_						:   Union[str, Any]		= TFOPTForCausalLM.from_pretrained(__a							)

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

             lowerCamelCase_						:   Any		= model.generate(__a		,				max_length=10							)

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

          self.assertListEqual(__a		,				__a							)



       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->Any:
          lowerCamelCase_						:   List[str]		= """facebook/opt-350m"""

          lowerCamelCase_						:   Any		= GPTaTokenizer.from_pretrained(__a							)
          lowerCamelCase_						:   Tuple		= TFOPTForCausalLM.from_pretrained(__a							)

          lowerCamelCase_						:   int		= """left"""

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

          lowerCamelCase_						:   Optional[int]		= tokenizer(__a		,				return_tensors="""tf"""		,				padding=__a							)
          lowerCamelCase_						:   str		= inputs["""input_ids"""]

          lowerCamelCase_						:   str		= model.generate(input_ids=__a		,				attention_mask=inputs["""attention_mask"""]							)

          lowerCamelCase_						:   Any		= tokenizer(sentences[0]		,				return_tensors="""tf"""							).input_ids
          lowerCamelCase_						:   List[Any]		= model.generate(input_ids=__a							)

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

          lowerCamelCase_						:   Dict		= tokenizer.batch_decode(__a		,				skip_special_tokens=__a							)
          lowerCamelCase_						:   Any		= tokenizer.decode(output_non_padded[0]		,				skip_special_tokens=__a							)
          lowerCamelCase_						:   Optional[Any]		= tokenizer.decode(output_padded[0]		,				skip_special_tokens=__a							)

          lowerCamelCase_						:   str		= [
              """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				:				Optional[int]							)					->List[str]:
          lowerCamelCase_						:   Union[str, Any]		= """facebook/opt-350m"""

          lowerCamelCase_						:   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""",
          ]

          lowerCamelCase_						:   Dict		= []
          lowerCamelCase_						:   str		= GPTaTokenizer.from_pretrained(__a							)
          lowerCamelCase_						:   str		= TFOPTForCausalLM.from_pretrained(__a							)

          for prompt in self.prompts:
             lowerCamelCase_						:   Union[str, Any]		= tokenizer(__a		,				return_tensors="""tf"""							).input_ids

             lowerCamelCase_						:   List[Any]		= model.generate(__a		,				max_length=10							)

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

          self.assertListEqual(__a		,				__a							)









from . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)





