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  __magic_name__  (		unittest.TestCase    ):





   """simple docstring"""




   def   lowerCAmelCase						(	self					:  Optional[Any]							):



    """simple docstring"""



    _UpperCamelCase:     Tuple							= 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		,  )

    _UpperCamelCase:     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

    _UpperCamelCase:     Union[str, 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

    _UpperCamelCase:     Tuple							= tf_top_k_top_p_filtering(_lowercase		,  top_k=10		,  top_p=0.6		,  min_tokens_to_keep=4							)

    _UpperCamelCase:     Union[str, Any]							= output[output != -float('''inf'''							)]
    _UpperCamelCase:     Any							= tf.cast(
        tf.where(tf.not_equal(_lowercase		,  tf.constant(-float('''inf'''							)		,  dtype=tf.floataa							)							)							)		,  dtype=tf.intaa		,  )

    tf.debugging.assert_near(_lowercase		,  _lowercase		,  rtol=1E-12							)
    tf.debugging.assert_equal(_lowercase		,  _lowercase							)




@require_tf
class  __magic_name__  (		unittest.TestCase      ,	__a    ):





   """simple docstring"""




   if is_tf_available():
      lowerCAmelCase      :    Optional[int]  			=   {
          '''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   lowerCAmelCase						(	self					:  List[str]							):



    """simple docstring"""



    _UpperCamelCase:     Tuple							= TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''							)
    _UpperCamelCase:     Tuple							= 2
    _UpperCamelCase:     List[Any]							= 2




    class  __magic_name__  (		tf.Module    ):





       """simple docstring"""




       def __init__(	self					:  int		,  _lowercase					:  Dict							):



        """simple docstring"""



        super(_lowercase		,  self							).__init__()
        _UpperCamelCase:     Optional[int]							= 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=_lowercase		,  )
       def   lowerCAmelCase						(	self					:  Union[str, Any]		,  _lowercase					:  str		,  _lowercase					:  List[str]							):



        """simple docstring"""



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




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



   @slow
   def   lowerCAmelCase						(	self					:  Any							):



    """simple docstring"""



    _UpperCamelCase:     Optional[int]							= TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''							)
    _UpperCamelCase:     Dict							= 1
    _UpperCamelCase:     Any							= 2




    class  __magic_name__  (		tf.Module    ):





       """simple docstring"""




       def __init__(	self					:  str		,  _lowercase					:  Union[str, Any]							):



        """simple docstring"""



        super(_lowercase		,  self							).__init__()
        _UpperCamelCase:     Union[str, 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=_lowercase		,  )
       def   lowerCAmelCase						(	self					:  Union[str, Any]		,  _lowercase					:  List[str]		,  _lowercase					:  Optional[int]							):



        """simple docstring"""



        _UpperCamelCase:     List[Any]							= self.model.generate(
            input_ids=_lowercase		,  attention_mask=_lowercase		,  max_new_tokens=_lowercase		,  return_dict_in_generate=_lowercase		,  )
        return {"sequences": outputs["sequences"]}




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



   @slow
   @require_tensorflow_text
   def   lowerCAmelCase						(	self					:  int							):



    """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=_lowercase							)




     class  __magic_name__  (		tf.keras.layers.Layer    ):





        """simple docstring"""




        def __init__(	self					:  int							):



         """simple docstring"""



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



        def   lowerCAmelCase						(	self					:  List[str]		,  _lowercase					:  Tuple		,  *_lowercase					:  Dict		,  **_lowercase					:  List[Any]							):



         """simple docstring"""



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




     _UpperCamelCase:     Tuple							= CompleteSentenceTransformer()
     _UpperCamelCase:     Any							= tf.keras.layers.Input(shape=(1,)		,  dtype=tf.string		,  name='''inputs'''							)
     _UpperCamelCase:     str							= complete_model(_lowercase							)
     _UpperCamelCase:     Union[str, Any]							= tf.keras.Model(_lowercase		,  _lowercase							)
     keras_model.save(_lowercase							)



   def   lowerCAmelCase						(	self					:  Tuple							):



    """simple docstring"""



    _UpperCamelCase:     Tuple							= {
        '''do_sample''': True,
        '''num_beams''': 1,
        '''top_p''': 0.7,
        '''top_k''': 10,
        '''temperature''': 0.7,
    }
    _UpperCamelCase:     int							= 14

    _UpperCamelCase:     Optional[int]							= AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''							)
    _UpperCamelCase:     Dict							= '''Hello, my dog is cute and'''
    _UpperCamelCase:     Any							= tokenizer(_lowercase		,  return_tensors='''tf'''							)
    _UpperCamelCase:     int							= TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''							)

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

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



   def   lowerCAmelCase						(	self					:  int							):



    """simple docstring"""



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




    class  __magic_name__  (		__a    ):





       """simple docstring"""




       def   lowerCAmelCase						(	self					:  Any		,  _lowercase					:  Union[str, Any]		,  _lowercase					:  str=None		,  **_lowercase					:  Union[str, Any]							):



        """simple docstring"""



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




    _UpperCamelCase:     List[str]							= FakeBart.from_pretrained('''hf-internal-testing/tiny-random-bart'''							)
    _UpperCamelCase:     Optional[int]							= bart_model.generate(_lowercase		,  foo='''bar'''							).numpy()
    self.assertTrue(np.array_equal(_lowercase		,  _lowercase							)							)




    class  __magic_name__  (		bart_model.model.encoder.__class__    ):





       """simple docstring"""




       def   lowerCAmelCase						(	self					:  List[str]		,  _lowercase					:  Optional[int]		,  **_lowercase					:  Optional[int]							):



        """simple docstring"""



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




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

    # Normal generation still works (the output will be different because the encoder weights are different)
    _UpperCamelCase:     List[Any]							= bart_model.generate(_lowercase							).numpy()
    with self.assertRaises(_lowercase							):
     # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo"
     bart_model.generate(_lowercase		,  foo='''bar'''							)
import numpy as np
from numpy import ndarray
from scipy.optimize import Bounds, LinearConstraint, minimize




def       lowerCAmelCase_     (		lowercase:							ndarray		)    ->				float:







 '''simple docstring'''
 return np.dot(lowercase  ,					lowercase		)




class  __magic_name__  :





   """simple docstring"""




   def __init__(	self					:  int		,  *,
       _lowercase					:  float = np.inf		,  _lowercase					:  str = "linear"		,  _lowercase					:  float = 0.0		,  ):



    """simple docstring"""



    _UpperCamelCase:     int							= regularization
    _UpperCamelCase:     Optional[int]							= gamma
    if kernel == "linear":
     _UpperCamelCase:     Optional[Any]							= self.__linear
    elif kernel == "rbf":
     if self.gamma == 0:
      raise ValueError('''rbf kernel requires gamma'''							)
     if not isinstance(self.gamma		,  (float, int)							):
      raise ValueError('''gamma must be float or int'''							)
     if not self.gamma > 0:
      raise ValueError('''gamma must be > 0'''							)
     _UpperCamelCase:     Optional[Any]							= self.__rbf
     # in the future, there could be a default value like in sklearn
     # sklear: def_gamma = 1/(n_features * X.var()) (wiki)
     # previously it was 1/(n_features)
    else:
     _UpperCamelCase:     Optional[int]							= f"""Unknown kernel: {kernel}"""
     raise ValueError(_lowercase							)



   def   lowerCAmelCase						(	self					:  Union[str, Any]		,  _lowercase					:  ndarray		,  _lowercase					:  ndarray							):



    """simple docstring"""



    return np.dot(_lowercase		,  _lowercase							)



   def   lowerCAmelCase						(	self					:  str		,  _lowercase					:  ndarray		,  _lowercase					:  ndarray							):



    """simple docstring"""



    return np.exp(-(self.gamma * norm_squared(vectora - vectora							))							)



   def   lowerCAmelCase						(	self					:  str		,  _lowercase					:  list[ndarray]		,  _lowercase					:  ndarray							):



    """simple docstring"""



    _UpperCamelCase:     List[str]							= observations
    _UpperCamelCase:     Optional[int]							= classes

    # using Wolfe's Dual to calculate w.
    # Primal problem: minimize 1/2*norm_squared(w)
    #   constraint: yn(w . xn + b) >= 1
    #
    # With l a vector
    # Dual problem: maximize sum_n(ln) -
    #       1/2 * sum_n(sum_m(ln*lm*yn*ym*xn . xm))
    #   constraint: self.C >= ln >= 0
    #           and sum_n(ln*yn) = 0
    # Then we get w using w = sum_n(ln*yn*xn)
    # At the end we can get b ~= mean(yn - w . xn)
    #
    # Since we use kernels, we only need l_star to calculate b
    # and to classify observations

    ((_UpperCamelCase)	,							):     Any							= np.shape(_lowercase							)

    def to_minimize(_lowercase					:  ndarray							) -> float:
     _UpperCamelCase:     Optional[int]							= 0
     ((_UpperCamelCase)	,							):     str							= np.shape(_lowercase							)
     for i in range(_lowercase							):
      for j in range(_lowercase							):
       s += (
           candidate[i]
           * candidate[j]
           * classes[i]
           * classes[j]
           * self.kernel(observations[i]		,  observations[j]							)
       )
     return 1 / 2 * s - sum(_lowercase							)

    _UpperCamelCase:     Optional[Any]							= LinearConstraint(_lowercase		,  0		,  0							)
    _UpperCamelCase:     Optional[int]							= Bounds(0		,  self.regularization							)

    _UpperCamelCase:     Dict							= minimize(
        _lowercase		,  np.ones(_lowercase							)		,  bounds=_lowercase		,  constraints=[ly_contraint]							).x
    _UpperCamelCase:     Union[str, Any]							= l_star

    # calculating mean offset of separation plane to points
    _UpperCamelCase:     List[str]							= 0
    for i in range(_lowercase							):
     for j in range(_lowercase							):
      s += classes[i] - classes[i] * self.optimum[i] * self.kernel(
          observations[i]		,  observations[j]							)
    _UpperCamelCase:     str							= s / n



   def   lowerCAmelCase						(	self					:  Optional[Any]		,  _lowercase					:  ndarray							):



    """simple docstring"""



    _UpperCamelCase:     Optional[int]							= sum(
        self.optimum[n]
        * self.classes[n]
        * self.kernel(self.observations[n]		,  _lowercase							)
        for n in range(len(self.classes							)							)							)
    return 1 if s + self.offset >= 0 else -1


if __name__ == "__main__":
  import doctest

  doctest.testmod()