Repository navigation
[Bugfix] MLA decode: merge only the splits gfx950 fp8 gqa16 stage 1 writes - #7613
Conversation
…rites The gfx950 kernel mla_a8w8_qh16_qseqlen1_gqaratio16 deals KV to the splits in 128-token tiles. A split with no tile is not written. Stage 2 used mgc = 64, so it could merge unwritten splits. Set mgc = 128 for this kernel. Extend the split-merge op test to fp8 gqa16 and check LSE too. Co-authored-by: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
|
LGTM. Same issue as bf16 gqa32: stage2 must not merge empty splits. Setting mgc=128 for fp8 qh16 qlen1 matches SUB_KV=128. Nice that the NaN-fill test was generalized. |
ChuanLi1101
left a comment
There was a problem hiding this comment.
LGTM. Same issue as bf16 gqa32: stage2 must not merge empty splits. Setting mgc=128 for fp8 qh16 qlen1 matches SUB_KV=128. Nice that the NaN-fill test was generalized.
ChuanLi1101
left a comment
There was a problem hiding this comment.
LGTM. Same issue as bf16 gqa32: stage2 must not merge empty splits. Setting mgc=128 for fp8 qh16 qlen1 matches SUB_KV=128. Nice that the NaN-fill test was generalized.
|
@AndreasKaratzas Thanks for the PR! LGTM. Could you please resolve the merge conflicts in op_tests/test_mla.py so we can get this merged? |
Co-authored-by: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
719494f
|
@kaiyang-1 Done :) Thanks for the review |
Summary
On gfx950, fp8 MLA decode with 16 heads and one query token can merge KV splits that stage 1 never wrote. Output and LSE then come from
torch.emptymemory. This PR setsmgc = 128for this kernel, so stage 2 merges only the splits that stage 1 writes.Motivation
The non-persistent kernel
mla_a8w8_qh16_qseqlen1_gqaratio16deals KV to the splits round-robin in 128-token tiles. A split that gets no tile is not written. It also does not writevalid_split_count. Stage 2 (_fwd_kernel_stage2_asm) mergesmin(num_kv_splits, cdiv(kv_len, mgc))splits, andmgcis 64 for 16 heads. So a row withkv_lenin (64, 128] and 2 or more splits merges split 1, which has no data. A row withkv_lenin (128, 192] and 3 or more splits merges split 2, and so on.Evidence, one row, 16 heads, fp8 Q and KV,
return_lse=True, aiter main 8e69f10:kv_len1..400 with 4 splits, with the split buffers pre-filled with NaN: 24 of 400 rows are wrong. Example: 111 tokens, output relative error 1.0, LSE error 77.This is the same bug as #7182 (bf16 gqa32, 32-token tiles) for a different kernel.
Changes
aiter/mla.py:mgc = 128for gfx950, fp8 Q, fp8 KV, 16 heads,max_seqlen_q == 1. Other cases do not change.op_tests/test_mla.py:check_bf16_gqa32_merges_only_written_splitsbecomescheck_merges_only_written_splits(dtype, nhead, tile). It now also asks for LSE and checks that LSE is finite and close to the reference.main()runs it for bf16 gqa32 (as before) and, on gfx950, for fp8 gqa16 with 128-token tiles.Performance (if applicable)
Stage 1 does not change. Stage 2 merges fewer splits only for rows where the extra splits had no data.
mla_decode_fwd, fp8, 16 heads, qlen 1,return_lse=True, splitsmax(get_meta_param(...), 2),run_perftest200 iterations, MI350X. Two runs with the fix, one on main:Testing
Tested on MI350X (gfx950), ROCm 7.15 (TheRock SDK 10.0.0), PyTorch 2.13.0+rocm10.0.0, aiter main 8e69f10.
The branch is rebased onto main 581579a. The new commits on main do not change
aiter/mla.py,op_tests/test_mla.pyor the fp8 gqa16 kernel. After the rebase, the bf16 gqa32 and fp8 gqa16 checks passed 3 of 3 runs.lsenot finite), passes with the fix. fp8 and bf16 checks together passed 10 of 10 runs in a row.kv_len1..400 sweep, one row, NaN-filled split buffers: 4 splits 24 wrong rows on main, 0 with the fix. vLLM split choice (max(get_meta_param(...), 2)) 0 wrong rows with the fix.lse_batch_check.py 16 100 400): the worst single-row error goes from 1.24 (67 tokens) to 0.0125, which is normal fp8 noise. The batched call also stays at about 0.015.python3 op_tests/test_mla.pyand the extra CI argument sets: exit code 0 with the fix forpython3 op_tests/test_mla.pyand for all 6 extra argument sets inaiter_test.sh.Documentation
The API does not change. Results that were wrong are now correct.
Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
TODO: paste the head SHA from the PR Self-Review comment.
Behavior and intent
Before: stage 2 used
mgc = 64for the gfx950 fp8 gqa16 qlen1 kernel, which uses 128-token tiles. Rows shorter thannum_kv_splits * 128could merge splits with no data. After:mgc = 128, so stage 2 merges the same splits that stage 1 writes.Validation and unverified scope
Commands and results are in "Testing" above. Not verified: gfx942. The change is gated to gfx950 because we could only measure the gfx950 tile size. gfx942 keeps
mgc = 64. Other fp8 gqa16 kernels (max_seqlen_q2 and 4) are not changed and not checked here.Separate issue, not fixed here: the same kernel sometimes writes a wrong partial output for a split with 256 to 383 tokens when the caches are cold (for example after a large memset). The split LSE stays correct. This happens on main and on v0.1.27, with and without this fix. It needs a kernel change; we will report it separately.
Separate issue, not fixed here: with
num_kv_splits == 1, this kernel does not write LSE, soreturn_lse=Truereturnstorch.emptymemory (128 of 400 rows wrong in the auto-split sweep, on main and with this fix). #6971 fixes the same problem for bf16 QH64/QH128 by forcing 2 splits.PR-specific question
TODO: answer the question from the PR Self-Review comment.
Changed op tests
op_tests/test_mla.py: the split-merge check now takes the dtype, head count and tile size, and checks LSE as well as output. The bf16 gqa32 case is the same as before plus the LSE check. The new fp8 gqa16 case is the only test that runs this kernel with more splits than a row has tiles.AI assistance (Claude) was used to find the cause, write the fix and the test, and run the validation.