





'''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 				snake_case_      (		SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__=None					,	SCREAMING_SNAKE_CASE__=None       ):







  '''simple docstring'''



  if attention_mask is None:
    _snake_case     		=				tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE__					,	config.pad_token_id       )					,	tf.inta       )
  return {"input_ids": input_ids, "attention_mask": attention_mask}


@require_tf
class       __SCREAMING_SNAKE_CASE	:



     '''simple docstring'''






     UpperCAmelCase__       :						List[Any]      =   OPTConfig
     UpperCAmelCase__       :						str      =   {}
     UpperCAmelCase__       :						Tuple      =   '''gelu'''

     def __init__(					self						,   lowerCamelCase						,   lowerCamelCase=13						,   lowerCamelCase=7						,   lowerCamelCase=True						,   lowerCamelCase=False						,   lowerCamelCase=99						,   lowerCamelCase=16						,   lowerCamelCase=2						,   lowerCamelCase=4						,   lowerCamelCase=4						,   lowerCamelCase="gelu"						,   lowerCamelCase=0.1						,   lowerCamelCase=0.1						,   lowerCamelCase=20						,   lowerCamelCase=2						,   lowerCamelCase=1						,   lowerCamelCase=0						,   lowerCamelCase=16						,   lowerCamelCase=16						,   ):
       _snake_case     		=				parent
       _snake_case     		=				batch_size
       _snake_case     		=				seq_length
       _snake_case     		=				is_training
       _snake_case     		=				use_labels
       _snake_case     		=				vocab_size
       _snake_case     		=				hidden_size
       _snake_case     		=				num_hidden_layers
       _snake_case     		=				num_attention_heads
       _snake_case     		=				intermediate_size
       _snake_case     		=				hidden_act
       _snake_case     		=				hidden_dropout_prob
       _snake_case     		=				attention_probs_dropout_prob
       _snake_case     		=				max_position_embeddings
       _snake_case     		=				eos_token_id
       _snake_case     		=				pad_token_id
       _snake_case     		=				bos_token_id
       _snake_case     		=				embed_dim
       _snake_case     		=				word_embed_proj_dim
       _snake_case     		=				False

     def 	UpperCamelCase(					self	):
       _snake_case     		=				ids_tensor([self.batch_size, self.seq_length - 1]						,   self.vocab_size	)
       _snake_case     		=				tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size	)						,   1	)
       _snake_case     		=				tf.concat([input_ids, eos_tensor]						,   axis=1	)

       _snake_case     		=				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=lowerCamelCase						,   **self.config_updates						,   )
       _snake_case     		=				prepare_opt_inputs_dict(lowerCamelCase						,   lowerCamelCase	)
       return config, inputs_dict






     def 	UpperCamelCase(					self						,   lowerCamelCase						,   lowerCamelCase	):
       _snake_case     		=				TFOPTModel(config=lowerCamelCase	)
       _snake_case     		=				inputs_dict["input_ids"]

       _snake_case     		=				input_ids[:1, :]
       _snake_case     		=				inputs_dict["attention_mask"][:1, :]
       _snake_case     		=				1

       # first forward pass
       _snake_case     		=				model(lowerCamelCase						,   attention_mask=lowerCamelCase						,   use_cache=lowerCamelCase	)

       _snake_case		,		_snake_case     		=				outputs.to_tuple()

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

       # append to next input_ids and
       _snake_case     		=				tf.concat([input_ids, next_tokens]						,   axis=-1	)
       _snake_case     		=				tf.concat([attention_mask, next_attn_mask]						,   axis=-1	)

       _snake_case     		=				model(lowerCamelCase						,   attention_mask=lowerCamelCase	)[0]
       _snake_case     		=				model(lowerCamelCase						,   attention_mask=lowerCamelCase						,   past_key_values=lowerCamelCase	)[0]

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

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

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







@require_tf
class       __SCREAMING_SNAKE_CASE	(						__UpperCamelCase					,    __UpperCamelCase					,    unittest.TestCase			):



     '''simple docstring'''






     UpperCAmelCase__       :						int      =   (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
     UpperCAmelCase__       :						Optional[Any]      =   (TFOPTForCausalLM,) if is_tf_available() else ()
     UpperCAmelCase__       :						str      =   (
         {'''feature-extraction''': TFOPTModel, '''text-generation''': TFOPTForCausalLM} if is_tf_available() else {}
     )
     UpperCAmelCase__       :						List[Any]      =   False
     UpperCAmelCase__       :						Optional[Any]      =   False
     UpperCAmelCase__       :						Dict      =   False
     UpperCAmelCase__       :						Optional[int]      =   10

     def 	UpperCamelCase(					self	):
       _snake_case     		=				TFOPTModelTester(self	)
       _snake_case     		=				ConfigTester(self						,   config_class=lowerCamelCase	)

     def 	UpperCamelCase(					self	):
       self.config_tester.run_common_tests()

     def 	UpperCamelCase(					self	):
       _snake_case     		=				self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.check_decoder_model_past_large_inputs(*lowerCamelCase	)






     def 	UpperCamelCase(					self	):
       _snake_case		,		_snake_case     		=				self.model_tester.prepare_config_and_inputs_for_common()

       def _get_word_embedding_weight(lowerCamelCase						,   lowerCamelCase	):
         if hasattr(lowerCamelCase						,   "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(lowerCamelCase						,   "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     		=				model_class(config=lowerCamelCase	)
           _snake_case     		=				_get_word_embedding_weight(lowerCamelCase						,   model.get_input_embeddings()	)
           _snake_case     		=				_get_word_embedding_weight(lowerCamelCase						,   model.get_output_embeddings()	)

           # reshape the embeddings
           model.resize_token_embeddings(lowerCamelCase	)
           _snake_case     		=				_get_word_embedding_weight(lowerCamelCase						,   model.get_input_embeddings()	)
           _snake_case     		=				_get_word_embedding_weight(lowerCamelCase						,   model.get_output_embeddings()	)

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

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

           # check that weights remain the same after resizing
           _snake_case     		=				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     		=				False
           self.assertTrue(lowerCamelCase	)

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

             _snake_case     		=				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     		=				False
             self.assertTrue(lowerCamelCase	)







def 				snake_case_      (		SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  return tf.constant(SCREAMING_SNAKE_CASE__					,	dtype=tf.intaa       )


@require_tf
class       __SCREAMING_SNAKE_CASE	(						unittest.TestCase			):



     '''simple docstring'''






     UpperCAmelCase__       :						Optional[Any]      =   99

     def 	UpperCamelCase(					self	):
       _snake_case     		=				tf.ones((4, 1)						,   dtype=tf.intaa	) * 2
       _snake_case     		=				tf.concat([ids_tensor((4, 6)						,   self.vocab_size - 3	) + 3, eos_column_vector]						,   axis=1	)
       _snake_case     		=				input_ids.shape[0]
       _snake_case     		=				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 	UpperCamelCase(					self	):
       _snake_case     		=				TFOPTModel.from_pretrained("facebook/opt-350m"	)
       _snake_case     		=				_long_tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]	)
       _snake_case     		=				tf.not_equal(lowerCamelCase						,   model.config.pad_token_id	)
       with tf.GradientTape():
         _snake_case     		=				model(input_ids=lowerCamelCase						,   attention_mask=lowerCamelCase	).last_hidden_state
       _snake_case     		=				(1, 11, 512)
       self.assertEqual(output.shape						,   lowerCamelCase	)
       _snake_case     		=				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]						,   lowerCamelCase						,   atol=4e-3	)	)

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







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



     '''simple docstring'''






     def 	UpperCamelCase(					self	):
       super().setUp()
       _snake_case     		=				"facebook/opt-350m"






     def 	UpperCamelCase(					self	):
       _snake_case     		=				TFOPTForCausalLM.from_pretrained(self.path_model	)
       _snake_case     		=				GPTaTokenizer.from_pretrained(self.path_model	)

       _snake_case     		=				[
           "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     		=				tokenizer(lowerCamelCase						,   return_tensors="tf"						,   padding=lowerCamelCase						,   add_special_tokens=lowerCamelCase	)
       _snake_case     		=				tf.math.reduce_mean(model(inputs.input_ids						,   attention_mask=inputs.attention_mask	)[0]						,   axis=-1	)
       _snake_case     		=				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(lowerCamelCase						,   lowerCamelCase						,   atol=1e-4	)	)

       _snake_case     		=				tf.function(lowerCamelCase						,   jit_compile=lowerCamelCase	)
       _snake_case     		=				tf.math.reduce_mean(xla_generate(inputs.input_ids						,   attention_mask=inputs.attention_mask	)[0]						,   axis=-1	)
       self.assertTrue(np.allclose(lowerCamelCase						,   lowerCamelCase						,   atol=1e-4	)	)







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



     '''simple docstring'''






     @property
     def 	UpperCamelCase(					self	):
       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 	UpperCamelCase(					self	):
       _snake_case     		=				"facebook/opt-125m"

       _snake_case     		=				[
           "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     		=				[]
       _snake_case     		=				GPTaTokenizer.from_pretrained(lowerCamelCase	)
       _snake_case     		=				TFOPTForCausalLM.from_pretrained(lowerCamelCase	)

       for prompt in self.prompts:
         _snake_case     		=				tokenizer(lowerCamelCase						,   return_tensors="tf"	).input_ids

         _snake_case     		=				model.generate(lowerCamelCase						,   max_length=10	)

         _snake_case     		=				tokenizer.batch_decode(lowerCamelCase						,   skip_special_tokens=lowerCamelCase	)
         predicted_outputs += generated_string

       self.assertListEqual(lowerCamelCase						,   lowerCamelCase	)

     def 	UpperCamelCase(					self	):
       _snake_case     		=				"facebook/opt-350m"

       _snake_case     		=				GPTaTokenizer.from_pretrained(lowerCamelCase	)
       _snake_case     		=				TFOPTForCausalLM.from_pretrained(lowerCamelCase	)

       _snake_case     		=				"left"

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

       _snake_case     		=				tokenizer(lowerCamelCase						,   return_tensors="tf"						,   padding=lowerCamelCase	)
       _snake_case     		=				inputs["input_ids"]

       _snake_case     		=				model.generate(input_ids=lowerCamelCase						,   attention_mask=inputs["attention_mask"]	)

       _snake_case     		=				tokenizer(sentences[0]						,   return_tensors="tf"	).input_ids
       _snake_case     		=				model.generate(input_ids=lowerCamelCase	)

       _snake_case     		=				inputs_non_padded.shape[-1] - tf.math.reduce_sum(
           tf.cast(inputs["attention_mask"][-1]						,   tf.intaa	)	)
       _snake_case     		=				tokenizer(sentences[1]						,   return_tensors="tf"	).input_ids
       _snake_case     		=				model.generate(input_ids=lowerCamelCase						,   max_length=model.config.max_length - num_paddings	)

       _snake_case     		=				tokenizer.batch_decode(lowerCamelCase						,   skip_special_tokens=lowerCamelCase	)
       _snake_case     		=				tokenizer.decode(output_non_padded[0]						,   skip_special_tokens=lowerCamelCase	)
       _snake_case     		=				tokenizer.decode(output_padded[0]						,   skip_special_tokens=lowerCamelCase	)

       _snake_case     		=				[
           "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(lowerCamelCase						,   lowerCamelCase	)
       self.assertListEqual(lowerCamelCase						,   [non_padded_sentence, padded_sentence]	)






     def 	UpperCamelCase(					self	):
       _snake_case     		=				"facebook/opt-350m"

       _snake_case     		=				[
           "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     		=				[]
       _snake_case     		=				GPTaTokenizer.from_pretrained(lowerCamelCase	)
       _snake_case     		=				TFOPTForCausalLM.from_pretrained(lowerCamelCase	)

       for prompt in self.prompts:
         _snake_case     		=				tokenizer(lowerCamelCase						,   return_tensors="tf"	).input_ids

         _snake_case     		=				model.generate(lowerCamelCase						,   max_length=10	)

         _snake_case     		=				tokenizer.batch_decode(lowerCamelCase						,   skip_special_tokens=lowerCamelCase	)
         predicted_outputs += generated_string

       self.assertListEqual(lowerCamelCase						,   lowerCamelCase	)









'''simple docstring'''



def 				snake_case_      (		SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  _snake_case     		=				[0 for i in range(len(SCREAMING_SNAKE_CASE__       )       )]

  # initialize interval's left pointer and right pointer
  _snake_case		,		_snake_case     		=				0, 0

  for i in range(1					,	len(SCREAMING_SNAKE_CASE__       )       ):
    # case when current index is inside the interval
    if i <= right_pointer:
      _snake_case     		=				min(right_pointer - i + 1					,	z_result[i - left_pointer]       )
      _snake_case     		=				min_edge

    while go_next(SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__       ):
      z_result[i] += 1

    # if new index's result gives us more right interval,
    # we've to update left_pointer and right_pointer
    if i + z_result[i] - 1 > right_pointer:
      _snake_case		,		_snake_case     		=				i, i + z_result[i] - 1

  return z_result




def 				snake_case_      (		SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  return i + z_result[i] < len(SCREAMING_SNAKE_CASE__       ) and s[z_result[i]] == s[i + z_result[i]]




def 				snake_case_      (		SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  _snake_case     		=				0
  # concatenate 'pattern' and 'input_str' and call z_function
  # with concatenated string
  _snake_case     		=				z_function(pattern + input_str       )

  for val in z_result:
    # if value is greater then length of the pattern string
    # that means this index is starting position of substring
    # which is equal to pattern string
    if val >= len(SCREAMING_SNAKE_CASE__       ):
      answer += 1

  return answer


if __name__ == "__main__":
 import doctest

 doctest.testmod()


