Skip to content

Studio sd.cpp: verify a new bundle starts before it replaces the installed one; Windows ROCm without the HIP SDK uses Vulkan - #15371

Merged
danielhanchen merged 5 commits into
mainfrom
studio-sdcpp-windows-amd-fallback
Oct 26, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-sdcpp-windows-amd-fallback

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On Windows with an AMD GPU and a ROCm torch, loading a native (stable-diffusion.cpp) image model such as unsloth/MOSS-Image-SFT-GGUF could leave Studio with no usable sd.cpp binary at all:

  1. The Unsloth mirror release master-982-0518b34-ud331c66 publishes no Windows ROCm asset (it ships win-cpu, win-cuda12, win-cuda13, win-vulkan), so a ROCm request falls back to the upstream leejet/stable-diffusion.cpp win-rocm-10.1.0 zip.
  2. That zip carries amdhip64_7.dll but its stable-diffusion.dll imports hipblas.dll, which only the AMD HIP SDK provides. Without the SDK on PATH, sd-cli.exe and sd-server.exe die in the Windows loader (exit 0xC0000135, "hipblas.dll was not found").
  3. The installer extracted that bundle, swept the working Vulkan build that was already installed as "superseded", and only then found out the new binaries could not start. The backend removed the unrunnable copies and the load failed with sd-cli (stable-diffusion.cpp) binary is unavailable.

This PR fixes all three steps.

Changes

studio/install_sd_cpp_prebuilt.py

  • Before a downloaded bundle touches the managed tree, it is extracted to a scratch directory beside the tree and its sd-cli / sd-server are started with --help (bundle directory first on the library path, loader error dialogs suppressed on Windows). If the OS loader refuses one (Windows 0xC0000135 / 0xC0000139 / 0xC0000142, or the POSIX dynamic loader's "error while loading shared libraries"), the install raises UnrunnableBuildError and the existing install is left exactly as it was. Any other outcome (a normal exit, an exec error, a timeout) is not treated as a refusal.
  • CUDA bundles are not probed: they load a host or llama.cpp CUDA runtime that the backend adds at spawn time, and the Windows cudart archive is fetched after extraction.
  • Kill switch: UNSLOTH_SD_CPP_VERIFY_INSTALL=0 restores the previous unverified install.
  • The check runs on every OS for non-CUDA bundles. On Linux, the upstream ROCm zip on a host without the ROCm 7 userspace is now refused at install time (libamdhip64.so.7 missing) and the Vulkan build is installed, where before the ROCm build was installed and the existing runtime ladder moved the host to Vulkan later.

studio/backend/core/inference/sd_cpp_backend.py

  • preferred_accelerator: on Windows, a ROCm request starts at the Vulkan build unless hipblas.dll is found in System32 or on PATH. This is a file search only; nothing is loaded into the Studio process. UNSLOTH_SD_CPP_WINDOWS_ROCM=1 always takes the ROCm build (the previous behaviour), 0 never takes it, and UNSLOTH_DIFFUSION_SD_CPP_VULKAN_FALLBACK=0 also keeps ROCm.
  • ensure_sd_cpp_binary / ensure_sd_server_binary: when the installer raises UnrunnableBuildError, the failure is recorded as proven for that accelerator, and the next rung (rocm -> vulkan -> cpu) is installed in the same call, so a fresh host ends up on a working build and later loads start there. The record is keyed on the pinned tag, which the replacement build also carries, so the record holds until the pin moves.

MOSS-Image not listed under On Device

That part is #15168 (moss_image registered as an image GGUF architecture). It merged on 2026-10-26, after v0.1.910-beta was tagged (7dbc8b9ec8, 2026-10-23), so that release still classifies MOSS-Image GGUFs as text-generation. On current main they are listed for Images. No change is needed here.

Testing

  • New studio/backend/tests/test_sd_cpp_install_verify.py (14 tests). On the merge base, 8 of them fail: the refused bundle replaces the working build, the Windows ROCm request stays on ROCm, and a fresh ROCm ensure returns the unrunnable build. On this branch all 14 pass. The other 6 are guards that pass on both (kill switches, a bundle that starts still replaces the old one, other hosts and accelerators unchanged).
  • Existing suites test_sd_cpp_install.py, test_sd_cpp_rocm_vulkan_fallback.py, test_sd_cpp_vulkan_cpu_torch.py, test_sd_cpp_pin_upgrade.py: 567 passed, 1 skipped on this branch.
  • Real prebuilts on a Windows 11 Strix Halo (Radeon 8060S) box, with the merge base and this branch run in the same job and HIP SDK DLLs kept off PATH: see the results below.

Windows 11 runner (Radeon 8060S, driver DLLs amdhip64_6.dll / amdhip64_7.dll in System32, no HIP SDK, no hipblas.dll anywhere on the loader path). The merge base and this branch ran in the same job, both with the real mirror Vulkan zip and the real upstream win-rocm-10.1.0 zip:

state ROCm install over a working Vulkan build Vulkan binaries left binaries in the tree (--help exit) preferred_accelerator("rocm") fresh ensure_sd_server_binary(accelerator="rocm")
merge base installed 0 of 2 sd-cli.exe 0xC0000135, sd-server.exe 0xC0000135 rocm ROCm sd-server.exe, exits 0xC0000135
this branch refused (UnrunnableBuildError) 2 of 2 sd-cli.exe 0, sd-server.exe 0 vulkan Vulkan sd-server.exe, exits 0

On the merge base the backend's own _server_binary_runnable also returns False for the swept-in server. That is the "is not runnable; removing it so it reinstalls" path that ended the load with no binary.

@danielhanchen danielhanchen self-assigned this Oct 26, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-10-26T14:27:13.183515Z 2155687 Manual request
🔒 Security Review ✅ Completed 2026-10-26T13:02:24.891560Z c0c935d 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: c0c935da6a

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: c0c935da6a

ℹ️ 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 +2592 to +2594
note_accelerator_runtime_failure(
klass, proven = True, fingerprint = {"bundle": want, **_host_fingerprint()}
)

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 Match fallback failure records against the requested pin

When the fallback bundle comes from an upstream release or a latest-release fallback, its actual tag differs from _pinned_tag(). This records the requested pin as bundle, but _accelerator_fingerprint() reads the installed record's actual tag, so _fingerprint_still_applies() immediately treats the proven failure as stale. Subsequent loads therefore request the rejected ROCm build again and repeat the download/install ladder instead of retaining the working fallback. Compare requested pins consistently (the install record already has requested_tag), or update the failure fingerprint to match the successfully installed fallback.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 2155687. The refusal is now recorded after the replacement build installs, with the fingerprint _accelerator_fingerprint() reads back from that build's own install record, so an upstream or latest-release fallback no longer retires the record at once. If no rung installs, the pin is used as before. New test_the_refusal_record_survives_a_fallback_from_another_release fails on the previous head and passes here; all sd.cpp test files: 1498 passed, 1 skipped.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 2155687425

ℹ️ 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 7d9f122 into main Oct 26, 2026
82 of 83 checks passed
@danielhanchen
danielhanchen deleted the studio-sdcpp-windows-amd-fallback branch October 26, 2026 16:37
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 28, 2026
…ts whose ROCm comes only from the torch wheel (unslothai#15430)

On a Linux AMD host whose ROCm userspace comes only from the pip torch
wheel (no `/opt/rocm`), an image load that wants sd.cpp fetches leejet's
ROCm prebuilt (~295 MB), and that build cannot start: it imports
`libamdhip64.so.7`, `libhipblas.so.3` and `librocblas.so.5` from the
host and bundles none of them. Reported in the Discord help thread
(CachyOS, RX 9070): on a slow connection the load spins while that
bundle downloads (the 300 s timeout is per read, so a trickling fetch
never trips it), and deleting the half-written zip "fixes" it only
because the SHA check then fails and the load falls back to the Vulkan
build already installed.

unslothai#15371 already recovers once the fetch completes: the bundle fails its
start check, the load steps down to Vulkan, and ROCm is recorded as
unrunnable. Two gaps remain, and this PR closes both: the 295 MB fetch
still happens first, and a fetch that stalls or is cut short records
nothing, so every later load starts it again.

## Cause

The runtime guard, `rocm_runtime_resolvable()`, asks `ctypes.CDLL` in
the Unsloth Studio process. By then torch's ROCm wheel has loaded its
own copies of those three sonames, so the lookup by soname succeeds and
the guard reports a runtime that sd-cli, a separate process, can't see.
Measured on Mint 22.1 with `torch 2.11.0+rocm7.13`:

| check | libamdhip64.so.7 | libhipblas.so.3 | librocblas.so.5 |
|---|---|---|---|
| in-process, before `import torch` | missing | missing | missing |
| in-process, after `import torch` | ok | ok | ok |
| fresh process | missing | missing | missing |

## Change

- `rocm_runtime_resolvable` / `rocm_runtime_missing_sonames` ask a fresh
`python -X utf8 -I` process (env from `utf8_child_env()`), memoised per
process.
- `ensure_sd_cpp_binary` / `ensure_sd_server_binary` check that answer
before any fetch: a ROCm request on a host whose loader can't find the
runtime installs the Vulkan rung instead and records ROCm as proven
unrunnable. `preferred_accelerator` is unchanged.
- `note_unlaunchable_accelerator_build` marks an exit-127 strike proven
when the runtime is absent, so an already-installed ROCm build diverts
after one failure, not two.
- Tests drive the new probe seam instead of patching in-process
`ctypes.CDLL`; the two ambiguous-launch tests pin "runtime loads" so
they no longer follow the test machine.

## Testing

Mint 22.1, RX 9060 XT (gfx1200), no system ROCm, Unsloth Studio from
source with `torch 2.11.0+rocm7.13.0` (HIP 7.13.99004, the reporter's),
Images > load `unsloth/MOSS-Image-SFT-GGUF` UD-Q4_K_XL, clean
`~/.unsloth/stable-diffusion.cpp` and no recorded fallback before each
run. "After" is this PR's diff applied to the same main commit.

Before, main `a20b732`:
```
downloading sd-master-0518b34-bin-Linux-Ubuntu-24.04-x86_64-rocm-10.1.0.zip -> ~/.unsloth/stable-diffusion.cpp/...
sd_cpp.unrunnable_build: ...rocm-10.1.0.zip: sd-cli cannot start on this host: the dynamic loader refused it ...
downloading sd-master-982-0518b34-ud331c66-bin-Linux-Ubuntu-22.04-x86_64-vulkan.zip -> ...
verified ...vulkan.zip starts on this host
sd_cpp.loaded: repo=unsloth/MOSS-Image-SFT-GGUF ... mode=server
```

After, this PR:
```
sd_cpp.rocm_runtime_absent: the ROCm build needs the host ROCm 7 runtime and the loader cannot find libamdhip64.so.7, libhipblas.so.3, librocblas.so.5 (...); installing the vulkan build instead
downloading sd-master-982-0518b34-ud331c66-bin-Linux-Ubuntu-22.04-x86_64-vulkan.zip -> ...
verified ...vulkan.zip starts on this host
sd_cpp.loaded: repo=unsloth/MOSS-Image-SFT-GGUF ... mode=server
```

Both runs end with MOSS loaded on the GPU; the difference is the 295 MB
ROCm fetch, which this PR skips. Mint's link is fast (the fetch took ~8
s), so the reporter's stalled-download case was not reproduced live; it
is covered by
`test_an_unloadable_rocm_runtime_installs_vulkan_without_fetching_rocm`.

| suite | result |
|---|---|
| every `sd_cpp*`, `diffusion_engine_router`, `diffusion_routes`,
`h3_native*` test file, on Mint | 1900 passed, 1 skipped, measured at
`e043dbf`; since then only `b3db89b` (probe child env), with
`test_chat_text_encoding` + the ROCm files at 378 passed |

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han <[email protected]>
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 29, 2026
…p build when its bin dir is not on PATH (unslothai#15853)

Reported on Reddit: [Anyone manage to get MOSS Image to
load?](https://www.reddit.com/r/unsloth/comments/1xecbd5/anyone_manage_to_get_moss_image_to_load/).
After unslothai#15371 moved Windows AMD hosts without the HIP runtime to Vulkan,
the reporter asked what to install to use the HIP build instead.

## Problem

On Windows AMD, image generation uses the Vulkan stable-diffusion.cpp
build unless `hipblas.dll` is found on `PATH`. The upstream `win-rocm`
bundle ships its own `amdhip64_7.dll` but imports `hipblas.dll`, which
only the AMD HIP SDK provides. The SDK installer never puts that DLL's
folder on `PATH`: HIP SDK 7.2.3's `ROCm_SDK_Core.msi` sets only
`HIP_PATH` and `HIP_PATH_72`, and AMD's install guide tells users to add
the `bin` folder to `PATH` themselves.

As a result, a user who installs the HIP SDK, as the existing log line
suggests, still gets Vulkan. Detection
(`windows_hip_runtime_missing_dlls()`), the installer's start probe
(`_probe_env()`) and every sd-cli/sd-server launch (`runtime_env()`)
look only at `PATH`.

## Fix

- New `windows_hip_sdk_bin_dirs()` in `install_sd_cpp_prebuilt.py`. It
returns the HIP SDK `bin` folders from `HIP_PATH`, then `ROCM_PATH`,
then `C:\Program Files\AMD\ROCm\<ver>` (newest first). It keeps only 7.x
folders, meaning `hipblas.dll` sits next to `amdhip64_7.dll`.
- Detection searches those folders in addition to `PATH`. For a ROCm
bundle, `_probe_env()` and `runtime_env()` append them after the
caller's `PATH`.
- The log line, the fallback reason and the installer hint now say HIP
SDK 7.x, mention the `HIP_PATH` lookup, and link AMD's download page.

## Behavior boundaries

- Without a HIP SDK, Windows AMD still starts at the Vulkan build, as on
main.
- A HIP SDK already on `PATH` keeps priority.
- Non-ROCm bundles and non-Windows hosts get the same child environment
as before.
- A 6.x SDK folder is not used: its `hipblas.dll` binds
`amdhip64_6.dll`, not the bundle's HIP 7 runtime.
- A host with a saved ROCm failure, for example from an earlier forced
attempt without the SDK, stays on Vulkan until the record is cleared
with `DELETE /api/settings/diffusion-accelerator-fallback`. This is
main's existing policy, the same as on Linux ROCm hosts.
- No UI changes.

## Verification

- 7 new tests in `test_sd_cpp_install_verify.py`. Six of them fail on
main, including `preferred_accelerator("rocm")` returning `"vulkan"`
when the SDK is found only through `HIP_PATH`.
- The sd.cpp, diffusion router, H3 and video suites pass: 2402 passed, 3
skipped.

Live A/B on Windows 11 with a Radeon 8060S (gfx1151):
- The HIP SDK 7.2.3 files were extracted to the layout its MSIs install,
and `HIP_PATH` was set the way the MSI sets it. `PATH` was left
unchanged.
- Each case used a fresh Studio home, ran Studio's own
`preferred_accelerator("rocm")` and `ensure_sd_cpp_binary()`, then
generated an SD-Turbo image through `runtime_env()`.

| Case | Accelerator chosen | Installed build | Generation |
|---|---|---|---|
| main, SDK installed | vulkan | win-vulkan | Vulkan0 |
| main, SDK installed, `UNSLOTH_SD_CPP_WINDOWS_ROCM=1` | rocm | ROCm
refused with `0xC0000135`, fell back to win-vulkan, ROCm recorded as
failed | Vulkan0 |
| main, SDK `bin` added to `PATH` by hand | rocm | win-rocm-10.1.0 |
ROCm0, gfx1151 |
| this PR, SDK installed | rocm | win-rocm-10.1.0, start probe passed |
ROCm0, gfx1151 |
| this PR, no SDK | vulkan | win-vulkan | not run |

With this PR the result matches the manual `PATH` edit on main, down to
an identical output image.

---------

Co-authored-by: Daniel Han <[email protected]>
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 30, 2026
…sing-DLL box (unslothai#16031)

Reported on Reddit in [a reply to "Anyone manage to get MOSS Image to
load?"](https://www.reddit.com/r/unsloth/comments/1xecbd5/anyone_manage_to_get_moss_image_to_load/):
the hipblas.dll box still pops up on load after switching to Vulkan.

## Problem

On Windows, an sd.cpp bundle that cannot load its DLLs (for example a
ROCm bundle installed before unslothai#15371 on a host without the HIP SDK) opens
a modal "sd-server.exe - System Error: ... hipblas.dll was not found"
box when the backend test-launches it. The probes pass only
`windows_hidden_subprocess_kwargs()`, which hides the console but not
the loader's hard-error box. The installer's `probe_binary_starts` and
`_vulkan_probe` already suppress it; the backend's probes did not.

The child stays blocked until someone clicks OK, so an unattended probe
hits its timeout instead of reading `0xC0000135`:

- `_server_binary_runnable` returns True from its timeout branch, so an
unrunnable managed binary is kept instead of being reinstalled.
- With Vulkan requested, `ensure_sd_server_binary` probes again under
`_install_lock`: a second box and another 20 s.
- With ROCm still requested (`UNSLOTH_SD_CPP_WINDOWS_ROCM=1`, or
`hipblas.dll` present but another HIP DLL missing), the broken binary is
returned and later probes open more boxes.

## Fix

`windows_loader_error_boxes_suppressed()` in
`utils/subprocess_compat.py` ORs `SEM_FAILCRITICALERRORS` into the
process error mode around a test-launch, so the child inherits it and
exits at once with `0xC0000135`. It wraps the four sd.cpp test-launches:
`_server_binary_runnable`, `_sd_cpp_probe_output`,
`sd_cpp_binary_identifies` and `SdCppEngine.version`.

The error mode is process-wide and these probes can overlap the
installer's own probe, so all holders, including the installer's
`_windows_error_mode_suppressed` (still `0x8003`), share one record: the
mode before the first holder plus the current holders' flags, restored
when the last one leaves. The record lives in `sys.modules` because the
installer and the backend import this module under different names.

## Boundaries

- `windows_hidden_subprocess_kwargs` and its ~95 callers are unchanged;
the error mode is parent process state, not a `CreateProcess` argument.
- The long-lived spawns are unchanged; the probes gate them.
- The installer still runs standalone.

## Verification

Windows 11, Radeon 8060S, no HIP SDK, with the upstream ROCm bundle
installed without the start check (as v0.1.911-beta did). Each scenario
ran against `main` (A) and this PR (B) in the logged-on desktop session:

| Scenario | A: `main` | B: this PR |
| --- | --- | --- |
| `_server_binary_runnable(sd-server)` | box, 20.3 s, returns **True** |
no box, 0.56 s, False |
| `_sd_cpp_probe_output(sd-cli, --help)` | box, 20.3 s | no box, 0.59 s
|
| `sd_cpp_binary_identifies` + `version()` | box, 20.3 s | no box, 0.58
s |
| Upgrade through both `ensure_*`, Vulkan requested | 2 popups, 42.9 s |
no popup, 3.5 s |
| ROCm forced | 3 popups, 60.5 s, keeps the broken ROCm binary | no
popup, 1.2 s, removes it for reinstall |

An installer scope and a probe scope overlapping on the same host, with
the installer leaving first, kept the bit for the probe and restored the
original mode afterwards. On `main` in the non-interactive service
session, the same probe returns in 0.6 s with no box, which is why
headless runs never showed this.

`tests/test_sd_cpp_probe_error_mode.py` adds 11 tests with a fake
`kernel32`, all failing on `main`. The sd.cpp, router, hires and Vulkan
probe suites pass (1798 passed, 9 skipped).

---------

Co-authored-by: Daniel Han <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant