Skip to content

[ASM] Fix MFMA operand race in gfx950 FP8 QH16 qseqlen4 v3 MLA decode - #7623

Open
AndreasKaratzas wants to merge 3 commits into
mainfrom
akaratza_mla_qh16_qseqlen4_mfma_order
Open

AndreasKaratzas wants to merge 3 commits into
mainfrom
akaratza_mla_qh16_qseqlen4_mfma_order

Conversation

@AndreasKaratzas

@AndreasKaratzas AndreasKaratzas commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes a register race in the gfx950 FP8 QH16 qseqlen4 v3 persistent MLA decode kernels (hsa/gfx950/mla/mla_a8w8_qh16_qseqlen4_gqaratio16*_v3_ps.co, 8 variants). In the loop copy that waves 2 and 3 run, an MFMA reads a[120:127] after LDS reads that overwrite a[120:127] have started. The PR moves the MFMA in front of the reads, as in the first loop copy. It also adds a regression check to op_tests/test_mla_persistent.py.

Motivation

These kernels run persistent fp8 MLA decode with 16 heads and 4 query tokens per row, for example MTP speculative decode with 3 draft tokens. Queries 2 and 3 of some rows get wrong output. The error is silent. With return_lse=True it is very frequent: 299 of 300 calls (128 rows each) had wrong rows under load, and 300 of 300 with no other work on the GPU.

check_fp8_gqa16_multi_tile_batch (from #7622, now on main): 128 rows, kv_len 257..383 (3 KV tiles), with a copy on a side stream. Wrong (row, query, head) outputs per run:

Kernel main this PR
qseqlen4_gqaratio16_v3_ps 457 to 960, 16 of 16 runs fail (2 GPUs) 40 of 40 runs pass (2 GPUs)
qseqlen4_gqaratio16_lse_v3_ps 8145 to 17423, 16 of 16 runs fail 40 of 40 runs pass

Root cause, from the disassembly. Waves 0 and 1 run one copy of the main loop, and waves 2 and 3 run a second copy. In each of the 4 quarters of the first copy:

v_mfma_f32_32x32x64_f8f6f4 v[172:187], a[120:127], v[44:51], v[172:187]
ds_read_b128 a[104:107], v28 offset:4096   ; 6 reads load a[104:127] for the next step
...
ds_read_b128 a[124:127], v29 offset:6144

In the second copy, the 6 ds_read_b128 come first, then 5 v_exp_f32, then the MFMA. There is no s_waitcnt between them. The MFMA must read the old a[120:127], as in the first copy. But when the LDS reads return before the MFMA reads its operands, it reads the new data. This depends on LDS latency, so it changes from call to call. A static check of the LGKM wait counters along the loop paths finds this pattern in the second loop copy only (4 sites per variant).

Related work: #7007 reports wrong, nondeterministic output of qseqlen2_gqaratio16_ps and qseqlen4_gqaratio16_v3_ps (query length 3 and 4). This PR fixes the qseqlen4 part. A separate PR fixes the qseqlen2 part (a different bug).

Related: #7617 fixes the same class of race in the non-persistent qseqlen1 kernel. #7622 (qseqlen1 persistent, merged) added the shared check_fp8_gqa16_multi_tile_batch helper to op_tests/test_mla_persistent.py; this PR and #7621 (qseqlen2) only add calls to it. #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

  • The 8 variants mla_a8w8_qh16_qseqlen4_gqaratio16_{,lse_,msk0_,msk0_lse_}{,cprr_}v3_ps.co, binary patch. The SP3 source is not in the repo. In the 4 quarters of the second loop copy, the MFMA moves up by 11 instructions, to the place right after the previous MFMA. The 6 ds_read_b128 and 5 v_exp_f32 move down by 8 bytes. The moved range is straight-line code with no label, branch or wait. The MFMA does not read or write any register that the moved instructions write, except a[104:127], which it must read before the LDS reads. (v3_ps: the MFMA moves from .text 0x623c, 0x6a4c, 0x725c, 0x7a6c to 0x61f8, 0x6a08, 0x7218, 0x7a28.) The file size, the ELF layout and the branch targets do not change.
  • After [ASM] Fix MLA PS mode nan error for kv page0 has nan data and the LTD OOB read page0 data #7872, 4 of the 8 files ({,lse_,cprr_,lse_cprr_}v3_ps) are main's KV page-table clamp rebuild with this rotation applied again; the instruction diff against main is exactly the 4 moved MFMAs per file. The 4 msk0_* files were not touched by [ASM] Fix MLA PS mode nan error for kv page0 has nan data and the LTD OOB read page0 data #7872.
  • op_tests/test_mla_persistent.py: main() calls the existing check_fp8_gqa16_multi_tile_batch on gfx950 for qlen 4, with and without LSE (3 lines).

The kernel owners may prefer to rebuild the .co files from SP3 with the first-copy order in both loop copies. The new check covers that rebuild too.

Performance (if applicable)

mla_decode_fwd, fp8, 16 heads, run_perftest 200 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.

Hardware Workload/configuration Before After Change
MI350X qlen 4, persistent, bs 1, ctx 1024 18.20 18.48 +1.6%
MI350X qlen 4, persistent, bs 1, ctx 8192 27.20 27.55 +0.1%
MI350X qlen 4, persistent, bs 32, ctx 1024 27.18 27.50 +0.6%
MI350X qlen 4, persistent, bs 32, ctx 4096 40.83 41.04 +0.9%
MI350X qlen 4, persistent, bs 128, ctx 1024 43.05 43.39 +0.8%
MI350X qlen 4, persistent, bs 128, ctx 4096 87.59 87.94 +0.2%
MI350X qlen 4, persistent, bs 256, ctx 8192 265.48 266.44 +0.1%
MI350X qlen 4, persistent, LSE, bs 1, ctx 1024 18.18 18.44 +0.3%
MI350X qlen 4, persistent, LSE, bs 1, ctx 8192 27.28 27.20 +0.3%
MI350X qlen 4, persistent, LSE, bs 32, ctx 1024 27.29 27.46 +0.5%
MI350X qlen 4, persistent, LSE, bs 32, ctx 4096 40.81 40.65 -0.1%
MI350X qlen 4, persistent, LSE, bs 128, ctx 1024 43.40 43.86 +0.9%
MI350X qlen 4, persistent, LSE, bs 128, ctx 4096 89.14 88.27 -0.2%
MI350X qlen 4, persistent, LSE, bs 256, ctx 8192 265.01 267.49 +0.2%

All medians are within -0.2% and +1.6%. The patch moves one instruction in each of 4 places and adds none.

Testing

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

The .co files are 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

b3ac1fa

Behavior and intent

In the loop copy of waves 2 and 3, the MFMA now reads a[120:127] before the LDS reads for the next step overwrite it, as in the first loop copy. Before, queries 2 and 3 of some rows were wrong and nondeterministic.

Validation and unverified scope

MI350X (gfx950), PyTorch 2.13.0+rocm10.0.0, aiter main 581579a.

Heads with error > 0.1 (128 rows, kv_len 257..384, 300 calls per run):

.co v3_ps, stream lse_v3_ps, stream lse_v3_ps, no other work
main 4593 76016 45166
this PR 0 0 0
this PR, all s_waitcnt set to 0 0 0 0

In the last row, making every wait strict on top of the fix does not change the result in these runs.

Variants (300 calls, stream, heads with error > 0.1, main / this PR). The log of each run shows which .co was loaded.

Variant Call main this PR
msk0_v3_ps causal=False 602 0
msk0_lse_v3_ps causal=False, LSE 1749 0
cprr_v3_ps CP 2, rank 0, kv_len 513..768 260 0
lse_cprr_v3_ps CP 2, rank 1, LSE 6438 0
msk0_cprr_v3_ps CP 2, rank 1, causal=False 169 0
msk0_lse_cprr_v3_ps CP 2, rank 0, causal=False, LSE 2184 0

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/.

  • 10 loops on each of 2 GPUs, in 3 modes (no other work, a 256 MiB read before each call, copies on a side stream), 200 calls of 128 rows per run: 0 wrong outputs in 12000 calls for v3_ps and for lse_v3_ps. Some heads have an error between 0.05 and 0.1 (205 and 246 heads in 12000 calls). This is fp8 rounding: the same count shows when every s_waitcnt is set to 0.
  • Bitwise check: with 4 rows and no other work, this PR gives the same bits as main for every call where main repeats itself (28 of 28 and 26 of 26).
  • DCP-like and long-context shapes, no other work, kv_len 300 to 8200 with 1 to 32 splits per row: main has wrong rows in every shape with LSE (for example 24865 heads in 200 calls at 64 rows, kv_len 1000, 4 splits). This PR: 0 in every shape, with and without LSE.
  • python3 op_tests/test_mla_persistent.py -n 16,4 -d fp8 -kvd fp8 with and without -lse: the new check passes. The sweep has the same 13 of 64 rows above 0.05 decode:err as main (short contexts, max 0.18 on both).

After merging main (95d2112, including #7872), on MI355X (gfx950), merged hsa/gfx950/mla loaded as an overlay, main .co vs this PR:

Not verified:

  • After the [ASM] Fix MLA PS mode nan error for kv page0 has nan data and the LTD OOB read page0 data #7872 merge, the cprr and msk0 variants were checked at the binary level only (same rotation in the same windows); the GPU runs above cover v3_ps and lse_v3_ps.
  • The prologue has a similar pattern: 18 K reads, then s_waitcnt lgkmcnt(4), then MFMAs that use the last 4 reads with more reads in between and no wait. There are 6 MFMAs of 64 cycles each before the first use, so we did not see a failure there, and we did not change it.
  • gfx942 kernels: not tested.
  • No end-to-end model accuracy run.

PR-specific question

Only gfx950 fp8 MLA decode with fp8 Q, fp8 KV and persistent mode reaches these kernels. mla_asm_decode_remap in csrc/py_itfs_cu/asm_mla.cu maps three shapes to Gqa=16 and qSeqLen=4: gqa 16 with max_seqlen_q 3 or 4, gqa 32 with max_seqlen_q 2 or 3, and gqa 64 with max_seqlen_q 1. The kernel is then looked up in hsa/<arch>/mla/mla_asm.csv by (qType, kvType, Gqa, ps, qSeqLen, prefill, causal, lse, cprr). Only hsa/gfx950/mla/mla_asm.csv has qh16_qseqlen4_gqaratio16*_v3_ps rows, all with ps=1. causal selects msk0, persistent return_lse selects lse, and a g_kv_indptr (context parallel round-robin) selects cprr. max_seqlen_q 1 always uses a causal kernel, so gqa 64 does not reach msk0. Other inputs cannot reach the patched code: gfx942 reads its own CSV, which has no such row; non-persistent calls use ps=0 rows (qh64_qseqlen4, not changed); gqa 32 with max_seqlen_q 4 or more, gqa 64 with max_seqlen_q 2 or more, gqa 96 and gqa 128 go to gqa 32 kernels; and bf16 Q or KV matches no fp8 row. The patch does not change any kernel argument, launch configuration, workspace or the CSV, so this selection is the same as on main.

Changed op tests

op_tests/test_mla_persistent.py: main() now also calls the existing check_fp8_gqa16_multi_tile_batch (from #7622) with qlen 4, with and without LSE. The helper runs 64 calls of 128 rows with 3 KV tiles each, next to a 256 MiB device-to-device copy on a side stream (the file sets torch.set_default_device("cuda")), and compares every (row, query, head) output with an fp32 reference. The existing calls cover qlen 1 only, and the existing sweeps use small batches and no concurrent work, so they pass on main. The qlen 4 calls fail on main in every run and pass with this PR. They take about 1.5 s per kernel.

AI assistance (Claude Code) was used for the analysis and the patch. A human reviewed the change.

Waves 2 and 3 of the qseqlen4 v3 persistent kernels run a second copy
of the main loop. In that copy, the six ds_read_b128 that load
a[104:127] for the next step come before the MFMA that still reads the
old a[120:127]. There is no s_waitcnt between them, so the MFMA reads
old or new data, depending on LDS latency. Queries 2 and 3 then get
wrong output. The first copy of the loop has the MFMA before the
reads.

In the 4 quarters of the second loop copy, move the MFMA in front of
the ds_reads, as in the first copy. This is an 8-byte rotation inside
straight-line code with no label, branch or wait. The MFMA does not
depend on the instructions that it moves past.

All 8 variants (v3_ps, lse, msk0, cprr and their combinations) have
the same code and get the same change. The file size, the ELF layout
and the branch targets do not change.

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]>
@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 7623 --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

aiter-bot commented Nov 9, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision b3ac1fae4dbf · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

In the loop copy of waves 2 and 3, the MFMA now reads a[120:127] before the LDS reads for the next step overwrite it, as in the first loop copy. Before, queries 2 and 3 of some rows were wrong and nondeterministic.

Validation and unverified scope

MI350X (gfx950), PyTorch 2.13.0+rocm10.0.0, aiter main 581579ab.

Heads with error > 0.1 (128 rows, `kv_len` 257..384, 300 calls per run):

| `.co` | `v3_ps`, `stream` | `lse_v3_ps`, `stream` | `lse_v3_ps`, no other work |
|---|---|---|---|
| main | 4593 | 76016 | 45166 |
| this PR | 0 | 0 | 0 |
| this PR, all `s_waitcnt` set to 0 | 0 | 0 | 0 |

In the last row, making every wait strict on top of the fix does not change the result in these runs.

Variants (300 calls, `stream`, heads with error > 0.1, main / this PR). The log of each run shows which `.co` was loaded.

| Variant | Call | main | this PR |
|---|---|---|---|
| `msk0_v3_ps` | `causal=False` | 602 | 0 |
| `msk0_lse_v3_ps` | `causal=False`, LSE | 1749 | 0 |
| `cprr_v3_ps` | CP 2, rank 0, `kv_len` 513..768 | 260 | 0 |
| `lse_cprr_v3_ps` | CP 2, rank 1, LSE | 6438 | 0 |
| `msk0_cprr_v3_ps` | CP 2, rank 1, `causal=False` | 169 | 0 |
| `msk0_lse_cprr_v3_ps` | CP 2, rank 0, `causal=False`, LSE | 2184 | 0 |

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/`.

- 10 loops on each of 2 GPUs, in 3 modes (no other work, a 256 MiB read before each call, copies on a side stream), 200 calls of 128 rows per run: 0 wrong outputs in 12000 calls for `v3_ps` and for `lse_v3_ps`. Some heads have an error between 0.05 and 0.1 (205 and 246 heads in 12000 calls). This is fp8 rounding: the same count shows when every `s_waitcnt` is set to 0.
- Bitwise check: with 4 rows and no other work, this PR gives the same bits as main for every call where main repeats itself (28 of 28 and 26 of 26).
- DCP-like and long-context shapes, no other work, `kv_len` 300 to 8200 with 1 to 32 splits per row: main has wrong rows in every shape with LSE (for example 24865 heads in 200 calls at 64 rows, `kv_len` 1000, 4 splits). This PR: 0 in every shape, with and without LSE.
- `python3 op_tests/test_mla_persistent.py -n 16,4 -d fp8 -kvd fp8` with and without `-lse`: the new check passes. The sweep has the same 13 of 64 rows above 0.05 `decode:err` as main (short contexts, max 0.18 on both).

After merging main (95d211284, including #7872), on MI355X (gfx950), merged `hsa/gfx950/mla` loaded as an overlay, main `.co` vs this PR:

- `check_fp8_gqa16_multi_tile_batch(4)`, 10 runs: main fails 10/10 (36-1121 wrong outputs per run); this PR 0/10.
- `check_fp8_gqa16_multi_tile_batch(4, return_lse=True)`, 10 runs: main fails 10/10 (~14k-18k wrong outputs per run); this PR 0/10.
- #7872 scenario (NaN in unreferenced KV slot 0, 5 shapes, with and without LSE): 10/10 pass with 0 NaN rows, bit-identical to clean runs. The pre-#7872 `.co` gives 192-6144 NaN rows, so the clamp is kept.
- Latency medians (bs32/kv2k, bs128/kv4k, bs64/kv16k) match main within noise.
- CI on 9b8ca874ad69: all checks pass. b3ac1fae4dbf only restores the executable bit (100755, as on main) on the 8 `.co` files; their contents are unchanged, so the runs above still apply.

Not verified:

- After the #7872 merge, the `cprr` and `msk0` variants were checked at the binary level only (same rotation in the same windows); the GPU runs above cover `v3_ps` and `lse_v3_ps`.
- The prologue has a similar pattern: 18 K reads, then `s_waitcnt lgkmcnt(4)`, then MFMAs that use the last 4 reads with more reads in between and no wait. There are 6 MFMAs of 64 cycles each before the first use, so we did not see a failure there, and we did not change it.
- gfx942 kernels: not tested.
- No end-to-end model accuracy run.

PR-specific question

Only gfx950 fp8 MLA decode with fp8 Q, fp8 KV and persistent mode reaches these kernels. `mla_asm_decode_remap` in `csrc/py_itfs_cu/asm_mla.cu` maps three shapes to `Gqa=16` and `qSeqLen=4`: gqa 16 with `max_seqlen_q` 3 or 4, gqa 32 with `max_seqlen_q` 2 or 3, and gqa 64 with `max_seqlen_q` 1. The kernel is then looked up in `hsa/<arch>/mla/mla_asm.csv` by (qType, kvType, Gqa, ps, qSeqLen, prefill, causal, lse, cprr). Only `hsa/gfx950/mla/mla_asm.csv` has `qh16_qseqlen4_gqaratio16*_v3_ps` rows, all with `ps=1`. `causal` selects `msk0`, persistent `return_lse` selects `lse`, and a `g_kv_indptr` (context parallel round-robin) selects `cprr`. `max_seqlen_q` 1 always uses a causal kernel, so gqa 64 does not reach `msk0`. Other inputs cannot reach the patched code: gfx942 reads its own CSV, which has no such row; non-persistent calls use `ps=0` rows (`qh64_qseqlen4`, not changed); gqa 32 with `max_seqlen_q` 4 or more, gqa 64 with `max_seqlen_q` 2 or more, gqa 96 and gqa 128 go to gqa 32 kernels; and bf16 Q or KV matches no fp8 row. The patch does not change any kernel argument, launch configuration, workspace or the CSV, so this selection is the same as on main.

Changed op tests

`op_tests/test_mla_persistent.py`: `main()` now also calls the existing `check_fp8_gqa16_multi_tile_batch` (from #7622) with qlen 4, with and without LSE. The helper runs 64 calls of 128 rows with 3 KV tiles each, next to a 256 MiB device-to-device copy on a side stream (the file sets `torch.set_default_device("cuda")`), and compares every (row, query, head) output with an fp32 reference. The existing calls cover qlen 1 only, and the existing sweeps use small batches and no concurrent work, so they pass on main. The qlen 4 calls fail on main in every run and pass with this PR. They take about 1.5 s per kernel.

AI assistance (Claude Code) was used for the analysis and the patch. A human reviewed the change.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

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

@ChuanLi1101

Copy link
Copy Markdown
Contributor

Please:

  1. Rebase — PR is CONFLICTING.
  2. Fill PR Self-Review (head SHA + the PR-specific question).
  3. Nit: torch.zeros(64 << 20) / torch.empty(...) are CPU, so the “load” is H2D, not a device copy. .cuda() would match the comment.
  4. check_fp8_gqa16_multi_tile_batch is duplicated with [MLA] [Test] Check the gfx950 FP8 QH16 qseqlen2 MLA decode for LDS races #7621; if both land, extract once.

#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]>
@AndreasKaratzas

Copy link
Copy Markdown
Contributor Author

Updated with main, verified on MI355X (gfx950)

Conflict with #7872. #7872 rebuilt 4 of the 8 kernels in this PR from SP3 sources with the KV page-table clamp: mla_a8w8_qh16_qseqlen4_gqaratio16_{,lse_,cprr_,lse_cprr_}v3_ps.co. Taking either side would drop one of the two fixes. For each of the 4 files I took main's clamped binary and applied this PR's rotation to it again:

  • Instruction by instruction, main's rebuild is this PR's base plus 17 clamp instructions (s_sub_u32 s46, s_lshl_b32 s46, 15x v_min_u32 v34, s46, v34). None of them falls in or next to the patched code, so all 4 windows per file show up unchanged, just at addresses shifted by 0x34-0x40.
  • In each window, the v_mfma_f32_32x32x64_f8f6f4 v[172:187], a[120:127], ... is moved again ahead of the six ds_read_b128 that overwrite a[104:127] (76-byte window, straight-line code).
  • Checks on main vs merged .co: the disassembly differs only in those 4 rotations per file (168 bytes). File size, ELF/program/section headers, notes and the set of branch targets are identical, and no branch target falls inside a moved window. The instruction diff of this PR vs merged is exactly [ASM] Fix MLA PS mode nan error for kv page0 has nan data and the LTD OOB read page0 data #7872's clamp.
  • The 4 msk0_* variants were not touched by main, so they keep this PR's versions.
  • op_tests/test_mla_persistent.py: both the qlen 1 ([ASM] Fix LDS races in gfx950 FP8 QH16 qseqlen1 persistent MLA decode #7622) and qlen 4 calls of check_fp8_gqa16_multi_tile_batch are kept.

MI355X results (one GPU, merged hsa/gfx950/mla loaded as an overlay):

Check main .co merged .co
check_fp8_gqa16_multi_tile_batch(4), 10 runs 10/10 runs fail (36-1121 wrong outputs/run) 0/10 fail
check_fp8_gqa16_multi_tile_batch(4, return_lse=True), 10 runs 10/10 runs fail (~14k-18k wrong outputs/run) 0/10 fail
#7872 scenario: NaN in unreferenced KV slot 0, 5 shapes x {lse, no lse} 10/10 pass 10/10 pass: 0 NaN rows, bit-identical to clean runs
Latency, median us (bs32/kv2k, bs128/kv4k, bs64/kv16k) 85.4-86.5 / 133.9-134.9 / 196.2-196.8 85.0-85.6 / 133.9-134.7 / 196.4-196.9

As a control, the pre-#7872 .co gives 192-6144 NaN rows in the KV slot 0 scenario, so the clamp is still in place after the merge. Latency is unchanged within noise. The GPU checks cover the causal v3_ps and lse_v3_ps kernels; the cprr and msk0 variants were checked at the binary level only (same rotation, same windows).

AI assistance (Claude) was used for the binary re-patch and the verification scripts.

@AndreasKaratzas

Copy link
Copy Markdown
Contributor Author

@ChuanLi1101 thanks for the review. All four points are addressed:

  1. Rebase: main (95d2112, including [ASM] Fix MLA PS mode nan error for kv page0 has nan data and the LTD OOB read page0 data #7872) is merged in at 9b8ca87. The conflict was with [ASM] Fix MLA PS mode nan error for kv page0 has nan data and the LTD OOB read page0 data #7872's rebuild of 4 of the 8 .co files; details and MI355X re-verification are in the comment above. The PR now merges cleanly and CI is green.
  2. PR Self-Review: filled in for 9b8ca87, including the PR-specific question (which arch/dtype/shape inputs reach these kernels and why nothing else can).
  3. CPU buffers: op_tests/test_mla_persistent.py sets torch.set_default_device("cuda") at module level (line 19), so torch.zeros(64 << 20) / torch.empty(64 << 20) are 256 MiB device tensors and the side-stream load is a device-to-device copy, as the docstring says. That code is now on main from [ASM] Fix LDS races in gfx950 FP8 QH16 qseqlen1 persistent MLA decode #7622; this PR doesn't touch it.
  4. Duplicated helper: [ASM] Fix LDS races in gfx950 FP8 QH16 qseqlen1 persistent MLA decode #7622 landed check_fp8_gqa16_multi_tile_batch once on main. This PR (and [MLA] [Test] Check the gfx950 FP8 QH16 qseqlen2 MLA decode for LDS races #7621) now only add the calls for their own qlen, so there is a single copy.

Could you take another look when you have a moment?

The regenerated gfx950 MLA qh16/qseqlen4 code objects were committed
as 100644, while main and the other .co files under hsa/ are 100755.
Restore the original mode so this PR only changes file contents.

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Andreas Karatzas <[email protected]>
@AndreasKaratzas

Copy link
Copy Markdown
Contributor Author

@ChuanLi1101 the four points from your review are addressed (see #7623 (comment)); I also restored the executable bit on the .co files. Could you take another look?

@ChuanLi1101 ChuanLi1101 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.

Could you confirm: (1) whether the cprr and msk0 variants can get the same MI355X runtime checks as v3_ps and lse_v3_ps, and (2) whether someone with ASM background can review the rotation windows?

@AndreasKaratzas

Copy link
Copy Markdown
Contributor Author

yeah, ran them on MI350X, 10 runs each. all 8 are reachable (cprr when g_kv_indptr is set, msk0 with causal=False):

main this PR
v3_ps / lse 10/10 runs bad 0/10
cprr / lse_cprr 8/10, 10/10 0/10
msk0 / msk0_lse 6/10, 10/10 0/10
msk0_cprr / msk0_lse_cprr 8/10, 10/10 0/10

latency's the same. for the asm side, the disasm diff is just the 4 rotations per file and nothing else, minmengdie did the msk0/cprr variants and the last rebuild so probably best placed to look at the windows.

@AndreasKaratzas

Copy link
Copy Markdown
Contributor Author

@minmengdie @shengnxu could one of you look at the asm side? it's the 4 rotation windows per .co (the mfma moved ahead of the six ds_read_b128 that overwrite a[104:127]), plus the prologue lgkmcnt(4) pattern i left alone. this also still needs a team_aiter approval to merge.

@ChuanLi1101 on your question: all 8 variants (v3_ps, cprr, msk0, msk0_cprr, each with and without lse) ran 10x on MI350X. on MI355X only v3_ps and lse_v3_ps ran, so cprr and msk0 have no MI355X run yet.

This branch has not been deployed

No deployments
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