Repository navigation
Studio sd.cpp: decode the VAE in one tile when the whole image fits - #18999
Conversation
…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.
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 |
|
Codex Review: Didn't find any major issues. 🎉 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". |
[skip ci]
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]
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! 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". |
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-sizeat launch, while the image size comes with each request. The pinned build's/sdcpp/v1/img_genbody does acceptvae_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_sizetake 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_sizeand_cpu_backend_offload(the restart onto the CPU backend) pass the loaded family through.UNSLOTH_SD_CPP_VAE_TILE/UNSLOTH_SD_CPP_VAE_GPUstill 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 waysd_cpp_backendreads them. Pinned prebuiltmaster-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:
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):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
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_loadwhere 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_loadgives Z-Image--vae-tile-size 1024, and_offload_vae_flagshas no family.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