Repository navigation
[ASM] Fix MFMA operand race in gfx950 FP8 QH16 qseqlen4 v3 MLA decode - #7623
AndreasKaratzas wants to merge 3 commits into
Conversation
Waves 2 and 3 of the qseqlen4 v3 persistent kernels run a second copy of the main loop. In that copy, the six ds_read_b128 that load a[104:127] for the next step come before the MFMA that still reads the old a[120:127]. There is no s_waitcnt between them, so the MFMA reads old or new data, depending on LDS latency. Queries 2 and 3 then get wrong output. The first copy of the loop has the MFMA before the reads. In the 4 quarters of the second loop copy, move the MFMA in front of the ds_reads, as in the first copy. This is an 8-byte rotation inside straight-line code with no label, branch or wait. The MFMA does not depend on the instructions that it moves past. All 8 variants (v3_ps, lse, msk0, cprr and their combinations) have the same code and get the same change. The file size, the ELF layout and the branch targets do not change. Add check_fp8_gqa16_multi_tile_batch to test_mla_persistent.py. It runs 128 rows of 3 KV tiles next to a copy on a side stream, with and without LSE. On main it fails in every run. With these .co files it passes. 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: |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
|
Please:
|
#7872 rebuilt four of the FP8 QH16 qseqlen4 v3_ps kernels (v3_ps, lse_v3_ps, cprr_v3_ps, lse_cprr_v3_ps) with the KV page-table clamp. Resolve each by taking main's binary and re-applying this branch's rotation (the a[120:127] MFMA moved ahead of the six ds_read_b128 that overwrite a[104:127], 4 windows of 76 bytes per file). main vs merged differs only by those rotations; file size, ELF headers and branch targets are unchanged. The msk0 variants were not touched by main and keep this branch's versions. op_tests/test_mla_persistent.py: run both the qlen 1 (#7622) and qlen 4 multi-tile batch checks. Co-Authored-By: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
Updated with main, verified on MI355X (gfx950)Conflict with #7872. #7872 rebuilt 4 of the 8 kernels in this PR from SP3 sources with the KV page-table clamp:
MI355X results (one GPU, merged
As a control, the pre-#7872 AI assistance (Claude) was used for the binary re-patch and the verification scripts. |
|
@ChuanLi1101 thanks for the review. All four points are addressed:
Could you take another look when you have a moment? |
The regenerated gfx950 MLA qh16/qseqlen4 code objects were committed as 100644, while main and the other .co files under hsa/ are 100755. Restore the original mode so this PR only changes file contents. Co-Authored-By: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
|
@ChuanLi1101 the four points from your review are addressed (see #7623 (comment)); I also restored the executable bit on the .co files. Could you take another look? |
ChuanLi1101
left a comment
There was a problem hiding this comment.
Could you confirm: (1) whether the cprr and msk0 variants can get the same MI355X runtime checks as v3_ps and lse_v3_ps, and (2) whether someone with ASM background can review the rotation windows?
|
yeah, ran them on MI350X, 10 runs each. all 8 are reachable (cprr when
latency's the same. for the asm side, the disasm diff is just the 4 rotations per file and nothing else, |
|
@minmengdie @shengnxu could one of you look at the asm side? it's the 4 rotation windows per .co (the mfma moved ahead of the six ds_read_b128 that overwrite a[104:127]), plus the prologue lgkmcnt(4) pattern i left alone. this also still needs a team_aiter approval to merge. @ChuanLi1101 on your question: all 8 variants (v3_ps, cprr, msk0, msk0_cprr, each with and without lse) ran 10x on MI350X. on MI355X only v3_ps and lse_v3_ps ran, so cprr and msk0 have no MI355X run yet. |
Summary
This PR fixes a register race in the gfx950 FP8 QH16 qseqlen4 v3 persistent MLA decode kernels (
hsa/gfx950/mla/mla_a8w8_qh16_qseqlen4_gqaratio16*_v3_ps.co, 8 variants). In the loop copy that waves 2 and 3 run, an MFMA reads a[120:127] after LDS reads that overwrite a[120:127] have started. The PR moves the MFMA in front of the reads, as in the first loop copy. It also adds a regression check toop_tests/test_mla_persistent.py.Motivation
These kernels run persistent fp8 MLA decode with 16 heads and 4 query tokens per row, for example MTP speculative decode with 3 draft tokens. Queries 2 and 3 of some rows get wrong output. The error is silent. With
return_lse=Trueit is very frequent: 299 of 300 calls (128 rows each) had wrong rows under load, and 300 of 300 with no other work on the GPU.check_fp8_gqa16_multi_tile_batch(from #7622, now on main): 128 rows,kv_len257..383 (3 KV tiles), with a copy on a side stream. Wrong (row, query, head) outputs per run:qseqlen4_gqaratio16_v3_psqseqlen4_gqaratio16_lse_v3_psRoot cause, from the disassembly. Waves 0 and 1 run one copy of the main loop, and waves 2 and 3 run a second copy. In each of the 4 quarters of the first copy:
In the second copy, the 6
ds_read_b128come first, then 5v_exp_f32, then the MFMA. There is nos_waitcntbetween them. The MFMA must read the old a[120:127], as in the first copy. But when the LDS reads return before the MFMA reads its operands, it reads the new data. This depends on LDS latency, so it changes from call to call. A static check of the LGKM wait counters along the loop paths finds this pattern in the second loop copy only (4 sites per variant).Related work: #7007 reports wrong, nondeterministic output of
qseqlen2_gqaratio16_psandqseqlen4_gqaratio16_v3_ps(query length 3 and 4). This PR fixes the qseqlen4 part. A separate PR fixes the qseqlen2 part (a different bug).Related: #7617 fixes the same class of race in the non-persistent qseqlen1 kernel. #7622 (qseqlen1 persistent, merged) added the shared
check_fp8_gqa16_multi_tile_batchhelper toop_tests/test_mla_persistent.py; this PR and #7621 (qseqlen2) only add calls to it. #7007 reports wrong results from the qseqlen2 and qseqlen4 kernels, and #4872 would route fp8 gqa16 qlen 2 to the qseqlen4 kernel. A separate bug is not fixed here: with more than one KV split per row, a few rows are still wrong on main and with these patches (stage 2 appears to merge split buffers that stage 1 did not write).Changes
mla_a8w8_qh16_qseqlen4_gqaratio16_{,lse_,msk0_,msk0_lse_}{,cprr_}v3_ps.co, binary patch. The SP3 source is not in the repo. In the 4 quarters of the second loop copy, the MFMA moves up by 11 instructions, to the place right after the previous MFMA. The 6ds_read_b128and 5v_exp_f32move down by 8 bytes. The moved range is straight-line code with no label, branch or wait. The MFMA does not read or write any register that the moved instructions write, except a[104:127], which it must read before the LDS reads. (v3_ps: the MFMA moves from.text0x623c, 0x6a4c, 0x725c, 0x7a6c to 0x61f8, 0x6a08, 0x7218, 0x7a28.) The file size, the ELF layout and the branch targets do not change.{,lse_,cprr_,lse_cprr_}v3_ps) are main's KV page-table clamp rebuild with this rotation applied again; the instruction diff against main is exactly the 4 moved MFMAs per file. The 4msk0_*files were not touched by [ASM] Fix MLA PS mode nan error for kv page0 has nan data and the LTD OOB read page0 data #7872.op_tests/test_mla_persistent.py:main()calls the existingcheck_fp8_gqa16_multi_tile_batchon gfx950 for qlen 4, with and without LSE (3 lines).The kernel owners may prefer to rebuild the
.cofiles from SP3 with the first-copy order in both loop copies. The new check covers that rebuild too.Performance (if applicable)
mla_decode_fwd, fp8, 16 heads,run_perftest200 iterations, kernel time in us (median over runs). Before is main, after is this PR. 6 interleaved rounds on each of 2 GPUs (12 pairs per shape). "Change" is the median of the per-pair ratios. Other jobs ran on the node at the same time, so single pairs vary a lot; the median is the useful number.All medians are within -0.2% and +1.6%. The patch moves one instruction in each of 4 places and adds none.
Testing
The
.cofiles are gfx950 only, so MI250X and MI300X do not apply.Documentation
The change fixes a kernel bug. The API and the behavior do not change.
Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
b3ac1fa
Behavior and intent
In the loop copy of waves 2 and 3, the MFMA now reads a[120:127] before the LDS reads for the next step overwrite it, as in the first loop copy. Before, queries 2 and 3 of some rows were wrong and nondeterministic.
Validation and unverified scope
MI350X (gfx950), PyTorch 2.13.0+rocm10.0.0, aiter main 581579a.
Heads with error > 0.1 (128 rows,
kv_len257..384, 300 calls per run):.cov3_ps,streamlse_v3_ps,streamlse_v3_ps, no other works_waitcntset to 0In the last row, making every wait strict on top of the fix does not change the result in these runs.
Variants (300 calls,
stream, heads with error > 0.1, main / this PR). The log of each run shows which.cowas loaded.msk0_v3_pscausal=Falsemsk0_lse_v3_pscausal=False, LSEcprr_v3_pskv_len513..768lse_cprr_v3_psmsk0_cprr_v3_pscausal=Falsemsk0_lse_cprr_v3_pscausal=False, LSEEvery patched run below counts the (row, query, head) outputs with relative error above 0.1 against an fp64 reference. Logs and scripts:
/app/kanban/vteam-158/pr66322_deps/qh16_cold_cache/siblings/r3/.v3_psand forlse_v3_ps. Some heads have an error between 0.05 and 0.1 (205 and 246 heads in 12000 calls). This is fp8 rounding: the same count shows when everys_waitcntis set to 0.kv_len300 to 8200 with 1 to 32 splits per row: main has wrong rows in every shape with LSE (for example 24865 heads in 200 calls at 64 rows,kv_len1000, 4 splits). This PR: 0 in every shape, with and without LSE.python3 op_tests/test_mla_persistent.py -n 16,4 -d fp8 -kvd fp8with and without-lse: the new check passes. The sweep has the same 13 of 64 rows above 0.05decode:erras main (short contexts, max 0.18 on both).After merging main (95d2112, including #7872), on MI355X (gfx950), merged
hsa/gfx950/mlaloaded as an overlay, main.covs this PR:check_fp8_gqa16_multi_tile_batch(4), 10 runs: main fails 10/10 (36-1121 wrong outputs per run); this PR 0/10.check_fp8_gqa16_multi_tile_batch(4, return_lse=True), 10 runs: main fails 10/10 (~14k-18k wrong outputs per run); this PR 0/10..cogives 192-6144 NaN rows, so the clamp is kept..cofiles; their contents are unchanged, so the runs above still apply.Not verified:
cprrandmsk0variants were checked at the binary level only (same rotation in the same windows); the GPU runs above coverv3_psandlse_v3_ps.s_waitcnt lgkmcnt(4), then MFMAs that use the last 4 reads with more reads in between and no wait. There are 6 MFMAs of 64 cycles each before the first use, so we did not see a failure there, and we did not change it.PR-specific question
Only gfx950 fp8 MLA decode with fp8 Q, fp8 KV and persistent mode reaches these kernels.
mla_asm_decode_remapincsrc/py_itfs_cu/asm_mla.cumaps three shapes toGqa=16andqSeqLen=4: gqa 16 withmax_seqlen_q3 or 4, gqa 32 withmax_seqlen_q2 or 3, and gqa 64 withmax_seqlen_q1. The kernel is then looked up inhsa/<arch>/mla/mla_asm.csvby (qType, kvType, Gqa, ps, qSeqLen, prefill, causal, lse, cprr). Onlyhsa/gfx950/mla/mla_asm.csvhasqh16_qseqlen4_gqaratio16*_v3_psrows, all withps=1.causalselectsmsk0, persistentreturn_lseselectslse, and ag_kv_indptr(context parallel round-robin) selectscprr.max_seqlen_q1 always uses a causal kernel, so gqa 64 does not reachmsk0. Other inputs cannot reach the patched code: gfx942 reads its own CSV, which has no such row; non-persistent calls useps=0rows (qh64_qseqlen4, not changed); gqa 32 withmax_seqlen_q4 or more, gqa 64 withmax_seqlen_q2 or more, gqa 96 and gqa 128 go to gqa 32 kernels; and bf16 Q or KV matches no fp8 row. The patch does not change any kernel argument, launch configuration, workspace or the CSV, so this selection is the same as on main.Changed op tests
op_tests/test_mla_persistent.py:main()now also calls the existingcheck_fp8_gqa16_multi_tile_batch(from #7622) with qlen 4, with and without LSE. The helper runs 64 calls of 128 rows with 3 KV tiles each, next to a 256 MiB device-to-device copy on a side stream (the file setstorch.set_default_device("cuda")), and compares every (row, query, head) output with an fp32 reference. The existing calls cover qlen 1 only, and the existing sweeps use small batches and no concurrent work, so they pass on main. The qlen 4 calls fail on main in every run and pass with this PR. They take about 1.5 s per kernel.AI assistance (Claude Code) was used for the analysis and the patch. A human reviewed the change.