Repository navigation
[HIP] Lift the 2 GiB limit on mla_reduce_v1 final output and LSE - #5698
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
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
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.
There was a problem hiding this comment.
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
Open (2)
Resolved since last review (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]>
….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]>



mla_reduce_v1rejected 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 atnhead=128andbatch=16384is exactly 2 GiB, so-n 128,1 -b 16384failed on every context length, on both the opus and asm decode paths.seq_idxandhead_idxare 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 withmla_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 thekMaxSpanBytesconstant are gone.Validation
The four previously rejected configurations now run and match the torch reference on both paths:
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:-b 32-b 64Both 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.