Skip to content

Studio: validate legacy sd binary discovery - #8560

Merged
danielhanchen merged 14 commits into
unslothai:mainfrom
Lyxot:fix/studio-sd-discovery
Aug 13, 2026
Merged

danielhanchen merged 14 commits into
unslothai:mainfrom
Lyxot:fix/studio-sd-discovery

Conversation

@Lyxot

@Lyxot Lyxot commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • validate ambiguous legacy sd executables found through PATH before accepting them as stable-diffusion.cpp
  • ignore unrelated commands without modifying them, allowing the existing managed sd-cli installer path to continue
  • preserve explicit overrides, normal sd-cli discovery, genuine legacy sd binaries, and the existing MiniMax-H3 capability gate

Root cause

Native diffusion discovery accepted the first executable named sd on PATH solely by filename. Unrelated tools using that generic name could therefore be treated as user-supplied stable-diffusion.cpp binaries and suppress managed installation.

The legacy PATH fallback now performs a bounded, read-only --help identity probe. It accepts stable-diffusion.cpp project output or the option signature present since the original legacy CLI, and logs a precise rejection for unrelated candidates.

Potential impact

This may resolve reports where Studio identifies /usr/bin/sd as a stable-diffusion.cpp build that predates MiniMax-H3 and consequently skips installing the pinned managed sd-cli. It applies when that PATH executable is an unrelated command; a genuine manually installed legacy stable-diffusion.cpp binary remains subject to the existing capability gate.

May resolve issue #8507.

Validation

  • 10 focused Studio backend tests covering discovery, installer continuation, and the unchanged H3 gate
  • mutation checks for rejection, legacy acceptance, timeout handling, and Windows hidden-process behavior
  • repository Ruff and formatting hooks

@Lyxot
Lyxot marked this pull request as ready for review August 12, 2026 13:00
@Lyxot
Lyxot requested a review from danielhanchen as a code owner August 12, 2026 13:00
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 7a2d5cb007

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

The legacy PATH probe answers "is this stable-diffusion.cpp". The H3 gate was answering
"does this build carry H3" and reporting the result as if it had answered the first, so an
unrelated binary reached through SD_CLI_PATH was described as a stable-diffusion.cpp build
predating MiniMax-H3 -- true of every program that is not stable-diffusion.cpp, and the
sentence that sent unslothai#8507 looking for a newer build of something never installed.

- share one predicate, help_text_identifies_sd_cpp, between the two callers
- the H3 gate takes ONE --help and derives both answers from it, asking identity first.
  --ref-video is a plain option name that unrelated reference-video tools also expose, so
  returning on the marker alone readmitted the same class of program through the override
- a user-supplied binary that is not stable-diffusion.cpp gets a message saying so, and the
  log lines on the managed path name the actual fault
- memoize the identity verdict per file revision (path + mtime + ctime + size). Discovery
  runs on every load and ensure_sd_cpp_binary resolves twice, so an unrelated "sd" was
  executed once per discovery. Only a verdict the probe produced is cached: a timeout
  leaves the file unchanged, so caching that "no" would blacklist a genuine build until
  Studio restarts
ensure_h3_sd_cpp_binary exists to refuse a build that cannot run H3 before the four-file
bundle is fetched, and its own error said so. _run_load_h3_native called it after the
download loop, so every refusal still cost tens of GB, and a None return -- auto-install
off, an unsupported platform, no network, or a managed copy something else is running out
of -- was not rejected until later still.

- move the preflight and the accelerator fallback above the asset resolution
- check cancel_event before it: the preflight may install the prebuilt and takes no
  cancel_event, so a load cancelled before its worker started paid for an install nobody
  was waiting for. The download loop used to be the first check
- refuse a preflight that produced no binary there, rather than after the downloads. The
  claimed re-vet keeps its own check; that one guards a replacement arriving mid-download
- test_sd_cpp_h3_matrix.py covers platform x GPU vendor x binary state, because the
  combinations are what broke: a CPU-only prebuilt on a CUDA host reaches its refusal
  through the fallback, and that path downloaded the bundle on the way to failing
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for this - the diagnosis is right and I have pushed two commits on top of it.

Confirming the root cause with the real binary

Rather than a fixture, I used the actual Ubuntu package: sd_1.0.0-2_amd64.deb (source rust-sd, universe/utils) installs /usr/bin/sd, so I extracted it and put it on PATH against Studio's real discovery.

tree find_sd_cpp_binary() ensure_h3_sd_cpp_binary()
main returns the Ubuntu sd raises the exact text from #8507
this PR None proceeds to the managed install

So the reporter's binary almost certainly is not an old stable-diffusion.cpp at all. macOS is exposed the same way through brew install sd, and Arch through pacman -S sd.

Worth noting the blast radius was wider than MiniMax-H3: on such a host, plain image generation was also pointed at the wrong program, since ensure_sd_cpp_binary handed the same path back.

The one assumption worth checking against the real artifact

The identity probe is only safe if the binary Studio actually installs identifies itself. That is not stock upstream - it is the Unsloth mirror build, pinned at master-813-bfbef5b-u13b9d92. I downloaded it and ran it:

$ ./sd-cli --help | head -1
stable-diffusion.cpp version unknown, commit unknown

Both of your acceptance branches match it independently - the banner, and all three of --negative-prompt / --cfg-scale / --steps - so identity survives upstream dropping either one. The macOS arm64 and Windows x64 assets in the same release carry the same strings.

What the two commits add

1. Identity and capability are different questions. The gate answered "does this build carry H3" and reported it as if it had answered "is this stable-diffusion.cpp". Every program that is not sd.cpp is missing --ref-video, which is precisely the sentence that sent #8507 hunting for a newer build of something never installed. It now asks identity first and says is not stable-diffusion.cpp when that is the actual fault.

Asking capability first was also a hole your PATH fix closes and the override path did not: --ref-video is a plain option name that unrelated reference-video CLIs expose too, so a binary reached through SD_CLI_PATH was accepted as H3-capable without ever being identified.

The verdict is now memoized per file revision (path + mtime + ctime + size). Discovery runs on every load and ensure_sd_cpp_binary resolves twice, so an unrelated sd was executed once per discovery - a candidate that hangs cost the full 10s each time. Only a verdict the probe actually produced is cached: a timeout leaves the file unchanged, so caching that "no" would blacklist a genuine build until Studio restarts.

2. The H3 preflight now runs before the download. ensure_h3_sd_cpp_binary's own error message promised it refused before the bundle was fetched, but _run_load_h3_native called it after the four-file download loop, and a None return was not rejected until later still. Cancellation is checked first, since the preflight can install the prebuilt and takes no cancel_event.

Testing

Everything is driven against real executables on a real PATH, not mocked shutil.which / subprocess.run:

  • 53 discovery/gate assertions covering precedence (both env overrides, managed root, legacy sibling root, in-tree build, PATH sd-cli), every historical install layout, timeouts, orphaned children, non-zero exits, stderr-only help, invalid UTF-8, 4 MB of output, symlinks, spaces and non-ASCII paths, and parallel discovery. main fails 19, this branch 0.
  • test_sd_cpp_h3_matrix.py, 88 cases over platform x GPU vendor x binary state, asserting every refusal costs zero downloads and that a capable build still loads on each. main fails 48, this branch 0.
  • Full studio backend suite: 22403 passed, with a failure set identical to main (all environment-driven on my box), so nothing regressed. Also green in a clean uv venv carrying only requirements/studio.txt.

sd-server discovery, both env overrides, the managed and legacy-sibling roots and in-tree builds are all untouched, and the installer only ever writes sd-cli / sd-server, so no existing install has a bare sd that could stop being found.

Two pre-existing issues I deliberately left alone, both outside this PR's scope: _sd_cpp_probe_output treats a non-zero --help exit as "cannot tell" and the gate then fails open, and _usable_or_discard_managed unlinks a managed binary without the _tree_claimed_for_install guard its sibling path uses.

danielhanchen and others added 4 commits August 13, 2026 01:11
# Conflicts:
#	studio/backend/core/inference/sd_cpp_backend.py
…es reach it

Mutation testing found the one production change in this branch that could be reverted
silently: forcing fault back to the H3 wording left every test green. That log line is
the same wrong diagnosis unslothai#8507 was reported as, written to the log instead of the user.

The comment above it also claimed only an env override reaches that branch. An in-tree
developer build and a PATH sd-cli do too, since the identity gate covers only the sd stem.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen and others added 3 commits August 13, 2026 01:59
A binary that cannot load a shared library exits 127 from the dynamic loader with nothing
identifying on either stream. That is a CompletedProcess, not an exception, so the previous
rule cached it as a definitive "not stable-diffusion.cpp" against a file that never
changed, and installing the missing library would not get it re-probed until restart.

Memoize a decisive verdict only: identifying output settles it whatever the exit code, and
otherwise only a clean exit is evidence of anything. The case this memo exists for is
unaffected, since Debian/Ubuntu's sd answers --help with rc 0.
Both found in review of the move itself.

Re-check cancellation on the way out of the preflight. The ensure takes no cancel_event
and can spend minutes downloading and extracting the prebuilt, so a cancel arriving during
it is already late; a CPU or MPS target then skipped the claimed accelerator probe (the
only other cancel-aware step) and paid four sequential model_info round trips before the
download loop noticed.

Re-vet EVERY binary after the download, not only a managed one. The managed-only test was
written when the ensure sat right there, so a user-supplied build had been vetted
microseconds earlier and only a concurrent install could have moved underneath it. Vetting
before the download makes that window the whole download -- long enough to rebuild or
repoint an SD_CLI_PATH copy, after which the replacement became the recorded identity that
every later generation compares against.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen and others added 2 commits August 13, 2026 02:18
…nswers

Both from review of the previous round.

The post-download re-vet asked only the capability question. --ref-video is a plain option
name that unrelated reference-video tools expose too, so a swap to one of those cleared a
marker-only check and was then recorded as the vetted build. It now applies the same
identity-and-capability pair the preflight does, off one --help.

And the memo key is not a content revision on Windows: st_ctime there is the CREATION
time, which an in-place overwrite preserves, so a same-sized write that also restores mtime
leaves the whole tuple unchanged. Hashing the binary per lookup would trade the exec this
memo avoids for a full read on a path walked every load, so verdicts expire instead. That
bounds what the key cannot see on any platform, and a minute is still far longer than the
several resolutions inside one load.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

native_device is decided on the accelerator reading and then committed for the life of the
runtime, and moving that reading before the download made the gap between deciding and
committing the whole fetch. The re-check still only ran for a managed binary, so a user's
own build rebuilt inside that window committed a GPU device around a CPU executable --
offload policy and an arbiter claim written against hardware nothing is running on.

The comparison needs a decisive reading, which is why this adds the verdict form rather
than reusing sd_cpp_lists_accelerator_device: that one folds "could not tell" into True,
and against a recorded False it would refuse the very CPU fallback that recorded it.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…mpare it to

Hoisting the verdict out of the condition made every H3 load spawn --list-devices, including
the CPU and MPS targets that deliberately never record a baseline and so cannot use the
answer. On a build that hangs on that flag it cost the full probe timeout before readiness,
for a value the comparison then discarded.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 13, 2026
@unslothai unslothai deleted a comment from Lyxot Aug 13, 2026
@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. You're on a roll.

Reviewed commit: c37e55c915

ℹ️ 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 22cb12e into unslothai:main Aug 13, 2026
34 of 40 checks passed
mahiatlinux added a commit to mahiatlinux/unsloth that referenced this pull request Aug 13, 2026
The identity gate added in unslothai#8560 reads --help for a stable-diffusion.cpp banner before the
H3 capability marker, so these fixtures need one to reach the load path they exercise.
rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Aug 13, 2026
_run_load_h3_native is about assets, and the block that acquires the sd-cli, installs it,
probes its device list and decides native_device now sits at the top of it because unslothai#8560
moved that work in front of the download. Nothing in the block reads a resolved path, so
it reads better as _h3_native_binary returning the three values the rest of the load needs.

Pure move. The behaviour it encodes -- the entry and exit cancellation checks, the
accelerator fallback, the refusal when no binary could be produced -- is unchanged, which
the 168 platform x GPU vendor cases in test_sd_cpp_h3_matrix.py pin without modification.
danielhanchen added a commit that referenced this pull request Aug 13, 2026
…8657)

* studio: claim the H3 companion repos before the preflight, not after it

asset_repos is what stops the delete-cached guard admitting a delete of the H3 companion repos
while a load is pulling from them. #8560 moved the sd-cli preflight ahead of the four-file
download, which is right, but the claim stayed below it, so the window the guard is blind to is
now the whole prebuilt install: minutes, not microseconds. A delete admitted inside that window
is not revoked by claiming the repos afterwards, and it can then remove cached files underneath
the load that follows.

The base_repo and asset_repos claim moves above the preflight. expected_bytes stays where it is,
since the sizes are not known any earlier.

* studio: publish the H3 companion claim with the loading state

* studio: tighten the H3 companion claim comments

* studio: re-read the load guard after reserving the delete scope

* studio: tighten the delete re-read comments
danielhanchen added a commit that referenced this pull request Aug 14, 2026
… smaller card (#8644)

* studio: give minimax-h3 the sd.cpp graph-cut flags so it renders on a smaller card

H3 allocates each module whole on the device when its graph runs, so --offload-to-cpu is
not enough on its own: the 20.5 GB pruned denoiser and the 17 GB Qwen3-VL encoder ask for
the full module at once and cudaMalloc fails on a 12 GB card. The abort lands in the
conditioner before any tensor is resident, so VRAM never rises and it presents as a model
that never loaded rather than an out-of-memory error.

sd.cpp's graph-cut executor is what splits a module into segments that fit, and Studio
never asked for it. The H3 native path now adds --max-vram -1 --stream-layers. A negative
budget auto-detects free VRAM per device, sparing that many GiB, and a module that already
fits merges back into a single segment, so a setup with room pays nothing for this. It is
deliberately not gated on the memory mode: auto and fast are what users reach first and are
exactly the modes that fail.

sd_cpp_supports_graph_cut reads both options out of --help and fails closed, the opposite
default to the H3 gate next to it. sd-cli exits non-zero on an option it does not know, so
an unreadable probe has to emit nothing rather than break a build that renders today. The
CPU runtime never asks, since it allocates from system RAM.

Part of #8636.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* carry the sd.cpp project banner in the h3 help fixtures

The identity gate added in #8560 reads --help for a stable-diffusion.cpp banner before the
H3 capability marker, so these fixtures need one to reach the load path they exercise.

* Gate --stream-layers on --offload-to-cpu for PR #8644

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

2 participants