Skip to content

installer: add pipe-safe --cpu / --vulkan flags for the llama.cpp backend (#7213) - #7675

Closed
LeoBorcherding wants to merge 3 commits into
unslothai:mainfrom
LeoBorcherding:fix-7213-installer-cpu-flag
Closed

LeoBorcherding wants to merge 3 commits into
unslothai:mainfrom
LeoBorcherding:fix-7213-installer-cpu-flag

Conversation

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Problem

Forcing the CPU llama.cpp build doesn't work through the documented pipe:

UNSLOTH_LLAMA_CPP_BACKEND=cpu curl -fsSL https://unsloth.ai/install.sh | sh

The VAR=x prefix binds to curl, not the sh on the other side of the pipe, so
setup.sh reads the backend as unset, auto-detects the reporter's Intel iGPU, and
installs the Vulkan prebuilt (their UNSLOTH_PREBUILT_INFO.json: force_cpu: false,
a vulkan asset). The selection logic is correct: when the var actually reaches the
environment, setup.sh forwards --force-cpu and the install honours it. It just
never arrives. install.sh already documents this same pipe footgun for --no-torch
and solves it with a flag; there was none for the backend.

Fix

Add --cpu / --vulkan flags to install.sh and install.ps1, mirroring
--no-torch, that export UNSLOTH_LLAMA_CPP_BACKEND so it survives the pipe:

curl -fsSL https://unsloth.ai/install.sh | sh -s -- --cpu

Also forwarded across the WSL reroute, and the header now shows the correct
post-pipe env form. No change to backend selection, prebuilt resolution, or setup.
An explicit flag wins over an inherited env var, matching the other flags.

Tests

New tests/sh/test_llama_backend_flag.sh (11 checks) pins the flag to env-var
wiring in both installers and the WSL forward; existing test_install_pipe_safety.sh
and test_with_llama_cpp_dir_flag.sh still pass; both installers parse clean.

Not verified end to end (no Intel-iGPU host here). The reporter can confirm with
... | sh -s -- --cpu, then check force_cpu: true and a cpu asset in the marker.

…kend (unslothai#7213)

UNSLOTH_LLAMA_CPP_BACKEND=cpu placed before `curl ... | sh` binds the var to
curl, not the piped sh, so setup.sh never sees it and installs the auto-detected
backend (Vulkan on an Intel/other iGPU, which crashed the reporter). Add --cpu /
--vulkan flags to install.sh and install.ps1 that export the env var setup.sh
already reads, so `| sh -s -- --cpu` works, forward it across the WSL reroute,
and document the correct post-pipe env form. No selection-logic change.
@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

@codex review

@LeoBorcherding
LeoBorcherding marked this pull request as ready for review July 31, 2026 17:01
@LeoBorcherding LeoBorcherding self-assigned this Jul 31, 2026
@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

Overall this is a clean, well-scoped fix and the root cause (the VAR=x curl | sh binding) is correct. The env-var wiring, WSL-reroute forward, and doc fix all look right. One medium item on the PowerShell side, plus two minor ones.

1. (medium) install.ps1 leaks $env:UNSLOTH_LLAMA_CPP_BACKEND into the caller's session on the iex path

irm ... | iex runs in the current PowerShell session, so $env:UNSLOTH_LLAMA_CPP_BACKEND = $LlamaCppBackendFlag (install.ps1:176) persists after the install finishes. A second install in the same session would then silently reuse the backend even with no --cpu passed.

This is the exact hazard the script already guards elsewhere: UNSLOTH_LOCAL_LLAMA_CPP_DIR (2810) is cleared with Remove-Item Env: after the setup run (2832), same for UNSLOTH_STUDIO_HOME (2801) and STUDIO_LOCAL_REPO (2786). Suggest matching that pattern: set the env var next to the setup.ps1 invocation and Remove-Item Env:UNSLOTH_LLAMA_CPP_BACKEND -ErrorAction SilentlyContinue after, or save/restore any prior value.

Counterpoint: $env:UNSLOTH_VERBOSE at 181 also leaks and isn't cleaned, so there's some precedent to not bother, but a sticky backend is closer to the local-dir case than to a debug toggle. install.sh isn't affected: the piped sh is a subshell, so its export dies on exit.

2. (minor) --cpu and --vulkan together resolve silently to last-wins

Passing both just takes whichever the arg loop sees last, with no signal. setup.sh already warns on an unrecognized backend value, so a short "both set, using X" warning here would be consistent. Low priority since it's user error.

3. (nit) the ps1 assertions in the new test pin exact interior whitespace

'"--cpu" { $LlamaCppBackendFlag = "cpu" }' will break on a harmless realignment of install.ps1. The sibling test_with_llama_cpp_dir_flag.sh matches just the flag token; suggest loosening to the semantic substring (e.g. $LlamaCppBackendFlag = "cpu").


Nice touches: the WSL-reroute forward is correctly ordered after the flag to export step so it picks up both the flag and a user-set env var, and the header comment calling out the before-vs-after-pipe footgun is exactly the confusion that opened #7213.

…leak into the iex session

Set the flag-derived backend only around the setup.ps1 invocation with
save/restore, mirroring UNSLOTH_STUDIO_HOME / UNSLOTH_LOCAL_LLAMA_CPP_DIR, so
`irm | iex` doesn't leave $env:UNSLOTH_LLAMA_CPP_BACKEND set in the caller's
session and a later same-session install can't silently reuse it. Pin the
cleanup in the wiring test and loosen the whitespace-exact ps1 assertions.
@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

🌊 Poseidon Review 🐚

Reviewed 3785117.

Two findings remain open, both at P2/P3. The misleading comment at install.sh:16 (raised by all 40 earlier runs) is still unchanged, and the env-var leak in install.ps1 on early --with-llama-cpp-dir failure is still structurally present. Neither is a blocking issue for ROCm specifically, but the comment one is the higher-value fix since it directly undermines the point of the PR.

P2 - Rewrite the anti-example comment so it does not read as the correct form
install.sh:16

Line 16 reads # Note the env var goes AFTER the pipe, on the \sh` side: `UNSLOTH_LLAMA_CPP_BACKEND=cpu curl ... | sh`. The colon after "side" introduces what English grammar parses as "here is how", but the example is the **broken form** (var bound to curl, not to sh`). Line 15 already shows the correct form; line 17 clarifies it is wrong, but a reader who stops at line 16 walks away with the opposite lesson. All 40 earlier review runs flagged this.

# Placing the env var before `curl` (`UNSLOTH_LLAMA_CPP_BACKEND=cpu curl ... | sh`)
# binds it to curl, not the piped sh, so the installer never sees it (#7213).
# Use the form above (env var after the pipe) or `sh -s -- --cpu`.

P3 - Env var leaks when --with-llama-cpp-dir validation fails early
install.ps1:2807

$env:UNSLOTH_LLAMA_CPP_BACKEND is set at line 2807, but the inner try that guards the restore does not start until line 2823. If the user passes --cpu --with-llama-cpp-dir nonexistent, Exit-InstallFailure at line 2813 throws (non-Tauri path) before entering the try, so the inner finally (line 2826) that restores the env var never runs. The outer finally at line 2944 only does venv rollback. In the irm | iex session this leaves UNSLOTH_LLAMA_CPP_BACKEND set, which silently changes the next install. The comment at line 2801 promises save/restore; this path breaks that promise. The same structural gap exists for the pre-existing UNSLOTH_STUDIO_HOME and UNSLOTH_TAURI_MODE saves at lines 2791-2800. Also raised by LeoBorcherding in the PR conversation.

Move lines 2805-2807 (the save/set block) into the try at line 2823, just above the & $UnslothExe call. Then any throw from the path validation is already inside the try, and the finally will restore the var.

@oobabooga

oobabooga commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

@LeoBorcherding I think your implementation is correct and good to merge, but I don't think we should add new flags for this as

curl -fsSL https://unsloth.ai/install.sh | UNSLOTH_LLAMA_CPP_BACKEND=cpu sh

for Linux/macOS/WSL, or

$env:UNSLOTH_LLAMA_CPP_BACKEND='cpu'; irm https://unsloth.ai/install.ps1 | iex

already work. Maybe we should just document this in the README somewhere visible/convenient.

@LeoBorcherding

Copy link
Copy Markdown
Collaborator 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: 37851175d7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread install.ps1
# an inherited value for this run.
$previousLlamaBackend = $env:UNSLOTH_LLAMA_CPP_BACKEND
$hadPreviousLlamaBackend = ($null -ne $previousLlamaBackend)
if ($LlamaCppBackendFlag) { $env:UNSLOTH_LLAMA_CPP_BACKEND = $LlamaCppBackendFlag }

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 Move backend override under the setup try/finally

When --cpu or --vulkan is combined with --with-llama-cpp-dir pointing at a missing directory, this line sets the process-scoped UNSLOTH_LLAMA_CPP_BACKEND before the later path validation calls Exit-InstallFailure, so the restore block in the finally is never entered. In the documented irm | iex / scriptblock path that runs in the caller's PowerShell session, the backend override is left behind and can affect subsequent commands or installs; validate the path before setting this env var, or include the validation inside the same try/finally.

Useful? React with 👍 / 👎.

@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

Closing this. ooba's call was right, and the ground moved under it since.

UNSLOTH_LLAMA_CPP_BACKEND already covers the case: it landed in #7228 the day after #7213 was filed. The bug turned out to be the variable sitting before curl instead of after the pipe, not a selection bug, and the reporter confirmed the post-pipe form works. Since then main documents the variable in the README under "Vulkan, custom llama.cpp backends", and #8520 made the backend switchable from the UI. Two installer flags are a third spelling of a knob that already has two.

Merging current main and taking the flags back out leaves a zero-line diff, so there is nothing here worth carrying forward. The one thing still undocumented is the broken form itself, UNSLOTH_LLAMA_CPP_BACKEND=cpu curl ... | sh, which binds to curl and silently auto-detects. That is a few lines and belongs in the next README pass, not in a PR of its own.

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.

3 participants