Repository navigation
[ASM] Fix LDS race between V reads and R re-layout in gfx950 BF16 QH3… - #7574
Conversation
…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.
There was a problem hiding this comment.
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.
🏷️ 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 |
…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.
There was a problem hiding this comment.
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.
#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.
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_barrierbefore the output re-layout writes to LDS. 16.coare rebuilt; there are no source, CSV or dispatch changes in aiter.Motivation
R_write_out(fp32 split partials) andR_write_out_bf16(bf16 finals) re-layout the output through LDS scratch atLDS_R_BASE/LDS_R_RSPwith 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 whenfull_tiles % 4 == 2with a tail, or whenfull_tiles % 4 == 3with 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.Changes
Kernel source: poc_kl_merg
mmd/fix/nhead96_nan.720300b:s_barrierbefore the re-layout writes inR_write_outandR_write_out_bf16(the bf16qlen==0call only; theqlen==1call reuses only the wave's own scratch). Applied toMLA_A16W16_QH32_1TG_4W_32mx4_32nx1_PS.sp3and its_gqaratio96copy.c059aff: adds theMASK = 0rows for the gqaratio32 msk0 builds tomla_kernel_list_mi350.csv. They existed only onmmd/dev/non-mask, so a rebuild from main used to skip them.Rebuilt
hsa/gfx950/mla/*.co:.co.comla_a16w16_qh32_qseqlen4_gqaratio96_{,lse_,cprr_,lse_cprr_,msk0_,msk0_lse_,msk0_cprr_,msk0_lse_cprr_}ps(8)s_barrieronly (+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)s_barrierrelative to a pre-fix rebuild of the current SP3. The shipped.copredate poc_kl_merga5fe0db/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)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..covs this PR.coare 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
.cochange.All commands were run with the 16 rebuilt
.coswapped in throughAITER_ASM_DIR; logs confirmLoadKernelpicked the new files.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-n 96,2,-n 96,2 --no-causal(with and without-lse)-n 32,4 64,2 128,2 --no-causal(with and without-lse, gqaratio32 msk0)test_mla_persistent_round_robin.py ... -cp -cpw 8 -n 96,1(with and without-lse),-n 128,1 -lses_sleeps after the tail-V barrier (not shipped)Documentation
Kernel binaries only; no API, behavior or tuning change.
Dependencies
Breaking Changes
None. Kernel names, kernarg layout,
mla_asm.csvand 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_barrierper 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
f705d6b7aplus this PR:.coequals its pre-fix rebuild plus exactly 2s_barrier(normalizedllvm-objdumpdiff). The pre-fix gqaratio96 rebuilds are instruction-identical to the shipped.co.op_tests/test_mla_persistent.pyandop_tests/test_mla_persistent_round_robin.pywith the commands in the Testing section, all passed. Replay of both ATOM#3051 dumps and the synthetic sweeps passed.Not verified:
gqaratio96_cprr_ps,gqaratio32_cprr_ps(the round-robin test always requests LSE) and of the fourmsk0_*cprr.co(reached withcausal=False, decode qlen > 1 andg_kv_indptr; the round-robin test is causal-only). Their instruction diff is the same 2s_barrier..cochanged..coalso carry the earlier, unreleased CKV global-load address changes (poc_kl_merga5fe0db,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 == 96maps toconfig_gqa_ratio = 96,config_max_seqlen_q = 4, givingmla_a16w16_qh32_qseqlen4_gqaratio96_*.gqa_ratio * max_seqlen_q >= 128 || gqa_ratio > 64(andgqa_ratio != 48) maps toconfig_gqa_ratio = 32,config_max_seqlen_q = 4, givingmla_a16w16_qh32_qseqlen4_gqaratio32_*.causal, except thatmax_seqlen_q == 1forces the causal kernel._lse_requiresreturn_lsein persistent mode._cprr_requiresg_kv_indptr.Unsupported inputs cannot reach these kernels for three reasons:
hsa/gfx950/mla/mla_asm.csvby the exact tuple (qType, kvType, Gqa, ps, qSeqLen, prefill, causal, lse, cprr); a missing entry is rejected.mla_decode_stage1_asm_fwdchecks that Q is contiguous,num_kv_heads == 1andhead_size == 576.kPackedQoLenPerWg128) 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.