Repository navigation
[ASM] [Bugfix] Fix LDS race in gfx950 FP8 QH16 qseqlen1 MLA decode - #7617
Conversation
mla_a8w8_qh16_qseqlen1_gqaratio16.co has two cross-wave LDS write-after-read races in the main loop: - After the V reads of a tile, a wave starts the global_load_lds prefetch of the next KV tile into the same LDS slot. There is no s_barrier, so a fast wave can overwrite rows that a slow wave has not read yet. The slow wave then reads V of the wrong tile (often KV row 0 from the past-the-end prefetch). The output is wrong and the LSE is correct. - The P write (ds_write_b64 to LDS 0..0x800) can overwrite the row-max scratch (LDS 0..0x400) before all waves read it. The SP3 source is not in the repo, so this is a binary patch of the .co. The file size, the ELF layout and the branch targets do not change. In each of the 4 main-loop halves: - Insert "s_waitcnt lgkmcnt(0); s_barrier" before the first global_load_lds of the half (.text 0x38a4, 0x415c, 0x4a24, 0x52dc). - Insert "s_waitcnt lgkmcnt(0); s_barrier" before the P write (ds_write_b64 v34). - Make room by removing 2 of the 3 identical duplicate DMAs of chunk 0 (same m0, same global address). Add check_fp8_gqa16_multi_tile_output to test_mla.py. It runs one row, 16 heads, fp8, kv_len 257..384 (3 KV tiles), one split, with a cache flush before each call. On main it fails (9 to 123 of 128 rows wrong). With this .co 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: |
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 |
…#7622) mla_a8w8_qh16_qseqlen1_gqaratio16_ps.co and _lse_ps.co have the races of the non-persistent kernel (#7617) and one more: - A: the next-tile KV prefetch (global_load_lds) of a loop half writes the LDS slot that other waves can still read. Before the first prefetch DMA of each of the 4 loop halves, add "s_waitcnt lgkmcnt(0); s_barrier; s_nop 0". The room comes from the add of v28 to the DMA address and its carry add. v28 is 0 there, so they do nothing, and vcc is written again before it is read. - B: the P write (ds_write_b64 v34, LDS 0..0x800) can overwrite the row-max scratch (LDS 0..0x400) before all waves read it. Add s_barrier between the row-max reads and the P write, in the 4 loop halves and the 2 tail halves. The room comes from folding "v_mov v28, v38; v_add_f32 v28, v39, v28" into "v_add_f32 v28, v39, v38" (same operands, same order). - C: waves 2 and 3 run a second copy of the main loop. There the K reads at the end of a half are in a different order, and "s_waitcnt lgkmcnt(4)" at the top of the next half leaves a[44:47] and a[52:55] in flight. The next two MFMAs read them. Use lgkmcnt(0) at these 2 loop-half tops. The file size, the ELF layout and the branch targets do not change. There is no code motion. 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. Signed-off-by: Andreas Karatzas <[email protected]> Co-authored-by: Claude <[email protected]>
Review by kernels-research found the patch incomplete. The object has six P-write sites. Four are the main-loop halves, which ROCm#7617 already guarded, and two are tail blocks: 0x5E78 and 0x62A0. This PR guarded only 0x62A0. Which tail block runs depends on the tile-count parity, not on the path being unsupported: at 0x5B34 the code tests the tile count and skips one block when the count is odd. A 428-token input is three tiles plus a tail, so it only ever reaches the guarded block, which is why the tests missed the other one. This commit patches 0x5E78 the same way and leaves 0x62A0 as it was, so both tail blocks now carry s_waitcnt lgkmcnt(0) and s_barrier before the P write. Verified in the rebuilt object: all six P-write sites in the decode path have that pair immediately before them, .text keeps its size and address, and the file stays 24,960 bytes. The test gains the shape the body says is also affected: 2 splits with return_lse=True, which is what the vLLM sparse MLA backend uses for layers with attention sinks. It runs a quarter of the launch budget because it is a second guard, and the docstring now says 1 split without return_lse is the cheap reproducer and 2 splits with return_lse is the serving shape. Also corrected: the body's claim that the label symbols do not move. Five of the 13 move by 8 bytes and the branch immediates follow, measured by diffing both objects.
Summary
This PR fixes a cross-wave LDS race in
hsa/gfx950/mla/mla_a8w8_qh16_qseqlen1_gqaratio16.co(gfx950, FP8 Q and KV, 16 heads, qseqlen 1, non-persistent). The fix addss_barrierbefore the KV prefetch and before the P write. The PR also adds a regression check toop_tests/test_mla.py.Motivation
The kernel gives wrong attention output when a split holds 2 or more 128-token KV tiles. The split LSE stays correct, so the error is silent. The race shows more often when the caches are cold or when other work runs on the GPU. vLLM decode context parallel (DCP) with FP8 KV and 16 or fewer local heads uses this kernel with
return_lse=True. At DCP-like shapes we see wrong rows without any cache flush:Shapes with only 1 tile per split gave 0 wrong rows on main.
Root cause, from the disassembly:
global_load_lds_dwordx4.s_waitcnt vmcnt(2)ands_barrier. Then every wave reads V of the next tile for all 128 tokens withds_read_b64_tr_b8. These reads include rows that other waves loaded.s_barrierbefore this DMA. A fast wave can overwrite rows that a slow wave did not read yet.ds_write_b64 v34, LDS 0..0x800) can overwrite the row-max scratch (ds_write_b32 v36/ds_read_b32 v37, LDS 0..0x400) before all waves read it. This one also makes the LSE wrong.A missing
vmcntwait does not explain the bug. Patched copies withs_waitcnt vmcnt(0)at the epilogue, loop or prologue barriers still fail. This is the same class of bug as #7574 (gfx950 BF16 QH32 PS kernels).Changes
mla_a8w8_qh16_qseqlen1_gqaratio16.co: binary patch. The SP3 source is not in the repo, so we could not rebuild it. The file size, the ELF layout and the branch targets do not change. In each of the 4 main-loop halves:s_waitcnt lgkmcnt(0); s_barrierbefore the firstglobal_load_ldsof the half (.text0x38a4, 0x415c, 0x4a24, 0x52dc),s_waitcnt lgkmcnt(0); s_barrierbefore the P write (ds_write_b64 v34),m0, same global address) to make room.op_tests/test_mla.py: addcheck_fp8_gqa16_multi_tile_output. It runs on gfx950 frommain(), next to the othercheck_*functions.The kernel owners may prefer to rebuild the
.cofrom SP3 with the same two barriers. The new check covers that rebuild too.Performance (if applicable)
mla_decode_fwdwith LSE, FP8, 16 heads, 200 iterations, median us. Two rounds each. The "control".cois the same patch with the twos_barrierreplaced bys_nop.The change is within run-to-run noise. Against the control
.co, the barriers cost at most about 1% (bs 128, ctx 4096: 77.41 vs 76.97 us and 77.42 vs 76.38 us).Testing
The
.cois 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
TODO: fill in from the PR Self-Review comment.
Behavior and intent
The kernel now synchronizes all waves before a wave overwrites an LDS region that other waves can still read. Before, the output of
mla_a8w8_qh16_qseqlen1_gqaratio16was wrong and nondeterministic when a split had 2 or more KV tiles. The split LSE was correct, so the reduce step could not detect the error.Validation and unverified scope
MI355X (gfx950), PyTorch 2.13.0+rocm10.0.0, aiter main 581579a.
check_fp8_gqa16_multi_tile_output: main fails in every run (117, 9, 123, 122, 20, 110 and 67 of 128 rows wrong, on 3 GPUs). This PR passes 27 of 27 runs on 3 GPUs.s_barrierchanged tos_nopstill fails (5, 61 and 13 of 128 rows). Withs_waitcnt lgkmcnt(0)kept and only thes_barrierremoved, it also fails (54, 63 and 42 of 128 rows). So the cross-wave barrier is the fix, not the removed duplicate DMAs.python3 op_tests/test_mla.py -n 16,1 -d fp8 -kvd fp8 -lsepasses.Not verified, and not changed by this PR:
qseqlen2_gqaratio1625, 20 / 36, 42 of 256;qseqlen1_gqaratio16_ps2, 2 / 1, 0 of 128;qseqlen2_gqaratio16_ps8, 14 / 3, 13 of 256;qseqlen4_gqaratio16_v3_ps49, 43 / 0, 0 of 512;qseqlen4_gqaratio16_lse_v3_ps79, 79 / 2, 6 of 512.qseqlen1_gqaratio16_lse_psgave 0 of 128. We did not disassemble these kernels, so we do not know if the cause is the same. [gfx950][MLA] FP8 qh16/q1 persistent sparse decode corrupts Stage-1 output nondeterministically #4638 and [gfx950][MLA] FP8-Q persistent decode mla_a8w8_qh16_qseqlen{2,4} returns wrong, nondeterministic output #7007 report similar symptoms on the_pskernels.PR-specific question
TODO: answer from the PR Self-Review comment.
Changed op tests
op_tests/test_mla.py:check_fp8_gqa16_multi_tile_outputcovers a single split with 3 KV tiles after a cache flush on the gfx950 FP8 QH16 qseqlen1 kernel. The existing sweeps do not flush the caches and use warm, repeated calls, so they pass on main. This check fails on main and passes with this PR.AI assistance (Claude Code) was used for the analysis and the patch. A human reviewed the change.