Skip to content

Studio images: render on the NVIDIA card when torch drives no GPU - #16853

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
debojyoti-dev:fix/sd-cpp-cuda-without-torch-16803
Nov 7, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
debojyoti-dev:fix/sd-cpp-cuda-without-torch-16803

Conversation

@debojyoti-dev

Copy link
Copy Markdown
Contributor

Fixes #16803

Problem

On a desktop install without a GPU torch (the "Chat only (GGUF)" option, or a CPU wheel), image generation runs through the native stable-diffusion.cpp engine, and image_install_accelerator("cpu") always asked for the plain CPU build. So on an NVIDIA box chat ran on the card (llama.cpp ships its own CUDA build) while every image rendered on the CPU with the GPU idle, which is what the screenshots in the issue show. The CPU-torch route already picks the Vulkan build beside AMD/Arc cards, but never anything beside NVIDIA.

Fix

  • diffusion_engine_router._cuda_gpu_for_cpu_torch(): when torch resolves to CPU, the installed llama.cpp is a CUDA build (its install marker's runtime_line, new sd_cpp_engine.llama_cpp_cuda_major()), the inventory lists an NVIDIA card, the host is Windows x64 / Linux x86_64 and CUDA_VISIBLE_DEVICES does not hide every card, the image engine asks for the CUDA sd.cpp build. The CUDA major comes from llama.cpp, whose cudart dir sd.cpp already borrows, so the driver is known to run it.
  • Placement: a CUDA build on a CPU torch renders on a card torch never measured, so it gets the same auto offload as the CPU-torch Vulkan route.
  • Fallback: cuda -> cpu is a ladder rung on the CPU-torch route only (_cuda_cpu_rung_applies). A CUDA build that cannot start there is recorded as proven and the same load steps down to the CPU build, instead of ending on diffusers (which has no torch to run on). A CUDA torch is unchanged: no CUDA records, no CPU rung.
  • The load route takes the GPU claim for this build like it does for the Vulkan one.
  • Opt out with UNSLOTH_DIFFUSION_SD_CPP_VULKAN_FALLBACK=0 (the same knob that gates the Vulkan route and the ladder).

Testing

Windows 11, RTX 3050 Laptop 4 GB (driver 566.07), global Python without torch, so the same as a chat-only install. llama.cpp isn't installed on this PC, so only its two lookups were stood in for: its CUDA marker (cuda12) and its cudart directory (a folder with cudart64_12/cublas64_12/cublasLt64_12).

  • Real engine selection + real install of the pinned release:
    • main: image_install_accelerator('cpu') = auto, installs win-cpu-x64, device listing shows only the CPU.
    • this PR: installs win-cuda12-x64, --list-devices shows CUDA0 NVIDIA GeForce RTX 3050 Laptop GPU, engine sd_cpp.
    • this PR without the cudart dir (CUDA build cannot start): the same select_and_activate_engine call steps down and ends on the win-cpu-x64 build with engine sd_cpp.
  • Real Z-Image-Turbo Q2_K render (512x512, 4 steps, seed 7) through SdCppBackend.begin_load + generate:
    • main: device=cpu, peak VRAM 0 MiB, GPU 0%, 170 s.
    • this PR: device=cuda, offload --offload-to-cpu --clip-on-cpu --vae-on-cpu --diffusion-fa --vae-tiling, peak VRAM 2487 MiB, GPU 100%, 71 s.
  • New tests/test_sd_cpp_cuda_cpu_torch.py (34 tests: routing gates, CUDA torch unchanged, the CPU rung only on a CPU torch, the loader-refusal ladder, same-load step-down, the pinned release's CUDA assets). Two existing tests now pin the host type they assert about (test_only_rocm_and_vulkan_have_a_rung_below_them, test_the_preflight_never_touches_a_non_rocm_accelerator), and the Vulkan-route fixture stubs the llama.cpp lookup so it never reads the real install.
  • All test_sd_cpp_*, test_diffusion_engine_* and test_diffusion_routes suites: same failures as a git archive of main on this Windows box (env-only: cudnn 3, engine 6, h3_matrix 36, install 2, moss_image 1, qwen21_edit 1, rocm_vulkan_fallback 2, vram_budget 10, diffusion_routes 25), none new.

Not tested: Linux (the gate and assets are covered by unit tests), multi-GPU NVIDIA hosts.

A chat-only install has no torch and a CPU wheel sees no card, so the
image engine always installed the CPU stable-diffusion.cpp build, even
beside an NVIDIA card that llama.cpp already renders chat on.

When torch resolves to CPU, llama.cpp's install marker says it is a
CUDA build and the inventory lists an NVIDIA card, the image engine now
installs the matching CUDA sd.cpp build and places loads on it with the
same auto offload the CPU-torch Vulkan route uses. CUDA gets a CPU rung
on that route only: a CUDA build that cannot start is proven
unrunnable on the spot and the same load steps down to the CPU build,
since nothing else renders without torch. A CUDA torch is unchanged.

Fixes unslothai#16803
…election source test, trim comments

The in-load retry now only runs where torch drives no GPU, so ROCm / XPU hosts keep today's diffusers fallback for the load.
The source test matches the wrapped condition, and the CUDA route tests stub the llama.cpp marker rather than the new helper.

[skip ci]
@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 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T10:07:45.719582Z 34d7273 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 34d7273030

ℹ️ 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".

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.

[Bug]: Image generation falls back to CPU/RAM on Windows 11 with RTX 3060 while text generation correctly uses GPU

2 participants