Repository navigation
Studio: late pins for streamed denoisers on low-RAM hosts, and dense FLUX.3 on 12 GB hosts - #17649
Conversation
…t16 builds A float16 FLUX.3 load (fp16-only cards such as T4) converted the bf16 text encoder and transformer to float16 on the host, a private copy of each (about 20 GB for klein, more while converting), which killed the process on a 12.7 GB host. The float16 build now reads both components as stored (memory mapped, no copy) with the exact per-call cast the float32 build already uses. - flux3_storage_dtype returns bf16 for float16 CUDA loads too (UNSLOTH_DIFFUSION_FLUX3_HALF_STORAGE=0 restores the widened load). - The transformer is read as stored only when a native quant rebuilds its Linears, or when storage saves memory (float32); a dense float16 transformer still loads widened so it never casts per step. - The native quantiser rounds a manual-cast weight to the compute dtype before quantising, so the int8 codes match a widened load bit for bit. - After a native quant on an fp16-only card, the Linears left dense become plain float16 modules again, exactly what a float16 load holds.
…r's weight Reading .weight on a native quantised Linear dequantises it, so the undo pass after the FLUX.3 klein int8 build dequantised all 228 quantised Linears (about 22 s of the load and a dense transient on the host). Only manual-cast modules are inspected now.
…nce the denoiser is placed On a 12 GB host the image load decides what to pin while it still holds the denoiser's host copy, so a FLUX.3 klein int8 load on a T4 pinned none of its 7.5 GiB text encoder. The encoder then streamed from its memory-mapped file, and since its streamed groups are slightly larger than the page cache left, every prompt re-read about 6.5 GB from disk (21 to 27 s of encode per image). When the plan-time budget misses, the streamed encoder now defers its pin with a cap read when the pinner starts: the load's freed heap is returned to the OS first, the usual max(4 GiB, 15%) reserve is kept, and only the prefix of offload groups that fits is pinned. Whole pins, the small-host route and the plan-time prefix are unchanged. Kill switch: UNSLOTH_DIFFUSION_LATE_IMAGE_ENCODER_PIN=0.
…once the load returns On a 12 GB host the small-host route never pins: its int8 denoiser lives in a memory-mapped scratch file and every streamed group is copied from there on each step. Qwen-Image-2.1 int8 on a T4 streams 5 GB of its denoiser per step, more than the page cache left, so every step re-read it from disk (134 GB per image, 23 s per step) while 7 GB of host RAM sat free after the load. The route now pins the leading streamed (non-resident) denoiser groups with a cap read when the background pinner starts: the load's freed heap is returned to the OS first and the usual max(4 GiB, 15%) reserve is kept. Resident groups spend none of the cap and keep their host copy. The bytes are the same, only the host copy moves into pinned memory. Kill switch: UNSLOTH_DIFFUSION_LATE_DENOISER_PIN=0.
The streaming tier (a denoiser or encoder larger than the card) decided its pins only at plan time, while the load still held its host copies. On a 12 GB host that pinned nothing: the encoder and every streamed denoiser group were copied from pageable or memory-mapped host memory on each use, and re-read from disk once they outgrew the page cache. An image load whose plan-time pin budget misses now does what the group tier does: the denoiser pins the leading streamed groups that fit when the background pinner starts, then the encoders pin the prefix still free, each cap read after the load's freed heap is returned to the OS and keeping the usual max(4 GiB, 15%) reserve. Whole pins, video loads (their own background pins) and the small-host encoders are unchanged. Kill switches: UNSLOTH_DIFFUSION_LATE_DENOISER_PIN=0 and UNSLOTH_DIFFUSION_LATE_IMAGE_ENCODER_PIN=0.
…dened copy does not fit host RAM A dense FLUX.3 load on an fp16-only card (transformer_quant none on a T4) still converted the bf16 transformer to float16 on the host. That conversion peaks at about 1.6x the stored bytes, about 18 GB for klein's 11.1 GB transformer, and on a 12.7 GB Colab T4 the load stalled with no output for an hour. By default the transformer now widens on the host only when that copy fits the available host RAM beside the small-host reserve; otherwise it reads bf16 as stored and casts each Linear per call, which the text encoder already does. The float16 weights each call sees are the ones the widened load holds, so the output is unchanged. Hosts with the room, quantised builds and an explicit transformer_storage keep their current load. Kill switch: UNSLOTH_DIFFUSION_FLUX3_DENSE_STORAGE=0 (UNSLOTH_DIFFUSION_HOST_RAM_CHECK=0 also keeps the widened load).
… unpinned Queuing the late prefix pin is an optimisation. On the streaming tier any exception during setup is fatal for the load, so a denoiser whose offload groups cannot be read (a duck-typed module) failed the load instead of streaming unpinned as before. The queueing now logs and returns instead.
…ncoders on any image load The late denoiser pin covered only the small-host route. A dense FLUX.3 klein on a 12.7 GB T4 takes the ordinary group tier: 2.9 GB of its transformer streams every step, the plan pinned nothing, and the late encoder pin then took the 6.5 GiB free after the load, so each step re-read the streamed blocks from disk (217 s per image). Any image load whose plan-time budget misses the denoiser now queues the denoiser's prefix pin first, then the encoder's; the encoder cap subtracts only the denoiser groups that still stream (resident groups are not owed). Whole pins and video loads are unchanged.
Codex Review Summary: ✅ CompletedThis 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: c6da3e339b
ℹ️ 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".
| fast = True, | ||
| byte_cap = _late_image_encoder_pin_cap(pipe, module, device), | ||
| skip_resident = True, |
There was a problem hiding this comment.
Require registered pin support before using the late byte cap
On ROCm, or when UNSLOTH_VIDEO_FAST_PIN=0, _defer_pinning silently changes fast=True to False, but the new denoiser pinner still selects groups using page-rounded tensor bytes. Its fallback calls Tensor.pin_memory(), whose power-of-two allocations can substantially exceed that cap (the existing test_image_encoder_background_pin.py demonstrates 3,000,000 bytes requiring 4 MiB). This can consume the host reserve on the low-RAM machines this change targets. Unlike _encoder_background_pin_wanted, the new denoiser path does not require _fast_pin_supported; add that gate or price fallback allocations at their actual rounded size.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed: _late_denoiser_pin_enabled now also requires _fast_pin_supported(onload), the same gate _encoder_background_pin_wanted uses, so on ROCm or with UNSLOTH_VIDEO_FAST_PIN=0 the denoiser is not late-pinned against the page-rounded byte cap. Both call sites pass the onload device; test_no_late_denoiser_pin_without_the_registered_pin covers it.
…r pin does [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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". |
…ndeferrable encoders on their old route UNSLOTH_DIFFUSION_GROUP_OFFLOAD_PIN=0 never pins, but the late image route still queued an encoder pinner with a zero cap: it dropped resident host copies, trimmed the heap and logged a pin plan. A pipe with no deferrable text encoder (none, or torchao weights) logged the late pin plan without queuing anything. Both now keep their old route and log. [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ed5ea0696d
ℹ️ 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".
| device, | ||
| logger, | ||
| fast = True, | ||
| byte_cap = _late_image_encoder_pin_cap(pipe, module, device), |
There was a problem hiding this comment.
Reserve the budget already promised to deferred encoders
When _streamed_pin_plan returns (False, True), the encoder is still queued for an uncapped whole pin, but this new denoiser cap only subtracts earlier pinners, not the encoder queued after it. For example, with a 4 GiB pin budget, a 10 GiB denoiser and a 2 GiB encoder, the denoiser can select 4 GiB before the encoder starts allocating, after which the encoder pins another 2 GiB. Both group and streaming offload permit this combination, so the background workers can consume the host reserve and cause memory pressure or OOM on the low-RAM hosts this change targets. Subtract the committed encoder allocation from the denoiser's late cap, or make those encoder pins share the late budget.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed: the late cap now also subtracts the outstanding bytes of pinners queued after it whose cap the plan committed (a whole pin, byte_cap None, or a fixed cap), so with the encoder whole-pinned after the denoiser the denoiser sees the budget less the encoder's bytes. Later late-capped pinners still price themselves. Two tests cover the whole and fixed-cap cases and fail on the previous head.
|
@codex review |
1 similar comment
|
@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". |
# Conflicts: # studio/backend/core/inference/diffusion.py [skip ci]
…sformer_quant none (#17667) Stacked on #17649 (base branch `studio-lowram-sweep`). Retarget to `main` once that merges. ## Summary This follows up on #17649 with fixes for two of the low-RAM problems that sweep found on a standard Colab T4 (12.7 GB host RAM, 15 GB card). It also records why the third one, the Qwen-Image-2.1 encode, is left as it is. 1. **First video load re-read its text encoder from disk on every clip.** The pin plan runs while the load still holds its host copies, and a first load in a Studio home (still writing its cast cache) skipped the late prefix pin a returning load takes. Wan2.1-T2V-1.3B's 12.5 GiB UMT5 therefore pinned nothing. A first load now queues the same late prefix pin: the cap is read when the pinner starts, after the load has returned its freed heap, against the host RAM free then less the existing max(4 GiB, 15%) reserve. The exact page-rounded pricing stays gated on the finished cast-cache write, as before. 2. **`transformer_quant=none` on a small host rendered int8 while status said `off`.** The small-host route stored every DiT as per-row int8 whatever the request (`_apply_small_host_route`). An explicit none / off now keeps the denoiser at its stored bf16 weights, memory-mapped, and casts each Linear per call, which the route already does for the streamed text encoders. Where the route still stores int8 (an auto request, or the kill switch), status, the resolved record and the image metadata now report int8 with the reason. Kill switches: `UNSLOTH_VIDEO_FIRST_LOAD_ENCODER_PREFIX=0` (fix 1), `UNSLOTH_DIFFUSION_SMALL_HOST_DENSE=0` (fix 2). ## Results on a standard Colab T4 (12.7 GB host RAM) Each arm ran on the same VM as its comparison arm, with the same seeds. Times are per image or per clip, with every render listed. **Wan2.1-T2V-1.3B, 33 frames at 832x480, 20 steps, 2 clips per load** | load | tree | encoder pin | s/clip | encode s | disk GB/clip | |---|---|---|---|---|---| | first load | #17649 | none | 352.9 / 291.2 | 115.7 / 67.7 | 27.4 / 13.5 | | first load | this PR | 6.4 GiB | 325.2 / 279.8 | 77.4 / 57.0 | 21.8 / 8.7 | | first load, repeat in a fresh home | this PR | 6.4 GiB | 327.9 / 334.5 | 77.0 / 110.5 | 22.0 / 8.7 | | second load, same home | #17649 | 6.2 GiB | 324.2 / 260.5 | 77.5 / 37.9 | 22.1 / 8.9 | | second load, same home | this PR | 6.2 GiB | 321.7 / 260.7 | 74.9 / 37.9 | 21.9 / 8.6 | - On a first load, each clip now reads about 5 GB less from disk, and the first clip's encode drops from 116 s to 77 s. - The second clip of a first load overlaps the denoiser's deferred cast-cache write, so its encode varies between 57 s and 110 s with the same disk reads. That variance is present on both trees. - A second load is unchanged. - Every clip's frames are md5-identical across all five loads, and to a 51 GB high-RAM T4. **Qwen-Image-2.1-Turbo, `transformer_quant=none`, 1024x1024, 8 steps** | tree | denoiser actually run | status | s/img | image md5 | |---|---|---|---|---| | #17649 | per-row int8 (small-host route) | `off` | 68.9 / 69.2 | equal to the int8 output | | this PR | bf16 memory-mapped, cast per call | `off` | 276.6 / 276.5 | equal to the 51 GB T4's dense output | | this PR, repeat | same | `off` | 264.7 / 262.4 | equal to the 51 GB T4's dense output | | this PR, auto request | per-row int8 | `int8` (was `off`) | 60.3 | equal to the int8 output | Honouring the request costs time: groups that fit neither the card nor the pinned host RAM are read from the file on every step, about 62-66 GB per image. The load log says so and names auto / int8 as the faster choice. **Qwen-Image-2.1 encode, about 29 s per image from the memory-mapped text encoder.** I measured the options before changing anything: - **Splitting the late-pin room between the denoiser and the encoder:** I did not build it. - The denoiser's streamed groups are read every step, 25 times per image. The encoder's are read once per prompt. - With the denoiser pinned first, the whole image reads 7.3-7.6 GB from disk, almost all of it in the encode, so the denoise phase reads well under 1 GB. - Each GiB moved from the denoiser pin to the encoder would save at most 1 GiB of encoder reads per image, and would put that GiB back into the per-step stream. - Without the denoiser pin, the same configuration thrashed on one VM (576 s/img, 131 GB/img in the #17649 sweep). - **Keeping encoder groups on the card in the VRAM a render leaves unused, sized after the first render:** I built and measured this, then did not ship it. | Qwen-Image-2.1 int8, 25 steps, same VM | s/img (renders 2-4) | encode s | disk GB/img | |---|---|---|---| | #17649, two loads | 145.4 / 144.0 / 144.3, 145.0 / 144.7 / 143.9 | 29.6-30.5 | 7.3-7.6 | | this PR (the path is unchanged) | 144.5 / 143.9 / 143.5 | 29.5 | 7.3-7.4 | | encoder kept in the unused VRAM, two loads | 134.5 / 135.1 / 135.4, 137.6 / 137.4 / 137.4 | 21.9-24.5 | 5.2-5.5 | - It saves about 6% with md5-identical images. On Qwen-Image-2.1-Turbo it saves about 4% (71.4 to 68.5 s/img). - Every later render then ran the card to its limit, and the allocator logged out-of-memory retries. - Its recorded activation peak rose from 6.7 GiB to 10.1 GiB. A later load in the same home would read that peak and place less of the denoiser on the card. - To be safe it needs to be tied into the decode-lend accounting, which is a separate change. - **Kept:** the denoiser-first late pin from #17649. **High-RAM T4 (51 GB), no-regression check:** this code does not engage there. | model | #17649 s/img or s/clip | this PR | md5 | |---|---|---|---| | Qwen-Image-2.1-Turbo none | 36.2 / 38.8 | 38.8 / 37.5 | identical | | Qwen-Image-2.1-Turbo int8 | 33.7 / 31.8 | 32.7 / 32.3 | identical | | Wan2.1-T2V-1.3B | 258.6 | 256.3 | identical | ## Tests - `tests/test_video_encoder_exact_pin.py` covers three cases: - a first load queues the late prefix with a callable cap; - both kill switches restore the old first load; - the cap returns freed heap before reading the budget. - `tests/test_small_host_dense_denoiser.py` covers the following: - the routing decision: no int8, no spill, mapped route size; - bit-exact equality of a mapped DiT with the converted one, repeated calls included; - the LoRA refusal; - int8 status reporting; - load plumbing. - The related backend suite was run on this branch and on #17649, two 8-worker runs side by side on one CPU host. - Both trees have failures there: 91 on this branch, 104 on #17649. Most failures are in the same files on both trees, and the counts move under load. - 38 tests failed only on this branch. Rerun alone, all 367 tests in those files pass (40 skipped). - The new and touched test files pass.
Stacked on #17644. I swept Studio image and video loads on a standard Colab T4 (12.7 GB host RAM, 15 GB card) beyond FLUX.3 klein int8. That turned up three problems; this PR fixes the first two and leaves the third:
transformer_quant=nonethe VM stops responding and gives no output for an hour. The float16 build still converts the 11.1 GB bf16 transformer on the host, and that conversion peaks at about 1.6x the stored bytes.Changes
Each change is its own commit:
_apply_streaming_offloaddecided its pins only at plan time. Image loads there now pin the denoiser prefix late, then the encoder prefix late, as the group tier does. Unchanged:Kill switches
UNSLOTH_DIFFUSION_LATE_DENOISER_PIN=0turns off 1, 2 and 5.UNSLOTH_DIFFUSION_PARTIAL_ENCODER_PIN=0also turns them off.UNSLOTH_DIFFUSION_LATE_IMAGE_ENCODER_PIN=0now covers the streaming tier too.UNSLOTH_DIFFUSION_FLUX3_DENSE_STORAGE=0turns off 3, as doesUNSLOTH_DIFFUSION_HOST_RAM_CHECK=0.A pin only moves a host copy into pinned memory, so the bytes are unchanged, and it never goes past the existing pin reserve.
Standard Colab T4 sweep (#17644 head, 1024x1024, encode / denoise per timed image)
offload=group), and Qwen-Image-2.1 took the small-host route. No model reached_apply_streaming_offloadon a real T4. I exercised that tier by capping the card at 7 GiB (see below).Results, standard T4, head-before vs this PR on the same VM
LATE_DENOISER_PIN=0vs on (ABAB)bc8107ba,952ea0be)Notes on these rows:
5003c0b3,9b896f69) equal the widened load on a high-RAM T4, so reading as stored is bit-identical on the GPU.High-RAM T4 (51 GB), head vs this PR
5003c0b3,9b896f69)bc8107ba,952ea0be)6caddcb2,a2821dca)The plan pins every streamed component up front there, so none of the new code runs.
Tests
studio/backend/tests/test_small_host_denoiser_pin.py: routing on both tiers, kill switches, resident groups skipped, the cap read at pin start, the encoder cap net of the denoiser's owed bytes, and that whole pins, video and torchao denoisers are unchanged.studio/backend/tests/test_diffusion_flux3_dense_storage.py: the host RAM gate, its kill switches, and that explicit choices are unchanged.test_diffusion_flux3_manual_cast.py, which checks the new helper. The float16 manual cast stays bit-identical to the float16 load (test_diffusion_flux3_half_storage.py).diffusion_memory, group offload, background pins, FLUX.3 or the small-host route.test_the_pin_budget_leaves_the_reserve_free,test_mirrored_base_still_trips_the_flux2_shape_guard, and the twotest_integrated_gpu_no_host_pinchecks, with identical messages.Not covered
_video_exact_pinskeeps the default plan while the cast cache is written). Its UMT5 then streams from disk on every clip: 90-115 s of a 342 s clip, 13-27 GB read. I did not change the video route or test a second load in the same home.transformer_quant=none: on this host they take the small-host int8 route. The output is md5-identical to int8, while status reportstransformer_quant=off. This is unchanged here.