Skip to content

Studio diffusion: place bf16 and GGUF / int8 packed denoisers from their learned peak - #13860

Merged
danielhanchen merged 25 commits into
mainfrom
studio-generalize-learned-peaks
Oct 21, 2026
Merged

danielhanchen merged 25 commits into
mainfrom
studio-generalize-learned-peaks

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Stacked on #13847 (learned peaks keyed on the unsloth mirror). Without it a fresh install keys every build as unknown, so nothing here engages. Merge #13847 first. Both branches now carry current main (c08dfbbe64), merged cleanly.

Problem

#13724 records a learned activation peak at every completed image render, and refine_plan_from_loaded_weights reads it on the next load to keep more of the model resident. For a dense denoiser that function still returns before the learned lookup on main when:

  • the compute dtype is bf16 (_denoiser_compute_bytes is None), or
  • the weights are GGUF or int8 packed (_pipe_denoisers_hold_packed).

The early return exists because the static dense eager table was measured on fp16 / fp32 unpacked weights only. That is a reason to skip the table, not the learned peak, which is a real measurement of the exact build (model revision, quant label, VAE savers, attention backend, graphs, GPU name). These loads recorded a peak on every render that no later load ever read: every dense load on a bf16 card (Ampere and newer) and every GGUF image load kept the flat 8 GiB/MP estimate and streamed the whole denoiser.

Change

  • measured_image_runtime_mib(..., static_seed=True): with static_seed=False, every static table is skipped and the answer comes from the learned peak alone (None when nothing was recorded).
  • refine_plan_from_loaded_weights: a bf16 or packed dense denoiser sets static_seed=False instead of returning. With no learned peak the lookup answers None and the flat plan stands, which is today's behaviour. Dense loads keep the conservative branch: the whole-DiT and wide-window pins stay torchao-only.
  • The reserve the request guard reads carries the flag (_unsloth_measured_reserve[8]), so a learned-only load never sizes requests from the fp16 table. Older 8-field reserves read as before. A layer split still drops the reserve (Studio: split one diffusion denoiser across GPUs by block ranges #14412), unchanged.
  • A family that can run classifier-free guidance used to skip recording every render without it, so Z-Image-Turbo at its default guidance 0 never learned a peak. Such a render now records under <label>|cfg:off. A load reads that key only when no CFG peak exists, and marks the reserve. The request guard then sizes any request that runs CFG, or whose guidance mode is unknown, from the flat estimate, because the doubled denoiser batch was never measured. Kill switch: UNSLOTH_DIFFUSION_LEARNED_PEAKS_CFG_OFF=0.
  • Unchanged: fp16 / fp32 dense and torchao loads, "a learned value only raises a seed", non-CUDA and unified-memory devices (the refine is a no-op there), and every kill switch (UNSLOTH_DIFFUSION_LEARNED_PEAKS=0, UNSLOTH_DIFFUSION_MEASURED_ACTIVATION=0, UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_DENSE=0, UNSLOTH_DIFFUSION_PARTIAL_RESIDENT=0).

Results

Colab A100 40 GB, allocator capped, fresh VM (models downloaded from the unsloth/ mirrors). Base is main + #13847, head is this branch, both at current main. Order base, head, head, base. Each arm loads twice in a fresh Studio home: load 1 has no learned peak and records one, load 2 reads it. Median s/step of 3 renders at 1024x1024, speed tier off.

Case base load 1 / load 2 (arms 1, 4) head load 1 / load 2 (arms 2, 3) head placement on load 2 Peak alloc base / head Output
FLUX.2-klein-4B bf16, 10 GiB 0.643 / 0.643, 0.641 / 0.641 0.643 / 0.491, 0.641 / 0.489 1943 of 7393 MiB DiT resident (learned peak 2816 MiB reserved) 2620 / 4397 MiB bit-identical
FLUX.2-klein-4B GGUF Q8_0, 8.75 GiB 0.687 / 0.687, 0.689 / 0.688 0.686 / 0.311, 0.689 / 0.310 663 of 4102 MiB DiT resident 2620 / 3245 MiB bit-identical
Z-Image-Turbo GGUF Q8_0, guidance 0, 10 GiB 0.621 / 0.623, 0.621 / 0.622 0.622 / 0.484, 0.621 / 0.484 1946 of 6888 MiB DiT resident, from its cfg:off peak 2611 / 4562 MiB bit-identical
FLUX.2-klein-4B GGUF Q8_0, 10 GiB 0.239 to 0.241 throughout 0.239 to 0.241 throughout unchanged: the plan is whole-module offload, out of scope 4842 / 4842 MiB bit-identical
  • Speedups on learned loads: klein bf16 1.31x, klein GGUF 2.21x, Z-Image GGUF 1.28x. Every load without a learned peak matches base within 0.5%, which is also the base-vs-base spread.
  • No OOM in any arm. The stored keys name the mirror (model:unsloth/FLUX.2-klein-4B@04e0a8a...), and the Z-Image peak is stored with a trailing cfg:off part. Base records no Z-Image peak at guidance 0.
  • A local B200 run (host heavily loaded, so timings are noisy) with an offline HF cache holding only the mirrors showed the same placements and bit-identical images: klein GGUF 0.80 to 1.01 s/step down to 0.13, while klein bf16 (0.148 vs 0.154) and Z-Image (0.16 to 0.19 vs 0.17) moved within that host's noise. On a card this fast the streamed bf16 copy hides behind compute, so the gain depends on the card's compute to PCIe ratio.

Tests

  • test_diffusion_learned_peaks.py: the CFG test now asserts the cfg:off key (it pinned the old skip), plus 2 new tests for the load fallback / guard and the wiring (the wiring check is whitespace-free so the formatter cannot break it).
  • test_diffusion_measured_placement_dense.py: 5 new tests, all failing without this change:
    • bf16 and GGUF denoisers place from a recorded learned peak, and each kill switch restores the flat plan.
    • An fp16 load keeps its 3072 MiB table seed over a lower learned peak.
    • The request guard of a learned-only load scales the learned peak and ignores the fp16 table.
    • The load stores the flag in the reserve.
  • CPU, base (main + Studio diffusion: key learned peaks on the unsloth mirror a fresh install downloads #13847) vs this branch, over test_diffusion_learned_peaks.py, test_diffusion_measured_placement.py, test_diffusion_measured_placement_dense.py, test_diffusion_small_host_planner.py, test_offload_planner.py, test_diffusion_gguf_offload_route.py, test_diffusion_stream_residency.py, test_video_stream_residency.py, test_diffusion_offload_seed_balanced.py, test_group_offload_background_pin.py: 6 fixed, 2 new, 573 unchanged, 6 skipped (CUDA only), 0 new failures. 4 tests fail identically on main and are not touched here (test_background_pinner_pins_gguf_host_copies and three video peak persistence tests).

Not covered

  • Video: this PR changes no video path.
  • LoRA loads still skip learned peaks. Runtime adapters attach after placement, so this needs a measured per-adapter overhead term, not just a key.
  • On the default speed tier a streamed load is labelled graphs:on but can render graph-free and record under graphs:off, so that peak is not read back. Follow-up: re-refine after the graph decision.
  • CFG requests on a load placed from a cfg:off-only peak are sized from the flat estimate (resident groups stream again for that request) until a CFG render records its own peak. Covered by CPU tests, not by a GPU run.
  • No real 8 / 10 / 12 GB consumer card, no AMD card and no Windows run: a larger card with a capped allocator. macOS / MPS and CPU are untouched (the refine returns before any of this on non-CUDA and unified memory).

…tall downloads

_peak_model_identity fingerprinted the vendor repo id (e.g. black-forest-labs/FLUX.2-klein-4B), but Studio fetches a mirrored base from its byte-identical unsloth/ mirror, so on a fresh install the vendor id has no cached revision, the key became unknown and learned peaks were never recorded nor read. Fall back to mirror_repo() for the denoiser repo and the companion base, and name the repo the revision came from.
…eir learned peak

refine_plan_from_loaded_weights returned before the learned-peak lookup for a
dense denoiser whose compute dtype is bf16 or whose weights are GGUF / int8
packed, because the dense eager table was measured on fp16 / fp32 unpacked
weights only. Those loads still recorded a peak at every render, but no later
load read it, so every bf16 card and every GGUF image load kept the flat
8 GiB/MP estimate and streamed the whole denoiser.

Such a load now places from the peak this machine recorded for its exact
build alone (no static seed); with nothing recorded the flat plan stands. The
fp16 table still seeds fp16 / fp32 loads and a learned value only raises it.
The request guard reads the same flag from the reserve so a learned-only load
never sizes requests from the fp16 table. Every kill switch is unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 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 ⚠️ Failed 2026-10-21T16:08:40.055898Z
We couldn’t complete this review. Comment @codex review to try again.
1d74c87 New commits
🔒 Security Review ⚠️ Failed 2026-10-15T14:20:22.416868Z
We couldn’t complete this review. Comment @codex security review to try again.
ff5a1c2 PR opened
ℹ️ 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.

danielhanchen and others added 6 commits October 15, 2026 07:24
… cfg:off key

A family that can run classifier-free guidance skipped recording every
render that did not run it, so Z-Image-Turbo at its default guidance 0
never learned a peak. Such a render now records under its load label plus
cfg:off. A load reads that key only when the label has no CFG peak, and
marks the reserve: the request guard then sizes any request that runs CFG
(or whose guidance mode is unknown) from the flat estimate, since the
doubled denoiser batch was never measured. A CFG render records the
unlabelled key, which wins on the next load.

UNSLOTH_DIFFUSION_LEARNED_PEAKS_CFG_OFF=0 restores the previous behaviour.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: b58fe018f2

ℹ️ 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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: ef011ce2b3

ℹ️ 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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 5f353f326d

ℹ️ 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".

Base automatically changed from studio-diffusion-learned-peaks-mirror-key to main October 21, 2026 15:53
@danielhanchen
danielhanchen merged commit bf2725e into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the studio-generalize-learned-peaks branch October 21, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant