Repository navigation
[ASM] Fix MLA PS mode nan error for kv page0 has nan data and the LTD OOB read page0 data - #7872
Conversation
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.
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: Reviews: |
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).
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.
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.
#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
Stop the gfx950 persistent (PS) MLA decode kernels from reading KV slot 0 on the out-of-range lanes of a work item's last KV tile. With a NaN anywhere in the latent part of KV slot 0, those lanes turned whole output rows into NaN even when no
kv_indicesentry pointed at slot 0. The kernels now clamp every page-table read to the last valid entry of the work item. 34.coare rebuilt; each one is main's.coplus only the clamp instructions. There are no source, CSV or dispatch changes in aiter.Motivation
The persistent decode kernels load the page table (
kv_indices) through a buffer resource whosenum_recordsis set to this work item'scount * 4bytes, and then gather KV rows by the loaded page ids. The last SUB_KV tile of a work item still runs full width, and the page-table prefetch runs one tile ahead. Lanes past the end of the work item are out of range, the OOBbuffer_loadreturns 0, and those lanes gather KV slot 0.P = 0and finite data in slot 0 never changes the output (bit-identical).P * V = 0 * V. If any element of the 512 latent dims of slot 0 is NaN (fp80x7f/0xff, bf16 NaN),0 * NaN = NaNreaches the accumulator. MLA has one shared KV head, so every head of the row gets NaN on those dims. The LSE stays correct.max_split_per_batch = -1) makes that rare.This contract (slot 0 must hold finite data even when nothing references it) is not documented, and real callers break it:
mla_a8w8_qh16_qseqlen1_gqaratio16_ps): block 0 was reused for recurrent-state checkpoints whose bytes include fp8 NaN codes. The per-token fp8 quant beforeo_projturned the all-NaN rows into zeros, so the MLA output was erased silently. ATOM now reserves block 0 as a workaround.gqaratio96), one NaN element in slot 0 gives exactly the "fixed few NaN dims, correct LSE" pattern seen in [Kimi-K3.1][DCP8] Long decodes collapse into repeated token 0 (!!!!) and later lose the prompt under TP8 + DCP8 (clean with DCP=1) ATOM#3051, on top of the LDS race fixed by [ASM] Fix LDS race between V reads and R re-layout in gfx950 BF16 QH3… #7574.Any other caller whose slot 0 can hold non-finite bytes (freed / reused blocks, padding targets) is exposed until this lands.
Changes
Kernel source: poc_kl_merg
mmd/fix/mla_kv_page0_nan.bd608c9: page-table clamp inMLA_A16W16_QH32_1TG_4W_32mx4_32nx1_PS_gqaratio96.sp3andMLA_A8W8_QH16_1TG_4W_16mx1_32nx4_PS.sp3.d9381a8: the same clamp in the other PS kernels ofmla_kernel_list_mi350.csv(MLA_A16W16_QH32_..._PS,MLA_A16W16_QH64_..._mtp0_v3_PS,MLA_A16W16_QH16_..._16mx4_32nx1_PS,MLA_A8W8_QH32_..._32mx4_64nx1_PS,MLA_A8W8_QH64_..._32mx2_32nx2_PS,MLA_A8W8_QH32_..._32mx1_32nx2_PS,MLA_A8W8_QH16_..._16mx4_64nx1_PS, plus the mtp0 / mtp1 sources that aiter does not ship).d06974a: ports the [ASM] Fix LDS races in gfx950 FP8 QH16 qseqlen1 persistent MLA decode #7622 LDS race fixes intoMLA_A8W8_QH16_1TG_4W_16mx1_32nx4_PS.sp3. [ASM] Fix LDS races in gfx950 FP8 QH16 qseqlen1 persistent MLA decode #7622 patched them into the.codirectly, so a rebuild from the SP3 would otherwise have dropped them.The clamp:
s_LTD_last = (s_total_ps - 1) * 4per work item, and av_min_u32after every page-table address update (address generation, the two prefetch increments,LTD_mem_va_upd). Tail lanes then repeat the last valid page id instead of reading id 0; their weight is still 0, so the result is unchanged for finite data.Rebuilt
hsa/gfx950/mla/*.co(34):.comla_a16w16_qh32_qseqlen4_gqaratio96_{,lse_,cprr_,lse_cprr_,msk0_,msk0_lse_,msk0_cprr_,msk0_lse_cprr_}psmla_a16w16_qh32_qseqlen4_gqaratio32_{,lse_,cprr_,lse_cprr_,msk0_,msk0_lse_,msk0_cprr_,msk0_lse_cprr_}psmla_a16w16_qh64_qseqlen1_gqaratio64_{,cprr_,lse_cprr_}v3_psmla_a16w16_qh16_qseqlen4_gqaratio16_{,lse_}ps(not inmla_asm.csv)mla_a8w8_qh16_qseqlen1_gqaratio16_{,lse_}psmla_a8w8_qh32_qseqlen4_gqaratio32_{,lse_,cprr_,lse_cprr_}psmla_a8w8_qh16_qseqlen4_gqaratio16_{,lse_,cprr_,lse_cprr_}v3_psmla_a8w8_qh32_qseqlen4_gqaratio8_{,lse_}psmla_a8w8_qh16_qseqlen4_gqaratio16_ps(not inmla_asm.csv)For every one of them, a normalized
llvm-objdumpdiff against main's.coshows no removed or changed instruction; the only additions are the clamp (v_min_u32and the twos_LTD_lastsetup instructions, 13 to 21 instructions per kernel).Not changed:
mla_a16w16_qh64_qseqlen1_gqaratio64_lse_v3_ps: its SP3 source has a different LSE sanitize bound (+-64) than the shipped.co(+-inf), so a rebuild would change behavior beyond this fix.mla_a16w16_qh16_m16x4_n16x1_coex0_mask1{,_lse}_ps: the SP3 source is not in poc_kl_merg.gqaratio8/gqaratio32msk0PS.co: they are not built from the kernel list, so their source is unknown.Performance (if applicable)
decode:asm_576fromop_tests/test_mla_persistent.py. Each of 3 rounds ran on its own idle MI355X, with Before and After alternating on that GPU; the table shows the median of the 3 rounds. "Before" is main's.co.Over all 28 shapes, the After/Before geomean is 0.9986 (range 0.984 .. 1.010). Running the same
.coagain varies by 1.15% (median of max-min over median) and up to 3.18%, so the change is within noise.a8w8_qh16_qseqlen1_gqaratio16_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa8w8_qh32_qseqlen4_gqaratio32_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa16w16_qh64_qseqlen1_gqaratio64_v3_psa16w16_qh32_qseqlen4_gqaratio32_psa16w16_qh32_qseqlen4_gqaratio96_lse_psAll 28 shapes (B {32, 128} x ctx {4096, 8192})
a8w8_qh16_qseqlen1_gqaratio16_psa8w8_qh16_qseqlen1_gqaratio16_psa8w8_qh16_qseqlen1_gqaratio16_psa8w8_qh16_qseqlen1_gqaratio16_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa8w8_qh32_qseqlen4_gqaratio32_psa8w8_qh32_qseqlen4_gqaratio32_psa8w8_qh32_qseqlen4_gqaratio32_psa8w8_qh32_qseqlen4_gqaratio32_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa8w8_qh16_qseqlen4_gqaratio16_v3_psa16w16_qh64_qseqlen1_gqaratio64_v3_psa16w16_qh64_qseqlen1_gqaratio64_v3_psa16w16_qh64_qseqlen1_gqaratio64_v3_psa16w16_qh64_qseqlen1_gqaratio64_v3_psa16w16_qh32_qseqlen4_gqaratio32_psa16w16_qh32_qseqlen4_gqaratio32_psa16w16_qh32_qseqlen4_gqaratio32_psa16w16_qh32_qseqlen4_gqaratio32_psa16w16_qh32_qseqlen4_gqaratio96_lse_psa16w16_qh32_qseqlen4_gqaratio96_lse_psa16w16_qh32_qseqlen4_gqaratio96_lse_psa16w16_qh32_qseqlen4_gqaratio96_lse_psTesting
.cochange.Every run swapped main's or this PR's 34
.coin throughAITER_ASM_DIR, with all other kernels identical, andLoadKernelin the logs confirmed which file was used. Before and After ran the same commands.test_mla_persistent.py -d bf16 -kvd bf16 -b 1 5 32 128 -c 21 256 1200 8192with-n 16,1,128,1(+-lse),32,4(+--no-causal,--no-causal -lse),96,1(+-lse),96,2 --no-causal(+-lse);16,1,128,1,96,1also with--varlen(13 configs x 16 shapes)err == 0err == 0test_mla_persistent.py -d fp8 -kvd fp8, same shapes,-n 16,1(+-lse,--varlen),16,4(+-lse),64,1,32,4(+-lse,--varlen),8,4(+-lse) (11 configs x 16 shapes)err> 0 only at ctx 21 (~0.13-0.21) and ctx 256 (~0.001), the usual fp8 tolerance patterntest_mla_persistent_round_robin.py -cp -cpw 2 8 -b 1 32 -c 64 1200 5200, bf16-n 128,1(+-lse),96,1(+-lse),16,1 -lse; fp8-n 32,4 -lse,64,1 -lseerr> 0max_split_per_batch1 and -1; 27 dispatch configs reaching every changed kernel except the twomsk0_*cprra8w8_qh16_qseqlen4_gqaratio16_*v3_ps, which is nondeterministic run to run on main already (same input, same.co, 29/29 runs differ in O)check_fp8_gqa16_multi_tile_batch, 3 repeats, with and without LSE) formla_a8w8_qh16_qseqlen1_gqaratio16{,_lse}_ps.cogives 6064 / 1182 wrong on the same machine)Documentation
Kernel binaries only; no API, behavior or tuning change. Callers no longer need to keep KV slot 0 finite.
Dependencies
Breaking Changes
None. Kernel names, kernarg layout,
mla_asm.csvand dispatch are unchanged.PR Self-Review
Reviewed revision
6589f37
Behavior and intent
The output of the changed gfx950 persistent MLA decode kernels now depends only on the KV slots listed in
kv_indices[kv_indptr[b] : kv_indptr[b + 1]]for each row. Before this PR, the out-of-range lanes of a work item's last KV tile (and of the one-tile-ahead page-table prefetch) got page id 0 from the OOB page-table load and gathered KV slot 0. Their softmax weight is 0, but0 * Vstill made the row NaN whenever the latent part of slot 0 held a NaN. That broke callers that reuse block 0 (ROCm/ATOM#3099), and nothing documented that slot 0 must stay finite. The existing behavior is insufficient because any caller can hold non-finite bytes in an unreferenced slot. Each changed kernel clamps every page-table address to the last valid entry of its work item, so tail lanes re-read a page the work item owns.Validation and unverified scope
Validated on MI355X (gfx950), rocm/atom-dev container, with main's and this PR's
.coswapped in throughAITER_ASM_DIR:.coequals main's.coplus only clamp instructions (normalizedllvm-objdumpdiff, nothing removed). Each one also rebuilds instruction-identically from poc_kl_mergd06974a.Not verified:
mla_a16w16_qh32_qseqlen4_gqaratio{32,96}_msk0_{,lse_}cprr_psat runtime: cprr is only selected withcausal, so dispatch never reaches them. Their instruction diff is the same clamp.mla_a16w16_qh16_qseqlen4_gqaratio16_{,lse_}psandmla_a8w8_qh16_qseqlen4_gqaratio16_psat runtime: they are not inmla_asm.csv. Their instruction diff is the same clamp.gqaratio32_cprr_ps/gqaratio96_cprr_psnon-LSE variants are covered by the poisoning matrix only; the round-robin op test always requests LSE..cochanged. The kernels listed under "Not changed" keep the old behavior.PR-specific question
The changed kernels are selected only on gfx950 in persistent mode (
csrc/py_itfs_cu/asm_mla.cu,mla_asm_decode_config), by the exact tuple (qType, kvType, Gqa, ps, qSeqLen, prefill, causal, lse, cprr) inhsa/gfx950/mla/mla_asm.csv. This PR does not change that selection or any input check. The clamp only changes which page id an out-of-range lane loads, which the kernel already masks to weight 0, so in-range lanes see the same data and instruction order as before.Changed op tests
None.