Skip to content

Studio diffusion: keep reference headroom on multi-GPU FLUX.2 klein splits so edits are not refused - #14858

Merged
danielhanchen merged 4 commits into
mainfrom
studio-diffusion-multigpu-reference-headroom
Oct 27, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-diffusion-multigpu-reference-headroom

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Problem

#14720 seeds dense fp16 denoisers (T4, V100, Turing) on the default and max tiers from a text-to-image measurement, and #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 #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:

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

…xt-to-image tier seed

The dense default / max tier seed (#14720) was measured on text-to-image.
measured_request_extra_mib sizes a Klein reference request from the flat
estimate, and on one card the resident DiT groups stream back to pay for it.
A companion or layer split keeps no such groups, yet the split was priced from
the seed, so it packed the primary with weights a reference request could not
get back. On two T4s a two-image edit at 1024x1024 was refused where the flat
placement tiled the decode and ran.

Mark a seeded plan whose family carries unpriced references
(measured_omits_references) and price the multi-GPU split from the flat or
calibrated headroom for it, as before the seed. One-card placement still uses
the seed, the eager tier is unchanged, and text-to-image-only families keep
the seeded split.
@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-27T13:08:55.635088Z 5bfbf5d 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 86c4227e46

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

Comment on lines +758 to +761
measured_headroom = (
None
if plan.estimates.get("measured_omits_references")
else plan.estimates.get("measured_runtime_headroom_mib")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep learned headroom above the flat fallback

When a FLUX.2 Klein configuration has a persisted learned peak above the flat runtime estimate, measured_image_runtime_mib stores that larger value in measured_runtime_headroom_mib, but this branch discards it solely because references are unpriced and substitutes the smaller flat estimate. The resulting companion or layer split can pack in more weights than the observed peak permits and OOM; the reference fallback should use the maximum of the measured and flat headroom rather than replacing the measured value.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The reference fallback now takes the larger of the measured headroom and the flat (or calibrated) estimate, so a learned peak above the flat estimate still prices the split. Added a test that checks the split is priced at max(measured, flat) for a learned peak both above and below the flat estimate.

…llback split

The multi-GPU reference fallback replaced the measured headroom with the flat
estimate. A learned peak above the flat estimate is stored as the measured
headroom, so the split could then be priced below a peak this machine already
recorded. Take the larger of the two instead.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 37b61ac783

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

# Conflicts:
#	studio/backend/core/inference/diffusion.py
#	studio/backend/core/inference/diffusion_multigpu.py

[skip ci]
@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. 🚀

Reviewed commit: 5bfbf5da2c

ℹ️ 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 c9af351 into main Oct 27, 2026
1 check passed
@danielhanchen
danielhanchen deleted the studio-diffusion-multigpu-reference-headroom branch October 27, 2026 13:51
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