Repository navigation
[ASM] Fix LDS races in gfx950 FP8 QH16 qseqlen1 persistent MLA decode - #7622
Conversation
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. 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 |
The last SUB_KV tile of a work item runs full width. Its out-of-range lanes
got page id 0 from the OOB buffer_load of kv_indices and read KV slot 0.
Their softmax weight is 0, but P*V still computes 0 * V, so a NaN in slot 0
(e.g. a reused or never-written block) turned whole output rows into NaN.
Rebuild from poc_kl_merg mmd/fix/mla_kv_page0_nan (d9381a8), which clamps
every page-table address, prefetches included, to the last valid entry of
the work item:
mla_a16w16_qh32_qseqlen4_gqaratio96{,_lse}{,_msk0}{,_cprr}_ps
mla_a16w16_qh32_qseqlen4_gqaratio32{,_lse}{,_msk0}{,_cprr}_ps
mla_a16w16_qh64_qseqlen1_gqaratio64{,_cprr}_v3_ps, ..._lse_cprr_v3_ps
mla_a8w8_qh32_qseqlen4_gqaratio32{,_lse}{,_cprr}_ps
mla_a8w8_qh16_qseqlen4_gqaratio16{,_lse}{,_cprr}_v3_ps
mla_a8w8_qh32_qseqlen4_gqaratio8{,_lse}_ps
mla_a16w16_qh16_qseqlen4_gqaratio16{,_lse}_ps, mla_a8w8_qh16_qseqlen4_gqaratio16_ps
Each .co differs from main's only by the added clamp instructions (no
instruction removed or changed). With NaN written into an unreferenced
KV slot 0, main's .co files return NaN rows and these stay clean; clean
outputs match main bit for bit except for the FP8 QH16 qseqlen4 v3 kernels,
which are already nondeterministic run to run on main.
Not rebuilt: mla_a8w8_qh16_qseqlen1_gqaratio16{,_lse}_ps (the SP3 source
lacks the #7622 LDS fixes, which were patched into the .co directly) and
mla_a16w16_qh64_qseqlen1_gqaratio64_lse_v3_ps (its SP3 source has a
different LSE sanitize bound than the shipped .co).
Rebuild mla_a8w8_qh16_qseqlen1_gqaratio16{,_lse}_ps from poc_kl_merg
mmd/fix/mla_kv_page0_nan (d06974a), which now carries the #7622 LDS race
fixes in the SP3 source as well as the KV slot 0 page-table clamp.
Both .co files are main's (#7622) plus only the clamp instructions. With NaN
written into an unreferenced KV slot 0 main returns NaN rows and these stay
clean; clean outputs match main bit for bit, and #7622's multi-tile race test
passes (the pre-#7622 .co fails it on the same machine).
… OOB read page0 data (#7872) * fix the nan error * mla: keep only the LTD-clamp .co files built from verified SP3 sources Narrow 8081b4e to the kernels whose fixed SP3 source is committed in poc_kl_merg mmd/fix/mla_kv_page0_nan and verified: MLA_A8W8_QH16_1TG_4W_16mx1_32nx4_PS.sp3 (e52636d) mla_a8w8_qh16_qseqlen1_gqaratio16{,_lse}_ps MLA_A16W16_QH32_1TG_4W_32mx4_32nx1_PS_gqaratio96.sp3 (fd1911a) mla_a16w16_qh32_qseqlen4_gqaratio96{,_lse}{,_msk0}{,_cprr}_ps Each kept .co is instruction-identical to a rebuild from that source, and the pre-fix .co is instruction-identical to a rebuild from the pre-fix source, so the only change is the page-table clamp that stops OOB tail lanes from reading KV slot 0. With NaN written into slot 0, the old .co files produce NaN output and the kept ones stay bit-identical to clean runs. Every other .co touched by 8081b4e goes back to its previous version, and the files it added (including the unregistered qh32ctl A/B control) are removed. * mla: clamp page-table reads in gfx950 MLA PS kernels (KV slot 0 NaN) The last SUB_KV tile of a work item runs full width. Its out-of-range lanes got page id 0 from the OOB buffer_load of kv_indices and read KV slot 0. Their softmax weight is 0, but P*V still computes 0 * V, so a NaN in slot 0 (e.g. a reused or never-written block) turned whole output rows into NaN. Rebuild from poc_kl_merg mmd/fix/mla_kv_page0_nan (d9381a8), which clamps every page-table address, prefetches included, to the last valid entry of the work item: mla_a16w16_qh32_qseqlen4_gqaratio96{,_lse}{,_msk0}{,_cprr}_ps mla_a16w16_qh32_qseqlen4_gqaratio32{,_lse}{,_msk0}{,_cprr}_ps mla_a16w16_qh64_qseqlen1_gqaratio64{,_cprr}_v3_ps, ..._lse_cprr_v3_ps mla_a8w8_qh32_qseqlen4_gqaratio32{,_lse}{,_cprr}_ps mla_a8w8_qh16_qseqlen4_gqaratio16{,_lse}{,_cprr}_v3_ps mla_a8w8_qh32_qseqlen4_gqaratio8{,_lse}_ps mla_a16w16_qh16_qseqlen4_gqaratio16{,_lse}_ps, mla_a8w8_qh16_qseqlen4_gqaratio16_ps Each .co differs from main's only by the added clamp instructions (no instruction removed or changed). With NaN written into an unreferenced KV slot 0, main's .co files return NaN rows and these stay clean; clean outputs match main bit for bit except for the FP8 QH16 qseqlen4 v3 kernels, which are already nondeterministic run to run on main. Not rebuilt: mla_a8w8_qh16_qseqlen1_gqaratio16{,_lse}_ps (the SP3 source lacks the #7622 LDS fixes, which were patched into the .co directly) and mla_a16w16_qh64_qseqlen1_gqaratio64_lse_v3_ps (its SP3 source has a different LSE sanitize bound than the shipped .co). * mla: clamp page-table reads in the FP8 QH16 qseqlen1 PS kernels too Rebuild mla_a8w8_qh16_qseqlen1_gqaratio16{,_lse}_ps from poc_kl_merg mmd/fix/mla_kv_page0_nan (d06974a), which now carries the #7622 LDS race fixes in the SP3 source as well as the KV slot 0 page-table clamp. Both .co files are main's (#7622) plus only the clamp instructions. With NaN written into an unreferenced KV slot 0 main returns NaN rows and these stay clean; clean outputs match main bit for bit, and #7622's multi-tile race test passes (the pre-#7622 .co fails it on the same machine). --------- Co-authored-by: guoliang <[email protected]>
#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]>
Summary
This PR fixes three LDS races in the gfx950 FP8 QH16 qseqlen1 persistent MLA decode kernels:
hsa/gfx950/mla/mla_a8w8_qh16_qseqlen1_gqaratio16_ps.coandmla_a8w8_qh16_qseqlen1_gqaratio16_lse_ps.co. Both are binary patches with the same file size. The PR also adds a regression check toop_tests/test_mla_persistent.py.Motivation
These kernels run persistent fp8 MLA decode with 16 heads and 1 query token, for example a 128-head model with TP 8. Under load, some heads of some rows get wrong output. The error is silent. With
return_lse=True, a row can be wrong for almost all 16 heads at once.check_fp8_gqa16_multi_tile_batch(added here): 128 rows,kv_len257..383 (3 KV tiles), with a copy on a side stream. Wrong (row, query, head) outputs per run:qseqlen1_gqaratio16_psqseqlen1_gqaratio16_lse_psWe also see wrong rows on main with no other work on the GPU: 67 of 400 calls (
_ps) and 29 of 400 calls (_lse_ps) of 128 rows each.Root cause, from the disassembly. The kernels have the same LDS layout as the non-persistent kernel (#7617): two KV slots at 0x4000 and 0x16000, 0x4800 bytes per wave.
ds_read_b64_tr_b8. These reads include rows that other waves loaded. The next loop half starts theglobal_load_ldsprefetch into that slot with nos_barrier. A fast wave overwrites rows that a slow wave did not read yet. This is the race that [ASM] [Bugfix] Fix LDS race in gfx950 FP8 QH16 qseqlen1 MLA decode #7617 fixes in the non-persistent kernel.ds_write_b64 v34, LDS 0..0x800) and the row-max reads (ds_read_b32 v37, LDS 0..0x400) are in the same barrier interval.s_waitcnt lgkmcnt(4)at the top of the next half still lets the last 4 reads be in flight. The next two MFMAs read a[44:47] and a[52:55] without a wait. A static check of the wait counters along the loop paths finds this in the second copy only (.text0x4918 and 0x5158 in_ps).All three are necessary (
_ps, 128 rows, 400 calls per run, heads with error > 0.1):.costream, 2 runsnone(no other work), 2 runss_barrierchanged tos_nop_lse_psgives the same picture (stream: main 655 and 708, this PR 0 and 0; A + B 11 and 8;s_nopcontrol 5187 and 6707).The
_lse_pswhole-row failure: a one-hot probe (each head attends to one token, soo[h] == V[t_h], 1 row, 600 tokens,streammode) gave 7 bad calls of 3000 on main and 0 of 3000 with this PR. In the bad calls, 13 of 16 heads on average are wrong, and 1853 of 1864 wrong 16-dim chunks are exact copies of KV rows that are not in the call. V is shared by all 16 heads, so when a slow wave reads V rows that a prefetch DMA already replaced, every head of that row is wrong. This is consistent with race A. The KV rows come from index entries that the call does not own (the probe takes its indices as a slice of a longer permutation). We did not prove which loop half overreads the indices.Related work: #7617 fixes race A and the P-write race in the non-persistent kernel. #4638 (closed, no linked fix) reported nondeterministic Stage-1 output of
qseqlen1_gqaratio16_psfor sparse decode.Related: #7617 fixes the same class of race in the non-persistent qseqlen1 kernel. This PR and its two sibling PRs (#7621 for qseqlen2, #7623 for qseqlen4 v3) each add the same
check_fp8_gqa16_multi_tile_batchhelper toop_tests/test_mla_persistent.py; whichever merges second drops its copy. #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_qseqlen1_gqaratio16_ps.coandmla_a8w8_qh16_qseqlen1_gqaratio16_lse_ps.co, binary patch. The SP3 source is not in the repo. The file size, the ELF layout and the branch targets do not change. There is no code motion.s_waitcnt lgkmcnt(0); s_barrier; s_nop 0. The room comes fromv_add_co_u32 v112, vcc, v112, v28andv_addc_co_u32 v113, vcc, v113, 0, vcc. Therev28is 0 (s_mov_b32 s61, 0; v_mov_b32 v28, s61), so they do not change the address, andvccis written again before it is read. (_ps:.text0x38e0, 0x4128, 0x4990, 0x51d0._lse_ps: 0x3804, 0x404c, 0x48b4, 0x50f4.)s_barrierbetween the row-max reads and the P write, in the 4 loop halves and the 2 tail halves. The room comes fromv_mov_b32 v28, v38; v_add_f32 v28, v39, v28, which becomesv_add_f32 v28, v39, v38(same operands, same order). (_ps: 0x3df4, 0x463c, 0x4e8c, 0x56cc, 0x5c6c, 0x6098._lse_ps: 0x3d18, 0x4560, 0x4db0, 0x55f0, 0x5b90, 0x5fbc.)s_waitcnt lgkmcnt(4)becomeslgkmcnt(0)at the 2 loop-half tops of the loop copy of waves 2 and 3. (_ps: 0x4918, 0x5158._lse_ps: 0x483c, 0x507c.)op_tests/test_mla_persistent.py: addcheck_fp8_gqa16_multi_tile_batch(decode_qlen, persistent, return_lse).main()runs it on gfx950 for qlen 1, with and without LSE.The kernel owners may prefer to rebuild the
.cofiles from SP3 with the same barriers and wait. 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 -2.5% and +0.7%, which is within run-to-run noise. Each loop half gets 2 more barriers (A and B), and 2 loop-half tops of waves 2 and 3 now wait for all LDS reads.
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
TODO: fill in from the PR Self-Review comment.
Behavior and intent
The qseqlen1 persistent kernels now synchronize all waves before a wave overwrites an LDS region that other waves can still read, and they wait for all K reads before the first MFMA of a loop half. Before, some heads of some rows were wrong and nondeterministic under load.
Validation and unverified scope
MI350X (gfx950), PyTorch 2.13.0+rocm10.0.0, aiter main 581579a.
Every 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/._lse_ps(1 row, 600 tokens,stream): main 7 of 3000 calls bad, this PR 0 of 3000.kv_len300 to 8200 with 1 to 32 splits per row._ps: main has wrong rows in every shape with more than 1 split (for example 624 heads in 200 calls at 64 rows,kv_len1000, 4 splits). This PR: 0 in every shape.python3 op_tests/test_mla_persistent.py -n 16,1 -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 of 21 and 64 tokens, max 0.17 on both).Not verified, and not changed by this PR:
mla_a8w8_qh16_qseqlen1_gqaratio16_msk0_ps.coand_msk0_lse_ps.cohave the same code and the same three races. The kernel table maps a 1-token non-causal call to the causal kernel, and our non-causal test loaded_ps, so we could not run them. We did not change them._lse_pson main and with this PR gives a few wrong rows (for example 186 heads in 200 calls at 64 rows,kv_len1000, 4 splits, against 765 on main). A second call with the same inputs is correct. This looks like the merge of a split that stage 1 did not write (the bug class of [Bugfix] MLA decode: merge only the splits gfx950 fp8 gqa16 stage 1 writes #7613), not an LDS race. We did not fix it here.PR-specific question
TODO: answer from the PR Self-Review comment.
Changed op tests
op_tests/test_mla_persistent.py:check_fp8_gqa16_multi_tile_batchruns 64 calls of 128 rows with 3 KV tiles each, next to a copy on a side stream, and compares every (row, query, head) output with an fp32 reference. The existing sweeps use small batches and no concurrent work, so they pass on main. This check fails on main in every run and passes with this PR. It takes about 1.5 s per kernel.This PR and the qseqlen2 and qseqlen4 PRs add the same helper function. Each PR calls it only for its own kernels. The second PR to merge must drop the duplicate helper.
AI assistance (Claude Code) was used for the analysis and the patch. A human reviewed the change.