code: def sample_topp(logits, keep_topp):
      sorted_logits, sorted_indices = torch.sort(
          logits, descending=True, dim=1
      )
  
      cumulative_probs = torch.cumsum(
          softmax(sorted_logits, dim=-1), dim=-1
      )
      sorted_indices_to_keep = cumulative_probs.lt(keep_topp)
  
      # keep indices until overflowing p
      cumsum_mask = sorted_indices_to_keep.cumsum(dim=1)
      last_included = cumsum_mask[:, -1:]
      last_included.clamp_(0, sorted_indices_to_keep.size()[1] - 1)
      sorted_indices_to_keep = sorted_indices_to_keep.scatter_(
          1, last_included, 1
      )
  
      # Set all logits that are not in the top-p to -10000.
      # This puts the probabilities close to 0.
      keep_indices = sorted_indices_to_keep.scatter(
          1,
          sorted_indices,
          sorted_indices_to_keep,
      )
      return logits.masked_fill(~keep_indices, -10000)
  
imports: import torch
  from torch.nn.functional import softmax
  from onmt.translate.decode_strategy import DecodeStrategy
label_desc: Use image node_id 1 for calling a global function with example usage: sample_topp(logits, keep_topp) and returns: logits