Skip to content

[HIP] [Perf] Add a wide split-K reduce for MLA decode and verify with few units and many splits - #7570

Merged
kaiyang-1 merged 4 commits into
ROCm:mainfrom
yuychang:up/mla-reduce-wide
Nov 9, 2026
Merged

kaiyang-1 merged 4 commits into
ROCm:mainfrom
yuychang:up/mla-reduce-wide

Conversation

@yuychang

@yuychang yuychang commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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=0 keeps 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_v1 gives 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_indptr for 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 <= 16
    • heads * max_seqlen_q <= 128
    • tiles * heads * rows <= 16 * CUs
    • num_kv_splits (the caller's bound on splits per tile) is 0 or at least 8

    Everything else keeps kn_mla_reduce_v1.

  • AITER_MLA_REDUCE_WIDE=0 forces the old kernel; =1 drops the size and split-count heuristics.

  • csrc/kernels/mla/reduce.cu passes max_seqlen_q and num_kv_splits into the params.

  • op_tests/test_mla_reduce.py adds LAYOUT_CASES: no final map, a -1 tile among multi-split tiles, a reduce_indptr tail padded for a 32-request graph, tiles with more rows than max_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):

Hardware Workload/configuration Before After Change
MI355X batch 1, 8-token verify, 8k context (255 splits) 31.1 us 12.3 us -60%
MI355X batch 1, 8-token verify, 68k context 44.0 us 15.5 us -65%
MI355X batch 1, 8-token verify, 480k context 45.2 us 16.0 us -65%
MI355X batch 1, 6-token verify, graph batch 32, 8k / 68k / 480k 30.3 / 43.2 / 44.6 us 10.9 / 13.7 / 13.5 us -64% to -70%
MI355X batch 2, 6-token verify, graph batch 32, 8k / 68k / 480k 20.5 / 24.4 / 26.0 us 11.1 / 12.3 / 13.6 us -46% to -50%

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):

Real splits per tile num_kv_splits = real max num_kv_splits = 256
2-4 0.99-1.01 (host keeps the old kernel) 0.43-1.00 (up to 2.3 us slower)
6-8 0.99-1.11 0.89-1.11
16 1.05-1.18 0.80-1.18
64 1.85-2.42 1.87-2.42
256 3.87-5.04 3.82-5.07

The 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_WIDE differs. 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:

Arm tok/s/GPU P90 ITL Requests
AITER_MLA_REDUCE_WIDE=0 2008.38 4.52 ms 238
default (wide) 2046.51 4.37 ms 240

That 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

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

On MI355X (gfx950), ROCm 7.2.4, on main at 8e0000d:

  • op_tests/test_mla_reduce.py -d bf16: ALL PASSED with AITER_MLA_REDUCE_WIDE unset, =0 (old kernel) and =1 (wide forced on every eligible shape), including the new layout cases.

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Internal kernel selection; the one new knob is an opt-out env var, described above.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None. MlaReduceKernelV1Params gains two fields (max_seqlen_q, num_kv_splits); it is built only in reduce.cu. Outputs match the fp32 reference to the same tolerance as kn_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:

  • Routing is decided on the host from num_reduce_tile, which is sized for the CUDA-graph batch. A graph captured past 16 * CUs units keeps the old kernel even when it replays with one live request.
  • Waves per unit come from the mean split count, so one long request among many short ones can get one wave for its many splits.
  • The 16 * CUs routing bound and the 4 * CUs grid cap were not swept.
  • At 8k and 68k contexts, 64-128 splits beat aiter's auto split count by about 5 us per layer (attention plus reduce). The auto law in metadata/v1_2_device.cuh was 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 -1 tile, a padded reduce_indptr tail, more rows than max_seqlen_q and more than 64 tiles. These are the branches of the wide kernel (and of kn_mla_reduce_v1) that the existing cases leave untested.

🤖 Generated with Claude Code

@yuychang
yuychang requested review from a team and a balanced review from Copilot November 6, 2026 10:19
@github-actions github-actions Bot changed the title [MLA] Add a wide split-K reduce for decode and verify with few units and many splits [HIP] [MLA] Add a wide split-K reduce for decode and verify with few units and many splits Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 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 7570 --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.

@github-actions github-actions Bot added the HIP label Nov 6, 2026
… 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.
@aiter-bot

aiter-bot commented Nov 6, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 3ff78126350c · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — PR-specific question is too short to show concrete reasoning (7 units; need 14)

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

Copilot AI 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.

🔵 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

@yuychang

yuychang commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

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.

kaiyang-1
kaiyang-1 previously approved these changes Nov 9, 2026
@kaiyang-1

Copy link
Copy Markdown
Contributor

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

Copilot AI balanced review requested due to automatic review settings November 9, 2026 01:45

Copilot AI 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.

🟡 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

Comment thread csrc/include/mla_reduce_kernels.cuh Resolved
@yuychang

yuychang commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

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

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]
Copilot AI balanced review requested due to automatic review settings November 9, 2026 04:00

Copilot AI 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.

🔵 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

@yuychang

yuychang commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

gfx942 (MI300X, 304 CU) validation at a1d39aa9c: validate-kernel-pr PASS (exit 0, head-bound).

a1d39aa9c adds -s/--splits and -n/--heads sweep axes to op_tests/test_mla_reduce.py, so the validator can run split configs the test doesn't run by default. The defaults reproduce the previous sweep exactly: the same 170 run_case calls and 16 layout cases, with the same arguments in the same order. Invalid configs exit 2 through argparse.

  • Stages: every stage passed: merge_sim, gpu_claim, runtime_compat, test_policy, baseline_control, repo tests, independent grid, execution receipt and perf. Index-width scan: 0 candidates.
  • Repo tests: 170 route calls; tolerances unchanged at 0.02.
  • Independent grid: 74 route calls, splits 5,9;257;1,1,1,1,1,1,1,1,1;9;7;1000;8,1,3;100x8 × heads 16/48, all outside the defaults.
  • Wide path on gfx942: both AITER_MLA_REDUCE_WIDE=1 and =0 run ALL PASSED. rocprofv3 shows the wide runs launching kn_mla_reduce_v1_wide and the =0 runs kn_mla_reduce_v1.

Perf, head vs base, same GPU, best of 3 per side (speedup = base µs / head µs):

Split config Rows Speedup (geomean)
all 170 1.32x
[300] 20 4.26x
[2,4,16,64] 20 1.82x
[33] 20 1.37x
[8,5,7] 20 0.995x
[8,4] 4 0.98x
[3,8] 2 0.94x (about 5.9 µs per row)

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.

@yuychang yuychang changed the title [HIP] [MLA] Add a wide split-K reduce for decode and verify with few units and many splits [HIP] [MLA] [Perf] Add a wide split-K reduce for decode and verify with few units and many splits Nov 9, 2026
@yuychang yuychang changed the title [HIP] [MLA] [Perf] Add a wide split-K reduce for decode and verify with few units and many splits [HIP] [Perf] Add a wide split-K reduce for MLA decode and verify with few units and many splits Nov 9, 2026
Copilot AI balanced review requested due to automatic review settings November 9, 2026 07:17
@yuychang

yuychang commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

@kaiyang-1 could you take a look at a1d39aa9c and confirm it's OK for this PR? Thanks.

Why the extra commit: a1d39aa9c (test: add --splits and --heads sweep axes to test_mla_reduce) changes only op_tests/test_mla_reduce.py. It doesn't touch the kernel (mla_reduce_kernels.cuh) or reduce.cu.

  • Problem: the test's only shape flag was --head_dim, which accepts 128 or 512, both already defaults. So the repo's validate-kernel-pr skill had nothing to vary for its independent-shape stage. At 95e2655d7 the verdict was INCONCLUSIVE, even though every test and the perf comparison passed.
  • Fix: I added two list-valued sweep axes, following the aiter-op-test convention (op-specific axes are lists the sweep iterates; no behavior toggles):
    • -s/--splits takes one or more split configs. Each config is a comma-separated list of per-tile split counts, and RxS means R tiles of S splits, e.g. 300, 8,5,7, 40x6.
    • -n/--heads sets head counts that apply to every --head_dim.
  • Defaults are unchanged: the default sweep is exactly the old one, with the same 170 run_case calls and 16 layout cases, same arguments, same order. Invalid configs exit 2 through argparse.
  • Result: with this commit, validate-kernel-pr on MI300X (gfx942) at a1d39aa9c is PASS, head-bound.
    • The grid used 8 split configs that are not in the defaults, e.g. 257, 1000, 100x8, 1,1,1,1,1,1,1,1,1, each at 16 and 48 heads. All passed.
    • Numbers are in my earlier comment above.
  • Current head: 3ff781263 is only a merge of main on top. The three PR files are byte-identical to a1d39aa9c.

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.

Copilot AI 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.

🔵 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

@kaiyang-1
kaiyang-1 self-requested a review November 9, 2026 15:20

@kaiyang-1 kaiyang-1 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.

LGTM

@kaiyang-1
kaiyang-1 merged commit 32caadc into ROCm:main Nov 9, 2026
ruanjm pushed a commit that referenced this pull request Nov 10, 2026
… 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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants