Repository navigation
[HIP] [Perf] Add a wide split-K reduce for MLA decode and verify with few units and many splits - #7570
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
… and many splits - kn_mla_reduce_v1 gives each (tile, head, row) one 2-wave workgroup that walks the splits two loads at a time. - A batch-1, 8-token verify with ~250 splits then uses 128 workgroups and spends 31-45 us latency-bound on gfx950. - kn_mla_reduce_v1_wide shares a unit's splits across up to 4 waves with 8 loads in flight, merging in LDS. - It also cuts 512-wide outputs into 256-column units, so batch 1 reaches every CU. - The planner sizes reduce_indptr for the CUDA-graph batch; each wave counts the live tiles itself, without LDS. - It picks waves per unit from the live tile count and the mean split count; one wave per unit takes no barrier. - The host routes max_seqlen_q <= 16, heads * max_seqlen_q <= 128 and tiles * heads * rows <= 16 * CUs to it. - It also needs num_kv_splits, the caller's bound on splits per tile, to be 0 or at least 8. - AITER_MLA_REDUCE_WIDE=0 keeps the old kernel; =1 drops the size and split-count heuristics. - 16 heads x 512, 8 rows, batch 1, cold L2: 31.1/44.0/45.2 -> 12.3/15.5/16.0 us at 8k/68k/480k tokens. - With a tight split bound no shape is slower; with bound 256 and 2-6 real splits it is up to 2.3 us slower. - op_tests/test_mla_reduce.py adds layouts: no final map, a -1 tile, a padded indptr tail, multi-pass rows, 80 tiles.
21632f4 to
63ae513
Compare
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 |
There was a problem hiding this comment.
🔵 Needs a closer look
The new default GPU path uses custom LDS synchronization and cross-wave reductions, but validation covers only gfx950, leaving gfx942 correctness and performance unverified with no GPU execution available for this review.
No open issues 🎉
What changed in this PR
Adds a wide split-K reduction path for MLA decode and verify, targeting small workloads with many KV splits that underutilize the GPU with the existing kernel.
Changes:
- Adds a multi-wave reduction kernel with column chunking and device-side work allocation.
- Selects the wide kernel using shape and split-count heuristics, with an environment override to retain the existing path.
- Expands correctness tests for partial-map layouts, padded metadata, and extra row passes.
| File | Description |
|---|---|
op_tests/test_mla_reduce.py |
Adds layout cases checked against a per-row PyTorch reference. |
csrc/kernels/mla/reduce.cu |
Passes query-length and split-count bounds into reduction parameters. |
csrc/include/mla_reduce_kernels.cuh |
Implements the wide kernel, selection heuristics, and launch dispatch. |
Review effort: Balanced
|
gfx942 (MI300X) validation: PASS (manual: test_mla_reduce.py all checks pass, wide path forced on and off). No change needed. Wide reduce uses 4 KB LDS; grid scales with 304 CUs. Head/base time geomean 0.76x over 339 cases (up to 5x faster at 300 splits); worst case about 7% at split counts just over the 8-split threshold. |
|
@yuychang Thanks for the PR! The only failing CI job is Standard Tests (MI35X, shard 2/8), and it's unrelated to this change. That bug was on main and was fixed by #7583 (a97e999), but this branch was cut before the fix. test_mla_reduce.py passes, including the new layout cases. Could you merge or rebase onto the latest main and re-run CI? That should clear the failure. |
There was a problem hiding this comment.
🟡 Changes recommended
The reduction kernel loses normalization for partial LSEs with a large common offset, producing an output of 2 instead of 1 on a default-routed case and requiring stable weight computation plus a regression test.
1 open issue
Review effort: Balanced
I've merged the latest main and re-run the CI jobs. Thanks! |
- Add -s/--splits: list of split configs, each comma-separated per-tile split counts; RxS means R tiles of S splits - Add -n/--heads: head counts swept for every --head_dim; default keeps the per-head_dim lists (now HEADS_FOR_DIM) - run_case takes the split-config spelling as its `splits` local, so a validator can capture it per call - Invalid split configs (non-numeric, empty item, zero counts, empty list) make argparse exit 2 - Multi-row cases use the same spelling; the sweep uses itertools.product and the help uses RawTextHelpFormatter - Defaults reproduce the previous sweep: same 170 ordered run_case calls and 16 layout cases - Ragged configs with more than 4 tiles print in full instead of as Nx[first]
There was a problem hiding this comment.
🔵 Needs a closer look
The new default HIP wave64 path uses custom LDS synchronization and multi-wave reductions, but validation covers only gfx950, leaving gfx942 correctness and latency unverified.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
|
gfx942 (MI300X, 304 CU) validation at
Perf, head vs base, same GPU, best of 3 per side (speedup = base µs / head µs):
On gfx942, multi-row tiles whose largest split count just reaches the wide threshold ([8,4], [3,8]) run 2–6% slower. So the gfx950-tuned threshold is slightly low for MI300X, though no regression gate fires. On the large-LSE question: with the same offset (0 to 1e8) added to every partial LSE, the wide and old kernels have the same error against an fp64 reference, to 3 digits at every offset. The wide path loses accuracy at huge offsets exactly as much as the old kernel does. |
|
@kaiyang-1 could you take a look at Why the extra commit:
If you'd rather keep this PR to the kernel change only, I can move the test change into a separate PR. One thing worth your judgment: on gfx942, multi-row tiles whose largest split count just reaches the wide threshold ([8,4], [3,8]) run 2–6% slower. The gfx950-tuned threshold may want a per-arch value. |
There was a problem hiding this comment.
🔵 Needs a closer look
The default-enabled wave64 path uses architecture-sensitive LDS synchronization, but supplied runtime evidence covers only gfx950, leaving correctness and latency on gfx942 and other supported GPUs unvalidated.
No open issues 🎉
Review effort: Balanced
… few units and many splits (#7570) * perf(mla): split-K reduce kernel for decode and verify with few units and many splits - kn_mla_reduce_v1 gives each (tile, head, row) one 2-wave workgroup that walks the splits two loads at a time. - A batch-1, 8-token verify with ~250 splits then uses 128 workgroups and spends 31-45 us latency-bound on gfx950. - kn_mla_reduce_v1_wide shares a unit's splits across up to 4 waves with 8 loads in flight, merging in LDS. - It also cuts 512-wide outputs into 256-column units, so batch 1 reaches every CU. - The planner sizes reduce_indptr for the CUDA-graph batch; each wave counts the live tiles itself, without LDS. - It picks waves per unit from the live tile count and the mean split count; one wave per unit takes no barrier. - The host routes max_seqlen_q <= 16, heads * max_seqlen_q <= 128 and tiles * heads * rows <= 16 * CUs to it. - It also needs num_kv_splits, the caller's bound on splits per tile, to be 0 or at least 8. - AITER_MLA_REDUCE_WIDE=0 keeps the old kernel; =1 drops the size and split-count heuristics. - 16 heads x 512, 8 rows, batch 1, cold L2: 31.1/44.0/45.2 -> 12.3/15.5/16.0 us at 8k/68k/480k tokens. - With a tight split bound no shape is slower; with bound 256 and 2-6 real splits it is up to 2.3 us slower. - op_tests/test_mla_reduce.py adds layouts: no final map, a -1 tile, a padded indptr tail, multi-pass rows, 80 tiles. * test: add --splits and --heads sweep axes to test_mla_reduce - Add -s/--splits: list of split configs, each comma-separated per-tile split counts; RxS means R tiles of S splits - Add -n/--heads: head counts swept for every --head_dim; default keeps the per-head_dim lists (now HEADS_FOR_DIM) - run_case takes the split-config spelling as its `splits` local, so a validator can capture it per call - Invalid split configs (non-numeric, empty item, zero counts, empty list) make argparse exit 2 - Multi-row cases use the same spelling; the sweep uses itertools.product and the help uses RawTextHelpFormatter - Defaults reproduce the previous sweep: same 170 ordered run_case calls and 16 layout cases - Ragged configs with more than 4 tiles print in full instead of as Nx[first]

Summary
Adds
kn_mla_reduce_v1_wide, a split-K reduce for MLA decode and verify when there are few output units and many KV splits. The host picks it automatically for small shapes with at least 8 splits;AITER_MLA_REDUCE_WIDE=0keeps the old kernel.Motivation
At batch 1 with an 8-token verify (16 heads x 512), aiter's auto planner gives each step 170-255 KV splits, even at 8k tokens of context.
kn_mla_reduce_v1gives each (tile, head, row) one 2-wave workgroup that walks the splits two loads at a time: 128 workgroups on a 256-CU MI355X, latency-bound, 31-45 us per layer. That is longer than the asm attention itself at 8k and 68k tokens.Changes
kn_mla_reduce_v1_wide(csrc/include/mla_reduce_kernels.cuh): up to 4 waves share one (tile, head, row, 256-column) unit, each with 8 loads in flight, and merge the partial LSE/output in LDS. One wave per unit takes no barrier.It cuts 512-wide outputs into 256-column units, so batch 1 reaches every CU.
The planner sizes
reduce_indptrfor the CUDA-graph batch. Each wave counts the live tiles itself (one round of loads, no LDS), then picks waves per unit from the live tile count and the mean split count.Host routing requires all of:
max_seqlen_q <= 16heads * max_seqlen_q <= 128tiles * heads * rows <= 16 * CUsnum_kv_splits(the caller's bound on splits per tile) is 0 or at least 8Everything else keeps
kn_mla_reduce_v1.AITER_MLA_REDUCE_WIDE=0forces the old kernel;=1drops the size and split-count heuristics.csrc/kernels/mla/reduce.cupassesmax_seqlen_qandnum_kv_splitsinto the params.op_tests/test_mla_reduce.pyaddsLAYOUT_CASES: no final map, a-1tile among multi-split tiles, areduce_indptrtail padded for a 32-request graph, tiles with more rows thanmax_seqlen_q(extra passes) and an 80-tile table. These use a per-row torch reference.Performance (if applicable)
Reduce kernel only, one MI355X, L2/MALL flushed before every call. Metadata is built as SGLang's aiter backend does with auto KV splits (16 zero-padded heads, fp8, page 1):
Synthetic grid (
build_reduce_problem, bf16, 1-128 heads, head_dim 128/512, 1 or 8 rows, cold L2), old/new ratio (above 1 means faster):num_kv_splits= real maxnum_kv_splits= 256The remaining regression needs a loose split bound together with 2-6 real splits. aiter's auto planner gives 100+ splits at the contexts above, so with auto splits that takes very short contexts. I did not find a cheaper prologue: removing the live-tile count saved about 0.05 us.
End to end, Kimi-K3 on 8x MI355X with SGLang, AgentX concurrency 1, 3600 s each, same tree, only
AITER_MLA_REDUCE_WIDEdiffers. This ran on an earlier revision of this PR, before the split-count gate and before the LDS staging was removed; both changes keep this path on the wide kernel and make it no slower:AITER_MLA_REDUCE_WIDE=0That is +1.9% throughput and -3.3% P90 ITL, within this benchmark's roughly 4% hour-to-hour spread, so one pair of hours does not prove it. gsm8k (200 examples, sgl-eval 0.1.3) scored 98.5% with both kernels.
Testing
On MI355X (gfx950), ROCm 7.2.4, on
mainat 8e0000d:op_tests/test_mla_reduce.py -d bf16: ALL PASSED withAITER_MLA_REDUCE_WIDEunset,=0(old kernel) and=1(wide forced on every eligible shape), including the new layout cases.Documentation
Internal kernel selection; the one new knob is an opt-out env var, described above.
Dependencies
Breaking Changes
None.
MlaReduceKernelV1Paramsgains two fields (max_seqlen_q,num_kv_splits); it is built only inreduce.cu. Outputs match the fp32 reference to the same tolerance askn_mla_reduce_v1.PR Self-Review
Reviewed revision
63ae513
Behavior and intent
Same reduce result, computed by a wider kernel for small decode/verify shapes with many splits, where the old kernel left most CUs idle.
Validation and unverified scope
Validated on MI355X (gfx950) only. The kernel runs on any wave64 GPU, so gfx942 takes it by default too. I have not run gfx942: please run
ci:all, and gfx942 if the CI does not cover it. Known limits, not changed here:num_reduce_tile, which is sized for the CUDA-graph batch. A graph captured past16 * CUsunits keeps the old kernel even when it replays with one live request.16 * CUsrouting bound and the4 * CUsgrid cap were not swept.metadata/v1_2_device.cuhwas tuned on one query row.PR-specific question
N/A until the automated comment posts one.
Changed op tests
op_tests/test_mla_reduce.py:LAYOUT_CASES/run_layout_case. They cover the partial-map layouts the uniform builder never makes: no final map, a-1tile, a paddedreduce_indptrtail, more rows thanmax_seqlen_qand more than 64 tiles. These are the branches of the wide kernel (and ofkn_mla_reduce_v1) that the existing cases leave untested.🤖 Generated with Claude Code