code: def top_k_top_p_filtering(
      logits, top_k=0, top_p=0.0, filter_value=-float("inf")
  ):
      # This function has been mostly taken from huggingface conversational ai code at
      # https://medium.com/huggingface/how-to-build-a-state-of-the-art-conversational-ai-with-transfer-learning-2d818ac26313
  
      if top_k > 0:
          # Remove all tokens with a probability less than the last token of the top-k
          indices_to_remove = (
              logits < torch.topk(logits, top_k)[0][..., -1, None]
          )
          logits[indices_to_remove] = filter_value
  
      batch_size = logits.size()[0]
      if top_p > 0.0:
          logits = logits.view(batch_size, -1).contiguous()
          for index in range(len(logits)):
              sorted_logits, sorted_indices = torch.sort(
                  logits[index].view(-1), descending=True
              )
              cumulative_probs = torch.cumsum(
                  F.softmax(sorted_logits, dim=-1), dim=-1
              )
              # Remove tokens with cumulative probability above the threshold
              sorted_indices_to_remove = cumulative_probs > top_p
              # Shift the indices to the right to keep also the first token above the threshold
              sorted_indices_to_remove[
                  ..., 1:
              ] = sorted_indices_to_remove[..., :-1].clone()
              sorted_indices_to_remove[..., 0] = 0
              indices_to_remove = sorted_indices[
                  sorted_indices_to_remove
              ]
              logits[index][indices_to_remove] = filter_value
  
          logits = logits.view(batch_size, -1).contiguous()
  
      return logits
  
file_path: DeepKE/example/llm/CPM-Bee/src/cpm_live/generation/generation_utils.py
imports: import torch
  import torch.nn.functional
label_desc: Use image node_id 1 for calling a global function with example usage: top_k_top_p_filtering(logits, top_k, top_p, filter_value) and returns: logits