Skip to content

[ASM] [HIP] MLA PS mode asm and opus kernel support nhead12,8 - #5695

Merged
shengnxu merged 4 commits into
mainfrom
mmd/dev/mla_ps_fp8_nhead12
Sep 23, 2026
Merged

shengnxu merged 4 commits into
mainfrom
mmd/dev/mla_ps_fp8_nhead12

Conversation

@minmengdie

@minmengdie minmengdie commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

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 by
get_mla_metadata_v1_2_device. A model with 12 query heads per KV head therefore had no path
at 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 = 16 for the opus 16mx8
kernel, SUB_Q = 32 for the asm qh32 kernel — and packed row p belongs to query token
p / nhead. While nhead is a multiple of that tile, every row of a wave lands on the same
token, 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 = 12 a 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:

row = warp_id * T::W_M + lane_id % T::W_M;   // lane_id % W_M is the MFMA M index,
diag_kv_bound = row / nhead + causal_diagonal;   // same one make_layout_o and the LSE store use

Measured on the unmodified opus kernel, -n 12,8 over the default grid: 16 of 64 cells
failed
, max |delta| 2.58 with 33% of elements off. The asm kernel had the same defect.

  • asm (SP3 MLA_A8W8_QH32_1TG_4W_32mx4_64nx1_PS): a third branch in causal_mask computes
    the token per lane with a v_rcp_f32 divide plus a 1/256 bias before the truncating convert
    (p < 128 and gqa_ratio <= 128, so p/gqa_ratio is either integral or at least 1/128 from
    the next integer, which absorbs the ≤1ulp reciprocal error in both directions). The existing
    fast paths — gqa_ratio a multiple of SUB_Q, and gqa_ratio == 16 — are kept and take the
    same instructions as before. Only the four rebuilt .co files are in this PR; the SP3 source
    lives in a separate repo.
  • opus (mla_decode_fp8_16mx8_32nx1.hpp): causal_kv_bound takes lane_id and is gated on
    a new compile-time trait WAVE_SPANS_TOKENS, which the host sets from H % W_M != 0. Head
    counts that are a multiple of W_M keep the wave-uniform SGPR form, so their codegen is
    untouched; 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_len columns ending at valid_kv_len - 1, so they sit inside
the last KV tile only when valid_kv_len % KV_TILE_SIZE >= q_len. Otherwise they reach back
into the previous tile, which tile_idx == tile_end - 1 left unmasked, and the rows bounded
there attended past their diagonal. With only the per-lane fix applied, -n 12,8 -c 33 still
failed at max |delta| 1.41 over 58% of elements.

mask_oob_scores now masks the last two tiles. Two is always enough: a work item holds at most
128 packed rows over at least 4 heads, so q_len <= 32 == KV_TILE_SIZE. Deriving the exact tile
set from causal_diagonal instead was tried and is slower — it keeps causal_diagonal live for
every 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_supported clauses:

arch == gfx950 && q_fp8 && kv_fp8 && (nhead % 16 != 0) && (nhead * max_seqlen_qo <= 128)
file role
csrc/kernels/mla/metadata/v1_2_device.cuh metadata natively_supported (no head folding)
csrc/py_itfs_cu/asm_mla.cu routes to the qh32 kernel: config_gqa_ratio = 32, config_max_seqlen_q = 4, real gqa in args.s_MQA
aiter/ops/attention.py metadata buffer sizing + decode_update_mla_metadata_v1
aiter/mla.py mla_decode_fwd native-support check
op_tests/test_mla_persistent.py the reference path's matching check

Two smaller pieces:

  • csrc/kernels/mla/reduce.cu gains the (12 heads, 512 dim) instantiation; the dispatch table
    is a whitelist and 12 was not in it.
  • get_mla_metadata_info_v1's assert num_head_qo % 8 == 0 is relaxed to % 4. Nothing in that
    function divides by the head count — it only sizes buffers.

opus_mla_decode_fwd.cu needs no head-count gate of its own; the metadata check upstream of it
is the gate, and WAVE_SPANS_TOKENS is picked from the runtime H.

Supported range

128 is what one work item holds: 8 waves × W_M 16 for opus, 4 waves × SUB_Q 32 for asm. The
gate is deliberately nhead * max_seqlen_qo <= 128 — i.e. one qo tile per batch — rather than a
per-tile test, because the metadata splits qo tiles in tokens: ceil(packed / 128) tiles of
ceil(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

# new head counts + the established shapes, both kernels, full default grid
for n in 12,8 12,6 16,8 32,4 64,2 128,2; do
  for opus in 0 1; do
    AITER_MLA_USE_OPUS=$opus python3 op_tests/test_mla_persistent.py -d fp8 -kvd fp8 -n $n
  done
done

# against origin/main, same grid, 3 reps, both kernels
AITER_MLA_USE_OPUS={0,1} python3 op_tests/test_mla_persistent.py -d fp8 -kvd fp8 \
    -n {12,8|12,6|24,8|16,8|32,4|64,2|128,2} -b 16 32 64 -c 5200 8192 16384 65535

Verdicts below are against the golden fp8 reference — the one that quantises the same way the
kernel 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.

shape asm passed / warn / failed max abs delta opus passed / warn / failed max abs delta
12,8 37 / 27 / 0 0.055 31 / 33 / 0 0.094
12,6 38 / 26 / 0 0.070 35 / 29 / 0 0.086
16,8 35 / 29 / 0 0.051 31 / 33 / 0 0.094
32,4 33 / 31 / 0 0.047 29 / 35 / 0 0.109
64,2 34 / 30 / 0 0.055 32 / 32 / 0 0.086
128,2 36 / 28 / 0 0.043 28 / 36 / 0 0.109

12,4 and 24,4 were also run over the full grid: 0 failed on both kernels. 24,8 is rejected
at metadata (24 × 8 = 192 > 128), as intended.

Before the fix, for reference: 12,8 opus 16 / 64 cells failed, max |delta| 2.58;
12,4 opus 16 / 64 failed, max |delta| 2.88.

3. Current vs origin/main, decode time

decode:asm_576 us, minimum of 3 reps. Each shape ran all four arms on one GPU with
the reps interleaved. Positive delta = current is faster.

asm

shape batch ctx origin/main us current us delta
16,8 16 5200 48.31 47.48 +1.72%
16,8 16 8192 60.93 60.41 +0.85%
16,8 16 16384 86.64 85.38 +1.45%
16,8 16 65535 219.64 219.89 -0.11%
16,8 32 5200 67.31 66.81 +0.73%
16,8 32 8192 87.72 87.53 +0.22%
16,8 32 16384 130.90 130.40 +0.38%
16,8 32 65535 397.25 398.95 -0.43%
16,8 64 5200 105.83 105.00 +0.79%
16,8 64 8192 132.75 131.79 +0.73%
16,8 64 16384 218.86 219.56 -0.32%
16,8 64 65535 759.30 757.43 +0.25%
16,8 total 2315.4 2310.6 +0.21%
32,4 16 5200 47.68 47.99 -0.64%
32,4 16 8192 59.90 60.50 -1.01%
32,4 16 16384 85.98 86.65 -0.78%
32,4 16 65535 218.72 218.96 -0.11%
32,4 32 5200 66.09 65.60 +0.75%
32,4 32 8192 85.66 85.06 +0.70%
32,4 32 16384 131.59 132.20 -0.46%
32,4 32 65535 396.74 397.15 -0.10%
32,4 64 5200 105.12 105.56 -0.41%
32,4 64 8192 134.75 131.03 +2.76%
32,4 64 16384 219.61 215.74 +1.76%
32,4 64 65535 758.52 758.55 -0.00%
32,4 total 2310.4 2305.0 +0.23%
64,2 16 5200 45.58 45.44 +0.31%
64,2 16 8192 59.98 60.42 -0.74%
64,2 16 16384 88.57 88.94 -0.41%
64,2 16 65535 227.53 226.46 +0.47%
64,2 32 5200 65.63 66.91 -1.94%
64,2 32 8192 86.87 87.82 -1.10%
64,2 32 16384 132.87 134.34 -1.11%
64,2 32 65535 416.24 414.96 +0.31%
64,2 64 5200 106.32 107.60 -1.21%
64,2 64 8192 136.54 137.15 -0.45%
64,2 64 16384 228.87 229.64 -0.33%
64,2 64 65535 795.19 799.77 -0.58%
64,2 total 2390.2 2399.4 -0.39%
128,2 16 5200 63.33 64.12 -1.25%
128,2 16 8192 79.33 78.71 +0.78%
128,2 16 16384 124.46 123.90 +0.45%
128,2 16 65535 363.86 367.67 -1.05%
128,2 32 5200 96.36 97.01 -0.68%
128,2 32 8192 125.10 125.70 -0.48%
128,2 32 16384 207.29 208.18 -0.43%
128,2 32 65535 691.10 688.32 +0.40%
128,2 64 5200 151.54 151.66 -0.08%
128,2 64 8192 207.51 208.57 -0.51%
128,2 64 16384 369.64 369.37 +0.07%
128,2 64 65535 1352.96 1351.74 +0.09%
128,2 total 3832.5 3834.9 -0.06%
all shapes 10848.5 10850.0 -0.01%

median per-cell -0.10%, range -1.94% .. +2.76%, 21/48 cells faster

4. Shapes origin/main cannot run -- current-tree time only

shape batch ctx asm us opus us opus vs asm
12,8 16 5200 43.55 41.04 +5.8%
12,8 16 8192 52.45 49.48 +5.7%
12,8 16 16384 77.57 75.28 +3.0%
12,8 16 65535 208.89 208.30 +0.3%
12,8 32 5200 60.49 58.88 +2.7%
12,8 32 8192 77.97 76.34 +2.1%
12,8 32 16384 126.75 121.67 +4.0%
12,8 32 65535 383.18 386.36 -0.8%
12,8 64 5200 98.01 95.73 +2.3%
12,8 64 8192 123.46 122.81 +0.5%
12,8 64 16384 209.04 209.89 -0.4%
12,8 64 65535 729.18 745.78 -2.3%
12,6 16 5200 40.49 37.87 +6.5%
12,6 16 8192 48.93 46.48 +5.0%
12,6 16 16384 71.65 72.70 -1.5%
12,6 16 65535 205.23 207.98 -1.3%
12,6 32 5200 57.85 55.14 +4.7%
12,6 32 8192 71.89 68.55 +4.6%
12,6 32 16384 121.13 120.22 +0.7%
12,6 32 65535 383.80 388.30 -1.2%
12,6 64 5200 93.38 91.68 +1.8%
12,6 64 8192 121.44 123.83 -2.0%
12,6 64 16384 208.87 209.40 -0.3%
12,6 64 65535 734.58 751.45 -2.3%

Submission Checklist

@minmengdie
minmengdie requested review from a team and a lite review from Copilot September 20, 2026 05:50
@minmengdie minmengdie changed the title MLA PS mode asm and opus kernel support nhead12,8 MI355 MLA PS mode asm and opus kernel support nhead12,8 Sep 20, 2026
@github-actions github-actions Bot changed the title MI355 MLA PS mode asm and opus kernel support nhead12,8 [ASM] [HIP] MLA PS mode asm and opus kernel support nhead12,8 Sep 20, 2026
@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 5695 --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.

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.

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 High severity · 1 Medium severity · 1 Low severity

Open (6)
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.

Comment thread aiter/mla.py Resolved
Comment thread aiter/ops/attention.py Resolved
Comment thread csrc/py_itfs_cu/asm_mla.cu Resolved
Comment thread op_tests/test_mla_persistent.py Resolved
Copilot AI review requested due to automatic review settings September 20, 2026 06:20

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.

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 High severity · 1 Medium severity

Open (3)
Resolved since last review (4)

Comment thread op_tests/test_mla_persistent.py Resolved
Copilot AI review requested due to automatic review settings September 20, 2026 06:32
@minmengdie
minmengdie force-pushed the mmd/dev/mla_ps_fp8_nhead12 branch from da98c21 to dafa76e Compare September 20, 2026 06:32
@minmengdie
minmengdie force-pushed the mmd/dev/mla_ps_fp8_nhead12 branch from dafa76e to 9763e15 Compare September 20, 2026 06:36

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.

Copilot review overview

🔵 Needs a closer look

A critical test-dispatch issue and multiple metadata consistency and coverage issues remain unresolved.

Review effort: Lite
Findings: 1 High severity · 1 Medium severity

Open (2)
Resolved since last review (1)

Copilot AI review requested due to automatic review settings September 20, 2026 06:37

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.

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 High severity

Open (1)
Resolved since last review (2)

Comment thread aiter/ops/attention.py Resolved
Copilot AI review requested due to automatic review settings September 20, 2026 06:53
@minmengdie
minmengdie force-pushed the mmd/dev/mla_ps_fp8_nhead12 branch from 9763e15 to 73b070d Compare September 20, 2026 06:53

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.

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 High severity

Open (2)

Comment thread csrc/py_itfs_cu/asm_mla.cu Resolved
@zufayu
zufayu requested a review from amd-ruitang3 September 21, 2026 01:11
@shengnxu
shengnxu merged commit 1e2230a into main Sep 23, 2026
57 checks passed
@shengnxu
shengnxu deleted the mmd/dev/mla_ps_fp8_nhead12 branch September 23, 2026 03:26
amd-ruitang3 pushed a commit that referenced this pull request Oct 7, 2026
….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]>
xiaohuguo2023 added a commit to xiaohuguo2023/vllm that referenced this pull request Oct 7, 2026
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]>
yhl-amd pushed a commit that referenced this pull request Oct 7, 2026
….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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants