code: def modify_logits_for_top_p_filtering(logits, top_p):
      """Set the logits for none top-p values to -inf. Done in-place."""
      if top_p <= 0.0 or top_p >= 1.0:
          return
      # First sort and calculate cumulative sum of probabilities.
      sorted_logits, sorted_indices = torch.sort(
          logits, descending=False
      )
      cumulative_probs = sorted_logits.softmax(dim=-1).cumsum(dim=-1)
      # Remove tokens with cumulative top_p above the threshold (token with 0 are kept)
      sorted_indices_to_remove = cumulative_probs <= (1 - top_p)
      # scatter sorted tensors to original indexing
      indices_to_remove = sorted_indices_to_remove.scatter(
          1, sorted_indices, sorted_indices_to_remove
      )
      logits.masked_fill_(indices_to_remove, float("-inf"))
  
imports: import gc
  import time
  from collections import namedtuple
  from dataclasses import dataclass, field
  from functools import partial
  from typing import Callable, Optional, Sequence, Union
  import torch
  import torch.nn.functional
  from einops import rearrange, repeat
  from torch import Tensor
  from torch.profiler import ProfilerActivity, profile, record_function
  from transformers.generation import GreedySearchDecoderOnlyOutput, SampleDecoderOnlyOutput
label_desc: Use image node_id 2 for calling a global function with example usage: modify_logits_for_top_p_filtering(logits, top_p) without return types