Skip to content

Fix fp8 sparse MLA folded batch mapping - #3310

Merged
XiaobingSuper merged 2 commits into
ROCm:mainfrom
XiaobingSuper:fix-fp8-sparse-mla-batch-mapping
May 23, 2026
Merged

XiaobingSuper merged 2 commits into
ROCm:mainfrom
XiaobingSuper:fix-fp8-sparse-mla-batch-mapping

Conversation

@XiaobingSuper

@XiaobingSuper XiaobingSuper commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Fix persistent sparse MLA metadata for folded fp8 decode shapes by mapping virtual folded work items back to the original KV batch.
  • Preserve the fast single-kernel decode path for TP4 DeepSeek V3.2 fp8 KV cache instead of splitting into two 16-head kernel launches.
  • Use the original KV batch when reading kv_last_page_lens, matching the corrected work_info.batch_idx mapping.

Problem

DeepSeek V3.2 with TP4 and --kv-cache-dtype fp8 has 32 local query heads. On gfx950, the fast persistent sparse MLA decode path does not natively handle fp8/fp8, nhead=32, q_len=1 as a direct 32-head tile. To keep one efficient decode launch, AITER folds the shape from:

[tokens, 32 heads, dim]

to:

[tokens * 2, 16 heads, dim]

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:

virtual entry 0 -> sparse KV token 0
virtual entry 1 -> sparse KV token 1  # wrong; should still be token 0

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:

curr_batch_kv = curr_batch / ori_seqlen_qo / qk_batch_ratio

This makes all folded head groups for one original query token use the same sparse KV batch. It also uses curr_batch_kv when indexing kv_last_page_lens, keeping last-page handling consistent with work_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 main and the AITER module_mla_metadata JIT artifact was deleted so the unfixed metadata kernel rebuilt from source.

Config Filter Before fix exact_match After fix exact_match
TP4 + fp8 KV flexible-extract 0.0076 +/- 0.0024 0.9507 +/- 0.0060
TP4 + fp8 KV strict-match 0.0008 +/- 0.0008 0.9500 +/- 0.0060
TP8 + fp8 KV flexible-extract not affected 0.9530 +/- 0.0058
TP8 + fp8 KV strict-match not affected 0.9522 +/- 0.0059

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
  • Unit-level comparison between single folded decode call and two 16-head baseline calls: max absolute difference = 0
  • DeepSeek V3.2 TP4 + --kv-cache-dtype fp8, GSM8K 20-shot accuracy before fix
  • DeepSeek V3.2 TP4 + --kv-cache-dtype fp8, GSM8K 20-shot accuracy after fix
  • DeepSeek V3.2 TP8 + --kv-cache-dtype fp8, GSM8K 20-shot accuracy after fix

Companion PR

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.
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 3310 --add-label <label>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 virtual curr_batch using ori_seqlen_qo (sparse) and qk_batch_ratio.
  • Write work_info.batch_idx = curr_batch_kv so downstream kernels read per-KV-batch metadata consistently.
  • Index params.p_kv_last_page_lens using curr_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]>
@XiaobingSuper

Copy link
Copy Markdown
Contributor Author

Follow-up note on the folded batch mapping:

The curr_batch / ori_seqlen_qo / params.qk_batch_ratio remapping is intentionally limited to the Traits::kIsSparse metadata path.

For sparse MLA metadata, curr_batch is a virtual query-side work item. The sparse path expands each original KV batch by the original query length, and the folded fp8 path can further expand it by qk_batch_ratio when multiple virtual 16-head groups represent the same original query token. Therefore the original KV batch is:

curr_batch_kv = curr_batch / ori_seqlen_qo / params.qk_batch_ratio;

That original KV batch is the correct index for work_info.batch_idx and p_kv_last_page_lens because downstream decode code uses those fields to read per-KV-batch metadata. Without this, the second folded head group can read sparse KV metadata from the next token/batch.

For non-sparse metadata, work_info.batch_idx already uses the virtual folded batch contract. Changing it to curr_batch / qk_batch_ratio collapses distinct virtual folded work items and can disturb existing non-sparse folded paths such as bf16/bf16 nhead=48. The latest commit restores non-sparse behavior to curr_batch while keeping the sparse original-KV-batch remapping.

Focused validation in vllm_atom_qwen3_test using /workdir/aiter_0521:

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 32

Both completed with exit code 0 after rebuilding module_mla_metadata.

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 3310 --add-label <label>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants