Repository navigation
[Triton/Gluon] Add fused_indexed_gated_residual for diffusion transformer blocks - #6416
Conversation
…rmer blocks out[m] = residual[m] + gate[indices[m]] * x[m]: the gated residual add that closes an attention or MLP branch of a DiT block, the counterpart of fused_rmsnorm_indexed_adaln. The gate table row is gathered per token (or broadcast when a block of tokens shares an index) instead of being materialised at [M, N], and the product is not written and re-read. round_intermediate=True reproduces the eager expression bit for bit. Signed-off-by: Sylvan Liu <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
Unsupported inputs are insufficiently validated, tuning is hardcoded, and the required benchmark is missing.
4 open findings
What changed in this PR
Adds a memory-efficient Triton kernel for indexed gated residual updates in diffusion transformer blocks.
Changes:
- Adds the fused kernel and public wrapper.
- Covers dtype, shape, indexing, striding, rounding, and in-place scenarios.
| File | Description |
|---|---|
aiter/ops/triton/fusions/fused_indexed_gated_residual.py |
Validates inputs and launches the kernel. |
aiter/ops/triton/_triton_kernels/fusions/fused_indexed_gated_residual.py |
Implements fused indexed gating and residual addition. |
op_tests/triton_tests/fusions/test_fused_indexed_gated_residual.py |
Adds correctness and API tests. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
vgokhale
left a comment
There was a problem hiding this comment.
Review: PR #6416 — fused_indexed_gated_residual
The kernel is well built. It computes out[m] = residual[m] + gate[indices[m]] * x[m] in one pass, with a repr, an int64 guard on the flattened index, a uniform-index broadcast fast path, and a round_intermediate option that matches the unfused rounding. The tests are well anchored: an independent reference, a test that the uniform fast path equals the scattered path, a test that every index is honoured, and an out=/in-place test. Comments are short.
Three points before merge.
1. Rule 6: share the indexed-gather with the sibling kernel
This kernel re-implements the per-token table gather that fused_rmsnorm_indexed_adaln already has, in the same fusions/ directory. The copied parts are the first-row fill for masked lanes (first = tl.load(idx_ptr + row_block * BLOCK_M), then other=first) and the uniform-index broadcast (tl.min(idx) == tl.max(idx) collapses the [BLOCK_M, BLOCK_N] gather to one row). Both kernels have this code.
These two are the open and close halves of the same diffusion-transformer block, so they will stay paired. Please factor the per-token indexed gather (with the uniform-broadcast fast path) into one shared Triton device helper, and call it from both fused_rmsnorm_indexed_adaln and this kernel. The gate/residual math stays here; only the gather moves to the helper.
2. Rule 3: add a benchmark
There is no benchmark. The whole point of this kernel is to save bandwidth by fusing the gather, the multiply, and the add. Please add bench_fused_indexed_gated_residual.py and report the fused time against the unfused residual + gate[indices] * x (the gather materialised at [M, N], product written and re-read), so the saving is measured, not assumed.
3. Test dtypes: drop fp16, and fp32 unless it is the reference
The dtype parameter is [torch.bfloat16, torch.float16, torch.float32]. aiter targets bf16; fp16 is not a target, so please remove it. Keep fp32 only if the test uses it as the high-precision reference; if it is tested as a production output dtype, drop it too. bf16 is the one to keep.
Summary
Good kernel: correct math, repr, int64 guard, the uniform fast path, and a test that pins it. Not a duplicate — no existing kernel does residual + gate[idx] * x. Before merge: factor the indexed gather into a shared helper with fused_rmsnorm_indexed_adaln (rule 6), add a benchmark (rule 3), and drop fp16 (and fp32 unless it is the reference) from the test.
… tiles to config, add benchmark - Factor the per-token table gather and its uniform-index broadcast into _triton_kernels/fusions/indexed_table.py and call it from both fused_rmsnorm_indexed_adaln and fused_indexed_gated_residual. Both kernels give bit-identical output before and after. - Move BLOCK_M, BLOCK_N and num_warps into configs/<arch>/triton/fusions/fused_indexed_gated_residual/DEFAULT.json for gfx942, gfx950 and gfx1201; gfx1250 is seeded byte-identical from gfx950. The values are the previous Python constants. - Validate the residual dtype (bf16/fp16/fp32), the gate dtype, and that all operands are CUDA tensors on one device. - Tests run bf16 only and cover the new checks. - Add bench_fused_indexed_gated_residual.py: fused against residual + gate[indices] * x. Signed-off-by: Sylvan Liu <[email protected]>
|
@vgokhale Thanks for the review. Addressed in 9bd5ac8:
|
The tests cover bf16 only, so reject the other dtypes instead of accepting untested ones. Signed-off-by: Sylvan Liu <[email protected]>
|
Can this PR be merged now? Thanks! |
|
The two failing Triton shards (3 and 7) timed out during |


Motivation
A diffusion transformer block ends each attention and MLP branch with a gated residual add:
As with the AdaLN modulation at the start of the branch, every token carries an index into a small table of gate vectors, one row per (modality, timestep) combination. #4659 added
fused_rmsnorm_indexed_adalnfor the opening step; this PR adds the matching closing step,fused_indexed_gated_residual.In torch the expression is three kernels:
gate[indices]is gathered and materialised at [M, N], multiplied byx(another [M, N] temporary), then added toresidual. It is memory bound, and most of its traffic is temporaries the math does not need.Technical Details
residualandxare read once andoutis written once.fused_rmsnorm_indexed_adaln.xcan be a column slice of a fused projection; only unit stride along N is required.outmay beresidual, giving an in-place update of the residual stream.round_intermediate=Trueroundsgate * xto the output dtype before the add, as the unfused expression does. The result is then bit-identical toresidual + gate[indices] * xin eager torch. The launch disables fp fusion; otherwise the compiler can contract the rounded product back into an FMA and silently drop that rounding (seen on gfx1201).Test Plan
op_tests/triton_tests/fusions/test_fused_indexed_gated_residual.py, 91 tests:round_intermediate× 7 shapes, including a single token, fewer rows thanBLOCK_M, N not a multiple ofBLOCK_N, and a row narrower than one tile. Each shape runs with both run-of-index and scattered indices.round_intermediate=Trueis checked bit-exact against the eager torch expression.out=and in-placeout=residual.Test Result
All 91 pass on gfx1201 (Radeon AI PRO R9700). No MI hardware was available to me, so gfx942/gfx950 coverage comes from CI.
Timing on gfx1201, bf16, at MiniMax-H3 shapes (N = 5376, a 9-row gate table,
round_intermediate=True). M is the token count per rank at Ulysses 1/2/4/8 for a 15 s, 1344x768 clip.residual + gate[idx] * xThe fused output is bit-identical to the torch expression at every size. Effective bandwidth counts 3 x M x N x 2 bytes, against a 640 GB/s peak.
A HIP implementation of the same op, with the same interface and results, is #6417 (one backend per PR).
AI assistance: this change was developed with the help of an AI coding assistant (GitHub Copilot). All tests and timings above were run on the listed hardware.