Skip to content

Studio: late pins for streamed denoisers on low-RAM hosts, and dense FLUX.3 on 12 GB hosts - #17649

Merged
danielhanchen merged 20 commits into
mainfrom
studio-lowram-sweep
Nov 13, 2026
Merged

danielhanchen merged 20 commits into
mainfrom
studio-lowram-sweep

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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:

  1. Dense FLUX.3 klein never finishes loading. With transformer_quant=none the 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.
  2. Streamed denoiser blocks are re-read from disk on every step. On a 12 GB host the image plan never pins the denoiser. The late encoder pin from Studio: pin a streamed image text encoder once the denoiser is placed (low-RAM hosts) #17644 then takes the RAM that is free after the load.
    • Dense klein, given a load that works: 52 GB read from disk per image, 217 s/img.
    • Qwen-Image-2.1 int8: up to 134 GB read from disk per image, 576 s/img. Whether it thrashes varies by VM and by prompt.
  3. The Wan2.1 text encoder streams from disk on a first load (only measured, not fixed here; see the end).

Changes

Each change is its own commit:

  1. Small-host route pins its streamed denoiser prefix late. The small-host route keeps its int8 denoiser in a memory-mapped scratch file and never pinned it. It now queues a background pin for the leading streamed (non-resident) denoiser groups.
    • The cap is read when the pinner starts, after the load's freed heap is returned to the OS.
    • It keeps the usual max(4 GiB, 15%) host reserve.
    • Resident groups spend none of the cap.
  2. Streaming tier gets the same late pins. _apply_streaming_offload decided 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:
    • video loads, which have their own background pins;
    • whole pins;
    • the small-host encoders.
  3. A dense float16 FLUX.3 transformer reads as stored when the widened copy does not fit.
    • It widens on the host only when about 1.6x its stored bytes, plus the small-host reserve, fits in available RAM.
    • Otherwise it reads bf16 as stored and casts each Linear per call, as the text encoder already does.
    • Each call sees the same float16 weights the widened load holds.
  4. A late denoiser pin that cannot be queued is never fatal. On the streaming tier any exception during setup fails the load. The pin is an optimisation, so a failure now logs and leaves the denoiser unpinned.
  5. The group tier pins the denoiser prefix before the encoders on any image load, not only on the small-host route. The encoder's late cap subtracts only the denoiser groups that still stream.

Kill switches

  • UNSLOTH_DIFFUSION_LATE_DENOISER_PIN=0 turns off 1, 2 and 5. UNSLOTH_DIFFUSION_PARTIAL_ENCODER_PIN=0 also turns them off.
  • UNSLOTH_DIFFUSION_LATE_IMAGE_ENCODER_PIN=0 now covers the streaming tier too.
  • UNSLOTH_DIFFUSION_FLUX3_DENSE_STORAGE=0 turns off 3, as does UNSLOTH_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)

model / mode result load s s/img encode s denoise s disk GB/img peak PSS GiB min MemAvailable GiB
FLUX.3 klein int8 loads 212 37.8 (0.8-1.2 in #17644) 9.2 3.3
FLUX.3 klein fp8 loads 349 (incl. download) 53.3 8.9 3.7
FLUX.3 klein dense VM unresponsive, no output for 60 min (two VMs) - - - - - - -
FLUX.3 klein-base int8 (50 steps, CFG) loads 419 242.8 2.7-3.9 238 0.2-0.6 9.8 2.8
Qwen-Image-2.1 int8 (VM 1) loads 485 576.0 29-30 544 131-134 8.8 6.9
Qwen-Image-2.1 int8 (VM 2) loads 419 147.3 (warm-up 396) 29 116 (warm-up 358) 8.5 (warm-up 81.6) 10.2 6.7
Qwen-Image-2.1-Turbo int8 loads 432 73.2 29 42 8.3 9.8 6.5
Wan2.1-T2V-1.3B, 33 frames 832x480, 20 steps loads 560 (incl. download) 342 s/clip 90-115 222-231 13-27 9.2 4.2
MiniMax-H3-Image refused before the weights download - - - - - - -
  • Offload tier: every load took the group tier (offload=group), and Qwen-Image-2.1 took the small-host route. No model reached _apply_streaming_offload on a real T4. I exercised that tier by capping the card at 7 GiB (see below).
  • MiniMax-H3-Image: the smallest tier (int8 DiT + int8 encoder) needs about 46 GB of host RAM and refuses with a clear message after fetching only configs and the VAE. There is no GGUF / sd.cpp tier for it.

Results, standard T4, head-before vs this PR on the same VM

model / mode before s/img after s/img disk GB/img before / after md5
FLUX.3 klein dense, LATE_DENOISER_PIN=0 vs on (ABAB) 217.6 / 214.6 66.0 / 65.5 52 / 4.2 identical in all four runs
Qwen-Image-2.1 int8 147.3 (warm-up thrashed: 396) 140.1 / 140.1 8.5 (warm-up 81.6) / 7.3 identical
Qwen-Image-2.1-Turbo int8, VM A 71.65 / 72.82 69.22 / 69.41
Qwen-Image-2.1-Turbo int8, VM B 72.6 / 74.6 / 75.9 71.1 / 71.2 / 71.2 7.2-8.4 / 6.8-6.9 identical
FLUX.3 klein int8 (the #17644 case) 37.79 37.84 (same as high-RAM)
FLUX.3 klein-base int8 242.8 246.2 0.2-0.6 / 0.3-0.6 identical
FLUX.3 klein int8, card capped at 7 GiB (streaming tier) 120.2 / 123.2 137.5 / 124.4 identical (bc8107ba, 952ea0be)

Notes on these rows:

  • Dense FLUX.3:
    • The head itself never loads dense klein on this host, so the before arm keeps change 3 and turns off the late denoiser pin with its kill switch.
    • The md5s (5003c0b3, 9b896f69) equal the widened load on a high-RAM T4, so reading as stored is bit-identical on the GPU.
  • Qwen-Image-2.1: the gain depends on the VM. Without the pin the streamed set (5 GB) sometimes stays in the page cache and sometimes thrashes:
    • On another VM every image read 131-134 GB (576 s/img).
    • The fixed tree pins 3.9 GiB of it and ran 140-146 s/img with 7.3-8.3 GB read per image on both VMs where it ran.
  • klein-base: its logs are identical in both arms (the transformer is resident, so the new pinner has nothing to pin). The 3 s gap is single-run variation.
  • Streaming tier: both pinners ran but read a 0 GiB cap, because the host was already under the reserve (min MemAvailable 0.7 GiB in both arms). The change engages there but has nothing to pin, so both arms do the same work: 7.3 GB read per image and identical images. Denoise times range from 73 to 104 s within both arms, so this host cannot resolve the 137.5 s run from noise.

High-RAM T4 (51 GB), head vs this PR

model / mode head s/img this PR s/img md5
FLUX.3 klein dense 43.74 43.51 identical (5003c0b3, 9b896f69)
FLUX.3 klein int8 37.95 37.98 identical (bc8107ba, 952ea0be)
Qwen-Image-2.1-Turbo int8 30.49 30.46 identical (6caddcb2, a2821dca)

The plan pins every streamed component up front there, so none of the new code runs.

Tests

  • New files:
    • 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.
  • Updated: 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).
  • Wider suite: every backend test file touching diffusion_memory, group offload, background pins, FLUX.3 or the small-host route.

Not covered

  • Wan2.1-T2V-1.3B: on a first load in a Studio home the encoder pins nothing (_video_exact_pins keeps 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.
  • Qwen-Image-2.1 encode: the encoder still streams 8.9 GB from its file on every prompt, about 30 s per image. After the denoiser pin there is no room left to pin it.
  • Qwen-Image-2.1 / Turbo with transformer_quant=none: on this host they take the small-host int8 route. The output is md5-identical to int8, while status reports transformer_quant=off. This is unchanged here.
  • Streaming tier: it was exercised only with a simulated 7 GiB card, where the host had no room to pin. That configuration also runs the host down to 0.7 GiB MemAvailable without this PR.
  • Not run: LTX-2.5, L4, A100, ROCm, Windows and macOS.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T12:43:22.525851Z 8f95d05 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 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".

Comment on lines +7710 to +7712
fast = True,
byte_cap = _late_image_encoder_pin_cap(pipe, module, device),
skip_resident = True,

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 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 👍 / 👎.

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.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 5afa8658be

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

…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]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 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),

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 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 👍 / 👎.

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.

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.

@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. Another round soon, please!

Reviewed commit: 8f95d05995

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

Base automatically changed from studio-lowram-te to main November 13, 2026 13:18
@danielhanchen
danielhanchen merged commit 8160488 into main Nov 13, 2026
@danielhanchen
danielhanchen deleted the studio-lowram-sweep branch November 13, 2026 13:19
danielhanchen added a commit that referenced this pull request Nov 13, 2026
danielhanchen added a commit that referenced this pull request Nov 13, 2026
# Conflicts:
#	studio/backend/core/inference/diffusion.py

[skip ci]
danielhanchen added a commit that referenced this pull request Nov 13, 2026
…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.
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