code: def top_k_top_p_filtering(
      logits,
      top_k=0,
      top_p=1.0,
      filter_value=-float("Inf"),
      min_tokens_to_keep=1,
  ):
      """
      根据top_k, top_p的值，将不满足的值置为filter_value的值
  
      :param torch.Tensor logits: bsz, vocab_size
      :param int top_k: 如果大于0，则只保留最top_k的词汇的概率，剩下的位置被置为filter_value
      :param int top_p: 根据(http://arxiv.org/abs/1904.09751)设置的筛选方式
      :param float filter_value:
      :param int min_tokens_to_keep: 每个sample返回的分布中有概率的词不会低于这个值
      :return:
      """
      if top_k > 0:
          top_k = min(
              max(top_k, min_tokens_to_keep), logits.size(-1)
          )  # Safety check
          # 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
  
      if top_p < 1.0:
          sorted_logits, sorted_indices = torch.sort(
              logits, descending=True
          )
          cumulative_probs = torch.cumsum(
              F.softmax(sorted_logits, dim=-1), dim=-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[..., :min_tokens_to_keep] = 0
          # 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
  
          # scatter sorted tensors to original indexing
          indices_to_remove = sorted_indices_to_remove.scatter(
              1, sorted_indices, sorted_indices_to_remove
          )
          logits[indices_to_remove] = filter_value
      return logits
  
file_path: fastNLP/fastNLP/modules/torch/generator/seq2seq_generator.py
imports: import torch
  from torch import nn
  import torch.nn.functional
  from ..decoder.seq2seq_decoder import Seq2SeqDecoder, State
  from functools import partial
label_desc: Use image node_id 6 for calling a global function with example usage: top_k_top_p_filtering(logits, top_k, top_p, filter_value, min_tokens_to_keep) and returns: logits