Skip to content

vllm.v1.sample.ops.topk_topp_triton

Combined Top-K and Top-P Triton kernels.

Based on the paper "Qrita: High-performance Top-k and Top-p Algorithm for GPUs using Pivot-based Truncation and Selection" By Park et al. (https://arxiv.org/abs/2602.01518)

Functions:

_apply_topp_split(logits, k, p, mask_value, num_sm)

Split-row top-p pipeline for small batches; masks logits in place.

_topp_sb_combine(PART_BASE, Z, p, lo, hi, best, best_mass, best_minl, best_nmin, S, F, IS_LAST)

Combine one round's partials and advance the search state.

Returns (done, nomask, pivot, dup, numdup, numkeep, lo, hi, best, best_mass, best_minl, best_nmin, sel_fidx, best_sel). best* tracks the tightest fallback: the largest evaluated pivot whose above-mass is >= p, which guarantees the top-p mass invariant if the search fails to converge. Pivots are recomputed from (lo, hi) via the same ladder the sweep used, so partials line up with pivots without storing them. sel_fidx / best_sel report which ladder slot produced dup / updated best this round (-1 if none), so callers can recover the per-slice (minl, nmin) partials behind the boundary value for deterministic duplicate handling.

_topp_sb_ladder(lo, hi, F)

Geometric ladder of F pivots inside (lo, hi).

_topp_sb_row_stats(STATS, row, S)

Combine per-slice partials into row (max, sum_exp, min, n_finite).

_update_min_larger_stats(data, above_mask, min_larger, num_min_larger, sentinel)

Update running (min, count) of values above a pivot across tiles.

Tracks the smallest value strictly above a pivot and how many times it occurs. Called once per tile per pivot; the running state is carried across tiles via min_larger / num_min_larger.

Merge rule
  • tile min < running min → replace both
  • tile min == running min → accumulate count
  • tile min > running min → keep running values

apply_top_k_top_p_triton(logits, k, p, mask_value=float('-inf'), has_topp_only_rows=True)

Apply combined top-k and top-p masking using Triton.

Top-k is applied first (by logit value), then top-p is applied to the remaining k values (by probability).

Parameters:

  • logits

    (Tensor) –

    [batch_size, vocab_size] float32 tensor. The returned tensor may alias this input or be a new contiguous tensor for unsupported layouts.

  • k

    (Tensor | None) –

    [batch_size] int32 tensor of top-k values per row, or None to disable top-k

  • p

    (Tensor | None) –

    [batch_size] float32 tensor of top-p values per row (0 to 1), or None to disable top-p

  • mask_value

    (float, default: float('-inf') ) –

    Value for masked positions (default: -inf)

  • has_topp_only_rows

    (bool, default: True ) –

    Whether any row has p < 1 and k >= vocab_size. False lets small batches skip the split top-p pipeline; pass it only if no such row exists, as such a row would get no top-p.

Returns:

  • Tensor –

    The masked logits tensor. It may or may not be modified in-place.