Skip to content

Studio: load FLUX.1 and Qwen-Image on fp16-only GPUs with small host RAM - #12480

Merged
danielhanchen merged 15 commits into
mainfrom
studio/small-host-load
Oct 2, 2026
Merged

danielhanchen merged 15 commits into
mainfrom
studio/small-host-load

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Problem

On a fp16-only card with a modest host (a Kaggle T4: 2x T4, 31 GB RAM), FLUX.1-schnell and Qwen-Image never load. The process is killed for host RAM before the first step.

  • Where the RAM goes: all of the host peak is inside pipeline.from_pretrained(torch_dtype=float16), before the memory planner runs. Each bf16 weight converted to fp16 becomes a private in-RAM copy instead of staying memory-mapped. Measured by sampling RSS every 20 ms on each load step:
    • FLUX.1: transformer +22.7 GB, T5 +11.0 GB, VAE and CLIP +0.4 GB, for 35.1 GB in total.
    • Qwen-Image: text encoder +15.8 GB and transformer +39.0 GB, for 56 GB.
    • The same FLUX load at the stored bf16 stays memory-mapped at 0.56 GB.
  • Qwen-Image in fp16 also renders all-black images (the DiT overflows fp16). Fixing the RAM alone would not have been enough.

Change

New diffusion_small_host.py, a load route for this case.

  • When it engages: only for a CUDA load whose large components are stored bf16 but must be converted (fp16 or fp32 compute), and only when the converted load would not fit in available host RAM. The available-RAM figure respects an enforcing cgroup limit. Loads that fit, and every bf16-compute load, are unchanged.
  • What it does:
    • Large components load at their stored bf16 and stay memory-mapped.
    • Text encoders stream from that mapping, cast to the compute dtype layer by layer. They are not pinned, so they never get copied into RAM.
    • A transformer that has to stream is stored as int8 weights, quantised one layer at a time on the GPU.
    • fp32 rotary buffers and the encoders' fp32-kept layers (e.g. T5 wo) keep their dense-load dtype.
    • GGUF companion loads take the same route.
  • Refusals: if even this route cannot fit, the load is refused with a message instead of being OOM-killed. The estimate counts the int8 DiT, the fp32-kept encoder layers (T5 wo), and a UNet or large VAE at its converted size; weight variants such as model.fp16.safetensors are not counted. LoRA on an int8 transformer is also refused with a message.
  • Status: load status reports what the route did.
  • Qwen-Image and Qwen-Image-Edit are marked fp16-incompatible, so an fp16 target computes in fp32 instead of rendering black.
  • Small-card activation reserve: for dense FLUX.2-klein, FLUX.1 and Qwen-Image on fp16/fp32 eager loads, the flat 8 GiB reserve is replaced by measured eager activation peaks. GGUF and int8 denoisers keep the flat reserve, since they dequantize a whole Linear per call. On bf16 cards placement is unchanged.
  • Kill switches:
    • UNSLOTH_DIFFUSION_SMALL_HOST=0 turns the route off; =1 forces it.
    • UNSLOTH_DIFFUSION_HOST_RAM_CHECK=0 skips the pre-load refusal.
    • UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_DENSE=0 restores the flat reserve.
    • The Qwen-Image fp32 promotion has no switch, since fp16 renders black.

Results

Kaggle T4x2 (31 GB host RAM), base and head on the same VM, same seeds. A warm render is a new prompt after the first.

family arm outcome warm render s peak host GB peak VRAM GB
FLUX.1-schnell main host OOM (killed at 28.9 GB) - - -
this PR ok 30.2 18.3 14.9
ComfyUI, same VM ok 25.6 18.2 14.9
Qwen-Image (CFG 4) main host OOM (killed at 29.1 GB) - - -
this PR ok 1026 (48.4 s/step) 24.5 13.8
ComfyUI, earlier VM ok 884 (44.0 s/step) 26.8 -
FLUX.2-klein-4B main ok 13.9 18.6 13.6
this PR ok 14.1 18.0 13.3
ComfyUI, same VM ok 12.4 17.0 14.9
  • FLUX.1 and Qwen-Image now render on a 31 GB host, about 1.2x and 1.1x ComfyUI's time.
  • klein: the measured reserve keeps all 26 of 26 transformer groups on the card, and the renders are bit-identical to main (6 of 6). The whole-image cells (13.9 vs 14.1 s, one each) were too noisy to show a change: the T4 is power-capped at 70 W and same-code cells ranged from 11.85 to 15.33 s with temperature. A later in-process measurement on a real T4 at a constant 77 to 78 C gave 2.46 to 2.32 s per step (-5.8%, 157 to 150 J) with every group resident, against streaming. That run was a one-way switch, not interleaved.
  • The Kaggle run used the branch before its final review fixes (the fp32-buffer rule, placement once per load, the bf16 gate, LoRA and status). None of them touches these three T4 loads. A FLUX render on the final head under the simulated T4 host is bit-identical to the patch Kaggle ran.

Accuracy

LPIPS, 1024x1024:

  • FLUX.1, int8 route vs dense fp16 (simulated T4 host on a large machine): 0.027 / 0.058 / 0.029 over 3 prompts.
    • Against a bf16 reference: the route gives 0.068 / 0.098, and dense fp16 itself 0.070 / 0.088. The int8 loss is about the size of the existing fp16-vs-bf16 gap.
  • Qwen-Image, int8 route (fp32 compute) vs dense fp32: 0.0145.
    • Against bf16: route 0.0152, dense fp32 0.0163.
    • The real-T4 renders are clean.
  • On a large host the route does not engage: FLUX.1, Qwen-Image and klein at speed off are bit-identical between main and this PR.

Tests

  • New test_diffusion_small_host.py (12 tests: the decision table for small hosts, large hosts and bf16 cards; the kill switch and force; the pre-load refusal; LoRA keeping the dense load, or being refused on the int8 route; header-only size reads; int8 storage against the dense layer; streamed encoders; the route plan; status). It errors at import on main.
  • New test_diffusion_small_host_planner.py and test_diffusion_measured_placement_dense.py, plus the updated Z-Image family test in test_diffusion_backend.py. Seven of these fail on main:
    • Qwen-Image fp16-incompatible;
    • the fp32-scaled measured peak;
    • bf16 denoisers skipped;
    • the measured headroom values;
    • klein resident on T4;
    • releasing an oversized request;
    • the updated test_zimage_is_fp16_incompatible.
  • With test_diffusion_backend.py, test_diffusion_memory.py and test_video_backend.py: 1316 passed. The full diffusion suite: 5933 passed, 0 failed.

Follow-up checks

Each row ran base and head on the same machine.

check where main this PR
FLUX.1-schnell, fresh cache, load through the Images route Colab T4, 12.7 GB host RAM killed for host RAM refused: "even the low-memory route needs about 17.8 GB of host memory and 9.5 GB is available"
Qwen-Image-2512 GGUF Q4_K_M, 1024x1024, 6 steps, CFG 4 Kaggle T4x2, 31 GB killed for host RAM ok, fp32 compute, 14.0 GB peak host, image renders (656 s)
FLUX.1-schnell, 1024x1024, 4 steps Kaggle T4x2, 31 GB killed for host RAM ok, int8 route, 16.7 GB peak host, warm render 47 s
FLUX.1-schnell, simulated T4 (fp16, 15 GB) on a 31 GB cgroup B200 host killed ok, int8 route, 17.2 GB peak host
FLUX.1-schnell, large host B200 renders bit-identical to main (2 of 2)
  • The 47 s warm FLUX render is slower than the 30.2 s above: the int8 transformer now keeps the flat runtime reserve (it dequantizes a whole Linear per call, which the measured eager peaks did not cover), so fewer groups stay on the card.
  • LPIPS of the int8 route against dense fp16 on the final head: 0.007 and 0.027.
  • The Studio regression switchboard reports no regression on the final head (68 same; the planner matrix's video probe errors on both sides, and its 640 image cells are identical).
  • The new tests also pass with transformers 4.57.6.

Not covered

  • klein reaching ComfyUI speed on T4 (not met), and the cause of that gap.
  • Qwen-Image against ComfyUI on the same VM.
  • One cell per arm, no repeats.
  • Not exercised on a GPU:
    • LoRA on a small host;
    • Qwen-Image-Edit in fp32;
    • compiled tiers on bf16 cards.
  • transformers 4.57 covered by the unit tests only (diffusers 0.40 needs huggingface_hub 1.x, which 4.57 does not install with); no A100 run.
  • The int8 route does not yet apply the per-family int8 exclusion lists. fuse_qkv at speed max is skipped with a warning on the int8 transformer.
  • This conflicts with Studio: run Z-Image and Wan2.2-5B / HunyuanVideo-1.5 in fp16 on fp16-only GPUs instead of fp32 #12476 in one test (test_zimage_is_fp16_incompatible); whichever lands second takes both sides.

A bf16 repo loaded at fp16 materialises every converted weight in host RAM
(FLUX.1-schnell 35 GB, Qwen-Image 56 GB), which OOM-kills a 31 GB host inside
from_pretrained. When the dense converted load does not fit the available host
RAM, load the large components at their stored dtype (memory-mapped), stream the
text encoders with layerwise casting, and store an offloaded denoiser as int8
per-channel weights. Qwen-Image is marked fp16-incompatible (NaN latents).
… for dense denoisers

On a 15 GB T4 (fp16, speed off, no transformer quant) the flat 8192 MiB activation
reserve streamed FLUX.2-klein-4B's 7.4 GB DiT block by block every step. Extend the
measured-activation placement to dense denoisers on the eager tier with a table of
measured fp16 peaks above the resident weights at 1024x1024 (klein 2455 MiB,
FLUX.1-schnell 2448, Qwen-Image 4248), bounded by the loaded DiT size so a larger
model of the same family keeps the flat estimate. The refinement only adds resident
groups to the flat plan's hooks, so oversized requests still release them.
Kill switch: UNSLOTH_DIFFUSION_MEASURED_ACTIVATION_DENSE=0 (the existing
UNSLOTH_DIFFUSION_MEASURED_ACTIVATION / UNSLOTH_DIFFUSION_PARTIAL_RESIDENT also apply).
… stay memory-mapped

Layerwise-cast the streamed encoders' embeddings too, convert kept-fp32 layers up
front, scale the dense eager activation table by the compute dtype (Qwen-Image
promoted to fp32 measured 8480 MiB above its weights vs 4248 at fp16), and test
the decision table, conversions and route plan. The Z-Image fp16 tests now use
FLUX.1 as the fp16-compatible control.
Keep fp32 buffers (RoPE inv_freq) as the dense load does, apply the route to the
GGUF companion load, int8 only denoisers, report the route in status and refuse
generation-time LoRA on an int8 denoiser with a clear message, keep the dense load
for LoRA loads without refusing, and gate the dense eager activation table to fp16
or fp32-promoted denoisers (bf16 cards keep the flat plan).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-02T09:25:42.430361Z 07f11c5 Manual request
🔒 Security Review ⚠️ Failed 2026-10-01T23:50:30.510209Z daaeddf PR opened
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-01T23:50:27.779569Z daaeddf PR opened
🔒 Security Review ⚠️ Failed 2026-10-01T23:50:30.510209Z daaeddf PR opened
ℹ️ 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

(Python 3.13, a-k) on head 9c846a02 fails in the test file this PR adds:

FAILED tests/test_diffusion_small_host.py::test_streamed_encoder_keeps_linear_storage_and_reports_compute_dtype - ModuleNotFoundError: No module named 'diffusers'

The test does from diffusers.hooks import HookRegistry (line 163), but that Backend CI shard does not install diffusers. A pytest.importorskip("diffusers.hooks") at the top of the test (or moving it to a job that installs diffusers) should clear it; the rest of the shard passed.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: de61dfc581

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

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 07f11c50ad

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

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 2, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 2, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant