Repository navigation
[diffusion] ERNIE-Image bit-exact fused RMSNorm+scale/shift (H200 1024^2 e2e 15.63 -> 15.00 s, denoise -3.3%) - #33854
Merged
Conversation
…4^2 e2e 15.63 -> 15.00 s, denoise 14.85 -> 14.35 s) Reimplement the two ERNIE adaLN norm fusions that sgl-project#33734 dropped (the CuTe-DSL fused_norm_scale_shift / fused_scale_residual_norm_scale_shift kernels from closed sgl-project#30170, re-rejected at same-seed PSNR 18.83 dB behind quality=high) as Triton kernels that replicate the eager chain's rounding step for step: the flashinfer CuTe RMSNormKernel reduction order (per-fragment ordered fp32 fadd chain with separately rounded squares, adjacent-pairs shfl-butterfly fold trees, rsqrt.approx.f32) plus every aten bf16 op boundary of the modulate chain, and, at the second site, the residual_gate_add_cuda pair it subsumes. Both kernels are bit-exact (torch.equal at real shapes; full 1024x1024 images md5 identical to main across four arms), so they run on the default lossless path with no quality gate. Because bit-exactness is a property of which rmsnorm implementation the platform dispatches to, the model wrappers verify torch.equal against the eager chain once at runtime and permanently fall back on mismatch or exception, so a platform with different rmsnorm numerics can never silently change outputs. H200, baidu/ERNIE-Image-Turbo, 1024x1024, 50 steps, fixed prompt+seed, same GPU, sequential arms, median of 9 (first of 10 dropped), baseline run twice: e2e wall 15.63 -> 15.00 s (-4.0%), DenoisingStage 14.85 -> 14.35 s (-3.3%). Kernel microbench at (1,4216,4096) bf16: norm site 115.8 -> 33.3 us (3.47x), gated-norm site 143.8 -> 64.5 us (2.23x); projected 582 ms/image vs 493 ms measured. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
BBuf
requested review from
AgainstEntropy,
HaiShaw,
mickqian,
ping1jing2,
yichiche and
yingluosanqian
as code owners
August 6, 2026 08:55
Collaborator
Author
saturn-acc
pushed a commit
to saturn-acc/sglang
that referenced
this pull request
Aug 16, 2026
…4^2 e2e 15.63 -> 15.00 s, denoise -3.3%) (sgl-project#33854) Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Atituiset
pushed a commit
to Atituiset/sglang
that referenced
this pull request
Sep 10, 2026
…4^2 e2e 15.63 -> 15.00 s, denoise -3.3%) (sgl-project#33854) Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
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
PR #33734 shipped the bit-exact residual-gate half of the ERNIE-Image adaLN block and explicitly dropped the other two fusions from closed PR #30170 — the CuTe-DSL
fused_norm_scale_shift/fused_scale_residual_norm_scale_shiftkernels — because they are not bit-exact: they computenorm(x)*(1+scale)+shiftin one fp32 pass, and re-validating them behind aquality=highgate perturbed the 50-step trajectory to same-seed full-image PSNR 18.83 dB / SSIM 0.797 vs lossless, far below the PSNR>25 bar.This PR is not a re-enable of those kernels. It reimplements both fusions as Triton kernels that are numerically re-engineered to be rounding-faithful to the eager chain, step for step, so they are bit-exact (
torch.equal, full-image md5-identical) and can be wired on the default lossless path with no quality gate — the same standard #33734 used.Per 1024x1024 image the block executes
norm+modulate2 sites x 36 blocks x 2 CFG x 50 steps = 7,200 times; eager spends 4-5 kernels per site (sgl_kernel.rmsnorm,1+scale,mul,add, plusresidual_gate_add_cudaat the second site), all bandwidth-bound on (1, 4216, 4096) bf16 activations.What "rounding-faithful" means here
The eager
RMSNorm.forward_cudadispatches tosgl_kernel.rmsnorm-> flashinfer's CuTe-DSLRMSNormKernel. The Triton kernel replicates that kernel's exact arithmetic DAG for the contiguous bf16 regime ERNIE uses (H == 64 * threads_per_row, cluster_n == 1):cols = 8*tpr*b + 8*tx + v,vfastest) and its ordered sequential fp32 fadd chain (eachx*xseparately rounded via an opaquemul.rn.f32, no FMA contraction — MLIRvector.reductionsemantics);shfl.bflybutterfly with offsets 1,2,4,8,16 produces (expressed with order-exacttl.reshape/tl.splitfolds), then the two warp sums added;rstd = rsqrt.approx.f32(sum_sq/H + eps)(inline PTX, matchingcute.math.rsqrtfastmath);(bf16)(x * rstd * w), then the aten modulate chain's per-op bf16 boundaries (round(1+scale),round(y*(1+scale)),round(+shift)) reproduced with a bitcast round-to-nearest-even helper (same trick asindexed_modulation.py);round(gate*update),round(residual+...)— identical to theresidual_gate_add_cudafast path it subsumes at that site.Because bit-exactness is a property of which rmsnorm implementation the platform dispatches to, the model-side wrappers verify
torch.equalagainst the eager chain once at runtime on the first real input and permanently fall back to eager on any mismatch (e.g.FLASHINFER_USE_CUDA_NORM=1, or a future flashinfer numerics change). The fast path can never silently change outputs.Modifications
python/sglang/kernels/ops/diffusion/triton/rmsnorm_scale_shift_bitexact.py(new): the two Triton kernels + guards, registered as custom ops (torch.compile-safe fake impls).ernie_image.py:_ernie_norm_scale_shift/_ernie_gated_norm_scale_shiftwrappers (first-call self-verification, one-time disable on exception/mismatch, same style as_ernie_residual_gate_addfrom [diffusion] ERNIE-Image bit-exact residual-gate fast path (H200 1024^2 e2e 16.17 -> 15.75 s) #33734) wired at both adaLN sites. The second site subsumesresidual_gate_add_cuda+ rmsnorm + modulate into one kernel and still returns the bit-identical residual stream.test/registered/kernels/ops/diffusion/test_ernie_norm_scale_shift.py(new): bit-exactness at the real ERNIE shape, a CFG-batched shape and the tpr=32 regime, plus an assertion that the fast paths were actually taken (not silently disabled).Speed Tests (H200)
baidu/ERNIE-Image-Turbo, 1024x1024, 50 steps, bf16, eager, fixed prompt,seed=42,use_pe=False. Single process, 10 consecutive generations, first dropped, median of 9. Both arms on the same GPU, sequential.Kernel microbenchmark at the real shape
(1, 4216, 4096)bf16, 200 iters:norm*(1+scale)+shift(rmsnorm + 3 aten)res=residual+gate*upd; norm(res)*(1+scale)+shift(residual_gate_add_cuda + rmsnorm + 3 aten)Attribution: 36 blocks x (82.5 + 79.2) us saved x 2 CFG x 50 steps ~ 582 ms/image projected vs 493 ms measured (~85% materialized).
Accuracy Tests
torch.equal, (1,4216,4096)/(1,4096,4096)/(2,1140,4096)/(1,128,2048) bf16True324a06b56c4616aeaebd0d8d5017de9eon all four arms, incl. a second-GPU cross-check; also identical to the #33734-era main md5)Unit test (registered,
base-b-kernel-unit):pre-commit run --files ...: all hooks pass.Relation to #33734 / #30170
#30170 (closed) contained three fusions. #33734 shipped the residual-gate one (bit-exact as-is) and dropped the two norm fusions with the 18.83 dB evidence. This PR completes the set by re-deriving the two norm fusions with faithful numerics instead of re-gating the lossy CuTe-DSL implementations, which remain unused by ERNIE.
🤖 Generated with Claude Code
CI States
Latest PR Test (Base): ⏳ Run #31086934254
Latest PR Test (Extra): ❌ Run #31086936610