flashinfer.sampling.top_p_renorm_probs

flashinfer.sampling.top_p_renorm_probs(probs: Tensor, top_p: Tensor | float, is_deterministic: bool = False) → Tensor

Fused GPU kernel for renormalizing probabilities by top-p thresholding.

Uses AIR Top-P algorithm (radix-based) for efficient threshold finding.

Parameters:
  • probs (torch.Tensor) – Probabilities, shape (batch_size, num_classes).

  • top_p (Union[torch.Tensor, float]) – Either a scalar or a tensor of shape (batch_size,), representing the top-p threshold for re-normalizing probabilities, should be in (0, 1). If a scalar, the same threshold is used for all requests. If a tensor, each request has its own threshold. We mask out the probabilities less than threshold where the cumulative sum of probs[probs >= threshold] is top_p, and renormalize the probabilities.

  • is_deterministic (bool) – If True, use deterministic integer accumulation for reproducible results. This will affect performance. Default is False.

Returns:

renorm_probs – Renormalized probabilities, shape (batch_size, num_classes).

Return type:

torch.Tensor

Examples

>>> import torch
>>> import flashinfer
>>> torch.manual_seed(42)
>>> batch_size = 4
>>> vocab_size = 5
>>> top_p = 0.3
>>> pre_norm_prob = torch.rand(batch_size, vocab_size).to(0)
>>> prob = pre_norm_prob / pre_norm_prob.sum(dim=-1, keepdim=True)
>>> prob
tensor([[0.2499, 0.2592, 0.1085, 0.2718, 0.1106],
        [0.2205, 0.0942, 0.2912, 0.3452, 0.0489],
        [0.2522, 0.1602, 0.2346, 0.1532, 0.2000],
        [0.1543, 0.3182, 0.2062, 0.0958, 0.2255]], device='cuda:0')
>>> renormed_probs = flashinfer.sampling.top_p_renorm_probs(prob, top_p)
>>> renormed_probs
tensor([[0.0000, 0.4882, 0.0000, 0.5118, 0.0000],
        [0.0000, 0.0000, 0.0000, 1.0000, 0.0000],
        [0.5181, 0.0000, 0.4819, 0.0000, 0.0000],
        [0.0000, 1.0000, 0.0000, 0.0000, 0.0000]], device='cuda:0')

Note

This combination of top_p_renorm_probs and sampling_from_probs should be equivalent to top_p_sampling_from_probs.

On compute capability 10.3 (B300 / GB300), float32 probs with a per-row float32 top_p tensor and is_deterministic=False are served by a single-launch thread-block-cluster kernel for the (batch_size, vocab_size) rows listed in top_p_renorm_probs_specialized_workloads.json. It applies the same rule with 64-bit fixed-point masses: each entry is a whole number of units of 2**-q of the row’s scale, exact for entries of at least 2**-35 of the row’s mass and rounded to the nearest unit below that, so the masses lie within V / 2 units of the exact ones (V is vocab_size; below 2**-41 of the row’s mass for V <= 2**18). The kept set is what exact arithmetic keeps unless a prefix mass lies within that bound of the target; AIR’s float32 sums may keep different entries where a prefix mass lies within float32 rounding of the target. A NaN entry is never kept. Every other call takes the AIR top-p kernels; FLASHINFER_SPECIALIZED_KERNEL_DISABLE=1 (read on every call) forces them.