Skip to content

vllm.model_executor.layers.sparse_attn_indexer

Custom Sparse Attention Indexer layers.

Classes:

  • SparseAttnIndexer –

    Sparse Attention Indexer Custom Op Layer. This layer is extracted as a

Functions:

SparseAttnIndexer

Bases: CustomOp

Sparse Attention Indexer Custom Op Layer. This layer is extracted as a separate custom op since it involves heavy custom kernels like mqa_logits, paged_mqa_logits and top_k_per_row, etc. Those kernels maybe requires specific memory layout or implementation for different hardware backends to achieve optimal performance.

For now, the default native path will use CUDA backend path. Other platform may requires add the corresponding Custom Op name sparse_attn_indexer to custom_ops in CompilationConfig to enable the platform specific path.

Methods:

  • forward_cpu –

    CPU sparse attention indexer: cache write stays eager Python glue

forward_cpu(hidden_states, q_quant, k, weights)

CPU sparse attention indexer: cache write stays eager Python glue (own K-cache layout, not shared with the main attention cache write). PREFILL and DECODE both call the ported fp8_paged_mqa_logits_cpu/topk_transform_512_cpu kernels, which read the paged K-cache directly via page_table -- no eager gather step, no per-request Python loop.

prefill_metadata.chunks always has exactly one entry here: DeepseekV4CPUIndexerMetadataBuilder overrides the base chunk split to always return the whole step's prefill batch as one chunk, since the base chunking only bounds CUDA/XPU's dense M*N logits tensor and flat K-gather workspace, neither of which this paged kernel allocates.

_decode_chunk_ranges(rows, width, budget, itemsize)

Partition independent Q rows under a payload budget of logits whose elements take itemsize bytes.

_gather_workspace_shapes(total_seq_lens, head_dim, fp8_dtype, use_fp4_cache)

Return ((values_shape, values_dtype), (scales_shape, scales_dtype)) for the K-gather workspace. FP8 path: (T, head_dim) fp8 + (T, 4) uint8 fp32 scales. MXFP4 path: (T, head_dim // 2) uint8 packed mxfp4 + (T, head_dim // MXFP4_BLOCK_SIZE) uint8 ue8m0 scales.

_merge_dcp_topk_global(logits, topk_indices, topk_tokens, dcp_rank, dcp_world_size, cp_interleave, row_starts=None, logits_dtype=torch.float32, num_positions=None, direct_max_rows=0)

Merge each DCP rank's local top-K into the global top-K.

topk_indices are this rank's local top-K positions into its 1/N KV shard. A token in the global top-K must also be in its owning rank's local top-K (at most topk_tokens - 1 tokens rank globally above it, hence at most that many on its own rank), so exchanging only the per-rank local candidates is exact -- equivalent to all-gathering the full logit matrix, but it ships dcp_world_size * topk_tokens candidates instead of the whole score row. Overwrites topk_indices with global token ids (-1 for padding); the attention backend localizes them back to physical slots per rank. The selector keys only the score bits that logits_dtype (the dtype the MQA logits were computed in, before the FP32 upcast) carries and the id bits that positions below num_positions use.

Batches of at most direct_max_rows rows exchange the candidates over symmetric memory instead of an NCCL all-gather (the workspace was created by SparseAttnIndexer).

_varlen_decode_rows(q, q_scale, weights, kv_cache, decode, max_model_len, max_seq_len, topk_backend, topk_tokens, topk_indices_buffer, candidate_blocks, candidate_block_size, candidate_write, logits_dtype, start, end)

The varlen (SM100) decode logits, candidate selection and top-k of rows [start, end), with the slice's own DeepGEMM schedule.

as_fp32_logits(logits, row_ends)

Upcast BF16 MQA logits for the FP32 top-k kernels.

Only columns below row_ends (int32, one per row) are converted, the span the consumers read; DeepGEMM's aligned row stride is kept, with storage for whole rows (gvr_2 reads the logits as [rows, stride]).

dcp_decode_logits_width(max_model_len, dcp_world_size, cp_kv_cache_interleave_size)

Logits columns a decode row can use on one rank.

Under DCP each rank scores only the KV it holds, so its decode context lengths are local (get_dcp_local_seq_lens): at most cdiv(max_model_len, dcp_world_size * interleave) * interleave tokens.

dcp_gather_kv_rows(local_padded, deinterleave_idx, gathered_buf, out_buf)

All-gather this rank's KV shard and return it in global token order.

dcp_shardq_context(forward_context, k_cache_prefix)

The MLA layer and gathered context of a DCP ShardQ step whose MLA layer this indexer cache follows (shardq_layer_name), or None.

fuse_indexer_wk_weights_proj(indexer)

Fill the fused BF16 wk_weights_proj weight in place (captured CUDA graphs keep its address) from wk and weights_proj, copying only a part that is not already a view of its rows (e.g. dots3's own weights_proj).

init_indexer_wk_weights_proj(indexer, hidden_size, quant_config, prefix, *, fused_linear_cls=MergedColumnParallelLinear)

Build the indexer's checkpoint layers wk (BF16, or FP8 as the checkpoint stores it and run in BF16: bf16_linear) and weights_proj (BF16), and wk_weights_proj, the BF16 linear that runs both as one GEMM. Its weight is not a parameter: it holds wk and weights_proj's rows and is refilled after every load and every in-place reload of the runtime weights (fuse_indexer_wk_weights_proj from the indexer's post-load hook).

kv_cache_as_quant_view(kv_cache, head_dim, use_fp4_cache)

4D [num_blocks, block_size, 1, head_width] view expected by DeepGEMM, from the 3D indexer kv-cache allocation.

should_skip_indexer_scoring(mla_metadata, cudagraph_runtime_mode)

Whether the indexer can skip scoring because the main MLA runs a pure prefill on dense MHA, which does not read the top-k indices.

The indexer and main MLA may classify the same short extend differently because they use independent decode thresholds, so this takes the main MLA metadata. The indexer K cache must still be written.