Repository navigation
Studio sd.cpp: a CPU torch on a 16 GB Vulkan card stops forcing MiniMax-H3-Image's 31 GB of weights into 15 GB of RAM (OOM kill to 18.1 s) - #18210
Conversation
…into host RAM when it cannot hold them Auto offloaded every load on a card a CPU torch never measured (unslothai#13666). --offload-to-cpu puts every weight in anonymous host RAM (*=cpu params, which also turns sd.cpp's auto-fit off), so MiniMax-H3-Image Q4_K_M (12132 MiB DiT + 17375 MiB text encoder + 2229 MiB VAE) on a 16 GB RX 9060 XT with 15 GB RAM was OOM-killed while loading. Auto now skips the offload when RAM cannot hold the weights and the card's free VRAM (amdgpu sysfs, or nvidia-smi for CUDA) holds the diffusion model; auto-fit then places the DiT on the card and spills the rest to RAM or disk. Unreadable readings keep the offload.
[skip ci]
|
@codex review |
1 similar comment
|
@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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c0ebb94c9
ℹ️ 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".
| if klass == "cuda": | ||
| from core.inference.diffusion_device import smi_free_total_mib | ||
| return smi_free_total_mib(0 if ordinal is None else int(ordinal)) |
There was a problem hiding this comment.
Use a torch-independent device inventory for the CUDA probe
On a CPU-torch/NVIDIA host with CUDA_VISIBLE_DEVICES unset, smi_free_total_mib() returns (None, None) without querying NVIDIA telemetry. It obtains its device IDs through get_parent_visible_gpu_ids(), whose unmasked path calls get_physical_gpu_count(); that function returns zero for DeviceType.CPU (studio/backend/utils/hardware/hardware.py:7191-7226). Consequently, the new low-RAM CUDA path retains CPU offloading even when a readable card has enough VRAM, leaving the host-RAM exhaustion unaddressed. Use an inventory independent of torch's backend. The added CUDA test hides this failure by mocking smi_free_total_mib() itself.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in d41df19: under CPU torch with no mask, a sole NVIDIA card (MIG off) is read straight from nvidia-smi. The new test runs a fake nvidia-smi instead of mocking the probe.
| if dit + max(CARD_RESERVE_MIB, _user_budget_reserve_mib(card_total or free)) > free: | ||
| return False |
There was a problem hiding this comment.
Preserve the VRAM budget when dropping offload
With CPU torch, Auto, the Mint-sized files and an 80% VRAM Budget, 12132 + 3260 <= 15800 passes this check and drops offloading. However, the 3260 MiB reserve never reaches server.start() or build_sd_cpp_server_command(): 80% and 100% produce identical uncapped commands, so native placement can consume the reserved space for encoder/VAE weights, defeating the user's budget. The captured native help documents --max-vram as the "maximum VRAM budget in GiB for graph-cut segmented execution" (studio/backend/tests/test_sd_cpp_multigpu.py:390), but no budget is passed here. Enforce the reserve in native placement, or retain offloading when a lowered budget cannot be enforced.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in d41df19: sd.cpp's auto-fit takes no VRAM cap, so any lowered VRAM Budget now keeps the offload.
…wered VRAM Budget keeps the offload [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
## Name the encoding on the sd.cpp nvidia-smi probe `studio/backend/tests/test_text_io_encoding.py::test_text_io_names_its_encoding[sd_cpp_backend.py]` fails on `main` since #18210. On staging it shows up as `(Python 3.13, rest)` failing on unrelated PRs, for example #17694 and #18275: ``` E sd_cpp_backend.py:3146: subprocess(text = True) without encoding E assert not ['sd_cpp_backend.py:3146: subprocess(text = True) without encoding'] ``` ### Cause #18210 added `_sole_nvidia_card_mib()`, which reads nvidia-smi's output with `subprocess.run(..., text = True)` but no `encoding`. That falls back to `locale.getencoding()`, the ANSI codepage on Windows, which is exactly what this lint exists to catch. The other three `subprocess.run(text = True)` calls in `sd_cpp_backend.py` already pass `encoding = "utf-8", errors = "replace"`. The new probe just missed it. ### Fix The probe now passes `encoding = "utf-8", errors = "replace"` like its neighbours. nvidia-smi's CSV output is ASCII, so what the probe parses does not change. A stray byte can no longer raise inside the probe. ### Verification - `tests/test_text_io_encoding.py`: fails on `main` on this line and passes with this change. - `tests/test_sd_cpp_backend.py` also passes: 1289 passed across both files. - ruff is clean.
What was wrong
When torch is the CPU build but sd.cpp runs a Vulkan or CUDA build, Unsloth Studio cannot measure the card, so memory mode Auto always passes
--offload-to-cpu --clip-on-cpu --vae-on-cpu. In sd.cpp that puts every weight in anonymous host RAM and turns off its own auto-fit placement. On Linux Mint with an RX 9060 XT 16 GB and 15 GB of RAM, MiniMax-H3-Image Q4_K_M is a 12132 MiB diffusion model, a 17375 MiB Qwen3-VL text encoder and a 2229 MiB VAE. sd-server reached 13.75 GB RSS and was OOM-killed on the first render, with the card at 452 MiB. The user saw "The native image renderer stopped unexpectedly." This still happens on v0.1.919-beta.The fix
For an unmeasured card under Auto, Unsloth Studio drops the offload when system RAM (available minus the small-host reserve) cannot hold all the weights and the card's free VRAM holds the diffusion model plus 1.5 GiB. Free VRAM is read without a device context: amdgpu sysfs for an AMD Vulkan device, nvidia-smi for CUDA. sd.cpp's auto-fit then places the text encoder and VAE itself. Everything else is unchanged: plenty of RAM still offloads (#13666); a diffusion model the card cannot hold, an unreadable reading, a non-AMD Vulkan device or several AMD cards keep the offload; an explicit CPU offload, Balanced or Low VRAM is always honored.
How it was tested
sd_cpp.placement: offloading 31733 MiB of weights exceeds host RAM (13455 MiB available); the card's 16032 MiB free holds the 12131 MiB diffusion model, so auto-fit places themand rendered 1024x1024 in 36.0 s, then 18.1 s, with a 15.6 GB VRAM peak, 5.8 GB RAM peak and no OOM kill. Without the change it was OOM-killed on every attempt, on both v0.1.918-beta and v0.1.919-beta.Not tested
The FLUX.3 prompt upsampler still starts without counting memory already in use; that is left for a separate change.