Skip to content

vllm.model_executor.layers.indexer_topk

Top-k kernels for the DSA sparse attention indexer.

Classes:

Functions:

SparseIndexerTopk

Bases: Module

The sparse indexer's decode top-k stage.

Selects among the available top-k kernels (see kernel_config.sparse_indexer_topk_backend) and runs the chosen one.

Methods:

_cooperative_constraints(logits, topk_tokens, num_rows)

Unmet constraints of cooperative_topk (empty when applicable).

_gvr2_constraints(logits, topk_tokens) staticmethod

Unmet input constraints of gvr_2 (empty when applicable).

_gvr2_warmup(logits, topk_tokens)

Compile gvr_2's kernel variants for every decode row count once.

The kernel family and template depend on the row count, and the indexer runs eagerly (outside CUDA graphs) in some models, so without this every new batch size would JIT-compile (~1 s) in the middle of serving. Must run eagerly; the launchers are cached per row count.

_resolve_auto(logits, topk_tokens, num_rows)

The priority chain: cooperative -> persistent -> per_row. aiter/deep_select/flashinfer/flashinfer_gvr2/torch are opt-in only.

cooperative_topk is preferred whenever it is applicable, i.e. within its AUTO_COOPERATIVE_MAX_ROWS row limit; larger batches go to persistent_topk.

aiter not auto-resolved as it does not outperform the per-row on every shape, hence we delegate to the caller to decide when to use it.

_row_ends(seq_lens, next_n, num_rows) staticmethod

Per-row exclusive end offsets (int32, (num_rows,)) for top-k kernels that take ragged lengths.

seq_lens is (B, next_n) per-row effective lens for native spec decode and (B, 1) otherwise, in which case per-row lens are derived the same way as the other decode top-k kernels.

forward(logits, seq_lens, next_n, topk_indices, topk_tokens, max_seq_len)

Run the resolved decode top-k implementation, writing into topk_indices (int32, -1 fill for rows shorter than topk_tokens).

forward_prefill(logits, row_starts, row_ends, topk_indices, topk_tokens)

Prefill top-k: row r selects over logits[r, row_starts[r]:row_ends[r]] (row_starts None means 0) and writes indices relative to the row start (int32, -1 fill for rows shorter than topk_tokens).

prefill_takes_bf16_logits(logits, topk_tokens, dcp_world_size)

Whether a prefill chunk ranks its BF16 logits with DeepSelect instead of an FP32 copy: for "deep_select", and for "auto" unless batch-invariant mode is on. The DCP top-k merge reads FP32 logits.

resolve_backend(logits, topk_tokens, num_rows)

Resolve the decode top-k implementation from the configured backend ("auto" = the pre-existing chain, or a validated explicit value).

resolve_prefill_backend(logits, topk_tokens, row_starts)

flashinfer_gvr2 for chunks of at least GVR2_PREFILL_MIN_ROWS row-relative rows (row_starts is None) when it was requested explicitly, otherwise per_row (top_k_per_row_prefill).

gvr_2 reads every row from column 0. Unmet gvr_2 constraints fall back instead of raising, since the same backend setting also drives decode.

takes_bf16_logits(logits, topk_tokens, num_rows)

Whether this decode call can consume BF16 without a full FP32 copy.

_get_empty_and_aligned_tensor(dim0, dim1, device, dtype)

Tensor with shape (dim0, dim1) whose stride(0) is 32B-aligned.

deep_select_topk(input, topk, end=None, output_idx=None, indices_dtype=torch.int32, mask_out_of_range=False)

Select the top-k indices per row of input with DeepSelect.

Parameters:

  • input

    (Tensor) –

    (num_rows, vocab_size), bf16 or fp32. stride(1) must be 1 and stride(0) must be 1024B-aligned.

  • topk

    (int) –

    Number of elements to select per row; must be <= 4096.

  • end

    (Tensor | None, default: None ) –

    Optional (num_rows,) int32 tensor with the exclusive right boundary of each row. Rows with end[i] < topk get their remaining indices filled with -1.

  • output_idx

    (Tensor | None, default: None ) –

    Optional preallocated (num_rows, topk) output tensor whose stride(0) is 32B-aligned (e.g. a slice of a wider buffer).

  • indices_dtype

    (dtype, default: int32 ) –

    Output dtype when output_idx is not provided.

  • mask_out_of_range

    (bool, default: False ) –

    Replace indices at or past end with -1. DeepSelect writes one to slot 0 of a row with NaN logits; callers that bound the indices themselves leave this off.

Returns:

  • Tensor –

    The (num_rows, topk) indices tensor.

get_deep_select_stride_requirement() cached

Stride alignment requirement (input, output) in bytes.

is_deep_select_supported(input, topk)

Whether the kernel accepts this input (dtype/stride/topk constraints).