Repository navigation
Fix fp8 sparse MLA folded batch mapping - #3310
Conversation
Map folded fp8 sparse MLA work items back to their original KV batch so TP4 32-head decode preserves the fast single-kernel path without reading the next token's sparse KV.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
There was a problem hiding this comment.
Pull request overview
Fixes persistent sparse MLA metadata generation for fp8 decode shapes that use folded batch/head mapping (e.g., TP4 DeepSeek V3.2 with 32 local heads folded into 2×16-head virtual entries). The change ensures folded “virtual” work items map back to the correct original KV batch when writing work_info.batch_idx and when indexing kv_last_page_lens, preventing incorrect sparse KV range selection (and avoiding potential OOB indexing when num_batches is multiplied by qk_batch_ratio).
Changes:
- Compute an “original KV batch” index (
curr_batch_kv) from the virtualcurr_batchusingori_seqlen_qo(sparse) andqk_batch_ratio. - Write
work_info.batch_idx = curr_batch_kvso downstream kernels read per-KV-batch metadata consistently. - Index
params.p_kv_last_page_lensusingcurr_batch_kv(not the folded/virtual batch).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Limit the folded batch remapping to sparse MLA metadata so existing non-sparse folded paths keep their virtual batch indexing contract. Co-authored-by: Cursor <[email protected]>
|
Follow-up note on the folded batch mapping: The For sparse MLA metadata, curr_batch_kv = curr_batch / ori_seqlen_qo / params.qk_batch_ratio;That original KV batch is the correct index for For non-sparse metadata, Focused validation in python3 op_tests/test_mla_persistent.py -n 48,1 -d bf16 -kvd bf16 -c 21 -b 1 3 -blk 1 -ms 32
python3 op_tests/test_mla_sparse.py -n 32,1 -d fp8 -kvd fp8 -c 21 -b 3 -qn 512 -blk 1 -ms 32Both completed with exit code 0 after rebuilding |
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
Summary
fp8KV cache instead of splitting into two 16-head kernel launches.kv_last_page_lens, matching the correctedwork_info.batch_idxmapping.Problem
DeepSeek V3.2 with TP4 and
--kv-cache-dtype fp8has 32 local query heads. On gfx950, the fast persistent sparse MLA decode path does not natively handlefp8/fp8, nhead=32, q_len=1as a direct 32-head tile. To keep one efficient decode launch, AITER folds the shape from:to:
Those two virtual folded query entries are two head groups for the same original query token, so both must read the same sparse KV token range.
Before this fix, sparse metadata used the folded virtual batch index directly in some paths. For TP4 this effectively mapped:
The second 16-head group could therefore read sparse KV pages from the next token. That produced a severe accuracy collapse with TP4 + fp8 KV cache.
Why This Fix Is Correct
The mathematical attention computation is unchanged. This patch only fixes metadata bookkeeping for the folded representation.
For sparse folded metadata, the KV batch should be derived from the original token index, not from the folded virtual query entry. The corrected mapping divides by both the original q length and the q/head folding ratio:
This makes all folded head groups for one original query token use the same sparse KV batch. It also uses
curr_batch_kvwhen indexingkv_last_page_lens, keeping last-page handling consistent withwork_info.batch_idx.This preserves the intended high-performance single-call decode path. The earlier workaround of launching two 16-head calls was numerically correct but regressed performance by adding an extra kernel launch and duplicated decode setup.
Accuracy Validation
GSM8K was run with DeepSeek V3.2,
--kv-cache-dtype fp8, local completions, and 20-shot evaluation.Before/after TP4 was measured by running the same TP4 + fp8 KV server and evaluation flow. For the before-fix run, ATOM/AITER were temporarily switched back to
mainand the AITERmodule_mla_metadataJIT artifact was deleted so the unfixed metadata kernel rebuilt from source.TP8 is included as a guardrail. TP8 has 16 local heads, which is already a native fast-path shape, so it does not require the TP4 32-head folded mapping.
Test Plan
git diff --check--kv-cache-dtype fp8, GSM8K 20-shot accuracy before fix--kv-cache-dtype fp8, GSM8K 20-shot accuracy after fix--kv-cache-dtype fp8, GSM8K 20-shot accuracy after fixCompanion PR