Repository navigation
Studio sd.cpp: size the offload path's CPU VAE tile from spare host RAM instead of a bare 256 px --vae-tiling - #18839
Conversation
…AM instead of a bare 256 px --vae-tiling
|
@codex review |
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: Didn't find any major issues. Keep it up! 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". |
The offloaded weights and the VAE decode buffer are committed memory on Windows, so a host with a small or no page file can have less commit headroom than physical available RAM. Size the tile from the smaller of the two, as the small-host guards do; host_commit_mib() is None elsewhere, so Linux and macOS are unchanged.
…t cap, skip reason A second load of the same model behind a running sd-server sizes its tile after the outgoing server is stopped (the running tree defers the install, so placement runs again after teardown) and gets the first load's tile. The commit headroom test pins the Windows cap, the RAM test pins host_commit_mib so it reads the same on Windows runners, and the pinned-help skip names its reason. [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
…as measured on [skip ci]
[skip ci]
…ncoder) on the GPU when it fits (#18863) ## 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 #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.
…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.
Summary
On the sd.cpp (GGUF) route, the
model/sequentialmemory policies run the VAE on the CPU (--vae-on-cpu) and pass a bare--vae-tiling, which means 256 px tiles at half-tile overlaps. For a 1024 px image that is 49 tiles and about 3x the work of one whole decode, and every tile normalizes on its own, which shifts colour against a whole decode.This PR keeps the CPU VAE and the tiling flag, and sends
--vae-tile-size Nwith it, where N is the largest of 1024 / 768 / 512 px whose CPU decode buffer fits the host RAM left after the offloaded weights and the host reserve. A 1024 px tile decodes a 1024 px image whole; larger images still tile, with the buffer bounded by one 1024 px tile.What changes
sd_cpp_args.cpu_vae_tile_size(spare_mib): picks the tile from spare host RAM. Buffer model: 5120 MiB per 1024 x 1024 tile (measured 2818 MiB for the FLUX.2 VAE and 3889 MiB for the Qwen-Image 2.1 VAE on the pinned build, plus 30%), scaled by tile area.sd_cpp_args.offload_flags(..., vae_tile_size=N): appends--vae-tile-size Nonly when the VAE is on the CPU and tiling is on.none/grouppolicies and a GPU VAE (vae_on_cpu=False, the H3 video path) are unchanged.sd_cpp_backend._cpu_vae_tile_size: spare RAM = available host RAM minus the diffusion model, VAE and text encoder file sizes (all of them sit in host RAM under--offload-to-cpu) minus the host reserve. On Windows the smaller of available RAM and the commit headroom is used, since the offloaded weights and the decode buffer are committed memory.--helpdocuments--vae-tile-sizein image pixels (older builds counted latent cells). Unknown builds, an unreadable host and too little RAM keep today's bare--vae-tiling.UNSLOTH_SD_CPP_VAE_TILE=0keeps 256 px; a number pins that tile size. Userextra_argsstill go last, so an explicit--vae-tile-size/--vae-relative-tile-sizewins (sd.cpp's parser is last-wins).Not changed: the resident path. There the pinned build already decodes the whole image with direct convolution on CUDA sm75+ (implicit GEMM) and retries a VAE allocation failure with halved tiles. Measured on the A100 host below, forcing im2col there decodes within noise of direct convolution and costs about 3.8 GB more peak VRAM, so the conv mode stays on the build's auto.
Where the flags land
The GGUF route runs a resident sd-server and only falls back to one-shot sd-cli when the server is missing, not runnable, too old for the family, or fails to start. Both are built from the same offload flags, which
_placementrecomputes for whichever build ends up running, so the server and its sd-cli fallback get the same tile size.In the pinned sd-server:
--conv-direct(and the deprecated--vae-conv-direct/--diffusion-conv-direct) is a context parameter: launch time only.--vae-tiling,--vae-tile-size,--vae-relative-tile-size,--vae-tile-overlapset the server's default generation parameters. Eachimg_genrequest starts from those defaults, and a request may override them withvae_tiling_params. Studio's request sends none, so the launch values apply to every image and the server is never restarted for them.Measurements
sd-server (Studio's resident route)
Colab CPU High-RAM (8 vCPU), pinned CPU prebuilt
sd-server, launched with the argv Studio builds for the model-offload policy (--offload-to-cpu --clip-on-cpu --vae-on-cpu --flash-attn off,diffusion=on,te=auto --vae-tiling [--vae-tile-size 1024] --diffusion-conv-direct --vae-format flux2), one fresh server per arm kept resident across 3POST /sdcpp/v1/img_genrequests built like Studio's (novae_tiling_params). FLUX.2 klein 4B Q4_K_M, 1024 x 1024, seed 42, 1 step (the decode does not depend on the step count). Median (min-max).--vae-tiling--vae-tiling --vae-tile-size 10242.74x faster decode on the resident server; the request gains less here only because this host also runs the denoiser on its CPU. The server logged the launch tile on every request, so the launch default reaches requests without a restart. The after image is the untiled decode (see the identity check below); the 25 dB is the 256 px tiles' colour shift.
sd-cli (one-shot fallback; also the GPU-host numbers)
Colab A100-SXM4-80GB host (12 vCPU), pinned prebuilt
master-982-0518b34-u27b9ddcCUDA 12, 1024 x 1024, seed 42, fresh process per run, arms interleaved per repeat, 3 repeats, median (min-max).off_*= Studio's model-offload flags (--offload-to-cpu --clip-on-cpu --vae-on-cpu --vae-tiling), the VAE decode runs on the host CPU. PSNR is against the whole GPU decode of the same seed (res_auto_full).Every arm repeats bit for bit (A/A PSNR infinite). The offload arms also run the text encoder on the CPU, so their latent is not bit-identical to the GPU arm's; the PSNR column ranks how far each tile size moves from a whole decode, and the 1024 px arm is the whole CPU decode itself. Same latent, pinned CPU build on a Colab CPU High-RAM VM: one tile that covers the image decodes bit-identical to no tiling (FLUX.2 klein 4B, a 1024 px tile on a 1024 px image and a 512 px tile on a 512 px image, PSNR infinite). Against that untiled decode, 256 px tiles land at 25.6 dB (1024 px image) and 26.6 dB (512 px), and 512 px tiles at 26.4 dB, with visibly lifted blacks and washed colour on the FLUX.2 VAE; on the Qwen-Image 2.1 VAE 512 px tiles stay at 51.2 dB. So the image this PR produces on a 1024 px render is the untiled decode, and the old default is the one that moved.
Peak host RSS goes from 9.4 to 12.0 GB (klein) and 10.2 to 13.9 GB (Qwen-Image), inside the RAM rule.
Resident path on the same host, for the conv mode decision (unchanged by this PR):
Second CPU (Colab CPU High-RAM, 8 vCPU, CPU build, FLUX.2 klein 4B, 1 repeat; repeats on the L4 host varied under 0.3%):
768 px costs the same as 512 px on a 1024 px image (both cover it with tiles of equal total area), so it only matters for larger images or tighter RAM.
Tests
tests/test_sd_cpp_cpu_vae_tile.py(new, 28 cases): RAM thresholds per size, kill switch and pinned size, flags only for the CPU VAE, CLI and server argv carry the pair and user args still win, pixel-unit detection from a fake--help(and fails closed), spare RAM subtracts weights and reserve, unreadable host keeps 256 px, plus the real pinnedsd-cli --helpwhen one is present.tests/test_sd_cpp_args.py,test_sd_cpp_backend.py,test_sd_cpp_multigpu.py,test_h3_native_memory_fit.py,test_h3_vae_tile_overlap.py,test_sd_cpp_flash_attn_modules.py: 456 passed.Not covered
When this path runs
On CUDA, ROCm and XPU a GGUF that does not fit VRAM goes to diffusers with async RAM streaming by default, so this change does not touch that case. The sd.cpp
model/sequentialpolicies (which decode the VAE on the CPU) run when:modelpolicy by default;