Skip to content

Studio sd.cpp: keep the offload path's VAE (and a roomy card's text encoder) on the GPU when it fits - #18863

Merged
danielhanchen merged 3 commits into
mainfrom
studio-sdcpp-vae-gpu-offload
Nov 22, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-sdcpp-vae-gpu-offload

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #18839. On the sd.cpp (GGUF) route, the model / sequential memory policies pass --offload-to-cpu --clip-on-cpu --vae-on-cpu --vae-tiling, so the VAE decodes on the host CPU while the GPU sits idle. At 1024 x 1024 that is 66 to 109 s on a 12 vCPU L4 host even with #18839's RAM-sized tile, and more than 290 to 470 s on a 2 vCPU T4 host, against 1.2 to 7 s for the same decode on the card.

This policy is what a card torch cannot see gets by default (an AMD or Intel card on Vulkan without a ROCm / XPU torch auto-offloads with the model policy), and what CUDA gets when the native engine is forced with CPU offload / Low VRAM. On those loads this PR keeps the VAE on the card whenever the card's free VRAM holds the VAE plus one tile's decode buffer, and moves the text encoder onto the card too when the card has 6000 MiB free. Otherwise it falls back to today's CPU VAE / text encoder (with #18839's RAM-sized tile).

Why the card has the room on the offload path

From the pinned build's sources (master-982-0518b34-u27b9ddc):

  • --offload-to-cpu sets the params backend to *=cpu: every weight lives in host RAM and is copied to the compute backend only while its model runs. --vae-on-cpu only moves the VAE's compute backend (vae=cpu). Without it the VAE weights still sit in host RAM and are copied to the card for the decode.
  • Before each compute segment the execution engine checks the card's capacity and, when the request does not fit, releases other runners' idle workspaces and then evicts other runners' weight blocks, least recently used first. The denoiser has finished sampling when the decode starts, so its card copies are the ones evicted.
  • Every runner is ended when the generation finishes (end_runners), which evicts its card copies. In sd-server this happens per request; weights stay in host RAM between requests.
  • A VAE decode that still fails to allocate is retried with halved tiles.

So the decode can use the card's free VRAM as measured at load time, not what is left beside the denoiser.

What changes

  • sd_cpp_args.gpu_vae_tile_size(free_mib, vae_mib): largest of 1024 / 768 / 512 / 256 px whose decode buffer fits the card's free VRAM after the VAE weights (file size counted twice, in case the card copy is widened to f32). Buffer model: 5632 MiB per 1024 x 1024 tile, scaled by tile area (whole-decode minus 512 px tile peaks on the resident path: Qwen-Image 2.1 VAE 4459 MiB, FLUX.2 VAE 3315 MiB, plus 25%). None keeps the CPU VAE.
  • sd_cpp_backend._offload_card_limit_mib: free VRAM of the card the build renders on, read without torch through the same reading the off-torch offload check already uses (_unmeasured_card_free_mib: nvidia-smi for CUDA, amdgpu sysfs for a sole AMD Vulkan card), less max(CARD_RESERVE_MIB, VRAM Budget reserve). The card class comes from the build's own device listing (sd_cpp_listed_device); the listing itself carries no memory figures.
  • sd_cpp_args.gpu_te_fits(limit): the text encoder goes to the card when the limit is at least 6000 MiB (GPU_TE_MIN_FREE_MIB), only alongside a GPU VAE. Its weights stay in host RAM; sd.cpp segments it to the live free VRAM and evicts it for the denoiser.
  • sd_cpp_backend._offload_vae_flags: for model / sequential on a CUDA or Vulkan build whose --help documents --vae-tile-size in image pixels, sends offload_flags(policy, vae_on_cpu=False, vae_tile_size=N, te_on_cpu=not te_gpu) when the card holds the decode, else the Studio sd.cpp: size the offload path's CPU VAE tile from spare host RAM instead of a bare 256 px --vae-tiling #18839 CPU path. none / group are unchanged. The device pin then becomes --backend diffusion=CUDA0,te=CUDA0,vae=CUDA0 (or Vulkan0), or te=cpu on a smaller card. ROCm, Metal, latent-unit builds and an unreadable card (Windows Vulkan) keep the CPU VAE and text encoder.
  • offload_flags now passes --vae-tile-size for a GPU VAE too when given a size. The H3 video path (vae_on_cpu=False, no size) still sends a bare --vae-tiling.
  • Kill switches: UNSLOTH_SD_CPP_VAE_GPU=0 keeps the CPU VAE (and text encoder), UNSLOTH_SD_CPP_TE_GPU=0 keeps only the text encoder on the CPU.

sd.cpp's own layer streaming instead

The pinned build has no --stream-layers (it rejects the flag; Studio only sends it where --help lists it). Its streaming is built in: under --offload-to-cpu any runner that does not fit is cut into segments with the next segment's weights prefetched asynchronously (--disable-prefetch / --disable-segmented-compute turn that off), and --max-vram only sets the budget, defaulting to the live free VRAM. So the offload flags above already stream: the 7.7 GB text encoder ran on a card with 7.6 GB free in segments at a 6.2 GB peak. An explicit --max-vram would add nothing at the default VRAM Budget; it is left as today (unset on this path).

A 1024 px tile decodes a 1024 px image whole, so on a card with room the image is bit-identical to an untiled GPU decode; larger images still tile.

Measurements

Pinned prebuilt CUDA 12 sd-cli and sd-server, 1024 x 1024, seed 42, 4 steps (the decode does not depend on the step count), fresh process per run. Each arm's flags come from that branch's own planner code (offload_flags / cpu_vae_tile_size / gpu_vae_tile_size lifted from the #18839 head and from this head) fed with free VRAM and host RAM read on the VM before each run, plus the speed and device flags Studio adds on CUDA. sd-server runs go through POST /sdcpp/v1/img_gen built like Studio's (no vae_tiling_params). Before = #18839 head, after = this head, same VM. PSNR is against an untiled GPU decode of the same offloaded latents. Peak VRAM is the sd process, sampled through NVML every 0.1 s. Median (min-max).

L4 (22.5 GB, 12 vCPU, 53 GB RAM)

model binary arm VAE VAE decode s request / total s peak VRAM MiB VRAM held after request MiB peak RSS MiB PSNR dB
FLUX.2 klein 4B Q4_K_M sd-server before (n=2) CPU, 1024 px 66.63 (66.41-66.85) 88.5 (88.3-88.7) 3662 288 12022 71.0
FLUX.2 klein 4B Q4_K_M sd-server after (n=3) GPU, 1024 px 1.21 (1.20-1.21) 22.9 (22.9-22.9) 3662 744 9363 identical
FLUX.2 klein 4B Q4_K_M sd-cli before (n=2) CPU, 1024 px 66.41 (66.38-66.44) 90.0 (90.0-90.0) 3662 - 11992 71.0
FLUX.2 klein 4B Q4_K_M sd-cli after (n=3) GPU, 1024 px 1.22 (1.21-1.22) 24.5 (24.5-24.5) 3662 - 9332 identical
Qwen-Image 2.1 Q4_K_M sd-server before (n=2) CPU, 1024 px 108.98 (108.24-109.73) 124.2 (123.6-124.8) 5838 282 13854 72.3
Qwen-Image 2.1 Q4_K_M sd-server after (n=3) GPU, 1024 px 2.54 (2.54-2.69) 17.7 (17.7-18.1) 5838 810 10096 identical
Qwen-Image 2.1 Q4_K_M sd-cli before (n=2) CPU, 1024 px 108.71 (108.62-108.79) 125.8 (125.5-126.0) 5838 - 13817 72.3
Qwen-Image 2.1 Q4_K_M sd-cli after (n=3) GPU, 1024 px 2.54 (2.54-2.55) 19.5 (19.5-19.5) 5838 - 10075 identical

Decode 43 to 55x faster, request 3.9x (klein, whose text encoder still runs on the CPU) and 7.0x (Qwen-Image). Peak VRAM on this table predates the text-encoder commit; see the re-check below. Host RSS drops 2.7 / 3.8 GB (no CPU decode buffer). The idle server keeps about 0.5 GB more VRAM after a GPU decode (the card's buffer cache).

T4 (15 GB, 2 vCPU, 12.7 GB RAM)

The common small-card host. #18839's RAM rule finds no spare host RAM here (the BF16 text encoder alone is 7.7 GB), so the before arm is the bare 256 px CPU tile. Each before run was capped at 600 s (klein) / 360 s (Qwen-Image) to stay in budget, so its numbers are lower bounds.

model binary arm VAE VAE decode s request / total s peak VRAM MiB VRAM held after request MiB PSNR dB
FLUX.2 klein 4B Q4_K_M sd-server before (n=1) CPU, 256 px > 470 (cap) > 600 (cap) 3590 - -
FLUX.2 klein 4B Q4_K_M sd-server after (n=3) GPU, 1024 px 3.20 (3.13-3.25) 134.0 (133.5-134.4) 3590 644 identical
FLUX.2 klein 4B Q4_K_M sd-cli after (n=3) GPU, 1024 px 3.28 (3.26-3.29) 138.1 (138.1-138.6) 3590 - identical
Qwen-Image 2.1 Q4_K_M sd-server before (n=1) CPU, 256 px > 293 (cap) > 360 (cap) 5714 - -
Qwen-Image 2.1 Q4_K_M sd-server after (n=3) GPU, 1024 px 7.03 (6.82-7.84) 77.2 (69.1-77.6) 5714 708 identical
Qwen-Image 2.1 Q4_K_M sd-cli after (n=3) GPU, 1024 px 6.46 (6.45-6.95) 75.5 (72.5-80.5) 5714 - identical

At least 4.5x (klein) and 4.7x (Qwen-Image) on the whole request. The klein request is dominated by its 7.7 GB BF16 text encoder running on 2 CPU threads, which this PR does not touch.

Text encoder on the card (T4, sd-server, separate VM)

Same harness; VAE only is the first commit of this PR (text encoder on the CPU), VAE + TE the second. PSNR against an untiled decode with the text encoder on the card (the resident path's placement).

model free VRAM arm n VAE decode s request s peak VRAM MiB PSNR dB
FLUX.2 klein 4B Q4_K_M full (14.9 GB) VAE only 1 2.75 132.8 3590 34.7
FLUX.2 klein 4B Q4_K_M full (14.9 GB) VAE + TE 2 2.78 (2.77-2.79) 55.6 (55.5-55.7) 6230 identical
FLUX.2 klein 4B Q4_K_M 7600 MiB VAE + TE, 768 px 1 4.88 58.2 6230 32.1
Qwen-Image 2.1 Q4_K_M full (14.9 GB) VAE only 1 6.03 74.3 5714 47.3
Qwen-Image 2.1 Q4_K_M full (14.9 GB) VAE + TE 2 6.15 (5.71-6.60) 66.8 (66.1-67.4) 5716 identical
Qwen-Image 2.1 Q4_K_M 7600 MiB VAE + TE, 768 px 1 9.98 70.9 5716 54.2

The klein request drops 2.4x again (its BF16 encoder no longer runs on 2 CPU threads): against the all-CPU request before this PR (more than 600 s) that is more than 10x. The PSNR of the CPU-encoder arms is the encoder's CPU vs GPU arithmetic, not the VAE. The klein encoder (7.7 GB) is larger than the 7.6 GB card and still ran, in segments, under the free VRAM.

Smaller cards (T4 with a held allocation)

A side process holds cudaMalloc'd memory so the card reports 7600 MiB free (an 8 GB card) or 3800 MiB free (a 4 GB card) before the load; the planner reads that. Qwen-Image 2.1 Q4_K_M, the larger VAE.

free VRAM binary n VAE tile picked VAE decode s request / total s peak VRAM MiB PSNR vs untiled dB OOM / tile-halving lines
7600 MiB sd-server 2 GPU, 768 px 9.50 (9.47-9.54) 76.9 (76.8-77.1) 5714 54.2 0
7600 MiB sd-cli 1 GPU, 768 px 9.77 80.0 5714 54.2 0
3800 MiB sd-server 1 GPU, 256 px 12.63 78.4 3554 42.0 0
3800 MiB sd-cli 1 GPU, 256 px 13.20 82.1 3554 42.0 0

On the 4 GB-class reading the denoiser's card copies were evicted for the decode and the run stayed under the free VRAM (peak 3554 of 3832 MiB). Even the 256 px GPU tiles are closer to an untiled decode than the CPU's 256 px tiles were (39.4 dB on the same model in #18839).

No run on any host logged an out-of-memory or tile-halving line.

Tests

  • tests/test_sd_cpp_gpu_vae_offload.py: the tile ladder against free VRAM and VAE size, the text encoder threshold, both kill switches, the argv and device pins (CUDA and Vulkan), the backend gate (card reserve, VRAM Budget reserve, an off-torch Vulkan card, an unread Vulkan card, other accelerators, latent-unit builds, an unread or failing card read, none / group untouched), the H3 video opt-out unchanged.
  • tests/test_sd_cpp_cpu_vae_tile.py: updated for offload_flags now passing a size for a GPU VAE.
  • test_sd_cpp_backend.py, test_sd_cpp_args.py, test_sd_cpp_conv_direct_off.py, test_h3_vae_tile_overlap.py and the offload / VAE tests of test_video_backend.py pass unchanged.

Not covered

  • Vulkan was not measured (no Vulkan card could be rented and no AMD CI ticket was open to batch into). The Vulkan case is inferred: the same flags, the same sd.cpp eviction and segmentation code, and a free-VRAM reading that exists only for a sole AMD card on Linux (amdgpu sysfs). Windows AMD / Intel Vulkan has no torch-free reading, so it keeps the CPU VAE and text encoder.
  • ROCm and Metal keep the CPU VAE.
  • The text encoder on a card under 6000 MiB free (it stays on the CPU there by design); the 4 GB-class run above is VAE only.
  • The 8 GB and 4 GB cards were simulated with a held allocation on a T4, not run on real cards.
  • The before arm on the T4 is one capped run per model (lower bounds); on the L4 it is two runs per binary. Their spread on the L4 is under 1%.
  • Windows was not run.

Re-check on the final head (text encoder on the card, CPU restart fix)

FLUX.2 klein 4B Q4_K_M, pinned sd-cli / sd-server, fresh process per run, flags from each tree's own planner code fed with the VM's readings:

card route decode s, before -> after request s, before -> after peak VRAM MiB, before -> after
L4, 1024 px sd-server 66.89 (CPU) -> 1.21 88.52 -> 10.89 3662 -> 6252
L4, 1024 px sd-cli 66.93 -> 1.22 90.04 -> 13.00 3662 -> 6252
T4, 512 px sd-server 214.33 (CPU, 256 px tiles) -> 1.39 351.62 -> 46.40 3016 -> 6230

The VRAM peak rises because the text encoder now runs on the card, which this PR only does with 6000 MiB free; users who picked Low VRAM / CPU offload on such a card see the higher peak in exchange for the faster request. Host RSS drops 2.6 GB on the L4.

A server restart onto the CPU backend (an unsupported-op abort, or a repeated Vulkan device loss) now rebuilds the CPU VAE flags and the RAM-sized tile instead of reusing the GPU VAE's VRAM-sized tile. Across a 16896-cell argv matrix (11 backends, 4 policies, 6 card sizes, host RAM, tile units, kill switches) every cell outside the target set is byte-identical to before, including ROCm, Metal, SYCL, CPU-only and the unreadable Windows AMD Vulkan card.

…free VRAM holds the tiled decode

Model / sequential offload decoded the VAE on the CPU (--vae-on-cpu). Under --offload-to-cpu every weight already
lives in host RAM and sd.cpp evicts the denoiser's card copies when the decode needs the room, so the decode can run
on the card: the VAE weights plus one tile's decode buffer must fit the card's free VRAM after the card reserve (or a
larger VRAM Budget reserve). The tile is the largest of 1024 / 768 / 512 / 256 px that fits; sd.cpp still halves it on
an allocation failure. Other accelerators, latent-unit builds, an unread card and too little VRAM keep the CPU VAE
with its RAM-sized tile. UNSLOTH_SD_CPP_VAE_GPU=0 keeps the CPU VAE.
…, and a roomy card's text encoder, on the GPU

The auto offload of a card torch cannot see (AMD / Intel on Vulkan) is the common way into the model-offload policy,
so the VAE gate now reads Vulkan cards too (the same torch-free reading the offload check uses: amdgpu sysfs for a
sole AMD card, nvidia-smi for CUDA). Unread cards (Windows Vulkan) keep the CPU VAE.

The text encoder follows the VAE onto the card when the card has 6000 MiB free after the reserve: its weights stay
in host RAM and sd.cpp segments it to the live free VRAM, then evicts it for the denoiser. On a 2 vCPU T4 the BF16
Qwen3-4B encoder of FLUX.2 klein took an sd-server request from 133 to 56 s. UNSLOTH_SD_CPP_TE_GPU=0 keeps it on the
CPU.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 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-22T09:57:14.473250Z 91becc8 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 8487ac4cbc

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

… not the GPU VAE's VRAM-sized tile

The CPU-backend restart (unsupported-op abort, repeated Vulkan device loss) reused the load's offload flags
without the pin. With the GPU VAE gate those carry a tile sized from the card's free VRAM and no --vae-on-cpu,
so the CPU decode ran at up to 1024 px whatever host RAM held. Rebuild the CPU VAE flags and RAM-sized tile the
load had before the gate.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 91becc8950

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

@danielhanchen
danielhanchen merged commit eca8344 into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the studio-sdcpp-vae-gpu-offload branch November 22, 2026 11:35
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…18999)

## Summary

Follow-up to #18839 and #18863. On the sd.cpp (GGUF) route the offload
policies (`model` / `sequential`) always pass `--vae-tiling`, and both
the CPU VAE tile (#18839, from spare host RAM) and the GPU VAE tile
(#18863, from free VRAM) stop at 1024 px. Any image larger than the tile
is decoded in pieces, and each tile normalises its GroupNorm layers on
its own statistics. There are no seams, but on FLUX.2 klein the tone and
contrast follow the tile grid: 31.3 dB PSNR against the untiled decode
at 1536 x 1536 and 25.9 dB at 2048 x 2048 (block-diff std 5.7 and 10.9
grey levels). This is the same per-tile statistics problem #17966 fixed
on the diffusers path. The tiled decode is also slower than the whole
one at every size we measured.

This PR decodes the image in one tile whenever the RAM / VRAM budget
holds a whole decode of the family's largest output. A tile at least as
large as the image is a single tile: the same pixels as an untiled
decode (bit-identical in every run below).

## Why a launch-time tile

sd-server fixes `--vae-tile-size` at launch, while the image size comes
with each request. The pinned build's `/sdcpp/v1/img_gen` body does
accept `vae_tiling_params`, so a per-request tile is possible. That
would need new plumbing on both the server path and the one-shot sd-cli
path, plus a budget kept on the loaded state. Restarting the server per
size costs a full model reload. A tile covering the family's largest
output gives every request up to that size an untiled decode, and only
the tile size changes. A smaller image still decodes in one tile sized
to the image (sd.cpp clamps the tile to the latent), so its buffer is
the same as before.

## What changes

- `sd_cpp_args.WHOLE_VAE_DECODE_MIB`: the measured VAE compute buffer
(ggml, MiB) of one whole 2048 px / 1536 px decode, per VAE: FLUX.2
(klein, dev) 17040 / 6336, FLUX.1 (Z-Image, FLUX.1, Kontext, Chroma)
17028 / 6336, Qwen-Image 2.1 15552 / 8748. The CPU decode allocates the
same buffers (17028 and 15556 MiB measured at 2048 px on the CPU
backend). The FLUX VAEs grow faster than the image area past 1536 px
(2816 MiB per 1024 x 1024 of area up to 1536 px, 4260 at 2048 px), so
nothing is extrapolated beyond 2048 px.
- `gpu_vae_tile_size` / `cpu_vae_tile_size` take the family's `(tile,
MiB)` pairs (`whole_vae_tiles(family)`) and try them before the existing
ladder: the card gets the 2048 / 1536 px tile when the measured buffer +
15% fits beside the VAE weights (counted twice, as before). The card
retries a failed allocation with halved tiles. The CPU VAE needs the
measured buffer + 30% in spare host RAM, because the CPU path has no
retry.
- `sd_cpp_backend._offload_vae_flags`, `_cpu_vae_tile_size` and
`_cpu_backend_offload` (the restart onto the CPU backend) pass the
loaded family through.
- Unmeasured families (Qwen-Image / Edit / Layered with the Wan-style
VAE, SDXL, FLUX.3, Krea 2, MiniMax-H3 and its own tiler / overlap path)
get exactly the old ladder. Below the 1536 px price every family gets
exactly the old tile, so tight hosts and cards are unchanged.
`UNSLOTH_SD_CPP_VAE_TILE` / `UNSLOTH_SD_CPP_VAE_GPU` still win. Video
(`video.py`) does not use these functions and is unchanged.

## Results

Same seed (42), prompt, steps and flags per (family, size), so every arm
decodes the same latent. Each arm's flags come from that tree's own
`sd_cpp_args` (`offload_flags`, the tile choosers, `gpu_te_fits`,
`device_backend_flags`), fed the VM's real readings the way
`sd_cpp_backend` reads them. Pinned prebuilt
`master-982-0518b34-u27b9ddc`. PSNR is against an untiled decode from
the same run.

GPU VAE, Colab L4 (idle free 22564 MiB, so the limit after the 1536 MiB
card reserve is 21028). Base picks 1024 px, head picks 2048 px for all
three families:

| family | size | base tiles | base PSNR dB | base decode s | head tiles
| head PSNR | head decode s | head card peak MiB |
|---|---|---|---|---|---|---|---|---|
| FLUX.2 klein 4B | 1024 | 1 | identical | 1.17 | 1 | identical | 1.17 |
6270 |
| FLUX.2 klein 4B | 1536 | 2x2 | 31.32 | 3.54 | 1 | identical | 2.32 |
7840 |
| FLUX.2 klein 4B | 2048 | 3x3 | 25.93 | 7.66 | 1 | identical | 4.29 |
19442 |
| Qwen-Image 2.1 | 1024 | 1 | identical | 2.69 | 1 | identical | 2.47 |
5206 |
| Qwen-Image 2.1 | 1536 | 2x2 | 59.52 | 7.56 | 1 | identical | 4.74 |
10786 |
| Qwen-Image 2.1 | 2048 | 3x3 | 58.50 | 16.11 | 1 | identical | 7.93 |
18600 |
| Z-Image Turbo | 1024 | 1 | identical | 1.14 | 1 | identical | 1.15 |
7706 |
| Z-Image Turbo | 1536 | 2x2 | 44.45 | 3.61 | 1 | identical | 2.32 |
7848 |
| Z-Image Turbo | 2048 | 3x3 | 42.45 | 7.64 | 1 | identical | 4.23 |
19438 |

A/A: two untiled klein 2048 decodes are bit-identical (4.17 / 4.19 s),
and repeated head decodes are bit-identical at 4.29 / 4.30 s (klein),
7.93 / 7.93 s (Qwen) and 4.23 / 4.23 s (Z-Image). The largest whole
decode peaks at 19442 MiB on the card. That is below the 21772 MiB of
free VRAM the head needs before it picks the 2048 px tile (17040 x 1.15
+ 2 x 320 VAE + 1536 reserve), and below the 22564 MiB the idle card
has.

CPU VAE (`--vae-on-cpu`, CPU build, 2 denoising steps; the 2048 px rows
ran 1 step, head arm only, to record the whole-decode buffer and RSS).
No Colab CPU High-RAM VM was free during this run, so the CPU arms ran
with the CPU build on GPU VMs (the card unused):

| family | size | host | base tiles | base PSNR dB | base decode s |
head tiles | head PSNR | head decode s | head peak RSS MiB (weights) |
|---|---|---|---|---|---|---|---|---|---|
| FLUX.2 klein 4B | 1024 x 1024 | L4 host, 12 vCPU, 53 GB | 1 |
identical | 52.1 | 1 | identical | 50.8 | 12063 (10481) |
| FLUX.2 klein 4B | 1536 x 864 | L4 host, 12 vCPU, 53 GB | 2x1 | 32.75 |
83.5 | 1 | identical | 67.4 / 68.0 (A/A) | 12843 (10481) |
| FLUX.2 klein 4B | 2048 x 2048, 1 step | L4 host, 12 vCPU, 53 GB | | |
| 1 | | 328.8 | 26557 (10481) |
| Z-Image Turbo | 1536 x 864 | G4 host, 48 vCPU, 185 GB | 2x1 | 47.25 |
17.0 | 1 | identical | 13.9 | 15257 (11668) |
| Z-Image Turbo | 2048 x 2048, 1 step | G4 host, 48 vCPU, 185 GB | | | |
1 | | 81.2 | 28979 (11668) |
| Qwen-Image 2.1 | 1536 x 864 | G4 host, 48 vCPU, 185 GB | 2x1 | 61.52 |
26.0 | 1 | identical | 19.5 | 14757 (9559) |
| Qwen-Image 2.1 | 2048 x 2048, 1 step | G4 host, 48 vCPU, 185 GB | | |
| 1 | | 63.6 | 25667 (9559) |

The base picks 1024 px on both hosts. The head picks 2048 px because
spare host RAM (36098 MiB on the L4 host) covers the priced whole decode
(22152 MiB for klein, 17040 x 1.3). At 1536 x 864 the head is
bit-identical to the untiled decode where the base is 32.8 dB (klein),
47.3 dB (Z-Image) and 61.5 dB (Qwen-Image 2.1), and its decode is 16 to
25% faster. The CPU backend allocates the same whole-decode buffer as
CUDA (17040 / 17028 / 15556 MiB at 2048 px). Peak RSS above the weights
at 2048 px is 16.1 GB (klein), 17.3 GB (Z-Image) and 16.1 GB (Qwen-Image
2.1), within the 22.2 / 22.1 / 20.2 GB the head prices.

## Tests

- New `tests/test_sd_cpp_whole_vae_tile.py` (42 cases): the measured
families and their prices, the GPU and CPU tile against the budget (an
idle L4 gets 2048 for all three families), unchanged choices below the
1536 px price and for unmeasured families (MiniMax-H3 included), env
switches, the restart onto the CPU backend, and a real `_run_load` where
Z-Image gets the 2048 px CPU tile and SDXL keeps 1024. On main the file
fails to import, and the behaviour cases fail: `_run_load` gives Z-Image
`--vae-tile-size 1024`, and `_offload_vae_flags` has no family.
- The 43 existing `tests/*sd_cpp*` files: 2009 passed, 5 skipped and the
same 2 failed on both main and head
(`test_sd_cpp_moss_image.py::test_diffusers_takes_a_moss_safetensors_single_file`,
`test_sd_cpp_torch_vae.py::test_decode_matches_the_diffusers_pipeline_decode`,
both outside this change).

## Not covered

- Prices were measured on CUDA (L4) and on the x86 CPU backend. A Vulkan
card (an off-torch AMD / Intel card on the offload path) was not run.
Its decode still has sd.cpp's halving retry if the allocation fails.
- The VAE encode of reference / edit images reuses the same tile and was
not measured separately at 2048 px on the CPU path.
- Windows and macOS were not run. The tile choice is OS-neutral Python,
and Windows commit headroom still caps the CPU spare as before.
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