Repository navigation
[diffusion] Add --offload-during-compile to fit max-autotune on tight-memory GPUs - #29862
Merged
Merged
Conversation
…-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]>
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
Collaborator
Author
|
/tag-and-rerun-ci |
…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
marked this pull request as ready for review
July 2, 2026 01:52
mickqian
requested review from
AgainstEntropy,
HaiShaw,
ping1jing2,
yhyang201 and
yichiche
as code owners
July 2, 2026 01:52
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
…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]>
This was referenced Jul 2, 2026
Chronostasys
pushed a commit
to MindLab-Research/sglang
that referenced
this pull request
Aug 24, 2026
… tight-memory GPUs (sgl-project#29862)
4 of 5 tasks
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.
Motivation
max-autotune(the default DiTtorch.compilemode) 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_argsonly gains the flag, and all mechanics live inDenoisingStage:configure_layerwise_offloadstages 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.server_argsdump stay exactly as configured.disable_offload) — a one-time cost absorbed before serving traffic.DenoisingStagesubclasses that overrideforward(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:
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