Repository navigation
[Triton/Gluon] add block_cat_fused fused op - #3269
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
e41e246 to
09223e3
Compare
|
i will reject this one as it is not pure kernel, and introduce torch inductor which is not our focus domain yet |
09223e3 to
c36e615
Compare
|
Split this down to the kernel only per your scope point: it now adds just the |
|
@reger-men The CI ran on June 11 and 12 out of 17 shards failed (both MI300X and MI35X), while the same shards all pass on main from the same period — so these are regressions introduced by this PR. The logs have expired so the exact error is unknown, but the breadth of failure across all shards suggests an import or registration issue with the new op. Please rebase onto main and re-run CI so we can see the current error. |
|
Hi @reger-men — thanks for splitting this down to the pure kernel op earlier per @valarLip's scope point; the op itself is approved. There's one blocker left before it can merge. CI regression is still open. As noted on 6/29: the 6/11 run had 12 of 17 Standard Tests shards failing on both MI300X and MI35X, while the same shards passed on main from the same period — so these look like regressions introduced by this PR, not flaky infra. The current head still shows the same broad failures. Could you rebase onto the latest origin/main and re-trigger CI, then look at the failing shards? Since you're actively pushing other PRs, a quick pass here would likely get it over the line — it's already approved, this is the only thing holding it back. One more thing worth double-checking while you're in there: the fp16 path. The dispatch gate accepts Once CI is green after rebase, I'm happy to merge. |
c36e615 to
b08a1d8
Compare
|
@zufayu rebased onto current main and fixed the fp16 store you flagged: the kernel was hardcoding tl.bfloat16 on its stores while the op accepts fp16, so fp16 was silently downcast. It now stores in the output pointer's dtype, so fp16 is bitwise-correct (0.0 error vs 1.56e-2 before) and bf16 is unchanged, and I added fp16 fwd/bwd tests. The old red shards were a stale June artifact-download timeout rather than a code regression, so this fresh push should give a clean CI run. The force-push dismissed valarLip's approval as expected. Could you re-review once CI is green? |
|
@zufayu the four red shards here (Standard Tests MI35X 3+4, MI300X 3+4) aren't from this PR. The failing tests are test_aiter_add.py and test_aiter_addInp.py, and they die in torch's kineto profiler (_parse_kineto_results hits a UnicodeDecodeError on a non-utf-8 event name out of prof.key_averages().table()), nothing to do with block_cat. This PR only adds two new files, import aiter doesn't load block_cat (its dormant until the op is called), and those two tests are byte-identical to main and only do import aiter. I ran both on an MI35X box with the exact CI image and torch build and they pass. So this looks like a flaky runner/profiler condition, not a regression from the change. Could you or CI/CD run a consistency check or re-run those shards so we can confirm it's the environment and not this PR? |
|
|
|
Advisory review (static + hand-run; not a merge gate). Validation/Perf ran no GPU stage — reasons are on their lines below. Findings tagged Registers a new custom op torch.ops.aiter.block_cat_fused that fuses cat([leaky_relu(l12[..., :W1], slope), l12[..., W1:] * l4]) into one Triton forward kernel plus a single Triton backward kernel emitting grad_l12 and grad_l4 together, with an eager CPU/fp32 fallback and op_tests coverage. Review (advisory): 🔴 HIGH RISK — elevated AI-code risk (two structural checks fired); recommend thorough manual verification of the dispatch logic and test coverage 🔴 [verified] aiter/ops/block_cat_fused.py block_cat_fused_bw allocates the backward outputs from the original inputs — grad_l12 = torch.empty_like(l12), grad_l4 = torch.empty_like(l4) — and torch.empty_like preserves strides for dense-but-permuted tensors, while the Triton kernel writes both flat-contiguous (grad_l12_out_ptr + xindex). Repro on MI300X: l12 (64, 96) bf16 contiguous with l4 supplied as a transpose view (w.t()) gives a bitwise-correct forward but grad_l4 max diff 7.9 vs eager autograd; with both inputs transposed, 15930/16384 grad elements are wrong and the returned grad_l12 equals the correct gradient transposed. The failure is silent (no error, forward stays correct) and corrupts weight updates in training; the forward wrapper does this right (cat_out = empty_like(l12.contiguous())). Author must allocate the backward outputs contiguous (empty_like on the contiguous copies, or memory_format=torch.contiguous_format) and add a non-contiguous-input backward test. 📝 [verified] The op is dormant: the from .ops.X import * list in aiter/init.py and aiter/ops/init.py (license header only) never import aiter/ops/block_cat_fused.py, so torch.ops.aiter.block_cat_fused does not exist after import aiter — every consumer must import the module by hand, and no consumer PR is mentioned. (It also means the expired June red shards in test_aiter_add.py cannot be this file's doing, since import aiter never loads it — but only a fresh CI run settles that.) Reviewer should ask who imports aiter.ops.block_cat_fused and whether it belongs in the aiter namespace export list. 📝 [verified] The GPU gate in aiter/ops/block_cat_fused.py checks only l12 (l12.is_cuda and l12.dtype in (bf16, fp16)) — l4's dtype and device are neither asserted nor checked. Repro on MI300X: l12 bf16 + l4 fp32 on CUDA silently returns a bf16 tensor where the eager chain it replaces promotes to fp32 (max value diff 1.5e-2); cross-device input at least fails loudly in Triton. Reviewer should ask whether to assert l4.dtype == l12.dtype and same device at the op boundary. |
87799b2 to
663aa46
Compare
|
@zufayu thanks for the review. The earlier all-red CI was a stale-branch issue (the branch predated the ensure_zstd CI step), I rebased onto current main so the suite runs properly now, and the backward-contiguity fix, the int64 index guard, and the random-grad + non-contiguous backward tests you flagged are all in. Could you take another look when you get a chance? |
|
@zufayu CI is green now on 663aa46, Aiter Test and Standard Test Results both pass: https://github.com/ROCm/aiter/actions/runs/36454114970 One thing I never answered from August is the xnumel/XBLOCK naming. I'm fine renaming those to the BLOCK_SIZE style used in the rest of aiter, and if the scope question from valarLip is the real blocker please tell me and I'll rework it. Could you take another look when you get a chance? |
|
Thanks for addressing the previously reported correctness issues: the non-contiguous backward outputs, random upstream-gradient coverage, fp16 stores, and int64 indexing are now fixed, and the latest CI on this head is green. The remaining blocker is that this still does not follow the current AITER Triton structure. The Triton kernels, Python wrapper, and custom-op registration are all mixed into Please restructure it consistently with the existing AITER fusion ops:
Two API/compile issues also remain:
This branch is also 165 commits behind current main. Please rebase, perform the structural rework above, and run fresh CI afterward. |
|
@Dewei-Wang-sh Could you please review this PR as the Triton code owner? It adds a Triton fused forward/backward custom op, and the remaining blocker is aligning both the implementation and tests with the current AITER Triton fusion structure. Your review and sign-off would be valuable after the author completes that rework. |
663aa46 to
d85f931
Compare
|
@zufayu thanks for the detailed review. I reworked it along those lines and rebased on current main, new head is d85f931.
Kernel math didnt change. I compared old vs new on MI355X and fwd out, grad_l12 and grad_l4 are bitwise identical in all 68 cases (fp16, bf16, fp32, contiguous and transposed inputs, the test shapes plus 960000x576 at W1=128 and 512, and 3-D). Perf on an idle MI355X with the clock locked at 960000x576 is within 0.2% of the old kernel for fwd and bwd at both W1. All 28 tests pass, black and ruff report no issues, and fresh CI is running on the new head. I kept the bounds mask on even when N divides the block. I tried a mask-free variant, it was slower in 3 of the 4 fwd/bwd cells at that shape (up to 7%), so I dropped it. @Dewei-Wang-sh could you review this when you get a chance? |
There was a problem hiding this comment.
🟡 Changes recommended
Input validation can be disabled, benchmark bandwidth is miscalculated, and kernel trace representations omit specialization settings.
5 open findings
What changed in this PR
Adds a fused Triton operation combining block-wise leaky ReLU, multiplication, concatenation, and backward computation.
Changes:
- Adds fused forward/backward kernels and public autograd wrapper.
- Adds architecture-specific launch configurations.
- Adds correctness tests and a performance benchmark.
| File | Description |
|---|---|
aiter/ops/triton/fusions/block_cat_fused.py |
Implements validation, dispatch, custom ops, and autograd. |
aiter/ops/triton/fusions/__init__.py |
Exports the new operation. |
aiter/ops/triton/_triton_kernels/fusions/block_cat_fused.py |
Defines fused forward and backward kernels. |
aiter/ops/triton/configs/gfx942/triton/fusions/block_cat_fused/DEFAULT.json |
Adds gfx942 launch settings. |
aiter/ops/triton/configs/gfx950/triton/fusions/block_cat_fused/DEFAULT.json |
Adds gfx950 launch settings. |
aiter/ops/triton/configs/gfx1250/triton/fusions/block_cat_fused/DEFAULT.json |
Adds gfx1250 launch settings. |
op_tests/triton_tests/fusions/test_block_cat_fused.py |
Tests numerical, layout, compile, and fallback behavior. |
op_tests/op_benchmarks/triton/bench_block_cat_fused.py |
Benchmarks fused and eager implementations. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Adds torch.ops.aiter.block_cat_fused, which packs cat([leaky_relu(l12[..., :W1], slope), l12[..., W1:] * l4], dim=-1) into one custom op. Forward runs a single fused Triton kernel on the bf16/fp16 GPU path with an eager torch fallback for CPU/fp32. The kernels store their result in the input dtype (via the output pointer's element type) so an fp16 input stays fp16 with no silent bf16 downcast. The registered backward is a second fused Triton kernel that produces grad_l12 and grad_l4 together, so AOTAutograd dispatches one fused backward target instead of decomposing into per-half pointwise grads. Op-level forward/backward correctness tests cover the CPU fp32 fallback and the GPU bf16 and fp16 Triton paths against an eager reference. Signed-off-by: adlashab <[email protected]>
…, int64 index guard Three fixes on the block_cat_fused op: - backward allocated grad_l12/grad_l4 with torch.empty_like, which keeps a non-contiguous (e.g. transposed) input's strides while the kernel writes flat-contiguous. A permuted l12/l4 then got the flat writes scattered under the wrong strides and silently wrong grads. Allocate the grad outputs with contiguous_format so they match what the kernel writes. - the flat index math (pid * XBLOCK, row * W4 + col) is int32 and wraps into an out-of-bounds fault once M * W12 >= 2^31. Promote the offset to int64 in both the forward and backward kernels. - the backward tests only used out.sum().backward(), i.e. an all-ones upstream grad, so a kernel that drops the grad_cat factor still passed. Add a backward test with a random upstream grad and a non-contiguous-input backward test, both compared against eager autograd. Signed-off-by: adlashab <[email protected]>
Kernels go to aiter/ops/triton/_triton_kernels/fusions/block_cat_fused.py, wrapper and custom ops to aiter/ops/triton/fusions/block_cat_fused.py, test to op_tests/triton_tests/fusions/, plus bench_block_cat_fused.py, same as the other fusion ops. The xnumel/XBLOCK builders are replaced by module level kernels with make_kernel_repr. Kernel math is unchanged, launch configs moved to configs/<arch>/triton/fusions/block_cat_fused/DEFAULT.json with the same values as before. The gfx1250 DEFAULT.json is a byte-identical copy of gfx950. Review fixes on top: - l4 has to match l12's device and dtype on every path, so mixed bf16/fp32 asserts instead of silently differing from eager type promotion - fake impls return contiguous tensors like the real fwd and bwd do - the fwd/bwd custom ops register through torch_compile_guard and autograd goes through a torch.autograd.Function; block_cat_fused is exported from aiter.ops.triton.fusions Signed-off-by: adlashab <[email protected]>
- _check_inputs raises ValueError instead of assert, so python -O can't let a short l4 through to the kernel - the fwd and bwd kernel repr now carry W1/W12/W4/SLOPE next to BLOCK_SIZE_N (SLOPE needs the float handling from ROCm#6290) - the bench counts the grad_l12 write in the fwd_bwd bytes (3*W12 + 2*W4) - the empty-input test runs the backward too Signed-off-by: adlashab <[email protected]>
d85f931 to
fd5a389
Compare
|
@Dewei-Wang-sh @zufayu I went through the review bot points and rebased onto current main again (e8bdaf8), new head fd5a389.
On MI355X (torch 2.14.0, triton 3.8.0 CI wheel): test_block_cat_fused.py 28 passed, test_fused_sigmoid_mul.py 130 passed, black and ruff clean. Could one of you take a look? |
|
@zufayu @Dewei-Wang-sh the red on fd5a389 is Triton Tests (MI35X) / Shard 4 (Triton Test Results is red only because of it), and its not from this PR. The shard died with a segfault, exit 139, in On MI355X (rocm/pytorch latest, triton 3.8.0 CI wheel) that test passes 20 of 20 on this head and on main 033e8bd, and the whole test_mla_v4_triton.py file is 114 passed / 10 skipped on both. I also went through the failed Triton Test shard jobs since 10-12 (92 of them): this segfault shows up only in this run, other runs have one-off "Fatal Python error: Aborted" crashes in other shards, main b7f2fca included. Could someone re-run the failed jobs of run 38779511856? |
|
@Dewei-Wang-sh I re-reviewed the current head. The Triton layout rework and the correctness/API issues from the previous review have been addressed, and all 28 Could you please provide the final Triton-owner review/sign-off when you have a chance? This is the remaining review dependency. |
* add block_cat_fused fused op Adds torch.ops.aiter.block_cat_fused, which packs cat([leaky_relu(l12[..., :W1], slope), l12[..., W1:] * l4], dim=-1) into one custom op. Forward runs a single fused Triton kernel on the bf16/fp16 GPU path with an eager torch fallback for CPU/fp32. The kernels store their result in the input dtype (via the output pointer's element type) so an fp16 input stays fp16 with no silent bf16 downcast. The registered backward is a second fused Triton kernel that produces grad_l12 and grad_l4 together, so AOTAutograd dispatches one fused backward target instead of decomposing into per-half pointwise grads. Op-level forward/backward correctness tests cover the CPU fp32 fallback and the GPU bf16 and fp16 Triton paths against an eager reference. Signed-off-by: adlashab <[email protected]> * block_cat_fused: fix backward stride corruption, add random-grad test, int64 index guard Three fixes on the block_cat_fused op: - backward allocated grad_l12/grad_l4 with torch.empty_like, which keeps a non-contiguous (e.g. transposed) input's strides while the kernel writes flat-contiguous. A permuted l12/l4 then got the flat writes scattered under the wrong strides and silently wrong grads. Allocate the grad outputs with contiguous_format so they match what the kernel writes. - the flat index math (pid * XBLOCK, row * W4 + col) is int32 and wraps into an out-of-bounds fault once M * W12 >= 2^31. Promote the offset to int64 in both the forward and backward kernels. - the backward tests only used out.sum().backward(), i.e. an all-ones upstream grad, so a kernel that drops the grad_cat factor still passed. Add a backward test with a random upstream grad and a non-contiguous-input backward test, both compared against eager autograd. Signed-off-by: adlashab <[email protected]> * block_cat_fused: move into the triton fusions layout Kernels go to aiter/ops/triton/_triton_kernels/fusions/block_cat_fused.py, wrapper and custom ops to aiter/ops/triton/fusions/block_cat_fused.py, test to op_tests/triton_tests/fusions/, plus bench_block_cat_fused.py, same as the other fusion ops. The xnumel/XBLOCK builders are replaced by module level kernels with make_kernel_repr. Kernel math is unchanged, launch configs moved to configs/<arch>/triton/fusions/block_cat_fused/DEFAULT.json with the same values as before. The gfx1250 DEFAULT.json is a byte-identical copy of gfx950. Review fixes on top: - l4 has to match l12's device and dtype on every path, so mixed bf16/fp32 asserts instead of silently differing from eager type promotion - fake impls return contiguous tensors like the real fwd and bwd do - the fwd/bwd custom ops register through torch_compile_guard and autograd goes through a torch.autograd.Function; block_cat_fused is exported from aiter.ops.triton.fusions Signed-off-by: adlashab <[email protected]> * block_cat_fused: raise on bad inputs, full kernel repr, bwd byte count - _check_inputs raises ValueError instead of assert, so python -O can't let a short l4 through to the kernel - the fwd and bwd kernel repr now carry W1/W12/W4/SLOPE next to BLOCK_SIZE_N (SLOPE needs the float handling from #6290) - the bench counts the grad_l12 write in the fwd_bwd bytes (3*W12 + 2*W4) - the empty-input test runs the backward too Signed-off-by: adlashab <[email protected]> --------- Signed-off-by: adlashab <[email protected]>



Adds
block_cat_fused(l12, l4, slope, l1_width), which computesas one op with a fused backward. Its exported as
aiter.ops.triton.fusions.block_cat_fused. The two custom ops underneath aretorch.ops.aiter.block_cat_fused_fwdandtorch.ops.aiter.block_cat_fused_bwd(registered throughtorch_compile_guard), and autograd goes through atorch.autograd.Function. Since AOTAutograd sees one node, the backward writes grad_l12 and grad_l4 in a single kernel instead of decomposing into per-half pointwise passes that each read the activation from HBM again.Files follow the other fusion ops:
aiter/ops/triton/_triton_kernels/fusions/block_cat_fused.pyaiter/ops/triton/fusions/block_cat_fused.pyaiter/ops/triton/configs/{gfx942,gfx950,gfx1250}/triton/fusions/block_cat_fused/DEFAULT.json, same values on all three for nowop_tests/triton_tests/fusions/test_block_cat_fused.pyop_tests/op_benchmarks/triton/bench_block_cat_fused.pyfp16/bf16 CUDA inputs run the Triton kernels (fp32 math, stored in the input dtype). fp32 and CPU inputs dont hit Triton, they take the eager torch path. l4 has to match l12 in device, dtype and leading shape. The fake impls return contiguous tensors, same as the real fwd and bwd.
Test plan:
pytest op_tests/triton_tests/fusions/test_block_cat_fused.py, 28 tests. They cover fwd/bwd vs eager for fp16 and bf16 with a random upstream grad, non-contiguous inputs, fake vs real output layout, torch.compile fullgraph, 3-D input, empty input, the fp32 fallback on cpu and cuda, and the input asserts. All 28 pass on MI355X.