Skip to content

[ASM] Fix LDS race between V reads and R re-layout in gfx950 BF16 QH3… - #7574

Merged
valarLip merged 2 commits into
mainfrom
mmd/fix/nhead96_nan
Nov 6, 2026
Merged

valarLip merged 2 commits into
mainfrom
mmd/fix/nhead96_nan

Conversation

@minmengdie

@minmengdie minmengdie commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix an LDS write-after-read race in the gfx950 BF16 persistent QH32 32mx4_32nx1 MLA decode kernels that occasionally produced NaN in the output (with a correct LSE) for Kimi-K3.1 with DCP8 (96 gathered heads), ROCm/ATOM#3051. The kernels now issue an s_barrier before the output re-layout writes to LDS. 16 .co are rebuilt; there are no source, CSV or dispatch changes in aiter.

Motivation

R_write_out (fp32 split partials) and R_write_out_bf16 (bf16 finals) re-layout the output through LDS scratch at LDS_R_BASE / LDS_R_RSP with per-wave offsets. That scratch overlaps KV slot 0 ([8 KB, 44 KB)), and slot 1 too on the bf16 path. A work item reads its last V tile from slot 0 when full_tiles % 4 == 2 with a tail, or when full_tiles % 4 == 3 with no tail. Example: an 80-key split is 2 full tiles plus a 16-token tail. After the tail-V barrier there was no barrier before the re-layout writes. So the first wave to finish started writing its fp32 partial O into slot 0 while the other waves were still reading V from it.

  • fp32 partials, reinterpreted as bf16 V, give NaN/Inf on roughly 1/256 of the elements. Since V is shared by all heads of a wave, whole 32-head groups end up with NaN on a few fixed dims. The LSE is still correct because QK/softmax finish before the tail-V barrier. Both production dumps in ATOM#3051 match this exactly: split rows with an 80-key work item, 2 of the 3 head groups bad (the good one belongs to the wave that ran ahead), and fixed NaN dims.
  • bf16 finals (unsplit rows) get finite but wrong values (max abs error ~6 in the skewed repro). This is silent.
  • The race needs inter-wave skew, so it does not reproduce offline. The base 128-head kernel has the same code; the gqaratio96 build (idle wave 3, contiguous M split) just made the skew more likely in production.

Changes

Kernel source: poc_kl_merg mmd/fix/nhead96_nan.

  • 720300b: s_barrier before the re-layout writes in R_write_out and R_write_out_bf16 (the bf16 qlen==0 call only; the qlen==1 call reuses only the wave's own scratch). Applied to MLA_A16W16_QH32_1TG_4W_32mx4_32nx1_PS.sp3 and its _gqaratio96 copy.
  • c059aff: adds the MASK = 0 rows for the gqaratio32 msk0 builds to mla_kernel_list_mi350.csv. They existed only on mmd/dev/non-mask, so a rebuild from main used to skip them.

Rebuilt hsa/gfx950/mla/*.co:

.co diff vs the shipped .co
mla_a16w16_qh32_qseqlen4_gqaratio96_{,lse_,cprr_,lse_cprr_,msk0_,msk0_lse_,msk0_cprr_,msk0_lse_cprr_}ps (8) +2 s_barrier only (+8 bytes each; the pre-fix rebuild is instruction-identical to the shipped .co)
mla_a16w16_qh32_qseqlen4_gqaratio32_{,lse_,cprr_,lse_cprr_}ps (4) +2 s_barrier relative to a pre-fix rebuild of the current SP3. The shipped .co predate poc_kl_merg a5fe0db / dc337e1 (CKV global-load address generation), so those changes ship with this PR too.
mla_a16w16_qh32_qseqlen4_gqaratio32_msk0_{,lse_,cprr_,lse_cprr_}ps (4) same as the row above

Performance (if applicable)

End-to-end time (stage1 + reduce) from op_tests/test_mla_persistent.py, mean of 3 GPUs. Each GPU runs every variant (order rotated) and only same-GPU numbers are compared. "Before" is the pre-fix rebuild of the same SP3 source; for gqaratio96 it is instruction-identical to the shipped .co.

Hardware Workload/configuration Before After Change
MI355X bf16, nhead 96, qlen 1, B 32, ctx 8192 113.7 us 112.8 us -0.79%
MI355X bf16, nhead 96, qlen 1, B 128, ctx 65536 2369.7 us 2378.8 us +0.38%
MI355X bf16, nhead 96, mean of B {32,64,128} x ctx {8K,16K,64K} -0.19% (range -1.55% .. +1.06%)
MI355X bf16, nhead 128, qlen 1, B 128, ctx 65536 2627.5 us 2629.2 us +0.07%
MI355X bf16, nhead 128, mean of B {32,64,128} x ctx {8K,16K,64K} +0.20% (range -0.32% .. +0.61%)
MI355X bf16, nhead 128, mean of the same 9 shapes, shipped .co vs this PR about -2% (new gqaratio32 .co are faster, from the CKV load changes)

Noise reference: two instruction-identical gqaratio96 .co (shipped vs pre-fix rebuild) differ by -1.23% .. +1.13% on the same shapes. The stage1 kernel alone (torch profiler, 50 warm iterations) shows -0.07% for nhead 96 and -0.49% for nhead 128.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X
  • Tested on MI355X (gfx950). Only gfx950 .co change.

All commands were run with the 16 rebuilt .co swapped in through AITER_ASM_DIR; logs confirm LoadKernel picked the new files.

Command Result
test_mla_persistent.py -d bf16 -kvd bf16 -c 21 64 256 1200 3200 8192 -b 1 3 16 64 128 -n 96,1 / -n 128,1, each with and without -lse all passed, 0 warning, 0 failed
same with -n 96,2, -n 96,2 --no-causal (with and without -lse) all passed
same with -n 32,4 64,2 128,2 --no-causal (with and without -lse, gqaratio32 msk0) 90 configs each, all passed
test_mla_persistent_round_robin.py ... -cp -cpw 8 -n 96,1 (with and without -lse), -n 128,1 -lse all passed, no NaN
Replay of both ATOM#3051 production dumps plus synthetic production-like batches (B 48 with 60-260 keys per row, B 1536 with 64-127 keys) against an fp32 reference, 96 and 128 heads all rows within tolerance
Deterministic repro: debug build where every wave but one s_sleeps after the tail-V barrier (not shipped) pre-fix kernels fail with exactly the production pattern (NaN on whole 32-head groups, correct LSE; finite errors on unsplit rows); the same build plus the fix passes. Both 96 and 128 heads.

Documentation

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

Kernel binaries only; no API, behavior or tuning change.

Dependencies

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

Breaking Changes

None. Kernel names, kernarg layout, mla_asm.csv and dispatch are unchanged.

PR Self-Review

Reviewed revision

0e82058

Behavior and intent

The output of the gfx950 BF16 persistent QH32 32mx4_32nx1 MLA decode kernels (gqaratio96 and gqaratio32, causal and msk0, with and without LSE and round-robin CP) no longer depends on inter-wave timing. Before this PR, the first wave to finish its tail-V GEMM could overwrite LDS KV slot 0 with its output re-layout scratch while other waves were still reading their last V tile from it. Results were then occasionally NaN (split rows, fp32 partials) or silently wrong (unsplit rows, bf16 finals), and the LSE stayed correct. That is ATOM#3051. The existing behavior is insufficient because it is wrong under any wave skew. It can only be avoided by padding 96 heads to 128 (ATOM's current workaround), and the 128-head kernel has the same latent race. The fix is one s_barrier per work item before the re-layout writes, so all waves finish reading V before any wave writes the scratch.

Validation and unverified scope

Validated on MI355X (gfx950), rocm/atom-dev container, aiter at f705d6b7a plus this PR:

  • Instruction-level: every rebuilt .co equals its pre-fix rebuild plus exactly 2 s_barrier (normalized llvm-objdump diff). The pre-fix gqaratio96 rebuilds are instruction-identical to the shipped .co.
  • Correctness: op_tests/test_mla_persistent.py and op_tests/test_mla_persistent_round_robin.py with the commands in the Testing section, all passed. Replay of both ATOM#3051 dumps and the synthetic sweeps passed.
  • Root cause: the skewed-wave debug build fails before and passes after the fix, for both 96 and 128 heads.
  • Perf: see the Performance section; within noise.

Not verified:

  • ATOM end-to-end with this PR (the AIME DCP8 run that showed 16/120 collapses with 96 heads).
  • Runtime coverage of gqaratio96_cprr_ps, gqaratio32_cprr_ps (the round-robin test always requests LSE) and of the four msk0_*cprr .co (reached with causal=False, decode qlen > 1 and g_kv_indptr; the round-robin test is causal-only). Their instruction diff is the same 2 s_barrier.
  • gfx942 is not affected: no gfx942 .co changed.
  • The gqaratio32 / gqaratio32_msk0 .co also carry the earlier, unreleased CKV global-load address changes (poc_kl_merg a5fe0db, dc337e1). Those are covered only by the tests above.

PR-specific question

The changed kernels are selected only on gfx950 for bf16 Q and bf16 KV in persistent mode (csrc/py_itfs_cu/asm_mla.cu, mla_asm_decode_config):

  • gqa_ratio == 96 maps to config_gqa_ratio = 96, config_max_seqlen_q = 4, giving mla_a16w16_qh32_qseqlen4_gqaratio96_*.
  • Otherwise, gqa_ratio * max_seqlen_q >= 128 || gqa_ratio > 64 (and gqa_ratio != 48) maps to config_gqa_ratio = 32, config_max_seqlen_q = 4, giving mla_a16w16_qh32_qseqlen4_gqaratio32_*.
  • The causal / msk0 variant follows causal, except that max_seqlen_q == 1 forces the causal kernel. _lse_ requires return_lse in persistent mode. _cprr_ requires g_kv_indptr.

Unsupported inputs cannot reach these kernels for three reasons:

  • The kernel is looked up from hsa/gfx950/mla/mla_asm.csv by the exact tuple (qType, kvType, Gqa, ps, qSeqLen, prefill, causal, lse, cprr); a missing entry is rejected.
  • mla_decode_stage1_asm_fwd checks that Q is contiguous, num_kv_heads == 1 and head_size == 576.
  • The persistent metadata (kPackedQoLenPerWg 128) limits each work item to the 128 packed query rows the kernel holds.

This PR does not change any of that selection logic, only the kernel bodies.

Changed op tests

None.

…2 PS MLA

R_write_out / R_write_out_bf16 stage the output through LDS at LDS_R_BASE /
LDS_R_RSP, which overlaps KV slot 0 (and slot 1 for the bf16 path). When a
work item's last V tile sits in slot 0, the first wave to finish started
writing its partial O there while the other waves were still reading V.
fp32 split partials read back as bf16 then gave NaN on a few dims of whole
32-head groups with a correct LSE (ATOM #3051, Kimi-K3.1 DCP8 with 96 gathered
heads); unsplit bf16 finals gave silent finite errors. The kernels now issue
an s_barrier before the re-layout writes.

Rebuilt from poc_kl_merg mmd/fix/nhead96_nan (720300b):
- mla_a16w16_qh32_qseqlen4_gqaratio96_{,lse_,cprr_,lse_cprr_,msk0_,msk0_lse_,
  msk0_cprr_,msk0_lse_cprr_}ps.co: previous code plus the 2 s_barrier.
- mla_a16w16_qh32_qseqlen4_gqaratio32_{,lse_,cprr_,lse_cprr_}ps.co: same fix;
  rebuilt from the current SP3 source, so they also pick up its CKV global-load
  address changes that the previous .co predate.

Tested on MI355X: with waves skewed on purpose the old kernels fail and the new
ones pass (96 and 128 heads); test_mla_persistent (-n 96,1 128,1 96,2 incl.
--no-causal, with and without -lse) and test_mla_persistent_round_robin
(-cp -cpw 8) pass. Perf vs the pre-fix source, B 32/64/128 x ctx 8K/16K/64K:
96 heads -0.19%, 128 heads +0.20% end to end (within noise); the new
gqaratio32 .co are ~2% faster than the previous ones.
@minmengdie
minmengdie requested review from a team and a balanced review from Copilot November 6, 2026 11:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

@github-actions

github-actions Bot commented Nov 6, 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 7574 --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.

@github-actions github-actions Bot added the ASM label Nov 6, 2026
@aiter-bot

aiter-bot commented Nov 6, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 0e820587433b · 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 — validation answer must explicitly state what remains unverified (or say none with a reason)

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

…SK=0 MLA

The non-causal mla_a16w16_qh32_qseqlen4_gqaratio32_msk0_{,lse_,cprr_,lse_cprr_}ps.co
are built from the same SP3 with MASK = 0 and have the same R_write_out LDS
write-after-read race as the causal ones fixed in the previous commit. Rebuilt
from poc_kl_merg mmd/fix/nhead96_nan (c059aff): the 2 s_barrier on top of the
current SP3 source, which also carries the CKV global-load address changes the
previous .co predate (same as the causal gqaratio32 .co).

Tested on MI355X: test_mla_persistent -n 32,4 64,2 128,2 --no-causal, with and
without -lse, 90 configs each, all passed.
Copilot AI balanced review requested due to automatic review settings November 6, 2026 11:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

@valarLip
valarLip merged commit 1bb9d8d into main Nov 6, 2026
@valarLip
valarLip deleted the mmd/fix/nhead96_nan branch November 6, 2026 22:36
ruanjm pushed a commit that referenced this pull request Nov 10, 2026
#7574)

* [ASM] Fix LDS race between V reads and R re-layout in gfx950 BF16 QH32 PS MLA

R_write_out / R_write_out_bf16 stage the output through LDS at LDS_R_BASE /
LDS_R_RSP, which overlaps KV slot 0 (and slot 1 for the bf16 path). When a
work item's last V tile sits in slot 0, the first wave to finish started
writing its partial O there while the other waves were still reading V.
fp32 split partials read back as bf16 then gave NaN on a few dims of whole
32-head groups with a correct LSE (ATOM #3051, Kimi-K3.1 DCP8 with 96 gathered
heads); unsplit bf16 finals gave silent finite errors. The kernels now issue
an s_barrier before the re-layout writes.

Rebuilt from poc_kl_merg mmd/fix/nhead96_nan (720300b):
- mla_a16w16_qh32_qseqlen4_gqaratio96_{,lse_,cprr_,lse_cprr_,msk0_,msk0_lse_,
  msk0_cprr_,msk0_lse_cprr_}ps.co: previous code plus the 2 s_barrier.
- mla_a16w16_qh32_qseqlen4_gqaratio32_{,lse_,cprr_,lse_cprr_}ps.co: same fix;
  rebuilt from the current SP3 source, so they also pick up its CKV global-load
  address changes that the previous .co predate.

Tested on MI355X: with waves skewed on purpose the old kernels fail and the new
ones pass (96 and 128 heads); test_mla_persistent (-n 96,1 128,1 96,2 incl.
--no-causal, with and without -lse) and test_mla_persistent_round_robin
(-cp -cpw 8) pass. Perf vs the pre-fix source, B 32/64/128 x ctx 8K/16K/64K:
96 heads -0.19%, 128 heads +0.20% end to end (within noise); the new
gqaratio32 .co are ~2% faster than the previous ones.

* [ASM] Apply the R re-layout LDS barrier to the gfx950 BF16 QH32 PS MASK=0 MLA

The non-causal mla_a16w16_qh32_qseqlen4_gqaratio32_msk0_{,lse_,cprr_,lse_cprr_}ps.co
are built from the same SP3 with MASK = 0 and have the same R_write_out LDS
write-after-read race as the causal ones fixed in the previous commit. Rebuilt
from poc_kl_merg mmd/fix/nhead96_nan (c059aff): the 2 s_barrier on top of the
current SP3 source, which also carries the CKV global-load address changes the
previous .co predate (same as the causal gqaratio32 .co).

Tested on MI355X: test_mla_persistent -n 32,4 64,2 128,2 --no-causal, with and
without -lse, 90 configs each, all passed.
zhiding512 pushed a commit that referenced this pull request Nov 13, 2026
The 10 .co files changed on this branch were also rebuilt on main
(#7574 for the BF16 QH32 gqaratio96 kernels, #7622 for the FP8 QH16
qseqlen1 PS kernels). Take main's version of all of them; the LTD clamp
is re-applied on top of main in the next commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants