Repository navigation
[ASM] [HIP] MLA PS mode asm and opus kernel support nhead12,8 - #5695
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Critical unsupported head-count dispatch and metadata/reduction paths remain, alongside a causal masking issue and missing default test coverage.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 4
Open (6)
Unrestricted head-count gate selects unsupported ASM and reduce paths · New Metadata gate admits unsupported head counts for reduction · New C++ planner admits head counts without matching reduce kernels · New Non-multiple GQA routing admits unsupported four-head reduction · New Penultimate-tile mask incorrectly applies to non-spanning waves · New CI defaults omit coverage for newly supported head-count paths · New
What changed in this PR
This PR adds gfx950 FP8 MLA support for nhead 8/12, including ASM dispatch, reduction, metadata, and Opus causal-tiling updates.
Changes:
- Adds 12-head reduction and expanded dispatch support.
- Updates metadata planning and validation.
- Adds wave-spanning Opus kernel handling.
| File | Reviewed change |
|---|---|
op_tests/test_mla_persistent.py |
Extends persistent MLA test support. |
csrc/py_itfs_cu/asm_mla.cu |
Adds ASM dispatch for new head ratios. |
csrc/kernels/mla/reduce.cu |
Adds 12-head reduction dispatch. |
csrc/kernels/mla/opus/mla_decode_traits.h |
Adds wave-spanning trait support. |
csrc/kernels/mla/opus/mla_decode_fp8_16mx8_32nx1.hpp |
Updates causal masking. |
csrc/kernels/mla/opus_mla_decode_fwd.cu |
Selects wave-spanning variants. |
csrc/kernels/mla/metadata/v1_2_device.cuh |
Expands native metadata support. |
aiter/ops/attention.py |
Updates metadata sizing and validation. |
aiter/mla.py |
Expands MLA dispatch gating. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The review identified unresolved test-coverage and sparse metadata-dispatch issues, including one critical issue.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 2
Open (3)
da98c21 to
dafa76e
Compare
dafa76e to
9763e15
Compare
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Metadata sizing can diverge from C++ validation, and automated coverage omits the new 12-head and causal-boundary cases.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Open (1)
9763e15 to
73b070d
Compare
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The 12-head cprr path retains the old causal mask, and required coverage for the new dispatch paths is missing.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 2
….6s -> 7.7s)
module_mla_reduce took 121.6 s to build, the slowest non-CK module in the
repo. Its wall was one translation unit: csrc/kernels/mla/reduce.cu at
121.5 s, of which 116.2 s (96%) was AMDGPU device codegen in a single clang
process. Header parsing was 0.95 s, so this was never a header problem.
reduce.cu defines only 3 __global__ functions, but DISPATCH_MLA_REDUCE_KERNEL
instantiates dispatch_mla_reduce_v1 23 x 7 x 2 = 322 times (support-matrix
rows x NUM_WG_PER_BH x output type), emitting 630 kernels from that one TU.
Device codegen is strictly linear in instantiation count (364-368 ms each,
+-1% across a 23x range), so the work shards cleanly.
This applies the pattern module_moe_topk already uses
(csrc/include/topk_gating_kernels.cuh + csrc/kernels/topk_gating/*.cu):
- csrc/include/mla_reduce_kernels.cuh: the device code and
dispatch_mla_reduce_v1, moved verbatim, plus extern template
declarations for every launcher.
- csrc/kernels/mla/reduce_instances/nh<N>_hd<D>.cu: one TU per
support-matrix row, each instantiating its own row.
- csrc/kernels/mla/reduce.cu: now the router only. It calls the
launchers it no longer instantiates.
- _AITER_MLA_REDUCE_FOR_EACH_ROW is the single source of truth for both
MLA_REDUCE_ROUTER and the extern template block, so the two lists
cannot drift. MLA_REDUCE_CASE_IF is gone: every row is an else-if
behind a leading `if(false) {}` so one table drives them all. That
compiles to nothing -- the host object disassembles with no dead
compare, and -Wall -Wextra is silent on it.
No kernel logic, launch configuration or exported signature changes.
Measured on MI355X (gfx950), ROCm 7.1.52802, cold build:
module wall 121.6 s -> 7.68 s (15.8x)
reduce.cu TU 121.5 s -> 2.1 s
slowest TU 121.5 s -> 7.6 s
link 0.09 s -> 0.09 s
total CPU 127 s -> 165 s (+30%, headers reparsed per TU)
.so 17.81 MB -> 18.05 MB (+1.3%, 23 fat binaries not 1)
Verified:
- op_tests/test_mla_reduce.py: 74/74 PASS, identical case by case to the
pre-change build.
- All 630 kernels carry identical agpr/sgpr/vgpr counts, spill counts,
LDS and private segment sizes, kernarg size, max_flat_workgroup_size,
dynamic stack use and wavefront size. Codegen is untouched.
- The move is verbatim: of 836 device and 112 host statement lines, the
only one not reproduced is `{ return (x + y - 1) >> y_log2; }`, which
clang-format expanded to three lines.
- op_tests/test_flydsl_mla_reduce.py unaffected (skips on gfx950).
Rebased onto main e4ceb9d. Two main commits changed reduce.cu after the
branch point, and both are carried into the split:
- #5698 (64-bit final output/LSE addressing): its device hunks are
applied to mla_reduce_kernels.cuh, and its removal of the 2 GiB
kMaxSpanBytes host check is kept in reduce.cu. The header is a new
file, so a plain conflict resolution would have kept the old int32
final_out_byte_offset_base code with the guard gone; a bf16
[32769, 128, 512] reduce then never writes row 32768 and corrupts row 0.
- #5695 (nhead 12 PS decode): (12, 512) is added to
_AITER_MLA_REDUCE_FOR_EACH_ROW in main's router order, with
reduce_instances/nh12_hd512.cu.
The row-table comment no longer says a missing TU fails the link: the .so
links with undefined symbols allowed and only fails at import.
Verified against a build of main on gfx950, ROCm 7.2.4:
- all 658 kernels have the same symbols, AMDGPU metadata and
disassembly (2,605,182 instructions) as main's single-TU build
- module_mla_reduce cold build: 126.1 s on main -> 7.9 s
- op_tests/test_mla_reduce.py 74/74; (12, 512) in bf16 and fp16, plus
fp16 on (128, 512) and (16, 128): 36/36
- a bf16 [32769, 128, 512] reduce writes rows 0, 32767 and 32768
correctly, as on main
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A 12-head-per-rank MLA model (Kimi-K3 at TP8) pads its decode query to 16 heads for AITER's persistent fp8 decode: a query copy per layer and a third more attention work. ROCm/aiter#5695 added a native H12 fp8 path on gfx950 to the decode dispatch, the metadata planner and the reducer. Use 12 heads when the whole path supports it: - AITER's planner, reducer and decode dispatch each have an H12 branch, and get_mla_metadata_info_v1 accepts 12 heads. Some AITER builds carry part of that source but still assert num_head_qo % 8 == 0 while sizing, so the sizing helper is called, not just read. - gfx950, an fp8 KV cache (the backend then quantizes the query to fp8) and 12 * (1 + num_speculative_tokens) <= 128, which is what the AITER kernel covers; anything else would reach AITER's "not supported" assert. The answer is fixed at backend setup, because the head count sizes the metadata buffers and the forward pass must agree with it. Kimi-K3 FP4, TP8, fp8 KV, DSpark 7 spec decode, conc 1, MI355X, proxy benchmark (ISL 64k-320k x OSL 512, estimated p90 interactivity), crossover on two nodes: node native H12 padded H16 A 178.25 176.42 (+1.0%) B 178.94 176.77 (+1.2%) Every ISL improves on both nodes (p90 per-token time -0.05 to -0.09 ms). Co-authored-by: Cursor <[email protected]> Signed-off-by: Xiaohu Guo <[email protected]>
….6s -> 7.7s) (#5481) module_mla_reduce took 121.6 s to build, the slowest non-CK module in the repo. Its wall was one translation unit: csrc/kernels/mla/reduce.cu at 121.5 s, of which 116.2 s (96%) was AMDGPU device codegen in a single clang process. Header parsing was 0.95 s, so this was never a header problem. reduce.cu defines only 3 __global__ functions, but DISPATCH_MLA_REDUCE_KERNEL instantiates dispatch_mla_reduce_v1 23 x 7 x 2 = 322 times (support-matrix rows x NUM_WG_PER_BH x output type), emitting 630 kernels from that one TU. Device codegen is strictly linear in instantiation count (364-368 ms each, +-1% across a 23x range), so the work shards cleanly. This applies the pattern module_moe_topk already uses (csrc/include/topk_gating_kernels.cuh + csrc/kernels/topk_gating/*.cu): - csrc/include/mla_reduce_kernels.cuh: the device code and dispatch_mla_reduce_v1, moved verbatim, plus extern template declarations for every launcher. - csrc/kernels/mla/reduce_instances/nh<N>_hd<D>.cu: one TU per support-matrix row, each instantiating its own row. - csrc/kernels/mla/reduce.cu: now the router only. It calls the launchers it no longer instantiates. - _AITER_MLA_REDUCE_FOR_EACH_ROW is the single source of truth for both MLA_REDUCE_ROUTER and the extern template block, so the two lists cannot drift. MLA_REDUCE_CASE_IF is gone: every row is an else-if behind a leading `if(false) {}` so one table drives them all. That compiles to nothing -- the host object disassembles with no dead compare, and -Wall -Wextra is silent on it. No kernel logic, launch configuration or exported signature changes. Measured on MI355X (gfx950), ROCm 7.1.52802, cold build: module wall 121.6 s -> 7.68 s (15.8x) reduce.cu TU 121.5 s -> 2.1 s slowest TU 121.5 s -> 7.6 s link 0.09 s -> 0.09 s total CPU 127 s -> 165 s (+30%, headers reparsed per TU) .so 17.81 MB -> 18.05 MB (+1.3%, 23 fat binaries not 1) Verified: - op_tests/test_mla_reduce.py: 74/74 PASS, identical case by case to the pre-change build. - All 630 kernels carry identical agpr/sgpr/vgpr counts, spill counts, LDS and private segment sizes, kernarg size, max_flat_workgroup_size, dynamic stack use and wavefront size. Codegen is untouched. - The move is verbatim: of 836 device and 112 host statement lines, the only one not reproduced is `{ return (x + y - 1) >> y_log2; }`, which clang-format expanded to three lines. - op_tests/test_flydsl_mla_reduce.py unaffected (skips on gfx950). Rebased onto main e4ceb9d. Two main commits changed reduce.cu after the branch point, and both are carried into the split: - #5698 (64-bit final output/LSE addressing): its device hunks are applied to mla_reduce_kernels.cuh, and its removal of the 2 GiB kMaxSpanBytes host check is kept in reduce.cu. The header is a new file, so a plain conflict resolution would have kept the old int32 final_out_byte_offset_base code with the guard gone; a bf16 [32769, 128, 512] reduce then never writes row 32768 and corrupts row 0. - #5695 (nhead 12 PS decode): (12, 512) is added to _AITER_MLA_REDUCE_FOR_EACH_ROW in main's router order, with reduce_instances/nh12_hd512.cu. The row-table comment no longer says a missing TU fails the link: the .so links with undefined symbols allowed and only fails at import. Verified against a build of main on gfx950, ROCm 7.2.4: - all 658 kernels have the same symbols, AMDGPU metadata and disassembly (2,605,182 instructions) as main's single-TU build - module_mla_reduce cold build: 126.1 s on main -> 7.9 s - op_tests/test_mla_reduce.py 74/74; (12, 512) in bf16 and fp16, plus fp16 on (128, 512) and (16, 128): 36/36 - a bf16 [32769, 128, 512] reduce writes rows 0, 32767 and 32768 correctly, as on main Co-authored-by: Claude Opus 5.5 <[email protected]>



Motivation
On gfx950, fp8/fp8 MLA persistent decode only accepted head counts that are 16 or a multiple
of 16 (16 / 32 / 64 / 96 / 128). Anything else was either folded down to 16 heads — which is
only possible when
num_heads % 16 == 0— or rejected outright byget_mla_metadata_v1_2_device. A model with 12 query heads per KV head therefore had no pathat all, on either the asm or the opus kernel.
This PR makes both kernels accept head counts that are not a multiple of 16.
Technical Details
Why a non-multiple-of-16 head count broke the causal mask
Both kernels give each wave a fixed tile of packed Q rows —
W_M = 16for the opus 16mx8kernel,
SUB_Q = 32for the asm qh32 kernel — and packed rowpbelongs to query tokenp / nhead. Whilenheadis a multiple of that tile, every row of a wave lands on the sametoken, so the causal bound is wave-uniform and lives in an SGPR:
diag_kv_bound = (warp_id * T::W_M) / nhead + causal_diagonal;With
nhead = 12a token boundary falls inside a wave: rows 0–11 of warp 0 are token 0,rows 12–15 are token 1. All 16 rows still got token 0's bound, so 4 of every 16 query rows
were masked one KV column too early. The bound has to be per lane:
Measured on the unmodified opus kernel,
-n 12,8over the default grid: 16 of 64 cellsfailed, max |delta| 2.58 with 33% of elements off. The asm kernel had the same defect.
MLA_A8W8_QH32_1TG_4W_32mx4_64nx1_PS): a third branch incausal_maskcomputesthe token per lane with a
v_rcp_f32divide plus a 1/256 bias before the truncating convert(
p < 128andgqa_ratio <= 128, sop/gqa_ratiois either integral or at least 1/128 fromthe next integer, which absorbs the ≤1ulp reciprocal error in both directions). The existing
fast paths —
gqa_ratioa multiple ofSUB_Q, andgqa_ratio == 16— are kept and take thesame instructions as before. Only the four rebuilt
.cofiles are in this PR; the SP3 sourcelives in a separate repo.
mla_decode_fp8_16mx8_32nx1.hpp):causal_kv_boundtakeslane_idand is gated ona new compile-time trait
WAVE_SPANS_TOKENS, which the host sets fromH % W_M != 0. Headcounts that are a multiple of
W_Mkeep the wave-uniform SGPR form, so their codegen isuntouched; only the new head counts pay the extra live VGPR in a kernel that already sits at
256.
The causal diagonal is not confined to the last KV tile
A work item's bounds span its
q_lencolumns ending atvalid_kv_len - 1, so they sit insidethe last KV tile only when
valid_kv_len % KV_TILE_SIZE >= q_len. Otherwise they reach backinto the previous tile, which
tile_idx == tile_end - 1left unmasked, and the rows boundedthere attended past their diagonal. With only the per-lane fix applied,
-n 12,8 -c 33stillfailed at max |delta| 1.41 over 58% of elements.
mask_oob_scoresnow masks the last two tiles. Two is always enough: a work item holds at most128 packed rows over at least 4 heads, so
q_len <= 32 == KV_TILE_SIZE. Deriving the exact tileset from
causal_diagonalinstead was tried and is slower — it keepscausal_diagonallive forevery tile's test, which measured −0.7% across the existing shapes versus −0.3% for the fixed
two-tile window.
This defect predates the PR and applies to every causal shape; the default test grid never hit
it because all of its ctx values are ≡ 0 or ≥ q_len mod 32.
Dispatch and metadata
The same predicate is added in the five places that have to agree, alongside the existing
natively_supportedclauses:csrc/kernels/mla/metadata/v1_2_device.cuhnatively_supported(no head folding)csrc/py_itfs_cu/asm_mla.cuconfig_gqa_ratio = 32,config_max_seqlen_q = 4, real gqa inargs.s_MQAaiter/ops/attention.pydecode_update_mla_metadata_v1aiter/mla.pymla_decode_fwdnative-support checkop_tests/test_mla_persistent.pyTwo smaller pieces:
csrc/kernels/mla/reduce.cugains the(12 heads, 512 dim)instantiation; the dispatch tableis a whitelist and 12 was not in it.
get_mla_metadata_info_v1'sassert num_head_qo % 8 == 0is relaxed to% 4. Nothing in thatfunction divides by the head count — it only sizes buffers.
opus_mla_decode_fwd.cuneeds no head-count gate of its own; the metadata check upstream of itis the gate, and
WAVE_SPANS_TOKENSis picked from the runtimeH.Supported range
128 is what one work item holds: 8 waves ×
W_M16 for opus, 4 waves ×SUB_Q32 for asm. Thegate is deliberately
nhead * max_seqlen_qo <= 128— i.e. one qo tile per batch — rather than aper-tile test, because the metadata splits qo tiles in tokens:
ceil(packed / 128)tiles ofceil(seqlen_qo / tiles)tokens. Two ceilings in a row can hand back a tile over the limit(12 heads × 21 tokens → 2 tiles of 11 tokens = 132 rows), which the kernels would silently
truncate. Keeping the batch inside a single tile avoids that case entirely.
Combinations outside the range are rejected at metadata with the existing "only supports #heads
…" error rather than producing wrong results.
Test Plan
Verdicts below are against the
golden fp8reference — the one that quantises the same way thekernel does. gfx950 / MI355X, ROCm 7.1.
Test Result
Accuracy, full default grid
-b 1 3 5 16 32 64 128 256×-c 21 64 256 512 1200 3200 5200 8192, 64 cells per shape.12,4and24,4were also run over the full grid: 0 failed on both kernels.24,8is rejectedat metadata (24 × 8 = 192 > 128), as intended.
Before the fix, for reference:
12,8opus 16 / 64 cells failed, max |delta| 2.58;12,4opus 16 / 64 failed, max |delta| 2.88.3. Current vs origin/main, decode time
decode:asm_576us, minimum of 3 reps. Each shape ran all four arms on one GPU withthe reps interleaved. Positive delta = current is faster.
asm
median per-cell -0.10%, range -1.94% .. +2.76%, 21/48 cells faster
4. Shapes origin/main cannot run -- current-tree time only
Submission Checklist