Skip to content

[HIP] Lift the 2 GiB limit on mla_reduce_v1 final output and LSE - #5698

Merged
shengnxu merged 2 commits into
mainfrom
kaiyang-1/fix_mla_reduce_offset
Sep 21, 2026
Merged

shengnxu merged 2 commits into
mainfrom
kaiyang-1/fix_mla_reduce_offset

Conversation

@kaiyang-1

Copy link
Copy Markdown
Contributor

mla_reduce_v1 rejected any call whose final output or final LSE spanned 2 GiB or more. The limit was real: both stores went through a buffer descriptor based at the tensor pointer with a 32-bit signed byte offset carrying the full (seq, head) index, so a larger tensor would have silently wrapped. It is easy to reach — bf16 output at nhead=128 and batch=16384 is exactly 2 GiB, so -n 128,1 -b 16384 failed on every context length, on both the opus and asm decode paths.

seq_idx and head_idx are wave-uniform in all four store sites, so they now go into the 64-bit descriptor base and only the per-lane element offset stays in the 32-bit voffset. This is the same treatment the partial-output buffer already gets a few lines above, and it brings the buffer-store paths in line with mla_reduce_v1_impl_* massive's plain-pointer variant, which was already 64-bit. With nothing large left in the voffset, both host-side span checks and the kMaxSpanBytes constant are gone.

Validation

The four previously rejected configurations now run and match the torch reference on both paths:

PYTHONPATH=. python3 op_tests/test_mla_persistent.py -d bf16 -kvd bf16 -n 128,1 -b 16384 -c 224 2048

No regression

Folding the offset into the base means the descriptor is rebuilt per sequence instead of hoisted out of the loop, so the cost was measured on the shape where the reduce carries the most weight relative to the decode (-n 96,4 -c 256, over a thousand partials against a ~30 us kernel), five runs each, comparing medians:

before after delta
-b 32 31.186 us 31.190 us +0.01%
-b 64 36.027 us 35.958 us -0.19%

Both directions sit inside the run-to-run spread and the two distributions overlap almost entirely. A single-shot comparison had suggested up to +1.2%, which the repeats did not reproduce.

@kaiyang-1
kaiyang-1 requested review from a team and a lite review from Copilot September 20, 2026 08:11
@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 5698 --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.

@github-actions github-actions Bot changed the title Lift the 2 GiB limit on mla_reduce_v1 final output and LSE [HIP] Lift the 2 GiB limit on mla_reduce_v1 final output and LSE Sep 20, 2026
@kaiyang-1
kaiyang-1 requested a review from ruanjm September 20, 2026 08:11
@github-actions github-actions Bot added the HIP label Sep 20, 2026

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 massive no-LSE path performs null-pointer arithmetic, and large individual output strides can still truncate through the int32 ABI.

Get a fresh assessment by requesting another Copilot review.

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

Open (2)
What changed in this PR

Lifts the 2 GiB final-output/LSE restriction in mla_reduce_v1 by moving sequence/head offsets into 64-bit descriptor bases.

Changes:

  • Updates massive and simple reduction stores to use 64-bit pointer offsets.
  • Removes host-side 2 GiB span checks.
File Description
csrc/​kernels/​mla/​reduce.cu Implements large-output addressing for final output and LSE stores.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread csrc/kernels/mla/reduce.cu Resolved
Comment thread csrc/kernels/mla/reduce.cu Resolved
Copilot AI review requested due to automatic review settings September 20, 2026 09: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.

Copilot review overview

🟡 Changes recommended

The DS32 dense-KV mode is disabled and the new LSE addressing path lacks regression coverage.

Get a fresh assessment by requesting another Copilot review.

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

Open (2)
Resolved since last review (2)

Comment thread op_tests/test_mla_persistent.py Resolved
Comment thread csrc/kernels/mla/reduce.cu Resolved

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

@shengnxu
shengnxu merged commit babc586 into main Sep 21, 2026
57 checks passed
@shengnxu
shengnxu deleted the kaiyang-1/fix_mla_reduce_offset branch September 21, 2026 02:51
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]>
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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants