Skip to content

vllm.v1.attention.backends.mla.flashinfer_mla_sparse_sm90

FlashInfer sparse MLA backend for SM90 (Hopper) NoPE models.

Wraps FlashInfer's BatchMLAPagedAttentionWrapper (FA2/FA3 paths), which as of FlashInfer 0.6.18 supports head_dim_kpe=0 (GLM-5.3-Flash NoPE MLA) and FP8 E4M3 KV caches on SM90 with in-kernel dequantization: the FP8 cache is read directly (half the bf16 HBM traffic) and converted to BF16 in shared memory, while queries stay BF16 (no query quantization).

Sparsity rides the same trick the FA-based sparse backend uses: with page_size=1 the per-token top-k slot indices ARE the page table, so each query token becomes one varlen batch row whose kv_indices slice is its top-k row and whose kv_len is its valid count. Causality is already encoded by the indexer's selection, so causal=False.

CUDA-graph handling: plan() copies its inputs to host unconditionally, so it must stay outside graph capture. Each metadata builder owns a wrapper, reserved capture-stable device buffers, and the plan parameters. The wrapper bakes the per-row kv_len into its int schedule at plan() time — run() never reads the device-side buffer. The builder plans (outside capture) from sync-free host upper bounds and clamps each scheduled work item's kv_end on device to the exact valid count, so the kernel never reads past a row's valid prefix (the -1 tail of the converted index buffer) and async scheduling needs no D2H sync. Per-step content (top-k slots) is written into the reserved buffers by kernels inside the captured forward, and captured runs read the refreshed plan buffers on replay.

KV cache format: E4M3 [num_blocks, block_size, 512] (any block stride) (uint8 storage) with a per-tensor k_scale; BF16 caches also work. The per-token x 128-channel-group ckv_scale_arr layout is supported by the kernel but not wired yet (it needs a group-quantizing cache-write op).

Classes:

FlashInferMLASparseSM90Builder

Bases: FlashInferMLASparseMetadataBuilder

Reuse the common sparse metadata (req ids, topk buffer access).

_kv_lens_host(cam)

Host per-row KV lengths for the plan and whether they are exact.

A row for the j-th query token of request i attends seq_lens[i] - q_len[i] + j + 1 tokens. The indexer's selection then bounds the valid count: contexts up to index_topk select everything (valid == context); longer contexts keep the top index_topk pool-expanded tokens plus the trailing incomplete pool (valid == index_topk + context % index_kpool). Both match the count of non -1 entries the convert kernel produces.

With seq_lens_cpu_upper_bound (optimistic under async spec decode) returns sync-free upper bounds, which the plan clamps on device. Otherwise returns exact lengths via a D2H copy.

_SM90State

Builder-owned wrapper, capture-stable buffers, and plan parameters.

One instance serves every MLA layer in an attention group because the plan depends only on the batch shape, not the layer.

Methods:

  • plan –

    Plan per-row KV lengths (CPU int32, [num_tokens]).

plan(num_tokens, kv_lens, cam, req_id_per_token, row_ids=None)

Plan per-row KV lengths (CPU int32, [num_tokens]).

The wrapper bakes kv_len into its int schedule from host values, so rows past their valid count would send the kernel into the -1 tail of the converted index buffer. With cam and req_id_per_token (device batch layout), kv_lens only needs to upper-bound the valid counts: the plan is padded by _PLAN_SLACK and reused while the bound still fits, and every call clamps each work item's kv_end on device to the exact count, so no D2H sync is needed. Without them the lengths must be exact. Must run outside CUDA graph capture: the in-place refreshed plan_info/indptr buffers are what captured runs read.