Skip to content

[Perf] diffusion: Cache the fused linear-GELU verdict per linear - #44643

Merged
mickqian merged 2 commits into
mainfrom
mick/linear-gelu-static-once
Oct 18, 2026
Merged

mickqian merged 2 commits into
mainfrom
mick/linear-gelu-static-once

Conversation

@mickqian

@mickqian mickqian commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

can_use_linear_gelu runs at every quality-gated GELU up-projection: FLUX, Qwen-Image, LongCat, LTX-2, HunyuanVideo, GLM-Image and Wan. On every call it re-ran the input-independent guards:

  • the SM120 architecture check;
  • the quantization, skip_bias_add and gather_output checks;
  • the bias, weight rank and dtype checks.

That is 2.67 µs per call on an x86 host, mostly getattr misses on nn.Module. In a py-spy profile of FLUX.1 TP2 it was 1.3% of the denoise host time.

Modifications

In fused_linear_gelu_site.py:

  • The input-independent verdict is kept per linear in a WeakKeyDictionary.
  • mount_fused_linear_gelu and unmount_fused_linear_gelu clear it, so a re-mount re-reads the linear.
  • The per-input checks still run on every call: device, half dtype and x.dtype == weight.dtype.

No model call site changes. No memory is involved.

Accuracy Tests

On H200:

  • test_sites.py: 25 passed, including the new test_linear_gelu_verdict_is_reused_until_remount. It checks that three calls read the linear once and that a re-mount reads it again.
  • test_wan_gelu_mlp.py: passed.
  • test_model_fast_paths.py: 114 passed.

Speed Tests and Profiling

Base c15edf314d, torch 2.14.1, H200 (Xeon 8558P).

µs per call
can_use_linear_gelu before 2.67
can_use_linear_gelu after 0.75

End to end, FLUX.1-dev TP2 eager on 2xH200, client wall-clock median. Both paths were built into one server and chosen per request, rotating in the same process, with #44251 applied to both. An extra base arm measures in-process noise.

vs base faster than base in in-process A/A
FLUX.1-dev 512² −0.92% 11/12 +0.33% (3/12)
FLUX.1-dev 1024² (GPU-bound) +0.1% 1/5 +0.0%

Output images are unchanged: the same path is chosen.

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #39018724667
Latest PR Test (Extra): ❌ Run #39018724597
Latest PR Test (AMD ROCm 10): ❌ Run #39018724683

can_use_linear_gelu re-ran the input-independent guards (architecture,
quantization, bias, weight layout) on every call, ~2.7 us per call. The
verdict is now kept per linear and cleared when sites are mounted or
unmounted; the input checks still run every call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mickqian

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Oct 16, 2026
@mickqian
mickqian merged commit 949e1a2 into main Oct 18, 2026
209 of 228 checks passed
@mickqian
mickqian deleted the mick/linear-gelu-static-once branch October 18, 2026 01:55
detain pushed a commit to detain/sglang that referenced this pull request Oct 19, 2026
…-project#44643)

Co-authored-by: Mick Qian <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 949e1a2)
Fork note (SM120 / skynet2): the arch carve-out inside the cached verdict returns
False for arch.major*10+arch.minor >= 120, so on our 4x RTX PRO 6000 Blackwell
Workstation cards the fused cublasLt GELU epilogue stays off. What still applies
for us is exactly the caching win: the per-call can_use_linear_gelu() short-circuits
after the cheap x.dtype/weight checks instead of re-running can_use_linear_gelu_static()
over the linear's module tree on every DiT block call. No behavior change vs today's
eager path; verdict is False either way.
(cherry picked from commit 5866482a023a7ca70479f1394ede3c39bda9329f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant