Skip to content

[ASM] [Bugfix] Fix LDS race in gfx950 FP8 QH16 qseqlen1 MLA decode - #7617

Merged
kaiyang-1 merged 1 commit into
mainfrom
akaratza_mla_qh16_cold
Nov 10, 2026
Merged

kaiyang-1 merged 1 commit into
mainfrom
akaratza_mla_qh16_cold

Conversation

@AndreasKaratzas

Copy link
Copy Markdown
Contributor

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 adds s_barrier before the KV prefetch and before the P write. The PR also adds a regression check to op_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:

bs kv_len splits main, no flush main, flush this PR, no flush this PR, flush
128 512 2 401 / 12800 1018 / 12800 0 / 7680 0 / 7680
64 1024 4 75 / 3840 312 / 3840 0 / 3840 0 / 3840
256 1024 2 15 / 15360 27 / 15360 0 / 15360 0 / 15360

Shapes with only 1 tile per split gave 0 wrong rows on main.

Root cause, from the disassembly:

  • The kernel has two KV slots in LDS (0x4000 and 0x16000). Each wave loads its 32 tokens into a slot with global_load_lds_dwordx4.
  • At the end of a tile, the kernel does s_waitcnt vmcnt(2) and s_barrier. Then every wave reads V of the next tile for all 128 tokens with ds_read_b64_tr_b8. These reads include rows that other waves loaded.
  • The next loop half then starts the DMA of the next tile into the slot that was just read. There is no s_barrier before this DMA. A fast wave can overwrite rows that a slow wave did not read yet.
  • The last loop half prefetches a tile past the end of the split. Its buffer load gives index 0, so it loads KV row 0. A one-hot probe (each head attends to one token) shows that every wrong 16-dim chunk is an exact copy of KV row 0. Row 0 was not in the index list.
  • A second, smaller race: the P write (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 vmcnt wait does not explain the bug. Patched copies with s_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:
    • add s_waitcnt lgkmcnt(0); s_barrier before the first global_load_lds of the half (.text 0x38a4, 0x415c, 0x4a24, 0x52dc),
    • add s_waitcnt lgkmcnt(0); s_barrier before the P write (ds_write_b64 v34),
    • remove 2 of the 3 identical duplicate chunk-0 DMAs (same m0, same global address) to make room.
  • op_tests/test_mla.py: add check_fp8_gqa16_multi_tile_output. It runs on gfx950 from main(), next to the other check_* functions.

The kernel owners may prefer to rebuild the .co from SP3 with the same two barriers. The new check covers that rebuild too.

Performance (if applicable)

mla_decode_fwd with LSE, FP8, 16 heads, 200 iterations, median us. Two rounds each. The "control" .co is the same patch with the two s_barrier replaced by s_nop.

Hardware Workload/configuration Before After Change
MI355X bs 1, ctx 300, 3 splits 13.78 / 13.74 13.79 / 13.88 +0.1% / +1.0%
MI355X bs 1, ctx 4096, 16 splits 21.37 / 21.38 21.29 / 21.33 -0.4% / -0.2%
MI355X bs 32, ctx 4096, 8 splits 31.04 / 31.61 31.45 / 31.65 +1.3% / +0.1%
MI355X bs 128, ctx 1024, 2 splits 31.03 / 31.40 31.46 / 31.48 +1.4% / +0.3%
MI355X bs 128, ctx 4096, 2 splits 77.20 / 76.64 77.41 / 77.42 +0.3% / +1.0%
MI355X bs 256, ctx 8192, 2 splits 255.83 / 260.11 260.54 / 260.31 +1.8% / +0.1%

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

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

The .co is gfx950 only, so MI250X and MI300X do not apply.

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

The change fixes a kernel bug. The API and the behavior do not change.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

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_gqaratio16 was 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.
  • Without the cache flush, the check on main fails only 1 row or passes. So the flush is necessary for the check.
  • Controls: the same patch with the two s_barrier changed to s_nop still fails (5, 61 and 13 of 128 rows). With s_waitcnt lgkmcnt(0) kept and only the s_barrier removed, it also fails (54, 63 and 42 of 128 rows). So the cross-wave barrier is the fix, not the removed duplicate DMAs.
  • Sweeps with one split, kv_len 250..400, cache flush or side-stream copies: 0 of 2416 rows wrong. One-hot probe: 0 of 1000.
  • python3 op_tests/test_mla.py -n 16,1 -d fp8 -kvd fp8 -lse passes.

Not verified, and not changed by this PR:

PR-specific question

TODO: answer from the PR Self-Review comment.

Changed op tests

op_tests/test_mla.py: check_fp8_gqa16_multi_tile_output covers 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.

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]>
@AndreasKaratzas AndreasKaratzas added bug Something isn't working ASM labels Nov 8, 2026
@AndreasKaratzas AndreasKaratzas self-assigned this Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

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:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 7617 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@aiter-bot

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 6be672e39a23 · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — PR-specific question is too short to show concrete reasoning (7 units; need 14)

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@zufayu
zufayu requested a review from kaiyang-1 November 10, 2026 01:19
kaiyang-1 pushed a commit that referenced this pull request Nov 10, 2026
…#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]>
@kaiyang-1
kaiyang-1 merged commit 35aceb8 into main Nov 10, 2026
@kaiyang-1
kaiyang-1 deleted the akaratza_mla_qh16_cold branch November 10, 2026 04:12
maeehart added a commit to maeehart/aiter that referenced this pull request Dec 3, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ASM bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants