Skip to content

[Diffusion] Fuse FLUX.2 gated residual normalization on Blackwell - #37112

Merged
BBuf merged 9 commits into
sgl-project:mainfrom
BBuf:codex/flux2-gated-resnorm-b300
Sep 1, 2026
Merged

BBuf merged 9 commits into
sgl-project:mainfrom
BBuf:codex/flux2-gated-resnorm-b300

Conversation

@BBuf

@BBuf BBuf commented Aug 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This PR adds a Blackwell JIT fast path for FLUX.2's gated residual update followed by LayerNorm and adaLN scale/shift. It is the gated-residual-normalization optimization described in Agentic Kernels in Production.

The production BF16 path previously launched residual_gate_add and LayerNorm/modulation separately. The new path carries an eligible FFN residual update to the next block and materializes both results with one model-specific CUDA kernel:

flowchart LR
    R[residual] --> F[fused CUDA JIT kernel]
    U[FFN or attention update] --> F
    G[gate] --> F
    M[next LayerNorm scale and shift] --> F
    F --> RO[updated BF16 residual]
    F --> NO[normalized and modulated BF16 input]
    X[unsupported shape, dtype, device, or compile mode] --> E[existing eager kernels]
Loading

In a profiled denoise step, 80 gated-residual and 80 norm/modulate calls become 77 fused calls plus 3+3 boundary calls. This removes 77 GPU launches per step.

Implementation details

  • Adds a dedicated D=6144, 128-thread CUDA JIT kernel for FLUX.2 instead of widening the generic norm/custom-op surface.
  • Preserves eager numerics: BF16-rounds the gate product before the residual add, reproduces PyTorch's four-warp Welford reduction tree, and preserves BF16 rounding through normalization and modulation.
  • Re-reads the updated residual in the output pass to reduce register pressure and retain occupancy.
  • Defers eligible block-final residual updates only until the next LayerNorm. Pending values are always materialized at double-stream/single-stream and model-output boundaries.
  • Keeps the original nn.LayerNorm modules and checkpoint/state-dict layout unchanged.
  • Limits the fast path to Blackwell+, BF16, batch 1, hidden size 6144, non-empty dense contiguous tensors, and 32-byte alignment. FP16, batch >1, other architectures, torch.compile, and unsupported layouts use the existing path.

End-to-end benchmark

Hardware/software: 1x NVIDIA GB300 288 GB (SM 10.3), driver 580.105.08, PyTorch 2.13.0+cu130. Model: FLUX.2-dev ModelOpt NVFP4. Native SGLang backend; transformer, text encoder, and VAE resident.

Workload: 1024x1024, 50 denoise steps, guidance scale 4.0, seed 20260830, lossless PNG, one request warmup per fresh process. Five main and five PR runs used balanced order main, PR, PR, main, PR, main, main, PR, main, PR.

Metric main, mean ± stdev PR, mean ± stdev Change
Denoise / step 136.191 ± 0.094 ms 135.133 ± 0.044 ms -0.7768%
Warmed end-to-end 6937.505 ± 5.735 ms 6888.294 ± 4.031 ms -0.7093%

Kernel microbenchmark

Median CUDA-event time after 25 warmups, 200 samples. The baseline is residual_gate_add followed by the production LayerNorm/modulation helper.

Shape baseline fused Speedup Output / residual max abs diff
[1, 512, 6144] 158.656 us 73.744 us 2.151x 0 / 0
[1, 4096, 6144] 146.368 us 98.224 us 1.490x 0 / 0
[1, 4608, 6144] 146.576 us 103.136 us 1.421x 0 / 0

Profile evidence

Two profiled denoise steps per side at the formal 1024x1024 shape:

Metric / step main PR Change
Total GPU kernel launches 1121 1044 -77 (-6.87%)
Standalone gated residual 80 3 -77
Standalone norm/modulate 80 3 -77
Fused gated resnorm 0 77 +77
Relevant kernel time 4056.527 us 3170.237 us -21.85%

Accuracy and generated-image comparison

The same formal runs were used for performance and quality measurement. All five images within each side were deterministic, and main and PR produced the same PNG bytes.

Metric Result
main SHA256 b00811e8777a7002f9e4b05400c032aa4c9932450127e2898d875fe2231d1d82
PR SHA256 b00811e8777a7002f9e4b05400c032aa4c9932450127e2898d875fe2231d1d82
Pixel exact Yes
Max / mean absolute pixel diff 0 / 0
Pixel cosine similarity 1.0
SSIM 1.0
PSNR infinity
LPIPS (AlexNet) 0.0

main vs PR generated image

Prompt: A cinematic technical poster of a red panda GPU engineer in a dark research laboratory, inspecting a luminous Blackwell accelerator board, with a notebook clearly labeled SGLANG RESNORM, realistic fur, intricate electronics, dramatic rim lighting

Validation

  • test/registered/kernels/ops/diffusion/test_model_fast_paths.py: 44 passed
  • Production-shape CUDA test checks bit-exact normalized output and updated residual
  • Fallback tests cover FP16, BF16 batch 2, and compile mode
  • Full native FLUX.2 generation with --enable-torch-compile: passed and produced an image
  • Full native FLUX.2 generation with --dit-layerwise-offload true: passed and produced an image
  • ruff check, ruff format --check, clang-format --dry-run --Werror, py_compile, and git diff --check: passed

CI States

Latest PR Test (Base): ⏳ Run #33423597740
Latest PR Test (Extra): ✅ Run #33423597299
Latest PR Test (AMD ROCm 7.2): ❌ Run #33423597670

@BBuf

BBuf commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-run-ci-label extra

@github-actions github-actions Bot added run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) labels Aug 30, 2026
@BBuf

BBuf commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@BBuf

BBuf commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@BBuf

BBuf commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@BBuf

BBuf commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@BBuf
BBuf merged commit 079afaf into sgl-project:main Sep 1, 2026
170 of 213 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diffusion SGLang Diffusion jit-kernel 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