vllm.v1.sample.ops.topk_topp_triton
¶
Top-k/top-p masking of FP32 logits through group maxima, in two launches.
- Levels (rows x splits): stream each row once and write the max of every group of 16 logits (level 1) and of every 256 (level 2), the row max, and the sum of exp. No compaction, so it runs close to the streaming bandwidth.
- One persistent cooperative kernel, with phases separated by grid barriers: a. Select (per row): exact selection through the levels, in one tier of 32, 64, 128 or 256 logits (top-p only: 128). At each level a histogram of the maxima picks the best bins that hold enough items, at most the tier's: they hold the best items of the next level. Only the tier's logits are sorted. b. Only if a row missed its tier (k > 256, a larger nucleus, crowded ties): rounds over each row's span of finite logits (at first all of them, below the row max; a row with fewer than k keeps them all). All programs histogram the spans (CUDA: 8192 bins of 1/512 nat at first, the last one open below), with the exact count and mass above them, then a plan per row finds the bins that can hold the k and p cuts (from counts and mass bounds). If they hold at most 4096 logits, one pass masks the bins below and gathers the cut bins, and an exact cut on the sorted window masks the row. Else the row narrows its span to those bins for the next round (to the k cut first if the cuts lie apart, and to the range of the open bin's logits). A row left after the last round is cut by one program, by an exact radix select. c. Fill (rows x splits): -inf, then the kept logits of the rows cut by a tier. Exactly k logits are kept, ties broken by the lowest index, then the smallest prefix whose mass reaches p. Runtime ints are not specialized, so one compiled variant serves every batch size and k/p mix.
Functions:
-
allocate–Level 1 and 2 maxima, (max, sum of exp) per split and per row, row states, kept
-
apply_top_k_top_p_triton–Mask logits outside the per-row top-k, then outside the top-p nucleus.
-
buffers–Per-row workspace for at least rows rows.
-
capacity–Programs of the persistent kernel that fit on the GPU at once, so that the
-
is_supported–Whether the kernel runs on this platform's GPUs for this vocab size. Triton
-
kernel_args–Arguments of the levels and the filter kernel.
-
num_splits–Split rows for streaming until the grid has about 4 programs per SM.
-
programs_per_sm_cuda–An estimate: the cooperative launch fails if it is too high.
-
programs_per_sm_rocm–ROCm launches without the cooperative check and would hang on a grid that does
-
reserve_workspace–Allocate the workspace for rows rows now, e.g. while memory profiling counts
_above(hist_row, before, total, at, F, NC, BINS)
¶
Logits in the bins before bin at (at most BINS).
_bin(x, top, scale, BINS)
¶
Histogram bin of a logit: bins of 1/scale nats below top, the last bin holds everything further down (and -inf).
_cut_window(row_ptr, scratch, found, above, between, k_at, k, p, row_max, row_sum, vocab, want_kept, N, WINDOW)
¶
Sort the gathered keys (the window, then bin kb), keep those up to the k-th and up to where the mass from the top reaches p of the kept mass, -inf over the rest. Returns the kept mass, if wanted.
_fill(row_ptr, state, kept_row, lo, vocab, chunk, BLOCK)
¶
Mask the split's chunk from the kept keys (state >= 0).
_find(hist_row, block, before, target, KIND, F, BINS, scale, floor_w)
¶
The first bin of the block where the running weight, from before, reaches target (else the block's last bin).
_flagged(k, p, vocab, flag)
¶
The k and p of a row's current cut.
_gather_part(row_ptr, scratch, found_row, more_row, row_max, window_row, span_row, first, start_at, vocab, chunk, BINS, BIN_SCALE, WINDOW, BLOCK)
¶
One split's share of the window pass: -inf over the bins below the window (but bin kb), the indices of the window and of bin kb gathered, and the mass of the bins above the window and of those between it and bin kb.
_gather_window(window_row, found_row, more_row, state_row, lo, hi, kb, k_at, WINDOW)
¶
Queue the row's window (bins lo to hi, and bin kb) for the gather pass.
_grid_exit(sync_ptr, programs)
¶
The last program to leave resets the barrier, the exit and the row counts.
_grid_sync(sync_ptr, target)
¶
Wait until target arrivals (every program, once per barrier so far).
_histogram(row_ptr, hist_row, span_row, above_row, tail_row, row_max, use_k, first, lo, vocab, chunk, BINS, BIN_SCALE, BLOCK)
¶
Add the chunk's finite logits in the row's span to its histogram. A narrowed span also adds the exact count and mass of the logits above it, and the mass below it. The first span of a top-k row also tracks its last bin's range.
_key(x, idx)
¶
Ascending order is logit descending, then index ascending.
_mass_above(hist_row, per_block, at, KIND, F, NC, BINS, scale, floor_w)
¶
Bound of the mass in the bins before bin at.
_ordered(x)
¶
Order-preserving uint32 of the logit, in an int64.
_pick(values, items, mask, need, row_max, slots, C)
¶
The items in the best bins (1/32 nat below the row max, the last bin refined over its own value range if crowded) that hold at least need of them, if at most C: whether they fit, the items (compacted through slots) and their count. With fewer than need finite items, they are all picked.
_plan_row(hist_row, state_row, window_row, found_row, more_row, span_row, above_row, tail_row, next_rows, cut_rows, k, p, row_max, row_sum, vocab, first, last, BINS, BIN_SCALE, WINDOW)
¶
From the histogram of the row's span: bin kb holds the k-th logit, bins lo to hi the top-p cut (mass bounds from the bin edges, the top-k mass bounded through bin kb). If the window and bin kb hold at most WINDOW logits, the row is gathered by every program. Else the row narrows its span to them for the next round (to bin kb first if it lies apart), or one program cuts it. Two levels: sums over blocks of F bins, then the bins of the one block that holds each cut.
_radix_select(row_ptr, vocab, bound_hi, bound_lo, target, row_max, MASS, BLOCK)
¶
The largest key whose keys at or above it, within the bound, reach target elements (or mass).
_reach(hist_row, per_block, target, KIND, F, NC, BINS, scale, floor_w)
¶
The first bin where the running weight reaches target (else the last bin).
_resolve_row(row_ptr, hist_row, scratch, state_row, span_row, above_row, tail_row, stats_row, k, p, vocab, last, gathered, found, more_row, window_row, BINS, BIN_SCALE, WINDOW, BLOCK)
¶
A gathered row: the exact cut among its window, which masks the row (after a top-k cut, the top-p cut is queued over the kept mass). Else one program cuts the row by radix select.
_row_params(k_ptr, p_ptr, row, vocab, has_k, has_p)
¶
The row's k and p, or the values that disable them (an absent k or p is a null pointer).
_row_stats(stats_ptr, row, splits, MAX_SPLITS)
¶
The row's max and sum of exp(logit - max), from the per-split partials.
_span(span_row, row_max, first, BIN_SCALE)
¶
The row's span (top, floor, scale, flag): every logit below the row max in the first round.
_span_bin(x, top, floor, scale, BINS)
¶
Bin of a logit in a span: -1 above it, BINS + 1 below it or NaN (BINS is no bin).
_start_span(hist_row, span_row, above_row, tail_row, top, floor, scale, flag, BINS)
¶
Queue the row for a histogram round over its logits from top down to floor.
_supported_device()
cached
¶
Tested on SM80, SM90, SM100, SM120, gfx942 (MI325X) and gfx950 (MI355X), and needs all programs of its grid resident at once.
_tier(row_ptr, level1_row, kept_row, level2_row, scratch, supergroups, k, p, row_max, row_sum, vocab, groups, C, SUPERGROUPS_P2)
¶
Store the kept keys if the row's best C logits decide the cutoff, return whether they did and how many are kept. Each level (supergroup maxima, group maxima, logits) picks the at most C items in its best bins that hold at least need: they hold the best need items of the next level. Histograms and one sort of C keep the code small (it runs cold, once per row).
_track_tail(row_ptr, tail_row, top, scale, lo, hi, BINS, BLOCK)
¶
Update the best logit in the first span's open last bin and the lowest finite logit with the chunk's. A pass of its own, so that the histogram's stays lean.
_weights(counts, j, KIND, BINS, scale, floor_w)
¶
Per bin: the count (KIND 0), or bounds of its mass relative to the span's top, from above (1) or below (2, the last bin bounded by the span's floor).
allocate(device, rows, vocab)
¶
Level 1 and 2 maxima, (max, sum of exp) per split and per row, row states, kept keys, histograms, per-row gather scratch, windows, gather counts and masses, spans (top, floor, scale, flag) with the count and mass above (and mass below) them, the best logit in their open last bin and the lowest finite logit, the rows queued for the histogram path, and the grid barrier (barrier count, exit count, histogram rows per round from 2, rows to cut per round from 8), which is zeroed once and reset by each call.
apply_top_k_top_p_triton(logits, k, p)
¶
Mask logits outside the per-row top-k, then outside the top-p nucleus.
Parameters:
-
(logits¶Tensor) –[batch_size, vocab_size] float32 CUDA tensor.
-
(k¶Tensor | None) –[batch_size] top-k per row, or None. k >= vocab_size disables it.
-
(p¶Tensor | None) –[batch_size] top-p per row, or None. p >= 1 disables it.
Returns:
buffers(device, rows, vocab)
¶
Per-row workspace for at least rows rows.
capacity(key, args)
¶
Programs of the persistent kernel that fit on the GPU at once, so that the grid barriers cannot deadlock (the cooperative launch fails otherwise).
is_supported(vocab_size)
¶
Whether the kernel runs on this platform's GPUs for this vocab size. Triton specializes a vocab of 1, which does not compile.
kernel_args(logits, k, p, has_k, has_p, b, sms)
¶
Arguments of the levels and the filter kernel.
num_splits(rows, sms)
¶
Split rows for streaming until the grid has about 4 programs per SM.
programs_per_sm_cuda(props, args)
¶
An estimate: the cooperative launch fails if it is too high.
programs_per_sm_rocm()
¶
ROCm launches without the cooperative check and would hang on a grid that does not fit, so it takes the one program that always fits a CU (hipModuleOccupancyMaxActiveBlocksPerMultiprocessor could tell if more fit).
reserve_workspace(device, rows, vocab)
¶
Allocate the workspace for rows rows now, e.g. while memory profiling counts it, so that serving does not grow it.