Repository navigation
Studio sd.cpp: keep the offload path's VAE (and a roomy card's text encoder) on the GPU when it fits - #18863
Conversation
…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.
|
@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". |
… 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]
|
@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". |
…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
Follow-up to #18839. On the sd.cpp (GGUF) route, the
model/sequentialmemory 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
modelpolicy), 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-cpusets 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-cpuonly 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.end_runners), which evicts its card copies. In sd-server this happens per request; weights stay in host RAM between requests.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), lessmax(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: formodel/sequentialon a CUDA or Vulkan build whose--helpdocuments--vae-tile-sizein image pixels, sendsoffload_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/groupare unchanged. The device pin then becomes--backend diffusion=CUDA0,te=CUDA0,vae=CUDA0(orVulkan0), orte=cpuon a smaller card. ROCm, Metal, latent-unit builds and an unreadable card (Windows Vulkan) keep the CPU VAE and text encoder.offload_flagsnow passes--vae-tile-sizefor a GPU VAE too when given a size. The H3 video path (vae_on_cpu=False, no size) still sends a bare--vae-tiling.UNSLOTH_SD_CPP_VAE_GPU=0keeps the CPU VAE (and text encoder),UNSLOTH_SD_CPP_TE_GPU=0keeps 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--helplists it). Its streaming is built in: under--offload-to-cpuany runner that does not fit is cut into segments with the next segment's weights prefetched asynchronously (--disable-prefetch/--disable-segmented-computeturn that off), and--max-vramonly 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-vramwould 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-cliandsd-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_sizelifted 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 throughPOST /sdcpp/v1/img_genbuilt like Studio's (novae_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)
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.
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 onlyis the first commit of this PR (text encoder on the CPU),VAE + TEthe second. PSNR against an untiled decode with the text encoder on the card (the resident path's placement).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.
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/groupuntouched), the H3 video opt-out unchanged.tests/test_sd_cpp_cpu_vae_tile.py: updated foroffload_flagsnow 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.pyand the offload / VAE tests oftest_video_backend.pypass unchanged.Not covered
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:
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.