Repository navigation
MLA v4 nm: faster cross-split merge for small decode grids - #5776
AMD-yanfeiwang wants to merge 3 commits into
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
|
No activity for 15 days, so this is now labelled |
|
@aiter-bot review |
ROCm/aiter PR #5776 — MLA v4 nm: faster cross-split merge for small decode gridsAdds a second stage-2 cross-split merge kernel for v4 nm decode ( Review (advisory): 🔴 HIGH RISK 🔴 [verified divergence, reachability inferred] 🔴 [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 🔴 [verified] aiter/mla.py is a Tier-2 downstream contract and this diff changes the default dispatch of 📝 [verified] The tile-cap loop floors at 📝 [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 |
0feea40 to
508efde
Compare
ROCm/aiter PR #5776 — MLA v4 nm: faster cross-split merge for small decode gridsAdds a second stage-2 cross-split merge for v4 nm decode ( Review (advisory): 🔴 HIGH RISK 🔴 [verified] |
|
No activity for 15 days, so this is now labelled |
508efde to
b647322
Compare
NOT READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
b647322 to
c217a1c
Compare
_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.
c217a1c to
1dc78dd
Compare
Motivation
In
mla_decode_fwd_v4_nm, the cross-split merge_fwd_kernel_stage2_asmruns 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:
get_mla_v4_nm_num_kv_splits.SGLANG_DSV4_FLYDSL_DECODE=0. 16/32/64-head DSv4 fp8 decode defaults to the FlyDSL kernel ([FlyDSL] [MLA] DeepSeek-V4 fp8 MLA decode (v4 nm) in one FlyDSL launch on gfx950 #5882).Changes
_fwd_kernel_stage2_dchunk. Same math as_fwd_kernel_stage2_asm, with two paths per (seq, head):Lvis split over a third grid axis and the splits are reduced as one[MAX_SPLITS, D_CHUNK]tile.mla_decode_fwd_v4_nmuses it on gfx950 whennum_seqs * num_heads <= 2048andnum_kv_splits <= 16(the auto-pick cap). Larger grids are bandwidth-bound and keep_fwd_kernel_stage2_asm; so do other archs (not measured).O = 0, same as_fwd_kernel_stage2_asmsince [ASM] [HIP] [JIT] mla version 6 #6394.test_mla_stage2_merge.py: add adchunkcandidate and a--validaxis (short KV).basenow 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: addtest_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-20261118with this PR'saiter/mla.pyon its aiter (581579ab5).Whole
mla_decode_fwd_v4_nmcall (stage 1 + merge), CUDA-graph replay, auto split count, µs, main → this PR: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.Merge only (
test_mla_stage2_merge.py, all splits valid), µs, base → dchunk: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
mla_decode_fwd_v4_nm. With speculative decoding the row count is several times the concurrency.Test
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 passedtest_pa_decode_sparse.pyandtests/test_plugin*.py.PR Self-Review
Reviewed revision
1dc78ddaecd9Behavior and intent
On gfx950,
mla_decode_fwd_v4_nmnow merges the split partials with_fwd_kernel_stage2_dchunkwhennum_seqs * num_heads <= 2048andnum_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.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_splitfails when the zero guard of the short-KV path is removed (checked): akv_len = 0sequence then gives 0/0 = NaN instead of 0. Thedchunkrows oftest_mla_stage2_merge.pywith--valid 6are 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 thedchunkcandidate next tobaseandvec, and a--validaxis 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: addstest_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 leavesvalid_split_countunwritten for an empty sequence.