Skip to content

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

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
LeoBorcherding:studio/sdcpp-ram-aware-offload
Nov 18, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
LeoBorcherding:studio/sdcpp-ram-aware-offload

Conversation

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

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

  • Linux Mint, RX 9060 XT 16 GB, 15 GB RAM, CPU torch, sd.cpp Vulkan: on v0.1.918-beta with this change applied, Auto logged 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 them and 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.
  • Unit tests on a Mac (Apple M5) feed the real component sizes into the load path. On main the Mint case fails with exactly the flags Mint logged; with this change it passes, along with 7 cases that must keep offloading. The sd.cpp and diffusion memory suites: 2121 passed, and the 12 failures fail the same way on main.

Not tested

  • The CUDA path (CPU torch beside an NVIDIA card).
  • Windows AMD Vulkan and multi-GPU or APU AMD hosts, which keep offloading.

The FLUX.3 prompt upsampler still starts without counting memory already in use; that is left for a separate change.

…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.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T08:00:26.124080Z d41df19 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +3121 to +3123
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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +3157 to +3158
if dit + max(CARD_RESERVE_MIB, _user_budget_reserve_mib(card_total or free)) > free:
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: d41df19bef

ℹ️ 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 e27ff3b into unslothai:main Nov 18, 2026
danielhanchen added a commit that referenced this pull request Nov 18, 2026
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants