



'''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  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__=None       ,	lowercase__=None       ):


     '''simple docstring'''







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







@require_tf
class 	UpperCAmelCase	:


 '''simple docstring'''




 __magic_name__					: Tuple  =      OPTConfig
 __magic_name__					: Any  =      {}
 __magic_name__					: Tuple  =      "gelu"




 def __init__(   self							,  lowerCAmelCase_							,  lowerCAmelCase_=1_3							,  lowerCAmelCase_=7							,  lowerCAmelCase_=True							,  lowerCAmelCase_=False							,  lowerCAmelCase_=9_9							,  lowerCAmelCase_=1_6							,  lowerCAmelCase_=2							,  lowerCAmelCase_=4							,  lowerCAmelCase_=4							,  lowerCAmelCase_="gelu"							,  lowerCAmelCase_=0.1							,  lowerCAmelCase_=0.1							,  lowerCAmelCase_=2_0							,  lowerCAmelCase_=2							,  lowerCAmelCase_=1							,  lowerCAmelCase_=0							,  lowerCAmelCase_=1_6							,  lowerCAmelCase_=1_6							,  ) ->      Optional[int]:



      """simple docstring"""







      a_       		=parent
      a_       		=batch_size
      a_       		=seq_length
      a_       		=is_training
      a_       		=use_labels
      a_       		=vocab_size
      a_       		=hidden_size
      a_       		=num_hidden_layers
      a_       		=num_attention_heads
      a_       		=intermediate_size
      a_       		=hidden_act
      a_       		=hidden_dropout_prob
      a_       		=attention_probs_dropout_prob
      a_       		=max_position_embeddings
      a_       		=eos_token_id
      a_       		=pad_token_id
      a_       		=bos_token_id
      a_       		=embed_dim
      a_       		=word_embed_proj_dim
      a_       		=False




 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







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

      a_       		=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							,  )
      a_       		=prepare_opt_inputs_dict(lowerCAmelCase_							,  lowerCAmelCase_)
      return config, inputs_dict




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      Dict:



      """simple docstring"""







      a_       		=TFOPTModel(config=lowerCAmelCase_)
      a_       		=inputs_dict["input_ids"]

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

      # first forward pass
      a_       		=model(lowerCAmelCase_							,  attention_mask=lowerCAmelCase_							,  use_cache=lowerCAmelCase_)

      a_		,						a_       		=outputs.to_tuple()

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

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

      a_       		=model(lowerCAmelCase_							,  attention_mask=lowerCAmelCase_)[0]
      a_       		=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
      a_       		=int(ids_tensor((1,)							,  output_from_past.shape[-1]))
      a_       		=output_from_no_past[:, -3:, random_slice_idx]
      a_       		=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 	UpperCAmelCase	(		__a					,	__a					,	unittest.TestCase):


 '''simple docstring'''




 __magic_name__					: Tuple  =      (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
 __magic_name__					: Dict  =      (TFOPTForCausalLM,) if is_tf_available() else ()
 __magic_name__					: Tuple  =      (
     {"feature-extraction": TFOPTModel, "text-generation": TFOPTForCausalLM} if is_tf_available() else {}
 )
 __magic_name__					: List[Any]  =      False
 __magic_name__					: List[str]  =      False
 __magic_name__					: Dict  =      False
 __magic_name__					: Tuple  =      10




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      a_       		=TFOPTModelTester(self)
      a_       		=ConfigTester(self							,  config_class=lowerCAmelCase_)




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      self.config_tester.run_common_tests()




 def       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs_for_common()
      self.model_tester.check_decoder_model_past_large_inputs(*lowerCAmelCase_)




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



      """simple docstring"""







      a_		,						a_       		=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 - 1_0, config.vocab_size + 1_0]:
                # build the embeddings
                a_       		=model_class(config=lowerCAmelCase_)
                a_       		=_get_word_embedding_weight(lowerCAmelCase_							,  model.get_input_embeddings())
                a_       		=_get_word_embedding_weight(lowerCAmelCase_							,  model.get_output_embeddings())

                # reshape the embeddings
                model.resize_token_embeddings(lowerCAmelCase_)
                a_       		=_get_word_embedding_weight(lowerCAmelCase_							,  model.get_input_embeddings())
                a_       		=_get_word_embedding_weight(lowerCAmelCase_							,  model.get_output_embeddings())

                # check that the resized embeddings size matches the desired size.
                a_       		=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
                a_       		=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_       		=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_)

                     a_       		=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_       		=False
                     self.assertTrue(lowerCAmelCase_)






def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







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







@require_tf
class 	UpperCAmelCase	(		unittest.TestCase):


 '''simple docstring'''




 __magic_name__					: Optional[Any]  =      99




 def       lowercase_  (   self) ->      Optional[int]:



      """simple docstring"""







      a_       		=tf.ones((4, 1)							,  dtype=tf.intaa) * 2
      a_       		=tf.concat([ids_tensor((4, 6)							,  self.vocab_size - 3) + 3, eos_column_vector]							,  axis=1)
      a_       		=input_ids.shape[0]
      a_       		=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):


 '''simple docstring'''




 @slow
 def       lowercase_  (   self) ->      str:



      """simple docstring"""







      a_       		=TFOPTModel.from_pretrained("facebook/opt-350m")
      a_       		=_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_       		=tf.not_equal(lowerCAmelCase_							,  model.config.pad_token_id)
      with tf.GradientTape():
           a_       		=model(input_ids=lowerCAmelCase_							,  attention_mask=lowerCAmelCase_).last_hidden_state
      a_       		=(1, 1_1, 5_1_2)
      self.assertEqual(output.shape							,  lowerCAmelCase_)
      a_       		=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]							,  lowerCAmelCase_							,  atol=4e-3))

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







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


 '''simple docstring'''




 def       lowercase_  (   self) ->      Optional[Any]:



      """simple docstring"""







      super().setUp()
      a_       		="facebook/opt-350m"




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



      """simple docstring"""







      a_       		=TFOPTForCausalLM.from_pretrained(self.path_model)
      a_       		=GPTaTokenizer.from_pretrained(self.path_model)

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

      a_       		=tf.function(lowerCAmelCase_							,  jit_compile=lowerCAmelCase_)
      a_       		=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 	UpperCAmelCase	(		unittest.TestCase):


 '''simple docstring'''




 @property
 def       lowercase_  (   self) ->      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       lowercase_  (   self) ->      Any:



      """simple docstring"""







      a_       		="facebook/opt-125m"

      a_       		=[
          "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_       		=[]
      a_       		=GPTaTokenizer.from_pretrained(lowerCAmelCase_)
      a_       		=TFOPTForCausalLM.from_pretrained(lowerCAmelCase_)

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

           a_       		=model.generate(lowerCAmelCase_							,  max_length=1_0)

           a_       		=tokenizer.batch_decode(lowerCAmelCase_							,  skip_special_tokens=lowerCAmelCase_)
           predicted_outputs += generated_string

      self.assertListEqual(lowerCAmelCase_							,  lowerCAmelCase_)




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



      """simple docstring"""







      a_       		="facebook/opt-350m"

      a_       		=GPTaTokenizer.from_pretrained(lowerCAmelCase_)
      a_       		=TFOPTForCausalLM.from_pretrained(lowerCAmelCase_)

      a_       		="left"

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

      a_       		=tokenizer(lowerCAmelCase_							,  return_tensors="tf"							,  padding=lowerCAmelCase_)
      a_       		=inputs["input_ids"]

      a_       		=model.generate(input_ids=lowerCAmelCase_							,  attention_mask=inputs["attention_mask"])

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

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

      a_       		=tokenizer.batch_decode(lowerCAmelCase_							,  skip_special_tokens=lowerCAmelCase_)
      a_       		=tokenizer.decode(output_non_padded[0]							,  skip_special_tokens=lowerCAmelCase_)
      a_       		=tokenizer.decode(output_padded[0]							,  skip_special_tokens=lowerCAmelCase_)

      a_       		=[
          "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       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







      a_       		="facebook/opt-350m"

      a_       		=[
          "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_       		=[]
      a_       		=GPTaTokenizer.from_pretrained(lowerCAmelCase_)
      a_       		=TFOPTForCausalLM.from_pretrained(lowerCAmelCase_)

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

           a_       		=model.generate(lowerCAmelCase_							,  max_length=1_0)

           a_       		=tokenizer.batch_decode(lowerCAmelCase_							,  skip_special_tokens=lowerCAmelCase_)
           predicted_outputs += generated_string

      self.assertListEqual(lowerCAmelCase_							,  lowerCAmelCase_)







'''simple docstring'''


from collections.abc import Sequence






def  UpperCAmelCase_       (		lowercase__ = None       ):


     '''simple docstring'''







     if nums is None or not nums:
          raise ValueError("Input sequence should not be empty"       )

     a_       		=nums[0]
     for i in range(1       ,	len(lowercase__       )       ):
          a_       		=nums[i]
          a_       		=max(lowercase__       ,	ans + num       ,	lowercase__       )

     return ans


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  # Try on a sample input from the user
  lowercase							       =			int(input('''Enter number of elements : ''').strip())
  lowercase							       =			list(map(int, input('''\nEnter the numbers : ''').strip().split()))[:n]
  print(max_subsequence_sum(array))


