Skip to content

Studio sd.cpp: decode the VAE in one tile when the whole image fits - #18999

Merged
danielhanchen merged 3 commits into
mainfrom
studio-sdcpp-vae-whole-image
Nov 23, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-sdcpp-vae-whole-image

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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.

…ds the whole image

The offload path (model / sequential policies) always passes --vae-tiling, sized by #18839 (CPU VAE, spare host
RAM) and #18863 (GPU VAE, free VRAM) to at most 1024 px. On FLUX.2 klein an image larger than the tile gets a
per-tile GroupNorm tone shift against the untiled decode (31.3 dB at 1536 px, 25.9 dB at 2048 px), and the tiled
decode is slower than the whole one.

sd-server fixes the tile at launch, so families whose whole decode was measured (FLUX.2, FLUX.1 / Z-Image and
Qwen-Image 2.1 VAEs) now first try a 2048 px, then a 1536 px tile, priced from that VAE's measured whole-decode
compute buffer plus 15% on the card (which retries a failed allocation with halved tiles) and 30% on the CPU. A
tile at least as large as the image is one tile, the same pixels as an untiled decode. Below those budgets, and
for every other family (MiniMax-H3's own tiler included), the tile ladder is unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 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-23T07:36:10.530579Z 537e1a9 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

Copy link
Copy Markdown

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

Reviewed commit: a26f0548e0

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

On AMD gfx1151 a whole klein decode through Vulkan places part of the work
in host RAM and runs 2.0-3.3x slower than 1024 px tiles, so a Vulkan card
keeps the tile ladder; the CPU whole tile is unchanged.

[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. Hooray!

Reviewed commit: 537e1a93d7

ℹ️ 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 43e0156 into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the studio-sdcpp-vae-whole-image branch November 23, 2026 08:35
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