code: def tf_top_k_top_p_filtering(
      logits,
      top_k=0,
      top_p=1.0,
      filter_value=-float("Inf"),
      min_tokens_to_keep=1,
  ):
      """
      Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
  
      Args:
          logits: logits distribution shape (batch size, vocabulary size)
          top_k (`int`, *optional*, defaults to 0):
              If > 0, only keep the top k tokens with highest probability (top-k filtering)
          top_p (`float`, *optional*, defaults to 1.0):
              If < 1.0, only keep the top tokens with cumulative probability >= top_p (nucleus filtering). Nucleus
              filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751)
          min_tokens_to_keep (`int`, *optional*, defaults to 1):
              Minimumber of tokens we keep per batch example in the output.
  
      From: https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317
      """
      logits_shape = shape_list(logits)
  
      if top_k > 0:
          top_k = min(
              max(top_k, min_tokens_to_keep), logits_shape[-1]
          )  # Safety check
          # Remove all tokens with a probability less than the last token of the top-k
          indices_to_remove = (
              logits < tf.math.top_k(logits, k=top_k)[0][..., -1, None]
          )
          logits = tf.where(indices_to_remove, filter_value, logits)
      if top_p < 1.0:
          sorted_indices = tf.argsort(logits, direction="DESCENDING")
          sorted_logits = tf.gather(
              logits, sorted_indices, axis=-1, batch_dims=1
          )  # expects logits to be of dim (batch_size, vocab_size)
  
          cumulative_probs = tf.math.cumsum(
              stable_softmax(sorted_logits, axis=-1), axis=-1
          )
  
          # Remove tokens with cumulative probability above the threshold (token with 0 are kept)
          sorted_indices_to_remove = cumulative_probs > top_p
  
          if min_tokens_to_keep > 1:
              # Keep at least min_tokens_to_keep (set to min_tokens_to_keep-1 because we add the first one below)
              sorted_indices_to_remove = tf.concat(
                  [
                      tf.zeros_like(
                          sorted_indices_to_remove[
                              :, :min_tokens_to_keep
                          ]
                      ),
                      sorted_indices_to_remove[:, min_tokens_to_keep:],
                  ],
                  -1,
              )
  
          # Shift the indices to the right to keep also the first token above the threshold
          sorted_indices_to_remove = tf.concat(
              [
                  tf.zeros_like(sorted_indices_to_remove[:, :1]),
                  sorted_indices_to_remove[:, :-1],
              ],
              -1,
          )
          # scatter sorted tensors to original indexing
          indices_to_remove = scatter_values_on_batch_indices(
              sorted_indices_to_remove, sorted_indices
          )
          logits = tf.where(indices_to_remove, filter_value, logits)
      return logits
  
imports: import inspect
  import warnings
  from dataclasses import dataclass
  from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  import tensorflow
  from tensorflow.compiler.tf2xla.python.xla import dynamic_update_slice
  from .generation_tf_logits_process import TFForcedBOSTokenLogitsProcessor, TFForcedEOSTokenLogitsProcessor, TFForceTokensLogitsProcessor, TFLogitsProcessorList, TFMinLengthLogitsProcessor, TFNoBadWordsLogitsProcessor, TFNoRepeatNGramLogitsProcessor, TFRepetitionPenaltyLogitsProcessor, TFSuppressTokensAtBeginLogitsProcessor, TFSuppressTokensLogitsProcessor, TFTemperatureLogitsWarper, TFTopKLogitsWarper, TFTopPLogitsWarper
  from .models.auto import TF_MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING, TF_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING, TF_MODEL_FOR_VISION_2_SEQ_MAPPING
  from .tf_utils import shape_list, stable_softmax
  from .utils import ModelOutput, logging
label_desc: Use image node_id 4 for calling a global function with example usage: tf_top_k_top_p_filtering(logits, top_k, top_p, filter_value, min_tokens_to_keep) and returns: logits