Repository navigation
Studio: run Qwen-Image-2.1 int8/fp8 on 24 GB cards with the transformer resident and the text encoder streamed - #11883
Conversation
…er resident when only the encoder does not fit
…, pin at allocator size, stream the transformer if a late encoder refuses
load_prequant_text_encoder can return None for a cached checkpoint too, and assembly then opens the dense shards on disk, so budget max(pre-cast, scanned dense) whether or not the checkpoint is cached. The prefetch skips covered shards, so the usual case is unchanged, and the resident-denoiser tier only streams encoders, so the 24 GB placement holds even with dense shards cached.
…artly offloaded encoder before falling back WDDM and WSL2 cap page-locked host memory near 1 GiB whatever RAM is free, so pinning a multi-GB encoder or transformer failed part way. On the resident-transformer tier the leaf hooks left by that failure made enable_model_cpu_offload reject the pipeline and the load failed. Those hosts keep the unpinned streaming they had before, and a first-encoder refusal now drops its hooks before whole-module offload takes over.
…' into studio-q21-small-card-placement
…ed on small cards
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. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a058c61048
ℹ️ 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".
| elif _resident_transformer_fits(): | ||
| policy = OFFLOAD_GROUP | ||
| stream_text_encoders = True | ||
| stream_transformer = False |
There was a problem hiding this comment.
Honor explicit whole-module offload in the new tier
When requested_mode is unset and cpu_offload=True, a load whose full model does not fit but whose resident_transformer_floor does now enters this branch with OFFLOAD_GROUP. The legacy override below only changes plans whose policy is OFFLOAD_NONE, so the explicit request no longer forces whole-module offload as the function contract promises; the transformer instead remains resident and can consume most of the device memory. The explicit-offload override should also cover this new automatically selected group tier while continuing to let an explicit memory mode win.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not a change in behaviour. The legacy cpu_offload override has only ever applied to a plan that picked 'none', and that check (required <= 85% of the budget) still runs before this tier, so the set of loads it forces to whole-module offload is the same as on main. A load that reaches this tier was already a group plan on main (transformer streamed, companions resident), which the override also left alone.
| elif _resident_transformer_fits(): | ||
| policy = OFFLOAD_GROUP | ||
| stream_text_encoders = True | ||
| stream_transformer = False |
There was a problem hiding this comment.
Keep denoiser speed gates enabled for encoder-only offload
When this tier sets stream_transformer=False, only the text encoders receive offload hooks and the denoiser's weights and pointers remain fixed. However, load_pipeline still keys the compile-cache fullgraph setting and apply_speed_optims(offload_active=...) on plan.offload_policy != OFFLOAD_NONE; this unnecessarily disables full-graph compilation and makes graph_eligible reject CUDA graphs as "offload active" for the new resident-denoiser path. These denoiser-specific speed gates should use plan_keeps_transformer_resident(plan) so encoder-only streaming does not forfeit the optimizations the resident placement can safely use.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct that fullgraph and the CUDA graph gate still key on offload_policy. Leaving it for a separate, measured change: for Qwen-Image-2.1 the graph wrapper refuses every call anyway (the kv_cache argument is an object, 25/25 refused even fully resident), so here it would only flip fullgraph, and on the tier this applies to the graph memory pool is not priced by the planner, which is exactly where the card is tight. Tracked as an open item.
| total, _available = _system_memory_mib() | ||
| available = _available_system_memory_mib() | ||
| if total is None or available is None: | ||
| return None | ||
| reserve = max(_PIN_RESERVE_MIN_MIB, int(int(total) * _PIN_RESERVE_FRACTION)) |
There was a problem hiding this comment.
Base the pin reserve on cgroup-limited capacity
Inside a memory-limited container, _available_system_memory_mib() is capped by the cgroup, but _system_memory_mib() returns the host's physical total. Computing the 15% reserve from that larger host total can consume the entire container allowance—for example, a 64 GiB container on a 512 GiB host gets a 76.8 GiB reserve and therefore a zero pin budget despite ample available memory. This forces low_cpu_mem_usage=True and loses the measured streamed-weight speedup on common container deployments; the total used for the reserve should be capped by the same cgroup capacity as the available reading.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Right, the available reading is capped by the cgroup but the reserve was sized from the host total, so a 64 GiB container on a 512 GiB host got no pin budget. Fixed in 5f4b613: the total used for the reserve is capped by the cgroup capacity. Regression test: test_the_pin_reserve_is_sized_from_the_container_not_the_host (fails without the fix); the existing budget test now pins the cgroup reading so it does not depend on the host.
| companion_override_mib = estimate.companions_mib, | ||
| text_encoder_override_mib = estimate.text_encoders_mib, | ||
| text_encoder_quant = text_encoder_quant, |
There was a problem hiding this comment.
Reprice the in-place pipeline quantization replan
This replan still passes estimate.companions_mib and estimate.text_encoders_mib, which describe the dense encoders, even when text_encoder_quant makes assembly inject a much smaller hosted pre-cast encoder. Passing text_encoder_quant here does not correct those values because _plan_memory only applies _precast_text_encoder_mib when companion sizes came from cache, not when caller overrides are present. Consequently an in-place quantized pipeline can be assigned encoder streaming or have quantization declined on a card where its actual pre-cast weights fit fully resident; this call should use _candidate_companion_overrides, as the other candidate replans now do.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, that was the one candidate replan still passing the dense encoder terms. Fixed in 5f4b613: it now uses _candidate_companion_overrides like the GGUF and seed replans. Regression test: test_a_pipeline_quant_replan_prices_the_encoder_the_load_opens (fails without the fix).
…lans; size the pin reserve from the cgroup capacity
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
# Conflicts: # studio/backend/tests/test_diffusion_backend.py
# Conflicts: # studio/backend/core/inference/diffusion_memory.py
|
@codex review |
|
@codex security review |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
|
Codex Review: Didn't find any major issues. Bravo. 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". |
# Conflicts: # studio/backend/core/inference/diffusion.py # studio/backend/core/inference/diffusion_memory.py # studio/backend/tests/test_diffusion_backend.py # studio/backend/tests/test_diffusion_memory.py # studio/backend/tests/test_diffusion_pipeline_prequant.py
|
@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". |
…er resident and the text encoder streamed (unslothai#11883) * Studio: price the hosted pre-cast text encoder and keep the transformer resident when only the encoder does not fit * Studio: pin the streamed-encoder tiers when host RAM allows, transformer first * Studio: price only the encoder folders a pre-cast checkpoint replaces, pin at allocator size, stream the transformer if a late encoder refuses * Format * Studio: never price a cached pre-cast encoder below its dense shards load_prequant_text_encoder can return None for a cached checkpoint too, and assembly then opens the dense shards on disk, so budget max(pre-cast, scanned dense) whether or not the checkpoint is cached. The prefetch skips covered shards, so the usual case is unchanged, and the resident-denoiser tier only streams encoders, so the 24 GB placement holds even with dense shards cached. * Studio: do not pin streamed modules on Windows or WSL, and unhook a partly offloaded encoder before falling back WDDM and WSL2 cap page-locked host memory near 1 GiB whatever RAM is free, so pinning a multi-GB encoder or transformer failed part way. On the resident-transformer tier the leaf hooks left by that failure made enable_model_cpu_offload reject the pipeline and the load failed. Those hosts keep the unpinned streaming they had before, and a first-encoder refusal now drops its hooks before whole-module offload takes over. * Studio: keep a torchao denoiser resident with the text encoder streamed on small cards * Studio: tests for the torchao resident-transformer placement * Studio: quote the encoder-streamed floor when a torchao build is declined * Studio: leave the pipeline replan's encoder sizing as it was * Format * Price the in-place pipeline quant replan's encoder like the other replans; size the pin reserve from the cgroup capacity * Studio: trim comments * Studio: do not add a pre-cast encoder on top of a bf16 table-priced pipeline * Format * Trim comments * Format * Keep main's native-quant tests whole beside this PR's placement tests
Builds on #11802 and contains its commits (merge #11802 first; this diff then shrinks to this PR's own change). #11802 adds the memory tier this PR uses: the transformer stays on the GPU and only the text encoders stream.
Why
Qwen-Image-2.1 on a 24 GB card, measured on a real Colab L4 (22.03 GiB), 1024x1024, 25 steps, today's main:
int8orfp8The INT8 transformer is 7.1 GiB and the fp8 text encoder 8.75 GiB. Together they do not fit a 24 GB card next to the 1024x1024 VAE decode, but the transformer alone does, and the encoder runs once per image, before step 0. #11802 already plans that placement for GGUF transformers. torchao weights survive being placed on the GPU once (the loader already relies on that for resident loads); what they do not survive are the per-forward hooks that stream the transformer itself. This placement never puts a hook on the transformer, so there is no reason to refuse it.
What changes
diffusion_memory.plan_keeps_transformer_resident(plan): true for a resident plan, and for the group tier withstream_transformer=False. Every place that allowed a torchao build only whenoffload_policy == "none"now asks this instead:pipelinepick after its replan,pipelinepick (both the pre-download probe and the load-time plan),te_prequant_budget_scale), and passes the planner the encoder share so it can find the tier at all.Results
Real L4 (22.03 GiB), Colab
Seconds per image after the first two (compile, and the first new prompt length), 1024x1024, 25 steps, CFG 1.
24 GiB cap simulated on a shared B200
Same renders, the VRAM cap simulated by patching
torch.cuda.mem_get_infoand capping the allocator withset_per_process_memory_fraction(1.2 GiB left for desktop and context). The GPU was shared with other jobs, so main's streamed rows are noisy; medians of 4 renders.The encoder pass costs 0.25 to 0.58 s per image on this host (pinned by #11802's gate).
16 GB
Nothing changes on 16 GB, on purpose. The resident-transformer floor for int8 is 18.3 GiB against a 12.8 GiB budget, so int8 / fp8 is still declined and the GGUF keeps streaming (#11802). Forcing the placement anyway rendered one image at a 13.73 GiB peak against a 14.8 GiB allocator cap and ran out of memory on the second from fragmentation, so the decline is the plan matching reality, not caution.
Tesla T4 (16 GB), Colab
Precision int8 is refused on the T4 by main and by this PR alike, before any memory planning: "this device cannot run a dense torchao quant (it needs a CUDA GPU in bf16)". The T4 (sm 7.5) has no bf16, so on T4 the torchao paths this PR opens do not apply, and the GGUF keeps #11802's placement. A second T4 session for the GGUF and Auto picks (which on a T4 need the dense bf16 text encoder, since the fp8 one needs sm 8.9) lost the load process within one to two minutes with no output, on main and on this branch alike, so there is no T4 timing here; I did not diagnose it further.
Accuracy
Same weights and the same kernels: the placement only decides where the text encoder lives between calls. With deterministic kernels (cuDNN benchmark off, native attention), the encoder-streamed placement and the fully resident one gave bit-identical images for all 4 prompts after the warm-up render. The warm-up render itself is not reproducible across processes on either placement (LPIPS 0.005 to 0.006 between two runs of the same placement), and neither is a repeat of one placement with cuDNN benchmark on, so the table above is timing only.
The prompt embeddings themselves are bit-identical between the streamed and the resident text encoder (compared with
torch.equalon the same prompts), so the denoiser sees exactly the same input either way.Coordination
stream_transformer=Falsetier is what this PR lets torchao use.plan_keeps_transformer_residentrather thanoffload_policy != "none", so a plan that keeps the transformer resident stays on torchao.Tests
test_diffusion_memory.py: the pin reserve sized from the container; the placement predicate over every policy (including plans that predate the field), telling torchao weights from GGUF and plain ones, a refusing encoder never sending a torchao transformer to whole-module offload or streaming it, and a dense transformer keeping the old fallback.test_diffusion_backend.py: the in-place pipeline replan pricing the encoder the load opens; a pipeline quantising where only the encoders stream and staying dense where the transformer would stream; a GGUF pick building the quant where only the encoders stream and declining where it would stream; a resident GGUF plan sizing the checkpoint that replaces it, and declining one that would stream; the encoder pricing of the candidate overrides.test_diffusion_pipeline_prequant.py: the seed probe and the load-time seed accepting the encoder-streamed plan and still declining a streamed transformer.diffusion.pyanddiffusion_memory.pyreverted, 11 of the 15 new tests fail; the 4 that pass are the negative controls (unchanged behaviour).test_diffusion_backend.py,test_diffusion_memory.py,test_diffusion_auto_policy.py,test_video_backend.py,test_diffusion_pipeline_prequant.py,test_diffusion_speed.py,test_diffusion_te_prequant.py: 1170 passed.test_diffusion_memory.pyalso passes on torch 2.6 (129 passed).Not measured