Repository navigation
Studio diffusion: keep whole-module offload for a bf16 denoiser past the streamed floor - #15129
Merged
Merged
Conversation
…the streamed floor Below the streamed group tiers' floor, whole-module offload is the only tier and it holds the whole denoiser on the card while it runs. A few hundred MiB more free VRAM switched to a group plan that streams the denoiser every step. For a plain bf16 denoiser that the streamed plan does not pin whole, and that already fit whole below the floor, keep whole-module offload. FLUX.2-klein-4B bf16 on an exclusive B200, 4 steps at 1024^2, 3 interleaved repeats: 12.00 GiB free, whole-module (unchanged): 0.345 s per image, 0.028 s per step 12.25 GiB free, streamed (before): 0.595 s per image, 0.170 s per step 12.25 GiB free, whole-module (after): 0.336 s per image, 0.029 s per step, peak 7.83 GiB 12.25 GiB free, 5.5 of 7.4 GB pinned: 0.727 s per image vs 0.373 s whole-module fp16 (the T4 dtype), packed, fp8-stored and torchao denoisers keep the ladder: on a Kaggle T4 stock diffusers whole-module offload ran slower than streaming. Plans below the first inversion are unchanged across the placement sweep; the new check runs after refine_plan_from_loaded_weights.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Oct 24, 2026
… from its step count (unslothai#15163) ## Summary unslothai#15129 keeps a bf16 denoiser that sits just above the streamed floor in whole-module offload instead of group streaming. Which of the two is faster depends on the request's step count, which a load cannot know: - On B200 and A100, whole-module offload wins at every step count. On A100-40GB, FLUX.2-klein-4B took 2.336 s against 3.429 s per image. - On a G4 (RTX PRO 6000), PCIe and compute are close. Streaming wins at 2 and 4 steps, and whole-module wins at 8. At FLUX.2-klein's default 4 steps, unslothai#15129 is 2.5% slower than streaming there. This PR makes the choice per request. ## Changes - **At load:** - Each mode is priced with a 0.1 s probe of this card's pinned host-to-device bandwidth and bf16 matmul rate, at the family's default step count: - whole-module = upload + steps x compute; - streamed = steps x max(compute, transfer). - Where whole-module wins (B200, A100), main's plan is placed unchanged. - Elsewhere the streamed plan is placed, and the probe readings are kept on the pipe. - **Per request:** - Each `generate()` is priced with its own steps, canvas, batch, CFG and condition images. - When that prices whole, every streamed offload group of the denoiser is made resident after the prompt encode, then streamed again when the decode starts. That is one upload per call, through the streamed tier's own hooks: no hook swap, and the pinned host copies are reused. - The blocks compile below the offload hooks, so compiled code never sees the move. - **When it keeps the load's plan:** - a background compile is pending; - a CUDA graph over the denoiser is still live or undecided; - a step cache is on; - the card does not have room right now for the weights plus this machine's learned pre-decode peak for the request's size. - **Pre-decode peak lookup:** after the deferred compile, the attention label and the speed tier in the peak key both change, and graph-captured renders record no peak. So the lookup falls back to the key the load's eager renders recorded. - **Kill switches:** - `UNSLOTH_DIFFUSION_WHOLE_MODULE_PER_REQUEST=0` turns off the per-request part. - `UNSLOTH_DIFFUSION_WHOLE_MODULE_OFFLOAD=1` restores main exactly. - `UNSLOTH_DIFFUSION_WHOLE_MODULE_OFFLOAD=0` never uses whole-module offload. ## Results Colab G4 (RTX PRO 6000), real FLUX.2-klein-4B, 12.25 GiB cap. - **Tree check:** the notebook verified the installed `studio/backend` tree hash against the commit. - **Arms:** - `main`; - `head_model`: head with `UNSLOTH_DIFFUSION_WHOLE_MODULE_OFFLOAD=1`, the cross-tree A/A against main; - `head`; - `head_group`: head with the per-request part off. - **Rounds:** 3 rotated rounds of processes; each process did 2 warm-up rounds and 5 measured rounds, with a new prompt per round. Every measured 8-step render ran after the deferred compile and the CUDA-graph decision. | steps | main | head_model (A/A) | head | head_group | |---|---|---|---|---| | 2 | 0.562 s | 0.561 s | **0.458 s (-18.5%)** | 0.458 s | | 4 | 0.624 s | 0.623 s | **0.617 s (-1.1%)** | 0.615 s | | 8 | 1.025 s | 1.026 s | **0.998 s (-2.6%)** | 1.182 s | - **8-step spread:** per-process medians were 0.997-0.999 s for head against 1.025-1.026 s for main. - **Mode per request:** head ran the denoiser whole on 15 of 15 measured 8-step renders, and streamed every 2- and 4-step render, as priced. - **Images:** md5 identical to main for all 21 images in every arm. - **Compilation:** 0 recompiles, 0 CUDA-graph captures and 0 FX cache misses on measured calls, in every arm. - **VRAM** (allocated / reserved MiB, cap 12544): - main: 7853 / 12294; - head streamed: 2266 / 11010; - head whole: 8021 / 11010. - **B200 (local):** the load picks whole-module, as main does, with identical images. This was checked on the first commit; the later commits change only the per-request peak lookup. ## Tests - New `tests/test_diffusion_offload_tier_per_request.py` (12 CPU tests, 2 CUDA tests): the request pricing, the refusals, all-or-nothing pinning and release, the peak-key fallback, and the kill switches. - New `tests/test_diffusion_placement_monotonic.py`. - Memory planner, offload, background compile, cold start and CUDA graph files: 908 passed, 136 skipped. ## Limits - After the compile engages, 8-step renders stream until the CUDA-graph decision is final, which on G4 is the 5th render. - Not measured: time to first image, A100 under the final commits, requests with CFG, a batch or reference images, and families other than FLUX.2-klein.
dayanruben
pushed a commit
to dayanruben/unsloth
that referenced
this pull request
Oct 25, 2026
…ntegrated GPU (unslothai#15178) ## Summary unslothai#15163 added a 0.1 s probe that times pinned host-to-device bandwidth and bf16 matmul throughput, which the whole-module vs streamed offload choice uses. The probe allocates a 256 MiB pinned host buffer without asking `host_pinning_allowed()`. On an integrated GPU (an APU, where the "VRAM" is system RAM), a pinned copy only takes RAM away from the card. Every other pinning site already skips pinning there. `tests/test_integrated_gpu_no_host_pin.py::test_every_pinning_site_asks_the_integrated_gpu_predicate_or_is_reached_through_one` has failed on main since unslothai#15163: ``` pinning without asking host_pinning_allowed: core/inference/diffusion_memory.py:4072 _offload_cost_measurements ``` ## Changes - `_offload_cost_measurements` returns None when host pinning is not allowed, as it already does off CUDA and on ROCm. - An unpriced load keeps unslothai#15163's load-time behaviour: the whole-module plan of unslothai#15129. That path's own host pinning already asks the predicate. - Discrete GPUs are unchanged. ## Tests - `tests/test_integrated_gpu_no_host_pin.py` passes again: it failed on main with the message above. - `tests/test_diffusion_offload_tier_per_request.py`: 52 passed, 2 skipped together with the guard test. The planner and offload suites pass: 612 passed, 129 skipped. ## Limits No run on a real integrated GPU here. The change only skips a probe on the path the predicate already marks as integrated.
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.
Follow-up to #15099. A budget sweep of the flat planner still showed 195 places where more free VRAM picked a slower placement. All of them sit at the same step, about 12288 to 12544 MiB free, where the streamed-encoder group floor (10400 MiB) starts to fit:
model), which runs the whole denoiser on the card while it is called.groupwith the transformer streamed, keeping 0, 4704 or 5216 MiB of the denoiser resident and streaming the rest every step.The real FLUX.2-klein-4B bf16 pipe shows the same flip:
modelup to 12288 MiB free, thengroupwith nothing resident from 12544.ComfyUI loads as much of the denoiser as fits beside its inference reserve for each sampling call (
load_models_gpu,partially_load), so at 12.25 GiB the whole klein denoiser would be resident there, which matches themodelplan.Changes
prefer_whole_module_offload(pipe, refined)indiffusion_memory.py, called indiffusion.pyright afterrefine_plan_from_loaded_weights.whole_module_kept_below_floor_mibestimate. Everything else, including every plan below the floor, is unchanged.Results
FLUX.2-klein-4B bf16 (real weights), 1024x1024, 4 steps, exclusive B200 with the free-memory cap probe, 3 interleaved process repeats x 2 renders.
modelarms at 12.00 vs 12.25 GiB differ by about 3% (A/A). Whole-module offload is 1.77x faster than streaming at the same budget, and 1.7 to 1.9x faster even with most of the denoiser pinned resident.CPU sweep: the planner budget sweep from #15099.
Why bf16 only
The fp16 evidence pointed the other way. On a Kaggle T4 (fp16, stock diffusers, not Studio), a 4-step render took 23.2 s with whole-module offload and 9.3 s streaming (one run each, no A/A). Stock diffusers copies the weights back to the CPU after every call and Studio does not, so this probably overstates the gap, but it is not enough to move small-card fp16 plans. Z-Image Turbo fp8 on the real torchao path is already monotonic (0.415 / 0.382 / 0.342 / 0.341 s at 10.75 / 11.0 / 13.25 / 13.5 GiB).
Tests
tests/test_diffusion_placement_monotonic.py:Not covered
modelinto streaming at load).