Skip to content

Studio diffusion: keep whole-module offload for a bf16 denoiser past the streamed floor - #15129

Merged
danielhanchen merged 3 commits into
mainfrom
studio-diffusion-planner-offload-tier
Oct 24, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-diffusion-planner-offload-tier

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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:

  • 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 #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).

…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.
@danielhanchen danielhanchen self-assigned this Oct 24, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T12:50:54.538770Z ce83772 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: ce83772d95

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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
danielhanchen merged commit 609524d into main Oct 24, 2026
42 checks passed
@danielhanchen
danielhanchen deleted the studio-diffusion-planner-offload-tier branch October 24, 2026 15:35
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant