

from __future__ import annotations

import os
import tempfile
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import is_tensorflow_text_available, is_tf_available
from transformers.testing_utils import require_tensorflow_text, require_tf, slow

from ..test_modeling_tf_common import floats_tensor
from .test_framework_agnostic import GenerationIntegrationTestsMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import (
        AutoTokenizer,
        TFAutoModelForCausalLM,
        TFAutoModelForSeqaSeqLM,
        TFAutoModelForSpeechSeqaSeq,
        TFAutoModelForVisionaSeq,
        TFBartForConditionalGeneration,
        TFLogitsProcessorList,
        TFMinLengthLogitsProcessor,
        tf_top_k_top_p_filtering,
    )

if is_tensorflow_text_available():
    import tensorflow_text as text
@require_tf
class  _A		(  unittest.TestCase):

      def  UpperCAmelCase    (				self					):





            """simple docstring"""







            SCREAMING_SNAKE_CASE_				:  Optional[int]           =     tf.convert_to_tensor(
                [
                    [
                        8.2220991,  # 3rd highest value; idx. 0
                        -0.5620044,
                        5.23229752,
                        4.0386393,
                        -6.8798378,
                        -0.54785802,
                        -3.2012153,
                        2.92777176,
                        1.88171953,
                        7.35341276,  # 5th highest value; idx. 9
                        8.43207833,  # 2nd highest value; idx. 10
                        -9.85711836,
                        -5.96209236,
                        -1.13039161,
                        -7.1115294,
                        -0.8369633,
                        -5.3186408,
                        7.06427407,
                        0.81369344,
                        -0.82023817,
                        -5.9179796,
                        0.58813443,
                        -6.99778438,
                        4.71551189,
                        -0.18771637,
                        7.44020759,  # 4th highest value; idx. 25
                        9.38450987,  # 1st highest value; idx. 26
                        2.12662941,
                        -9.32562038,
                        2.35652522,
                    ],  # cummulative prob of 5 highest values <= 0.6
                    [
                        0.58425518,
                        4.53139238,
                        -5.57510464,
                        -6.28030699,
                        -7.19529503,
                        -4.02122551,
                        1.39337037,
                        -6.06707057,
                        1.59480517,
                        -9.643119,
                        0.03907799,
                        0.67231762,
                        -8.88206726,
                        6.27115922,  # 4th highest value; idx. 13
                        2.28520723,
                        4.82767506,
                        4.30421368,
                        8.8275313,  # 2nd highest value; idx. 17
                        5.44029958,  # 5th highest value; idx. 18
                        -4.4735794,
                        7.38579536,  # 3rd highest value; idx. 20
                        -2.91051663,
                        2.61946077,
                        -2.5674762,
                        -9.48959302,
                        -4.02922645,
                        -1.35416918,
                        9.67702323,  # 1st highest value; idx. 27
                        -5.89478553,
                        1.85370467,
                    ],  # cummulative prob of 5 highest values <= 0.6
                ] ,					dtype=tf.floataa ,					)

            SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     tf.convert_to_tensor(
                [[0, 0], [0, 9], [0, 10], [0, 25], [0, 26], [1, 13], [1, 17], [1, 18], [1, 20], [1, 27]] ,					dtype=tf.intaa ,					)  # expected non filtered idx as noted above

            SCREAMING_SNAKE_CASE_				:  List[Any]           =     tf.convert_to_tensor(
                [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023] ,					dtype=tf.floataa ,					)  # expected non filtered values as noted above

            SCREAMING_SNAKE_CASE_				:  Any           =     tf_top_k_top_p_filtering(__A ,					top_k=10 ,					top_p=0.6 ,					min_tokens_to_keep=4					)

            SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     output[output != -float('inf'					)]
            SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     tf.cast(
                tf.where(tf.not_equal(__A ,					tf.constant(-float('inf'					) ,					dtype=tf.floataa					)					)					) ,					dtype=tf.intaa ,					)

            tf.debugging.assert_near(__A ,					__A ,					rtol=1e-12					)
            tf.debugging.assert_equal(__A ,					__A					)

@require_tf
class  _A		(  unittest.TestCase   ,     UpperCamelCase_):






      if is_tf_available():
            SCREAMING_SNAKE_CASE    :      Tuple       =  {
                '''AutoModelForCausalLM''': TFAutoModelForCausalLM,
                '''AutoModelForSpeechSeq2Seq''': TFAutoModelForSpeechSeqaSeq,
                '''AutoModelForSeq2SeqLM''': TFAutoModelForSeqaSeqLM,
                '''AutoModelForVision2Seq''': TFAutoModelForVisionaSeq,
                '''LogitsProcessorList''': TFLogitsProcessorList,
                '''MinLengthLogitsProcessor''': TFMinLengthLogitsProcessor,
                '''create_tensor_fn''': tf.convert_to_tensor,
                '''floats_tensor''': floats_tensor,
                '''return_tensors''': '''tf''',
            }

      @slow
      def  UpperCAmelCase    (				self					):





            """simple docstring"""







            SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2'					)
            SCREAMING_SNAKE_CASE_				:  Any           =     2
            SCREAMING_SNAKE_CASE_				:  List[Any]           =     2
            class  _A		(  tf.Module):

                  def __init__(				self ,					_SCREAMING_SNAKE_CASE					):





                        """simple docstring"""







                        super(__A ,					self					).__init__()
                        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     model



                  @tf.function(
                      input_signature=(
                          tf.TensorSpec((None, input_length) ,					tf.intaa ,					name='input_ids'					),
                          tf.TensorSpec((None, input_length) ,					tf.intaa ,					name='attention_mask'					),
                      ) ,					jit_compile=__A ,					)
                  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





                        """simple docstring"""







                        SCREAMING_SNAKE_CASE_				:  List[str]           =     self.model.generate(
                            input_ids=__A ,					attention_mask=__A ,					max_new_tokens=__A ,					return_dict_in_generate=__A ,					)
                        return {"sequences": outputs["sequences"]}

            SCREAMING_SNAKE_CASE_				:  int           =     [[2, 0], [102, 103]]
            SCREAMING_SNAKE_CASE_				:  Tuple           =     [[1, 0], [1, 1]]
            SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     DummyModel(model=__A					)
            with tempfile.TemporaryDirectory() as tmp_dir:
                  tf.saved_model.save(__A ,					__A ,					signatures={'serving_default': dummy_model.serving}					)
                  SCREAMING_SNAKE_CASE_				:  Tuple           =     tf.saved_model.load(__A					).signatures["serving_default"]
                  for batch_size in range(1 ,					len(__A					) + 1					):
                        SCREAMING_SNAKE_CASE_				:  Any           =     {
                            "input_ids": tf.constant(dummy_input_ids[:batch_size]					),
                            "attention_mask": tf.constant(dummy_attention_masks[:batch_size]					),
                        }
                        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     serving_func(**__A					)["sequences"]
                        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     test_model.generate(**__A ,					max_new_tokens=__A					)
                        tf.debugging.assert_equal(__A ,					__A					)

      @slow
      def  UpperCAmelCase    (				self					):





            """simple docstring"""







            SCREAMING_SNAKE_CASE_				:  Optional[int]           =     TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2'					)
            SCREAMING_SNAKE_CASE_				:  Any           =     1
            SCREAMING_SNAKE_CASE_				:  Optional[int]           =     2
            class  _A		(  tf.Module):

                  def __init__(				self ,					_SCREAMING_SNAKE_CASE					):





                        """simple docstring"""







                        super(__A ,					self					).__init__()
                        SCREAMING_SNAKE_CASE_				:  Any           =     model



                  @tf.function(
                      input_signature=(
                          tf.TensorSpec((batch_size, None) ,					tf.intaa ,					name='input_ids'					),
                          tf.TensorSpec((batch_size, None) ,					tf.intaa ,					name='attention_mask'					),
                      ) ,					jit_compile=__A ,					)
                  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





                        """simple docstring"""







                        SCREAMING_SNAKE_CASE_				:  int           =     self.model.generate(
                            input_ids=__A ,					attention_mask=__A ,					max_new_tokens=__A ,					return_dict_in_generate=__A ,					)
                        return {"sequences": outputs["sequences"]}

            SCREAMING_SNAKE_CASE_				:  Tuple           =     [[2], [102, 103]]
            SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     [[1], [1, 1]]
            SCREAMING_SNAKE_CASE_				:  Tuple           =     DummyModel(model=__A					)
            with tempfile.TemporaryDirectory() as tmp_dir:
                  tf.saved_model.save(__A ,					__A ,					signatures={'serving_default': dummy_model.serving}					)
                  SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     tf.saved_model.load(__A					).signatures["serving_default"]
                  for input_row in range(len(__A					)					):
                        SCREAMING_SNAKE_CASE_				:  Dict           =     {
                            "input_ids": tf.constant([dummy_input_ids[input_row]]					),
                            "attention_mask": tf.constant([dummy_attention_masks[input_row]]					),
                        }
                        SCREAMING_SNAKE_CASE_				:  int           =     serving_func(**__A					)["sequences"]
                        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     test_model.generate(**__A ,					max_new_tokens=__A					)
                        tf.debugging.assert_equal(__A ,					__A					)

      @slow
      @require_tensorflow_text
      def  UpperCAmelCase    (				self					):





            """simple docstring"""







            with tempfile.TemporaryDirectory() as tmp_dir:
                  # file needed to load the TF tokenizer
                  hf_hub_download(repo_id='google/flan-t5-small' ,					filename='spiece.model' ,					local_dir=__A					)
                  class  _A		(  tf.keras.layers.Layer):

                        def __init__(				self					):





                              """simple docstring"""







                              super().__init__()
                              SCREAMING_SNAKE_CASE_				:  Any           =     text.SentencepieceTokenizer(
                                  model=tf.io.gfile.GFile(os.path.join(__A ,					'spiece.model'					) ,					'rb'					).read()					)
                              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     TFAutoModelForSeqaSeqLM.from_pretrained('hf-internal-testing/tiny-random-t5'					)



                        def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					*_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





                              """simple docstring"""







                              SCREAMING_SNAKE_CASE_				:  Dict           =     self.tokenizer.tokenize(__A					)
                              SCREAMING_SNAKE_CASE_				:  List[Any]           =     text.pad_model_inputs(
                                  __A ,					max_seq_length=64 ,					pad_value=self.model.config.pad_token_id					)
                              SCREAMING_SNAKE_CASE_				:  str           =     self.model.generate(input_ids=__A ,					attention_mask=__A					)
                              return self.tokenizer.detokenize(__A					)

                  SCREAMING_SNAKE_CASE_				:  Any           =     CompleteSentenceTransformer()
                  SCREAMING_SNAKE_CASE_				:  str           =     tf.keras.layers.Input(shape=(1,) ,					dtype=tf.string ,					name='inputs'					)
                  SCREAMING_SNAKE_CASE_				:  str           =     complete_model(__A					)
                  SCREAMING_SNAKE_CASE_				:  Optional[int]           =     tf.keras.Model(__A ,					__A					)
                  keras_model.save(__A					)

      def  UpperCAmelCase    (				self					):





            """simple docstring"""







            SCREAMING_SNAKE_CASE_				:  Any           =     {
                "do_sample": True,
                "num_beams": 1,
                "top_p": 0.7,
                "top_k": 10,
                "temperature": 0.7,
            }
            SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     14

            SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2'					)
            SCREAMING_SNAKE_CASE_				:  Tuple           =     "Hello, my dog is cute and"
            SCREAMING_SNAKE_CASE_				:  str           =     tokenizer(__A ,					return_tensors='tf'					)
            SCREAMING_SNAKE_CASE_				:  str           =     TFAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2'					)

            SCREAMING_SNAKE_CASE_				:  Any           =     638
            # forces the generation to happen on CPU, to avoid GPU-related quirks
            with tf.device(':/CPU:0'					):
                  tf.random.set_seed(0					)
                  SCREAMING_SNAKE_CASE_				:  int           =     model.generate(**__A ,					eos_token_id=__A ,					**__A					)
            self.assertTrue(expectation == len(generated_tokens[0]					)					)

            SCREAMING_SNAKE_CASE_				:  Optional[int]           =     [638, 198]
            with tf.device(':/CPU:0'					):
                  tf.random.set_seed(0					)
                  SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     model.generate(**__A ,					eos_token_id=__A ,					**__A					)
            self.assertTrue(expectation == len(generated_tokens[0]					)					)



      def  UpperCAmelCase    (				self					):





            """simple docstring"""







            SCREAMING_SNAKE_CASE_				:  Any           =     AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-bart'					)
            SCREAMING_SNAKE_CASE_				:  Optional[int]           =     "Hugging Face is a technology company based in New York and Paris."
            SCREAMING_SNAKE_CASE_				:  List[str]           =     bart_tokenizer(__A ,					return_tensors='tf'					).input_ids
            SCREAMING_SNAKE_CASE_				:  Any           =     TFBartForConditionalGeneration.from_pretrained('hf-internal-testing/tiny-random-bart'					)
            SCREAMING_SNAKE_CASE_				:  str           =     bart_model.generate(__A					).numpy()
            class  _A		(  UpperCamelCase_):

                  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE=None ,					**_SCREAMING_SNAKE_CASE					):





                        """simple docstring"""







                        return super().call(__A ,					**__A					)

            SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     FakeBart.from_pretrained('hf-internal-testing/tiny-random-bart'					)
            SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     bart_model.generate(__A ,					foo='bar'					).numpy()
            self.assertTrue(np.array_equal(__A ,					__A					)					)
            class  _A		(  bart_model.model.encoder.__class__):

                  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





                        """simple docstring"""







                        return super().call(__A ,					**__A					)

            SCREAMING_SNAKE_CASE_				:  str           =     FakeEncoder(bart_model.config ,					bart_model.model.shared					)
            SCREAMING_SNAKE_CASE_				:  List[Any]           =     fake_encoder

            # Normal generation still works (the output will be different because the encoder weights are different)
            SCREAMING_SNAKE_CASE_				:  str           =     bart_model.generate(__A					).numpy()
            with self.assertRaises(__A					):
                  # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo"
                  bart_model.generate(__A ,					foo='bar'					)









def    A_  (							a					,   a					,   a  ):

      """simple docstring"""



      if len(a  ) != len(a  ):
            raise ValueError('The length of profit and weight must be same.'  )
      if max_weight <= 0:
            raise ValueError('max_weight must greater than zero.'  )
      if any(p < 0 for p in profit  ):
            raise ValueError('Profit can not be negative.'  )
      if any(w < 0 for w in weight  ):
            raise ValueError('Weight can not be negative.'  )

      # List created to store profit gained for the 1kg in case of each weight
      # respectively.  Calculate and append profit/weight for each element.
      SCREAMING_SNAKE_CASE_				:  Tuple           =     [p / w for p, w in zip(a					,   a  )]

      # Creating a copy of the list and sorting profit/weight in ascending order
      SCREAMING_SNAKE_CASE_				:  List[Any]           =     sorted(a  )

      # declaring useful variables
      SCREAMING_SNAKE_CASE_				:  List[Any]           =     len(a  )
      SCREAMING_SNAKE_CASE_				:  str           =     0
      SCREAMING_SNAKE_CASE_				:  List[str]           =     0
      SCREAMING_SNAKE_CASE_				:  Any           =     0

      # loop till the total weight do not reach max limit e.g. 15 kg and till i<length
      while limit <= max_weight and i < length:
            # flag value for encountered greatest element in sorted_profit_by_weight
            SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     sorted_profit_by_weight[length - i - 1]
            SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     profit_by_weight.index(a  )
            SCREAMING_SNAKE_CASE_				:  Dict           =     -1

            # check if the weight encountered is less than the total weight
            # encountered before.
            if max_weight - limit >= weight[index]:
                  limit += weight[index]
                  # Adding profit gained for the given weight 1 ===
                  # weight[index]/weight[index]
                  gain += 1 * profit[index]
            else:
                  # Since the weight encountered is greater than limit, therefore take the
                  # required number of remaining kgs and calculate profit for it.
                  # weight remaining / weight[index]
                  gain += (max_weight - limit) / weight[index] * profit[index]
                  break
            i += 1
      return gain


if __name__ == "__main__":
    print(
        'Input profits, weights, and then max_weight (all positive ints) separated by '
        'spaces.'
    )

    lowerCAmelCase     :   Tuple						     =  [int(x) for x in input('Input profits separated by spaces: ').split()]
    lowerCAmelCase     :   Union[str, Any]						     =  [int(x) for x in input('Input weights separated by spaces: ').split()]
    lowerCAmelCase     :   Dict						     =  int(input('Max weight allowed: '))

    # Function Call
    calc_profit(profit, weight, max_weight)






