Repository navigation
[HIP] [OPUS] [JIT] Fix the hardcoded die and CU counts in the opus kernels - #5848
Conversation
1491cda to
b97918a
Compare
bc66791 to
47d4b6c
Compare
b97918a to
66e312b
Compare
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🟡 Changes recommended
The gfx950 generator ignores explicit target selection and can emit incorrect XCD traits, while the changes also break an existing caller and the generator test’s torch stub.
3 open issues
What changed in this PR
This PR updates gfx950 OPUS kernels to use build-target die counts and device CU counts, continuing the backend-wide hardware-count fixes. It also repairs partial swizzle tails that could leave output tiles unwritten.
Changes:
- Pass build-target die counts through GEMM, BMM, and MoE code generation and traits.
- Correct partial-group tile mapping in GEMM and MoE kernels.
- Use device CU counts for persistent GEMM scheduling and add per-SKU die-count tests.
| File | Description |
|---|---|
op_tests/test_gemm_codegen.py |
Adds per-SKU die-count checks. |
csrc/opus_moe/include/gfx950/a8w4/opus_moe_traits_stage2_a8w4_decode_gfx950.cuh |
Parameterizes decode route-out die count. |
csrc/opus_moe/include/gfx950/a8w4/opus_moe_pipeline_stage2_a8w4_decode_main_gfx950.cuh |
Fixes partial-group tile mapping. |
csrc/opus_moe/include/gfx950/a16w16/opus_moe_traits_stage2_gfx950.cuh |
Parameterizes BF16 stage-2 die count. |
csrc/opus_moe/include/gfx950/a16w16/opus_moe_pipeline_stage2_gemmstyle_gfx950.cuh |
Fixes stage-2 swizzle tails. |
csrc/opus_moe/gen_instances.py |
Emits target die counts in MoE traits. |
csrc/opus_gemm/opus_gemm_tune.py |
Uses supplied CU counts in shape filtering. |
csrc/opus_gemm/include/gfx950/opus_gemm_traits_a8w8_scale_gfx950.cuh |
Threads die counts through MXFP8 traits. |
csrc/opus_gemm/include/gfx950/opus_gemm_traits_a16w16_gfx950.cuh |
Parameterizes standard and persistent GEMM die counts. |
csrc/opus_gemm/include/gfx950/opus_gemm_pipeline_a8w8_mxscale_flatmm_splitk_gfx950.cuh |
Uses trait die counts for tile mapping. |
csrc/opus_gemm/include/gfx950/opus_gemm_pipeline_a8w8_mxscale_bpreshuffle_wave8_gfx950.cuh |
Uses trait die counts for swizzling. |
csrc/opus_gemm/include/gfx950/opus_gemm_pipeline_a16w16_persistent_gfx950.cuh |
Updates the swizzle comment only. |
csrc/opus_gemm/include/gfx950/opus_gemm_pipeline_a16w16_persistent_4g_safe_gfx950.cuh |
Updates the swizzle comment only. |
csrc/opus_gemm/include/gfx950/opus_gemm_pipeline_a16w16_gfx950.cuh |
Corrects partial-group resume offsets. |
csrc/opus_gemm/include/gfx950/opus_bmm_traits_mxscale_compact_gfx950.cuh |
Adds a compact BMM die-count parameter. |
csrc/opus_gemm/include/gfx950/opus_bmm_pipeline_mxscale_compact_gfx950.cuh |
Distributes BMM tiles using the trait count. |
csrc/opus_gemm/codegen/gen_instances_gfx950.py |
Emits die counts, matching grids, and device-CU scheduling. |
csrc/gemm_a16w16/gemm_a16w16_tune.py |
Passes CU count into OPUS shape filtering. |
aiter/jit/utils/build_targets.py |
Adds die-count lookup and cached build-target resolution. |
Review effort: Balanced
- Bakes the build target's XCD count into the opus GEMM, BMM and MoE instances through build_num_xcds, instead of a fixed eight. - Adds target_num_xcds and build_num_xcds to build_targets, resolving the count from the (gfx, CU count) targets a build compiles for. - Reads the device's CU count in the generated GEMM launcher, and passes the tuned CU count to the tuners' shape check, where both assumed 256.
19b36a4 to
d97a1ae
Compare
|
I undid the PR stack, based each branch on main as one commit, and force-pushed. They don't depend on each other, so they can merge in any order. |
- Imports build_targets from aiter/jit/utils in the opus GEMM and MoE generators instead of through aiter.jit.utils, which ran aiter/__init__ and its torch import during codegen. - Shares the aiter/jit/utils lookup between the opus GEMM entry point and its gfx950 emitters through codegen/common.py.
…argets - Resolves the gfx950 die count once in gen_instances.py and passes it to the emitters on the codegen object, instead of each emitter resolving it from the environment. - Takes the count from --targets when the generator is given them, so an explicit gfx950/128 build bakes four dies whatever GPU_ARCHS says; without them it resolves the build target as before.
vpietila-amd
left a comment
There was a problem hiding this comment.
Looks good to me. These are mostly mechanical changes. The only more complex change is the XCD swizzle fix where we could add a new test that fails prior to the fix and passes with the current code changes.
- Runs the a16w16 pipeline on a 13x20 tile grid into a NaN-filled output, where the per-cycle XCD remap covers only the first 256 tiles and the remaining 4 fall in a last row group shorter than the swizzle width, and checks that every tile is written and matches the reference. The remap permutes whole cycles of dies times SWIZZLE_C workgroups, so the leftover partial cycle takes the tail path at both four and eight dies, and the previous tail resume left tiles of that short group unwritten.
- Applies the XCD remap only below the last full cycle and leaves the tail workgroups' indices as they are, so the same group mapping places every tile. - Removes the separate tail arithmetic from the a16w16 GEMM pipeline and the two opus MoE stage-2 pipelines. The remap permutes each full cycle onto itself, so keeping the tail's indices leaves a permutation of all tiles, and the group mapping already handles a short last row group.
- Targets twice the device's CU count when the persistent a16w16 launcher picks split_m, matching the two workgroups per CU its __launch_bounds__ keeps resident. The launcher used to aim for one workgroup per CU. Its previous hard-coded 256 CUs happened to give two per CU on a 128-CU part, so reading the real CU count halved the grid there, and on 256-CU parts the grid was half what the kernel can hold.


Passes the build target's die count into the opus GEMM and MoE traits, sizes the persistent GEMM grid for the device's CUs at two workgroups each, and fixes a swizzle tail that drops work at any die count.
The persistent a16w16 GEMM sized its grid for one workgroup per CU with a hard-coded 256 CUs, though the kernel holds two; sizing it from the device's CUs at two per CU makes kid 30 (the heuristic's pick for most shapes above M=128) faster over the 94 tuned opus shapes: 1.17x on geomean on MI350P (at most 1.89x) and 1.09x on MI355X (at most 2.00x). The tuned GEMM and MoE rows keep their timings on MI355X.
Third of four, one per backend: FlyDSL (#5504), Triton (#5505), this one, HIP (#6877).
Motivation
The split-barrier traits already carried a
NUM_XCD_parameter, but codegen never emitted a value, so every instance took the default of eight. On a four-die part both the launcher and the kernel still group tiles for eight dies: the swizzle stays a bijection, but it lays work out for dies the part does not have.Wiring it up surfaced an older defect. The tail covering a partial last group divides the covered tile count by the group width, while the layout it indexes uses that group's own row extent. When the last row group is shorter than the group width, the tail resumes too early in that group: it recomputes tiles the swizzle already wrote and runs out of workgroups before the group's last tiles, which keep whatever the output buffer held. This predates this branch. On 256-CU gfx950 parts it affects three shipped bf16 tuned rows (N=50016, K=6144) when a batch size between tuned sizes rounds up to them, for example M=2305 using the M=4096 row.
Technical Details
NUM_XCD_moves ahead of the defaulted trait parameters so codegen can reach it positionally. Three of the five traits cannot drop the default, since C++ forbids a non-defaulted parameter after a defaulted one.aiter/jit/utils/build_targets.py, so the opus codegen, the FlyDSL AOT and the HIP helper in [HIP] Fix the hardcoded die count in the HIP kernels #6877 can also use it.ImportErroronly, so a real failure in target resolution surfaces rather than silently falling back to eight.split_mfor two workgroups per CU, the residency its__launch_bounds__declares. It aimed for one per CU with a hard-coded 256 CUs: on a 128-CU part that happened to be two per CU, so reading the device's CU count halved the grid there, and on 256-CU parts the grid was half what the kernel holds.wave1kernel groups tiles by eight so that all splits of a tile run on one die and meet in its L2. That still holds on four dies, since workgroups equal modulo eight are also equal modulo four.Testing
On an MI355X (eight dies):
test_gfx950_xcd_swizzle_tail_writes_every_tileruns kid 5 on 13x20 tiles into a NaN-filled output. It fails with the tail fix reverted and passes with it.test_opus_a16w16_gemm.pypasses (37 passed, 9 skipped), and so doestest_gemm_codegen.py.test_moe_2stage.pyon the 24 shipped rows with a non-atomic opus a8w4 stage 2:logits_diffat most 0.0005.split_mtarget.At 59ee9a4, timed against the merge base, run base, head, base on one GPU; speedup is the mean of the two base runs over head:
The two GEMM rows at 0.94x match the second base run; their two base runs differ by 10% and 13%. The five tuned rows on a persistent kernel are not among the shapes the occupancy target changes. The MXFP8 BMM device code is identical at eight dies; its timings differ between builds by up to 11% in either direction, and swap sign between two measurements, so they are not attributed to this branch.
The persistent kernels are where the die and CU counts change performance. Persistent kid 30, the heuristic's pick for most shapes above M=128, over the 94 shapes of the tuned opus rows at batch 1, against main:
On MI350P the die count gives the small-M shapes up to 1.89x, and the two-per-CU target the large-N ones up to 1.78x; 3072x4096x1536 (0.94x) and 2268, 5994 and 16644 by 6144 by 6144 (0.96x to 0.97x) remain slightly slower. On MI355X only the occupancy target changes, giving the large-N and large-M shapes 1.1x to 2.0x. Persistent kid 76 refuses most of those shapes and stays within 1.5% on both. The MXFP8 BMM sweep is unchanged on MI350P (all shapes within 1.5%).
Persistent kid 30 on MI355X (µs)
Persistent kid 30 on MI350P (µs)
PR Self-Review
Reviewed revision
59ee9a4
Behavior and intent
The opus GEMM and MoE codegen bakes the build target's die count instead of 8. It also fixes a swizzle tail that skips tiles when the last group is short, at any die count, by sending the tail through the same row-group mapping as the rest. The persistent launcher now sizes its grid for two workgroups per CU.
Validation and unverified scope
On MI350P (gfx950, 128 CUs, 4 XCDs, ROCm 7.2.3), against
main, at 8881187 (before the tail was simplified):op_tests/test_gemm_codegen.pypasses, and the generated instances bake 4.mainand 0 on this branch.main.On a 256-CU gfx950,
opus_gemm_512x256x256x64at M=2305, N=50016 with a NaN-filled output leaves 168 tiles unwritten onmain(96175f6) and none on this branch. On MI350P the affected kernels are not built by default.At ebe1de5 on MI355X: the new swizzle-tail test fails with the fix reverted and passes with it,
test_opus_a16w16_gemm.pypasses, the 24 shipped non-atomic opus a8w4 MoE rows pass intest_moe_2stage.py, and timings match the merge base (see Testing). Persistent kid 30 is 1.09x faster on geomean on MI355X and 1.17x on MI350P, with one shape below 0.95x.Unverified: none
PR-specific question
Only gfx950 opus kernels change. The die count is fixed per build from the build target, and the launcher and kernel read the same constant, so a mismatched part loses cache locality, not correctness. The tail change applies to workgroups past the last full swizzle cycle.
Changed op tests
op_tests/test_gemm_codegen.py: addstest_target_num_xcds, covering the die count per SKU. No other test covers that table.op_tests/test_opus_a16w16_gemm.py: addstest_gfx950_xcd_swizzle_tail_writes_every_tile, the only test that reaches the swizzle tail with a short last row group.op_tests/test_opus_co_integration.py: passes the CU count tokid_rejects_shape, which now takes it. No test is added.