Skip to content

Studio: budget a pre-cast fp8 text encoder at the size it actually loads - #8218

Merged
danielhanchen merged 8 commits into
mainfrom
tebudget
Aug 9, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
tebudget

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The video memory plan budgets its companions from the family's bf16 size table and scales them only by the pipeline dtype. It never scales them by the text-encoder quantisation the load is about to apply, so a pick that takes its encoder pre-cast from a hosted fp8 checkpoint is still budgeted at bf16.

The bug

video.load_pipeline reads fam.bf16_components_gb, a (transformer, text_encoder, vae) triple, and forms the companion term as components[1] + components[2] scaled by dtype_scale alone. That number becomes companion_dense_mib and, on the GGUF and single-file kinds, the bulk of model_dense_mib. The pipeline kind has the same problem one branch up, where the total is sum(components).

There is a dense-quant re-plan immediately below, but it does not cover this. It multiplies components[0] by _QUANT_STEADY_FACTOR and carries components[1] + components[2] through unscaled, and it is gated on kind == "pipeline", so it never runs for a GGUF load at all.

For ltx-2 the encoder is Gemma3-12B, 24.4 GB at bf16, while unsloth/LTX-2-FP8 publishes LTX-2-text_encoder-FP8.pt at 13.2 GB and VideoFamily.te_prequant_repos wires it up. So the plan over-states the resident requirement by about 11 GB for a load that already works: a measured LTX-2.3 Q6_K run with the fp8 encoder generates a clip and reports resolved.text_encoder_quant = fp8.

Companion sizing dominates the answer for the smaller checkpoints. For wan2.2-ti2v-5b at Q2_K the denoiser is 7.4 percent of the resident requirement and companions plus runtime are 84.6 percent, so the term being wrong is not a rounding detail.

Why it matters more now

PR #8213 adds a hard load refusal on unified-memory hosts when model_dense_mib + base_overhead_mib exceeds the safe budget. That is exactly the number computed here. Once that lands, an over-stated companion term stops being a conservative offload choice and becomes a load refused outright that would in fact have fitted. This change is written to compose with #8213 rather than fight it: the planner is untouched, and the only thing that moves is the size handed to it.

The change

Scale the text-encoder term of the companion budget when, and only when, the load takes its encoder pre-cast from a hosted fp8 checkpoint.

te_prequant_budget_scale(fam, te_quant_mode=..., target=...) returns TE_PREQUANT_BUDGET_SCALE or 1.0. It keys on te_prequant_sources, the same pure, IO-free resolver the download plan and the load itself already use, so the budget cannot disagree with them about what will be loaded. text_encoder_quant is a load_pipeline request parameter validated ~700 lines above the plan, so nothing had to be plumbed earlier.

It keys on the pre-cast source, not on the requested scheme

This is the conservative distinction and it is deliberate. There are two ways an encoder ends up fp8:

  • the hosted pre-cast checkpoint is loaded directly, so both the transient and the steady footprint are fp8;
  • quantize_text_encoders casts the encoder layerwise, which happens after pipeline assembly has already materialised the dense weights, so the steady footprint is fp8 but the peak is bf16.

A budget has to cover the peak, so only the first case earns the credit. In practice that means ltx-2 changes and the Wan and HunyuanVideo families do not: none of them host a pre-cast encoder, and crediting them would under-state a real bf16 peak. A prior estimate that credited wan2.2-ti2v-5b at 0.55 and took it from 24.70 to 19.92 GiB was optimistic for this reason, and that cell is deliberately not changed here.

The factor, and where it came from

TE_PREQUANT_BUDGET_SCALE = 0.65.

fp8 storage is one byte per parameter against bf16's two, so 0.5 is the floor, but the cast keeps modules dense on purpose: nn.Embedding tables, the norms in DEFAULT_SKIP_MODULES_PATTERN, the encoder's own _keep_in_fp32_modules (T5's wo) and an lm_head tied to the input embedding. Measured from Hub file metadata over every published artifact, as hosted checkpoint bytes over the bf16-equivalent dense bytes of the same component (an fp32-stored encoder is halved first, since the pipeline loads it bf16):

artifact component encoder fp8 bytes bf16-equivalent bytes ratio
FLUX.2-dev text_encoder Mistral-24B 24,683,130,873 48,022,800,560 0.514
HiDream-I1-Full text_encoder_4 Llama-3.1-8B 8,555,963,320 16,060,556,376 0.533
Qwen-Image text_encoder Qwen2.5-VL-7B 8,839,210,073 16,584,414,544 0.533
LTX-2 text_encoder Gemma3-12B 13,205,302,695 24,374,720,836 0.542
Krea-2-Turbo text_encoder Qwen3-4B 4,831,262,424 8,875,715,136 0.544
Z-Image-Turbo text_encoder Qwen3-4B 4,411,751,967 8,044,982,000 0.548
Lumina-Image-2.0 text_encoder Gemma2-2B 3,204,501,909 5,228,699,608 0.613
FLUX.1-schnell text_encoder_2 T5-XXL 5,900,818,800 9,524,648,584 0.620

The small encoders sit highest: their embedding tables are a large share of the parameters and stay dense. 0.65 is the observed maximum rounded up, so the budget over-states every measured artifact rather than under-stating any. That is the direction to err in: an under-estimate lets an oversized load through to the OS killer, which is the failure #8213 exists to prevent. The realized ltx-2 encoder is 0.542, so the budget carries about 20 percent slack on the one family this currently affects, and still recovers 8.5 GB of the 11 GB error.

_QUANT_STEADY_FACTOR["fp8"] = 0.55 was not reused. Its docstring derives it for transformer weights, whereas a text encoder additionally keeps its vision tower, lm_head and T5 wo dense, and two of the eight measured artifacts exceed it.

The VAE is not scaled

quantize_text_encoders iterates _TEXT_ENCODER_ATTRS only, TE_PREQUANT_COMPONENTS lists text encoders only, and there is no VAE quantisation anywhere in the backend. The video path moves the VAE the other way: vae_force_fp32 families load it at float32. So components[2] keeps its full size.

Before and after

Driven through the shipped plan_diffusion_memory, every video family, 16 / 24 / 32 / 64 / 96 / 128 GB, with and without a quantised text encoder, on a unified-memory target (with #8213's refusal rule applied) and on a discrete CUDA target. Free is modelled at 80 percent of RAM. Only cells that move are marked; everything else is byte identical.

Note te_quant_supported requires a CUDA device, so a pre-cast encoder is reachable on unified memory only on an integrated CUDA SoC or Jetson, where memory_kind is unified_memory. Apple Silicon reports mps, never resolves a pre-cast encoder, and is unchanged by this PR in every cell.

Unified memory, pipeline kind, #8213 refusal applied

family te_quant 16G 24G 32G 64G 96G 128G
ltx-2 off REFUSE REFUSE REFUSE REFUSE REFUSE none
ltx-2 fp8 REFUSE REFUSE REFUSE REFUSE REFUSE to none none
wan2.2-ti2v-5b off / fp8 REFUSE REFUSE REFUSE none none none
wan2.2-t2v-a14b off / fp8 REFUSE REFUSE REFUSE REFUSE REFUSE none
hunyuanvideo-1.5 off / fp8 REFUSE REFUSE REFUSE none none none
hunyuanvideo-1.5-720p off / fp8 REFUSE REFUSE REFUSE none none none

Unified memory, GGUF, #8213 refusal applied (ltx-2 rows; no other family moves at any quant)

quant te_quant 16G 24G 32G 64G 96G 128G
Q6_K off REFUSE REFUSE REFUSE REFUSE REFUSE none
Q6_K fp8 REFUSE REFUSE REFUSE REFUSE REFUSE to none none
Q4_K_M off / fp8 REFUSE REFUSE REFUSE REFUSE none none
Q2_K off REFUSE REFUSE REFUSE REFUSE none none
Q2_K fp8 REFUSE REFUSE REFUSE REFUSE to none none none

Discrete CUDA, offload policy (pipeline kind and Q6_K / Q2_K: zero cells change; only Q4_K_M ltx-2 moves)

quant family te_quant 16G 24G 32G 64G 96G 128G
Q4_K_M ltx-2 off model model model group group none
Q4_K_M ltx-2 fp8 model model model group group to none none

Four cells move in total across 360 decisions, all of them ltx-2 with an fp8 text encoder, all of them in the direction of allowing a load that fits.

Discrete CUDA is deliberately included

The over-estimate is wrong on discrete VRAM too, where it costs a needless offload tier rather than a refusal, so it is fixed there as well rather than being special-cased to unified memory. The single discrete cell that moves takes ltx-2 Q4_K_M with an fp8 encoder on a 96 GB card from group offload to fully resident, which is the correct answer: the actual weights are 27.6 GB of DiT plus 13.2 GB of encoder plus 5.5 GB of VAE.

Every load that does not request a text-encoder quant is unchanged bit for bit on every device. The scale is 1.0 in that case, and the regrouped arithmetic was checked to produce identical integers for all five families at both dtype_scale values, so the diff is inert on the default path by construction rather than by inspection.

Does the image path have the same gap?

Partly, and differently, so it is left alone here.

The image plan sizes companions from actual cached bytes (_companion_cache_bytes) rather than the bf16 table, and the pre-cast prefetch skips the base repo's dense encoder shards, so with an fp8 encoder that scan tends to under-count rather than over-count. The one place the bf16 table reaches the planner is estimate_dense_quant, whose companions = (text_encoders_gb + vae_gb) is the same unscaled expression, fed in as companion_override_mib by the GGUF dense-quant re-plan. That is a real but narrower gap on a different kind, reached through a different helper, and it deserves its own change with its own image-side table rather than being folded in here.

Verification

  • Before and after driven through the shipped planner for every family at all six memory sizes, both target classes, pipeline and three GGUF quants, with and without a quantised encoder. The table above is that output.
  • Default loads proven inert: with the scale at 1.0 the new expressions produce integers identical to the old ones for all five families at dtype_scale 1.0 and 2.0.
  • Mutation testing, 12 mutations, all killed, no survivors, suite green again after each restore: scale never applies; scale always applies; requested scheme ignored; fails closed instead of open on an unresolvable pick; constant below the measured maximum; constant below the one-byte-per-parameter floor; constant at bf16; VAE scaled too; transformer scaled instead of the encoder; GGUF companion term drops the scale; pipeline-kind total drops the scale; dense-quant re-plan drops the scale.
  • studio/backend/hub/tests/: 304 passed.
  • studio/backend/tests/: the sorted FAILED and ERROR line sets are compared against a clean checkout of the merge base and are unchanged.

Not verified: no real fp8 encoder was resident-profiled, so the 0.542 ltx-2 figure is on-disk checkpoint bytes rather than a measured RSS delta, and the 0.65 budget carries slack partly to cover that gap. The unified-memory rows assume #8213's refusal rule, which is not merged yet.

danielhanchen and others added 4 commits August 9, 2026 05:41
The video memory plan sizes companions from the family's bf16 component table
and scales them only by the pipeline dtype, so a load that takes its text
encoder pre-cast from a hosted fp8 checkpoint is still budgeted at bf16. For
ltx-2 that encoder is Gemma3-12B at 24.4 GB against a measured 13.2 GB
artifact, so the plan over-states the resident requirement by about 11 GB.

Scale the text-encoder term by TE_PREQUANT_BUDGET_SCALE when te_prequant
resolves a hosted checkpoint for this pick. The VAE keeps its full size: it is
never quantised on this path.

@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: aba421bdd7

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

sources = te_prequant_sources(fam, te_quant_mode = te_quant_mode, target = target)
except Exception: # noqa: BLE001 -- an unresolvable pre-cast just means the dense encoder
return 1.0
return TE_PREQUANT_BUDGET_SCALE if sources else 1.0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Retain the dense budget until the encoder is injected

When an LTX-2 FP8 source is configured but cannot actually be used—for example, its Hub fetch fails, the cached checkpoint is corrupt or incompatible, or a custom base_repo fails checkpoint base validation—te_prequant_sources() still returns the configured source, so this returns 0.65. Later, te_prequant_pipe_kwargs() deliberately falls back to the dense encoder, but the already-created memory plan remains roughly 8.5 GB too small and can choose resident/group placement that OOMs instead of model offload. The reduced budget needs to be gated on the checkpoint having been successfully loaded and injected, not merely resolving a source descriptor.

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.

Correct, and fixed in f25b1ba. The budget cannot be gated on a successful injection at the point it is computed (the plan is built ~100 lines before te_prequant_pipe_kwargs runs), but it does not have to be final there: apply_memory_plan runs after assembly, so the plan is rebuilt at the dense bf16 encoder size when injection returns nothing. That mirrors the existing dense-quant fallback a few lines below, which restores bf16_plan when transformer_quant does not engage. The planning block is now a small local helper so both budgets come from one code path, and test_plan_returns_to_bf16_when_the_pre_cast_encoder_does_not_inject covers the fallback.

@danielhanchen

Copy link
Copy Markdown
Member Author

Full backend suite, baseline against the merge-base and after, same invocation, sorted FAILED/ERROR line sets:

base   71 failed, 18879 passed, 106 skipped, 18 errors   (89 lines)
after  71 failed, 18889 passed, 106 skipped, 18 errors   (89 lines)

only in after (regressions): none
only in base (fixed):        none

Zero set difference, +10 passes, which are the new tests. The 89 pre-existing lines are the transformers vs huggingface-hub pin, identical on both sides.

danielhanchen and others added 4 commits August 9, 2026 11:48
main's strict precision refusal now rejects an explicit text_encoder_quant the
CPU test stub cannot cast, which reaches the pre-cast budget tests even though
they are about the plan and not the cast. Allow the legacy silent fallback in
that helper, the same way the LTX-2.3 wiring test already does.
@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. Nice work!

Reviewed commit: 74f747e194

ℹ️ 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 f12ab6b into main Aug 9, 2026
40 of 44 checks passed
@danielhanchen
danielhanchen deleted the tebudget branch August 9, 2026 12:55
danielhanchen added a commit that referenced this pull request Aug 9, 2026
…ads (#8218)

* Studio: budget a pre-cast fp8 text encoder at its real size

The video memory plan sizes companions from the family's bf16 component table
and scales them only by the pipeline dtype, so a load that takes its text
encoder pre-cast from a hosted fp8 checkpoint is still budgeted at bf16. For
ltx-2 that encoder is Gemma3-12B at 24.4 GB against a measured 13.2 GB
artifact, so the plan over-states the resident requirement by about 11 GB.

Scale the text-encoder term by TE_PREQUANT_BUDGET_SCALE when te_prequant
resolves a hosted checkpoint for this pick. The VAE keeps its full size: it is
never quantised on this path.

* Studio: pin the text-encoder budget on the measured artifacts and the call sites

* Studio: log the text-encoder budget when it is scaled

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Studio: return the video memory plan to bf16 when the pre-cast encoder does not inject

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Merge origin/main into tebudget

main's strict precision refusal now rejects an explicit text_encoder_quant the
CPU test stub cannot cast, which reaches the pre-cast budget tests even though
they are about the plan and not the cast. Allow the legacy silent fallback in
that helper, the same way the LTX-2.3 wiring test already does.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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