Skip to content

MLA v4 nm: faster cross-split merge for small decode grids - #5776

Open
AMD-yanfeiwang wants to merge 3 commits into
ROCm:mainfrom
AMD-yanfeiwang:perf/mla-v4nm-stage2-dchunk
Open

AMD-yanfeiwang wants to merge 3 commits into
ROCm:mainfrom
AMD-yanfeiwang:perf/mla-v4nm-stage2-dchunk

Conversation

@AMD-yanfeiwang

@AMD-yanfeiwang AMD-yanfeiwang commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

In mla_decode_fwd_v4_nm, the cross-split merge _fwd_kernel_stage2_asm runs one 1-wave CTA per (seq, head) and walks the splits serially. On a small decode grid that leaves most of the GPU idle: at 1 seq × 16 heads × 16 splits the merge is about 5 µs of a 19 µs call (MI355X).

Who runs this path today:

Changes

  • Add _fwd_kernel_stage2_dchunk. Same math as _fwd_kernel_stage2_asm, with two paths per (seq, head):
    • Tile: Lv is split over a third grid axis and the splits are reduced as one [MAX_SPLITS, D_CHUNK] tile.
    • Short KV: when only a few of the planned splits are valid, the extra CTAs cost more than a serial merge. Chunk 0 then merges the whole row four splits at a time and the other chunks exit. The valid count is written by stage 1 on the device, so the host cannot make this choice.
  • mla_decode_fwd_v4_nm uses it on gfx950 when num_seqs * num_heads <= 2048 and num_kv_splits <= 16 (the auto-pick cap). Larger grids are bandwidth-bound and keep _fwd_kernel_stage2_asm; so do other archs (not measured).
  • A sequence with no valid split gets O = 0, same as _fwd_kernel_stage2_asm since [ASM] [HIP] [JIT] mla version 6 #6394.
  • Tests:
    • test_mla_stage2_merge.py: add a dchunk candidate and a --valid axis (short KV). base now runs as production launches it (MAYBE_FINAL_OUT=False), and a mismatch fails the test instead of only being logged.
    • test_mla_v4_nm.py: add test_v4_nm_empty_seq_multi_split (empty and short sequences on both merges).

Results (MI355X)

Measured on lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261118 with this PR's aiter/mla.py on its aiter (581579ab5).

Whole mla_decode_fwd_v4_nm call (stage 1 + merge), CUDA-graph replay, auto split count, µs, main → this PR:

heads seqs kv 32 kv 2048 kv 8192
16 1 12.87 → 13.02 (0.99×) 19.0 → 15.7 (1.21×) 28.6 → 25.4 (1.13×)
16 8 12.93 → 13.20 (0.98×) 17.8 → 16.0 (1.11×) 30.0 → 27.0 (1.11×)
16 16 13.09 → 13.21 (0.99×) 21.0 → 17.8 (1.18×) 32.1 → 29.7 (1.08×)
16 32 13.22 → 13.27 (1.00×) 24.9 → 23.6 (1.06×) 42.6 → 41.3 (1.03×)
16 128 14.43 → 14.28 (1.01×) 42.4 → 42.1 (1.01×) 133.8 → 133.0 (1.01×)
128 1 15.17 → 15.29 (0.99×) 23.4 → 19.4 (1.21×) 33.3 → 29.0 (1.15×)
128 8 16.07 → 16.28 (0.99×) 30.4 → 28.4 (1.07×) 40.3 → 38.0 (1.06×)
128 16 17.39 → 17.58 (0.99×) 32.2 → 30.8 (1.05×) 52.9 → 51.9 (1.02×)
  • With the split count from get_mla_v4_nm_num_kv_splits, 16 heads, kv 2048: 1.29× at 1 seq, 1.06× at 8, 1.09× at 16, 1.05× at 32.
  • Over heads 16/32/64/128, 1–128 seqs, kv 16–32768 and three split modes (734 cells that take the new merge): 0.96–1.36×, median 1.02×; 1.07× median where the tile runs. Run-to-run noise is about ±2%. Cells that keep the loop kernel are bit-identical to main.

Merge only (test_mla_stage2_merge.py, all splits valid), µs, base → dchunk:

heads seqs 4 splits 8 splits 16 splits
16 1 3.12 → 2.54 (1.23×) 3.66 → 2.64 (1.39×) 4.79 → 3.04 (1.58×)
16 8 3.10 → 2.48 (1.25×) 3.71 → 2.74 (1.36×) 4.95 → 3.11 (1.59×)
16 64 3.57 → 3.06 (1.17×) 4.30 → 3.40 (1.27×) 7.24 → 4.83 (1.50×)
128 8 3.64 → 3.26 (1.12×) 4.46 → 3.62 (1.23×) 7.39 → 4.84 (1.53×)
128 16 4.34 → 3.85 (1.13×) 6.14 → 4.95 (1.24×) 13.78 → 12.05 (1.14×)

Inside the dispatch range: 1.02–1.65× with all splits valid (median 1.24×), 1.05–1.31× with 6 valid splits, 0.92–1.07× with 1 valid split (median 1.00×).

End to end, SGLang with DeepSeek-V4-Flash, TP2 (32 local heads), fp8 unified_kv, SGLANG_DSV4_FLYDSL_DECODE=0, CUDA graphs: decode ITL p50 at concurrency 1 goes from 7.541 → 7.459 ms (1.5k prompt) and 7.586 → 7.494 ms (8k prompt); concurrency 8 and 32 are within noise. GSM8K is unchanged (0.940–0.943 → 0.939–0.945). These runs used an earlier revision of this branch with the same kernel and launch parameters. ATOM was not measured end to end.

Scope

  • This is a low-concurrency optimization: about 1% ITL at concurrency 1, and only where decode goes through mla_decode_fwd_v4_nm. With speculative decoding the row count is several times the concurrency.
  • Short KV is not faster. With 1 valid split the call is 0.96–1.01× of main (at most +0.6 µs).
  • The output is not bit-identical to the loop kernel, because the sum order differs: fewer than 0.1% of the bf16 output elements move, by about one rounding step. Both kernels are equally close to a float64 merge.

Test

python3 op_tests/test_mla_stage2_merge.py   # 80 rows, all match
pytest op_tests/test_mla_v4_nm.py           # 53 passed (50 existing + 3 new)

Rebased on main (a485545d3). Tests were run with this branch's three files on the aiter in the image above (gfx950). A source build of main plus an earlier revision of this branch also passed test_pa_decode_sparse.py and tests/test_plugin*.py.

PR Self-Review

Reviewed revision

1dc78ddaecd9

Behavior and intent

On gfx950, mla_decode_fwd_v4_nm now merges the split partials with _fwd_kernel_stage2_dchunk when num_seqs * num_heads <= 2048 and num_kv_splits <= 16. The loop kernel runs one 1-wave CTA per (seq, head) and leaves a small decode grid mostly idle. Other archs, larger grids and larger split counts keep _fwd_kernel_stage2_asm. The output is not bit-identical to the loop kernel (different sum order).

Validation and unverified scope

MI355X (gfx950), image lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261118, this branch's three files on its aiter:

  • pytest op_tests/test_mla_v4_nm.py: 53 passed. python3 op_tests/test_mla_stage2_merge.py: 80 rows, all match the torch reference.
  • Fuzz against a float64 merge and the loop kernel (2800 standalone cases, 1199 end-to-end calls): no NaN, no unwritten or out-of-range element; all 5 launch variants compile without VGPR spill.
  • Whole call: 0.96–1.36× of main over 734 cells; SGLang DeepSeek-V4-Flash TP2 ITL p50 −1.1% to −1.2% at concurrency 1.

Unverified: the end-to-end numbers are from an earlier revision with the same kernel and launch parameters. ATOM was not run end to end. gfx942 and gfx1250 are not measured, and keep the loop kernel. Sequences with more than one query row are covered by the fuzz only, not by a test in the tree.

PR-specific question

Both tests cover a kernel that does not exist before this PR, so neither can fail on main. test_v4_nm_empty_seq_multi_split fails when the zero guard of the short-KV path is removed (checked): a kv_len = 0 sequence then gives 0/0 = NaN instead of 0. The dchunk rows of test_mla_stage2_merge.py with --valid 6 are the only test that runs more than one block of the short-KV loop; dropping its rescale makes them fail (checked).

Changed op tests

  • op_tests/test_mla_stage2_merge.py: adds the dchunk candidate next to base and vec, and a --valid axis for a short KV. It covers the tile path, a partly valid tile and the multi-block short path against a torch reference; no other test launches the merge kernel alone. A mismatch now fails the test instead of only being logged.
  • op_tests/test_mla_v4_nm.py: adds test_v4_nm_empty_seq_multi_split (3 cases): empty and short sequences through the whole call on a small dchunk grid, a larger dchunk grid and a loop-kernel grid. It is the only test where stage 1 leaves valid_split_count unwritten for an empty sequence.

@github-actions

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; main branch always runs both MI35X and MI300X
multigpu 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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 5776 --add-label <label>

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 opt this PR out.

@AMD-yanfeiwang
AMD-yanfeiwang marked this pull request as ready for review September 23, 2026 08:07
@AMD-yanfeiwang
AMD-yanfeiwang requested a review from a team September 23, 2026 08:07
@github-actions github-actions Bot changed the title [Triton] MLA v4 nm: faster cross-split merge for small decode grids MLA v4 nm: faster cross-split merge for small decode grids Sep 23, 2026
@zufayu
zufayu requested a review from amd-ruitang3 September 24, 2026 01:46
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Oct 9, 2026
@zufayu
zufayu requested review from fangche123 and removed request for amd-ruitang3 October 15, 2026 08:46
@zufayu zufayu assigned fangche123 and unassigned amd-ruitang3 Oct 15, 2026
@zufayu
zufayu requested a review from amd-ruitang3 October 15, 2026 08:47
@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Oct 16, 2026
@fangche123

Copy link
Copy Markdown
Contributor

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

ROCm/aiter PR #5776 — MLA v4 nm: faster cross-split merge for small decode grids

Adds a second stage-2 cross-split merge kernel for v4 nm decode (_fwd_kernel_stage2_dchunk, a 3-axis grid that reduces all splits as one [MAX_SPLITS, D_CHUNK] tile) and routes mla_decode_fwd_v4_nm to it when num_seqs * num_heads <= 2048, keeping the existing serial kernel above that.

Review (advisory): 🔴 HIGH RISK
Validation (deterministic): NOT RUN — auto-validation disabled (REVIEW_AUTO_VALIDATE=0) and this box has no torch/triton python environment (import torch fails) with all 8 GPUs at 98–99% VRAM, so the triaged target op_tests/test_mla_stage2_merge.py could not be run; environment gap, not a PR defect
Perf (advisory): NOT RUN — same environment gap (no torch/triton env, GPUs full); the PR's own MI355X merge table (e.g. 16 heads × 8 seqs: 3.82 → 3.10 µs at 4 splits, 9.17 → 4.29 µs at 32) is the only measurement and was not re-run here

🔴 [verified divergence, reachability inferred] _fwd_kernel_stage2_dchunk divides by tl.sum(w, axis=0) unguarded (aiter/mla.py:319): at num_valid == 0 — a sequence with kv_indptr[b] == kv_indptr[b+1] (kv_len=0) or valid_split_count[b] == 0 — inside the num_seqs * num_heads <= 2048 dispatch, w is all-zero and 0/0 = NaN is stored to that row, while the merge target's _fwd_kernel_stage2_asm now defines exactly that row as 0.0 (tl.where(e_sum > 0, acc / e_sum, 0.0), "No valid split (empty sequence)", added to main in #6394 on 2026-10-12, after this PR branched) — and no test in the PR generates num_valid == 0 (test_mla_stage2_merge.py always sets kv_len = N * MGC, so its NaN-scribble branch is dead). If an empty-KV sequence ever reaches a small decode grid — main's maintainers added the asm-side guard for that case, and the _ps sibling documents empty KV ranges as a real input — the row is NaN instead of 0. Author must add the same zero-guard to the dchunk store, or cite a test proving no caller can pass an empty sequence.

🔴 [verified] The changed merge is on the default path of a downstream-consumed op — the PR body itself benchmarks SGLang DeepSeek-V4-Pro TP8 fp8 unified_kv through mla_decode_fwd_v4_nm (aiter/mla.py:2010), and op_tests/test_mla_v4_nm.py names ATOM's OP5 sparse_attn_v4_paged_decode as the same production path — but the PR carries no ci:* label, so Sglang Downstream (sglang_downstream.yaml:27) and Atom Test (atom-test.yaml:34) are both skipped and the change can merge green having never run downstream. Reviewer should ask for ci:all (DeepSeek is exercised by atom+sglang+vllm) and require it green before merge.

🔴 [verified] aiter/mla.py is a Tier-2 downstream contract and this diff changes the default dispatch of mla_decode_fwd_v4_nm's stage-2 merge — grids with num_seqs * num_heads <= 2048 now run a different kernel with a different reduction order — which is not a pure-additive, arch-gated, behavior-preserving change, yet the PR has no reviews and the author is not the path's de-facto owner (top committer of aiter/mla.py is minmengdie at 20 commits; the author is third at 8). Reviewer must @-mention @minmengdie in a PR comment requesting explicit sign-off on the dispatch change before merge, on top of the downstream CI label above.

📝 [verified] The tile-cap loop floors at d_chunk > 64 (aiter/mla.py:343), so at an explicit num_kv_splits of 65–128 (max_splits = 128) the tile is 128 × 64 = 8192 fp32 elements = 128 VGPRs/lane at num_warps=1 — double the "4K fp32 elements (64 VGPRs/lane)" cap the comment at aiter/mla.py:341 claims to enforce; the auto-pick never reaches it (get_meta_param caps its search at 16), so this is an override-only configuration. Reviewer should ask whether num_kv_splits > 64 is a supported configuration and, if so, lower the floor or correct the comment.

📝 [inferred] Every number in the PR is measured on MI355X, while the always-on CI runs gfx942 + gfx950 correctness only — the 2048 dispatch threshold and the 256/128 d_chunk picks are single-arch tunings, and the PR's own above-threshold rows (0.76–1.02×) show the crossover is grid- and CU-dependent. Reviewer should ask for a gfx942/gfx950 spot-check inside the dispatched range (e.g. 16 heads × 8/64 seqs × 16/32 splits via op_tests/test_mla_stage2_merge.py) to confirm dchunk is not slower there.

@ROCm ROCm deleted a comment from aiter-bot Oct 28, 2026
@AMD-yanfeiwang
AMD-yanfeiwang force-pushed the perf/mla-v4nm-stage2-dchunk branch from 0feea40 to 508efde Compare October 28, 2026 03:05
@aiter-bot

Copy link
Copy Markdown

ROCm/aiter PR #5776 — MLA v4 nm: faster cross-split merge for small decode grids

Adds a second stage-2 cross-split merge for v4 nm decode (_fwd_kernel_stage2_dchunk, which chunks Lv across a third grid axis and reduces all splits as one [MAX_SPLITS, D_CHUNK] tile instead of a serial online-softmax loop) and makes it the default merge in mla_decode_fwd_v4_nm for gfx950 grids with num_seqs * num_heads <= 2048, keeping the existing kernel everywhere else.

Review (advisory): 🔴 HIGH RISK
Validation (deterministic): NOT RUN — triage required a runtime target (runtime surface aiter/mla.py) but selected none (2 candidate targets, op_tests/test_mla_stage2_merge.py and op_tests/test_mla_v4_nm.py, no unique added one), auto-validation is disabled (REVIEW_AUTO_VALIDATE=0), and this box has no torch/triton python env (import torch fails) with all 8 GPUs at 98–99% VRAM, so nothing could be run by hand either — environment gap, not a PR defect
Perf (advisory): NOT RUN — same environment gap (no torch env, GPUs full); the only base-vs-head numbers are the author's own MI355X tables (merge-only 1.00–2.14× over 35 in-range cases, whole-call 1.00–1.25×, e2e ITL 13.94 → 13.74 ms on SGLang DSv4-Pro TP8), not re-run here

🔴 [verified] _fwd_kernel_stage2_dchunk becomes the default stage-2 merge for every gfx950 decode grid with num_seqs * num_heads <= 2048 in mla_decode_fwd_v4_nm (aiter/mla.py:2693) — the op SGLang drives for DSv4-Pro fp8 unified_kv (the PR's own e2e benchmark) and ATOM's OP5 sparse_attn_v4_paged_decode (op_tests/test_mla_v4_nm.py:1035) — but the PR carries no ci:* label, so Sglang Downstream and Atom Test are both skipped and this default-path change can merge green having never run downstream (the same gap that let an MLA decode change break ATOM only after merge, aiter#3459). Reviewer should ask for ci:all (DeepSeek is exercised by atom+sglang+vllm) and require it green before merge.
🔴 [verified] aiter/mla.py is a Tier-2 downstream contract and this PR changes the default dispatch of its stage-2 merge (small gfx950 grids now reduce the splits in a different kernel with a different fp32 reduction order), yet the PR has no reviews and the author (Fei.Wang, 8 commits to aiter/mla.py) is not the path's de-facto owner (minmengdie, 20 commits; valarLip, 9 — git log on the merge target). Reviewer must @-mention @minmengdie in a PR comment requesting explicit sign-off on the dispatch change before merge, on top of the downstream CI label above.
⚠️ [verified] No base-vs-head timing exists outside the author's own MI355X tables: CI collects op_tests/test_mla_stage2_merge.py for correctness only (its µs columns are informational, never asserted), the auto-validator selected no target and is disabled here, and this box could not re-run it (no torch env, all 8 GPUs at 98–99% VRAM) — so the 2048 dispatch threshold and the 256/128 d_chunk picks rest on one author's sweep on one part. Reviewer should ask for a CI-side run of op_tests/test_mla_stage2_merge.py -b 1 8 16 64 256 --heads 16 128 --splits 2 4 8 16 32 128 on gfx950 to confirm the dchunk column is not slower than base on that part.

@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Nov 18, 2026
@AMD-yanfeiwang
AMD-yanfeiwang force-pushed the perf/mla-v4nm-stage2-dchunk branch from 508efde to b647322 Compare November 19, 2026 12:42
@aiter-bot

aiter-bot commented Nov 19, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 1dc78ddaecd9 · 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

On gfx950, `mla_decode_fwd_v4_nm` now merges the split partials with `_fwd_kernel_stage2_dchunk` when `num_seqs * num_heads <= 2048` and `num_kv_splits <= 16`. The loop kernel runs one 1-wave CTA per (seq, head) and leaves a small decode grid mostly idle. Other archs, larger grids and larger split counts keep `_fwd_kernel_stage2_asm`. The output is not bit-identical to the loop kernel (different sum order).

Validation and unverified scope

MI355X (gfx950), image `lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261118`, this branch's three files on its aiter:

- `pytest op_tests/test_mla_v4_nm.py`: 53 passed. `python3 op_tests/test_mla_stage2_merge.py`: 80 rows, all match the torch reference.
- Fuzz against a float64 merge and the loop kernel (2800 standalone cases, 1199 end-to-end calls): no NaN, no unwritten or out-of-range element; all 5 launch variants compile without VGPR spill.
- Whole call: 0.96–1.36× of main over 734 cells; SGLang DeepSeek-V4-Flash TP2 ITL p50 −1.1% to −1.2% at concurrency 1.

Unverified: the end-to-end numbers are from an earlier revision with the same kernel and launch parameters. ATOM was not run end to end. gfx942 and gfx1250 are not measured, and keep the loop kernel. Sequences with more than one query row are covered by the fuzz only, not by a test in the tree.

PR-specific question

Both tests cover a kernel that does not exist before this PR, so neither can fail on main. `test_v4_nm_empty_seq_multi_split` fails when the zero guard of the short-KV path is removed (checked): a `kv_len = 0` sequence then gives 0/0 = NaN instead of 0. The `dchunk` rows of `test_mla_stage2_merge.py` with `--valid 6` are the only test that runs more than one block of the short-KV loop; dropping its rescale makes them fail (checked).

Changed op tests

- `op_tests/test_mla_stage2_merge.py`: adds the `dchunk` candidate next to `base` and `vec`, and a `--valid` axis for a short KV. It covers the tile path, a partly valid tile and the multi-block short path against a torch reference; no other test launches the merge kernel alone. A mismatch now fails the test instead of only being logged.
- `op_tests/test_mla_v4_nm.py`: adds `test_v4_nm_empty_seq_multi_split` (3 cases): empty and short sequences through the whole call on a small dchunk grid, a larger dchunk grid and a loop-kernel grid. It is the only test where stage 1 leaves `valid_split_count` unwritten for an empty sequence.

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
  • FAIL — Aiter Test Gate: failed
  • Required CI failed; fix it before requesting human review.

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

@AMD-yanfeiwang
AMD-yanfeiwang force-pushed the perf/mla-v4nm-stage2-dchunk branch from b647322 to c217a1c Compare November 19, 2026 13:01
@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Nov 20, 2026
Yanfei Wang and others added 3 commits November 20, 2026 03:13
_fwd_kernel_stage2_asm runs one 1-wave CTA per (seq, head) and walks the
splits serially, which underfills the GPU at decode sizes. Add
_fwd_kernel_stage2_dchunk: same merge, Lv split over a third grid axis and
the splits reduced as one [MAX_SPLITS, D_CHUNK] tile. mla_decode_fwd_v4_nm
uses it when num_seqs * num_heads <= 2048 and keeps the existing kernel
for larger (bandwidth-bound) grids.

Signed-off-by: Yanfei Wang <[email protected]>
…28 splits, gfx950 only

- _fwd_kernel_stage2_dchunk: a sequence with no valid split (kv_len == 0,
  valid_split_count == 0, or no splits) now gets O = 0 like
  _fwd_kernel_stage2_asm, instead of 0/0 = NaN.
- _launch_stage2_dchunk: let d_chunk go down to 32, so 65..128 splits use a
  128x32 tile (124 VGPRs, no spill) instead of 128x64 (85 VGPRs spilled,
  up to 3.5x slower than the loop merge). Configs at <= 64 splits are unchanged.
- mla_decode_fwd_v4_nm: dispatch dchunk only on gfx950 and for
  num_kv_splits <= 128; the tile sizing assumes wave64, and gfx1250 (wave32)
  is not measured.
- Tests: empty-seq cases for both merges (test_mla_stage2_merge.py) and for the
  whole op on dchunk and loop grids (test_mla_v4_nm.py); the merge test now
  fails on a mismatch (assertAllclose) instead of only logging it.
…t KV

A short KV fills only a few of the planned splits. The tile merge then costs
more than the loop kernel (down to 0.45x merge-only), because it launches
Lv / D_CHUNK CTAs per (seq, head) whatever the valid count. With at most half
of the splits valid, chunk 0 now merges the whole row four splits at a time
and the other chunks exit. D_CHUNK is the widest that keeps the tile within
4K elements.

The dispatch stops at the 16 splits the auto pick can return: above that,
few valid splits out of many planned are slower than the loop kernel.

test_mla_stage2_merge.py: time base as mla_decode_fwd_v4_nm launches it
(MAYBE_FINAL_OUT=False) and add short-KV rows (--valid). The end-to-end
empty-sequence test covers both merges, so the standalone one is dropped.
@AMD-yanfeiwang
AMD-yanfeiwang force-pushed the perf/mla-v4nm-stage2-dchunk branch from c217a1c to 1dc78dd Compare November 20, 2026 03:14

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants