Repository navigation
Studio: budget a pre-cast fp8 text encoder at the size it actually loads - #8218
Conversation
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.
There was a problem hiding this comment.
💡 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 |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
Full backend suite, baseline against the merge-base and after, same invocation, sorted FAILED/ERROR line sets: Zero set difference, +10 passes, which are the new tests. The 89 pre-existing lines are the |
…r does not inject
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.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! 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". |
…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>
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_pipelinereadsfam.bf16_components_gb, a(transformer, text_encoder, vae)triple, and forms the companion term ascomponents[1] + components[2]scaled bydtype_scalealone. That number becomescompanion_dense_miband, on the GGUF and single-file kinds, the bulk ofmodel_dense_mib. The pipeline kind has the same problem one branch up, where the total issum(components).There is a dense-quant re-plan immediately below, but it does not cover this. It multiplies
components[0]by_QUANT_STEADY_FACTORand carriescomponents[1] + components[2]through unscaled, and it is gated onkind == "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-FP8publishesLTX-2-text_encoder-FP8.ptat 13.2 GB andVideoFamily.te_prequant_reposwires it up. So the plan over-states the resident requirement by about 11 GB for a load that already works: a measured LTX-2.3Q6_Krun with the fp8 encoder generates a clip and reportsresolved.text_encoder_quant = fp8.Companion sizing dominates the answer for the smaller checkpoints. For wan2.2-ti2v-5b at
Q2_Kthe 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_mibexceeds 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=...)returnsTE_PREQUANT_BUDGET_SCALEor 1.0. It keys onte_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_quantis aload_pipelinerequest 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:
quantize_text_encoderscasts 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.Embeddingtables, the norms inDEFAULT_SKIP_MODULES_PATTERN, the encoder's own_keep_in_fp32_modules(T5'swo) and anlm_headtied 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):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.55was not reused. Its docstring derives it for transformer weights, whereas a text encoder additionally keeps its vision tower,lm_headand T5wodense, and two of the eight measured artifacts exceed it.The VAE is not scaled
quantize_text_encodersiterates_TEXT_ENCODER_ATTRSonly,TE_PREQUANT_COMPONENTSlists text encoders only, and there is no VAE quantisation anywhere in the backend. The video path moves the VAE the other way:vae_force_fp32families load it at float32. Socomponents[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_supportedrequires a CUDA device, so a pre-cast encoder is reachable on unified memory only on an integrated CUDA SoC or Jetson, wherememory_kindisunified_memory. Apple Silicon reportsmps, never resolves a pre-cast encoder, and is unchanged by this PR in every cell.Unified memory, pipeline kind, #8213 refusal applied
Unified memory, GGUF, #8213 refusal applied (ltx-2 rows; no other family moves at any quant)
Discrete CUDA, offload policy (pipeline kind and Q6_K / Q2_K: zero cells change; only Q4_K_M ltx-2 moves)
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_Mwith an fp8 encoder on a 96 GB card fromgroupoffload 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_scalevalues, 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 isestimate_dense_quant, whosecompanions = (text_encoders_gb + vae_gb)is the same unscaled expression, fed in ascompanion_override_mibby 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
dtype_scale1.0 and 2.0.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.