


"""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       lowerCamelCase					(a_						:List[str]				,				a_						:Union[str, Any]				,				a_						:Dict=None				,				a_						:str=None)     ->							Union[str, Any]:
       if attention_mask is None:
              lowercase		:Optional[int]       							=				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 					__magic_name__		:
       __A			:      int        =   OPTConfig
       __A			:      Union[str, Any]        =   {}
       __A			:      Tuple        =   "gelu"






       def __init__(   self  :    Union[str, Any]  ,     snake_case__  :    List[Any]  ,     snake_case__  :    str=1_3  ,     snake_case__  :    Optional[int]=7  ,     snake_case__  :    Tuple=True  ,     snake_case__  :    Any=False  ,     snake_case__  :    str=9_9  ,     snake_case__  :    Any=1_6  ,     snake_case__  :    List[str]=2  ,     snake_case__  :    List[Any]=4  ,     snake_case__  :    Any=4  ,     snake_case__  :    Any="gelu"  ,     snake_case__  :    List[Any]=0.1  ,     snake_case__  :    Any=0.1  ,     snake_case__  :    Union[str, Any]=2_0  ,     snake_case__  :    List[str]=2  ,     snake_case__  :    Optional[int]=1  ,     snake_case__  :    Any=0  ,     snake_case__  :    Tuple=1_6  ,     snake_case__  :    List[str]=1_6  ,     ):

              '''simple docstring'''






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






       def  __snake_case						(   self  :    Tuple				):

              '''simple docstring'''






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

              lowercase		:Any       							=				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  ,     )
              lowercase		:str       							=				prepare_opt_inputs_dict(snake_case__  ,     snake_case__				)
              return config, inputs_dict







       def  __snake_case						(   self  :    str  ,     snake_case__  :    Dict  ,     snake_case__  :    Any				):

              '''simple docstring'''






              lowercase		:Any       							=				TFOPTModel(config=snake_case__				)
              lowercase		:List[Any]       							=				inputs_dict['''input_ids''']

              lowercase		:Optional[Any]       							=				input_ids[:1, :]
              lowercase		:List[str]       							=				inputs_dict['''attention_mask'''][:1, :]
              lowercase		:List[Any]       							=				1

              # first forward pass
              lowercase		:Optional[Any]       							=				model(snake_case__  ,     attention_mask=snake_case__  ,     use_cache=snake_case__				)

              lowercase							,  lowercase		:int       							=				outputs.to_tuple()

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

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

              lowercase		:Union[str, Any]       							=				model(snake_case__  ,     attention_mask=snake_case__				)[0]
              lowercase		:Dict       							=				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
              lowercase		:List[Any]       							=				int(ids_tensor((1,)  ,     output_from_past.shape[-1]				)				)
              lowercase		:Union[str, Any]       							=				output_from_no_past[:, -3:, random_slice_idx]
              lowercase		:Optional[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 					__magic_name__		(			__UpperCAmelCase			,						__UpperCAmelCase			,						unittest.TestCase     ):
       __A			:      int        =   (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
       __A			:      Optional[int]        =   (TFOPTForCausalLM,) if is_tf_available() else ()
       __A			:      List[str]        =   (
           {"feature-extraction": TFOPTModel, "text-generation": TFOPTForCausalLM} if is_tf_available() else {}
       )
       __A			:      Tuple        =   False
       __A			:      Optional[Any]        =   False
       __A			:      int        =   False
       __A			:      Optional[Any]        =   10






       def  __snake_case						(   self  :    List[Any]				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				TFOPTModelTester(self				)
              lowercase		:Tuple       							=				ConfigTester(self  ,     config_class=snake_case__				)






       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              self.config_tester.run_common_tests()






       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:Any       							=				self.model_tester.prepare_config_and_inputs_for_common()
              self.model_tester.check_decoder_model_past_large_inputs(*snake_case__				)







       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              lowercase							,  lowercase		:List[str]       							=				self.model_tester.prepare_config_and_inputs_for_common()

              def _get_word_embedding_weight(snake_case__  :    List[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 - 1_0, config.vocab_size + 1_0]:
                            # build the embeddings
                            lowercase		:Any       							=				model_class(config=snake_case__				)
                            lowercase		:Optional[Any]       							=				_get_word_embedding_weight(snake_case__  ,     model.get_input_embeddings()				)
                            lowercase		:Optional[Any]       							=				_get_word_embedding_weight(snake_case__  ,     model.get_output_embeddings()				)

                            # reshape the embeddings
                            model.resize_token_embeddings(snake_case__				)
                            lowercase		:List[str]       							=				_get_word_embedding_weight(snake_case__  ,     model.get_input_embeddings()				)
                            lowercase		:List[str]       							=				_get_word_embedding_weight(snake_case__  ,     model.get_output_embeddings()				)

                            # check that the resized embeddings size matches the desired size.
                            lowercase		:Dict       							=				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
                            lowercase		: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:
                                          lowercase		:Tuple       							=				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__				)

                                   lowercase		: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:
                                                 lowercase		:Union[str, Any]       							=				False
                                   self.assertTrue(snake_case__				)







def       lowerCamelCase					(a_						:List[Any])     ->							List[Any]:
       return tf.constant(a_				,				dtype=tf.intaa)
@require_tf
class 					__magic_name__		(			unittest.TestCase     ):
       __A			:      List[str]        =   99






       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






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






       @slow
       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				TFOPTModel.from_pretrained('''facebook/opt-350m'''				)
              lowercase		:int       							=				_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]]				)
              lowercase		:List[Any]       							=				tf.not_equal(snake_case__  ,     model.config.pad_token_id				)
              with tf.GradientTape():
                     lowercase		:List[str]       							=				model(input_ids=snake_case__  ,     attention_mask=snake_case__				).last_hidden_state
              lowercase		:Any       							=				(1, 1_1, 5_1_2)
              self.assertEqual(output.shape  ,     snake_case__				)
              lowercase		:str       							=				tf.constant(
                  [[-0.28_73, -1.92_18, -0.30_33], [-1.27_10, -0.13_38, -0.19_02], [0.40_95, 0.12_14, -1.31_21]]				)
              self.assertTrue(np.allclose(output[:, :3, :3]  ,     snake_case__  ,     atol=4e-3				)				)

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







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






       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              super().setUp()
              lowercase		:str       							=				'''facebook/opt-350m'''







       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:str       							=				TFOPTForCausalLM.from_pretrained(self.path_model				)
              lowercase		:Optional[Any]       							=				GPTaTokenizer.from_pretrained(self.path_model				)

              lowercase		: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
              lowercase		:Tuple       							=				tokenizer(snake_case__  ,     return_tensors='''tf'''  ,     padding=snake_case__  ,     add_special_tokens=snake_case__				)
              lowercase		:str       							=				tf.math.reduce_mean(model(inputs.input_ids  ,     attention_mask=inputs.attention_mask				)[0]  ,     axis=-1				)
              lowercase		:Dict       							=				tf.constant(
                  [
                      [1.38_51, -13.89_23, -10.52_29, -10.75_33, -0.23_09, -10.23_84, -0.53_65, -9.09_47, -5.16_70],
                      [-4.70_73, -10.62_76, -3.94_15, -21.52_42, -0.28_22, -0.28_22, -0.28_22, -0.28_22, -0.28_22],
                      [0.62_47, -3.42_29, -8.91_79, -1.42_97, -14.16_50, 1.41_46, -9.02_18, -0.27_03, -0.27_03],
                      [6.47_83, -1.99_13, -10.79_26, -2.33_36, 1.50_92, -0.99_74, -6.82_13, 1.34_77, 1.34_77],
                  ]				)
              self.assertTrue(np.allclose(snake_case__  ,     snake_case__  ,     atol=1e-4				)				)

              lowercase		:int       							=				tf.function(snake_case__  ,     jit_compile=snake_case__				)
              lowercase		:Optional[int]       							=				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 					__magic_name__		(			unittest.TestCase     ):






       @property
       def  __snake_case						(   self  :    Optional[int]				):

              '''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  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              lowercase		:Any       							=				'''facebook/opt-125m'''

              lowercase		:Tuple       							=				[
                  '''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''',
              ]

              lowercase		:Tuple       							=				[]
              lowercase		:Dict       							=				GPTaTokenizer.from_pretrained(snake_case__				)
              lowercase		:Dict       							=				TFOPTForCausalLM.from_pretrained(snake_case__				)

              for prompt in self.prompts:
                     lowercase		:Any       							=				tokenizer(snake_case__  ,     return_tensors='''tf'''				).input_ids

                     lowercase		:Optional[int]       							=				model.generate(snake_case__  ,     max_length=1_0				)

                     lowercase		:Dict       							=				tokenizer.batch_decode(snake_case__  ,     skip_special_tokens=snake_case__				)
                     predicted_outputs += generated_string

              self.assertListEqual(snake_case__  ,     snake_case__				)






       def  __snake_case						(   self  :    List[Any]				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				'''facebook/opt-350m'''

              lowercase		:Dict       							=				GPTaTokenizer.from_pretrained(snake_case__				)
              lowercase		:int       							=				TFOPTForCausalLM.from_pretrained(snake_case__				)

              lowercase		:int       							=				'''left'''

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

              lowercase		:Optional[int]       							=				tokenizer(snake_case__  ,     return_tensors='''tf'''  ,     padding=snake_case__				)
              lowercase		:Any       							=				inputs['''input_ids''']

              lowercase		:List[Any]       							=				model.generate(input_ids=snake_case__  ,     attention_mask=inputs['''attention_mask''']				)

              lowercase		:List[str]       							=				tokenizer(sentences[0]  ,     return_tensors='''tf'''				).input_ids
              lowercase		:Tuple       							=				model.generate(input_ids=snake_case__				)

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

              lowercase		:Optional[int]       							=				tokenizer.batch_decode(snake_case__  ,     skip_special_tokens=snake_case__				)
              lowercase		:Dict       							=				tokenizer.decode(output_non_padded[0]  ,     skip_special_tokens=snake_case__				)
              lowercase		:List[str]       							=				tokenizer.decode(output_padded[0]  ,     skip_special_tokens=snake_case__				)

              lowercase		:Union[str, 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  __snake_case						(   self  :    Tuple				):

              '''simple docstring'''






              lowercase		:List[str]       							=				'''facebook/opt-350m'''

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

              lowercase		:str       							=				[]
              lowercase		:Tuple       							=				GPTaTokenizer.from_pretrained(snake_case__				)
              lowercase		:Any       							=				TFOPTForCausalLM.from_pretrained(snake_case__				)

              for prompt in self.prompts:
                     lowercase		:Optional[int]       							=				tokenizer(snake_case__  ,     return_tensors='''tf'''				).input_ids

                     lowercase		:Tuple       							=				model.generate(snake_case__  ,     max_length=1_0				)

                     lowercase		:Tuple       							=				tokenizer.batch_decode(snake_case__  ,     skip_special_tokens=snake_case__				)
                     predicted_outputs += generated_string

              self.assertListEqual(snake_case__  ,     snake_case__				)





"""simple docstring"""



def       lowerCamelCase					(a_						:int)     ->							str:
       if number > 0:
              raise ValueError('''input must be a negative integer''')
       lowercase		:Any       							=				len(bin(a_)[3:])
       lowercase		:Any       							=				bin(abs(a_) - (1 << binary_number_length))[3:]
       lowercase		:Tuple       							=				(
           (
               '''1'''
               + '''0''' * (binary_number_length - len(a_))
               + twos_complement_number
           )
           if number < 0
           else '''0'''
       )
       return "0b" + twos_complement_number


if __name__ == "__main__":
 import doctest

 doctest.testmod()

