Repository navigation
[Model][DSv4.1] Store the whole KV in MXFP8 (FlashMLA V4.1 record) - #56893
Conversation
DeepSeek-V4.1 has been reusing DeepSeek-V4's paged fp8_ds_mla record: 448 fp8 e4m3 NoPE values, 64 bf16 RoPE values and 7 UE8M0 scales of 64 dims plus a pad byte, 584 B per token on 576 B pages. FlashMLA's V4.1 record (vllm-project/FlashMLA@b6ef01c, ModelType::V41) quantizes the RoPE dims as well: all 512 dims as fp8 e4m3 with one UE8M0 scale per 32 dims, i.e. MXFP8, for 512 data bytes + 16 scale bytes = 528 B per token on 512 B pages. Switch DeepSeek-V4.1 to it on SM100, where FlashMLA's sparse decode supports it; SM90, ROCm and the FlashInfer SM120 path keep the V4 record. Bump the FlashMLA pin to b6ef01c and follow the fork's tree reorganization (csrc/sm* -> csrc/kernels/sm*, the API implementations split into .cpp files). api.cpp now registers fused_norm_rope_attn_rope_cast_* and the two permute kernels, so their instantiations have to be compiled for _flashmla_C to link; the source list mirrors the fork's setup.py minus the dense backward, which is training-only. Update every writer and reader of the record: the fused SWA insert (new kv_mxfp8 argument, one scale per lane pair instead of per four), the compressed-cache insert (RoPE now has to be applied before the scales are picked, since the RoPE dims are quantized), and both gather paths, with the CuteDSL kernel parameterized by record rather than hard-coded to 448/64. The Triton and CuteDSL paths pick the record from k_cache.shape[-1], which is the per-token width the KV cache spec publishes. The indexer cache has to move to 512 B pages with them. _get_bytes_per_block makes a block's stride the sum of every page in the group, and DeepSeek-V4/V4.1 packs the SWA cache, the compressed-KV cache, the indexer cache and the compressor ring into one block, so divisibility only holds if every contributing page carries the same padding. That is what the indexer's 576 B alignment was for. Leaving it while the main caches moved to 512 produced a block stride of 212160 and tripped FlashMLA's block_stride_bytes % TMA_K_STRIDE == 0 assertion during the startup profile run. The record is also 9.6% smaller per token, and because block_size is always a multiple of 32 every page is an exact multiple of 512, so the page padding the V4 record needed disappears. Signed-off-by: Yongye Zhu <yongye@inferact.ai> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
/ci run |
|
β
Triggered Buildkite CI #88922 for commit |
|
/ci run |
|
β
Triggered Buildkite CI #88936 for commit |
ivanium
left a comment
There was a problem hiding this comment.
KV cache part LGTM. I'd prefer we can later refactor the alignment part to avoid too much hardcoded numbers like 576 and 512
| # FlashMLA packing stride; 512B for FlashInfer sparse (#44577). | ||
| alignment=self.packed_page_alignment if uses_fp8_ds_mla_layout else 512, |
There was a problem hiding this comment.
given that we now have self.packed_page_alignment, putting 512 here looks a bit hacky
4e14ec5 to
c0a11da
Compare
b6ef01c fails to build on vLLM CI's CUDA 13.0.3 toolkit: all 16 core_attn instantiations this PR adds trip two `static_assert`s in the discarded `else` of an `if constexpr`, which nvcc 13.0.88 instantiates anyway on the SM100 pass. vllm-project/FlashMLA#24 turns both into `else if constexpr`; SASS is byte-identical, so the record and its numerics are untouched. The bump also picks up vllm-project#23 (optional caller-provided FP8 output buffers). Its new `out_fp8` / `out_sf` arguments default to None and nothing in vLLM calls the op yet β these sources are compiled and linked, not invoked β so it is a no-op here. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Yongye Zhu <yongye@inferact.ai> Signed-off-by: Yongye Zhu <zyy1102000@gmail.com>
|
/ci run |
|
β This PR is 9 commits behind upstream |
|
/ci run |
|
β
Triggered Buildkite CI #88975 for commit |
β¦ache FlashMLA's mega-attention kernel does Q RoPE, sparse attention, the inverse RoPE of the output and its FP8 cast in one launch, writing straight into the buffer wo_a consumes. Add it as FlashMLAMegaAttnBackend (FLASHMLA_MEGA_ATTN_DSV41, SM100 only) with its own attention layer, and add the V4.1 NVFP4 compressed KV record the kernel can read, on top of the V4.1 MXFP8 record from vllm-project#56893. The attention interface grows two declared capabilities, both of which the base layer acts on rather than merely documenting: accepts_unnormed_unroped_query, where the kernel applies the Q norm and RoPE itself so the layer only publishes KV beforehand, and produces_inv_roped_quantized_output, where forward_mqa fills a QuantizedActivation and the projection is wo_a's einsum plus wo_b instead of _o_proj. Everything else keeps the bf16 output and _o_proj, so the FlashInfer and ROCm layers are unchanged. The output travels as a QuantizedActivation (kMxfp8Dynamic: fp8 e4m3 data, packed-ue8m0 MN-major scales). FlashMLA's fused ops accept caller-provided out_fp8 / out_sf buffers, so one buffer pair covers a whole step: the prefill chunks and the decode segment fill disjoint token ranges, and a single fp8_einsum over all N tokens replaces one call per segment. The scale buffer's head-dim stride is ceil4(N), which is both what the kernel requires of a caller buffer and what DeepGEMM expects for that N. wq_b rows and wo_a columns are permuted once at load into the kernel's chunk-interleaved layouts, so the surrounding GEMMs speak them directly. nvfp4_ds_mla now means the V4.1 MXFP8 sliding-window record beside a 288 B NVFP4 compressed record (256 B of e2m1 pairs, then 32 e4m3 scales of 16 dims). It rides the byte-width dispatch the fp8 records already use, so rope_quant_insert and dequantize_and_gather_k_cache pick it up with no call-site change; CuteDSL has no fp4 gather, so that record takes Triton. Each layer declares the packed record its kernel prefers, and an unspecific --kv-cache-dtype (auto / fp8) resolves to it, so the mega backend defaults to nvfp4_ds_mla while the split-KV backends keep fp8_ds_mla. An explicit --kv-cache-dtype fp8_ds_mla is still honoured on either. Decode always takes the mega kernel: benchmarks/kernels/benchmark_dsv41_mega_attn.py finds no batch size where falling back to split-KV decode pays, so there is no minimum-token threshold and no fallback path. The ratio that does decide whether the backend is worth selecting is live heads over padded heads: the kernel absorbs the inverse RoPE and the FP8 cast, whose cost scales with live heads, while its own cost scales with padded heads and it needs a separate Q-pad kernel. Measured on one GB300 under CUDA graphs, counting every per-step op both paths run, it is up to 1.47x faster at 64 live heads and a wash at 16 (TP4 on a 64-head model). Both numbers are decode; prefill is unmeasured. Signed-off-by: Yongye Zhu <yongye@inferact.ai> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
β¦llm-project#56893) Signed-off-by: Yongye Zhu <yongye@inferact.ai> Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Yongye Zhu <yongye@inferact.ai> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
β¦ache FlashMLA's mega-attention kernel does Q RoPE, sparse attention, the inverse RoPE of the output and its FP8 cast in one launch, writing straight into the buffer wo_a consumes. Add it as FlashMLAMegaAttnBackend (FLASHMLA_MEGA_ATTN_DSV41, SM100 only) with its own attention layer, and add the V4.1 NVFP4 compressed KV record the kernel can read, on top of the V4.1 MXFP8 record from vllm-project#56893. The attention interface grows two declared capabilities, both of which the base layer acts on rather than merely documenting: accepts_unnormed_unroped_query, where the kernel applies the Q norm and RoPE itself so the layer only publishes KV beforehand, and produces_inv_roped_quantized_output, where forward_mqa fills a QuantizedActivation and the projection is wo_a's einsum plus wo_b instead of _o_proj. Everything else keeps the bf16 output and _o_proj, so the FlashInfer and ROCm layers are unchanged. The output travels as a QuantizedActivation (kMxfp8Dynamic: fp8 e4m3 data, packed-ue8m0 MN-major scales). FlashMLA's fused ops accept caller-provided out_fp8 / out_sf buffers, so one buffer pair covers a whole step: the prefill chunks and the decode segment fill disjoint token ranges, and a single fp8_einsum over all N tokens replaces one call per segment. The scale buffer's head-dim stride is ceil4(N), which is both what the kernel requires of a caller buffer and what DeepGEMM expects for that N. wq_b rows and wo_a columns are permuted once at load into the kernel's chunk-interleaved layouts, so the surrounding GEMMs speak them directly. nvfp4_ds_mla now means the V4.1 MXFP8 sliding-window record beside a 288 B NVFP4 compressed record (256 B of e2m1 pairs, then 32 e4m3 scales of 16 dims). It rides the byte-width dispatch the fp8 records already use, so rope_quant_insert and dequantize_and_gather_k_cache pick it up with no call-site change; CuteDSL has no fp4 gather, so that record takes Triton. Each layer declares the packed record its kernel prefers, and an unspecific --kv-cache-dtype (auto / fp8) resolves to it, so the mega backend defaults to nvfp4_ds_mla while the split-KV backends keep fp8_ds_mla. An explicit --kv-cache-dtype fp8_ds_mla is still honoured on either. Decode always takes the mega kernel: benchmarks/kernels/benchmark_dsv41_mega_attn.py finds no batch size where falling back to split-KV decode pays, so there is no minimum-token threshold and no fallback path. The ratio that does decide whether the backend is worth selecting is live heads over padded heads: the kernel absorbs the inverse RoPE and the FP8 cast, whose cost scales with live heads, while its own cost scales with padded heads and it needs a separate Q-pad kernel. Measured on one GB300 under CUDA graphs, counting every per-step op both paths run, it is up to 1.47x faster at 64 live heads and a wash at 16 (TP4 on a 64-head model). Both numbers are decode; prefill is unmeasured. Signed-off-by: Yongye Zhu <yongye@inferact.ai> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Review follow-up to vllm-project#56893's mega-attention layer and to #13, which moved the Q pad into the input-preparation hook so MRV2 captures it. Do the same thing one launch cheaper: the pad belongs to the fused qnorm/RoPE/KV-insert kernel every other V4.1 layer already runs there, not to a Triton kernel of its own. That kernel grows a Q_FUSED_LAYOUT instantiation. A qElemOffset<> helper switches its Q load and store between the head-major layout and FlashMLA's mega-attention one -- 32 chunks of [heads, 16] per token, where the padding heads interleave with the live ones instead of following them -- and the RoPE gate becomes `isKV || !Q_FUSED_LAYOUT`, since that attention kernel rotates Q itself. kNumHeadsQPadded == 0 is now a valid instantiation, leaving one slot per token: the KV insert alone, which is what TP1 wants with its 64 live heads already at the kernel's head count. Only the three flag combinations that exist are instantiated, and the reduced grid is skipped when there are no Q slots to iterate. So the op takes q_fused_layout and reads q_head_padded == 0 as "KV insert only, empty return", _prepare_q_and_insert_kv collapses back into _fused_qnorm_rope_kv_insert, and pad_fused_q_heads and the Triton rope_quant_insert detour both go away with q_layout.py. The rest is review cleanup with no behaviour change. The interface contract's three ClassVars become documented properties, and produces_inv_roped_quantized_output goes: nothing read it, the dispatch being the _alloc_attn_out / _o_proj overrides, so its wording moves to _o_proj. _wo_a_einsum moves out of the base layer into the only _o_proj that calls it. The mega-attention wrappers leave vllm/v1/attention/ops/flashmla.py -- that file is untouched by this series again -- and the layer calls fused_norm_rope_attn_rope_cast_{fwd,decode} directly beside the constants and buffer helpers they need. is_flashmla_mega_attn_supported now answers FlashMLAMegaAttnBackend.supports_combination, where before only the benchmark called it, so an unbuilt kernel is rejected at backend selection. test_pad_fused_q_heads and #13's test_query_preparation_replays_padded_heads both exercised deleted code. Their coverage carries over to the op's own suite as test_q_fused_layout_pads_without_rope (live elements bit-exact, padding zero, both grid variants, and the KV-insert-only launch) and test_q_fused_layout_survives_graph_replay (a captured insert refills its allocated Q buffer over changed input, which is what #13 was protecting). Perf, on one GB300 under CUDA graphs, counting every per-step op both paths run (64-head model, topk_swa=128, topk_extra=512, NVFP4 compressed cache), as mega / split-KV microseconds: s_q 1 32 128 256 512 TP1 (64 live heads) 29/29 31/31 31/40 53/72 96/140 TP2 (32 live) 29/29 31/30 35/35 61/63 110/119 TP4 (16 live) 29/29 31/29 34/34 59/58 109/109 TP8 (8 live) 29/29 31/29 33/33 59/57 107/104 The win is TP1's: up to 1.45x at s_q=512 and 1.29x at 128, which is also where the fused insert skips the Q pad entirely. The kernel absorbs the inverse RoPE and the FP8 cast, whose cost scales with live heads, while its own cost scales with padded heads -- always 64, since FlashMLA's FP8 decode takes only h_q in {64, 128}. So from TP2 down the two cancel to within a few percent, and at TP4/TP8 the mega path is marginally behind at small s_q (0.93x at 32), where its padded-head cost has too little live-head work to absorb. This corrects TP1, and TP4/TP8 are a wash at best. Every row is decode; prefill is unmeasured. Tests, on one GB300: pytest tests/kernels/test_fused_deepseek_v4_qnorm_rope_kv_insert.py tests/kernels/test_compressor_kv_cache.py tests/kernels/test_dsv41_mega_attn_layouts.py -> 288 passed, 11 skipped No end-to-end model eval yet. Signed-off-by: Yongye Zhu <yongye@inferact.ai> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
β¦llm-project#56893) Signed-off-by: Yongye Zhu <yongye@inferact.ai> Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Yongye Zhu <yongye@inferact.ai> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
β¦llm-project#56893) Signed-off-by: Yongye Zhu <yongye@inferact.ai> Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Yongye Zhu <yongye@inferact.ai> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Purpose
DeepSeek-V4.1 has been reusing DeepSeek-V4's paged
fp8_ds_mlarecord. FlashMLA has a V4.1-specific one that quantizes the RoPE dims too; this switches DeepSeek-V4.1 onto it on SM100.ModelType::V4)ModelType::V41)Both records put a page's whole data region ahead of its whole scale region, so a page is
block_size * bytes_per_tokenrounded up to the TMA stride. Becauseblock_sizeis always a multiple of 32, every V4.1 page is an exact multiple of 512 β the padding the V4 record needed disappears, on top of the 9.6% smaller record.Gated on SM100:
ModelType::V41is absent from FlashMLA's SM90 sparse-decode feature set, so SM90, ROCm and the FlashInfer SM120 path keep the V4 record. No new cache dtype and no new flag β--kv-cache-dtype fp8resolves tofp8_ds_mlaas before and the record follows the arch, exactly asfp8_ds_mlaalready means 656 B for V3.2 and 584 B for V4.What changed
vllm-project/FlashMLA@b6ef01c, following the fork's tree reorganization (csrc/sm*βcsrc/kernels/sm*, API implementations split into.cppfiles).api.cppnow registersfused_norm_rope_attn_rope_cast_*and the two permute kernels, so their instantiations must be compiled for_flashmla_Cto link; the source list mirrors the fork'ssetup.pyminus the dense backward, which is training-only.kv_mxfp8argument (one UE8M0 scale per lane pair instead of per four, and the RoPE lanes quantize instead of storing bf16); the compressed-cache insert gets an MXFP8 kernel that applies RoPE before choosing scales, since the RoPE dims are now quantized.k_cache.shape[-1]β the per-token width the KV cache spec already publishes β so the V4 record keeps working unchanged on the same code path.The indexer page has to move with them
_get_bytes_per_blockmakes a block's stride the sum of every page in the group, and DeepSeek-V4/V4.1 packs the SWA cache, the compressed-KV cache, the indexer cache and the compressor ring into one block. Divisibility by the TMA stride therefore only holds if every contributing page carries the same padding β which is what the indexer cache's 576 B alignment was for, rather than any constraint of its own kernel.Leaving the indexer at 576 while the main caches moved to 512 produced a block stride of 212160 (a multiple of neither) and tripped FlashMLA's
block_stride_bytes % TMA_K_STRIDE == 0assertion in the startup profile run, after a clean weight load. Every per-spec page size looked correct in isolation. This PR ties the indexer's alignment to the same gate.Relationship to other open PRs
[Perf] Deepseek V41 Attention MegaKernel, same author) integrates FlashMLA PR Add half rmsnorm kernelΒ #221's fused norm+RoPE+attention megakernel behind--attention-config '{"dsv4_fused_attention": true}'and adds annvfp4_ds_mladtype pairing a 528 B SWA cache with a 288 B fp4 compressed cache. This PR is the narrower, unconditional half: it moves DeepSeek-V4.1's existingfp8_ds_mlacache to the V4.1 record with no opt-in and no new dtype, and it does not touch the attention path. The two overlap onflashmla.cmake(that one pinsc112cc1, this oneb6ef01cβ one commit later, a test-only change in the fork) and on the V4.1 writers/readers; whichever lands first, the other rebases onto it. [Perf] Deepseek V41 Attention MegaKernelΒ #56344 does not currently touchDeepseekV4IndexerCache.get_kv_cache_spec, so the alignment fix above is worth carrying across either way.[Perf][DSpark] Add KV-only context insertion across V4.1 cache formats) touches the same threecsrc/libtorch_stable/files to add a KV-only wrapper op. The changes are disjoint in intent (its wrapper drops the dummy query; this adds a record selector) but will conflict textually on the op signature and bindings.Not duplicating either: neither makes the V4.1 MXFP8 record the layout DeepSeek-V4.1 actually runs with by default, and neither carries the indexer-alignment fix.
Test Plan & Result
Three new tests cover the record: a byte-exact check of the compressed-cache insert against a reference quantizer derived from FlashMLA's
tests/quant.py, an insertβgather round trip (which also exercises the CuteDSL path), and the CUDA op's MXFP8 branch against the Triton encoder.Model evaluation
DeepSeek-V4.1-Flash (revision
517ef625), TP4 on one GB300,--kv-cache-dtype fp8 --block-size 128 --max-model-len 65536, no speculative decoding. Baseline is the same weights/TP/node on the V4 584 B record.0 request errors on both. GPQA truncated 3/792 rows; answered-only accuracy 0.9062. Quantizing the 64 RoPE dims costs nothing measurable.
Comparability caveat: the baselines were taken with the
deepseekv41-flash-0909image at gpqaepoch_num: 1, this run with a local build atepoch_num: 4, so the arms differ in image and epoch count as well as in the record. The gsm8k arm at temperature 0 is the tight comparison and it is clean.Engine-level evidence beyond the scores: FlashMLA hard-errors on any byte count that is not 584/528/288 for
d_qk=512, so a clean startup β memory profiling, KV allocation (161.73 GiB, 11,589,848 tokens) and full CUDA-graph capture of the sparse decode path β is itself proof the 528 B record was accepted asModelType::V41.AI assistance
This change was developed with AI assistance (Claude Code). The FlashMLA pin bump, the record change across the writers/readers, the indexer-alignment fix, the new tests and the evaluation runs above were produced in that session and reviewed by me.
π€ Generated with Claude Code