Skip to content

[diffusion] ERNIE-Image bit-exact residual-gate fast path (H200 1024^2 e2e 16.17 -> 15.75 s) - #33734

Merged
BBuf merged 3 commits into
sgl-project:mainfrom
BBuf:t8-ernie-fused-adaln
Aug 6, 2026
Merged

BBuf merged 3 commits into
sgl-project:mainfrom
BBuf:t8-ernie-fused-adaln

Conversation

@BBuf

@BBuf BBuf commented Aug 5, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

The ERNIE-Image shared-AdaLN block updates its residual stream twice per layer with an eager two-kernel pair:

x = residual + gate_msa * self.self_attention(...)
x = residual + gate_mlp * self.mlp(x)

The in-tree residual_gate_add_cuda kernel (already used by LTX-2 on its default path) collapses each pair into one launch and, for half dtypes, reproduces the eager pair's rounding bit-for-bit. ERNIE-Image runs bf16 and, with CFG enabled by default, executes 2 forwards/step x 36 blocks x 2 sites x 50 steps = 7,200 such pairs per 1024x1024 image, so the launch/round-trip saving is visible end to end while the output stays byte-identical.

This revives the lossless-safe half of closed PR #30170 (see "Relation to #30170" below).

Modifications

  • ernie_image.py: add _ernie_residual_gate_add (same shape/style as the LTX-2 helper) and use it at both residual-gate sites of ErnieImageSharedAdaLNBlock.forward. Guarded fast path with one-time fallback to the eager expression; restricted to fp16/bf16 where the kernel is bit-exact (for fp32 it would contract to an fma and stop being bit-exact, so fp32 stays on the eager pair).
  • Make the six AdaLN modulation tensors (B, 1, D) contiguous once per transformer forward (a bit-exact copy of tiny tensors) so the kernel guard accepts the broadcast gate layout.
  • No checkpoint parameter names changed. No quality gate needed: the default lossless path is byte-identical (proof below).

Speed Tests (H200)

baidu/ERNIE-Image-Turbo, 1024x1024, 50 steps, bf16, eager (torch.compile is not enabled by default for this pipeline), fixed prompt and seed=42, use_pe disabled to remove prompt-enhancement nondeterminism. Client wall-clock, 10 consecutive generations in one process, first dropped, median of 9. Baseline was run twice to bound run-to-run noise.

Implementation E2E wall (ms) DenoisingStage (ms)
main (run 1) 16229.0 15431.7
main (run 2) 16118.3 15424.5
this PR 15752.7 15115.0
delta vs pooled main -2.6% -2.0%

The DenoisingStage delta is fully explained by the kernel microbenchmark: 2 CFG forwards/step x 36 blocks x 2 sites x 43 us saved x 50 steps ~ 310 ms ~ the measured 313 ms.

Kernel microbenchmark at the real shape (1, 4216, 4096) bf16 (4096 image tokens + text tokens, hidden 4096), 200 iters after warmup:

Op eager residual + gate * update residual_gate_add_cuda
per site 71.0 us 28.0 us

Accuracy Tests

Bit-exactness is proven at three levels (H200, bf16):

Check Result
Kernel vs eager pair, torch.equal, real shapes, bf16/fp16 True, max_abs_diff = 0
Full 1024x1024 image, same prompt/seed, this PR vs main md5 identical (324a06b56c4616aeaebd0d8d5017de9e on both)
Determinism: main run 1 vs run 2, and every iteration within each arm md5 identical

Unit test: test/registered/kernels/ops/diffusion/test_ernie_residual_gate_add.py (bit-exactness across dtypes at real ERNIE shapes, broadcast and full-shape gate layouts; registered for base-b-kernel-unit).

3 passed

pre-commit run --files ...: all hooks pass.

Relation to closed PR #30170

#30170 wired three fused kernels into this block and was closed for quality regressions (SSIM 0.72-0.80). This PR re-validated all three with a per-kernel bit-exactness protocol:

Kernel bf16 bit-exact vs eager chain? Disposition
residual_gate_add_cuda yes (torch.equal) shipped here, lossless default
fused_norm_scale_shift (CuTe-DSL) no (max_abs ~ 0.06-0.125) dropped
fused_scale_residual_norm_scale_shift (CuTe-DSL) no (max_abs ~ 0.25) dropped

The two non-bit-exact norm+modulation fusions were additionally re-tested behind a quality="high" mount (per the #33536 DenoisingStage mount/unmount pattern) with a clean fixed-prompt protocol: they add a further -3.8 pp of denoise time but produce same-seed full-image PSNR 18.83 dB / SSIM 0.797 vs lossless, missing the quality="high" bar (PSNR > 25). The 50-step trajectory is genuinely perturbed by the fp32 single-pass modulation, confirming (with a clean baseline) why #30170 was rejected, so they are excluded rather than gated.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #31064821373
Latest PR Test (Extra): ⏳ Run #31064821237

…2 e2e 16.17 -> 15.75 s, denoise 15.43 -> 15.12 s)

Wire the existing residual_gate_add_cuda kernel into both residual-gate
sites of the ERNIE-Image shared-AdaLN block. The fused kernel is bit-exact
vs the eager residual + gate * update pair for half dtypes (torch.equal;
full 1024x1024 generated image is md5-identical to main), so it runs on
the lossless default path with no quality gate. The AdaLN modulation
params are made contiguous once per forward (bit-exact copy) so the
kernel guard accepts the broadcast gate layout.

Revives the lossless-safe half of closed PR sgl-project#30170. The non-bit-exact
CuTe-DSL norm+scale/shift fusions from that PR were re-validated behind a
quality=high mount and rejected again: same-seed full-image PSNR 18.83 dB
/ SSIM 0.797 vs lossless misses the quality=high bar (PSNR > 25), so they
are not included.

H200, baidu/ERNIE-Image-Turbo, 1024x1024, 50 steps, fixed prompt+seed,
median of 9 (first of 10 dropped): e2e wall 16.17 -> 15.75 s (-2.6%),
DenoisingStage 15.43 -> 15.12 s (-2.0%). Kernel microbench per site
(1,4216,4096) bf16: 71.0 -> 28.0 us.

Co-Authored-By: Claude Fable 5 <[email protected]>
@BBuf BBuf added the run-ci CI: run the baseline test suite on this PR label Aug 5, 2026
@github-actions github-actions Bot added the diffusion SGLang Diffusion label Aug 5, 2026
@BBuf BBuf added the run-ci-extra CI: also run the extra suite (requires run-ci) label Aug 6, 2026
@BBuf
BBuf merged commit b6876fc into sgl-project:main Aug 6, 2026
146 of 170 checks passed
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diffusion SGLang Diffusion run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant