code: def calc_banned_ngram_tokens(
      prev_input_ids, num_hypos, no_repeat_ngram_size, cur_len
  ):
      # Copied from fairseq for no_repeat_ngram in beam_search
      if cur_len + 1 < no_repeat_ngram_size:
          # return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet
          return [[] for _ in range(num_hypos)]
      generated_ngrams = [{} for _ in range(num_hypos)]
      for idx in range(num_hypos):
          gen_tokens = prev_input_ids[idx].numpy().tolist()
          generated_ngram = generated_ngrams[idx]
          for ngram in zip(
              *[gen_tokens[i:] for i in range(no_repeat_ngram_size)]
          ):
              prev_ngram_tuple = tuple(ngram[:-1])
              generated_ngram[prev_ngram_tuple] = generated_ngram.get(
                  prev_ngram_tuple, []
              ) + [ngram[-1]]
  
      def _get_generated_ngrams(hypo_idx):
          # Before decoding the next token, prevent decoding of ngrams that have already appeared
          start_idx = cur_len + 1 - no_repeat_ngram_size
          ngram_idx = tuple(
              prev_input_ids[hypo_idx, start_idx:cur_len]
              .numpy()
              .tolist()
          )
          return generated_ngrams[hypo_idx].get(ngram_idx, [])
  
      banned_tokens = [
          _get_generated_ngrams(hypo_idx)
          for hypo_idx in range(num_hypos)
      ]
      return banned_tokens
  
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 2 for calling a global function with example usage: calc_banned_ngram_tokens(prev_input_ids, num_hypos, no_repeat_ngram_size, cur_len) and returns: banned_tokens, unknown, generated_ngrams