Skip to content

Studio diffusion: measured activation rows for MOSS-Image and FLUX.3 Klein base; layer split prices Z-Image from its measured row - #14950

Merged
danielhanchen merged 5 commits into
mainfrom
studio-diffusion-activation-factors
Oct 23, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-diffusion-activation-factors

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

ComfyUI prices each model's activations with memory_usage_factor (comfyanonymous/ComfyUI d07dff4, comfy/supported_models.py): Qwen-Image 2.1 6.0 (2168), MOSS-Image 14.6 (2048), and FLUX.3 (class Flux3(Flux2), 847) inherits the Flux2 formula factor * 4 * hidden_size / 2604 (820). model_base.memory_required (424-442) turns the factor into MiB of activations per latent pixel. Those three numbers are ComfyUI constants, not Studio measurements.

Studio's equivalent is the per-phase activation table _MEASURED_IMAGE_ACTIVATION_MIB in diffusion_memory.py (text encoder, denoise, untiled decode, tiled decode, 2048x2048 worst case, per speed tier). On NVIDIA, _calibrated_activation reads it when the load is planned. On main:

family own row on main load plan on main
qwen-image-2.1 yes measured row
flux.3-klein yes measured row
flux.3-klein-base no flat 8192 MiB per megapixel
moss-image no (#14552 lists it as not added) flat 8192 MiB per megapixel

So the MOSS-Image and FLUX.3 Klein base load plans reserved 8 GiB of activations at 1024x1024, about 2.2x what they use.

Fix

Two measured rows. B200, bf16 (transformer_quant=auto picked bf16 with the hub offline), 1024x1024 and 2048x2048, 4 steps, every tier (off / default / max). Each phase is the peak allocated during that phase minus the resident weights.

  • moss-image: (1940, 528, 3666, 3662, 3750) on both tiers.
    • Denoise: 519-528 eager, 374-462 compiled.
    • Decode: the fp32 VAE decode is the worst phase, 3666 eager / 2807 compiled. A 512 px tile barely lowers it at 1024x1024 (3662 / 2700).
    • 2048x2048: max(denoise 2106, tiled decode 3750).
    • Text encoder: 1940 MiB above the weights when streamed (group offload on a 36 GiB cap), 4-22 MiB when resident.
  • flux.3-klein-base: (301, 901, 2512, 2512, 3359) on both tiers.
    • Measured: denoise 601 / 618 / 652 (off / default / max), untiled decode 2443 / 2512, 2048x2048 max(denoise 2369, tiled decode 2474).
    • It shares Klein's DiT and VAE (ComfyUI prices both with the one Flux2 formula), so each column takes the larger of the two families. That brings in Klein's 301 MiB text encoder window and its 901 MiB int8 / fp8 denoise, which this bf16 run could not measure.

The 1.2 margin applies on top, as for every row.

The multi-GPU layer split also reads the measured row now. Before, it took measured_runtime_headroom_mib, else calibrated_headroom_mib, else the flat runtime_headroom_mib. But calibrated_headroom_mib is only recorded when the measured row changes the one-card plan, so on a multi-card load the split usually got the flat 8192 MiB per megapixel. The companion split already used the row as its phase headroom. On a multi-card selection, plan_diffusion_memory now records calibrated_split_headroom_mib (untiled worst phase, since a split keeps the VAE resident on the primary), and refine_plan_for_multigpu uses it before the flat figure. One-card plans are unchanged: the key is only written under a multi-card selection.

This fixes a Kaggle 2x T4 report: guided Z-Image-Turbo (fp16) declined the layer split, "11931 MiB of denoiser and VAE weights with the text encoders streamed, short by about 1007 MiB after the per-card reserves and 6963 MiB of activations on cuda:0", at both 1024 and 768. Every 2-card arm then streamed one card from host RAM and was SIGKILLed. The load plan always budgets the 1024x1024 default, because the request size is unknown at load. The generate-time guard re-prices the real request, which scales with the pixel count (6963 MiB at 1024, 3916 MiB at 768). Both rows use the eager worst case on every tier, because the table's tiers may only grow with speed.

Tests

  • New tests/test_diffusion_activation_factors.py (CPU). Each listed family resolves to its measured row, tiers only grow with speed, base >= Klein, and _calibrated_activation returns the row on NVIDIA for the real family objects. On main 8 of 13 fail, all MOSS-Image and FLUX.3 base cases. Qwen-Image 2.1 and FLUX.3 Klein already pass on main.
  • test_diffusion_auto_placement.py, using the Kaggle numbers on 2x T4 (Z-Image denoiser 11739 MiB, VAE 211 MiB, encoder 7672 MiB):
    • the flat 6963 MiB still declines the layer split (the same message);
    • with Z-Image's measured row the split is accepted across both cards, and each card's requirement fits its limit (fails on main);
    • the activation estimate scales with resolution (6963 MiB at 1024, 3916 MiB at 768).
  • test_diffusion_flux3.py: the two tests that pinned "base unmeasured, flat estimate" now pin the base row.
  • Planner, placement and family suites (memory, multigpu, multigpu wiring, auto placement, auto GPU, layer split plan and layer split, ROCm APU reserve, DGX Spark, auto policy, learned peaks, dense tier activation, stream residency, fp8 stored label, measured placement and measured placement dense, MPS / Metal budget, calibrated activation, flux3, MOSS-Image, image decode phase, test_diffusion_backend.py): main 1931 passed, 1 failed. This branch has the same single failure (test_mirrored_base_still_trips_the_flux2_shape_guard, no gguf module in this venv, fails on both) once the two pinned flux3 tests are updated.
  • Planner prediction vs a real render, MOSS-Image SFT, default tier, bf16, 1024x1024, on a B200 capped to a 42 GiB card (free reading and caching allocator):
tree plan predicted activations (MiB above weights) measured render peak above weights (render 1 / 2) resident weights OOM
main group, encoder streamed 8192 (flat) 2807 / 2700 (encode phase 1940) 9963 MiB no
this branch none, everything resident 4399 (calibrated row, 1.2 margin) 2807 / 2700 25101 MiB no

The prediction covers the measured peak with 1.57x headroom, against 2.9x for the flat estimate. The peak, 25101 + 2807 MiB = 27.3 GiB, stays inside the 42 GiB cap. Images are 4-step smoke renders, so the comparison is placement only, not speed. The same capped run at 36 GiB gives group on both trees (the full set does not fit there).

Not covered

  • int8 / fp8 MOSS-Image and FLUX.3 base denoisers: the prequantised files were not in the local cache, so only bf16 ran. The rows keep the eager worst case, and for the base Klein's measured int8 / fp8 denoise.
  • NVIDIA B200 only. Like every row in this table, the rows only apply on NVIDIA with a sub-quadratic attention kernel. ROCm, MPS and math-only attention keep the flat estimate.
  • Qwen-Image 2.1 is unchanged: it already had its own row on main.
  • The 2x T4 Z-Image split is proven by the unit case only. It was not re-run on Kaggle. The Z-Image row was measured at B200 bf16. On T4 the split takes it only where _calibrated_activation already accepts Z-Image (fp16 kept by its guard, sub-quadratic SDPA confirmed). Whether one-card host-RAM streaming still gets SIGKILLed on a 31 GB host is a separate issue.

@danielhanchen danielhanchen self-assigned this Oct 23, 2026
[skip ci]
@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-23T08:36:06.515932Z dc0e229 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: 16097926a4

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

if current_selection() is not None:
# Untiled: a layer split keeps the VAE resident on the primary. Kept apart from calibrated_headroom_mib,
# which means the one-card plan was decided from these rows.
estimates["calibrated_split_headroom_mib"] = calibrated_activation.headroom(False)

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 Propagate split calibration to the generation guard

When a multi-GPU plan only fits because this new estimate replaces the flat headroom—for example, a FLUX.1 layer split—the final _LoadState.calibrated_placement in diffusion.py still checks only for calibrated_headroom_mib. As a result, ControlNet renders on such a split are not treated as conditioned: image_activation_verdict neither adds the output-sized ControlNet activation nor disables the default-resolution exemption, so a tight plan can be admitted and then overrun VRAM. Treat calibrated_split_headroom_mib as calibrated when constructing the load state as well.

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.

Real. A split that only fits because the layer split read calibrated_split_headroom_mib was still recorded with calibrated_placement = False, so the generate guard did not charge a ControlNet or references on top, and it kept the flat default-resolution exemption the split never reserved.

Fixed in dc0e229. The new plan_is_calibrated(plan) in diffusion.py is true for:

  • a plan that carries calibrated_headroom_mib (unchanged);
  • a multi-GPU split (a layer split or a companion split) whose estimates carry calibrated_split_headroom_mib.

The load state now takes its calibrated_placement from it. The one-card fallback still carries the key but was decided from the flat estimate, so it stays uncalibrated, and one-card behaviour is unchanged.

Test: test_a_split_priced_with_the_measured_row_is_a_calibrated_placement (2x T4, Z-Image, the Kaggle sizes). It checks that the measured-row split is calibrated, and that the one-card fallback and the flat-estimate decline are not. It fails on 1609792, where the split counted as uncalibrated. Auto placement, ControlNet, calibrated activation, multigpu, layer split, memory, activation factors and backend suites: 1287 passed, 1 failure that also fails on main (test_mirrored_base_still_trips_the_flux2_shape_guard, no gguf module in this environment).

…d placement for the generate guard

[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. 🚀

Reviewed commit: dc0e229c1e

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

[skip ci]
@danielhanchen
danielhanchen merged commit bef946c into main Oct 23, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-activation-factors branch October 23, 2026 16:40
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