Repository navigation
[diffusion] Qwen-Image 2.1 VAE decoder fast paths: bit-exact norm/bias fusions by default, channels_last decode under quality=extra-high - #41340
Conversation
…m+SiLU tail and folded conv padding Install two bit-exact fast paths on the Qwen-Image 2.1 VAE decoder: - The channel RMSNorm keeps aten's fp32 L2 norm and runs the normalize / scale / gamma / SiLU tail in one CUDA launch where the decoder applies RMS_norm -> SiLU (residual blocks and the output head), replacing the Triton finish kernel and the separate SiLU pass. - The symmetric F.pad in front of every causal conv is folded into the convolution's own padding; because cuDNN may choose another algorithm for the new descriptor, each conv compares the folded result per input shape on first sight and keeps the explicit pad where it differs. Parameter names are unchanged, both paths verify themselves and fall back on mismatch, and the spatial-parallel decoder is left alone.
… VAE Under quality=extra-high or high the decoder switches to channels_last so the cuDNN NHWC conv kernels stop wrapping every 3x3 conv in NCHW/NHWC transposes, and the channel RMSNorm + SiLU runs as one CUDA kernel over the contiguous channels of each pixel: a lane group per pixel with 16-byte loads, sized from C by the Python wrapper so the narrow last stage (C=144 at 1024x1024) stays near bandwidth instead of latency-bound. The kernel has its own reduction order, so it is not bit-exact and never runs on the lossless path; it also takes an optional per-channel conv bias, used by the bias fusion later in the series. The nearest 2x upsample re-labels the merged-batch view with canonical NHWC strides and uses the bit-exact Triton NHWC gather. The gate is decode-scoped through VaeFastPathGate and flipping it back restores the NCHW parameter layout and kernels exactly.
…2d under quality=extra-high Each decoder upsampler runs nearest-2x + conv3x3(p1); under the quality gate it becomes one ConvTranspose2d(k4, s2, p1) whose kernel is the fp32 sum of the conv taps each output phase touches (2.25x fewer MACs, no 4x intermediate). The fold helper moves out of the FLUX.2 module into conv_fold.py so both VAEs share it. Also points the block-fusion benchmark at the decoder's production norm shapes (decoder_base_dim 144).
…m kernel and the residual add aten adds a conv's bias as a separate pass over the output. In each decoder residual block the second conv now runs without its bias and a new bit-exact JIT kernel, bias_residual_add, applies bias and residual in one pass with aten's per-op bf16 rounding (default on, first-sight verified). Under quality=extra-high the first conv hands its bias to the channels_last RMSNorm+SiLU kernel, so that pass disappears as well. Also points the VAE benchmark cases at the decoder's real per-stage shapes.
…3D shortcut add dup_up3d_add takes an optional per-channel bias, added with aten's rounding before the residual add, so the up block's upsampler conv can run without its bias pass; the fused add is first-sight verified against the eager chain and default on.
…ting Adds a marker benchmark for the decoder's channel RMSNorm + SiLU sites on the production shapes, the kernel README rows and the fast-paths inventory entry, and applies pre-commit formatting to the series (no functional change).
|
/tag-and-rerun-ci |
mickqian
left a comment
There was a problem hiding this comment.
CI follow-up for exact head 16cadcebe65ba42b66b92b3de38ff6bb6afaf417 (Base run 36233408214, attempt 2): the broad CPU/NVIDIA failures share a deterministic test-registration blocker, not evidence of a VAE runtime regression.
The collection error is test/registered/kernels/ops/attention/test_deep_select.py: missing if __name__ == "__main__": entry. For example: https://github.com/sgl-project/sglang/actions/runs/36233408214/job/108384018086 . I inspected all 33 failed-job logs: 29 have this exception, one is fail-fast, coverage lacks reports, and the remaining two are aggregate failures.
This unrelated file is absent from this PR's changed-file list. Main now contains the specific fix in a947b56 (#41341), adding import sys and the pytest entry point. Please merge the current target branch (main) into this branch with a normal merge commit, then validate CI on the new head. Rerunning the old tested checkout will not repair its missing entry point. No thresholds or assertions need relaxing. I have not pushed to your fork (push permission is false), rerun these jobs, or claimed runtime tests passed.
|
Merged the current |
|
CI follow-up for head |
… VAE test The padding fold is kept only where cuDNN's algorithm for the folded descriptor reproduces the padded conv bit for bit; on H100 it does not and the gate falls back as designed while the decode stays bit-identical. The test now requires that outcome (a decision per conv plus an exact decode) instead of a verified fold everywhere, and still requires the exact-by- construction norm kernels to have engaged.
|
Second non-gate failure on head |
Since sgl-project#41272 the Qwen-Image lossless added-QKV split applies only when to_added_qkv is a MergedColumnParallelLinear (LoRA wrappers are called as they are), so the plain nn.Module fake in test_qwen_added_qkv_lossless_uses_three_reference_gemms now takes the packed path and the test fails on main and on every PR's merge ref. The fake subclasses MergedColumnParallelLinear and skips its __init__, which needs no parallel state here. (cherry picked from commit 6b44c61)
A projection that is not a MergedColumnParallelLinear (a LoRA wrapper) is called as it is on the lossless path since sgl-project#41272; the packed fake exercises the genuine split, the wrapped fake pins the call-through. (cherry picked from commit 5baf93b)
|
channels_last decode could be moved to |
|
/rerun-failed-ci |
|
Pushed head
This is the first run in which the H100 job can execute the folded-conv fix, so |
|
H100 result for head |
|
Run 36373246079 status: the only non-cascade failure so far is |
mickqian
left a comment
There was a problem hiding this comment.
Exact-head CI follow-up for 47db8aee32830fe92c4d4c008055a5abfbfb7c03, Base run 36373246079 attempt 2:
The previous QKV fixture blocker is now green: test_qwen_added_qkv_lossless_uses_three_reference_gemms and the separate wrapped-linear test both executed and passed in job 108774327635. The named Qwen21 VAE lossless, mismatch-fallback and extra-high/restoration tests in test_model_fast_paths.py also executed and passed. All six JIT jobs passed; this is not a claim that the entire PR is green.
The remaining root failure is dual-H100 partition 2, job 108774329591, runner h100-novita8-gpu-67. After the initial pass and three failed-item retries, two cases remain:
| Case | Final actual / limit | Final AV denoise / refinement / decode |
|---|---|---|
ltx_2_two_stage_t2v |
median step 131.3178 / 130.9680 ms | 5299.73 / 636.34 / 199.81 ms |
ltx_2_3_two_stage_ti2v_2gpus |
average step 301.5049 / 300.1920 ms | 9220.71 / 733.35 / 291.32 ms |
The next retry was not started: 68.1 s remained versus an estimated 213.5 s required. This is not six-retry exhaustion. The initially failing FSDP and one-stage TI2V cases recovered. Nine other failed Base leaves explicitly stopped at fail-fast; wait-for-base-b and finish are aggregates. Lint, B200, the other diffusion partitions, unit, component accuracy and BCG passed.
The stage timing identifies where time was spent, but does not establish a CPU/GPU/communication cause or prove these failures unrelated. Matching H100 development capacity is currently unavailable in the free rx inventory. No blind rerun, threshold/assertion relaxation or fork push was performed.
Logs:
https://github.com/sgl-project/sglang/actions/runs/36373246079/job/108774327635
https://github.com/sgl-project/sglang/actions/runs/36373246079/job/108774329591
|
Thanks for confirming the H100 result. The two LTX-2 cases left in partition 2 missed their limits by 0.3% and 0.4% (median step 131.32 vs 130.97 ms, average step 301.50 vs 300.19 ms) with the in-job retries cut off by the time budget rather than exhausted, so I am rerunning the failed job once; the folded-conv fix and the kernel tests do not depend on it. |
|
/rerun-failed-ci |
|
Attempt 3 of run 36373246079 put partition 2 on a fourth host ( |
…-decoder-fast-paths # Conflicts: # python/sglang/kernels/ops/diffusion/__init__.py # python/sglang/multimodal_gen/.claude/skills/sglang-diffusion-benchmark-profile/existing-fast-paths.md # test/registered/kernels/ops/diffusion/test_model_fast_paths.py
|
Pushed head |
|
Run 36432308415 (head |
|
/rerun-failed-ci |
|
Attempt 2 of run 36432308415 (head
Nothing that fails here exercises the Qwen-Image 2.1 VAE; the cases that do ( |
…-project#41340) Squash of 16 commits a99db03..47db8ae (net diff from PR base f385390 via git apply -3). Adds fused norm+SiLU tail, folded conv padding, bias-fused residual add, DupUp3D->ConvTranspose2d folding and quality-gated channels_last decode for the Qwen-Image 2.1 VAE; new JIT CUDA kernels bias_residual_add / channel_rmsnorm_finish_silu / channel_rmsnorm_silu_nhwc (+ .cuh headers) exported from kernels.ops.diffusion, conv_fold.py helper, qwen_image21_vae_cuda_opt.py, flux2_vae_cuda_opt.py + wan_causal_cache_triton.py updates, cuda.py hooks. Conflict/resolution notes: - Skill doc path redirected .agents/skills -> .claude/skills (our tree predates the upstream .agents split; only directory differs). - kernels README table: kept our conv_bias-annotated wan_rmsnorm_silu + KDA residual_gate_add rows; inserted theirs' three new kernel rows. Tests: test_norm.py 254 passed; test_model_fast_paths.py+test_layout.py 181 passed / 51 skipped (JIT kernels compile on SM120).
Independent of the attention PR; both add entries to
kernels/ops/diffusion/__init__.py, the kernel README and the fast-paths inventory (adjacent lines, trivial to rebase; the inventory section is numbered 18 assuming the attention PR's 17 lands first).Motivation
The Qwen-Image 2.1 VAE decode of a 1024x1024 image takes 186 ms on one SM120 GPU. Its decoder (dims 1152/1152/1152/576/288/144, five up blocks that run three residual blocks at their own resolution and then upsample) spends that time on cuDNN convs, but also on work that is pure data movement: cuDNN's NCHW/NHWC transposes around every conv (25 ms), aten's channel RMSNorm chain (fp32 copy + reduce + four pointwise passes + SiLU), a separate bias-add pass after every conv (46 launches, 10 ms) and nearest-2x upsampling that quadruples the pixels before a 3x3 conv.
This PR removes that overhead in two tiers, following the existing VAE fast-path conventions (
VaeFastPathGatefor quality-gated paths,BitExactFusionGatefirst-sight verification for default-on paths):quality=lossless): bit-exact fusions only; the output PNG is unchanged;quality=extra-high/high: the decoder switches to channels_last and uses an NHWC norm kernel with its own reduction order plus fp32-folded upsampler weights; documented as close, not bit-exact.Modifications
One commit per optimization (
runtime/models/vaes/qwen_image21_vae_cuda_opt.py, mounted fromplatforms/cuda.py::optimize_vae; parameter names are preserved so checkpoints load unchanged):a99db03979Lossless fast paths (default on):channel_rmsnorm_finish_siluJIT kernel keeps aten's fp32norm(dim=1)reduction and fuses the pointwise finish (max(norm, 1e-12), divide, scale, gamma,+0.0) with SiLU;FoldedPadConv2dfolds the symmetricF.padof every causal conv into the conv's own padding, verified per (dtype, shape, stride) signature because cuDNN may pick another algorithm; the fold is only attempted above 2^18 output elements, where a bit-exact first-sight match is decisive (two engines with different accumulation orders agree on all but ~1e-4 of the elements, so on a few thousand elements they can match by chance, as the H100 runner showed at a 4x4 test latent).a99052507bchannels_last decode under the quality gate: the decoder flips to channels_last (parameter memory only; flipping back restores the NCHW kernels exactly),channel_rmsnorm_silu_nhwcreduces each pixel's channels in a lane group (1-32 lanes per pixel with 16-byte loads, chosen from C by the Python wrapper so the C=144 last stage stays near bandwidth), and the nearest 2x upsample re-labels the merged-batch view with canonical NHWC strides and uses the bit-exact Triton NHWC gather.119ab3c768Upsampler fold (quality gate): eachnearest-2x + conv3x3(p1)becomes oneConvTranspose2d(k4, s2, p1)whose kernel is the fp32 sum of the taps each output phase touches (2.25x fewer MACs, no 4x intermediate). The fold helper moves out of the FLUX.2 module intovaes/conv_fold.pyso both VAEs share it.53da9a360cConv bias fusions: aten adds a conv's bias as a separate pass over the output. Each residual block's second conv now runs without its bias and the new bit-exactbias_residual_addJIT kernel applies bias and residual in one pass with aten's per-op bf16 rounding (default on, first-sight verified; channels_last-dense or NCHW-contiguous bf16). Under the quality gate the first conv hands its bias to the NHWC norm kernel, so that pass disappears too.8d1e569f15Upsampler bias into the shortcut add: the shared Wan Triton kerneldup_up3d_addtakes an optional per-channel bias (aten rounding), so the upsampler conv runs without its bias pass (default on, first-sight verified).16cadcebe6kernel README rows, fast-paths inventory entry, pre-commit formatting (a marker benchmark added here was dropped again later in the series).Fallbacks: spatial-parallel decode, ROCm,
torch.compile, unsupported layouts and any first-sight mismatch keep the original module chain.Accuracy Tests
channel_rmsnorm_finish_silusection oftest_norm.py(14), thebias_residual_addsection oftest_norm.py(25,torch.equalin both layouts), thedup_up3d_addbias case intest_layout.py, and the Qwen-Image 2.1 VAE section oftest_model_fast_paths.py(decode of an optimized tiny decoder with the real latent channel count equals the original bit for bit on a 64x64 latent, where every fold site's first-sight comparison is decisive, and again on a second latent while every gate reports verified; a mismatching kernel disables its gate and the decode stays bit-identical;state_dictkeys unchanged). End to end the same-seed 1024x1024 PNG keepsmain's sha256 (2781b27ac4b17cb1...) in every lossless launch.extra-highpath: thechannel_rmsnorm_silu_nhwcsection oftest_norm.py(within two bf16 ulps of the eager chain, >95% of elements bit-identical, all lane-group specializations, fused bias) and the extra-high case in that section (channels_last decode close to the reference, gate off restores bit-identity). End to end, the extra-high image against the lossless image (RGBA, 1024x1024, seed 42; deterministic: the same sha256 on two GPUs and seven launches):Speed Tests and Profiling
Hardware and workload as in the attention PR: one cc 12.0 (SM120) GPU, 73 GB, 350 W cap, torch 2.13.0+cu132, cuDNN 9.20;
sglang serve --performance-mode speed --attention-backend torch_sdpa, 1024x1024, 40 steps, bf16. Decode times are theDecodingStageunderSGLANG_DIFFUSION_SYNC_STAGE_PROFILING=1, mean of 3 requests after a warmup request.VAE decode, whole series on one GPU, interleaved with
main:mainmain2781b27a...mainPer commit (single launches on a second GPU of the same type; its
maindecode is 184.4 ms):Smoke of this exact branch (rebased on
main@3ed56a341b, one launch each on a second GPU of the same type, stage-profiling cohort):maindecode 184.4 ms; this PR 158.0 ms lossless (PNG sha256 identical tomain) and 98.8 ms extra-high (PSNR 59.35 dB vs lossless, same sha256 as on the other GPU).Kernel level (
marker.do_benchon the decoder's production shapes, median us;cuda= lossless finish kernel with aten's reduction,cuda_nhwc= quality-gated kernel):Other measured pieces: the four upsampler
nearest-2x + conv3x3sites go from 9754 us to 5529 us as ConvTranspose2d; the separate conv-bias adds go from 46 launches / 10.3 ms to 9 launches / 1.2 ms under extra-high (26 launches / 4.6 ms under lossless, where the first conv's bias cannot enter the aten norm reduction); cuDNN's NCHW/NHWC transposes (119 launches, 24.7 ms) disappear under extra-high. What remains of the extra-high decode (~100 ms) is ~80 ms of cuDNN conv, whichcudnn.benchmarkdoes not improve on this GPU.Checklist
CI States
Latest PR Test (Base): ✅ Run #38027089458
Latest PR Test (Extra): ❌ Run #38027089365
Latest PR Test (AMD ROCm 10): ❌ Run #38027089433