Skip to content

vllm.v1.attention.backends.mla.flashmla_sparse

Classes:

Functions:

Attributes:

MIN_TOKENS_FOR_DCP_TOPK_LENGTH = 32 module-attribute

NOTE: FlashMLA Sparse uses an fp8 cache with the following format

For DeepSeek V3.2, in the "FP8 with scale" format, each token's KV cache is 656 Bytes, structured as: - First 512 bytes: The "quantized NoPE" part, containing 512 float8_e4m3 values. - Next 16 bytes: Scale factors, containing 4 float32 values. The first float32 is the scale for the first 128 float8_e4m3 values, the second for the next 128, and so on. - Last 128 bytes: The "RoPE" part, containing 64 bfloat16 values. This part is not quantized for accuracy.

For DeepSeek V4, in the "FP8 with scale" format, each token's KV cache is 584 Bytes, structured as: - First 448 bytes: The "quantized NoPE" part, containing 448 float8_e4m3 values. - Next 128 bytes: The "RoPE" part, containing 64 bfloat16 values. This part is not quantized for accuracy. - Last 8 bytes: Scale factors, containing 7 ue8m0 values + 1B pad. The first ue8m0 is the scale for the first 64 float8_e4m3 values, the second for the next 64, and so on.

For DeepSeek V4.1, each token's KV cache is 528 Bytes, structured as: - First 512 bytes: all 512 dims as float8_e4m3. Unlike V4 the 64 RoPE dims are quantized too, so there is no bfloat16 part. - Last 16 bytes: Scale factors, containing 16 ue8m0 values, one per 32 consecutive float8_e4m3 values (i.e. MXFP8).

The V4 record is not laid out token-by-token within a page: a page holds all its data rows first and all its scale rows after, so a page is block_size * bytes_per_token bytes rounded up to the decode kernel's 576 B TMA stride. The V4.1 records are interleaved: each token's scales directly follow its own data, and the decode kernel's TMA stride is the record width.

In the "nvfp4_ds_mla" format (SM100 only, DeepSeek V3.2 geometry), each token's KV cache is 352 Bytes, structured as: - First 256 bytes: 512 e2m1 NoPE values packed 2/byte (low nibble = even element). - Next 64 bytes: 64 float8_e4m3 RoPE values. These carry no scale factor: e4m3's 4 exponent bits span the RoPE magnitude range unaided. - Last 32 bytes: 32 float8_e4m3 NoPE scale factors, one per 16 elements, stored permuted (an 8x4 -> 4x8 transpose: the scale for element block s lives at byte 8 * (s & 3) + (s >> 2)) so that the 8 scales one FlashMLA dequant thread needs are contiguous. See the layout comment in csrc/cache_kernels.cu.

FlashMLASparseImpl

Bases: SparseMLACommonImpl[FlashMLASparseMetadata]

_forward_fp8_kv_mixed_batch(q, kv_c_and_k_pe_cache, topk_indices, attn_metadata, kernel_metadata=None)

Mixed batch FP8 forward path that treats all tokens as one batch.

This is equivalent to main branch's approach and avoids the BF16 prefill kernel which has head padding overhead when num_heads is small. Used when use_mixed_batch is True.

The lse is only returned when DCP needs it, otherwise None.

_forward_fp8_kv_separate_prefill_decode(q, kv_c_and_k_pe_cache, topk_indices, attn_metadata)

The lse covers the decode rows and is only returned under PCP+DCP.

_gather_prefill_chunk(chunk, shard, topk_indices, attn_metadata, prefill_meta)

All-gather this rank's upconverted KV shard so the chunk's rows attend the whole context, and map their top-k onto the rank-major result.

FlashMLASparseMetadata dataclass

Bases: SparseMLACommonMetadata

Classes:

FP8SeparatePrefillDecode dataclass

Classes:

Prefill dataclass

Classes:

  • Chunk –

    Metadata for a chunk of prefill requests.

Chunk dataclass

Metadata for a chunk of prefill requests.

Prefill requests may be chunked to fit within the fixed workspace size.

FlashMLASparseMetadataBuilder

Bases: SparseMLACommonMetadataBuilder[FlashMLASparseMetadata]

_build_fp8_mixed_decode_prefill()

Build FP8 metadata treating MQA tokens as one batch.

The scheduler initializes lazily from the runtime query shape, which may be the full batch or only decodes when prefills use dense MHA. This avoids the BF16 prefill kernel's head-padding overhead at high TP.

_fp8_kv_pages(kv_cache, block_size)

The cache as the fp8 kernel's 64-token pages, and the block stride in rows when a block spans several pages.

fp8_mixed_batch_chunk_tokens(num_heads)

Tokens per mixed-batch kernel call. FP8_MIXED_BATCH_CHUNK_TOKENS is for 64 padded heads; ShardQ calls attend every head (up to 128 padded), so they take proportionally fewer tokens at the same transient memory.

gathered_prefill_shards(row_req_idx, row_shard_rows)

Under PCP+DCP, group this rank's prefill rows by request and take each request's slice of this rank's KV shard.

PCP gives a rank two adjacent chunk rows of a split prefill; they share one context, so the workspace holds it once. row_shard_rows is CommonAttentionMetadata.dcp_local_seq_lens_cpu_upper_bound: the largest DCP shard of the whole request on every row, identical on every PCP rank, so the all-gather is shaped the same everywhere.

Returns the request row bounds and, per request, its shard rows.