Repository navigation
[diffusion] Fuse DiT FFN tanh-GELU into up-proj GEMM (cublasLt epilogue) behind quality=high (Qwen-Image 1024^2 denoise 12.36 -> 12.05 s on H200) - #33536
Merged
Conversation
…ue) behind quality=high (Qwen-Image 1024^2 denoise 12.36 -> 12.05 s on H200) Re-land the sgl-project#28166/sgl-project#28419/sgl-project#28167 GELU-epilogue fusion family inside the quality sampling-param contract that sgl-project#28708 asked for: the lossless default is byte-identical to main (cmp-verified for Qwen-Image and GLM-Image), and torch._addmm_activation(use_gelu=True) replaces the up-proj GEMM + separate tanh-GELU kernel only for quality=high batches. - new kernels/ops/diffusion/fused_linear_gelu.py: custom op + static/ runtime guards (unquantized, bias, no skip_bias_add, no multi-rank gather, half dtype) + mark/mount/unmount site protocol; mounting is all-or-nothing per transformer and fail-closed with reason logging. - QwenImageGELU / GlmImageGELU marked as fusion sites (fused branch is a no-op until mounted). - DenoisingStage mounts/unmounts at the batch boundary next to the cache-dit decision, keyed on batch.sampling_params.quality (which is in the dynamic-batch signature, so batches are quality-uniform). H200 results (denoise stage, warmed 9-request median): Qwen-Image 1024^2 12.356 -> 12.049 s (1.026x), GLM-Image 1024^2 6.491 -> 6.365 s (1.020x); e2e 12.875 -> 12.590 s / 40.3 -> 39.6 s. PSNR vs lossless 37.1-55.4 dB (bar >25), SSIM >= 0.984. fp32-truth: fused bf16 is slightly closer to the fp32 reference than the baseline bf16 path. FLUX.1 (no weight access) and Wan2.1 shared-MLP (model cannot load on main: pre-existing UMT5 loader regression) are follow-ups. Co-Authored-By: Claude Fable 5 <[email protected]>
BBuf
requested review from
AgainstEntropy,
HaiShaw,
mickqian,
ping1jing2,
yichiche and
yingluosanqian
as code owners
August 4, 2026 10:43
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
This was referenced Aug 5, 2026
Merged
Merged
saturn-acc
pushed a commit
to saturn-acc/sglang
that referenced
this pull request
Aug 16, 2026
…ue) behind quality=high (Qwen-Image 1024^2 denoise 12.36 -> 12.05 s on H200) (sgl-project#33536) Co-authored-by: Claude Fable 5 <[email protected]>
Atituiset
pushed a commit
to Atituiset/sglang
that referenced
this pull request
Sep 10, 2026
…ue) behind quality=high (Qwen-Image 1024^2 denoise 12.36 -> 12.05 s on H200) (sgl-project#33536) Co-authored-by: Claude Fable 5 <[email protected]>
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.
Branch:
pr-gelu-epilogue-quality-high, single commitd6473eae5bonmain(17d1908), standalone — no dependencies. 6 files, +397/−0.Motivation
Diffusion DiT FeedForwards compute
gelu(up_proj(x), approximate="tanh")as a standalone GEMM followed by a separate, bandwidth-bound GELU kernel over the[tokens, 4*dim]MLP intermediate.torch._addmm_activation(bias, x, w.t(), use_gelu=True)folds the bias-add and the GELU into the GEMM epilogue (cublasLt), removing the extra kernel launch and the intermediate HBM round-trip. cublasLt's GELU is the tanh-approximate GELU (max abs diff ~5e-6 vsF.gelu(approximate="tanh")in fp32), so for bf16 inference the fused path differs from the reference only at rounding-order level.This fusion family shipped before: #28166 (FLUX) merged, then #28708 reverted it, deleting the shared
fused_linear_act/helper; the sibling PRs #28419 (Qwen-Image, denoise 1.061x on B200, SSIM 0.984), #28167 (generic FeedForward, GLM ~1.03x) and #28366 (shared MLP) were closed unmerged in the same sweep. The revert reason (#28708, quoted verbatim):That reason is exactly the definition of the
qualitysampling param that has landed since (#33453 tier, already used by the FLUX.2 VAE fast path #33451 and the MiniMax-H3 cache-dit gating):"lossless"(default) = the exact reference path, bit-exact vs the CI golden outputs;"high"= opt into validated accelerated paths that are quality-guaranteed (PSNR > 25 dB bar) but not bit-exact. This PR re-lands the fusion inside that contract: the lossless default is byte-identical tomain(verified below), and the fused path runs only forquality="high"requests.Scope = the models we could actually measure end-to-end on this box: Qwen-Image (
QwenImageGELU) and GLM-Image (GlmImageGELU). Two sites are deliberately left as follow-ups rather than shipped unvalidated:flux.pyFluxGELU— the original [Diffusion] FLUX: fuse FeedForward GELU into up-proj GEMM (cublasLt epilogue) #28166 site): we have no FLUX.1 weight access to revalidate, and FLUX.2-klein does not go through that site (itsFlux2FeedForwardis SwiGLU with the MLP-in GEMM merged into the QKV projection, so a GELU epilogue does not apply).layers/mlp.pyMLPgelu_pytorch_tanhFFN, the [diffusion] Fuse tanh-GELU into shared MLP FFN up-proj GEMM epilogue #28366 site): Wan2.1-T2V-1.3B currently fails to load onmainitself — the UMT5 text-encoder loader raisesFollowing text encoder weights were not initialized from checkpoint: [... SelfAttention.qkv_proj ...]on the pristine 17d1908 tree (wan13b-main.log), so no lossless baseline, PSNR pair, or byte-exact check is obtainable. The wiring is a two-line follow-up once that pre-existing regression is fixed; the kernel-level evidence at the exact Wan shapes (1.17x microbench, fp32-equidistance) is already in this PR's tables.Modifications
1. Kernel helper — new
python/sglang/kernels/ops/diffusion/fused_linear_gelu.py(+182), restoring the #28708-deleted helper at its post-RFC-#29630 home (kernels side), plus the gating machinery:fused_linear_gelu_tanh(x, weight, bias): the cublasLt-epilogue GEMM, registered viaregister_custom_op(opaque under torch.compile, like the other diffusion ops) and asdiffusion.fused_linear_gelu_tanh(KernelBackend.TORCH) in the kernel registry.can_fuse_linear_gelu_static/can_fuse_linear_gelu: the [Diffusion] FLUX: fuse FeedForward GELU into up-proj GEMM (cublasLt epilogue) #28166 guards (unquantizedUnquantizedLinearMethod-or-plain linear, has-bias, noskip_bias_add, no multi-rankgather_output, half dtype, CUDA input). The weight-device check is runtime-only: underdit_cpu_offloadthe weights live on CPU at mount time.mark_fused_gelu_site(module, linear_attr)+mount_fused_linear_gelu/unmount_fused_linear_gelu: the site protocol. Sites start unmounted (reference path, bit-exact). Mounting is all-or-nothing per transformer and fail-closed: if any marked site fails the static guards (with the reason logged), every site stays on the reference path.2. Model sites (fused branch +
mark_fused_gelu_sitein__init__; the unmounted forward is the unmodified reference code path):qwen_image.pyQwenImageGELU(60 blocks x img+txt streams, dim 3072 -> 12288),glm_image.pyGlmImageGELU(30 blocks, dim 4096 -> 16384),All other GELU sites (shared
layers/mlp.pyMLP/FeedForwardusers, FLUX, embedders) are untouched and stay on the reference path.3. DenoisingStage gating —
pipelines_core/stages/denoising.py(+36):_maybe_toggle_fused_gelu(batch)runs at the same batch-boundary hook as the cache-dit decision (_maybe_enable_cache_dit_and_torch_compile), mounting iffbatch.sampling_params.quality == "high"(the MiniMax-H3 precedent:qualityparticipates in the dynamic-batch signature, so a worker batch is uniform inqualityand the process-wide transition is safe at the batch boundary). Models without marked sites are no-ops.Speedup
All numbers H200, bf16, this branch, GPUs otherwise idle. Three levels:
Kernel microbenchmark (real up-proj shapes, us per call, median of 200 after 30 warmup; ref =
F.linear+F.gelu(tanh), fused =torch._addmm_activation):The epilogue is nearly free (fused ~= linear-only + a few %), so the win is the eliminated GELU pass over the
[tokens, 4*dim]intermediate.Component denoise A/B (per-request
[DenoisingStage] finished in Xfrom the e2e runs below — same warmed process, 9 requests, drop first, 15% outlier filter, median of survivors):Cross-check: a
--quality highrun on the pre-fix branch state where the mount was rejected by the static guards showed denoise 12.332 s = lossless within noise, independently confirming the gate itself (attribute check per forward) adds no measurable overhead.End-to-end latency (#33451 protocol:
generateCLI, seed 42,--dit-layerwise-offload false --warmup --save-output; metric = client-side "Pixel data generated successfully in X seconds" wall-clock; one warmed process serving 9 sequential identical requests, first dropped, 15% outlier filter, median of >= 5 survivors):--quality high(GLM-Image e2e is dominated by its AR vision-language stage (~33 s of the request), so the denoise win is diluted e2e; the component table above is the authoritative measurement.)
Lossless-path timing on this branch is unregressed vs
main(same protocol,main= 17d1908): Qwen-Image 12.895 s / GLM-Image 42.18 (denoise 6.495) s medians vs branch-lossless 12.875 / 40.30 s.Accuracy
cmp) toorigin/mainoutputs for the same seed/prompt — Qwen-Image byte-identical (cmpclean), GLM-Image byte-identical (cmpclean). Golden-output CI is unaffected.quality="high": PSNR > 25 dB vs the lossless output, same seed, saved outputs):test/registered/kernels/ops/diffusion/test_fused_linear_gelu.py(7 cases: fused-vs-reference numeric bound bf16/fp16, fp32-truth equidistance, gate-offtorch.equalbit-exactness + unmount restore, all-or-nothing mount, static-guard rejections, runtime-guard fallback). 7 passed on H200.Sample images
Four lossless/high pairs saved on the box under
/tmp/prG_imgs/(to be attached here):qwen_cyberpunk_{lossless,high}.png,qwen_lake_{lossless,high}.png,glm_cyberpunk_{lossless,high}.png,glm_fisherman_{lossless,high}.png.Checklist
Sample outputs (lossless vs
--quality high)--quality high🤖 Generated with Claude Code
CI States
Latest PR Test (Base): ⏳ Run #30903491527
Latest PR Test (Extra): ❌ Run #30903492322