Repository navigation
[diffusion] fix: keep residual_gate_add on the JIT CUDA path for cont… - #42240
Merged
BBuf merged 2 commits intoOct 3, 2026
Merged
Conversation
…iguous layouts sgl-project#41305 replaced the shared residual_gate_add JIT CUDA operator with Triton kernels and a dispatch heuristic tuned for SANA-Video on H200. The kernel is shared by Ideogram, Flux-2, Qwen-Image 2.1, ERNIE-Image and LTX-2, and for the contiguous layouts those models use the Triton path costs 13-15.6us more host time per call. The extra cost is flat in tensor size, so it is per-call dispatch overhead rather than slower arithmetic, and it dominates whenever the call is launch-bound. LTX-2 is launch-bound here: a denoise step issues roughly 800 calls, at (1, 384, 4096) on the video path and (1, 26, 2048) on the audio path, from eight call sites across 48 layers. That turned into ~8ms per denoise step and pushed ltx_2_two_stage_t2v and ltx_2_3_two_stage_ti2v_2gpus onto the 20% pr_test limit, where about half of all CI runs now cross it. Take the Triton path only for the transposed-dense layout it was benchmarked on and keep the JIT CUDA kernel elsewhere. Also drop two redundant can_use_residual_gate_add_cuda calls: it ran three times per invocation (entry, residual_gate_add_cuda, _residual_gate_add_triton) at ~2.7us each, where the pre-sgl-project#41305 code ran it twice. Measured on 2x H200, dependencies pinned, sglang commit the only variable. Host time per call, bfloat16, at the shapes LTX-2 actually issues: shape sgl-project#41305 this patch pre-sgl-project#41305 (1, 26, 2048) bcast 36.50us 23.80us 23.46us (1, 384, 4096) bcast 39.18us 23.92us 23.43us (1, 1536, 4096) full 34.43us 19.91us 19.19us (1, 384, 4096) full 34.66us 20.35us 20.04us ltx_2_two_stage_t2v avg denoise, three runs each: main 544e46c 164.7 / 162.5 / 161.6 median 162.5 main + this patch 158.2 / 158.1 / 157.4 median 158.1 main + whole file reverted 157.1 / 156.8 / 157.0 median 157.0 fc9e1c8 (before sgl-project#41305) 156.3 / 157.9 / 154.7 median 156.3 Output is unchanged: the CUDA and Triton backends were compared on 37 configurations (bfloat16 and float16, six shapes, full/broadcast/per-token gates, plus the transposed-dense layout) and are bit-identical on all of them. The SANA-Video shapes keep the Triton path. If it also wins on contiguous layouts at the sizes SANA-Video issues, the dispatch can be widened again on the back of a benchmark that covers the other models sharing this kernel.
niehen6174
requested review from
BBuf,
DarkSharpness,
HaiShaw,
HydraQYH,
celve and
yuan-luo
as code owners
October 2, 2026 15:52
This was referenced Oct 2, 2026
BBuf
approved these changes
Oct 3, 2026
This was referenced Oct 3, 2026
4 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
#41305 replaced the shared
residual_gate_addJIT CUDA operator with Triton kernels, benchmarked on SANA-Video on H200 where it gained 1.6–1.7% E2E. That PR's profiling confirmed the branch executing there is_rga_transposed.The kernel is also shared by Ideogram, Flux-2, Qwen-Image 2.1, ERNIE-Image and LTX-2, which call it on contiguous layouts. There the Triton path costs 13–15.6 µs more host time per call. The cost is flat in tensor size, so it is per-call dispatch overhead, not slower math — negligible when bandwidth-bound, dominant when launch-bound.
LTX-2 is launch-bound: ~800 calls per denoise step at
(1, 384, 4096)and(1, 26, 2048), from 8 call sites across 48 layers. That adds ~8 ms per step and movedltx_2_two_stage_t2vandltx_2_3_two_stage_ti2v_2gpusfrom ~1.12× to 1.20–1.23× of theirh100.jsonbaselines. Thepr_testlimit is 1.20×, so about half of CI runs now cross it.Found by bisecting 315 commits on 2× H200. Reverting this one file on current main removes the regression entirely.
Modifications
One file, +9/−4.
can_use_residual_gate_add_cudacalls — it ran 3× per invocation at ~2.7 µs each (pre-[KDA+Kimi K3] Speed up SANA-Video residual gate add on H200 #41305 ran it twice).residual_gate_add, which has already run the same predicate.residual_gate_add_cudastays public and keeps validating.Accuracy Tests
The CUDA and Triton backends were compared on 37 configurations (bf16/fp16, six shapes, full / broadcast / per-token gates, plus transposed-dense) and are bit-identical on all of them (
torch.equal). Both already claimed bit-exact rounding, so routing between them by layout cannot change output.Speed Tests and Profiling
2× H200, dependencies pinned, sglang commit the only variable.
Host time per call, bf16, at the shapes LTX-2 actually issues (confirmed by counting dispatch branches over a real run):
(1, 26, 2048)(1, 384, 4096)(1, 1536, 4096)ltx_2_two_stage_t2vavg denoise step, three runs each:544e46c080(current main)fc9e1c8d29(before #41305)Recovers 4.4 ms of the 6.2 ms regression. A ~1.1 ms gap to the full revert remains: 672 of 24000 calls (gate same shape as residual) still fall through to the eager path. Can follow up separately.
The microbenchmark covers contiguous layouts only — I did not measure whether Triton also wins there at SANA-Video's sizes. If it does, the dispatch can be widened on the back of a benchmark covering the other models sharing this kernel;
test/registered/kernels/benchmark/diffusion/bench_residual_gate_add.pyalready has workloads for most of them.Checklist
cc @BBuf
CI States
Latest PR Test (Base): ⏳ Run #37088752701
Latest PR Test (Extra): ❌ Run #37088767953
Latest PR Test (AMD ROCm 10): ❌ Run #37088752682