Skip to content

[diffusion] Add --offload-during-compile to fit max-autotune on tight-memory GPUs - #29862

Merged
mickqian merged 16 commits into
sgl-project:mainfrom
mickqian:mick/offload-during-compile
Jul 2, 2026
Merged

mickqian merged 16 commits into
sgl-project:mainfrom
mickqian:mick/offload-during-compile

Conversation

@mickqian

@mickqian mickqian commented Jul 1, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

max-autotune (the default DiT torch.compile mode) needs substantially more peak GPU memory during compilation than steady-state inference does, so a large DiT can OOM at compile-warmup time on a GPU that comfortably fits inference — leaving no way to serve it compiled on that hardware.

Concretely, FLUX.2-dev 1024², TP=2 OOMs during the max-autotune warmup on both 2×H200 (peak ~123 GB/GPU) and 2×H100, even though resident inference fits.

Modifications

Add --offload-during-compile (default on). For the compile warmup only, offload everything; after the warmup, serving runs with exactly the residency the user configured.

Server args are never mutated — server_args only gains the flag, and all mechanics live in DenoisingStage:

  • At stage init, the DiT is layerwise-configured (configure_layerwise_offload stages weights to pinned CPU and installs per-layer hooks). Layerwise hooks introduce graph breaks, so each layer compiles/autotunes with only itself resident — this is what caps the compile-time peak.
  • During warmup denoising forwards, resident non-DiT components (e.g. a resident VAE) are moved off-device and moved back right after the denoising returns. The bracket covers exactly the autotune window: the text encoder has already run, the VAE is back before decoding. Residency strategies and the server_args dump stay exactly as configured.
  • On the first real forward, the DiT is restored to resident (disable_offload) — a one-time cost absorbed before serving traffic.
  • Skipped when the DiT is already layerwise-offloaded (user-configured), when warmup is disabled (nothing to bracket), under cache-dit / FSDP, or for DenoisingStage subclasses that override forward (they would never run the restore).

Files: server_args.py (+7 lines, flag only), denoising.py (stage-local implementation).

Accuracy Tests

No model code or compute-graph change — this only controls where weights reside during the compile warmup. Equivalence spot-check (Z-Image-Turbo 1024², 9 steps, compile-on, 2×H100, back-to-back runs on the same GPUs): steady-state single-request latency 1.082 s with the flag off (resident compile) vs 1.078 s with the flag on. Happy to add an accuracy CI case.

Speed Tests and Profiling

FLUX.2-dev 1024², 50 steps, TP=2, max-autotune:

compile warmup steady-state
2×H200, without flag OOM (peak ~123 GB/GPU) —
2×H200, with flag fits (peak ~41 GB/GPU) ~12.8 s single-request
2×H100, without flag OOM —
2×H100, with flag fits (peak ~42 GB/GPU) 13.1 s single-request, serves resident

Small models are unaffected (Z-Image: 1.078 s vs 1.082 s baseline).

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #28584386409
Latest PR Test (Extra): ❌ Run #28584386229

…-memory GPUs

Offload the DiT layerwise during the torch.compile warmup, then restore it to resident once warmup finishes, so serving runs at full speed. Default on; only applies when compile is enabled and the DiT is not already offloaded by the user (and not under cache-dit / FSDP). max-autotune needs more peak memory during compilation than steady-state inference, so large DiTs (e.g. FLUX.2-dev on 2xH200) can OOM at compile time even when inference fits.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@github-actions github-actions Bot added the diffusion SGLang Diffusion label Jul 1, 2026
@mickqian

mickqian commented Jul 1, 2026

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 Jul 1, 2026
…ttr/local import

Offload every layerwise-offloadable component during the compile warmup (not just the DiT) and restore only the normally-resident ones afterward, keeping components the tuner already offloads (e.g. text_encoder) offloaded. Use a dataclass field + explicit init instead of getattr, and hoist the layerwise imports to module scope.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mickqian
mickqian marked this pull request as ready for review July 2, 2026 01:52
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

mickqian and others added 14 commits July 2, 2026 09:55
…fix guard

Two fixes found by verification on 2xH100:

- Layerwise-offloading every component corrupted non-layer-stacked modules
  (Z-Image VAE decode failed with a GroupNorm weight-shape error). Keep the
  DiT layerwise (required so each layer compiles with only itself resident)
  and fully cpu-offload the VAE / image encoder instead.
- The guard skipped the feature whenever --dit-layerwise-offload was set
  explicitly, including 'false' (= keep resident), which is exactly the case
  that needs compile-time offload (FLUX profiles set it and OOMed on 80 GB).
  Only skip when the DiT is actually configured to serve layerwise.

Equivalence check (Z-Image 1024, compile-on, 2xH100): resident 1.083s vs
offload-during-compile 1.092s single-request steady state; FLUX.2-dev TP=2
compiles at ~42 GB/GPU peak (resident compile OOMs) and serves resident.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
vae_cpu_offload / image_encoder_cpu_offload are persistent flags, so setting them changed steady-state serving behavior (per-request H2D transfers) long after the warmup. The compile-time peak belongs to the DiT autotune working set; offloading the small VAE / image encoder contributes nothing to fitting compilation (peak 41GB vs 42GB with them offloaded). Layerwise-offload only the DiT and restore it after warmup; leave every other component exactly as configured.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…, restore all after

During the compile warmup, cpu-offload every non-DiT component as well (the DiT stays layerwise) so the autotune gets the full GPU. On the first real forward, restore the user-configured residency for everything: the DiT and any component that was resident before goes back on-device (disable_offload for layerwise-configured modules, .to(device) otherwise), and the offload flags are reset so steady-state serving behavior is exactly what the user configured.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…egies on restore

Residency strategies are lru-cached per component and refresh_server_args only clears the cache on an identity change, so restoring the offload flags in place left the warmup-time VanillaD2HStrategy active and the VAE kept paying a per-request H2D (~18ms on Z-Image). Clear the strategy cache explicitly after restoring the flags. Also gate the feature on warmup being enabled: without a warmup the restore would run before the first real request compiles, making the feature a no-op.

Verified 2xH100 Z-Image compile-on: resident 1.082s vs offload-during-compile 1.078s steady-state (first request pays a one-time ~45ms restore).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Rework the feature so server args are never mutated: server_args only gains the offload_during_compile flag, and all mechanics live in DenoisingStage. The DiT is layerwise-configured at stage init (configure_layerwise_offload stages weights to pinned CPU and installs hooks); during warmup denoising forwards, resident non-DiT components are moved off-device and moved back right after, so the autotune peak gets the whole GPU while residency strategies and observability stay exactly as configured; the first real forward restores the DiT with disable_offload. This removes the restore dict, the private flags, and the strategy-cache invalidation the flag-mutation approach needed. Skipped for subclasses that override forward (they would never run the restore).

Verified on 2xH100: Z-Image compile-on steady state 1.078s vs 1.082s resident baseline; FLUX.2-dev TP=2 compiles at ~42GB/GPU peak (resident compile OOMs) and serves resident at 13.1s.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
… offload_during_compile

Unit tests construct DenoisingStage with a minimal SimpleNamespace server_args that sets enable_torch_compile=False but omits offload_during_compile, so evaluating offload_during_compile first raised AttributeError. Order the guard so the enable_torch_compile short-circuit (the primary gate) runs first.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mickqian
mickqian merged commit 119b765 into sgl-project:main Jul 2, 2026
109 of 122 checks passed
Chronostasys pushed a commit to MindLab-Research/sglang that referenced this pull request Aug 24, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant