Repository navigation
Studio diffusion: seed dense fp16 denoisers on the default and max tiers from measured peaks - #14720
Conversation
…ers from measured peaks Pre-Ampere cards (T4, V100, Turing) load the denoiser dense in fp16, so the measured-activation lookup took its dense branch, which only knew the eager tier. On Studio's default tier it returned nothing and the load placed from the flat 8 GiB per megapixel estimate (6.8 GiB for turbo models) against measured peaks of about 2.5 GiB, streaming a DiT that fits. The dense branch now also reads the per-phase table's worst phase for the tier, only for families the dense table measured at the loaded DiT size (FLUX.2-klein-4B, FLUX.3-klein, FLUX.1). Z-Image keeps the flat estimate: partial residency of its 11.5 GiB DiT on a T4 ran slower than streaming it whole. UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_DENSE_TIERS=0 restores the flat estimate.
|
@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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 491ec22a4b
ℹ️ 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".
| or _env_off(MEASURED_ACTIVATION_DENSE_TIERS_ENV) | ||
| ): | ||
| return None | ||
| return max(peak, max(phases[1 if speed_mode == "max" else 0][:3]) * widen // 2) |
There was a problem hiding this comment.
Gate dense tier seeds on calibrated target eligibility
When a dense fp16 load uses the default or max tier on ROCm or on a CUDA device whose SDPA probe does not confirm a sub-quadratic kernel, this enables the static phase seed anyway. The same _MEASURED_IMAGE_ACTIVATION_MIB data is deliberately guarded by _calibrated_activation/phase_seeds elsewhere because it was measured only on NVIDIA with sub-quadratic attention; bypassing that guard can replace the flat ~8 GiB reserve with ~3 GiB and retain transformer groups even though math attention needs substantially more memory, causing generation-time OOMs. Require the phase-seed eligibility signal before activating these new tier seeds.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
You're right. The new default / max tier seeds read _MEASURED_IMAGE_ACTIVATION_MIB, which was measured only on NVIDIA with a sub-quadratic attention kernel, and nothing gated them on that. A dense fp16 load on ROCm, or on a CUDA card whose SDPA probe only ran math, would have dropped from the flat reserve to about 3 GiB, even though quadratic attention needs far more than that.
Fixed:
diffusion._measured_target(target)is True only for NVIDIA CUDA where a sub-quadratic SDPA kernel was seen to run. ROCm, math-only attention, MPS and a probe that raises are all False.- The load records it on the pipeline as
_unsloth_dense_tier_seeds.refine_plan_from_loaded_weightsand the per-request guard (measured_request_extra_mib) pass it tomeasured_image_runtime_mib(dense_tier_seeds=...), which defaults to False. Without it, the default / max tiers keep the flat estimate. - The eager dense table is unchanged, so it behaves exactly as on main.
I did not reuse _unsloth_phase_seeds, because it folds in _calibrated_activation's reference-image gate. That gate is False for FLUX.2-klein on every card, and the dense table never applied it, so reusing it would have turned the fix off for the model it is for. The new flag carries only the target condition.
On the Kaggle T4 runs in the description the probe ran the memory-efficient kernel (sdpa_subquadratic_confirmed True, vendor nvidia), so those placements and numbers are unchanged.
Tests:
test_unmeasured_target_keeps_the_flat_plan: off the measured target, the default and max tiers return the flat plan, and eager still refines.test_measured_target_needs_nvidia_and_a_subquadratic_kernel: covers the gate itself.- The seed-floor test and
test_dense_headroom_valuesnow also assert None without the flag.
These fail before the fix and pass after it. 1583 passed, 26 skipped across the diffusion memory, measured placement, learned peaks, layer split and backend suites.
The default / max tier seeds read the per-phase table, which was measured on NVIDIA with a sub-quadratic attention kernel. On ROCm, math-only attention or an unanswered SDPA probe a dense fp16 load now keeps the flat reserve on those tiers. The flag is separate from the phase-seed flag, which also folds in the reference-image gate the dense table never used. [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7c2ce4732d
ℹ️ 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".
| # the phase-table seed holds only where it was measured | ||
| pipe._unsloth_phase_seeds = _calibrated_activation(fam, target) is not None | ||
| # the dense default / max tier seeds hold on the measured target only | ||
| pipe._unsloth_dense_tier_seeds = _measured_target(target) |
There was a problem hiding this comment.
Keep unpriced reference families off dense tier seeds
For a dense fp16 FLUX.2 Klein load on an eligible NVIDIA card, this sets the tier marker even though the adjacent _calibrated_activation logic deliberately rejects this family because its reference inputs cannot be sized. A single-image reference request contributes zero guard_condition_pixels in the non-FLUX.3/no-reference_resolutions branch, so measured_request_extra_mib retains the ~3 GiB text-to-image seed and does not release the newly resident DiT groups for the additional reference activation, which can OOM the small cards this placement targets. Apply the same reference-family eligibility here, or ensure the request guard charges the primary reference image before enabling the seed.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
You're right. A FLUX.2 Klein reference render adds the source image's latents to the denoiser's sequence. The request guard doesn't count them: guard_condition_pixels only prices extra images for this family, not the primary reference. The dense default / max tier seed was measured on text-to-image. So on a tier-seeded placement, a single-reference request kept the ~3 GiB reserve and never released the resident DiT groups for the larger denoise.
I fixed it in the request guard rather than turning the tier seed off for klein. Gating the seed on the reference family would remove the fix for plain klein text-to-image on every card, which is the case it exists for.
generate()now marks a request as carrying unpriced references when the family takes references, an input image is present, and the family prices that image neither throughreference_resolutionsnor as FLUX.3. It passes this asmeasured_request_extra_mib(..., unpriced_references=...).- On a placement made from a dense default / max tier seed, such a request sizes from the flat estimate, the reserve main places klein with. The guard then streams the resident groups again for that call, so the request runs as main's flat plan would. When the release falls short, the resident transformer is hooked, the same as for a priced conditioned request.
- Text-to-image requests keep the seed. The eager dense table, used before this PR without a reference check, is unchanged.
Tests:
test_unpriced_reference_request_sizes_from_the_flat_estimate: a reference request on the tier-seeded klein reserve asks for the flat estimate minus the reserve; text-to-image asks for nothing; the eager reserve is unchanged.test_generate_passes_unpriced_references_to_the_guard: checks thegenerate()wiring.test_generate_hooks_the_resident_transformer_only_when_the_release_falls_short: now covers the unpriced case.
All three fail before this change and pass after. 1584 tests pass across the diffusion memory, placement, learned peaks, layer split and backend suites.
I did not run a T4 reference render. Kaggle is out of quota, and the change only makes the guard reserve more for those requests.
…mate on tier-seeded loads A FLUX.2 Klein reference render adds the source image's latents to the denoiser's sequence. The request guard does not count them in condition_pixels, and the dense default / max tier seed was measured on text-to-image, so such a request on a tier-seeded placement now sizes from the flat estimate and streams the resident groups again (hooking the resident transformer when the release falls short). Text-to-image keeps the seed; the eager tier is unchanged. [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
[skip ci]
# Conflicts: # studio/backend/tests/test_diffusion_measured_placement_dense.py [skip ci]
…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.
Summary
On a T4 (and any pre-Ampere card: V100, Turing), Studio placed FLUX.2-klein-4B as if one 1024x1024 render needed 8 GiB of activation memory on top of the weights. Measured on a real T4, the peak above the resident weights is 2.5 GiB on the first render and 1.4 GiB after that. Because of that estimate the 7.4 GiB DiT streamed from host memory every step, even though it fits the card.
The cause is not the attention check. On a Kaggle T4 (sm75, torch 2.13) the SDPA probe runs the memory-efficient kernel, so
sdpa_subquadratic_confirmedis True. The real cause:measured_image_runtime_mibtakes its dense branch._MEASURED_DENSE_SPEED_MODES = ("off",)).refine_plan_from_loaded_weightsthen kept the flat plan (8192 MiB x pixels, 0.85 for turbo models).measured_runtime_headroom_mibfell back to the same flat number.Ampere and newer cards load bf16, which has no static seed (learned peaks only), so they never take this branch. That is why only small cards were affected.
Change
studio/backend/core/inference/diffusion_memory.py,_dense_seed_mib:Eager tier: unchanged (the dense table).
Default / max tiers: for a family the dense table measured at the loaded DiT size, the seed is now the larger of:
_MEASURED_IMAGE_ACTIVATION_MIB: encoder, denoise, untiled decode).The result is then multiplied by the usual 1.15 margin and rounded up to 256 MiB. For FLUX.2-klein-4B that is
max(2455, 2645) x 1.15= 3072 MiB on default, and 3328 MiB on max.What it never touches:
diffusion._measured_target, recorded on the pipeline as_unsloth_dense_tier_seeds). ROCm, math-only attention, MPS or a probe that raises keep the flat estimate, since math attention needs far more than the measured reserve. The Kaggle T4 runs below had the memory-efficient kernel, so they take the seed.measured_request_extra_mib) reads the same seed, scaled by pixels and batch. A 2048x2048 request asks for 12288 MiB and streams resident groups again, so the OOM ladder stays a backstop.Kill switch:
UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_DENSE_TIERS=0restores the flat estimate. The existingUNSLOTH_DIFFUSION_MEASURED_ACTIVATION(_DENSE)=0switches still apply.This follows ComfyUI in spirit. ComfyUI prices inference memory per pixel per model (
memory_usage_factor, plusminimum_inference_memory= 0.8 GiB +EXTRA_RESERVED_VRAM), not as a flat amount. Here the per-model number is a measurement rather than a factor, and the margin is above what ComfyUI keeps.Z-Image is deliberately left out. An earlier version also seeded it from the phase table (no dense entry). On a T4 that kept its 11.5 GiB DiT partly resident, and that ran slower than streaming it whole: 3.8 to 4.4 s/step against 3.2 to 3.6 on main. Z-Image keeps the flat plan until partial residency of a dense DiT is faster than streaming.
Results, Kaggle T4 (15 GB, fp16, torch 2.13)
Each arm is a fresh process with an empty Studio home (no learned peaks), loading through the real
DiffusionBackendwith no GPU selected (Automatic). Settings: FLUX.2-klein-4B, 4 steps, 1024x1024, 3 prompts with new seeds, default speed tier unless noted.1 card=UNSLOTH_DIFFUSION_MULTIGPU=0. Peak is the torch peak allocated over the render; renders 1 / 2 / 3.Peak above the resident weights with this PR is 2523 MiB on render 1 (it includes a 690 MiB CUDA graph pool) and 1426 MiB after that. The 3072 MiB reserve covers both.
Largest canvas the UI allows (2048x2048, this PR): 1 card 13.4 / 13.9 s/step, peak 7862 MiB (the guard released the resident groups for the request); 2 cards 13.8 s/step, peak 9197 / 11185 MiB. No OOM on either.
Compiles and captures: renders 2 and 3 add 0 Dynamo graphs and 0 CUDA graph captures in every arm. Render 1 compiles and captures as on main.
Images:
No regression elsewhere:
Tests
tests/test_diffusion_dense_tier_activation.py(CPU, fake T4 and B200 budgets, loaded sizes stubbed). 4 of its 7 tests fail on main and all pass here:tests/test_diffusion_measured_placement_dense.py: the assertion that the default tier keeps the flat estimate now asserts the new seed.917 passed, 26 skipped across the diffusion memory, measured placement, learned peaks, layer split, stream residency, small host, FLUX.3 and auto policy suites.
Not covered