Skip to content

vllm.v1.attention.backends.mla.flashinfer_mla_sparse

FlashInfer sparse MLA attention backend.

Classes:

Functions:

FlashInferMLASparseImpl

Bases: SparseMLACommonImpl[FlashInferMLASparseMetadata]

FlashInfer MLA Sparse implementation.

Uses the TRT-LLM MLA kernel with sparse_mla_top_k parameter for sparse attention computation.

Methods:

_forward_prims_ts(q, kv_cache, topk_indices, valid_counts, layer, block_size)

Sparse MLA of every row of q [T, H, 576] (E4M3, quantized by the layer with q_scale) over its valid_counts leading physical rows of topk_indices (-1 entries are masked).

autotune_hisparse_decode(layer)

Autotune the largest legal HiSparse decode batch.

FlashInferMLASparseMetadata dataclass

Bases: SparseMLACommonMetadata

Attention metadata for FlashInfer MLA Sparse backend.

FlashInferMLASparseMetadataBuilder

Bases: SparseMLACommonMetadataBuilder[FlashInferMLASparseMetadata]

Builder for FlashInfer MLA Sparse attention metadata.

FlashInferMLASparseSM120Backend

Bases: _FlashInferMLASparseBackendBase

FlashInfer sparse MLA backend for SM120.

FlashInferMLASparseTRTLLMBackend

Bases: _FlashInferMLASparseBackendBase

FlashInfer sparse MLA backend using the TRTLLM-gen launcher.

FlashInferMLASparseTRTLLMMetadataBuilder

Bases: FlashInferMLASparseMetadataBuilder

Metadata builder for the SM100 TRT-LLM sparse MLA kernel.

_FlashInferMLASparseBackendBase

Bases: AttentionBackend

Common metadata for concrete FlashInfer sparse MLA backends.

_PrimsTSPlans

Planned prims_ts wrappers, shared by all layers.

A plan fixes FlashInfer's schedule (chosen from its row capacity) and compiles that schedule's kernel on first use, which takes seconds. So every row capacity of a geometry is planned at once, at startup (from the profile run's reserve_workspace), and a call runs on the smallest capacity that holds its rows: no planning or compilation happens while serving, and the plan count stays fixed. Each plan is a packed-query plan of capacity one-token requests, so its schedule is the one FlashInfer picks for capacity decode rows; a call with fewer rows passes the matching offsets (qo_indptr = [0, 1, ..., rows, ..., rows]).

The sparse MLA layers run one at a time on one stream, so every plan shares one workspace. A geometry that needs more workspace replaces it; plans keep the buffer they were planned with, so graphs captured with them stay valid.

Methods:

  • get –

    {capacity: (wrapper, qo_indptr of a full call)} for one geometry.

get(device, buckets, heads, kv_cache_format, max_topk, return_lse)

{capacity: (wrapper, qo_indptr of a full call)} for one geometry.

_finish_dcp_partials(out, lse, counts)

In place, for trtllm-gen's DCP partials out [T, H, D] and lse [T, H] (views of its head-padded buffers): rows without top-k entries (counts 0; the DCP index conversion compacts a row's valid entries to its front and counts them, so these are the rows whose entries are all -1) get output 0 and LSE -inf, the empty-shard identity of the DCP combine. One small kernel that writes only the empty rows.

_finish_dcp_partials_kernel(out_ptr, lse_ptr, counts_ptr, heads, out_row_stride, lse_row_stride, HEAD_DIM, HEADS_BLOCK, OUT_BLOCK)

One row of a trtllm-gen DCP partial: without top-k entries (count 0) its output is zeroed and its LSE set to -inf; other rows are not touched.

_kv_counters(num_rows, num_heads)

multi_ctas_kv_counter_buffer for one launch when the reserved counters cover it; otherwise FlashInfer allocates its own.

_pad_query_heads(query, heads)

query [T, H, D] in a new contiguous [T, heads, D] buffer whose extra heads are zero (the head count a decode kernel runs), in one kernel.

_pad_query_heads_kernel(src_ptr, dst_ptr, src_row_stride, row_words, padded_row_words, BLOCK)

One query row of 8-byte words, zero-extended to the padded heads.

_prims_ts_row_buckets(vllm_config)

Row capacities to plan: every CUDA graph size (graph replays keep the schedule FlashInfer picks for their exact row count) and the powers of two up to the largest call, which serve every other row count.

_required_workspace_bytes(dcp_world_size, num_heads_per_rank, max_num_batched_tokens)

Resolve the workspace size, honoring an explicit env override.

_reserve_kv_counters(device, num_rows, num_heads)

Allocates the zeroed trtllm-gen multi-CTA KV counters for launches of up to num_rows x num_heads, before CUDA graph capture. The kernel resets them after each launch, so all layers share them, as they share the workspace. Without them FlashInfer zero-allocates counters on every call.

compute_trtllm_sparse_mla_workspace_bytes(base_workspace_bytes, dcp_world_size, num_heads_per_rank, max_num_batched_tokens)

Workspace bytes needed by the trtllm-gen sparse MLA decode kernel.

Under DCP the query is all-gathered across the DCP group in the head dim and the kernel is asked for LSE, so trtllm-gen carves the softmax-stats slab described above from the workspace before its (batch-independent) counter and scratch regions. base_workspace_bytes must stay available for those regions, so the slab is added on top of it (see #50781).

Without DCP no LSE is requested, no slab is carved, and the base size is returned unchanged. A launch covers at most _TRTLLM_GEN_MAX_REQUESTS tokens, and launches run in order on one stream, so larger batches reuse the same slab.

trtllm_gen_query_heads(num_heads)

Query heads the trtllm-gen MLA decode runs: it takes no head count strictly between 64 and 128 (e.g. 96 gathered under DCP8 at 12 heads per rank), so those are zero-padded to 128.

use_prims_ts(vllm_config, kv_cache_dtype)

Whether FLASHINFER_MLA_SPARSE runs this model's sparse MLA on prims_ts.

"auto" uses prims_ts under DCP when it can run (faster than trtllm-gen for per-tensor fp8 prefill and decode on SM100 there) and trtllm-gen otherwise, including TP-only (DCP 1). "trtllm" keeps trtllm-gen for per-tensor fp8. nvfp4_ds_mla (no trtllm-gen kernel) always uses prims_ts. Raises if prims_ts is required but cannot run.