




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

    '''simple docstring'''



    def     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      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     ,  )

           lowerCamelCase   :      List[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

           lowerCamelCase   :      str			 =					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

           lowerCamelCase   :      Any			 =					tf_top_k_top_p_filtering(UpperCAmelCase_     ,  top_k=10     ,  top_p=0.6     ,  min_tokens_to_keep=4     )

           lowerCamelCase   :      List[str]			 =					output[output != -float("inf"     )]
           lowerCamelCase   :      List[Any]			 =					tf.cast(
               tf.where(tf.not_equal(UpperCAmelCase_     ,  tf.constant(-float("inf"     )     ,  dtype=tf.floataa     )     )     )     ,  dtype=tf.intaa     ,  )

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




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

    '''simple docstring'''



    if is_tf_available():
        lowerCamelCase_      :	List[str]	     =      {
            """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     _lowercase     (    self     )     ->      Dict:
           lowerCamelCase   :      Optional[int]			 =					TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"     )
           lowerCamelCase   :      Any			 =					2
           lowerCamelCase   :      List[Any]			 =					2




           class 		UpperCamelCase__	(tf.Module     ):

               '''simple docstring'''



               def __init__(    self     ,  UpperCamelCase__     )     ->      Union[str, Any]:
                      super(UpperCAmelCase_     ,  self     ).__init__()
                      lowerCamelCase   :      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=UpperCAmelCase_     ,  )
               def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     )     ->      int:
                      lowerCamelCase   :      Any			 =					self.model.generate(
                          input_ids=UpperCAmelCase_     ,  attention_mask=UpperCAmelCase_     ,  max_new_tokens=UpperCAmelCase_     ,  return_dict_in_generate=UpperCAmelCase_     ,  )
                      return {"sequences": outputs["sequences"]}




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







    @slow
    def     _lowercase     (    self     )     ->      Dict:
           lowerCamelCase   :      Tuple			 =					TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"     )
           lowerCamelCase   :      Tuple			 =					1
           lowerCamelCase   :      int			 =					2




           class 		UpperCamelCase__	(tf.Module     ):

               '''simple docstring'''



               def __init__(    self     ,  UpperCamelCase__     )     ->      Tuple:
                      super(UpperCAmelCase_     ,  self     ).__init__()
                      lowerCamelCase   :      str			 =					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=UpperCAmelCase_     ,  )
               def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     )     ->      Tuple:
                      lowerCamelCase   :      Dict			 =					self.model.generate(
                          input_ids=UpperCAmelCase_     ,  attention_mask=UpperCAmelCase_     ,  max_new_tokens=UpperCAmelCase_     ,  return_dict_in_generate=UpperCAmelCase_     ,  )
                      return {"sequences": outputs["sequences"]}




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







    @slow
    @require_tensorflow_text
    def     _lowercase     (    self     )     ->      Any:



           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=UpperCAmelCase_     )




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

                      '''simple docstring'''



                      def __init__(    self     )     ->      Optional[int]:
                             super().__init__()
                             lowerCamelCase   :      Dict			 =					text.SentencepieceTokenizer(
                                 model=tf.io.gfile.GFile(os.path.join(UpperCAmelCase_     ,  "spiece.model"     )     ,  "rb"     ).read()     )
                             lowerCamelCase   :      Dict			 =					TFAutoModelForSeqaSeqLM.from_pretrained("hf-internal-testing/tiny-random-t5"     )







                      def     _lowercase     (    self     ,  UpperCamelCase__     ,  *UpperCamelCase__     ,  **UpperCamelCase__     )     ->      Optional[int]:
                             lowerCamelCase   :      Any			 =					self.tokenizer.tokenize(UpperCAmelCase_     )
                             lowerCamelCase   :      Dict			 =					text.pad_model_inputs(
                                 UpperCAmelCase_     ,  max_seq_length=64     ,  pad_value=self.model.config.pad_token_id     )
                             lowerCamelCase   :      Dict			 =					self.model.generate(input_ids=UpperCAmelCase_     ,  attention_mask=UpperCAmelCase_     )
                             return self.tokenizer.detokenize(UpperCAmelCase_     )




                  lowerCamelCase   :      int			 =					CompleteSentenceTransformer()
                  lowerCamelCase   :      Dict			 =					tf.keras.layers.Input(shape=(1,)     ,  dtype=tf.string     ,  name="inputs"     )
                  lowerCamelCase   :      Optional[Any]			 =					complete_model(UpperCAmelCase_     )
                  lowerCamelCase   :      Optional[int]			 =					tf.keras.Model(UpperCAmelCase_     ,  UpperCAmelCase_     )
                  keras_model.save(UpperCAmelCase_     )







    def     _lowercase     (    self     )     ->      int:
           lowerCamelCase   :      str			 =					{
               "do_sample": True,
               "num_beams": 1,
               "top_p": 0.7,
               "top_k": 10,
               "temperature": 0.7,
           }
           lowerCamelCase   :      Any			 =					14

           lowerCamelCase   :      Tuple			 =					AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2"     )
           lowerCamelCase   :      int			 =					"Hello, my dog is cute and"
           lowerCamelCase   :      Optional[int]			 =					tokenizer(UpperCAmelCase_     ,  return_tensors="tf"     )
           lowerCamelCase   :      Tuple			 =					TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"     )

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

           lowerCamelCase   :      List[str]			 =					[638, 198]
           with tf.device(":/CPU:0"     ):
                  tf.random.set_seed(0     )
                  lowerCamelCase   :      Dict			 =					model.generate(**UpperCAmelCase_     ,  eos_token_id=UpperCAmelCase_     ,  **UpperCAmelCase_     )
           self.assertTrue(expectation == len(generated_tokens[0]     )     )







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




           class 		UpperCamelCase__	(__SCREAMING_SNAKE_CASE     ):

               '''simple docstring'''



               def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__=None     ,  **UpperCamelCase__     )     ->      Any:
                      return super().call(UpperCAmelCase_     ,  **UpperCAmelCase_     )




           lowerCamelCase   :      Optional[Any]			 =					FakeBart.from_pretrained("hf-internal-testing/tiny-random-bart"     )
           lowerCamelCase   :      Union[str, Any]			 =					bart_model.generate(UpperCAmelCase_     ,  foo="bar"     ).numpy()
           self.assertTrue(np.array_equal(UpperCAmelCase_     ,  UpperCAmelCase_     )     )




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

               '''simple docstring'''



               def     _lowercase     (    self     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      Optional[int]:
                      return super().call(UpperCAmelCase_     ,  **UpperCAmelCase_     )




           lowerCamelCase   :      Union[str, Any]			 =					FakeEncoder(bart_model.config     ,  bart_model.model.shared     )
           lowerCamelCase   :      int			 =					fake_encoder

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













import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A									=				logging.get_logger(__name__)

__A									=				{
    "microsoft/unispeech-large-1500h-cv": (
        "https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}



class    _SCREAMING_SNAKE_CASE						(	__SCREAMING_SNAKE_CASE    ):







 '''simple docstring'''
 lowercase_		=			"unispeech"

 def __init__(self				:   Any       , UpperCAmelCase_				:   Any=32       , UpperCAmelCase_				:   List[str]=768       , UpperCAmelCase_				:   Any=12       , UpperCAmelCase_				:   Union[str, Any]=12       , UpperCAmelCase_				:   Optional[Any]=3_072       , UpperCAmelCase_				:   List[Any]="gelu"       , UpperCAmelCase_				:   int=0.1       , UpperCAmelCase_				:   Optional[int]=0.1       , UpperCAmelCase_				:   int=0.1       , UpperCAmelCase_				:   Any=0.0       , UpperCAmelCase_				:   str=0.0       , UpperCAmelCase_				:   Dict=0.1       , UpperCAmelCase_				:   Optional[int]=0.1       , UpperCAmelCase_				:   Optional[Any]=0.02       , UpperCAmelCase_				:   Union[str, Any]=1E-5       , UpperCAmelCase_				:   str="group"       , UpperCAmelCase_				:   List[Any]="gelu"       , UpperCAmelCase_				:   Tuple=(512, 512, 512, 512, 512, 512, 512)       , UpperCAmelCase_				:   str=(5, 2, 2, 2, 2, 2, 2)       , UpperCAmelCase_				:   Any=(10, 3, 3, 3, 3, 2, 2)       , UpperCAmelCase_				:   Optional[Any]=False       , UpperCAmelCase_				:   str=128       , UpperCAmelCase_				:   int=16       , UpperCAmelCase_				:   Dict=False       , UpperCAmelCase_				:   Optional[int]=True       , UpperCAmelCase_				:   Dict=0.05       , UpperCAmelCase_				:   Optional[int]=10       , UpperCAmelCase_				:   Tuple=2       , UpperCAmelCase_				:   Union[str, Any]=0.0       , UpperCAmelCase_				:   int=10       , UpperCAmelCase_				:   List[Any]=0       , UpperCAmelCase_				:   Optional[Any]=320       , UpperCAmelCase_				:   int=2       , UpperCAmelCase_				:   Union[str, Any]=0.1       , UpperCAmelCase_				:   str=100       , UpperCAmelCase_				:   Any=256       , UpperCAmelCase_				:   int=256       , UpperCAmelCase_				:   Optional[Any]=0.1       , UpperCAmelCase_				:   str="mean"       , UpperCAmelCase_				:   Union[str, Any]=False       , UpperCAmelCase_				:   List[str]=False       , UpperCAmelCase_				:   List[Any]=256       , UpperCAmelCase_				:   Optional[int]=80       , UpperCAmelCase_				:   Optional[int]=0       , UpperCAmelCase_				:   Optional[Any]=1       , UpperCAmelCase_				:   Union[str, Any]=2       , UpperCAmelCase_				:   Dict=0.5       , **UpperCAmelCase_				:   Optional[int]       , )       ->str:



   '''simple docstring'''



   super().__init__(**UpperCAmelCase_       , pad_token_id=UpperCAmelCase_       , bos_token_id=UpperCAmelCase_       , eos_token_id=UpperCAmelCase_)
   lowerCamelCase__:      Union[str, Any]			  =hidden_size
   lowerCamelCase__:      List[str]			  =feat_extract_norm
   lowerCamelCase__:      Dict			  =feat_extract_activation
   lowerCamelCase__:      Optional[Any]			  =list(UpperCAmelCase_)
   lowerCamelCase__:      Any			  =list(UpperCAmelCase_)
   lowerCamelCase__:      Union[str, Any]			  =list(UpperCAmelCase_)
   lowerCamelCase__:      Dict			  =conv_bias
   lowerCamelCase__:      Optional[Any]			  =num_conv_pos_embeddings
   lowerCamelCase__:      Dict			  =num_conv_pos_embedding_groups
   lowerCamelCase__:      int			  =len(self.conv_dim)
   lowerCamelCase__:      Union[str, Any]			  =num_hidden_layers
   lowerCamelCase__:      Union[str, Any]			  =intermediate_size
   lowerCamelCase__:      Dict			  =hidden_act
   lowerCamelCase__:      List[Any]			  =num_attention_heads
   lowerCamelCase__:      Dict			  =hidden_dropout
   lowerCamelCase__:      Optional[Any]			  =attention_dropout
   lowerCamelCase__:      Optional[Any]			  =activation_dropout
   lowerCamelCase__:      Tuple			  =feat_proj_dropout
   lowerCamelCase__:      int			  =final_dropout
   lowerCamelCase__:      Optional[Any]			  =layerdrop
   lowerCamelCase__:      Dict			  =layer_norm_eps
   lowerCamelCase__:      Optional[Any]			  =initializer_range
   lowerCamelCase__:      int			  =num_ctc_classes
   lowerCamelCase__:      Tuple			  =vocab_size
   lowerCamelCase__:      Dict			  =do_stable_layer_norm
   lowerCamelCase__:      List[Any]			  =use_weighted_layer_sum
   lowerCamelCase__:      Dict			  =classifier_proj_size

   if (
       (len(self.conv_stride) != self.num_feat_extract_layers)
       or (len(self.conv_kernel) != self.num_feat_extract_layers)
       or (len(self.conv_dim) != self.num_feat_extract_layers)
   ):
     raise ValueError(
         "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
         " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
         F""" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,"""
         F""" `len(config.conv_kernel) = {len(self.conv_kernel)}`.""")

   # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
   lowerCamelCase__:      int			  =apply_spec_augment
   lowerCamelCase__:      List[str]			  =mask_time_prob
   lowerCamelCase__:      Union[str, Any]			  =mask_time_length
   lowerCamelCase__:      List[Any]			  =mask_time_min_masks
   lowerCamelCase__:      Any			  =mask_feature_prob
   lowerCamelCase__:      Optional[Any]			  =mask_feature_length
   lowerCamelCase__:      List[str]			  =mask_feature_min_masks

   # parameters for pretraining with codevector quantized representations
   lowerCamelCase__:      Optional[Any]			  =num_codevectors_per_group
   lowerCamelCase__:      str			  =num_codevector_groups
   lowerCamelCase__:      Tuple			  =contrastive_logits_temperature
   lowerCamelCase__:      int			  =feat_quantizer_dropout
   lowerCamelCase__:      Any			  =num_negatives
   lowerCamelCase__:      List[str]			  =codevector_dim
   lowerCamelCase__:      Union[str, Any]			  =proj_codevector_dim
   lowerCamelCase__:      Any			  =diversity_loss_weight

   # ctc loss
   lowerCamelCase__:      Any			  =ctc_loss_reduction
   lowerCamelCase__:      Dict			  =ctc_zero_infinity

   # pretraining loss
   lowerCamelCase__:      Dict			  =replace_prob







 @property
 def 							SCREAMING_SNAKE_CASE_					(self				:   List[Any])       ->Optional[Any]:



   '''simple docstring'''



   return functools.reduce(operator.mul       , self.conv_stride       , 1)

