Repository navigation
Studio diffusion: keep the denoiser resident ahead of the text encoders - #15099
Merged
Merged
Conversation
Once the flat plan's companion floor fits, it keeps the text encoders resident and refine_plan_from_loaded_weights takes their bytes out of the denoiser's room. Just past that budget fewer denoiser MiB stayed resident than one 256 MiB step below it (int8 FLUX.1: 11420 -> 5370 MiB at about 19.9 GiB of budget; Qwen-Image measured on a B200 at 28 GiB free: 852 MiB instead of 15629 MiB). The refinement now also prices the same group plan with the encoders streamed and takes it whenever it keeps more of the denoiser resident, as ComfyUI frees the text encoder for the sampler. The denoiser runs every step, the encoders once per prompt. Plans where the encoders cost the denoiser nothing, and every plan below the inversion budget, are unchanged.
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. Chef's kiss. 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 Etherll/unsloth
that referenced
this pull request
Oct 24, 2026
…the streamed floor (unslothai#15129) Follow-up to unslothai#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: - **Below the floor**, the only tier is whole-module offload (`model`), which runs the whole denoiser on the card while it is called. - **Above it**, the plan becomes `group` with 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: `model` up to 12288 MiB free, then `group` with nothing resident from 12544. | model / precision | inversions | |---|---| | FLUX.2-klein bf16 | 60 | | FLUX.2-klein fp16 | 60 | | FLUX.2-klein fp8-stored | 15 | | Z-Image fp8-stored | 60 | 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 the `model` plan. ### Changes - `prefer_whole_module_offload(pipe, refined)` in `diffusion_memory.py`, called in `diffusion.py` right after `refine_plan_from_loaded_weights`. - **When it applies:** in auto mode, the plan is a streamed group plan, the denoiser is plain bf16 (not torchao, packed or fp8-stored), every component would have fit under the streamed floor (so whole-module offload still holds the denoiser whole), and the refined plan does not already pin the whole denoiser. - **What it does:** keeps whole-module offload and adds a reason line plus the `whole_module_kept_below_floor_mib` estimate. Everything else, including every plan below the floor, is unchanged. - **Out of scope:** fp16, packed, fp8-stored and torchao denoisers keep the current ladder (see below). ### 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. | arm | plan | render, median (min to max) | s/step | peak alloc | |---|---|---|---|---| | 12.00 GiB free | model | 0.345 s (0.337 to 0.377) | 0.028 | 7.83 GiB | | 12.25 GiB free, main | group, nothing resident | 0.595 s (0.594 to 0.600) | 0.170 | 3.04 GiB | | 12.25 GiB free, this PR | model | 0.336 s (0.335 to 0.341) | 0.029 | 7.83 GiB | | 12.25 GiB free, learned peak, main | group, 5471 of 7393 MiB resident | 0.727 s (0.724 to 0.757) | 0.170 | | | 13.5 GiB free, learned peak, main | group, 6751 MiB resident | 0.624 s (0.621 to 0.629) | 0.145 | | - The two `model` arms 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. - Peak allocation stays within the budget. **CPU sweep:** the planner budget sweep from unslothai#15099. - Plans below each case's first inversion are byte-identical to main. - 810 plans changed, all in the 60 bf16 cases and all at or above the inversion. - The bf16 inversions go to 0. The other 135 are now reported as hardware-dependent rather than fixed. ### 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 - New cases in `tests/test_diffusion_placement_monotonic.py`: - the bf16 inversion budgets switch to whole-module offload; - fp16 / packed / fp8-stored / torchao denoisers, explicit modes and fully pinned plans are left alone; - plans below the floor are unchanged. - Placement / memory / measured-placement / auto-placement suites: 487 passed. ### Not covered - No run on a real 12-16 GB bf16 consumer card. The B200 has PCIe 5 and fast compute, so the size of the gain on slower cards is not measured. - Studio's own path on a T4, so fp16 is left unchanged. - Z-Image and klein fp8-stored timing on a real pipe. - Qwen-Image-2.1 int8 (torchao turns `model` into streaming at load).
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.
The flat memory planner could keep less of the denoiser on the card when it had more VRAM to work with. For an int8 FLUX.1 load, once the free budget reached about 19.9 GiB, the plan switched to keeping the text encoders resident. Their bytes then came out of the denoiser's room, so one 256 MiB step past that point the resident denoiser dropped from 11420 MiB to 5370 MiB. That is a slower placement, because the denoiser runs every step while the text encoders run once per prompt.
Root cause (
diffusion_memory.py)elif _group_fits():) ranks "transformer streams, encoders resident" above "encoders streamed", so the plan flips to resident encoders as soon as their floor fits.refine_plan_from_loaded_weightsthen subtracts the resident encoder bytes from the denoiser's room (room -= encoders).ComfyUI orders it the other way. It loads the text encoder, runs it, and then
load_models_gpufor the sampler callsfree_memory, which unloads the encoder before the denoiser's partial-load budget is sized, so the denoiser gets priority.Fix
refine_plan_from_loaded_weightsis now a thin wrapper around the old body (renamed_refine_plan_from_loaded_weights).autogroup plan with the encoders resident, the transformer streamed, and a known encoder size.denoiser_first_resident_te_dit_mib.CPU sweep
GPU check
Qwen/Qwen-Image, int8, B200 capped to 28 GiB free.
Tests
tests/test_diffusion_placement_monotonic.py(116 tests): a budget sweep that asserts monotonicity per family, the four int8 FLUX.1 card cases, encoders staying resident when they cost the denoiser nothing, and explicit modes left untouched. 43 of them fail on main.test_whole_transformer_tier_needs_streamed_encodersasserted the old behaviour. It is renamed..._streams_resident_encodersand now asserts 6922 MiB resident with the encoders streamed.Not covered