Skip to content

Studio diffusion: seed FLUX.2 klein residency from its measured peak on the first load - #14866

Merged
danielhanchen merged 4 commits into
mainfrom
studio-diffusion-reference-phase-seeds
Oct 23, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-diffusion-reference-phase-seeds

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On its first load on a machine, an automatic FLUX.2 Klein that does not fit resident streams every block on every step, even when the int8 transformer would fit the card whole.

refine_plan_from_loaded_weights places a torchao denoiser from a measured activation figure. For Klein that figure is the per-phase seed (_MEASURED_IMAGE_ACTIVATION_MIB["flux.2-klein"], measured on klein 4B and 9B). The seed was gated by _calibrated_activation, which refuses every reference family the request guard cannot price, because Klein's source image is not in condition_pixels. So the first load had no headroom figure and kept the flat plan. Only a later load, after a render had recorded a learned peak, kept the transformer resident.

The request guard already handles that unpriced reference for the dense default / max tier seeds: measured_request_extra_mib(..., unpriced_references=True) sizes the request from the flat estimate, so the resident groups stream again for that call. This change gives the phase seeds the same treatment:

  • _calibrated_activation(..., unpriced_references_ok=True) skips the reference rule. Only the placement seed (pipe._unsloth_phase_seeds) asks for that; the planner's tier choice keeps the rule.
  • measured_request_extra_mib treats a phase-seeded torchao placement like a dense tier-seeded one when a request carries an unpriced reference.

Nothing changes for non-reference families, for ROCm / math-only attention (still no seed), or for the eager tier.

Evidence

Single B200, card capped with set_per_process_memory_fraction, with mem_get_info reporting the capped total. Studio in-process, automatic memory mode, default speed tier (compiled, CUDA graphs), auto int8. Each arm is a fresh process with a fresh Studio home (first load). 1024x1024, 4 steps, 3 new prompts. s/call is the median DiT forward on renders 2 and 3.

model, cap resident DiT peak reserved s/call renders 2 / 3 graph breaks / recompiles, renders 2-3
klein-4B, 12 GiB merge base 0 of 3.77 GiB 4.6 GiB (38%) 0.094 / 0.076 0 / 0
klein-4B, 12 GiB this branch 3.77 of 3.77 GiB 7.3 GiB (61%) 0.024 / 0.021 0 / 0
klein-9B, 16 GiB merge base 0 of 8.75 GiB 6.0 GiB (37%) 0.189 / 0.180 0 / 0
klein-9B, 16 GiB this branch 8.75 of 8.75 GiB 13.9 GiB (87%) 0.042 / 0.049 0 / 0

For reference, the same models resident on an uncapped 24 GiB card run 0.022 (4B) and 0.041 (9B) s/call.

Parity:

  • klein-9B 16 GiB: images are byte-identical to the merge base at every render.
  • klein-4B 12 GiB: images are byte-identical to the merge base at 8, 16 and 24 GiB. Two merge-base processes at 12 GiB disagree with each other and with every other cap, so the 12 GiB base is not reproducible across processes. This branch at 12 GiB matches the 8 / 16 / 24 GiB output exactly.

Edit request (the unpriced reference) at klein-4B 12 GiB, 2 renders with the first image as the source: both complete with no OOM, at a 7.2 GiB peak, and the transformer is back to resident afterwards.

CUDA graphs: klein-9B at 16 GiB re-records the step graph after each new prompt (captures 1 / 2 / 3), because the prompt encode streams resident groups out and back. The merge base streams everything, so it re-records nothing. That re-record is what #14765 removes, and on that branch FLUX.1-schnell int8 at 16 GiB shows captures 1 / 1 / 1. The re-record costs one capture per new prompt; the residency itself is still a 3.7 to 4.3x speed-up per step here.

Tests

  • New tests/test_diffusion_reference_phase_seeds.py: three of its four tests fail on the merge base and pass here. They cover the opt-in skip of the reference rule, the load path using it, and the unpriced reference sizing from the flat estimate on a phase-seeded placement. The fourth pins the first-load 16 GB placement.
  • test_diffusion_learned_peaks.py: its source pin follows the new call.
  • test_diffusion_calibrated_activation.py, test_diffusion_measured_placement*.py, test_diffusion_dense_tier_activation.py, test_diffusion_learned_peaks.py, test_diffusion_flux3.py: 718 passed, 2 skipped.

…the first load

The phase-table seed was withheld from every reference family the request
guard cannot price, so an automatic int8 Klein had no headroom figure until a
learned peak existed and streamed every block every step on its first load
(klein-9B at 16 GB: 0.18 s/step at a 6 GB peak; klein-4B at 12 GB: 0.076-0.094
s/step at 4.6 GB). The placement now takes the seed, and a request carrying
Klein's unpriced source image sizes from the flat estimate, the path the dense
tier seeds already use, so its resident groups stream again for that call.
@danielhanchen danielhanchen self-assigned this Oct 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-23T02:34:59.071742Z 2d7577f 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. Nice work!

Reviewed commit: 2d7577f884

ℹ️ 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 a4e25b4 into main Oct 23, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-reference-phase-seeds branch October 23, 2026 05:42
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 27, 2026
…plits so edits are not refused (unslothai#14858)

## Problem

unslothai#14720 seeds dense fp16 denoisers (T4, V100, Turing) on the default and
max tiers from a text-to-image measurement, and unslothai#14866 seeds FLUX.2
Klein from its measured phase table. A FLUX.2 Klein reference request is
not covered by either seed. Its source image joins the denoiser's
sequence without being priced, so `measured_request_extra_mib` sizes the
request from the flat estimate. On one card the resident DiT groups
stream back to pay for that, which is fine.

A multi-GPU companion split or layer split has no resident groups to
stream back, and `hook_resident_denoiser` only hooks torchao denoisers.
The split was still priced from the seed. That packs the primary with
weights a reference request cannot get back, and the request guard then
refuses the edit:

| Topology (fake cards, real planners) | Parent of unslothai#14720 | main |
|---|---|---|
| 2x T4, two-image edit at 1024x1024 | layer split, 533 MiB of weights
on cuda:0, guard: tile and run | layer split, 3053 MiB on cuda:0, guard:
refuse |
| 24 GB Turing with 6 GB in use + T4 | layer split, guard: tile and run
| companion split, 7553 MiB on cuda:0, guard: refuse |

The refusal is not needed. Measured on real FLUX.2-klein-4B (bf16, B200,
diffusers, 1024x1024), the reference images add at most about 1.5 GiB to
the peak. The latent-only peak is 621 MiB for text-to-image, 1583 MiB
with one reference, 1652 MiB with two and 2168 MiB with three. With the
untiled decode included, the peak is 2477 to 2487 MiB in every case.

## Fix

- `refine_plan_from_loaded_weights` records `measured_omits_references`
under the same condition that makes `measured_request_extra_mib` size a
reference request from the flat estimate: the family's references go
unpriced, the tier is default / max, and the plan was placed from a
dense tier seed (dense denoiser) or a phase seed (torchao denoiser).
- `refine_plan_for_multigpu` prices the companion and layer split from
the larger of the measured headroom and the calibrated / flat headroom
for such a plan.
- `DiffusionBackend` sets `pipe._unsloth_unpriced_references` from the
family through `_references_unpriced`. That covers FLUX.2 Klein, and
excludes FLUX.3 and families with `reference_resolutions`.
`_calibrated_activation` reuses the same helper for its reference rule.

What does not change:

- One-card placement still uses the seed. The single T4 keeps its
resident DiT, which was the main speed win in unslothai#14720.
- The eager tier is unchanged.
- Text-to-image-only families keep the seeded split.
- Placement happens once at load, so later prompts get no new compiles
or CUDA graph captures.

## Tests

`tests/test_diffusion_multigpu_reference_headroom.py` (CPU, fake cards).
5 of its 9 tests fail on main and all pass here:

- on 2x T4 and on a busy 24 GB Turing + T4, Klein's split matches the
flat placement and a two-image edit is not refused (main refuses it);
- the one-card plan still places from the seed;
- a phase-seeded (torchao) Klein plan is flagged too, and a
text-to-image-only one is not;
- a learned peak above the flat estimate still prices the split;
- a text-to-image-only family keeps the seeded split, with more weights
on the primary than the flat plan;
- the eager tier is unchanged;
- only Klein-style families are flagged.

Across all 279 `tests/test_diffusion*.py` files (CPU, `-n 4`): 17145
passed here and 17136 on main (the 9 new tests). The same 47 tests fail
on both sides (identical IDs), for example
`test_diffusion_multigpu.py::test_execution_device_reports_the_primary_of_a_split_pipeline`;
none of them touch this change.

## Real 2x T4 (Kaggle, one kernel)

FLUX.2-klein-4B (fp16), default tier, automatic multi-GPU placement, a
two-image reference edit at 1024x1024 (source + one reference, both
1024x1024), 4 steps. Base is main, head is this branch, both in the same
kernel.

| Arm | Placement | Weights after load (cuda:0 / cuda:1) | Two-image
edit | Peak allocated (cuda:0 / cuda:1) |
|---|---|---|---|---|
| main | layer split, 14 blocks (4446 MiB) on cuda:0, 11 on cuda:1, text
encoder on cuda:1 | 4993 / 8554 MiB | refused: needs about 11.00 GB,
7.54 GB usable | 4993 / 8554 MiB (nothing ran) |
| this PR | layer split, all 25 blocks (7020 MiB) on cuda:1, text
encoder streamed to cuda:0 | 547 / 7020 MiB | rendered, 8.55 s/step,
84.8 s | 1974 / 10244 MiB |
| main, dense tier seed off (flat placement) | same as this PR | 547 /
7020 MiB | rendered, 8.85 s/step, same image bytes as this PR | 1974 /
10244 MiB |

Text-to-image at 1024x1024 on this PR's placement: 2.42 s/step (first
render, including the CUDA graph captures), peak 1967 / 8197 MiB.

## Not covered

- The reference costs were measured on a B200 in bf16, not on a T4 in
fp16. The real 2x T4 run covers one topology (two idle T4s); the busy 24
GB Turing + T4 case is from fake cards only.
- The phase-seeded (torchao) Klein split is covered by a CPU test only.
- On two T4s, Klein text-to-image goes back to the pre-unslothai#14720 split.
unslothai#14720 measured that split at 2.99 / 1.78 / 1.82 s/step against 2.28 /
1.70 / 1.72 with the seeded split, so the steady-state cost is about 5%.
- Requests with three or more references at 1024x1024 are refused before
and after this change. That comes from the guard's flat estimate, which
this change does not touch.
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