" and "Updated ..." status lines first.
+ CONNECT_CMD="$(grep -vE '^(export |unset |Unsloth |Updated |Disabled |Warning|Loading)' "$raw" \
| grep -E '[^[:space:]]' | tail -1)"
[ -n "$CONNECT_CMD" ] || guide_fail "could not parse a launch command from connect --no-launch output"
redact "$raw"
diff --git a/.github/scripts/assert-llama-loads.sh b/.github/scripts/assert-llama-loads.sh
index c2ffe27469..62ef80d364 100755
--- a/.github/scripts/assert-llama-loads.sh
+++ b/.github/scripts/assert-llama-loads.sh
@@ -2,7 +2,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
#
-# Assert Studio installed a llama.cpp that loads and runs on THIS macOS. Tests
+# Assert Unsloth installed a llama.cpp that loads and runs on THIS macOS. Tests
# the contract that matters (binaries load and their minimum-OS is <= this host)
# instead of the old "did install.sh fall back to a source build?" grep, since a
# source build with a correct deployment target is a valid outcome.
diff --git a/.github/scripts/assert-prompt-cache.sh b/.github/scripts/assert-prompt-cache.sh
index f5b6b075eb..8c28569f77 100755
--- a/.github/scripts/assert-prompt-cache.sh
+++ b/.github/scripts/assert-prompt-cache.sh
@@ -31,7 +31,7 @@
# (llama_cpp.py:337-340). So default: ~/.unsloth/studio/logs/llama-server/.
#
# is the INTERNAL llama-server port (self._find_free_port(),
-# llama_cpp.py:3489 / :4641) -- a RANDOM port, NOT the Studio port. So we must
+# llama_cpp.py:3489 / :4641) -- a RANDOM port, NOT the Unsloth port. So we must
# NOT filter the log glob by STUDIO_PORT (the brief's `port-`
# glob would never match). We pick the newest llama-*.log instead.
#
diff --git a/.github/scripts/hf-download-with-retry.sh b/.github/scripts/hf-download-with-retry.sh
index 013a459f46..6dec93356a 100755
--- a/.github/scripts/hf-download-with-retry.sh
+++ b/.github/scripts/hf-download-with-retry.sh
@@ -3,7 +3,7 @@
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
#
# Download a single file from a Hugging Face repo with a stall-retry
-# watchdog. Used by the Studio CI workflows so a hung hf-xet transfer
+# watchdog. Used by the Unsloth CI workflows so a hung hf-xet transfer
# kills + retries instead of silently consuming the job's timeout.
#
# Usage: hf-download-with-retry.sh REPO FILE LOCAL_DIR
@@ -35,7 +35,7 @@ REPO="${1:?usage: hf-download-with-retry.sh REPO FILE [LOCAL_DIR]}"
FILE="${2:?usage: hf-download-with-retry.sh REPO FILE [LOCAL_DIR]}"
# LOCAL_DIR is optional. If empty, hf falls back to HF_HUB_CACHE
# (~/.cache/huggingface/hub) which is the desired path for callers
-# that populate HF_HOME for a downstream Studio model load.
+# that populate HF_HOME for a downstream Unsloth model load.
LOCAL_DIR="${3:-}"
# Stall threshold per attempt, in seconds. Override with
diff --git a/.github/workflows/lint-ci.yml b/.github/workflows/lint-ci.yml
index bd859a6e9e..e1f0afd299 100644
--- a/.github/workflows/lint-ci.yml
+++ b/.github/workflows/lint-ci.yml
@@ -13,10 +13,10 @@
# committed YAML / JSON config.
#
# TypeScript and Rust are NOT duplicated here on purpose:
-# - Studio Frontend CI runs `npm run typecheck` (= `tsc --noEmit`)
+# - Unsloth Frontend CI runs `npm run typecheck` (= `tsc --noEmit`)
# and `npm run build` (vite/swc) on every studio/frontend/**
# change, which is a full TS AST + type check.
-# - Studio Tauri CI runs `tauri build --debug --no-bundle` on
+# - Unsloth Tauri CI runs `tauri build --debug --no-bundle` on
# every studio/src-tauri/** or studio/frontend/** change, which
# compiles the Rust crate (= cargo check + cargo build).
# Each is a stricter check than a parse-only step would be, so a
diff --git a/.github/workflows/local-agent-guides-ci.yml b/.github/workflows/local-agent-guides-ci.yml
index 25796bd5cf..c48328e90f 100644
--- a/.github/workflows/local-agent-guides-ci.yml
+++ b/.github/workflows/local-agent-guides-ci.yml
@@ -154,7 +154,7 @@ jobs:
path: gguf-cache
key: ${{ runner.os }}-gguf-${{ env.GGUF_REPO }}-${{ env.GGUF_FILE }}-v1
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Gated off PR (see note above); public GGUF still downloads.
@@ -256,7 +256,7 @@ jobs:
done
fi
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
# Guard the PID: an unset/zero UNSLOTH_SERVER_PID would make
@@ -359,7 +359,7 @@ jobs:
path: gguf-cache
key: ${{ runner.os }}-gguf-${{ env.GGUF_REPO }}-${{ env.GGUF_FILE }}-v1
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Gated off PR (see note above); public GGUF still downloads.
@@ -448,7 +448,7 @@ jobs:
done
fi
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
# Guard the PID: an unset/zero UNSLOTH_SERVER_PID would make
@@ -543,7 +543,7 @@ jobs:
path: gguf-cache
key: ${{ runner.os }}-gguf-${{ env.GGUF_REPO }}-${{ env.GGUF_FILE }}-v1
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HF_TOKEN: ${{ secrets.HF_TOKEN }}
@@ -620,7 +620,7 @@ jobs:
done
fi
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
if [ -n "${UNSLOTH_SERVER_PID:-}" ] && [ "${UNSLOTH_SERVER_PID}" != "0" ]; then
@@ -706,7 +706,7 @@ jobs:
path: hf-cache
key: ${{ runner.os }}-hf-${{ env.GGUF_REPO }}-${{ env.GGUF_VARIANT }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Gated off PR (see note above); public GGUF still downloads.
@@ -764,7 +764,7 @@ jobs:
done
fi
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
# Guard the PID: an unset/zero UNSLOTH_SERVER_PID would make
diff --git a/.github/workflows/mlx-ci.yml b/.github/workflows/mlx-ci.yml
index a2f716a93c..aadf0b54e6 100644
--- a/.github/workflows/mlx-ci.yml
+++ b/.github/workflows/mlx-ci.yml
@@ -130,7 +130,7 @@ jobs:
# MLX support landed after the most recent unsloth-zoo PyPI
# release; the wheel still raises NotImplementedError on
# Apple Silicon when device_type.get_device_type() runs
- # unguarded. Studio's own install.sh overlays unsloth-zoo
+ # unguarded. Unsloth's own install.sh overlays unsloth-zoo
# from git main for the same reason. Pulling deps lets pip
# resolve the platform-conditional MLX-only wheels (mlx,
# mlx-lm, mlx-vlm gated on darwin+arm64 in unsloth-zoo's
@@ -317,13 +317,13 @@ jobs:
echo
done
- # Validates the macOS prebuilt path Studio's setup.sh uses (#5963): install the
+ # Validates the macOS prebuilt path Unsloth's setup.sh uses (#5963): install the
# unslothai/llama.cpp fork's latest release, download a small public GGUF, and
# check llama-server /completion end to end. Split and placed last so the
# untrusted binary runs only in the final smoke step, after every HF_TOKEN step,
# leaving no token-bearing step or shared workspace for a tampered prebuilt to
# corrupt. GH_TOKEN: releases API; HF_TOKEN (withheld on PR): probe + GGUF fetch.
- - name: Studio prebuilt llama.cpp install + GGUF download (Mac M1)
+ - name: Unsloth prebuilt llama.cpp install + GGUF download (Mac M1)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -344,12 +344,12 @@ jobs:
# Final step: runs the downloaded binaries with no secrets present, and clears
# the GitHub Actions command files so a tampered prebuilt cannot influence the job.
- - name: Studio prebuilt llama.cpp GGUF inference smoke (Mac M1)
+ - name: Unsloth prebuilt llama.cpp GGUF inference smoke (Mac M1)
run: |
set -euo pipefail
unset GITHUB_ENV GITHUB_PATH GITHUB_OUTPUT GITHUB_STEP_SUMMARY
INSTALL_DIR="$HOME/.unsloth-studio-prebuilt-test/llama.cpp"
- # Studio bundles only llama-server + llama-quantize (not llama-cli);
+ # Unsloth bundles only llama-server + llama-quantize (not llama-cli);
# inference goes through llama-server's HTTP /completion endpoint.
LLAMA_SERVER="$INSTALL_DIR/build/bin/llama-server"
LLAMA_QUANT="$INSTALL_DIR/build/bin/llama-quantize"
@@ -400,4 +400,4 @@ jobs:
tail -40 /tmp/llama-server.log
exit 1
fi
- echo "OK: Studio prebuilt llama.cpp on Mac M1 + GGUF /completion works"
+ echo "OK: Unsloth prebuilt llama.cpp on Mac M1 + GGUF /completion works"
diff --git a/.github/workflows/release-desktop.yml b/.github/workflows/release-desktop.yml
index 4daafae35d..081eda4e32 100644
--- a/.github/workflows/release-desktop.yml
+++ b/.github/workflows/release-desktop.yml
@@ -4,7 +4,7 @@ on:
workflow_dispatch:
inputs:
studio_version:
- description: 'Studio version tag to release (for example, v0.1.39-beta)'
+ description: 'Unsloth version tag to release (for example, v0.1.39-beta)'
type: string
required: true
pypi_version:
@@ -69,7 +69,7 @@ jobs:
if not studio_version:
sys.exit('studio_version is required, for example v0.1.39-beta')
if re.fullmatch(r'v?20\d{2}\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?', studio_version):
- sys.exit(f'studio_version must be a Studio SemVer tag, not a date-style backend version: {studio_version}')
+ sys.exit(f'studio_version must be an Unsloth SemVer tag, not a date-style backend version: {studio_version}')
semver_tag = re.compile(
r'^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)'
@@ -146,7 +146,7 @@ jobs:
print(f'pypi_version={pypi_version}', file=output)
PY
- - name: Verify PyPI package and Studio stamp
+ - name: Verify PyPI package and Unsloth stamp
shell: bash
env:
STUDIO_VERSION: ${{ steps.prepare.outputs.studio_version }}
@@ -211,7 +211,7 @@ jobs:
fi
python3 scripts/stamp_studio_release.py --verify-dist "$RUNNER_TEMP/pypi-unsloth-dist" --expected "$STUDIO_VERSION"
else
- echo "scripts/stamp_studio_release.py not found; release-desktop requires #5308 to verify the PyPI Studio stamp." >&2
+ echo "scripts/stamp_studio_release.py not found; release-desktop requires #5308 to verify the PyPI Unsloth stamp." >&2
exit 1
fi
diff --git a/.github/workflows/security-audit.yml b/.github/workflows/security-audit.yml
index 1275d12216..27eafbedea 100644
--- a/.github/workflows/security-audit.yml
+++ b/.github/workflows/security-audit.yml
@@ -36,8 +36,8 @@
# - unsloth `huggingfacenotorch` extras (the canonical install path
# for fine-tuning users; pulls transformers / peft / accelerate /
# trl / datasets / diffusers / sentence-transformers / etc.)
-# - all six Studio backend requirements files
-# - Studio frontend (npm) and Tauri shell (cargo)
+# - all six Unsloth backend requirements files
+# - Unsloth frontend (npm) and Tauri shell (cargo)
# Each Python step builds a filtered dep list from pyproject.toml +
# requirements/*.txt before auditing. We do NOT install any of these
# -- pip-audit resolves through PyPI metadata, scan_packages.py
@@ -218,7 +218,7 @@ jobs:
# on the runner). A comment line is left in place so the
# skipped specs are obvious in the artifact.
# The `huggingface` extra is `huggingfacenotorch` plus torch /
- # torchvision / triton, deliberately skipped: Studio backend
+ # torchvision / triton, deliberately skipped: Unsloth backend
# already pins a torch and the +cu* / +cpu local-version tags
# trip up the PyPI resolver in `-r` mode.
run: |
@@ -253,7 +253,7 @@ jobs:
# `-r requirements.txt` resolves the requirements through pip's
# dependency resolver against PyPI metadata and audits the
# resolved tree without ever executing setup.py / install
- # hooks. Way faster than installing the full Studio runtime
+ # hooks. Way faster than installing the full Unsloth runtime
# and -- critically -- safer: an attacker who has compromised
# a transitive dep cannot run code in this job.
#
@@ -326,9 +326,9 @@ jobs:
} >> "$GITHUB_STEP_SUMMARY"
# ─────────────────────────────────────────────────────────────
- # npm: Studio frontend
+ # npm: Unsloth frontend
# ─────────────────────────────────────────────────────────────
- - name: npm audit (Studio frontend)
+ - name: npm audit (Unsloth frontend)
# `npm audit` resolves the lockfile through the npmjs.com
# advisory DB. `--audit-level=high` filters the noise floor
# to only HIGH and CRITICAL. We do NOT pass --omit=dev: a
@@ -342,7 +342,7 @@ jobs:
# Always also write the full JSON for grep-ability.
npm audit --json > ../../logs-npm-audit.json || true
{
- echo "## npm audit (Studio frontend)"
+ echo "## npm audit (Unsloth frontend)"
echo
echo '```'
tail -200 ../../logs-npm-audit.txt
@@ -350,9 +350,9 @@ jobs:
} >> "$GITHUB_STEP_SUMMARY"
# ─────────────────────────────────────────────────────────────
- # cargo: Studio Tauri shell
+ # cargo: Unsloth Tauri shell
# ─────────────────────────────────────────────────────────────
- - name: cargo audit (Studio Tauri)
+ - name: cargo audit (Unsloth Tauri)
# `--deny warnings` would make the job fail on any advisory.
# Keep non-blocking initially; drop continue-on-error after
# the baseline closes.
@@ -362,7 +362,7 @@ jobs:
set +e
cargo audit | tee ../../logs-cargo-audit.txt
{
- echo "## cargo audit (Studio Tauri)"
+ echo "## cargo audit (Unsloth Tauri)"
echo
echo '```'
tail -200 ../../logs-cargo-audit.txt
@@ -559,7 +559,7 @@ jobs:
# ─────────────────────────────────────────────────────────────
# CycloneDX SBOM. Lets downstream consumers audit what's
- # actually shipped in unsloth wheels and the Studio backend
+ # actually shipped in unsloth wheels and the Unsloth backend
# runtime. Generates one JSON file per requirements input plus
# a combined SBOM keyed off pyproject.toml; uploads as a build
# artifact (and a future step can attest it via SLSA).
@@ -740,7 +740,7 @@ jobs:
# `--with-deps` makes the scan transitive: every package the
# declared set resolves to gets fetched and pattern-scanned, not
# just the top-level pins. Resolving the full transitive closure
- # of the unsloth + Studio dep tree downloads several hundred
+ # of the unsloth + Unsloth dep tree downloads several hundred
# archives, hence the longer timeout.
#
# Sharded across runners for wall-clock parallelism. Each shard
@@ -749,7 +749,7 @@ jobs:
# composition tries to balance load:
# - hf-stack: pyproject extras + no-torch-runtime
# (~150 archives, transformers/peft/accelerate/...)
- # - studio: FastAPI/Studio backend + overrides + extras-no-deps
+ # - studio: FastAPI/Unsloth backend + overrides + extras-no-deps
# (~150 archives, smaller scientific stack)
# - extras: the heavy openai-whisper / scikit-learn / librosa
# stack (~250 archives, dominant cost)
@@ -964,7 +964,7 @@ jobs:
# documented at scripts/scan_npm_packages.py top-of-file. The
# script is stdlib-only so adding it does not increase the
# transitive supply-chain surface.
- name: npm scan-packages (Studio frontend tarballs)
+ name: npm scan-packages (Unsloth frontend tarballs)
runs-on: ubuntu-latest
timeout-minutes: 30
needs: []
@@ -1173,7 +1173,7 @@ jobs:
with:
python-version: '3.12'
- - name: Install Studio frontend deps (--ignore-scripts)
+ - name: Install Unsloth frontend deps (--ignore-scripts)
# `npm audit signatures` requires node_modules to be populated.
# `--ignore-scripts` is mandatory: this is exactly the lever the
# new-install-script gate below protects against, and we must
diff --git a/.github/workflows/studio-api-smoke.yml b/.github/workflows/studio-api-smoke.yml
index 15efee382e..cdf1f6bf12 100644
--- a/.github/workflows/studio-api-smoke.yml
+++ b/.github/workflows/studio-api-smoke.yml
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-# Studio API & Auth Tests -- HTTP-level integration tests for the
+# Unsloth API & Auth Tests -- HTTP-level integration tests for the
# FastAPI surface. No Playwright, no model UI; tests/studio/test_studio_api_smoke.py
# runs ~30 s and asserts:
# - CORS hardening (no wildcard + credentials, no bootstrap leak)
@@ -15,7 +15,7 @@
# Reuses the GGUF cache key from studio-ui-smoke.yml so the model
# download is one cache-hit on the second job.
-name: Studio API CI
+name: Unsloth API CI
on:
pull_request:
@@ -40,7 +40,7 @@ permissions:
jobs:
api-smoke:
- name: Studio API & Auth Tests
+ name: Unsloth API & Auth Tests
runs-on: ubuntu-latest
timeout-minutes: 12
env:
@@ -98,7 +98,7 @@ jobs:
path: hf-cache
key: ${{ runner.os }}-hf-${{ env.GGUF_REPO }}-${{ env.GGUF_VARIANT }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -111,7 +111,7 @@ jobs:
- name: Install pyjwt for the JWT-expiry forge test
run: pip install 'pyjwt>=2.6'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -144,7 +144,7 @@ jobs:
echo "STUDIO_NEW_PW=$NEW" >> "$GITHUB_ENV"
echo "STUDIO_NEW2_PW=$NEW2" >> "$GITHUB_ENV"
- - name: Run Studio API & Auth tests
+ - name: Run Unsloth API & Auth tests
# The script is named WITHOUT a `test_` prefix so it isn't
# auto-collected by pytest in Backend CI's `tests/` walk
# (which doesn't set BASE_URL and would crash at import).
@@ -153,7 +153,7 @@ jobs:
STUDIO_AUTH_DIR: /home/runner/.unsloth/studio/auth
run: python tests/studio/studio_api_smoke.py
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
diff --git a/.github/workflows/studio-backend-ci.yml b/.github/workflows/studio-backend-ci.yml
index 3022127a2b..b8f587b63e 100644
--- a/.github/workflows/studio-backend-ci.yml
+++ b/.github/workflows/studio-backend-ci.yml
@@ -64,7 +64,7 @@ jobs:
- name: Install backend test dependencies (CPU only)
run: |
python -m pip install --upgrade pip
- # Studio's declared backend deps:
+ # Unsloth's declared backend deps:
pip install -r studio/backend/requirements/studio.txt
# Extras that studio.txt does not list but the import chain needs
# (python-multipart for FastAPI form/file uploads, sqlalchemy/cryptography
diff --git a/.github/workflows/studio-export-capability-ci.yml b/.github/workflows/studio-export-capability-ci.yml
index 1ee6489209..83df3ed476 100644
--- a/.github/workflows/studio-export-capability-ci.yml
+++ b/.github/workflows/studio-export-capability-ci.yml
@@ -9,7 +9,7 @@
# export is validated separately. No GPU / model / llama.cpp: the tests mock the probes and block
# torch/unsloth, so the job installs only a CPU PyTorch plus import deps.
-name: Studio export capability
+name: Unsloth export capability
on:
pull_request:
diff --git a/.github/workflows/studio-frontend-ci.yml b/.github/workflows/studio-frontend-ci.yml
index b42086f191..3a9e373915 100644
--- a/.github/workflows/studio-frontend-ci.yml
+++ b/.github/workflows/studio-frontend-ci.yml
@@ -136,7 +136,7 @@ jobs:
- name: Build
run: npm run build
- - name: Built bundle must not contain Studio's unstable_Provider call site
+ - name: Built bundle must not contain Unsloth's unstable_Provider call site
run: |
set -e
JS=$(ls dist/assets/index-*.js | head -1)
@@ -144,7 +144,7 @@ jobs:
echo "main bundle: $JS"
echo "unstable_Provider: hits=$HITS (assistant-ui internals contribute up to 3)"
if [ "$HITS" -gt 3 ]; then
- echo "::error file=studio/frontend/src/features/chat/runtime-provider.tsx::Studio bundle still passes unstable_Provider through useRemoteThreadListRuntime; this is the 2026.5.1 chat-history regression. Pass adapters directly into useLocalRuntime instead."
+ echo "::error file=studio/frontend/src/features/chat/runtime-provider.tsx::Unsloth bundle still passes unstable_Provider through useRemoteThreadListRuntime; this is the 2026.5.1 chat-history regression. Pass adapters directly into useLocalRuntime instead."
exit 1
fi
diff --git a/.github/workflows/studio-inference-smoke.yml b/.github/workflows/studio-inference-smoke.yml
index 58ef2558f3..c2d52eac22 100644
--- a/.github/workflows/studio-inference-smoke.yml
+++ b/.github/workflows/studio-inference-smoke.yml
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-# Three end-to-end smoke jobs that boot a freshly-installed Studio and
+# Three end-to-end smoke jobs that boot a freshly-installed Unsloth and
# exercise the surfaces real users hit through the OpenAI / Anthropic
# SDKs and curl. Each job picks the smallest model that exercises the
# behaviour under test, primes HF_HOME via actions/cache, and shares
@@ -27,7 +27,7 @@
# All three jobs run in parallel. Total wall time is dominated by job 3
# on a cold cache; warm cache cuts that to ~3 min.
-name: Studio GGUF CI
+name: Unsloth GGUF CI
on:
pull_request:
@@ -112,7 +112,7 @@ jobs:
path: hf-cache
key: ${{ runner.os }}-hf-${{ env.GGUF_REPO }}-${{ env.GGUF_VARIANT }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -125,7 +125,7 @@ jobs:
- name: Install OpenAI + Anthropic Python SDKs
run: pip install 'openai>=1.50' 'anthropic>=0.40'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -142,7 +142,7 @@ jobs:
fi
sleep 1
done
- echo "Studio did not become healthy in 180s"
+ echo "Unsloth did not become healthy in 180s"
tail -200 logs/studio.log
exit 1
@@ -229,11 +229,11 @@ jobs:
return replies
def run_anthropic():
- # Two SDK quirks vs. Studio:
+ # Two SDK quirks vs. Unsloth:
# 1. base_url must NOT include /v1 -- the SDK appends
# /v1/messages itself; otherwise the request hits
# /v1/v1/messages and 405s.
- # 2. The SDK sends `x-api-key` by default, but Studio's
+ # 2. The SDK sends `x-api-key` by default, but Unsloth's
# auth layer is HTTPBearer-only. Override via
# default_headers so Authorization: Bearer ... is
# sent instead.
@@ -276,7 +276,7 @@ jobs:
print(
f"[{label}] WARN non-determinism at temperature=0.0 across "
f"{len(determinism_failures)} of {len(first)} turn(s); "
- f"small-quant model drift, not a Studio regression. "
+ f"small-quant model drift, not an Unsloth regression. "
f"Details: " + " | ".join(determinism_failures)
)
# Sanity: turn-2 reply should mention the earlier question, and
@@ -290,7 +290,7 @@ jobs:
print(f"[{label}] {status_word} -- 4 turns, history grounded ('paris' present)")
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
@@ -323,7 +323,7 @@ jobs:
# store xet chunks + blobs + snapshots = ~4 GiB compressed --
# 4-5x file-size inflation, dominated by xet chunks. Use main's
# `--local-dir gguf-cache` pattern to cache the flat .gguf only.
- # Studio's /api/inference/load accepts either a HF repo (which
+ # Unsloth's /api/inference/load accepts either a HF repo (which
# uses HF_HOME) or an absolute file path; passing the absolute
# path keeps the test off HF_HOME entirely so the cache size
# tracks the GGUF file 1:1. The OpenAI/Anth and JSON+images
@@ -380,7 +380,7 @@ jobs:
path: gguf-cache
key: ${{ runner.os }}-gguf-${{ env.GGUF_REPO }}-${{ env.GGUF_FILE }}-v1
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -390,7 +390,7 @@ jobs:
set -o pipefail
bash install.sh --local --no-torch 2>&1 | tee logs/install.log
- - name: Reset auth + boot Studio (API-only, default tool policy)
+ - name: Reset auth + boot Unsloth (API-only, default tool policy)
# We deliberately use the API-only mode rather than
# `unsloth studio run` because the latter calls
# `set_tool_policy(...)` with a resolved bool: on loopback the
@@ -503,7 +503,7 @@ jobs:
that the tool path executed.
A shared CI runner can stall the stream transport (the
- connection opening, or a mid-stream read) even when Studio
+ connection opening, or a mid-stream read) even when Unsloth
is healthy, so retry a stall once with a fresh request
capped at 300s. A stall means the stream did NOT complete,
so partial events are normally NOT returned (an early
@@ -575,11 +575,11 @@ jobs:
def _tool_invoked(events):
"""Structural check: True iff some SSE payload is a real
- tool envelope (Studio tool_start/tool_end, Anthropic
+ tool envelope (Unsloth tool_start/tool_end, Anthropic
tool_use/tool_result, OpenAI non-empty delta.tool_calls /
message.tool_calls / finish_reason='tool_calls' /
role:'tool' / function_call). tool_status is NOT
- evidence: Studio emits empty tool_status events on
+ evidence: Unsloth emits empty tool_status events on
iteration boundaries even when no tool ran.
"""
for raw in events:
@@ -698,7 +698,7 @@ jobs:
attempt has structural invocation evidence. WARN (not
FAIL) if invoked but no attempt produces the expected
literal in tool_end.result -- small-quant Qwen3.5-2B can
- emit OpenAI tool_calls deltas without Studio's GGUF
+ emit OpenAI tool_calls deltas without Unsloth's GGUF
agentic loop intercepting them, and that GGUF-vs-OpenAI
format mismatch is out of scope for #5642.
"""
@@ -811,7 +811,7 @@ jobs:
# because (a) the search may legitimately return no results,
# and (b) DuckDuckGo upstream blocks GHA IP ranges often
# enough that requiring a tool_call marker would create
- # red-herring failures from infra rather than from Studio.
+ # red-herring failures from infra rather than from Unsloth.
try:
# Best-effort and bounded: a single 180s attempt keeps a stall
# from eating the job's timeout-minutes (it already WARNs, so a
@@ -834,7 +834,7 @@ jobs:
print(f"[tools] WARN web_search probe failed (non-blocking): {exc}")
# ── 5. Thinking on / off ─────────────────────────────────────
- # Studio strips think blocks from message.content for tools-mode
+ # Unsloth strips think blocks from message.content for tools-mode
# responses, so we toggle plain chat (no enable_tools) and look
# at the surfaced reasoning_content / message.thinking field.
def thinking_call(enable):
@@ -848,7 +848,7 @@ jobs:
})
assert status == 200
msg = data["choices"][0]["message"]
- # Studio surfaces thinking via reasoning_content (OpenAI
+ # Unsloth surfaces thinking via reasoning_content (OpenAI
# extension). Fall back to inline markers for
# robustness across template versions.
raw = (msg.get("content") or "") + (msg.get("reasoning_content") or "")
@@ -868,7 +868,7 @@ jobs:
print(f"[tools] PASS thinking on/off (on={len(on_text)} chars, off={len(off_text)} chars)")
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
@@ -960,7 +960,7 @@ jobs:
path: hf-cache
key: ${{ runner.os }}-hf-${{ env.GGUF_REPO }}-${{ env.GGUF_VARIANT }}-${{ env.MMPROJ_FILE }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -973,7 +973,7 @@ jobs:
- name: Install OpenAI + Anthropic Python SDKs
run: pip install 'openai>=1.50' 'anthropic>=0.40'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
# See Job 2's comment: API-only mode keeps tool_policy=None so
# response_format requests aren't routed through the agentic
# tool loop.
@@ -1076,13 +1076,13 @@ jobs:
# llama.cpp's HTTP server supports OpenAI-compatible JSON
# mode: `response_format: {"type": "json_object"}` constrains
# the model to emit syntactically-valid JSON. We use raw HTTP
- # rather than the OpenAI SDK so that the field shape Studio
+ # rather than the OpenAI SDK so that the field shape Unsloth
# forwards to llama-server is unambiguous (the SDK rewrites
# response_format depending on which variant it recognises).
# We deliberately do NOT pass a strict JSON schema -- on
# small Gemma-4 quants the GBNF-from-schema path occasionally
# produces empty output, and JSON mode is the surface we care
- # about exposing through Studio.
+ # about exposing through Unsloth.
status, data = post("/v1/chat/completions", {
"model": "default",
"messages": [
@@ -1112,7 +1112,7 @@ jobs:
print(f"[json] PASS json_object -> {parsed}")
# ── 2. OpenAI image_url (data URI base64) ───────────────────
- # 64x64 solid-red PNG. stb_image (used by Studio's image
+ # 64x64 solid-red PNG. stb_image (used by Unsloth's image
# normaliser at routes/inference.py:3410) rejects 4x4 or
# smaller PNGs as truncated, so we go up to 64x64 -- still
# tiny in token cost. The assertion is loose: any non-empty
@@ -1148,9 +1148,9 @@ jobs:
print("[image/openai] PASS image_url accepted, non-empty response")
# ── 3. Anthropic source/base64 image ────────────────────────
- # Two SDK quirks vs. Studio: base_url must NOT include /v1
+ # Two SDK quirks vs. Unsloth: base_url must NOT include /v1
# (the SDK appends it itself; otherwise /v1/v1/messages -> 405),
- # and Studio's auth is HTTPBearer-only so the SDK's default
+ # and Unsloth's auth is HTTPBearer-only so the SDK's default
# x-api-key header is ignored -- send Authorization: Bearer
# via default_headers.
anthropic = Anthropic(
@@ -1184,7 +1184,7 @@ jobs:
print("[image/anthropic] PASS source/base64 accepted, non-empty response")
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
diff --git a/.github/workflows/studio-load-orchestrator-ci.yml b/.github/workflows/studio-load-orchestrator-ci.yml
index 93d1a7742d..8710efc2bd 100644
--- a/.github/workflows/studio-load-orchestrator-ci.yml
+++ b/.github/workflows/studio-load-orchestrator-ci.yml
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
#
-# Event-loop regression test for the Studio model-load orchestrator.
+# Event-loop regression test for the Unsloth model-load orchestrator.
# Pins down issue #5642 (Win10 UI freeze on model load): the /load
# route calls LlamaCppBackend.detect_audio_type synchronously, blocking
# the FastAPI event loop on a chain of sync httpx.Client.post() probes.
@@ -14,7 +14,7 @@
# danielhanchen/unsloth-staging-2 (Ubuntu / macOS / Windows all
# green at PR time).
-name: Studio load-orchestrator CI
+name: Unsloth load-orchestrator CI
on:
pull_request:
diff --git a/.github/workflows/studio-mac-api-smoke.yml b/.github/workflows/studio-mac-api-smoke.yml
index 617ce189dc..1968885a1d 100644
--- a/.github/workflows/studio-mac-api-smoke.yml
+++ b/.github/workflows/studio-mac-api-smoke.yml
@@ -33,7 +33,7 @@ permissions:
jobs:
api-smoke:
- name: Studio API & Auth Tests
+ name: Unsloth API & Auth Tests
runs-on: macos-14
timeout-minutes: 25
env:
@@ -83,7 +83,7 @@ jobs:
path: hf-cache
key: ${{ runner.os }}-hf-${{ env.GGUF_REPO }}-${{ env.GGUF_VARIANT }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -99,7 +99,7 @@ jobs:
- name: Install pyjwt for the JWT-expiry forge test
run: pip install 'pyjwt>=2.6'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -129,13 +129,13 @@ jobs:
echo "STUDIO_NEW_PW=$NEW" >> "$GITHUB_ENV"
echo "STUDIO_NEW2_PW=$NEW2" >> "$GITHUB_ENV"
- - name: Run Studio API & Auth tests
+ - name: Run Unsloth API & Auth tests
env:
BASE_URL: http://127.0.0.1:18895
STUDIO_AUTH_DIR: /Users/runner/.unsloth/studio/auth
run: python tests/studio/studio_api_smoke.py
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
diff --git a/.github/workflows/studio-mac-inference-smoke.yml b/.github/workflows/studio-mac-inference-smoke.yml
index 946681706a..ce15eed5c8 100644
--- a/.github/workflows/studio-mac-inference-smoke.yml
+++ b/.github/workflows/studio-mac-inference-smoke.yml
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-# Three end-to-end smoke jobs that boot a freshly-installed Studio and
+# Three end-to-end smoke jobs that boot a freshly-installed Unsloth and
# exercise the surfaces real users hit through the OpenAI / Anthropic
# SDKs and curl. Each job picks the smallest model that exercises the
# behaviour under test, primes a model cache via actions/cache, and
@@ -108,7 +108,7 @@ jobs:
path: hf-cache
key: ${{ runner.os }}-hf-${{ env.GGUF_REPO }}-${{ env.GGUF_VARIANT }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -124,7 +124,7 @@ jobs:
- name: Install OpenAI + Anthropic Python SDKs
run: pip install 'openai>=1.50' 'anthropic>=0.40'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -141,7 +141,7 @@ jobs:
fi
sleep 1
done
- echo "Studio did not become healthy in 180s"
+ echo "Unsloth did not become healthy in 180s"
tail -200 logs/studio.log
exit 1
@@ -228,11 +228,11 @@ jobs:
return replies
def run_anthropic():
- # Two SDK quirks vs. Studio:
+ # Two SDK quirks vs. Unsloth:
# 1. base_url must NOT include /v1 -- the SDK appends
# /v1/messages itself; otherwise the request hits
# /v1/v1/messages and 405s.
- # 2. The SDK sends `x-api-key` by default, but Studio's
+ # 2. The SDK sends `x-api-key` by default, but Unsloth's
# auth layer is HTTPBearer-only. Override via
# default_headers so Authorization: Bearer ... is
# sent instead.
@@ -283,7 +283,7 @@ jobs:
print(f"[{label}] OK -- 4 turns, run1 == run2, history grounded")
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
@@ -363,7 +363,7 @@ jobs:
path: gguf-cache
key: ${{ runner.os }}-gguf-${{ env.GGUF_REPO }}-${{ env.GGUF_FILE }}-v1
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -376,7 +376,7 @@ jobs:
- name: Assert llama.cpp loads on this macOS
run: bash .github/scripts/assert-llama-loads.sh
- - name: Reset auth + boot Studio (API-only, default tool policy)
+ - name: Reset auth + boot Unsloth (API-only, default tool policy)
# We deliberately use the API-only mode rather than
# `unsloth studio run` because the latter calls
# `set_tool_policy(...)` with a resolved bool: on loopback the
@@ -478,7 +478,7 @@ jobs:
call with enable_tools=true must use this helper.
A shared CI runner can stall the stream transport (the
- connection opening, or a mid-stream read) even when Studio
+ connection opening, or a mid-stream read) even when Unsloth
is healthy, so harden the read three ways: retry a stall
once with a fresh request capped at 300s; return any text
already streamed before a stall (a stall on the trailing
@@ -574,11 +574,11 @@ jobs:
assert status == 200, f"tool call status {status}: {data}"
choice = data["choices"][0]
tool_calls = (choice.get("message") or {}).get("tool_calls") or []
- # Studio's contract: when tool_choice='required', llama.cpp's
+ # Unsloth's contract: when tool_choice='required', llama.cpp's
# grammar should force a tool_calls payload. On Mac that
# contract is sometimes broken by the underlying quant; the
# PASS path is "tool_calls present + correct schema", the
- # WARN path documents Studio still returned 200 with a
+ # WARN path documents Unsloth still returned 200 with a
# well-formed choices[] envelope.
if tool_calls:
tc = tool_calls[0]
@@ -660,7 +660,7 @@ jobs:
print(f"[tools] WARN web_search probe failed (non-blocking): {exc}")
# ── 4. Thinking on / off ─────────────────────────────────────
- # Studio strips think blocks from message.content for tools-mode
+ # Unsloth strips think blocks from message.content for tools-mode
# responses, so we toggle plain chat (no enable_tools) and look
# at the surfaced reasoning_content / message.thinking field.
def thinking_call(enable):
@@ -678,7 +678,7 @@ jobs:
}, timeout = 180)
assert status == 200
msg = data["choices"][0]["message"]
- # Studio surfaces thinking via reasoning_content (OpenAI
+ # Unsloth surfaces thinking via reasoning_content (OpenAI
# extension). Fall back to inline markers for
# robustness across template versions.
raw = (msg.get("content") or "") + (msg.get("reasoning_content") or "")
@@ -704,7 +704,7 @@ jobs:
print(f"[tools] PASS thinking on/off (on={len(on_text)} chars, off={len(off_text)} chars)")
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
@@ -810,7 +810,7 @@ jobs:
path: gguf-cache
key: ${{ runner.os }}-gguf-${{ env.GGUF_REPO }}-${{ env.GGUF_FILE }}-${{ env.MMPROJ_FILE }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -826,7 +826,7 @@ jobs:
- name: Install OpenAI + Anthropic Python SDKs
run: pip install 'openai>=1.50' 'anthropic>=0.40'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
# See Job 2's comment: API-only mode keeps tool_policy=None so
# response_format requests aren't routed through the agentic
# tool loop.
@@ -929,13 +929,13 @@ jobs:
# llama.cpp's HTTP server supports OpenAI-compatible JSON
# mode: `response_format: {"type": "json_object"}` constrains
# the model to emit syntactically-valid JSON. We use raw HTTP
- # rather than the OpenAI SDK so that the field shape Studio
+ # rather than the OpenAI SDK so that the field shape Unsloth
# forwards to llama-server is unambiguous (the SDK rewrites
# response_format depending on which variant it recognises).
# We deliberately do NOT pass a strict JSON schema -- on
# small Gemma-4 quants the GBNF-from-schema path occasionally
# produces empty output, and JSON mode is the surface we care
- # about exposing through Studio.
+ # about exposing through Unsloth.
status, data = post("/v1/chat/completions", {
"model": "default",
"messages": [
@@ -1007,7 +1007,7 @@ jobs:
)
# ── 2. OpenAI image_url (data URI base64) ───────────────────
- # 64x64 solid-red PNG. stb_image (used by Studio's image
+ # 64x64 solid-red PNG. stb_image (used by Unsloth's image
# normaliser at routes/inference.py:3410) rejects 4x4 or
# smaller PNGs as truncated, so we go up to 64x64 -- still
# tiny in token cost. The assertion is loose: any non-empty
@@ -1023,11 +1023,11 @@ jobs:
# The Mac prebuilt llama.cpp server has a known crash when
# processing image inputs alongside the gemma-4-E2B mmproj
# (server disconnects mid-completion). This is upstream
- # llama.cpp behaviour, not Studio. Wrap both SDK calls in
+ # llama.cpp behaviour, not Unsloth. Wrap both SDK calls in
# try/except so an upstream crash registers as a WARN rather
- # than failing the whole job. Studio's contract (OpenAI/
+ # than failing the whole job. Unsloth's contract (OpenAI/
# Anthropic image fields are accepted and forwarded) is
- # validated by the request body Studio constructs, not by
+ # validated by the request body Unsloth constructs, not by
# whether llama.cpp can decode it on Mac Metal.
client = OpenAI(base_url = f"{BASE}/v1", api_key = KEY)
try:
@@ -1053,14 +1053,14 @@ jobs:
except Exception as exc:
print(
f"[image/openai] WARN image_url SDK call raised: {type(exc).__name__}: "
- f"{exc}. Likely upstream llama.cpp Mac+vision crash, NOT a Studio "
- f"regression. Studio successfully forwarded the request."
+ f"{exc}. Likely upstream llama.cpp Mac+vision crash, NOT an Unsloth "
+ f"regression. Unsloth successfully forwarded the request."
)
# ── 3. Anthropic source/base64 image ────────────────────────
- # Two SDK quirks vs. Studio: base_url must NOT include /v1
+ # Two SDK quirks vs. Unsloth: base_url must NOT include /v1
# (the SDK appends it itself; otherwise /v1/v1/messages -> 405),
- # and Studio's auth is HTTPBearer-only so the SDK's default
+ # and Unsloth's auth is HTTPBearer-only so the SDK's default
# x-api-key header is ignored -- send Authorization: Bearer
# via default_headers.
anthropic = Anthropic(
@@ -1099,11 +1099,11 @@ jobs:
print(
f"[image/anthropic] WARN anthropic image SDK call raised: "
f"{type(exc).__name__}: {exc}. Likely upstream llama.cpp Mac+vision "
- f"crash, NOT a Studio regression."
+ f"crash, NOT an Unsloth regression."
)
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
diff --git a/.github/workflows/studio-mac-install-matrix.yml b/.github/workflows/studio-mac-install-matrix.yml
index 362305cdd4..e990f752d4 100644
--- a/.github/workflows/studio-mac-install-matrix.yml
+++ b/.github/workflows/studio-mac-install-matrix.yml
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-# Proves Studio's llama.cpp install loads on every supported macOS. The heavy
+# Proves Unsloth's llama.cpp install loads on every supported macOS. The heavy
# app smokes stay single-OS; this matrix covers the OS-version dimension cheaply
# (install.sh + binary-load assert). Regression guard for the macOS-version
# selection in studio/install_llama_prebuilt.py.
@@ -60,7 +60,7 @@ jobs:
with:
python-version: '3.12'
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
diff --git a/.github/workflows/studio-mac-ui-smoke.yml b/.github/workflows/studio-mac-ui-smoke.yml
index 20ca247b9f..378e8ee5a6 100644
--- a/.github/workflows/studio-mac-ui-smoke.yml
+++ b/.github/workflows/studio-mac-ui-smoke.yml
@@ -83,7 +83,7 @@ jobs:
path: hf-cache
key: ${{ runner.os }}-hf-${{ env.GGUF_REPO }}-${{ env.GGUF_VARIANT }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -143,7 +143,7 @@ jobs:
print(f"pipeTransport.js: patched JSON.parse calls in {path}")
PY
- - name: Reset auth + boot Studio
+ - name: Reset auth + boot Unsloth
run: |
unsloth studio reset-password
mkdir -p logs
@@ -188,7 +188,7 @@ jobs:
# dies mid-test, (2) Chromium net::ERR_NO_BUFFER_SPACE when the
# runner's kernel briefly runs out of socket buffers, and (3) a
# goto 'interrupted by another navigation' when the SPA auth
- # guard redirects mid-navigation. The retry FULLY resets Studio
+ # guard redirects mid-navigation. The retry FULLY resets Unsloth
# (kill, reset-password, reboot, wait /api/health, re-export
# bootstrap pw) before re-running the script. A real test failure
# (assertion / timeout) does NOT match any pattern so it bypasses
@@ -209,7 +209,7 @@ jobs:
|| grep -q "ERR_NO_BUFFER_SPACE" logs/playwright_attempt_${attempt}.log \
|| grep -q "interrupted by another navigation" logs/playwright_attempt_${attempt}.log; } \
&& [ "$attempt" -lt "$max_attempts" ]; then
- echo "::warning::Playwright flake on attempt ${attempt}; resetting Studio and retrying..."
+ echo "::warning::Playwright flake on attempt ${attempt}; resetting Unsloth and retrying..."
kill "${STUDIO_PID}" 2>/dev/null || true
sleep 2
unsloth studio reset-password
@@ -238,13 +238,13 @@ jobs:
exit "$rc"
done
- - name: Stop Studio (chat-ui ends with Shutdown click; this is belt-and-suspenders)
+ - name: Stop Unsloth (chat-ui ends with Shutdown click; this is belt-and-suspenders)
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
sleep 2
- - name: Reset auth + boot Studio for extra UI tests (port 18897)
+ - name: Reset auth + boot Unsloth for extra UI tests (port 18897)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -271,7 +271,7 @@ jobs:
echo "STUDIO_EXTRA_OLD_PW=$OLD" >> "$GITHUB_ENV"
echo "STUDIO_EXTRA_NEW_PW=$NEW" >> "$GITHUB_ENV"
- - name: Drive Compare/Recipes/Export/Studio/Settings with Playwright
+ - name: Drive Compare/Recipes/Export/Unsloth/Settings with Playwright
env:
BASE_URL: http://127.0.0.1:18897
STUDIO_OLD_PW: ${{ env.STUDIO_EXTRA_OLD_PW }}
@@ -300,7 +300,7 @@ jobs:
|| grep -q "ERR_NO_BUFFER_SPACE" logs/playwright_extra_attempt_${attempt}.log \
|| grep -q "interrupted by another navigation" logs/playwright_extra_attempt_${attempt}.log; } \
&& [ "$attempt" -lt "$max_attempts" ]; then
- echo "::warning::Playwright flake on attempt ${attempt}; resetting Studio and retrying..."
+ echo "::warning::Playwright flake on attempt ${attempt}; resetting Unsloth and retrying..."
kill "${STUDIO_EXTRA_PID}" 2>/dev/null || true
sleep 2
unsloth studio reset-password
@@ -327,7 +327,7 @@ jobs:
exit "$rc"
done
- - name: Stop second Studio
+ - name: Stop second Unsloth
if: always()
run: |
kill "${STUDIO_EXTRA_PID}" 2>/dev/null || true
diff --git a/.github/workflows/studio-mac-update-smoke.yml b/.github/workflows/studio-mac-update-smoke.yml
index d104306c7e..fe9880f3ca 100644
--- a/.github/workflows/studio-mac-update-smoke.yml
+++ b/.github/workflows/studio-mac-update-smoke.yml
@@ -4,15 +4,15 @@
# Mac counterpart to studio-update-smoke.yml. Verifies that on a real
# Apple Silicon (macos-14, M1) runner:
#
-# 1. install.sh --local --no-torch installs Studio AND auto-fetches
+# 1. install.sh --local --no-torch installs Unsloth AND auto-fetches
# the prebuilt llama.cpp Mac binary (llama-bNNNN-bin-macos-arm64
# from ggml-org/llama.cpp). Hitting the source-build fallback is
-# treated as an Unsloth bug -- Studio must always pick the
+# treated as an Unsloth bug -- Unsloth must always pick the
# prebuilt on Mac.
# 2. unsloth studio update --local is idempotent. Two consecutive
# runs both report "prebuilt up to date and validated", no
# source-build fallback.
-# 3. The installed Studio still boots and /api/health returns
+# 3. The installed Unsloth still boots and /api/health returns
# healthy after the update path.
name: Mac Studio Update CI
@@ -42,7 +42,7 @@ permissions:
jobs:
update-idempotency:
- name: Studio Updating Tests
+ name: Unsloth Updating Tests
runs-on: macos-14
timeout-minutes: 30
steps:
@@ -59,7 +59,7 @@ jobs:
python-version: '3.12'
cache: 'pip'
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -106,7 +106,7 @@ jobs:
grep -qE "prebuilt up to date and validated|prebuilt installed and validated" logs/update2.log
echo "second update was clean"
- - name: Boot Studio briefly to confirm the install is still usable
+ - name: Boot Unsloth briefly to confirm the install is still usable
run: |
mkdir -p logs
UNSLOTH_API_ONLY=1 unsloth studio -H 127.0.0.1 -p 18891 \
@@ -123,13 +123,13 @@ jobs:
sleep 1
done
if [ -z "$HEALTHY" ]; then
- echo "Studio failed to come up after \`update\`"
+ echo "Unsloth failed to come up after \`update\`"
tail -200 logs/studio.log
kill "$PID" 2>/dev/null || true
exit 1
fi
kill "$PID" 2>/dev/null || true
- echo "post-update Studio /api/health OK"
+ echo "post-update Unsloth /api/health OK"
- name: Uninstall and verify clean
# Round-trip through scripts/uninstall.sh on real macOS. As a side
diff --git a/.github/workflows/studio-tauri-smoke.yml b/.github/workflows/studio-tauri-smoke.yml
index 018857de68..8e26b9fd0c 100644
--- a/.github/workflows/studio-tauri-smoke.yml
+++ b/.github/workflows/studio-tauri-smoke.yml
@@ -12,7 +12,7 @@
# stay in release-desktop.yml (manual `workflow_dispatch`) because they need
# code-signing secrets and ~30 min of runner time each.
-name: Studio Tauri CI
+name: Unsloth Tauri CI
on:
pull_request:
diff --git a/.github/workflows/studio-ui-smoke.yml b/.github/workflows/studio-ui-smoke.yml
index 297a585430..b6d6d7d6e2 100644
--- a/.github/workflows/studio-ui-smoke.yml
+++ b/.github/workflows/studio-ui-smoke.yml
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-# End-to-end Studio chat UI smoke via Playwright + Chromium against a
-# headless Linux runner. Boots Studio with the smallest GGUF
+# End-to-end Unsloth chat UI smoke via Playwright + Chromium against a
+# headless Linux runner. Boots Unsloth with the smallest GGUF
# (gemma-3-270m-it UD-Q4_K_XL, ~254 MiB), drives the actual frontend
# bundle, and asserts the full bootstrap-password / change-password /
# send-message / persist-on-reload journey works end to end.
@@ -14,7 +14,7 @@
# frontend-only CI happily pass while the actual user-visible UI is
# broken (cf. the 2026.5.1 chat-history release).
-name: Studio UI CI
+name: Unsloth UI CI
on:
pull_request:
@@ -97,7 +97,7 @@ jobs:
path: hf-cache
key: ${{ runner.os }}-hf-${{ env.GGUF_REPO }}-${{ env.GGUF_VARIANT }}-v2
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Withheld on PR: this step runs checked-out PR code; public GGUF still downloads.
@@ -115,7 +115,7 @@ jobs:
# warm runner.
python -m playwright install --with-deps chromium
- - name: Reset auth + boot Studio
+ - name: Reset auth + boot Unsloth
run: |
unsloth studio reset-password
mkdir -p logs
@@ -147,7 +147,7 @@ jobs:
# NEW + NEW2 are generated freshly per CI run via secrets.token_urlsafe
# rather than hardcoded. If a workflow gets compromised, the
# attacker can't replay a known-good rotated password against
- # any future / parallel Studio install -- the rotated value
+ # any future / parallel Unsloth install -- the rotated value
# only ever exists for the lifetime of this single job, masked
# in the log via ::add-mask::.
run: |
@@ -165,18 +165,18 @@ jobs:
env:
BASE_URL: http://127.0.0.1:18892
# The test file lives in the repo so it can be run locally
- # against a freshly-installed Studio (BASE_URL=...; STUDIO_OLD_PW=
+ # against a freshly-installed Unsloth (BASE_URL=...; STUDIO_OLD_PW=
# $(cat ~/.unsloth/studio/auth/.bootstrap_password); python ...).
PW_ART_DIR: logs/playwright
# Strict mode: in CI a missing button / nav / dialog must
# FAIL the test. Locally the test still runs against partial
- # Studio installs without STUDIO_UI_STRICT.
+ # Unsloth installs without STUDIO_UI_STRICT.
STUDIO_UI_STRICT: '1'
run: |
mkdir -p logs/playwright
python tests/studio/playwright_chat_ui.py
- - name: Stop Studio (chat-ui ends with Shutdown click; this is belt-and-suspenders)
+ - name: Stop Unsloth (chat-ui ends with Shutdown click; this is belt-and-suspenders)
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
@@ -184,10 +184,10 @@ jobs:
# The chat UI test ends by clicking the Shutdown menuitem, which
# leaves the server dead. The extra UI test (Compare / Recipes /
- # Export / Studio / Settings) needs a fresh Studio, so we boot a
+ # Export / Unsloth / Settings) needs a fresh Unsloth, so we boot a
# second one on a different port. Boot is fast (~3-5s on the
# warm install we already did) so this adds little wall time.
- - name: Reset auth + boot Studio for extra UI tests (port 18894)
+ - name: Reset auth + boot Unsloth for extra UI tests (port 18894)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -214,7 +214,7 @@ jobs:
echo "STUDIO_EXTRA_OLD_PW=$OLD" >> "$GITHUB_ENV"
echo "STUDIO_EXTRA_NEW_PW=$NEW" >> "$GITHUB_ENV"
- - name: Drive Compare/Recipes/Export/Studio/Settings with Playwright
+ - name: Drive Compare/Recipes/Export/Unsloth/Settings with Playwright
env:
BASE_URL: http://127.0.0.1:18894
STUDIO_OLD_PW: ${{ env.STUDIO_EXTRA_OLD_PW }}
@@ -227,16 +227,16 @@ jobs:
mkdir -p logs/playwright_extra
python tests/studio/playwright_extra_ui.py
- - name: Stop second Studio
+ - name: Stop second Unsloth
if: always()
run: |
kill "${STUDIO_EXTRA_PID}" 2>/dev/null || true
sleep 2
# IME + multilingual paste regression (issue #5318 / PR #5327).
- # Third Studio on its own port so a hang here cannot poison the
+ # Third Unsloth on its own port so a hang here cannot poison the
# earlier UI tests. No GGUF -- the bug surface is the composer.
- - name: Reset auth + boot Studio for IME / i18n tests (port 18896)
+ - name: Reset auth + boot Unsloth for IME / i18n tests (port 18896)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -256,7 +256,7 @@ jobs:
- name: Pass bootstrap pw for IME / i18n test
# IME smoke does the change-password against the bootstrap that
- # Studio's frontend injects into the page, so it only needs the
+ # Unsloth's frontend injects into the page, so it only needs the
# NEW password.
run: |
NEW="CIIme-$(python -c 'import secrets; print(secrets.token_urlsafe(16))')"
@@ -273,7 +273,7 @@ jobs:
mkdir -p logs/playwright_ime
python tests/studio/playwright_chat_ime_i18n.py
- - name: Stop third Studio
+ - name: Stop third Unsloth
if: always()
run: |
kill "${STUDIO_IME_PID}" 2>/dev/null || true
diff --git a/.github/workflows/studio-update-smoke.yml b/.github/workflows/studio-update-smoke.yml
index 08a79afacd..625c2c7811 100644
--- a/.github/workflows/studio-update-smoke.yml
+++ b/.github/workflows/studio-update-smoke.yml
@@ -9,7 +9,7 @@
# This catches regressions in setup.sh's update path that the existing
# GGUF / wheel jobs would miss because they only invoke install.sh once.
-name: Studio Update CI
+name: Unsloth Update CI
on:
pull_request:
@@ -36,7 +36,7 @@ permissions:
jobs:
update-idempotency:
- name: Studio Updating Tests
+ name: Unsloth Updating Tests
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
@@ -63,7 +63,7 @@ jobs:
# post-step then fatal-errors with "Cache folder path is
# retrieved for pip but doesn't exist on disk".
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
# Pass the workflow token so the llama.cpp prebuilt installer's
# GitHub-API call to list releases isn't rate-limited (60/hr
# unauthenticated). Without this, three consecutive install +
@@ -122,7 +122,7 @@ jobs:
grep -qE "prebuilt up to date and validated|prebuilt installed and validated" logs/update2.log
echo "second update was clean"
- - name: Boot Studio briefly to confirm the install is still usable
+ - name: Boot Unsloth briefly to confirm the install is still usable
# If `update --local` accidentally broke the venv or wiped the
# llama-server binary, the server would fail to start here.
run: |
@@ -138,13 +138,13 @@ jobs:
sleep 1
done
if ! jq -e '.status == "healthy"' /tmp/health.json 2>/dev/null; then
- echo "Studio failed to come up after `update`"
+ echo "Unsloth failed to come up after `update`"
tail -200 logs/studio.log
kill "$PID" 2>/dev/null || true
exit 1
fi
kill "$PID" 2>/dev/null || true
- echo "post-update Studio /api/health OK"
+ echo "post-update Unsloth /api/health OK"
- name: Uninstall and verify clean
# Round-trip the installer through scripts/uninstall.sh: confirms the
diff --git a/.github/workflows/studio-windows-api-smoke.yml b/.github/workflows/studio-windows-api-smoke.yml
index e9abd2d669..6dbcceebbd 100644
--- a/.github/workflows/studio-windows-api-smoke.yml
+++ b/.github/workflows/studio-windows-api-smoke.yml
@@ -9,7 +9,7 @@
# (Section 6) is Linux-only and short-circuits on non-POSIX; the rest
# is platform-portable.
-name: Windows Studio API CI
+name: Windows Unsloth API CI
on:
pull_request:
@@ -34,7 +34,7 @@ permissions:
jobs:
api-smoke:
- name: Studio API & Auth Tests
+ name: Unsloth API & Auth Tests
runs-on: windows-latest
timeout-minutes: 30
defaults:
@@ -105,7 +105,7 @@ jobs:
# studio-windows-update-smoke.yml for the full rationale --
# creating an empty studio/frontend/dist trips setup.ps1's
# mtime-based staleness check into "frontend up to date, skip
- # rebuild" and Studio boots with an empty dist directory.
+ # rebuild" and Unsloth boots with an empty dist directory.
# Add-MpPreference accepts paths that do not yet exist.
foreach ($p in @(
"$env:USERPROFILE\.unsloth",
@@ -121,7 +121,7 @@ jobs:
}
}
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -161,7 +161,7 @@ jobs:
echo "install.ps1 installed the Windows prebuilt llama.cpp:"
cat "$INFO"
- - name: Add Studio shim to GITHUB_PATH
+ - name: Add Unsloth shim to GITHUB_PATH
# install.ps1's User-PATH update doesn't propagate to a
# running Git Bash session; export the shim dir so the
# next `unsloth ...` invocation finds it.
@@ -177,7 +177,7 @@ jobs:
- name: Install pyjwt for the JWT-expiry forge test
run: python -m pip install 'pyjwt>=2.6'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -207,7 +207,7 @@ jobs:
echo "STUDIO_NEW_PW=$NEW" >> "$GITHUB_ENV"
echo "STUDIO_NEW2_PW=$NEW2" >> "$GITHUB_ENV"
- - name: Run Studio API & Auth tests
+ - name: Run Unsloth API & Auth tests
# Do NOT pin STUDIO_AUTH_DIR here. The Mac/Linux mirrors
# hardcode runner-specific paths (/Users/runner/...,
# /home/runner/...), but on Windows the path is
@@ -219,7 +219,7 @@ jobs:
BASE_URL: http://127.0.0.1:18895
run: python tests/studio/studio_api_smoke.py
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
diff --git a/.github/workflows/studio-windows-inference-smoke.yml b/.github/workflows/studio-windows-inference-smoke.yml
index 63a7e9dc8f..3ebe442f52 100644
--- a/.github/workflows/studio-windows-inference-smoke.yml
+++ b/.github/workflows/studio-windows-inference-smoke.yml
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-# Three end-to-end smoke jobs that boot a freshly-installed Studio and
+# Three end-to-end smoke jobs that boot a freshly-installed Unsloth and
# exercise the surfaces real users hit through the OpenAI / Anthropic
# SDKs and curl, on the FREE windows-latest runner. Each job picks the
# smallest model that exercises the behaviour under test, primes
@@ -16,7 +16,7 @@
# Qwen3-VL-2B-Instruct UD-IQ2_XXS + mmproj-F16 (~1.4 GiB total).
# Within the 14 GB windows-latest SSD budget.
-name: Windows Studio GGUF CI
+name: Windows Unsloth GGUF CI
on:
pull_request:
@@ -57,7 +57,7 @@ jobs:
STUDIO_PORT: '18888'
HF_HOME: ${{ github.workspace }}/hf-cache
# Force UTF-8 for stdio (Windows defaults to cp1252; hf
- # download / Studio CLI print "✓" checkmarks and crash
+ # download / Unsloth CLI print "✓" checkmarks and crash
# otherwise).
PYTHONIOENCODING: utf-8
PYTHONUTF8: '1'
@@ -160,7 +160,7 @@ jobs:
# studio-windows-update-smoke.yml for the full rationale --
# creating an empty studio/frontend/dist trips setup.ps1's
# mtime-based staleness check into "frontend up to date, skip
- # rebuild" and Studio boots with an empty dist directory.
+ # rebuild" and Unsloth boots with an empty dist directory.
# Add-MpPreference accepts paths that do not yet exist.
foreach ($p in @(
"$env:USERPROFILE\.unsloth",
@@ -176,7 +176,7 @@ jobs:
}
}
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -214,7 +214,7 @@ jobs:
echo "install.ps1 installed the Windows prebuilt llama.cpp:"
cat "$INFO"
- - name: Add Studio shim to GITHUB_PATH
+ - name: Add Unsloth shim to GITHUB_PATH
run: |
SHIM_DIR=~/.unsloth/studio/bin
if [ ! -f "$SHIM_DIR/unsloth.exe" ]; then
@@ -227,7 +227,7 @@ jobs:
- name: Install OpenAI + Anthropic Python SDKs
run: python -m pip install 'openai>=1.50' 'anthropic>=0.40'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -244,7 +244,7 @@ jobs:
fi
sleep 1
done
- echo "Studio did not become healthy in 180s"
+ echo "Unsloth did not become healthy in 180s"
tail -200 logs/studio.log
exit 1
@@ -281,7 +281,7 @@ jobs:
# Retry the load step a few times so a transient TCP RST during
# llama-server warm-up (Windows runner image churn,
# windows-latest -> windows-2025-vs2026 rollout) doesn't fail
- # the whole job. The Studio backend's _wait_for_health now
+ # the whole job. The Unsloth backend's _wait_for_health now
# catches httpx.ReadError too; this retry layer covers the
# cases the backend can't recover from on its own.
LOAD_OK=0
@@ -382,15 +382,15 @@ jobs:
print(f"[{label}] OK -- 4 turns, run1 == run2, history grounded")
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
# Run as cmd so we are not running through the Git Bash shell;
# Git Bash on windows-latest has been observed to exit 143
# (SIGTERM) from any inline kill/sleep block, masking a green
- # test run. The runner reclaims the Studio child process at
+ # test run. The runner reclaims the Unsloth child process at
# job end either way, so just emit a marker and exit 0.
shell: cmd
- run: echo Stop Studio (no-op; runner reclaims STUDIO_PID=%STUDIO_PID% at job end)
+ run: echo Stop Unsloth (no-op; runner reclaims STUDIO_PID=%STUDIO_PID% at job end)
- name: Collect llama-server logs
if: always()
@@ -398,10 +398,10 @@ jobs:
# copy must not fail an otherwise-green job.
continue-on-error: true
shell: bash
- # Copy llama-server's own stdout/stderr (teed by Studio under
+ # Copy llama-server's own stdout/stderr (teed by Unsloth under
# ~/.unsloth/studio/logs/llama-server/) into the workspace so
# upload-artifact can pick it up. Crucial for diagnosing a
- # subprocess crash where Studio's traceback only shows the
+ # subprocess crash where Unsloth's traceback only shows the
# symptom (httpx ReadError) but not the cause.
run: |
mkdir -p logs/llama-server
@@ -439,14 +439,14 @@ jobs:
# (211 s on first run; subsequent runs hit the cache, but the
# one-time cost recurs every time the cache key bumps). Use
# main's `--local-dir gguf-cache` pattern: cache the flat .gguf
- # only, pass an absolute path to Studio's /api/inference/load.
+ # only, pass an absolute path to Unsloth's /api/inference/load.
# The OpenAI/Anth and JSON+images jobs still cover the
# gguf_variant resolution path.
GGUF_REPO: unsloth/Qwen3.5-2B-GGUF
GGUF_FILE: Qwen3.5-2B-UD-Q4_K_XL.gguf
STUDIO_PORT: '18898'
# Force UTF-8 for stdio (Windows defaults to cp1252; hf
- # download / Studio CLI print "✓" checkmarks and crash
+ # download / Unsloth CLI print "✓" checkmarks and crash
# otherwise).
PYTHONIOENCODING: utf-8
PYTHONUTF8: '1'
@@ -507,7 +507,7 @@ jobs:
# studio-windows-update-smoke.yml for the full rationale --
# creating an empty studio/frontend/dist trips setup.ps1's
# mtime-based staleness check into "frontend up to date, skip
- # rebuild" and Studio boots with an empty dist directory.
+ # rebuild" and Unsloth boots with an empty dist directory.
# Add-MpPreference accepts paths that do not yet exist.
foreach ($p in @(
"$env:USERPROFILE\.unsloth",
@@ -523,7 +523,7 @@ jobs:
}
}
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -561,7 +561,7 @@ jobs:
echo "install.ps1 installed the Windows prebuilt llama.cpp:"
cat "$INFO"
- - name: Add Studio shim to GITHUB_PATH
+ - name: Add Unsloth shim to GITHUB_PATH
run: |
SHIM_DIR=~/.unsloth/studio/bin
if [ ! -f "$SHIM_DIR/unsloth.exe" ]; then
@@ -571,7 +571,7 @@ jobs:
fi
cygpath -w "$SHIM_DIR" >> "$GITHUB_PATH"
- - name: Reset auth + boot Studio (API-only, default tool policy)
+ - name: Reset auth + boot Unsloth (API-only, default tool policy)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -607,7 +607,7 @@ jobs:
# raw string, but we cannot embed `\a` etc. in JSON without
# JSON-string-escaping every backslash. Replace `\` with `/`
# via bash parameter expansion -- pathlib.Path on Windows
- # accepts forward slashes natively, so Studio's loader sees
+ # accepts forward slashes natively, so Unsloth's loader sees
# a normal path.
GGUF_PATH="${GITHUB_WORKSPACE//\\//}/gguf-cache/${GGUF_FILE}"
ls -lh "$GGUF_PATH"
@@ -680,7 +680,7 @@ jobs:
def post_sse(path, body, *, timeout = 600, retries = 1, soft = False):
# The server-side agentic loop always answers over SSE. A
# shared CI runner can stall the stream transport (the
- # connection opening, or a mid-stream read) even when Studio
+ # connection opening, or a mid-stream read) even when Unsloth
# is healthy, so harden the read three ways:
# * retry a transport stall once with a fresh request,
# capped at 300s (a healthy server answers a retry
@@ -882,15 +882,15 @@ jobs:
print(f"[tools] PASS thinking on/off (on={len(on_text)} chars, off={len(off_text)} chars)")
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
# Run as cmd so we are not running through the Git Bash shell;
# Git Bash on windows-latest has been observed to exit 143
# (SIGTERM) from any inline kill/sleep block, masking a green
- # test run. The runner reclaims the Studio child process at
+ # test run. The runner reclaims the Unsloth child process at
# job end either way, so just emit a marker and exit 0.
shell: cmd
- run: echo Stop Studio (no-op; runner reclaims STUDIO_PID=%STUDIO_PID% at job end)
+ run: echo Stop Unsloth (no-op; runner reclaims STUDIO_PID=%STUDIO_PID% at job end)
- name: Collect llama-server logs
if: always()
@@ -898,10 +898,10 @@ jobs:
# copy must not fail an otherwise-green job.
continue-on-error: true
shell: bash
- # Copy llama-server's own stdout/stderr (teed by Studio under
+ # Copy llama-server's own stdout/stderr (teed by Unsloth under
# ~/.unsloth/studio/logs/llama-server/) into the workspace so
# upload-artifact can pick it up. Crucial for diagnosing a
- # subprocess crash where Studio's traceback only shows the
+ # subprocess crash where Unsloth's traceback only shows the
# symptom (httpx ReadError) but not the cause.
run: |
mkdir -p logs/llama-server
@@ -939,7 +939,7 @@ jobs:
STUDIO_PORT: '18899'
HF_HOME: ${{ github.workspace }}/hf-cache
# Force UTF-8 for stdio (Windows defaults to cp1252; hf
- # download / Studio CLI print "✓" checkmarks and crash
+ # download / Unsloth CLI print "✓" checkmarks and crash
# otherwise).
PYTHONIOENCODING: utf-8
PYTHONUTF8: '1'
@@ -1005,7 +1005,7 @@ jobs:
# studio-windows-update-smoke.yml for the full rationale --
# creating an empty studio/frontend/dist trips setup.ps1's
# mtime-based staleness check into "frontend up to date, skip
- # rebuild" and Studio boots with an empty dist directory.
+ # rebuild" and Unsloth boots with an empty dist directory.
# Add-MpPreference accepts paths that do not yet exist.
foreach ($p in @(
"$env:USERPROFILE\.unsloth",
@@ -1021,7 +1021,7 @@ jobs:
}
}
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -1059,7 +1059,7 @@ jobs:
echo "install.ps1 installed the Windows prebuilt llama.cpp:"
cat "$INFO"
- - name: Add Studio shim to GITHUB_PATH
+ - name: Add Unsloth shim to GITHUB_PATH
run: |
SHIM_DIR=~/.unsloth/studio/bin
if [ ! -f "$SHIM_DIR/unsloth.exe" ]; then
@@ -1072,7 +1072,7 @@ jobs:
- name: Install OpenAI + Anthropic Python SDKs
run: python -m pip install 'openai>=1.50' 'anthropic>=0.40'
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -1262,7 +1262,7 @@ jobs:
except Exception as exc:
print(
f"[image/openai] WARN image_url SDK call raised: {type(exc).__name__}: "
- f"{exc}. Studio successfully forwarded the request; failure here is "
+ f"{exc}. Unsloth successfully forwarded the request; failure here is "
f"upstream llama.cpp vision behaviour."
)
@@ -1303,19 +1303,19 @@ jobs:
print(
f"[image/anthropic] WARN anthropic image SDK call raised: "
f"{type(exc).__name__}: {exc}. Likely upstream llama.cpp vision "
- f"behaviour, NOT a Studio regression."
+ f"behaviour, NOT an Unsloth regression."
)
PY
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
# Run as cmd so we are not running through the Git Bash shell;
# Git Bash on windows-latest has been observed to exit 143
# (SIGTERM) from any inline kill/sleep block, masking a green
- # test run. The runner reclaims the Studio child process at
+ # test run. The runner reclaims the Unsloth child process at
# job end either way, so just emit a marker and exit 0.
shell: cmd
- run: echo Stop Studio (no-op; runner reclaims STUDIO_PID=%STUDIO_PID% at job end)
+ run: echo Stop Unsloth (no-op; runner reclaims STUDIO_PID=%STUDIO_PID% at job end)
- name: Collect llama-server logs
if: always()
@@ -1323,10 +1323,10 @@ jobs:
# copy must not fail an otherwise-green job.
continue-on-error: true
shell: bash
- # Copy llama-server's own stdout/stderr (teed by Studio under
+ # Copy llama-server's own stdout/stderr (teed by Unsloth under
# ~/.unsloth/studio/logs/llama-server/) into the workspace so
# upload-artifact can pick it up. Crucial for diagnosing a
- # subprocess crash where Studio's traceback only shows the
+ # subprocess crash where Unsloth's traceback only shows the
# symptom (httpx ReadError) but not the cause.
run: |
mkdir -p logs/llama-server
@@ -1348,7 +1348,7 @@ jobs:
# ── folded from studio-windows-no-vs-smoke.yml: install + run with no Visual Studio ──
no-vs-cpu:
- name: Studio install + inference without Visual Studio
+ name: Unsloth install + inference without Visual Studio
runs-on: windows-latest
timeout-minutes: 35
defaults:
@@ -1502,7 +1502,7 @@ jobs:
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu --extra-index-url https://pypi.org/simple
python -c "import torch; print('torch', torch.__version__, 'cuda?', torch.cuda.is_available())"
- - name: Install Studio (--local, --no-torch) with no build tools present
+ - name: Install Unsloth (--local, --no-torch) with no build tools present
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -1538,13 +1538,13 @@ jobs:
echo "Prebuilt installed with no build tools:"
cat "$INFO"
- - name: Add Studio shim to GITHUB_PATH
+ - name: Add Unsloth shim to GITHUB_PATH
run: |
SHIM_DIR=~/.unsloth/studio/bin
[ -f "$SHIM_DIR/unsloth.exe" ] || { echo "::error::unsloth.exe shim not found"; ls -la ~/.unsloth/studio/ || true; exit 1; }
cygpath -w "$SHIM_DIR" >> "$GITHUB_PATH"
- - name: Reset auth + boot Studio (API-only)
+ - name: Reset auth + boot Unsloth (API-only)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -1613,10 +1613,10 @@ jobs:
}
Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction SilentlyContinue
- - name: Stop Studio
+ - name: Stop Unsloth
if: always()
shell: cmd
- run: echo Stop Studio (no-op; runner reclaims STUDIO_PID=%STUDIO_PID% at job end)
+ run: echo Stop Unsloth (no-op; runner reclaims STUDIO_PID=%STUDIO_PID% at job end)
- name: Collect llama-server logs
if: always()
diff --git a/.github/workflows/studio-windows-ui-smoke.yml b/.github/workflows/studio-windows-ui-smoke.yml
index 405309916a..12d7475b53 100644
--- a/.github/workflows/studio-windows-ui-smoke.yml
+++ b/.github/workflows/studio-windows-ui-smoke.yml
@@ -4,11 +4,11 @@
# Windows counterpart to studio-ui-smoke.yml / studio-mac-ui-smoke.yml.
# Same Playwright + Chromium end-to-end chat UI flow + extra UI flow,
# but on the FREE windows-latest runner so we catch Windows-specific
-# regressions in the install path (install.ps1), the Studio CLI's
+# regressions in the install path (install.ps1), the Unsloth CLI's
# Windows process-management branches, and the llama.cpp prebuilt's
# Windows HTTP layer.
-name: Windows Studio UI CI
+name: Windows Unsloth UI CI
on:
pull_request:
@@ -49,7 +49,7 @@ jobs:
GGUF_FILE: gemma-3-270m-it-UD-Q4_K_XL.gguf
STUDIO_PORT: '18896'
HF_HOME: ${{ github.workspace }}/hf-cache
- # Force UTF-8 for stdio so Python tools (hf download, Studio
+ # Force UTF-8 for stdio so Python tools (hf download, Unsloth
# CLI, etc.) can print Unicode characters like the success
# checkmark "✓". Windows defaults to cp1252 / charmap and
# any tool that prints "OK ✓" hits a UnicodeEncodeError.
@@ -121,7 +121,7 @@ jobs:
# studio-windows-update-smoke.yml for the full rationale --
# creating an empty studio/frontend/dist trips setup.ps1's
# mtime-based staleness check into "frontend up to date, skip
- # rebuild" and Studio boots with an empty dist directory.
+ # rebuild" and Unsloth boots with an empty dist directory.
# Add-MpPreference accepts paths that do not yet exist.
foreach ($p in @(
"$env:USERPROFILE\.unsloth",
@@ -148,7 +148,7 @@ jobs:
Set-Content -LiteralPath (Join-Path $appDir 'launch-studio.vbs') -Value 'WScript.Echo "legacy"' -Encoding Unicode
Write-Host "seeded legacy launch-studio.vbs at $appDir"
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
# install.ps1 is the supported Windows installer. install.sh
# has no Windows branch (apt-get / brew calls). The PS1
# script's `Install-UnslothStudio @args` line at the bottom
@@ -205,7 +205,7 @@ jobs:
echo "install.ps1 installed the Windows prebuilt llama.cpp:"
cat "$INFO"
- - name: Assert Studio launcher chain (no VBS, hidden PowerShell shortcut)
+ - name: Assert Unsloth launcher chain (no VBS, hidden PowerShell shortcut)
# The shortcut launch path is otherwise untested here (the steps below
# boot `unsloth studio` directly). Guard against re-introducing the VBS
# that tripped Kaspersky HEUR:Trojan.VBS.Agent.gen and against the .lnk
@@ -234,7 +234,7 @@ jobs:
}
Write-Host "launcher chain OK (no VBS; hidden powershell over launch-studio.ps1)"
- - name: Launch Studio via the shortcut and assert health
+ - name: Launch Unsloth via the shortcut and assert health
# Run the exact command the .lnk stores (hidden PowerShell over
# launch-studio.ps1) and confirm it brings the backend up. This is the
# only step that proves the shortcut launch is not silently broken.
@@ -265,10 +265,10 @@ jobs:
$owner = (Get-NetTCPConnection -LocalPort $foundPort -State Listen -ErrorAction Stop | Select-Object -First 1).OwningProcess
if ($owner) { taskkill /PID $owner /T /F 2>$null | Out-Null }
} catch {}
- if (-not $foundPort) { throw "Studio did not become healthy when launched via the shortcut" }
- Write-Host "Studio healthy on port $foundPort (launched via the shortcut)"
+ if (-not $foundPort) { throw "Unsloth did not become healthy when launched via the shortcut" }
+ Write-Host "Unsloth healthy on port $foundPort (launched via the shortcut)"
- - name: Add Studio shim to GITHUB_PATH
+ - name: Add Unsloth shim to GITHUB_PATH
# install.ps1 puts unsloth.exe at $StudioHome\bin\unsloth.exe
# and adds that dir to the User PATH via the Windows registry.
# Registry-level PATH updates don't propagate to a running
@@ -284,7 +284,7 @@ jobs:
fi
# GITHUB_PATH wants Windows-style paths; convert via cygpath.
cygpath -w "$SHIM_DIR" >> "$GITHUB_PATH"
- echo "Added Studio shim dir to PATH: $(cygpath -w "$SHIM_DIR")"
+ echo "Added Unsloth shim dir to PATH: $(cygpath -w "$SHIM_DIR")"
- name: Install Playwright + Chromium
# No --with-deps on Windows: that flag installs Linux apt
@@ -294,7 +294,7 @@ jobs:
python -m pip install 'playwright>=1.45'
python -m playwright install chromium
- - name: Reset auth + boot Studio
+ - name: Reset auth + boot Unsloth
run: |
unsloth studio reset-password
mkdir -p logs
@@ -339,13 +339,13 @@ jobs:
mkdir -p logs/playwright
python tests/studio/playwright_chat_ui.py
- - name: Stop Studio (chat-ui ends with Shutdown click; this is belt-and-suspenders)
+ - name: Stop Unsloth (chat-ui ends with Shutdown click; this is belt-and-suspenders)
if: always()
run: |
kill "${STUDIO_PID}" 2>/dev/null || true
sleep 2
- - name: Reset auth + boot Studio for extra UI tests (port 18897)
+ - name: Reset auth + boot Unsloth for extra UI tests (port 18897)
run: |
unsloth studio reset-password
mkdir -p logs
@@ -372,7 +372,7 @@ jobs:
echo "STUDIO_EXTRA_OLD_PW=$OLD" >> "$GITHUB_ENV"
echo "STUDIO_EXTRA_NEW_PW=$NEW" >> "$GITHUB_ENV"
- - name: Drive Compare/Recipes/Export/Studio/Settings with Playwright
+ - name: Drive Compare/Recipes/Export/Unsloth/Settings with Playwright
env:
BASE_URL: http://127.0.0.1:18897
STUDIO_OLD_PW: ${{ env.STUDIO_EXTRA_OLD_PW }}
@@ -386,7 +386,7 @@ jobs:
mkdir -p logs/playwright_extra
python tests/studio/playwright_extra_ui.py
- - name: Stop second Studio
+ - name: Stop second Unsloth
if: always()
run: |
kill "${STUDIO_EXTRA_PID}" 2>/dev/null || true
diff --git a/.github/workflows/studio-windows-update-smoke.yml b/.github/workflows/studio-windows-update-smoke.yml
index 5b92f1a3e0..42d74d47d2 100644
--- a/.github/workflows/studio-windows-update-smoke.yml
+++ b/.github/workflows/studio-windows-update-smoke.yml
@@ -5,19 +5,19 @@
# studio-mac-update-smoke.yml. Verifies that on the FREE
# windows-latest runner:
#
-# 1. install.ps1 --local --no-torch installs Studio AND auto-fetches
+# 1. install.ps1 --local --no-torch installs Unsloth AND auto-fetches
# the prebuilt llama.cpp Windows binary (app--windows-x64-cpu
# from unslothai/llama.cpp). Hitting the source-build fallback is
-# treated as an Unsloth bug -- Studio must always pick the
+# treated as an Unsloth bug -- Unsloth must always pick the
# prebuilt on Windows.
# 2. unsloth studio update --local is idempotent. Two consecutive
# runs both report "prebuilt up to date and validated", no
# source-build fallback. The CLI's _find_setup_script picks
# setup.ps1 on Windows automatically.
-# 3. The installed Studio still boots and /api/health returns
+# 3. The installed Unsloth still boots and /api/health returns
# healthy after the update path.
-name: Windows Studio Update CI
+name: Windows Unsloth Update CI
on:
pull_request:
@@ -45,7 +45,7 @@ permissions:
jobs:
update-idempotency:
- name: Studio Updating Tests
+ name: Unsloth Updating Tests
runs-on: windows-latest
timeout-minutes: 30
defaults:
@@ -53,7 +53,7 @@ jobs:
shell: bash
env:
# Force UTF-8 for stdio (Windows defaults to cp1252; hf
- # download / Studio CLI print "✓" checkmarks and crash
+ # download / Unsloth CLI print "✓" checkmarks and crash
# otherwise).
PYTHONIOENCODING: utf-8
PYTHONUTF8: '1'
@@ -90,7 +90,7 @@ jobs:
# reuses the existing Node with no download.
#
# (2) Defender. windows-latest's real-time scan opens / hashes
- # every file Studio writes during install (Vite output =
+ # every file Unsloth writes during install (Vite output =
# thousands of small chunks, uv pip = wheel-extraction =
# thousands of small files). The latency dominates the
# 200 s frontend build and the 90 s deps install. Adding
@@ -109,7 +109,7 @@ jobs:
# setup.ps1 line 1281-1296's mtime-based "is the frontend
# stale?" check into "up to date, skip rebuild", because the
# newly-created dist's mtime is younger than every source
- # file. Studio then boots with an empty dist and 500s on
+ # file. Unsloth then boots with an empty dist and 500s on
# GET / with FileNotFoundError: dist\index.html. See run
# 25546676715 / job 74984469728.
# Add-MpPreference accepts paths that do not yet exist; the
@@ -129,7 +129,7 @@ jobs:
}
}
- - name: Install Studio (--local, --no-torch)
+ - name: Install Unsloth (--local, --no-torch)
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -168,7 +168,7 @@ jobs:
echo "install.ps1 installed the Windows prebuilt llama.cpp:"
cat "$INFO"
- - name: Add Studio shim to GITHUB_PATH
+ - name: Add Unsloth shim to GITHUB_PATH
run: |
SHIM_DIR=~/.unsloth/studio/bin
if [ ! -f "$SHIM_DIR/unsloth.exe" ]; then
@@ -212,7 +212,7 @@ jobs:
grep -qE "prebuilt up to date and validated|prebuilt installed and validated" logs/update2.log
echo "second update was clean"
- - name: Boot Studio briefly to confirm the install is still usable
+ - name: Boot Unsloth briefly to confirm the install is still usable
run: |
mkdir -p logs
UNSLOTH_API_ONLY=1 unsloth studio -H 127.0.0.1 -p 18891 \
@@ -239,13 +239,13 @@ jobs:
sleep 1
done
if [ -z "$HEALTHY" ]; then
- echo "Studio failed to come up after \`update\`"
+ echo "Unsloth failed to come up after \`update\`"
tail -200 logs/studio.log
kill "$PID" 2>/dev/null || true
exit 1
fi
kill "$PID" 2>/dev/null || true
- echo "post-update Studio /api/health OK"
+ echo "post-update Unsloth /api/health OK"
- name: Uninstall and verify clean
# Round-trip through scripts/uninstall.ps1 against the default
diff --git a/.github/workflows/wheel-smoke.yml b/.github/workflows/wheel-smoke.yml
index 3de3c33ca2..cdad617027 100644
--- a/.github/workflows/wheel-smoke.yml
+++ b/.github/workflows/wheel-smoke.yml
@@ -3,7 +3,7 @@
# Builds the PyPI wheel from the PR branch, then verifies the built wheel
# actually contains what we expect to ship and does NOT contain the broken
-# Studio bundle that 2026.5.1 published. This is the single workflow that
+# Unsloth bundle that 2026.5.1 published. This is the single workflow that
# would have blocked the 2026.5.1 release before twine upload.
#
# Verified locally end-to-end against this branch:
@@ -12,7 +12,7 @@
# lockfile shipped, frontend dist shipped,
# no node_modules in wheel, no bun.lock in wheel,
# main bundle has unstable_Provider hits=1 (assistant-ui internals only).
-# - Studio backend imports cleanly from the installed wheel with the
+# - Unsloth backend imports cleanly from the installed wheel with the
# lightweight dep set below.
name: Wheel CI
@@ -101,7 +101,7 @@ jobs:
hits = data.count("unstable_Provider:")
print(f"main bundle: {js[0]}")
print(f"unstable_Provider hits: {hits} (>=4 indicates 2026.5.1 regression)")
- checks["bundle has no Studio unstable_Provider call site"] = (hits < 4)
+ checks["bundle has no Unsloth unstable_Provider call site"] = (hits < 4)
print()
for k, v in checks.items():
@@ -109,7 +109,7 @@ jobs:
sys.exit(0 if all(checks.values()) else 1)
PY
- - name: Studio backend import smoke
+ - name: Unsloth backend import smoke
# Imports `studio.backend.main:app` from the freshly-installed wheel in
# a clean venv. This catches the class of bug that 2026.5.1 shipped with:
# frontend dist missing, package-lock.json missing, or the wheel's Python
@@ -125,7 +125,7 @@ jobs:
/tmp/v/bin/pip install --no-deps dist/unsloth-*.whl
# Run from /tmp so Python imports the installed package, not the source tree.
cd /tmp
- /tmp/v/bin/python -c "from studio.backend.main import app; print('Studio backend OK:', app.title)"
+ /tmp/v/bin/python -c "from studio.backend.main import app; print('Unsloth backend OK:', app.title)"
- name: Upload wheel on failure
if: failure()
diff --git a/README.md b/README.md
index ef45b91430..085c7718e5 100644
--- a/README.md
+++ b/README.md
@@ -65,7 +65,7 @@ Unsloth Studio (Beta) works on **Windows, Linux, WSL** and **macOS**.
* **CPU:** Supported for Chat and Data Recipes currently
* **NVIDIA:** Training works on RTX 30/40/50, Blackwell, DGX Spark, Station and more
* **macOS:** Training, MLX and GGUF inference are ALL supported.
-* **AMD:** Chat + Data works. Train with [Unsloth Core](#unsloth-core-code-based). Studio support is out soon.
+* **AMD:** Chat + Data works. Train with [Unsloth Core](#unsloth-core-code-based). Unsloth Studio support is out soon.
* **Multi-GPU:** Available now, with a major upgrade on the way
#### macOS, Linux, WSL:
@@ -86,7 +86,7 @@ unsloth studio -p 8888
```
For LAN or cloud access, add `-H 0.0.0.0` (raw port only; add `--cloudflare` for a public URL). By default, Unsloth is accessible only locally.
-To reach Studio over HTTPS, use `unsloth studio --secure`. Studio stays bound to localhost and is reached only through a free Cloudflare tunnel, which publishes it at a public `https://*.trycloudflare.com` URL (it fails closed if the tunnel can't start, so the raw port is never exposed). This makes Studio reachable from the internet, so anyone with the link and API key can use it and run code: keep your API key private (see Remote access below).
+To reach Unsloth over HTTPS, use `unsloth studio --secure`. Unsloth stays bound to localhost and is reached only through a free Cloudflare tunnel, which publishes it at a public `https://*.trycloudflare.com` URL (it fails closed if the tunnel can't start, so the raw port is never exposed). This makes Unsloth reachable from the internet, so anyone with the link and API key can use it and run code: keep your API key private (see Remote access below).
#### Docker
Use our [Docker image](https://hub.docker.com/r/unsloth/unsloth) ```unsloth/unsloth``` container. Run:
@@ -208,7 +208,7 @@ unsloth studio -p 8888
#### Remote access: `--secure` (HTTPS tunnel) vs raw port
By default `unsloth studio` binds to `127.0.0.1` (this machine only). To reach it from another device, pick one of:
-- `--secure` (recommended): serve **only** through a free Cloudflare HTTPS link. Studio stays bound to localhost and the tunnel provides the public URL; it fails closed (does not start) if the tunnel can't come up, so the raw port is never exposed.
+- `--secure` (recommended): serve **only** through a free Cloudflare HTTPS link. Unsloth stays bound to localhost and the tunnel provides the public URL; it fails closed (does not start) if the tunnel can't come up, so the raw port is never exposed.
```bash
unsloth studio --secure -p 8888
```
@@ -218,7 +218,7 @@ unsloth studio -H 0.0.0.0 -p 8888
```
The Cloudflare tunnel is **off by default**: `-H 0.0.0.0` exposes the raw port only, not a public internet URL. Pair the wildcard bind with `--cloudflare` (`unsloth studio -H 0.0.0.0 --cloudflare`) to also publish a public `https://*.trycloudflare.com` link, or prefer `--secure` (above), which keeps the raw port private. `--cloudflare` has no effect on a loopback bind.
-The first time Studio is published on a public URL (`--secure` or `--cloudflare`) with the auto-generated admin password still in place, it asks for a new admin password in the terminal (masked input with confirmation) before the public link goes up. Without an attached terminal it warns instead and keeps the bootstrap deadline: Studio shuts down after `UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT` (default 1 hour) unless the password is changed in the web UI.
+The first time Unsloth is published on a public URL (`--secure` or `--cloudflare`) with the auto-generated admin password still in place, it asks for a new admin password in the terminal (masked input with confirmation) before the public link goes up. Without an attached terminal it warns instead and keeps the bootstrap deadline: Unsloth shuts down after `UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT` (default 1 hour) unless the password is changed in the web UI.
For headless setups that cannot answer that prompt, set the initial admin password non-interactively with `--password` (only takes effect when no password is set yet; if one already exists it is a hard error, so rotate later with `unsloth studio reset-password`):
@@ -230,7 +230,7 @@ printf '%s\n' 'your-strong-password' | unsloth studio --secure --password - #
A literal `--password VALUE` is visible in the process list and shell history, so prefer the `UNSLOTH_STUDIO_PASSWORD` env var or `--password -` (stdin) for automation. This applies to any launch (public or a headless `-H 0.0.0.0` bind), and the password is set in the parent before the server binds, so it never reaches a re-executed child process.
-Server-side tools (web search, Python and terminal code execution) run as your user and are on by default. Anyone who can reach the server with the API key can run code on this machine, so keep your API key private and pass `--disable-tools` when exposing Studio.
+Server-side tools (web search, Python and terminal code execution) run as your user and are on by default. Anyone who can reach the server with the API key can run code on this machine, so keep your API key private and pass `--disable-tools` when exposing Unsloth.
#### Advanced launch options
Installer options can be passed as environment variables. On macOS, Linux and WSL place the variable after the pipe so the shell passes it to `sh`; on Windows set it with `$env:` before piping to `iex`.
@@ -243,7 +243,7 @@ curl -fsSL https://unsloth.ai/install.sh | UNSLOTH_NO_TORCH=1 sh
$env:UNSLOTH_NO_TORCH=1; irm https://unsloth.ai/install.ps1 | iex
```
-Skip the post-install prompt that starts Studio (useful for automated installs):
+Skip the post-install prompt that starts Unsloth (useful for automated installs):
```bash
curl -fsSL https://unsloth.ai/install.sh | UNSLOTH_SKIP_AUTOSTART=1 sh
```
@@ -279,9 +279,9 @@ UNSLOTH_NPM_REGISTRY=https://artifactory.example.com/api/npm/npm/ ./install.sh -
```powershell
$env:UNSLOTH_NPM_REGISTRY='https://artifactory.example.com/api/npm/npm/'; .\install.ps1 --local
```
-It is threaded as `--registry` into the Studio frontend `npm`/`bun` installs; the supply-chain locks (7-day `min-release-age`, exact version pins) stay in force.
+It is threaded as `--registry` into the Unsloth frontend `npm`/`bun` installs; the supply-chain locks (7-day `min-release-age`, exact version pins) stay in force.
-Cap Studio's native CPU thread pools on high-core hosts: `UNSLOTH_CPU_THREADS=8 unsloth studio -p 8888`.
+Cap Unsloth's native CPU thread pools on high-core hosts: `UNSLOTH_CPU_THREADS=8 unsloth studio -p 8888`.
#### Uninstall
The recommended way to fully remove Unsloth Studio is the matching uninstall script for your OS. It stops any running servers, removes the install dir, the launcher data dir, the desktop shortcut, and any platform-specific entries (macOS `.app` bundle + Launch Services on Mac; Start Menu, `HKCU\Software\Unsloth` registry key and user `PATH` entries on Windows):
diff --git a/build.sh b/build.sh
index dc272f0de1..2a836e19d9 100644
--- a/build.sh
+++ b/build.sh
@@ -4,9 +4,9 @@
set -euo pipefail
-# PyPI/Studio release publishing must use `./build.sh publish` (or an
-# equivalent stamp -> build -> verify-dist -> upload flow) so packaged Studio
-# artifacts include the display-only Studio release version.
+# PyPI/Unsloth release publishing must use `./build.sh publish` (or an
+# equivalent stamp -> build -> verify-dist -> upload flow) so packaged Unsloth
+# artifacts include the display-only Unsloth release version.
# 1. Build frontend (Vite outputs to dist/)
cd studio/frontend
@@ -87,7 +87,7 @@ cd ../..
# 2. Clean old artifacts
rm -rf build dist *.egg-info
-# 3. Stamp display-only Studio release metadata for packaged builds.
+# 3. Stamp display-only Unsloth release metadata for packaged builds.
_STUDIO_BUILD_INFO="studio/backend/utils/_studio_release_build.py"
_STUDIO_BUILD_INFO_BACKUP="$(mktemp)"
cp "$_STUDIO_BUILD_INFO" "$_STUDIO_BUILD_INFO_BACKUP"
diff --git a/install.ps1 b/install.ps1
index c25d7e7b7f..df49414620 100644
--- a/install.ps1
+++ b/install.ps1
@@ -176,7 +176,7 @@ function Install-UnslothStudio {
$envOverride = $env:STUDIO_HOME.Trim()
}
- # Custom Studio roots are not supported with --tauri (desktop app still
+ # Custom Unsloth roots are not supported with --tauri (desktop app still
# resolves %USERPROFILE%\.unsloth\studio). Pass through if override == legacy.
if ($TauriMode -and $envOverride) {
$_tauriOverride = $envOverride
@@ -756,7 +756,7 @@ function Find-FreeLaunchPort {
return `$null
}
-# If Studio is already healthy on any expected port, just open it and exit.
+# If Unsloth is already healthy on any expected port, just open it and exit.
`$existingPort = Find-HealthyStudioPort
if (`$existingPort) {
Start-Process "http://localhost:`$existingPort"
@@ -772,7 +772,7 @@ try {
`$haveMutex = `$true
}
if (-not `$haveMutex) {
- # Another launcher is already running; wait for it to bring Studio up
+ # Another launcher is already running; wait for it to bring Unsloth up
`$deadline = (Get-Date).AddSeconds(`$timeoutSec)
while ((Get-Date) -lt `$deadline) {
`$port = Find-HealthyStudioPort
@@ -1438,7 +1438,7 @@ exit 0
if (Test-Path -LiteralPath $VenvPython) {
# why: matching guard to the .venv branch below -- in env-mode
# $StudioHome is a user-chosen workspace, so refuse to nuke an
- # existing $StudioHome\unsloth_studio that lacks Studio sentinels.
+ # existing $StudioHome\unsloth_studio that lacks Unsloth sentinels.
# -PathType Leaf rejects a directory at the sentinel path. Accept the
# in-VENV ownership marker so partial-install retries are not blocked.
if (
@@ -1449,7 +1449,7 @@ exit 0
) {
Write-Host "[ERROR] $VenvDir already exists but does not look like an Unsloth Studio install." -ForegroundColor Red
Write-Host " Move it aside or choose an empty UNSLOTH_STUDIO_HOME." -ForegroundColor Yellow
- throw "Refusing to delete non-Studio venv at $VenvDir"
+ throw "Refusing to delete non-Unsloth venv at $VenvDir"
}
# New layout already exists -- replace only after preserving rollback copy.
substep "preserving existing environment for rollback..."
@@ -1468,7 +1468,7 @@ exit 0
# workspace root (e.g. user's existing project Python venv).
$OldVenv = Join-Path $StudioHome ".venv"
$OldPy = Join-Path $OldVenv "Scripts\python.exe"
- substep "found legacy Studio environment, validating..."
+ substep "found legacy Unsloth environment, validating..."
$prevEAP2 = $ErrorActionPreference
$ErrorActionPreference = "Continue"
try {
@@ -1498,7 +1498,7 @@ exit 0
# Skip in env-mode so we don't relocate the default-install venv into
# the workspace root.
$CwdVenv = Join-Path $env:USERPROFILE "unsloth_studio"
- substep "found CWD-relative Studio environment, migrating to $VenvDir..."
+ substep "found CWD-relative Unsloth environment, migrating to $VenvDir..."
Move-Item -LiteralPath $CwdVenv -Destination $VenvDir -Force
substep "moved ~/unsloth_studio -> ~/.unsloth/studio/unsloth_studio"
$_Migrated = $true
@@ -1517,7 +1517,7 @@ exit 0
substep "$VenvDir"
}
- # Mark the freshly-created venv as Studio-owned so a partial install can be
+ # Mark the freshly-created venv as Unsloth-owned so a partial install can be
# repaired by re-running install.ps1; the env-mode deletion guard above
# accepts this marker as the primary sentinel.
if (Test-Path -LiteralPath $VenvDir -PathType Container) {
@@ -1526,7 +1526,7 @@ exit 0
# ── Helper: run amd-smi without triggering a UAC elevation prompt ──
# amd-smi on Windows auto-elevates to read GPU/APU memory, surfacing a confusing
- # DiskPart UAC prompt mid-install (Studio backend amd.py hits the same).
+ # DiskPart UAC prompt mid-install (Unsloth backend amd.py hits the same).
# __COMPAT_LAYER=RunAsInvoker forces it (and helpers it spawns) to run
# un-elevated; on failure the WMI name -> gfx fallback still resolves the arch.
function Invoke-AmdSmiNoElevate {
@@ -1653,7 +1653,7 @@ exit 0
function Test-HipinfoIsVenvInternal {
param([AllowNull()][string]$HipinfoPath)
if ([string]::IsNullOrWhiteSpace($HipinfoPath)) { return $false }
- # Also derive the venv from the setup python + default Studio home, so
+ # Also derive the venv from the setup python + default Unsloth home, so
# the venv hipInfo is caught when VenvDir/VIRTUAL_ENV are unset.
$venvRoots = @()
if ($env:VIRTUAL_ENV) { $venvRoots += $env:VIRTUAL_ENV }
@@ -1663,7 +1663,7 @@ exit 0
try { $venvRoots += (Split-Path -Parent (Split-Path -Parent $env:UNSLOTH_SETUP_PYTHON)) } catch {}
}
if ($env:USERPROFILE) { $venvRoots += (Join-Path $env:USERPROFILE ".unsloth\studio\unsloth_studio") }
- # A custom Studio home (UNSLOTH_STUDIO_HOME / STUDIO_HOME alias) moves the
+ # A custom Unsloth home (UNSLOTH_STUDIO_HOME / STUDIO_HOME alias) moves the
# venv off the default path; seed it too or its hipInfo escapes the filter.
$studioHomeEnv = if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) { $env:UNSLOTH_STUDIO_HOME.Trim() } elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) { $env:STUDIO_HOME.Trim() } else { $null }
if ($studioHomeEnv) {
@@ -1942,7 +1942,7 @@ exit 0
substep " Ensure the ROCm compute driver is installed alongside the display driver:" "Yellow"
substep " https://rocm.docs.amd.com/en/latest/deploy/windows/index.html" "Yellow"
} elseif ($ROCmGfxArch) {
- # Known arch: Studio setup installs AMD's bundled-runtime ROCm PyTorch wheels
+ # Known arch: Unsloth setup installs AMD's bundled-runtime ROCm PyTorch wheels
# (repo.amd.com), which ship their own runtime -- HIP SDK optional.
step "gpu" "AMD ROCm ($ROCmGfxArch)" "Cyan"
substep "Detected: $ROCmGpuLabel" "Cyan"
@@ -2219,8 +2219,8 @@ exit 0
$torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (AMD ROCm)" { uv pip install --python $VenvPython --force-reinstall --default-index $ROCmIndexUrl $torchSpec $visionSpec $audioSpec }
if ($torchInstallExit -ne 0) {
# Transient AMD-index failure: fall back to a CPU base so the install
- # still completes; Studio setup retries ROCm afterwards.
- substep "ROCm PyTorch install failed (exit $torchInstallExit); using a CPU base, Studio setup retries ROCm." "Yellow"
+ # still completes; Unsloth setup retries ROCm afterwards.
+ substep "ROCm PyTorch install failed (exit $torchInstallExit); using a CPU base, Unsloth setup retries ROCm." "Yellow"
# --force-reinstall: a failed ROCm install can leave an unpinned ROCm
# torch (e.g. 2.10.0+rocm on gfx110X/gfx90a) that still satisfies the CPU
# torch>= range, so without it uv would keep the ROCm build and only swap
@@ -2422,7 +2422,7 @@ exit 0
Write-TauriLog "ERROR" "unsloth CLI was not installed correctly"
Write-Host "[ERROR] unsloth CLI was not installed correctly." -ForegroundColor Red
Write-Host " Expected: $UnslothExe" -ForegroundColor Yellow
- Write-Host " This usually means an older unsloth version was installed that does not include the Studio CLI." -ForegroundColor Yellow
+ Write-Host " This usually means an older unsloth version was installed that does not include the Unsloth CLI." -ForegroundColor Yellow
Write-Host " Try re-running the installer or see: https://github.com/unslothai/unsloth?tab=readme-ov-file#-quickstart" -ForegroundColor Yellow
return (Exit-InstallFailure "unsloth CLI was not installed correctly")
}
@@ -2533,7 +2533,7 @@ exit 0
Write-Host " Move or remove it manually, then re-run the installer." -ForegroundColor Yellow
throw "Cannot create unsloth launcher: $ShimExe is a directory."
}
- # try/catch: if unsloth.exe is locked (Studio running), keep the old shim.
+ # try/catch: if unsloth.exe is locked (Unsloth running), keep the old shim.
$shimUpdated = $false
try {
if (Test-Path -LiteralPath $ShimExe) { Remove-Item -LiteralPath $ShimExe -Force -ErrorAction Stop }
@@ -2551,7 +2551,7 @@ exit 0
if (Test-Path -LiteralPath $ShimExe) {
Write-Host "[WARN] Could not refresh unsloth launcher at $ShimExe." -ForegroundColor Yellow
Write-Host " This usually means a running 'unsloth studio' process still holds the file open." -ForegroundColor Yellow
- Write-Host " Close Studio and re-run the installer to pick up the latest launcher." -ForegroundColor Yellow
+ Write-Host " Close Unsloth and re-run the installer to pick up the latest launcher." -ForegroundColor Yellow
Write-Host " Continuing with the existing launcher." -ForegroundColor Yellow
} else {
Write-Host "[WARN] Could not create unsloth launcher at $ShimExe" -ForegroundColor Yellow
@@ -2616,7 +2616,7 @@ exit 0
# Diagnostic only; never block install on a probe failure.
}
- # In interactive terminals, ask the user before starting Studio unless the
+ # In interactive terminals, ask the user before starting Unsloth unless the
# caller explicitly disabled the post-install prompt.
# In non-interactive environments (CI, Docker) just print instructions.
$IsInteractive = (-not $SkipAutostart) -and [Environment]::UserInteractive -and (-not [Console]::IsInputRedirected)
diff --git a/install.sh b/install.sh
index 4a3c4471fa..5972379d26 100755
--- a/install.sh
+++ b/install.sh
@@ -97,7 +97,7 @@ if [ "$_VERBOSE" = true ]; then
export UNSLOTH_VERBOSE=1
fi
-# Custom Studio roots are not supported with --tauri (desktop app still
+# Custom Unsloth roots are not supported with --tauri (desktop app still
# resolves ~/.unsloth/studio). Pass through if the override == legacy default.
if [ "$TAURI_MODE" = true ]; then
_tauri_override_var=""
@@ -663,7 +663,7 @@ POLL_INTERVAL_SEC=0.25
LOG_FILE="$DATA_DIR/studio.log"
# why: in env-override mode multiple installs share an OS user; namespace the
# lock and remember our own healthy port so we never attach to an unrelated
-# Studio listening on the global 8888..8908 range.
+# Unsloth listening on the global 8888..8908 range.
LOCK_DIR="${XDG_RUNTIME_DIR:-/tmp}/unsloth-studio-launcher-$(id -u).lock"
PORT_FILE=""
# why: gate on the install-time mode (baked above) instead of the runtime env
@@ -734,7 +734,7 @@ _candidate_ports() {
_find_healthy_port() {
if [ -n "$PORT_FILE" ] && [ -f "$PORT_FILE" ]; then
# why: env-mode installs only attach to a port we previously launched
- # ourselves; never to a sibling Studio that happens to be healthy.
+ # ourselves; never to a sibling Unsloth that happens to be healthy.
_p=$(cat "$PORT_FILE" 2>/dev/null || true)
case "$_p" in
''|*[!0-9]*) ;;
@@ -901,7 +901,7 @@ _acquire_lock() {
# Lock dir exists -- check if owner is still alive
_old_pid=$(cat "$LOCK_DIR/pid" 2>/dev/null || true)
if [ -n "$_old_pid" ] && kill -0 "$_old_pid" 2>/dev/null; then
- # Another launcher is running; wait for it to bring Studio up
+ # Another launcher is running; wait for it to bring Unsloth up
_deadline=$(($(date +%s) + TIMEOUT_SEC))
while [ "$(date +%s)" -lt "$_deadline" ]; do
_port=$(_find_healthy_port) && {
@@ -1371,7 +1371,7 @@ WSLPS1_EOF
# shortcut wasn't created; tell the user how to launch / re-enable it.
if [ "$_css_created" -ne 1 ]; then
substep "Couldn't create the Windows shortcut (WSL interop may be disabled)." "$C_WARN"
- substep " Launch Studio from Windows: wsl -d \"$_css_distro\" -- bash -lc 'unsloth studio'" "$C_WARN"
+ substep " Launch Unsloth from Windows: wsl -d \"$_css_distro\" -- bash -lc 'unsloth studio'" "$C_WARN"
substep " (re-enable shortcuts: turn WSL interop back on, e.g. run 'wsl --shutdown' then reopen WSL.)" "$C_WARN"
fi
fi
@@ -1439,7 +1439,7 @@ if [ "$MAC_INTEL" = true ]; then
echo ""
echo " NOTE: Intel Mac (x86_64) detected."
echo " PyTorch is unavailable for this platform (dropped Jan 2024)."
- echo " Studio will install in GGUF-only mode."
+ echo " Unsloth will install in GGUF-only mode."
echo " Chat, inference via GGUF, and data recipes will work."
echo " Training requires Apple Silicon or Linux with GPU."
echo ""
@@ -1671,7 +1671,7 @@ _maybe_reroute_strixhalo_to_2404() {
_maybe_reroute_strixhalo_to_2404 || true
# ── Check system dependencies ──
-# cmake/git are only needed to *build* llama.cpp from source. Studio downloads a
+# cmake/git are only needed to *build* llama.cpp from source. Unsloth downloads a
# prebuilt by default, and setup.sh self-skips the source build when they're
# absent -- so macOS doesn't block on cmake (requiring it would force a manual
# Homebrew install). Linux keeps requiring them; its package manager has them.
@@ -1825,7 +1825,7 @@ _MIGRATED=false
if [ -x "$VENV_DIR/bin/python" ]; then
# why: matching guard to the .venv branch below -- in env-mode
# $STUDIO_HOME is a user-chosen workspace, so refuse to nuke an
- # existing $STUDIO_HOME/unsloth_studio that lacks Studio sentinels.
+ # existing $STUDIO_HOME/unsloth_studio that lacks Unsloth sentinels.
# Accept the in-VENV ownership marker so partial-install retries are
# not blocked. Sentinels must be regular files: -f follows symlinks
# to files (the legitimate ln -s shim shape) but rejects directories
@@ -1846,7 +1846,7 @@ elif [ "$_STUDIO_HOME_REDIRECT" != "env" ] && [ -x "$STUDIO_HOME/.venv/bin/pytho
# Skip in env-mode so we don't rm -rf an unrelated .venv at the
# workspace root (e.g. user's existing project Python venv).
# In no-torch mode, a missing torch package is expected; validate Python only.
- substep "found legacy Studio environment, validating..."
+ substep "found legacy Unsloth environment, validating..."
_legacy_ok=false
if [ "$SKIP_TORCH" = true ]; then
if "$STUDIO_HOME/.venv/bin/python" -c "import sys; print(sys.executable)" >/dev/null 2>&1; then
@@ -1903,7 +1903,7 @@ if [ ! -x "$VENV_DIR/bin/python" ]; then
fi
fi
-# Mark the freshly-created venv as Studio-owned so a partial install can be
+# Mark the freshly-created venv as Unsloth-owned so a partial install can be
# repaired by re-running install.sh; the env-mode deletion guard above accepts
# this marker as the primary sentinel.
if [ -x "$VENV_DIR/bin/python" ]; then
@@ -2335,7 +2335,7 @@ _pick_radeon_wheel() {
# the installer -- always returns 0. Runs the idempotent helper (ROCm 7.2 +
# librocdxg), then sources the env it persisted so detection finds the GPU.
# Export the ROCm-on-WSL env into this process and persist it to /etc/profile.d
-# so non-login Studio/llama launches inherit it. Idempotent (writes only when
+# so non-login Unsloth/llama launches inherit it. Idempotent (writes only when
# the drop-in is missing); no-op without librocdxg, so never fires off WSL.
# /etc/profile.d is root-owned -- sudo-tee when not root, else ROCm vanishes
# after this shell on a non-root reinstall. Best-effort either way.
@@ -2380,7 +2380,7 @@ _maybe_bootstrap_rocm_wsl() {
rocminfo 2>/dev/null | awk '/Name:[[:space:]]*gfx[1-9]/ && !/generic/{found=1} END{exit !found}'; then
# rocminfo may work only via the transient env _ensure_rocm_probe_env
# just set, which dies with the installer. Persist the drop-in so login
- # shells (Studio, llama.cpp) inherit it -- else a reinstall over an
+ # shells (Unsloth, llama.cpp) inherit it -- else a reinstall over an
# existing /opt/rocm (uninstall keeps ROCm but drops it) loses the GPU.
_persist_rocm_wsl_dropin
return 0
@@ -2402,7 +2402,7 @@ _maybe_bootstrap_rocm_wsl() {
# shellcheck disable=SC1091
. /etc/profile.d/unsloth-rocm-wsl.sh || true
else
- # librocdxg present but the env drop-in is gone (e.g. a Studio
+ # librocdxg present but the env drop-in is gone (e.g. an Unsloth
# uninstall removed it while keeping shared ROCm). Restore the env.
_persist_rocm_wsl_dropin
fi
@@ -3033,7 +3033,7 @@ if [ "$SKIP_TORCH" = false ] && [ -n "${TORCH_INDEX_URL:-}" ]; then
fi
# ── Run studio setup ──
-tauri_log "STEP" "Running Studio setup"
+tauri_log "STEP" "Running Unsloth setup"
# When --local, use the repo's own setup.sh directly.
# Otherwise, find it inside the installed package.
SETUP_SH=""
@@ -3227,7 +3227,7 @@ printf " ${C_TITLE}%s${C_RST}\n" "Unsloth Studio installed!"
printf " ${C_DIM}%s${C_RST}\n" "$RULE"
echo ""
-# In interactive terminals, ask the user before starting Studio unless the
+# In interactive terminals, ask the user before starting Unsloth unless the
# caller explicitly disabled the post-install prompt.
# In non-interactive environments (Docker, CI, cloud-init) just print instructions.
if [ "$_SKIP_AUTOSTART" != true ] && [ -t 1 ]; then
diff --git a/scripts/install_rocm_wsl_strixhalo.sh b/scripts/install_rocm_wsl_strixhalo.sh
index aa560fc432..697aae933f 100644
--- a/scripts/install_rocm_wsl_strixhalo.sh
+++ b/scripts/install_rocm_wsl_strixhalo.sh
@@ -219,7 +219,7 @@ fi
echo "${ROCM_DIR}/lib" | $SUDO tee /etc/ld.so.conf.d/rocm.conf >/dev/null
$SUDO ldconfig
-# ── Step 4: persist environment (system-wide so Studio's worker inherits it) ──
+# ── Step 4: persist environment (system-wide so Unsloth's worker inherits it) ──
say "Persisting ROCm-on-WSL environment"
_envfile="/etc/profile.d/unsloth-rocm-wsl.sh"
$SUDO tee "$_envfile" >/dev/null < str:
return f'''# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Build-stamped Studio release metadata."""
+"""Build-stamped Unsloth release metadata."""
STUDIO_RELEASE_VERSION = {literal}
'''
@@ -168,7 +168,7 @@ def stamp(require_release: bool) -> int:
version, source = resolve_version()
if version is not None and not is_valid_version(version):
print(
- f"Invalid Studio release version from {source}: {version!r}",
+ f"Invalid Unsloth release version from {source}: {version!r}",
file = sys.stderr,
)
return 2
@@ -196,9 +196,9 @@ def stamp(require_release: bool) -> int:
if version is None:
if require_release:
print(
- "No Studio release version available. Set "
+ "No Unsloth release version available. Set "
"UNSLOTH_STUDIO_RELEASE_VERSION, build from a GitHub tag, "
- "or run from an exact local Studio release tag.",
+ "or run from an exact local Unsloth release tag.",
file = sys.stderr,
)
return 2
@@ -207,7 +207,7 @@ def stamp(require_release: bool) -> int:
return 0
_atomic_write_text(BUILD_INFO_PATH, build_info_source(version), encoding = "utf-8")
- print(f"Stamping Studio release version {version} from {source}", file = sys.stderr)
+ print(f"Stamping Unsloth release version {version} from {source}", file = sys.stderr)
print(version)
return 0
@@ -233,7 +233,7 @@ def _read_sdist_member(path: Path) -> str | None:
def verify_dist(expected: str, dist_dir: Path) -> int:
if not is_valid_version(expected):
- print(f"Invalid expected Studio release version: {expected!r}", file = sys.stderr)
+ print(f"Invalid expected Unsloth release version: {expected!r}", file = sys.stderr)
return 2
artifacts = list(dist_dir.glob("*.whl")) + list(dist_dir.glob("*.tar.gz"))
@@ -251,14 +251,14 @@ def verify_dist(expected: str, dist_dir: Path) -> int:
if content is None:
failures.append(f"{artifact.name}: missing {BUILD_INFO_SUFFIX}")
elif expected_line not in content:
- failures.append(f"{artifact.name}: Studio release version mismatch")
+ failures.append(f"{artifact.name}: Unsloth release version mismatch")
if failures:
for failure in failures:
print(failure, file = sys.stderr)
return 2
- print(f"Verified Studio release version {expected} in {len(artifacts)} artifact(s)")
+ print(f"Verified Unsloth release version {expected} in {len(artifacts)} artifact(s)")
return 0
diff --git a/scripts/uninstall.ps1 b/scripts/uninstall.ps1
index 88defb9ea0..9b6e6ebb86 100644
--- a/scripts/uninstall.ps1
+++ b/scripts/uninstall.ps1
@@ -83,7 +83,7 @@ function Uninstall-UnslothStudio {
}
}
- # A path is a Studio-owned root iff one of install.ps1's sentinels exists:
+ # A path is an Unsloth-owned root iff one of install.ps1's sentinels exists:
# \share\studio.conf, \unsloth_studio\.unsloth-studio-owned,
# or \bin\unsloth.exe.
function _IsStudioRoot {
@@ -164,7 +164,7 @@ function Uninstall-UnslothStudio {
return $p
}
- # Discover non-default Studio roots from env vars + studio.conf files.
+ # Discover non-default Unsloth roots from env vars + studio.conf files.
# Mirrors install.ps1's precedence: UNSLOTH_STUDIO_HOME wins, STUDIO_HOME
# is ignored when both are set, so uninstalling install A doesn't also
# delete install B if the user has a stale STUDIO_HOME pointing at B.
@@ -207,7 +207,7 @@ function Uninstall-UnslothStudio {
# Return $true iff the PID's image path lives under one of $KnownRoots.
# Prevents killing an unrelated process that happens to listen on a stale
- # Studio port.
+ # Unsloth port.
function _PidUnderKnownRoot {
param([int]$Pid_, [string[]]$KnownRoots)
if (-not $KnownRoots -or $KnownRoots.Count -eq 0) { return $false }
@@ -223,8 +223,8 @@ function Uninstall-UnslothStudio {
return $false
}
- # Stop a Studio backend whose port is recorded in \studio.port.
- # Only kills if the listening PID's exe path is under a known Studio root.
+ # Stop an Unsloth backend whose port is recorded in \studio.port.
+ # Only kills if the listening PID's exe path is under a known Unsloth root.
function _StopByPortFile {
param([string]$PortFile, [string[]]$KnownRoots)
if (-not (Test-Path -LiteralPath $PortFile -PathType Leaf)) { return }
@@ -372,7 +372,7 @@ function Uninstall-UnslothStudio {
continue
}
if (-not (_IsStudioRoot $r)) {
- _Substep "refusing to remove non-Studio path: $r" "Yellow"
+ _Substep "refusing to remove non-Unsloth path: $r" "Yellow"
continue
}
_RemovePath $r
@@ -436,7 +436,7 @@ function Uninstall-UnslothStudio {
$entries = $rawPath -split ';'
$kept = New-Object System.Collections.ArrayList
$removedAny = $false
- # Only remove PATH entries that live inside a Studio root we
+ # Only remove PATH entries that live inside an Unsloth root we
# actually own (default or env-mode). A literal substring
# match on `unsloth_studio` would clobber unrelated user
# virtualenvs that happen to share the name.
diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh
index 31e851fcbb..957d2b7af2 100755
--- a/scripts/uninstall.sh
+++ b/scripts/uninstall.sh
@@ -12,7 +12,7 @@
set -e
-# Stop a Studio server via its PID file (written by install.sh's _spawn_terminal).
+# Stop an Unsloth server via its PID file (written by install.sh's _spawn_terminal).
_kill_pid_file() {
_pid_file="$1"
[ -f "$_pid_file" ] || return 0
@@ -47,7 +47,7 @@ _pkill_studio() {
command -v pkill >/dev/null 2>&1 || return 0
# Scope fallback patterns to the install roots we are removing so a
- # different Studio install (different UNSLOTH_STUDIO_HOME) is not touched.
+ # different Unsloth install (different UNSLOTH_STUDIO_HOME) is not touched.
_kill_roots="$HOME/.unsloth/studio"
_roots_from_conf=$(_custom_studio_roots 2>/dev/null || true)
[ -n "$_roots_from_conf" ] && _kill_roots="$_kill_roots
@@ -89,7 +89,7 @@ _remove_path() {
fi
}
-# Accept as Studio root only if Studio sentinels exist (matches install.sh's
+# Accept as Unsloth root only if Unsloth sentinels exist (matches install.sh's
# env-mode ownership guard at install.sh:1358-1361). A bare unsloth_studio/
# directory is NOT enough -- require the install-time owner marker so a user
# directory that happens to contain a folder named "unsloth_studio" is safe.
@@ -175,8 +175,8 @@ _custom_studio_roots() {
_from_conf "$HOME/.local/share/unsloth/studio.conf"
}
-# Remove $HOME/.local/bin/unsloth only if it's a Studio-managed symlink.
-# Studio's install.sh writes this as a symlink into the studio venv
+# Remove $HOME/.local/bin/unsloth only if it's an Unsloth-managed symlink.
+# Unsloth's install.sh writes this as a symlink into the studio venv
# (install.sh: `ln -sfn "$VENV_DIR/bin/unsloth" "$_shim_path"`). A
# pip-installed `unsloth` CLI is a regular file — leave it alone to avoid
# wiping an unrelated install.
@@ -206,7 +206,7 @@ _custom_studio_roots | while IFS= read -r _custom_root; do
continue
fi
if ! _is_studio_root "$_custom_root"; then
- echo " refusing to remove non-Studio path: $_custom_root" >&2
+ echo " refusing to remove non-Unsloth path: $_custom_root" >&2
continue
fi
_remove_path "$_custom_root"
@@ -234,7 +234,7 @@ _remove_path "$HOME/.unsloth/rocm-smoketest"
# Drop ~/.unsloth only if now empty (rmdir refuses non-empty, so user content is kept).
rmdir "$HOME/.unsloth" 2>/dev/null || true
_remove_path "$HOME/.local/share/unsloth"
-# CLI shim: only the symlink Studio created, never a pip-installed file.
+# CLI shim: only the symlink Unsloth created, never a pip-installed file.
_remove_cli_shim
echo "Removing desktop shortcut and launcher lock..."
diff --git a/studio/MCP.md b/studio/MCP.md
index 91b39fcc69..127a85a116 100644
--- a/studio/MCP.md
+++ b/studio/MCP.md
@@ -1,10 +1,10 @@
# Unsloth Studio MCP server
-Studio can expose a local MCP server so an MCP client can inspect models and
+Unsloth can expose a local MCP server so an MCP client can inspect models and
GPU state, validate recipes, start or stop training, inspect recipe output, and
export a loaded model.
-The server is disabled by default. Enable it for a local Studio process with:
+The server is disabled by default. Enable it for a local Unsloth process with:
```bash
UNSLOTH_STUDIO_ENABLE_MCP=1 \
@@ -12,8 +12,8 @@ UNSLOTH_STUDIO_MCP_TOKEN='use-a-local-secret' \
unsloth studio
```
-The endpoint is `http://127.0.0.1:8888/mcp/` when Studio uses its default port
-(a request to `/mcp` redirects to the canonical `/mcp/`). Use the actual Studio
+The endpoint is `http://127.0.0.1:8888/mcp/` when Unsloth uses its default port
+(a request to `/mcp` redirects to the canonical `/mcp/`). Use the actual Unsloth
port when it is configured differently.
The high-impact tools are:
@@ -23,9 +23,9 @@ The high-impact tools are:
- `validate_recipe`, `get_recipe_job_status`, and `get_recipe_job_dataset`
- `load_checkpoint` and `export_gguf`
-`start_training` accepts the same fields as the Studio `TrainingStartRequest`.
+`start_training` accepts the same fields as the Unsloth `TrainingStartRequest`.
The request is validated by the existing Pydantic model before a subprocess is
-started. Export paths use the existing Studio validation as well.
+started. Export paths use the existing Unsloth validation as well.
The endpoint always requires `UNSLOTH_STUDIO_MCP_TOKEN` and checks an exact
Bearer token for both HTTP and WebSocket connections. Keep it on localhost
diff --git a/studio/Unsloth_Studio_Colab.ipynb b/studio/Unsloth_Studio_Colab.ipynb
index 619395bd6d..44282b2255 100644
--- a/studio/Unsloth_Studio_Colab.ipynb
+++ b/studio/Unsloth_Studio_Colab.ipynb
@@ -33,7 +33,7 @@
"\n",
"We are actively working on making Unsloth Studio install on Colab T4 GPUs faster.\n",
"\n",
- "[Features](https://unsloth.ai/docs/new/unsloth-studio#features) • [Quickstart](https://unsloth.ai/docs/new/unsloth-studio/start) • [Data Recipes](https://unsloth.ai/docs/new/unsloth-studio/data-recipe) • [Studio Chat](https://unsloth.ai/docs/new/unsloth-studio/chat) • [Export](https://unsloth.ai/docs/new/unsloth-studio/export)"
+ "[Features](https://unsloth.ai/docs/new/unsloth-studio#features) • [Quickstart](https://unsloth.ai/docs/new/unsloth-studio/start) • [Data Recipes](https://unsloth.ai/docs/new/unsloth-studio/data-recipe) • [Unsloth Chat](https://unsloth.ai/docs/new/unsloth-studio/chat) • [Export](https://unsloth.ai/docs/new/unsloth-studio/export)"
]
},
{
diff --git a/studio/backend/assets/chat_templates/gemma-4-edge.jinja b/studio/backend/assets/chat_templates/gemma-4-edge.jinja
index 0266127233..74fa73ddd3 100644
--- a/studio/backend/assets/chat_templates/gemma-4-edge.jinja
+++ b/studio/backend/assets/chat_templates/gemma-4-edge.jinja
@@ -3,7 +3,7 @@
Source: google/gemma-4-31B-it HF discussion/PR #118 (adds the preserve_thinking
flag plus null-rendering, string-arguments validation, balanced turn tags, empty
messages handling, and OpenAI image_url/input_audio aliases).
- Studio-local changes vs PR #118:
+ Unsloth-local changes vs PR #118:
1. preserve_thinking defaults to false (see SETUP block below).
2. The empty "<|channel>thought\n" block on enable_thinking=false is
NOT emitted. Google ships a distinct template for E2B/E4B (google/gemma-4-E2B-it,
diff --git a/studio/backend/assets/chat_templates/gemma-4.jinja b/studio/backend/assets/chat_templates/gemma-4.jinja
index 65ab39df57..cc5f98065f 100644
--- a/studio/backend/assets/chat_templates/gemma-4.jinja
+++ b/studio/backend/assets/chat_templates/gemma-4.jinja
@@ -3,7 +3,7 @@
Source: google/gemma-4-31B-it HF discussion/PR #118 (adds the preserve_thinking
flag plus null-rendering, string-arguments validation, balanced turn tags, empty
messages handling, and OpenAI image_url/input_audio aliases).
- Studio-local change: preserve_thinking defaults to false (see SETUP block below).
+ Unsloth-local change: preserve_thinking defaults to false (see SETUP block below).
Applied to unsloth/gemma-4-*-GGUF models so the embedded GGUF template does not
need re-downloading. Keep in sync with upstream if PR #118 changes.
-#}
diff --git a/studio/backend/auth/authentication.py b/studio/backend/auth/authentication.py
index b13cd1c851..dfb8fc513e 100644
--- a/studio/backend/auth/authentication.py
+++ b/studio/backend/auth/authentication.py
@@ -148,7 +148,7 @@ async def authenticated_via_api_key(
) -> bool:
"""True when the caller used an sk-unsloth API key, not a UI session JWT.
- Lets routes treat programmatic API callers differently from the Studio UI
+ Lets routes treat programmatic API callers differently from the Unsloth UI
(e.g. refuse a teardown the UI would allow).
"""
return bool(credentials and credentials.credentials.startswith(API_KEY_PREFIX))
diff --git a/studio/backend/auth/bootstrap_timeout.py b/studio/backend/auth/bootstrap_timeout.py
index 728433dc54..97a8086f04 100644
--- a/studio/backend/auth/bootstrap_timeout.py
+++ b/studio/backend/auth/bootstrap_timeout.py
@@ -1,13 +1,13 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Auto-shutdown for an exposed first-run Studio whose admin password is unchanged.
+"""Auto-shutdown for an exposed first-run Unsloth whose admin password is unchanged.
On a fresh install the seeded bootstrap admin password stays a valid login
credential until first login changes it. When the web UI is put on the network
(``--secure`` / ``0.0.0.0``) and nobody completes that first-login change within
-a deadline, tear Studio down so a fresh, unconfigured instance does not stay
-publicly reachable indefinitely. If the password was changed, Studio keeps
+a deadline, tear Unsloth down so a fresh, unconfigured instance does not stay
+publicly reachable indefinitely. If the password was changed, Unsloth keeps
running.
Scope: web UI launches only (never ``--api-only``, which authenticates by API
@@ -98,7 +98,7 @@ def enforce_bootstrap_password_deadline(
) -> bool:
"""Deadline handler: shut down iff the seeded admin password is still unchanged.
- Returns True if it shut Studio down, False if it left it running (the
+ Returns True if it shut Unsloth down, False if it left it running (the
password was changed in time).
"""
try:
@@ -106,7 +106,7 @@ def enforce_bootstrap_password_deadline(
except Exception:
return False
if not still_default:
- return False # password changed in time -> leave Studio running
+ return False # password changed in time -> leave Unsloth running
message = (
"\nUnsloth Studio was exposed on the network but its default admin "
diff --git a/studio/backend/auth/storage.py b/studio/backend/auth/storage.py
index 9bb3ab5735..39fa691304 100644
--- a/studio/backend/auth/storage.py
+++ b/studio/backend/auth/storage.py
@@ -146,7 +146,7 @@ def get_connection() -> sqlite3.Connection:
pass
conn.row_factory = sqlite3.Row
# WAL lets token reads run concurrently with refresh-token writes;
- # busy_timeout bounds lock waits. Matches the other Studio SQLite stores.
+ # busy_timeout bounds lock waits. Matches the other Unsloth SQLite stores.
# Set busy_timeout first: switching journal_mode needs a lock, so if a
# refresh-token write already holds one, journal_mode=WAL raises SQLITE_BUSY;
# with busy_timeout already in effect it waits instead of failing and leaving
@@ -305,8 +305,8 @@ def get_or_create_identity_secret() -> bytes:
def compute_identity_proof(nonce: bytes, host: str, port: int) -> str:
"""HMAC-SHA256 proof that the caller holds this install's identity secret,
bound to the loopback address and port the connection landed on. A proof
- relayed from a Studio on a different address/port (a squatter proxying to the
- real one, e.g. localhost resolving to ::1 while Studio is on 127.0.0.1) was
+ relayed from an Unsloth on a different address/port (a squatter proxying to the
+ real one, e.g. localhost resolving to ::1 while Unsloth is on 127.0.0.1) was
computed for that other endpoint and won't match the one the client dialed."""
try:
host = ipaddress.ip_address(host).compressed # normalise 127.0.0.1 / ::1 forms
diff --git a/studio/backend/auth/terminal_prompt.py b/studio/backend/auth/terminal_prompt.py
index 8491019ae9..e855f4078b 100644
--- a/studio/backend/auth/terminal_prompt.py
+++ b/studio/backend/auth/terminal_prompt.py
@@ -2,14 +2,14 @@
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Interactive terminal prompt that forces a bootstrap password change before
-Studio is exposed on a public Cloudflare URL (``--secure`` / ``--cloudflare``).
+Unsloth is exposed on a public Cloudflare URL (``--secure`` / ``--cloudflare``).
Masked input echoes one ``*`` per keystroke (unlike ``getpass``). Works on
Windows (``msvcrt``) and Linux/macOS (``termios``). All output goes to stderr so
redirected stdout never swallows the prompt.
Mirrored for the CLI at ``unsloth_cli/commands/_password_prompt.py`` (the CLI
-cannot import the Studio backend package); keep the two in sync.
+cannot import the Unsloth backend package); keep the two in sync.
"""
from __future__ import annotations
@@ -252,7 +252,7 @@ def prompt_for_password_change(
out.flush()
return True
except (KeyboardInterrupt, EOFError):
- out.write("Password change aborted; not exposing Studio.\n")
+ out.write("Password change aborted; not exposing Unsloth.\n")
out.flush()
return False
diff --git a/studio/backend/cloudflare_tunnel.py b/studio/backend/cloudflare_tunnel.py
index ef7bacba67..b1ddc74c32 100644
--- a/studio/backend/cloudflare_tunnel.py
+++ b/studio/backend/cloudflare_tunnel.py
@@ -1,13 +1,13 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Free Cloudflare quick tunnel for Studio's 0.0.0.0 launches.
+"""Free Cloudflare quick tunnel for Unsloth's 0.0.0.0 launches.
The raw http://: is often unreachable (https-vs-http, blocked ports,
closed security groups); a cloudflared quick tunnel gives a free
https://*.trycloudflare.com URL that works anywhere, with no account or domain.
-Best-effort throughout: any failure collapses to "no URL" and Studio keeps
+Best-effort throughout: any failure collapses to "no URL" and Unsloth keeps
running. Stdlib only (back-end imports are lazy) so it is safe to import early.
"""
@@ -95,7 +95,7 @@ def _cache_path() -> Optional[Path]:
def find_cloudflared() -> Optional[str]:
- """Locate an existing cloudflared: PATH first, then the Studio bin cache."""
+ """Locate an existing cloudflared: PATH first, then the Unsloth bin cache."""
on_path = shutil.which("cloudflared")
if on_path:
return on_path
@@ -309,7 +309,7 @@ class CloudflareTunnel:
pass
-# Single serving process per Studio launch, so one module-level tunnel handle is
+# Single serving process per Unsloth launch, so one module-level tunnel handle is
# enough; the lock guards the start/stop/shutdown races.
_active_tunnel: Optional[CloudflareTunnel] = None
_active_lock = threading.Lock()
diff --git a/studio/backend/colab.py b/studio/backend/colab.py
index e04543b3aa..1762469bcf 100644
--- a/studio/backend/colab.py
+++ b/studio/backend/colab.py
@@ -129,7 +129,7 @@ def start_cloudflare_tunnel(port: int) -> "str | None":
logger.warning(
"Cloudflare link not started: the admin account still has its temporary "
"bootstrap password, which is exposed to anyone who can load the page. "
- "Open Studio in this tab, log in and change the admin password, then re-run "
+ "Open Unsloth in this tab, log in and change the admin password, then re-run "
"start(cloudflare=True) to get the shareable link."
)
return None
@@ -203,7 +203,7 @@ def _shareable_link_html(cloudflare_url: str) -> str:
display: flex; align-items: center; gap: 12px;">
- Shareable Studio Link is Ready!
+ Shareable Unsloth Link is Ready!
ParsedUpdate | None:
source = "github",
status = "rate_limited",
retry_after_sec = seconds,
- message = ("Waiting for GitHub rate limit. Studio will resume automatically."),
+ message = ("Waiting for GitHub rate limit. Unsloth will resume automatically."),
),
)
@@ -147,7 +147,7 @@ def parse_log_message(msg: str) -> ParsedUpdate | None:
status = "rate_limited",
retry_after_sec = seconds,
message = (
- "Waiting for GitHub secondary rate limit. Studio will resume automatically."
+ "Waiting for GitHub secondary rate limit. Unsloth will resume automatically."
),
),
)
@@ -161,7 +161,7 @@ def parse_log_message(msg: str) -> ParsedUpdate | None:
source = "github",
status = "rate_limited",
retry_after_sec = seconds,
- message = ("Waiting for GitHub rate limit. Studio will resume automatically."),
+ message = ("Waiting for GitHub rate limit. Unsloth will resume automatically."),
),
)
diff --git a/studio/backend/core/data_recipe/local_callable_validators.py b/studio/backend/core/data_recipe/local_callable_validators.py
index ebb1d39dfb..ffc81669ae 100644
--- a/studio/backend/core/data_recipe/local_callable_validators.py
+++ b/studio/backend/core/data_recipe/local_callable_validators.py
@@ -238,7 +238,7 @@ def _run_oxc_batch(
if not node_executable:
return _fallback_results(
len(code_values),
- "Node.js not found (install Node >= 20.19, or re-run Studio setup to provision it).",
+ "Node.js not found (install Node >= 20.19, or re-run Unsloth setup to provision it).",
)
try:
tmp_dir = ensure_dir(oxc_validator_tmp_root())
diff --git a/studio/backend/core/data_recipe/service.py b/studio/backend/core/data_recipe/service.py
index 4647dc098d..9770e88b7f 100644
--- a/studio/backend/core/data_recipe/service.py
+++ b/studio/backend/core/data_recipe/service.py
@@ -280,8 +280,8 @@ def create_data_designer(recipe: dict[str, Any], *, artifact_path: str | None =
from data_designer.interface.data_designer import DataDesigner # pyright: ignore[reportMissingImports]
if artifact_path is None:
- # DataDesigner defaults to cwd/artifacts; packaged Studio can run with
- # cwd=/, so keep default callers on Studio's writable recipe artifact root.
+ # DataDesigner defaults to cwd/artifacts; packaged Unsloth can run with
+ # cwd=/, so keep default callers on Unsloth's writable recipe artifact root.
artifact_path = str(recipe_datasets_root())
recipe = _strip_frontend_model_config_metadata(recipe)
diff --git a/studio/backend/core/inference/__init__.py b/studio/backend/core/inference/__init__.py
index ad78157418..1491dfa749 100644
--- a/studio/backend/core/inference/__init__.py
+++ b/studio/backend/core/inference/__init__.py
@@ -11,7 +11,7 @@ subprocess and can be imported directly from .inference when needed.
Public names are resolved lazily (PEP 562): importing this package -- or a
dependency-light leaf like ``core.inference.chat_eos`` -- must NOT eagerly pull
the orchestrator / llama_cpp import chain (httpx, subprocess plumbing, the ML
-backend and its Studio dependencies). Those load only when a public name is
+backend and its Unsloth dependencies). Those load only when a public name is
actually accessed, so standalone helpers stay unit-testable without the full
inference stack.
"""
diff --git a/studio/backend/core/inference/anthropic_compat.py b/studio/backend/core/inference/anthropic_compat.py
index 3c7a4cb182..34445cc58e 100644
--- a/studio/backend/core/inference/anthropic_compat.py
+++ b/studio/backend/core/inference/anthropic_compat.py
@@ -539,7 +539,7 @@ class AnthropicPassthroughEmitter:
Only calls naming a tool in ``allowed_tools`` (the client's declared
tools) are promoted; everything else streams as text exactly as before.
- Never enabled for Studio's own tool loop.
+ Never enabled for Unsloth's own tool loop.
"""
from core.inference.passthrough_healing import StreamToolCallHealer
diff --git a/studio/backend/core/inference/chat_template_helpers.py b/studio/backend/core/inference/chat_template_helpers.py
index 5113eebb36..528c059fbc 100644
--- a/studio/backend/core/inference/chat_template_helpers.py
+++ b/studio/backend/core/inference/chat_template_helpers.py
@@ -150,7 +150,7 @@ def _split_partial_marker(text: str, marker: str) -> tuple[str, str]:
class ReasoningChannelNormalizer:
"""Incrementally convert one native reasoning channel to ````.
- The parser follows mlx-vlm's streaming boundary behavior but emits Studio's
+ The parser follows mlx-vlm's streaming boundary behavior but emits Unsloth's
established canonical text contract. Only the configured opening and
closing markers are consumed; tool-call and other control markers remain
available to downstream parsers.
diff --git a/studio/backend/core/inference/chat_templates.py b/studio/backend/core/inference/chat_templates.py
index 58f63ff61b..04c0db6aae 100644
--- a/studio/backend/core/inference/chat_templates.py
+++ b/studio/backend/core/inference/chat_templates.py
@@ -4,13 +4,13 @@
"""Bundled chat-template selection for GGUF inference.
Some shipped GGUF quants embed an older chat template. Rather than re-cutting and
-asking users to re-download every quant, Studio can override the embedded template
+asking users to re-download every quant, Unsloth can override the embedded template
at llama-server launch time with a bundled, up-to-date Jinja template for known
model families. The override is wired through the existing ``chat_template_override``
-> ``--chat-template-file`` path in ``LlamaCppBackend.load_model``.
Currently this covers ``unsloth/gemma-4-*-GGUF``, which gains the upstream PR #118
-``preserve_thinking`` flag (defaulted OFF here) so the Studio "Preserve thinking"
+``preserve_thinking`` flag (defaulted OFF here) so the Unsloth "Preserve thinking"
toggle appears while staying disabled by default.
"""
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.py
index 20312e067c..2debf946e9 100644
--- a/studio/backend/core/inference/external_provider.py
+++ b/studio/backend/core/inference/external_provider.py
@@ -473,7 +473,7 @@ def _apply_mistral_reasoning_controls(
# handles every provider without storing credentials.
def _create_shared_http_client() -> httpx.AsyncClient:
# Unsupported env proxy schemes (socks:// etc) raise at construction and
- # would crash Studio startup (#6090); retry ignoring env proxies instead.
+ # would crash Unsloth startup (#6090); retry ignoring env proxies instead.
try:
return httpx.AsyncClient()
except (ImportError, ValueError) as exc:
@@ -858,7 +858,7 @@ class ExternalProviderClient:
if not self._is_openai_compatible():
# Gemini speaks its own native REST shape (contents/parts);
# `_stream_gemini` translates request/response into the OpenAI
- # Chat Completions chunk format the rest of Studio expects.
+ # Chat Completions chunk format the rest of Unsloth expects.
# API ref: https://ai.google.dev/gemini-api/docs
if self.provider_type == "gemini":
async for line in self._stream_gemini(
@@ -1706,7 +1706,7 @@ class ExternalProviderClient:
# Translate OpenAI multimodal parts -> Anthropic native shapes.
# - `image_url` -> `{type:"image", source:...}`
# - `input_document` -> `{type:"document", source:...}`
- # (Studio extension; mirrors Anthropic's document block,
+ # (Unsloth extension; mirrors Anthropic's document block,
# which supports PDFs as base64 or URL per
# https://platform.claude.com/docs/en/build-with-claude/vision)
anthropic_parts: list[dict[str, Any]] = []
@@ -1749,7 +1749,7 @@ class ExternalProviderClient:
}
)
elif part.get("type") == "input_document":
- # Studio's normalised PDF/doc type (file_data data-URI or
+ # Unsloth's normalised PDF/doc type (file_data data-URI or
# file_url) -> Anthropic's native `document` block.
url = part.get("file_url") or ""
data_uri = part.get("file_data") or ""
@@ -4704,7 +4704,7 @@ class ExternalProviderClient:
{"type": "image_generation_call", "id": call_id}
)
elif part_type == "input_document":
- # Map Studio's `input_document` onto Responses' `input_file`.
+ # Map Unsloth's `input_document` onto Responses' `input_file`.
# https://developers.openai.com/api/docs/guides/images-vision
file_url = part.get("file_url")
file_data = part.get("file_data")
@@ -6010,7 +6010,7 @@ class ExternalProviderClient:
if not models and self.provider_type == "ollama":
models = await self._list_ollama_native_models()
# Gemini's native /v1beta/models uses a different shape; repackage
- # into the OpenAI-compatible one Studio expects.
+ # into the OpenAI-compatible one Unsloth expects.
if not models and self.provider_type == "gemini":
models = self._parse_gemini_models(data)
return models
@@ -6213,7 +6213,7 @@ def _friendly_provider_error_text(
*,
model: str | None = None,
) -> str:
- """Rewrite common provider errors into actionable Studio copy."""
+ """Rewrite common provider errors into actionable Unsloth copy."""
if status_code == 404 and model:
lowered = raw_message.lower()
if "not found" in lowered or "not_found" in lowered:
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index fec5214cf9..c9ab7eb83b 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -116,7 +116,7 @@ LLAMA_SERVER_NOT_FOUND_DETAIL = (
# llama-server can serve HTTP 200 while running a model entirely on CPU when a
# GPU backend fails to init (#5807 / #5106 / #5830). Classify the startup log so
-# Studio can warn. Priority: explicit "offloaded N/M layers to GPU" counts
+# Unsloth can warn. Priority: explicit "offloaded N/M layers to GPU" counts
# (authoritative), then GPU "model buffer size" lines (host-pinned _Host
# excluded), then the "device_info:" device table (disconfirm only).
_GPU_OFFLOAD_MARKERS = (
@@ -1363,7 +1363,7 @@ def _kv_bytes_per_elem(cache_type: Optional[str]) -> float:
def _env_main_cache_type_for_budget(env: Optional[Mapping[str, str]] = None) -> Optional[str]:
"""Heavier of the inherited LLAMA_ARG_CACHE_TYPE_K/_V env types when it
- exceeds the f16 default, else None. Studio emits --cache-type only for the
+ exceeds the f16 default, else None. Unsloth emits --cache-type only for the
param/extras path, so a heavier env (f32) would otherwise reach the child
unbudgeted; quantized env types stay over-reserved by f16 (-> None)."""
e = os.environ if env is None else env
@@ -1682,7 +1682,7 @@ def _build_ngram_mod_flags(
return []
-# Canonical Speculative Decoding modes exposed by the Studio chat UI.
+# Canonical Speculative Decoding modes exposed by the Unsloth chat UI.
# Dropdown renders five (auto, mtp, ngram, mtp+ngram, off); the load API
# also accepts legacy values the original Switch and external callers emit
# (default, draft-mtp, ngram-mod, ngram-simple).
@@ -1731,7 +1731,7 @@ def _backfill_usage_from_timings(usage, timings):
"""Synthesize ``usage`` from llama-server's ``timings`` when the
OpenAI-style usage block is missing or reports zero tokens.
- The Studio chat UI computes generation t/s from
+ The Unsloth chat UI computes generation t/s from
``meta.usage.completion_tokens / totalStreamTime``. llama-server always
populates ``timings.predicted_n`` (true decoded count) and
``timings.prompt_n``, but the final SSE chunk's ``usage`` can be absent
@@ -1804,7 +1804,7 @@ def _llama_lib_dir(binary: str) -> Path:
def _is_external_link(path: Path) -> bool:
"""True when ``path`` is a --with-llama-cpp-dir local link: a POSIX symlink
or a Windows directory junction / reparse point. Such a link resolves into
- the user's own llama.cpp checkout, which Studio does not own."""
+ the user's own llama.cpp checkout, which Unsloth does not own."""
try:
if os.path.islink(path):
return True
@@ -1960,7 +1960,7 @@ class LlamaCppBackend:
# observes it (direct proxy endpoints, or nothing in flight).
self._mtp_watchdog_thread: Optional[threading.Thread] = None
self._mtp_watchdog_stop = threading.Event()
- # True when the launch actually runs MTP+tensor (Studio- or user/env-driven);
+ # True when the launch actually runs MTP+tensor (Unsloth- or user/env-driven);
# gates the probe, watchdog, and recovery so pass-through MTP is covered.
self._mtp_runtime_fallback_active = False
self._stdout_lines: list[str] = []
@@ -2353,7 +2353,7 @@ class LlamaCppBackend:
@staticmethod
def _resolved_studio_root_and_is_legacy() -> "tuple[Optional[Path], bool]":
- """Resolve the Studio install root and classify it as the legacy
+ """Resolve the Unsloth install root and classify it as the legacy
~/.unsloth/studio root vs. a custom (env/venv-inferred) root.
Returns (resolved_root, is_legacy). On any import/resolution failure the
@@ -3241,7 +3241,7 @@ class LlamaCppBackend:
return
prev = curr
- # Free-VRAM fraction at which Studio pins the GPU directly instead of
+ # Free-VRAM fraction at which Unsloth pins the GPU directly instead of
# deferring to ``--fit on``. 3% headroom: the compute buffer is now modelled in
# the fit, so this only guards fragmentation + multi-GPU per-device CUDA context
# (~2-3%); kept >= 3% as a floor (0.90 dropped 91-94% fits to CPU offload, #5106).
@@ -3800,7 +3800,7 @@ class LlamaCppBackend:
return total if total > 0 else None
return draft_kv + weights + target_ctx_copy
- _DEFAULT_N_UBATCH = 512 # llama.cpp --ubatch default; Studio does not override it
+ _DEFAULT_N_UBATCH = 512 # llama.cpp --ubatch default; Unsloth does not override it
_COMPUTE_BUFFER_SAFETY = 1.15 # upper-bound margin on the compute-buffer estimate
# Soft VRAM the modeled terms omit; charged to the fit budget on tight tiers (#6682).
_CUDA_CONTEXT_RESERVE_BYTES = 320 * 1024 * 1024 # CUDA ctx + cuBLAS workspace (~330 MiB)
@@ -3940,7 +3940,7 @@ class LlamaCppBackend:
n_ubatch: Optional[int] = None,
) -> tuple[Optional[list[int]], bool, int]:
"""Largest serving-slot count in [1, n_parallel) whose fully-on-GPU footprint fits,
- so Studio keeps the model on GPU (-ngl -1) instead of --fit on, which offloads layers
+ so Unsloth keeps the model on GPU (-ngl -1) instead of --fit on, which offloads layers
to host and collapses decode ~3x (oobabooga #6718). ``base_footprint_bytes`` is the
slot-independent footprint (weights + soft overhead + MTP + context-linear compute,
minus the folded compute buffer); each candidate re-adds the slot-sized compute buffer
@@ -4416,7 +4416,7 @@ class LlamaCppBackend:
]
# Otherwise hand off to the resolver (cache / bootstrap / transformers / HF). Diffusion models
- # skip it: they do not use Studio's SWA pattern and the resolver can raise for them.
+ # skip it: they do not use Unsloth's SWA pattern and the resolver can raise for them.
if (
self._sliding_window_pattern is None
and self._sliding_window
@@ -4536,7 +4536,7 @@ class LlamaCppBackend:
) -> bool:
"""Launch the OpenAI-compat diffusion shim (which drives the on-device
visual decoder) and wait for health. Presents the same /v1 + /health
- interface as llama-server, so the rest of Studio is unchanged.
+ interface as llama-server, so the rest of Unsloth is unchanged.
"""
assets = self._find_diffusion_assets()
if assets is None:
@@ -4608,7 +4608,7 @@ class LlamaCppBackend:
logger.debug(f"Could not open diffusion runner log file: {e}")
# The shim (and its visual server) die with this backend process, so a
- # Studio crash/restart never orphans a GPU process.
+ # Unsloth crash/restart never orphans a GPU process.
self._process = subprocess.Popen(
cmd,
stdout = subprocess.PIPE,
@@ -5242,7 +5242,7 @@ class LlamaCppBackend:
return (
f"'{arch}' is a diffusion (image-generation) GGUF, which "
"llama-server cannot run as a chat/completion model. Use "
- "Studio's Images page to generate with local diffusion "
+ "Unsloth's Images page to generate with local diffusion "
"GGUFs such as FLUX and Qwen-Image."
)
if is_ollama:
@@ -6103,7 +6103,7 @@ class LlamaCppBackend:
and not bool(mtp_draft_path)
)
# LLAMA_ARG_SPEC_TYPE only reaches the child when neither extras
- # nor Studio emit a spec flag (mode "off", no user --spec-type),
+ # nor Unsloth emit a spec flag (mode "off", no user --spec-type),
# since _build_speculative_flags emits one for every other mode.
# Consult the env for the reserve only then, else a stale MTP env
# would over-reserve.
@@ -6112,7 +6112,7 @@ class LlamaCppBackend:
if (not _extra_args_set_spec_type(extra_args) and _mtp_canonical == "off")
else {}
)
- # Extras can run MTP even when Studio suppresses its own emission.
+ # Extras can run MTP even when Unsloth suppresses its own emission.
_user_mtp_via_extras = _extra_args_requests_mtp(extra_args, env = _spec_env)
# A non-MTP model-based draft mode (draft-simple/draft-eagle3) in
# extras also loads a separate draft model that needs reserving;
@@ -6178,7 +6178,7 @@ class LlamaCppBackend:
_mtp_eff_n_max = 2 if gpus else 3
# Separate-drafter weights live on GPU (an embedded head is
# already in model_size). Size the drafter the launch loads, by
- # precedence: extras --model-draft (last-wins), else Studio's
+ # precedence: extras --model-draft (last-wins), else Unsloth's
# emitted mtp_draft_path, else the env drafter. Sizing the wrong
# one would under-reserve and OOM.
_cli_draft_for_budget = _extra_args_mtp_draft_path(extra_args, env = {})
@@ -7097,12 +7097,12 @@ class LlamaCppBackend:
# Vulkan pins via --device (a cmd arg, unlike the env-based
# CUDA/ROCm pin below), emitted BEFORE user extras so llama.cpp's
- # last-wins parsing lets a user --device override Studio's pick.
+ # last-wins parsing lets a user --device override Unsloth's pick.
if is_vulkan_backend and gpu_indices is not None:
cmd += LlamaCppBackend._vulkan_pin_args(gpu_indices)
# User pass-through args go last so llama.cpp's last-wins parsing
- # lets the user override Studio's auto-set flags. Already
+ # lets the user override Unsloth's auto-set flags. Already
# validated by the route via validate_extra_args().
if extra_args:
cmd.extend(str(a) for a in extra_args)
@@ -7118,9 +7118,9 @@ class LlamaCppBackend:
if "--threads" not in cmd:
env.pop("LLAMA_ARG_THREADS", None)
- # Reconcile the inherited LLAMA_ARG_* env with Studio's final
+ # Reconcile the inherited LLAMA_ARG_* env with Unsloth's final
# decision: stripping CLI extras on a tensor->layer downgrade
- # can't remove env vars, so the child could run a mode/KV Studio
+ # can't remove env vars, so the child could run a mode/KV Unsloth
# didn't budget.
if not tensor_parallel:
# Layer split: clear a non-layer inherited split mode (and any
@@ -7130,7 +7130,7 @@ class LlamaCppBackend:
env.pop("LLAMA_ARG_SPLIT_MODE", None)
env.pop("LLAMA_ARG_TENSOR_SPLIT", None)
else:
- # Studio owns the tensor split: it emits --tensor-split when it
+ # Unsloth owns the tensor split: it emits --tensor-split when it
# picks an uneven one (CLI wins) and nothing when an even split
# is safe. Clear any inherited LLAMA_ARG_TENSOR_SPLIT so the even
# case can't be overridden by a stale env (the layer branch above
@@ -7201,7 +7201,7 @@ class LlamaCppBackend:
# 'on') even when -ngl is explicit. That step has aborted on
# some ROCm hosts (ggml-cuda.cu ROCm error during worst-case
# estimation, e.g. MTP + mmproj models on gfx1151). When
- # Studio's own VRAM math already placed the model
+ # Unsloth's own VRAM math already placed the model
# (use_fit=False), the step is redundant second-guessing --
# retry once with --fit off before declaring the load failed.
# Never retry when fit was requested (use_fit) or the caller
@@ -7284,7 +7284,7 @@ class LlamaCppBackend:
and _startup_crashed
and not _split_axis_crash
):
- # We forced --fit off because Studio's (conservative) VRAM
+ # We forced --fit off because Unsloth's (conservative) VRAM
# math placed the model fully on GPU. A startup crash here
# means that estimate was optimistic, so fall back to --fit
# on and let llama.cpp offload rather than fail the load.
@@ -7296,7 +7296,7 @@ class LlamaCppBackend:
self._process.returncode,
self._llama_log_path,
)
- # Flip Studio's own --fit off (added first, before any
+ # Flip Unsloth's own --fit off (added first, before any
# user extra args) to on; a user's later --fit still wins
# by last-arg. Defensive: if absent, the default is already
# --fit on, so leave it.
@@ -7313,7 +7313,7 @@ class LlamaCppBackend:
):
logger.warning(
"llama-server crashed during startup (exit code %s) "
- "with the default memory-fit step enabled; Studio "
+ "with the default memory-fit step enabled; Unsloth "
"already verified the model fits, retrying once "
"with --fit off. Crash log: %s",
self._process.returncode,
@@ -7393,7 +7393,7 @@ class LlamaCppBackend:
cmd = _fa_cmd
healthy = _spawn_and_wait(_fa_cmd, label = "-noflash")
- # MTP from Studio's spec flags or the user's (extra_args
+ # MTP from Unsloth's spec flags or the user's (extra_args
# --spec-type / LLAMA_ARG_SPEC_TYPE). The env reaches the child
# only when neither emits a spec flag, so consult it only then.
_launch_spec_env: Mapping[str, str] = (
@@ -7587,11 +7587,11 @@ class LlamaCppBackend:
if self._gpu_offload_active is False:
logger.warning(
"llama-server appears to have loaded the model entirely "
- "on CPU even though Studio detected at least one GPU. "
+ "on CPU even though Unsloth detected at least one GPU. "
"This usually means the prebuilt binary's GPU backend "
"failed to load -- on Windows, cudart64_X.dll / "
"cublas64_X.dll could not be resolved. Reinstall the "
- "Studio llama.cpp prebuilt or install a matching CUDA "
+ "Unsloth llama.cpp prebuilt or install a matching CUDA "
"toolkit (issue unslothai/unsloth#5106).",
)
@@ -7888,7 +7888,7 @@ class LlamaCppBackend:
logger.info(
"Auto: MLA embedded-MTP model detected; llama.cpp's MLA/DSA "
"MTP path is slower than no speculation, so using ngram-mod "
- "instead. Override via the Studio Speculative Decoding "
+ "instead. Override via the Unsloth Speculative Decoding "
"dropdown or UNSLOTH_MLA_MTP_ENABLED=1."
)
_emit_ngram_mod()
@@ -7916,7 +7916,7 @@ class LlamaCppBackend:
f"MTP GGUF detected but model size {_mtp_size_b:.1f}B "
"is below the 3B speedup threshold; using ngram-mod "
"only (zero-VRAM, no draft head). Override via "
- "--spec-type or the Studio Speculative Decoding "
+ "--spec-type or the Unsloth Speculative Decoding "
"dropdown."
)
_emit_ngram_mod()
@@ -8294,7 +8294,7 @@ class LlamaCppBackend:
def _pid_parent_is_alive(pid: int) -> bool:
"""True if the recorded server's parent is still running, i.e. the server is
NOT orphaned. Lets the cross-session reap kill only a true orphan (parent
- gone) and never a live server owned by a running Studio, regardless of which
+ gone) and never a live server owned by a running Unsloth, regardless of which
process performs the sweep. Biased toward "alive" on uncertainty so a live
server is never mistakenly reaped."""
try:
@@ -8334,9 +8334,9 @@ class LlamaCppBackend:
@classmethod
def _reap_recorded_pid(cls) -> int:
"""Kill the exact llama-server PID recorded at spawn, but only when it is a
- genuine orphan -- its parent (the Studio that spawned it) is gone. This is
+ genuine orphan -- its parent (the Unsloth that spawned it) is gone. This is
the cross-session backstop the parent-death reaper (Job Object /
- PR_SET_PDEATHSIG) cannot cover: an orphan left by an already-dead Studio
+ PR_SET_PDEATHSIG) cannot cover: an orphan left by an already-dead Unsloth
(macOS, a best-effort failure, or a pre-existing orphan). Path-independent,
so it also catches an orphan the install-root match would miss.
@@ -8393,7 +8393,7 @@ class LlamaCppBackend:
"""Kill orphaned llama-server processes started by studio.
Only kills processes whose resolved binary lives under a known
- Studio install dir (or matches an exact env-var override), to avoid
+ Unsloth install dir (or matches an exact env-var override), to avoid
terminating unrelated llama-server instances. Mirrors every location
_find_llama_server_binary() can return, so orphans from any
supported install path are cleaned up.
@@ -8413,7 +8413,7 @@ class LlamaCppBackend:
try:
# -- Build the ownership allowlist --------------------------------
# exact_binaries -- env var overrides (exact path match).
- # install_roots -- Studio-owned dir trees (binary must be under one).
+ # install_roots -- Unsloth-owned dir trees (binary must be under one).
install_roots: list[Path] = []
# Env-mode custom root (mirrors _find_llama_server_binary).
@@ -8423,7 +8423,7 @@ class LlamaCppBackend:
install_roots.append(_resolved_sr / "llama.cpp")
# Primary install dir (default mode only). Env-mode skips this so a
- # custom-root Studio can't kill a default-install Studio's server.
+ # custom-root Unsloth can't kill a default-install Unsloth's server.
if not _is_custom_root:
install_roots.append(Path.home() / ".unsloth" / "llama.cpp")
@@ -8497,7 +8497,7 @@ class LlamaCppBackend:
if not is_ours:
continue
- # A live parent means a running Studio (or the user's
+ # A live parent means a running Unsloth (or the user's
# shell) still owns it -- not an orphan.
if LlamaCppBackend._pid_parent_is_alive(proc.info["pid"]):
continue
@@ -8577,7 +8577,7 @@ class LlamaCppBackend:
def _fit_off_retry_eligible(cmd: "list[str]", use_fit: bool) -> bool:
"""Whether a llama-server startup crash may be retried with --fit off.
- Only when Studio's own VRAM math placed the model (use_fit=False)
+ Only when Unsloth's own VRAM math placed the model (use_fit=False)
and nothing on the command line set the fit mode explicitly
(-fit / --fit, space- or equals-form). --fit-ctx / --fit-target /
-fitc / -fitt tune the fit step but do not select the mode, so
@@ -8821,7 +8821,7 @@ class LlamaCppBackend:
return None
def _reconcile_effective_ctx_with_server(self) -> None:
- """Adopt the server's real ``n_ctx`` when it is below Studio's value.
+ """Adopt the server's real ``n_ctx`` when it is below Unsloth's value.
Keeps ``context_length`` (load response, status route, passthrough
``max_tokens`` ceiling) honest; clients sized to the requested value
diff --git a/studio/backend/core/inference/llama_keepwarm.py b/studio/backend/core/inference/llama_keepwarm.py
index 4ce663c3ce..86a8c8a404 100644
--- a/studio/backend/core/inference/llama_keepwarm.py
+++ b/studio/backend/core/inference/llama_keepwarm.py
@@ -59,7 +59,7 @@ _INFERENCE_SUFFIXES = (
"/messages/count_tokens", # counts via the loaded tokenizer; protect like /messages
"/embeddings",
"/responses",
- "/generate/stream", # Studio's own streaming route on the same llama-server
+ "/generate/stream", # Unsloth's own streaming route on the same llama-server
"/audio/generate", # direct GGUF TTS; can outlive the idle TTL
)
diff --git a/studio/backend/core/inference/llama_server_args.py b/studio/backend/core/inference/llama_server_args.py
index f400d2ae40..70d0dc774d 100644
--- a/studio/backend/core/inference/llama_server_args.py
+++ b/studio/backend/core/inference/llama_server_args.py
@@ -3,10 +3,10 @@
"""Boundary validator for user-supplied llama-server pass-through args.
-Reject only flags Studio manages (model identity, auth, network, parallel
+Reject only flags Unsloth manages (model identity, auth, network, parallel
slots). Everything else (sampling, ``-c``, ``-ngl``, ``--flash-attn``,
``--cache-type-*``, ``--spec-*``, ``--jinja``, ...) is appended after
-Studio's auto-set flags so llama.cpp's last-wins parser lets the user override.
+Unsloth's auto-set flags so llama.cpp's last-wins parser lets the user override.
Ref: https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md
"""
@@ -22,12 +22,12 @@ _DENYLIST_GROUPS: tuple[frozenset[str], ...] = (
# Parallel slots: owned by typer --parallel; a pass-through would desync
# app.state.llama_parallel_slots from llama-server.
frozenset({"-np", "--parallel", "--n-parallel"}),
- # Model identity: Studio resolves it from LoadRequest; a second -m would
- # load a different model than Studio thinks it loaded.
+ # Model identity: Unsloth resolves it from LoadRequest; a second -m would
+ # load a different model than Unsloth thinks it loaded.
frozenset({"-m", "--model"}),
- # Public model id: Studio sets a sanitized --alias so the OpenAI API never
+ # Public model id: Unsloth sets a sanitized --alias so the OpenAI API never
# exposes the local .gguf path. A user-supplied alias is appended after
- # Studio's and, with llama.cpp's last-wins parsing, would reintroduce the
+ # Unsloth's and, with llama.cpp's last-wins parsing, would reintroduce the
# path leak this is meant to prevent.
frozenset({"-a", "--alias"}),
frozenset({"-mu", "--model-url"}),
@@ -39,14 +39,14 @@ _DENYLIST_GROUPS: tuple[frozenset[str], ...] = (
frozenset({"-hft", "--hf-token"}),
frozenset({"-mm", "--mmproj"}),
frozenset({"-mmu", "--mmproj-url"}),
- # Networking: Studio binds + proxies; retargeting orphans the proxy.
+ # Networking: Unsloth binds + proxies; retargeting orphans the proxy.
frozenset({"--host"}),
frozenset({"--port"}),
frozenset({"--path"}),
frozenset({"--api-prefix"}),
frozenset({"--reuse-port"}),
- # Auth / TLS: Studio terminates auth; upstream --api-key / TLS shadows
- # Studio's key and breaks the proxy hop.
+ # Auth / TLS: Unsloth terminates auth; upstream --api-key / TLS shadows
+ # Unsloth's key and breaks the proxy hop.
frozenset({"--api-key"}),
frozenset({"--api-key-file"}),
frozenset({"--ssl-key-file"}),
@@ -64,11 +64,11 @@ _DENYLIST_GROUPS: tuple[frozenset[str], ...] = (
frozenset({"--models-max"}),
frozenset({"--models-autoload", "--no-models-autoload"}),
# Server-mode flips: --embedding / --rerank restrict llama-server to
- # those endpoints, breaking Studio's /v1/chat/completions hop.
+ # those endpoints, breaking Unsloth's /v1/chat/completions hop.
frozenset({"--embedding", "--embeddings"}),
frozenset({"--rerank", "--reranking"}),
# llama-server's own built-in tools flag would silently stack on top of
- # Studio's --enable-tools / --disable-tools policy resolver.
+ # Unsloth's --enable-tools / --disable-tools policy resolver.
frozenset({"--tools"}),
)
@@ -120,7 +120,7 @@ def validate_extra_args(args: Optional[Iterable[str]]) -> list[str]:
def is_managed_flag(flag: str) -> bool:
- """True if ``flag`` is Studio-managed. Normalises via ``_flag_name`` so
+ """True if ``flag`` is Unsloth-managed. Normalises via ``_flag_name`` so
`-np8` / `--parallel=8` classify like the canonical tokens."""
normalised = _flag_name(flag)
return normalised is not None and normalised in _DENYLIST
@@ -142,7 +142,7 @@ _SPEC_FLAGS: frozenset[str] = frozenset(
"--draft-min",
"--draft-max",
# MTP path (llama.cpp #22673). The drafter selectors (local --model-draft
- # and HF --spec-draft-hf aliases) are Studio-managed since the separate-
+ # and HF --spec-draft-hf aliases) are Unsloth-managed since the separate-
# drafter support (Gemma 4): an inherited copy must not last-wins-override
# the auto-detected drafter. Explicit extras for the current load are never
# stripped. The per-drafter tuning knobs (--spec-draft-type-*, -ngld,
@@ -179,9 +179,9 @@ _TEMPLATE_FLAGS: frozenset[str] = frozenset(
# (--split-mode tensor). Pass-through stays allowed so users keep the
# row/none/layer modes the toggle doesn't expose, but it's stripped on
# inherit and reconciled into the round-tripped tensor_parallel state.
-# --tensor-split is coupled to the split mode and is stripped with it: Studio
+# --tensor-split is coupled to the split mode and is stripped with it: Unsloth
# owns the tensor-mode split ratios, so an inherited/stale --tensor-split must
-# not last-wins-override Studio's computed asymmetric split.
+# not last-wins-override Unsloth's computed asymmetric split.
_SPLIT_MODE_FLAGS: frozenset[str] = frozenset({"-sm", "--split-mode"})
_TENSOR_SPLIT_FLAGS: frozenset[str] = frozenset({"-ts", "--tensor-split"})
_SPLIT_SHADOWING_FLAGS: frozenset[str] = _SPLIT_MODE_FLAGS | _TENSOR_SPLIT_FLAGS
@@ -197,7 +197,7 @@ _BOOLEAN_SHADOWING_FLAGS: frozenset[str] = frozenset({"--spec-default", "--jinja
def parse_ctx_override(args: Optional[Iterable[str]]) -> Optional[int]:
"""Return the last user-supplied ``-c`` / ``--ctx-size`` value.
- Mirrors llama.cpp's last-wins parsing for the one numeric knob Studio's
+ Mirrors llama.cpp's last-wins parsing for the one numeric knob Unsloth's
load-time fit logic needs.
"""
if not args:
@@ -286,7 +286,7 @@ def parse_cache_override(args: Optional[Iterable[str]]) -> Optional[str]:
Mirrors parse_ctx_override but for cache type. Recognises both -ctk
(key) and -ctv (value). When both flags appear, returns the last-wins
value, treating key and value cache flags as the same setting because
- Studio's KV estimate has a single cache_type_kv knob.
+ Unsloth's KV estimate has a single cache_type_kv knob.
"""
return _last_flag_value(args, _CACHE_FLAGS)
@@ -341,7 +341,7 @@ def resolve_tensor_parallel(args: Optional[Iterable[str]], fallback_tensor_paral
def _env_split_mode_is_tensor(env: Optional[Mapping[str, str]] = None) -> bool:
- """True when the inherited LLAMA_ARG_SPLIT_MODE env selects tensor. Studio
+ """True when the inherited LLAMA_ARG_SPLIT_MODE env selects tensor. Unsloth
emits --split-mode only on its tensor branch, so a tensor env on the layer
path would run the child tensor-parallel unbudgeted; this flips the budget
to tensor. Only tensor is heavier, so other modes are ignored."""
@@ -425,7 +425,7 @@ def strip_shadowing_flags(
strip_template: bool = True,
strip_split_mode: bool = True,
) -> list[str]:
- """Strip flags that shadow first-class Studio settings.
+ """Strip flags that shadow first-class Unsloth settings.
Used when inheriting a previous load's ``llama_extra_args`` so an
inherited `-c 4096` can't override the current `max_seq_length`
diff --git a/studio/backend/core/inference/llama_stats.py b/studio/backend/core/inference/llama_stats.py
index 6047aedbc0..ab0d287e8c 100644
--- a/studio/backend/core/inference/llama_stats.py
+++ b/studio/backend/core/inference/llama_stats.py
@@ -5,7 +5,7 @@
engine-stats log line (generation/prompt throughput, requests in flight).
llama-server already computes these (it needs `--metrics`); this lifts them
-into Studio's structured log so the terminal shows serving health, not just
+into Unsloth's structured log so the terminal shows serving health, not just
per-request access lines. Emitted only while there is activity.
"""
diff --git a/studio/backend/core/inference/local_model_resolver.py b/studio/backend/core/inference/local_model_resolver.py
index 002cafe2c8..86ad8b9fd8 100644
--- a/studio/backend/core/inference/local_model_resolver.py
+++ b/studio/backend/core/inference/local_model_resolver.py
@@ -130,7 +130,7 @@ def info_has_local_gguf(info) -> bool:
def _build_index() -> dict[str, _LocalGgufEntry]:
"""Map normalized id/model_id/display_name -> local GGUF entry.
- Scans the same roots Studio's model picker lists (./models, the active plus
+ Scans the same roots Unsloth's model picker lists (./models, the active plus
legacy/default HF caches, LM Studio dirs, and user scan folders) so a named
local model is never missed and silently served as the loaded one. Ollama's
scanner is skipped: it creates symlinks as a side effect and this runs on the
@@ -199,7 +199,7 @@ def _build_index() -> dict[str, _LocalGgufEntry]:
raw_id = getattr(info, "id", None)
if not raw_id:
continue
- # Skip what Studio hides from its pickers (validation probe, RAG embed
+ # Skip what Unsloth hides from its pickers (validation probe, RAG embed
# weights): not chat models, so never an auto-switch target.
if _is_hidden_model(raw_id, getattr(info, "path", None)):
continue
diff --git a/studio/backend/core/inference/mcp_client.py b/studio/backend/core/inference/mcp_client.py
index 6b5ce02216..0256df944e 100644
--- a/studio/backend/core/inference/mcp_client.py
+++ b/studio/backend/core/inference/mcp_client.py
@@ -906,7 +906,7 @@ def _call_stdio_tool(
def _remaining() -> Optional[float]:
return None if deadline is None else max(0.0, deadline - time.monotonic())
- # Callers without a Studio session id must retain the former one-shot
+ # Callers without an Unsloth session id must retain the former one-shot
# behavior: no browser/cookie/tool state can leak into another request.
# Use an ephemeral key (and close it below) rather than the shared empty
# scope that the persistent-session cache used previously.
diff --git a/studio/backend/core/inference/passthrough_healing.py b/studio/backend/core/inference/passthrough_healing.py
index ed7c7ecfcf..e6da0a22b0 100644
--- a/studio/backend/core/inference/passthrough_healing.py
+++ b/studio/backend/core/inference/passthrough_healing.py
@@ -5,7 +5,7 @@
With server-side tools disabled (``unsloth run --disable-tools``, every
``unsloth start`` coding agent), requests carrying the client's own ``tools``
-bypass Studio's tool loop and are relayed to/from llama-server verbatim. Small
+bypass Unsloth's tool loop and are relayed to/from llama-server verbatim. Small
GGUF models often emit their tool calls as TEXT (``{...} ``,
Gemma ``<|tool_call>...``, ```` XML) instead of structured
``tool_calls`` -- on the passthrough that text reaches the agent as prose and
@@ -18,7 +18,7 @@ promotes calls whose function name exactly matches a declared tool. Promotion
removes EXACTLY the promoted calls' markup spans (the parser reports them):
undeclared calls, unparseable blocks, and suppressed alternate formats keep
every byte and relay as text, so healing can never silently delete model
-output. Responses without a tool signal, requests without tools, and Studio's
+output. Responses without a tool signal, requests without tools, and Unsloth's
own enable-tools loop are untouched. Per-request opt-out:
``auto_heal_tool_calls: false``. Process kill-switch:
``UNSLOTH_DISABLE_TOOL_CALL_HEALING=1``.
diff --git a/studio/backend/core/inference/pricing.py b/studio/backend/core/inference/pricing.py
index 3b611d3596..30fec47723 100644
--- a/studio/backend/core/inference/pricing.py
+++ b/studio/backend/core/inference/pricing.py
@@ -122,12 +122,12 @@ def calculate_cost(provider: str, model: str, usage: dict[str, Any]) -> dict[str
"priced": bool(prices),
}
- # Accept raw (input_tokens/output_tokens) and Studio chat-style
+ # Accept raw (input_tokens/output_tokens) and Unsloth chat-style
# (prompt_tokens/completion_tokens) envelopes. Cache buckets differ:
# raw Anthropic: input_tokens EXCLUDES cache buckets
# raw OpenAI: input_tokens INCLUDES cache_read
- # Studio Anthropic: prompt_tokens INCLUDES cache_creation + cache_read
- # Studio OpenAI: prompt_tokens == raw input_tokens
+ # Unsloth Anthropic: prompt_tokens INCLUDES cache_creation + cache_read
+ # Unsloth OpenAI: prompt_tokens == raw input_tokens
# Clamp >=0 so corrupted payloads can't produce a negative bill.
cache_creation = max(0, int(usage.get("cache_creation_input_tokens") or 0))
cache_read_native_present = (
@@ -160,7 +160,7 @@ def calculate_cost(provider: str, model: str, usage: dict[str, Any]) -> dict[str
output_tokens = max(0, int(usage.get("completion_tokens") or 0))
if provider == "openai":
# Cached tokens land on input_tokens_details (raw Responses) or
- # prompt_tokens_details (Studio chat-style).
+ # prompt_tokens_details (Unsloth chat-style).
for key in ("input_tokens_details", "prompt_tokens_details"):
details = usage.get(key) or {}
if isinstance(details, dict):
diff --git a/studio/backend/core/inference/safetensors_agentic.py b/studio/backend/core/inference/safetensors_agentic.py
index 9110315815..40731de57b 100644
--- a/studio/backend/core/inference/safetensors_agentic.py
+++ b/studio/backend/core/inference/safetensors_agentic.py
@@ -995,7 +995,7 @@ def run_safetensors_tool_loop(
if not safety_tc:
# Re-prompt once on plan-without-action, before any tool runs
# (GGUF loop parity). The retry is gated on nudge_tool_calls so
- # Studio callers (which send True) always nudge, while API callers
+ # Unsloth callers (which send True) always nudge, while API callers
# who omit the flag keep today's no-reprompt behavior (opt-in).
intent_text = _reprompt_intent_text(
content_accum,
diff --git a/studio/backend/core/inference/sandbox_site/sitecustomize.py b/studio/backend/core/inference/sandbox_site/sitecustomize.py
index d655e8e35a..244fa95145 100644
--- a/studio/backend/core/inference/sandbox_site/sitecustomize.py
+++ b/studio/backend/core/inference/sandbox_site/sitecustomize.py
@@ -4,7 +4,7 @@
"""Sandbox-side compatibility shim for ChatGPT code-interpreter paths.
Models habitually write to /mnt/data (or /mnt/outputs, /home/sandbox,
-/workspace), none of which exist in the Studio sandbox. This module sits on the
+/workspace), none of which exist in the Unsloth sandbox. This module sits on the
sandbox subprocess PYTHONPATH (see ``tools._build_safe_env``), so it loads at
interpreter startup in every sandboxed ``python`` run and any Python the
``terminal`` tool launches.
diff --git a/studio/backend/core/inference/tool_loop_controller.py b/studio/backend/core/inference/tool_loop_controller.py
index f7ed450d11..61643b5795 100644
--- a/studio/backend/core/inference/tool_loop_controller.py
+++ b/studio/backend/core/inference/tool_loop_controller.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Shared controller state for Studio local agentic tool loops.
+"""Shared controller state for Unsloth local agentic tool loops.
This module is intentionally dependency-light: it owns only per-response
ledger state and value objects used by the GGUF and safetensors loops.
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 5fd57e1b2c..bc9ffe85c2 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -2502,7 +2502,7 @@ def _build_safe_env(workdir: str) -> dict[str, str]:
shim directory.
"""
# Start from the running interpreter's dir so 'python'/'pip' resolve to the
- # same environment the Studio server runs in.
+ # same environment the Unsloth server runs in.
exe_dir = os.path.dirname(sys.executable)
path_entries = [exe_dir] if exe_dir else []
@@ -2792,7 +2792,7 @@ def _bypass_preexec():
"""Minimal pre-exec for bypass exec: os.setsid() only.
Required, not a restriction: _kill_process_tree does killpg(getpgid(child)),
- so without a new session a timeout/cancel would kill the Studio server too.
+ so without a new session a timeout/cancel would kill the Unsloth server too.
"""
try:
os.setsid()
@@ -2800,13 +2800,13 @@ def _bypass_preexec():
pass
-# Hardening the Studio parent is done once (PR_SET_DUMPABLE is process-global
+# Hardening the Unsloth parent is done once (PR_SET_DUMPABLE is process-global
# and sticky); guarded so repeated bypass calls do not re-issue the prctl.
_parent_proc_hardened = False
def _harden_parent_against_proc_env_leak() -> bool:
- """Make the Studio process's /proc//environ unreadable to its children.
+ """Make the Unsloth process's /proc//environ unreadable to its children.
Stripping the child env is not enough on Linux: a bypassed same-UID child
can read /proc//environ to recover the parent's unfiltered
@@ -5482,7 +5482,7 @@ def _truncate(text: str, limit: int = _MAX_OUTPUT_CHARS) -> str:
# ChatGPT code-interpreter path conventions models write out of habit; none
-# exist in the Studio sandbox, so a failure on one earns the retry hint.
+# exist in the Unsloth sandbox, so a failure on one earns the retry hint.
_MISSING_PATH_PREFIXES = (
"/mnt/data",
"/mnt/outputs",
@@ -5688,7 +5688,7 @@ def _python_exec(
# Close the /proc//environ secret-recovery path first; if it
# cannot be applied, fail closed rather than leak the parent environ.
return (
- "Execution error: could not harden the Studio process against "
+ "Execution error: could not harden the Unsloth process against "
"/proc environment reads; refusing bypass execution."
)
@@ -5833,7 +5833,7 @@ def _bash_exec(
# Close the /proc//environ secret-recovery path first; if it
# cannot be applied, fail closed rather than leak the parent environ.
return (
- "Execution error: could not harden the Studio process against "
+ "Execution error: could not harden the Unsloth process against "
"/proc environment reads; refusing bypass execution."
)
diff --git a/studio/backend/core/rag/captioner.py b/studio/backend/core/rag/captioner.py
index 6d1512a770..8398506f21 100644
--- a/studio/backend/core/rag/captioner.py
+++ b/studio/backend/core/rag/captioner.py
@@ -6,7 +6,7 @@
Both turn pixels into indexable text and are a no-op (never raise) without a loaded
vision model. They reuse the chat model's vision endpoint, so it must be served with
``--ubatch-size`` >= one image's tokens (some encoders, e.g. Gemma, attend
-non-causally and abort otherwise); Studio's vision chat already requires this."""
+non-causally and abort otherwise); Unsloth's vision chat already requires this."""
from __future__ import annotations
diff --git a/studio/backend/core/rag/embed_llama_server.py b/studio/backend/core/rag/embed_llama_server.py
index 46a282c939..b141e59422 100644
--- a/studio/backend/core/rag/embed_llama_server.py
+++ b/studio/backend/core/rag/embed_llama_server.py
@@ -10,7 +10,7 @@ Opt-in (``RAG_EMBED_BACKEND=llama-server``). Runs a dedicated
Device is ``auto`` (GPU when present, else CPU, falling back to CPU if a GPU start
fails); ``RAG_EMBED_DEVICE`` forces it. We call only llama_cpp's *static* helpers
(no torch), copying the instance-coupled bits locally, since constructing a
-``LlamaCppBackend`` runs an ``__init__`` reaper that kills any Studio llama-server
+``LlamaCppBackend`` runs an ``__init__`` reaper that kills any Unsloth llama-server
-- so each request re-spawns ours if it died (self-heal).
"""
diff --git a/studio/backend/core/rag/embeddings.py b/studio/backend/core/rag/embeddings.py
index b0ecedd593..15be7f1249 100644
--- a/studio/backend/core/rag/embeddings.py
+++ b/studio/backend/core/rag/embeddings.py
@@ -39,7 +39,7 @@ _model = None
_name: str | None = None
-# Studio device -> torch device string. Apple has no torch device -> CPU.
+# Unsloth device -> torch device string. Apple has no torch device -> CPU.
_TORCH_DEVICE = {DeviceType.CUDA: "cuda", DeviceType.XPU: "xpu"}
diff --git a/studio/backend/core/training/resume.py b/studio/backend/core/training/resume.py
index 2a4a198610..bbd9a895ab 100644
--- a/studio/backend/core/training/resume.py
+++ b/studio/backend/core/training/resume.py
@@ -53,7 +53,7 @@ def get_resume_checkpoint_path(path_value: str) -> Optional[str]:
def normalize_resume_output_dir(path_value: str) -> str:
path = resolve_output_dir(path_value)
if not _is_under_outputs(path):
- raise ValueError("Resume checkpoint must be inside Studio outputs.")
+ raise ValueError("Resume checkpoint must be inside Unsloth outputs.")
return str(path)
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.py
index 883a535a89..26720865f4 100644
--- a/studio/backend/core/training/trainer.py
+++ b/studio/backend/core/training/trainer.py
@@ -797,7 +797,7 @@ class UnslothTrainer:
)
logger.info("Loaded text model")
- raise_if_offloaded(self.model, device_map, "Studio training")
+ raise_if_offloaded(self.model, device_map, "Unsloth training")
if self.should_stop:
return False
diff --git a/studio/backend/core/training/training.py b/studio/backend/core/training/training.py
index 38f6b92f6d..b407ba39a5 100644
--- a/studio/backend/core/training/training.py
+++ b/studio/backend/core/training/training.py
@@ -140,7 +140,7 @@ def should_use_mlx_training_backend(*, device: Optional[Any] = None) -> bool:
def _build_training_worker_config(values: dict[str, Any]) -> dict[str, Any]:
- """Build the normalized worker config shared by Studio and the CLI adapter."""
+ """Build the normalized worker config shared by Unsloth and the CLI adapter."""
config = {
"model_name": values["model_name"],
"project_name": values.get("project_name"),
@@ -307,7 +307,7 @@ PLOT_HEIGHT = 3.5
@dataclass
class TrainingProgress:
- """Shared training progress payload for Studio and backend-aware trainers."""
+ """Shared training progress payload for Unsloth and backend-aware trainers."""
epoch: float = 0
step: int = 0
@@ -328,7 +328,7 @@ class TrainingProgress:
class _MLXTrainerAdapter:
- """Adapts the legacy UnslothTrainer API to the shared Studio MLX worker path."""
+ """Adapts the legacy UnslothTrainer API to the shared Unsloth MLX worker path."""
def __init__(self):
self.model = None
diff --git a/studio/backend/core/training/worker.py b/studio/backend/core/training/worker.py
index c52adbe8fa..111f4fdd0f 100644
--- a/studio/backend/core/training/worker.py
+++ b/studio/backend/core/training/worker.py
@@ -1100,7 +1100,7 @@ _MLX_VLM_RESIZED_IMAGE_LAYOUT_CACHE = {}
def _mlx_vlm_resized_image_layout(processor = None) -> str | None:
- """Return the numpy image layout expected after Studio-side VLM resizing."""
+ """Return the numpy image layout expected after Unsloth-side VLM resizing."""
image_processor = getattr(processor, "image_processor", None)
if image_processor is None:
return None
@@ -1257,7 +1257,7 @@ _MLX_STUDIO_LR_SCHEDULERS = {"linear", "cosine", "constant"}
# Fallback alias map mirroring unsloth_zoo._normalize_mlx_optimizer_name, used
-# only when mlx (Apple Silicon) is not importable so Studio config validation
+# only when mlx (Apple Silicon) is not importable so Unsloth config validation
# still works on non-MLX hosts. The zoo function stays the source of truth.
_MLX_STUDIO_ADAMW_ALIASES = frozenset(
(
@@ -1309,7 +1309,7 @@ def _normalize_mlx_studio_scheduler(value):
def _resolve_mlx_local_dataset_files(file_paths: list) -> list[str]:
- """Resolve CLI paths and Studio local dataset uploads without importing the GPU trainer."""
+ """Resolve CLI paths and Unsloth local dataset uploads without importing the GPU trainer."""
from utils.paths import resolve_dataset_path
all_files: list[str] = []
@@ -1912,7 +1912,7 @@ def _run_mlx_training(event_queue, stop_queue, config):
if "max_grad_leaf_norm" in _supported_fields:
mlx_config_kwargs["max_grad_leaf_norm"] = max_grad_leaf_norm
if "append_eos" in _supported_fields:
- # Studio SFT formatting owns rendered examples; raw/CPT text still
+ # Unsloth SFT formatting owns rendered examples; raw/CPT text still
# needs MLX to append EOS like the CUDA raw-text path.
mlx_config_kwargs["append_eos"] = bool(raw_text_mode)
@@ -2121,7 +2121,7 @@ def run_mlx_training_process(
config: dict,
transformers_activated: bool = False,
) -> None:
- """MLX worker entrypoint shared by Studio subprocesses and the CLI adapter."""
+ """MLX worker entrypoint shared by Unsloth subprocesses and the CLI adapter."""
model_name = config["model_name"]
backend_path = str(Path(__file__).resolve().parent.parent.parent)
@@ -2780,7 +2780,7 @@ def run_training_process(*, event_queue: Any, stop_queue: Any, config: dict) ->
)
# Unified Windows APUs: the WDDM budget is user-raisable, but
# nothing on the box says so -- users see "48 GB VRAM" on a
- # 96 GB machine and assume a Studio bug. Say where the limit
+ # 96 GB machine and assume an Unsloth bug. Say where the limit
# comes from and how to raise it.
if _is_unified and sys.platform == "win32":
try:
diff --git a/studio/backend/hub/services/download_lifecycle.py b/studio/backend/hub/services/download_lifecycle.py
index e5d48872c1..23f8c7c911 100644
--- a/studio/backend/hub/services/download_lifecycle.py
+++ b/studio/backend/hub/services/download_lifecycle.py
@@ -76,7 +76,7 @@ def spawn_worker(
env["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
env["HF_HUB_DISABLE_TELEMETRY"] = "1"
env["HF_HUB_DISABLE_XET"] = "0" if use_xet else "1"
- # No token in Studio settings: fall back to the backend's own HF_TOKEN so
+ # No token in Unsloth settings: fall back to the backend's own HF_TOKEN so
# private repos stay downloadable (needed while inkling repos are private).
if not hf_token:
hf_token = os.environ.get("HF_TOKEN") or None
diff --git a/studio/backend/hub/services/models/folder_browser.py b/studio/backend/hub/services/models/folder_browser.py
index d56b62c318..7d9c3ac665 100644
--- a/studio/backend/hub/services/models/folder_browser.py
+++ b/studio/backend/hub/services/models/folder_browser.py
@@ -165,7 +165,7 @@ def _looks_like_model_dir(directory: Path) -> bool:
def _build_browse_allowlist(
media_roots: Optional[list[Path]] = None, drive_roots: Optional[list[Path]] = None
) -> list[Path]:
- """Root directories the browser may walk (also seeds the suggestion chips): HOME, resolved HF cache dirs, Studio outputs/exports/root, registered scan folders, and well-known local-LLM dirs. Each is added only if it resolves to a real directory so the sandbox has no dead boundary.
+ """Root directories the browser may walk (also seeds the suggestion chips): HOME, resolved HF cache dirs, Unsloth outputs/exports/root, registered scan folders, and well-known local-LLM dirs. Each is added only if it resolves to a real directory so the sandbox has no dead boundary.
*media_roots* / *drive_roots* let the caller pass already-probed
removable-media and Windows drive roots so they aren't scanned again (a
diff --git a/studio/backend/hub/services/models/ollama.py b/studio/backend/hub/services/models/ollama.py
index 96a4114620..2ccdbb44f1 100644
--- a/studio/backend/hub/services/models/ollama.py
+++ b/studio/backend/hub/services/models/ollama.py
@@ -85,7 +85,7 @@ def _contained_link_path(link_dir: Path, link_name: str) -> Optional[Path]:
def _ollama_links_dir(ollama_dir: Path) -> Optional[Path]:
- """Writable directory for Ollama ``.gguf`` symlinks. Prefers ``/.studio_links/`` next to the blobs; falls back to Studio's cache (read-only system installs), then the temp dir (sandboxed installs)."""
+ """Writable directory for Ollama ``.gguf`` symlinks. Prefers ``/.studio_links/`` next to the blobs; falls back to Unsloth's cache (read-only system installs), then the temp dir (sandboxed installs)."""
def _ensure_writable_dir(path: Path) -> Optional[Path]:
try:
diff --git a/studio/backend/hub/utils/state_dir.py b/studio/backend/hub/utils/state_dir.py
index 183e934724..898c03c87d 100644
--- a/studio/backend/hub/utils/state_dir.py
+++ b/studio/backend/hub/utils/state_dir.py
@@ -3,7 +3,7 @@
"""Filesystem layout for Hub download state.
-State directory sits beside HF's cache (under Studio's own cache root)
+State directory sits beside HF's cache (under Unsloth's own cache root)
so it survives ``huggingface-cli delete-cache`` and any other HF-side
cache lifecycle. Two subdirectories:
diff --git a/studio/backend/main.py b/studio/backend/main.py
index 6e16dc00ca..4797764ce7 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -19,7 +19,7 @@ os.environ["PYTHONWARNINGS"] = "ignore"
# Pin GPU index ordering to PCI bus id before any torch import creates a CUDA
# context. Without this, torch/CUDA default to FASTEST_FIRST while nvidia-smi
-# (and Studio's VRAM probes) use PCI-bus order, so a GPU index chosen from
+# (and Unsloth's VRAM probes) use PCI-bus order, so a GPU index chosen from
# nvidia-smi data can resolve to a different physical card via
# CUDA_VISIBLE_DEVICES. setdefault so an explicit user override wins. See
# utils/hardware/hardware.py for the full rationale; set here too so the entry
@@ -93,7 +93,7 @@ if sys.platform == "win32":
# ── Windows AMD ROCm: make hipInfo.exe resolvable for subprocess probes ──
# bitsandbytes' get_rocm_gpu_arch() runs `hipinfo.exe` via PATH at import
# time; the AMD torch wheel ships it in the venv Scripts dir, which is on
- # PATH only when the venv is activated -- Studio launches python directly.
+ # PATH only when the venv is activated -- Unsloth launches python directly.
# Without this, every bitsandbytes import logs a scary (but harmless)
# "Could not detect ROCm GPU architecture: [WinError 2]" ERROR + WARNING.
# Gated on the file existing: only AMD ROCm wheels ship hipInfo.exe, so
@@ -252,7 +252,7 @@ def _read_studio_install_id() -> str:
Returns "" when absent or not a 64-char lowercase-hex token; then
/api/health emits "" and the launcher accepts any healthy backend.
- Carries no install-path info (matters when Studio runs -H 0.0.0.0)."""
+ Carries no install-path info (matters when Unsloth runs -H 0.0.0.0)."""
try:
token = (_STUDIO_ROOT_RESOLVED / "share" / "studio_install_id").read_text().strip()
except (OSError, ValueError):
@@ -573,7 +573,7 @@ async def lifespan(app: FastAPI):
print("DEFAULT ADMIN ACCOUNT CREATED")
print(f" username: {storage.DEFAULT_ADMIN_USERNAME}")
print(f" password saved to: {bootstrap_path}")
- print(" Open the Studio UI to sign in and change it.")
+ print(" Open the Unsloth UI to sign in and change it.")
print("=" * 60 + "\n")
else:
app.state.bootstrap_password = (
@@ -613,7 +613,7 @@ app = FastAPI(
)
# The MCP surface is opt-in because it can start GPU jobs and write model
-# artifacts. Mount it only when explicitly enabled by the Studio process.
+# artifacts. Mount it only when explicitly enabled by the Unsloth process.
if os.environ.get("UNSLOTH_STUDIO_ENABLE_MCP") == "1":
from fastmcp.utilities.lifespan import combine_lifespans
@@ -973,7 +973,7 @@ app.include_router(training_router, prefix = "/api/train", tags = ["training"])
app.include_router(models_router, prefix = "/api/models", tags = ["models"])
app.include_router(chat_history_router, prefix = "/api/chat", tags = ["chat"])
app.include_router(inference_router, prefix = "/api/inference", tags = ["inference"])
-# Studio-only inference endpoints (cancel, etc.) are NOT exposed on the /v1
+# Unsloth-only inference endpoints (cancel, etc.) are NOT exposed on the /v1
# OpenAI-compat prefix below.
app.include_router(inference_studio_router, prefix = "/api/inference", tags = ["inference"])
@@ -1080,7 +1080,7 @@ def studio_install_source(_current_subject: str = Depends(get_current_subject)):
@app.get("/api/studio/update-status")
def studio_update_status(_current_subject: str = Depends(get_current_subject)):
- """Return source-aware manual update status for browser-served Studio."""
+ """Return source-aware manual update status for browser-served Unsloth."""
return get_studio_update_status(UNSLOTH_VERSION)
diff --git a/studio/backend/mcp_server.py b/studio/backend/mcp_server.py
index f837f46425..e93490411d 100644
--- a/studio/backend/mcp_server.py
+++ b/studio/backend/mcp_server.py
@@ -3,7 +3,7 @@
"""Curated MCP tools for driving an Unsloth Studio instance.
-The MCP surface deliberately wraps the existing Studio services instead of
+The MCP surface deliberately wraps the existing Unsloth services instead of
duplicating training or export logic. It is opt-in because several tools can
start GPU work or write model artifacts.
"""
@@ -17,14 +17,14 @@ from fastmcp import FastMCP
class BearerTokenMiddleware:
- """Require an exact bearer token when Studio MCP is exposed remotely."""
+ """Require an exact bearer token when Unsloth MCP is exposed remotely."""
def __init__(self, app: Any, token: str) -> None:
if not token or not token.strip():
- raise ValueError("Studio MCP bearer token must be a non-empty value")
+ raise ValueError("Unsloth MCP bearer token must be a non-empty value")
if not token.isascii():
# A non-ASCII token cannot be sent in an HTTP header; reject it here.
- raise ValueError("Studio MCP bearer token must contain ASCII characters only")
+ raise ValueError("Unsloth MCP bearer token must contain ASCII characters only")
self.app = app
# Compare on raw header bytes: str hmac.compare_digest raises on non-ASCII
# input, which would surface as a 500 instead of a clean 401.
@@ -76,18 +76,18 @@ def _dump(value: Any) -> Any:
def _clamp(value: int, low: int, high: int) -> int:
"""Clamp an MCP-supplied integer into an inclusive range.
- MCP tools call the Studio route functions directly, which skips FastAPI's
+ MCP tools call the Unsloth route functions directly, which skips FastAPI's
Query(ge=, le=) validation, so we re-apply the same bounds here.
"""
return max(low, min(value, high))
def create_studio_mcp() -> FastMCP:
- """Create the Studio MCP server and register the high-value tools."""
+ """Create the Unsloth MCP server and register the high-value tools."""
mcp = FastMCP(
"Unsloth Studio",
instructions = (
- "Use read tools to inspect the local Studio state before starting GPU work. "
+ "Use read tools to inspect the local Unsloth state before starting GPU work. "
"Training and export tools can consume substantial VRAM and write files. "
"Never expose tokens or local paths from tool results unless the user asks."
),
@@ -116,7 +116,7 @@ def create_studio_mcp() -> FastMCP:
@mcp.tool
async def list_local_models(models_dir: str = "./models") -> dict[str, Any]:
- """List local and cached models available to Studio."""
+ """List local and cached models available to Unsloth."""
from routes.models import list_local_models as list_models
return _dump(await list_models(models_dir = models_dir, current_subject = "mcp"))
@@ -128,9 +128,9 @@ def create_studio_mcp() -> FastMCP:
@mcp.tool
async def start_training(config: dict[str, Any]) -> dict[str, Any]:
- """Start a validated Studio training job from a TrainingStartRequest-shaped object.
+ """Start a validated Unsloth training job from a TrainingStartRequest-shaped object.
- The config is validated by the same Pydantic model used by the Studio UI.
+ The config is validated by the same Pydantic model used by the Unsloth UI.
Call get_training_status first and do not start work while another job runs.
"""
from models import TrainingStartRequest
@@ -138,7 +138,7 @@ def create_studio_mcp() -> FastMCP:
request = TrainingStartRequest.model_validate(config)
# Pass via_api_key explicitly (a direct call leaves it a Depends object).
- # MCP drives Studio like the UI session, so it coexists and frees VRAM.
+ # MCP drives Unsloth like the UI session, so it coexists and frees VRAM.
return _dump(await start(request, current_subject = "mcp", via_api_key = False))
@mcp.tool
@@ -159,7 +159,7 @@ def create_studio_mcp() -> FastMCP:
@mcp.tool
def validate_recipe(recipe: dict[str, Any]) -> dict[str, Any]:
- """Validate a Data Recipe with the same validator used by Studio."""
+ """Validate a Data Recipe with the same validator used by Unsloth."""
from models.data_recipe import RecipePayload
from routes.data_recipe.validate import validate
@@ -225,7 +225,7 @@ def create_studio_mcp() -> FastMCP:
imatrix: bool = False,
imatrix_path: str | None = None,
) -> dict[str, Any]:
- """Export the loaded model to GGUF using Studio's existing path validation.
+ """Export the loaded model to GGUF using Unsloth's existing path validation.
quantization_method may be a single method or a list to produce several
GGUFs from one load. Pass hf_token when push_to_hub is set (the backend
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index 3ae974448e..f3ae0f70df 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -105,7 +105,7 @@ class LoadRequest(BaseModel):
description = (
"Extra arguments forwarded verbatim to llama-server for GGUF models. "
"One token per list entry, e.g. ['--top-k', '20', '--seed', '42']. "
- "Studio-managed flags (model identity, port, context length, GPU placement, "
+ "Unsloth-managed flags (model identity, port, context length, GPU placement, "
"auth, UI/server mode) are rejected. Ignored for non-GGUF models."
),
)
@@ -151,13 +151,13 @@ class TransformersUpgradeInfo(BaseModel):
)
supported_in_pypi: bool = Field(
False,
- description = "True if the latest PyPI release ships this model_type; Studio can "
+ description = "True if the latest PyPI release ships this model_type; Unsloth can "
"install it into a persistent sidecar after user consent.",
)
supported_in_main: bool = Field(
False,
description = "True if transformers GitHub main ships this model_type (dev-only; "
- "not installable through Studio yet).",
+ "not installable through Unsloth yet).",
)
@@ -533,7 +533,7 @@ class ImageContentPart(BaseModel):
class InputDocumentContentPart(BaseModel):
"""Document (PDF / file) content part in a multimodal message.
- Studio-normalised shape (file_data or file_url, plus optional filename/media_type).
+ Unsloth-normalised shape (file_data or file_url, plus optional filename/media_type).
Mapped onto Anthropic ``document`` / OpenAI ``input_file`` for vision providers;
dropped for non-vision providers.
"""
@@ -689,7 +689,7 @@ class ThinkingConfig(BaseModel):
"""Anthropic-compatible thinking/reasoning configuration.
Use type='disabled' to turn off thinking, or type='enabled' to turn it on.
Only type is read; extra fields (e.g. budget_tokens) are ignored, since
- Studio sets provider thinking budgets itself.
+ Unsloth sets provider thinking budgets itself.
"""
type: Literal["disabled", "enabled"] = "disabled"
@@ -748,7 +748,7 @@ class ChatCompletionRequest(BaseModel):
None,
description = (
"OpenAI function-tool definitions. When provided without `enable_tools=true`, "
- "Studio forwards the tools to the backend so the model returns structured "
+ "Unsloth forwards the tools to the backend so the model returns structured "
"tool_calls for the client to execute (standard OpenAI function calling)."
),
)
@@ -1160,7 +1160,7 @@ class ChatCompletionRequest(BaseModel):
and (self.enable_tools is True or bool(self.mcp_enabled))
):
# "Ask" gates every call, so a direct API caller that omits the legacy
- # confirm flag must still hit the confirmation gate for Studio's own
+ # confirm flag must still hit the confirmation gate for Unsloth's own
# tool loop. An explicit confirm_tool_calls=False wins over the mode
# (mirrors _permission_mode_confirm and the Anthropic pre-switch guard),
# so only self-enable when the flag is unset. Only self-enable when that
@@ -1168,7 +1168,7 @@ class ChatCompletionRequest(BaseModel):
# (enable_tools / mcp_enabled) -- the router enters the loop on those
# signals, not on enabled_tools alone (which merely filters which tools
# run). A plain client-tool passthrough (client-supplied `tools` that
- # Studio does not execute) must route verbatim, and external-provider
+ # Unsloth does not execute) must route verbatim, and external-provider
# routing rejects confirm_tool_calls with tools, so skip the fold there.
#
# "auto" is deliberately NOT folded: it only prompts for a call the
diff --git a/studio/backend/models/training.py b/studio/backend/models/training.py
index ff815a2fa9..0b50f63b95 100644
--- a/studio/backend/models/training.py
+++ b/studio/backend/models/training.py
@@ -446,7 +446,7 @@ class TrainingStartRequest(BaseModel):
random_seed: int = Field(
3407,
description = (
- "Random seed; matches the Studio backend / MLX worker default "
+ "Random seed; matches the Unsloth backend / MLX worker default "
"and unsloth's historical recommended value."
),
)
diff --git a/studio/backend/plugins/data-designer-github-repo-seed/README.md b/studio/backend/plugins/data-designer-github-repo-seed/README.md
index 346d94b305..44519496f5 100644
--- a/studio/backend/plugins/data-designer-github-repo-seed/README.md
+++ b/studio/backend/plugins/data-designer-github-repo-seed/README.md
@@ -4,7 +4,7 @@ A Data Designer seed-reader plugin for **Unsloth Studio** that scrapes real
GitHub data (issues, pull requests, commits) from one or more repositories
and hands it to the recipe pipeline as a seed dataset.
-Designed to ship with Studio as a default seed source so any user with a
+Designed to ship with Unsloth as a default seed source so any user with a
GitHub token can build training datasets straight from live repos.
## What it does
@@ -64,7 +64,7 @@ sleeps until reset when the budget drops below a safety threshold.
## Install
-Shipped as a default Studio plugin. For development:
+Shipped as a default Unsloth plugin. For development:
```bash
pip install -e .
diff --git a/studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/__init__.py b/studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/__init__.py
index 62ecb2e280..d4d46da370 100644
--- a/studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/__init__.py
+++ b/studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/__init__.py
@@ -3,4 +3,4 @@
# Intentionally empty. Data-designer loads submodules lazily via qualified names
# in plugin.py, so importing this package must not touch data_designer.engine.*
-# during Studio bootstrap (circular import).
+# during Unsloth bootstrap (circular import).
diff --git a/studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/scraper.py b/studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/scraper.py
index 637193e8b3..1af8133cc5 100644
--- a/studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/scraper.py
+++ b/studio/backend/plugins/data-designer-github-repo-seed/src/data_designer_github_repo_seed/scraper.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Multi-repo GitHub scraper for the Studio seed plugin.
+"""Multi-repo GitHub scraper for the Unsloth seed plugin.
Drives the GraphQL scraper in `scraper_impl/` per repo, capped via trial_limits
to stop at `limit` items per resource. Then reads the per-resource JSONL shards
diff --git a/studio/backend/requirements/extras-no-deps.txt b/studio/backend/requirements/extras-no-deps.txt
index 5830a47789..3361af50dd 100644
--- a/studio/backend/requirements/extras-no-deps.txt
+++ b/studio/backend/requirements/extras-no-deps.txt
@@ -5,7 +5,7 @@ julius
torchcodec==0.10.0
snac
-# peft 0.19.0 causes export subprocess shutdown issues in Studio;
+# peft 0.19.0 causes export subprocess shutdown issues in Unsloth;
# installing with --no-deps to avoid pulling in torch>=0.11.0
peft==0.18.1
diff --git a/studio/backend/requirements/no-torch-runtime.txt b/studio/backend/requirements/no-torch-runtime.txt
index de321f80ed..378fb33a60 100644
--- a/studio/backend/requirements/no-torch-runtime.txt
+++ b/studio/backend/requirements/no-torch-runtime.txt
@@ -70,7 +70,7 @@ cut_cross_entropy
pillow
# RAG store + document parsing, mirroring studio.txt. Pinned here because
-# this file installs --no-deps; without them Studio runs with RAG disabled.
+# this file installs --no-deps; without them Unsloth runs with RAG disabled.
sqlite-vec==0.1.9
pymupdf==1.27.2.3
# 0.3.x keeps pymupdf-layout (which pulls onnxruntime) an optional extra; the
diff --git a/studio/backend/requirements/single-env/constraints.txt b/studio/backend/requirements/single-env/constraints.txt
index 0ed2bf8b26..0a5619924a 100644
--- a/studio/backend/requirements/single-env/constraints.txt
+++ b/studio/backend/requirements/single-env/constraints.txt
@@ -4,7 +4,7 @@ transformers==4.57.6
trl==0.23.1
huggingface-hub==0.36.2
-# Studio stack
+# Unsloth stack
datasets==4.3.0
pyarrow==23.0.1
diff --git a/studio/backend/requirements/studio.txt b/studio/backend/requirements/studio.txt
index 6f4a5c3292..0c7503a5ca 100644
--- a/studio/backend/requirements/studio.txt
+++ b/studio/backend/requirements/studio.txt
@@ -1,4 +1,4 @@
-# Studio UI backend dependencies
+# Unsloth UI backend dependencies
typer
fastapi
uvicorn
@@ -9,7 +9,7 @@ pandas
nest_asyncio
datasets==4.3.0
pyjwt
-# gradio>=4.0.0 # 148 MB - Studio uses React + FastAPI, not Gradio
+# gradio>=4.0.0 # 148 MB - Unsloth uses React + FastAPI, not Gradio
huggingface-hub==0.36.2
structlog>=24.1.0
diceware
diff --git a/studio/backend/routes/auth.py b/studio/backend/routes/auth.py
index c61c1a16e4..d779c8784e 100644
--- a/studio/backend/routes/auth.py
+++ b/studio/backend/routes/auth.py
@@ -338,11 +338,11 @@ def _clear_login_bucket(key: tuple[str, str]) -> None:
# so FastAPI runs it in the threadpool rather than blocking the event loop.
@router.get("/identity")
def identity(nonce: str, request: Request) -> dict:
- """Challenge-response proof this is the real local Studio: caller sends a nonce,
+ """Challenge-response proof this is the real local Unsloth: caller sends a nonce,
gets HMAC(install identity secret, nonce, connection address + port).
Unauthenticated and side-effect free; a process that can't read the same-user
secret can't forge a proof, and binding to the address/port the connection
- landed on stops a squatter relaying a proof from the real Studio elsewhere."""
+ landed on stops a squatter relaying a proof from the real Unsloth elsewhere."""
try:
raw = base64.urlsafe_b64decode(nonce)
except Exception:
diff --git a/studio/backend/routes/data_recipe/jobs.py b/studio/backend/routes/data_recipe/jobs.py
index 59714380da..e870e8855e 100644
--- a/studio/backend/routes/data_recipe/jobs.py
+++ b/studio/backend/routes/data_recipe/jobs.py
@@ -37,7 +37,7 @@ def _resolve_local_v1_endpoint(request: Request) -> str:
Resolution order:
1. ``app.state.server_port`` (run.py, post-bind) - survives proxies/tunnels.
- 2. ``request.scope["server"]`` - when Studio starts outside ``run_server``.
+ 2. ``request.scope["server"]`` - when Unsloth starts outside ``run_server``.
3. parsed ``request.base_url`` - last resort for test fixtures.
"""
port: Any = getattr(request.app.state, "server_port", None)
diff --git a/studio/backend/routes/datasets.py b/studio/backend/routes/datasets.py
index 46319ca2ba..5456080f34 100644
--- a/studio/backend/routes/datasets.py
+++ b/studio/backend/routes/datasets.py
@@ -485,7 +485,7 @@ async def upload_dataset(
# Stream to disk in chunks to avoid holding the whole file in memory. The
# route-level cap gives a clear training-dataset error and avoids leaving
- # oversized partial files in the Studio uploads directory.
+ # oversized partial files in the Unsloth uploads directory.
upload_limit_bytes = get_upload_limit_bytes()
total_bytes = 0
upload_complete = False
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 9299e26d56..3d527bf317 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -92,7 +92,7 @@ def _mlx_distributed_launch_detected() -> bool:
def _install_httpcore_asyncgen_silencer() -> None:
"""Silence benign httpx/httpcore asyncgen GC noise on Python 3.13.
- When Studio proxies a llama-server stream via httpx, the innermost
+ When Unsloth proxies a llama-server stream via httpx, the innermost
``HTTP11ConnectionByteStream.__aiter__`` async generator is finalised by
the asyncgen GC hook on a task different from the one that opened it. Its
``aclose`` calls ``anyio.Lock.acquire`` → ``cancel_shielded_checkpoint``,
@@ -229,14 +229,14 @@ def _friendly_upstream_error(text: str) -> str:
parse grammar" / "failed to initialize samplers"). This surfaces to coding agents as
a hard 400 on every tool-bearing turn. It is a llama-server limitation with some
model/quant + tool-schema combinations, and recent llama.cpp builds handle the common
- coding-agent tools, so point the user at updating Studio rather than the raw body.
+ coding-agent tools, so point the user at updating Unsloth rather than the raw body.
"""
lowered = text.lower()
if "failed to parse grammar" in lowered or "failed to initialize samplers" in lowered:
return (
"The model couldn't compile a tool-calling grammar for this request. This is a "
"llama-server limitation with some model/quant and tool-schema combinations. "
- "Update Studio (it installs the latest llama.cpp, which handles the common "
+ "Update Unsloth (it installs the latest llama.cpp, which handles the common "
"coding-agent tools) or try a different GGUF model."
)
return f"llama-server error: {text}"
@@ -731,7 +731,7 @@ def _openai_passthrough_sse_line_terminal_state(raw_line: str) -> Optional[str]:
Some llama-server builds can emit the logical final chunk (``finish_reason``)
and optional usage chunk, then keep the HTTP stream open without sending the
- OpenAI ``data: [DONE]`` sentinel. Classifying those chunks lets Studio close
+ OpenAI ``data: [DONE]`` sentinel. Classifying those chunks lets Unsloth close
the client stream promptly while preserving an optional trailing usage chunk.
"""
if not raw_line.startswith("data:"):
@@ -1786,7 +1786,7 @@ import numpy as np
from datetime import date as _date
router = APIRouter()
-# Studio-only router (not mounted on /v1 OpenAI-compat).
+# Unsloth-only router (not mounted on /v1 OpenAI-compat).
studio_router = APIRouter()
@@ -2108,9 +2108,9 @@ def _effective_enable_tools(payload) -> Optional[bool]:
def _explicit_studio_tool_loop_requested(payload) -> bool:
- """True when the request itself asks Studio to execute local tools.
+ """True when the request itself asks Unsloth to execute local tools.
- Process-wide CLI policy can default Studio's tool loop on for ordinary chat,
+ Process-wide CLI policy can default Unsloth's tool loop on for ordinary chat,
but it must not steal OpenAI-compatible client tools or response_format
requests from the llama-server passthrough path. A policy of ``False``
(--disable-tools) vetoes even an explicit ``enable_tools: true`` ask.
@@ -2122,7 +2122,7 @@ def _explicit_studio_tool_loop_requested(payload) -> bool:
def _permission_mode_confirm(payload) -> bool:
- """Effective confirm-gate intent for Studio's own local tool loop.
+ """Effective confirm-gate intent for Unsloth's own local tool loop.
Honors the documented default that an unset permission_mode behaves as
"ask". An explicit confirm_tool_calls (True or False) wins; explicit
@@ -2144,7 +2144,7 @@ def _permission_mode_confirm(payload) -> bool:
def _confirm_gate_needs_stream(payload) -> bool:
- """Whether Studio's local tool-loop confirm gate still requires stream=true.
+ """Whether Unsloth's local tool-loop confirm gate still requires stream=true.
The gate can only prompt while streaming, so a non-streaming request that will
prompt must 400 up front. auto ("Approve for me") only prompts for a call the
@@ -3143,7 +3143,7 @@ def _is_explicit_tensor_drop(request: LoadRequest) -> bool:
"""True only when the request explicitly selects a non-tensor --split-mode (e.g.
layer/row/none), a deliberate departure from a preserved tensor->layer fallback.
- A bare tensor_parallel field is NOT a drop: the Studio UI always sends it and echoes
+ A bare tensor_parallel field is NOT a drop: the Unsloth UI always sends it and echoes
the /load response's resolved value back, so after a fallback every reload carries
tensor_parallel=false even though the user never changed it -- treating that as a drop
would collapse the preserved multi-GPU placement on the next ctx/settings reload. An
@@ -4197,8 +4197,8 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
raise HTTPException(
status_code = 400,
detail = (
- "Studio does not support distributed MLX inference under "
- "mlx.launch. Use `mlx.launch ... unsloth chat` or run Studio "
+ "Unsloth does not support distributed MLX inference under "
+ "mlx.launch. Use `mlx.launch ... unsloth chat` or run Unsloth "
"without the distributed launcher."
),
)
@@ -4288,7 +4288,7 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
# omits chat_template_override, so strip the inherited
# --chat-template-file in that case too -- otherwise the stale
# extra arg (appended last) shadows the bundled template while
- # Studio reports the bundled template's capabilities.
+ # Unsloth reports the bundled template's capabilities.
fields_set = getattr(request, "model_fields_set", set())
stripped = strip_shadowing_flags(
llama_backend.extra_args,
@@ -4727,7 +4727,7 @@ def _requires_trust_remote_code_for_model(
model_identifier: str, hf_token: Optional[str] = None
) -> bool:
"""Whether loading this model would execute custom repo code, so the consent
- dialog must run first. True if the Studio YAML default enables
+ dialog must run first. True if the Unsloth YAML default enables
``trust_remote_code`` OR the raw config declares an ``auto_map`` (Hub/local,
config.json or tokenizer_config.json). Reads raw JSON only; never imports
model code."""
@@ -5360,7 +5360,7 @@ async def confirm_tool_call(
@studio_router.get("/monitor")
async def get_api_monitor(current_subject: str = Depends(get_current_subject)):
- """Return recent OpenAI-compatible API activity for Studio."""
+ """Return recent OpenAI-compatible API activity for Unsloth."""
active_model = _monitor_active_model()
active_requests = api_monitor.active_count(subject = current_subject)
if active_requests:
@@ -5548,7 +5548,7 @@ async def get_status(current_subject: str = Depends(get_current_subject)):
_display_model_id = os.path.basename(_model_id)
_inference_cfg = load_inference_config(_model_id) if _model_id else None
_audio_type = getattr(llama_backend, "_audio_type", None)
- # Don't surface Studio's auto-applied bundled family template (e.g. the
+ # Don't surface Unsloth's auto-applied bundled family template (e.g. the
# gemma-4 override) as a user-authored override: the frontend adopts
# status.chat_template_override as editable state and would otherwise
# re-send it as an explicit override for a later, unrelated model. Only
@@ -6173,7 +6173,7 @@ def _build_external_messages(
metadata; strip it for providers that can't parse the unknown key.
2. Marked server-side builtin cards (`_server_tool: true` on a
canonical builtin name, or a Gemini `native_part` payload) are
- Studio-internal tool cards from a prior native Gemini turn;
+ Unsloth-internal tool cards from a prior native Gemini turn;
forwarding them to OpenAI / Anthropic / custom OAI-compat gateways
sends an orphan `tool_calls` entry (no matching tool declaration,
often no matching `role="tool"` reply) that can be rejected. We
@@ -6859,7 +6859,7 @@ async def openai_chat_completions(
# is invalid and must not evict the resident model first.
#
# Enter the local-loop arm exactly when the passthrough router below would
- # run Studio's own tool loop. That gate is `_tools_on or _mcp_allowed`
+ # run Unsloth's own tool loop. That gate is `_tools_on or _mcp_allowed`
# (see the use_tools block): _effective_enable_tools (which lets a
# process-wide --enable-tools policy force the loop on) plus mcp_enabled
# honoring --disable-tools, and tool_choice="none" disabling it unless the
@@ -6884,7 +6884,7 @@ async def openai_chat_completions(
or bool(payload.openai_code_exec_container_id)
or bool(payload.anthropic_code_exec_container_id)
# A JSON-schema response_format is guided-decoding structured output the
- # router forwards to the llama-server passthrough, not Studio's tool
+ # router forwards to the llama-server passthrough, not Unsloth's tool
# loop, so a --enable-tools policy must not 400 it as a local-confirm
# request under ask/auto.
or bool(_extract_response_format(payload))
@@ -6961,7 +6961,7 @@ async def openai_chat_completions(
using_gguf = llama_backend.is_loaded
# OpenAI-SDK clients send ``chat_template_kwargs`` via ``extra_body``, which
- # the SDK spreads into the request body at the top level. Studio's
+ # the SDK spreads into the request body at the top level. Unsloth's
# ChatCompletionRequest has ``extra="allow"`` so pydantic stashes them in
# ``model_extra``, but downstream generators consume the typed
# ``payload.enable_thinking``. Lift ``enable_thinking`` from the extra-body
@@ -7215,7 +7215,7 @@ async def openai_chat_completions(
# ── Standard OpenAI function-calling pass-through (GGUF only) ────
# When a client (opencode / Claude Code via OpenAI compat / Cursor /
- # Continue / ...) sends standard OpenAI `tools` without Studio's
+ # Continue / ...) sends standard OpenAI `tools` without Unsloth's
# `enable_tools` shorthand, forward the request to llama-server
# verbatim so structured `tool_calls` flow back to the client. This
# branch runs BEFORE `_extract_content_parts` because that helper is
@@ -7238,7 +7238,7 @@ async def openai_chat_completions(
_has_tool_catalog = bool(payload.tools and len(payload.tools) > 0)
_has_active_tool_catalog = _has_tool_catalog and payload.tool_choice != "none"
_has_client_tool_contract = _has_active_tool_catalog or _has_tool_messages
- # The Studio tool loop needs a tool-capable backend, so a request that asks
+ # The Unsloth tool loop needs a tool-capable backend, so a request that asks
# for it on a backend that can't run it (DiffusionGemma forces supports_tools
# off) must not steal client tools from the passthrough (#6851).
_studio_tool_loop_requested = (
@@ -7434,7 +7434,7 @@ async def openai_chat_completions(
use_tools = False
if use_tools:
- # permission_mode ask/auto require the confirm gate for Studio's own
+ # permission_mode ask/auto require the confirm gate for Unsloth's own
# tool loop. The request validator self-enables confirm only for
# request-level tool signals (enable_tools/enabled_tools/mcp_enabled);
# when a CLI policy (--enable-tools) forces the loop on without those,
@@ -8711,7 +8711,7 @@ async def openai_chat_completions(
_sf_model_info = backend.models.get(backend.active_model_name, {})
_sf_tpl = (_sf_model_info.get("chat_template_info") or {}).get("template")
# Named templates may expose native reasoning only in their ``tool_use``
- # branch. Use a truthy placeholder for Studio-managed tools, whose concrete
+ # branch. Use a truthy placeholder for Unsloth-managed tools, whose concrete
# schemas are selected below, and the request schemas for client passthrough.
_sf_server_tool_intent = bool(
_effective_enable_tools(payload) or _explicit_studio_tool_loop_requested(payload)
@@ -8790,7 +8790,7 @@ async def openai_chat_completions(
_sf_use_tools = False
if _sf_use_tools:
- # permission_mode ask/auto require the confirm gate for Studio's own tool
+ # permission_mode ask/auto require the confirm gate for Unsloth's own tool
# loop; when a CLI policy (--enable-tools) forces the loop on without a
# request-level tool signal, derive confirm here so the mode still gates
# the call (matching the GGUF path). off/full never prompt.
@@ -12050,7 +12050,7 @@ def _anthropic_requested_studio_tools(tools: Optional[list]) -> set[str]:
def _select_anthropic_server_tools(
all_tools: list[dict], requested_studio_tools: set[str], enabled_tools: Optional[list[str]]
) -> list[dict]:
- """Select Studio tools requested through Anthropic tools and extensions."""
+ """Select Unsloth tools requested through Anthropic tools and extensions."""
if not requested_studio_tools and enabled_tools is None:
return all_tools
@@ -12289,7 +12289,7 @@ async def anthropic_messages(
),
)
- # Reject an unsupported confirm-gated permission mode for Studio's own
+ # Reject an unsupported confirm-gated permission mode for Unsloth's own
# ("server") Anthropic tools before the switch, mirroring the malformed- and
# mixed-tool checks above. ask always wants a per-call pause this passthrough
# cannot offer, so it 400s whenever server tools are selected. auto only needs
@@ -12778,11 +12778,11 @@ async def _anthropic_tool_stream(
ends_on_tool_use = True
elif etype == "tool_end":
tool_blocks_emitted += 1
- # A tool_end means Studio executed the tool server-side, so
+ # A tool_end means Unsloth executed the tool server-side, so
# the response no longer ends on a pending client action.
# Without this, a server tool that produces no trailing text
# would be mislabeled stop_reason "tool_use", telling the
- # client to run a tool Studio already ran.
+ # client to run a tool Unsloth already ran.
ends_on_tool_use = False
elif etype == "content" and event.get("text"):
ends_on_tool_use = False
@@ -13698,7 +13698,7 @@ def _openai_messages_for_passthrough(payload) -> list[dict]:
structured ``tool_calls``. Content-parts images already in the list are
left untouched.
- When a client uses Studio's legacy ``image_base64`` top-level field, the
+ When a client uses Unsloth's legacy ``image_base64`` top-level field, the
image is re-encoded to PNG (llama-server's stb_image has limited format
support) and spliced into the last user message as an OpenAI ``image_url``
content part so vision + function-calling requests work transparently.
@@ -13832,7 +13832,7 @@ def _build_openai_passthrough_body(
) -> dict:
"""Assemble the llama-server request body from a ChatCompletionRequest.
- Only known OpenAI / llama-server fields are forwarded, so Studio-specific
+ Only known OpenAI / llama-server fields are forwarded, so Unsloth-specific
extensions (``enable_tools``, ``enabled_tools``, ``session_id``, ...) never
leak to the backend.
"""
@@ -14082,7 +14082,7 @@ async def _openai_passthrough_stream_admitted(
admission_lease: LlamaAdmissionLease,
tracker,
):
- """Streaming client-side pass-through after Studio granted an upstream slot.
+ """Streaming client-side pass-through after Unsloth granted an upstream slot.
Forwards the client's OpenAI function-calling request to llama-server and
relays the SSE stream back with minimal normalization (reasoning-only
diff --git a/studio/backend/routes/mcp_servers.py b/studio/backend/routes/mcp_servers.py
index 71f0fd2874..dc018d163a 100644
--- a/studio/backend/routes/mcp_servers.py
+++ b/studio/backend/routes/mcp_servers.py
@@ -82,7 +82,7 @@ def _validate_url(url: str) -> str:
if _looks_like_command(trimmed):
detail = (
"Local commands aren't enabled on this server. To allow them, "
- "set UNSLOTH_STUDIO_ALLOW_STDIO_MCP=1 and restart Studio, or use "
+ "set UNSLOTH_STUDIO_ALLOW_STDIO_MCP=1 and restart Unsloth, or use "
"an http:// or https:// URL instead."
)
else:
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index b8526c75e7..742ecde3ba 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -544,7 +544,7 @@ def _ollama_links_dir(ollama_dir: Path) -> Optional[Path]:
"""Return a writable directory for Ollama ``.gguf`` symlinks.
Prefers ``/.studio_links/`` so links sit next to their
- blobs; falls back to a per-ollama-dir namespace under Studio's cache
+ blobs; falls back to a per-ollama-dir namespace under Unsloth's cache
when the models dir is read-only (common for system installs).
"""
from utils.paths.storage_roots import cache_root
@@ -555,7 +555,7 @@ def _ollama_links_dir(ollama_dir: Path) -> Optional[Path]:
return primary
except OSError as e:
logger.debug(
- "Ollama dir %s not writable for .studio_links (%s); falling back to Studio cache",
+ "Ollama dir %s not writable for .studio_links (%s); falling back to Unsloth cache",
ollama_dir,
e,
)
@@ -594,7 +594,7 @@ def _scan_ollama_dir(ollama_dir: Path, limit: Optional[int] = None) -> List[Loca
model, keyed by a short hash of the manifest path, so
``detect_mmproj_file`` only sees that model's projector). Links are
symlinks when possible, else hardlinks; the link dir is
- ``.studio_links/`` when writable, else Studio's cache.
+ ``.studio_links/`` when writable, else Unsloth's cache.
"""
manifests_root = ollama_dir / "manifests"
if not manifests_root.is_dir():
@@ -1194,7 +1194,7 @@ def _build_browse_allowlist(
"""Return the root directories the folder browser may walk.
The same list seeds the sidebar suggestion chips, so chip targets are
- always reachable. Roots: HOME, resolved HF cache dirs, Studio's
+ always reachable. Roots: HOME, resolved HF cache dirs, Unsloth's
outputs/exports/studio root, registered scan folders, and well-known
local-LLM dirs (LM Studio, Ollama, ``~/models``); each added only if
it resolves to a real directory.
@@ -1486,7 +1486,7 @@ def browse_folders(
"Directory to list. If omitted, defaults to the current user's "
"home directory. Tilde (`~`) and relative paths are expanded. "
"Must resolve inside the allowlist of browseable roots (HOME, "
- "HF cache, Studio dirs, registered scan folders, well-known "
+ "HF cache, Unsloth dirs, registered scan folders, well-known "
"model dirs)."
),
),
@@ -2251,15 +2251,15 @@ async def delete_finetuned_model(
gguf_variant: Optional[str] = Body(None),
current_subject: str = Depends(get_current_subject),
):
- """Delete a Studio-trained or exported model from disk.
+ """Delete an Unsloth-trained or exported model from disk.
- Only paths under Studio's outputs/exports roots are accepted.
+ Only paths under Unsloth's outputs/exports roots are accepted.
Exported GGUF entries can delete one quant variant at a time.
"""
if source not in {"training", "exported"}:
raise HTTPException(
status_code = 400,
- detail = "Only trained or exported Studio models can be deleted",
+ detail = "Only trained or exported Unsloth models can be deleted",
)
if not model_path or not model_path.strip():
@@ -2291,14 +2291,14 @@ async def delete_finetuned_model(
if not _is_path_under_lexically(delete_path, allowed_root):
raise HTTPException(
status_code = 400,
- detail = "Model path is outside Studio storage",
+ detail = "Model path is outside Unsloth storage",
)
if export_type == "gguf" and gguf_variant:
target_path = delete_path.resolve()
if not _is_path_under(target_path, allowed_root):
raise HTTPException(
status_code = 400,
- detail = "Model path is outside Studio storage",
+ detail = "Model path is outside Unsloth storage",
)
else:
target_path = delete_path
@@ -2311,7 +2311,7 @@ async def delete_finetuned_model(
if should_check_resolved_path and not _is_path_under(target_path, allowed_root):
raise HTTPException(
status_code = 400,
- detail = "Model path is outside Studio storage",
+ detail = "Model path is outside Unsloth storage",
)
if target_path == allowed_root:
raise HTTPException(
@@ -3456,7 +3456,7 @@ _EXPORT_SIZE_CACHE: dict[str, tuple[int, int, str]] = {}
def _is_sizable_local_path(model: str) -> bool:
- """True only for local paths under a Studio data root.
+ """True only for local paths under an Unsloth data root.
Containment is decided lexically (no filesystem access) before the path is
touched, then the path is symlink-resolved and re-checked so a symlink
diff --git a/studio/backend/routes/training.py b/studio/backend/routes/training.py
index d53e8f2bbc..53b1c4d991 100644
--- a/studio/backend/routes/training.py
+++ b/studio/backend/routes/training.py
@@ -127,9 +127,9 @@ async def start_training(
try:
logger.info(f"Starting training job with model: {request.model_name}")
- # When Studio is driven as an inference API (API-key auth), refuse to start
+ # When Unsloth is driven as an inference API (API-key auth), refuse to start
# training while a request is in flight: training frees VRAM by unloading
- # the chat model, which would kill the stream. The Studio UI (session auth)
+ # the chat model, which would kill the stream. The Unsloth UI (session auth)
# still starts training and coexists/frees VRAM as before. (A mixed UI+API
# session is not yet special-cased.)
if via_api_key is True:
@@ -139,7 +139,7 @@ async def start_training(
status_code = 409,
detail = (
"Cannot start training over the API while an inference request is in "
- "progress. Wait for it to finish, or start training from the Studio UI."
+ "progress. Wait for it to finish, or start training from the Unsloth UI."
),
)
diff --git a/studio/backend/run.py b/studio/backend/run.py
index 4f105b53b1..398943cc2c 100644
--- a/studio/backend/run.py
+++ b/studio/backend/run.py
@@ -232,7 +232,7 @@ def _working_local_url(port: int) -> "str | None":
def _localhost_ipv6_mismatch_url(bind_host: str, port: int) -> "str | None":
"""Return the IPv4 loopback URL when localhost won't reach 127.0.0.1.
- Local Studio binds to 127.0.0.1. Where localhost resolves to IPv6 only (::1),
+ Local Unsloth binds to 127.0.0.1. Where localhost resolves to IPv6 only (::1),
http://localhost: fails (or hits a different process on ::1) even though
http://127.0.0.1: works. Return the IPv4 URL for the caller to surface.
"""
@@ -243,7 +243,7 @@ def _localhost_ipv6_mismatch_url(bind_host: str, port: int) -> "str | None":
ipv4_url = f"http://127.0.0.1:{port}"
- # Only warn once Studio is confirmed answering on IPv4 loopback.
+ # Only warn once Unsloth is confirmed answering on IPv4 loopback.
if _working_local_url(port) != ipv4_url:
return None
@@ -265,7 +265,7 @@ def _localhost_ipv6_mismatch_url(bind_host: str, port: int) -> "str | None":
if host == "::1":
has_ipv6_loopback = True
- # A connection to ::1 is NOT evidence Studio is reachable there: Studio binds
+ # A connection to ::1 is NOT evidence Unsloth is reachable there: Unsloth binds
# 127.0.0.1 only, so anything on ::1 is a different process. Dual-stack
# localhost is fine (browsers fall back to 127.0.0.1), so only the IPv6-only
# case strands the user.
@@ -287,7 +287,7 @@ def _stdout_color_ok() -> bool:
def _print_localhost_ipv6_mismatch_warning(local_url: str, port: int) -> None:
- """Warn that localhost points at ::1 while Studio is bound to 127.0.0.1."""
+ """Warn that localhost points at ::1 while Unsloth is bound to 127.0.0.1."""
use_color = _stdout_color_ok()
warn_c = "\033[38;5;215;1m" if use_color else ""
reset = "\033[0m" if use_color else ""
@@ -303,7 +303,7 @@ def _print_localhost_ipv6_mismatch_warning(local_url: str, port: int) -> None:
def _verify_global_reachability(display_host: str, port: int) -> None:
"""Probe check-host.net to confirm display_host:port is reachable from the
public internet. Synchronous so output lands between the banner URLs and the
- stop hint. Bounded at ~15s; failures swallowed (verifier failing != Studio
+ stop hint. Bounded at ~15s; failures swallowed (verifier failing != Unsloth
failing). Only meaningful for a wildcard bind."""
global _public_reachable
# Reset to "unknown" each run; set True/False only when the probe decides.
@@ -563,15 +563,15 @@ def _print_cloudflare_line(secure: bool = False, loopback_host: str = "127.0.0.1
" Cloudflare tunnel: ON. This Cloudflare URL is PUBLIC, and the "
"raw port is also publicly reachable. --no-cloudflare disables "
f"only the Cloudflare URL; bind {loopback_host} or close firewall "
- "access to keep Studio private.",
+ "access to keep Unsloth private.",
warn,
)
else:
_emit(
" Cloudflare tunnel: ON. This is a PUBLIC internet URL: anyone "
- "who has it can reach this Studio. Relaunch with --no-cloudflare "
+ "who has it can reach this Unsloth. Relaunch with --no-cloudflare "
f"to disable the Cloudflare URL; bind {loopback_host} or close "
- "firewall access to keep Studio private.",
+ "firewall access to keep Unsloth private.",
warn,
)
return
@@ -580,12 +580,12 @@ def _print_cloudflare_line(secure: bool = False, loopback_host: str = "127.0.0.1
_emit(
" Cloudflare tunnel: requested but failed to start. The raw port is "
"still reachable from the public internet (see the reachability check "
- "above): anyone who can reach it can access this Studio.",
+ "above): anyone who can reach it can access this Unsloth.",
warn,
)
elif _public_reachable is False:
_emit(
- " Cloudflare tunnel: requested but failed to start. Studio is reachable "
+ " Cloudflare tunnel: requested but failed to start. Unsloth is reachable "
"on your local network only (no public link).",
warn,
)
@@ -593,7 +593,7 @@ def _print_cloudflare_line(secure: bool = False, loopback_host: str = "127.0.0.1
_emit(
" Cloudflare tunnel: requested but failed to start. There is no "
"Cloudflare public link. Raw port reachability was not verified; "
- f"bind {loopback_host} or close firewall access to keep Studio private.",
+ f"bind {loopback_host} or close firewall access to keep Unsloth private.",
warn,
)
elif _cloudflare_flag:
@@ -601,19 +601,19 @@ def _print_cloudflare_line(secure: bool = False, loopback_host: str = "127.0.0.1
_emit(
" Cloudflare tunnel: OFF for this mode. The raw port is still "
"reachable from the public internet (see the reachability check above): "
- "anyone who can reach it can access this Studio.",
+ "anyone who can reach it can access this Unsloth.",
warn,
)
elif _public_reachable is False:
_emit(
- " Cloudflare tunnel: OFF for this mode. Studio is reachable on your "
+ " Cloudflare tunnel: OFF for this mode. Unsloth is reachable on your "
"local network only (no public link)."
)
else:
_emit(
" Cloudflare tunnel: OFF for this mode. There is no Cloudflare public "
"link. Raw port reachability was not verified; "
- f"bind {loopback_host} or close firewall access to keep Studio private.",
+ f"bind {loopback_host} or close firewall access to keep Unsloth private.",
warn,
)
elif _cloudflare_flag is False or _cloudflare_flag is None:
@@ -624,12 +624,12 @@ def _print_cloudflare_line(secure: bool = False, loopback_host: str = "127.0.0.1
f" Cloudflare tunnel: OFF ({_reason}). The raw port is still "
"reachable from the public internet (see the reachability check above): "
"pass --cloudflare to also expose a public Cloudflare HTTPS link, or "
- f"bind {loopback_host} to keep Studio private.",
+ f"bind {loopback_host} to keep Unsloth private.",
warn,
)
elif _public_reachable is False:
_emit(
- f" Cloudflare tunnel: OFF ({_reason}). Studio is reachable on your "
+ f" Cloudflare tunnel: OFF ({_reason}). Unsloth is reachable on your "
"local network only. Pass --cloudflare to expose a public "
"Cloudflare HTTPS link."
)
@@ -638,7 +638,7 @@ def _print_cloudflare_line(secure: bool = False, loopback_host: str = "127.0.0.1
f" Cloudflare tunnel: OFF ({_reason}). There is no Cloudflare "
"public link. Raw port reachability was not verified; pass --cloudflare "
"to expose a public Cloudflare HTTPS link, or "
- f"bind {loopback_host} or close firewall access to keep Studio private.",
+ f"bind {loopback_host} or close firewall access to keep Unsloth private.",
warn,
)
@@ -674,7 +674,7 @@ def _is_port_free(host: str, port: int) -> bool:
For a ``0.0.0.0`` wildcard host, also check whether anything is listening on
``127.0.0.1`` (and ``::1`` when IPv6 exists): an SSH tunnel may hold loopback
- while the wildcard bind succeeds, making Studio unreachable via ``localhost``.
+ while the wildcard bind succeeds, making Unsloth unreachable via ``localhost``.
"""
import socket
@@ -1087,7 +1087,7 @@ def _terminal_password_gate(
) -> Tuple[bool, bool]:
"""Force a terminal password change before the public tunnel goes up.
- When the tunnel is about to publish Studio and the seeded admin password was
+ When the tunnel is about to publish Unsloth and the seeded admin password was
never changed, ask for a new one (masked, confirmed) before any public URL
exists. The CLI normally does this before re-exec'ing the backend; this is
the backstop for direct `python run.py` launches and older-CLI installs.
@@ -1147,7 +1147,7 @@ def _terminal_password_gate(
)
if not deadline_arms:
print(
- "Refusing to publish Studio on a public Cloudflare URL: the "
+ "Refusing to publish Unsloth on a public Cloudflare URL: the "
"default admin password was never changed, no terminal is "
"attached to change it here, and the bootstrap shutdown "
"deadline does not apply to this launch (api-only, or "
@@ -1163,11 +1163,11 @@ def _terminal_password_gate(
# terminal-attached run / reset-password instead of reading it from disk.
print(
" WARNING: the default admin password is still active while "
- "Studio is about to be published on a public Cloudflare URL, and "
+ "Unsloth is about to be published on a public Cloudflare URL, and "
"no terminal is attached to change it here. The public page will "
"NOT auto-fill the bootstrap credential. Set a new password by "
"running `unsloth studio` locally with a terminal attached, or "
- "`unsloth studio reset-password`. Studio shuts down after the "
+ "`unsloth studio reset-password`. Unsloth shuts down after the "
"bootstrap deadline (UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT, default 1h) "
"unless the password is changed.",
file = sys.stderr,
@@ -1222,7 +1222,7 @@ def _apply_supplied_password(password_value: "Optional[str]") -> None:
_auth_storage.ensure_default_admin()
if not _auth_storage.requires_password_change(_admin):
print(
- "Error: a Studio admin password is already set; --password only sets "
+ "Error: an Unsloth admin password is already set; --password only sets "
"the initial password. Run `unsloth studio reset-password` first.",
file = sys.stderr,
flush = True,
@@ -1337,7 +1337,7 @@ def run_server(
pass
# Persist a session log + native-crash stacks BEFORE importing main, so
- # even import-time failures leave evidence on disk. Field report: Studio
+ # even import-time failures leave evidence on disk. Field report: Unsloth
# "terminates without a warning" -- a native crash in the GPU runtime
# kills the process with no Python traceback, and a desktop-shortcut
# console closes before anything can be read. Console-only logging made
@@ -1406,7 +1406,7 @@ def run_server(
ensure_studio_directories()
logger.info(
- "Ensured Studio directories in %.1fms",
+ "Ensured Unsloth directories in %.1fms",
(time.perf_counter() - boot_started) * 1000,
)
@@ -1455,7 +1455,7 @@ def run_server(
installer_bin = home / "unsloth_studio" / "bin" / "unsloth"
tried_lines = "\n".join(f" - {p}" for p in attempted) or " (none)"
raise SystemExit(
- "[ERROR] Studio frontend build not found.\n"
+ "[ERROR] Unsloth frontend build not found.\n"
f"Tried:\n{tried_lines}\n"
"\n"
"Likely cause: another 'unsloth' on PATH is shadowing the "
@@ -1557,7 +1557,7 @@ def run_server(
)
if not _pw_proceed:
print(
- "Not starting Studio; set a new admin password first, or launch "
+ "Not starting Unsloth; set a new admin password first, or launch "
"without --secure/--cloudflare.",
file = sys.stderr,
flush = True,
@@ -1695,7 +1695,7 @@ def run_server(
logger = logger,
)
logger.info(
- "Studio will shut down in %ds unless the default admin password is changed.",
+ "Unsloth will shut down in %ds unless the default admin password is changed.",
_bootstrap_timeout,
)
except Exception as e: # best-effort: never block startup on the timeout
@@ -1753,11 +1753,11 @@ def _build_arg_parser():
"--cloudflare",
action = argparse.BooleanOptionalAction,
default = None,
- help = "Expose Studio on a PUBLIC internet URL via a free Cloudflare HTTPS "
+ help = "Expose Unsloth on a PUBLIC internet URL via a free Cloudflare HTTPS "
"tunnel, for non-api-only wildcard binds (0.0.0.0 or ::). Off by default; "
"pass --cloudflare to enable it (--secure implies it), --no-cloudflare to "
"force it off. It does not change a raw wildcard bind. If the admin "
- "password was never changed, Studio asks for a new one in the terminal "
+ "password was never changed, Unsloth asks for a new one in the terminal "
"before publishing the URL.",
)
parser.add_argument(
@@ -1767,7 +1767,7 @@ def _build_arg_parser():
help = "Expose ONLY a Cloudflare HTTPS link: bind localhost and fail closed "
"if the tunnel can't start. Without it, --no-secure also serves the raw "
"0.0.0.0 port, which is reachable from anywhere on the network. If the "
- "admin password was never changed, Studio asks for a new one in the "
+ "admin password was never changed, Unsloth asks for a new one in the "
"terminal before publishing the URL.",
)
# Back-compat: accept --not-secure as a hidden alias for --no-secure.
diff --git a/studio/backend/startup_banner.py b/studio/backend/startup_banner.py
index ea951a4325..9ec7a4f91c 100644
--- a/studio/backend/startup_banner.py
+++ b/studio/backend/startup_banner.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Terminal banner for Studio startup.
+"""Terminal banner for Unsloth startup.
Stdlib only -- safe to import without the rest of the backend.
"""
@@ -172,7 +172,7 @@ def print_studio_access_banner(
secondary,
),
style(
- " Only on trusted networks -- anyone who reaches this machine can use Studio.",
+ " Only on trusted networks -- anyone who reaches this machine can use Unsloth.",
secondary,
),
]
diff --git a/studio/backend/tests/conftest.py b/studio/backend/tests/conftest.py
index b0b9ee309c..c2216104a3 100644
--- a/studio/backend/tests/conftest.py
+++ b/studio/backend/tests/conftest.py
@@ -101,13 +101,13 @@ def studio_server(request):
@pytest.fixture
def base_url(studio_server):
- """Base URL for the e2e Studio server (from ``studio_server``)."""
+ """Base URL for the e2e Unsloth server (from ``studio_server``)."""
return studio_server[0]
@pytest.fixture
def api_key(studio_server):
- """API key for the e2e Studio server (from ``studio_server``)."""
+ """API key for the e2e Unsloth server (from ``studio_server``)."""
return studio_server[1]
diff --git a/studio/backend/tests/test_amd_apu_unified_memory.py b/studio/backend/tests/test_amd_apu_unified_memory.py
index 4df9e85b30..9fd8260bf2 100644
--- a/studio/backend/tests/test_amd_apu_unified_memory.py
+++ b/studio/backend/tests/test_amd_apu_unified_memory.py
@@ -91,7 +91,7 @@ class TestApuRamShortfall:
"""On a unified-memory APU the weights load into system RAM, so a model
larger than available RAM (the field case: a 64.6 GB GGUF on a WSL VM capped
well below the ROCm-reported APU budget) must be refused before spawning,
- not left to OOM-kill the Studio process."""
+ not left to OOM-kill the Unsloth process."""
def test_field_case_wsl_cap_refuses(self):
# 64.6 GB weights, ~46 GB available (WSL VM): refuse with guidance.
diff --git a/studio/backend/tests/test_anthropic_compaction.py b/studio/backend/tests/test_anthropic_compaction.py
index 1528eebe8b..acc0acc2e0 100644
--- a/studio/backend/tests/test_anthropic_compaction.py
+++ b/studio/backend/tests/test_anthropic_compaction.py
@@ -4,7 +4,7 @@
"""Unit tests for Anthropic server-side context compaction wiring.
Compaction is a beta (header ``compact-2026-01-12``) gated to Opus 4.6/4.7,
-Sonnet 4.6, and Mythos preview. When enabled, Studio attaches
+Sonnet 4.6, and Mythos preview. When enabled, Unsloth attaches
``context_management.edits[{type:"compact_20260112", trigger:{type:"input_tokens",
value:N}}]``; the 50k-token minimum is clamped up so the request doesn't 400.
diff --git a/studio/backend/tests/test_anthropic_fast_mode_edge.py b/studio/backend/tests/test_anthropic_fast_mode_edge.py
index dd69d77590..03f5d1c0eb 100644
--- a/studio/backend/tests/test_anthropic_fast_mode_edge.py
+++ b/studio/backend/tests/test_anthropic_fast_mode_edge.py
@@ -330,7 +330,7 @@ def test_refusal_chunk_is_proper_openai_delta_shape(monkeypatch):
def test_refusal_tool_event_chunk_shape(monkeypatch):
- """Drop signal rides a Studio `_toolEvent` envelope (delta={},
+ """Drop signal rides an Unsloth `_toolEvent` envelope (delta={},
finish_reason=null); the frontend latches on
`_toolEvent.type == "anthropic_refusal"`."""
_, lines = _capture(monkeypatch, sse = _refusal_sse(), model = "claude-opus-4-7")
@@ -409,7 +409,7 @@ def _fast_speed_sse(model: str = "claude-opus-4-7", speed: str = "fast") -> byte
def test_usage_speed_propagates_to_final_usage_chunk_fast(monkeypatch):
- """``usage.speed == "fast"`` from upstream must reach the Studio usage chunk."""
+ """``usage.speed == "fast"`` from upstream must reach the Unsloth usage chunk."""
_, lines = _capture(monkeypatch, sse = _fast_speed_sse(speed = "fast"))
usage_lines = [l for l in lines if l.startswith("data: ") and '"usage"' in l]
assert usage_lines, lines
@@ -428,7 +428,7 @@ def test_usage_speed_propagates_to_final_usage_chunk_standard(monkeypatch):
def test_usage_speed_absent_when_anthropic_does_not_report(monkeypatch):
- """Studio must not invent ``usage.speed`` when upstream omits it."""
+ """Unsloth must not invent ``usage.speed`` when upstream omits it."""
_, lines = _capture(monkeypatch)
parsed = [
json.loads(l[len("data: ") :]) for l in lines if l.startswith("data: ") and '"usage"' in l
diff --git a/studio/backend/tests/test_anthropic_messages.py b/studio/backend/tests/test_anthropic_messages.py
index 3b0ea37372..9ccc3f44dd 100644
--- a/studio/backend/tests/test_anthropic_messages.py
+++ b/studio/backend/tests/test_anthropic_messages.py
@@ -1418,7 +1418,7 @@ class TestNormalizeAnthropicOpenAIImages:
# =====================================================================
-# Studio-tool alias detection (/v1/messages tool routing)
+# Unsloth-tool alias detection (/v1/messages tool routing)
# =====================================================================
@@ -1436,7 +1436,7 @@ class TestAnthropicRequestedStudioTools:
def test_client_tool_named_python_is_not_misclassified(self):
# input_schema is the client-tool discriminator; its presence must
- # prevent the name from being treated as a Studio alias.
+ # prevent the name from being treated as an Unsloth alias.
tools = [
{
"name": "python",
@@ -1747,9 +1747,9 @@ class TestAnthropicMessagesToolRouting:
assert "name" in exc.value.detail
def test_alias_named_client_tool_without_schema_rejected_with_400(self, monkeypatch):
- # Regression: a typo'd client tool whose name collides with a Studio
+ # Regression: a typo'd client tool whose name collides with an Unsloth
# alias (e.g. a custom "python" tool missing input_schema) must
- # surface a 400, not silently switch into Studio's built-in python
+ # surface a 400, not silently switch into Unsloth's built-in python
# execution.
_mock_backend(monkeypatch)
payload = _basic_payload(tools = [{"name": "python"}])
@@ -1770,7 +1770,7 @@ class TestAnthropicMessagesToolRouting:
def test_disable_tools_policy_overrides_server_tool_alias(self, monkeypatch):
# CLI `unsloth run --disable-tools` sets policy=False. A request with
- # a Studio server-tool alias must NOT enter the agentic loop then.
+ # an Unsloth server-tool alias must NOT enter the agentic loop then.
backend = _mock_backend(monkeypatch)
set_tool_policy(False)
payload = _basic_payload(
diff --git a/studio/backend/tests/test_compute_buffer.py b/studio/backend/tests/test_compute_buffer.py
index 8408f8203d..3e95acc98d 100644
--- a/studio/backend/tests/test_compute_buffer.py
+++ b/studio/backend/tests/test_compute_buffer.py
@@ -152,7 +152,7 @@ class TestFallback:
class TestParallel1Default:
- """At Studio's default --parallel 1 the buffer is negligible in pipeline."""
+ """At Unsloth's default --parallel 1 the buffer is negligible in pipeline."""
def test_default_n_parallel(self):
est = _backend()._estimate_compute_buffer_bytes() / MIB
diff --git a/studio/backend/tests/test_cpu_threads.py b/studio/backend/tests/test_cpu_threads.py
index 2930c9f081..9d8795b6c0 100644
--- a/studio/backend/tests/test_cpu_threads.py
+++ b/studio/backend/tests/test_cpu_threads.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Tests for Studio's early CPU thread-pool configuration."""
+"""Tests for Unsloth's early CPU thread-pool configuration."""
import ast
import os
@@ -30,7 +30,7 @@ def test_cpu_thread_cap_seeds_native_pool_limits():
}
-# Explicit per-library values win over the Studio knob via setdefault.
+# Explicit per-library values win over the Unsloth knob via setdefault.
def test_cpu_thread_cap_preserves_runtime_specific_override():
env = {"UNSLOTH_CPU_THREADS": "4", "OMP_NUM_THREADS": "2"}
diff --git a/studio/backend/tests/test_frontend_resolution.py b/studio/backend/tests/test_frontend_resolution.py
index c3e0524a30..7ac2717aae 100644
--- a/studio/backend/tests/test_frontend_resolution.py
+++ b/studio/backend/tests/test_frontend_resolution.py
@@ -218,7 +218,7 @@ def test_systemexit_message_contains_actionable_fixes(tmp_path, monkeypatch):
installer_bin = home / "unsloth_studio" / "bin" / "unsloth"
tried_lines = "\n".join(f" - {p}" for p in attempted)
message = (
- "[ERROR] Studio frontend build not found.\n"
+ "[ERROR] Unsloth frontend build not found.\n"
f"Tried:\n{tried_lines}\n"
"\n"
"Likely cause: another 'unsloth' on PATH is shadowing the "
diff --git a/studio/backend/tests/test_gemini_provider.py b/studio/backend/tests/test_gemini_provider.py
index 85ceb04d27..c6ffa798d0 100644
--- a/studio/backend/tests/test_gemini_provider.py
+++ b/studio/backend/tests/test_gemini_provider.py
@@ -768,7 +768,7 @@ def test_cached_content_pass_through(monkeypatch):
def test_boolean_caching_does_not_set_cached_content(monkeypatch):
- """Studio's existing True/False signals shouldn't fabricate a cache id."""
+ """Unsloth's existing True/False signals shouldn't fabricate a cache id."""
captured = _capture_body(monkeypatch, enable_prompt_caching = True)
assert "cachedContent" not in captured["body"]
@@ -2613,7 +2613,7 @@ def test_gemini_native_skips_orphan_function_response_for_native_part_replay(mon
def test_gemini_native_part_falls_back_to_args_google(monkeypatch):
"""Round 27: a direct OpenAI-compat API caller (or imported third-party
- thread) cannot use Studio's non-standard `tool_calls[].extra_content`
+ thread) cannot use Unsloth's non-standard `tool_calls[].extra_content`
field, so the native_part payload round-trips through `function.arguments`
as `{"google": {"native_part": {...}}}`. The synthetic-builtin detector
recognizes that location, but the replay branch was only reading from
diff --git a/studio/backend/tests/test_gemma4_chat_template_override.py b/studio/backend/tests/test_gemma4_chat_template_override.py
index f726741aa5..9fb24a4cf6 100644
--- a/studio/backend/tests/test_gemma4_chat_template_override.py
+++ b/studio/backend/tests/test_gemma4_chat_template_override.py
@@ -3,7 +3,7 @@
"""Auto-override of the chat template for ``unsloth/gemma-4-*-GGUF``.
-Studio ships a bundled ``gemma-4.jinja`` (PR #118 based, ``preserve_thinking``
+Unsloth ships a bundled ``gemma-4.jinja`` (PR #118 based, ``preserve_thinking``
defaulted off) and applies it to gemma-4 GGUF loads via the existing
``chat_template_override`` -> ``--chat-template-file`` path, so users do not need
to re-download quants. Pins the family matcher, the resolver precedence, the
diff --git a/studio/backend/tests/test_hf_xet_fallback.py b/studio/backend/tests/test_hf_xet_fallback.py
index 2fff744b64..48aff29659 100644
--- a/studio/backend/tests/test_hf_xet_fallback.py
+++ b/studio/backend/tests/test_hf_xet_fallback.py
@@ -1,10 +1,10 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Tests for the Studio shim over the shared unsloth_zoo Xet -> HTTP fallback.
+"""Tests for the Unsloth shim over the shared unsloth_zoo Xet -> HTTP fallback.
The transport-policy matrix is tested once in unsloth_zoo; here we assert only the
-Studio seam: re-exporting the shared API and injecting the marker-aware
+Unsloth seam: re-exporting the shared API and injecting the marker-aware
prepare_cache_for_transport on the HTTP retry. CPU-only, no network, no real subprocess.
"""
@@ -69,7 +69,7 @@ def test_child_should_disable_xet_truth_table():
def test_shim_injects_studio_prepare_on_http_retry(monkeypatch):
- """A Xet stall retries over HTTP and the shim runs Studio's marker-aware
+ """A Xet stall retries over HTTP and the shim runs Unsloth's marker-aware
``prepare_cache_for_transport(..., 'http')`` before the retry."""
_requires_shared()
for var in ("UNSLOTH_DISABLE_XET", "UNSLOTH_STABLE_DOWNLOADS", "HF_HUB_DISABLE_XET"):
@@ -107,11 +107,11 @@ def test_shim_injects_studio_prepare_on_http_retry(monkeypatch):
out = xf.hf_hub_download_with_xet_fallback(DL_REPO, FILE, None)
assert out == "/cache/model.gguf"
assert seen_disable_xet == [False, True] # Xet first, then HTTP
- assert prepared == [("model", DL_REPO, "http")], "shim must run Studio's marker-aware prep"
+ assert prepared == [("model", DL_REPO, "http")], "shim must run Unsloth's marker-aware prep"
def test_shim_snapshot_injects_studio_prepare(monkeypatch):
- """The snapshot wrapper forwards Studio's marker-aware prep, like the file wrapper."""
+ """The snapshot wrapper forwards Unsloth's marker-aware prep, like the file wrapper."""
captured = {}
def fake_snapshot(repo_id, **kwargs):
@@ -127,7 +127,7 @@ def test_shim_snapshot_injects_studio_prepare(monkeypatch):
def test_degrades_gracefully_without_shared_helper(monkeypatch):
- """On an older unsloth_zoo lacking the shared helper, the shim still imports (Studio
+ """On an older unsloth_zoo lacking the shared helper, the shim still imports (Unsloth
boots) and exposes stub API doing plain HF downloads with the watchdog disabled."""
import importlib
@@ -206,7 +206,7 @@ def test_degrades_gracefully_without_shared_helper(monkeypatch):
def test_degrades_when_unsloth_zoo_entirely_absent():
"""When unsloth_zoo is absent entirely, the import raises
ModuleNotFoundError(name='unsloth_zoo') (top-level package). Guard that the shim still
- degrades and does not re-raise, breaking every Studio import that pulls it in."""
+ degrades and does not re-raise, breaking every Unsloth import that pulls it in."""
import importlib
class _BlockZoo:
@@ -248,7 +248,7 @@ def test_degrades_when_unsloth_zoo_entirely_absent():
def test_degrades_when_shared_helper_import_raises_importerror():
"""unsloth_zoo can be installed yet fail to import when torch is missing (llama.cpp/GGUF-only
- Studio), raising ImportError not ModuleNotFoundError. The shim must degrade for that too."""
+ Unsloth), raising ImportError not ModuleNotFoundError. The shim must degrade for that too."""
import importlib
class _BlockWithImportError:
@@ -329,7 +329,7 @@ def test_retries_under_light_gpu_init_when_import_fails(monkeypatch):
# with it set); accessing DownloadStallError drives it via __getattr__.
stall_error = degraded.DownloadStallError
assert seen_env == [None, "1"], seen_env
- # Both attempts raised -> Studio still boots in degraded mode.
+ # Both attempts raised -> Unsloth still boots in degraded mode.
assert issubclass(stall_error, RuntimeError)
# The env override must not leak past the load.
assert os.environ.get("UNSLOTH_ZOO_DISABLE_GPU_INIT") is None
diff --git a/studio/backend/tests/test_identity.py b/studio/backend/tests/test_identity.py
index 1e84ddef35..712348f7ca 100644
--- a/studio/backend/tests/test_identity.py
+++ b/studio/backend/tests/test_identity.py
@@ -3,7 +3,7 @@
"""Tests for the server identity handshake (`GET /api/auth/identity`).
-The endpoint lets a client confirm an endpoint is really this Studio install
+The endpoint lets a client confirm an endpoint is really this Unsloth install
before sending it a credential: the client sends a random nonce and checks the
returned HMAC against one computed from the install identity secret. A process
that cannot read this same-user secret cannot forge a matching proof.
diff --git a/studio/backend/tests/test_index_bootstrap_origin_extra.py b/studio/backend/tests/test_index_bootstrap_origin_extra.py
index feda88c14c..e1c52a653e 100644
--- a/studio/backend/tests/test_index_bootstrap_origin_extra.py
+++ b/studio/backend/tests/test_index_bootstrap_origin_extra.py
@@ -26,7 +26,7 @@ def _build_request(
def test_is_same_origin_request_ipv6_loopback_same_origin():
- """Studio supports ``-H ::1`` binds; netloc is ``[::1]:8902``. Bare
+ """Unsloth supports ``-H ::1`` binds; netloc is ``[::1]:8902``. Bare
``partition(":")`` mis-parses the bracketed form and would refuse the
bootstrap on legitimate same-origin navigation.
"""
diff --git a/studio/backend/tests/test_llama_cpp_context_fit.py b/studio/backend/tests/test_llama_cpp_context_fit.py
index d3a10df8ca..2a4f6d19d2 100644
--- a/studio/backend/tests/test_llama_cpp_context_fit.py
+++ b/studio/backend/tests/test_llama_cpp_context_fit.py
@@ -567,7 +567,7 @@ class TestClassifyGpuOffload:
assert inst._classify_gpu_offload(False, []) is None
def test_user_did_not_intend_gpu_returns_none(self):
- # Studio called start_llama_server without expecting GPU; don't warn.
+ # Unsloth called start_llama_server without expecting GPU; don't warn.
inst = self._backend(
[
"load_tensors: CPU_Mapped model buffer size = 21000.0 MiB",
diff --git a/studio/backend/tests/test_llama_cpp_mmproj_fallback.py b/studio/backend/tests/test_llama_cpp_mmproj_fallback.py
index 04d4aac9e1..049058e511 100644
--- a/studio/backend/tests/test_llama_cpp_mmproj_fallback.py
+++ b/studio/backend/tests/test_llama_cpp_mmproj_fallback.py
@@ -222,7 +222,7 @@ class TestFlashAttnOff:
assert _flash_off(["llama-server", "-fa=on"]) == ["llama-server", "-fa=off"]
def test_flips_every_occurrence_last_wins(self):
- # extra_args can re-enable FA after Studio's flag; llama.cpp is last-wins,
+ # extra_args can re-enable FA after Unsloth's flag; llama.cpp is last-wins,
# so one leftover 'on' would re-crash the retry. Every enable must flip.
cmd = ["llama-server", "--flash-attn", "on", "--mmproj", "/p", "--flash-attn", "on"]
out = _flash_off(cmd)
@@ -234,7 +234,7 @@ class TestFlashAttnOff:
assert _flash_off(["llama-server", "--flash-attn=off"]) is None
def test_none_when_user_off_wins_last(self):
- # User appended 'off' after Studio's 'on'; effective (last-wins) is off,
+ # User appended 'off' after Unsloth's 'on'; effective (last-wins) is off,
# so there is nothing to retry.
assert _flash_off(["llama-server", "--flash-attn", "on", "--flash-attn", "off"]) is None
diff --git a/studio/backend/tests/test_llama_cpp_mtp_detection.py b/studio/backend/tests/test_llama_cpp_mtp_detection.py
index 3f9d2a8f50..8fe04c0e39 100644
--- a/studio/backend/tests/test_llama_cpp_mtp_detection.py
+++ b/studio/backend/tests/test_llama_cpp_mtp_detection.py
@@ -1014,7 +1014,7 @@ def test_already_in_target_state_2b_falls_back_to_ngram_below_threshold(monkeypa
)
-# usage backfill from timings (Studio UI t/s widget fix).
+# usage backfill from timings (Unsloth UI t/s widget fix).
def test_backfill_usage_from_timings_fills_when_completion_tokens_zero():
@@ -1606,7 +1606,7 @@ def test_reload_forced_mtp_bounces_auto_mla():
)
-# ── Full named-repo resolver matrix (the shipping Studio families) ─────
+# ── Full named-repo resolver matrix (the shipping Unsloth families) ─────
#
# Locks auto / off / forced-mtp routing for every Qwen3.5 (MTP + plain) and
# gemma-4 (regular + QAT) GGUF repo, including the giant MoEs that stay
diff --git a/studio/backend/tests/test_llama_cpp_no_context_shift.py b/studio/backend/tests/test_llama_cpp_no_context_shift.py
index 10b1dc7ff6..f320d29a02 100644
--- a/studio/backend/tests/test_llama_cpp_no_context_shift.py
+++ b/studio/backend/tests/test_llama_cpp_no_context_shift.py
@@ -5,7 +5,7 @@
With llama-server's default context-shift behavior, the UI cannot tell the user
the KV cache was rotated -- earlier turns silently vanish from the conversation.
-The Studio backend always passes ``--no-context-shift`` so the server returns a
+The Unsloth backend always passes ``--no-context-shift`` so the server returns a
clean error instead, and the chat adapter can point the user at the
``Context Length`` input in the settings panel.
diff --git a/studio/backend/tests/test_llama_cpp_props_readback.py b/studio/backend/tests/test_llama_cpp_props_readback.py
index 316956325f..488645ee5a 100644
--- a/studio/backend/tests/test_llama_cpp_props_readback.py
+++ b/studio/backend/tests/test_llama_cpp_props_readback.py
@@ -4,7 +4,7 @@
"""Tests for the post-launch /props context readback.
llama-server's memory-fit step or --parallel slot split can allocate less
-context than the requested -c while Studio keeps advertising the requested
+context than the requested -c while Unsloth keeps advertising the requested
value; clients sized to it then die on exceed_context_size_error 400s.
``_reconcile_effective_ctx_with_server`` must adopt the server's real
``default_generation_settings.n_ctx`` whenever it is smaller.
@@ -223,7 +223,7 @@ _CAPS_NONE = {"supports_kv_unified": False, "supports_fit_ctx": False}
def test_kv_unified_added_for_multi_slot():
"""Explicit --parallel N disables llama-server's auto-slots kv-unified
- default, splitting -c into per-slot windows of -c/N; Studio must restore
+ default, splitting -c into per-slot windows of -c/N; Unsloth must restore
the shared pool so one request can use the full advertised context."""
flags = LlamaCppBackend._ctx_integrity_flags(4, False, 98304, 98304, _CAPS_ALL)
assert "--kv-unified" in flags
diff --git a/studio/backend/tests/test_llama_cpp_tool_loop.py b/studio/backend/tests/test_llama_cpp_tool_loop.py
index bd2c008589..e99e227d40 100644
--- a/studio/backend/tests/test_llama_cpp_tool_loop.py
+++ b/studio/backend/tests/test_llama_cpp_tool_loop.py
@@ -122,7 +122,7 @@ def _structured_tool_call(tool_name: str, arguments: dict, call_id: str) -> list
def test_structured_tool_call_after_visible_preface_is_executed(monkeypatch):
"""llama-server may emit content first and then native delta.tool_calls.
- Studio must not drop that tool call after it has streamed the preface.
+ Unsloth must not drop that tool call after it has streamed the preface.
"""
tool_call_id = "call_render_late"
diff --git a/studio/backend/tests/test_llama_cpp_wait_for_health.py b/studio/backend/tests/test_llama_cpp_wait_for_health.py
index 82c5b4931a..423c3dd009 100644
--- a/studio/backend/tests/test_llama_cpp_wait_for_health.py
+++ b/studio/backend/tests/test_llama_cpp_wait_for_health.py
@@ -224,7 +224,7 @@ class TestRetryLogFilenameUnique:
class TestFitOffRetryEligible:
"""Gate for the one-shot --fit off startup-crash retry.
- Retry only when Studio's own VRAM math placed the model and nothing
+ Retry only when Unsloth's own VRAM math placed the model and nothing
on the command line chose the fit mode explicitly."""
def test_eligible_for_plain_ngl_launch(self):
diff --git a/studio/backend/tests/test_llama_cpp_wait_for_vram_settle.py b/studio/backend/tests/test_llama_cpp_wait_for_vram_settle.py
index d0213f6079..b28df7ec3f 100644
--- a/studio/backend/tests/test_llama_cpp_wait_for_vram_settle.py
+++ b/studio/backend/tests/test_llama_cpp_wait_for_vram_settle.py
@@ -346,7 +346,7 @@ def test_helper_is_static_method_callable_off_class():
def test_kill_orphaned_servers_returns_count():
"""The reaper reports how many owned orphans it killed, so __init__ can
- arm the settle wait. Only Studio-owned llama-server procs count."""
+ arm the settle wait. Only Unsloth-owned llama-server procs count."""
import os
mypid = os.getpid()
@@ -376,7 +376,7 @@ def test_kill_orphaned_servers_returns_count():
patch.object(LlamaCppBackend, "_pid_parent_is_alive", staticmethod(lambda pid: False)),
):
n = LlamaCppBackend._kill_orphaned_servers()
- assert n == 1, "only the Studio-owned orphan should be counted"
+ assert n == 1, "only the Unsloth-owned orphan should be counted"
assert killed == [mypid + 1]
# No owned orphans -> zero, so __init__ leaves the cold-start sentinel.
@@ -392,8 +392,8 @@ def test_kill_orphaned_servers_returns_count():
def test_kill_orphaned_servers_spares_live_parent():
- """A Studio-owned llama-server whose parent is still running is not an
- orphan (a live Studio or the user's shell owns it) and must never be
+ """An Unsloth-owned llama-server whose parent is still running is not an
+ orphan (a live Unsloth or the user's shell owns it) and must never be
killed; only the true orphan (parent gone) is reaped."""
import os
@@ -548,7 +548,7 @@ def test_record_then_reap_round_trip_identity_matches(tmp_path):
def test_reap_recorded_pid_spares_live_server(tmp_path):
- """A recorded server whose parent is still alive (the running Studio) is NEVER
+ """A recorded server whose parent is still alive (the running Unsloth) is NEVER
reaped, and its pidfile is kept. This is the finding-3 guard: a helper backend
constructed in-process must not kill the active chat server. Uses the REAL
_pid_parent_is_alive (the child's parent is this live test process)."""
diff --git a/studio/backend/tests/test_llama_cpp_windows_nvidia_path.py b/studio/backend/tests/test_llama_cpp_windows_nvidia_path.py
index 957de4bad6..489d9eb8d1 100644
--- a/studio/backend/tests/test_llama_cpp_windows_nvidia_path.py
+++ b/studio/backend/tests/test_llama_cpp_windows_nvidia_path.py
@@ -3,7 +3,7 @@
"""Tests for the Windows pip-nvidia DLL dir resolver.
-Studio installs torch with bundled CUDA wheels (nvidia-cuda-runtime-cu13,
+Unsloth installs torch with bundled CUDA wheels (nvidia-cuda-runtime-cu13,
nvidia-cublas-cu13, etc.) and the prebuilt llama-server.exe must find those
DLLs at runtime to load CUDA. Mirrors the Linux LD_LIBRARY_PATH block.
See unslothai/unsloth#5106.
diff --git a/studio/backend/tests/test_llama_server_args.py b/studio/backend/tests/test_llama_server_args.py
index deeb228026..ba52afad1c 100644
--- a/studio/backend/tests/test_llama_server_args.py
+++ b/studio/backend/tests/test_llama_server_args.py
@@ -75,7 +75,7 @@ validate_extra_args = _lsa.validate_extra_args
# Reasoning controls
["--reasoning-format", "deepseek"],
["-rea", "auto"],
- # Soft-managed: user flags last-wins over Studio's auto-set version.
+ # Soft-managed: user flags last-wins over Unsloth's auto-set version.
# --parallel / -np / --n-parallel are hard-denied (KV-cache + slot
# count would desync); use `unsloth studio run --parallel N` instead.
["-c", "131072"],
@@ -150,7 +150,7 @@ def test_non_flag_token_passes_through():
"--mmproj",
"-mmu",
"--mmproj-url",
- # Networking (Studio binds + proxies)
+ # Networking (Unsloth binds + proxies)
"--host",
"--port",
"--path",
@@ -176,12 +176,12 @@ def test_non_flag_token_passes_through():
"--models-autoload",
"--no-models-autoload",
# Server-mode flips: --embedding / --rerank restrict llama-server to
- # those endpoints and break Studio's chat hop.
+ # those endpoints and break Unsloth's chat hop.
"--embedding",
"--embeddings",
"--rerank",
"--reranking",
- # llama-server's own --tools clashes with Studio's tool policy.
+ # llama-server's own --tools clashes with Unsloth's tool policy.
"--tools",
],
)
@@ -194,7 +194,7 @@ def test_denylist_rejects_all_aliases(denied):
"args,offending",
[
# Pass-through --parallel would last-wins-override the real slot
- # count while Studio's KV-cache fit + llama_parallel_slots stay at
+ # count while Unsloth's KV-cache fit + llama_parallel_slots stay at
# the typer value -- plan vs. process disagree.
(["--parallel", "8"], "--parallel"),
(["--parallel=8"], "--parallel"),
@@ -656,7 +656,7 @@ def test_extra_args_disable_mmproj_last_wins():
def test_strip_shadowing_flags_drops_model_draft_with_spec():
- # --model-draft (and aliases) are Studio-managed since the separate
+ # --model-draft (and aliases) are Unsloth-managed since the separate
# MTP drafter support: an inherited copy must not last-wins-override
# the auto-detected drafter.
out = strip_shadowing_flags(
@@ -681,7 +681,7 @@ def test_strip_shadowing_flags_drops_model_draft_with_spec():
)
def test_strip_shadowing_flags_drops_hf_drafter_selectors_with_spec(selector):
# HF drafter selectors must reset on inherit like local --model-draft, or a
- # stale inherited HF drafter last-wins over Studio's re-derived spec choice.
+ # stale inherited HF drafter last-wins over Unsloth's re-derived spec choice.
out = strip_shadowing_flags(
selector + ["--top-k", "20"],
strip_context = False,
@@ -769,7 +769,7 @@ def test_strip_split_mode_only_preserves_none_and_empty():
def test_strip_shadowing_flags_drops_tensor_split_with_split_mode():
# --tensor-split is coupled to the split mode: stripped together so a stale
- # ratio can't override Studio's computed tensor split. Other flags survive.
+ # ratio can't override Unsloth's computed tensor split. Other flags survive.
out = strip_shadowing_flags(
["--split-mode", "row", "--tensor-split", "1,1", "--top-k", "20"],
strip_context = False,
diff --git a/studio/backend/tests/test_local_llama_cpp_link.py b/studio/backend/tests/test_local_llama_cpp_link.py
index c78c029d91..6b44f61972 100644
--- a/studio/backend/tests/test_local_llama_cpp_link.py
+++ b/studio/backend/tests/test_local_llama_cpp_link.py
@@ -4,7 +4,7 @@
"""Behavioral tests for the --with-llama-cpp-dir 'unmanaged local link' contract.
When the canonical llama.cpp dir is a symlink (POSIX) / junction (Windows) to a
-user's own checkout, Studio must treat it as externally managed:
+user's own checkout, Unsloth must treat it as externally managed:
- the in-app updater must not offer or apply a prebuilt over the link
- orphan cleanup must not kill a llama-server the user launched from that tree
@@ -67,7 +67,7 @@ def test_active_install_is_local_link(tmp_path: Path) -> None:
binary = str(link / _server_subpath())
assert u._active_install_is_local_link(binary) is True
- # A plain (non-link) llama.cpp dir is Studio-managed, not a local link.
+ # A plain (non-link) llama.cpp dir is Unsloth-managed, not a local link.
plain = tmp_path / "plain" / "llama.cpp"
plain.mkdir(parents = True)
assert u._active_install_is_local_link(str(plain / _server_subpath())) is False
diff --git a/studio/backend/tests/test_mcp_servers.py b/studio/backend/tests/test_mcp_servers.py
index 6432ffb8e1..c5c37f098f 100644
--- a/studio/backend/tests/test_mcp_servers.py
+++ b/studio/backend/tests/test_mcp_servers.py
@@ -577,7 +577,7 @@ def test_clear_oauth_tokens_swallows_constructor_errors(tmp_path, monkeypatch):
def test_tool_xml_parser_handles_hyphenated_function_names():
"""Hyphenated tool names like `mcp__srv__list-issues` must parse, else the
- model can call the tool but Studio can't dispatch."""
+ model can call the tool but Unsloth can't dispatch."""
from core.inference.tool_call_parser import parse_tool_calls_from_text
calls = parse_tool_calls_from_text(
diff --git a/studio/backend/tests/test_mcp_stdio_improvements.py b/studio/backend/tests/test_mcp_stdio_improvements.py
index b0bfd45135..745c2cc447 100644
--- a/studio/backend/tests/test_mcp_stdio_improvements.py
+++ b/studio/backend/tests/test_mcp_stdio_improvements.py
@@ -188,7 +188,7 @@ def test_validate_url_allows_url_in_argument(monkeypatch):
# ── P6: Data Recipe stdio path obeys the same host gate ─────────────
-# build_mcp_providers needs the Studio-only data_designer plugin; skip if absent.
+# build_mcp_providers needs the Unsloth-only data_designer plugin; skip if absent.
_STDIO_RECIPE = {
"mcp_providers": [
diff --git a/studio/backend/tests/test_mlx_inference_backend.py b/studio/backend/tests/test_mlx_inference_backend.py
index 7b8aefb722..fafaea0043 100644
--- a/studio/backend/tests/test_mlx_inference_backend.py
+++ b/studio/backend/tests/test_mlx_inference_backend.py
@@ -236,7 +236,7 @@ def test_mlx_inference_vlm_lora_uses_unsloth_loader_without_native_adapter_rewri
_install_fake_fast_mlx(monkeypatch, calls)
def _native_vlm_load(*_args, **_kwargs):
- raise AssertionError("Studio MLX VLM inference must use FastMLXModel")
+ raise AssertionError("Unsloth MLX VLM inference must use FastMLXModel")
mlx_vlm = types.ModuleType("mlx_vlm")
mlx_vlm.load = _native_vlm_load
diff --git a/studio/backend/tests/test_mlx_repair.py b/studio/backend/tests/test_mlx_repair.py
index 365cc46410..47a695ccbd 100644
--- a/studio/backend/tests/test_mlx_repair.py
+++ b/studio/backend/tests/test_mlx_repair.py
@@ -103,7 +103,7 @@ def test_repair_install_pins_transformers_and_cleans_up(monkeypatch):
assert mr.attempt_mlx_repair() is True
cmd = captured["cmd"]
# transformers is pinned via a constraint file so the mlx install cannot
- # upgrade it underneath Studio, and the temp constraint file is cleaned up.
+ # upgrade it underneath Unsloth, and the temp constraint file is cleaned up.
assert "--constraint" in cmd
assert "--upgrade" in cmd
reinstall_pairs = set(zip(cmd, cmd[1:]))
@@ -123,7 +123,7 @@ def test_install_requires_prebuilt_wheels(monkeypatch):
# A source distribution's PEP 517 build backend runs arbitrary code at install
# time, before the post-install stack check. The unattended self-heal must
# require pre-built wheels so a malicious resolver-selected sdist cannot execute
- # during ordinary Studio startup. mlx/mlx-metal ship wheels only and
+ # during ordinary Unsloth startup. mlx/mlx-metal ship wheels only and
# mlx-lm/mlx-vlm publish py3-none-any wheels, so a healthy self-heal still works.
pytest.importorskip("transformers")
captured = {}
@@ -143,7 +143,7 @@ def test_install_requires_prebuilt_wheels(monkeypatch):
def test_install_env_drops_secrets_and_source_redirects(monkeypatch):
- # The unattended self-heal must not hand resolver/build code the full Studio
+ # The unattended self-heal must not hand resolver/build code the full Unsloth
# environment: secrets and package-source redirects are dropped, while the
# variables uv genuinely needs are forwarded.
monkeypatch.setenv("HF_TOKEN", "secret-hf")
diff --git a/studio/backend/tests/test_mtp_vram_budget.py b/studio/backend/tests/test_mtp_vram_budget.py
index 0efbbf596d..694d60cfc6 100644
--- a/studio/backend/tests/test_mtp_vram_budget.py
+++ b/studio/backend/tests/test_mtp_vram_budget.py
@@ -502,7 +502,7 @@ class TestExtraArgsMtpDetection:
assert _extra_args_mtp_draft_path([], env = dict(os.environ)) == "/large.gguf"
def test_load_model_gates_env_spec_type_on_off_mode(self):
- # LLAMA_ARG_SPEC_TYPE only reaches the child when Studio emits no spec
+ # LLAMA_ARG_SPEC_TYPE only reaches the child when Unsloth emits no spec
# flag (UI mode "off", no user --spec-type); otherwise the emitted
# --spec-type/--spec-default overrides the env, so the reserve must not
# consult it or a stale MTP env over-reserves (Finding F3). Whitespace-
@@ -530,8 +530,8 @@ class TestExtraArgsMtpDetection:
def test_load_model_drafter_budget_precedence(self):
# The budget sizes the drafter the launch actually loads: CLI extras win,
- # then Studio's emitted mtp_draft_path (overrides LLAMA_ARG_SPEC_DRAFT_MODEL),
- # then the env drafter -- not the env before Studio's (reviewer.py R3).
+ # then Unsloth's emitted mtp_draft_path (overrides LLAMA_ARG_SPEC_DRAFT_MODEL),
+ # then the env drafter -- not the env before Unsloth's (reviewer.py R3).
compact = "".join(inspect.getsource(LlamaCppBackend.load_model).split())
assert "_cli_draft_for_budget=_extra_args_mtp_draft_path(extra_args,env={})" in compact
assert "_env_draft_for_budget=_extra_args_mtp_draft_path([],env=os.environ)" in compact
@@ -732,7 +732,7 @@ class TestExtraArgsMtpDetection:
assert _extra_args_n_ubatch([], env = {"LLAMA_ARG_UBATCH": "notint"}) is None
def test_env_main_cache_type_for_budget(self):
- # The child inherits LLAMA_ARG_CACHE_TYPE_K/_V, but Studio emits no
+ # The child inherits LLAMA_ARG_CACHE_TYPE_K/_V, but Unsloth emits no
# --cache-type when neither param nor extras set it -> a heavier env
# main KV (f32) must be adopted so the reserve matches the child.
assert _env_main_cache_type_for_budget(env = {}) is None
@@ -765,7 +765,7 @@ class TestExtraArgsMtpDetection:
assert "cache_type_kv=_env_main_cache_type_for_budget()" in compact
def test_env_split_mode_is_tensor(self):
- # The child inherits LLAMA_ARG_SPLIT_MODE, but Studio emits --split-mode
+ # The child inherits LLAMA_ARG_SPLIT_MODE, but Unsloth emits --split-mode
# only on its tensor branch -> a tensor env must flip the budget so the
# heavier per-device compute buffer is reserved (not layer overhead).
assert _env_split_mode_is_tensor(env = {}) is False
@@ -918,7 +918,7 @@ class TestExtraArgsMtpDetection:
# Cluster A: when the final decision is layer split, an inherited
# non-layer LLAMA_ARG_SPLIT_MODE (and paired LLAMA_ARG_TENSOR_SPLIT) must
# be popped from the child env so the child cannot run tensor/row/none
- # against Studio's layer budget. Whitespace-stripped for formatter.
+ # against Unsloth's layer budget. Whitespace-stripped for formatter.
compact = "".join(inspect.getsource(LlamaCppBackend.load_model).split())
assert 'env.get("LLAMA_ARG_SPLIT_MODE")' in compact
assert '_inherited_sm!="layer"' in compact
@@ -936,10 +936,10 @@ class TestExtraArgsMtpDetection:
assert "env.pop(_ct_var,None)" in compact
def test_load_model_clears_tensor_split_env_in_tensor_mode(self):
- # review run3 #2: Studio owns the tensor split. When it emits no
+ # review run3 #2: Unsloth owns the tensor split. When it emits no
# --tensor-split (even split), a stale inherited LLAMA_ARG_TENSOR_SPLIT must
# be cleared in the TENSOR branch too (not just the layer downgrade), or the
- # child runs a split Studio didn't budget. The else (tensor) branch pops it.
+ # child runs a split Unsloth didn't budget. The else (tensor) branch pops it.
src = inspect.getsource(LlamaCppBackend.load_model)
compact = "".join(src.split())
# appears in both the layer branch and the tensor branch.
@@ -1005,14 +1005,14 @@ def test_qwen36_class_regression_picks_lower_ctx_with_mtp():
def test_mtp_draft_budget_prefers_user_extras_drafter():
# A user --model-draft in extras is appended last and wins at launch, so the
- # VRAM budget must size it first; then Studio's emitted mtp_draft_path (which
+ # VRAM budget must size it first; then Unsloth's emitted mtp_draft_path (which
# overrides LLAMA_ARG_SPEC_DRAFT_MODEL), then the env drafter (load_model is too
# entangled to drive end-to-end; assert the precedence at the source level).
# Whitespace-stripped so the check survives any formatter line-wrapping.
compact = "".join(inspect.getsource(LlamaCppBackend.load_model).split())
- # CLI extras sized first (env={} so the env doesn't pre-empt Studio's drafter).
+ # CLI extras sized first (env={} so the env doesn't pre-empt Unsloth's drafter).
assert "_cli_draft_for_budget=_extra_args_mtp_draft_path(extra_args,env={})" in compact
- # Order: CLI extras, then Studio's mtp_draft_path, then the env drafter.
+ # Order: CLI extras, then Unsloth's mtp_draft_path, then the env drafter.
assert "_cli_draft_for_budgetor_studio_draft_for_budgetor_env_draft_for_budget" in compact
- # The env must not be consulted before Studio's resolved drafter.
+ # The env must not be consulted before Unsloth's resolved drafter.
assert "_extra_args_mtp_draft_path(extra_args)ormtp_draft_path" not in compact
diff --git a/studio/backend/tests/test_multimodal_document.py b/studio/backend/tests/test_multimodal_document.py
index 5cd7c876cc..b347c4aef8 100644
--- a/studio/backend/tests/test_multimodal_document.py
+++ b/studio/backend/tests/test_multimodal_document.py
@@ -3,7 +3,7 @@
"""Tests for PDF / document attachment translation on external providers.
-Studio adds a normalised `input_document` content part on
+Unsloth adds a normalised `input_document` content part on
ChatCompletionRequest so the frontend needn't know the per-provider
attachment shape:
diff --git a/studio/backend/tests/test_nudge_tool_calls_wiring.py b/studio/backend/tests/test_nudge_tool_calls_wiring.py
index e03fd0c7d7..82a6543aeb 100644
--- a/studio/backend/tests/test_nudge_tool_calls_wiring.py
+++ b/studio/backend/tests/test_nudge_tool_calls_wiring.py
@@ -3,7 +3,7 @@
"""Wiring guard for the plan-without-action ``nudge_tool_calls`` policy.
-Decided policy: the re-prompt is ALWAYS ON for the Studio inference paths
+Decided policy: the re-prompt is ALWAYS ON for the Unsloth inference paths
(safetensors, GGUF/llama_cpp, MLX) and OPT-IN for the API (/v1 OpenAI-compat +
Anthropic-compat, controlled by the request's ``nudge_tool_calls``, default off).
@@ -16,7 +16,7 @@ Mechanism (verified here without loading a model):
opt-in), while the GGUF loop keeps its pre-existing default-on behaviour
(``None`` keeps nudging) so an omitted flag never disables GGUF;
* the API request models default the flag to ``None`` (opt-in / off);
- * the Studio-facing routes forward the request's flag, and the Studio frontend
+ * the Unsloth-facing routes forward the request's flag, and the Unsloth frontend
sends ``nudge_tool_calls: true`` -- exercised behaviourally in
``test_safetensors_tool_loop.py`` and ``test_llama_cpp_tool_loop.py``.
"""
@@ -87,7 +87,7 @@ def test_api_request_models_default_the_flag_off():
def test_studio_routes_forward_the_request_flag():
- # The Studio chat frontend posts to /v1/chat/completions and /v1/messages
+ # The Unsloth chat frontend posts to /v1/chat/completions and /v1/messages
# with nudge_tool_calls=true; the route handlers forward the request value
# (external API clients that omit it fall back to the opt-in default).
from routes import inference as routes_inference
diff --git a/studio/backend/tests/test_offline_gguf_cache_fallback.py b/studio/backend/tests/test_offline_gguf_cache_fallback.py
index 0b9a1e704f..295549c443 100644
--- a/studio/backend/tests/test_offline_gguf_cache_fallback.py
+++ b/studio/backend/tests/test_offline_gguf_cache_fallback.py
@@ -897,7 +897,7 @@ class TestHfOfflineIfDnsDead:
assert "HF_HUB_OFFLINE" not in os.environ
def test_user_set_hf_hub_offline_is_preserved(self, dns, clean_offline_env, monkeypatch):
- # User explicitly set offline before launching Studio.
+ # User explicitly set offline before launching Unsloth.
monkeypatch.setenv("HF_HUB_OFFLINE", "1")
dns.fail()
with _hf_offline_if_dns_dead() as did_set:
diff --git a/studio/backend/tests/test_offline_inference_parent.py b/studio/backend/tests/test_offline_inference_parent.py
index 71331220d6..bd0014ea64 100644
--- a/studio/backend/tests/test_offline_inference_parent.py
+++ b/studio/backend/tests/test_offline_inference_parent.py
@@ -139,7 +139,7 @@ class TestLoraDetectOffline:
monkeypatch.setenv("HF_HUB_OFFLINE", "1")
- # Studio catches Exception broadly; pin that the call still happens
+ # Unsloth catches Exception broadly; pin that the call still happens
# (so cached LoRAs aren't missed) and returns fast via the mock.
class _OfflineModeIsEnabled(Exception):
pass
diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.py
index 8742b84ae7..90fbd19297 100644
--- a/studio/backend/tests/test_openai_auto_switch.py
+++ b/studio/backend/tests/test_openai_auto_switch.py
@@ -689,7 +689,7 @@ def test_v1_models_retrieve_is_case_insensitive(monkeypatch):
def test_index_excludes_hidden_models(tmp_path, monkeypatch):
# The llama.cpp validation probe and RAG embedding weights are hidden from
- # Studio's pickers; they must never become auto-switch targets.
+ # Unsloth's pickers; they must never become auto-switch targets.
from types import SimpleNamespace
import routes.models as models_route
diff --git a/studio/backend/tests/test_openai_compaction.py b/studio/backend/tests/test_openai_compaction.py
index c7de0a9aed..6fad2c5eaf 100644
--- a/studio/backend/tests/test_openai_compaction.py
+++ b/studio/backend/tests/test_openai_compaction.py
@@ -86,7 +86,7 @@ def test_cloud_openai_sets_compaction_block(monkeypatch):
def test_cloud_openai_below_default_threshold_passes_through(monkeypatch):
- # Studio doesn't clamp the OpenAI side -- the API accepts whatever the
+ # Unsloth doesn't clamp the OpenAI side -- the API accepts whatever the
# caller sends, so a small probe like 60k still goes through.
captured = _capture(
monkeypatch,
diff --git a/studio/backend/tests/test_openai_image_generation.py b/studio/backend/tests/test_openai_image_generation.py
index ace57588d3..c2eef0381f 100644
--- a/studio/backend/tests/test_openai_image_generation.py
+++ b/studio/backend/tests/test_openai_image_generation.py
@@ -4,7 +4,7 @@
"""Unit tests for OpenAI Responses API image_generation tool wiring.
The tool is a server-side Responses-API tool (``{type: "image_generation"}``);
-the result comes back as an ``image_generation_call`` output item, which Studio
+the result comes back as an ``image_generation_call`` output item, which Unsloth
translates into ``_toolEvent`` chunks so the chat adapter renders it inline.
Tests pin: the tool is added to the body only on a cloud OpenAI base when asked
for, the done event produces the expected chunks, and non-cloud bases drop it.
diff --git a/studio/backend/tests/test_openai_tool_passthrough.py b/studio/backend/tests/test_openai_tool_passthrough.py
index 8725b28ac8..161c8743c4 100644
--- a/studio/backend/tests/test_openai_tool_passthrough.py
+++ b/studio/backend/tests/test_openai_tool_passthrough.py
@@ -119,7 +119,7 @@ class TestFriendlyUpstreamError:
raw = '{"error":{"code":400,"message":"Failed to initialize samplers: failed to parse grammar","type":"invalid_request_error"}}'
msg = _friendly_upstream_error(raw)
assert "failed to parse grammar" not in msg # raw body is not surfaced verbatim
- assert "tool-calling grammar" in msg and "Update Studio" in msg
+ assert "tool-calling grammar" in msg and "Update Unsloth" in msg
def test_failed_to_initialize_samplers_alone_matches(self):
assert "tool-calling grammar" in _friendly_upstream_error("Failed to initialize samplers")
@@ -262,7 +262,7 @@ class TestChatMessageToolRoles:
def test_tool_empty_content_accepted(self):
# Empty tool output (mkdir, git add, ...) is routine in agentic loops;
- # OpenAI and llama-server both accept it, so Studio must not 400.
+ # OpenAI and llama-server both accept it, so Unsloth must not 400.
msg = ChatMessage(role = "tool", tool_call_id = "call_1", content = "")
assert msg.content == ""
@@ -400,7 +400,7 @@ class TestChatCompletionRequestToolFields:
assert req.session_id == "abc"
def test_stream_defaults_false_matching_openai_spec(self):
- # OpenAI defaults `stream` to false. Studio used to default true,
+ # OpenAI defaults `stream` to false. Unsloth used to default true,
# breaking naive curl/.NET clients (#5047) that omit it. Pin the fix.
req = self._make()
assert req.stream is False
@@ -664,7 +664,7 @@ class TestChatCompletionRequestToolFields:
raise AssertionError("client tools must use passthrough")
def generate_chat_completion_with_tools(self, **_kwargs):
- raise AssertionError("Studio tool loop must stay disabled")
+ raise AssertionError("Unsloth tool loop must stay disabled")
async def fake_passthrough(llama_backend, payload, model_name, **kwargs):
captured["body"] = inference_route._build_openai_passthrough_body(
@@ -707,11 +707,11 @@ class TestChatCompletionRequestToolFields:
assert monitor.active_count() == 0
def test_permission_mode_does_not_reject_client_tool_passthrough(self, monkeypatch):
- # A non-streaming client-tool passthrough (client tools, no Studio tool
+ # A non-streaming client-tool passthrough (client tools, no Unsloth tool
# loop) that also carries permission_mode "ask"/"auto" must reach the
# provider passthrough, not the confirm-without-stream guard: the
# validator leaves confirm_tool_calls unset for passthrough, and a bare
- # permission_mode only gates Studio's own local tool loop. An explicit
+ # permission_mode only gates Unsloth's own local tool loop. An explicit
# confirm_tool_calls=True still forces the local-confirm rejection.
# The pre-switch guard only runs when an automatic load may run, so force
# that predicate on to exercise it against a resident passthrough backend.
@@ -732,7 +732,7 @@ class TestChatCompletionRequestToolFields:
raise AssertionError("client tools must use passthrough")
def generate_chat_completion_with_tools(self, **_kwargs):
- raise AssertionError("Studio tool loop must stay disabled")
+ raise AssertionError("Unsloth tool loop must stay disabled")
async def fake_passthrough(llama_backend, payload, model_name, **kwargs):
inference_route.api_monitor.finish(kwargs.get("monitor_id"))
@@ -757,7 +757,7 @@ class TestChatCompletionRequestToolFields:
return self._v1_client(monkeypatch, _GGUFBackend())
# A process --enable-tools policy must not turn a client-tool passthrough
- # into a Studio local loop, so a policy of None or True both keep the
+ # into an Unsloth local loop, so a policy of None or True both keep the
# passthrough (the guard mirrors _explicit_studio_tool_loop_requested).
for policy in (None, True):
for mode in ("ask", "auto"):
@@ -810,7 +810,7 @@ class TestChatCompletionRequestToolFields:
assert "requires stream=true" in resp.json()["error"]["message"]
def test_permission_mode_policy_forced_local_loop_rejected_before_switch(self, monkeypatch):
- # A process --enable-tools policy forces Studio's own tool loop on even
+ # A process --enable-tools policy forces Unsloth's own tool loop on even
# when the request omits enable_tools and carries no client tools. A
# non-streaming ask/auto request is then confirm-gated with no stream to
# prompt on, so it must 400 at the pre-switch guard -- before
@@ -863,7 +863,7 @@ class TestChatCompletionRequestToolFields:
def test_enable_tools_on_non_tool_backend_keeps_client_tools_on_passthrough(self, monkeypatch):
# DiffusionGemma forces supports_tools off while passthrough stays
# available (#6851): enable_tools=True must not steal client tools
- # from the passthrough into a Studio tool loop that cannot run.
+ # from the passthrough into an Unsloth tool loop that cannot run.
import routes.inference as inference_route
captured = {}
@@ -883,7 +883,7 @@ class TestChatCompletionRequestToolFields:
raise AssertionError("client tools must use passthrough")
def generate_chat_completion_with_tools(self, **_kwargs):
- raise AssertionError("Studio tool loop cannot run on a non-tool backend")
+ raise AssertionError("Unsloth tool loop cannot run on a non-tool backend")
async def fake_passthrough(llama_backend, payload, model_name, **kwargs):
captured["body"] = inference_route._build_openai_passthrough_body(
@@ -2581,7 +2581,7 @@ class TestGgufVisionToolRouting:
raise AssertionError("plain GGUF path should not be used")
def _tools(**_kwargs):
- raise AssertionError("Studio tool loop should not steal response_format")
+ raise AssertionError("Unsloth tool loop should not steal response_format")
backend = SimpleNamespace(
is_loaded = True,
@@ -2654,7 +2654,7 @@ class TestGgufVisionToolRouting:
raise AssertionError("plain GGUF path should not be used")
def _tools(**_kwargs):
- raise AssertionError("Studio tool loop should not replace client tools")
+ raise AssertionError("Unsloth tool loop should not replace client tools")
backend = SimpleNamespace(
is_loaded = True,
@@ -2726,7 +2726,7 @@ class TestGgufVisionToolRouting:
yield "plain response"
def _tools(**_kwargs):
- raise AssertionError("tool_choice='none' must not start Studio's tool loop")
+ raise AssertionError("tool_choice='none' must not start Unsloth's tool loop")
backend = SimpleNamespace(
is_loaded = True,
@@ -2780,7 +2780,7 @@ class TestGgufVisionToolRouting:
raise AssertionError("plain GGUF path should not be used")
def _tools(**_kwargs):
- raise AssertionError("enabled_tools alone must not start Studio's tool loop")
+ raise AssertionError("enabled_tools alone must not start Unsloth's tool loop")
backend = SimpleNamespace(
is_loaded = True,
@@ -2844,7 +2844,7 @@ class TestGgufVisionToolRouting:
raise AssertionError("plain GGUF path should not be used")
def _tools(**_kwargs):
- raise AssertionError("enabled_tools alone must not start Studio's tool loop")
+ raise AssertionError("enabled_tools alone must not start Unsloth's tool loop")
backend = SimpleNamespace(
is_loaded = True,
diff --git a/studio/backend/tests/test_password_prompt_backstop.py b/studio/backend/tests/test_password_prompt_backstop.py
index 597eac1625..3c2c1956f9 100644
--- a/studio/backend/tests/test_password_prompt_backstop.py
+++ b/studio/backend/tests/test_password_prompt_backstop.py
@@ -3,7 +3,7 @@
"""Pre-tunnel terminal password gate: never publish a public Cloudflare URL
while the seeded default admin password is active. Imports run.py directly,
-so run under the Studio venv."""
+so run under the Unsloth venv."""
from __future__ import annotations
diff --git a/studio/backend/tests/test_permission_mode.py b/studio/backend/tests/test_permission_mode.py
index 3b7197fc49..4fc64a6291 100644
--- a/studio/backend/tests/test_permission_mode.py
+++ b/studio/backend/tests/test_permission_mode.py
@@ -1438,7 +1438,7 @@ def test_unknown_permission_mode_normalizes_to_ask_on_request_models():
def test_ask_auto_self_enable_confirm_on_chat_request():
# "Ask" gates every call, so a direct /chat/completions caller that requests
- # ask but omits the legacy confirm flag self-enables it when Studio's own tool
+ # ask but omits the legacy confirm flag self-enables it when Unsloth's own tool
# loop is requested. Only the router's loop-entry signals count (enable_tools /
# mcp_enabled); enabled_tools alone never starts the loop.
for loop in ({"enable_tools": True}, {"mcp_enabled": True}):
@@ -1481,7 +1481,7 @@ def test_ask_auto_self_enable_confirm_on_chat_request():
confirm_tool_calls = False,
)
assert req.confirm_tool_calls is False
- # A plain client-tool passthrough (client-supplied tools that Studio does not
+ # A plain client-tool passthrough (client-supplied tools that Unsloth does not
# execute) must NOT self-enable confirm, or the route rejects the passthrough.
req = ChatCompletionRequest(
messages = [{"role": "user", "content": "hi"}],
diff --git a/studio/backend/tests/test_providers_api.py b/studio/backend/tests/test_providers_api.py
index 5e24ed752d..7cac3a9e99 100644
--- a/studio/backend/tests/test_providers_api.py
+++ b/studio/backend/tests/test_providers_api.py
@@ -38,11 +38,11 @@ BASE_URL = os.getenv("STUDIO_TEST_URL", "http://localhost:8000")
USERNAME = os.getenv("STUDIO_TEST_USER", "unsloth")
PASSWORD = os.getenv("STUDIO_TEST_PASSWORD", "")
-# Skip the whole module when no live Studio server / bootstrap password is
+# Skip the whole module when no live Unsloth server / bootstrap password is
# available (e.g. on CI) so pytest discovery does not error out.
pytestmark = pytest.mark.skipif(
not PASSWORD,
- reason = "Integration test requires a running Studio server; set STUDIO_TEST_PASSWORD to enable.",
+ reason = "Integration test requires a running Unsloth server; set STUDIO_TEST_PASSWORD to enable.",
)
# provider_type → (env var name, model for inference test)
diff --git a/studio/backend/tests/test_rag_embed_llama_server.py b/studio/backend/tests/test_rag_embed_llama_server.py
index 0e1f74cefe..3a332ee19b 100644
--- a/studio/backend/tests/test_rag_embed_llama_server.py
+++ b/studio/backend/tests/test_rag_embed_llama_server.py
@@ -149,7 +149,7 @@ def test_build_env_gpu_inherits_devices(monkeypatch):
monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "0,1")
b = LlamaServerBackend()
env = b._build_env("/bin/llama-server", use_gpu = True)
- assert env.get("CUDA_VISIBLE_DEVICES") == "0,1" # inherit Studio's selection
+ assert env.get("CUDA_VISIBLE_DEVICES") == "0,1" # inherit Unsloth's selection
def test_use_gpu_explicit_modes(monkeypatch):
diff --git a/studio/backend/tests/test_recommended_folders_permission.py b/studio/backend/tests/test_recommended_folders_permission.py
index 33a457755e..b65695ad93 100644
--- a/studio/backend/tests/test_recommended_folders_permission.py
+++ b/studio/backend/tests/test_recommended_folders_permission.py
@@ -112,7 +112,7 @@ def test_path_under_unreadable_parent_returns_false_not_raises(tmp_path):
)
def test_demonstrates_the_underlying_stdlib_regression(tmp_path):
"""Documents *why* _safe_is_dir exists: the old bare pattern raises on
- the interpreters Studio ships on (3.12+)."""
+ the interpreters Unsloth ships on (3.12+)."""
parent = tmp_path / "ollama"
parent.mkdir()
os.chmod(parent, 0o000)
diff --git a/studio/backend/tests/test_responses_tool_passthrough.py b/studio/backend/tests/test_responses_tool_passthrough.py
index 46dd0d42e4..69715649b7 100644
--- a/studio/backend/tests/test_responses_tool_passthrough.py
+++ b/studio/backend/tests/test_responses_tool_passthrough.py
@@ -120,7 +120,7 @@ class TestResponsesRequestTools:
def test_builtin_tool_type_passes_validation(self):
"""Non-function built-in tools (web_search, file_search, mcp, ...)
must not raise at validation so SDKs that default to them don't
- fail on Studio; they're filtered out during translation."""
+ fail on Unsloth; they're filtered out during translation."""
req = ResponsesRequest(
input = "hi",
tools = [{"type": "web_search_preview"}],
diff --git a/studio/backend/tests/test_rocm_oom_guard.py b/studio/backend/tests/test_rocm_oom_guard.py
index 6e70c7cde4..699d0b74f5 100644
--- a/studio/backend/tests/test_rocm_oom_guard.py
+++ b/studio/backend/tests/test_rocm_oom_guard.py
@@ -36,7 +36,7 @@ class TestIsIntegratedSignal:
"""hipDeviceProp_t.integrated wins when truthy; 0/absent never downgrades.
Same universal gate PR #5988's UMA safetensors fast-load uses -- keeps
- Studio's two unified-memory consumers on one signal."""
+ Unsloth's two unified-memory consumers on one signal."""
def test_integrated_upgrades_unknown_apu(self) -> None:
# gfx1103 Phoenix iGPU: outside the hardcoded arch set, but the
diff --git a/studio/backend/tests/test_safetensors_tool_loop.py b/studio/backend/tests/test_safetensors_tool_loop.py
index 915f82ac8e..31c728afca 100644
--- a/studio/backend/tests/test_safetensors_tool_loop.py
+++ b/studio/backend/tests/test_safetensors_tool_loop.py
@@ -2230,8 +2230,8 @@ def _reprompt_loop(*, auto_heal_tool_calls):
tools = [{"type": "function", "function": {"name": "search_knowledge_base"}}],
execute_tool = exec_fn,
auto_heal_tool_calls = auto_heal_tool_calls,
- # Studio always nudges (always-on for the Studio inference paths); the
- # API opts in per request. Model the Studio caller here.
+ # Unsloth always nudges (always-on for the Unsloth inference paths); the
+ # API opts in per request. Model the Unsloth caller here.
nudge_tool_calls = True,
max_tool_iterations = 3,
)
@@ -3203,7 +3203,7 @@ class TestLoopBehaviour:
class TestLoopRePrompt:
- """Plan-without-action re-prompt parity with GGUF: nudge instead of terminating, up to ``MAX_ACT_REPROMPTS`` extra slots. Studio always nudges, so these drive the loop with ``nudge_tool_calls=True``."""
+ """Plan-without-action re-prompt parity with GGUF: nudge instead of terminating, up to ``MAX_ACT_REPROMPTS`` extra slots. Unsloth always nudges, so these drive the loop with ``nudge_tool_calls=True``."""
def test_reasoning_intent_does_not_reprompt_a_visible_answer(self):
generations = 0
@@ -4258,7 +4258,7 @@ class TestPlanWithoutActionReprompt:
def test_omitted_nudge_flag_is_not_reprompted(self):
# The retry is new on this loop: API callers who do not send the flag
- # must keep today's behavior. Studio opts in explicitly.
+ # must keep today's behavior. Unsloth opts in explicitly.
loop, exec_fn = _make_loop(
turns = [
["I'll search the web for that."],
diff --git a/studio/backend/tests/test_secure_tunnel_gate.py b/studio/backend/tests/test_secure_tunnel_gate.py
index 2c13e13bbb..a8c0c2305f 100644
--- a/studio/backend/tests/test_secure_tunnel_gate.py
+++ b/studio/backend/tests/test_secure_tunnel_gate.py
@@ -2,7 +2,7 @@
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Cloudflare tunnel start gate, incl. --secure on loopback. Imports run.py
-directly, so run under the Studio venv."""
+directly, so run under the Unsloth venv."""
from __future__ import annotations
diff --git a/studio/backend/tests/test_server_disk_logging.py b/studio/backend/tests/test_server_disk_logging.py
index 05d03d869c..ce733c2aaa 100644
--- a/studio/backend/tests/test_server_disk_logging.py
+++ b/studio/backend/tests/test_server_disk_logging.py
@@ -3,7 +3,7 @@
"""Tests for the server session log + native-crash capture in run.py.
-Field regression: Studio "terminates without a warning" -- a native crash in
+Field regression: Unsloth "terminates without a warning" -- a native crash in
the GPU runtime kills the process with no Python traceback, and a desktop-
shortcut console closes before anything can be read. The server must tee its
console output to disk and aim faulthandler at the same file so even hard
diff --git a/studio/backend/tests/test_slot_offload_fit.py b/studio/backend/tests/test_slot_offload_fit.py
index ac606e4627..d354c7e113 100644
--- a/studio/backend/tests/test_slot_offload_fit.py
+++ b/studio/backend/tests/test_slot_offload_fit.py
@@ -3,7 +3,7 @@
"""Tests for the offload-avoidance serving-slot reduction (`_slots_that_fit_on_gpu`).
-When a pinned context does not fit at the requested `--parallel` slot count, Studio would
+When a pinned context does not fit at the requested `--parallel` slot count, Unsloth would
flip to `--fit on` and llama-server offloads layers to host RAM, collapsing decode ~3x
(oobabooga #6718). Instead the loader retries the on-GPU fit at fewer slots and keeps the
largest count that stays fully on GPU (`-ngl -1`). These tests drive the real helper with
diff --git a/studio/backend/tests/test_studio_api.py b/studio/backend/tests/test_studio_api.py
index 928b636e3e..087c00b648 100644
--- a/studio/backend/tests/test_studio_api.py
+++ b/studio/backend/tests/test_studio_api.py
@@ -11,7 +11,7 @@ the CLI's ``--help`` output:
1. curl -- basic chat completions (non-streaming)
2. curl -- streaming chat completions
3. Python OpenAI SDK -- streaming completions
- 4. curl -- Studio server-side tools (enable_tools=true)
+ 4. curl -- Unsloth server-side tools (enable_tools=true)
5. curl -- Standard OpenAI function calling (non-streaming)
6. curl -- Standard OpenAI function calling (streaming)
7. curl -- Standard OpenAI function calling (multi-turn tool loop)
@@ -31,7 +31,7 @@ Usage:
python tests/test_studio_api.py
python tests/test_studio_api.py --model unsloth/... --gguf-variant ...
- # Pytest mode, external server — start a Studio server yourself,
+ # Pytest mode, external server — start an Unsloth server yourself,
# then point pytest at it. Fastest iteration loop.
unsloth studio run --model unsloth/Qwen3-1.7B-GGUF --gguf-variant UD-Q4_K_XL &
export UNSLOTH_E2E_BASE_URL=http://127.0.0.1:8080
@@ -341,7 +341,7 @@ def _final_finish_reason(chunks: list[dict]) -> str | None:
def test_openai_tools_nonstream(base_url: str, api_key: str):
"""Standard OpenAI function calling, non-streaming, tool_choice='required'.
- Regression: before the fix, Studio stripped `tools` and the model
+ Regression: before the fix, Unsloth stripped `tools` and the model
returned plain text with finish_reason='stop'. After the fix,
llama-server's response is forwarded verbatim so the client sees
finish_reason='tool_calls' with a structured tool_calls array and
diff --git a/studio/backend/tests/test_tensor_parallel.py b/studio/backend/tests/test_tensor_parallel.py
index 0d71b89d87..06f72d3b9f 100644
--- a/studio/backend/tests/test_tensor_parallel.py
+++ b/studio/backend/tests/test_tensor_parallel.py
@@ -420,7 +420,7 @@ def test_runtime_recovery_fires_for_user_env_mtp(monkeypatch):
# MTP driven by user extra_args / LLAMA_ARG_SPEC_TYPE leaves _speculative_type
# unset, but the launch flag still gates recovery on (pass-through MTP).
b = _recovery_backend()
- b._speculative_type = None # Studio stepped back; user/env owns the spec
+ b._speculative_type = None # Unsloth stepped back; user/env owns the spec
done = threading.Event()
captured = {}
diff --git a/studio/backend/tests/test_tool_confirm_stream.py b/studio/backend/tests/test_tool_confirm_stream.py
index b8e0472e12..0813f6b68d 100644
--- a/studio/backend/tests/test_tool_confirm_stream.py
+++ b/studio/backend/tests/test_tool_confirm_stream.py
@@ -3,12 +3,12 @@
"""End-to-end handshake test for the tool-confirmation gate, no model.
-The real Studio stream wrappers in ``routes/inference.py`` drive the
+The real Unsloth stream wrappers in ``routes/inference.py`` drive the
synchronous agentic generator with ``await asyncio.to_thread(next, gen,
...)`` so the blocking ``threading.Event`` wait runs off the event loop.
This test rebuilds that exact pattern around the real
``state.tool_approvals`` functions, served by a real uvicorn process on
-loopback (the same server Studio uses), and proves the load-bearing
+loopback (the same server Unsloth uses), and proves the load-bearing
property:
* ``tool_start`` reaches the client before the gate blocks, and
diff --git a/studio/backend/tests/test_tool_message_empty_content.py b/studio/backend/tests/test_tool_message_empty_content.py
index d63b16ce80..636a35f5a9 100644
--- a/studio/backend/tests/test_tool_message_empty_content.py
+++ b/studio/backend/tests/test_tool_message_empty_content.py
@@ -4,7 +4,7 @@
"""Empty ``role="tool"`` content must be accepted on the OpenAI-compat surface.
Agentic clients send ``content: ""`` when a command produced no output;
-OpenAI and llama-server both accept it. Studio used to 400, which standard
+OpenAI and llama-server both accept it. Unsloth used to 400, which standard
clients treat as non-retryable and kill the session. The validator must
normalize empty/missing tool content to ``""`` instead of raising.
"""
diff --git a/studio/backend/tests/test_tp_vision_regression.py b/studio/backend/tests/test_tp_vision_regression.py
index 09af876da6..fb0989b306 100644
--- a/studio/backend/tests/test_tp_vision_regression.py
+++ b/studio/backend/tests/test_tp_vision_regression.py
@@ -625,7 +625,7 @@ def _fallback_loaded_backend(layer_preserves_tensor_intent: bool) -> LlamaCppBac
def test_tensor_off_echo_preserves_multi_gpu_fallback():
- """The Studio UI always sends tensor_parallel and echoes the /load response's
+ """The Unsloth UI always sends tensor_parallel and echoes the /load response's
resolved value, so after a fallback a ctx/settings reload carries tensor_parallel=
false even though the user never changed it. That echo must NOT collapse the
preserved multi-GPU placement -- it dedupes (Codex #6659)."""
diff --git a/studio/backend/tests/test_trained_model_scan.py b/studio/backend/tests/test_trained_model_scan.py
index 7bf572e214..5d74bb7d28 100644
--- a/studio/backend/tests/test_trained_model_scan.py
+++ b/studio/backend/tests/test_trained_model_scan.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Tests for Studio trained-model discovery used by Chat."""
+"""Tests for Unsloth trained-model discovery used by Chat."""
import json
from pathlib import Path
diff --git a/studio/backend/tests/test_training_nan_loss_handling.py b/studio/backend/tests/test_training_nan_loss_handling.py
index a2dc78bee2..5a477a084d 100644
--- a/studio/backend/tests/test_training_nan_loss_handling.py
+++ b/studio/backend/tests/test_training_nan_loss_handling.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-"""Pin Studio's behavior when a training event reports non-finite (NaN/Inf) loss.
+"""Pin Unsloth's behavior when a training event reports non-finite (NaN/Inf) loss.
The training event handler used to filter NaN/Inf to None silently while
leaving the previous finite loss in progress.loss — so the API kept reporting
diff --git a/studio/backend/tests/test_transformers_latest.py b/studio/backend/tests/test_transformers_latest.py
index 20616dccba..af48d674cc 100644
--- a/studio/backend/tests/test_transformers_latest.py
+++ b/studio/backend/tests/test_transformers_latest.py
@@ -1036,7 +1036,7 @@ def test_upgrade_check_mixed_pypi_main_reports_dev_only(monkeypatch):
def test_install_endpoint_not_mounted_on_v1():
- """The consented pip-install endpoint is a Studio admin action; it must live
+ """The consented pip-install endpoint is an Unsloth admin action; it must live
on studio_router (kept off the OpenAI-compatible /v1 mount), not router."""
from routes import inference as ri
diff --git a/studio/backend/utils/_studio_release_build.py b/studio/backend/utils/_studio_release_build.py
index 267197a202..07ede36912 100644
--- a/studio/backend/utils/_studio_release_build.py
+++ b/studio/backend/utils/_studio_release_build.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Build-stamped Studio release metadata.
+"""Build-stamped Unsloth release metadata.
Release builds may rewrite this module in the build workspace before creating
Python artifacts. Keep the committed value neutral so source checkouts do not
diff --git a/studio/backend/utils/api_errors.py b/studio/backend/utils/api_errors.py
index cae8daf287..a3686c3a26 100644
--- a/studio/backend/utils/api_errors.py
+++ b/studio/backend/utils/api_errors.py
@@ -20,7 +20,7 @@ client-error responses on the ``/v1/*`` surface:
CRITICAL: the exception handlers installed by :func:`install_api_error_handlers`
are global, but they ONLY transform responses for paths that start with ``/v1/``.
For every other path (``/api/...``, frontend routes) they reproduce FastAPI's
-default behavior byte-for-byte, because the Studio frontend depends on the
+default behavior byte-for-byte, because the Unsloth frontend depends on the
``{"detail": ...}`` shape for ``/api/*``.
Public contract (other modules depend on these):
@@ -107,7 +107,7 @@ def anthropic_error_body(
Returns ``{"type": "error", "request_id": None, "error": {"type", "message"}}``.
``request_id`` is a required (nullable) field on the spec's ErrorResponse;
- Studio has no request-id system, so it is null. ``err_type`` defaults to
+ Unsloth has no request-id system, so it is null. ``err_type`` defaults to
:data:`ANTHROPIC_TYPE_BY_STATUS` for ``status`` (``"api_error"`` fallback).
"""
return {
@@ -192,7 +192,7 @@ def install_api_error_handlers(app) -> None:
Both handlers are global but only transform responses for OpenAI/Anthropic-
compatible surfaces (see :func:`wants_api_error_envelope`: the ``/v1/*`` mount
and the preview ``/p/.../v1/*`` mount). Every other path reproduces FastAPI's
- default ``{"detail": ...}`` behavior exactly so the Studio frontend keeps working.
+ default ``{"detail": ...}`` behavior exactly so the Unsloth frontend keeps working.
"""
@app.exception_handler(RequestValidationError)
diff --git a/studio/backend/utils/client_ip.py b/studio/backend/utils/client_ip.py
index 94acbf1809..cc48a096d2 100644
--- a/studio/backend/utils/client_ip.py
+++ b/studio/backend/utils/client_ip.py
@@ -4,12 +4,12 @@
"""Resolve the caller's IP for rate limiting.
Trust model, in order:
- 1. If the operator opts in via ``UNSLOTH_STUDIO_TRUST_FORWARDED`` (Studio behind
+ 1. If the operator opts in via ``UNSLOTH_STUDIO_TRUST_FORWARDED`` (Unsloth behind
their own reverse proxy), honor the *rightmost* ``X-Forwarded-For`` hop -- the
one the trusted proxy appended. The leftmost entry is client-controlled and
spoofable, so this assumes a proxy that appends (or overwrites) the header;
only enable the env var behind such a proxy.
- 2. If the socket peer is loopback, honor ``CF-Connecting-IP``. Studio's managed
+ 2. If the socket peer is loopback, honor ``CF-Connecting-IP``. Unsloth's managed
Cloudflare tunnel terminates at 127.0.0.1, so every tunneled visitor would
otherwise collapse onto the same socket peer (the local cloudflared process)
and share one rate-limit bucket. ``CF-Connecting-IP`` is set by Cloudflare's
diff --git a/studio/backend/utils/cpu_threads.py b/studio/backend/utils/cpu_threads.py
index 4ed0021054..91d577408d 100644
--- a/studio/backend/utils/cpu_threads.py
+++ b/studio/backend/utils/cpu_threads.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Early CPU thread-pool configuration for Studio processes."""
+"""Early CPU thread-pool configuration for Unsloth processes."""
import os
from typing import MutableMapping, Optional
diff --git a/studio/backend/utils/datasets/cache_safe.py b/studio/backend/utils/datasets/cache_safe.py
index e629210f33..d2dc7b737a 100644
--- a/studio/backend/utils/datasets/cache_safe.py
+++ b/studio/backend/utils/datasets/cache_safe.py
@@ -7,7 +7,7 @@ A shared HF datasets cache can contain subtrees owned by another user (for
example populated by an earlier root-run job). datasets then raises
"[Errno 13] Permission denied: ..._builder.lock" while locking the cached
builder, killing the training run even though the dataset itself is fine.
-Retry such loads in a Studio-owned cache so the run proceeds; the worst case
+Retry such loads in an Unsloth-owned cache so the run proceeds; the worst case
is one rebuild of the dataset in the fallback location.
"""
@@ -26,7 +26,7 @@ def studio_datasets_cache() -> str:
def load_dataset_cache_safe(*args, **kwargs):
- """datasets.load_dataset, retried in a Studio-owned cache on EACCES."""
+ """datasets.load_dataset, retried in an Unsloth-owned cache on EACCES."""
from datasets import load_dataset
try:
return load_dataset(*args, **kwargs)
diff --git a/studio/backend/utils/hardware/VRAM_ESTIMATION.md b/studio/backend/utils/hardware/VRAM_ESTIMATION.md
index a6b4de29d2..68ca1d5ffd 100644
--- a/studio/backend/utils/hardware/VRAM_ESTIMATION.md
+++ b/studio/backend/utils/hardware/VRAM_ESTIMATION.md
@@ -106,7 +106,7 @@ Non_flash_attention = B * num_attention_heads * S^2 * 2 * 12.0 * effective_layer
Activations = max(Per_layer_with_gc, Non_flash_attention)
```
-Studio resolves the attention implementation with Unsloth's
+Unsloth resolves the attention implementation with Unsloth's
`resolve_attention_implementation` helper and uses that result directly. The
estimator does not duplicate model-family attention policy.
diff --git a/studio/backend/utils/hardware/amd.py b/studio/backend/utils/hardware/amd.py
index f5b64c45d0..91a06c9a2a 100644
--- a/studio/backend/utils/hardware/amd.py
+++ b/studio/backend/utils/hardware/amd.py
@@ -125,7 +125,7 @@ def _run_amd_smi(*args: str, timeout: int = _AMD_SMI_DEFAULT_TIMEOUT) -> Optiona
# amd-smi does not exist on Windows (neither Adrenalin nor the HIP SDK
# ship a CLI) and can be absent on minimal Linux installs. Disable the
# poller in one step instead of burning the 3-strike circuit breaker
- # on guaranteed FileNotFoundError spawns. Studio's VRAM display falls
+ # on guaranteed FileNotFoundError spawns. Unsloth's VRAM display falls
# back to torch mem_get_info.
if not _amd_smi_disabled:
logger.info(
diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py
index 117ad7b780..adc9a54aab 100644
--- a/studio/backend/utils/hardware/hardware.py
+++ b/studio/backend/utils/hardware/hardware.py
@@ -37,7 +37,7 @@ logger = get_logger(__name__)
# ── GPU index ordering ──────────────────────────────────────────────────────
# CUDA defaults to CUDA_DEVICE_ORDER=FASTEST_FIRST, numbering GPUs by compute
-# performance. nvidia-smi -- and every free-VRAM probe in Studio -- numbers GPUs
+# performance. nvidia-smi -- and every free-VRAM probe in Unsloth -- numbers GPUs
# by PCI bus id instead. On a mixed-GPU host (e.g. an RTX 5090 alongside an RTX
# PRO 6000) the two orderings disagree, so an index picked from nvidia-smi data
# ("the emptiest card is GPU 1") gets written into CUDA_VISIBLE_DEVICES and then
@@ -49,7 +49,7 @@ logger = get_logger(__name__)
# and spawn workers copy os.environ. setdefault so an explicit user override wins.
os.environ.setdefault("CUDA_DEVICE_ORDER", "PCI_BUS_ID")
-# Studio workers can import MLX without importing unsloth first, so mirror the
+# Unsloth workers can import MLX without importing unsloth first, so mirror the
# package bootstrap here. Keep an explicit user value authoritative.
if platform.system() == "Darwin" and platform.machine() == "arm64":
os.environ.setdefault("AGX_RELAX_CDM_CTXSTORE_TIMEOUT", "1")
@@ -117,7 +117,7 @@ def _has_mlx() -> bool:
def _has_usable_mlx_stack() -> bool:
- """True only when the FULL Studio MLX training/export stack is usable
+ """True only when the FULL Unsloth MLX training/export stack is usable
(mlx + mlx-lm + mlx-vlm at the minimum versions unsloth-zoo requires), not
just a bare ``import mlx.core``. A backtracked/old mlx-vlm still imports but
breaks VLM Train/Export, so the training gate must match the self-heal's own
diff --git a/studio/backend/utils/helper_precache_settings.py b/studio/backend/utils/helper_precache_settings.py
index db19a2d028..e7d3c0e6dd 100644
--- a/studio/backend/utils/helper_precache_settings.py
+++ b/studio/backend/utils/helper_precache_settings.py
@@ -32,7 +32,7 @@ def helper_model_disabled_by_env() -> bool:
def get_helper_precache_enabled() -> bool:
"""Read the persisted startup pre-cache preference.
- Missing or unreadable settings default to False so Studio startup never
+ Missing or unreadable settings default to False so Unsloth startup never
performs optional network work unless the user explicitly opted in.
"""
try:
@@ -45,7 +45,7 @@ def get_helper_precache_enabled() -> bool:
def set_helper_precache_enabled(value: Any) -> bool:
- """Persist whether Studio should pre-cache the Helper LLM at startup."""
+ """Persist whether Unsloth should pre-cache the Helper LLM at startup."""
parsed = _coerce_bool(value)
if parsed is None:
raise ValueError("Helper LLM startup pre-cache must be true or false.")
diff --git a/studio/backend/utils/hf_xet_fallback.py b/studio/backend/utils/hf_xet_fallback.py
index 9bc4a60fad..2628b99a2d 100644
--- a/studio/backend/utils/hf_xet_fallback.py
+++ b/studio/backend/utils/hf_xet_fallback.py
@@ -1,9 +1,9 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Studio shim over the shared ``unsloth_zoo.hf_xet_fallback`` Xet -> HTTP stall fallback.
+"""Unsloth shim over the shared ``unsloth_zoo.hf_xet_fallback`` Xet -> HTTP stall fallback.
-Re-exports the shared API and injects Studio's marker-aware cache purge
+Re-exports the shared API and injects Unsloth's marker-aware cache purge
(``prepare_cache_for_transport``) so the download manager keeps its ``.transport``
marker semantics on the HTTP retry.
@@ -68,7 +68,7 @@ def _load_shared() -> bool:
_shared_available = True
_shared_import_error = None
return True
- except Exception as exc2: # noqa: BLE001 - degrade so Studio still boots with plain HF
+ except Exception as exc2: # noqa: BLE001 - degrade so Unsloth still boots with plain HF
_shared_import_error = exc2
_shared_available = False
import logging as _logging
@@ -263,7 +263,7 @@ __all__ = [
def _studio_prepare_for_http(repo_type: str, repo_id: str) -> None:
- """Studio's marker-aware purge before an HTTP resume, keeping the download manager's ``.transport``
+ """Unsloth's marker-aware purge before an HTTP resume, keeping the download manager's ``.transport``
accounting consistent (vs unsloth_zoo's generic default). Guarded: a purge failure is logged,
not fatal to the retry."""
try:
@@ -273,7 +273,7 @@ def _studio_prepare_for_http(repo_type: str, repo_id: str) -> None:
try:
from loggers import get_logger
get_logger(__name__).debug(
- "Studio prepare_cache_for_transport failed for %s: %s", repo_id, exc
+ "Unsloth prepare_cache_for_transport failed for %s: %s", repo_id, exc
)
except ModuleNotFoundError as logger_exc:
if logger_exc.name != "loggers":
@@ -294,8 +294,8 @@ def hf_hub_download_with_xet_fallback(
on_status: Optional[Callable[[str], None]] = None,
force_download: bool = False,
) -> str:
- """Single-file download via the shared fallback with Studio's marker-aware HTTP-retry prep.
- ``force_download`` re-fetches a newer blob over a cached one (Studio's model-update path)."""
+ """Single-file download via the shared fallback with Unsloth's marker-aware HTTP-retry prep.
+ ``force_download`` re-fetches a newer blob over a cached one (Unsloth's model-update path)."""
return _shared_hf_hub_download_with_xet_fallback(
repo_id,
filename,
@@ -313,6 +313,6 @@ def hf_hub_download_with_xet_fallback(
def snapshot_download_with_xet_fallback(repo_id: str, **kwargs: Any) -> str:
- """Whole-repo download via the shared fallback with Studio's marker-aware HTTP-retry prep."""
+ """Whole-repo download via the shared fallback with Unsloth's marker-aware HTTP-retry prep."""
kwargs.setdefault("prepare_for_http_fn", _studio_prepare_for_http)
return _shared_snapshot_download_with_xet_fallback(repo_id, **kwargs)
diff --git a/studio/backend/utils/host_policy.py b/studio/backend/utils/host_policy.py
index f506eadc03..55565bb338 100644
--- a/studio/backend/utils/host_policy.py
+++ b/studio/backend/utils/host_policy.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Bind-host trust policy for the Studio backend.
+"""Bind-host trust policy for the Unsloth backend.
Stdlib only -- safe to import without the rest of the backend.
diff --git a/studio/backend/utils/llama_cpp_update.py b/studio/backend/utils/llama_cpp_update.py
index f6d3635301..31dbda63ea 100644
--- a/studio/backend/utils/llama_cpp_update.py
+++ b/studio/backend/utils/llama_cpp_update.py
@@ -82,7 +82,7 @@ def _utcnow() -> str:
def _find_binary() -> Optional[str]:
"""Locate the active llama-server binary via the inference backend's own
- resolver, so update targets exactly what Studio runs. Lazy import keeps the
+ resolver, so update targets exactly what Unsloth runs. Lazy import keeps the
heavy inference module off this module's import path."""
try:
from core.inference.llama_cpp import LlamaCppBackend
@@ -109,7 +109,7 @@ def _installer_script() -> Optional[Path]:
"""Locate install_llama_prebuilt.py. Honours UNSLOTH_LLAMA_INSTALLER, then
searches up from this file for both ``/install_llama_prebuilt.py`` and
``/studio/install_llama_prebuilt.py`` so it works in the dev tree and
- in an installed Studio layout."""
+ in an installed Unsloth layout."""
env = os.environ.get("UNSLOTH_LLAMA_INSTALLER")
if env and Path(env).is_file():
return Path(env)
@@ -227,7 +227,7 @@ def _is_under(path: Path, root: Path) -> bool:
def _llama_install_root(binary: Optional[str]) -> Optional[Path]:
- """The Studio-managed llama.cpp root the active binary lives under, or None
+ """The Unsloth-managed llama.cpp root the active binary lives under, or None
when the binary is unmanaged. Installing anywhere the active binary is not
would not replace what _find_llama_server_binary runs (which prefers a pinned
LLAMA_SERVER_PATH, then UNSLOTH_LLAMA_CPP_PATH, then a llama.cpp tree), so we
@@ -327,7 +327,7 @@ def _source_build_status(binary: str, *, force_refresh: bool) -> Optional[dict]:
def _is_external_link(path: Optional[Path]) -> bool:
"""True when ``path`` is a --with-llama-cpp-dir local link: a POSIX symlink
or a Windows directory junction / reparse point. Such a link resolves into
- the user's own llama.cpp checkout, so Studio must never auto-update it."""
+ the user's own llama.cpp checkout, so Unsloth must never auto-update it."""
if path is None:
return False
try:
@@ -635,7 +635,7 @@ def start_update() -> dict:
"reason": "local_link",
"message": (
"llama.cpp is a local directory linked with --with-llama-cpp-dir; "
- "Studio won't replace it. Update your own llama.cpp checkout instead."
+ "Unsloth won't replace it. Update your own llama.cpp checkout instead."
),
"job": get_update_status()["job"],
}
diff --git a/studio/backend/utils/mlx_repair.py b/studio/backend/utils/mlx_repair.py
index 7e1c9864c9..4ea1ec62f5 100644
--- a/studio/backend/utils/mlx_repair.py
+++ b/studio/backend/utils/mlx_repair.py
@@ -3,7 +3,7 @@
"""Best-effort MLX self-heal for Apple Silicon.
-On macOS, Studio enables Train/Export only when the MLX training/export stack is
+On macOS, Unsloth enables Train/Export only when the MLX training/export stack is
usable (see utils.hardware.hardware.detect_hardware -> CHAT_ONLY). MLX is pulled
only transitively via unsloth-zoo, and a resolver backtrack (mlx-vlm ->
transformers>=5 vs the single-env transformers pin) can silently drop it, leaving
@@ -13,7 +13,7 @@ a background thread, then re-detects so the gate re-opens without a manual
The install mirrors the main Apple Silicon installer (install_python_stack.py):
it points UV_OVERRIDE at overrides-darwin-arm64.txt so the resolver keeps the
-Studio transformers pin AND installs a current mlx-vlm, and it requires the same
+Unsloth transformers pin AND installs a current mlx-vlm, and it requires the same
minimum versions unsloth-zoo declares so a backtracked old mlx-vlm (which still
imports but breaks VLM Train/Export) is never accepted as healthy.
@@ -69,11 +69,11 @@ _MLX_REINSTALL_ARGS = tuple(
# reject anything. mlx/mlx-metal ship wheels only (no sdist on PyPI) and
# mlx-lm/mlx-vlm publish py3-none-any wheels, so requiring wheels does not break a
# healthy self-heal; if a wheel is genuinely unavailable the install fails and
-# Studio stays chat-only (the existing safe fallback) until `unsloth studio update`.
+# Unsloth stays chat-only (the existing safe fallback) until `unsloth studio update`.
_ONLY_BINARY_ARG = "--only-binary=:all:"
# Allowlist of environment variables forwarded to the install subprocess. The
# self-heal runs without confirmation on the default startup path, so it must not
-# hand resolver/build code the full Studio environment. Everything outside this
+# hand resolver/build code the full Unsloth environment. Everything outside this
# set is dropped, which excludes three dangerous classes by construction:
# * secrets (HF_TOKEN, AWS_*, WANDB_API_KEY, ...) that a malicious wheel/sdist
# build hook would otherwise read straight out of os.environ;
@@ -207,13 +207,13 @@ def _mlx_install_env() -> dict[str, str]:
The self-heal runs without confirmation on the default startup path, so it
forwards only the variables uv genuinely needs (see _MLX_ENV_ALLOWLIST) instead
- of the full Studio environment: secrets and package-source redirects in
+ of the full Unsloth environment: secrets and package-source redirects in
os.environ are dropped so a malicious resolver-selected artifact cannot read
- Studio secrets or be steered to a hostile index.
+ Unsloth secrets or be steered to a hostile index.
Mirror the main installer (install_python_stack.py) by pointing UV_OVERRIDE at
overrides-darwin-arm64.txt, which relaxes mlx-vlm/mlx-lm's transformers>=5
- requirement to >=4.57.6. Without it, uv keeps the Studio transformers pin only
+ requirement to >=4.57.6. Without it, uv keeps the Unsloth transformers pin only
by silently backtracking mlx-vlm to an old, unsupported version (uv honours
UV_OVERRIDE; plain pip ignores it, so the transformers constraint below is the
pip-path safety net). We set UV_OVERRIDE ourselves, so a poisoned one in the
@@ -234,17 +234,17 @@ def _mlx_install_env() -> dict[str, str]:
def _transformers_constraint_args() -> tuple[list[str], str | None]:
"""Pin transformers to the running version for the mlx install.
- The install must never upgrade transformers underneath a running Studio
+ The install must never upgrade transformers underneath a running Unsloth
(the single-env install pins transformers==4.57.6). With UV_OVERRIDE set this
is belt-and-suspenders; on the plain-pip path (no UV_OVERRIDE support) it is
the actual guard -- the resolver either finds an mlx build compatible with the
- pin or fails, leaving us chat-only rather than breaking Studio. Returns
+ pin or fails, leaving us chat-only rather than breaking Unsloth. Returns
(pip args, temp file path to clean up).
Read the version from installed metadata rather than `import transformers`:
transformers can have valid metadata yet fail to import (e.g. an incompatible
huggingface_hub), and in that case we still want to pin it so the mlx install
- cannot quietly upgrade it out from under Studio."""
+ cannot quietly upgrade it out from under Unsloth."""
from importlib.metadata import PackageNotFoundError, version as _dist_version
try:
@@ -263,10 +263,10 @@ def attempt_mlx_repair(*, timeout: int = _REPAIR_TIMEOUT_S) -> bool:
"""Install a usable mlx/mlx-lm/mlx-vlm stack by name into the running venv.
Best-effort; returns True iff the resulting stack meets unsloth-zoo's minimums
(so a backtracked old mlx-vlm is rejected, not accepted). transformers is held
- at its pinned version so the install can never upgrade it underneath Studio."""
+ at its pinned version so the install can never upgrade it underneath Unsloth."""
# Prepare the constraint inside the try: this runs on a daemon thread, so an
# exception here (e.g. tempfile.mkstemp failing on a full disk or bad TMPDIR)
- # must leave Studio chat-only, not crash the background self-heal thread.
+ # must leave Unsloth chat-only, not crash the background self-heal thread.
constraint_path = None
try:
constraint_args, constraint_path = _transformers_constraint_args()
@@ -279,7 +279,7 @@ def attempt_mlx_repair(*, timeout: int = _REPAIR_TIMEOUT_S) -> bool:
)
if cmd is None:
logger.warning(
- "MLX self-heal requires uv so Studio can apply dependency overrides; "
+ "MLX self-heal requires uv so Unsloth can apply dependency overrides; "
"staying chat-only. Run `unsloth studio update` to restore uv."
)
return False
diff --git a/studio/backend/utils/models/checkpoints.py b/studio/backend/utils/models/checkpoints.py
index b6b080b1c4..f2125ad034 100644
--- a/studio/backend/utils/models/checkpoints.py
+++ b/studio/backend/utils/models/checkpoints.py
@@ -37,7 +37,7 @@ def _checkpoint_sort_key(checkpoint_path: Path) -> tuple[int, int, str]:
def _infer_base_model_from_history(checkpoint_dir: Path) -> Optional[str]:
- """Best-effort base-model lookup using persisted Studio run metadata."""
+ """Best-effort base-model lookup using persisted Unsloth run metadata."""
checkpoint_name = checkpoint_dir.name
resolved_checkpoint_dir = str(checkpoint_dir.resolve())
diff --git a/studio/backend/utils/models/model_config.py b/studio/backend/utils/models/model_config.py
index 284bbb5745..dadf103cea 100644
--- a/studio/backend/utils/models/model_config.py
+++ b/studio/backend/utils/models/model_config.py
@@ -2083,7 +2083,7 @@ def _has_model_weight_files(model_dir: Path) -> bool:
def _detect_training_output_type(model_dir: Path) -> Optional[str]:
- """Classify a Studio training output as LoRA or full finetune."""
+ """Classify an Unsloth training output as LoRA or full finetune."""
adapter_config = model_dir / "adapter_config.json"
adapter_model = model_dir / "adapter_model.safetensors"
if adapter_config.exists() or adapter_model.exists():
@@ -2105,7 +2105,7 @@ def _looks_like_lora_adapter(model_dir: Path) -> bool:
def scan_trained_models(outputs_dir: str = str(outputs_root())) -> List[Tuple[str, str, str]]:
- """Scan outputs folder for trained Studio models.
+ """Scan outputs folder for trained Unsloth models.
Returns:
List of (display_name, model_path, model_type), where model_type is
diff --git a/studio/backend/utils/paths/storage_roots.py b/studio/backend/utils/paths/storage_roots.py
index 759681da3f..1faa2b1281 100644
--- a/studio/backend/utils/paths/storage_roots.py
+++ b/studio/backend/utils/paths/storage_roots.py
@@ -36,7 +36,7 @@ def _infer_studio_home_from_venv() -> Path | None:
def studio_root() -> Path:
- """Studio install root.
+ """Unsloth install root.
Priority: UNSLOTH_STUDIO_HOME, then STUDIO_HOME alias, then sys.prefix
inference, then legacy ~/.unsloth/studio. UNSLOTH_STUDIO_HOME wins if
@@ -62,7 +62,7 @@ def cache_root() -> Path:
def studio_bin_root() -> Path:
- """Dir for Studio-managed executables (the `unsloth` shim, downloaded tools like cloudflared)."""
+ """Dir for Unsloth-managed executables (the `unsloth` shim, downloaded tools like cloudflared)."""
return studio_root() / "bin"
@@ -443,7 +443,7 @@ def resolve_export_write_dir(path_value: str | None = None) -> Path:
Unlike :func:`resolve_export_dir`, this function passes absolute
paths through as-is so users can target a different drive when
- their Studio install lives on a constrained system volume
+ their Unsloth install lives on a constrained system volume
(see :gh-issue:`6082`). Used only by the export write path.
"""
if not path_value or not str(path_value).strip():
diff --git a/studio/backend/utils/preview_rate_limit.py b/studio/backend/utils/preview_rate_limit.py
index dd38cfd5e7..c59a1bf5b3 100644
--- a/studio/backend/utils/preview_rate_limit.py
+++ b/studio/backend/utils/preview_rate_limit.py
@@ -5,7 +5,7 @@
A signed link stops ref guessing, but anyone with a link can still drive GPU
generation. This bounds sustained abuse from a single source. In-process and
-single-worker only (like the login limiter in ``routes/auth.py``); Studio runs as
+single-worker only (like the login limiter in ``routes/auth.py``); Unsloth runs as
one uvicorn process, so a shared store isn't needed.
"""
diff --git a/studio/backend/utils/process_lifetime.py b/studio/backend/utils/process_lifetime.py
index 3ffd54cc26..c63227ae86 100644
--- a/studio/backend/utils/process_lifetime.py
+++ b/studio/backend/utils/process_lifetime.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Bind Studio child processes to the parent's lifetime so none survive an
+"""Bind Unsloth child processes to the parent's lifetime so none survive an
abnormal parent exit (terminal-window close, Task Manager "End Task", SIGKILL,
crash) -- the cooperative shutdown path only runs on graceful exits.
@@ -139,7 +139,7 @@ def _install_windows_job() -> None:
kernel32.CloseHandle(job)
return
# AssignProcessToJobObject(parent) makes children inherit the job. May
- # fail if Studio already runs inside an incompatible host job (pre-Win8);
+ # fail if Unsloth already runs inside an incompatible host job (pre-Win8);
# degrade to the cooperative path rather than blocking startup.
if not kernel32.AssignProcessToJobObject(job, kernel32.GetCurrentProcess()):
kernel32.CloseHandle(job)
diff --git a/studio/backend/utils/studio_version.py b/studio/backend/utils/studio_version.py
index 9c18070fbb..82ade74bba 100644
--- a/studio/backend/utils/studio_version.py
+++ b/studio/backend/utils/studio_version.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Network-free Studio release version resolution for display-only UI."""
+"""Network-free Unsloth release version resolution for display-only UI."""
from __future__ import annotations
@@ -20,7 +20,7 @@ _MAX_VERSION_LENGTH = 64
def is_valid_studio_release_version(value: object) -> bool:
- """Return True for Studio release tags such as ``v0.1.39-beta``."""
+ """Return True for Unsloth release tags such as ``v0.1.39-beta``."""
if not isinstance(value, str):
return False
version = value.strip()
@@ -102,7 +102,7 @@ def _git_branch(repo_root: Path) -> str | None:
def get_studio_version(repo_root: Path | None = None) -> str:
- """Return the installed Studio release tag for display, or ``dev``.
+ """Return the installed Unsloth release tag for display, or ``dev``.
Intentionally separate from the PyPI ``unsloth`` package version used by
update checks. Never performs network requests.
diff --git a/studio/backend/utils/training_runs.py b/studio/backend/utils/training_runs.py
index dc2535e570..dcdfa1395d 100644
--- a/studio/backend/utils/training_runs.py
+++ b/studio/backend/utils/training_runs.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Helpers for naming and describing Studio training runs."""
+"""Helpers for naming and describing Unsloth training runs."""
from __future__ import annotations
diff --git a/studio/backend/utils/transformers_latest.py b/studio/backend/utils/transformers_latest.py
index 40c8f729a5..9f1d11be5b 100644
--- a/studio/backend/utils/transformers_latest.py
+++ b/studio/backend/utils/transformers_latest.py
@@ -5,7 +5,7 @@
When a model's ``model_type`` is absent from every installed transformers overlay
(base 4.57.x plus the .venv_t5_530/550/510 sidecars and, if provisioned, .venv_t5_latest),
-Studio cannot load it today. This module answers, without authentication, code execution,
+Unsloth cannot load it today. This module answers, without authentication, code execution,
or trust_remote_code:
1. Does the LATEST transformers release on PyPI ship this ``model_type``?
@@ -387,7 +387,7 @@ def check_upgrade_for_model(model_name: str, hf_token: str | None = None) -> dic
_SHADOWABLE_DEPS = frozenset({"tokenizers", "safetensors"})
# Provided by the sidecar recipe; checked against its pin, not the base env.
_SIDECAR_PROVIDED = {"huggingface-hub": "1.8.0", "hf-xet": "1.4.2"}
-# CLI-only; never imported at runtime in Studio's workers.
+# CLI-only; never imported at runtime in Unsloth's workers.
_IGNORED_DEPS = frozenset({"typer"})
@@ -538,7 +538,7 @@ def _install_latest_transformers_locked(version: str, before_swap = None) -> dic
return {
"success": False,
"version": version,
- "message": "Cannot install: Studio is in offline mode.",
+ "message": "Cannot install: Unsloth is in offline mode.",
}
# Re-verify against a LIVE snapshot (a release may land inside the cache TTL);
# fall back to the cached one on fetch failure.
@@ -573,13 +573,13 @@ def _install_latest_transformers_locked(version: str, before_swap = None) -> dic
"version": version,
"message": "Cannot install transformers "
f"{version}: this environment does not satisfy {', '.join(blockers)}. "
- "A Studio update is required first.",
+ "An Unsloth update is required first.",
}
if not ensure_latest_transformers_venv(version, extra_packages, before_swap = before_swap):
return {
"success": False,
"version": version,
- "message": f"Installing transformers {version} failed; see the Studio logs.",
+ "message": f"Installing transformers {version} failed; see the Unsloth logs.",
}
_invalidate_capability_caches()
return {
diff --git a/studio/backend/utils/transformers_version.py b/studio/backend/utils/transformers_version.py
index 9f9f8aa3de..1fbcc9f46f 100644
--- a/studio/backend/utils/transformers_version.py
+++ b/studio/backend/utils/transformers_version.py
@@ -2151,7 +2151,7 @@ def end_sidecar_swap() -> None:
def sidecar_swap_in_progress() -> bool:
"""True while a .venv_t5_latest install or repair holds the reservation,
- in this process or any other Studio process (lock file)."""
+ in this process or any other Unsloth process (lock file)."""
return sidecar_swap_kind() is not None
diff --git a/studio/backend/utils/upload_limits.py b/studio/backend/utils/upload_limits.py
index c21ea69af7..fff0ac423f 100644
--- a/studio/backend/utils/upload_limits.py
+++ b/studio/backend/utils/upload_limits.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Shared Studio upload/request size limits."""
+"""Shared Unsloth upload/request size limits."""
from __future__ import annotations
diff --git a/studio/frontend/.npmrc b/studio/frontend/.npmrc
index 19783b5ff4..414379da6e 100644
--- a/studio/frontend/.npmrc
+++ b/studio/frontend/.npmrc
@@ -1,4 +1,4 @@
-# Studio frontend npm configuration.
+# Unsloth frontend npm configuration.
#
# Mini Shai-Hulud / Axios-style supply chain defense.
# Requires npm >=11.10.0. Refuses tarballs published less than 7 days ago,
diff --git a/studio/frontend/src/app/provider.tsx b/studio/frontend/src/app/provider.tsx
index c35706e50a..a7e9469cfc 100644
--- a/studio/frontend/src/app/provider.tsx
+++ b/studio/frontend/src/app/provider.tsx
@@ -412,7 +412,7 @@ function TauriWrapper({ children }: { children: ReactNode }) {
{desktopBooting ? (
-
Preparing Studio
+
Preparing Unsloth
The local backend is ready. Signing in to your desktop session
before loading chats.
diff --git a/studio/frontend/src/components/assistant-ui/model-selector/pickers.tsx b/studio/frontend/src/components/assistant-ui/model-selector/pickers.tsx
index 8e06181585..ff4bd4bebd 100644
--- a/studio/frontend/src/components/assistant-ui/model-selector/pickers.tsx
+++ b/studio/frontend/src/components/assistant-ui/model-selector/pickers.tsx
@@ -1679,7 +1679,7 @@ export function HubModelPicker({
const deviceType = usePlatformStore((s) => s.deviceType);
const isMac = deviceType === "mac";
- // Drop models Studio can't run for chat (diffusion / image / video / etc.)
+ // Drop models Unsloth can't run for chat (diffusion / image / video / etc.)
// using the Hub's classifier on the tags the listing already carries.
const isChatSupported = useCallback(
(r: HfModelResult) =>
@@ -1730,7 +1730,7 @@ export function HubModelPicker({
let rows = recommendedSearch.results
.filter((r) => !isHiddenModelId(r.id))
.filter((r) => !isMobileVariant(r.id));
- // Drop models Studio can't run for chat (diffusion / image / video / etc.).
+ // Drop models Unsloth can't run for chat (diffusion / image / video / etc.).
rows = rows.filter(isChatSupported);
// With no explicit format, show the device-recommended formats (GGUF, plus
// MLX on Mac). When the user picks a format, honor it instead so Safetensors
@@ -1868,7 +1868,7 @@ export function HubModelPicker({
// eslint-disable-next-line react-hooks/exhaustive-deps
[lmStudioModels, downloadedSort, formatFilter, loadTimes, localQuery],
);
- // Local ./models entries. Chat-only Studio runs GGUF (any host) and MLX (Mac
+ // Local ./models entries. Chat-only Unsloth runs GGUF (any host) and MLX (Mac
// only), so raw checkpoints there are hidden (mirrors the cached non-GGUF
// rule). An MLX build a Mac user dropped in ./models stays selectable.
const sortedLocalDir = useMemo(
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 62b8af6e3a..235dcb3c3d 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -1970,7 +1970,7 @@ function isNativeComposing(event: Event) {
}
// Fallback timeout for stuck IME composition. With Chrome on Windows against
-// a WSL-hosted Studio (issue #5546), `compositionend` never fires after the
+// a WSL-hosted Unsloth (issue #5546), `compositionend` never fires after the
// candidate commits, so `composingRef` stays true and Send stays disabled.
// Every compositionupdate / non-composing input resets the timer; only a true
// gap-after-commit lets it fire. 2500ms is above a normal candidate-window
diff --git a/studio/frontend/src/components/ui/confetti.tsx b/studio/frontend/src/components/ui/confetti.tsx
index 892bffdb18..35f5913240 100644
--- a/studio/frontend/src/components/ui/confetti.tsx
+++ b/studio/frontend/src/components/ui/confetti.tsx
@@ -34,7 +34,7 @@ export type ConfettiRef = Api | null;
const ConfettiContext = createContext
({} as Api);
-// Studio CSP blocks canvas-confetti's default blob: worker, so force
+// Unsloth CSP blocks canvas-confetti's default blob: worker, so force
// useWorker: false. Module-scoped so the prop default keeps stable
// identity across renders (`canvasRef` depends on `globalOptions`).
const DEFAULT_GLOBAL_OPTIONS: ConfettiGlobalOptions = {
diff --git a/studio/frontend/src/features/auth/components/auth-form.tsx b/studio/frontend/src/features/auth/components/auth-form.tsx
index 119471da10..73db10d41b 100644
--- a/studio/frontend/src/features/auth/components/auth-form.tsx
+++ b/studio/frontend/src/features/auth/components/auth-form.tsx
@@ -298,7 +298,7 @@ export function AuthForm({ mode }: AuthFormProps): ReactElement | null {
// reset-password"), which the installer puts on PATH on every platform.
// Do NOT rewrite it to a relative Windows path like
// ".\unsloth_studio\Scripts\unsloth.exe ..." -- that only resolves inside
- // the Studio home dir and fails with CommandNotFoundException elsewhere.
+ // the Unsloth home dir and fails with CommandNotFoundException elsewhere.
// Show the backend message as-is.
const msg = err instanceof Error ? err.message : "Auth failed.";
setError(msg);
diff --git a/studio/frontend/src/features/chat/artifacts/html-frame.tsx b/studio/frontend/src/features/chat/artifacts/html-frame.tsx
index b26f2f6685..36e3ed8a5b 100644
--- a/studio/frontend/src/features/chat/artifacts/html-frame.tsx
+++ b/studio/frontend/src/features/chat/artifacts/html-frame.tsx
@@ -28,7 +28,7 @@ export function buildArtifactSrcDoc(code: string): string {
}
// Preview iframes intentionally omit allow-downloads: generated canvases can
-// offer their own UI, but downloads must go through Studio's explicit
+// offer their own UI, but downloads must go through Unsloth's explicit
// copy/download controls outside the no-same-origin sandbox.
export function ArtifactHtmlFrame({
code,
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts b/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
index 0a0df1139b..bfb3eeb14c 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-sidebar-items.ts
@@ -45,7 +45,7 @@ export function groupThreads(
for (const t of threads) {
// Coerce archived to a boolean before comparing. Legacy threads (from the
- // older browser-only Studio, or any record predating the archived field)
+ // older browser-only Unsloth, or any record predating the archived field)
// can have archived === undefined or null; a raw `!== archived` comparison
// would drop those from BOTH the Recents (archived=false) and Archived
// (archived=true) lists, hiding existing chats. Treat missing as false.
diff --git a/studio/frontend/src/features/chat/lib/friendly-names.ts b/studio/frontend/src/features/chat/lib/friendly-names.ts
index 79744b3181..bc9c77b12d 100644
--- a/studio/frontend/src/features/chat/lib/friendly-names.ts
+++ b/studio/frontend/src/features/chat/lib/friendly-names.ts
@@ -5,7 +5,7 @@
* Friendly default names for auto-created OpenAI shell containers, used by the
* chat-adapter's lazy-create path (Code pill on, no thread container, non-default
* TTL). Goal: a memorable label like "otter" instead of "chat-abc12345"; users
- * can still rename via the Studio alias map.
+ * can still rename via the Unsloth alias map.
*
* The list is curated to be unambiguous, non-offensive nouns from natural
* categories (animals, plants, geography, materials, weather), avoid
diff --git a/studio/frontend/src/features/chat/provider-capabilities.ts b/studio/frontend/src/features/chat/provider-capabilities.ts
index 79c9a3205c..ec251cdada 100644
--- a/studio/frontend/src/features/chat/provider-capabilities.ts
+++ b/studio/frontend/src/features/chat/provider-capabilities.ts
@@ -409,7 +409,7 @@ function isGeminiImageModel(modelId: string): boolean {
* Whether the saved Gemini connection points at a custom OpenAI-compat gateway
* (any non-Google host). The backend `_is_openai_compatible` routes these
* through `/chat/completions` instead of the native translator, so native Gemini
- * tool envelopes never reach them. Hide the matching Studio pills here so the
+ * tool envelopes never reach them. Hide the matching Unsloth pills here so the
* request, builder, and UI agree.
*/
export function isGeminiCustomOpenAICompatBase(
diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
index af99458349..192ce1ec69 100644
--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
@@ -671,7 +671,7 @@ type ChatRuntimeStore = {
// Describe figures/charts at ingest time (vision model required).
ragCaptionFigures: boolean;
/**
- * When on, local Studio tool calls pause for an explicit allow/deny in the
+ * When on, local Unsloth tool calls pause for an explicit allow/deny in the
* chat before they run.
*/
confirmToolCalls: boolean;
@@ -1824,7 +1824,7 @@ export const useChatRuntimeStore = create((set, get) => ({
setContextUsage: (contextUsage) => set({ contextUsage }),
}));
-// Mirror token edits made through the shared store (e.g. Studio's field).
+// Mirror token edits made through the shared store (e.g. Unsloth's field).
const unsubscribeHfTokenMirror = mirrorHfTokenInto(useChatRuntimeStore);
if (import.meta.hot) {
import.meta.hot.dispose(unsubscribeHfTokenMirror);
diff --git a/studio/frontend/src/features/chat/utils/chat-history-storage.ts b/studio/frontend/src/features/chat/utils/chat-history-storage.ts
index 00df3657b1..2ed17c26a0 100644
--- a/studio/frontend/src/features/chat/utils/chat-history-storage.ts
+++ b/studio/frontend/src/features/chat/utils/chat-history-storage.ts
@@ -324,7 +324,7 @@ async function importLegacyChatsIfNeeded(): Promise {
if (legacyChatImportPromise) return legacyChatImportPromise;
legacyChatImportPromise = (async () => {
- // Fast-path: no Dexie DB -- new user, never had browser-only Studio.
+ // Fast-path: no Dexie DB -- new user, never had browser-only Unsloth.
if (await dexieDbAbsent()) {
markLegacyChatImportDone();
return;
diff --git a/studio/frontend/src/features/hub/download-manager/api.ts b/studio/frontend/src/features/hub/download-manager/api.ts
index 3373a47b04..2c55b90edd 100644
--- a/studio/frontend/src/features/hub/download-manager/api.ts
+++ b/studio/frontend/src/features/hub/download-manager/api.ts
@@ -12,7 +12,7 @@ function parseErrorText(status: number, body: unknown): string {
const detail = (body as { detail?: unknown }).detail;
const formatted = formatFastApiDetail(detail);
if (status === 405) {
- return `${formatted || "Method Not Allowed"} - the Studio backend did not accept this API method. Restart Studio so the frontend and backend are on the same build.`;
+ return `${formatted || "Method Not Allowed"} - the Unsloth backend did not accept this API method. Restart Unsloth so the frontend and backend are on the same build.`;
}
if (formatted) return formatted;
const message = (body as { message?: unknown }).message;
diff --git a/studio/frontend/src/features/onboarding/components/steps/model-selection-step.tsx b/studio/frontend/src/features/onboarding/components/steps/model-selection-step.tsx
index a5ce25dd32..e60786decb 100644
--- a/studio/frontend/src/features/onboarding/components/steps/model-selection-step.tsx
+++ b/studio/frontend/src/features/onboarding/components/steps/model-selection-step.tsx
@@ -113,7 +113,7 @@ export function ModelSelectionStep() {
return applyPriorityOrdering(ids);
}, [hfResults]);
- // Match Studio: only show exception signals (OOM/TIGHT) in training flows.
+ // Match Unsloth: only show exception signals (OOM/TIGHT) in training flows.
const vramMap = useMemo(() => {
const fitMap = buildModelVramMap(
hfResults,
diff --git a/studio/frontend/src/features/settings/components/usage-examples.tsx b/studio/frontend/src/features/settings/components/usage-examples.tsx
index 86125585cc..4ccc43d16c 100644
--- a/studio/frontend/src/features/settings/components/usage-examples.tsx
+++ b/studio/frontend/src/features/settings/components/usage-examples.tsx
@@ -229,12 +229,12 @@ curl.exe ${base}/v1/chat/completions \`
}
// A second OpenAI call naming a different downloaded GGUF: with auto-switch on,
-// Studio loads it before serving, so the model field selects the served model.
+// Unsloth loads it before serving, so the model field selects the served model.
function pythonSwitchDemo(): string {
return `
# "Switch model by request" is on: replace the model below with another GGUF you
-# have downloaded and Studio loads it before serving. Unknown names keep serving
+# have downloaded and Unsloth loads it before serving. Unknown names keep serving
# the current model.
response = client.chat.completions.create(
model=${j(SWITCH_MODEL)},
@@ -349,7 +349,7 @@ function javascriptSwitchDemo(): string {
return `
// "Switch model by request" is on: replace the model below with another GGUF you
-// have downloaded and Studio loads it before serving. Unknown names keep serving
+// have downloaded and Unsloth loads it before serving. Unknown names keep serving
// the current model.
const switchResponse = await client.chat.completions.create({
model: ${j(SWITCH_MODEL)},
@@ -512,7 +512,7 @@ export function UsageExamples({ apiKey }: { apiKey?: string | null }) {
if (!localAgentDetection) {
setDetectedAgents([]);
// A previously auto-picked agent was only ever verified against the
- // Studio backend's PATH, which is meaningless now that this panel no
+ // Unsloth backend's PATH, which is meaningless now that this panel no
// longer targets a loopback base -- don't leave it selected, but
// never touch a choice the user made by hand.
if (!agentPickedByUserRef.current) {
diff --git a/studio/frontend/src/features/studio/recent-trainings-section.tsx b/studio/frontend/src/features/studio/recent-trainings-section.tsx
index 43808a7ca9..ba65d7f736 100644
--- a/studio/frontend/src/features/studio/recent-trainings-section.tsx
+++ b/studio/frontend/src/features/studio/recent-trainings-section.tsx
@@ -8,7 +8,7 @@ import { HistoryCardGrid } from "./history-card-grid";
/**
* Recent training runs surfaced on Data Recipes and Export. Selecting a run
- * stores its id and navigates to Studio, which auto-opens its History tab.
+ * stores its id and navigates to Unsloth, which auto-opens its History tab.
* Renders nothing once we know there are no runs.
*/
export function RecentTrainingsSection() {
diff --git a/studio/frontend/src/features/training/stores/training-config-store.ts b/studio/frontend/src/features/training/stores/training-config-store.ts
index a927f83fd8..d204e24f47 100644
--- a/studio/frontend/src/features/training/stores/training-config-store.ts
+++ b/studio/frontend/src/features/training/stores/training-config-store.ts
@@ -1004,7 +1004,7 @@ export const useTrainingConfigStore = create()(
}
if (version < 12) {
// hfToken moved to the shared hf-token-store; seed it once so an
- // existing Studio-only token isn't lost.
+ // existing Unsloth-only token isn't lost.
const legacyToken = typeof s.hfToken === "string" ? s.hfToken.trim() : "";
if (legacyToken && !getHfToken()) {
useHfTokenStore.getState().setToken(legacyToken);
diff --git a/studio/frontend/src/features/transformers-upgrade/components/transformers-upgrade-dialog.tsx b/studio/frontend/src/features/transformers-upgrade/components/transformers-upgrade-dialog.tsx
index 98c590e5e4..3e39a8ff1c 100644
--- a/studio/frontend/src/features/transformers-upgrade/components/transformers-upgrade-dialog.tsx
+++ b/studio/frontend/src/features/transformers-upgrade/components/transformers-upgrade-dialog.tsx
@@ -82,7 +82,7 @@ export function TransformersUpgradeDialog() {
<>
Even the latest transformers release on PyPI does not
support it yet: the architecture is only available on the
- transformers development branch (main), and Studio does
+ transformers development branch (main), and Unsloth does
not install development builds. Support arrives with the
next transformers release on PyPI.
>
diff --git a/studio/frontend/src/hooks/use-tauri-backend.ts b/studio/frontend/src/hooks/use-tauri-backend.ts
index db996768a6..53122864e7 100644
--- a/studio/frontend/src/hooks/use-tauri-backend.ts
+++ b/studio/frontend/src/hooks/use-tauri-backend.ts
@@ -71,8 +71,8 @@ function externalConflictMessage(preflight: DesktopPreflightResult) {
}
return preflight.port
- ? `A Unsloth server for this install is already running from a terminal on port ${preflight.port}. Stop that server, or run \`unsloth studio update\` from that terminal before using the desktop app.`
- : "A Unsloth server for this install is already running from a terminal. Stop that server, or run `unsloth studio update` from that terminal before using the desktop app.";
+ ? `An Unsloth server for this install is already running from a terminal on port ${preflight.port}. Stop that server, or run \`unsloth studio update\` from that terminal before using the desktop app.`
+ : "An Unsloth server for this install is already running from a terminal. Stop that server, or run `unsloth studio update` from that terminal before using the desktop app.";
}
async function waitForManagedServerPort(
diff --git a/studio/frontend/src/i18n/README.md b/studio/frontend/src/i18n/README.md
index ba8cce5d8d..7594e4723f 100644
--- a/studio/frontend/src/i18n/README.md
+++ b/studio/frontend/src/i18n/README.md
@@ -8,5 +8,5 @@
- Preserve interpolation variables exactly, for example `{count}`, `{model}`, and `{provider}`.
- Keep product and technical names unchanged unless there is an established localized name, for example `Unsloth Studio`, `LoRA`, `GGUF`, and `Hugging Face`.
- Keep translation changes small and reviewable. Prefer separate commits for runtime changes, UI migration, and locale text.
-- When adding user-facing Studio UI text, add the English message key first and add non-English overrides only when the translation is clear.
+- When adding user-facing Unsloth UI text, add the English message key first and add non-English overrides only when the translation is clear.
- Run `npx tsx src/i18n/check-parity.ts` before committing to ensure there are no shape mismatches or placeholder discrepancies in the non-English overlays.
diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.ts
index 744a2002c9..4bd4328cce 100644
--- a/studio/frontend/src/i18n/locales/ar.ts
+++ b/studio/frontend/src/i18n/locales/ar.ts
@@ -113,7 +113,7 @@ export const ar = {
showToken: "إظهار التوكن",
tokenSaved: "تم حفظ التوكن",
password: "كلمة المرور",
- passwordDescription: "تغيير كلمة المرور لحساب Studio هذا.",
+ passwordDescription: "تغيير كلمة المرور لحساب Unsloth هذا.",
passwordDialog: {
trigger: "تغيير كلمة المرور",
title: "تغيير كلمة المرور",
@@ -284,7 +284,7 @@ export const ar = {
},
resources: {
title: "النظام",
- description: "مراقبة أجهزة خادم Studio هذا وتخزينه.",
+ description: "مراقبة أجهزة خادم Unsloth هذا وتخزينه.",
liveUpdates: "التحديثات المباشرة",
floatingWindow: "نافذة عائمة",
disableOverlay: "تعطيل التراكب",
diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.ts
index 7d94e7656e..c28d07790f 100644
--- a/studio/frontend/src/i18n/locales/de.ts
+++ b/studio/frontend/src/i18n/locales/de.ts
@@ -115,7 +115,7 @@ export const de = {
tokenSaved: "Token gespeichert",
password: "Passwort",
passwordDescription:
- "Ändern Sie das Passwort für dieses Studio-Konto.",
+ "Ändern Sie das Passwort für dieses Unsloth-Konto.",
passwordDialog: {
trigger: "Passwort ändern",
title: "Passwort ändern",
@@ -295,7 +295,7 @@ export const de = {
resources: {
title: "System",
description:
- "Überwachen Sie Hardware und Speicher dieses Studio-Servers.",
+ "Überwachen Sie Hardware und Speicher dieses Unsloth-Servers.",
liveUpdates: "Live-Updates",
floatingWindow: "Schwebendes Fenster",
disableOverlay: "Overlay deaktivieren",
diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.ts
index 988c109a3f..b7dfee10b8 100644
--- a/studio/frontend/src/i18n/locales/es.ts
+++ b/studio/frontend/src/i18n/locales/es.ts
@@ -115,7 +115,7 @@ export const es = {
tokenSaved: "Token guardado",
password: "Contraseña",
passwordDescription:
- "Cambia la contraseña de esta cuenta de Studio.",
+ "Cambia la contraseña de esta cuenta de Unsloth.",
passwordDialog: {
trigger: "Cambiar contraseña",
title: "Cambiar contraseña",
@@ -294,7 +294,7 @@ export const es = {
resources: {
title: "Sistema",
description:
- "Monitorea el hardware y el almacenamiento de este servidor de Studio.",
+ "Monitorea el hardware y el almacenamiento de este servidor de Unsloth.",
liveUpdates: "Actualizaciones en vivo",
floatingWindow: "Ventana flotante",
disableOverlay: "Desactivar superposición",
diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.ts
index e1f2a0c5ec..6105cd8ccf 100644
--- a/studio/frontend/src/i18n/locales/fr.ts
+++ b/studio/frontend/src/i18n/locales/fr.ts
@@ -115,7 +115,7 @@ export const fr = {
tokenSaved: "Token enregistré",
password: "Mot de passe",
passwordDescription:
- "Changez le mot de passe de ce compte Studio.",
+ "Changez le mot de passe de ce compte Unsloth.",
passwordDialog: {
trigger: "Changer le mot de passe",
title: "Changer le mot de passe",
@@ -291,7 +291,7 @@ export const fr = {
resources: {
title: "Système",
description:
- "Surveillez le matériel et le stockage de ce serveur Studio.",
+ "Surveillez le matériel et le stockage de ce serveur Unsloth.",
liveUpdates: "Mises à jour en direct",
floatingWindow: "Fenêtre flottante",
disableOverlay: "Désactiver la superposition",
diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.ts
index 77b6265e7b..732ae1d7fa 100644
--- a/studio/frontend/src/i18n/locales/hi.ts
+++ b/studio/frontend/src/i18n/locales/hi.ts
@@ -113,7 +113,7 @@ export const hi = {
showToken: "token दिखाएं",
tokenSaved: "Token सहेजा गया",
password: "पासवर्ड",
- passwordDescription: "इस Studio खाते के लिए पासवर्ड बदलें।",
+ passwordDescription: "इस Unsloth खाते के लिए पासवर्ड बदलें।",
passwordDialog: {
trigger: "पासवर्ड बदलें",
title: "पासवर्ड बदलें",
@@ -283,7 +283,7 @@ export const hi = {
},
resources: {
title: "सिस्टम",
- description: "इस Studio सर्वर के हार्डवेयर और स्टोरेज की निगरानी करें।",
+ description: "इस Unsloth सर्वर के हार्डवेयर और स्टोरेज की निगरानी करें।",
liveUpdates: "लाइव अपडेट",
floatingWindow: "फ्लोटिंग विंडो",
disableOverlay: "ओवरले अक्षम करें",
diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.ts
index a261994f03..de5e93c672 100644
--- a/studio/frontend/src/i18n/locales/ja.ts
+++ b/studio/frontend/src/i18n/locales/ja.ts
@@ -360,7 +360,7 @@ export const ja = {
},
resources: {
title: "システム",
- description: "この Studio サーバーのハードウェアとストレージを監視します。",
+ description: "この Unsloth サーバーのハードウェアとストレージを監視します。",
liveUpdates: "リアルタイム更新",
floatingWindow: "フローティングウィンドウ",
disableOverlay: "オーバーレイを無効化",
diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.ts
index 7c5691925e..6ff9cdbb5e 100644
--- a/studio/frontend/src/i18n/locales/ko.ts
+++ b/studio/frontend/src/i18n/locales/ko.ts
@@ -113,7 +113,7 @@ export const ko = {
showToken: "토큰 표시",
tokenSaved: "토큰이 저장되었습니다",
password: "비밀번호",
- passwordDescription: "이 Studio 계정의 비밀번호를 변경합니다.",
+ passwordDescription: "이 Unsloth 계정의 비밀번호를 변경합니다.",
passwordDialog: {
trigger: "비밀번호 변경",
title: "비밀번호 변경",
@@ -282,7 +282,7 @@ export const ko = {
},
resources: {
title: "시스템",
- description: "이 Studio 서버의 하드웨어와 저장소를 모니터링합니다.",
+ description: "이 Unsloth 서버의 하드웨어와 저장소를 모니터링합니다.",
liveUpdates: "실시간 업데이트",
floatingWindow: "플로팅 창",
disableOverlay: "오버레이 비활성화",
diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.ts
index c7464a3b44..60e939bb0d 100644
--- a/studio/frontend/src/i18n/locales/ru.ts
+++ b/studio/frontend/src/i18n/locales/ru.ts
@@ -113,7 +113,7 @@ export const ru = {
showToken: "Показать токен",
tokenSaved: "Токен сохранён",
password: "Пароль",
- passwordDescription: "Изменить пароль для этого аккаунта Studio.",
+ passwordDescription: "Изменить пароль для этого аккаунта Unsloth.",
passwordDialog: {
trigger: "Изменить пароль",
title: "Изменить пароль",
@@ -283,7 +283,7 @@ export const ru = {
},
resources: {
title: "Система",
- description: "Мониторинг оборудования и хранилища этого сервера Studio.",
+ description: "Мониторинг оборудования и хранилища этого сервера Unsloth.",
liveUpdates: "Обновления в реальном времени",
floatingWindow: "Плавающее окно",
disableOverlay: "Отключить оверлей",
diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.ts
index ff218adad2..37c73086cf 100644
--- a/studio/frontend/src/i18n/locales/zh-CN.ts
+++ b/studio/frontend/src/i18n/locales/zh-CN.ts
@@ -375,7 +375,7 @@ export const zhCN = {
},
resources: {
title: "系统",
- description: "监控此 Studio 服务器的硬件和存储。",
+ description: "监控此 Unsloth 服务器的硬件和存储。",
liveUpdates: "实时更新",
floatingWindow: "悬浮窗口",
disableOverlay: "禁用悬浮层",
diff --git a/studio/frontend/src/index.css b/studio/frontend/src/index.css
index 6d4c21eec8..a43e3d974b 100644
--- a/studio/frontend/src/index.css
+++ b/studio/frontend/src/index.css
@@ -2599,7 +2599,7 @@ html[data-chat-font] .aui-root {
* the documented WCAG outcome (motion is "minimised, not removed").
*
* .animate-spin and generated image loading dots are the exceptions: loading
- * indicators across Studio (tool execution loaders, sonner toasts, Tauri
+ * indicators across Unsloth (tool execution loaders, sonner toasts, Tauri
* startup / update screens, the primitive, and image generation
* cards). Freezing them removes the only visual signal that work is in flight,
* so they keep animating.
diff --git a/studio/frontend/src/lib/tauri-diagnostics.ts b/studio/frontend/src/lib/tauri-diagnostics.ts
index 5c07931a86..2b687478fe 100644
--- a/studio/frontend/src/lib/tauri-diagnostics.ts
+++ b/studio/frontend/src/lib/tauri-diagnostics.ts
@@ -62,7 +62,7 @@ export function redactDiagnosticsText(text: string): string {
"$1$2",
);
- // Redact Studio paths before broader home-directory paths.
+ // Redact Unsloth paths before broader home-directory paths.
redacted = redacted.replace(
/(?:\/Users|\/home)\/[^\s/]+\/\.unsloth\/studio/gi,
"",
diff --git a/studio/install_llama_prebuilt.py b/studio/install_llama_prebuilt.py
index cca8886777..9bbd0cb8be 100644
--- a/studio/install_llama_prebuilt.py
+++ b/studio/install_llama_prebuilt.py
@@ -2897,7 +2897,7 @@ def _pick_rocm_gfx_target(out: str) -> str | None:
break
if _vis_raw is not None:
_vis = _vis_raw.strip()
- # Empty or "-1" means "no AMD GPU visible" (matches the rest of Studio).
+ # Empty or "-1" means "no AMD GPU visible" (matches the rest of Unsloth).
if _vis == "" or _vis == "-1":
return None
_first = _vis.split(",")[0].strip()
@@ -4437,7 +4437,7 @@ def ensure_diffusion_visual_server(
approved_checksums: ApprovedReleaseChecksums,
) -> None:
"""Best-effort placement of the DiffusionGemma visual-server binary next to
- llama-server in the install tree, so Studio can serve DiffusionGemma GGUFs
+ llama-server in the install tree, so Unsloth can serve DiffusionGemma GGUFs
without any DG_* env. This is an Unsloth artifact (not a ggml-org one), so it
is optional: if it is already present we just make it executable, otherwise we
try the published release and quietly skip on absence. A source build
@@ -4719,7 +4719,7 @@ def runtime_patterns_for_choice(choice: AssetChoice) -> list[str]:
# repackage the SO/DLL set (e.g. ggml-org/llama.cpp#23462 split the
# per-binary entry code into paired ``lib-impl.so`` shared
# libraries between b9279 and b9283) without us re-enumerating
- # every new file. Studio invokes llama-server, llama-quantize, and the
+ # every new file. Unsloth invokes llama-server, llama-quantize, and the
# DiffusionGemma visual-server (when the bundle ships it, for native
# DiffusionGemma serving); other CLIs upstream ships (llama-cli,
# llama-bench, ...) are skipped.
@@ -6320,7 +6320,7 @@ def _linux_published_attempts(host: HostInfo, bundle: PublishedReleaseBundle) ->
from asset names."""
attempts: list[AssetChoice] = []
if host.has_usable_nvidia:
- # Prefer the cudart major Studio loads at runtime (torch's bundled
+ # Prefer the cudart major Unsloth loads at runtime (torch's bundled
# libcudart), not the newest detected on disk. Without this a stray
# cuda13 runtime outranks the torch cuda12 the binary links against.
torch_preference = detect_torch_cuda_runtime_preference(host)
diff --git a/studio/install_node_prebuilt.py b/studio/install_node_prebuilt.py
index 4038216eef..fb40634e95 100644
--- a/studio/install_node_prebuilt.py
+++ b/studio/install_node_prebuilt.py
@@ -6,7 +6,7 @@
Downloads an official Node.js archive from nodejs.org into an isolated
``/node`` and never touches the system Node/npm. Pinning Node 24+
-LTS clears the Studio frontend build floor (Vite 8: Node ^20.19 || >=22.12,
+LTS clears the Unsloth frontend build floor (Vite 8: Node ^20.19 || >=22.12,
npm >= 11) with the npm it bundles.
Archives are verified against sha256 digests pinned in ``node_prebuilt_pins.json``
diff --git a/studio/install_python_stack.py b/studio/install_python_stack.py
index 19c492deaa..95c9356d4a 100644
--- a/studio/install_python_stack.py
+++ b/studio/install_python_stack.py
@@ -1008,7 +1008,7 @@ def _install_bnb_windows_rocm() -> bool:
# `hipinfo.exe` at import time to detect the GPU arch and logs a scary
# (harmless) ERROR + WARNING on every import when it is missing. The venv
# Scripts dir is on PATH only when the venv is activated, which neither
- # Studio nor the installer's child processes ever do.
+ # Unsloth nor the installer's child processes ever do.
_scripts_dir = os.path.dirname(sys.executable)
if os.path.isfile(os.path.join(_scripts_dir, "hipInfo.exe")) and not shutil.which(
"hipinfo.exe"
@@ -2318,7 +2318,7 @@ def install_python_stack() -> int:
_progress("dependency overrides (skipped, no torch)")
elif _rocm_windows_torch_installed or _installed_torch_is_windows_rocm():
# No working Windows ROCm torchao build: it imports an absent c10d backend
- # and crashes transformers.quantizers. Studio stubs it at runtime, so
+ # and crashes transformers.quantizers. Unsloth stubs it at runtime, so
# installing it only ships a package that crashes on import -- skip it.
_progress("dependency overrides (skipped, Windows ROCm)")
_safe_print(" Windows ROCm -- skipping torchao (no working build; stubbed at runtime)")
@@ -2371,7 +2371,7 @@ def install_python_stack() -> int:
# "https://raw.githubusercontent.com/unslothai/unsloth/refs/heads/main/unsloth/save.py",
# )
- # 8. Studio dependencies
+ # 8. Unsloth dependencies
_progress("studio deps")
pip_install(
"Installing studio dependencies",
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index dab1e1e73f..98e801cd3c 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -322,7 +322,7 @@ function Write-CudaDriverToolkitMismatch {
$driverMajor = $DriverMaxCuda.Split('.')[0]
substep "CUDA Toolkit $ToolkitVersion is a major-version mismatch: toolkit major $toolkitMajor exceeds driver CUDA major $driverMajor ($DriverMaxCuda)." $Color
substep "Update the NVIDIA GPU driver to run CUDA Toolkit $ToolkitVersion, or install a CUDA $driverMajor.x toolkit." $Color
- substep "Or let Studio use the prebuilt CUDA bundle; it does not need the local toolkit." $Color
+ substep "Or let Unsloth use the prebuilt CUDA bundle; it does not need the local toolkit." $Color
}
# Detect CUDA Compute Capability via nvidia-smi.
@@ -937,13 +937,13 @@ function Show-NpmRegistryHint {
Write-Host ""
step "frontend" "registry.npmjs.org looks blocked (corporate firewall/proxy?)" "Yellow"
if ($mirror) {
- substep "Studio pins the public npm registry; your mirror is being ignored."
+ substep "Unsloth pins the public npm registry; your mirror is being ignored."
substep "Detected a registry in your npm config:"
substep " $mirror"
- substep "Re-run pointing Studio at it:"
+ substep "Re-run pointing Unsloth at it:"
substep " `$env:UNSLOTH_NPM_REGISTRY='$mirror'; .\install.ps1 --local"
} else {
- substep "If you use a private mirror/proxy, point Studio at it and re-run:"
+ substep "If you use a private mirror/proxy, point Unsloth at it and re-run:"
substep " `$env:UNSLOTH_NPM_REGISTRY='https://your-mirror.example/api/npm/'; .\install.ps1 --local"
}
substep "(min-release-age and save-exact stay enforced.)"
@@ -1070,7 +1070,7 @@ if (-not $HasNvidiaSmi) {
}
# ── Helper: run amd-smi without triggering a UAC elevation prompt ──
# amd-smi on Windows auto-elevates to read GPU/APU memory, surfacing a confusing
-# DiskPart UAC prompt mid-install (Studio backend amd.py hits the same). RunAsInvoker
+# DiskPart UAC prompt mid-install (Unsloth backend amd.py hits the same). RunAsInvoker
# forces it (and helpers it spawns) to run un-elevated; on failure the WMI name ->
# gfx fallback still resolves the arch.
function Invoke-AmdSmiNoElevate {
@@ -1132,7 +1132,7 @@ if (-not $HasNvidiaSmi) {
if ([string]::IsNullOrWhiteSpace($HipinfoPath)) { return $false }
# VenvDir/VIRTUAL_ENV can be unset this early (the update flow probes before
# VenvDir is set), so also derive the venv from the setup python + default
- # Studio home, else the venv hipInfo isn't caught.
+ # Unsloth home, else the venv hipInfo isn't caught.
$venvRoots = @()
if ($env:VIRTUAL_ENV) { $venvRoots += $env:VIRTUAL_ENV }
$vd = Get-Variable -Name VenvDir -ValueOnly -ErrorAction SilentlyContinue
@@ -1141,7 +1141,7 @@ if (-not $HasNvidiaSmi) {
try { $venvRoots += (Split-Path -Parent (Split-Path -Parent $env:UNSLOTH_SETUP_PYTHON)) } catch {}
}
if ($env:USERPROFILE) { $venvRoots += (Join-Path $env:USERPROFILE ".unsloth\studio\unsloth_studio") }
- # A custom Studio home (UNSLOTH_STUDIO_HOME / STUDIO_HOME alias) moves the
+ # A custom Unsloth home (UNSLOTH_STUDIO_HOME / STUDIO_HOME alias) moves the
# venv off the default path; seed it too or its hipInfo escapes the filter.
$studioHomeEnv = if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) { $env:UNSLOTH_STUDIO_HOME.Trim() } elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) { $env:STUDIO_HOME.Trim() } else { $null }
if ($studioHomeEnv) {
@@ -1860,7 +1860,7 @@ $SysNpmVersion = ""
$NodeSource = $null
if (-not $IsPipInstall) {
- # Put Node beside the Studio root. OXC can still need npm when the
+ # Put Node beside the Unsloth root. OXC can still need npm when the
# frontend build is skipped.
if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) { $NodeOverride = $env:UNSLOTH_STUDIO_HOME.Trim() }
elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) { $NodeOverride = $env:STUDIO_HOME.Trim() }
@@ -2137,17 +2137,17 @@ if ($NeedNodeForSetup) {
step "frontend" "skipped (no suitable Node; system left untouched)" "Yellow"
}
$NeedFrontendBuild = $false
- substep "found Node='$SysNodeVersion' npm='$SysNpmVersion'; Studio needs Node >=20.19/22.12/23 and npm >= 11" "Yellow"
+ substep "found Node='$SysNodeVersion' npm='$SysNpmVersion'; Unsloth needs Node >=20.19/22.12/23 and npm >= 11" "Yellow"
substep "install a suitable Node + npm, or unset UNSLOTH_SKIP_NODE_INSTALL to let Unsloth manage an isolated Node" "Yellow"
} elseif ($NodeSource -eq "bundled") {
New-Item -ItemType Directory -Force -Path $NodeParent -ErrorAction SilentlyContinue | Out-Null
- # Minimal ownership guard for a custom-home dir (the full Studio-owned
+ # Minimal ownership guard for a custom-home dir (the full Unsloth-owned
# helpers are defined later); never os.replace over a user-owned dir.
if ($NodeOverride -and (Test-Path -LiteralPath $NodeDir -PathType Container)) {
$nodeOwnedMarker = Join-Path $NodeDir ".unsloth-studio-owned"
$nodeMeta = Join-Path $NodeDir "UNSLOTH_NODE_PREBUILT_INFO.json"
if (-not (Test-Path -LiteralPath $nodeOwnedMarker) -and -not (Test-Path -LiteralPath $nodeMeta)) {
- Write-Host "[ERROR] $NodeDir already exists and is not a Studio-owned Node install." -ForegroundColor Red
+ Write-Host "[ERROR] $NodeDir already exists and is not an Unsloth-owned Node install." -ForegroundColor Red
Write-Host " Move it aside or choose an empty UNSLOTH_STUDIO_HOME before re-running." -ForegroundColor Yellow
exit 1
}
@@ -2160,7 +2160,7 @@ if ($NeedNodeForSetup) {
$nodeExit = $LASTEXITCODE
if ($nodeExit -eq 3) {
Write-Host $nodeOut -ForegroundColor DarkGray
- step "node" "install blocked by another active Studio install" "Red"
+ step "node" "install blocked by another active Unsloth install" "Red"
exit 3
} elseif ($nodeExit -ne 0) {
Write-Host $nodeOut -ForegroundColor DarkGray
@@ -2486,7 +2486,7 @@ if (Test-Path -LiteralPath $LegacyStudioHome -PathType Container) {
$LegacyStudioHome = (Resolve-Path -LiteralPath $LegacyStudioHome).Path
}
$StudioHomeIsCustom = ($_studioHomeCanon -ne $LegacyStudioHome)
-# Directory-local evidence that Studio created $Path, used to adopt a custom-home
+# Directory-local evidence that Unsloth created $Path, used to adopt a custom-home
# llama.cpp predating the .unsloth-studio-owned marker (see setup.sh). Only the
# prebuilt UNSLOTH_PREBUILT_INFO.json counts; source builds are indistinguishable
# from a user clone on Windows and stay under the strict guard.
@@ -2506,7 +2506,7 @@ function Assert-StudioOwnedOrAbsent {
Mark-StudioOwned $Path
return
}
- Write-Host "[ERROR] $Path already exists and is not marked as a Studio-owned $Label." -ForegroundColor Red
+ Write-Host "[ERROR] $Path already exists and is not marked as an Unsloth-owned $Label." -ForegroundColor Red
Write-Host " Move it aside or choose an empty UNSLOTH_STUDIO_HOME before re-running." -ForegroundColor Yellow
exit 1
}
@@ -2576,7 +2576,7 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
$reason = if ($installedTorchTag) { "torch $installedTorchTag != required $expectedTorchTag" } else { "torch could not be imported" }
if ($InstallerManagedSetup) {
substep "Stale venv detected ($reason)." "Yellow"
- Write-Host " [ERROR] The existing Studio environment needs repair." -ForegroundColor Red
+ Write-Host " [ERROR] The existing Unsloth environment needs repair." -ForegroundColor Red
Write-Host " Re-run install.ps1 so it can replace the environment safely with rollback." -ForegroundColor Yellow
exit 1
}
@@ -2598,7 +2598,7 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
Remove-Item -LiteralPath $VenvDir -Recurse -Force -ErrorAction Stop
} catch {
Write-Host " [ERROR] Could not remove stale venv: $($_.Exception.Message)" -ForegroundColor Red
- Write-Host " Close any running Studio/Python processes and re-run setup." -ForegroundColor Red
+ Write-Host " Close any running Unsloth/Python processes and re-run setup." -ForegroundColor Red
exit 1
}
}
@@ -2764,7 +2764,7 @@ if ($script:UnslothVerbose) {
# The CUDA tag is chosen based on the driver's max supported CUDA version.
# Triton/inductor filenames are long and can hit Windows MAX_PATH (260). With long
-# paths on, cache under Studio home; else use a short drive-root dir for headroom.
+# paths on, cache under Unsloth home; else use a short drive-root dir for headroom.
if ($LongPathsEnabled) {
$TorchCacheDir = Join-Path $StudioHome "TORCHINDUCTOR_CACHE_DIR"
} else {
@@ -2793,7 +2793,7 @@ $ROCmIndexUrl = $null
# Install AMD ROCm PyTorch wheels when ROCm is confirmed OR a gfx arch is known
# (name-inferred on Adrenalin-only hosts). The per-arch wheels bundle the runtime
# (rocm-sdk-libraries-), so torch.cuda.is_available() is True without a HIP
-# SDK -- which flips Studio out of chat-only (CHAT_ONLY) and enables Train/Export.
+# SDK -- which flips Unsloth out of chat-only (CHAT_ONLY) and enables Train/Export.
# Gating on $HasROCm alone left Strix Halo / Radeon 8060S on CPU torch; a failed
# ROCm install still falls back to CPU below, so this is safe.
if (($HasROCm -or $ROCmGfxArch) -and $CuTag -eq "cpu") {
@@ -3231,7 +3231,7 @@ if ($LocalLlamaCppSrc) {
# Reusing a local dir disables both the prebuilt download and the source
# build, so a runnable llama-server.exe must already be present. Accept any
# layout LlamaCppBackend._layout_candidates() resolves (root-level, build\bin,
- # or build\bin\Release) so the flag never rejects a tree Studio could run.
+ # or build\bin\Release) so the flag never rejects a tree Unsloth could run.
$LocalLlamaServerFound = $false
foreach ($_cand in @(
(Join-Path $ResolvedLocal "llama-server.exe"),
@@ -3253,7 +3253,7 @@ if ($LocalLlamaCppSrc) {
}
} else {
# Fail clearly rather than junction an unbuilt or wrong-platform checkout
- # and leave Studio with no usable binary.
+ # and leave Unsloth with no usable binary.
if (-not $LocalLlamaServerFound) {
step "llama.cpp" "no llama-server.exe under $ResolvedLocal (looked for .\llama-server.exe, .\build\bin and .\build\bin\Release) -- build llama.cpp there first, or drop --with-llama-cpp-dir" "Red"
exit 1
@@ -3281,7 +3281,7 @@ if ($LocalLlamaCppSrc) {
# prebuilt path's active-process handling and stop with a clear message.
if (Test-Path -LiteralPath $LlamaCppDir) {
step "llama.cpp" "install blocked by active llama.cpp process" "Yellow"
- substep "Close Studio or other llama.cpp users and retry" "Yellow"
+ substep "Close Unsloth or other llama.cpp users and retry" "Yellow"
exit 3
}
}
@@ -3414,7 +3414,7 @@ if ($LocalLlamaCppLinked) {
if (Test-Path -LiteralPath $LlamaCppDir) {
substep "Existing install was restored" "Yellow"
}
- substep "Close Studio or other llama.cpp users and retry" "Yellow"
+ substep "Close Unsloth or other llama.cpp users and retry" "Yellow"
exit 3
} else {
step "llama.cpp" "prebuilt install failed (continuing)" "Yellow"
@@ -4003,7 +4003,7 @@ if ($LocalLlamaCppLinked) {
}
# -- Step E: Build the DiffusionGemma visual server (optional, best-effort) --
- # An example target present on llama.cpp PR #24423; lets Studio serve
+ # An example target present on llama.cpp PR #24423; lets Unsloth serve
# DiffusionGemma GGUFs without DG_VISUAL_BIN. No-op when not configured.
if ($BuildOk) {
$null = cmake --build $BuildDir --config Release --target llama-diffusion-gemma-visual-server -j $NumCpu 2>&1 | Out-String
diff --git a/studio/setup.sh b/studio/setup.sh
index 3d67db3da7..8d47eecfda 100755
--- a/studio/setup.sh
+++ b/studio/setup.sh
@@ -127,13 +127,13 @@ _suggest_npm_registry() {
printf '\n' >&2
step "frontend" "registry.npmjs.org looks blocked (corporate firewall/proxy?)" "$C_WARN" >&2
if [ -n "$_mirror" ]; then
- substep "Studio pins the public npm registry; your mirror is being ignored." >&2
+ substep "Unsloth pins the public npm registry; your mirror is being ignored." >&2
substep "Detected a registry in your npm config:" >&2
substep " $_mirror" >&2
- substep "Re-run pointing Studio at it:" >&2
+ substep "Re-run pointing Unsloth at it:" >&2
substep " UNSLOTH_NPM_REGISTRY=$_mirror ./install.sh --local" >&2
else
- substep "If you use a private mirror/proxy, point Studio at it and re-run:" >&2
+ substep "If you use a private mirror/proxy, point Unsloth at it and re-run:" >&2
substep " UNSLOTH_NPM_REGISTRY=https://your-mirror.example/api/npm/ ./install.sh --local" >&2
fi
substep "(min-release-age and save-exact stay enforced.)" >&2
@@ -396,7 +396,7 @@ _print_cuda_driver_toolkit_mismatch() {
local _driver_major=${_driver_version%%.*}
substep "CUDA Toolkit $_toolkit_version is a major-version mismatch: toolkit major $_toolkit_major exceeds driver CUDA major $_driver_major ($_driver_version)." "$C_WARN"
substep "Update the NVIDIA GPU driver to run CUDA Toolkit $_toolkit_version, or install a CUDA $_driver_major.x toolkit." "$C_WARN"
- substep "Or let Studio use the prebuilt CUDA bundle; it does not need the local toolkit." "$C_WARN"
+ substep "Or let Unsloth use the prebuilt CUDA bundle; it does not need the local toolkit." "$C_WARN"
}
print_llama_error_log() {
@@ -531,7 +531,7 @@ _STUDIO_HOME_IS_CUSTOM=false
if [ "$_studio_home_canon" != "$_LEGACY_STUDIO_HOME" ]; then
_STUDIO_HOME_IS_CUSTOM=true
fi
-# Directory-local evidence Studio created "$1": only prebuilt-installer metadata
+# Directory-local evidence Unsloth created "$1": only prebuilt-installer metadata
# counts (UNSLOTH_PREBUILT_INFO.json for llama.cpp, UNSLOTH_NODE_PREBUILT_INFO.json
# for Node), both written only by our installers. Mirrors the setup.ps1 Node guard.
# A markerless source build stays strict since this runs right before an rm -rf.
@@ -549,7 +549,7 @@ _assert_studio_owned_or_absent() {
: > "$_aso_dir/$_STUDIO_OWNED_MARKER" 2>/dev/null || true
return 0
fi
- echo "ERROR: $_aso_dir already exists and is not marked as a Studio-owned $_aso_label." >&2
+ echo "ERROR: $_aso_dir already exists and is not marked as an Unsloth-owned $_aso_label." >&2
echo " Move it aside or choose an empty UNSLOTH_STUDIO_HOME before re-running." >&2
exit 1
fi
@@ -585,7 +585,7 @@ if [ "$_NEED_FRONTEND_BUILD" = false ] && [ ! -d "$_OXC_DIR" ]; then
else
# ── Node (isolated; never touches the system Node/npm) ──
-# Studio's frontend (Vite 8) needs Node ^20.19 || >=22.12 || >=23 and npm >= 11.
+# Unsloth's frontend (Vite 8) needs Node ^20.19 || >=22.12 || >=23 and npm >= 11.
# Three sources:
# system -- system Node + npm already satisfy both; used read-only.
# bundled -- install a pinned isolated Node under $UNSLOTH_HOME/node, build-only.
@@ -666,9 +666,9 @@ elif [ "$NODE_SOURCE" = bundled ]; then
fi
set -e
if [ "$_NODE_STATUS" -eq 3 ]; then
- step "node" "install blocked by another active Studio install" "$C_ERR"
+ step "node" "install blocked by another active Unsloth install" "$C_ERR"
sed 's/^/ | /' "$_NODE_LOG" >&2; rm -f "$_NODE_LOG"
- substep "close other Studio installs and retry"
+ substep "close other Unsloth installs and retry"
exit 3
elif [ "$_NODE_STATUS" -ne 0 ]; then
step "node" "isolated Node install failed" "$C_ERR"
@@ -692,7 +692,7 @@ elif [ "$NODE_SOURCE" = bundled ]; then
else
_FRONTEND_SKIP=true
step "frontend" "skipped (no suitable Node; system left untouched)" "$C_WARN"
- substep "found Node='${_SYS_NODE_VER:-none}' npm='${_SYS_NPM_VER:-none}'; Studio needs Node >=20.19/22.12/23 and npm >= 11"
+ substep "found Node='${_SYS_NODE_VER:-none}' npm='${_SYS_NPM_VER:-none}'; Unsloth needs Node >=20.19/22.12/23 and npm >= 11"
substep "install a suitable Node + npm, or unset UNSLOTH_SKIP_NODE_INSTALL to let Unsloth manage an isolated Node"
fi
verbose_substep "node source: $NODE_SOURCE (sys node=${_SYS_NODE_VER:-none} npm=${_SYS_NPM_VER:-none}) dir=$NODE_DIR"
@@ -873,11 +873,11 @@ _remove_agent_instruction_files \
_COLAB_NO_VENV=false
if [ ! -x "$VENV_DIR/bin/python" ]; then
if [ "$IS_COLAB" = true ]; then
- # On Colab there is no Studio venv -- install backend deps into system Python.
+ # On Colab there is no Unsloth venv -- install backend deps into system Python.
# Strip all version constraints so pip keeps Colab's pre-installed
# packages (huggingface-hub, datasets, transformers) and only pulls
# in genuinely missing ones (structlog, fastapi, etc.).
- substep "Colab detected, installing Studio backend dependencies..."
+ substep "Colab detected, installing Unsloth backend dependencies..."
_COLAB_REQS_TMP="$(mktemp)"
sed 's/[><=!~;].*//' "$SCRIPT_DIR/backend/requirements/studio.txt" \
| grep -v '^#' | grep -v '^$' > "$_COLAB_REQS_TMP"
@@ -1254,7 +1254,7 @@ _link_local_llama_quantize_shim() {
}
# Accept any layout LlamaCppBackend._layout_candidates() resolves so the flag
-# never rejects a tree Studio could actually run: a root-level llama-server (a
+# never rejects a tree Unsloth could actually run: a root-level llama-server (a
# `make` build or a flat-extracted release) or the CMake build/bin/llama-server.
_has_local_llama_server() {
[ -x "$1/llama-server" ] || [ -x "$1/build/bin/llama-server" ]
@@ -1298,13 +1298,13 @@ if [ -n "${UNSLOTH_LOCAL_LLAMA_CPP_DIR:-}" ]; then
# Reusing disables BOTH the prebuilt download and the source build, so the
# linked tree must already contain a runnable llama-server in one of the
# layouts the backend resolves (root-level or build/bin/). Fail clearly
- # rather than link an unbuilt or wrong-platform checkout and leave Studio
+ # rather than link an unbuilt or wrong-platform checkout and leave Unsloth
# with no usable binary.
if ! _has_local_llama_server "$_RESOLVED_LOCAL"; then
step "llama.cpp" "no llama-server under $_RESOLVED_LOCAL (looked for ./llama-server and ./build/bin/llama-server) -- build llama.cpp there first, or drop --with-llama-cpp-dir" "$C_ERR"
exit 1
fi
- # A stale link from a previous --with-llama-cpp-dir run isn't Studio-owned
+ # A stale link from a previous --with-llama-cpp-dir run isn't Unsloth-owned
# content; drop it before the ownership check so re-runs stay idempotent
# for a custom UNSLOTH_STUDIO_HOME (the assert would otherwise follow the
# link into the user's dir and reject it as unowned).
@@ -1389,7 +1389,7 @@ else
if [ -d "$LLAMA_CPP_DIR" ]; then
substep "existing install was restored"
fi
- substep "close Studio or other llama.cpp users and retry"
+ substep "close Unsloth or other llama.cpp users and retry"
exit 3
else
step "llama.cpp" "prebuilt install failed (continuing)" "$C_WARN"
@@ -1886,7 +1886,7 @@ else
ln -sf build/bin/llama-quantize "$LLAMA_CPP_DIR/llama-quantize"
fi
# DiffusionGemma visual server, if it was built (PR #24423): link next to
- # llama-server so Studio serves DiffusionGemma GGUFs without DG_VISUAL_BIN.
+ # llama-server so Unsloth serves DiffusionGemma GGUFs without DG_VISUAL_BIN.
if [ -f "$LLAMA_CPP_DIR/build/bin/llama-diffusion-gemma-visual-server" ]; then
ln -sf build/bin/llama-diffusion-gemma-visual-server "$LLAMA_CPP_DIR/llama-diffusion-gemma-visual-server"
fi
@@ -1983,7 +1983,7 @@ echo ""
# When called from install.sh (SKIP_STUDIO_BASE=1), exit non-zero so the
# installer can report the GGUF failure after finishing PATH/shortcut setup.
# When called directly via 'unsloth studio update', keep the install
-# successful -- the footer above already reports the limitation and Studio
+# successful -- the footer above already reports the limitation and Unsloth
# is still usable for non-GGUF workflows.
if [ "$_LLAMA_CPP_DEGRADED" = true ] && [ "${SKIP_STUDIO_BASE:-0}" = "1" ]; then
exit 1
diff --git a/studio/src-tauri/src/commands.rs b/studio/src-tauri/src/commands.rs
index 6bc2116786..5d41cb9217 100644
--- a/studio/src-tauri/src/commands.rs
+++ b/studio/src-tauri/src/commands.rs
@@ -25,12 +25,12 @@ fn should_emit_repair_failed(msg: &str) -> bool {
fn external_conflict_message(conflict: &crate::preflight::ExternalBackendConflict) -> String {
if conflict.reason == "desktop_owned_backend_active" {
return format!(
- "A desktop-owned Studio server for this install is already running on port {}. Quit the other desktop app instance, then try again.",
+ "A desktop-owned Unsloth server for this install is already running on port {}. Quit the other desktop app instance, then try again.",
conflict.port
);
}
format!(
- "A Studio server for this install is already running from a terminal on port {}. Stop that server, or run `unsloth studio update` from that terminal before using desktop repair/update.",
+ "An Unsloth server for this install is already running from a terminal on port {}. Stop that server, or run `unsloth studio update` from that terminal before using desktop repair/update.",
conflict.port
)
}
@@ -475,7 +475,7 @@ pub async fn start_backend_update(
.map_err(|e| format!("Update task panicked: {e}"))?
}
-/// Repair a stale managed Studio install.
+/// Repair a stale managed Unsloth install.
#[tauri::command]
pub async fn start_managed_repair(
app: AppHandle,
@@ -522,7 +522,7 @@ pub async fn start_managed_repair(
let repair_group_id = install::take_pending_repair_group_for_resume(&install_state)
.unwrap_or_else(|| diagnostics::begin_repair_group(&diagnostics_state));
- let _ = app.emit("repair-progress", "Updating existing Studio install...");
+ let _ = app.emit("repair-progress", "Updating existing Unsloth install...");
let update_app = app.clone();
let update_state = update_state.inner().clone();
let update_diagnostics = diagnostics_state.clone();
@@ -549,7 +549,7 @@ pub async fn start_managed_repair(
warn!("Managed repair update finished, but preflight is still not ready; falling back to installer");
let _ = app.emit(
"repair-progress",
- "Update finished, but Studio is still not ready. Running bundled installer...",
+ "Update finished, but Unsloth is still not ready. Running bundled installer...",
);
}
Err(msg) => {
@@ -627,7 +627,7 @@ pub async fn start_managed_repair(
return Ok(());
}
- let msg = "Repair finished, but Studio install is still not desktop-ready.".to_string();
+ let msg = "Repair finished, but Unsloth install is still not desktop-ready.".to_string();
error!("{}", msg);
diagnostics::finish_repair_group(
&diagnostics_state,
diff --git a/studio/src-tauri/src/desktop_auth.rs b/studio/src-tauri/src/desktop_auth.rs
index 33605f6c65..db65b8796f 100644
--- a/studio/src-tauri/src/desktop_auth.rs
+++ b/studio/src-tauri/src/desktop_auth.rs
@@ -201,7 +201,7 @@ async fn exchange_desktop_secret(
if response.status() == reqwest::StatusCode::NOT_FOUND {
return Err(AuthError::StaleResponder(
- "Running Studio backend is too old for this desktop app. Update that backend and restart."
+ "Running Unsloth backend is too old for this desktop app. Update that backend and restart."
.to_string(),
));
}
@@ -364,7 +364,7 @@ async fn desktop_auth_inner(
}
Err(
- "Desktop auth failed. Update or repair the managed Studio install, then restart Studio."
+ "Desktop auth failed. Update or repair the managed Unsloth install, then restart Unsloth."
.to_string(),
)
}
@@ -465,7 +465,7 @@ mod tests {
.message();
assert_eq!(
error,
- "Running Studio backend is too old for this desktop app. Update that backend and restart."
+ "Running Unsloth backend is too old for this desktop app. Update that backend and restart."
);
}
}
diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs
index 4ed12051ed..4724317601 100644
--- a/studio/src-tauri/src/main.rs
+++ b/studio/src-tauri/src/main.rs
@@ -107,7 +107,7 @@ fn cleanup_child_processes(app: &tauri::AppHandle) {
}
fn setup_tray(app: &tauri::App) -> Result<(), Box> {
- let open = MenuItemBuilder::with_id("open", "Open Studio").build(app)?;
+ let open = MenuItemBuilder::with_id("open", "Open Unsloth").build(app)?;
let toggle = MenuItemBuilder::with_id("toggle", "Start/Stop Server").build(app)?;
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
let menu = MenuBuilder::new(app)
diff --git a/studio/src-tauri/src/native_path_policy.rs b/studio/src-tauri/src/native_path_policy.rs
index b2ebb34621..b82e516e7a 100644
--- a/studio/src-tauri/src/native_path_policy.rs
+++ b/studio/src-tauri/src/native_path_policy.rs
@@ -191,7 +191,7 @@ fn reject_sensitive_artifact(path: &Path) -> Result<(), String> {
"\\pid",
] {
if lowered.contains(needle) {
- return Err("Sensitive Studio state cannot be registered as an artifact.".to_string());
+ return Err("Sensitive Unsloth state cannot be registered as an artifact.".to_string());
}
}
if let Some(ext) = path.extension().and_then(|ext| ext.to_str()) {
diff --git a/tests/python/test_e2e_no_torch_sandbox.py b/tests/python/test_e2e_no_torch_sandbox.py
index f412de2063..bb61af462d 100644
--- a/tests/python/test_e2e_no_torch_sandbox.py
+++ b/tests/python/test_e2e_no_torch_sandbox.py
@@ -30,7 +30,7 @@ VLM_PROCESSING = DATASETS_DIR / "vlm_processing.py"
ITERABLE = DATASETS_DIR / "iterable.py"
HARDWARE_PY = HARDWARE_DIR / "hardware.py"
-# Studio venv for server tests
+# Unsloth venv for server tests
STUDIO_VENV = Path.home() / ".unsloth" / "studio" / "unsloth_studio"
sys.path.insert(0, str(STUDIO_DIR))
@@ -957,20 +957,20 @@ server = pytest.mark.server
@server
class TestLiveServerStartup:
- """Live server startup against the existing Studio venv with torch made unimportable (pytest -m server)."""
+ """Live server startup against the existing Unsloth venv with torch made unimportable (pytest -m server)."""
@pytest.fixture(autouse = True)
def _check_studio_venv(self):
py = _studio_venv_python()
if py is None:
- pytest.skip("Studio venv not found at ~/.unsloth/studio/unsloth_studio")
+ pytest.skip("Unsloth venv not found at ~/.unsloth/studio/unsloth_studio")
@pytest.fixture(scope = "class")
def server_process(self):
"""Start the studio backend server without torch, yield (proc, port), then stop."""
py = _studio_venv_python()
if py is None:
- pytest.skip("Studio venv not found")
+ pytest.skip("Unsloth venv not found")
port = _server_port()
backend_dir = BACKEND_DIR
diff --git a/tests/python/test_studio_import_no_torch.py b/tests/python/test_studio_import_no_torch.py
index c4efbc8cea..f551519de9 100644
--- a/tests/python/test_studio_import_no_torch.py
+++ b/tests/python/test_studio_import_no_torch.py
@@ -1,4 +1,4 @@
-"""Sandbox tests: Studio dataset modules load/run in isolated no-torch venvs."""
+"""Sandbox tests: Unsloth dataset modules load/run in isolated no-torch venvs."""
from __future__ import annotations
diff --git a/tests/saving/test_prewarm_base_model_hub_cache.py b/tests/saving/test_prewarm_base_model_hub_cache.py
index cbb52863ba..4269f9d61c 100644
--- a/tests/saving/test_prewarm_base_model_hub_cache.py
+++ b/tests/saving/test_prewarm_base_model_hub_cache.py
@@ -4,7 +4,7 @@
"""Regression tests for #6890: repeated base-model downloads across checkpoint exports.
merge_and_overwrite_lora downloads missing 16-bit shards with hf_hub_download(local_dir),
-which never populates the persistent HF hub cache; a temporary merge directory (Studio
+which never populates the persistent HF hub cache; a temporary merge directory (Unsloth
GGUF exports delete it) means every checkpoint export re-downloads the full base model.
_prewarm_base_model_hub_cache snapshot-downloads the base into the hub cache first so
the zoo's cache-copy fast path is hit on later exports.
diff --git a/tests/studio/_playwright_robust.py b/tests/studio/_playwright_robust.py
index a4590066d4..b2f6df751e 100644
--- a/tests/studio/_playwright_robust.py
+++ b/tests/studio/_playwright_robust.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Shared CI-runner workarounds for the Studio Playwright tests (Chromium flags,
+"""Shared CI-runner workarounds for the Unsloth Playwright tests (Chromium flags,
view-transition killer, page recovery, post-action response wait). Imported
directly by the standalone scripts; does NOT depend on pytest.
"""
@@ -130,7 +130,7 @@ def wait_for_health(
timeout = 3.0,
)
last_status, last_body = status, body
- # Accept any 200 -- different Studio builds report status differently.
+ # Accept any 200 -- different Unsloth builds report status differently.
if status == 200:
if info is not None:
info(f"health pre-flight OK: status=200, body keys={list((body or {}).keys())}")
diff --git a/tests/studio/install/smoke_test_parallel_studio_home.py b/tests/studio/install/smoke_test_parallel_studio_home.py
index 9d840355e1..55b6df5190 100644
--- a/tests/studio/install/smoke_test_parallel_studio_home.py
+++ b/tests/studio/install/smoke_test_parallel_studio_home.py
@@ -80,7 +80,7 @@ def _launch_backend(
env = os.environ.copy()
env["HOME"] = str(fake_home)
# Pin UNSLOTH_STUDIO_HOME and clear the alias so the child can't inherit a
- # Studio root from the caller's shell and resolve to the wrong install.
+ # Unsloth root from the caller's shell and resolve to the wrong install.
env["UNSLOTH_STUDIO_HOME"] = str(studio_home)
env.pop("STUDIO_HOME", None)
# Popen dups stdout into the child, so closing the parent's handle here is safe.
diff --git a/tests/studio/install/test_launch_studio_launcher.py b/tests/studio/install/test_launch_studio_launcher.py
index a7396aaf5d..1c12024f6d 100644
--- a/tests/studio/install/test_launch_studio_launcher.py
+++ b/tests/studio/install/test_launch_studio_launcher.py
@@ -1,4 +1,4 @@
-"""Guard install.ps1's Studio launcher against the AV-heuristic shape (Kaspersky
+"""Guard install.ps1's Unsloth launcher against the AV-heuristic shape (Kaspersky
HEUR:Trojan.VBS.Agent.gen): a WScript .vbs spawning a hidden ExecutionPolicy-Bypass PowerShell.
The shortcut must stay windowless via powershell.exe -WindowStyle Hidden over launch-studio.ps1,
never a .vbs/WScript.Shell.Run wrapper, and any pre-existing .vbs must be deleted on upgrade."""
diff --git a/tests/studio/install/test_managed_node_runtime.py b/tests/studio/install/test_managed_node_runtime.py
index 0dbc6788ca..17c7e3e60f 100644
--- a/tests/studio/install/test_managed_node_runtime.py
+++ b/tests/studio/install/test_managed_node_runtime.py
@@ -3,7 +3,7 @@
"""Tests for the runtime managed-Node resolver (studio/backend/utils/node_runtime.py).
-The Studio frontend installer may provision an isolated Node under
+The Unsloth frontend installer may provision an isolated Node under
``/node`` that is never added to the user's PATH. The backend OXC
validator must still find a usable Node at runtime: a version-adequate system
Node, else the managed isolated one. These tests pin that resolution and the
diff --git a/tests/studio/install/test_pr5940_followups.py b/tests/studio/install/test_pr5940_followups.py
index ac6a96167a..d6dc8b2f8e 100644
--- a/tests/studio/install/test_pr5940_followups.py
+++ b/tests/studio/install/test_pr5940_followups.py
@@ -420,7 +420,7 @@ def test_ps_installers_gate_amd_smi_on_windows():
assert (
"UNSLOTH_SETUP_PYTHON" in text
), f"{ps.name} venv-internal check must seed the venv root from UNSLOTH_SETUP_PYTHON"
- # A custom Studio home moves the venv off the default path; it must be
+ # A custom Unsloth home moves the venv off the default path; it must be
# seeded too or its hipInfo escapes the filter and reopens the gate.
assert (
"UNSLOTH_STUDIO_HOME" in text
@@ -429,7 +429,7 @@ def test_ps_installers_gate_amd_smi_on_windows():
@pytest.mark.parametrize("ps", [_INSTALL_PS1, _SETUP_PS1], ids = ["install.ps1", "setup.ps1"])
def test_ps_venv_probe_expands_tilde_for_custom_studio_home(ps):
- # The probe seeds the venv root from a custom Studio home; a ~\studio form
+ # The probe seeds the venv root from a custom Unsloth home; a ~\studio form
# must expand to USERPROFILE like the canonical resolver, else GetFullPath
# keeps the literal ~ (cwd-relative) and the hipInfo escapes the filter.
text = ps.read_text(encoding = "utf-8")
@@ -439,7 +439,7 @@ def test_ps_venv_probe_expands_tilde_for_custom_studio_home(ps):
block = text[i:j]
assert "USERPROFILE" in block and ".Substring(1)" in block, (
f"{ps.name}: the venv-internal probe must expand a leading ~ in the custom "
- "Studio home before seeding the venv root (mirroring the canonical resolver)"
+ "Unsloth home before seeding the venv root (mirroring the canonical resolver)"
)
# The ~ expansion must be guarded on a non-empty USERPROFILE; otherwise
# Join-Path $env:USERPROFILE throws on a service/SYSTEM account with no profile,
diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.py
index 5cabf41f57..e7ac0ec82d 100644
--- a/tests/studio/install/test_rocm_support.py
+++ b/tests/studio/install/test_rocm_support.py
@@ -360,7 +360,7 @@ class TestRuntimePatterns:
install_kind = "windows-hip",
)
patterns = runtime_patterns_for_choice(choice)
- # Narrowed from "*.exe" to the two binaries Studio actually invokes.
+ # Narrowed from "*.exe" to the two binaries Unsloth actually invokes.
assert "llama-server.exe" in patterns
assert "llama-quantize.exe" in patterns
assert "*.dll" in patterns
@@ -378,7 +378,7 @@ class TestRuntimePatterns:
assert "lib*.dylib" in patterns
def test_diffusion_visual_server_kept(self):
- # The DiffusionGemma visual-server must survive the prune so Studio can
+ # The DiffusionGemma visual-server must survive the prune so Unsloth can
# serve DiffusionGemma GGUFs natively.
for kind, name in (
("linux-cuda", "llama-diffusion-gemma-visual-server"),
diff --git a/tests/studio/install/test_selection_logic.py b/tests/studio/install/test_selection_logic.py
index 7575dea581..e372a3bbeb 100644
--- a/tests/studio/install/test_selection_logic.py
+++ b/tests/studio/install/test_selection_logic.py
@@ -270,13 +270,13 @@ def mock_windows_runtime(monkeypatch, lines):
# ===========================================================================
-# Studio run.py localhost warning
+# Unsloth run.py localhost warning
# ===========================================================================
class TestStudioLocalhostIpv6Warning:
def _prepare_loopback(self, run_module, monkeypatch):
- # Studio confirmed answering on the IPv4 loopback.
+ # Unsloth confirmed answering on the IPv4 loopback.
monkeypatch.setattr(
run_module,
"_working_local_url",
@@ -327,7 +327,7 @@ class TestStudioLocalhostIpv6Warning:
assert "http://localhost:8888" in captured.out
def test_ipv6_listener_does_not_suppress_warning(self, monkeypatch):
- # A process on ::1 is NOT Studio (binds 127.0.0.1 only), so the warning must
+ # A process on ::1 is NOT Unsloth (binds 127.0.0.1 only), so the warning must
# still fire -- that is exactly when http://localhost opens the wrong service.
run_module = load_studio_run_module(monkeypatch)
self._prepare_loopback(run_module, monkeypatch)
@@ -356,7 +356,7 @@ class TestStudioLocalhostIpv6Warning:
assert run_module._localhost_ipv6_mismatch_url("127.0.0.1", port) is None
def test_ipv4_not_answering_suppresses_warning(self, monkeypatch):
- # Studio not confirmed on 127.0.0.1 -> no warning.
+ # Unsloth not confirmed on 127.0.0.1 -> no warning.
run_module = load_studio_run_module(monkeypatch)
monkeypatch.setattr(run_module, "_working_local_url", lambda port: None)
self._set_getaddrinfo(monkeypatch, [self._ipv6()])
@@ -3668,7 +3668,7 @@ class TestCpuFallback:
# ===========================================================================
-@pytest.mark.skipif(sys.platform == "win32", reason = "bash-only Studio installer tests")
+@pytest.mark.skipif(sys.platform == "win32", reason = "bash-only Unsloth installer tests")
class TestCudaDriverToolkitMismatchMessage:
_SETUP_SH = PACKAGE_ROOT / "studio" / "setup.sh"
_SETUP_PS1 = PACKAGE_ROOT / "studio" / "setup.ps1"
@@ -3873,7 +3873,7 @@ class TestCudaDriverToolkitMismatchMessage:
"or install a CUDA $driverMajor.x toolkit." in source
)
assert (
- "Or let Studio use the prebuilt CUDA bundle; it does not need the local toolkit."
+ "Or let Unsloth use the prebuilt CUDA bundle; it does not need the local toolkit."
) in source
assert (
"Write-CudaDriverToolkitMismatch -ToolkitVersion $IncompatibleToolkit "
diff --git a/tests/studio/playwright_chat_ime_i18n.py b/tests/studio/playwright_chat_ime_i18n.py
index 9c01e95fd4..5bebf0a9e8 100644
--- a/tests/studio/playwright_chat_ime_i18n.py
+++ b/tests/studio/playwright_chat_ime_i18n.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Studio chat composer IME + multilingual regression smoke.
+"""Unsloth chat composer IME + multilingual regression smoke.
Covers: stuck IME composition (#5318 / PR #5327), multilingual paste round-trip,
stuck compositionend (#5546), and Mac input-method switch recovery (keydown/blur).
diff --git a/tests/studio/playwright_chat_ui.py b/tests/studio/playwright_chat_ui.py
index 065ba7a745..35b18756ff 100644
--- a/tests/studio/playwright_chat_ui.py
+++ b/tests/studio/playwright_chat_ui.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Comprehensive Studio chat UI test, run locally + in CI."""
+"""Comprehensive Unsloth chat UI test, run locally + in CI."""
import json
import os
@@ -156,7 +156,7 @@ with sync_playwright() as p:
# pointer events and break Playwright's actionability check.
reduced_motion = "reduce",
)
- # Hard-disable CSS view-transitions: Studio's theme toggle + sidebar
+ # Hard-disable CSS view-transitions: Unsloth's theme toggle + sidebar
# collapse run startViewTransition() which can leave intercepting
# pointer events for a beat after each route swap. See _playwright_robust.py.
install_view_transition_killer(ctx)
@@ -477,7 +477,7 @@ with sync_playwright() as p:
fail(f"/api/inference/load returned {load_resp['status']}: {load_resp.get('body')!r}")
info(f"loaded model: {(load_resp['body'] or {}).get('display_name')}")
- # Studio caches model state in zustand; reload so the composer picks
+ # Unsloth caches model state in zustand; reload so the composer picks
# up the loaded model.
page.reload()
composer = page.locator('textarea[aria-label="Message input"]')
@@ -493,7 +493,7 @@ with sync_playwright() as p:
# (app-sidebar.tsx) -- as stable as anything in the codebase.
picker_btn = page.locator('[data-tour="chat-model-selector"]').first
if picker_btn.count() == 0:
- # Fall back to text-based locators for older Studio builds.
+ # Fall back to text-based locators for older Unsloth builds.
picker_btn = page.locator(
'button:has-text("gemma-3-270m"), '
'button:has-text("Gemma 3"), '
@@ -893,7 +893,7 @@ with sync_playwright() as p:
if len(observed) < 3:
soft_fail(f"theme toggle ran only {len(observed)} cycle(s), expected 3")
# Don't strict-fail on both polarities: the runner's
- # prefers-color-scheme + Studio's "system" default can collapse
+ # prefers-color-scheme + Unsloth's "system" default can collapse
# to one polarity even when .dark toggles correctly. The 3-cycle
# completion above is the real invariant.
if light_seen and dark_seen:
diff --git a/tests/studio/playwright_extra_ui.py b/tests/studio/playwright_extra_ui.py
index 209a8a06f1..dde6c5d635 100644
--- a/tests/studio/playwright_extra_ui.py
+++ b/tests/studio/playwright_extra_ui.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Studio extra-UI Playwright test: Compare tab, Recipes editor, /export, /studio, Settings tabs."""
+"""Unsloth extra-UI Playwright test: Compare tab, Recipes editor, /export, /studio, Settings tabs."""
import json
import os
@@ -90,11 +90,11 @@ with sync_playwright() as p:
)
install_view_transition_killer(ctx)
page = ctx.new_page()
- # 60s default for slow macos-14 --single-process Chromium (second Studio boot of the job).
+ # 60s default for slow macos-14 --single-process Chromium (second Unsloth boot of the job).
page.set_default_timeout(60_000)
page_errors = []
- # Filter out known-benign React errors (timing artefacts on slow CI runners, not Studio bugs);
+ # Filter out known-benign React errors (timing artefacts on slow CI runners, not Unsloth bugs);
# shared base list lives in _playwright_robust.BENIGN_PAGE_ERROR_PATTERNS.
def _on_pageerror(e):
msg = str(e)
@@ -451,9 +451,9 @@ with sync_playwright() as p:
)
# ─────────────────────────────────────────────────────
- # 4. Studio training route.
+ # 4. Unsloth training route.
# ─────────────────────────────────────────────────────
- step(f"Studio route ({'chat-only redirect' if chat_only else 'tabs + sections'})")
+ step(f"Unsloth route ({'chat-only redirect' if chat_only else 'tabs + sections'})")
page.goto(f"{BASE}/studio")
page.wait_for_timeout(1500)
shoot("08-studio")
diff --git a/tests/studio/run_real_mlx_smoke.py b/tests/studio/run_real_mlx_smoke.py
index 275fe7ac57..63bc0dbba9 100644
--- a/tests/studio/run_real_mlx_smoke.py
+++ b/tests/studio/run_real_mlx_smoke.py
@@ -106,7 +106,7 @@ def _compute_loss_and_grad_norm(model, tokenizer, text: str) -> tuple[float, flo
import mlx.nn as nn
from mlx.utils import tree_flatten
- # Match Studio's text dataset path: no EOS appended behind the user's back.
+ # Match Unsloth's text dataset path: no EOS appended behind the user's back.
ids = list(tokenizer.encode(text))
if len(ids) < 2:
raise RuntimeError(f"text too short to compute loss: {len(ids)} tokens")
diff --git a/tests/studio/studio_api_smoke.py b/tests/studio/studio_api_smoke.py
index 845a9ed021..d30bd11dca 100644
--- a/tests/studio/studio_api_smoke.py
+++ b/tests/studio/studio_api_smoke.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""End-to-end Studio API & Auth HTTP integration tests against an externally-booted Studio."""
+"""End-to-end Unsloth API & Auth HTTP integration tests against an externally-booted Unsloth."""
import json
import os
@@ -571,7 +571,7 @@ EXPECTED_AUTH_ENDPOINTS = [
for method, path in EXPECTED_AUTH_ENDPOINTS:
if (method, path) in PUBLIC:
continue
- # Don't actually shut Studio down: an unauthenticated call must 401/403 before the trigger fires.
+ # Don't actually shut Unsloth down: an unauthenticated call must 401/403 before the trigger fires.
if path == "/api/shutdown":
code, _ = http(method, path)
if code in (401, 403):
@@ -610,6 +610,6 @@ if _failed:
sys.exit(1)
_emit(
"",
- "PASS all Studio API & Auth assertions"
+ "PASS all Unsloth API & Auth assertions"
+ (f" ({len(_warned)} audit findings logged)" if _warned else ""),
)
diff --git a/tests/studio/test_auth_form_input_count.py b/tests/studio/test_auth_form_input_count.py
index 4d5d72d20b..75e6cfd1fb 100644
--- a/tests/studio/test_auth_form_input_count.py
+++ b/tests/studio/test_auth_form_input_count.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-"""Fast source and runtime contracts for Studio's frontend authentication flows.
+"""Fast source and runtime contracts for Unsloth's frontend authentication flows.
PR #5490 added a third "Current password" input, regressing first-boot UX to
three inputs; PR #5545 restores two by rendering it only when BOOTSTRAP is absent.
diff --git a/tests/studio/test_chat_title_generation.py b/tests/studio/test_chat_title_generation.py
index b568a51400..6a47cfbce4 100644
--- a/tests/studio/test_chat_title_generation.py
+++ b/tests/studio/test_chat_title_generation.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
-"""Regression checks for Studio chat title generation context."""
+"""Regression checks for Unsloth chat title generation context."""
from __future__ import annotations
diff --git a/tests/studio/test_cli_studio_stop_windows.py b/tests/studio/test_cli_studio_stop_windows.py
index 778679c73b..2267d7feda 100644
--- a/tests/studio/test_cli_studio_stop_windows.py
+++ b/tests/studio/test_cli_studio_stop_windows.py
@@ -5,7 +5,7 @@
`stop` once used `os.kill(pid, 0)`, which raises WinError 87 on Windows before
reaching taskkill; the fix adds cross-platform `_pid_alive` (tasklist on Windows,
-signal-0 elsewhere). AST + mock-only; no real processes, no Studio deps imported.
+signal-0 elsewhere). AST + mock-only; no real processes, no Unsloth deps imported.
"""
import ast
diff --git a/tests/studio/test_hardware_dispatch_matrix.py b/tests/studio/test_hardware_dispatch_matrix.py
index bccddac967..19619bc318 100644
--- a/tests/studio/test_hardware_dispatch_matrix.py
+++ b/tests/studio/test_hardware_dispatch_matrix.py
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only
-"""Studio hardware dispatch matrix: spoofs platform/torch/mlx per PROFILES to exercise CUDA/ROCm/XPU/MLX/CPU paths without real hardware."""
+"""Unsloth hardware dispatch matrix: spoofs platform/torch/mlx per PROFILES to exercise CUDA/ROCm/XPU/MLX/CPU paths without real hardware."""
from __future__ import annotations
@@ -31,9 +31,9 @@ class HardwareProfile:
mps_available: bool # torch.backends.mps.is_available() value
expect_is_mlx: bool # unsloth._IS_MLX
- expect_device_type: str # Studio DeviceType (uppercased name: "CUDA"/"XPU"/"MLX"/"CPU")
- expect_is_rocm: bool # Studio IS_ROCM
- expect_apple_silicon: bool # Studio is_apple_silicon()
+ expect_device_type: str # Unsloth DeviceType (uppercased name: "CUDA"/"XPU"/"MLX"/"CPU")
+ expect_is_rocm: bool # Unsloth IS_ROCM
+ expect_apple_silicon: bool # Unsloth is_apple_silicon()
extra_notes: str = ""
@@ -66,7 +66,7 @@ PROFILES = [
expect_is_rocm = True,
expect_apple_silicon = False,
extra_notes = "PyTorch ROCm reuses torch.cuda.* over HIP; "
- "Studio still uses DeviceType.CUDA but flips IS_ROCM=True.",
+ "Unsloth still uses DeviceType.CUDA but flips IS_ROCM=True.",
),
HardwareProfile(
name = "intel_xpu",
@@ -154,7 +154,7 @@ def spoof_hardware(monkeypatch):
import platform
import torch
- # platform spoof (used by both the unsloth gate and Studio's helpers)
+ # platform spoof (used by both the unsloth gate and Unsloth's helpers)
monkeypatch.setattr(platform, "system", lambda: profile.system)
monkeypatch.setattr(platform, "machine", lambda: profile.machine)
@@ -227,7 +227,7 @@ def spoof_hardware(monkeypatch):
monkeypatch.setattr(importlib.util, "find_spec", _no_mlx)
- # Studio's _has_mlx() does `import mlx.core`, not find_spec; block it
+ # Unsloth's _has_mlx() does `import mlx.core`, not find_spec; block it
# with a meta_path finder that raises ImportError for mlx.*.
class _BlockMLXFinder:
def find_spec(
@@ -266,7 +266,7 @@ def _evaluate_unsloth_is_mlx_gate() -> bool:
def _import_studio_hardware_module():
- """Lazy-load Studio's hardware module under the bare-imports layout."""
+ """Lazy-load Unsloth's hardware module under the bare-imports layout."""
if str(STUDIO_BACKEND) not in sys.path:
sys.path.insert(0, str(STUDIO_BACKEND))
# Fresh import so detect_hardware re-runs under the current spoofs.
@@ -290,7 +290,7 @@ def test_unsloth_is_mlx_gate_matches_profile(profile, spoof_hardware):
@pytest.mark.parametrize("profile", PROFILES, ids = PROFILE_IDS)
def test_studio_detect_hardware_matches_profile(profile, spoof_hardware):
- """Studio's detect_hardware() routes to the right DeviceType per profile."""
+ """Unsloth's detect_hardware() routes to the right DeviceType per profile."""
spoof_hardware(profile)
hw = _import_studio_hardware_module()
detected = hw.detect_hardware()
@@ -306,7 +306,7 @@ def test_studio_detect_hardware_matches_profile(profile, spoof_hardware):
@pytest.mark.parametrize("profile", PROFILES, ids = PROFILE_IDS)
def test_studio_is_apple_silicon_matches_profile(profile, spoof_hardware):
- """Studio's is_apple_silicon() helper agrees with platform spoof."""
+ """Unsloth's is_apple_silicon() helper agrees with platform spoof."""
spoof_hardware(profile)
hw = _import_studio_hardware_module()
assert hw.is_apple_silicon() is profile.expect_apple_silicon, (
diff --git a/tests/studio/test_is_mlx_dispatch_gate.py b/tests/studio/test_is_mlx_dispatch_gate.py
index f31f6d1655..0e5de1b789 100644
--- a/tests/studio/test_is_mlx_dispatch_gate.py
+++ b/tests/studio/test_is_mlx_dispatch_gate.py
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: AGPL-3.0-only
-"""Regression tests for the CUDA-vs-MLX dispatch gates Studio relies on.
+"""Regression tests for the CUDA-vs-MLX dispatch gates Unsloth relies on.
Two gates: (1) ``unsloth._IS_MLX`` (import-time, delegates to the zoo MLX
runtime gate behind a local precheck barrier); (2)
@@ -147,7 +147,7 @@ def test_is_mlx_gate_false_on_non_apple_silicon():
def _import_studio_hardware():
- """Lazy import of the Studio hardware module (studio/backend on sys.path)."""
+ """Lazy import of the Unsloth hardware module (studio/backend on sys.path)."""
studio_backend = REPO_ROOT / "studio" / "backend"
if str(studio_backend) not in sys.path:
sys.path.insert(0, str(studio_backend))
diff --git a/tests/studio/test_llama_cpp_wall_clock_cap.py b/tests/studio/test_llama_cpp_wall_clock_cap.py
index f173cfbc55..b7b6917092 100644
--- a/tests/studio/test_llama_cpp_wall_clock_cap.py
+++ b/tests/studio/test_llama_cpp_wall_clock_cap.py
@@ -1,4 +1,4 @@
-"""Timeout policy checks for Studio's local llama-server path."""
+"""Timeout policy checks for Unsloth's local llama-server path."""
from __future__ import annotations
diff --git a/tests/studio/test_locale_root_direction_contract.py b/tests/studio/test_locale_root_direction_contract.py
index 1baabdbbca..20d10aa805 100644
--- a/tests/studio/test_locale_root_direction_contract.py
+++ b/tests/studio/test_locale_root_direction_contract.py
@@ -1,4 +1,4 @@
-"""Regression guard for locale changes affecting the entire Studio layout."""
+"""Regression guard for locale changes affecting the entire Unsloth layout."""
from pathlib import Path
diff --git a/tests/studio/test_node_decision.ps1 b/tests/studio/test_node_decision.ps1
index bd5d5c8677..44f3ef0e1e 100644
--- a/tests/studio/test_node_decision.ps1
+++ b/tests/studio/test_node_decision.ps1
@@ -60,7 +60,7 @@ $globalBunOffset = $source.IndexOf('npm install -g bun')
Check "NodeSource initialized before SKIP_STUDIO_FRONTEND branch" (
$nodeSourceOffset -ge 0 -and $skipFrontendBranchOffset -ge 0 -and $nodeSourceOffset -lt $skipFrontendBranchOffset
)
-Check "custom Studio home validated before Node parent creation" (
+Check "custom Unsloth home validated before Node parent creation" (
$customHomeErrorOffset -ge 0 -and $nodeParentMkdirOffset -ge 0 -and $customHomeErrorOffset -lt $nodeParentMkdirOffset
)
Check "bundled Node pins npm prefix and clears NODE_PATH" (
diff --git a/tests/studio/test_studio_gguf_export_script_pin.py b/tests/studio/test_studio_gguf_export_script_pin.py
index 1f7e7adaa4..defd0d49d4 100644
--- a/tests/studio/test_studio_gguf_export_script_pin.py
+++ b/tests/studio/test_studio_gguf_export_script_pin.py
@@ -1,4 +1,4 @@
-"""Studio GGUF export pins convert_hf_to_gguf.py via UNSLOTH_LLAMA_CPP_SCRIPTS_DIR, with a once-per-process warning fallback when unsloth_zoo lacks the local-script resolver."""
+"""Unsloth GGUF export pins convert_hf_to_gguf.py via UNSLOTH_LLAMA_CPP_SCRIPTS_DIR, with a once-per-process warning fallback when unsloth_zoo lacks the local-script resolver."""
from __future__ import annotations
diff --git a/tests/studio/test_studio_text_descender_clipping.py b/tests/studio/test_studio_text_descender_clipping.py
index 73d1244ee3..359ca4873a 100644
--- a/tests/studio/test_studio_text_descender_clipping.py
+++ b/tests/studio/test_studio_text_descender_clipping.py
@@ -1,4 +1,4 @@
-"""Regression guard: Studio text spans must not pair `leading-none` with
+"""Regression guard: Unsloth text spans must not pair `leading-none` with
`truncate`, which clips glyph descenders (g, p, q, y, j) in visible labels.
"""
diff --git a/tests/test_studio_install_workspace_guard.py b/tests/test_studio_install_workspace_guard.py
index 89836cfb0d..ea5b34672c 100644
--- a/tests/test_studio_install_workspace_guard.py
+++ b/tests/test_studio_install_workspace_guard.py
@@ -1,4 +1,4 @@
-"""install.sh/install.ps1 must refuse to rm -rf an existing Studio venv in env-mode without a sentinel."""
+"""install.sh/install.ps1 must refuse to rm -rf an existing Unsloth venv in env-mode without a sentinel."""
from __future__ import annotations
@@ -127,7 +127,7 @@ def test_install_ps1_has_matching_env_mode_guard():
), "install.ps1 must gate Remove-Item $VenvDir on env-mode"
assert "share\\studio.conf" in block, "install.ps1 guard must check share\\studio.conf sentinel"
assert "bin\\unsloth.exe" in block, "install.ps1 guard must check bin\\unsloth.exe sentinel"
- assert "Refusing to delete non-Studio venv" in block
+ assert "Refusing to delete non-Unsloth venv" in block
def test_setup_ps1_has_writability_probe():
@@ -160,7 +160,7 @@ def test_env_mode_blocks_when_bin_unsloth_is_a_directory(tmp_path):
capture_output = True,
)
assert res.returncode != 0, (
- "directory at bin/unsloth must NOT satisfy the Studio sentinel; "
+ "directory at bin/unsloth must NOT satisfy the Unsloth sentinel; "
f"stdout={res.stdout!r} stderr={res.stderr!r}"
)
assert (venv / "important.txt").is_file(), "unrelated workspace data must survive"
@@ -205,7 +205,7 @@ def test_install_ps1_sentinel_uses_pathtype_leaf():
def test_setup_ps1_stale_venv_has_env_mode_guard():
- """setup.ps1 stale-venv branch must gate Remove-Item $VenvDir on a custom-root Studio sentinel."""
+ """setup.ps1 stale-venv branch must gate Remove-Item $VenvDir on a custom-root Unsloth sentinel."""
src = SETUP_PS1.read_text()
idx = src.index("Stale venv detected")
block = src[idx : idx + 1500]
diff --git a/tests/test_studio_root_resilience.py b/tests/test_studio_root_resilience.py
index 0dfb826376..779ff2f3f1 100644
--- a/tests/test_studio_root_resilience.py
+++ b/tests/test_studio_root_resilience.py
@@ -1,4 +1,4 @@
-"""Studio install-root inference must not crash under hostile filesystem conditions (PermissionError/OSError swallowed; custom root kept when resolve() fails)."""
+"""Unsloth install-root inference must not crash under hostile filesystem conditions (PermissionError/OSError swallowed; custom root kept when resolve() fails)."""
from __future__ import annotations
diff --git a/tests/test_studio_shutdown_thread_wait.py b/tests/test_studio_shutdown_thread_wait.py
index 4ec2afc0f0..8299116d9a 100644
--- a/tests/test_studio_shutdown_thread_wait.py
+++ b/tests/test_studio_shutdown_thread_wait.py
@@ -130,4 +130,4 @@ def test_cli_entrypoints_wait_before_returning_to_shell():
assert (
_calls_shutdown_wait_getattr(tree) >= 3
- ), "Studio CLI terminal paths must wait for the backend thread after requesting shutdown"
+ ), "Unsloth CLI terminal paths must wait for the backend thread after requesting shutdown"
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index bd612db1d5..f47c78ba80 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -2047,7 +2047,7 @@ def get_chat_template(
.replace("'assistant'", "'" + mapping["assistant"] + "'")
if use_zoo_tokenizer_patch:
- # Studio MLX avoids the model-utils tokenizer wrapper because that
+ # Unsloth MLX avoids the model-utils tokenizer wrapper because that
# import path pulls in Torch/GPU-specific modules before MLX training.
from unsloth_zoo.tokenizer_utils import patch_tokenizer
else:
diff --git a/unsloth/import_fixes.py b/unsloth/import_fixes.py
index 09de248c7b..5d54815705 100644
--- a/unsloth/import_fixes.py
+++ b/unsloth/import_fixes.py
@@ -3008,7 +3008,7 @@ def maybe_set_windows_rocm_bnb_version():
No-op unless ALL of: Windows, a real HIP torch build (env hints like
HIP_PATH do not count), a ROCm DLL installed, and no explicit user value.
- Linux is untouched. Values seeded by Studio's venv sitecustomize.py
+ Linux is untouched. Values seeded by Unsloth's venv sitecustomize.py
(marked ``UNSLOTH_BNB_ROCM_VERSION_SOURCE=sitecustomize``) are
redetectable defaults, not overrides; ``UNSLOTH_SKIP_BNB_ROCM_VERSION=1``
opts out and drops a seeded default. Returns the value set, else None.
diff --git a/unsloth/models/loader_utils.py b/unsloth/models/loader_utils.py
index 1e9cc641e9..7661b0d714 100644
--- a/unsloth/models/loader_utils.py
+++ b/unsloth/models/loader_utils.py
@@ -821,7 +821,7 @@ def _exclude_rope_inv_freq_from_ddp(model):
# =============================================================================
# Offline loading - single source of truth (shared by vision.py, loader.py and
-# the Studio exporter). Decide offline ONCE at the load boundary and force it
+# the Unsloth exporter). Decide offline ONCE at the load boundary and force it
# ONCE around the whole load, so every nested HF call inherits it.
# =============================================================================
diff --git a/unsloth/save.py b/unsloth/save.py
index 7d5774aa97..0e2650b174 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -228,7 +228,7 @@ def _loaded_via_remote_code(obj):
Transformers loads auto_map code into the ``transformers_modules`` package, so a
``transformers_modules`` class proves the original load actually ran that remote code
- (which the caller's / Studio's consent gate scans at load time). Export paths derive their
+ (which the caller's / Unsloth's consent gate scans at load time). Export paths derive their
reload trust_remote_code from this - the already approved load decision - instead of from a
checkpoint's static ``auto_map``: a model that loads with built-in classes must not have its
unvetted remote code run when it is re-read during quantization export. Walks PEFT / wrapper
@@ -3858,7 +3858,7 @@ def _prewarm_base_model_hub_cache(
from huggingface_hub import HfFileSystem, hf_hub_download, snapshot_download
# Resolve the cache from the live env like the merge, not huggingface_hub's frozen
- # constants: a runtime cache redirect (read-only default, Studio) would else miss (#6890).
+ # constants: a runtime cache redirect (read-only default, Unsloth) would else miss (#6890).
try:
from unsloth_zoo.hf_cache import _active_caches
_hub_cache = _active_caches()[1]
diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py
index d6e8247ec4..c7f61288d5 100644
--- a/unsloth/tokenizer_utils.py
+++ b/unsloth/tokenizer_utils.py
@@ -1476,7 +1476,7 @@ def get_tokenizer_info(tokenizer) -> dict:
"""Return a concise diagnostic summary of a tokenizer instance.
Collects key properties into a JSON-safe dict for logging, debugging, or the
- Studio UI. Missing attributes fall back to ``None`` rather than raising.
+ Unsloth UI. Missing attributes fall back to ``None`` rather than raising.
Example output::
diff --git a/unsloth_cli/__init__.py b/unsloth_cli/__init__.py
index b3831f5314..121b26f03f 100644
--- a/unsloth_cli/__init__.py
+++ b/unsloth_cli/__init__.py
@@ -81,7 +81,7 @@ app.add_typer(studio_app, name = "studio", help = "Unsloth Studio commands.")
app.add_typer(
start_app,
name = "start",
- help = "Start a coding agent (Claude, Codex, OpenClaw, OpenCode, Hermes, Pi) against Studio.",
+ help = "Start a coding agent (Claude, Codex, OpenClaw, OpenCode, Hermes, Pi) against Unsloth.",
)
# Backwards-compatible hidden alias: `unsloth connect` routes to `unsloth start`.
app.add_typer(
diff --git a/unsloth_cli/_inference.py b/unsloth_cli/_inference.py
index c3b188710e..551bef4787 100644
--- a/unsloth_cli/_inference.py
+++ b/unsloth_cli/_inference.py
@@ -18,7 +18,7 @@ _THINK_OPEN = ""
_THINK_BLOCK = re.compile(rf"{re.escape(_THINK_OPEN)}.*? ", re.DOTALL)
_STREAMED_ERROR_PREFIX = "Error: "
-# Cloudflare (in front of remote Studio proxies like RunPod) 403s the default
+# Cloudflare (in front of remote Unsloth proxies like RunPod) 403s the default
# "Python-urllib/X.Y" User-Agent as a bot; send a real one on every request.
_USER_AGENT = "unsloth-cli"
_MPI_ENV_PAIRS = (
@@ -36,7 +36,7 @@ _no_redirect_opener = None
def urlopen_no_redirect(request, timeout):
"""urlopen that errors on any redirect: following a 3xx would send a bearer
token (or accept an identity proof) to a base we never vetted, letting a port
- squatter relay a real Studio's response."""
+ squatter relay a real Unsloth's response."""
global _no_redirect_opener
if _no_redirect_opener is None:
import urllib.error
@@ -540,7 +540,7 @@ def find_studio_server(timeout: float = 3.0) -> Optional[str]:
def is_loopback_url(base: str) -> bool:
"""True only when *base* resolves to loopback. find_studio_server() trusts a
base after only a health probe, so credentials are auto-sent only to loopback
- (a local Studio or an SSH tunnel on 127.0.0.1), the targets the auto flows mean."""
+ (a local Unsloth or an SSH tunnel on 127.0.0.1), the targets the auto flows mean."""
from urllib.parse import urlparse
host = (urlparse(base).hostname or "").lower()
@@ -554,7 +554,7 @@ def is_loopback_url(base: str) -> bool:
def verify_studio_identity(base: str, timeout: float = 3.0) -> bool:
- """Confirm `base` is really this machine's Studio before sending a secret.
+ """Confirm `base` is really this machine's Unsloth before sending a secret.
Send a random nonce to /api/auth/identity and check the returned HMAC against
the one computed from the local same-user secret; an endpoint without that
@@ -578,7 +578,7 @@ def verify_studio_identity(base: str, timeout: float = 3.0) -> bool:
port = parsed.port or (443 if parsed.scheme == "https" else 80)
# Resolve to one concrete address and talk to *that* address, then bind the
# proof to (address, port). A name like localhost can resolve to a squatter on
- # ::1 while the real Studio is on 127.0.0.1; connecting to the resolved IP and
+ # ::1 while the real Unsloth is on 127.0.0.1; connecting to the resolved IP and
# binding to it means a proof relayed from a different address/port won't match.
try:
ip = socket.getaddrinfo(host, port, type = socket.SOCK_STREAM)[0][4][0]
@@ -592,7 +592,7 @@ def verify_studio_identity(base: str, timeout: float = 3.0) -> bool:
headers = {"User-Agent": _USER_AGENT, "Host": parsed.netloc},
)
try:
- # No redirects: a 302 could relay a real Studio's proof (see urlopen_no_redirect).
+ # No redirects: a 302 could relay a real Unsloth's proof (see urlopen_no_redirect).
# Cap the read: the server is still unverified, so don't trust its length.
with urlopen_no_redirect(request, timeout = timeout) as response:
proof = json.loads(response.read(65536).decode() or "{}").get("proof")
@@ -623,7 +623,7 @@ def _studio_token() -> Optional[str]:
class HttpChatBackend:
- """Chat against a running Studio server over its OpenAI-compatible API.
+ """Chat against a running Unsloth server over its OpenAI-compatible API.
close() leaves the model loaded on purpose — the next session (or the
UI) starts instantly.
@@ -666,7 +666,7 @@ class HttpChatBackend:
tensor_parallel: bool = False,
llama_extra_args: Optional[List[str]] = None,
) -> None:
- typer.echo(f"Loading {model} on the Studio server", err = True)
+ typer.echo(f"Loading {model} on the Unsloth server", err = True)
payload = {
"model_path": model,
"hf_token": hf_token,
@@ -769,7 +769,7 @@ def connect_studio_server(
tensor_parallel: bool = False,
llama_extra_args: Optional[List[str]] = None,
):
- """Backend on a running Studio server, or None (caller loads locally)."""
+ """Backend on a running Unsloth server, or None (caller loads locally)."""
base_url = find_studio_server()
if not base_url:
return None
@@ -782,20 +782,20 @@ def connect_studio_server(
if not explicit:
return None
typer.echo(
- f"Can't attach to the Studio server at {base_url}: {reason} Run Studio "
+ f"Can't attach to the Unsloth server at {base_url}: {reason} Run Unsloth "
"on this machine, or unset UNSLOTH_STUDIO_URL to load the model locally.",
err = True,
)
raise typer.Exit(code = 1)
# Only hand the self-issued JWT (signed with the local secret) to loopback: a
- # remote URL is unverified and a real remote Studio would reject it anyway.
+ # remote URL is unverified and a real remote Unsloth would reject it anyway.
if not is_loopback_url(base_url):
return _refuse(
- "it isn't a local Studio, so a self-issued token can't "
+ "it isn't a local Unsloth, so a self-issued token can't "
"authenticate to it and must not be sent to it."
)
- # Confirm the loopback responder is really our Studio (not a port squatter).
+ # Confirm the loopback responder is really our Unsloth (not a port squatter).
if not verify_studio_identity(base_url):
return _refuse(
"its identity couldn't be verified (it may be running as a "
@@ -803,7 +803,7 @@ def connect_studio_server(
)
token = _studio_token()
if not token:
- return _refuse("couldn't self-issue a Studio token (is Studio set up here?).")
+ return _refuse("couldn't self-issue an Unsloth token (is Unsloth set up here?).")
backend = HttpChatBackend(base_url, token)
backend.ensure_loaded(
model,
diff --git a/unsloth_cli/commands/chat.py b/unsloth_cli/commands/chat.py
index bc4a72f36c..bba5fab08e 100644
--- a/unsloth_cli/commands/chat.py
+++ b/unsloth_cli/commands/chat.py
@@ -206,7 +206,7 @@ def chat(
no_server: bool = typer.Option(
False,
"--no-server",
- help = "Load the model in-process even if a Studio server is running.",
+ help = "Load the model in-process even if an Unsloth server is running.",
),
):
"""Start an interactive chat with a model (loads once, stays warm)."""
@@ -262,14 +262,14 @@ def chat(
llama_extra_args = llama_extra_args,
)
- # Prefer a running Studio server: instant starts, model shared with the UI.
+ # Prefer a running Unsloth server: instant starts, model shared with the UI.
chat_backend = (
None if (no_server or is_mlx_distributed) else connect_studio_server(model, **load_opts)
)
server_mode = chat_backend is not None
if server_mode and should_print:
console.print(
- "(Studio server connected — model stays warm after /exit)",
+ "(Unsloth server connected — model stays warm after /exit)",
style = "bright_black",
)
else:
diff --git a/unsloth_cli/commands/inference.py b/unsloth_cli/commands/inference.py
index 84a126163e..524d8fd015 100644
--- a/unsloth_cli/commands/inference.py
+++ b/unsloth_cli/commands/inference.py
@@ -67,7 +67,7 @@ def inference(
no_server: bool = typer.Option(
False,
"--no-server",
- help = "Load the model in-process even if a Studio server is running.",
+ help = "Load the model in-process even if an Unsloth server is running.",
),
):
"""Run a single inference using the specified model."""
@@ -85,7 +85,7 @@ def inference(
)
raise typer.Exit(code = 1)
- # A running Studio server keeps the model warm between runs. Under
+ # A running Unsloth server keeps the model warm between runs. Under
# mlx.launch, every rank must enter the local MLX path instead of rank 0
# alone talking to a server.
load_opts = dict(
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.py
index fa39fdf761..9257a7fbcb 100644
--- a/unsloth_cli/commands/start.py
+++ b/unsloth_cli/commands/start.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""`unsloth start` — launch a coding agent against a running Studio server."""
+"""`unsloth start` — launch a coding agent against a running Unsloth server."""
import atexit
import contextlib
@@ -35,7 +35,7 @@ from unsloth_cli._inference import (
)
start_app = typer.Typer(
- help = "Start a coding agent against a running Studio server.",
+ help = "Start a coding agent against a running Unsloth server.",
no_args_is_help = True,
context_settings = {"help_option_names": ["-h", "--help"]},
)
@@ -75,14 +75,14 @@ _CLAUDE_ENV_UNSET = ("ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN")
# Shared by every agent command; only the config/env/command differ.
_MODEL_OPTION = typer.Option(
- None, "--model", "-m", help = "Model for the agent; defaults to the one loaded in Studio."
+ None, "--model", "-m", help = "Model for the agent; defaults to the one loaded in Unsloth."
)
_KEY_OPTION = typer.Option(
None,
"--api-key",
envvar = "UNSLOTH_API_KEY",
help = (
- "Studio API key. For a local Studio it is minted automatically and "
+ "Unsloth API key. For a local Unsloth it is minted automatically and "
"remembered per server. For a remote server, pass one with --api-key "
"(or UNSLOTH_API_KEY); it is remembered for next time."
),
@@ -96,7 +96,7 @@ _SERVE_OPTION = typer.Option(
True,
"--serve/--no-serve",
help = (
- "If no Studio server is running, auto-start one for --model and stop it when the "
+ "If no Unsloth server is running, auto-start one for --model and stop it when the "
"agent exits. --no-serve keeps the old behavior of erroring out."
),
)
@@ -347,7 +347,7 @@ def _shutdown_auto_served() -> None:
global _auto_served_server
server, _auto_served_server = _auto_served_server, None
if server is not None and server.poll() is None:
- typer.echo("Stopping the auto-started Studio server…")
+ typer.echo("Stopping the auto-started Unsloth server…")
_shutdown_server(server)
@@ -381,7 +381,7 @@ def _start_studio_server(base: str, model: str, load: LoadOptions) -> subprocess
log_path = Path(tempfile.gettempdir()) / f"unsloth-start-server-{os.getpid()}.log"
typer.echo(
- f"No Studio server at {base}. Starting one for {model} (loading the model can take a while)…"
+ f"No Unsloth server at {base}. Starting one for {model} (loading the model can take a while)…"
)
typer.echo(f"Server log: {log_path}")
# 0600: the `unsloth run` banner in this log carries the minted sk-unsloth- key, and
@@ -408,16 +408,16 @@ def _start_studio_server(base: str, model: str, load: LoadOptions) -> subprocess
if server.poll() is not None:
tail = _log_tail(log_path)
_shutdown_auto_served()
- _fail(f"The Studio server stopped before it was ready. Last log lines:\n{tail}")
+ _fail(f"The Unsloth server stopped before it was ready. Last log lines:\n{tail}")
# `unsloth run` prints the minted key only after the server is up AND the model is
# loaded, so it is the fully-ready signal (same contract serve-unsloth-run.sh uses).
if _studio_healthy(base) and "sk-unsloth-" in _log_tail(log_path, lines = 400):
- typer.echo(f"Studio server ready at {base}.")
+ typer.echo(f"Unsloth server ready at {base}.")
return server
time.sleep(2.0)
_shutdown_auto_served()
_fail(
- f"The Studio server didn't become ready within {_SERVER_START_TIMEOUT_S}s. See {log_path}."
+ f"The Unsloth server didn't become ready within {_SERVER_START_TIMEOUT_S}s. See {log_path}."
)
@@ -463,7 +463,7 @@ def _require_studio(
return expected, _start_studio_server(expected, model, load or LoadOptions())
model_hint = "" if model else " Pass --model to have it start one for you, or"
_fail(
- f"No running Studio server found at {expected}.{model_hint} start one with "
+ f"No running Unsloth server found at {expected}.{model_hint} start one with "
"`unsloth studio`, or point UNSLOTH_STUDIO_URL at a remote server."
)
@@ -567,12 +567,12 @@ def _key_accepted(base: str, key: str) -> bool:
if exc.code in (401, 403):
return False
_fail(
- f"Studio server error while checking an API key ({exc.code}). "
+ f"Unsloth server error while checking an API key ({exc.code}). "
"The server may be starting up or unhealthy; try again shortly."
)
except (urllib.error.URLError, TimeoutError) as exc:
_fail(
- "Couldn't reach the Studio server while checking an API key: "
+ "Couldn't reach the Unsloth server while checking an API key: "
f"{getattr(exc, 'reason', None) or exc}"
)
@@ -592,10 +592,10 @@ def _agent_api_key(
# UNSLOTH_API_KEY meant for some other server must not fail the
# launch: the loopback mint path below is guaranteed to work.
# (An explicit key that the fresh server accepts, e.g. one persisted
- # in this Studio home's auth db, is still honored above.)
+ # in this Unsloth home's auth db, is still honored above.)
# Replay a key the user saved for *this exact* server first (scoped per base,
- # so it only goes back there -- including a remote/SSH-tunnelled Studio whose
+ # so it only goes back there -- including a remote/SSH-tunnelled Unsloth whose
# secret the local handshake can't match). Skip ones the server rejects.
for key in _cached_keys(cache, base, "saved"):
if _key_accepted(base, key):
@@ -608,15 +608,15 @@ def _agent_api_key(
if not is_loopback_url(base):
_fail(
f"No saved API key for {base} and automatic minting only runs against "
- "a local Studio. Create an API key in Studio → Settings → API and "
+ "a local Unsloth. Create an API key in Unsloth → Settings → API and "
"pass it with --api-key (it is remembered per server), or set "
"UNSLOTH_API_KEY."
)
if not verify_studio_identity(base):
_fail(
- f"Couldn't verify that {base} is your Studio (it may be running as a "
+ f"Couldn't verify that {base} is your Unsloth (it may be running as a "
"different OS user, or another process took the port). Create an API "
- "key in Studio → Settings → API and pass it with --api-key, or set "
+ "key in Unsloth → Settings → API and pass it with --api-key, or set "
"UNSLOTH_API_KEY."
)
@@ -630,8 +630,8 @@ def _agent_api_key(
token = _studio_token()
if token is None:
_fail(
- "Couldn't authenticate with the Studio server automatically. Create "
- "an API key in Studio → Settings → API and pass it with --api-key, "
+ "Couldn't authenticate with the Unsloth server automatically. Create "
+ "an API key in Unsloth → Settings → API and pass it with --api-key, "
"or set UNSLOTH_API_KEY."
)
key = _http_json(
@@ -664,7 +664,7 @@ def _is_hub_model_id(value: object) -> bool:
return False
# A hub id is exactly "namespace/name" over a restricted charset. Anything with
# extra path segments (e.g. a server-side relative path such as
- # models/Llama/Foo.gguf on a remote Studio) is not a hub id and must not be
+ # models/Llama/Foo.gguf on a remote Unsloth) is not a hub id and must not be
# casefold-matched against a differently cased path on a case-sensitive
# filesystem. This is host independent, unlike the existence probe below which
# cannot see a path that only exists on the server.
@@ -690,8 +690,8 @@ def _model_id_matches(
if actual == requested:
return True
# Case-insensitive matching is only safe when the local existence probe in
- # _is_hub_model_id is authoritative, i.e. against a loopback Studio on this host.
- # Against a remote Studio a two-segment string is indistinguishable from a
+ # _is_hub_model_id is authoritative, i.e. against a loopback Unsloth on this host.
+ # Against a remote Unsloth a two-segment string is indistinguishable from a
# server-side relative path (e.g. Models/Foo vs models/foo), so casefolding it
# could attach to the wrong model on a case-sensitive server; defer to an exact
# match there and let the load endpoint resolve the requested path.
@@ -709,7 +709,7 @@ def _resolve_model(
load: LoadOptions = LoadOptions(),
) -> dict:
models = _loaded_models(base, key)
- # Only casefold-match ids against a loopback Studio, where _is_hub_model_id's
+ # Only casefold-match ids against a loopback Unsloth, where _is_hub_model_id's
# local existence probe can actually reject a server-side path; see the note there.
allow_casefold = is_loopback_url(base)
# /v1/models reports the model id but not the active GGUF variant or runtime load
@@ -741,7 +741,7 @@ def _resolve_model(
typer.echo(
f"Ensuring {requested} is loaded with the requested settings…"
if load_has_overrides
- else f"Loading {requested} on the Studio server (this can take a while)…"
+ else f"Loading {requested} on the Unsloth server (this can take a while)…"
)
# Mirror `unsloth run`'s load knobs; keep the default payload as just
# model_path so a bare `--model` load is unchanged.
@@ -762,7 +762,7 @@ def _resolve_model(
timeout = 3600,
error = "Model load failed",
)
- # Studio registers the model under a canonical id (resolved identifier,
+ # Unsloth registers the model under a canonical id (resolved identifier,
# casing) that /v1/models echoes but which may differ from the path we
# passed; match on the id the load reports so we don't silently fall
# through to models[0] and connect to a different loaded model.
@@ -783,22 +783,22 @@ def _resolve_model(
if match is not None:
return match
if requested:
- # We asked Studio to load it and it didn't surface in /v1/models; don't
+ # We asked Unsloth to load it and it didn't surface in /v1/models; don't
# silently hand back an unrelated loaded model.
_fail(
- f"Studio didn't report '{requested}' as loaded. Double-check the model "
+ f"Unsloth didn't report '{requested}' as loaded. Double-check the model "
"id, or load it from the model dropdown in the UI."
)
if not models:
_fail(
- "No model is loaded in Studio. Load one from the model dropdown in "
+ "No model is loaded in Unsloth. Load one from the model dropdown in "
"the UI, or pass --model to load it from here."
)
return models[0]
def _require_gguf_for_codex(base: str, key: str, model_id: str) -> None:
- # Codex always streams, and Studio only streams /v1/responses from llama-server.
+ # Codex always streams, and Unsloth only streams /v1/responses from llama-server.
try:
status = _http_json("GET", f"{base}/api/inference/status", key)
except urllib.error.HTTPError as exc:
@@ -901,7 +901,7 @@ def _codex_supports_model_catalog() -> bool:
def _codex_model_catalog(model: dict) -> dict:
- """Return conservative metadata for a Studio model unknown to Codex's built-in catalog."""
+ """Return conservative metadata for an Unsloth model unknown to Codex's built-in catalog."""
model_id = model["id"]
window = model.get("context_length") or model.get("max_context_length")
entry = {
@@ -1202,7 +1202,7 @@ def _connect(
# `--model org/name:QUANT` is shorthand for `--model org/name --gguf-variant QUANT`.
# Split it before we match/serve so the attach path resolves against the already-loaded
# `org/name` (listed without the suffix) instead of reloading a `:`-suffixed repo id --
- # which Studio rejects and which would evict a model another session is using.
+ # which Unsloth rejects and which would evict a model another session is using.
if model:
repo, variant = _split_repo_variant(model)
if variant:
@@ -1240,7 +1240,7 @@ def _run(
# --no-launch recipes stay intact.
if launch and clear_screen:
click.clear()
- typer.echo(f"Studio {base} · model {entry['id']}")
+ typer.echo(f"Unsloth {base} · model {entry['id']}")
wsl_env_bridge = _wsl_bridge_names(env, unset_env) if _wsl_windows_executable(command) else ()
if not launch:
_print_env(env, command, unset_env = unset_env, wsl_env_bridge = wsl_env_bridge)
@@ -1306,7 +1306,7 @@ def write_openclaw_config(
)
return
before = json.dumps(config, sort_keys = True)
- # Studio is a generic OpenAI-compatible /v1 endpoint (the vLLM/LM Studio path).
+ # Unsloth is a generic OpenAI-compatible /v1 endpoint (the vLLM/LM Studio path).
provider_model = {"id": model["id"], "name": model["id"]}
window = model.get("context_length") or model.get("max_context_length")
if window:
@@ -1570,14 +1570,14 @@ def write_pi_config(base: str, key: str, model: dict, path: Path) -> None:
return
before = json.dumps(config, sort_keys = True)
# Pi reads custom providers from ~/.pi/agent/models.json (HOME-relocated for the
- # session). Studio is a generic OpenAI-compatible /v1 endpoint, and the key lives
+ # session). Unsloth is a generic OpenAI-compatible /v1 endpoint, and the key lives
# in the config rather than the env (matching openclaw/opencode).
provider_model = {"id": model["id"]}
window = model.get("context_length") or model.get("max_context_length")
if window:
window = int(window)
# An unspecified model defaults to contextWindow 128000 / maxTokens 16384,
- # far larger than a small Studio context, so Pi compacts too late and overflows
+ # far larger than a small Unsloth context, so Pi compacts too late and overflows
# the server. Pin the real window and a sane output cap (mirrors OpenCode).
provider_model["contextWindow"] = window
provider_model["maxTokens"] = min(window // 4, 8192)
@@ -1606,7 +1606,7 @@ def claude(
yolo: bool = _YOLO_OPTION,
persist: bool = _PERSIST_OPTION,
):
- """Point Claude Code at the running Studio server and start it."""
+ """Point Claude Code at the running Unsloth server and start it."""
base, key, entry = _connect(
api_key,
model,
@@ -1690,7 +1690,7 @@ def codex(
yolo: bool = _YOLO_OPTION,
persist: bool = _PERSIST_OPTION,
):
- """Point OpenAI Codex at the running Studio server and start it."""
+ """Point OpenAI Codex at the running Unsloth server and start it."""
base, key, entry = _connect(
api_key,
model,
@@ -1734,7 +1734,7 @@ def openclaw(
yolo: bool = _YOLO_OPTION,
persist: bool = _PERSIST_OPTION,
):
- """Point OpenClaw at the running Studio server and start it."""
+ """Point OpenClaw at the running Unsloth server and start it."""
base, key, entry = _connect(
api_key,
model,
@@ -1791,7 +1791,7 @@ def opencode(
yolo: bool = _YOLO_OPTION,
persist: bool = _PERSIST_OPTION,
):
- """Point OpenCode at the running Studio server and start it."""
+ """Point OpenCode at the running Unsloth server and start it."""
base, key, entry = _connect(
api_key,
model,
@@ -1835,7 +1835,7 @@ def opencode(
# setting them in the highest-priority inline overlay neutralizes any user allowlist
# or denylist for the launch. It is session-only: it lives in OPENCODE_CONFIG_CONTENT
# for this invocation and never touches the user's config files, so their normal
- # `opencode` is unchanged; only this session is limited to the Studio provider.
+ # `opencode` is unchanged; only this session is limited to the Unsloth provider.
# small_model is opencode's separate model for lightweight tasks; pin it to the
# session model too, or a user/project small_model on another (now filtered)
# provider would resolve a not-found error mid-session. The session serves one
@@ -1869,7 +1869,7 @@ def hermes(
yolo: bool = _YOLO_OPTION,
persist: bool = _PERSIST_OPTION,
):
- """Point Hermes (Nous Research) at the running Studio server and start it."""
+ """Point Hermes (Nous Research) at the running Unsloth server and start it."""
native_args = [*_yolo_command_flags("hermes", yolo), *ctx.args]
command = ["hermes", *_hermes_resume_oneshot_args(native_args)]
base, key, entry = _connect(
@@ -1902,7 +1902,7 @@ def pi(
yolo: bool = _YOLO_OPTION,
persist: bool = _PERSIST_OPTION,
):
- """Point Pi (coding agent) at the running Studio server and start it."""
+ """Point Pi (coding agent) at the running Unsloth server and start it."""
base, key, entry = _connect(
api_key,
model,
diff --git a/unsloth_cli/commands/studio.py b/unsloth_cli/commands/studio.py
index 09355bd454..f2f41fc583 100644
--- a/unsloth_cli/commands/studio.py
+++ b/unsloth_cli/commands/studio.py
@@ -40,7 +40,7 @@ def _enable_verbose_access_logs() -> None:
# UNSLOTH_STUDIO_HOME wins when both env vars are set.
def _looks_like_installer_managed_studio_home(candidate: Path) -> bool:
"""Sentinel check (studio.conf or bin shim) so a dev venv named
- unsloth_studio is not misidentified as a custom Studio root.
+ unsloth_studio is not misidentified as a custom Unsloth root.
"""
shim_name = "unsloth.exe" if platform.system() == "Windows" else "unsloth"
return (candidate / "share" / "studio.conf").is_file() or (
@@ -212,7 +212,7 @@ def _find_run_py() -> Optional[Path]:
run_py = _PACKAGE_ROOT / "studio" / "backend" / "run.py"
if run_py.is_file():
return run_py
- # 2. Studio venv's site-packages (Linux + Windows layouts)
+ # 2. Unsloth venv's site-packages (Linux + Windows layouts)
for pattern in (
"lib/python*/site-packages/studio/backend/run.py",
"Lib/site-packages/studio/backend/run.py",
@@ -273,7 +273,7 @@ def _find_setup_script() -> Optional[Path]:
s = _PACKAGE_ROOT / "studio" / name
if s.is_file():
return s
- # 2. Studio venv's site-packages
+ # 2. Unsloth venv's site-packages
for pattern in (
f"lib/python*/site-packages/studio/{name}",
f"Lib/site-packages/studio/{name}",
@@ -641,7 +641,7 @@ def _create_desktop_secret_in_cli() -> str:
def _should_prompt_password_change(
*, cloudflare: Optional[bool], host: str, secure: bool, api_only: bool
) -> bool:
- """Whether this launch will expose Studio through the Cloudflare tunnel.
+ """Whether this launch will expose Unsloth through the Cloudflare tunnel.
CLI mirror of run.py's _cloudflare_tunnel_should_start, minus the Colab
case (Colab launches never come through this CLI path). --secure implies
@@ -747,7 +747,7 @@ def _apply_supplied_password_before_launch(supplied_password: "str | None") -> N
conn = _connect_auth_db()
except (OSError, sqlite3.Error) as exc:
typer.echo(
- f"Error: --password could not open the Studio auth database ({exc}); not starting.",
+ f"Error: --password could not open the Unsloth auth database ({exc}); not starting.",
err = True,
)
raise typer.Exit(1)
@@ -767,7 +767,7 @@ def _apply_supplied_password_before_launch(supplied_password: "str | None") -> N
raise typer.Exit(1)
if not row[2]:
typer.echo(
- "Error: a Studio admin password is already set; --password only sets "
+ "Error: an Unsloth admin password is already set; --password only sets "
"the initial password. Run `unsloth studio reset-password` first "
"(or change it in the UI).",
err = True,
@@ -790,7 +790,7 @@ def _apply_supplied_password_before_launch(supplied_password: "str | None") -> N
# Any DB failure fails closed (typer.Exit is not caught here, so the
# deliberate Exit(1) branches above propagate unchanged).
typer.echo(
- f"Error: --password could not update the Studio auth database ({exc}); not starting.",
+ f"Error: --password could not update the Unsloth auth database ({exc}); not starting.",
err = True,
)
raise typer.Exit(1)
@@ -813,9 +813,9 @@ def _strip_seeded_bootstrap_password_or_exit(*, context: str) -> None:
bootstrap_file.unlink(missing_ok = True)
except OSError as exc:
typer.echo(
- "Error: refusing to publish Studio on a public Cloudflare URL: "
+ "Error: refusing to publish Unsloth on a public Cloudflare URL: "
f"could not remove the seeded bootstrap password file ({exc}), so an "
- f"older Studio child could still serve the default credential ({context}). "
+ f"older Unsloth child could still serve the default credential ({context}). "
"Delete it manually or change the admin password (run `unsloth studio` "
"locally with a terminal attached, or `unsloth studio reset-password`), "
"then retry.",
@@ -851,7 +851,7 @@ def _require_servable_frontend_or_exit(
return frontend
typer.echo(
"Error: --frontend points at a directory with no index.html, so a "
- "public Studio launch would have no login page to change the seeded "
+ "public Unsloth launch would have no login page to change the seeded "
"admin password. Point --frontend at a built dist, rebuild it (re-run "
"install.sh), or use --api-only.",
err = True,
@@ -862,7 +862,7 @@ def _require_servable_frontend_or_exit(
if resolved is not None:
return resolved
typer.echo(
- "Error: the Studio frontend is not built, so a public launch would have "
+ "Error: the Unsloth frontend is not built, so a public launch would have "
"no login page to change the seeded admin password. Build it (re-run "
"install.sh), pass --frontend PATH to a built dist, or use --api-only.",
err = True,
@@ -892,8 +892,8 @@ def _validate_inproc_backend_before_strip(
_load_run_module()
except Exception as exc:
typer.echo(
- f"Error: the Studio backend could not be loaded ({exc}); refusing to "
- "expose Studio publicly before it is confirmed runnable. Re-run: "
+ f"Error: the Unsloth backend could not be loaded ({exc}); refusing to "
+ "expose Unsloth publicly before it is confirmed runnable. Re-run: "
"unsloth studio setup",
err = True,
)
@@ -902,7 +902,7 @@ def _validate_inproc_backend_before_strip(
def _tunnel_binary_confirmed_unavailable() -> bool:
"""True only if cloudflared is provably unavailable (found nowhere on PATH or
- in the Studio cache AND the download failed), so the tunnel cannot start.
+ in the Unsloth cache AND the download failed), so the tunnel cannot start.
Used on the --secure path (loopback bind, so the tunnel is the ONLY public
exposure) to skip stripping the seeded recovery password before a public URL
@@ -921,7 +921,7 @@ def _tunnel_binary_confirmed_unavailable() -> bool:
if not tunnel_py.is_file():
return False
# ensure_cloudflared() lazily imports utils.paths.storage_roots to resolve the
- # Studio bin cache. The outer CLI hasn't added studio/backend to sys.path yet,
+ # Unsloth bin cache. The outer CLI hasn't added studio/backend to sys.path yet,
# so that import would fail and return None (a false "unavailable" that wrongly
# refuses --secure). Add the backend dir so the cache path resolves as in the child.
added_backend_path = False
@@ -946,7 +946,7 @@ def _tunnel_binary_confirmed_unavailable() -> bool:
def _child_self_suppresses(*, in_studio_venv: bool, child_run_py: Optional[Path]) -> bool:
- """True when the child that will serve Studio is provably THIS install's
+ """True when the child that will serve Unsloth is provably THIS install's
backend, whose pre-bind gate sets app.state.suppress_bootstrap_injection and
so never serves the seeded credential publicly -- even with .bootstrap_password
on disk. The parent-side strip is then unnecessary and can be skipped to avoid
@@ -1002,8 +1002,8 @@ def _enforce_password_change_before_exposure(
# Refuse rather than risk a child serving the default login; a transient
# lock clears on retry.
typer.echo(
- "Error: refusing to publish Studio on a public Cloudflare URL: could "
- f"not open the Studio auth database ({exc}) to confirm the admin "
+ "Error: refusing to publish Unsloth on a public Cloudflare URL: could "
+ f"not open the Unsloth auth database ({exc}) to confirm the admin "
"password was changed. Retry (a transient database lock clears), or "
"change the password first (run `unsloth studio` locally with a "
"terminal attached, or `unsloth studio reset-password`).",
@@ -1028,8 +1028,8 @@ def _enforce_password_change_before_exposure(
except OSError:
pass
typer.echo(
- "Error: refusing to publish Studio on a public Cloudflare URL: could "
- f"not initialize the admin account ({exc}), so a re-exec'd Studio "
+ "Error: refusing to publish Unsloth on a public Cloudflare URL: could "
+ f"not initialize the admin account ({exc}), so a re-exec'd Unsloth "
"child could regenerate and serve a default credential. Retry (a "
"transient database lock clears), or change the password first (run "
"`unsloth studio` locally with a terminal attached, or `unsloth "
@@ -1053,7 +1053,7 @@ def _enforce_password_change_before_exposure(
# regenerate; we just couldn't read must_change back. Strip the seeded
# file so nothing serves it, failing closed if the strip itself fails.
typer.echo(
- f"Warning: could not read the Studio admin state back ({exc}); "
+ f"Warning: could not read the Unsloth admin state back ({exc}); "
"removing the seeded bootstrap password before public exposure.",
err = True,
)
@@ -1066,7 +1066,7 @@ def _enforce_password_change_before_exposure(
# the launch: it never arms for api-only, and TIMEOUT=0 disables it.
if api_only or not _bootstrap_deadline_active():
typer.echo(
- "Error: refusing to publish Studio on a public Cloudflare "
+ "Error: refusing to publish Unsloth on a public Cloudflare "
"URL: the default admin password was never changed, no "
"terminal is attached to change it here, and the bootstrap "
"shutdown deadline does not apply to this launch (api-only, "
@@ -1085,12 +1085,12 @@ def _enforce_password_change_before_exposure(
# fails). Keep the file for LOCAL recovery; must_change stays set
# and the deadline arms.
typer.echo(
- "Warning: Studio is being exposed publicly while the admin "
+ "Warning: Unsloth is being exposed publicly while the admin "
"account still uses its auto-generated bootstrap password. The "
"login page forces a change and the credential is never served "
"on the public page. Set a new password by running `unsloth "
"studio` locally with a terminal attached, or `unsloth studio "
- "reset-password`; Studio shuts down after ~1h if the password "
+ "reset-password`; Unsloth shuts down after ~1h if the password "
"stays unchanged (UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT).",
err = True,
)
@@ -1104,7 +1104,7 @@ def _enforce_password_change_before_exposure(
# uncertainty.)
if secure and _tunnel_binary_confirmed_unavailable():
typer.echo(
- "Error: refusing to expose Studio: the Cloudflare tunnel binary "
+ "Error: refusing to expose Unsloth: the Cloudflare tunnel binary "
"(cloudflared) is unavailable and could not be downloaded, so no "
"public URL can start. The seeded bootstrap password is preserved "
"for recovery; fix connectivity and retry, or change the password "
@@ -1121,11 +1121,11 @@ def _enforce_password_change_before_exposure(
# forces a change and the timer still arms; only the on-disk copy goes.
_strip_seeded_bootstrap_password_or_exit(context = "no terminal to change it")
typer.echo(
- "Warning: Studio is being exposed publicly while the admin account "
+ "Warning: Unsloth is being exposed publicly while the admin account "
"still uses its auto-generated bootstrap password. The seeded password "
"file has been removed so it is not served on the public page. Set a new "
"password by running `unsloth studio` locally with a terminal attached, "
- "or `unsloth studio reset-password`; Studio shuts down after ~1h if the "
+ "or `unsloth studio reset-password`; Unsloth shuts down after ~1h if the "
"password stays unchanged (UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT).",
err = True,
)
@@ -1146,7 +1146,7 @@ def _enforce_password_change_before_exposure(
new_password = _password_prompt.prompt_new_password(_is_current_password)
except (KeyboardInterrupt, EOFError):
typer.echo(
- "\nError: password change aborted; refusing to expose Studio "
+ "\nError: password change aborted; refusing to expose Unsloth "
"with the default admin password. Re-run and set a password, "
"or launch without --secure/--cloudflare.",
err = True,
@@ -1247,7 +1247,7 @@ def studio_default(
cloudflare: Optional[bool] = typer.Option(
None,
"--cloudflare/--no-cloudflare",
- help = "Expose Studio on a PUBLIC internet URL via a free Cloudflare HTTPS "
+ help = "Expose Unsloth on a PUBLIC internet URL via a free Cloudflare HTTPS "
"tunnel, for non-api-only wildcard binds (0.0.0.0 or ::). Off by default; "
"pass --cloudflare to enable it (--secure implies it). --no-cloudflare forces "
"it off but does not change a raw wildcard bind.",
@@ -1404,7 +1404,7 @@ def studio_default(
studio_python = _studio_venv_python()
run_py = _find_run_py()
if not (studio_python and run_py):
- typer.echo("Studio not set up. Run install.sh first.")
+ typer.echo("Unsloth Studio not set up. Run install.sh first.")
raise typer.Exit(1)
# A public UI launch must have a servable login page BEFORE the gate can
# strip the seeded .bootstrap_password, or the child has no way to change
@@ -1510,7 +1510,7 @@ def studio_default(
rc = proc.wait()
if rc != 0:
typer.echo(
- f"\nError: Studio server exited unexpectedly (code {rc}).",
+ f"\nError: Unsloth server exited unexpectedly (code {rc}).",
err = True,
)
typer.echo(
@@ -1522,7 +1522,7 @@ def studio_default(
else:
os.execvp(str(studio_python), args)
else:
- typer.echo("Studio not set up. Run install.sh first.")
+ typer.echo("Unsloth Studio not set up. Run install.sh first.")
raise typer.Exit(1)
run_mod = _load_run_module()
@@ -1733,7 +1733,7 @@ def run(
cloudflare: Optional[bool] = typer.Option(
None,
"--cloudflare/--no-cloudflare",
- help = "Expose Studio on a PUBLIC internet URL via a free Cloudflare HTTPS "
+ help = "Expose Unsloth on a PUBLIC internet URL via a free Cloudflare HTTPS "
"tunnel, for non-api-only wildcard binds (0.0.0.0 or ::). Off by default; "
"pass --cloudflare to enable it (--secure implies it). --no-cloudflare forces "
"it off but does not change a raw wildcard bind.",
@@ -1769,16 +1769,16 @@ def run(
"process list and shell history. Rotate later with `unsloth studio reset-password`.",
),
):
- """Start Studio, load a model, print an API key -- one-liner server.
+ """Start Unsloth, load a model, print an API key -- one-liner server.
- Unknown flags pass through to llama-server (GGUF only). Studio
+ Unknown flags pass through to llama-server (GGUF only). Unsloth
rejects managed flags with HTTP 400: model identity, network
(--host/--port/--path/--api-prefix/--reuse-port), auth/TLS
(--api-key/--ssl-*), single-model UI (--ui/--models-*/--webui),
and parallel slots (use --parallel above). Full denylist in
studio/backend/core/inference/llama_server_args.py. Other knobs
(-c, -ngl, --jinja, --flash-attn, -t, ...) pass through and
- last-wins-override Studio's auto-set value.
+ last-wins-override Unsloth's auto-set value.
Example:
unsloth studio run --model unsloth/Qwen3-1.7B-GGUF --gguf-variant UD-Q4_K_XL
@@ -1792,7 +1792,7 @@ def run(
# Set before any re-exec so the in-venv server inherits it via the env.
# `run --verbose` used to pass through to llama-server (its own -v); keep
- # that by forwarding --log-verbose so we add Studio logs without dropping it.
+ # that by forwarding --log-verbose so we add Unsloth logs without dropping it.
if verbose:
_enable_verbose_access_logs()
if not any(a in ("--verbose", "-v", "--log-verbose") for a in extra_llama_args):
@@ -1878,14 +1878,14 @@ def run(
if not in_studio_venv:
studio_python = _studio_venv_python()
if not studio_python:
- typer.echo("Studio not set up. Run install.sh first.")
+ typer.echo("Unsloth Studio not set up. Run install.sh first.")
raise typer.Exit(1)
# Re-exec via the studio venv's `unsloth` console-script.
studio_bin = studio_python.parent / "unsloth"
if not studio_bin.is_file():
- typer.echo("Studio venv missing 'unsloth' entry point. Re-run: unsloth studio setup")
+ typer.echo("Unsloth venv missing 'unsloth' entry point. Re-run: unsloth studio setup")
raise typer.Exit(1)
- # `run` serves the same Studio UI (unless --api-only); a public launch must
+ # `run` serves the same Unsloth UI (unless --api-only); a public launch must
# have a servable login page BEFORE the gate strips the seeded password, or
# the child has no way to change it. Validate here and forward the resolved
# dist so a shadowed child that can't self-resolve one still serves it.
@@ -2216,7 +2216,7 @@ def stop():
import signal as _signal
if not _PID_FILE.is_file():
- typer.echo("No running Studio server found (no PID file).")
+ typer.echo("No running Unsloth server found (no PID file).")
raise typer.Exit(0)
pid_text = _PID_FILE.read_text().strip()
@@ -2229,7 +2229,7 @@ def stop():
# Check if still alive (os.kill(pid, 0) is invalid on Windows -- see _pid_alive).
if not _pid_alive(pid):
- typer.echo(f"Studio server (PID {pid}) is not running. Cleaning up stale PID file.")
+ typer.echo(f"Unsloth server (PID {pid}) is not running. Cleaning up stale PID file.")
_PID_FILE.unlink(missing_ok = True)
raise typer.Exit(0)
@@ -2239,13 +2239,13 @@ def stop():
subprocess.run(["taskkill", "/PID", str(pid), "/F"], check = True)
else:
os.kill(pid, _signal.SIGTERM)
- typer.echo(f"Sent shutdown signal to Studio server (PID {pid}).")
+ typer.echo(f"Sent shutdown signal to Unsloth server (PID {pid}).")
except ProcessLookupError:
- typer.echo(f"Studio server (PID {pid}) already exited.")
+ typer.echo(f"Unsloth server (PID {pid}) already exited.")
_PID_FILE.unlink(missing_ok = True)
raise typer.Exit(0)
except Exception as e:
- typer.echo(f"Failed to stop Studio server (PID {pid}): {e}", err = True)
+ typer.echo(f"Failed to stop Unsloth server (PID {pid}): {e}", err = True)
raise typer.Exit(1)
# Wait briefly for the process to exit and clean up.
@@ -2253,10 +2253,10 @@ def stop():
time.sleep(0.5)
if not _pid_alive(pid):
_PID_FILE.unlink(missing_ok = True)
- typer.echo("Studio server stopped.")
+ typer.echo("Unsloth server stopped.")
raise typer.Exit(0)
- typer.echo("Studio server is shutting down (may take a few seconds).")
+ typer.echo("Unsloth server is shutting down (may take a few seconds).")
# ── unsloth studio setup / update ─────────────────────────────────────
@@ -2471,7 +2471,7 @@ def setup(
help = "Full pip/build output during setup for troubleshooting.",
),
):
- """Run Studio setup (called by install.ps1 / install.sh)."""
+ """Run Unsloth setup (called by install.ps1 / install.sh)."""
_run_setup_script(verbose = verbose)
@@ -2632,10 +2632,10 @@ def provision_desktop_auth():
@studio_app.command("reset-password")
def reset_password():
- """Reset the Studio admin password.
+ """Reset the Unsloth admin password.
Deletes the auth database so that a fresh admin account with a new
- random password is created on the next server start. The Studio
+ random password is created on the next server start. The Unsloth
server must be restarted after running this command.
"""
auth_dir = STUDIO_HOME / "auth"
@@ -2647,7 +2647,7 @@ def reset_password():
had_db = db_file.exists()
# Delete auth.db FIRST and prove it is gone before touching the seeded
- # credential files. If it cannot be removed (a running Studio or Windows
+ # credential files. If it cannot be removed (a running Unsloth or Windows
# holds it open, or a read-only auth dir), abort with the credential files
# untouched: deleting them while an un-resettable DB (must_change_password=1)
# survives would lock a forgotten-password reset out of any recovery
@@ -2657,7 +2657,7 @@ def reset_password():
except OSError as exc:
typer.echo(
f"Error: could not delete the auth database ({exc}). Stop any running "
- "Studio and retry; no credential files were changed.",
+ "Unsloth and retry; no credential files were changed.",
err = True,
)
raise typer.Exit(1)
@@ -2679,7 +2679,7 @@ def reset_password():
except OSError as exc:
typer.echo(
f"Error: could not remove or clear {path.name} ({exc}); delete "
- "it manually before restarting Studio or the old password may "
+ "it manually before restarting Unsloth or the old password may "
"be reused.",
err = True,
)
diff --git a/unsloth_cli/tests/test_inference_chat.py b/unsloth_cli/tests/test_inference_chat.py
index 56633408fb..ae6f8dcfd4 100644
--- a/unsloth_cli/tests/test_inference_chat.py
+++ b/unsloth_cli/tests/test_inference_chat.py
@@ -372,7 +372,7 @@ def test_find_studio_server_none_when_not_running(monkeypatch):
def test_find_studio_server_prefers_ipv4_loopback_for_localhost(monkeypatch):
- # localhost resolving ::1-first must not hide a Studio bound to 127.0.0.1:
+ # localhost resolving ::1-first must not hide an Unsloth bound to 127.0.0.1:
# discovery tries each loopback address and returns the one that answers.
import socket
import urllib.request
diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.py
index 7e76465144..2405ba0480 100644
--- a/unsloth_cli/tests/test_start.py
+++ b/unsloth_cli/tests/test_start.py
@@ -648,7 +648,7 @@ def test_resolve_model_attaches_to_loaded_catalog_hit_without_reload(monkeypatch
def test_resolve_model_remote_studio_does_not_casefold_attach(monkeypatch):
- # Against a remote Studio the local existence probe cannot see server-side paths,
+ # Against a remote Unsloth the local existence probe cannot see server-side paths,
# so a case-variant loaded id must NOT attach without a load: it could be a distinct
# server-side path on a case-sensitive host. The load endpoint resolves the request.
calls = []
@@ -738,7 +738,7 @@ def test_no_launch_output_is_parseable(fake_studio):
result = CliRunner().invoke(start.start_app, ["codex", "--no-launch"])
assert result.exit_code == 0, result.output
lines = [ln for ln in result.output.splitlines() if ln.strip()]
- skip = ("export ", "unset ", "Studio ", "Updated ", "Disabled ", "Warning", "Loading")
+ skip = ("export ", "unset ", "Unsloth ", "Updated ", "Disabled ", "Warning", "Loading")
body = [ln for ln in lines if not ln.startswith(skip)]
assert "codex --oss --profile unsloth_api" in body[-1]
assert any(ln.startswith("export CODEX_HOME=") for ln in lines)
@@ -767,7 +767,7 @@ def test_no_launch_last_line_is_self_contained(fake_studio, tmp_path):
def test_no_launch_claude_last_line_blanks_conflicting_auth(fake_studio):
# The unset vars must be neutralized inline too, or a partial copy would send the
- # user's own ANTHROPIC_API_KEY to the Studio base.
+ # user's own ANTHROPIC_API_KEY to the Unsloth base.
result = CliRunner().invoke(start.start_app, ["claude", "--no-launch"])
assert result.exit_code == 0, result.output
last = [ln for ln in result.output.splitlines() if ln.strip()][-1]
@@ -814,7 +814,7 @@ def test_https_loopback_never_auto_serves(fake_studio, monkeypatch):
)
result = CliRunner().invoke(start.start_app, ["claude", "--model", "unsloth/Qwen3-1.7B-GGUF"])
assert result.exit_code == 1
- assert "No running Studio server" in result.output
+ assert "No running Unsloth server" in result.output
assert started["called"] is False
@@ -967,7 +967,7 @@ def test_connect_model_flag_forwards_load_options(fake_studio):
def test_connect_model_flag_matches_canonical_id(fake_studio, monkeypatch):
- # Studio registers a loaded model under a canonical id (resolved identifier
+ # Unsloth registers a loaded model under a canonical id (resolved identifier
# / casing) that can differ from the path we passed. The agent must connect
# to that model, not silently fall through to the first loaded one.
requested = "Unsloth/Qwen3.5-35B-A3B"
@@ -1024,7 +1024,7 @@ def test_connect_model_bare_id_matches_loaded_without_reload(fake_studio):
def test_connect_model_variant_suffix_defers_to_server_dedup(fake_studio):
# `--model repo:QUANT` splits into a VALID load payload (bare repo + gguf_variant),
- # never the `:`-suffixed repo id Studio rejects. The variant knob defers to
+ # never the `:`-suffixed repo id Unsloth rejects. The variant knob defers to
# /api/inference/load, whose already-loaded dedup answers without reloading when the
# active variant+settings match -- so a second session running the same command
# attaches without evicting the first, while a genuinely different quant reloads.
@@ -1116,7 +1116,7 @@ def test_connect_no_model_loaded_errors(fake_studio, monkeypatch):
def test_connect_requested_model_not_loaded_fails(fake_studio, monkeypatch):
- # Studio never surfaces the requested model; fail loudly rather than
+ # Unsloth never surfaces the requested model; fail loudly rather than
# silently connecting to whatever else happens to be loaded.
inner = start._http_json
@@ -1187,7 +1187,7 @@ def test_connect_nonloopback_explicit_key_is_allowed(fake_studio, monkeypatch):
def test_connect_nonloopback_replays_saved_key(fake_studio, tmp_path, monkeypatch):
- # A key saved for a remote (non-loopback) Studio is replayed on keyless runs;
+ # A key saved for a remote (non-loopback) Unsloth is replayed on keyless runs;
# auto-minting stays blocked for non-loopback.
remote = "http://studio.example:8888"
monkeypatch.setattr(start, "find_studio_server", lambda: remote)
@@ -1201,7 +1201,7 @@ def test_connect_nonloopback_replays_saved_key(fake_studio, tmp_path, monkeypatc
def test_connect_studio_server_errors_on_explicit_remote(monkeypatch):
- # A user who pointed UNSLOTH_STUDIO_URL at a remote Studio should get an
+ # A user who pointed UNSLOTH_STUDIO_URL at a remote Unsloth should get an
# error, not a silent local model load (which they did not ask for).
import typer
@@ -1242,7 +1242,7 @@ def test_connect_unverified_loopback_without_cached_key_refuses_to_mint(
def test_connect_replays_saved_key_without_identity_check(fake_studio, tmp_path, monkeypatch):
- # A "saved" key (e.g. for an SSH-tunnelled Studio the handshake can't match)
+ # A "saved" key (e.g. for an SSH-tunnelled Unsloth the handshake can't match)
# replays on keyless runs without the handshake, scoped to its own base.
cache = tmp_path / "agent_api_key.json"
cache.write_text(json.dumps({"servers": {BASE: {"saved": ["sk-unsloth-deadbeefdeadbeef"]}}}))
@@ -1358,7 +1358,7 @@ def _serve_redirect(target):
def test_verify_studio_identity_rejects_redirect(tmp_path, monkeypatch):
- # A squatter could 302 /api/auth/identity to the real Studio and relay its
+ # A squatter could 302 /api/auth/identity to the real Unsloth and relay its
# proof; redirects must be refused so the squatter's base isn't accepted.
import unsloth_cli._inference as inference
@@ -1384,7 +1384,7 @@ def test_verify_studio_identity_rejects_redirect(tmp_path, monkeypatch):
def test_verify_studio_identity_rejects_relayed_proof(tmp_path, monkeypatch):
- # A squatter that proxies the nonce to the real Studio on another port gets a
+ # A squatter that proxies the nonce to the real Unsloth on another port gets a
# proof bound to *that* port; the client expects one bound to the port it
# connected to, so the relayed proof is rejected.
import unsloth_cli._inference as inference
@@ -1435,7 +1435,7 @@ def test_connect_no_studio_errors(fake_studio, monkeypatch):
monkeypatch.setattr(start, "find_studio_server", lambda: None)
result = CliRunner().invoke(start.start_app, ["claude", "--no-launch"])
assert result.exit_code == 1
- assert "No running Studio server" in result.output
+ assert "No running Unsloth server" in result.output
@pytest.fixture(autouse = True)
@@ -1564,7 +1564,7 @@ def test_no_serve_preserves_error(fake_studio, monkeypatch):
start.start_app, ["claude", "--model", "unsloth/Qwen3-1.7B-GGUF", "--no-serve"]
)
assert result.exit_code == 1
- assert "No running Studio server" in result.output
+ assert "No running Unsloth server" in result.output
assert started["called"] is False
@@ -1578,7 +1578,7 @@ def test_no_launch_never_serves(fake_studio, monkeypatch):
start.start_app, ["claude", "--model", "unsloth/Qwen3-1.7B-GGUF", "--no-launch"]
)
assert result.exit_code == 1
- assert "No running Studio server" in result.output
+ assert "No running Unsloth server" in result.output
assert started["called"] is False
@@ -1871,7 +1871,7 @@ def _opencode_inline_config(output: str) -> dict:
def test_opencode_inline_scopes_session_to_studio_provider(fake_studio):
# opencode filters even config-defined providers through enabled/disabled_providers,
# and a model pin does not bypass that gate. The inline overlay (session-only, highest
- # layer, arrays replace) allowlists our provider and clears the denylist so the Studio
+ # layer, arrays replace) allowlists our provider and clears the denylist so the Unsloth
# model always loads regardless of the user's config, without reading or editing it.
result = CliRunner().invoke(start.start_app, ["opencode", "--no-launch"])
assert result.exit_code == 0, result.output
@@ -2657,7 +2657,7 @@ def test_agent_api_key_auto_started_rejected_env_key_falls_back(fake_studio, tmp
def test_agent_api_key_auto_started_accepted_key_is_honored(fake_studio, tmp_path):
- # An explicit key the fresh server accepts (e.g. persisted in this Studio
+ # An explicit key the fresh server accepts (e.g. persisted in this Unsloth
# home's auth db across restarts) keeps working exactly as before.
key = start._agent_api_key(BASE, "sk-unsloth-deadbeefdeadbeef", auto_started = True)
assert key == "sk-unsloth-deadbeefdeadbeef"
@@ -2937,11 +2937,11 @@ def test_hermes_resume_oneshot_rejects_usage_file(monkeypatch, usage_arg):
def test_native_resume_flag_passes_through_unchanged(fake_studio, monkeypatch):
# The persistence flag is --persist, NOT --resume, so an agent's own
# `--resume ` (e.g. `unsloth start claude --resume `) still flows
- # through to the agent verbatim and is not swallowed as a Studio option.
+ # through to the agent verbatim and is not swallowed as an Unsloth option.
monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/claude")
monkeypatch.setattr(start, "_claude_flags", lambda: [])
captured = _capture_launch(monkeypatch, ["claude", "--resume", "some-session-guid"])
assert captured["command"][-2:] == ["--resume", "some-session-guid"]
- # Studio never auto-appends its own resume token when the user drives resume.
+ # Unsloth never auto-appends its own resume token when the user drives resume.
assert captured["command"].count("--resume") == 1
assert "--continue" not in captured["command"]
diff --git a/unsloth_cli/tests/test_studio_cloudflare_flag.py b/unsloth_cli/tests/test_studio_cloudflare_flag.py
index fb57d7aaf4..7287737f75 100644
--- a/unsloth_cli/tests/test_studio_cloudflare_flag.py
+++ b/unsloth_cli/tests/test_studio_cloudflare_flag.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Tests for the `--cloudflare/--no-cloudflare` Studio flag.
+"""Tests for the `--cloudflare/--no-cloudflare` Unsloth flag.
Pins the typer Option (tri-state, default off / None) on both `unsloth studio`
and `unsloth studio run`, and that the chosen polarity reaches the re-exec'd
diff --git a/unsloth_cli/tests/test_studio_password_prompt.py b/unsloth_cli/tests/test_studio_password_prompt.py
index 7bbdfe3703..6e9a2c1d52 100644
--- a/unsloth_cli/tests/test_studio_password_prompt.py
+++ b/unsloth_cli/tests/test_studio_password_prompt.py
@@ -885,7 +885,7 @@ def test_run_non_tty_deletes_bootstrap_password_file(monkeypatch, tmp_path):
def test_run_missing_frontend_exits_before_stripping_bootstrap(monkeypatch, tmp_path):
# Regression (item B / reviewer finding 4): `unsloth studio run` serves the
- # same Studio UI and strips the seeded password on a headless public launch,
+ # same Unsloth UI and strips the seeded password on a headless public launch,
# so a missing frontend dist must abort BEFORE the strip -- the same lockout
# guard as `unsloth studio`, not just `studio run`'s model-load residual.
import typer as _typer
@@ -1104,7 +1104,7 @@ def test_cli_update_password_truncates_locked_bootstrap_after_change(monkeypatch
def test_reset_password_fails_closed_when_db_cannot_be_deleted(monkeypatch, tmp_path):
- # If auth.db cannot be removed (running Studio / Windows lock, read-only dir),
+ # If auth.db cannot be removed (running Unsloth / Windows lock, read-only dir),
# reset must abort BEFORE touching the credential files -- deleting them while
# an un-resettable must_change_password=1 DB survives would lock a
# forgotten-password reset out with no recovery credential.
diff --git a/unsloth_cli/tests/test_studio_secure_flag.py b/unsloth_cli/tests/test_studio_secure_flag.py
index 5e5895309c..2a67aad95a 100644
--- a/unsloth_cli/tests/test_studio_secure_flag.py
+++ b/unsloth_cli/tests/test_studio_secure_flag.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Tests for the `--secure/--no-secure` Studio flag: option registration,
+"""Tests for the `--secure/--no-secure` Unsloth flag: option registration,
re-exec/run_server forwarding, the forced 127.0.0.1 bind, and rejection
alongside --no-cloudflare or before a subcommand. Modeled on
test_studio_cloudflare_flag.py."""
diff --git a/unsloth_cli/tests/test_studio_verbose_flag.py b/unsloth_cli/tests/test_studio_verbose_flag.py
index 4af32fd4a2..20468b5f02 100644
--- a/unsloth_cli/tests/test_studio_verbose_flag.py
+++ b/unsloth_cli/tests/test_studio_verbose_flag.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Tests for the `--verbose/-v` Studio flag: option registration on both the
+"""Tests for the `--verbose/-v` Unsloth flag: option registration on both the
plain callback and the `run` subcommand, re-exec forwarding, the access-log
env override, and rejection before a subcommand. Modeled on
test_studio_secure_flag.py."""
@@ -123,7 +123,7 @@ def test_run_without_verbose_leaves_env_unset(monkeypatch):
def test_run_verbose_preserves_llama_server_verbosity(monkeypatch):
- # Studio consumes --verbose but still forwards llama-server's own verbosity.
+ # Unsloth consumes --verbose but still forwards llama-server's own verbosity.
monkeypatch.delenv(_DEDUP, raising = False)
monkeypatch.delenv(_POLL, raising = False)
captured = _invoke_run(monkeypatch, _BASE + ["--verbose"])
From 74d1a284ebe2fcb7ee0123e0a47b9f4bac8a7690 Mon Sep 17 00:00:00 2001
From: Michael Han <107991372+shimmyshimmer@users.noreply.github.com>
Date: Sun, 19 Jul 2026 03:20:56 -0700
Subject: [PATCH 028/271] Studio: hide the RAG embedder and llama.cpp probe
from the hub cached inventory (#7018)
* Studio: hide infra models from the hub cached inventory
The hub inventory scans behind /api/hub/cached-gguf and /api/hub/cached-models
returned the llama.cpp install validation probe (ggml-org/models) and the RAG
embedder (unsloth/bge-small-en-v1.5[-GGUF]) as on-device models. Share the
hidden-model check from routes/models.py via utils/models/hidden_models.py and
apply it in both scans. A GGUF infra repo stays visible when the user
explicitly downloaded a variant through the Hub, since variant manifests only
exist for user-initiated downloads.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: make On Device trust the hub inventory, match repo ids exactly, lighten the hidden-model import
Follow-up on the hub cached-inventory hidden-model change, addressing the review.
On Device now trusts the Hub inventory API for cached rows. The backend already
hides the RAG embedder and the llama.cpp probe and re-includes a GGUF infra repo
once the user downloads a variant through the Hub, but the frontend was
re-hiding it by repo id, so the user-downloaded variant never appeared in the On
Device list or the count. isVisibleInventoryRow now short-circuits cached rows
(kind === "cache") to visible and keeps client-side needle hiding only for local
filesystem rows and Discover.
is_hidden_model matches Hub repo ids exactly (case-insensitive) against the probe
plus the effective embedder and its GGUF companion, instead of substring
matching the configured-embedder basename. A custom embedder with a generic
basename like org/model no longer hides unrelated cached repos such as
user/model-chat or org/model-instruct. The probe filename and local-path
embedders keep exact matching.
The helper moves to utils/hidden_models.py and is imported at module scope in the
hub cache scanner, so it no longer pulls in utils/models/__init__ (the eager
model-config/checkpoint stack) and a broken import fails at startup instead of
being swallowed per-repo and silently emptying the inventory. routes.models
keeps the _is_hidden_model and _safe_resolve aliases and drops the unused
_HF_REPO_ID_RE re-export that was failing source lint.
Tests: exact repo-id matching with a custom embedder, the cached-models scan
keeping an unrelated repo, and a clean-interpreter check that the helper imports
without the model-config stack.
* Studio: match the llama.cpp probe filename on both path separators
The hidden-model check compared the probe's on-disk filename with
Path(value).name, which on a POSIX interpreter does not split a Windows-style
path ("...\stories260K.gguf") and would let the probe through. Split on both
separators so the probe is matched regardless of which OS produced the path,
matching the tolerance of the previous substring check. Adds a Windows-path
assertion to the probe test.
* Studio: harden hidden infra model handling
* Fix hidden cache row confirmation
* Fix hidden local rows and confirmed hint merges
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Handle snapshot-configured hidden models
* Hide basename-only default embedders
* Fix dynamic embedder inventory filtering
* Studio: hide the configured RAG embedder from Discover and feed rows
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: danielhanchen
Co-authored-by: Daniel Han <23090290+danielhanchen@users.noreply.github.com>
---
.../core/inference/local_model_resolver.py | 6 +-
studio/backend/core/rag/config.py | 25 +-
.../hub/services/models/cache_inventory.py | 40 ++-
.../hub/services/models/local_inventory.py | 17 +-
.../backend/hub/tests/test_model_services.py | 338 ++++++++++++++++++
studio/backend/routes/models.py | 61 +---
studio/backend/routes/settings.py | 5 +
.../backend/tests/test_cached_gguf_routes.py | 139 +++++++
.../test_embedding_model_security_gate.py | 17 +
.../tests/test_embedding_model_settings.py | 7 +
.../backend/tests/test_openai_auto_switch.py | 27 +-
studio/backend/utils/hidden_models.py | 142 ++++++++
.../hub/hooks/use-hidden-embedding-models.ts | 46 +++
studio/frontend/src/features/hub/hub-page.tsx | 53 ++-
studio/frontend/src/features/hub/index.ts | 1 +
.../features/hub/inventory/inventory-hints.ts | 11 +-
.../src/features/hub/inventory/types.ts | 1 +
.../hub/inventory/use-hub-inventory.ts | 1 +
.../src/features/hub/inventory/view-models.ts | 18 +-
.../src/features/hub/lib/hidden-models.ts | 26 +-
.../features/settings/api/embedding-model.ts | 17 +-
.../frontend/src/features/settings/index.ts | 1 +
22 files changed, 899 insertions(+), 100 deletions(-)
create mode 100644 studio/backend/utils/hidden_models.py
create mode 100644 studio/frontend/src/features/hub/hooks/use-hidden-embedding-models.ts
diff --git a/studio/backend/core/inference/local_model_resolver.py b/studio/backend/core/inference/local_model_resolver.py
index 86ad8b9fd8..64ab38ec75 100644
--- a/studio/backend/core/inference/local_model_resolver.py
+++ b/studio/backend/core/inference/local_model_resolver.py
@@ -201,7 +201,11 @@ def _build_index() -> dict[str, _LocalGgufEntry]:
continue
# Skip what Unsloth hides from its pickers (validation probe, RAG embed
# weights): not chat models, so never an auto-switch target.
- if _is_hidden_model(raw_id, getattr(info, "path", None)):
+ if _is_hidden_model(
+ raw_id,
+ getattr(info, "model_id", None),
+ getattr(info, "path", None),
+ ):
continue
# Advertise a client-facing alias, not an absolute filesystem path.
loader_id = _advertised_loader_id(info)
diff --git a/studio/backend/core/rag/config.py b/studio/backend/core/rag/config.py
index 2de32a68e4..f54d795731 100644
--- a/studio/backend/core/rag/config.py
+++ b/studio/backend/core/rag/config.py
@@ -87,6 +87,22 @@ def _names_gguf(model: str) -> bool:
return "gguf" in re.split(r"[^a-z0-9]+", model.lower())
+def gguf_repo_for_embedding_model(model: str) -> str:
+ """GGUF repo for ``model``, honoring an explicit companion override."""
+ if "RAG_EMBED_GGUF_REPO" in os.environ:
+ return EMBED_GGUF_REPO
+ if model == DEFAULT_EMBEDDING_MODEL:
+ return EMBED_GGUF_REPO
+ if _names_gguf(model):
+ return model
+ return f"{model}-GGUF"
+
+
+def default_gguf_repo() -> str:
+ """GGUF companion for the env/default embedding model."""
+ return gguf_repo_for_embedding_model(EMBEDDING_MODEL)
+
+
def effective_gguf_repo() -> str:
"""GGUF repo for the llama-server backend, tracking the effective model.
@@ -95,14 +111,7 @@ def effective_gguf_repo() -> str:
``-GGUF`` companion repo (the unsloth convention the default pair follows),
or is used as-is when it already names a GGUF repo.
"""
- if "RAG_EMBED_GGUF_REPO" in os.environ:
- return EMBED_GGUF_REPO
- model = effective_embedding_model()
- if model == DEFAULT_EMBEDDING_MODEL:
- return EMBED_GGUF_REPO
- if _names_gguf(model):
- return model
- return f"{model}-GGUF"
+ return gguf_repo_for_embedding_model(effective_embedding_model())
# llama-server backend only. F16 over Q8_0: faster (no per-block dequant for this
diff --git a/studio/backend/hub/services/models/cache_inventory.py b/studio/backend/hub/services/models/cache_inventory.py
index 1f38af9381..54a25482f2 100644
--- a/studio/backend/hub/services/models/cache_inventory.py
+++ b/studio/backend/hub/services/models/cache_inventory.py
@@ -37,6 +37,13 @@ from hub.services.models.common import (
_runtime_for_format,
)
+# Imported at module scope (not inside the per-repo scan loop) so a broken
+# import surfaces at startup instead of silently emptying the inventory: the
+# scan loop swallows per-repo exceptions and would drop every repo. Lives under
+# ``utils`` (not ``utils.models``) to avoid the eager model-config/checkpoint
+# imports in ``utils/models/__init__.py``.
+from utils.hidden_models import is_hidden_model
+
logger = get_logger(__name__)
_repo_size_cache: "OrderedDict[tuple[str, str, str], tuple[int, frozenset[str], float]]" = (
@@ -243,6 +250,13 @@ def invalidate_hf_cache_scans() -> None:
hf_cache_scan.invalidate_hf_cache_scans()
+def _is_hidden_infra_repo(*values: str | None) -> bool:
+ """True for infra-only repos (the RAG embedder and the llama.cpp install
+ validation probe) that are cached as a side effect of Studio itself and are
+ not usable chat models."""
+ return is_hidden_model(*values)
+
+
def _scan_cached_gguf() -> list[dict]:
"""Synchronous HF-cache disk walk for GGUF repos; runs in a worker thread."""
cache_scans = all_hf_cache_scans()
@@ -254,13 +268,24 @@ def _scan_cached_gguf() -> list[dict]:
if str(repo_info.repo_type) != "model":
continue
repo_id = repo_info.repo_id
+ repo_path = Path(repo_info.repo_path)
+ snapshot_path = _cached_model_snapshot_path(repo_path)
total_size = _repo_gguf_size_bytes(repo_info)
has_variant_state, variant_state_size = _gguf_variant_state_summary(repo_id)
+ is_hidden_infra = _is_hidden_infra_repo(
+ repo_id,
+ str(repo_path),
+ str(snapshot_path) if snapshot_path is not None else None,
+ )
+ # Hide infra repos unless the user downloaded a variant via
+ # the Hub; variant state only exists for user downloads.
+ if is_hidden_infra and not has_variant_state:
+ continue
if total_size == 0 and not has_variant_state:
continue
partial = hf_cache_scan.is_gguf_repo_partial(
repo_id,
- Path(repo_info.repo_path),
+ repo_path,
)
if total_size == 0 and not partial:
continue
@@ -283,6 +308,9 @@ def _scan_cached_gguf() -> list[dict]:
requires_variant = True,
)
)
+ # Visible infra variants remain management-only.
+ if is_hidden_infra:
+ row["capabilities"]["can_chat"] = False
if _prefer_cache_row(row, existing):
seen_lower[key] = row
except Exception as e:
@@ -475,6 +503,15 @@ def _scan_cached_models() -> list[dict]:
if str(repo_info.repo_type) != "model":
continue
repo_id = repo_info.repo_id
+ repo_path = Path(repo_info.repo_path)
+ snapshot_path = _cached_model_snapshot_path(repo_path)
+ # The non-GGUF embedder has no variant downloads; always hide.
+ if _is_hidden_infra_repo(
+ repo_id,
+ str(repo_path),
+ str(snapshot_path) if snapshot_path is not None else None,
+ ):
+ continue
has_main_gguf = _repo_has_gguf_files(repo_info)
payload = _repo_non_gguf_model_payload(repo_info)
if payload.size_bytes == 0:
@@ -486,7 +523,6 @@ def _scan_cached_models() -> list[dict]:
continue
key = repo_id.lower()
existing = seen_lower.get(key)
- repo_path = Path(repo_info.repo_path)
snapshot_partial = hf_cache_scan.is_snapshot_partial(
"model",
repo_id,
diff --git a/studio/backend/hub/services/models/local_inventory.py b/studio/backend/hub/services/models/local_inventory.py
index a3782efead..b34532fa35 100644
--- a/studio/backend/hub/services/models/local_inventory.py
+++ b/studio/backend/hub/services/models/local_inventory.py
@@ -36,6 +36,7 @@ from hub.utils.paths import (
)
from hub.services.models import common as model_common
from hub.services.models.ollama import scan_ollama_dir
+from utils.hidden_models import is_hidden_model
logger = get_logger(__name__)
_MAX_MODELS_PER_CUSTOM_FOLDER = 200
@@ -623,6 +624,20 @@ def _dedupe_local_models(local_models: List[LocalModelInfo]) -> list[LocalModelI
)
+def _filter_hidden_models(local_models: List[LocalModelInfo]) -> list[LocalModelInfo]:
+ """Remove infrastructure-only models from the shared local inventory."""
+ visible: list[LocalModelInfo] = []
+ for model in local_models:
+ resolved_cache_path = (
+ hf_cache_scan.resolve_hf_cache_realpath(Path(model.path))
+ if model.source == "hf_cache"
+ else None
+ )
+ if not is_hidden_model(model.id, model.model_id, model.path, resolved_cache_path):
+ visible.append(model)
+ return visible
+
+
async def list_local_models_response(models_dir: str = "./models") -> LocalModelListResponse:
"""List local model candidates from every supported on-device source."""
hf_cache_dir = _resolve_hf_cache_dir()
@@ -653,7 +668,7 @@ async def list_local_models_response(models_dir: str = "./models") -> LocalModel
ollama_dirs,
)
local_models += await _collect_models_from_custom_folders()
- models = _dedupe_local_models(local_models)
+ models = _dedupe_local_models(_filter_hidden_models(local_models))
return LocalModelListResponse(
models_dir = str(models_root),
diff --git a/studio/backend/hub/tests/test_model_services.py b/studio/backend/hub/tests/test_model_services.py
index 2c33e09b2b..693d945ee1 100644
--- a/studio/backend/hub/tests/test_model_services.py
+++ b/studio/backend/hub/tests/test_model_services.py
@@ -439,6 +439,287 @@ def test_cached_gguf_scan_includes_variant_state_without_completed_gguf(monkeypa
assert row["capabilities"]["requires_variant"] is True
+def test_cached_gguf_scan_hides_infra_repos_without_user_downloads(monkeypatch, tmp_path):
+ probe = _repo(
+ "ggml-org/models",
+ [_file("tinyllamas/stories260K.gguf", 1_200_000)],
+ tmp_path / "probe",
+ )
+ embedder = _repo(
+ "unsloth/bge-small-en-v1.5-GGUF",
+ [_file("bge-small-en-v1.5-f16.gguf", 60_000_000)],
+ tmp_path / "embedder",
+ )
+ chat = _repo("Org/Chat-GGUF", [_file("Q4_K_M.gguf", 100)], tmp_path / "chat")
+ monkeypatch.setattr(
+ cache_inventory,
+ "all_hf_cache_scans",
+ lambda: [SimpleNamespace(repos = [probe, embedder, chat])],
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_gguf_repo_partial",
+ lambda _repo_id, _path: False,
+ )
+
+ result = {"cached": cache_inventory._scan_cached_gguf()}
+
+ assert [row["repo_id"] for row in result["cached"]] == ["Org/Chat-GGUF"]
+
+
+def test_cached_gguf_scan_keeps_infra_repo_with_user_downloaded_variant(monkeypatch, tmp_path):
+ monkeypatch.setattr(state_dir, "cache_root", lambda: tmp_path / "state")
+ embedder = _repo(
+ "unsloth/bge-small-en-v1.5-GGUF",
+ [
+ _file("bge-small-en-v1.5-f16.gguf", 60_000_000),
+ _file("bge-small-en-v1.5-Q8_0.gguf", 35_000_000),
+ ],
+ tmp_path / "embedder",
+ )
+ # Variant manifests only exist for user Hub downloads, not auto-downloads.
+ assert download_manifest.write_manifest(
+ "model",
+ "unsloth/bge-small-en-v1.5-GGUF",
+ "Q8_0",
+ [download_manifest.ExpectedFile(path = "bge-small-en-v1.5-Q8_0.gguf", size = 35_000_000)],
+ "http",
+ )
+ monkeypatch.setattr(
+ cache_inventory,
+ "all_hf_cache_scans",
+ lambda: [SimpleNamespace(repos = [embedder])],
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_gguf_repo_partial",
+ lambda _repo_id, _path: False,
+ )
+
+ result = {"cached": cache_inventory._scan_cached_gguf()}
+
+ assert [row["repo_id"] for row in result["cached"]] == ["unsloth/bge-small-en-v1.5-GGUF"]
+ assert result["cached"][0]["capabilities"]["can_chat"] is False
+
+
+def test_cached_models_scan_hides_non_gguf_embedder(monkeypatch, tmp_path):
+ embedder_path = tmp_path / "hub" / "models--unsloth--bge-small-en-v1.5"
+ embedder_path.mkdir(parents = True)
+ embedder = _repo(
+ "unsloth/bge-small-en-v1.5",
+ [_file("config.json", 12), _file("model.safetensors", 130_000_000)],
+ embedder_path,
+ )
+ chat_path = tmp_path / "hub" / "models--Org--Chat"
+ chat_path.mkdir(parents = True)
+ chat = _repo(
+ "Org/Chat",
+ [_file("config.json", 12), _file("model.safetensors", 100)],
+ chat_path,
+ )
+ monkeypatch.setattr(
+ cache_inventory,
+ "all_hf_cache_scans",
+ lambda: [SimpleNamespace(repos = [embedder, chat])],
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_snapshot_partial",
+ lambda _kind, _repo_id, _path: False,
+ )
+
+ result = {"cached": cache_inventory._scan_cached_models()}
+
+ assert [row["repo_id"] for row in result["cached"]] == ["Org/Chat"]
+
+
+def test_cached_scans_hide_embedders_configured_by_cache_path(monkeypatch, tmp_path):
+ from core.rag import config as rag_config
+
+ gguf_path = tmp_path / "hub" / "models--Org--PathEmbedder-GGUF"
+ gguf_path.mkdir(parents = True)
+ gguf = _repo(
+ "Org/PathEmbedder-GGUF",
+ [_file("model-F16.gguf", 60_000_000)],
+ gguf_path,
+ )
+ model_path = tmp_path / "hub" / "models--Org--PathEmbedder"
+ model_path.mkdir(parents = True)
+ model = _repo(
+ "Org/PathEmbedder",
+ [_file("config.json", 12), _file("model.safetensors", 130_000_000)],
+ model_path,
+ )
+ monkeypatch.setattr(
+ rag_config,
+ "effective_embedding_model",
+ lambda: str(model_path),
+ )
+ monkeypatch.setattr(
+ rag_config,
+ "effective_gguf_repo",
+ lambda: str(gguf_path),
+ )
+ monkeypatch.setattr(
+ cache_inventory,
+ "all_hf_cache_scans",
+ lambda: [SimpleNamespace(repos = [gguf, model])],
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_gguf_repo_partial",
+ lambda _repo_id, _path: False,
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_snapshot_partial",
+ lambda _kind, _repo_id, _path: False,
+ )
+
+ assert cache_inventory._scan_cached_gguf() == []
+ assert cache_inventory._scan_cached_models() == []
+
+
+def test_cached_scans_hide_embedders_configured_by_snapshot_path(monkeypatch, tmp_path):
+ from core.rag import config as rag_config
+
+ gguf_path = tmp_path / "hub" / "models--Org--SnapshotEmbedder-GGUF"
+ gguf_snapshot = gguf_path / "snapshots" / "gguf-revision"
+ gguf_snapshot.mkdir(parents = True)
+ gguf = _repo(
+ "Org/SnapshotEmbedder-GGUF",
+ [_file("model-F16.gguf", 60_000_000)],
+ gguf_path,
+ )
+ model_path = tmp_path / "hub" / "models--Org--SnapshotEmbedder"
+ model_snapshot = model_path / "snapshots" / "model-revision"
+ model_snapshot.mkdir(parents = True)
+ model = _repo(
+ "Org/SnapshotEmbedder",
+ [_file("config.json", 12), _file("model.safetensors", 130_000_000)],
+ model_path,
+ )
+ monkeypatch.setattr(
+ rag_config,
+ "effective_embedding_model",
+ lambda: str(model_snapshot),
+ )
+ monkeypatch.setattr(
+ rag_config,
+ "effective_gguf_repo",
+ lambda: str(gguf_snapshot),
+ )
+ monkeypatch.setattr(
+ cache_inventory,
+ "all_hf_cache_scans",
+ lambda: [SimpleNamespace(repos = [gguf, model])],
+ )
+
+ def _resolve_snapshot(repo_path):
+ return str(
+ {
+ gguf_path: gguf_snapshot,
+ model_path: model_snapshot,
+ }.get(Path(repo_path), Path(repo_path))
+ )
+
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "resolve_hf_cache_realpath",
+ _resolve_snapshot,
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_gguf_repo_partial",
+ lambda _repo_id, _path: False,
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_snapshot_partial",
+ lambda _kind, _repo_id, _path: False,
+ )
+
+ assert cache_inventory._scan_cached_gguf() == []
+ assert cache_inventory._scan_cached_models() == []
+
+
+def test_cached_models_scan_keeps_unrelated_repo_with_custom_generic_embedder(
+ monkeypatch, tmp_path
+):
+ # A custom embedder with a generic basename ("org/model") must be hidden by
+ # EXACT repo-id match only. An unrelated cached chat model whose id merely
+ # contains "model" (e.g. "user/model-chat") must stay on device: substring
+ # basename matching used to drop real chat models from the inventory.
+ from core.rag import config as rag_config
+
+ monkeypatch.setattr(rag_config, "effective_embedding_model", lambda: "org/model")
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/model-GGUF")
+
+ def _model_repo(repo_id: str):
+ path = tmp_path / "hub" / f"models--{repo_id.replace('/', '--')}"
+ path.mkdir(parents = True)
+ return _repo(
+ repo_id,
+ [_file("config.json", 12), _file("model.safetensors", 100)],
+ path,
+ )
+
+ embedder = _model_repo("org/model")
+ chat = _model_repo("user/model-chat")
+ monkeypatch.setattr(
+ cache_inventory,
+ "all_hf_cache_scans",
+ lambda: [SimpleNamespace(repos = [embedder, chat])],
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_snapshot_partial",
+ lambda _kind, _repo_id, _path: False,
+ )
+
+ result = {"cached": cache_inventory._scan_cached_models()}
+
+ assert [row["repo_id"] for row in result["cached"]] == ["user/model-chat"]
+
+
+def test_cached_scans_hide_stale_default_embedder_after_custom_setting(monkeypatch, tmp_path):
+ from core.rag import config as rag_config
+
+ monkeypatch.setattr(rag_config, "effective_embedding_model", lambda: "org/custom")
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/custom-GGUF")
+
+ gguf = _repo(
+ "unsloth/bge-small-en-v1.5-GGUF",
+ [_file("bge-small-en-v1.5-f16.gguf", 60_000_000)],
+ tmp_path / "default-gguf",
+ )
+ weights_path = tmp_path / "hub" / "models--unsloth--bge-small-en-v1.5"
+ weights_path.mkdir(parents = True)
+ weights = _repo(
+ "unsloth/bge-small-en-v1.5",
+ [_file("config.json", 12), _file("model.safetensors", 130_000_000)],
+ weights_path,
+ )
+ monkeypatch.setattr(
+ cache_inventory,
+ "all_hf_cache_scans",
+ lambda: [SimpleNamespace(repos = [gguf, weights])],
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_gguf_repo_partial",
+ lambda _repo_id, _path: False,
+ )
+ monkeypatch.setattr(
+ cache_inventory.hf_cache_scan,
+ "is_snapshot_partial",
+ lambda _kind, _repo_id, _path: False,
+ )
+
+ assert cache_inventory._scan_cached_gguf() == []
+ assert cache_inventory._scan_cached_models() == []
+
+
def test_gguf_variant_requirements_include_split_files_and_preferred_mmproj():
requirements = gguf_variants._build_gguf_variant_requirements(
[
@@ -1610,6 +1891,63 @@ def test_hf_cache_scan_uses_gguf_partial_row_for_variant_state(monkeypatch, tmp_
assert rows[0].capabilities.requires_variant is True
+def test_local_inventory_filters_custom_embedder_hf_cache_row(monkeypatch, tmp_path):
+ from core.rag import config as rag_config
+
+ monkeypatch.setattr(rag_config, "effective_embedding_model", lambda: "org/embedder")
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/embedder-GGUF")
+
+ def _row(repo_id: str):
+ repo_path = tmp_path / f"models--{repo_id.replace('/', '--')}"
+ return model_common._local_model_info(
+ scan_path = repo_path,
+ load_path = repo_path,
+ source = "hf_cache",
+ model_format = "safetensors",
+ model_id = repo_id,
+ )
+
+ rows = local_inventory._filter_hidden_models([_row("org/embedder"), _row("org/chat-model")])
+
+ assert [row.model_id for row in rows] == ["org/chat-model"]
+
+
+def test_local_inventory_filters_embedder_configured_by_snapshot_path(monkeypatch, tmp_path):
+ from core.rag import config as rag_config
+
+ embedder_path = tmp_path / "hub" / "models--org--embedder"
+ embedder_snapshot = embedder_path / "snapshots" / "revision"
+ embedder_snapshot.mkdir(parents = True)
+ chat_path = tmp_path / "hub" / "models--org--chat-model"
+ chat_path.mkdir(parents = True)
+ monkeypatch.setattr(
+ rag_config,
+ "effective_embedding_model",
+ lambda: str(embedder_snapshot),
+ )
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/embedder-GGUF")
+ monkeypatch.setattr(
+ local_inventory.hf_cache_scan,
+ "resolve_hf_cache_realpath",
+ lambda path: str(embedder_snapshot) if Path(path) == embedder_path else str(path),
+ )
+
+ def _row(repo_id: str, repo_path: Path):
+ return model_common._local_model_info(
+ scan_path = repo_path,
+ load_path = repo_path,
+ source = "hf_cache",
+ model_format = "safetensors",
+ model_id = repo_id,
+ )
+
+ rows = local_inventory._filter_hidden_models(
+ [_row("org/embedder", embedder_path), _row("org/chat-model", chat_path)]
+ )
+
+ assert [row.model_id for row in rows] == ["org/chat-model"]
+
+
def test_model_download_job_helpers_preserve_idle_shape():
key = downloads._download_job_key("Org/Model", None)
status = downloads._job_status(key)
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index 742ecde3ba..bb321695cd 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -59,59 +59,12 @@ def _safe_is_dir(path) -> bool:
return False
-# Hub repo id shape ("owner/name", no leading separator); anything else is
-# treated as a local filesystem path.
-_HF_REPO_ID_RE = re.compile(r"^[A-Za-z0-9][\w.\-]*/[\w.\-]+$")
-
-
-def _is_hidden_model(*values: str | None) -> bool:
- """True if any id/path is the RAG embedding model (EMBEDDING_MODEL or
- EMBED_GGUF_REPO basename) or the llama.cpp install validation probe
- (ggml-org/models / stories260K), so pickers hide them (GGUF and non-GGUF).
- None are usable chat models; the probe can be cached as a side effect of
- installing the prebuilt llama-server and otherwise sorts smallest, so it
- would be auto-selected. A local-path embedder is matched by exact resolved
- path only: a generic basename like "model" must not substring-hide
- unrelated chat models."""
- from core.rag import config as rag_config
-
- needles = [
- # The validation probe's repo (matches the cached repo id) and its exact
- # filename (matches the on-disk path). The filename carries the .gguf so
- # it does not hide unrelated repos like ``user/stories260K-finetune-GGUF``.
- "ggml-org/models",
- "stories260k.gguf",
- ]
- exact_paths: list[str] = []
- for model in (
- rag_config.effective_embedding_model(),
- rag_config.effective_gguf_repo(),
- ):
- if _HF_REPO_ID_RE.match(model):
- needles.append(model.split("/")[-1].lower())
- else:
- resolved = _safe_resolve(Path(model).expanduser())
- if resolved:
- exact_paths.append(resolved.lower())
- for v in values:
- if not v:
- continue
- low = v.lower()
- if any(n in low for n in needles):
- return True
- if exact_paths:
- resolved = _safe_resolve(Path(v).expanduser())
- if resolved and resolved.lower() in exact_paths:
- return True
- return False
-
-
-def _safe_resolve(path: Path) -> Optional[str]:
- """resolve() to a string, or None when the path is inaccessible."""
- try:
- return str(path.resolve())
- except OSError:
- return None
+# Shared with the hub inventory scans; keep the private aliases so existing
+# importers (core.inference.local_model_resolver, tests) stay valid.
+from utils.hidden_models import (
+ _safe_resolve,
+ is_hidden_model as _is_hidden_model,
+)
backend_path = Path(__file__).parent.parent.parent
@@ -853,7 +806,7 @@ def collect_local_models(models_root: Path) -> List[LocalModelInfo]:
key = lambda item: (item.updated_at or 0),
reverse = True,
)
- return [m for m in models if not _is_hidden_model(m.id, m.path)]
+ return [m for m in models if not _is_hidden_model(m.id, m.model_id, m.path)]
@router.get("/local", response_model = LocalModelListResponse)
diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.py
index 1ddfc0eacb..ab0fd2fd99 100644
--- a/studio/backend/routes/settings.py
+++ b/studio/backend/routes/settings.py
@@ -10,6 +10,7 @@ from pydantic import BaseModel, ConfigDict, Field, field_validator
from auth.authentication import get_current_subject
from auth.storage import rotate_preview_link_secret
+from core.rag.config import default_gguf_repo, effective_gguf_repo
from loggers import get_logger
from utils.utils import safe_error_detail, log_and_http_error
from utils.personalization_settings import (
@@ -263,14 +264,18 @@ class EmbeddingModelPayload(BaseModel):
class EmbeddingModelResponse(BaseModel):
embedding_model: str
+ embedding_gguf_repo: str
default_embedding_model: str
+ default_embedding_gguf_repo: str
is_custom: bool
def _embedding_model_response() -> EmbeddingModelResponse:
return EmbeddingModelResponse(
embedding_model = get_rag_embedding_model(),
+ embedding_gguf_repo = effective_gguf_repo(),
default_embedding_model = default_embedding_model(),
+ default_embedding_gguf_repo = default_gguf_repo(),
is_custom = get_stored_embedding_model() is not None,
)
diff --git a/studio/backend/tests/test_cached_gguf_routes.py b/studio/backend/tests/test_cached_gguf_routes.py
index d4a7cae208..b3e6255d55 100644
--- a/studio/backend/tests/test_cached_gguf_routes.py
+++ b/studio/backend/tests/test_cached_gguf_routes.py
@@ -120,12 +120,151 @@ def test_is_hidden_model_hides_validation_probe_everywhere():
assert models_route._is_hidden_model(
None, "/hf/models--ggml-org--models/snapshots/abc/tinyllamas/stories260K.gguf"
)
+ # A Windows-style snapshot path must match too, even on a POSIX interpreter
+ # (the filename check splits on both separators).
+ assert models_route._is_hidden_model(
+ r"C:\Users\u\.cache\huggingface\hub\models--ggml-org--models\snapshots\abc\tinyllamas\stories260K.gguf"
+ )
assert not models_route._is_hidden_model("unsloth/gemma-3-270m-it-GGUF")
# The exact-filename needle must not hide a real repo that merely
# references stories260K in its name.
assert not models_route._is_hidden_model("user/stories260K-finetune-GGUF")
+def test_is_hidden_model_matches_repo_ids_exactly(monkeypatch):
+ """A custom embedder with a generic basename is hidden by EXACT repo-id
+ match only, so unrelated cached repos that merely contain the basename stay
+ visible. Regression: substring basename matching hid real chat models like
+ ``user/model-chat`` from the On Device inventory."""
+ from core.rag import config as rag_config
+
+ monkeypatch.setattr(rag_config, "effective_embedding_model", lambda: "org/model")
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/model-GGUF")
+
+ # The exact embedder repo and its GGUF companion are hidden.
+ assert models_route._is_hidden_model("org/model")
+ assert models_route._is_hidden_model("org/model-GGUF")
+ # Unrelated repos that merely contain "model" must NOT be hidden.
+ assert not models_route._is_hidden_model("user/model-chat")
+ assert not models_route._is_hidden_model("org/model-instruct")
+ assert not models_route._is_hidden_model("acme/remodelled-chat")
+ # The validation probe stays hidden regardless of embedder config.
+ assert models_route._is_hidden_model("ggml-org/models")
+
+
+def test_is_hidden_model_matches_repo_derived_local_paths(monkeypatch):
+ """Match exact repo-derived cache and LM Studio paths."""
+ from core.rag import config as rag_config
+
+ monkeypatch.setattr(rag_config, "effective_embedding_model", lambda: "org/model")
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/model-GGUF")
+
+ assert models_route._is_hidden_model(
+ "/cache/models--org--model/snapshots/abc/model.safetensors"
+ )
+ assert models_route._is_hidden_model(
+ r"C:\Users\u\.cache\huggingface\hub\models--org--model-GGUF\snapshots\abc"
+ )
+ assert models_route._is_hidden_model("/lm-studio/org/model-GGUF/model-Q8_0.gguf")
+ assert not models_route._is_hidden_model("/lm-studio/user/model-chat/model-Q8_0.gguf")
+ assert not models_route._is_hidden_model("/cache/models--org--model-instruct")
+
+
+def test_is_hidden_model_prefers_existing_relative_path(monkeypatch, tmp_path):
+ """Prefer an existing relative path over repo-id syntax."""
+ from core.rag import config as rag_config
+
+ embedder = tmp_path / "models" / "embedder"
+ embedder.mkdir(parents = True)
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(rag_config, "effective_embedding_model", lambda: "models/embedder")
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/embedder-GGUF")
+
+ assert models_route._is_hidden_model(str(embedder))
+
+
+def test_is_hidden_model_keeps_stale_default_embedder_hidden(monkeypatch):
+ """Keep default embedders hidden after a settings change."""
+ from core.rag import config as rag_config
+
+ monkeypatch.setattr(rag_config, "effective_embedding_model", lambda: "org/custom")
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/custom-GGUF")
+
+ assert models_route._is_hidden_model("unsloth/bge-small-en-v1.5")
+ assert models_route._is_hidden_model("unsloth/bge-small-en-v1.5-GGUF")
+ assert models_route._is_hidden_model("/models/bge-small-en-v1.5")
+ assert models_route._is_hidden_model("/models/bge-small-en-v1.5-F16.gguf")
+ assert models_route._is_hidden_model(r"C:\models\bge-small-en-v1.5-Q8_0.gguf")
+ # Repo IDs still use exact matching, and similar local basenames must have
+ # a real separator after the static default name.
+ assert not models_route._is_hidden_model("user/bge-small-en-v1.5-chat")
+ assert not models_route._is_hidden_model("/models/bge-small-en-v1.50")
+
+
+def test_is_hidden_model_keeps_env_default_hidden_after_override(monkeypatch):
+ """A persisted override must not expose the deployment's env default."""
+ from core.rag import config as rag_config
+
+ monkeypatch.delenv("RAG_EMBED_GGUF_REPO", raising = False)
+ monkeypatch.setattr(rag_config, "EMBEDDING_MODEL", "org/env-default")
+ monkeypatch.setattr(rag_config, "effective_embedding_model", lambda: "org/custom")
+ monkeypatch.setattr(rag_config, "effective_gguf_repo", lambda: "org/custom-GGUF")
+
+ assert models_route._is_hidden_model("org/env-default")
+ assert models_route._is_hidden_model("org/env-default-GGUF")
+ assert models_route._is_hidden_model("org/custom")
+ assert models_route._is_hidden_model("org/custom-GGUF")
+ assert not models_route._is_hidden_model("org/env-default-chat")
+
+
+def test_hidden_models_importable_without_heavy_model_stack():
+ """The hub cache scanner imports ``is_hidden_model`` at module scope, so it
+ must not drag in ``utils/models/__init__`` (the model-config + checkpoint
+ stack). Verify in a clean interpreter that importing the helper touches
+ neither ``utils.models`` nor those heavy submodules, and still classifies
+ the probe."""
+ import os
+ import subprocess
+ import textwrap
+
+ backend = Path(__file__).resolve().parents[1]
+ code = textwrap.dedent(
+ """
+ import sys
+
+ class _Blocker:
+ _blocked = (
+ "utils.models",
+ "utils.models.model_config",
+ "utils.models.checkpoints",
+ )
+
+ def find_spec(self, name, path=None, target=None):
+ if name in self._blocked:
+ raise ImportError("blocked heavy import: " + name)
+ return None
+
+ sys.meta_path.insert(0, _Blocker())
+ from utils.hidden_models import is_hidden_model
+
+ loaded = sorted(m for m in sys.modules if m.startswith("utils.models"))
+ assert not loaded, loaded
+ assert is_hidden_model("ggml-org/models") is True
+ assert is_hidden_model("unsloth/gemma-3-270m-it-GGUF") is False
+ print("HIDDEN_MODELS_IMPORT_OK")
+ """
+ )
+ env = dict(os.environ, PYTHONPATH = str(backend))
+ proc = subprocess.run(
+ [sys.executable, "-c", code],
+ capture_output = True,
+ text = True,
+ env = env,
+ )
+ assert proc.returncode == 0, proc.stderr
+ assert "HIDDEN_MODELS_IMPORT_OK" in proc.stdout
+
+
def test_list_cached_gguf_hides_llama_validation_probe(monkeypatch, tmp_path):
"""The ggml-org/models / stories260K install validation probe can land in
the HF cache as a side effect of installing the prebuilt llama-server.
diff --git a/studio/backend/tests/test_embedding_model_security_gate.py b/studio/backend/tests/test_embedding_model_security_gate.py
index 940b35d7ba..b3fa98b604 100644
--- a/studio/backend/tests/test_embedding_model_security_gate.py
+++ b/studio/backend/tests/test_embedding_model_security_gate.py
@@ -52,6 +52,16 @@ def client(monkeypatch):
monkeypatch.setattr(settings, "_resolves_as_local_gguf", lambda m: False)
monkeypatch.setattr(settings, "get_rag_embedding_model", lambda: saved.get("model", ""))
monkeypatch.setattr(settings, "get_stored_embedding_model", lambda: saved.get("model"))
+ monkeypatch.setattr(
+ settings,
+ "effective_gguf_repo",
+ lambda: f"{saved.get('model', 'unsloth/default-embed')}-GGUF",
+ )
+ monkeypatch.setattr(
+ settings,
+ "default_gguf_repo",
+ lambda: "unsloth/default-embed-GGUF",
+ )
app = FastAPI()
app.include_router(settings.router)
@@ -257,6 +267,13 @@ def test_clean_repo_saves_under_force(client, monkeypatch):
r = c.put("/embedding-model", json = {"embedding_model": "acme/clean-embed", "force": True})
assert r.status_code == 200
assert saved.get("model") == "acme/clean-embed"
+ assert r.json() == {
+ "embedding_model": "acme/clean-embed",
+ "embedding_gguf_repo": "acme/clean-embed-GGUF",
+ "default_embedding_model": "unsloth/default-embed",
+ "default_embedding_gguf_repo": "unsloth/default-embed-GGUF",
+ "is_custom": True,
+ }
def test_load_sink_refuses_flagged_model(monkeypatch):
diff --git a/studio/backend/tests/test_embedding_model_settings.py b/studio/backend/tests/test_embedding_model_settings.py
index 3be4af0e32..bcf3ded71c 100644
--- a/studio/backend/tests/test_embedding_model_settings.py
+++ b/studio/backend/tests/test_embedding_model_settings.py
@@ -53,3 +53,10 @@ def test_custom_model_overrides_default_and_derives_gguf(settings_store, monkeyp
assert ems.reset_rag_embedding_model() == rag_config.EMBEDDING_MODEL
assert ems.get_stored_embedding_model() is None
+
+
+def test_env_default_derives_its_gguf_companion(monkeypatch):
+ monkeypatch.delenv("RAG_EMBED_GGUF_REPO", raising = False)
+ monkeypatch.setattr(rag_config, "EMBEDDING_MODEL", "org/env-default-embedder")
+
+ assert rag_config.default_gguf_repo() == "org/env-default-embedder-GGUF"
diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.py
index 90fbd19297..c4c0ce15c9 100644
--- a/studio/backend/tests/test_openai_auto_switch.py
+++ b/studio/backend/tests/test_openai_auto_switch.py
@@ -697,6 +697,10 @@ def test_index_excludes_hidden_models(tmp_path, monkeypatch):
normal.write_bytes(b"x" * 32)
probe = tmp_path / "stories260K.gguf" # llama.cpp install-validation probe
probe.write_bytes(b"x" * 32)
+ embedder = tmp_path / "embedding-Q8_0.gguf"
+ embedder.write_bytes(b"x" * 32)
+ local_default_embedder = tmp_path / "bge-small-en-v1.5-F16.gguf"
+ local_default_embedder.write_bytes(b"x" * 32)
def _info(mid, path):
return SimpleNamespace(id = mid, path = str(path), model_id = mid, display_name = mid)
@@ -704,7 +708,22 @@ def test_index_excludes_hidden_models(tmp_path, monkeypatch):
monkeypatch.setattr(
models_route,
"_scan_models_dir",
- lambda *a, **k: [_info("org/Normal-GGUF", normal), _info("ggml-org/models", probe)],
+ lambda *a, **k: [
+ _info("org/Normal-GGUF", normal),
+ _info("ggml-org/models", probe),
+ SimpleNamespace(
+ id = str(embedder),
+ path = str(embedder),
+ model_id = "unsloth/bge-small-en-v1.5-GGUF",
+ display_name = "embedding-Q8_0",
+ ),
+ SimpleNamespace(
+ id = str(local_default_embedder),
+ path = str(local_default_embedder),
+ model_id = None,
+ display_name = local_default_embedder.name,
+ ),
+ ],
)
monkeypatch.setattr(models_route, "_scan_hf_cache", lambda *a, **k: [])
monkeypatch.setattr(models_route, "_resolve_hf_cache_dir", lambda: tmp_path)
@@ -713,6 +732,8 @@ def test_index_excludes_hidden_models(tmp_path, monkeypatch):
index = resolver._index()
assert "org/normal-gguf" in index # keys are normalized to lowercase
assert "ggml-org/models" not in index
+ assert "unsloth/bge-small-en-v1.5-gguf" not in index
+ assert str(local_default_embedder).lower() not in index
# And the hidden probe cannot be auto-switched to by name.
resolver._scan = (0.0, {})
assert resolver.resolve_local_gguf("ggml-org/models") is None
@@ -1729,6 +1750,8 @@ def test_index_advertises_alias_not_filesystem_path(tmp_path, monkeypatch):
# host path in /v1/models, yet the model stays resolvable by that path too.
from types import SimpleNamespace
import routes.models as models_route
+ from storage import studio_db
+ import utils.paths as paths
gguf = tmp_path / "model-Q4_K_M.gguf"
gguf.write_bytes(b"x" * 32)
@@ -1742,6 +1765,8 @@ def test_index_advertises_alias_not_filesystem_path(tmp_path, monkeypatch):
monkeypatch.setattr(models_route, "_scan_hf_cache", lambda *a, **k: [])
monkeypatch.setattr(models_route, "_resolve_hf_cache_dir", lambda: tmp_path)
monkeypatch.setattr(models_route, "_is_hidden_model", lambda *a, **k: False)
+ monkeypatch.setattr(paths, "lmstudio_model_dirs", lambda: [])
+ monkeypatch.setattr(studio_db, "list_scan_folders", lambda: [])
resolver._scan = (0.0, {})
# The advertised id is the alias, never the absolute path.
diff --git a/studio/backend/utils/hidden_models.py b/studio/backend/utils/hidden_models.py
new file mode 100644
index 0000000000..20d0bb966e
--- /dev/null
+++ b/studio/backend/utils/hidden_models.py
@@ -0,0 +1,142 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Infra-only model detection shared by the model routes and the hub
+inventory. Lives directly under ``utils`` (not ``utils.models``) so the hub
+cache scanner can import it without pulling in ``utils/models/__init__.py``,
+which eagerly loads the model-config/checkpoint stack, and without importing
+``routes.models`` (import-time side effects, would cycle)."""
+
+from __future__ import annotations
+
+import re
+from pathlib import Path
+from typing import Optional
+
+# Hub repo id shape ("owner/name", no leading separator); anything else is
+# treated as a local filesystem path.
+_HF_REPO_ID_RE = re.compile(r"^[A-Za-z0-9][\w.\-]*/[\w.\-]+$")
+
+# The llama.cpp install-validation probe repo. Always hidden.
+_PROBE_REPO_ID = "ggml-org/models"
+# The probe's on-disk filename. Carries the ".gguf" so it stays specific and
+# does not hide unrelated repos like ``user/stories260K-finetune-GGUF``.
+_PROBE_FILENAME = "stories260k.gguf"
+# Keep previously cached defaults hidden after settings changes.
+_DEFAULT_EMBEDDING_REPO_IDS = {
+ "unsloth/bge-small-en-v1.5",
+ "unsloth/bge-small-en-v1.5-GGUF",
+}
+# Local copies do not always retain the repo id. Keep a narrow basename
+# fallback for Studio's static default embedder only; configured custom repos
+# remain exact-match-only.
+_DEFAULT_EMBEDDING_PATH_BASENAMES = {"bge-small-en-v1.5"}
+
+
+def _safe_resolve(path: Path) -> Optional[str]:
+ """resolve() to a string, or None when the path is inaccessible."""
+ try:
+ return str(path.resolve())
+ except OSError:
+ return None
+
+
+def _existing_resolved_path(value: str) -> Optional[str]:
+ """Resolve an existing local path."""
+ path = Path(value).expanduser()
+ try:
+ if not path.exists():
+ return None
+ except OSError:
+ return None
+ return _safe_resolve(path)
+
+
+def _path_contains_repo_id(value: str, repo_ids: set[str]) -> bool:
+ """Match exact repo-derived path segments."""
+ parts = [part for part in value.lower().replace("\\", "/").split("/") if part]
+ for repo_id in repo_ids:
+ owner, name = repo_id.split("/", 1)
+ if f"models--{owner}--{name}" in parts:
+ return True
+ if any(
+ parts[index] == owner and parts[index + 1] == name for index in range(len(parts) - 1)
+ ):
+ return True
+ return False
+
+
+def _path_basename_is_default_embedder(value: str) -> bool:
+ """Match a default embedder folder or a suffixed local weight filename."""
+ normalized = value.lower().replace("\\", "/").rstrip("/")
+ basename = normalized.rsplit("/", 1)[-1]
+ return any(
+ basename == needle
+ or any(basename.startswith(f"{needle}{separator}") for separator in ("-", "_", "."))
+ for needle in _DEFAULT_EMBEDDING_PATH_BASENAMES
+ )
+
+
+def is_hidden_model(*values: str | None) -> bool:
+ """True if any id/path is the RAG embedding model (the effective embedder
+ or its GGUF companion repo) or the llama.cpp install validation probe
+ (ggml-org/models / stories260K), so pickers hide them (GGUF and non-GGUF).
+ None are usable chat models; the probe can be cached as a side effect of
+ installing the prebuilt llama-server and otherwise sorts smallest, so it
+ would be auto-selected.
+
+ Hub repo ids are matched EXACTLY (case-insensitive full "owner/name"), so a
+ custom embedder with a generic basename like "org/model" cannot substring
+ hide unrelated cached repos such as "user/model-chat" or "org/model-GGUF".
+ Existing paths take precedence over the identical ``owner/name`` repo
+ shape. Cache and LM Studio paths use exact repo-derived segments. Local
+ copies of the static default embedder also use a boundary-aware basename
+ fallback; configured custom repos never do."""
+ from core.rag import config as rag_config
+
+ hidden_repo_ids = {
+ _PROBE_REPO_ID.lower(),
+ *(repo_id.lower() for repo_id in _DEFAULT_EMBEDDING_REPO_IDS),
+ }
+ exact_paths: list[str] = []
+ for model in {
+ rag_config.EMBEDDING_MODEL,
+ rag_config.default_gguf_repo(),
+ rag_config.effective_embedding_model(),
+ rag_config.effective_gguf_repo(),
+ }:
+ existing_path = _existing_resolved_path(model)
+ if existing_path:
+ exact_paths.append(existing_path.lower())
+ elif _HF_REPO_ID_RE.match(model):
+ hidden_repo_ids.add(model.lower())
+ else:
+ resolved = _safe_resolve(Path(model).expanduser())
+ if resolved:
+ exact_paths.append(resolved.lower())
+ for v in values:
+ if not v:
+ continue
+ low = v.lower()
+ if _HF_REPO_ID_RE.match(v):
+ # A repo id ("owner/name"): match the hidden set exactly. It is
+ # never a filesystem path, so skip the path/filename checks.
+ if low in hidden_repo_ids:
+ return True
+ continue
+ # Anything else is treated as a filesystem path (the cached snapshot
+ # path, or a local model id). Match the probe by its exact filename and
+ # any configured local-path embedder by exact resolved path. Split on
+ # both separators so a Windows-style path ("...\\stories260K.gguf") is
+ # matched even when this runs on a POSIX interpreter (and vice versa).
+ if low.replace("\\", "/").rsplit("/", 1)[-1] == _PROBE_FILENAME:
+ return True
+ if _path_basename_is_default_embedder(v):
+ return True
+ if _path_contains_repo_id(v, hidden_repo_ids):
+ return True
+ if exact_paths:
+ resolved = _safe_resolve(Path(v).expanduser())
+ if resolved and resolved.lower() in exact_paths:
+ return True
+ return False
diff --git a/studio/frontend/src/features/hub/hooks/use-hidden-embedding-models.ts b/studio/frontend/src/features/hub/hooks/use-hidden-embedding-models.ts
new file mode 100644
index 0000000000..f78679310f
--- /dev/null
+++ b/studio/frontend/src/features/hub/hooks/use-hidden-embedding-models.ts
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import { loadEmbeddingModelSettings } from "@/features/settings";
+import { useEffect, useState } from "react";
+import { useInventoryVersion } from "../stores/inventory-events";
+
+/** Backend-resolved embedding repos that optimistic inventory rows must hide. */
+export function useHiddenEmbeddingModelIds(
+ enabled: boolean,
+): ReadonlySet {
+ const inventoryVersion = useInventoryVersion();
+ const [hiddenIds, setHiddenIds] = useState>(
+ () => new Set(),
+ );
+
+ // biome-ignore lint/correctness/useExhaustiveDependencies: inventory invalidation must reload backend-resolved embedder ids
+ useEffect(() => {
+ if (!enabled) {
+ return;
+ }
+ let cancelled = false;
+ loadEmbeddingModelSettings()
+ .then((settings) => {
+ if (cancelled) {
+ return;
+ }
+ setHiddenIds(
+ new Set(
+ [
+ settings.embeddingModel,
+ settings.embeddingGgufRepo,
+ settings.defaultEmbeddingModel,
+ settings.defaultEmbeddingGgufRepo,
+ ].map((value) => value.trim().toLowerCase()),
+ ),
+ );
+ })
+ .catch(() => undefined);
+ return () => {
+ cancelled = true;
+ };
+ }, [enabled, inventoryVersion]);
+
+ return hiddenIds;
+}
diff --git a/studio/frontend/src/features/hub/hub-page.tsx b/studio/frontend/src/features/hub/hub-page.tsx
index 630daa48ad..d57f9636fe 100644
--- a/studio/frontend/src/features/hub/hub-page.tsx
+++ b/studio/frontend/src/features/hub/hub-page.tsx
@@ -63,6 +63,7 @@ import { useDiscoverSearch } from "./hooks/use-discover-search";
import { useFeedWriteBack } from "./hooks/use-feed-write-back";
import { useHubFeed } from "./hooks/use-hub-feed";
import { useHubModelVram } from "./hooks/use-hub-model-vram";
+import { useHiddenEmbeddingModelIds } from "./hooks/use-hidden-embedding-models";
import { useModelsSelection } from "./hooks/use-models-selection";
import {
CHANNEL_TO_SECTION,
@@ -73,7 +74,10 @@ import {
SECTION_TO_CHANNEL,
findChannel,
} from "./lib/channels";
-import { isHiddenModelId } from "./lib/hidden-models";
+import {
+ isConfiguredHiddenModelId,
+ isHiddenModelId,
+} from "./lib/hidden-models";
import { inventoryRowMatches, tokenizeQuery } from "./lib/inventory-search";
import { resolveOwnerProviderLogo } from "./lib/provider-logos";
import {
@@ -386,6 +390,7 @@ export function ModelsPage() {
useState("all");
const isDiscoverTab = tab === "discover";
const isDatasetMode = resourceType === "datasets";
+ const hiddenEmbeddingModelIds = useHiddenEmbeddingModelIds(!isDatasetMode);
const urlSection = hubSearch.section ?? null;
const isModelDiscover = isDiscoverTab && !isDatasetMode;
const sectionChannelId: ChannelId | null = urlSection
@@ -700,6 +705,7 @@ export function ModelsPage() {
return discoverRows.filter(
(row) =>
!isHiddenModelId(row.id) &&
+ !isConfiguredHiddenModelId(hiddenEmbeddingModelIds, row.id) &&
// The default feed only shows models with a provider logo.
(!isFeedMode ||
resolveOwnerProviderLogo(row.owner, row.repo) !== null) &&
@@ -714,6 +720,7 @@ export function ModelsPage() {
);
}, [
discoverRows,
+ hiddenEmbeddingModelIds,
isDatasetMode,
isFeedMode,
effectiveDiscoverFormat,
@@ -739,7 +746,11 @@ export function ModelsPage() {
effectiveCachedRows,
effectiveLocalRows,
)
- .filter((row) => !isHiddenModelId(row.id))
+ .filter(
+ (row) =>
+ !isHiddenModelId(row.id) &&
+ !isConfiguredHiddenModelId(hiddenEmbeddingModelIds, row.id),
+ )
.filter((row) => matchesFormat(row.result.isGguf, "gguf"))
// Same fit filter as the main Discover list, so the feed carousel
// honors the toggle too.
@@ -751,6 +762,7 @@ export function ModelsPage() {
),
[
hubFeed.trending.results,
+ hiddenEmbeddingModelIds,
modelDiscoveryInventorySignature,
fitOnDeviceOnly,
gpu,
@@ -778,22 +790,29 @@ export function ModelsPage() {
() => (isDiscoverTab ? [] : tokenizeQuery(deferredDebouncedQuery)),
[isDiscoverTab, deferredDebouncedQuery],
);
- // Hide infra models (e.g. the RAG embedder bge-small-en-v1.5) from the On
- // Device list like Discover, but reveal a row when a query matches it so the
- // user can confirm it is already downloaded.
+ // Server cache rows already apply variant-aware infra hiding. Optimistic
+ // rows are not server-confirmed, so apply the client filter first.
const isVisibleInventoryRow = useCallback(
- (row: CachedInventoryRow | LocalInventoryRow) =>
- // Local rows can have a null repoId and an id that is a hash rather than
- // the file path/name, so also check path/title (the backend's
- // _is_hidden_model checks the on-disk path for the same reason).
- !isHiddenModelId(
- row.id,
- row.repoId,
- row.kind !== "cache" ? row.path : undefined,
- row.kind !== "cache" ? row.title : undefined,
- ) ||
- (inventoryTokens.length > 0 && inventoryRowMatches(row, inventoryTokens)),
- [inventoryTokens],
+ (row: CachedInventoryRow | LocalInventoryRow) => {
+ if (row.kind === "cache") {
+ return (
+ !row.optimistic ||
+ (!isHiddenModelId(row.id, row.repoId, row.cachePath) &&
+ !isConfiguredHiddenModelId(
+ hiddenEmbeddingModelIds,
+ row.id,
+ row.repoId,
+ row.cachePath,
+ ))
+ );
+ }
+ // Local rows may lack a repo id, so also check path and title.
+ return (
+ !isHiddenModelId(row.id, row.repoId, row.path, row.title) ||
+ (inventoryTokens.length > 0 && inventoryRowMatches(row, inventoryTokens))
+ );
+ },
+ [hiddenEmbeddingModelIds, inventoryTokens],
);
// Format filter is a deliberate scope narrowing, so hard-filter it out. The
// text query instead drives dim-not-filter on On Device (see ModelsCatalog) so
diff --git a/studio/frontend/src/features/hub/index.ts b/studio/frontend/src/features/hub/index.ts
index 3515f6ca76..5d4151e87d 100644
--- a/studio/frontend/src/features/hub/index.ts
+++ b/studio/frontend/src/features/hub/index.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
export { cancelStagedModelDownload } from "./download-manager";
+export { bumpInventoryVersion } from "./stores/inventory-events";
export {
getHfToken,
mirrorHfTokenInto,
diff --git a/studio/frontend/src/features/hub/inventory/inventory-hints.ts b/studio/frontend/src/features/hub/inventory/inventory-hints.ts
index af9f254ab3..5e202e3150 100644
--- a/studio/frontend/src/features/hub/inventory/inventory-hints.ts
+++ b/studio/frontend/src/features/hub/inventory/inventory-hints.ts
@@ -12,6 +12,7 @@ export type InventoryHintRow = {
repo_id: string;
size_bytes: number;
partial?: boolean;
+ optimistic?: boolean;
};
export type InventoryHintReconciliation = {
@@ -41,6 +42,7 @@ function optimisticRow(hint: InventoryHint): InventoryHintRow {
repo_id: hint.repoId,
size_bytes: hint.bytes ?? 0,
partial: false,
+ optimistic: true,
};
}
@@ -101,9 +103,14 @@ function mergeInventoryHint(
if (idx === -1) {
return [...rows, seed];
}
+ const serverRow = rows[idx];
const merged = {
- ...rows[idx],
- ...seed,
+ ...serverRow,
+ // A completed hint may arrive before a partial server scan catches up. In
+ // that case keep the synthetic row non-runnable. A complete server row is
+ // already authoritative even when its runnable-weight size is smaller than
+ // the hint's full-snapshot byte count, so do not mark that merge optimistic.
+ ...(serverRow.partial ? seed : { optimistic: false }),
size_bytes: Math.max(rowSizeBytes(rows[idx]), rowSizeBytes(seed)),
};
return [...rows.slice(0, idx), merged, ...rows.slice(idx + 1)];
diff --git a/studio/frontend/src/features/hub/inventory/types.ts b/studio/frontend/src/features/hub/inventory/types.ts
index c86ffb1d86..6f65a56037 100644
--- a/studio/frontend/src/features/hub/inventory/types.ts
+++ b/studio/frontend/src/features/hub/inventory/types.ts
@@ -54,6 +54,7 @@ export interface CachedInventoryRow {
libraryName?: string | null;
quantMethod?: string | null;
liveDownload?: boolean;
+ optimistic?: boolean;
}
export interface LocalInventoryRow {
diff --git a/studio/frontend/src/features/hub/inventory/use-hub-inventory.ts b/studio/frontend/src/features/hub/inventory/use-hub-inventory.ts
index a7dc7ae3f3..fea7b3d331 100644
--- a/studio/frontend/src/features/hub/inventory/use-hub-inventory.ts
+++ b/studio/frontend/src/features/hub/inventory/use-hub-inventory.ts
@@ -204,6 +204,7 @@ function liveDownloadInventoryRows(
size_bytes: job.displayBytes,
partial: true,
partial_transport: null,
+ optimistic: true,
},
modelFormat,
),
diff --git a/studio/frontend/src/features/hub/inventory/view-models.ts b/studio/frontend/src/features/hub/inventory/view-models.ts
index 63d70418be..334050fab4 100644
--- a/studio/frontend/src/features/hub/inventory/view-models.ts
+++ b/studio/frontend/src/features/hub/inventory/view-models.ts
@@ -176,6 +176,7 @@ export function buildCachedInventoryRow(
runtime?: string | null;
format_variant?: string | null;
capabilities?: BackendModelCapabilities | null;
+ optimistic?: boolean;
},
fallbackFormat: ModelInventoryFormat,
): CachedInventoryRow {
@@ -185,6 +186,15 @@ export function buildCachedInventoryRow(
const inferredFromEndpoint =
rawModelFormat === "unknown" && modelFormat !== "unknown";
const requiresVariant = modelFormat === "gguf";
+ const capabilities = normalizeCapabilities(
+ inferredFromEndpoint ? null : row.capabilities,
+ modelFormat,
+ row.partial ?? false,
+ requiresVariant,
+ );
+ if (row.optimistic) {
+ capabilities.canChat = false;
+ }
return {
kind: "cache",
id:
@@ -202,12 +212,7 @@ export function buildCachedInventoryRow(
modelFormat,
),
formatVariant: row.format_variant ?? null,
- capabilities: normalizeCapabilities(
- inferredFromEndpoint ? null : row.capabilities,
- modelFormat,
- row.partial ?? false,
- requiresVariant,
- ),
+ capabilities,
bytes: row.size_bytes,
cachePath: row.cache_path ?? null,
partial: row.partial ?? false,
@@ -216,6 +221,7 @@ export function buildCachedInventoryRow(
tags: row.tags,
libraryName: row.library_name ?? null,
quantMethod: row.quant_method ?? null,
+ optimistic: row.optimistic,
};
}
diff --git a/studio/frontend/src/features/hub/lib/hidden-models.ts b/studio/frontend/src/features/hub/lib/hidden-models.ts
index 2dbe257947..634a061e0c 100644
--- a/studio/frontend/src/features/hub/lib/hidden-models.ts
+++ b/studio/frontend/src/features/hub/lib/hidden-models.ts
@@ -1,11 +1,13 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Infra models hidden from every browse/preview list (Hub discover and the chat
-// model selector). Mirrors the backend `_is_hidden_model`: the RAG embedding
-// model and the llama.cpp validation probe are not usable chat models. Per-repo
-// file/download views are NOT filtered, so a reinstall still shows the model as
-// already downloaded.
+// Infra models hidden from browse/preview lists (Hub Discover, the chat model
+// selector, and local on-device rows). Mirrors the backend
+// `utils.hidden_models`: the RAG embedding model and the llama.cpp validation
+// probe are not usable chat models. Server-confirmed cache rows are trusted
+// because the backend applies variant-aware filtering. Optimistic cache rows
+// still use these needles until the server confirms them. Per-repo views are
+// not filtered, so reinstall flows still show downloaded files.
const HIDDEN_NEEDLES = [
"bge-small-en-v1.5", // RAG embedder: unsloth/bge-small-en-v1.5[-GGUF]
"ggml-org/models", // llama.cpp validation probe repo
@@ -17,8 +19,20 @@ export function isHiddenModelId(
...values: (string | null | undefined)[]
): boolean {
return values.some((v) => {
- if (!v) return false;
+ if (!v) {
+ return false;
+ }
const lower = v.toLowerCase();
return HIDDEN_NEEDLES.some((needle) => lower.includes(needle));
});
}
+
+/** Exact-match configured infra repos without hiding similarly named models. */
+export function isConfiguredHiddenModelId(
+ configuredIds: ReadonlySet,
+ ...values: (string | null | undefined)[]
+): boolean {
+ return values.some(
+ (value) => value != null && configuredIds.has(value.trim().toLowerCase()),
+ );
+}
diff --git a/studio/frontend/src/features/settings/api/embedding-model.ts b/studio/frontend/src/features/settings/api/embedding-model.ts
index 9a61142f73..cc21559f38 100644
--- a/studio/frontend/src/features/settings/api/embedding-model.ts
+++ b/studio/frontend/src/features/settings/api/embedding-model.ts
@@ -2,11 +2,14 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { authFetch } from "@/features/auth";
+import { bumpInventoryVersion } from "@/features/hub";
import { readFastApiError } from "@/lib/format-fastapi-error";
export type EmbeddingModelSettings = {
embeddingModel: string;
+ embeddingGgufRepo: string;
defaultEmbeddingModel: string;
+ defaultEmbeddingGgufRepo: string;
isCustom: boolean;
};
@@ -14,8 +17,12 @@ type ApiEmbeddingModelSettings = {
// biome-ignore lint/style/useNamingConvention: API schema
embedding_model: string;
// biome-ignore lint/style/useNamingConvention: API schema
+ embedding_gguf_repo: string;
+ // biome-ignore lint/style/useNamingConvention: API schema
default_embedding_model: string;
// biome-ignore lint/style/useNamingConvention: API schema
+ default_embedding_gguf_repo: string;
+ // biome-ignore lint/style/useNamingConvention: API schema
is_custom: boolean;
};
@@ -30,7 +37,9 @@ export class EmbeddingModelBlockedError extends Error {}
function fromApi(settings: ApiEmbeddingModelSettings): EmbeddingModelSettings {
return {
embeddingModel: settings.embedding_model,
+ embeddingGgufRepo: settings.embedding_gguf_repo,
defaultEmbeddingModel: settings.default_embedding_model,
+ defaultEmbeddingGgufRepo: settings.default_embedding_gguf_repo,
isCustom: settings.is_custom,
};
}
@@ -75,7 +84,9 @@ export async function updateEmbeddingModelSettings(
await readFastApiError(res, "Failed to save embedding model"),
);
}
- return fromApi(await res.json());
+ const settings = fromApi(await res.json());
+ bumpInventoryVersion();
+ return settings;
}
export async function resetEmbeddingModelSettings(): Promise {
@@ -87,5 +98,7 @@ export async function resetEmbeddingModelSettings(): Promise
Date: Sun, 19 Jul 2026 18:37:23 +0800
Subject: [PATCH 029/271] fix(registry): don't register deepseek models at
import time (#7227)
* fix(registry): don't register deepseek models at import time
`_deepseek.py` called `register_deepseek_models(include_original_model=True)`
at module scope, so merely importing `unsloth.registry` registered models
(and reached the hub via `list_models`) as a side effect. None of the other
five families (`_gemma`/`_llama`/`_mistral`/`_phi`/`_qwen`) do this; they only
register when `register_models()` asks them to.
Two consequences:
- Importing the registry populated MODEL_REGISTRY on its own (32 entries,
including 10 `deepseek-ai` original models that no other family leaks) and
did network I/O at import time.
- Because the import-time call set the `_IS_DEEPSEEK_*_REGISTERED` guards with
`include_original_model=True`, the later `register_models()` call (which uses
the default `include_original_model=False`) early-returned, so the
original-model set won permanently.
Remove the stray module-level call. The `if __name__ == "__main__"` block below
still registers with `include_original_model=True` for standalone use, so the
generator script is unaffected.
Co-Authored-By: Claude Opus 4.8 (1M context)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* test(registry): make import-side-effect test pass on CPU-only runners
The new test spawned a fresh `python -c "import unsloth.registry"` that did
not inherit tests/conftest.py's GPU-free harness, so on no-accelerator CI
runners the child raised NotImplementedError from unsloth_zoo.device_type
before printing REGISTRY_SIZE. With check=True this surfaced only as an
opaque CalledProcessError, turning the "Repo tests (CPU)" job red even
though the registry fix is correct.
Import this directory's conftest inside the child first so it applies the
same device_type stubs and torch.cuda probe patches. Also use check=False
and include the child stdout/stderr in the assertion message so a future
import regression is legible instead of an opaque non-zero exit.
* test(registry): assert register_models() leaks no upstream originals
Adds a fresh-interpreter test that register_models() registers only
unsloth-org models (deepseek still present via the normal path) and never
leaks the upstream deepseek-ai originals that the import-time guard poisoning
used to leak (129 -> 139). Factors the conftest-harness subprocess runner
into a shared helper reused by both registry import tests.
---------
Co-authored-by: Claude Opus 4.8 (1M context)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han
---
tests/test_model_registry.py | 85 +++++++++++++++++++++++++++++++++++
unsloth/registry/_deepseek.py | 2 -
2 files changed, 85 insertions(+), 2 deletions(-)
diff --git a/tests/test_model_registry.py b/tests/test_model_registry.py
index 283b099107..76f462c5cf 100644
--- a/tests/test_model_registry.py
+++ b/tests/test_model_registry.py
@@ -1,5 +1,8 @@
"""Register each model set and check the registered ids exist on the HF Hub."""
+import os
+import subprocess
+import sys
from dataclasses import dataclass
import pytest
@@ -77,3 +80,85 @@ def test_quant_type():
assert all(m.quant_type == QuantType.UNSLOTH for m in dynamic_quant_models)
quant_tag = QUANT_TAG_MAP[QuantType.UNSLOTH]
assert all(quant_tag in m.model_path for m in dynamic_quant_models)
+
+
+def _run_registry_child(body: str) -> subprocess.CompletedProcess:
+ """Run ``body`` in a fresh interpreter that first imports this directory's
+ ``conftest`` so it inherits the same GPU-free harness the pytest session
+ uses (device_type stubs plus torch.cuda probe patches). Without it,
+ ``import unsloth.registry`` raises ``NotImplementedError`` from
+ ``unsloth_zoo.device_type`` on no-accelerator CI runners, so the child
+ would exit non-zero and the test would fail even though the registry code
+ is correct. A fresh process also keeps each check independent of any
+ ``register_models()`` calls other tests make on the shared registry.
+ """
+ tests_dir = os.path.dirname(os.path.abspath(__file__))
+ prelude = (
+ f"import sys; sys.path.insert(0, {tests_dir!r})\n"
+ "try:\n"
+ " import conftest # noqa: F401 GPU-free harness on no-accelerator runners\n"
+ "except Exception:\n"
+ " pass\n"
+ )
+ return subprocess.run(
+ [sys.executable, "-c", prelude + body],
+ capture_output = True,
+ text = True,
+ check = False,
+ )
+
+
+def test_importing_registry_does_not_register_models():
+ """Importing the registry must not populate MODEL_REGISTRY on its own.
+
+ ``_deepseek`` used to call ``register_deepseek_models(...)`` at module
+ scope, so merely importing ``unsloth.registry`` registered models as an
+ import side effect, unlike every other family which only registers on
+ demand.
+ """
+ result = _run_registry_child(
+ "import unsloth.registry\n"
+ "from unsloth.registry.registry import MODEL_REGISTRY\n"
+ "print('REGISTRY_SIZE', len(MODEL_REGISTRY))"
+ )
+ assert result.returncode == 0, (
+ f"registry import subprocess exited {result.returncode}\n"
+ f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"
+ )
+ size_lines = [line for line in result.stdout.splitlines() if line.startswith("REGISTRY_SIZE")]
+ assert size_lines == ["REGISTRY_SIZE 0"], result.stdout + result.stderr
+
+
+def test_register_models_registers_no_upstream_originals():
+ """``register_models()`` must register each family's ``unsloth``-org models
+ and must NOT leak upstream vendor "original" models.
+
+ Before the fix, ``_deepseek``'s import-time
+ ``register_deepseek_models(include_original_model = True)`` set the
+ ``_IS_DEEPSEEK_*_REGISTERED`` guards, so the later default
+ ``register_models()`` early-returned for deepseek and its 10 ``deepseek-ai``
+ originals leaked permanently (129 -> 139). This asserts the whole registry
+ is ``unsloth``-org after ``register_models()`` while deepseek is still
+ registered via the normal path. Runs in a fresh interpreter so it is
+ independent of other tests' registry mutations.
+ """
+ result = _run_registry_child(
+ "import unsloth.registry\n"
+ "from unsloth.registry import register_models\n"
+ "from unsloth.registry.registry import MODEL_REGISTRY\n"
+ "register_models()\n"
+ "orgs = sorted({m.org for m in MODEL_REGISTRY.values()})\n"
+ "deepseek = [k for k in MODEL_REGISTRY if 'deepseek' in k.lower()]\n"
+ "print('ORGS', orgs)\n"
+ "print('NUM_DEEPSEEK', len(deepseek))"
+ )
+ assert result.returncode == 0, (
+ f"register_models subprocess exited {result.returncode}\n"
+ f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"
+ )
+ out = result.stdout
+ # Every registered model is unsloth-org: no upstream "original" leaked.
+ assert "ORGS ['unsloth']" in out, out + result.stderr
+ # Deepseek is still registered via the normal path, just without originals.
+ deepseek_lines = [line for line in out.splitlines() if line.startswith("NUM_DEEPSEEK")]
+ assert deepseek_lines and int(deepseek_lines[0].split()[1]) > 0, out + result.stderr
diff --git a/unsloth/registry/_deepseek.py b/unsloth/registry/_deepseek.py
index e29190f0f2..618453fb82 100644
--- a/unsloth/registry/_deepseek.py
+++ b/unsloth/registry/_deepseek.py
@@ -171,8 +171,6 @@ def _list_deepseek_r1_distill_models():
return distill_models
-register_deepseek_models(include_original_model = True)
-
if __name__ == "__main__":
from unsloth.registry.registry import MODEL_REGISTRY, _check_model_info
From e8db1cecff48cfda2a3376f5d85b0a10a1170416 Mon Sep 17 00:00:00 2001
From: Hakan Baysal
Date: Sun, 19 Jul 2026 13:45:29 +0300
Subject: [PATCH 030/271] studio: show the active run's saved config in the
Training Progress popover (#7217)
* studio: show the active run's saved config in the Training Progress popover
The Training Config popover on the live Training Progress page read the
editable form store (useTrainingConfigStore), so it showed stale/static values
whenever the form changed after the run started; only the History view read the
run's saved config snapshot, which is why re-opening the same run from Recents
showed the correct values (#6853).
Wire the live view to the same authoritative source History already uses:
- Extract History's field mapping into sections/run-config-override.ts
(mapRunConfigToOverride) so both views share one mapper over
GET /api/train/runs/{id} config.
- LiveTrainingView fetches the run record as soon as the job id is known and
passes the mapped override to ProgressSection; the fetched config is keyed
by job id, and until it loads (or if the fetch fails) the form store remains
the fallback. The run record is created at job start, so it is available
while the run is live.
- ProgressSection prefers configOverride whenever one is present instead of
only when isHistorical, so the live override takes effect.
Adds a source-level regression test pinning the wiring and the mapper's
backend config keys.
Fixes #6853
* studio: retry the run-config fetch after the first step, carry the saved method
Two review fixes on the live Training Config popover source:
1. The backend creates the run row only on the first progress event, so the
fetch issued as soon as the job id appeared commonly 404'd during
model/dataset preparation and never retried -- leaving the popover on the
form store for the whole run. The effect is now also keyed on
firstStepReceived (and skips once resolved for the job), so it re-fetches
exactly when the row is guaranteed to exist.
2. The popover's method label and LoRA-row visibility came from
viewData.trainingMethod, still read from the editable form store; changing
the form (e.g. LoRA -> Full) after starting a run relabeled it and hid its
saved LoRA rows. The run-config mapper now derives trainingMethod from the
snapshot's training_type/load_in_4bit (via parseBackendTrainingMethod, now
exported from the feature index) and the live view prefers it.
* studio: fetch the run config on a terminal phase too, not just the first step
The live config-popover fetch was keyed on firstStepReceived, which the runtime
store sets only when step > 0. A run that fails or completes during preparation
(before step 1) creates and finalizes its row from the terminal error/complete
event, but neither the job id nor firstStepReceived changed, so the fetch never
ran and the popover stayed on the editable form store -- showing the wrong
config/method if the form was edited afterward (Configure re-enables on failure).
Gate the fetch on a runRowReady signal = firstStepReceived OR a terminal phase
(completed/error/stopped), the states in which the backend guarantees the row
exists. This also stops the earlier fetch-then-404 churn during preparation and
lets the effect depend only on values it reads (no lint suppression needed).
* studio: retry the run-config lookup and accept a hydrated step as row-ready
Two ways the popover could stay stuck on the editable form store for a whole
run:
- The backend publishes the progress event that reveals the run before
create_run commits, so the first lookup can lose that race and 404. The catch
changed neither runRowReady nor fetchedRunConfig, leaving every effect
dependency identical, so no further attempt was ever made for that job. The
failure path now schedules an explicit retry, bounded and keyed by job id, so
a genuinely absent row falls back to the form store instead of polling.
- A run recovered through status/metrics polling (SSE unavailable or blocked)
has currentStep restored by applyStatus/applyMetrics but never
firstStepReceived, and the phase stays training, so the row was treated as
not ready even at step > 0. currentStep > 0 is now a readiness signal of its
own.
* studio: fetch the saved run config as soon as the job id exists
start_training() inserts the run row before the pump can consume any event --
deliberately, so the run appears in history during model loading -- and /status
exposes the job id throughout the pre-step phases. Gating the lookup on a first
step or a terminal phase therefore held the popover on the editable form store
for the whole configuring/loading/downloading window, which on a long model or
dataset load is minutes, and indefinitely for a run adopted from another client.
The job id is now the entire readiness condition; the existing bounded retry
still covers the instant before the insert commits.
* Fix Training Config popover fallback for history runs without a saved config; tighten popover comments
---------
Co-authored-by: danielhanchen
---
.../test_training_config_popover_source.py | 109 ++++++++++++++++++
.../studio/historical-training-view.tsx | 21 +---
.../features/studio/live-training-view.tsx | 91 ++++++++++++++-
.../studio/sections/progress-section.tsx | 40 +++----
.../studio/sections/run-config-override.ts | 54 +++++++++
.../frontend/src/features/training/index.ts | 1 +
6 files changed, 270 insertions(+), 46 deletions(-)
create mode 100644 studio/backend/tests/test_training_config_popover_source.py
create mode 100644 studio/frontend/src/features/studio/sections/run-config-override.ts
diff --git a/studio/backend/tests/test_training_config_popover_source.py b/studio/backend/tests/test_training_config_popover_source.py
new file mode 100644
index 0000000000..4263b012eb
--- /dev/null
+++ b/studio/backend/tests/test_training_config_popover_source.py
@@ -0,0 +1,109 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Source-level regression guards for the Training Config popover data source
+(#6853).
+
+The live Training Progress popover used to read the editable form store
+(useTrainingConfigStore) while a run was active, so it showed stale/static
+values whenever the user touched the form after starting the run; only the
+History view read the run's saved config snapshot. These guards pin the fixed
+wiring: both views feed ProgressSection a config override mapped from
+GET /api/train/runs/{id}, and ProgressSection prefers that override whenever
+one is present -- not only for historical views.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+_STUDIO_FRONTEND = Path(__file__).resolve().parents[2] / "frontend" / "src" / "features" / "studio"
+
+
+def _read(rel: str) -> str:
+ return (_STUDIO_FRONTEND / rel).read_text(encoding = "utf-8")
+
+
+def test_progress_section_prefers_override_over_form_store():
+ src = _read("sections/progress-section.tsx")
+ # Fields key on the override's presence, not isHistorical: a live view passing
+ # an override wins over the store; without one, live keeps the store while
+ # History shows blanks rather than unrelated live form values.
+ assert "const cfg = configOverride ?? (isHistorical ? undefined : config)" in src
+ assert "const cfgEpochs = cfg?.epochs" in src
+ assert "isHistorical ? configOverride?.epochs" not in src
+
+
+def test_live_view_fetches_the_active_run_config():
+ src = _read("live-training-view.tsx")
+ # Live view resolves the run's saved config snapshot by job id...
+ assert "getTrainingRun(" in src
+ assert "mapRunConfigToOverride(" in src
+ # ...and hands it to the popover.
+ assert "configOverride={runConfigOverride}" in src
+
+
+def test_live_view_fetches_as_soon_as_the_job_id_exists():
+ # start_training() inserts the run row BEFORE the pump consumes any event, so
+ # the saved config is available during configuring/loading/downloading. The
+ # job id is therefore the whole readiness condition: gating on a first step
+ # or a terminal phase would show the wrong config for the entire pre-step
+ # window of a long load, or for a run adopted from another client.
+ src = _read("live-training-view.tsx")
+ assert "if (!runtime.jobId) {" in src
+ assert "[runtime.jobId, fetchedRunConfig, fetchAttempt]" in src
+ # No step/phase readiness gate may creep back in.
+ assert "runRowReady" not in src
+
+
+def test_live_view_retries_the_transient_row_miss():
+ # start_training() creates the row before the pump, but a lookup racing that
+ # commit can still 404. Nothing else in the effect deps changes on failure, so
+ # the retry must be explicit and bounded, else a genuinely absent row would
+ # poll forever instead of falling back to the form store.
+ src = _read("live-training-view.tsx")
+ assert "RUN_CONFIG_FETCH_RETRIES" in src
+ assert "RUN_CONFIG_FETCH_RETRY_MS" in src
+ assert "setFetchAttempt(" in src
+ assert "attempts >= RUN_CONFIG_FETCH_RETRIES" in src
+ # The budget is keyed by job so a new run always starts fresh.
+ assert "fetchAttempt?.jobId === jobId ? fetchAttempt.count : 0" in src
+ # The pending retry must be cancelled with the effect.
+ assert "clearTimeout(retryTimer)" in src
+
+
+def test_live_view_prefers_saved_training_method():
+ # The method label / LoRA-row visibility must come from the run snapshot,
+ # not the editable form (which may have changed since the run started).
+ src = _read("live-training-view.tsx")
+ assert "runConfigOverride?.trainingMethod ?? config.trainingMethod" in src
+
+
+def test_history_view_uses_the_shared_mapper():
+ src = _read("historical-training-view.tsx")
+ # Shared mapper, not a re-inlined field-by-field copy that could drift.
+ assert "mapRunConfigToOverride(detail.config)" in src
+ assert "num_epochs" not in src
+
+
+def test_shared_mapper_matches_backend_config_keys():
+ src = _read("sections/run-config-override.ts")
+ # The mapper reads the run config JSON the backend snapshots at job start;
+ # keep the key set pinned so a silent rename breaks loudly here.
+ for key in (
+ "training_type",
+ "load_in_4bit",
+ "num_epochs",
+ "batch_size",
+ "learning_rate",
+ "max_steps",
+ "max_seq_length",
+ "warmup_steps",
+ "optim",
+ "lora_r",
+ "lora_alpha",
+ "lora_dropout",
+ "use_rslora",
+ "use_loftq",
+ ):
+ assert key in src, f"run-config mapper lost backend key {key}"
diff --git a/studio/frontend/src/features/studio/historical-training-view.tsx b/studio/frontend/src/features/studio/historical-training-view.tsx
index 2f80fc29ca..b6ec06b06a 100644
--- a/studio/frontend/src/features/studio/historical-training-view.tsx
+++ b/studio/frontend/src/features/studio/historical-training-view.tsx
@@ -8,6 +8,7 @@ import { parseBackendTrainingMethod } from "@/features/training/lib/training-met
import { type ReactElement, useEffect, useState } from "react";
import { ChartsSection } from "./sections/charts-section";
import { ProgressSection } from "./sections/progress-section";
+import { mapRunConfigToOverride } from "./sections/run-config-override";
import { translate, useT } from "@/i18n";
type StudioT = ReturnType;
@@ -147,25 +148,7 @@ export function HistoricalTrainingView({
}
const viewData = mapToViewData(detail, t);
- const configOverride = detail.config
- ? {
- epochs: detail.config.num_epochs as number | undefined,
- batchSize: detail.config.batch_size as number | undefined,
- learningRate: detail.config.learning_rate as string | undefined,
- maxSteps: detail.config.max_steps as number | undefined,
- contextLength: detail.config.max_seq_length as number | undefined,
- warmupSteps: detail.config.warmup_steps as number | undefined,
- optimizerType: detail.config.optim as string | undefined,
- loraRank: detail.config.lora_r as number | undefined,
- loraAlpha: detail.config.lora_alpha as number | undefined,
- loraDropout: detail.config.lora_dropout as number | undefined,
- loraVariant: detail.config.use_rslora
- ? "rslora"
- : detail.config.use_loftq
- ? "loftq"
- : "lora",
- }
- : undefined;
+ const configOverride = mapRunConfigToOverride(detail.config);
return (
diff --git a/studio/frontend/src/features/studio/live-training-view.tsx b/studio/frontend/src/features/studio/live-training-view.tsx
index cce39adbf4..0aecc7030e 100644
--- a/studio/frontend/src/features/studio/live-training-view.tsx
+++ b/studio/frontend/src/features/studio/live-training-view.tsx
@@ -1,18 +1,42 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-import { cn } from "@/lib/utils";
import {
+ getTrainingRun,
useTrainingConfigStore,
useTrainingRuntimeStore,
} from "@/features/training";
import type { TrainingViewData } from "@/features/training";
+import { cn } from "@/lib/utils";
import type { ReactElement } from "react";
+import { useEffect, useState } from "react";
import { useShallow } from "zustand/react/shallow";
import { ChartsSection } from "./sections/charts-section";
import { ProgressSection } from "./sections/progress-section";
+import {
+ type RunConfigOverride,
+ mapRunConfigToOverride,
+} from "./sections/run-config-override";
import { TrainingStartOverlay } from "./training-start-overlay";
+/** Retry budget for the run-config lookup. The row is inserted at
+ * start_training(), but a lookup issued in the same instant can still miss it;
+ * a few short retries cover that without polling a genuinely absent row. */
+const RUN_CONFIG_FETCH_RETRIES = 5;
+const RUN_CONFIG_FETCH_RETRY_MS = 1000;
+
+/** The fetched run config only applies while it belongs to the active job;
+ * a stale record from a previous run falls back to the form store. */
+function activeRunOverride(
+ fetched: { jobId: string; override: RunConfigOverride | undefined } | null,
+ jobId: string | null,
+): RunConfigOverride | undefined {
+ if (fetched === null || fetched.jobId !== jobId) {
+ return undefined;
+ }
+ return fetched.override;
+}
+
export function LiveTrainingView(): ReactElement {
const runtime = useTrainingRuntimeStore(
useShallow((state) => ({
@@ -52,6 +76,59 @@ export function LiveTrainingView(): ReactElement {
})),
);
+ // Show the ACTIVE run's saved config, not the editable form store the user may
+ // have changed since starting (#6853). start_training() commits the run row
+ // before the pump, so the job id alone gates the fetch; the bounded retry below
+ // covers the narrow uncommitted window, and until it loads ProgressSection falls
+ // back to the form store. The result is keyed by job id and filtered at render.
+ const [fetchedRunConfig, setFetchedRunConfig] = useState<{
+ jobId: string;
+ override: RunConfigOverride | undefined;
+ } | null>(null);
+ // Retry budget for the transient 404 below, keyed by job so a new run always
+ // starts with a fresh budget.
+ const [fetchAttempt, setFetchAttempt] = useState<{
+ jobId: string;
+ count: number;
+ } | null>(null);
+ useEffect(() => {
+ if (!runtime.jobId) {
+ return;
+ }
+ const jobId = runtime.jobId;
+ if (fetchedRunConfig !== null && fetchedRunConfig.jobId === jobId) {
+ return; // already resolved for this job
+ }
+ const attempts = fetchAttempt?.jobId === jobId ? fetchAttempt.count : 0;
+ const controller = new AbortController();
+ let retryTimer: ReturnType
| undefined;
+ getTrainingRun(jobId, controller.signal)
+ .then((detail) => {
+ setFetchedRunConfig({
+ jobId,
+ override: mapRunConfigToOverride(detail.config),
+ });
+ })
+ .catch(() => {
+ // A lookup racing the row commit can miss transiently; nothing else in
+ // the deps changes on failure, so retry explicitly. Bounded so a genuinely
+ // absent row falls back to the form store instead of polling forever.
+ if (controller.signal.aborted || attempts >= RUN_CONFIG_FETCH_RETRIES) {
+ return;
+ }
+ retryTimer = setTimeout(() => {
+ setFetchAttempt({ jobId, count: attempts + 1 });
+ }, RUN_CONFIG_FETCH_RETRY_MS);
+ });
+ return () => {
+ controller.abort();
+ if (retryTimer !== undefined) {
+ clearTimeout(retryTimer);
+ }
+ };
+ }, [runtime.jobId, fetchedRunConfig, fetchAttempt]);
+ const runConfigOverride = activeRunOverride(fetchedRunConfig, runtime.jobId);
+
const activeProjectName =
runtime.startProjectName !== null
? runtime.startProjectName.trim() || null
@@ -76,7 +153,11 @@ export function LiveTrainingView(): ReactElement {
isTrainingRunning: runtime.isTrainingRunning,
modelName: runtime.startModelName ?? config.selectedModel ?? "",
projectName: activeProjectName,
- trainingMethod: config.trainingMethod ?? "",
+ // Prefer the saved run's method: the form may have been edited (e.g. LoRA
+ // -> Full) after the run started, which would relabel the run and hide its
+ // saved LoRA rows in the popover.
+ trainingMethod:
+ runConfigOverride?.trainingMethod ?? config.trainingMethod ?? "",
lossHistory: runtime.lossHistory,
lrHistory: runtime.lrHistory,
gradNormHistory: runtime.gradNormHistory,
@@ -105,7 +186,11 @@ export function LiveTrainingView(): ReactElement {
)}
>
o.value === cfgOptimizerType)?.label ??
diff --git a/studio/frontend/src/features/studio/sections/run-config-override.ts b/studio/frontend/src/features/studio/sections/run-config-override.ts
new file mode 100644
index 0000000000..a1272bfeb0
--- /dev/null
+++ b/studio/frontend/src/features/studio/sections/run-config-override.ts
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import { parseBackendTrainingMethod } from "@/features/training";
+
+/** Shape of the Training Config popover's data when it is driven by a saved
+ * run snapshot instead of the editable form store. */
+export interface RunConfigOverride {
+ trainingMethod?: string;
+ epochs?: number;
+ batchSize?: number;
+ learningRate?: string;
+ maxSteps?: number;
+ contextLength?: number;
+ warmupSteps?: number;
+ optimizerType?: string;
+ loraRank?: number;
+ loraAlpha?: number;
+ loraDropout?: number;
+ loraVariant?: string;
+}
+
+/** Map a saved run's config (GET /api/train/runs/{id} `detail.config`) into the
+ * Training Config popover's override shape. Shared by the History view and the
+ * live Current Run view so both read the same authoritative run snapshot
+ * instead of the editable form store (#6853). */
+export function mapRunConfigToOverride(
+ config: Record | null | undefined,
+): RunConfigOverride | undefined {
+ if (!config) {
+ return undefined;
+ }
+ return {
+ trainingMethod: parseBackendTrainingMethod(
+ config.training_type,
+ config.load_in_4bit,
+ ),
+ epochs: config.num_epochs as number | undefined,
+ batchSize: config.batch_size as number | undefined,
+ learningRate: config.learning_rate as string | undefined,
+ maxSteps: config.max_steps as number | undefined,
+ contextLength: config.max_seq_length as number | undefined,
+ warmupSteps: config.warmup_steps as number | undefined,
+ optimizerType: config.optim as string | undefined,
+ loraRank: config.lora_r as number | undefined,
+ loraAlpha: config.lora_alpha as number | undefined,
+ loraDropout: config.lora_dropout as number | undefined,
+ loraVariant: config.use_rslora
+ ? "rslora"
+ : config.use_loftq
+ ? "loftq"
+ : "lora",
+ };
+}
diff --git a/studio/frontend/src/features/training/index.ts b/studio/frontend/src/features/training/index.ts
index 553dcc2af5..a0d249ff1b 100644
--- a/studio/frontend/src/features/training/index.ts
+++ b/studio/frontend/src/features/training/index.ts
@@ -12,6 +12,7 @@ export {
getTrainingRunDisplayTitle,
getTrainingRunModelSubtitle,
} from "./lib/run-display";
+export { parseBackendTrainingMethod } from "./lib/training-methods";
export { useTrainingHistorySidebarItems } from "./hooks/use-training-history-sidebar";
export { useTrainingRuntimeLifecycle } from "./hooks/use-training-runtime-lifecycle";
export { useTrainingCompletionWatch } from "./hooks/use-training-completion-watch";
From ecd97a935a2c71f918b93653e36b5320fd8aa872 Mon Sep 17 00:00:00 2001
From: Daniel Han
Date: Sun, 19 Jul 2026 04:54:17 -0700
Subject: [PATCH 031/271] test(version-compat): keep GRPO fake-run logits
finite on CPU (#7247)
* test(version-compat): keep GRPO fake-run logits finite on CPU
The GRPO fake-run test samples completions from a tiny untrained model on
CPU. Such a model can emit non-finite logits, so torch.multinomial inside
generate() intermittently raises "probability tensor contains either inf,
nan or element < 0" -- a nondeterministic sampling failure, not a regression
(the Trainer already fixes the seed, but CPU reduction order is not
bit-reproducible). Add a forward hook that sanitizes the LM head logits to a
finite bounded range before sampling, so the fake run reliably exercises the
whole train loop; the test checks the loop runs, not the numerics.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* test(version-compat): drop redundant nan_to_num bounds (clamp handles them)
* test(version-compat): scope GRPO finite-logits guard to the GRPO test
Only test_grpo_trains_on_cpu autoregressively samples completions, so it is
the only canary that can hit the non-finite-logits torch.multinomial crash.
Move the _guard_finite_logits hook out of the shared _load_plain() and into
test_grpo_trains_on_cpu so the SFT and DPO canaries keep asserting against the
model's true, unclamped logits.
---------
Co-authored-by: Daniel Han
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
---
.../version_compat/test_trl_fake_train_cpu.py | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/tests/version_compat/test_trl_fake_train_cpu.py b/tests/version_compat/test_trl_fake_train_cpu.py
index 4dae696282..2f7b469428 100644
--- a/tests/version_compat/test_trl_fake_train_cpu.py
+++ b/tests/version_compat/test_trl_fake_train_cpu.py
@@ -158,6 +158,37 @@ except Exception:
_MODEL = "hf-internal-testing/tiny-random-LlamaForCausalLM"
+def _guard_finite_logits(model):
+ """Keep the LM head logits finite so GRPO sampling can't crash.
+
+ ``test_grpo_trains_on_cpu`` samples completions from a tiny, *untrained*
+ random model on CPU. Driven autoregressively -- and nudged by the fake
+ reward's optimizer step between the two train steps -- such a model can emit
+ non-finite logits, so ``torch.multinomial`` inside ``generate()``
+ intermittently raises "probability tensor contains either `inf`, `nan` or
+ element < 0". That is a well-known nondeterministic sampling failure, not an
+ Unsloth/TRL regression: the Trainer already fixes the seed, but CPU reduction
+ order is not bit-reproducible, so the blow-up still surfaces every so often.
+
+ Sanitize the logits to a finite, bounded range (out of place, so autograd
+ stays valid) before they reach the sampler. This test asserts the train loop
+ runs end to end, not the (deliberately meaningless) numerics, so bounding the
+ logits changes nothing it checks while making the run reliable.
+ """
+
+ def _finite_logits_hook(_module, _inputs, output):
+ logits = getattr(output, "logits", None)
+ if logits is None:
+ return output
+ # nan_to_num maps nan -> 0 and the infinities to large finite values;
+ # clamp then bounds everything to [-30, 30].
+ output.logits = torch.nan_to_num(logits).clamp(-30.0, 30.0)
+ return output
+
+ model.register_forward_hook(_finite_logits_hook)
+ return model
+
+
def _load_plain():
"""Tiny plain HF model + tokenizer on CPU. Skips (not fails) if the model
cannot be fetched -- that is a network/hub issue, not an unsloth regression."""
@@ -233,6 +264,11 @@ def test_grpo_trains_on_cpu(tmp_path):
assert GRPOTrainer.__name__ == "UnslothGRPOTrainer", "GRPO patch did not apply"
model, tok = _load_plain()
+ # GRPO is the only canary that autoregressively samples completions, so it is
+ # the only one that can hit the non-finite-logits multinomial crash. Install
+ # the guard here (not in _load_plain) so the SFT/DPO canaries keep asserting
+ # against the model's true, unclamped outputs.
+ _guard_finite_logits(model)
ds = Dataset.from_list([{"prompt": "hi there"}] * 4)
cfg = GRPOConfig(
output_dir = str(tmp_path / "ci_grpo"),
From 5f1f30ec82d097d92d1093d1d557427dcbf079e6 Mon Sep 17 00:00:00 2001
From: oobabooga
Date: Sun, 19 Jul 2026 09:46:22 -0300
Subject: [PATCH 032/271] Studio: GPU memory configuration for GGUF models
(#6414)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* Studio: GPU memory dropdown — llama.cpp --fit on and manual gpu-layers/cpu-moe
* Studio: simplify GPU memory changes (reuse ParamSlider, GPU_LAYERS_ALL, loadedGpuMemoryFields helper)
* Studio: GPU picker — choose which GPUs a GGUF model loads on (gpu_ids)
* Studio: simplify GPU picker (share /api/system fetch, validate gpu_ids)
* Studio: GPU picker review fixes (gate relative indices, no cross-model leak, validate, types)
* Studio: group GPU controls under a collapsible GPU section
* Studio: GPU feature review fixes (fix fit-ctx test, behavior-test the floor, comment accuracy)
* Studio: make GPU a top-level settings section (not nested under Model)
* Studio: flatten GPU controls into the Model section, group by GPU/context/generation
* Studio: move GPU Memory to the bottom of Model with its dependent controls beneath it
* Studio: move GPU Memory below Tensor Parallelism and GPUs below GPU Memory
* Studio: tighten GPU Memory and GPU Layers tooltip copy
* Studio: fix fit-mode context slider track-click, restore GPU Memory tooltip, shorten fit dropdown label
* Studio: GPU Memory tooltip one mode per line, briefer
* Studio: note HIP_VISIBLE_DEVICES (ROCm) in the GPUs picker tooltip
* Studio: narrow the GPU Memory dropdown to fit the shortened label
* Studio: use 'llama.cpp --fit' in the GPU Memory tooltip for consistency
* Studio: allow Tensor Parallelism in Manual GPU mode
* Studio: graduated MoE-on-CPU offload (--n-cpu-moe) replacing the all-or-nothing toggle
* Studio: size the MoE-offload slider for staged (deferred-load) models
* Studio: share one GGUF header walk for the context-length and MoE-count readers
* Studio: size the GPU Layers slider for staged models (one staged-header read)
* Studio: move Tensor Parallelism below the GPUs picker
* Studio: GPU split (--tensor-split) per-GPU model share in Manual mode
* Studio: tolerate whitespace in GPU split input, move it below GPU Layers
* Studio: rename the GPU split control to "Split ratio"
* Studio: Split ratio sends explicit even input; fix blank=free-VRAM (not even) copy
* Studio: tighten llama.cpp --fit VRAM margin with --fit-target 512
* Studio: GPU memory review fixes (rollback re-baseline, single-GPU TP gate, accurate copy)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: move Split ratio below MoE Layers on CPU
* Studio: address PR review (fix GPU-info hydration race, share fit context-length across load paths)
* Studio: address codex review (manual single-GPU TP guard, GPU-aware spec defaults in fit/manual, GGUF-only context/preference)
* Studio: address codex review round 2 (gpu_present seed, single-GPU tensor-split guard, staged manual-knob reset, strip inherited offload flags)
* Studio: address codex review round 3 (strip inherited --n-cpu-moe, CPU-fallback warning in Manual mode)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: address codex review round 4 (preserve pinned fit context across a later Apply)
* Studio: address codex review round 5 (honor GPU picker for diffusion GGUFs, clear fit pin on cross-model switch)
* Studio: preserve the pending GPU Memory mode when staging a model
* Studio: pin diffusion GPU device order and reset GPU-memory state for diffusion loads
* Studio: address codex review round 6 (fit-Auto rollback context, preserve manual non-tensor split modes, persist GPU mode on load not select)
* Studio: persist the applied GPU Memory mode, not the requested one (skip diffusion loads)
* Studio: replace Manual-mode split-ratio field with per-GPU layer sliders
* Studio: clarify per-GPU layer split hint for tensor-parallel mode
* Studio: address codex review round 7 (allow GGUF gpu_ids past the legacy guard, replay GPU-memory fields on respawn)
* Studio: address codex review round 8 (size the validate preflight like the load in fit mode, across both load paths)
* Studio: skip the training-OOM guard for llama.cpp --fit GGUF loads (they spill to RAM)
* Studio: drop the now-redundant compare-path validate sizing (the --fit guard skip makes it moot)
* Studio: address codex review round 9 (keep the training guard for fit loads, forward gpu_ids to validate, strip inherited manual tensor-split)
* Studio: address codex review round 10 (gate GPU-memory adoption on is_gguf, record manual knobs only in Manual mode)
* Studio: handle diffusion GGUFs symmetrically in the GPU Memory controls (preserve the standing mode preference, hide the inapplicable mode/TP controls)
* Studio: remember the GPU Memory settings per model
* Studio: consolidate --fit mode and Manual mode into a single Manual mode
* Studio: preserve the per-GPU layer split across GPU Layers changes
* Studio: trim overly long GPU Memory comments
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* address GPU memory config review comments
* trim redundant GPU memory tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Reconcile manual-mode TP drops with the #6659 drop-site invariants
* Preserve quantized KV in manual --fit, charge GGUF companions in full, reconcile GPU pick on load
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Clear stale GPU baseline on non-GGUF loads so it can't read as dirty
* Fix no-context-shift test for the conditional -c flag
* Credit manual GPU-layer offload for cached HF GGUFs
* Reset per-model load knobs on GGUF quant switch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Strip inherited tensor-split when manual ratio is cleared
* Match auto-load validation to safetensors placement
* Reset editable manual knobs after Auto GGUF loads
* Record a single device for diffusion GPU picks
* Reset per-model GPU knobs before applying saved settings
* Address review comments
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Guard manual tensor splits and keep remembered context on auto-load
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Snapshot compare knobs, seed splits from free VRAM, flag zero-offload loads
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Exempt CPU-only loads from the guard floor and harden compare and reseed paths
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Reach full offload from the layers slider and charge extras drafters in the guard
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Warm the GPU device cache before pick reconciles and disable staged GPU controls
* Align the training guard with inherited extras, spec mode, and compare targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Hide GPUs from companion-less zero-offload loads
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Size diffusion picks per device, own manual offload flags, reject XPU picks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Drop tensor flags at zero layers and exempt CPU-pinned drafters
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Allowlist the zero-layer tensor parallel drop site
* Keep validate and load guards on the same extras and refresh stale baselines
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Drop mismatched manual tensor splits before launch
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Gate XPU picks on the real backend field and harden split and hydration paths
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Weight full GPUs as zero, clamp split shares, and refine the zero-layer mask gate
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Carry fit context across mode changes and align drafter and picker gates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Catch variant switches, uncached diffusion repos, and text-only mmproj skips
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Check companions on the first device and size native and remote zero-layer loads
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Replace the training guard's precise VRAM modeling with a conservative bound
* Baseline context pins on non-GGUF hydration and reprobe list-seeded staged GGUFs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Size manual splits by their largest share and preserve resolved context from Default
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Default-deny unsized required companions and price KV at the effective cache dtype
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Reserve MTP draft KV and MLA target-copy in the training guard
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Size tensor-parallel loads per device and show GPU controls for native GGUFs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Reserve MTP overhead for uncached remote GGUFs and the mmproj runtime factor
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Drop the training-coexistence VRAM estimation this PR added
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Gate remembered load settings to GGUF picks
* Lock the remaining load-time controls during a staged load
* Clear the stale native-path token on compare loads
* Drop a stale guard reference from the zero-offload masking comment
* Seed GPU baselines from the rollback response and drop never-emitted offload flags
* Match validate's training guard to load and keep the native reload token
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Trim verbose GPU-memory comments
* Thread the variants header walk off the event loop, honor device pins on zero-offload, and hold staged GPU edits
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Honor manual placement and classify pinned zero-offload loads
* Close diffusion admission and status hydration gaps
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Check the actual diffusion GPU during training
* Align staged baselines and manual reload dedupe
* Fix GGUF placement and rollback state
* Harden manual GGUF placement boundaries
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove unused resolve_tensor_parallel import in llama_cpp.py
The name is used only in llama_server_args.py, routes/inference.py, and tests,
not in llama_cpp.py; the unused hoisted import trips the import-hoist verifier
in the source-lint CI job.
* Fix diffusion GPU dedup and training guard for non-numeric device tokens
The diffusion runner drives only its single lowest device and the backend
records that one device (self._gpu_ids = [sorted(gpu_ids)[0]]), but the reload
dedupe compared it against the full requested list, so a multi-GPU pick that
resolves to the same device forced a needless reload. Normalize the request the
same way for a loaded diffusion model in both _already_in_target_state and the
route _request_matches_loaded_settings.
The chat-during-training coexistence guard called int() on the single-device
token and hard-rejected when it could not parse. A non-numeric token (a CUDA
UUID / MIG handle) now sizes against the whole visible pool like the GGUF guard
instead of falsely blocking the load, and an empty token (a CPU-only runner such
as a CPU diffusion GGUF) is allowed outright since it uses no GPU VRAM.
* Tighten comments added by the GPU memory config changes
* Harden GGUF placement from independent review: VRAM sizing, diffusion TP reset, tensor_split validation
- Training coexistence guard: a single-device runner pinned through an
unresolvable UUID/MIG token was sized against the aggregate visible-VRAM pool,
so a load could pass on capacity it cannot use and then OOM active training.
Size against the worst-case visible device (min free) instead, keeping the
guard's documented default-deny contract. The empty-token (CPU-only runner)
allow path is unchanged.
- Diffusion startup: _start_diffusion_server now resets self._tensor_parallel to
False alongside the other placement resets. A prior tensor-parallel chat load
(process killed but not fully unload-reset) otherwise left /status misreporting
tensor parallelism and made an identical diffusion re-Apply reload against the
stale state.
- tensor_split: reject negative / non-finite / all-zero splits up front. They
were dropped at launch but still compared raw in the reload dedupe, so an
identical Apply reloaded indefinitely.
- Tests: the shared httpx stub was incomplete and, installed via setdefault
before real httpx loaded, broke a combined pytest run (collection errors on
httpx.Response). Import the real installed httpx instead.
* [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: danielhanchen
Co-authored-by: danielhanchen
---
studio/backend/core/inference/llama_cpp.py | 715 +++++++++++++-
.../core/inference/llama_server_args.py | 27 +-
studio/backend/main.py | 14 +
studio/backend/models/inference.py | 144 ++-
studio/backend/routes/inference.py | 518 ++++++++---
studio/backend/routes/models.py | 6 +-
studio/backend/routes/training_vram.py | 42 +-
.../tests/test_chat_load_during_training.py | 333 ++++++-
studio/backend/tests/test_gguf_metadata.py | 73 ++
studio/backend/tests/test_gpu_memory_mode.py | 879 ++++++++++++++++++
studio/backend/tests/test_gpu_selection.py | 18 +-
.../tests/test_llama_cpp_no_context_shift.py | 12 +-
.../tests/test_llama_cpp_props_readback.py | 31 +-
.../backend/tests/test_llama_server_args.py | 45 +
studio/backend/tests/test_tensor_parallel.py | 5 +-
.../tests/test_tp_vision_regression.py | 17 +-
studio/backend/utils/models/gguf_metadata.py | 109 ++-
.../remembered-load-settings.ts | 24 +-
.../src/features/chat/api/chat-adapter.ts | 128 ++-
.../src/features/chat/api/chat-api.ts | 38 +-
.../frontend/src/features/chat/chat-page.tsx | 13 +-
.../src/features/chat/chat-settings-sheet.tsx | 450 ++++++++-
.../chat/hooks/use-chat-model-runtime.ts | 217 ++++-
.../hooks/use-staged-model-preparation.ts | 46 +-
.../lib/apply-inference-status-to-store.ts | 117 ++-
.../features/chat/presets/preset-policy.ts | 31 +
.../src/features/chat/shared-composer.tsx | 109 ++-
.../chat/stores/chat-runtime-store.ts | 369 +++++++-
.../frontend/src/features/chat/types/api.ts | 38 +
studio/frontend/src/hooks/use-gpu-info.ts | 186 ++--
studio/frontend/src/hooks/use-system.ts | 3 +
31 files changed, 4356 insertions(+), 401 deletions(-)
create mode 100644 studio/backend/tests/test_gpu_memory_mode.py
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index c9ab7eb83b..d7c7eed518 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -11,6 +11,7 @@ import atexit
import contextlib
import functools
import json
+import math
import os
import re
import struct
@@ -23,11 +24,22 @@ import sys
import threading
import time
from pathlib import Path
-from typing import Callable, Collection, Generator, Iterable, List, Mapping, Optional, Union
+from typing import (
+ Callable,
+ Collection,
+ Generator,
+ Iterable,
+ List,
+ Literal,
+ Mapping,
+ Optional,
+ Union,
+)
import httpx
from core.inference.llama_server_args import (
+ _LAYER_OFFLOAD_FLAGS,
_effective_tensor_parallel,
_tensor_parallel_matches_loaded,
extra_args_disable_mmproj,
@@ -234,8 +246,7 @@ def _wsl_system_rocm_lib_dirs() -> "list[str]":
return out
-# Plan-without-action re-prompt state (intent signal, caps, message) now lives
-# in tool_call_parser, imported above under its old aliases.
+# Plan-without-action re-prompt state now lives in tool_call_parser (imported above).
# Default max_tokens to the effective context when known. The floor is high
# enough for reasoning-heavy GGUFs and max_tokens-omitting API clients.
@@ -1431,7 +1442,10 @@ def _extra_args_set_spec_type(extra_args: Optional[Iterable[str]]) -> bool:
return _extra_args_set_any_flag(extra_args, {"--spec-type", "--spec-default"})
-_GPU_OFFLOAD_OVERRIDE_FLAGS = frozenset({"-ngl", "--gpu-layers", "--n-gpu-layers", "-fit", "--fit"})
+# Layer-offload override detection. Single-sourced from llama_server_args, which
+# also strips these (plus the MoE flags) from inherited extras; sharing the layer
+# set keeps detection and stripping from drifting.
+_GPU_OFFLOAD_OVERRIDE_FLAGS = _LAYER_OFFLOAD_FLAGS
_THREAD_OVERRIDE_FLAGS = frozenset({"-t", "--threads"})
@@ -1895,6 +1909,17 @@ class LlamaCppBackend:
self._cache_type_kv: Optional[str] = None
# Whether --split-mode tensor was applied on the active load.
self._tensor_parallel: bool = False
+ # GPU memory strategy applied on the active load ("auto"/"manual").
+ self._gpu_memory_mode: str = "auto"
+ # Manual-mode load options (echoed back so the UI round-trips them).
+ self._gpu_layers: int = -1
+ # MoE expert layers to keep on CPU (--n-cpu-moe); 0 = none.
+ self._n_cpu_moe: int = 0
+ # Relative model share per GPU (--tensor-split), in GPU order; None =
+ # default (llama.cpp splits by free VRAM).
+ self._tensor_split: Optional[List[float]] = None
+ # User-picked physical GPU indices (None = automatic selection).
+ self._gpu_ids: Optional[List[int]] = None
# Layer load kept multi-GPU only to honor a downgraded tensor request, so a
# later explicit tensor-off reloads instead of deduping to it (#6659).
self._layer_preserves_tensor_intent: bool = False
@@ -1909,6 +1934,11 @@ class LlamaCppBackend:
self._spec_draft_n_max: Optional[int] = None
# KV-cache estimation fields (populated by _read_gguf_metadata)
self._n_layers: Optional[int] = None
+ # MoE metadata (populated by _read_gguf_metadata): expert count (>0 =
+ # MoE) and leading dense-layer count (offsets --n-cpu-moe, which counts
+ # from layer 0). See the n_moe_layers property.
+ self._n_experts: Optional[int] = None
+ self._leading_dense_block_count: Optional[int] = None
self._n_kv_heads: Optional[int] = None
self._n_kv_heads_by_layer: Optional[list[int]] = None
self._n_heads: Optional[int] = None
@@ -2329,6 +2359,79 @@ class LlamaCppBackend:
"""Whether --split-mode tensor is active on the loaded server."""
return self._tensor_parallel
+ @property
+ def gpu_memory_mode(self) -> str:
+ """Active GPU memory strategy: 'auto' or 'manual' (gpu_layers < 0 = Auto/--fit, >= 0 = pinned)."""
+ return self._gpu_memory_mode
+
+ @property
+ def gpu_layers(self) -> int:
+ """Requested --gpu-layers for manual mode (-1 when not manual)."""
+ return self._gpu_layers
+
+ @property
+ def n_cpu_moe(self) -> int:
+ """MoE expert layers manual mode kept on CPU (--n-cpu-moe); 0 = none."""
+ return self._n_cpu_moe
+
+ @property
+ def tensor_split(self) -> Optional[List[float]]:
+ """Manual-mode relative model share per GPU (--tensor-split); None =
+ default (split by free VRAM)."""
+ return self._tensor_split
+
+ @property
+ def gpu_ids(self) -> Optional[List[int]]:
+ """User-picked physical GPU indices, or None for automatic selection."""
+ return self._gpu_ids
+
+ @property
+ def n_layers(self) -> Optional[int]:
+ """Model layer count (GGUF block_count), or None if unknown."""
+ return self._n_layers
+
+ @property
+ def n_moe_layers(self) -> int:
+ """Number of MoE expert layers (the --n-cpu-moe ceiling), 0 if not MoE.
+
+ block_count minus the leading dense layers (which carry no experts):
+ --n-cpu-moe counts from layer 0, so those dense layers are no-ops.
+ """
+ if not self._n_experts or not self._n_layers:
+ return 0
+ return max(0, self._n_layers - (self._leading_dense_block_count or 0))
+
+ @staticmethod
+ def _resolve_cpu_moe_flag(
+ n_cpu_moe: int, n_moe_layers: int, leading_dense: int
+ ) -> Optional[int]:
+ """The --n-cpu-moe value (absolute first-N layers), or None to omit it.
+
+ Clamps the requested count to the model's MoE layers, then offsets past
+ the leading dense layers (--n-cpu-moe counts from layer 0). Returns None
+ for nothing-to-offload (0 requested) or a non-MoE model.
+ """
+ if n_cpu_moe <= 0 or n_moe_layers <= 0:
+ return None
+ return leading_dense + min(n_cpu_moe, n_moe_layers)
+
+ @staticmethod
+ def _sanitize_tensor_split(tensor_split: Optional[List[float]]) -> List[float]:
+ """Per-GPU shares with negative and non-finite entries clamped to 0.
+
+ A direct caller's negative entry would launch a placement different
+ from the ratio the UI showed, and inf would pass a plain ``> 0`` total
+ gate and emit ``--tensor-split inf,...``. Returns [] for input that
+ can't be read as floats (the length gate at the call site then drops
+ the split).
+ """
+ try:
+ return [
+ x if math.isfinite(x) and x > 0.0 else 0.0 for x in (float(v) for v in tensor_split)
+ ]
+ except (TypeError, ValueError, OverflowError):
+ return []
+
@property
def layer_preserves_tensor_intent(self) -> bool:
"""True when a downgraded tensor request kept this layer load multi-GPU."""
@@ -2530,6 +2633,7 @@ class LlamaCppBackend:
"spec_draft_n_max_flag": None,
"supports_kv_unified": False,
"supports_fit_ctx": False,
+ "supports_fit_target": False,
"supports_cache_ram": False,
"supports_ctx_checkpoints": False,
"supports_no_cache_prompt": False,
@@ -2549,6 +2653,7 @@ class LlamaCppBackend:
spec_draft_n_max_flag: Optional[str] = None
supports_kv_unified = False
supports_fit_ctx = False
+ supports_fit_target = False
supports_cache_ram = False
supports_ctx_checkpoints = False
supports_no_cache_prompt = False
@@ -2646,6 +2751,7 @@ class LlamaCppBackend:
supports_kv_unified = _is_real("--kv-unified")
supports_fit_ctx = _is_real("--fit-ctx")
+ supports_fit_target = _is_real("--fit-target")
supports_cache_ram = _is_real("--cache-ram")
supports_ctx_checkpoints = _is_real("--ctx-checkpoints")
supports_no_cache_prompt = _is_real("--no-cache-prompt")
@@ -2662,6 +2768,7 @@ class LlamaCppBackend:
"spec_draft_n_max_flag": spec_draft_n_max_flag,
"supports_kv_unified": supports_kv_unified,
"supports_fit_ctx": supports_fit_ctx,
+ "supports_fit_target": supports_fit_target,
"supports_cache_ram": supports_cache_ram,
"supports_ctx_checkpoints": supports_ctx_checkpoints,
"supports_no_cache_prompt": supports_no_cache_prompt,
@@ -2746,6 +2853,57 @@ class LlamaCppBackend:
except ValueError:
return None
+ @staticmethod
+ def _emit_child_gpu_visibility(env: dict, pinned: str) -> None:
+ """Write the child's GPU visibility mask (CUDA, plus the HIP mirror on
+ ROCm, where narrowing only CUDA_VISIBLE_DEVICES leaves an AMD child
+ seeing the full set). Do NOT also set ROCR_VISIBLE_DEVICES: ROCR and HIP
+ mask at different layers, so the same indices apply twice -- ROCR reduces
+ and re-indexes from 0, then a non-zero HIP pin points out of range, HIP
+ enumerates 0 devices, and llama.cpp falls back to CPU. The HIP mask alone
+ narrows correctly; clear any inherited ROCR mask so it can't double up."""
+ env["CUDA_VISIBLE_DEVICES"] = pinned
+ try:
+ import torch as _torch
+ if getattr(_torch.version, "hip", None) is not None:
+ env["HIP_VISIBLE_DEVICES"] = pinned
+ env.pop("ROCR_VISIBLE_DEVICES", None)
+ except Exception as e:
+ logger.debug("Failed to set ROCm visibility env vars for child: %s", e)
+
+ @staticmethod
+ def _pin_visible_gpu_order_for_split(env: dict) -> None:
+ """Pin the child's GPU enumeration to the picker's order for a manual
+ ``--tensor-split`` across the whole visible set. CUDA's default
+ FASTEST_FIRST enumeration applies the shares to the wrong cards on
+ heterogeneous hosts (#5025), and CUDA_DEVICE_ORDER only fixes the
+ numbering base: an inherited numeric visibility mask ALSO defines
+ enumeration order, so a reordered parent mask (CUDA_VISIBLE_DEVICES=3,1)
+ would still hand the shares to the wrong cards. The UI built the split
+ positionally over get_backend_visible_gpu_info's device list (ascending
+ physical via nvidia-smi, inherited mask order on the torch fallback), so
+ re-emit the same set in that report order -- not an assumed ascending
+ sort. The visible set itself never changes. No mask, an empty mask, or a
+ UUID/MIG mask (which resolves to None) is left alone -- the multi-GPU
+ controls are hidden for the latter."""
+ env["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
+ inherited = LlamaCppBackend._resolve_visible_physical_ids()
+ if not inherited:
+ return
+ order = None
+ try:
+ from utils.hardware import get_backend_visible_gpu_info
+ info = get_backend_visible_gpu_info()
+ if info.get("available") and info.get("index_kind") == "physical":
+ reported = [d["index"] for d in info.get("devices", [])]
+ if sorted(reported) == sorted(inherited):
+ order = reported
+ except Exception as e:
+ logger.debug("Could not read reported GPU order for split pin: %s", e)
+ if order is None:
+ order = sorted(inherited)
+ LlamaCppBackend._emit_child_gpu_visibility(env, ",".join(str(i) for i in order))
+
@staticmethod
def _amd_apu_wants_unified_memory(gpu_indices = None) -> bool:
"""True only for AMD unified-memory APUs (gfx1150/gfx1151), where
@@ -3262,6 +3420,20 @@ class LlamaCppBackend:
# aborts a --split-mode tensor load, so it's dropped for the tensor attempt.
_TENSOR_PARALLEL_KV_TYPES = frozenset({"f16", "bf16", "f32"})
+ # Main-model placement settings that Manual mode owns. They must not leak
+ # from Studio's parent environment into llama-server and silently override
+ # the command assembled from the current request. Draft-model placement is
+ # intentionally separate and remains available to speculative decoding.
+ _MANUAL_PLACEMENT_ENV_VARS = (
+ "LLAMA_ARG_CPU_MOE",
+ "LLAMA_ARG_N_CPU_MOE",
+ "LLAMA_ARG_N_GPU_LAYERS",
+ "LLAMA_ARG_TENSOR_SPLIT",
+ "LLAMA_ARG_FIT",
+ "LLAMA_ARG_FIT_TARGET",
+ "LLAMA_ARG_FIT_CTX",
+ )
+
# (binary, mtime, model) that aborted on --split-mode tensor this process (#6415
# geometry limit, e.g. MQA n_head_kv=1). Model-keyed so one model's abort doesn't
# skip tensor for others; tensor is tried by default, recorded only on a real abort.
@@ -3426,6 +3598,12 @@ class LlamaCppBackend:
return env
+ @classmethod
+ def _clear_manual_placement_env(cls, env: dict[str, str]) -> None:
+ """Remove inherited main-model placement owned by Manual mode."""
+ for name in cls._MANUAL_PLACEMENT_ENV_VARS:
+ env.pop(name, None)
+
@staticmethod
def _select_gpus(
model_size_bytes: int,
@@ -4246,6 +4424,8 @@ class LlamaCppBackend:
self._supports_preserve_thinking = False
self._supports_tools = False
self._n_layers = None
+ self._n_experts = None
+ self._leading_dense_block_count = None
self._n_kv_heads = None
self._n_kv_heads_by_layer = None
self._n_heads = None
@@ -4335,6 +4515,8 @@ class LlamaCppBackend:
arch_keys = {
f"{arch}.context_length": "context_length",
f"{arch}.block_count": "n_layers",
+ f"{arch}.expert_count": "n_experts",
+ f"{arch}.leading_dense_block_count": "leading_dense_block_count",
f"{arch}.attention.head_count_kv": "n_kv_heads",
f"{arch}.attention.head_count": "n_heads",
f"{arch}.embedding_length": "embedding_length",
@@ -4523,6 +4705,28 @@ class LlamaCppBackend:
return None
+ @staticmethod
+ def _diffusion_gpu_arg(gpu_ids: Optional[List[int]], *, cpu_only: bool = False) -> str:
+ """Device token passed to the diffusion visual-server child.
+
+ The visual engine replaces its child's CUDA visibility mask with this
+ token, so an unpinned load must carry forward the first token from the
+ parent's mask rather than turning a parent-relative ordinal into a new
+ physical selection.
+ """
+ if gpu_ids:
+ return str(sorted(gpu_ids)[0])
+ if cpu_only:
+ return ""
+ if "DG_GPU" in os.environ:
+ return os.environ["DG_GPU"]
+ parent_mask = os.environ.get("CUDA_VISIBLE_DEVICES")
+ if parent_mask:
+ first = next((token.strip() for token in parent_mask.split(",") if token.strip()), "")
+ if first and first != "-1":
+ return first
+ return "0"
+
def _start_diffusion_server(
self,
*,
@@ -4533,6 +4737,7 @@ class LlamaCppBackend:
model_identifier: str,
n_ctx: int,
extra_args: Optional[List[str]],
+ gpu_ids: Optional[List[int]] = None,
) -> bool:
"""Launch the OpenAI-compat diffusion shim (which drives the on-device
visual decoder) and wait for health. Presents the same /v1 + /health
@@ -4558,7 +4763,11 @@ class LlamaCppBackend:
# CUDA_VISIBLE_DEVICES="" to force CPU serving. Keep the visual-server child
# CPU-masked (empty --gpu) so the shim does not re-expose GPU 0 via its default.
cpu_only = self._effective_gpu_count() == 0
- gpu = "" if cpu_only else os.environ.get("DG_GPU", "0")
+ # Honor the GPU picker first: the diffusion runner takes a single device,
+ # so use the lowest selected GPU (matches the sorted set recorded below, so
+ # the device used == the echoed gpu_ids[0]). With no pick, fall back to the
+ # CPU-only mask, else DG_GPU / 0.
+ gpu = self._diffusion_gpu_arg(gpu_ids, cpu_only = cpu_only)
cmd = list(shim_cmd) + [
"--gguf",
@@ -4586,6 +4795,11 @@ class LlamaCppBackend:
env.setdefault("UNSLOTH_ALLOW_CPU", "1")
env["DG_VISUAL_BIN"] = visual_bin
env["DG_GPU"] = gpu
+ if gpu_ids:
+ # The visual server remasks via CUDA_VISIBLE_DEVICES=; pin PCI
+ # order (as the llama-server path does) so the picked physical id maps
+ # to the GPU the picker showed, not CUDA's default fastest-first order.
+ env["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
# The file-override shim imports its sibling visual_engine; put its dir on PYTHONPATH.
# (The zoo-package shim is an installed module and needs no PYTHONPATH change.)
if extra_pythonpath:
@@ -4631,6 +4845,23 @@ class LlamaCppBackend:
self._model_identifier = model_identifier
self._cache_type_kv = None
self._gpu_offload_active = True
+ # Diffusion doesn't use the llama.cpp GPU-memory knobs; reset them to
+ # defaults (the picked device is still recorded below) so /load, /status
+ # and reload dedup don't report a previous GGUF's manual settings.
+ self._gpu_memory_mode = "auto"
+ self._gpu_layers = -1
+ self._n_cpu_moe = 0
+ self._tensor_split = None
+ # Diffusion is never tensor-parallel; clear any state left by a prior TP
+ # chat load (load_model phase 1 only kills the process, it doesn't run
+ # the unload reset) so /status doesn't misreport TP and an identical
+ # re-Apply doesn't reload against stale tensor-parallel state.
+ self._tensor_parallel = False
+ # Record only the single device the runner actually uses (the lowest
+ # selected GPU, chosen above) -- not the whole pick. The diffusion runner
+ # is single-device, so echoing a multi-GPU list would misreport placement
+ # in /status and let a re-Apply dedup against GPUs the runner never used.
+ self._gpu_ids = [sorted(gpu_ids)[0]] if gpu_ids else None
if hf_variant:
self._hf_variant = hf_variant
elif gguf_path:
@@ -5721,6 +5952,11 @@ class LlamaCppBackend:
speculative_type: Optional[str] = None,
spec_draft_n_max: Optional[int] = None,
tensor_parallel: bool = False,
+ gpu_memory_mode: Literal["auto", "manual"] = "auto",
+ gpu_layers: int = -1,
+ n_cpu_moe: int = 0,
+ tensor_split: Optional[List[float]] = None,
+ gpu_ids: Optional[List[int]] = None,
n_threads: Optional[int] = None,
n_gpu_layers: Optional[int] = None, # caller compat, unused
n_parallel: int = 1,
@@ -5753,6 +5989,14 @@ class LlamaCppBackend:
"speculative_type": speculative_type,
"spec_draft_n_max": spec_draft_n_max,
"tensor_parallel": tensor_parallel,
+ # GPU-memory placement: replayed on respawn so a server SIGKILL'd by
+ # GPU/RAM pressure reloads onto the same devices with the same
+ # offload, not the auto defaults.
+ "gpu_memory_mode": gpu_memory_mode,
+ "gpu_layers": gpu_layers,
+ "n_cpu_moe": n_cpu_moe,
+ "tensor_split": list(tensor_split) if tensor_split is not None else None,
+ "gpu_ids": list(gpu_ids) if gpu_ids is not None else None,
"n_threads": n_threads,
"n_gpu_layers": n_gpu_layers,
"n_parallel": n_parallel,
@@ -5779,6 +6023,11 @@ class LlamaCppBackend:
speculative_type = speculative_type,
spec_draft_n_max = spec_draft_n_max,
tensor_parallel = tensor_parallel,
+ gpu_memory_mode = gpu_memory_mode,
+ gpu_layers = gpu_layers,
+ n_cpu_moe = n_cpu_moe,
+ tensor_split = tensor_split,
+ gpu_ids = gpu_ids,
chat_template_override = chat_template_override,
extra_args = extra_args,
is_vision = is_vision,
@@ -5899,6 +6148,7 @@ class LlamaCppBackend:
model_identifier = model_identifier,
n_ctx = n_ctx,
extra_args = extra_args,
+ gpu_ids = gpu_ids,
)
if not binary:
@@ -5960,6 +6210,59 @@ class LlamaCppBackend:
# use the same helper so a healthy env-driven tensor server matches.
split_mode_override = parse_split_mode_override(extra_args)
tensor_parallel = _effective_tensor_parallel(extra_args, tensor_parallel)
+ # gpu_layers=0 leaves nothing to split, yet --split-mode tensor or
+ # a per-GPU ratio still launches tensor mode -- and under the
+ # CPU-only mask below (no visible devices) that aborts the server
+ # instead of loading on CPU. Drop both here (nothing to split).
+ if gpu_memory_mode == "manual" and gpu_layers == 0:
+ if tensor_parallel or tensor_split:
+ logger.info(
+ "Manual gpu_layers=0: dropping tensor split/parallel "
+ "flags (nothing to split on the GPU)"
+ )
+ tensor_parallel = False
+ tensor_split = None
+ # Record the requested strategy for /status and the load
+ # response. 'manual' has no fallback, so the request value is the
+ # value actually applied.
+ self._gpu_memory_mode = gpu_memory_mode
+ # The layer/MoE/split knobs apply only with an explicit offload
+ # (manual + gpu_layers >= 0); else record defaults so /status and
+ # /load don't report knobs the server never applied.
+ if gpu_memory_mode == "manual" and gpu_layers >= 0:
+ self._gpu_layers = gpu_layers
+ self._n_cpu_moe = n_cpu_moe
+ self._tensor_split = tensor_split
+ else:
+ self._gpu_layers = -1
+ self._n_cpu_moe = 0
+ self._tensor_split = None
+ self._gpu_ids = sorted(gpu_ids) if gpu_ids else None
+ # Manual offload skips the TP planner but still emits --split-mode
+ # tensor at launch; drop it when fewer than 2 GPUs are in use --
+ # tensor split is a no-op there and aborts on some architectures.
+ # Done before the cache-drop below so a quantized KV survives.
+ if (
+ tensor_parallel
+ and gpu_memory_mode == "manual"
+ and gpu_layers >= 0
+ and self._effective_gpu_count(sorted(gpu_ids) if gpu_ids else None) < 2
+ ):
+ logger.info(
+ "Tensor parallelism requested in manual mode but fewer "
+ "than 2 GPUs are in use; ignoring (needs >= 2)."
+ )
+ tensor_parallel = False
+ # Drop TP for manual + Auto layers before the cache-drop below (like
+ # the <2-GPU guard above), so a requested quantized KV survives into
+ # the --fit load rather than being stripped for a tensor attempt.
+ if tensor_parallel and gpu_memory_mode == "manual" and gpu_layers < 0:
+ logger.info(
+ "Manual mode with Auto layers hands memory management to "
+ "llama.cpp --fit, which is incompatible with tensor "
+ "parallelism; ignoring the tensor split."
+ )
+ tensor_parallel = False
# Tensor mode aborts on a quantized KV cache, so drop it for the
# tensor attempt (and strip any inherited/explicit --cache-type
# that would re-impose it when appended last). Layer split does
@@ -6040,6 +6343,12 @@ class LlamaCppBackend:
"Vision-capable GGUF loaded without a usable mmproj; "
"image input will be disabled for this session"
)
+ # Seed before the try: the except (GPU-selection failure ->
+ # --fit on) falls through to the launch which reads this, and the
+ # probe that assigns it may throw first. Captured before manual
+ # empty `gpus` so the speculative defaults stay GPU-aware and the
+ # CPU-fallback check still knows GPUs were present.
+ _detected_gpus: list[tuple[int, int]] = []
model_size = None # set in the fit try; used by the APU RAM guard
# Layer-fallback min GPUs; raised below on a tensor downgrade. Bound
# before the try so the --fit-on except path still has it (no UnboundLocal).
@@ -6057,6 +6366,18 @@ class LlamaCppBackend:
_gpu_mem = self._get_gpu_memory(binary)
gpus = [(idx, free) for idx, free, _t in _gpu_mem]
total_by_idx = {idx: total for idx, _f, total in _gpu_mem}
+ # GPU picker: restrict every mode to the chosen devices, so
+ # auto selection only considers them and manual mask to
+ # them (the env block below pins CUDA/HIP_VISIBLE_DEVICES).
+ if gpu_ids:
+ _picked = set(gpu_ids)
+ gpus = [g for g in gpus if g[0] in _picked]
+
+ # GPUs the model will run on -- captured before manual
+ # empty `gpus` to bypass the planner. bool() drives the
+ # GPU-aware speculative defaults; the list feeds the
+ # CPU-fallback check.
+ _detected_gpus = list(gpus)
def _gpu_usable(g, frac = _CTX_FIT_VRAM_FRACTION):
# Per-GPU usable budget for ranking: free - (1-frac)*total.
@@ -6088,6 +6409,44 @@ class LlamaCppBackend:
# GPU/VRAM-fit logic below may shrink it on limited HW.
max_available_ctx = self._context_length or effective_ctx
+ # Manual + Auto layers (the Manual default): hand memory
+ # management to llama.cpp's --fit. Emptying the probed GPU set
+ # no-ops the selection/TP planning below, leaving gpu_indices
+ # None (an explicit gpu_ids pick still pins below) and use_fit
+ # True. An explicit context is honored (--fit optimizes around
+ # it); 0 lets --fit size it.
+ if gpu_memory_mode == "manual" and gpu_layers < 0:
+ # Tensor parallelism was already dropped above (before the
+ # cache-drop), so a quantized KV survives into this --fit load.
+ gpus = []
+ effective_ctx = requested_ctx if requested_ctx > 0 else 0
+ original_ctx = effective_ctx
+ # --fit aborts under --split-mode tensor; a raw extras
+ # --split-mode/--tensor-split (appended last) would
+ # otherwise reach llama-server. Strip it like the TP
+ # downgrade does.
+ extra_args = strip_split_mode_only(extra_args)
+ elif gpu_memory_mode == "manual":
+ # Manual offload (--gpu-layers + --fit off): no automatic
+ # device masking (a gpu_ids pick still pins below) or
+ # context cap -- the user owns both. tensor_parallel is
+ # honored but skips the memory-based planner (gpus = []);
+ # the toggle just emits --split-mode tensor (split by free
+ # VRAM, or by the Split ratio if set).
+ gpus = []
+ effective_ctx = (
+ requested_ctx if requested_ctx > 0 else (self._context_length or 0)
+ )
+ original_ctx = effective_ctx
+ # Strip the user --split-mode when the toggle owns the split
+ # (TP engaged -> Studio emits --split-mode tensor) or when the
+ # user asked for tensor (which aborts on a single GPU even if
+ # the manual <2-GPU guard downgraded TP). Otherwise keep their
+ # non-tensor mode (row/none/layer) -- the toggle can't express
+ # those.
+ if tensor_parallel or split_mode_override == "tensor":
+ extra_args = strip_split_mode_only(extra_args)
+
# Will MTP engage? If so, auto-fit reserves draft-model VRAM.
# Mirrors _build_speculative_flags: forced mtp/mtp+ngram always
# engage; auto only on an MTP model >= 3B; ngram/off never. A
@@ -6175,7 +6534,10 @@ class LlamaCppBackend:
_extra_n_max = _extra_args_spec_draft_n_max(extra_args)
_mtp_eff_n_max = _extra_n_max if _extra_n_max is not None else spec_draft_n_max
if _mtp_eff_n_max is None:
- _mtp_eff_n_max = 2 if gpus else 3
+ # _detected_gpus (not gpus) so manual -- which empty
+ # gpus to bypass the planner -- keep the GPU draft depth the
+ # launch flags also use, instead of the CPU default.
+ _mtp_eff_n_max = 2 if _detected_gpus else 3
# Separate-drafter weights live on GPU (an embedded head is
# already in model_size). Size the drafter the launch loads, by
# precedence: extras --model-draft (last-wins), else Unsloth's
@@ -6313,7 +6675,8 @@ class LlamaCppBackend:
# honor it, cap only if it fits no combination. Auto (native):
# prefer fewer GPUs with reduced context (multi-GPU is slower).
gpu_indices, use_fit = None, True
- # Per-GPU weight proportions for tensor mode (None = even).
+ # Per-GPU weight proportions for tensor mode (None lets
+ # llama.cpp split by free VRAM).
tp_tensor_split: Optional[list[int]] = None
explicit_ctx = requested_ctx > 0
# Flat MTP reserve fraction: used only as the fallback when the
@@ -6388,7 +6751,12 @@ class LlamaCppBackend:
# GPUs below that reserve from the set up front (gpu_indices
# becomes the CUDA_VISIBLE_DEVICES mask, fully excluding them).
tp_gpus = gpus
- if tensor_parallel:
+ # Manual mode owns the layer count and context, so it skips
+ # the memory-based planner; its toggle still emits
+ # --split-mode tensor below (split by free VRAM, or by the
+ # Split ratio if set). auto plans here.
+ plan_tp = tensor_parallel and gpu_memory_mode != "manual"
+ if plan_tp:
# Deterministic per-device compute buffer (replicated on
# every device in tensor mode); flat fallback when dims
# are unavailable. _plan_tensor_parallel uses the same.
@@ -6407,7 +6775,7 @@ class LlamaCppBackend:
# free yet have no budget left.
tp_gpus = [g for g in gpus if _gpu_usable(g) >= reserve_mib]
- if tensor_parallel and len(tp_gpus) < 2:
+ if plan_tp and len(tp_gpus) < 2:
# Tensor parallelism needs >= 2 usable GPUs. On a single
# GPU --split-mode tensor is a no-op; with 0 GPUs (CPU-only
# or probe failed) it must not reach llama-server; and a
@@ -6823,6 +7191,12 @@ class LlamaCppBackend:
tp_tensor_split = None
effective_ctx = requested_ctx # fall back to original
+ # GPU picker: when no narrower subset was chosen (manual, or
+ # a failed/file-size selection), pin the whole picked set so the
+ # model can't spill onto an unpicked GPU.
+ if gpu_ids and gpu_indices is None:
+ gpu_indices = sorted(gpu_ids)
+
# Unified-memory APUs load weights into system RAM (under WSL the VM
# cap, not the ROCm-reported VRAM, is the real ceiling); refuse an
# oversize load the OS would otherwise kill mid-flight. Base model
@@ -6859,8 +7233,6 @@ class LlamaCppBackend:
model_path,
"--port",
str(self._port),
- "-c",
- str(effective_ctx) if effective_ctx > 0 else "0",
"--parallel",
str(n_parallel),
"--flash-attn",
@@ -6868,6 +7240,17 @@ class LlamaCppBackend:
# Error out at n_ctx instead of silently rotating the KV cache; frontend catches it and points the user at "Context Length".
"--no-context-shift",
]
+ # A positive context is always passed (in auto-fit, --fit then
+ # optimizes the gpu-layer offload around it). When auto-fit has
+ # no explicit context, omit -c so --fit sizes it to fit VRAM:
+ # "-c 0" would instead pin the FULL native context (llama.cpp's
+ # -c handler sets fit_params_min_ctx = UINT32_MAX on value 0,
+ # disabling --fit's reduction). See gpu_memory_mode.
+ auto_fit = gpu_memory_mode == "manual" and gpu_layers < 0
+ if effective_ctx > 0:
+ cmd.extend(["-c", str(effective_ctx)])
+ elif not auto_fit:
+ cmd.extend(["-c", "0"])
# Report a clean public model id (matching GET /v1/models) rather
# than the raw -m path in llama-server's own /v1/models and the
@@ -6879,7 +7262,63 @@ class LlamaCppBackend:
cmd.extend(["--alias", _alias])
fully_gpu_offloaded = False
- if use_fit:
+ # Set when a positional --tensor-split is emitted, so the env block
+ # can pin CUDA to PCI order even without a GPU subset (see below).
+ manual_tensor_split_emitted = False
+ if gpu_memory_mode == "manual" and gpu_layers >= 0:
+ # Pin the user's layer count and disable auto-fit. --fit off
+ # also means _ctx_integrity_flags must not add --fit-ctx.
+ use_fit = False
+ cmd.extend(["--gpu-layers", str(gpu_layers), "--fit", "off"])
+ # Keep the first n_cpu_moe MoE layers' experts on CPU.
+ moe_flag = self._resolve_cpu_moe_flag(
+ n_cpu_moe,
+ self.n_moe_layers,
+ self._leading_dense_block_count or 0,
+ )
+ if moe_flag is not None:
+ cmd.extend(["--n-cpu-moe", str(moe_flag)])
+ elif n_cpu_moe:
+ # Requested on a dense model: nothing was emitted, so
+ # don't report a count llama-server never received.
+ self._n_cpu_moe = 0
+ # Distribute the model across GPUs by the user's per-GPU shares
+ # (--tensor-split). Works with layer split and tensor
+ # parallelism; --fit off means no fit/tensor abort. Only emit
+ # when >1 GPU is in use AND the list length matches that count:
+ # the field is hidden (not cleared) when the picker narrows to
+ # one, and a direct caller can send a stale ratio for a different
+ # GPU set. Studio drops any mismatch to the free-VRAM default
+ # (llama.cpp would silently zero-pad a short list, or abort past
+ # its 16-device cap).
+ _split_gpus = self._effective_gpu_count(gpu_indices)
+ if tensor_split and _split_gpus > 1:
+ # An all-zero/non-positive sanitized split assigns nothing
+ # anywhere, so fall through to the free-VRAM default in
+ # that case.
+ _sanitized_split = self._sanitize_tensor_split(tensor_split)
+ _split_total = sum(_sanitized_split)
+ if len(_sanitized_split) == _split_gpus and _split_total > 0:
+ cmd.extend(
+ ["--tensor-split", ",".join(f"{x:g}" for x in _sanitized_split)]
+ )
+ self._tensor_split = _sanitized_split
+ manual_tensor_split_emitted = True
+ else:
+ logger.warning(
+ "Dropping manual --tensor-split (%d entries for "
+ "%d GPUs, sanitized total %s); llama.cpp's "
+ "free-VRAM split applies instead",
+ len(tensor_split),
+ _split_gpus,
+ _split_total,
+ )
+ self._tensor_split = None
+ elif tensor_split:
+ # Single effective GPU: the split is never emitted, so
+ # don't report it as active via /status and /load.
+ self._tensor_split = None
+ elif use_fit:
cmd.extend(["--fit", "on"])
elif gpu_indices is not None:
# Fits on selected GPU(s) -- force all layers on GPU. --fit off is
@@ -6897,6 +7336,7 @@ class LlamaCppBackend:
self._ctx_integrity_flags(
n_parallel,
use_fit,
+ auto_fit,
requested_ctx,
effective_ctx,
server_caps,
@@ -6960,9 +7400,11 @@ class LlamaCppBackend:
self._cache_type_kv = None
# Tensor parallelism: split the model across GPUs by tensor
- # rather than by layer. Multi-GPU only -- a no-op on a single
- # GPU. Default (layer split) is left implicit by omitting the
- # flag. See llama.cpp --split-mode.
+ # rather than by layer. The UI only offers it on multi-GPU; a
+ # direct single-GPU caller is redundant (supported archs no-op,
+ # unsupported ones abort and the /load path retries layer split).
+ # Default (layer split) is left implicit by omitting the flag.
+ # See llama.cpp --split-mode.
if tensor_parallel:
cmd.extend(["--split-mode", "tensor"])
if tp_tensor_split and len(tp_tensor_split) > 1:
@@ -6994,7 +7436,7 @@ class LlamaCppBackend:
extra_args = extra_args,
model_identifier = model_identifier,
model_path = model_path,
- gpus = bool(gpus),
+ gpus = bool(_detected_gpus),
binary = binary,
mtp_draft_path = launch_mtp_draft_path,
)
@@ -7112,6 +7554,8 @@ class LlamaCppBackend:
# Library paths so llama-server finds its shared libs and CUDA DLLs.
env = self._llama_server_env_for_binary(binary)
+ if gpu_memory_mode == "manual":
+ self._clear_manual_placement_env(env)
# Omitting --threads relies on llama.cpp's physical-core default, so
# drop an inherited LLAMA_ARG_THREADS that would otherwise feed the
# arg handler and silently force hardware_concurrency(). #5692
@@ -7170,28 +7614,39 @@ class LlamaCppBackend:
# CUDA_VISIBLE_DEVICES leaves an AMD child seeing the full set, so
# set HIP_VISIBLE_DEVICES too. Vulkan is pinned via --device
# (above), not here.
- if gpu_indices is not None and not is_vulkan_backend:
- pinned = ",".join(str(i) for i in gpu_indices)
- env["CUDA_VISIBLE_DEVICES"] = pinned
- try:
- import torch as _torch
- if getattr(_torch.version, "hip", None) is not None:
- env["HIP_VISIBLE_DEVICES"] = pinned
- # Do NOT also set ROCR_VISIBLE_DEVICES to the same
- # value. ROCR_VISIBLE_DEVICES filters at the HSA/ROCr
- # layer and HIP_VISIBLE_DEVICES at the HIP layer, so
- # setting both with the same physical indices applies
- # the mask twice: ROCR reduces the visible set and
- # re-indexes it from 0, then HIP indexes into the
- # already-reduced set. A single non-zero pin (e.g.
- # "1") then points out of range at the HIP layer, HIP
- # enumerates 0 devices, and llama.cpp falls back to
- # CPU ("ggml_cuda_init: no ROCm-capable device is
- # detected"). The HIP mask alone narrows correctly;
- # clear any inherited ROCR mask so it can't double up.
- env.pop("ROCR_VISIBLE_DEVICES", None)
- except Exception as e:
- logger.debug("Failed to set ROCm visibility env vars for child: %s", e)
+ # A deliberate zero-offload load with no GPU companions runs
+ # entirely on CPU, yet a visible CUDA device still costs the child
+ # ~0.5 GB (context + compute scratch) that the CPU-only
+ # classification below reports as free. Hide the GPUs so the load
+ # is exactly what it claims: zero VRAM (verified: GPU stays at idle
+ # baseline and generation runs). Companion loads keep the normal
+ # masking, and a user device pin (in extras or an inherited
+ # LLAMA_ARG_DEVICE) keeps control of its own devices -- the child
+ # aborts on a pin it can't see. The draft-device forms count too:
+ # llama-server parses them even with no drafter loaded.
+ _cpu_only_zero_offload = (
+ gpu_memory_mode == "manual"
+ and gpu_layers == 0
+ and not is_vulkan_backend
+ and not self._zero_offload_keeps_gpu_visible(cmd, env)
+ )
+ if _cpu_only_zero_offload:
+ self._emit_child_gpu_visibility(env, "-1")
+ elif gpu_indices is not None and not is_vulkan_backend:
+ # When the user picked GPUs by index, align CUDA's ordering
+ # with the PCI-bus order the picker enumerated (nvidia-smi),
+ # so "GPU 1" in the UI is GPU 1 to llama.cpp -- not CUDA's
+ # default FASTEST_FIRST order (#5025).
+ if gpu_ids:
+ env["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
+ self._emit_child_gpu_visibility(env, ",".join(str(i) for i in gpu_indices))
+ elif manual_tensor_split_emitted and not is_vulkan_backend:
+ # A manual per-GPU ratio across ALL GPUs (no explicit pick, so
+ # no CUDA_VISIBLE_DEVICES mask above): the UI built the
+ # --tensor-split list in ascending physical/PCI index order,
+ # so pin the child's enumeration to that order too. The whole
+ # visible set stays in use; only its ordering is fixed.
+ self._pin_visible_gpu_order_for_split(env)
# Captured before any text-only fallback strips it from cmd.
launched_with_mmproj = "--mmproj" in cmd
@@ -7350,7 +7805,6 @@ class LlamaCppBackend:
self._effective_context_length = (
effective_ctx if effective_ctx > 0 else self._context_length
)
- self._reconcile_effective_ctx_with_server()
self._max_context_length = (
max_available_ctx if max_available_ctx > 0 else self._effective_context_length
)
@@ -7535,6 +7989,10 @@ class LlamaCppBackend:
"session; run 'unsloth studio update' to enable vision."
)
cmd = self._strip_mmproj_args(_last_spawn_cmd)
+ # This retry bypasses _spawn_and_wait, so refresh the
+ # launched-argv snapshot itself -- the zero-offload
+ # classification below must not see the stripped --mmproj.
+ _last_spawn_cmd = list(cmd)
self._is_vision = False
self._mmproj_has_audio = False
self._start_llama_process(cmd, env)
@@ -7566,6 +8024,13 @@ class LlamaCppBackend:
self._healthy = True
self._commit_effective_parallel_slots(n_parallel)
+ # Server is up: adopt the real per-request context it allocated
+ # -- the length --fit chose, or a --parallel slot split -- so the
+ # reported context_length matches reality. (Querying /props
+ # before the spawn above always failed; the seeded value was the
+ # requested/native length.)
+ self._reconcile_effective_ctx_with_server()
+
# Commit caller intent only after _healthy=True so a failed start
# can't poison the next inheritance check. None keeps prior, []
# clears, list sets. Source records hf_variant for the route's
@@ -7580,11 +8045,24 @@ class LlamaCppBackend:
self._mtp_runtime_fallback_active = _mtp_active_for_launched_server
self._start_mtp_crash_watchdog()
- # Catch silent CPU fallback when GPU was intended (#5106).
- self._gpu_offload_active = self._classify_gpu_offload(
- gpu_indices is not None or use_fit, gpus or []
- )
- if self._gpu_offload_active is False:
+ # Catch silent CPU fallback when GPU was intended (#5106). Manual
+ # offload (no picker) leaves gpu_indices None and use_fit False, so
+ # include its GPU-layer intent; use the preserved probe since
+ # auto-layers/manual empty `gpus`. A deliberate zero-offload load
+ # classifies by its launched argv instead: the main model is
+ # CPU-only by construction and must read False (not None), or
+ # training needlessly unloads a server holding no VRAM.
+ _deliberate_cpu_only = gpu_memory_mode == "manual" and gpu_layers == 0
+ if _deliberate_cpu_only:
+ self._gpu_offload_active = self._zero_offload_gpu_flag(
+ _last_spawn_cmd, _detected_gpus, env
+ )
+ else:
+ self._gpu_offload_active = self._classify_gpu_offload(
+ gpu_indices is not None or use_fit or gpu_memory_mode == "manual",
+ _detected_gpus,
+ )
+ if self._gpu_offload_active is False and not _deliberate_cpu_only:
logger.warning(
"llama-server appears to have loaded the model entirely "
"on CPU even though Unsloth detected at least one GPU. "
@@ -7947,6 +8425,11 @@ class LlamaCppBackend:
gguf_path: Optional[str] = None,
spec_draft_n_max: Optional[int] = None,
tensor_parallel: bool = False,
+ gpu_memory_mode: Literal["auto", "manual"] = "auto",
+ gpu_layers: int = -1,
+ n_cpu_moe: int = 0,
+ tensor_split: Optional[List[float]] = None,
+ gpu_ids: Optional[List[int]] = None,
mtp_draft_path: Optional[str] = None,
preserve_multi_gpu_on_layer: bool = False,
) -> bool:
@@ -8003,6 +8486,38 @@ class LlamaCppBackend:
):
return False
+ # The diffusion runner is mode-agnostic (always "auto", ignores the
+ # layer/MoE/split knobs), so a standing manual preference in the
+ # request must not force a needless reload -- only the GPU pick matters.
+ if not self._is_diffusion:
+ # A GPU-memory-mode flip (Unsloth / manual) must always reload.
+ if self._gpu_memory_mode != gpu_memory_mode:
+ return False
+ # Manual: a layer-count change always reloads (covers Auto(-1) <-> a
+ # pinned count); MoE/split only matter with an explicit offload.
+ if gpu_memory_mode == "manual" and (
+ self._gpu_layers != gpu_layers
+ or (
+ gpu_layers >= 0
+ and (
+ self._n_cpu_moe != n_cpu_moe
+ or (self._tensor_split or None) != (tensor_split or None)
+ )
+ )
+ ):
+ return False
+ # A changed GPU pick must reload (compare order-insensitively; None/[]
+ # both mean automatic). The diffusion runner collapses a multi-GPU pick
+ # to its single lowest device, so self._gpu_ids holds just that device;
+ # normalize the request the same way, or a multi-GPU pick that resolves
+ # to the same device needlessly reloads.
+ if self._is_diffusion:
+ requested_gpu_pick = [sorted(gpu_ids)[0]] if gpu_ids else None
+ else:
+ requested_gpu_pick = sorted(gpu_ids) if gpu_ids else None
+ if (self._gpu_ids or None) != requested_gpu_pick:
+ return False
+
# Compare on the canonical requested mode. With --spec-type in
# extra_args the backend stores None; mirror that here.
if _extra_args_set_spec_type(extra_args):
@@ -8071,6 +8586,78 @@ class LlamaCppBackend:
return None
return classify_gpu_offload_lines(self._stdout_lines)
+ @staticmethod
+ def _cmd_has_gpu_companion(cmd: list, env: Optional[Mapping[str, str]] = None) -> bool:
+ """True when the argv/env carries a GPU companion: any --mmproj form, or
+ a drafter (Studio's --model-draft, the extras aliases, or the
+ LLAMA_ARG_SPEC_DRAFT_* env) -- these offload to the GPU regardless of
+ the main ``--gpu-layers``. A drafter explicitly forced to CPU
+ (--spec-draft-ngl 0 / --spec-draft-device cpu) doesn't count."""
+ if any(str(a).startswith("--mmproj") for a in cmd):
+ return True
+ if _extra_args_mtp_draft_path(cmd, env) is None:
+ return False
+ return not _extra_args_draft_offloaded_to_cpu(cmd, env)
+
+ @staticmethod
+ def _zero_offload_keeps_gpu_visible(cmd: list, env: Optional[Mapping[str, str]] = None) -> bool:
+ """Whether a zero-layer launch still has a reason to use visible GPUs.
+
+ Keep this shared by child masking and post-launch residency bookkeeping:
+ a device pin, surviving tensor mode, mmproj, or GPU drafter prevents the
+ launch from being a confirmed zero-VRAM server.
+ """
+ return (
+ LlamaCppBackend._cmd_has_gpu_device_pin(cmd, env)
+ or _effective_tensor_parallel(cmd, False, env)
+ or LlamaCppBackend._cmd_has_gpu_companion(cmd, env)
+ )
+
+ @staticmethod
+ def _cmd_has_gpu_device_pin(cmd: list, env: Optional[Mapping[str, str]] = None) -> bool:
+ """True when the effective main or draft ``--device`` pin names a GPU."""
+ main_flags = {"--device", "-dev"}
+ draft_flags = {"--spec-draft-device", "-devd", "--device-draft"}
+ last_main: Optional[str] = None
+ last_draft: Optional[str] = None
+ args = [str(arg) for arg in cmd]
+ for index, raw in enumerate(args):
+ flag, equals, inline = raw.partition("=")
+ if flag not in main_flags and flag not in draft_flags:
+ continue
+ value = inline if equals else (args[index + 1] if index + 1 < len(args) else "")
+ if flag in main_flags:
+ last_main = value
+ else:
+ last_draft = value
+ if last_main is None:
+ last_main = (env or {}).get("LLAMA_ARG_DEVICE")
+
+ def _names_gpu(value: Optional[str]) -> bool:
+ if value is None:
+ return False
+ devices = [item.strip().lower() for item in value.split(",") if item.strip()]
+ return not devices or any(item not in ("cpu", "none") for item in devices)
+
+ return _names_gpu(last_main) or _names_gpu(last_draft)
+
+ @staticmethod
+ def _zero_offload_gpu_flag(
+ spawn_cmd: list,
+ detected_gpus: list,
+ env: Optional[Mapping[str, str]] = None,
+ ) -> Optional[bool]:
+ """GPU-residency flag for a deliberate manual zero-offload load. The
+ main model is CPU-only by construction, but device pins, tensor mode,
+ mmproj, and GPU drafters can still make the server hold VRAM. The counted
+ offload classifier cannot see those allocations. This uses the same
+ predicate as the launch-time zero-VRAM mask; None means no GPU signal."""
+ if not detected_gpus:
+ return None
+ if LlamaCppBackend._is_vulkan_backend():
+ return True
+ return LlamaCppBackend._zero_offload_keeps_gpu_visible(spawn_cmd, env)
+
def load_cancelled(self) -> bool:
"""True if a load was cancelled (e.g. via unload/_cancel_event) and not
yet consumed by the next load_model. Lets the tensor->layer fallback
@@ -8114,11 +8701,18 @@ class LlamaCppBackend:
self._supports_tools = False
self._cache_type_kv = None
self._tensor_parallel = False
+ self._gpu_memory_mode = "auto"
+ self._gpu_layers = -1
+ self._n_cpu_moe = 0
+ self._tensor_split = None
+ self._gpu_ids = None
self._layer_preserves_tensor_intent = False
self._speculative_type = None
self._requested_spec_mode = None
self._spec_draft_n_max = None
self._n_layers = None
+ self._n_experts = None
+ self._leading_dense_block_count = None
self._n_kv_heads = None
self._n_kv_heads_by_layer = None
self._n_heads = None
@@ -8181,6 +8775,10 @@ class LlamaCppBackend:
# Clear healthy so a /load during the replacement's warm-up can't
# short-circuit against the previous server's health (#5401).
self._healthy = False
+ # Reset to unknown so the training guard treats the next (still
+ # loading) server as VRAM-resident rather than reading the killed
+ # server's stale zero-offload flag until the health probe reclassifies.
+ self._gpu_offload_active = None
# Drives _wait_for_vram_settle in the next load_model; set in finally
# so both in-process and frontend Apply paths record the kill.
self._last_kill_monotonic = time.monotonic()
@@ -8785,7 +9383,12 @@ class LlamaCppBackend:
@staticmethod
def _ctx_integrity_flags(
- n_parallel: int, use_fit: bool, requested_ctx: int, effective_ctx: int, caps: dict
+ n_parallel: int,
+ use_fit: bool,
+ auto_fit: bool,
+ requested_ctx: int,
+ effective_ctx: int,
+ caps: dict,
) -> list[str]:
"""Flags that keep the per-request window equal to the advertised ctx.
@@ -8793,14 +9396,28 @@ class LlamaCppBackend:
``--kv-unified`` default, silently splitting ``-c`` into per-slot
windows of ``-c / N``; restore the shared pool so one request can use
the full context. With ``--fit on``, ``--fit-ctx`` floors the fit step
- at an explicitly requested ctx (default floor is 4096) so it offloads
- or fails instead of silently shrinking the window.
+ at an explicitly requested ctx so it offloads or fails instead of
+ silently shrinking the window. The 8192 auto-floor and the tighter
+ ``--fit-target`` margin apply only under Manual + Auto (``auto_fit``),
+ which omits ``-c``: on the legacy auto path ``-c 0`` already pins the
+ native window and ``--fit-ctx 8192`` would override it down to 8192.
"""
flags: list[str] = []
if n_parallel > 1 and caps.get("supports_kv_unified"):
flags.append("--kv-unified")
- if use_fit and requested_ctx > 0 and effective_ctx > 0 and caps.get("supports_fit_ctx"):
- flags.extend(["--fit-ctx", str(effective_ctx)])
+ if use_fit and caps.get("supports_fit_ctx"):
+ if requested_ctx > 0 and effective_ctx > 0:
+ # Floor the fit step at the explicitly requested ctx.
+ flags.extend(["--fit-ctx", str(effective_ctx)])
+ elif auto_fit:
+ # Manual + Auto omits -c, so floor at 8192 so --fit doesn't
+ # shrink the window below a usable size.
+ flags.extend(["--fit-ctx", "8192"])
+ if use_fit and auto_fit and caps.get("supports_fit_target"):
+ # llama.cpp's --fit leaves 1 GiB free per device by default;
+ # tighten that to 512 MiB so it packs more of the model onto
+ # the GPU before spilling to system RAM.
+ flags.extend(["--fit-target", "512"])
return flags
def _query_server_n_ctx(self) -> Optional[int]:
diff --git a/studio/backend/core/inference/llama_server_args.py b/studio/backend/core/inference/llama_server_args.py
index 70d0dc774d..e72e10e071 100644
--- a/studio/backend/core/inference/llama_server_args.py
+++ b/studio/backend/core/inference/llama_server_args.py
@@ -186,12 +186,25 @@ _SPLIT_MODE_FLAGS: frozenset[str] = frozenset({"-sm", "--split-mode"})
_TENSOR_SPLIT_FLAGS: frozenset[str] = frozenset({"-ts", "--tensor-split"})
_SPLIT_SHADOWING_FLAGS: frozenset[str] = _SPLIT_MODE_FLAGS | _TENSOR_SPLIT_FLAGS
+# GPU-offload flags. Stripped only when the GPU Memory mode owns offload
+# (manual emits --fit / --gpu-layers / --n-cpu-moe); in auto, a user's
+# inherited -ngl is respected (the offload_overridden path), so this group is
+# opt-in, not default. Layer flags are shared with llama_cpp's override
+# detection; the MoE flags are strip-only (manual's --n-cpu-moe slider owns them).
+_LAYER_OFFLOAD_FLAGS: frozenset[str] = frozenset(
+ {"-ngl", "--gpu-layers", "--n-gpu-layers", "-fit", "--fit"}
+)
+_MOE_OFFLOAD_FLAGS: frozenset[str] = frozenset({"-ncmoe", "--n-cpu-moe", "-cmoe", "--cpu-moe"})
+_OFFLOAD_SHADOWING_FLAGS: frozenset[str] = _LAYER_OFFLOAD_FLAGS | _MOE_OFFLOAD_FLAGS
+
_SHADOWING_FLAGS: frozenset[str] = (
_CONTEXT_FLAGS | _CACHE_FLAGS | _SPEC_FLAGS | _TEMPLATE_FLAGS | _SPLIT_SHADOWING_FLAGS
)
# Shadowing flags that take no value -- strip the flag only, not the next token.
-_BOOLEAN_SHADOWING_FLAGS: frozenset[str] = frozenset({"--spec-default", "--jinja", "--no-jinja"})
+_BOOLEAN_SHADOWING_FLAGS: frozenset[str] = frozenset(
+ {"--spec-default", "--jinja", "--no-jinja", "-cmoe", "--cpu-moe"}
+)
def parse_ctx_override(args: Optional[Iterable[str]]) -> Optional[int]:
@@ -424,6 +437,8 @@ def strip_shadowing_flags(
strip_spec: bool = True,
strip_template: bool = True,
strip_split_mode: bool = True,
+ strip_tensor_split: bool = False,
+ strip_offload: bool = False,
) -> list[str]:
"""Strip flags that shadow first-class Unsloth settings.
@@ -432,6 +447,12 @@ def strip_shadowing_flags(
(same for cache / spec / template / split-mode). Each ``strip_*``
toggle controls one group; the route only strips groups whose
first-class field the caller actually supplied.
+
+ ``strip_split_mode`` removes both ``--split-mode`` and the coupled
+ ``--tensor-split`` (the Tensor Parallelism toggle owns the whole split).
+ ``strip_tensor_split`` removes ``--tensor-split`` *alone*, so manual mode can
+ replace an inherited per-GPU ratio while leaving the user's ``--split-mode``
+ row/none/layer choice intact.
"""
shadowing: set[str] = set()
if strip_context:
@@ -444,6 +465,10 @@ def strip_shadowing_flags(
shadowing |= _TEMPLATE_FLAGS
if strip_split_mode:
shadowing |= _SPLIT_SHADOWING_FLAGS
+ if strip_tensor_split:
+ shadowing |= _TENSOR_SPLIT_FLAGS
+ if strip_offload:
+ shadowing |= _OFFLOAD_SHADOWING_FLAGS
tokens = [str(a) for a in (args or [])]
out: list[str] = []
diff --git a/studio/backend/main.py b/studio/backend/main.py
index 4797764ce7..81d4c16e52 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -1156,9 +1156,23 @@ def _get_cached_system_gpu_info(logger) -> dict[str, Any]:
enriched_dev["vram_utilization_pct"] = util.get("vram_utilization_pct")
enriched_devices.append(enriched_dev)
+ # Whether GGUF loads accept an explicit gpu_ids pick: /load and
+ # /validate 400 picks on XPU hosts (no visibility mask speaks torch-xpu
+ # ordinals) and on Vulkan-only builds (--device pins ggml's own
+ # ordinals), so the picker must not offer them.
+ try:
+ from core.inference.llama_cpp import LlamaCppBackend
+ from utils.hardware import DeviceType, get_device
+ gpu_ids_supported = (
+ get_device() != DeviceType.XPU and not LlamaCppBackend._is_vulkan_backend()
+ )
+ except Exception as e:
+ logger.debug(f"Could not resolve gpu_ids support: {e}")
+ gpu_ids_supported = True
gpu_info = {
"available": visibility_info.get("available", False),
"devices": enriched_devices,
+ "gguf_gpu_ids_supported": gpu_ids_supported,
}
_system_gpu_cache = (time.monotonic(), gpu_info)
return gpu_info
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index f3ae0f70df..d51d35189b 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -64,7 +64,7 @@ class LoadRequest(BaseModel):
)
gpu_ids: Optional[List[int]] = Field(
None,
- description = "Physical GPU indices to use, for example [0, 1]. Omit or pass [] to use automatic selection. Explicit gpu_ids are unsupported when the parent CUDA_VISIBLE_DEVICES uses UUID/MIG entries. Not supported for GGUF models.",
+ description = "Physical GPU indices to use, for example [0, 1]. Omit or pass [] to use automatic selection. Explicit gpu_ids are unsupported when the parent CUDA_VISIBLE_DEVICES uses UUID/MIG entries. For GGUF models the picked devices are pinned via CUDA/HIP_VISIBLE_DEVICES.",
)
speculative_type: Optional[str] = Field(
None,
@@ -100,6 +100,66 @@ class LoadRequest(BaseModel):
"No effect on a single GPU. Ignored for non-GGUF models."
),
)
+ gpu_memory_mode: Literal["auto", "manual"] = Field(
+ "auto",
+ description = (
+ "GPU memory strategy for GGUF models. 'auto' (default): Unsloth "
+ "selects GPUs and caps context to fit VRAM. 'manual': you own the "
+ "offload. Leave gpu_layers at -1 (Auto) to hand memory management to "
+ "llama.cpp's --fit (no device masking, no context auto-reduce, no "
+ "gpu-layer/tensor-split planning); set gpu_layers >= 0 to pin layers "
+ "and n_cpu_moe yourself (--fit off), with tensor_parallel still "
+ "applying (split by free VRAM unless tensor_split is set, no planner). "
+ "Ignored for non-GGUF."
+ ),
+ )
+ gpu_layers: int = Field(
+ -1,
+ ge = -1,
+ description = (
+ "Manual mode only: number of layers to offload to the GPU "
+ "(--gpu-layers, with --fit off). A value >= the model's layer count "
+ "offloads all of them. -1 = Auto: hand layer + context sizing to "
+ "llama.cpp's --fit. Ignored unless gpu_memory_mode is 'manual'."
+ ),
+ )
+ n_cpu_moe: int = Field(
+ 0,
+ ge = 0,
+ description = (
+ "Manual mode only: keep the first N MoE expert layers on the CPU "
+ "(--n-cpu-moe) to save VRAM on MoE models. 0 = none, N = number of "
+ "MoE layers offloaded (the backend offsets past any leading dense "
+ "layers). Ignored unless gpu_memory_mode is 'manual' with gpu_layers >= 0."
+ ),
+ )
+ tensor_split: Optional[List[float]] = Field(
+ None,
+ description = (
+ "Manual mode only: relative share of the model per GPU (--tensor-split), "
+ "in the order of the GPUs in use, e.g. [2, 1] for 2:1. Omit it to let "
+ "llama.cpp use its default, which splits by free VRAM. Any list given is "
+ "passed through as-is, so send [1, 1] to force an even split. Ignored "
+ "unless gpu_memory_mode is 'manual' with gpu_layers >= 0."
+ ),
+ )
+
+ @field_validator("tensor_split")
+ @classmethod
+ def _reject_degenerate_tensor_split(cls, value: Optional[List[float]]) -> Optional[List[float]]:
+ # A negative / non-finite / all-zero split is silently dropped at launch
+ # (stored as None) yet still compared raw in the reload dedupe, so an
+ # identical Apply reloads forever. Reject it up front; [] = no split.
+ if not value:
+ return value
+ import math
+
+ if any((not math.isfinite(v)) or v < 0 for v in value):
+ raise ValueError("tensor_split entries must be finite and non-negative")
+ if sum(value) <= 0:
+ raise ValueError("tensor_split must have a positive total")
+ return value
+
llama_extra_args: Optional[List[str]] = Field(
None,
description = (
@@ -133,6 +193,14 @@ class ValidateModelRequest(BaseModel):
max_seq_length: int = Field(0, ge = 0, le = 1048576)
load_in_4bit: bool = Field(True)
gpu_ids: Optional[List[int]] = Field(None)
+ gpu_memory_mode: Literal["auto", "manual"] = Field(
+ "auto",
+ description = (
+ "GGUF GPU-memory strategy intended for the follow-up load. Manual "
+ "placement bypasses the training coexistence estimate: Auto layers "
+ "delegate fitting to llama.cpp, while explicit layers are user-owned."
+ ),
+ )
include_context_length: bool = Field(
False,
description = "Also read the native context length from the local GGUF header. "
@@ -188,6 +256,16 @@ class ValidateModelResponse(BaseModel):
description = "Native training context length, read from the GGUF header when the file "
"is already downloaded locally; None for non-GGUF, gated, or not-yet-downloaded models.",
)
+ layer_count: Optional[int] = Field(
+ None,
+ description = "Total layer count (GGUF block_count), the manual gpu-layers ceiling, read "
+ "from the header alongside context_length; None when not read.",
+ )
+ moe_layer_count: Optional[int] = Field(
+ None,
+ description = "MoE expert-layer count (the manual --n-cpu-moe ceiling), read from the GGUF "
+ "header alongside context_length; 0 for dense models, None when not read.",
+ )
# Additive fields; the consuming consent dialog ships in a follow-up frontend PR.
requires_transformers_upgrade: bool = Field(
False,
@@ -333,6 +411,34 @@ class LoadResponse(BaseModel):
False,
description = "Whether tensor-parallel split (--split-mode tensor) is active.",
)
+ gpu_memory_mode: Literal["auto", "manual"] = Field(
+ "auto",
+ description = "Active GPU memory strategy ('auto' or 'manual').",
+ )
+ gpu_layers: int = Field(
+ -1,
+ description = "Manual mode: requested --gpu-layers value (-1 = Auto/--fit, or when not manual).",
+ )
+ n_cpu_moe: int = Field(
+ 0,
+ description = "Manual mode: MoE expert layers pinned to CPU (--n-cpu-moe); 0 = none.",
+ )
+ tensor_split: Optional[List[float]] = Field(
+ None,
+ description = "Manual mode: relative model share per GPU (--tensor-split); None = default (split by free VRAM).",
+ )
+ n_layers: Optional[int] = Field(
+ None,
+ description = "Model's layer count (GGUF block_count), for the manual gpu-layers ceiling.",
+ )
+ n_moe_layers: int = Field(
+ 0,
+ description = "Model's MoE expert-layer count (the n_cpu_moe ceiling); 0 if not an MoE model.",
+ )
+ gpu_ids: Optional[List[int]] = Field(
+ None,
+ description = "Physical GPU indices the model is pinned to, or None for automatic selection.",
+ )
class UnloadResponse(BaseModel):
@@ -461,6 +567,42 @@ class InferenceStatusResponse(BaseModel):
False,
description = "Whether tensor-parallel split (--split-mode tensor) is active.",
)
+ gpu_memory_mode: Literal["auto", "manual"] = Field(
+ "auto",
+ description = "Active GPU memory strategy ('auto' or 'manual').",
+ )
+ gpu_layers: int = Field(
+ -1,
+ description = "Manual mode: requested --gpu-layers value (-1 = Auto/--fit, or when not manual).",
+ )
+ n_cpu_moe: int = Field(
+ 0,
+ description = "Manual mode: MoE expert layers pinned to CPU (--n-cpu-moe); 0 = none.",
+ )
+ tensor_split: Optional[List[float]] = Field(
+ None,
+ description = "Manual mode: relative model share per GPU (--tensor-split); None = default (split by free VRAM).",
+ )
+ requested_context_length: Optional[int] = Field(
+ None,
+ description = (
+ "The n_ctx the active GGUF load was invoked with (0 = Auto). Lets the "
+ "UI re-seed a Manual + Auto-layers context pin on hydration, where "
+ "context_length only exposes the resolved value. None for non-GGUF."
+ ),
+ )
+ n_layers: Optional[int] = Field(
+ None,
+ description = "Model's layer count (GGUF block_count), for the manual gpu-layers ceiling.",
+ )
+ n_moe_layers: int = Field(
+ 0,
+ description = "Model's MoE expert-layer count (the n_cpu_moe ceiling); 0 if not an MoE model.",
+ )
+ gpu_ids: Optional[List[int]] = Field(
+ None,
+ description = "Physical GPU indices the model is pinned to, or None for automatic selection.",
+ )
llama_cpp_supports_mtp: bool = Field(
True,
description = (
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 3d527bf317..136e4f7645 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -13,7 +13,7 @@ from pathlib import Path
from fastapi import APIRouter, Depends, HTTPException, Request, status
from fastapi.responses import StreamingResponse, JSONResponse, Response
from starlette.requests import ClientDisconnect
-from typing import Any, Callable, List, Optional, Union
+from typing import Any, Callable, List, Literal, Optional, Union
import json
import httpx
from loggers import get_logger
@@ -3115,13 +3115,16 @@ def _normalise_settings_str(value: Optional[str]) -> Optional[str]:
def _should_strip_split_mode(request: LoadRequest, backend_extra: Optional[list[str]]) -> bool:
- """Whether an inherited --split-mode should be stripped on reload.
+ """Whether an inherited --split-mode (and its coupled --tensor-split) should
+ be stripped on reload.
The binary Tensor Parallelism toggle can't carry --split-mode's row/none/
layer modes, so only strip when the toggle overrides it: tensor being turned
on, or the inherited mode is tensor (toggle turning it off). Non-tensor modes
- survive. Shared by the inheritance strip and the already-loaded stale check
- so they agree on what reload would do.
+ survive. A manual per-GPU ratio is handled by _should_strip_tensor_split,
+ which strips only --tensor-split so the inherited mode is kept. Shared by the
+ inheritance strip and the already-loaded stale check so they agree on what
+ reload would do.
"""
fields_set = getattr(request, "model_fields_set", set())
return "tensor_parallel" in fields_set and (
@@ -3129,6 +3132,25 @@ def _should_strip_split_mode(request: LoadRequest, backend_extra: Optional[list[
)
+def _should_strip_tensor_split(request: LoadRequest) -> bool:
+ """Whether an inherited --tensor-split alone should be stripped on reload.
+
+ Manual explicit offload (gpu_layers >= 0) owns the per-GPU split: with a ratio
+ it emits its own --tensor-split (an inherited one, appended last, would
+ override it), and with the ratio cleared it wants llama.cpp's default
+ free-VRAM split. Either way an inherited --tensor-split must go, else the
+ cleared case silently keeps the stale ratio while status reports None.
+ Unlike _should_strip_split_mode this leaves --split-mode untouched, so a
+ user's row/none/layer mode survives a Studio split-ratio edit. When the
+ Tensor Parallelism toggle IS overriding the mode, _should_strip_split_mode
+ (called alongside this at every site) strips --split-mode anyway.
+ """
+ return (
+ getattr(request, "gpu_memory_mode", "auto") == "manual"
+ and getattr(request, "gpu_layers", -1) >= 0
+ )
+
+
def _carry_preserved_tensor_intent(
*, preserved: bool, same_model: bool, explicit_drop: bool
) -> bool:
@@ -3187,12 +3209,44 @@ def _request_matches_loaded_settings(
else strip_shadowing_flags(
backend_extra,
strip_split_mode = _should_strip_split_mode(request, backend_extra),
+ strip_tensor_split = _should_strip_tensor_split(request),
+ strip_offload = request.gpu_memory_mode == "manual",
)
)
if not _tensor_parallel_matches_loaded(
effective_extra, request.tensor_parallel, llama_backend.tensor_parallel
):
return False
+ # The diffusion runner is mode-agnostic (it always reports "auto" and ignores
+ # the layer/MoE/split knobs), so a standing manual preference in the request
+ # must not force a needless reload -- only the GPU pick matters.
+ if not llama_backend.is_diffusion:
+ if request.gpu_memory_mode != llama_backend.gpu_memory_mode:
+ return False
+ # Manual: a layer-count change always reloads; MoE/split only matter with
+ # an explicit offload (gpu_layers >= 0), so a leftover value under Auto
+ # must not force one. Mirrors LlamaCppBackend._already_in_target_state.
+ if request.gpu_memory_mode == "manual" and (
+ request.gpu_layers != llama_backend.gpu_layers
+ or (
+ request.gpu_layers >= 0
+ and (
+ request.n_cpu_moe != llama_backend.n_cpu_moe
+ or (request.tensor_split or None) != (llama_backend.tensor_split or None)
+ )
+ )
+ ):
+ return False
+ # A changed GPU pick must reload. The diffusion runner collapses a multi-GPU
+ # request to its single lowest device (it drives one device only), so the
+ # backend records just that device; compare the request the same way, or a
+ # multi-GPU pick that resolves to the same device needlessly reloads.
+ if llama_backend.is_diffusion:
+ _req_gpu_ids = [sorted(request.gpu_ids)[0]] if request.gpu_ids else None
+ else:
+ _req_gpu_ids = sorted(request.gpu_ids) if request.gpu_ids else None
+ if _req_gpu_ids != llama_backend.gpu_ids:
+ return False
# Preserved tensor->layer fallback (both report tensor=off, so the check above
# matches): if the user now explicitly drops tensor intent, reload so placement
# re-selects instead of keeping the all-GPU mask (#6659). The effective check
@@ -3235,14 +3289,17 @@ def _request_matches_loaded_settings(
# contain any shadow flag, so the reload path strips them rather than
# leaving a stale override in effect. (backend_extra computed above.)
if request.llama_extra_args is None:
- # Mirror the reload's conditional split-mode strip, so a preserved
- # non-tensor mode (row/none/layer) isn't seen as stale and doesn't
- # trigger a needless reload of a healthy server.
+ # Mirror the reload's conditional strips, so a preserved non-tensor mode
+ # (row/none/layer) isn't seen as stale and doesn't trigger a needless
+ # reload of a healthy server, while an inherited offload/ratio flag that
+ # the reload *would* strip is correctly seen as stale.
if (
backend_extra
and strip_shadowing_flags(
backend_extra,
strip_split_mode = _should_strip_split_mode(request, backend_extra),
+ strip_tensor_split = _should_strip_tensor_split(request),
+ strip_offload = request.gpu_memory_mode == "manual",
)
!= backend_extra
):
@@ -3861,6 +3918,46 @@ def _estimate_gguf_required_gb(
return None
+def _classify_diffusion_gguf(config: ModelConfig) -> Optional[bool]:
+ """Classify a GGUF as diffusion, normal, or unknown before it is loaded.
+
+ ``None`` is important here: a remote GGUF whose header is not cached can
+ still be routed to the single-GPU diffusion runner after download. Treating
+ that case as normal would let Manual mode skip the training guard even
+ though the runner ignores Manual's llama-server placement controls.
+ """
+ identity = " ".join(
+ str(getattr(config, attr, "") or "") for attr in ("identifier", "gguf_hf_repo", "gguf_file")
+ ).lower()
+ if "diffusion" in identity:
+ return True
+
+ try:
+ main = getattr(config, "gguf_file", None)
+ if not (main and Path(main).is_file()):
+ repo = getattr(config, "gguf_hf_repo", None)
+ variant = getattr(config, "gguf_variant", None)
+ if repo and variant:
+ from hub.utils.gguf import resolve_local_gguf_path
+ main = resolve_local_gguf_path(repo, variant)
+ if not main or not Path(main).is_file():
+ return None
+
+ probe = LlamaCppBackend()
+ probe._read_gguf_metadata(str(main))
+ if probe.is_diffusion:
+ return True
+ # A successfully decoded architecture proves that this is a normal
+ # llama-server GGUF. No architecture means the lightweight probe could
+ # not establish the routing decision, so preserve the unknown state.
+ if getattr(probe, "_architecture", None):
+ return False
+ return None
+ except Exception as e:
+ logger.debug("Could not identify diffusion GGUF for training guard: %s", e)
+ return None
+
+
def _guard_chat_load_against_training(
config: ModelConfig,
*,
@@ -3871,11 +3968,19 @@ def _guard_chat_load_against_training(
requested_gpu_ids: Optional[List[int]],
llama_extra_args: Optional[list[str]] = None,
n_parallel: int = 1,
+ gpu_memory_mode: Literal["auto", "manual"] = "auto",
) -> None:
- """Refuse loading a local chat model that would OOM an active training run.
+ """Protect active training from automatically placed chat-model loads.
+
No-op when training is inactive or unknown. `load_in_4bit` must be the
- effective quantization (see _effective_load_in_4bit). Raises HTTP 409 when the
- model would not fit alongside training."""
+ effective quantization (see _effective_load_in_4bit). Manual chat-GGUF
+ placement is an explicit override: Auto layers delegate fitting to
+ llama.cpp's ``--fit`` and pinned layers are owned by the user, so neither is
+ estimated here. Diffusion is still guarded because its mode-agnostic runner
+ ignores those controls and uses one GPU. An unclassified GGUF is guarded as
+ potentially diffusion until its local header proves otherwise. Other loads
+ raise HTTP 409 when they would not fit beside training.
+ """
from core.training import get_training_backend
from routes.training_vram import can_load_chat_during_training
@@ -3887,6 +3992,19 @@ def _guard_chat_load_against_training(
return
is_gguf = bool(getattr(config, "is_gguf", False))
+ diffusion_kind = _classify_diffusion_gguf(config) if is_gguf else False
+ if is_gguf and gpu_memory_mode == "manual" and diffusion_kind is False:
+ return
+
+ diffusion_gpu = None
+ if is_gguf and diffusion_kind is not False:
+ # Use the same token selection as the runner: an explicit pick wins,
+ # followed by DG_GPU, the first parent-visible token, then GPU 0.
+ diffusion_gpu = LlamaCppBackend._diffusion_gpu_arg(
+ requested_gpu_ids,
+ cpu_only = LlamaCppBackend._effective_gpu_count() == 0,
+ )
+
required_override_gb = (
_estimate_gguf_required_gb(
config,
@@ -3907,6 +4025,7 @@ def _guard_chat_load_against_training(
requested_gpu_ids = requested_gpu_ids,
is_gguf = is_gguf,
required_override_gb = required_override_gb,
+ single_device_gpu = diffusion_gpu,
)
if ok:
return
@@ -3934,6 +4053,98 @@ def _guard_chat_load_against_training(
raise HTTPException(status_code = 409, detail = detail)
+def _resolve_inherited_extra_args(
+ request,
+ config: ModelConfig,
+ model_identifier: str,
+ extra_llama_args: Optional[list[str]],
+ effective_chat_template_override: Optional[str] = None,
+) -> Optional[list[str]]:
+ """Effective pass-through extras for a GGUF request that omitted the field:
+ the previous same-model load's extras, shadow-stripped, so a settings-Apply
+ reload (which does not round-trip the extras field) keeps them (#5401)."""
+ if getattr(request, "llama_extra_args", None) is not None:
+ return extra_llama_args
+ if not getattr(config, "is_gguf", False):
+ return extra_llama_args
+ llama_backend = get_llama_cpp_backend()
+ if not llama_backend.extra_args:
+ return extra_llama_args
+ # Inherit the previous load's extras (the chat-settings Apply path doesn't
+ # round-trip them; an explicit [] still clears). Gated on (model_identifier,
+ # hf_variant) to refuse cross-model pickup, and shadowing flags are
+ # stripped so an inherited override can't win the last-wins CLI
+ # parse against a freshly-supplied first-class field.
+ source = llama_backend.extra_args_source
+ # Compare against the resolved variant, not the request field: callers
+ # commonly omit gguf_variant for local ``.gguf`` paths and HF auto-pick
+ # flows. ``config.gguf_variant`` is the variant load_model was actually
+ # invoked with, so both sides of the comparison key off the same string.
+ resolved_variant = (config.gguf_variant or "").lower()
+ request_variant = (request.gguf_variant or "").lower()
+ stored_variant = (source[1] or "").lower() if source else ""
+ same_model = bool(source and source[0] and source[0].lower() == model_identifier.lower())
+ if request.gguf_variant:
+ variant_mismatch = request_variant != stored_variant
+ else:
+ variant_mismatch = bool(stored_variant and resolved_variant != stored_variant)
+ same_source = same_model and not variant_mismatch
+ if not same_source:
+ logger.info(
+ "Not inheriting llama_extra_args: stored args came from %s, loading %s",
+ source,
+ (model_identifier, resolved_variant),
+ )
+ # Cross-model: clear explicitly so the backend doesn't
+ # inherit via "no opinion" semantics.
+ extra_llama_args = []
+ else:
+ # Strip only the groups whose first-class field was set by the caller, so
+ # an inherited --chat-template-file survives an Apply that omits
+ # chat_template_override. A bundled family template (e.g. gemma-4) counts as
+ # a first-class template even when the request omits chat_template_override,
+ # so strip the inherited --chat-template-file then too -- else the stale arg
+ # (appended last) shadows the bundled template while Studio reports its caps.
+ fields_set = getattr(request, "model_fields_set", set())
+ stripped = strip_shadowing_flags(
+ llama_backend.extra_args,
+ strip_context = "max_seq_length" in fields_set,
+ strip_cache = "cache_type_kv" in fields_set,
+ strip_spec = ("speculative_type" in fields_set or "spec_draft_n_max" in fields_set),
+ strip_template = (
+ "chat_template_override" in fields_set
+ or effective_chat_template_override is not None
+ ),
+ strip_split_mode = _should_strip_split_mode(request, llama_backend.extra_args),
+ # manual + per-GPU ratio emits its own --tensor-split; drop
+ # an inherited one (appended last would override it) while
+ # keeping the user's --split-mode row/none/layer choice.
+ strip_tensor_split = _should_strip_tensor_split(request),
+ # manual emits its own --fit/--gpu-layers, so an inherited offload flag
+ # must not last-wins-override it. auto leaves a user's inherited -ngl
+ # alone. getattr: a validate request reuses this resolver, no offload fields.
+ strip_offload = getattr(request, "gpu_memory_mode", "auto") == "manual",
+ )
+ try:
+ extra_llama_args = validate_extra_args(stripped)
+ except ValueError:
+ # Shouldn't happen on already-validated args; degrade to
+ # no-extras rather than 400 if managed flags changed.
+ logger.warning(
+ "Stored llama_extra_args failed revalidation; loading without them: %s",
+ stripped,
+ )
+ extra_llama_args = []
+ else:
+ if extra_llama_args:
+ logger.info(
+ "Inheriting llama_extra_args from previous "
+ "load (same model, shadow-stripped): %s",
+ extra_llama_args,
+ )
+ return extra_llama_args
+
+
def _model_json_response(model, status_code: int = 200) -> Response:
"""Serialize a pydantic response once via pydantic-core.
@@ -4040,6 +4251,35 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
None if request.llama_extra_args is None else extra_llama_args
)
+ # Manual mode owns the offload flags: strip them from EXPLICIT extras
+ # too (the inherited path already does), or a last-wins --gpu-layers /
+ # --fit in extras re-enables GPU offload on a load status reports as
+ # CPU-only. Manual + per-GPU ratio owns --tensor-split the same way.
+ if request.gpu_memory_mode == "manual" and extra_llama_args:
+ _stripped_explicit = strip_shadowing_flags(
+ extra_llama_args,
+ strip_context = False,
+ strip_cache = False,
+ strip_spec = False,
+ strip_template = False,
+ strip_split_mode = False,
+ strip_tensor_split = _should_strip_tensor_split(request),
+ strip_offload = True,
+ )
+ if _stripped_explicit != extra_llama_args:
+ logger.info(
+ "Manual GPU memory owns the offload flags; stripping them "
+ "from explicit llama_extra_args: %s -> %s",
+ extra_llama_args,
+ _stripped_explicit,
+ )
+ extra_llama_args = _stripped_explicit
+
+ # Keep every downstream consumer on the normalized explicit list. In
+ # particular, the already-loaded comparator must not compare the raw
+ # request's managed offload flags against the stripped launch state.
+ request = request.model_copy(update = {"llama_extra_args": extra_llama_args})
+
model_identifier, model_log_label, native_grant_backed = (
_resolve_model_identifier_for_request(request, operation = "load-model")
)
@@ -4121,6 +4361,13 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
speculative_type = llama_backend.requested_spec_mode,
spec_draft_n_max = llama_backend.spec_draft_n_max,
tensor_parallel = llama_backend.tensor_parallel,
+ gpu_memory_mode = llama_backend.gpu_memory_mode,
+ gpu_layers = llama_backend.gpu_layers,
+ n_cpu_moe = llama_backend.n_cpu_moe,
+ tensor_split = llama_backend.tensor_split,
+ n_layers = llama_backend.n_layers,
+ n_moe_layers = llama_backend.n_moe_layers,
+ gpu_ids = llama_backend.gpu_ids,
)
else:
if (
@@ -4187,12 +4434,41 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
# Normalize gpu_ids: empty list means auto-selection, same as None
effective_gpu_ids = request.gpu_ids if request.gpu_ids else None
- # Reject GGUF + gpu_ids first so the guard can't mask it with a VRAM 409.
+ # GGUF supports gpu_ids: validate the pick up front (before the training
+ # guard) so a bad pick is a clean 400, not masked by a VRAM 409. Rejects
+ # negative / out-of-range / duplicate ids and UUID/MIG parents. XPU hosts
+ # are rejected outright: the picker's indices are torch-xpu ordinals neither
+ # applicator speaks (CUDA/HIP masks don't apply, the Vulkan --device pin
+ # uses ggml's own Vulkan ordinals), so a pick could land on the wrong device.
if config.is_gguf and effective_gpu_ids is not None:
- raise HTTPException(
- status_code = 400,
- detail = "gpu_ids is not supported for GGUF models yet.",
- )
+ from utils.hardware import DeviceType, get_device
+ from utils.hardware.hardware import resolve_requested_gpu_ids
+
+ if get_device() == DeviceType.XPU:
+ raise HTTPException(
+ status_code = 400,
+ detail = (
+ "GPU selection (gpu_ids) is not supported on Intel XPU. "
+ "Omit gpu_ids to use all devices."
+ ),
+ )
+ # Same reasoning for a Vulkan-only build: --device pins ggml's own
+ # Vulkan ordinals, so a physical pick can land on the wrong card on
+ # masked or non-contiguous hosts.
+ if LlamaCppBackend._is_vulkan_backend():
+ raise HTTPException(
+ status_code = 400,
+ detail = (
+ "GPU selection (gpu_ids) is not supported with a Vulkan "
+ "llama.cpp build: physical GPU ids have no defined "
+ "mapping to Vulkan device ordinals. Omit gpu_ids to use "
+ "all devices."
+ ),
+ )
+ try:
+ resolve_requested_gpu_ids(effective_gpu_ids)
+ except ValueError as exc:
+ raise HTTPException(status_code = 400, detail = str(exc)) from exc
if not config.is_gguf and _mlx_distributed_launch_detected():
raise HTTPException(
status_code = 400,
@@ -4222,8 +4498,20 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
"architectures)"
)
- # Refuse a load that would OOM active training, before the unload step below
- # frees the resident model. Off-loop: guard does sync nvidia-smi / HF work.
+ # Inherit the previous same-model load's pass-through extras when this
+ # request omits the field (a settings-Apply reload doesn't round-trip
+ # them); shadow-stripped so an inherited flag can't override a
+ # first-class field the caller did set (#5401).
+ extra_llama_args = _resolve_inherited_extra_args(
+ request,
+ config,
+ model_identifier,
+ extra_llama_args,
+ effective_chat_template_override,
+ )
+
+ # Apply the training coexistence policy before the unload step below
+ # frees the resident model. Off-loop: the default-mode guard does sync work.
await asyncio.to_thread(
_guard_chat_load_against_training,
config,
@@ -4234,6 +4522,7 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
requested_gpu_ids = effective_gpu_ids,
llama_extra_args = extra_llama_args,
n_parallel = getattr(fastapi_request.app.state, "llama_parallel_slots", 1),
+ gpu_memory_mode = request.gpu_memory_mode,
)
# ── GGUF path: load via llama-server ──────────────────────
@@ -4245,84 +4534,6 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
from core.inference.llama_cpp import gguf_load_in_flight
gguf_load_stack.enter_context(gguf_load_in_flight(config.gguf_hf_repo))
- # Inherit llama_extra_args from the previous load when the request
- # omits the field (the chat-settings Apply path doesn't round-trip
- # them; explicit [] still clears). Gated on (model_identifier,
- # hf_variant) to refuse cross-model pickup, and shadowing flags are
- # stripped so an inherited override can't win the last-wins CLI
- # parse against a freshly-supplied first-class field.
- if request.llama_extra_args is None and llama_backend.extra_args:
- source = llama_backend.extra_args_source
- # Compare against the resolved variant, not the request
- # field: callers commonly omit gguf_variant for local
- # ``.gguf`` paths and HF auto-pick flows. ``config.gguf_
- # variant`` is the variant load_model was actually
- # invoked with (see the HF / local branches below), so
- # both sides of the comparison key off the same string.
- resolved_variant = (config.gguf_variant or "").lower()
- request_variant = (request.gguf_variant or "").lower()
- stored_variant = (source[1] or "").lower() if source else ""
- same_model = bool(
- source and source[0] and source[0].lower() == model_identifier.lower()
- )
- if request.gguf_variant:
- variant_mismatch = request_variant != stored_variant
- else:
- variant_mismatch = bool(stored_variant and resolved_variant != stored_variant)
- same_source = same_model and not variant_mismatch
- if not same_source:
- logger.info(
- "Not inheriting llama_extra_args: stored args came from %s, loading %s",
- source,
- (model_identifier, resolved_variant),
- )
- # Cross-model: clear explicitly so the backend doesn't
- # inherit via "no opinion" semantics.
- extra_llama_args = []
- else:
- # Strip only the groups whose first-class field was set by
- # the caller, so an inherited --chat-template-file survives
- # an Apply that omits chat_template_override. A bundled family
- # template (e.g. the gemma-4 override) is an effective
- # first-class template setting even when the raw request
- # omits chat_template_override, so strip the inherited
- # --chat-template-file in that case too -- otherwise the stale
- # extra arg (appended last) shadows the bundled template while
- # Unsloth reports the bundled template's capabilities.
- fields_set = getattr(request, "model_fields_set", set())
- stripped = strip_shadowing_flags(
- llama_backend.extra_args,
- strip_context = "max_seq_length" in fields_set,
- strip_cache = "cache_type_kv" in fields_set,
- strip_spec = (
- "speculative_type" in fields_set or "spec_draft_n_max" in fields_set
- ),
- strip_template = (
- "chat_template_override" in fields_set
- or effective_chat_template_override is not None
- ),
- strip_split_mode = _should_strip_split_mode(
- request, llama_backend.extra_args
- ),
- )
- try:
- extra_llama_args = validate_extra_args(stripped)
- except ValueError:
- # Shouldn't happen on already-validated args; degrade to
- # no-extras rather than 400 if managed flags changed.
- logger.warning(
- "Stored llama_extra_args failed revalidation; loading without them: %s",
- stripped,
- )
- extra_llama_args = []
- else:
- if extra_llama_args:
- logger.info(
- "Inheriting llama_extra_args from previous "
- "load (same model, shadow-stripped): %s",
- extra_llama_args,
- )
-
# Block cache writes that would race the download manager. This runs
# after pass-through argument inheritance so a carried --no-mmproj
# changes the companion requirement exactly as it does for the load.
@@ -4370,6 +4581,11 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
cache_type_kv = request.cache_type_kv,
speculative_type = request.speculative_type,
spec_draft_n_max = request.spec_draft_n_max,
+ gpu_memory_mode = request.gpu_memory_mode,
+ gpu_layers = request.gpu_layers,
+ n_cpu_moe = request.n_cpu_moe,
+ tensor_split = request.tensor_split,
+ gpu_ids = effective_gpu_ids,
n_parallel = _n_parallel,
)
if config.gguf_hf_repo:
@@ -4537,6 +4753,13 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
speculative_type = llama_backend.requested_spec_mode,
spec_draft_n_max = llama_backend.spec_draft_n_max,
tensor_parallel = llama_backend.tensor_parallel,
+ gpu_memory_mode = llama_backend.gpu_memory_mode,
+ gpu_layers = llama_backend.gpu_layers,
+ n_cpu_moe = llama_backend.n_cpu_moe,
+ tensor_split = llama_backend.tensor_split,
+ n_layers = llama_backend.n_layers,
+ n_moe_layers = llama_backend.n_moe_layers,
+ gpu_ids = llama_backend.gpu_ids,
)
# ── Standard path: load via Unsloth/transformers ──────────
@@ -4795,7 +5018,9 @@ def _requires_security_review_for_model(
@router.post("/validate", response_model = ValidateModelResponse)
async def validate_model(
- request: ValidateModelRequest, current_subject: str = Depends(get_current_subject)
+ request: ValidateModelRequest,
+ fastapi_request: Request = None,
+ current_subject: str = Depends(get_current_subject),
):
"""
Lightweight validation endpoint for model identifiers.
@@ -4823,15 +5048,39 @@ async def validate_model(
detail = f"Invalid model identifier: {model_log_label}",
)
- # Refuse early (before the frontend unloads to load this) if it can't fit
- # alongside training, using the same settings /load uses so they agree.
+ # Apply the same training coexistence policy as /load before the frontend
+ # unloads the current model.
effective_gpu_ids = request.gpu_ids if request.gpu_ids else None
- # Mirror /load: reject GGUF + gpu_ids before the guard so both return 400.
+ # Mirror /load: GGUF supports gpu_ids, so validate the pick (a bad one is
+ # a clean 400) before the guard sizes the model against training VRAM.
+ # XPU-host picks are rejected like /load (no defined mapping from the
+ # picker's torch-xpu ordinals to the launcher's device spaces).
if config.is_gguf and effective_gpu_ids is not None:
- raise HTTPException(
- status_code = 400,
- detail = "gpu_ids is not supported for GGUF models yet.",
- )
+ from utils.hardware import DeviceType, get_device
+ from utils.hardware.hardware import resolve_requested_gpu_ids
+
+ if get_device() == DeviceType.XPU:
+ raise HTTPException(
+ status_code = 400,
+ detail = (
+ "GPU selection (gpu_ids) is not supported on Intel XPU. "
+ "Omit gpu_ids to use all devices."
+ ),
+ )
+ if LlamaCppBackend._is_vulkan_backend():
+ raise HTTPException(
+ status_code = 400,
+ detail = (
+ "GPU selection (gpu_ids) is not supported with a Vulkan "
+ "llama.cpp build: physical GPU ids have no defined "
+ "mapping to Vulkan device ordinals. Omit gpu_ids to use "
+ "all devices."
+ ),
+ )
+ try:
+ resolve_requested_gpu_ids(effective_gpu_ids)
+ except ValueError as exc:
+ raise HTTPException(status_code = 400, detail = str(exc)) from exc
effective_load_in_4bit = _effective_load_in_4bit(config, request.load_in_4bit)
# Both checks cover the [adapter, base] set (matching the scan route and workers):
@@ -4895,16 +5144,32 @@ async def validate_model(
latest_tier_active_for, config.identifier, request.hf_token
):
effective_load_in_4bit = False
- # Off-loop: guard does sync nvidia-smi / HF work.
- await asyncio.to_thread(
- _guard_chat_load_against_training,
- config,
- model_identifier = model_identifier,
- hf_token = request.hf_token,
- load_in_4bit = effective_load_in_4bit,
- max_seq_length = request.max_seq_length,
- requested_gpu_ids = effective_gpu_ids,
- )
+ # A metadata-only probe just reads the GGUF header and allocates no VRAM,
+ # so it must not be refused by the training guard. Real loads validate
+ # without include_context_length and /load applies the guard again.
+ if not request.include_context_length:
+ # Match /load's inherited llama.cpp extras and parallel slot count so
+ # validation cannot pass a smaller estimate than the subsequent load.
+ effective_extra_args = _resolve_inherited_extra_args(
+ request, config, model_identifier, None
+ )
+ # Off-loop: guard does sync nvidia-smi / HF work.
+ await asyncio.to_thread(
+ _guard_chat_load_against_training,
+ config,
+ model_identifier = model_identifier,
+ hf_token = request.hf_token,
+ load_in_4bit = effective_load_in_4bit,
+ max_seq_length = request.max_seq_length,
+ requested_gpu_ids = effective_gpu_ids,
+ llama_extra_args = effective_extra_args,
+ n_parallel = (
+ getattr(fastapi_request.app.state, "llama_parallel_slots", 1)
+ if fastapi_request is not None
+ else 1
+ ),
+ gpu_memory_mode = request.gpu_memory_mode,
+ )
# A selected GGUF loads via llama.cpp: auto_map Python and root pickle weights in a
# mixed repo are inert for this load, so gating on them is a false positive. Only
@@ -4918,10 +5183,15 @@ async def validate_model(
# Native context length, read from the local GGUF header when present.
# Lets the staged ("Load on selection" off) flow populate the context
# slider before the GPU load; None until the file is downloaded.
+ # Staged header dims (one read): native context, total layer count, and
+ # MoE expert-layer count -- let the staged flow size the context, GPU-
+ # layers and manual --n-cpu-moe sliders before the load.
context_length: Optional[int] = None
+ layer_count: Optional[int] = None
+ moe_layer_count: Optional[int] = None
if request.include_context_length and is_gguf:
from hub.utils.gguf import resolve_local_gguf_path
- from utils.models.gguf_metadata import read_gguf_context_length
+ from utils.models.gguf_metadata import read_gguf_staged_dims
# Best-effort: a header-read failure must never fail validation of an
# otherwise-valid model (the outer except turns it into a 400).
@@ -4937,9 +5207,15 @@ async def validate_model(
model_identifier, request.gguf_variant
)
if local_gguf:
- context_length = read_gguf_context_length(local_gguf)
+ # Header walk reads tokenizer arrays for dense models (tens of
+ # ms); keep it off the event loop.
+ dims = await asyncio.to_thread(read_gguf_staged_dims, local_gguf)
+ if dims:
+ context_length = dims["context_length"]
+ layer_count = dims["layer_count"]
+ moe_layer_count = dims["moe_layer_count"]
except Exception as e:
- logger.debug("Context-length probe failed for %s: %s", model_log_label, e)
+ logger.debug("Header probe failed for %s: %s", model_log_label, e)
return ValidateModelResponse(
valid = True,
@@ -4954,6 +5230,8 @@ async def validate_model(
requires_trust_remote_code = requires_trust_remote_code,
requires_security_review = requires_security_review,
context_length = context_length,
+ layer_count = layer_count,
+ moe_layer_count = moe_layer_count,
requires_transformers_upgrade = transformers_upgrade is not None,
transformers_upgrade = transformers_upgrade,
)
@@ -5593,6 +5871,14 @@ async def get_status(current_subject: str = Depends(get_current_subject)):
speculative_type = llama_backend.requested_spec_mode,
spec_draft_n_max = llama_backend.spec_draft_n_max,
tensor_parallel = llama_backend.tensor_parallel,
+ gpu_memory_mode = llama_backend.gpu_memory_mode,
+ gpu_layers = llama_backend.gpu_layers,
+ n_cpu_moe = llama_backend.n_cpu_moe,
+ tensor_split = llama_backend.tensor_split,
+ requested_context_length = llama_backend.requested_n_ctx,
+ n_layers = llama_backend.n_layers,
+ n_moe_layers = llama_backend.n_moe_layers,
+ gpu_ids = llama_backend.gpu_ids,
llama_cpp_supports_mtp = _supports_mtp,
spec_fallback_reason = llama_backend.spec_fallback_reason,
llama_cpp_prebuilt_stale = _stale,
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index bb321695cd..0806c2f513 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -2731,7 +2731,11 @@ async def get_gguf_variants(
],
has_vision = response.has_vision,
default_variant = response.default_variant,
- context_length = _read_native_context_length(repo_id, is_local = local),
+ # The header walk reads tokenizer arrays on dense models (tens of
+ # ms per uncached file); keep it off the event loop.
+ context_length = await asyncio.to_thread(
+ _read_native_context_length, repo_id, is_local = local
+ ),
)
except HTTPException:
raise
diff --git a/studio/backend/routes/training_vram.py b/studio/backend/routes/training_vram.py
index fb361d3359..fd96fe2175 100644
--- a/studio/backend/routes/training_vram.py
+++ b/studio/backend/routes/training_vram.py
@@ -197,15 +197,18 @@ def can_load_chat_during_training(
requested_gpu_ids: Optional[List[int]],
is_gguf: bool = False,
required_override_gb: Optional[float] = None,
+ single_device_gpu: Optional[str] = None,
) -> Tuple[bool, Dict[str, Any]]:
"""Decide if a NEW chat model can load without OOMing active training (inverse
of can_keep_chat_during_training: training is already resident, so size the
chat model against the free VRAM that remains). Sizes/places it the same way
the loader will: HF auto reuses auto_select_gpu_ids; HF explicit requires an
even-share per-GPU floor for device_map="balanced"; GGUF sizes from
- required_override_gb over the visible pool. `load_in_4bit` must be effective
- (LoRA can flip 4-bit -> 16-bit). Non-CUDA allows the load; default-deny on any
- CUDA case it can't size, so a load never OOMs training."""
+ required_override_gb over the visible pool. ``single_device_gpu`` is the
+ exact physical device token selected by a single-device runner.
+ `load_in_4bit` must be effective (LoRA can flip 4-bit -> 16-bit). Non-CUDA
+ allows the load; default-deny on any CUDA case it can't size, so a load never
+ OOMs training."""
try:
from utils.hardware import (
DeviceType,
@@ -251,26 +254,49 @@ def can_load_chat_during_training(
}
# Explicit GPUs, or GGUF: size directly and check live free VRAM.
+ if single_device_gpu is not None:
+ mode = "single_device"
+ elif is_gguf:
+ mode = "gguf"
+ else:
+ mode = "explicit"
required_gb = required_override_gb
if required_gb is None:
required_gb, _meta = estimate_required_model_memory_gb(model_name, **est_kwargs)
if required_gb is None:
- mode = "explicit" if requested_gpu_ids else "gguf"
return False, {"mode": mode, "reason": "estimate_unavailable"}
free_by_index = _free_vram_by_index(get_visible_gpu_utilization().get("devices", []))
- if requested_gpu_ids:
+ if single_device_gpu is not None:
+ token = str(single_device_gpu).strip()
+ if not token:
+ # Empty token = a CPU-only single-device runner (e.g. a CPU
+ # diffusion GGUF): it uses no GPU VRAM, so it never threatens
+ # active training and can always load.
+ return True, {"mode": "single_device", "reason": "cpu_only"}
+ try:
+ selected_gpu = int(token)
+ if selected_gpu < 0:
+ raise ValueError
+ except (TypeError, ValueError):
+ # A non-numeric device token (e.g. a CUDA UUID / MIG handle)
+ # can't be mapped to a free-VRAM index, but the runner still
+ # drives ONE device. Size against the worst-case visible device
+ # (min free), never the aggregate pool, so a single-device load
+ # is never OK'd on capacity it can't use and OOMs training.
+ free_vals = [min(free_by_index.values())] if free_by_index else []
+ else:
+ free_vals = [free_by_index.get(selected_gpu, 0.0)]
+ elif requested_gpu_ids:
# Invalid ids -> load_model 400s first, so don't block; missing id = 0.
try:
resolved = resolve_requested_gpu_ids(requested_gpu_ids)
except ValueError:
- return True, {"mode": "explicit", "reason": "invalid_gpu_ids"}
+ return True, {"mode": mode, "reason": "invalid_gpu_ids"}
free_vals = [free_by_index.get(i, 0.0) for i in resolved]
- mode = "explicit"
else:
# GGUF: llama.cpp picks the GPU(s); any visible GPU is a candidate.
free_vals = list(free_by_index.values())
- mode = "gguf"
if not free_vals:
return False, {"mode": mode, "reason": "no_visible_gpus"}
diff --git a/studio/backend/tests/test_chat_load_during_training.py b/studio/backend/tests/test_chat_load_during_training.py
index 63dba8579c..7daa4224aa 100644
--- a/studio/backend/tests/test_chat_load_during_training.py
+++ b/studio/backend/tests/test_chat_load_during_training.py
@@ -168,11 +168,14 @@ class TestCanLoadGGUF(_GpuCacheResetMixin, unittest.TestCase):
devices,
required_override = None,
estimate = None,
+ single_device_gpu = None,
+ gpu_ids = None,
):
with (
patch("utils.hardware.get_device", return_value = DeviceType.CUDA),
patch("utils.hardware.estimate_required_model_memory_gb", return_value = (estimate, {})),
patch("utils.hardware.get_visible_gpu_utilization", return_value = {"devices": devices}),
+ patch("utils.hardware.resolve_requested_gpu_ids", return_value = gpu_ids),
patch("utils.hardware.auto_select_gpu_ids") as auto_mock,
):
ok, info = tv.can_load_chat_during_training(
@@ -180,9 +183,10 @@ class TestCanLoadGGUF(_GpuCacheResetMixin, unittest.TestCase):
hf_token = None,
load_in_4bit = True,
max_seq_length = 0,
- requested_gpu_ids = None,
+ requested_gpu_ids = gpu_ids,
is_gguf = True,
required_override_gb = required_override,
+ single_device_gpu = single_device_gpu,
)
return ok, info, auto_mock
@@ -198,6 +202,88 @@ class TestCanLoadGGUF(_GpuCacheResetMixin, unittest.TestCase):
ok, _, _ = self._run(devices = _devices((0, 80, 35), (1, 80, 70)), required_override = 20.0)
self.assertTrue(ok)
+ def test_no_per_gpu_floor_for_gguf_with_explicit_gpu_ids(self):
+ # gpu_ids narrows llama.cpp's candidate pool but does not turn its
+ # self-placement into HF device_map="balanced". The uneven selected
+ # pair therefore keeps the aggregate GGUF check without an even-share
+ # floor on the nearly-full card.
+ ok, info, _ = self._run(
+ devices = _devices((0, 80, 35), (1, 80, 70), (2, 80, 0)),
+ required_override = 20.0,
+ gpu_ids = [0, 1],
+ )
+ self.assertTrue(ok)
+ self.assertEqual(info["mode"], "gguf")
+
+ def test_single_device_uses_selected_gpu(self):
+ # The model needs 27 GB with headroom. GPU 0 has 45 GB free, while an
+ # unrelated training-heavy GPU 1 has only 10 GB free.
+ ok, info, _ = self._run(
+ devices = _devices((0, 80, 35), (1, 80, 70)),
+ required_override = 20.0,
+ single_device_gpu = "0",
+ )
+ self.assertTrue(ok)
+ self.assertEqual(info["usable_gb"], 45.0)
+
+ blocked, blocked_info, _ = self._run(
+ devices = _devices((0, 80, 35), (1, 80, 70)),
+ required_override = 20.0,
+ single_device_gpu = "1",
+ )
+ self.assertFalse(blocked)
+ self.assertEqual(blocked_info["usable_gb"], 10.0)
+
+ def test_single_device_unresolved_token_sizes_against_worst_device(self):
+ # A non-numeric device token (a CUDA UUID / MIG handle) can't map to a
+ # free-VRAM index. The runner still drives ONE device, so size against the
+ # worst-case visible device (min free), not the aggregate pool: one GPU
+ # with 80 GB free vs a 20 GB model -> allow.
+ ok, info, _ = self._run(
+ devices = _devices((0, 80, 0)),
+ required_override = 20.0,
+ single_device_gpu = "GPU-uuid",
+ )
+ self.assertTrue(ok)
+ self.assertEqual(info["mode"], "single_device")
+ self.assertNotIn("reason", info)
+
+ def test_single_device_unresolved_token_refuses_when_worst_device_full(self):
+ # Same UUID fallback, worst-case device nearly full (2 GB for a 20 GB
+ # model) -> refuse (default-deny), not on an unresolved-token technicality.
+ ok, info, _ = self._run(
+ devices = _devices((0, 80, 78)),
+ required_override = 20.0,
+ single_device_gpu = "GPU-uuid",
+ )
+ self.assertFalse(ok)
+ self.assertNotEqual(info.get("reason"), "unresolved_gpu_id")
+
+ def test_single_device_unresolved_token_uses_min_free_not_aggregate(self):
+ # The single-device runner uses ONE device but we can't tell which from a
+ # UUID token. Sizing against the aggregate pool would let a 20 GB model
+ # "fit" 160 GB of pooled free VRAM while landing on a 2 GB card and OOMing
+ # training. Min-free (2 GB) is the safe worst case -> refuse.
+ ok, info, _ = self._run(
+ devices = _devices((0, 80, 78), (1, 80, 0), (2, 80, 0)),
+ required_override = 20.0,
+ single_device_gpu = "GPU-uuid",
+ )
+ self.assertFalse(ok)
+ self.assertEqual(info["mode"], "single_device")
+
+ def test_single_device_cpu_token_allows(self):
+ # An empty device token = a CPU-only single-device runner (CPU diffusion
+ # GGUF): it uses no GPU VRAM, so it never threatens training -> allow
+ # regardless of how full the GPUs are.
+ ok, info, _ = self._run(
+ devices = _devices((0, 80, 78)),
+ required_override = 20.0,
+ single_device_gpu = "",
+ )
+ self.assertTrue(ok)
+ self.assertEqual(info["reason"], "cpu_only")
+
def test_estimate_unavailable_refuses(self):
# No override and the estimator can't size it -> default-deny.
ok, info, _ = self._run(devices = _devices((0, 80, 0)), required_override = None, estimate = None)
@@ -309,6 +395,8 @@ class TestChatLoadGuardRoute(unittest.TestCase):
captured = None,
training_active,
decision,
+ gpu_memory_mode = "auto",
+ requested_gpu_ids = None,
):
config = config or SimpleNamespace(is_gguf = False, is_lora = False, path = None)
with _stub_guard_deps(
@@ -320,7 +408,8 @@ class TestChatLoadGuardRoute(unittest.TestCase):
hf_token = None,
load_in_4bit = True,
max_seq_length = 0,
- requested_gpu_ids = None,
+ requested_gpu_ids = requested_gpu_ids,
+ gpu_memory_mode = gpu_memory_mode,
)
def test_noop_when_training_inactive(self):
@@ -332,6 +421,141 @@ class TestChatLoadGuardRoute(unittest.TestCase):
def test_allows_when_fits(self):
self._guard(training_active = True, decision = (True, {"mode": "auto"}))
+ def test_diffusion_detection_uses_name_before_download(self):
+ config = SimpleNamespace(
+ identifier = "unsloth/DiffusionGemma-GGUF",
+ gguf_hf_repo = "unsloth/DiffusionGemma-GGUF",
+ gguf_file = None,
+ )
+ self.assertTrue(self.route._classify_diffusion_gguf(config))
+
+ def test_uncached_gguf_classification_remains_unknown(self):
+ config = SimpleNamespace(
+ identifier = "owner/renamed-model",
+ gguf_hf_repo = "owner/renamed-model",
+ gguf_variant = "Q4_K_M",
+ gguf_file = None,
+ )
+ self.assertIsNone(self.route._classify_diffusion_gguf(config))
+
+ def test_diffusion_detection_reuses_loader_metadata_probe(self):
+ import tempfile
+
+ seen = []
+
+ class _Probe:
+ is_diffusion = False
+ _architecture = None
+
+ def _read_gguf_metadata(self, path):
+ seen.append(path)
+ self.is_diffusion = True
+
+ with tempfile.TemporaryDirectory() as d:
+ model = Path(d) / "renamed.gguf"
+ model.write_bytes(b"GGUF")
+ config = SimpleNamespace(identifier = "local", gguf_file = str(model))
+ with patch.object(self.route, "LlamaCppBackend", _Probe):
+ self.assertTrue(self.route._classify_diffusion_gguf(config))
+ self.assertEqual(seen, [str(model)])
+
+ def test_local_chat_gguf_classification_is_definitive(self):
+ import tempfile
+ class _Probe:
+ is_diffusion = False
+ _architecture = "llama"
+
+ def _read_gguf_metadata(self, _path):
+ pass
+
+ with tempfile.TemporaryDirectory() as d:
+ model = Path(d) / "renamed.gguf"
+ model.write_bytes(b"GGUF")
+ config = SimpleNamespace(identifier = "local", gguf_file = str(model))
+ with patch.object(self.route, "LlamaCppBackend", _Probe):
+ self.assertFalse(self.route._classify_diffusion_gguf(config))
+
+ def test_manual_known_normal_gguf_bypasses_training_estimate(self):
+ captured = []
+ config = SimpleNamespace(is_gguf = True)
+ with patch.object(self.route, "_classify_diffusion_gguf", return_value = False):
+ self._guard(
+ config = config,
+ captured = captured,
+ training_active = True,
+ decision = (False, {"reason": "must not run"}),
+ gpu_memory_mode = "manual",
+ )
+ self.assertEqual(captured, [])
+
+ def test_manual_unknown_gguf_keeps_single_device_training_guard(self):
+ captured = []
+ config = SimpleNamespace(is_gguf = True)
+ with (
+ patch.object(self.route, "_classify_diffusion_gguf", return_value = None),
+ patch.object(self.route, "_estimate_gguf_required_gb", return_value = 12.5),
+ patch.object(
+ self.route.LlamaCppBackend,
+ "_diffusion_gpu_arg",
+ return_value = "2",
+ ),
+ ):
+ self._guard(
+ config = config,
+ captured = captured,
+ training_active = True,
+ decision = (True, {"mode": "single_device"}),
+ gpu_memory_mode = "manual",
+ )
+ self.assertEqual(len(captured), 1)
+ self.assertEqual(captured[0]["single_device_gpu"], "2")
+
+ def test_manual_diffusion_uses_single_device_guard(self):
+ captured = []
+ config = SimpleNamespace(is_gguf = True)
+ with (
+ patch.object(self.route, "_classify_diffusion_gguf", return_value = True),
+ patch.object(self.route, "_estimate_gguf_required_gb", return_value = 12.5),
+ ):
+ self._guard(
+ config = config,
+ captured = captured,
+ training_active = True,
+ decision = (True, {"mode": "gguf"}),
+ gpu_memory_mode = "manual",
+ requested_gpu_ids = [3, 1],
+ )
+ self.assertEqual(len(captured), 1)
+ self.assertEqual(captured[0]["single_device_gpu"], "1")
+ self.assertEqual(captured[0]["requested_gpu_ids"], [3, 1])
+
+ def test_unpinned_diffusion_uses_runner_default_gpu(self):
+ captured = []
+ config = SimpleNamespace(is_gguf = True)
+ with (
+ patch.object(self.route, "_classify_diffusion_gguf", return_value = True),
+ patch.object(self.route, "_estimate_gguf_required_gb", return_value = 12.5),
+ patch.object(
+ self.route.LlamaCppBackend,
+ "_effective_gpu_count",
+ return_value = 2,
+ ),
+ patch.object(
+ self.route.LlamaCppBackend,
+ "_diffusion_gpu_arg",
+ return_value = "3",
+ ) as gpu_arg,
+ ):
+ self._guard(
+ config = config,
+ captured = captured,
+ training_active = True,
+ decision = (True, {"mode": "single_device"}),
+ gpu_memory_mode = "manual",
+ )
+ gpu_arg.assert_called_once_with(None, cpu_only = False)
+ self.assertEqual(captured[0]["single_device_gpu"], "3")
+
def test_refuses_with_headroom_number(self):
info = {"required_gb": 30.0, "usable_gb": 6.0, "needed_gb": 39.0, "mode": "auto"}
with self.assertRaises(HTTPException) as exc:
@@ -467,36 +691,115 @@ class TestValidateRefusesDuringTraining(unittest.TestCase):
self.assertEqual(captured[0]["load_in_4bit"], False)
self.assertEqual(captured[0]["max_seq_length"], 4096)
- def test_rejects_gguf_with_gpu_ids_before_guard(self):
- # /validate must mirror /load's GGUF + gpu_ids 400, before the VRAM guard.
+ def test_validate_forwards_manual_gpu_memory_mode_to_guard(self):
from models.inference import ValidateModelRequest
- request = ValidateModelRequest(model_path = "x.gguf", gpu_ids = [0])
+ request = ValidateModelRequest(
+ model_path = "unsloth/model-GGUF",
+ gguf_variant = "Q4_K_M",
+ gpu_memory_mode = "manual",
+ )
cfg = SimpleNamespace(
- identifier = "x.gguf",
- display_name = "x",
+ identifier = "unsloth/model-GGUF",
+ display_name = "model-GGUF",
is_gguf = True,
is_lora = False,
is_vision = False,
path = None,
base_model = None,
)
- captured = []
+ captured = {}
with (
patch.object(
self.route,
"_resolve_model_identifier_for_request",
- return_value = ("x.gguf", "x.gguf", False),
+ return_value = ("unsloth/model-GGUF", "unsloth/model-GGUF", False),
),
patch.object(self.route.ModelConfig, "from_identifier", return_value = cfg),
patch.object(self.route, "load_inference_config", return_value = {}),
- _stub_guard_deps(training_active = True, decision = (True, {}), captured = captured),
+ patch.object(
+ self.route,
+ "_guard_chat_load_against_training",
+ lambda config, **kw: captured.update(kw),
+ ),
):
- with self.assertRaises(HTTPException) as exc:
- asyncio.run(self.route.validate_model(request, current_subject = "u"))
- self.assertEqual(exc.exception.status_code, 400)
- self.assertIn("gpu_ids is not supported for GGUF", exc.exception.detail)
- self.assertEqual(captured, []) # guard never reached
+ asyncio.run(self.route.validate_model(request, current_subject = "u"))
+ self.assertEqual(captured.get("gpu_memory_mode"), "manual")
+
+ def test_validate_forwards_inherited_extras_and_parallel_to_guard(self):
+ # Regression: /load resolves inherited same-model extras and passes the
+ # real slot count to the guard; validate must do the same, else it sizes
+ # a smaller estimate (no inherited -c/--model-draft, n_parallel=1) and
+ # /load then 409s after the frontend has already unloaded.
+ from models.inference import ValidateModelRequest
+
+ request = ValidateModelRequest(model_path = "unsloth/Qwen3-1.7B", max_seq_length = 4096)
+ cfg = SimpleNamespace(
+ identifier = "unsloth/Qwen3-1.7B",
+ display_name = "Qwen3-1.7B",
+ is_gguf = False,
+ is_lora = False,
+ is_vision = False,
+ path = None,
+ base_model = None,
+ )
+ captured = {}
+ with (
+ patch.object(
+ self.route,
+ "_resolve_model_identifier_for_request",
+ return_value = ("unsloth/Qwen3-1.7B", "unsloth/Qwen3-1.7B", False),
+ ),
+ patch.object(self.route.ModelConfig, "from_identifier", return_value = cfg),
+ patch.object(self.route, "load_inference_config", return_value = {}),
+ patch.object(self.route, "_resolve_inherited_extra_args", return_value = ["-c", "32768"]),
+ patch.object(
+ self.route,
+ "_guard_chat_load_against_training",
+ lambda config, **kw: captured.update(kw),
+ ),
+ ):
+ asyncio.run(self.route.validate_model(request, current_subject = "u"))
+ self.assertEqual(captured.get("llama_extra_args"), ["-c", "32768"])
+ self.assertIn("n_parallel", captured)
+
+ def test_metadata_probe_skips_training_guard(self):
+ # A header-only probe (include_context_length) allocates no VRAM, so the
+ # training guard must not run -- else the staging GPU-layers / MoE sliders
+ # it feeds are hidden exactly when a during-training user needs them.
+ from models.inference import ValidateModelRequest
+
+ request = ValidateModelRequest(
+ model_path = "unsloth/Qwen3-1.7B",
+ max_seq_length = 4096,
+ include_context_length = True,
+ )
+ cfg = SimpleNamespace(
+ identifier = "unsloth/Qwen3-1.7B",
+ display_name = "Qwen3-1.7B",
+ is_gguf = False,
+ is_lora = False,
+ is_vision = False,
+ path = None,
+ base_model = None,
+ )
+ guard_called = []
+ with (
+ patch.object(
+ self.route,
+ "_resolve_model_identifier_for_request",
+ return_value = ("unsloth/Qwen3-1.7B", "unsloth/Qwen3-1.7B", False),
+ ),
+ patch.object(self.route.ModelConfig, "from_identifier", return_value = cfg),
+ patch.object(self.route, "load_inference_config", return_value = {}),
+ patch.object(
+ self.route,
+ "_guard_chat_load_against_training",
+ lambda *a, **kw: guard_called.append(True),
+ ),
+ ):
+ asyncio.run(self.route.validate_model(request, current_subject = "u"))
+ self.assertEqual(guard_called, [])
# ── _estimate_gguf_required_gb (sizes the same weights the loader loads) ──────
diff --git a/studio/backend/tests/test_gguf_metadata.py b/studio/backend/tests/test_gguf_metadata.py
index a5be07f8e3..ec0330ce05 100644
--- a/studio/backend/tests/test_gguf_metadata.py
+++ b/studio/backend/tests/test_gguf_metadata.py
@@ -15,6 +15,7 @@ from utils.models.gguf_metadata import (
pairing_score,
read_gguf_context_length,
read_gguf_general_metadata,
+ read_gguf_staged_dims,
read_mmproj_audio_capability,
)
@@ -153,6 +154,78 @@ def test_context_length_ignores_foreign_arch_key(tmp_path: Path):
assert read_gguf_context_length(str(p)) is None
+# --- read_gguf_staged_dims (one pass: context + layer + moe counts) ----
+
+
+def test_staged_dims_none_for_missing_or_non_gguf(tmp_path: Path):
+ assert read_gguf_staged_dims(str(tmp_path / "nope.gguf")) is None
+ p = tmp_path / "garbage.gguf"
+ p.write_bytes(b"not a gguf at all")
+ assert read_gguf_staged_dims(str(p)) is None
+
+
+def test_staged_dims_moe_with_leading_dense(tmp_path: Path):
+ # GLM-4.7-Flash shape: context + total layers + MoE layers in one read.
+ p = _write_synthetic_gguf(
+ tmp_path / "glm.gguf",
+ {"general.architecture": "deepseek2"},
+ extra_uint32 = {
+ "deepseek2.context_length": 202752,
+ "deepseek2.block_count": 47,
+ "deepseek2.expert_count": 64,
+ "deepseek2.leading_dense_block_count": 1,
+ },
+ )
+ assert read_gguf_staged_dims(str(p)) == {
+ "context_length": 202752,
+ "layer_count": 47,
+ "moe_layer_count": 46,
+ }
+
+
+def test_staged_dims_dense_model(tmp_path: Path):
+ # Dense: layer_count present, moe_layer_count 0 (slider hidden).
+ p = _write_synthetic_gguf(
+ tmp_path / "dense.gguf",
+ {"general.architecture": "qwen3"},
+ extra_uint32 = {"qwen3.context_length": 40960, "qwen3.block_count": 36},
+ )
+ assert read_gguf_staged_dims(str(p)) == {
+ "context_length": 40960,
+ "layer_count": 36,
+ "moe_layer_count": 0,
+ }
+
+
+def test_staged_dims_all_moe_no_leading_dense(tmp_path: Path):
+ # Experts present, no leading_dense key -> every block is a MoE layer.
+ p = _write_synthetic_gguf(
+ tmp_path / "moe.gguf",
+ {"general.architecture": "qwen35moe"},
+ extra_uint32 = {"qwen35moe.block_count": 40, "qwen35moe.expert_count": 256},
+ )
+ assert read_gguf_staged_dims(str(p)) == {
+ "context_length": None,
+ "layer_count": 40,
+ "moe_layer_count": 40,
+ }
+
+
+def test_staged_dims_uint64_block_count(tmp_path: Path):
+ # block_count stored as uint64 (vtype 10) still parses; moe == block_count.
+ p = _write_synthetic_gguf(
+ tmp_path / "moe64.gguf",
+ {"general.architecture": "gpt-oss"},
+ extra_uint32 = {"gpt-oss.expert_count": 32},
+ extra_uint64 = {"gpt-oss.block_count": 24},
+ )
+ assert read_gguf_staged_dims(str(p)) == {
+ "context_length": None,
+ "layer_count": 24,
+ "moe_layer_count": 24,
+ }
+
+
def test_context_length_read_from_uint64(tmp_path: Path):
# Some models store context_length as a uint64 (vtype 10).
p = _write_synthetic_gguf(
diff --git a/studio/backend/tests/test_gpu_memory_mode.py b/studio/backend/tests/test_gpu_memory_mode.py
new file mode 100644
index 0000000000..b17274197f
--- /dev/null
+++ b/studio/backend/tests/test_gpu_memory_mode.py
@@ -0,0 +1,879 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Backend contract for the GPU Memory mode dropdown.
+
+The dropdown threads a single ``gpu_memory_mode`` ("auto" | "manual") from the
+chat UI through the load request. "manual" lets the user own the offload: with
+``gpu_layers < 0`` (Auto, the default) it hands all memory management to
+llama.cpp's ``--fit on`` (no CUDA/HIP device masking, no context auto-reduce, no
+gpu-layer or tensor-split planning); with ``gpu_layers >= 0`` it pins the layers
+and MoE offload itself (``--fit off``). These tests pin:
+
+ * the pydantic request/response/status contract (snake_case key, default
+ "auto", unknown values rejected),
+ * the backend ``gpu_memory_mode`` property and its reset on unload,
+ * the ``_already_in_target_state`` reload-detection branch, and
+ * that the manual + Auto-layers branch in ``load_model`` empties the probed
+ GPU set and drops tensor parallelism so the selection below no-ops, while
+ the explicit-offload branch emits ``--gpu-layers`` / ``--fit off``.
+"""
+
+from __future__ import annotations
+
+import inspect
+import sys
+import types as _types
+from pathlib import Path
+
+import pytest
+
+_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
+if _BACKEND_DIR not in sys.path:
+ sys.path.insert(0, _BACKEND_DIR)
+
+# Same external-dep stubs as the other llama_cpp unit tests so importing
+# the backend doesn't drag in structlog / httpx / loggers.
+_loggers_stub = _types.ModuleType("loggers")
+_loggers_stub.get_logger = lambda name: __import__("logging").getLogger(name)
+sys.modules.setdefault("loggers", _loggers_stub)
+
+_structlog_stub = _types.ModuleType("structlog")
+_structlog_stub.get_logger = lambda *a, **k: __import__("logging").getLogger("stub")
+sys.modules.setdefault("structlog", _structlog_stub)
+
+# httpx is a real, installed backend dependency: import it so the genuine module
+# is in sys.modules. A hand-rolled stub here is inevitably incomplete and, since
+# setdefault installs it before real httpx loads, would poison a combined pytest
+# run -- routes/inference references httpx.Response (and other attrs) at def time.
+import httpx # noqa: F401
+
+from core.inference import llama_cpp as llama_cpp_module
+from core.inference.llama_cpp import LlamaCppBackend
+from models.inference import (
+ InferenceStatusResponse,
+ LoadRequest,
+ LoadResponse,
+)
+
+
+# ── Pydantic contract (snake_case key, default "auto") ───────────────
+
+
+def test_load_request_defaults_gpu_memory_mode_auto():
+ assert LoadRequest(model_path = "owner/repo").gpu_memory_mode == "auto"
+
+
+def test_load_request_round_trips_json_key():
+ req = LoadRequest.model_validate({"model_path": "owner/repo", "gpu_memory_mode": "manual"})
+ assert req.gpu_memory_mode == "manual"
+ assert req.model_dump()["gpu_memory_mode"] == "manual"
+
+
+def test_load_request_rejects_unknown_mode():
+ with pytest.raises(ValueError):
+ LoadRequest(model_path = "owner/repo", gpu_memory_mode = "bogus")
+
+
+@pytest.mark.parametrize("model_cls", [LoadResponse, InferenceStatusResponse])
+def test_response_models_emit_gpu_memory_mode(model_cls):
+ if model_cls is LoadResponse:
+ default = model_cls(
+ status = "loaded",
+ model = "owner/repo",
+ display_name = "repo",
+ inference = {},
+ )
+ manual = model_cls(
+ status = "loaded",
+ model = "owner/repo",
+ display_name = "repo",
+ inference = {},
+ gpu_memory_mode = "manual",
+ )
+ else:
+ default = model_cls()
+ manual = model_cls(gpu_memory_mode = "manual")
+ assert default.model_dump()["gpu_memory_mode"] == "auto"
+ assert manual.model_dump()["gpu_memory_mode"] == "manual"
+
+
+# ── Backend property + reset ─────────────────────────────────────────
+
+
+class _FakeProcess:
+ """Stand-in for subprocess.Popen so _kill_process is a no-op."""
+
+ def terminate(self):
+ pass
+
+ def wait(self, timeout = None):
+ return 0
+
+ def kill(self):
+ pass
+
+ def poll(self):
+ return 0
+
+
+def test_gpu_memory_mode_property_defaults_auto():
+ assert LlamaCppBackend().gpu_memory_mode == "auto"
+
+
+def test_gpu_memory_mode_property_reflects_field():
+ backend = LlamaCppBackend()
+ backend._gpu_memory_mode = "manual"
+ assert backend.gpu_memory_mode == "manual"
+
+
+def test_unload_resets_gpu_memory_mode():
+ backend = LlamaCppBackend()
+ backend._process = _FakeProcess()
+ backend._gpu_memory_mode = "manual"
+ backend.unload_model()
+ assert backend.gpu_memory_mode == "auto"
+
+
+# ── _already_in_target_state reload-detection branch ─────────────────
+
+
+def _loaded_backend(gpu_memory_mode: str) -> LlamaCppBackend:
+ backend = LlamaCppBackend()
+ backend._process = _FakeProcess() # is_loaded only checks "is not None"
+ backend._healthy = True
+ backend._model_identifier = "owner/repo"
+ backend._hf_variant = "Q4_K_M"
+ backend._requested_n_ctx = 8192
+ backend._cache_type_kv = None
+ backend._requested_spec_mode = "auto"
+ backend._chat_template_override = None
+ backend._is_vision = False
+ backend._extra_args = None
+ backend._gguf_path = None
+ backend._gpu_memory_mode = gpu_memory_mode
+ return backend
+
+
+def _target_state(backend: LlamaCppBackend, gpu_memory_mode: str) -> bool:
+ return backend._already_in_target_state(
+ gguf_path = None,
+ model_identifier = "owner/repo",
+ hf_variant = "Q4_K_M",
+ n_ctx = 8192,
+ cache_type_kv = None,
+ speculative_type = "auto",
+ chat_template_override = None,
+ extra_args = None,
+ is_vision = False,
+ gpu_memory_mode = gpu_memory_mode,
+ )
+
+
+@pytest.mark.parametrize("mode", ["auto", "manual"])
+def test_already_in_target_state_matches_same_mode(mode):
+ assert _target_state(_loaded_backend(mode), mode) is True
+
+
+@pytest.mark.parametrize("loaded,requested", [("auto", "manual"), ("manual", "auto")])
+def test_already_in_target_state_reloads_on_mode_change(loaded, requested):
+ # Flipping the dropdown either direction must force a reload so the command
+ # is rebuilt with/without the Unsloth GPU masking.
+ assert _target_state(_loaded_backend(loaded), requested) is False
+
+
+def test_already_in_target_state_ignores_mode_for_diffusion():
+ # The diffusion runner is mode-agnostic (always "auto"), so a standing manual
+ # preference must not force a needless reload.
+ backend = _loaded_backend("auto")
+ backend._is_diffusion = True
+ assert _target_state(backend, "manual") is True
+
+
+# ── load_model: manual + Auto layers bypasses Unsloth GPU management ──
+
+
+def _load_model_source() -> str:
+ return inspect.getsource(llama_cpp_module.LlamaCppBackend.load_model)
+
+
+def test_auto_layers_branch_empties_gpus_and_drops_tensor_parallel():
+ # Emptying the probed set makes the selection / TP planning below no-op, so
+ # gpu_indices stays None and use_fit True (--fit on).
+ src = _load_model_source()
+ gate = src.find('if gpu_memory_mode == "manual" and gpu_layers < 0:')
+ assert gate != -1, "load_model must branch on manual + Auto layers (gpu_layers < 0)"
+ block = src[gate : gate + 1400]
+ assert "gpus = []" in block, "Auto-layers branch must empty the probed GPU set"
+ # --fit aborts under --split-mode tensor, so a raw-extras split-mode is stripped.
+ assert "strip_split_mode_only(extra_args)" in block
+ assert "requested_ctx if requested_ctx > 0 else 0" in block
+ # The branch sits before GPU selection assigns gpu_indices; --fit on is its emission.
+ assert gate < src.find("gpu_indices, use_fit = None, True")
+ assert 'cmd.extend(["--fit", "on"])' in src
+ # TP drops for this path, but at a guard BEFORE the quantized-KV cache-drop, so
+ # a requested quantized cache survives into the --fit load.
+ tp_drop = src.find('if tensor_parallel and gpu_memory_mode == "manual" and gpu_layers < 0:')
+ assert tp_drop != -1, "manual + Auto layers must drop tensor_parallel"
+ assert "tensor_parallel = False" in src[tp_drop : tp_drop + 400]
+ cache_drop = src.find("Tensor parallelism requires a non-quantized KV cache")
+ assert cache_drop != -1
+ assert (
+ tp_drop < cache_drop
+ ), "TP must drop before the cache-drop so a quantized KV survives --fit"
+
+
+def test_auto_layers_never_sends_ctx_size_zero():
+ # Sending "-c 0" sets fit_params_min_ctx = UINT32_MAX in llama.cpp, pinning
+ # the full native context and disabling --fit's reduction. So the base cmd
+ # must never carry -c, "-c 0" is emitted only outside the Auto-layers (--fit)
+ # case, and a positive context is passed through (which --fit optimizes
+ # layers around).
+ src = _load_model_source()
+ base_start = src.find("cmd = [")
+ base_end = src.find("\n ]", base_start)
+ base_block = src[base_start:base_end]
+ assert '"-c"' not in base_block, "-c must be conditional, not in the base cmd list"
+ assert 'cmd.extend(["-c", str(effective_ctx)])' in src, "positive ctx must pass -c"
+ assert 'auto_fit = gpu_memory_mode == "manual" and gpu_layers < 0' in src
+ zero = src.find('cmd.extend(["-c", "0"])')
+ assert zero != -1, '"-c 0" emission must exist outside the Auto-layers case'
+ guard = src.rfind("elif not auto_fit:", 0, zero)
+ assert guard != -1 and zero - guard < 120, '"-c 0" must sit under the not-auto_fit guard'
+
+
+def test_manual_mode_clears_inherited_main_model_placement_env():
+ env = {name: "inherited" for name in LlamaCppBackend._MANUAL_PLACEMENT_ENV_VARS}
+ env["LLAMA_ARG_N_GPU_LAYERS_DRAFT"] = "7"
+ env["UNRELATED"] = "kept"
+
+ LlamaCppBackend._clear_manual_placement_env(env)
+
+ assert not (set(env) & set(LlamaCppBackend._MANUAL_PLACEMENT_ENV_VARS))
+ assert env["LLAMA_ARG_N_GPU_LAYERS_DRAFT"] == "7"
+ assert env["UNRELATED"] == "kept"
+
+
+def test_load_model_sanitizes_manual_env_after_building_child_env():
+ src = _load_model_source()
+ env_build = src.find("env = self._llama_server_env_for_binary(binary)")
+ env_clear = src.find("self._clear_manual_placement_env(env)", env_build)
+ launch = src.find("subprocess.Popen", env_build)
+ assert env_build != -1
+ assert env_build < env_clear < launch
+
+
+# ── Manual offload (--gpu-layers + --fit off + --n-cpu-moe) ───────────
+
+
+def test_load_request_accepts_manual():
+ req = LoadRequest(
+ model_path = "owner/repo",
+ gpu_memory_mode = "manual",
+ gpu_layers = 20,
+ n_cpu_moe = 8,
+ tensor_split = [2, 1],
+ )
+ assert req.gpu_memory_mode == "manual"
+ assert req.gpu_layers == 20
+ assert req.n_cpu_moe == 8
+ assert req.tensor_split == [2, 1]
+
+
+def test_load_request_manual_defaults():
+ req = LoadRequest(model_path = "owner/repo")
+ assert req.gpu_layers == -1
+ assert req.n_cpu_moe == 0
+ assert req.tensor_split is None
+
+
+@pytest.mark.parametrize("bad", [[0, 0], [-1, 2], [float("inf"), 1], [float("nan"), 1]])
+def test_load_request_rejects_degenerate_tensor_split(bad):
+ # A negative/non-finite/all-zero split is dropped at launch but compared raw
+ # in the reload dedupe, so it would reload forever -- reject it up front.
+ with pytest.raises(ValueError):
+ LoadRequest(model_path = "owner/repo", tensor_split = bad)
+
+
+@pytest.mark.parametrize("good", [[2, 1], [1, 1], [], None])
+def test_load_request_accepts_valid_tensor_split(good):
+ assert LoadRequest(model_path = "owner/repo", tensor_split = good).tensor_split == good
+
+
+def test_route_normalizes_explicit_extras_before_reload_dedupe():
+ route_src = (Path(_BACKEND_DIR) / "routes" / "inference.py").read_text(encoding = "utf-8")
+ load_impl = route_src[route_src.index("async def _load_model_impl") :]
+ strip = load_impl.index("_stripped_explicit = strip_shadowing_flags")
+ normalize = load_impl.index(
+ 'request = request.model_copy(update = {"llama_extra_args": extra_llama_args})'
+ )
+ dedupe = load_impl.index("and _request_matches_loaded_settings(")
+ assert strip < normalize < dedupe
+
+
+@pytest.mark.parametrize("model_cls", [LoadResponse, InferenceStatusResponse])
+def test_response_models_emit_manual_fields(model_cls):
+ if model_cls is LoadResponse:
+ obj = model_cls(
+ status = "loaded",
+ model = "owner/repo",
+ display_name = "repo",
+ inference = {},
+ gpu_memory_mode = "manual",
+ gpu_layers = 20,
+ n_cpu_moe = 8,
+ tensor_split = [2, 1],
+ n_layers = 32,
+ n_moe_layers = 32,
+ )
+ else:
+ obj = model_cls(
+ gpu_memory_mode = "manual",
+ gpu_layers = 20,
+ n_cpu_moe = 8,
+ tensor_split = [2, 1],
+ n_layers = 32,
+ n_moe_layers = 32,
+ )
+ dumped = obj.model_dump()
+ assert dumped["gpu_memory_mode"] == "manual"
+ assert dumped["gpu_layers"] == 20
+ assert dumped["n_cpu_moe"] == 8
+ assert dumped["tensor_split"] == [2, 1]
+ assert dumped["n_layers"] == 32
+ assert dumped["n_moe_layers"] == 32
+
+
+def test_manual_properties_default_and_reflect_and_reset():
+ backend = LlamaCppBackend()
+ assert backend.gpu_layers == -1 and backend.n_cpu_moe == 0
+ assert backend.tensor_split is None
+ backend._gpu_layers = 20
+ backend._n_cpu_moe = 8
+ backend._tensor_split = [2, 1]
+ assert backend.gpu_layers == 20 and backend.n_cpu_moe == 8
+ assert backend.tensor_split == [2, 1]
+ backend._process = _FakeProcess()
+ backend.unload_model()
+ assert backend.gpu_layers == -1 and backend.n_cpu_moe == 0
+ assert backend.tensor_split is None
+
+
+def test_n_moe_layers_property():
+ # 0 for a dense model (hides the slider); block_count for all-MoE;
+ # block_count - leading_dense otherwise (GLM-4.7-Flash: 47 - 1 -> 46).
+ b = LlamaCppBackend()
+ b._n_layers = 36
+ b._n_experts = None
+ assert b.n_moe_layers == 0
+ b._n_experts = 128
+ b._leading_dense_block_count = None
+ assert b.n_moe_layers == 36
+ b._n_layers = 47
+ b._leading_dense_block_count = 1
+ assert b.n_moe_layers == 46
+
+
+def _target_state_manual(
+ backend,
+ *,
+ gpu_layers,
+ n_cpu_moe,
+ tensor_split = None,
+):
+ return backend._already_in_target_state(
+ gguf_path = None,
+ model_identifier = "owner/repo",
+ hf_variant = "Q4_K_M",
+ n_ctx = 8192,
+ cache_type_kv = None,
+ speculative_type = "auto",
+ chat_template_override = None,
+ extra_args = None,
+ is_vision = False,
+ gpu_memory_mode = "manual",
+ gpu_layers = gpu_layers,
+ n_cpu_moe = n_cpu_moe,
+ tensor_split = tensor_split,
+ )
+
+
+def test_manual_reloads_on_gpu_layers_or_n_cpu_moe_or_split_change():
+ backend = _loaded_backend("manual")
+ backend._gpu_layers = 20
+ backend._n_cpu_moe = 0
+ backend._tensor_split = None
+ # Same knobs -> no reload.
+ assert _target_state_manual(backend, gpu_layers = 20, n_cpu_moe = 0) is True
+ # Changed layer count -> reload.
+ assert _target_state_manual(backend, gpu_layers = 16, n_cpu_moe = 0) is False
+ # Changed MoE offload -> reload.
+ assert _target_state_manual(backend, gpu_layers = 20, n_cpu_moe = 8) is False
+ # Added a GPU split -> reload.
+ assert _target_state_manual(backend, gpu_layers = 20, n_cpu_moe = 0, tensor_split = [2, 1]) is False
+ # Same GPU split -> no reload.
+ backend._tensor_split = [2, 1]
+ assert _target_state_manual(backend, gpu_layers = 20, n_cpu_moe = 0, tensor_split = [2, 1]) is True
+
+
+def test_auto_layers_reload_tracks_only_gpu_layers():
+ # Under Auto (gpu_layers < 0) the MoE/split knobs don't apply, so a leftover
+ # request value must not reload -- only a gpu_layers change (Auto -> pinned) does.
+ backend = _loaded_backend("manual")
+ backend._gpu_layers = -1
+ backend._n_cpu_moe = 0
+ backend._tensor_split = None
+ # Same Auto, leftover MoE/split in the request -> still no reload.
+ assert _target_state_manual(backend, gpu_layers = -1, n_cpu_moe = 8, tensor_split = [2, 1]) is True
+ # Auto -> explicit offload reloads.
+ assert _target_state_manual(backend, gpu_layers = 20, n_cpu_moe = 0) is False
+
+
+def test_manual_offload_emits_gpu_layers_fit_off_and_n_cpu_moe():
+ src = _load_model_source()
+ gate = src.find('elif gpu_memory_mode == "manual":')
+ assert gate != -1, "load_model must have an explicit-offload manual branch"
+ block = src[gate : gate + 700]
+ # Empties the probed set (skips the planner) but keeps the user's TP choice
+ # (only the Auto-layers branch above drops TP).
+ assert "gpus = []" in block
+ assert "tensor_parallel = False" not in block
+ # The cmd emits the layer count with fit disabled, gated on gpu_layers >= 0.
+ assert 'if gpu_memory_mode == "manual" and gpu_layers >= 0:' in src
+ assert 'cmd.extend(["--gpu-layers", str(gpu_layers), "--fit", "off"])' in src
+ # MoE offload uses --n-cpu-moe via _resolve_cpu_moe_flag (tested behaviorally below).
+ assert "_resolve_cpu_moe_flag(" in src
+ assert 'cmd.extend(["--n-cpu-moe", str(moe_flag)])' in src
+ # A count requested on a dense model is never emitted, so it must also be
+ # dropped from the recorded state -- else /status and /load report a count
+ # llama-server never received (same rule as the tensor-split drop below).
+ moe_emit = src.find('cmd.extend(["--n-cpu-moe", str(moe_flag)])')
+ assert "elif n_cpu_moe:" in src[moe_emit : moe_emit + 300]
+ assert "self._n_cpu_moe = 0" in src[moe_emit : moe_emit + 300]
+ # The offload path forces use_fit False so --fit-ctx is never added under --fit off.
+ emit = src.find('cmd.extend(["--gpu-layers", str(gpu_layers), "--fit", "off"])')
+ assert "use_fit = False" in src[src.rfind("\n", 0, emit) - 200 : emit + 80]
+
+
+def test_status_reports_requested_context_length():
+ # The hydration path re-seeds a Manual+Auto context pin from the REQUESTED
+ # n_ctx (0 = Auto); context_length only exposes the resolved value.
+ assert "requested_context_length" in InferenceStatusResponse.model_fields
+ s = InferenceStatusResponse(requested_context_length = 8192)
+ assert s.model_dump()["requested_context_length"] == 8192
+ assert InferenceStatusResponse().model_dump()["requested_context_length"] is None
+ # The /status route must actually wire it from the backend (a declared-but-
+ # never-populated field would leave hydration silently reverting the pin).
+ from pathlib import Path as _P
+
+ route_src = (_P(_BACKEND_DIR) / "routes" / "inference.py").read_text(encoding = "utf-8")
+ assert "requested_context_length = llama_backend.requested_n_ctx" in route_src
+
+
+def test_manual_offload_emits_tensor_split():
+ # The offload path emits --tensor-split from the per-GPU shares, only when
+ # provided, with >1 GPU in use, AND matching that count (a stale ratio on a
+ # narrowed picker or a mismatched direct-API list must not emit -- llama-
+ # server aborts on a split/GPU-count mismatch).
+ src = _load_model_source()
+ assert "if tensor_split and _split_gpus > 1:" in src
+ # Emit only on a length match AND a positive sanitized total: a mismatched
+ # or all-zero split aborts llama-server / assigns nothing, so it's dropped.
+ # The emitted list is the sanitized one (clamping tested behaviorally below).
+ assert "_sanitized_split = self._sanitize_tensor_split(tensor_split)" in src
+ assert "if len(_sanitized_split) == _split_gpus and _split_total > 0:" in src
+ assert '"--tensor-split"' in src
+ # Joined as a comma list (e.g. "2,1") within the explicit-offload cmd branch.
+ gate = src.find('if gpu_memory_mode == "manual" and gpu_layers >= 0:')
+ nxt = src.find("elif use_fit:", gate)
+ assert '","' in src[gate:nxt] and "tensor_split" in src[gate:nxt]
+ # A split with a single effective GPU is never emitted, so it must also be
+ # dropped from the recorded state -- else /status and /load report a ratio
+ # llama-server never received and the dedupe baseline preserves it.
+ assert "elif tensor_split:" in src[gate:nxt]
+ drop = src.find("elif tensor_split:", gate, nxt)
+ assert "self._tensor_split = None" in src[drop : drop + 250]
+
+
+def test_sanitize_tensor_split_clamps_negative_and_non_finite():
+ # Negative entries would launch a placement different from the ratio the
+ # UI showed; inf passes a plain > 0 total gate and would emit
+ # "--tensor-split inf,..." (llama.cpp normalizes shares by the running
+ # total, so an inf poisons the shares from that entry on). Both clamp to 0.
+ sanitize = LlamaCppBackend._sanitize_tensor_split
+ assert sanitize([2, 1]) == [2.0, 1.0]
+ assert sanitize([-1, 2]) == [0.0, 2.0]
+ assert sanitize([float("inf"), 1]) == [0.0, 1.0]
+ assert sanitize([float("nan"), 1]) == [0.0, 1.0]
+ # All-zero survives sanitization; the call site's total gate drops it.
+ assert sanitize([0, 0]) == [0.0, 0.0]
+ # Unreadable input -> []; the call site's length gate drops it.
+ assert sanitize(["x", 1]) == []
+ assert sanitize([10**400, 1]) == []
+
+
+def test_zero_offload_mask_honors_device_pin_spellings():
+ # A user device pin must keep the GPUs visible: llama-server aborts on a
+ # pin it can't see ('error: invalid device'). The pin can arrive as
+ # --device or its -dev alias, as the draft forms (parsed even with no
+ # drafter loaded), or as an inherited LLAMA_ARG_DEVICE env var.
+ load_src = _load_model_source()
+ assert "self._zero_offload_keeps_gpu_visible(cmd, env)" in load_src
+ block = inspect.getsource(LlamaCppBackend._cmd_has_gpu_device_pin)
+ for flag in (
+ '"--device"',
+ '"-dev"',
+ '"--spec-draft-device"',
+ '"-devd"',
+ '"--device-draft"',
+ ):
+ assert flag in block
+ assert '"LLAMA_ARG_DEVICE"' in block
+
+
+def test_resolve_cpu_moe_flag():
+ # Clamp the requested MoE-layer count to the model's MoE layers, then offset
+ # past leading dense layers (--n-cpu-moe counts from layer 0).
+ R = LlamaCppBackend._resolve_cpu_moe_flag
+ assert R(0, 40, 0) is None # nothing requested
+ assert R(8, 0, 0) is None # dense model (no MoE layers)
+ assert R(8, 40, 0) == 8 # all-MoE: direct
+ assert R(100, 40, 0) == 40 # clamp to the MoE layer count
+ # GLM-4.7-Flash (deepseek2): block_count 47, leading_dense 1, n_moe 46.
+ assert R(5, 46, 1) == 6 # offset past the 1 dense layer
+ assert R(46, 46, 1) == 47 # all MoE on CPU == block_count
+
+
+def test_manual_allows_tensor_parallel_via_split_mode():
+ # Manual offload keeps the user's TP choice but skips the memory-based planner
+ # (plan_tp excludes manual, so its empty gpu set can't downgrade TP). The
+ # --split-mode tensor emission gates on tensor_parallel alone, so manual
+ # reaches it -- with tp_tensor_split None it's an even split (no
+ # --tensor-split). --fit off means no fit/tensor abort.
+ src = _load_model_source()
+ assert 'plan_tp = tensor_parallel and gpu_memory_mode != "manual"' in src
+ assert "if plan_tp:" in src
+ assert "if plan_tp and len(tp_gpus) < 2:" in src
+ sm = src.find('cmd.extend(["--split-mode", "tensor"])')
+ assert sm != -1, "TP must emit --split-mode tensor"
+ guard = src.rfind("if tensor_parallel:", 0, sm)
+ assert guard != -1 and sm - guard < 200, "split-mode gates on tensor_parallel"
+ # The tensor-split is only emitted for a planned (non-even) split, which
+ # manual never produces, so manual stays an even split.
+ assert "if tp_tensor_split and len(tp_tensor_split) > 1:" in src
+
+
+def test_fit_sets_target_margin():
+ # Manual + Auto (auto_fit) tightens the per-device VRAM margin to 512 MiB.
+ caps = {"supports_fit_target": True}
+ flags = LlamaCppBackend._ctx_integrity_flags(1, True, True, 0, 0, caps)
+ assert flags[flags.index("--fit-target") + 1] == "512"
+ # Not emitted on the legacy auto path (fit on but not auto_fit): -c 0 pins
+ # native there, so the tighter margin must not ride along.
+ assert "--fit-target" not in LlamaCppBackend._ctx_integrity_flags(1, True, False, 0, 0, caps)
+ # Not emitted when fit is off.
+ assert "--fit-target" not in LlamaCppBackend._ctx_integrity_flags(1, False, False, 0, 0, caps)
+ # Not emitted when the binary lacks support.
+ assert "--fit-target" not in LlamaCppBackend._ctx_integrity_flags(
+ 1, True, True, 0, 0, {"supports_fit_target": False}
+ )
+
+
+# ── GPU picker (gpu_ids -> CUDA_VISIBLE_DEVICES) ─────────────────────
+
+
+def test_load_request_accepts_gpu_ids():
+ req = LoadRequest(model_path = "owner/repo", gpu_ids = [1, 0])
+ assert req.gpu_ids == [1, 0]
+ assert LoadRequest(model_path = "owner/repo").gpu_ids is None
+
+
+@pytest.mark.parametrize("model_cls", [LoadResponse, InferenceStatusResponse])
+def test_response_models_emit_gpu_ids(model_cls):
+ if model_cls is LoadResponse:
+ obj = model_cls(status = "loaded", model = "m", display_name = "m", inference = {}, gpu_ids = [1])
+ else:
+ obj = model_cls(gpu_ids = [1])
+ assert obj.model_dump()["gpu_ids"] == [1]
+
+
+def test_gpu_ids_property_default_and_reset():
+ backend = LlamaCppBackend()
+ assert backend.gpu_ids is None
+ backend._gpu_ids = [0, 1]
+ assert backend.gpu_ids == [0, 1]
+ backend._process = _FakeProcess()
+ backend.unload_model()
+ assert backend.gpu_ids is None
+
+
+def _target_state_gpu_ids(backend, gpu_ids):
+ return backend._already_in_target_state(
+ gguf_path = None,
+ model_identifier = "owner/repo",
+ hf_variant = "Q4_K_M",
+ n_ctx = 8192,
+ cache_type_kv = None,
+ speculative_type = "auto",
+ chat_template_override = None,
+ extra_args = None,
+ is_vision = False,
+ gpu_ids = gpu_ids,
+ )
+
+
+def test_gpu_ids_reload_detection_is_order_insensitive():
+ backend = _loaded_backend("auto")
+ backend._gpu_ids = [0, 1]
+ # Same set, different order -> no reload.
+ assert _target_state_gpu_ids(backend, [1, 0]) is True
+ # Different set -> reload.
+ assert _target_state_gpu_ids(backend, [0]) is False
+ # Dropping the pick (auto) -> reload.
+ assert _target_state_gpu_ids(backend, None) is False
+
+
+def test_gpu_ids_reload_detection_collapses_diffusion_to_single_device():
+ # The diffusion runner drives only its single lowest device, so the backend
+ # records [lowest]. A later multi-GPU request that still resolves to that
+ # same lowest device must dedupe (no needless reload); a request whose lowest
+ # device moves, or that drops the pick, must reload.
+ backend = _loaded_backend("auto")
+ backend._is_diffusion = True
+ backend._gpu_ids = [1] # loaded on the lowest of an earlier [3, 1] pick
+ assert _target_state_gpu_ids(backend, [3, 1]) is True
+ assert _target_state_gpu_ids(backend, [1]) is True
+ # Lowest device changes (2, not 1) -> reload.
+ assert _target_state_gpu_ids(backend, [3, 2]) is False
+ # Dropping the pick (auto) -> reload.
+ assert _target_state_gpu_ids(backend, None) is False
+
+
+def test_start_diffusion_server_resets_tensor_parallel():
+ # A prior tensor-parallel chat load leaves self._tensor_parallel True (load_model
+ # phase 1 only kills the process, it skips the unload reset). Diffusion is never
+ # TP, so startup must clear it -- else /status misreports TP and an identical
+ # diffusion re-Apply reloads against stale tensor-parallel state.
+ src = inspect.getsource(llama_cpp_module.LlamaCppBackend._start_diffusion_server)
+ assert "self._tensor_parallel = False" in src
+
+
+def test_route_matches_loaded_settings_collapses_diffusion_gpu_ids():
+ # The route-level reload dedupe mirrors the backend: for a loaded diffusion
+ # model it compares the request against the single recorded device, not the
+ # full requested list, or a same-device multi-GPU pick reloads needlessly.
+ route_src = (Path(_BACKEND_DIR) / "routes" / "inference.py").read_text(encoding = "utf-8")
+ match_impl = route_src[route_src.index("def _request_matches_loaded_settings") :]
+ guard = match_impl.index("if llama_backend.is_diffusion:")
+ collapse = match_impl.index("[sorted(request.gpu_ids)[0]] if request.gpu_ids else None")
+ compare = match_impl.index("if _req_gpu_ids != llama_backend.gpu_ids:")
+ assert guard < collapse < compare
+
+
+# ── Manual tensor split: child enumeration pinned to the picker's order ──────
+
+
+def _patch_split_pin_env(monkeypatch, *, inherited, reported):
+ """Point the pin helper at a fake inherited mask and picker report.
+ ``reported`` None = enumeration unavailable (falls back to ascending)."""
+ import utils.hardware as hw
+
+ monkeypatch.setattr(
+ LlamaCppBackend, "_resolve_visible_physical_ids", staticmethod(lambda: inherited)
+ )
+ info = (
+ {"available": False}
+ if reported is None
+ else {
+ "available": True,
+ "index_kind": "physical",
+ "devices": [{"index": i} for i in reported],
+ }
+ )
+ monkeypatch.setattr(hw, "get_backend_visible_gpu_info", lambda: info)
+
+
+def test_split_pin_reorders_inherited_numeric_mask(monkeypatch):
+ # Parent CUDA_VISIBLE_DEVICES=3,1 makes the child enumerate dev0=phys3, but
+ # nvidia-smi reported the picker's list ascending -- the mask must be
+ # re-emitted in that order or the per-GPU shares land on the wrong cards.
+ _patch_split_pin_env(monkeypatch, inherited = [3, 1], reported = [1, 3])
+ env = {"CUDA_VISIBLE_DEVICES": "3,1"}
+ LlamaCppBackend._pin_visible_gpu_order_for_split(env)
+ assert env["CUDA_DEVICE_ORDER"] == "PCI_BUS_ID"
+ assert env["CUDA_VISIBLE_DEVICES"] == "1,3"
+
+
+def test_split_pin_keeps_mask_order_when_picker_reported_it(monkeypatch):
+ # Torch-fallback enumeration (no nvidia-smi) reports devices in inherited
+ # mask order, so the picker's split list follows the mask -- the pin must
+ # keep that order, not re-sort it into a mismatch.
+ _patch_split_pin_env(monkeypatch, inherited = [3, 1], reported = [3, 1])
+ env = {"CUDA_VISIBLE_DEVICES": "3,1"}
+ LlamaCppBackend._pin_visible_gpu_order_for_split(env)
+ assert env["CUDA_VISIBLE_DEVICES"] == "3,1"
+
+
+def test_split_pin_falls_back_to_ascending_without_report(monkeypatch):
+ # Enumeration unavailable: ascending physical is the best guess (it matches
+ # the dominant nvidia-smi report order).
+ _patch_split_pin_env(monkeypatch, inherited = [3, 1], reported = None)
+ env = {"CUDA_VISIBLE_DEVICES": "3,1"}
+ LlamaCppBackend._pin_visible_gpu_order_for_split(env)
+ assert env["CUDA_VISIBLE_DEVICES"] == "1,3"
+
+
+def test_split_pin_without_mask_only_sets_pci_order(monkeypatch):
+ # No inherited mask (or a UUID/MIG one resolving to None): enumeration order
+ # is fully fixed by CUDA_DEVICE_ORDER, so no mask is written.
+ _patch_split_pin_env(monkeypatch, inherited = None, reported = None)
+ env = {}
+ LlamaCppBackend._pin_visible_gpu_order_for_split(env)
+ assert env == {"CUDA_DEVICE_ORDER": "PCI_BUS_ID"}
+
+
+def test_split_pin_mirrors_hip_mask_on_rocm(monkeypatch):
+ # ROCm: the pin must land in HIP_VISIBLE_DEVICES too, and an inherited ROCR
+ # mask is cleared so the mask can't apply twice (ROCR re-indexes, then HIP
+ # would index into the already-reduced set).
+ _patch_split_pin_env(monkeypatch, inherited = [3, 1], reported = [1, 3])
+ torch_stub = _types.ModuleType("torch")
+ torch_stub.version = _types.SimpleNamespace(hip = "6.0")
+ monkeypatch.setitem(sys.modules, "torch", torch_stub)
+ env = {"CUDA_VISIBLE_DEVICES": "3,1", "ROCR_VISIBLE_DEVICES": "3,1"}
+ LlamaCppBackend._pin_visible_gpu_order_for_split(env)
+ assert env["CUDA_VISIBLE_DEVICES"] == "1,3"
+ assert env["HIP_VISIBLE_DEVICES"] == "1,3"
+ assert "ROCR_VISIBLE_DEVICES" not in env
+
+
+# ── Diffusion single-device selection ───────────────────────────────────────
+
+
+def test_diffusion_gpu_arg_uses_lowest_explicit_physical_id(monkeypatch):
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "3,1")
+ monkeypatch.setenv("DG_GPU", "7")
+ assert LlamaCppBackend._diffusion_gpu_arg([3, 1]) == "1"
+
+
+def test_diffusion_gpu_arg_preserves_parent_mask_order(monkeypatch):
+ monkeypatch.delenv("DG_GPU", raising = False)
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "3,1")
+ assert LlamaCppBackend._diffusion_gpu_arg(None) == "3"
+
+
+def test_diffusion_gpu_arg_honors_override_and_cpu_mask(monkeypatch):
+ monkeypatch.setenv("DG_GPU", "GPU-abc")
+ assert LlamaCppBackend._diffusion_gpu_arg(None) == "GPU-abc"
+ assert LlamaCppBackend._diffusion_gpu_arg(None, cpu_only = True) == ""
+
+
+# ── Deliberate zero-offload (manual gpu_layers=0): training-skip flag ─────────
+
+
+def test_zero_offload_flag_false_without_companions():
+ # CPU-only by construction: False lets training skip unloading a server that
+ # holds no VRAM.
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0", "--fit", "off"]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], {}) is False
+
+
+@pytest.mark.parametrize(
+ "companion",
+ ["--mmproj", "--model-draft", "-md", "--spec-draft-model", "-hfd"],
+)
+def test_zero_offload_flag_true_with_companion(companion):
+ # mmproj / a drafter offload to GPU regardless of --gpu-layers, so the
+ # server still holds VRAM and training must unload it. Drafter detection
+ # reuses the extras parser, so pass-through aliases count too.
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0", companion, "x.gguf"]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], {}) is True
+
+
+def test_zero_offload_flag_true_with_inline_companion_forms():
+ cmd = ["llama-server", "-m", "model.gguf", "--spec-draft-model=x.gguf"]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], {}) is True
+ cmd = ["llama-server", "-m", "model.gguf", "--mmproj=proj.gguf"]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], {}) is True
+
+
+def test_zero_offload_flag_true_with_env_drafter():
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0"]
+ env = {"LLAMA_ARG_SPEC_DRAFT_MODEL": "x.gguf"}
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], env) is True
+
+
+@pytest.mark.parametrize(
+ "device_args",
+ [
+ ["--device", "CUDA0"],
+ ["--device=CUDA0"],
+ ["-dev", "CUDA0"],
+ ["--spec-draft-device", "CUDA0"],
+ ["--device-draft=CUDA0"],
+ ],
+)
+def test_zero_offload_flag_true_with_device_pin(device_args):
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0", *device_args]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], {}) is True
+
+
+def test_zero_offload_flag_true_with_env_device_pin():
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0"]
+ env = {"LLAMA_ARG_DEVICE": "CUDA0"}
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], env) is True
+
+
+@pytest.mark.parametrize(
+ ("device_args", "env"),
+ [
+ (["--device", "cpu"], {}),
+ (["--device=none"], {}),
+ (["--spec-draft-device", "cpu"], {}),
+ ([], {"LLAMA_ARG_DEVICE": "none"}),
+ (["--device", "CUDA0", "--device", "cpu"], {}),
+ ],
+)
+def test_zero_offload_flag_false_with_cpu_device_pin(device_args, env):
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0", *device_args]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], env) is False
+
+
+def test_zero_offload_flag_true_with_surviving_tensor_mode():
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0", "--split-mode", "tensor"]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], {}) is True
+
+
+def test_zero_offload_flag_true_for_unmasked_vulkan(monkeypatch):
+ monkeypatch.setattr(LlamaCppBackend, "_is_vulkan_backend", staticmethod(lambda: True))
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0"]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [(0, 8000, 24000)], {}) is True
+
+
+def test_zero_offload_flag_none_without_gpus():
+ cmd = ["llama-server", "-m", "model.gguf", "--gpu-layers", "0"]
+ assert LlamaCppBackend._zero_offload_gpu_flag(cmd, [], {}) is None
+
+
+def test_cmd_has_gpu_companion_detection():
+ # The env mask for CPU-only zero-offload loads keys off this scan: any
+ # --mmproj form or a drafter (flag aliases / env) keeps the GPUs visible.
+ has = LlamaCppBackend._cmd_has_gpu_companion
+ assert has(["llama-server", "-m", "m.gguf"], {}) is False
+ assert has(["llama-server", "--mmproj", "p.gguf"], {}) is True
+ assert has(["llama-server", "--mmproj=p.gguf"], {}) is True
+ assert has(["llama-server", "-md", "d.gguf"], {}) is True
+ assert has(["llama-server"], {"LLAMA_ARG_SPEC_DRAFT_MODEL": "d.gguf"}) is True
+
+
+def test_cmd_companion_ignores_cpu_forced_drafter():
+ # A CPU-pinned drafter holds no VRAM: the zero-offload mask may hide the GPUs
+ # and training may leave the server alone.
+ has = LlamaCppBackend._cmd_has_gpu_companion
+ cmd = ["llama-server", "-md", "d.gguf", "--spec-draft-ngl", "0"]
+ assert has(cmd, {}) is False
+ cmd = ["llama-server", "-md", "d.gguf", "--spec-draft-device", "cpu"]
+ assert has(cmd, {}) is False
+ # mmproj still counts even alongside a CPU drafter.
+ cmd = ["llama-server", "-md", "d.gguf", "--spec-draft-ngl", "0", "--mmproj", "p.gguf"]
+ assert has(cmd, {}) is True
diff --git a/studio/backend/tests/test_gpu_selection.py b/studio/backend/tests/test_gpu_selection.py
index 69ad560788..d4f2fbe993 100644
--- a/studio/backend/tests/test_gpu_selection.py
+++ b/studio/backend/tests/test_gpu_selection.py
@@ -853,7 +853,13 @@ class TestRouteErrors(unittest.TestCase):
self.assertIn("only supported on CUDA devices", str(exc_info.exception))
- def test_inference_route_rejects_gpu_ids_for_gguf(self):
+ def test_inference_route_validates_gpu_ids_for_gguf(self):
+ # gpu_ids is now SUPPORTED for GGUF (the GPU picker), but still
+ # validated: a rejected pick surfaces as a clean 400, not the old
+ # "not supported for GGUF" rejection. Patch the validator so the test
+ # is deterministic regardless of the host's (or a prior test's) GPU env.
+ import utils.hardware.hardware as hardware_mod
+
inference_route = _load_route_module(
"inference_route_module_for_gguf_gpu_ids_test",
"routes/inference.py",
@@ -887,6 +893,11 @@ class TestRouteErrors(unittest.TestCase):
),
patch.object(inference_route.asyncio, "to_thread", new = _inline_to_thread),
patch.object(inference_route, "_hf_offline_if_dns_dead", nullcontext),
+ patch.object(
+ hardware_mod,
+ "resolve_requested_gpu_ids",
+ side_effect = ValueError("Invalid gpu_ids [0, 1]: rejected by test"),
+ ),
):
with self.assertRaises(HTTPException) as exc_info:
asyncio.run(
@@ -901,8 +912,11 @@ class TestRouteErrors(unittest.TestCase):
)
)
+ # The validator's ValueError becomes a clean 400 (not the removed
+ # "not supported for GGUF" rejection).
self.assertEqual(exc_info.exception.status_code, 400)
- self.assertIn("GGUF", exc_info.exception.detail)
+ self.assertIn("gpu_ids", exc_info.exception.detail.lower())
+ self.assertNotIn("not supported", exc_info.exception.detail.lower())
def test_training_route_returns_400_for_invalid_gpu_ids(self):
training_route = _load_route_module(
diff --git a/studio/backend/tests/test_llama_cpp_no_context_shift.py b/studio/backend/tests/test_llama_cpp_no_context_shift.py
index f320d29a02..662c918305 100644
--- a/studio/backend/tests/test_llama_cpp_no_context_shift.py
+++ b/studio/backend/tests/test_llama_cpp_no_context_shift.py
@@ -118,9 +118,17 @@ def test_flag_sits_inside_the_base_cmd_list():
"conditional branch -- otherwise some code paths would still "
"run with silent context shift enabled."
)
- # Pin that it sits next to -c / --ctx so the grouping makes sense.
- assert '"-c"' in block
assert '"--flash-attn"' in block
+ # -c is emitted in the conditional right after the base list, not inside
+ # it: auto-fit (--fit on with no pinned context) must omit -c entirely,
+ # because "-c 0" pins the full native context and disables --fit's
+ # VRAM-based sizing. Pin that it still sits next to the base block so the
+ # context grouping stays intact.
+ after = rest[end_rel : end_rel + 1000]
+ assert '"-c"' in after, (
+ "-c must still be emitted in the conditional immediately after the "
+ "base cmd list (omitted only in auto-fit, where --fit sizes context)."
+ )
def _iter_lines_with_offset(text: str):
diff --git a/studio/backend/tests/test_llama_cpp_props_readback.py b/studio/backend/tests/test_llama_cpp_props_readback.py
index 488645ee5a..fe1e67edad 100644
--- a/studio/backend/tests/test_llama_cpp_props_readback.py
+++ b/studio/backend/tests/test_llama_cpp_props_readback.py
@@ -225,31 +225,46 @@ def test_kv_unified_added_for_multi_slot():
"""Explicit --parallel N disables llama-server's auto-slots kv-unified
default, splitting -c into per-slot windows of -c/N; Unsloth must restore
the shared pool so one request can use the full advertised context."""
- flags = LlamaCppBackend._ctx_integrity_flags(4, False, 98304, 98304, _CAPS_ALL)
+ flags = LlamaCppBackend._ctx_integrity_flags(4, False, False, 98304, 98304, _CAPS_ALL)
assert "--kv-unified" in flags
def test_kv_unified_skipped_for_single_slot_or_old_build():
assert "--kv-unified" not in LlamaCppBackend._ctx_integrity_flags(
- 1, False, 98304, 98304, _CAPS_ALL
+ 1, False, False, 98304, 98304, _CAPS_ALL
)
assert "--kv-unified" not in LlamaCppBackend._ctx_integrity_flags(
- 4, False, 98304, 98304, _CAPS_NONE
+ 4, False, False, 98304, 98304, _CAPS_NONE
)
def test_fit_ctx_floors_explicit_request_under_fit():
- flags = LlamaCppBackend._ctx_integrity_flags(1, True, 98304, 98304, _CAPS_ALL)
+ # An explicit requested ctx floors --fit-ctx at that value on any --fit
+ # path, including legacy auto (auto_fit False).
+ flags = LlamaCppBackend._ctx_integrity_flags(1, True, False, 98304, 98304, _CAPS_ALL)
assert flags[flags.index("--fit-ctx") + 1] == "98304"
-def test_fit_ctx_skipped_without_fit_or_explicit_ctx_or_support():
+def test_fit_ctx_skipped_without_fit_or_support():
+ # No --fit on -> no --fit-ctx.
assert "--fit-ctx" not in LlamaCppBackend._ctx_integrity_flags(
- 1, False, 98304, 98304, _CAPS_ALL
+ 1, False, False, 98304, 98304, _CAPS_ALL
)
- assert "--fit-ctx" not in LlamaCppBackend._ctx_integrity_flags(1, True, 0, 262144, _CAPS_ALL)
+ # --fit on but the binary doesn't support --fit-ctx.
assert "--fit-ctx" not in LlamaCppBackend._ctx_integrity_flags(
- 1, True, 98304, 98304, _CAPS_NONE
+ 1, True, True, 98304, 98304, _CAPS_NONE
+ )
+
+
+def test_fit_ctx_floors_auto_request_at_8192_only_under_auto_fit():
+ # Manual + Auto (auto_fit) floors the auto window at 8192 so --fit can't
+ # shrink it to a tiny size.
+ flags = LlamaCppBackend._ctx_integrity_flags(1, True, True, 0, 262144, _CAPS_ALL)
+ assert flags[flags.index("--fit-ctx") + 1] == "8192"
+ # Legacy auto (fit on but not auto_fit) emits -c 0 to pin native, so the
+ # 8192 floor must NOT ride along and override that pin.
+ assert "--fit-ctx" not in LlamaCppBackend._ctx_integrity_flags(
+ 1, True, False, 0, 262144, _CAPS_ALL
)
diff --git a/studio/backend/tests/test_llama_server_args.py b/studio/backend/tests/test_llama_server_args.py
index ba52afad1c..c6d16363f8 100644
--- a/studio/backend/tests/test_llama_server_args.py
+++ b/studio/backend/tests/test_llama_server_args.py
@@ -747,6 +747,34 @@ def test_strip_shadowing_flags_defaults_strip_split_mode_too():
assert strip_shadowing_flags(["--split-mode", "tensor"]) == []
+def test_strip_offload_is_opt_in_and_covers_moe():
+ base = dict(
+ strip_context = False,
+ strip_cache = False,
+ strip_spec = False,
+ strip_template = False,
+ strip_split_mode = False,
+ )
+ # Default: offload (incl. MoE) flags are NOT stripped.
+ assert strip_shadowing_flags(["--n-cpu-moe", "8", "--top-k", "20"], **base) == [
+ "--n-cpu-moe",
+ "8",
+ "--top-k",
+ "20",
+ ]
+ # Opt-in strips layer AND MoE offload flags (value-aware), keeps the rest.
+ assert strip_shadowing_flags(
+ ["--n-cpu-moe", "8", "--gpu-layers", "33", "--fit", "off", "--top-k", "20"],
+ **base,
+ strip_offload = True,
+ ) == ["--top-k", "20"]
+ # Boolean --cpu-moe drops the flag only, not the following value.
+ assert strip_shadowing_flags(["--cpu-moe", "--seed", "-1"], **base, strip_offload = True) == [
+ "--seed",
+ "-1",
+ ]
+
+
@pytest.mark.parametrize(
"args",
[
@@ -796,6 +824,23 @@ def test_strip_split_mode_only_drops_tensor_split_too():
assert strip_split_mode_only(["-sm=tensor", "-ts=3,1"]) == []
+def test_strip_tensor_split_alone_preserves_split_mode():
+ # Manual mode emits its own --tensor-split, so an inherited ratio is dropped
+ # -- but the user's --split-mode row/none/layer choice (which the manual
+ # ratio toggle can't express) must survive. strip_tensor_split removes only
+ # the ratio, unlike strip_split_mode which removes the whole group.
+ out = strip_shadowing_flags(
+ ["--split-mode", "row", "--tensor-split", "1,1", "--top-k", "20"],
+ strip_context = False,
+ strip_cache = False,
+ strip_spec = False,
+ strip_template = False,
+ strip_split_mode = False,
+ strip_tensor_split = True,
+ )
+ assert out == ["--split-mode", "row", "--top-k", "20"]
+
+
def test_strip_shadowing_flags_keeps_model_draft_without_spec():
out = strip_shadowing_flags(
["--model-draft", "/custom/mtp.gguf"],
diff --git a/studio/backend/tests/test_tensor_parallel.py b/studio/backend/tests/test_tensor_parallel.py
index 06f72d3b9f..00c7aeac69 100644
--- a/studio/backend/tests/test_tensor_parallel.py
+++ b/studio/backend/tests/test_tensor_parallel.py
@@ -262,9 +262,12 @@ def test_proportional_tensor_split_is_emitted_in_tensor_mode():
src = _load_model_source()
assert '"--tensor-split"' in src
gate = src.find("if tensor_parallel:")
- ts = src.find('"--tensor-split"')
+ # Find the TP block's emission (after the gate); manual mode emits its own
+ # --tensor-split earlier in the source from the user's per-GPU shares.
+ ts = src.find('"--tensor-split"', gate)
nxt_else = src.find("self._tensor_parallel = False")
assert 0 <= gate < ts < nxt_else, "--tensor-split must be emitted under `if tensor_parallel:`"
+ assert "tp_tensor_split" in src[gate:nxt_else]
def test_mtp_decode_probe_wired_under_tensor_parallel():
diff --git a/studio/backend/tests/test_tp_vision_regression.py b/studio/backend/tests/test_tp_vision_regression.py
index fb0989b306..d1372ca415 100644
--- a/studio/backend/tests/test_tp_vision_regression.py
+++ b/studio/backend/tests/test_tp_vision_regression.py
@@ -126,10 +126,21 @@ _ALLOWED_TP_DROP_GUARDS = {
# Capability: --split-mode tensor aborted for this (binary, model) (#6415).
# Self-healing -- tried by default, skipped only after a real abort (vs #6416).
"tensor_parallel and self._tensor_split_aborts(binary, model_identifier)",
- # Capacity: tensor needs >= 2 GPUs clearing the compute-buffer reserve.
- "tensor_parallel and len(tp_gpus) < 2",
+ # Capacity: tensor needs >= 2 GPUs clearing the compute-buffer reserve. Gated
+ # on plan_tp (not raw tensor_parallel) so manual mode skips this planner (#6414).
+ "plan_tp and len(tp_gpus) < 2",
# Capacity: pooled usable VRAM can't hold weights + MTP reserve -> layer split.
"_tp_weight_budget_mib <= _tp_required_mib",
+ # Manual mode, Auto layers: --fit owns memory and is incompatible with a
+ # tensor split, so TP is dropped (surfaced via logger.info) before the
+ # cache-drop, so a quantized KV survives into the --fit load (#6414).
+ "tensor_parallel and gpu_memory_mode == 'manual' and (gpu_layers < 0)",
+ # Manual mode, explicit layers: a tensor split still needs >= 2 GPUs in use.
+ "tensor_parallel and gpu_memory_mode == 'manual' and (gpu_layers >= 0) and (self._effective_gpu_count(sorted(gpu_ids) if gpu_ids else None) < 2)",
+ # Manual mode, zero layers: nothing to split on the GPU, and a tensor-mode
+ # launch under the CPU-only GPU mask (no visible devices) aborts the server
+ # instead of the intended CPU-only load (#6414).
+ "gpu_memory_mode == 'manual' and gpu_layers == 0",
}
@@ -364,7 +375,7 @@ def test_compute_buffer_downgrade_preserves_multi_gpu_intent():
full GPU set too, so it is symmetric with the budget/geometry downgrades and
doesn't collapse a multi-GPU layer load to one card (reviewer.py P1 on #6659)."""
src = inspect.getsource(LlamaCppBackend.load_model)
- gate = src.find("tensor_parallel and len(tp_gpus) < 2")
+ gate = src.find("plan_tp and len(tp_gpus) < 2")
assert gate != -1
# Bound to exactly this block: from its gate to the next (budget) downgrade.
nxt = src.find("_tp_weight_budget_mib <= _tp_required_mib", gate)
diff --git a/studio/backend/utils/models/gguf_metadata.py b/studio/backend/utils/models/gguf_metadata.py
index c24ec28e1d..50b3cd3513 100644
--- a/studio/backend/utils/models/gguf_metadata.py
+++ b/studio/backend/utils/models/gguf_metadata.py
@@ -50,9 +50,11 @@ _CACHE_MAX_ENTRIES = 4096
# keyed by (file cache key, wanted key). None = key absent / file unreadable.
_BOOL_CACHE: Dict[Tuple[_CacheKey, str], Optional[bool]] = {}
-# Native training context length (``{arch}.context_length``). None = absent /
-# unreadable. Lets the UI show the real context ceiling before a model loads.
-_CONTEXT_CACHE: Dict[_CacheKey, Optional[int]] = {}
+# GGUF header dims for the staged/deferred-load UI: context_length, layer_count
+# (block_count), and moe_layer_count (block_count minus leading dense layers; 0
+# if not MoE). One cached pass fills all three so the staged sheet can size every
+# slider before the model loads. None = unreadable / not a GGUF.
+_DIMS_CACHE: Dict[_CacheKey, Optional[Dict[str, Optional[int]]]] = {}
def _cache_key(path: str) -> Optional[_CacheKey]:
@@ -142,32 +144,45 @@ def _parse_gguf_header(path: str) -> Optional[Dict[str, str]]:
return out
-def read_gguf_context_length(path: str) -> Optional[int]:
- """Return the GGUF's native training context length (``{arch}.context_length``),
- or ``None`` if missing/unreadable/not a GGUF. Cached by (path, mtime, size).
- Lets the UI populate the context slider before the model is loaded."""
+def read_gguf_staged_dims(path: str) -> Optional[Dict[str, Optional[int]]]:
+ """GGUF header dims for the staged-load UI in one cached pass:
+ ``{"context_length", "layer_count", "moe_layer_count"}``. Each may be None
+ when absent (moe_layer_count is 0 for a dense model). Returns ``None`` if not
+ a GGUF / unreadable. Cached by (path, mtime, size). Lets the staged sheet size
+ the context, GPU-layers and MoE sliders before the model loads."""
key = _cache_key(path)
if key is None:
return None
with _CACHE_LOCK:
- if key in _CONTEXT_CACHE:
- return _CONTEXT_CACHE[key]
- result = _parse_gguf_context_length(path)
+ if key in _DIMS_CACHE:
+ return _DIMS_CACHE[key]
+ result = _parse_gguf_staged_dims(path)
with _CACHE_LOCK:
- while len(_CONTEXT_CACHE) >= _CACHE_MAX_ENTRIES:
+ while len(_DIMS_CACHE) >= _CACHE_MAX_ENTRIES:
try:
- _CONTEXT_CACHE.pop(next(iter(_CONTEXT_CACHE)))
+ _DIMS_CACHE.pop(next(iter(_DIMS_CACHE)))
except StopIteration:
break
- _CONTEXT_CACHE[key] = result
+ _DIMS_CACHE[key] = result
return result
-def _parse_gguf_context_length(path: str) -> Optional[int]:
- # The context key is architecture-namespaced (``llama.context_length`` etc.),
- # so we learn the key only after reading ``general.architecture``. GGUF writes
- # general.* before arch.* keys, matching the loader's own parser.
- ctx_key: Optional[str] = None
+def read_gguf_context_length(path: str) -> Optional[int]:
+ """Native training context length (``{arch}.context_length``), or ``None``.
+ Thin accessor over read_gguf_staged_dims."""
+ dims = read_gguf_staged_dims(path)
+ return dims["context_length"] if dims else None
+
+
+def _parse_gguf_arch_uints(path: str, wanted_suffixes: frozenset[str]) -> Optional[Dict[str, int]]:
+ """Walk a GGUF header once and return the requested architecture-namespaced
+ uint (vtype 4/10) keys, e.g. ``{"block_count": 32}``. Keys are
+ ``{arch}.``; the arch is learned from ``general.architecture`` (GGUF
+ writes general.* before arch.* keys, matching the loader's own parser).
+ Returns ``None`` if not a GGUF / unreadable, else a dict (possibly empty or
+ partial when some keys are absent)."""
+ arch: Optional[str] = None
+ found: Dict[str, int] = {}
try:
with open(path, "rb") as f:
head = f.read(24)
@@ -204,28 +219,68 @@ def _parse_gguf_context_length(path: str) -> Optional[int]:
sbytes = f.read(slen)
if len(sbytes) < slen:
break
- ctx_key = f"{sbytes.decode('utf-8', 'replace')}.context_length"
- elif ctx_key is not None and key == ctx_key and vtype in (4, 10):
+ arch = sbytes.decode("utf-8", "replace")
+ elif (
+ arch is not None
+ and vtype in (4, 10)
+ and key.startswith(f"{arch}.")
+ and key[len(arch) + 1 :] in wanted_suffixes
+ ):
width = 4 if vtype == 4 else 8
n_bytes = f.read(width)
if len(n_bytes) < width:
break
- value = struct.unpack(" 0 else None
+ found[key[len(arch) + 1 :]] = struct.unpack(
+ " Optional[Dict[str, Optional[int]]]:
+ vals = _parse_gguf_arch_uints(
+ path,
+ frozenset(
+ {
+ "context_length",
+ "block_count",
+ "expert_count",
+ "leading_dense_block_count",
+ }
+ ),
+ )
+ if vals is None:
+ return None
+ ctx = vals.get("context_length")
+ block = vals.get("block_count")
+ # A real context/layer count is positive; treat 0/garbage as absent so the
+ # UI never builds a slider with max < min.
+ context_length = ctx if ctx and ctx > 0 else None
+ layer_count = block if block and block > 0 else None
+ # MoE layer count = block_count - leading dense layers, only when experts
+ # exist; else 0 (dense -> slider hidden). Mirrors n_moe_layers in
+ # core/inference/llama_cpp.py.
+ if not vals.get("expert_count") or not block:
+ moe_layer_count: Optional[int] = 0
+ else:
+ moe_layer_count = max(0, block - (vals.get("leading_dense_block_count") or 0))
+ return {
+ "context_length": context_length,
+ "layer_count": layer_count,
+ "moe_layer_count": moe_layer_count,
+ }
# Strings (8) and arrays (9) are handled inline.
diff --git a/studio/frontend/src/components/assistant-ui/model-selector/remembered-load-settings.ts b/studio/frontend/src/components/assistant-ui/model-selector/remembered-load-settings.ts
index ec75b17f20..08492ab480 100644
--- a/studio/frontend/src/components/assistant-ui/model-selector/remembered-load-settings.ts
+++ b/studio/frontend/src/components/assistant-ui/model-selector/remembered-load-settings.ts
@@ -2,7 +2,9 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
// Per-model pre-load inference settings, persisted in localStorage so the load
-// dialog can offer "Remember settings for ".
+// dialog can offer "Remember settings for ". GGUF picks only: every
+// field is a llama.cpp load knob, so all save/restore call sites gate on
+// GGUF-ness (a non-GGUF blob would only snapshot leftover standing values).
const KEY = "unsloth_load_settings";
@@ -12,14 +14,22 @@ export interface RememberedLoadSettings {
speculativeType: string | null;
specDraftNMax: number | null;
tensorParallel: boolean;
+ // GPU Memory controls. Optional so an older blob (which lacked them) still
+ // parses, leaving the live knobs untouched on apply. The mode is kept with the
+ // manual knobs (gpuLayers/nCpuMoe are ignored outside Manual mode). A null
+ // selectedGpuIds is meaningful (all GPUs), so it's distinguished from absent.
+ // The per-GPU split ratio is deliberately NOT remembered: it's positionally
+ // bound to the exact GPU set/order and unvalidated, so it would mismatch.
+ gpuMemoryMode?: "auto" | "manual";
+ gpuLayers?: number;
+ nCpuMoe?: number;
+ selectedGpuIds?: number[] | null;
}
-// Storage key for a pick's remembered settings. The remembered knobs are
-// VRAM-budget driven (context override, KV-cache dtype, tensor-parallel), so the
-// right values differ per quant. An HF repo collapses all its GGUF variants into
-// one `id`, so fold the variant in to scope settings per quant. Local .gguf
-// paths key by their file path (already file-specific); native drag-drop files
-// key by display label, so same-named files in different folders share an entry.
+// Storage key for a pick's remembered settings, scoped per quant (the VRAM-budget
+// knobs differ per quant). An HF repo collapses its GGUF variants into one `id`,
+// so fold the variant in. Local .gguf paths are already file-specific; native
+// drag-drop files key by display label, so same-named files share an entry.
export function rememberedLoadSettingsKey(selection: {
id: string;
ggufVariant?: string | null;
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index 0bf46e7343..7083f02288 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -45,12 +45,18 @@ import {
import {
type PendingImageEditReference,
type RagAutoInject,
+ GPU_LAYERS_AUTO,
+ loadedGpuMemoryFieldsUnlessStaged,
+ reconcilePersistedGpuIds,
resolveLoadedSpeculativeSettings,
resolveSpeculativeSettingsForLoad,
+ persistGpuMemoryModeOnLoad,
resolveToolsEnabledOnLoad,
saveSpeculativeType,
useChatRuntimeStore,
} from "../stores/chat-runtime-store";
+import { resolveFitMaxSeqLength, resolveManualAutoCtxPin } from "../presets/preset-policy";
+import { ensureGpuDeviceCache } from "@/hooks/use-gpu-info";
import { useExternalProvidersStore } from "../stores/external-providers-store";
import {
shouldPreserveFullOutput,
@@ -1489,6 +1495,13 @@ async function autoLoadSmallestModel(): Promise<{
max_seq_length: number;
is_lora: boolean;
gguf_variant?: string | null;
+ // GGUF-only: scopes the training guard to the same placement policy /load
+ // will use. Manual mode must match because it makes placement user-owned.
+ // The layer/MoE/split/KV/spec knobs are deliberately not sent: Auto mode's
+ // guard sizes conservatively, while Manual mode bypasses that estimate.
+ // The safetensors fallback omits both fields and uses HF auto-placement.
+ gpu_ids?: number[];
+ gpu_memory_mode?: "auto" | "manual";
}): Promise {
const validation = await validateModel({
...payload,
@@ -1520,12 +1533,18 @@ async function autoLoadSmallestModel(): Promise<{
return false;
}
const currentStore = useChatRuntimeStore.getState();
- const remembered = loadRememberedLoadSettings(
- rememberedLoadSettingsKey({
- id: candidate.id,
- ggufVariant: candidate.ggufVariant,
- }),
- );
+ // Blobs are saved for GGUF picks only (the sheet gates on it), so don't
+ // let a legacy non-GGUF blob feed a stale context/spec choice into a
+ // safetensors auto-load.
+ const remembered =
+ candidate.kind === "gguf"
+ ? loadRememberedLoadSettings(
+ rememberedLoadSettingsKey({
+ id: candidate.id,
+ ggufVariant: candidate.ggufVariant,
+ }),
+ )
+ : null;
const effectiveMaxSeqLength = resolveLoadMaxSeqLength({
modelId: candidate.id,
ggufVariant: candidate.ggufVariant,
@@ -1537,6 +1556,38 @@ async function autoLoadSmallestModel(): Promise<{
maxSeqLength: candidate.maxSeqLength,
presetSource: currentStore.activePresetSource,
});
+ // The GPU knobs are per-model, so read them from the same remembered
+ // settings that fed effectiveMaxSeqLength -- on a background auto-load the
+ // live store holds session defaults, not the saved Manual mode / layer pin /
+ // GPU pick. Absent fields fall back like applyRememberedLoadSettings: the
+ // mode to the store (a persisted standing preference), the per-model knobs to
+ // their defaults. The saved GPU pick is reconciled against the GPUs present
+ // now, like the interactive restore.
+ const effectiveGpuMemoryMode =
+ remembered?.gpuMemoryMode ?? currentStore.gpuMemoryMode;
+ const effectiveGpuLayers = remembered?.gpuLayers ?? GPU_LAYERS_AUTO;
+ const effectiveNCpuMoe = remembered?.nCpuMoe ?? 0;
+ if (remembered?.selectedGpuIds != null) {
+ // Warm the device cache first: on a cold cache the reconcile passes the
+ // saved pick through unvalidated, and a stale cross-host pick then fails
+ // the load with the picker hidden.
+ await ensureGpuDeviceCache();
+ }
+ const effectiveGpuIds =
+ remembered?.selectedGpuIds !== undefined
+ ? reconcilePersistedGpuIds(remembered.selectedGpuIds)
+ : null;
+ // Under Manual GPU memory + Auto layers, llama.cpp's --fit owns context
+ // sizing, so send 0 (or the pinned length). GGUF-only; a no-op otherwise.
+ // The context pin is per-model too, so it comes from remembered settings,
+ // not the live store.
+ const fitMaxSeqLength = resolveFitMaxSeqLength(
+ candidate.kind === "gguf",
+ effectiveGpuMemoryMode,
+ effectiveGpuLayers,
+ remembered?.contextLength ?? null,
+ effectiveMaxSeqLength,
+ );
const effectiveSpeculativeType =
remembered?.speculativeType ?? specSettings.speculativeType;
const effectiveSpecDraftNMax =
@@ -1544,9 +1595,16 @@ async function autoLoadSmallestModel(): Promise<{
if (
!(await canAutoLoad({
model_path: candidate.id,
- max_seq_length: effectiveMaxSeqLength,
+ max_seq_length: fitMaxSeqLength,
is_lora: false,
gguf_variant: candidate.ggufVariant,
+ // The same remembered-derived GPU pick the load below sends.
+ ...(candidate.kind === "gguf"
+ ? {
+ gpu_ids: effectiveGpuIds ?? undefined,
+ gpu_memory_mode: effectiveGpuMemoryMode,
+ }
+ : {}),
}))
) {
skippedAutoLoadCandidates.add(
@@ -1558,7 +1616,7 @@ async function autoLoadSmallestModel(): Promise<{
const loadResp = await loadModel({
model_path: candidate.id,
hf_token: hfToken,
- max_seq_length: effectiveMaxSeqLength,
+ max_seq_length: fitMaxSeqLength,
load_in_4bit: true,
is_lora: false,
gguf_variant: candidate.ggufVariant,
@@ -1567,8 +1625,22 @@ async function autoLoadSmallestModel(): Promise<{
speculative_type: effectiveSpeculativeType,
spec_draft_n_max: effectiveSpecDraftNMax,
tensor_parallel: remembered?.tensorParallel ?? false,
+ // GGUF-only: the safetensors fallback loads via HF auto-placement (no
+ // explicit pins). The split ratio is deliberately never remembered
+ // (positionally bound to an exact GPU set), so auto-load leaves llama.cpp's
+ // free-VRAM default in charge rather than sending a stale store value.
+ ...(candidate.kind === "gguf"
+ ? {
+ gpu_memory_mode: effectiveGpuMemoryMode,
+ gpu_layers: effectiveGpuLayers,
+ n_cpu_moe: effectiveNCpuMoe,
+ gpu_ids: effectiveGpuIds ?? undefined,
+ }
+ : {}),
});
saveSpeculativeType(effectiveSpeculativeType);
+ // Self-gates on is_gguf (skips diffusion), so persists only for a real GGUF load.
+ persistGpuMemoryModeOnLoad(loadResp, effectiveGpuMemoryMode);
useChatRuntimeStore
.getState()
.setCheckpoint(candidate.id, candidate.ggufVariant ?? undefined);
@@ -1597,6 +1669,15 @@ async function autoLoadSmallestModel(): Promise<{
store.setModels([...store.models, autoModel]);
}
if (candidate.kind === "gguf") {
+ // Keep an explicit Manual+Auto context pin the load just applied (so a
+ // later Apply doesn't silently revert it to auto-fit sizing), mirroring
+ // the interactive path's keepCustomCtx; other cases baseline on
+ // ggufContextLength.
+ const keepCustomCtx = resolveManualAutoCtxPin(
+ effectiveGpuMemoryMode,
+ effectiveGpuLayers,
+ remembered?.contextLength ?? null,
+ );
useChatRuntimeStore.setState({
ggufContextLength: loadResp.context_length ?? 131072,
ggufMaxContextLength:
@@ -1613,6 +1694,10 @@ async function autoLoadSmallestModel(): Promise<{
loadedKvCacheDtype: loadResp.cache_type_kv ?? null,
tensorParallel: loadResp.tensor_parallel ?? false,
loadedTensorParallel: loadResp.tensor_parallel ?? false,
+ ...loadedGpuMemoryFieldsUnlessStaged(loadResp, {
+ customContextLength: keepCustomCtx,
+ }),
+ loadedCustomContextLength: keepCustomCtx,
defaultChatTemplate: loadResp.chat_template ?? null,
chatTemplateOverride: null,
loadedChatTemplateOverride: null,
@@ -1633,6 +1718,9 @@ async function autoLoadSmallestModel(): Promise<{
loadedKvCacheDtype: loadResp.cache_type_kv ?? null,
tensorParallel: loadResp.tensor_parallel ?? false,
loadedTensorParallel: loadResp.tensor_parallel ?? false,
+ // Non-GGUF response: clears any stale GPU baseline a prior manual-GPU
+ // GGUF load left, matching the interactive/status sibling load paths.
+ ...loadedGpuMemoryFieldsUnlessStaged(loadResp),
defaultChatTemplate: loadResp.chat_template ?? null,
chatTemplateOverride: null,
loadedChatTemplateOverride: null,
@@ -1820,12 +1908,17 @@ async function autoLoadSmallestModel(): Promise<{
duration: 30000,
});
try {
+ const rt = useChatRuntimeStore.getState();
if (
!(await canAutoLoad({
model_path: "unsloth/Qwen3.5-4B-MTP-GGUF",
max_seq_length: 0,
is_lora: false,
gguf_variant: "UD-Q4_K_XL",
+ // The same live-store GPU pick the load below sends (a fresh default
+ // model has no remembered settings to prefer).
+ gpu_ids: rt.selectedGpuIds ?? undefined,
+ gpu_memory_mode: rt.gpuMemoryMode,
}))
) {
toast.dismiss(toastId);
@@ -1835,6 +1928,9 @@ async function autoLoadSmallestModel(): Promise<{
const loadResp = await loadModel({
model_path: "unsloth/Qwen3.5-4B-MTP-GGUF",
hf_token: hfToken,
+ // Model default under both modes: Auto layers + no pin means
+ // resolveFitMaxSeqLength returns 0 for every mode (the canAutoLoad
+ // preflight above sends the same).
max_seq_length: 0,
load_in_4bit: true,
is_lora: false,
@@ -1842,8 +1938,20 @@ async function autoLoadSmallestModel(): Promise<{
trust_remote_code: trustRemoteCode,
speculative_type: specSettings.speculativeType,
spec_draft_n_max: specSettings.specDraftNMax,
+ // GPU Memory mode is a standing preference, so honor it on auto-load.
+ // The layer/MoE/split knobs and the context pin are per-model: the live
+ // store may hold edits drafted for a staged pick, and a fresh default
+ // model has no remembered settings, so those stay at their defaults like
+ // the cached-candidate path. The GPU pick deliberately differs (it's the
+ // picker's current on-screen selection, which the canAutoLoad preflight
+ // above already committed to).
+ gpu_memory_mode: rt.gpuMemoryMode,
+ gpu_layers: GPU_LAYERS_AUTO,
+ n_cpu_moe: 0,
+ gpu_ids: rt.selectedGpuIds ?? undefined,
});
saveSpeculativeType(specSettings.speculativeType);
+ persistGpuMemoryModeOnLoad(loadResp, rt.gpuMemoryMode);
useChatRuntimeStore
.getState()
.setCheckpoint("unsloth/Qwen3.5-4B-MTP-GGUF", "UD-Q4_K_XL");
@@ -1880,6 +1988,10 @@ async function autoLoadSmallestModel(): Promise<{
loadedKvCacheDtype: loadResp.cache_type_kv ?? null,
tensorParallel: loadResp.tensor_parallel ?? false,
loadedTensorParallel: loadResp.tensor_parallel ?? false,
+ ...loadedGpuMemoryFieldsUnlessStaged(loadResp),
+ // Drives the GPU Memory controls' diffusion gate; set alongside the
+ // GPU fields on every load path so the gate can't read stale.
+ loadedIsDiffusion: loadResp.is_diffusion ?? false,
defaultChatTemplate: loadResp.chat_template ?? null,
chatTemplateOverride: null,
loadedIsMultimodal: isMultimodalResponse(loadResp),
diff --git a/studio/frontend/src/features/chat/api/chat-api.ts b/studio/frontend/src/features/chat/api/chat-api.ts
index ebf9461172..0f6af38033 100644
--- a/studio/frontend/src/features/chat/api/chat-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-api.ts
@@ -127,28 +127,38 @@ export async function validateModel(
native_path_lease: payload.nativePathLease ?? null,
hf_token: payload.hf_token,
gguf_variant: payload.gguf_variant ?? null,
- // Send the intended load settings so validate's VRAM check matches the
- // follow-up /load and doesn't unload for a load /load would then reject.
+ // Intended load settings so validate's preflight matches the follow-up
+ // /load. Default placement is sized against the selected GPUs.
max_seq_length: payload.max_seq_length,
load_in_4bit: payload.load_in_4bit,
+ gpu_ids: payload.gpu_ids,
+ // Manual placement is an explicit override: Auto layers use llama.cpp
+ // --fit, while a pinned layer count is owned by the user. Tell validate
+ // so it applies the same training-guard policy as /load.
+ gpu_memory_mode: payload.gpu_memory_mode,
}),
});
return parseJsonOrThrow(response);
}
/**
- * Read a GGUF's native context length from its local header (no GPU load, no
- * download). Returns null when the file isn't downloaded yet, the model isn't a
- * GGUF, or it's gated. For a native (drag-drop / picked) file, pass
- * `nativePathToken` so the backend reads the granted local path. Used by the
- * deferred-load staging flow to fill the context slider before the single load.
+ * Read a GGUF's header dims (native context length, total layer count, MoE
+ * expert-layer count) from its local file (no GPU load, no download). All are
+ * null when the file isn't downloaded yet, the model isn't a GGUF, or it's
+ * gated. For a native (drag-drop / picked) file, pass `nativePathToken` so the
+ * backend reads the granted local path. Used by the deferred-load staging flow
+ * to size the context, GPU-layers and MoE sliders before the single load.
*/
-export async function fetchGgufContextLength(payload: {
+export async function fetchGgufStagedMetadata(payload: {
model_path: string;
gguf_variant?: string | null;
hf_token?: string | null;
nativePathToken?: string | null;
-}): Promise {
+}): Promise<{
+ contextLength: number | null;
+ layerCount: number | null;
+ moeLayerCount: number | null;
+}> {
let nativePathLease: string | null = null;
if (payload.nativePathToken) {
try {
@@ -156,8 +166,8 @@ export async function fetchGgufContextLength(payload: {
await consumeNativePathToken(payload.nativePathToken, "validate-model")
).nativePathLease;
} catch {
- // Lease expired / revoked: degrade to no context (the load can re-mint).
- return null;
+ // Lease expired / revoked: degrade to no metadata (the load can re-mint).
+ return { contextLength: null, layerCount: null, moeLayerCount: null };
}
}
const response = await authFetch("/api/inference/validate", {
@@ -172,7 +182,11 @@ export async function fetchGgufContextLength(payload: {
}),
});
const res = await parseJsonOrThrow(response);
- return res.context_length ?? null;
+ return {
+ contextLength: res.context_length ?? null,
+ layerCount: res.layer_count ?? null,
+ moeLayerCount: res.moe_layer_count ?? null,
+ };
}
export async function unloadModel(payload: UnloadModelRequest): Promise {
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsx
index ec0ad977bf..217eaf8b6d 100644
--- a/studio/frontend/src/features/chat/chat-page.tsx
+++ b/studio/frontend/src/features/chat/chat-page.tsx
@@ -1445,9 +1445,11 @@ export function ChatPage({
// were already seeded on stage, so keepSpeculative only when a config was
// saved -- otherwise the standing speculative preference should win.
autoLoadStagedRef.current = (pending) => {
- const remembered = loadRememberedLoadSettings(
- rememberedLoadSettingsKey(pending),
- );
+ // Blobs are saved for GGUF picks only (the sheet gates on it), so don't
+ // let a legacy non-GGUF blob claim a seeded config here.
+ const remembered = hasGgufSource(pending)
+ ? loadRememberedLoadSettings(rememberedLoadSettingsKey(pending))
+ : null;
void selectModel({
...pending,
isDownloaded: true,
@@ -2813,6 +2815,11 @@ export function ChatPage({
selectModel({
id: state.params.checkpoint,
ggufVariant: state.activeGgufVariant ?? undefined,
+ // A native (drag-drop / picked) GGUF's checkpoint is only a display
+ // label, so the reload needs its path token to re-mint a lease --
+ // else applying the now-exposed GPU/context controls can't resolve
+ // the file. Null for non-native loads, which reload by id as before.
+ nativePathToken: state.activeNativePathToken ?? undefined,
forceReload: true,
isDownloaded: true,
loadingDescription: "Reloading with updated chat template.",
diff --git a/studio/frontend/src/features/chat/chat-settings-sheet.tsx b/studio/frontend/src/features/chat/chat-settings-sheet.tsx
index cedd298ecf..b368a811fa 100644
--- a/studio/frontend/src/features/chat/chat-settings-sheet.tsx
+++ b/studio/frontend/src/features/chat/chat-settings-sheet.tsx
@@ -55,6 +55,7 @@ import { Switch } from "@/components/ui/switch";
import { Textarea } from "@/components/ui/textarea";
import { InfoHint } from "@/components/ui/info-hint";
import { Tooltip, TooltipContent } from "@/components/ui/tooltip";
+import { useGpuDevices } from "@/hooks/use-gpu-info";
import { useIsMobile } from "@/hooks/use-mobile";
import { useLlamaUpdateCheck } from "@/hooks/use-llama-update-check";
import { cn } from "@/lib/utils";
@@ -99,8 +100,11 @@ import {
providerSupportsFastMode,
} from "./provider-capabilities";
import {
+ GPU_LAYERS_AUTO,
+ distributeByWeight,
isPendingGguf,
pendingSelectionMatches,
+ rebalanceSplit,
useChatRuntimeStore,
} from "./stores/chat-runtime-store";
import { RetrievalSettingsSection } from "@/features/rag/components/retrieval-settings-section";
@@ -250,6 +254,7 @@ function ParamSlider({
displayValue,
info,
valueSize,
+ disabled,
}: {
label: string;
value: number;
@@ -260,6 +265,7 @@ function ParamSlider({
displayValue?: string;
info?: ReactNode;
valueSize?: number;
+ disabled?: boolean;
}) {
return (
@@ -279,6 +285,7 @@ function ParamSlider({
displayValue={displayValue}
ariaLabel={label}
size={valueSize ?? 4}
+ disabled={disabled}
/>
onChange(snapToStep(v, step, min, max))}
className="panel-slider"
+ disabled={disabled}
/>
);
@@ -540,8 +548,17 @@ export function ChatSettingsPanel({
const base = slash >= 0 ? id.slice(slash + 1) : id;
return base || id;
})();
+ const activeNativePathToken = useChatRuntimeStore(
+ (s) => s.activeNativePathToken,
+ );
+ const loadedGgufContextLength = useChatRuntimeStore((s) => s.ggufContextLength);
+ // A GGUF loaded from a native path / direct .gguf has no HF variant, so key
+ // off the same signal the status hydration uses -- variant OR native token OR
+ // a GGUF context -- else the GPU Memory controls hide for a loaded local GGUF.
const isLoadedGguf =
- useChatRuntimeStore((s) => s.activeGgufVariant) != null;
+ useChatRuntimeStore((s) => s.activeGgufVariant) != null ||
+ activeNativePathToken != null ||
+ loadedGgufContextLength != null;
// While a pick is staged the sheet configures *that* model, so its GGUF-ness
// (not the currently loaded model's) decides whether the GGUF-only controls
// show. Otherwise a staged non-GGUF Hub repo would inherit the loaded GGUF's
@@ -607,6 +624,25 @@ export function ChatSettingsPanel({
const loadedTensorParallel = useChatRuntimeStore(
(s) => s.loadedTensorParallel,
);
+ const gpuMemoryMode = useChatRuntimeStore((s) => s.gpuMemoryMode);
+ const setGpuMemoryMode = useChatRuntimeStore((s) => s.setGpuMemoryMode);
+ const loadedGpuMemoryMode = useChatRuntimeStore((s) => s.loadedGpuMemoryMode);
+ const loadedIsDiffusion = useChatRuntimeStore((s) => s.loadedIsDiffusion);
+ const gpuLayers = useChatRuntimeStore((s) => s.gpuLayers);
+ const setGpuLayers = useChatRuntimeStore((s) => s.setGpuLayers);
+ const loadedGpuLayers = useChatRuntimeStore((s) => s.loadedGpuLayers);
+ const nCpuMoe = useChatRuntimeStore((s) => s.nCpuMoe);
+ const setNCpuMoe = useChatRuntimeStore((s) => s.setNCpuMoe);
+ const loadedNCpuMoe = useChatRuntimeStore((s) => s.loadedNCpuMoe);
+ const splitRatio = useChatRuntimeStore((s) => s.splitRatio);
+ const setSplitRatio = useChatRuntimeStore((s) => s.setSplitRatio);
+ const loadedSplitRatio = useChatRuntimeStore((s) => s.loadedSplitRatio);
+ const ggufLayerCount = useChatRuntimeStore((s) => s.ggufLayerCount);
+ const moeLayerCount = useChatRuntimeStore((s) => s.moeLayerCount);
+ const selectedGpuIds = useChatRuntimeStore((s) => s.selectedGpuIds);
+ const setSelectedGpuIds = useChatRuntimeStore((s) => s.setSelectedGpuIds);
+ const loadedGpuIds = useChatRuntimeStore((s) => s.loadedGpuIds);
+ const gpuDevices = useGpuDevices();
const chatTemplateOverride = useChatRuntimeStore(
(s) => s.chatTemplateOverride,
);
@@ -614,6 +650,9 @@ export function ChatSettingsPanel({
(s) => s.loadedChatTemplateOverride,
);
const customContextLength = useChatRuntimeStore((s) => s.customContextLength);
+ const loadedCustomContextLength = useChatRuntimeStore(
+ (s) => s.loadedCustomContextLength,
+ );
const setCustomContextLength = useChatRuntimeStore(
(s) => s.setCustomContextLength,
);
@@ -641,10 +680,14 @@ export function ChatSettingsPanel({
: null;
useEffect(() => {
if (!pendingKey) return;
- const saved = loadRememberedLoadSettings(pendingKey);
+ // GGUF-only, like the stageOrLoad / Hub restore paths: every remembered
+ // field is a llama.cpp knob, so a non-GGUF pick has nothing to restore --
+ // and applying its blob would clobber the standing gpuMemoryMode with a
+ // stale snapshot (the save on Load below is gated the same way).
+ const saved = pendingIsGguf ? loadRememberedLoadSettings(pendingKey) : null;
setRemember(saved != null);
if (saved) applyRememberedLoadSettings(saved);
- }, [pendingKey, applyRememberedLoadSettings]);
+ }, [pendingKey, pendingIsGguf, applyRememberedLoadSettings]);
// While staging, the sheet reflects the STAGED model, so its header context
// takes precedence over the loaded model's (which may differ or be larger).
const baseContext = pendingIsGguf ? stagedContextLength : ggufContextLength;
@@ -661,15 +704,132 @@ export function ChatSettingsPanel({
const ctxDisplayValue = customContextLength ?? baseContext ?? "";
const ctxMaxValue = baseNativeContext ?? baseContext ?? null;
const kvDirty = kvCacheDtype !== loadedKvCacheDtype;
- const ctxDirty = customContextLength !== null;
+ const ctxDirty = customContextLength !== loadedCustomContextLength;
const specDirty = speculativeType !== loadedSpeculativeType;
const specDraftDirty = specDraftNMax !== loadedSpecDraftNMax;
const tpDirty = tensorParallel !== (loadedTensorParallel ?? false);
+ // A loaded diffusion GGUF runs mode-agnostic (pins all layers on one GPU,
+ // ignores --fit/--gpu-layers), so the GPU Memory mode + manual controls don't
+ // apply -- hide them and don't let the preserved standing mode read as dirty.
+ // The GPU picker still applies (diffusion pins the chosen device). A staged pick
+ // keeps the controls (a pending pick's diffusion-ness isn't known until load).
+ const gpuModeApplies =
+ isGguf && (pendingSelection != null || !loadedIsDiffusion);
+ const gpuDirty =
+ gpuModeApplies && gpuMemoryMode !== (loadedGpuMemoryMode ?? "auto");
+ const isManual = gpuModeApplies && gpuMemoryMode === "manual";
+ // Manual with the GPU Layers slider at "Auto" (leftmost): --fit owns the whole
+ // layout, so the offload knobs (MoE, split, TP) don't apply.
+ const autoLayers = isManual && gpuLayers < 0;
+ // GPUs actually in use: the picked subset, or all visible when none picked.
+ const gpusInUse = selectedGpuIds ?? gpuDevices.map((d) => d.index);
+ // TP is off with fewer than 2 GPUs in use (single GPU, or the picker narrowed
+ // to one): tensor split is a no-op there and aborts on some archs. Mirrors the
+ // multi-GPU gate on the GPU picker / Split ratio. (Under Auto layers the whole
+ // TP control is hidden -- llama.cpp's --fit aborts under --split-mode tensor.)
+ const tpDisabled = gpusInUse.length <= 1;
+ // Manual gpu-layers ceiling = model layer count + 1 (else a safe fallback):
+ // llama.cpp counts the output layer as one more offloadable layer past the
+ // repeating blocks ("offloaded 33/33" needs -ngl 33 on a 32-block model), so
+ // the slider max must reach it or full offload is unreachable. While staging,
+ // use the staged model's layer count (read from its header).
+ const stagedLayerCount = pendingSelection?.layerCount ?? null;
+ const modelLayerCount = pendingIsGguf ? stagedLayerCount : ggufLayerCount;
+ const gpuLayersMax = modelLayerCount != null ? modelLayerCount + 1 : 256;
+ // MoE-offload slider: shown only for MoE models, capped at their MoE-layer
+ // count. While staging, use the staged model's count (read from its header);
+ // otherwise the loaded model's.
+ const stagedMoeLayerCount = pendingSelection?.moeLayerCount ?? null;
+ const moeLayersMax = pendingIsGguf
+ ? (stagedMoeLayerCount ?? 0)
+ : (moeLayerCount ?? 0);
+ const showMoeSlider = isManual && !autoLayers && moeLayersMax > 0;
+ // gpuLayers always counts; MoE only with an explicit layer count (see above).
+ const manualDirty =
+ isManual &&
+ (gpuLayers !== loadedGpuLayers ||
+ (!autoLayers && nCpuMoe !== (loadedNCpuMoe ?? 0)));
+ // GPU picker: only meaningful on multi-GPU, and only when the reported
+ // indices are physical (relative ordinals from a parent CUDA_VISIBLE_DEVICES
+ // mask can't be mapped back to pin a device). null = use all (auto).
+ const showGpuPicker =
+ isGguf &&
+ gpuDevices.length > 1 &&
+ gpuDevices.every((d) => d.physicalIndex);
+ const isGpuChecked = (index: number) =>
+ selectedGpuIds === null || selectedGpuIds.includes(index);
+ const toggleGpu = (index: number) => {
+ const all = gpuDevices.map((d) => d.index);
+ const current = selectedGpuIds ?? all;
+ const next = current.includes(index)
+ ? current.filter((i) => i !== index)
+ : [...current, index].sort((a, b) => a - b);
+ if (next.length === 0) return; // keep at least one GPU selected
+ setSelectedGpuIds(next.length === all.length ? null : next);
+ // The per-GPU split is positional, so any change to the set of GPUs in use
+ // invalidates it: drop it (the sliders fall back to the VRAM-weighted
+ // default). TP needs 2+ GPUs, so disable it when only one remains.
+ setSplitRatio(null);
+ if (next.length <= 1) {
+ setTensorParallel(false);
+ }
+ };
+ const gpuIdsKey = (ids: number[] | null) => (ids === null ? "auto" : ids.join(","));
+ const gpuIdsDirty = gpuIdsKey(selectedGpuIds) !== gpuIdsKey(loadedGpuIds);
+ // Per-GPU layer split (--tensor-split): manual + 2+ GPUs in use. One slider
+ // per GPU, each a layer count; together they sum to the GPU Layers total.
+ const showSplitRatio =
+ isManual && !autoLayers && showGpuPicker && gpusInUse.length > 1;
+ // The total the per-GPU counts sum to (the GPU Layers slider value); 0 under
+ // Auto, where the split is hidden. The devices behind the GPUs in use, for
+ // labels + the VRAM-weighted default.
+ const splitTotal = Math.max(0, Math.min(gpuLayers, gpuLayersMax));
+ const gpusInUseDevices = gpusInUse.map(
+ (i) => gpuDevices.find((d) => d.index === i) ?? null,
+ );
+ // Displayed per-GPU counts. splitRatio is a stable reference balance (only a
+ // slider edit changes it), rescaled to the current total; deriving rather than
+ // mutating it on GPU Layers changes keeps the balance intact when the total
+ // passes through low values or Auto. No saved split: free-VRAM-weighted default
+ // (llama.cpp's unset default splits by free VRAM, so the first edit starts from
+ // the default's placement, not a total-VRAM ratio that can land layers on a
+ // busy GPU). A genuine 0 (a full GPU) is a real weight, not missing data: the
+ // probe's no-data case degrades to the total server-side, and an all-zero list
+ // falls back to an even split in distributeByWeight. Not yet sent.
+ const splitCounts =
+ splitRatio && splitRatio.length === gpusInUse.length
+ ? distributeByWeight(splitTotal, splitRatio)
+ : distributeByWeight(
+ splitTotal,
+ gpusInUseDevices.map((d) => d?.memoryFreeGb ?? d?.memoryTotalGb ?? 1),
+ );
+ const setSplitCount = (k: number, v: number) =>
+ setSplitRatio(rebalanceSplit(splitTotal, splitCounts, k, v));
+ const splitRatioDirty =
+ isManual &&
+ !autoLayers &&
+ JSON.stringify(splitRatio ?? null) !== JSON.stringify(loadedSplitRatio ?? null);
+ // Auto-fit context (Manual + Auto layers): <= 0 means "Auto" (--fit sizes it);
+ // a positive value pins it. Surface the length --fit chose once it's loaded.
+ const fitCtxAuto = autoLayers && (customContextLength ?? 0) <= 0;
+ const loadedAutoLayers =
+ loadedGpuMemoryMode === "manual" && (loadedGpuLayers ?? GPU_LAYERS_AUTO) < 0;
+ const fitResolvedCtx =
+ fitCtxAuto && loadedAutoLayers ? ggufContextLength : null;
// A saved chat-template override is a reload-time setting too, so surface
// Apply for a template-only edit (otherwise it could never be applied).
const templateDirty = chatTemplateOverride !== loadedChatTemplateOverride;
const modelSettingsDirty =
- kvDirty || ctxDirty || specDirty || specDraftDirty || tpDirty || templateDirty;
+ kvDirty ||
+ ctxDirty ||
+ specDirty ||
+ specDraftDirty ||
+ tpDirty ||
+ gpuDirty ||
+ manualDirty ||
+ gpuIdsDirty ||
+ splitRatioDirty ||
+ templateDirty;
const [presetNameInput, setPresetNameInput] = useState(activePreset);
const [systemPromptEditorOpen, setSystemPromptEditorOpen] = useState(false);
const [systemPromptDraft, setSystemPromptDraft] = useState("");
@@ -980,7 +1140,64 @@ export function ChatSettingsPanel({
)}
{isGguf && (
<>
- {showContextControl && (
+ {showContextControl && (autoLayers ? (
+
+
+
+
+ Context Length
+
+
+ Auto: llama.cpp's --fit sizes the context to fit VRAM.
+ Set a length to pin it instead -- --fit then optimizes
+ GPU layer offload around it. The length --fit chose
+ shows here after loading.
+
+
+
{
+ setCustomContextLength(v > 0 ? v : null);
+ }}
+ ariaLabel="Context Length"
+ size={8}
+ disabled={modelControlsDisabled}
+ />
+
+
{
+ // Far-left snaps to Auto; otherwise to the nearest 1024.
+ if (v < 512) {
+ setCustomContextLength(null);
+ } else {
+ setCustomContextLength(Math.round(v / 1024) * 1024);
+ }
+ }}
+ className="panel-slider"
+ disabled={modelControlsDisabled}
+ />
+ {fitResolvedCtx != null && (
+
+ llama.cpp loaded {fitResolvedCtx.toLocaleString()} tokens.
+
+ )}
+
+ ) : (
@@ -1036,7 +1253,7 @@ export function ChatSettingsPanel({
)}
- )}
+ ))}
@@ -1191,6 +1408,163 @@ export function ChatSettingsPanel({
)}
>
)}
+ {gpuModeApplies && (
+
+
+
+ GPU Memory
+
+
+
+
+ Default: Unsloth
+ fits the model and context to your GPUs.
+
+
+ Manual: set GPU
+ Layers yourself. Leave it on Auto to let llama.cpp size
+ the context and offload overflow (including MoE experts)
+ to RAM.
+
+
+
+
+
+ {
+ setGpuMemoryMode(v as "auto" | "manual");
+ }}
+ // An in-flight staged load already snapshotted its
+ // settings, so edits here could not apply -- disable like
+ // the sibling context/KV/spec controls.
+ disabled={modelControlsDisabled}
+ >
+
+
+
+
+ Default
+ Manual
+
+
+
+
+ )}
+ {isManual && (
+ <>
+
+ Layers to keep on the GPU (--gpu-layers); the rest run
+ on CPU. Auto lets llama.cpp size the split (and the
+ context) to fit VRAM. At the maximum, the whole model
+ is on the GPU.
+ >
+ }
+ />
+ {showMoeSlider && (
+
+ Keep the experts of this many MoE layers on the CPU
+ (--n-cpu-moe) to save VRAM. 0 = all experts on the
+ GPU; at the maximum, all are on the CPU.
+ >
+ }
+ />
+ )}
+ {showSplitRatio && (
+
+
+
+ Layers per GPU
+
+
+ Splits GPU Layers across GPUs (--tensor-split).
+ Without Tensor Parallelism each value is the layer
+ count on that GPU; with it, every GPU holds a slice
+ of each layer, so the values are only a ratio.
+
+
+ {gpusInUseDevices.map((d, k) => (
+
setSplitCount(k, v)}
+ valueSize={6}
+ disabled={modelControlsDisabled}
+ />
+ ))}
+
+ )}
+ >
+ )}
+ {showGpuPicker && (
+
+
+
+ GPUs
+
+
+ Which GPUs this model may use. Unchecked GPUs are hidden
+ from llama.cpp (CUDA_VISIBLE_DEVICES, or
+ HIP_VISIBLE_DEVICES on ROCm). Leave all checked to use
+ every GPU.
+
+
+
+ {gpuDevices.map((d) => (
+
+
+ GPU {d.index}: {d.name}
+ {d.memoryTotalGb
+ ? ` · ${Math.round(d.memoryTotalGb)} GB`
+ : ""}
+
+ toggleGpu(d.index)}
+ data-test-id={`gpu-pick-${d.index}`}
+ disabled={modelControlsDisabled}
+ />
+
+ ))}
+
+
+ )}
+ {gpuModeApplies && !autoLayers && (
@@ -1206,10 +1580,11 @@ export function ChatSettingsPanel({
className="panel-switch shrink-0"
checked={tensorParallel}
onCheckedChange={setTensorParallel}
- disabled={modelControlsDisabled}
+ disabled={tpDisabled || modelControlsDisabled}
data-test-id="tensor-parallel-switch"
/>
+ )}
>
)}
{/* No persistent "enable custom code" toggle: it is consented per model
@@ -1228,14 +1603,21 @@ export function ChatSettingsPanel({
{Math.round((stagedDownloadFraction ?? 0) * 100)}%
)}
-
- setRemember(v === true)}
- />
- Remember settings next time
-
+ {/* GGUF picks only: a non-GGUF pick shows none of the load
+ knobs the blob captures, so there is nothing to remember. */}
+ {pendingIsGguf && (
+
+ setRemember(v === true)}
+ // The save/clear already ran in the Load click handler, so
+ // a mid-load toggle could not apply -- lock it like the knobs.
+ disabled={modelControlsDisabled}
+ />
+ Remember settings next time
+
+ )}
{stagedLoading ? (
// Mid-load: nothing to load or abandon until it settles, so disable.
) : null}
-
+ {/* The template override is a load-time knob too (applied on the next
+ reload) and the in-flight load already snapshotted it, so lock its
+ editors like the sibling controls -- a mid-load save would be
+ silently clobbered by the load response despite its toast. */}
+
)}
@@ -2086,7 +2477,7 @@ function BypassPermissionsToggle() {
);
}
-function ChatTemplateFields() {
+function ChatTemplateFields({ disabled = false }: { disabled?: boolean }) {
const defaultTemplate = useChatRuntimeStore((s) => s.defaultChatTemplate);
const override = useChatRuntimeStore((s) => s.chatTemplateOverride);
const setOverride = useChatRuntimeStore((s) => s.setChatTemplateOverride);
@@ -2120,7 +2511,8 @@ function ChatTemplateFields() {
Chat Template
@@ -2131,7 +2523,8 @@ function ChatTemplateFields() {
setOverride(null)}
- className="nav-icon-btn text-nav-icon-idle hover:bg-panel-surface-hover hover:text-black dark:hover:text-white"
+ disabled={disabled}
+ className="nav-icon-btn text-nav-icon-idle hover:bg-panel-surface-hover hover:text-black dark:hover:text-white disabled:pointer-events-none disabled:opacity-50"
aria-label="Revert chat template"
>
Cancel
-
+ {/* Also locked mid-load: an autoLoad can start with this dialog
+ already open, and a save then would be silently clobbered. */}
+
Save
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
index 10e0904e4f..3003b52230 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
@@ -29,9 +29,14 @@ import {
} from "../api/chat-api";
import { formatEta, formatRate } from "../utils/format-transfer";
import {
+ GPU_LAYERS_AUTO,
isLocalModelPath,
+ loadedGpuMemoryFields,
+ loadedGpuMemoryFieldsUnlessStaged,
pendingSelectionMatches,
+ persistGpuMemoryModeOnLoad,
readPersistedSpeculativeType,
+ reconcilePersistedGpuIds,
resolveToolsEnabledOnLoad,
saveSpeculativeType,
useChatRuntimeStore,
@@ -46,9 +51,12 @@ import {
} from "../lib/apply-inference-status-to-store";
import {
mergeBackendRecommendedInference,
+ resolveFitMaxSeqLength,
resolveLoadMaxSeqLength,
+ resolveManualAutoCtxPin,
} from "../presets/preset-policy";
import { recordLastLocalModelLoad } from "../utils/last-local-model-load";
+import { ensureGpuDeviceCache } from "@/hooks/use-gpu-info";
import {
isMultimodalResponse,
} from "../types/api";
@@ -291,9 +299,12 @@ async function syncInferenceStatusToStore(options?: {
if (statusRes.active_model && !isExternalSelectionActive) {
const checkpointId = resolveInferenceCheckpointId(statusRes);
if (checkpointId) {
+ const previousGgufVariant =
+ useChatRuntimeStore.getState().activeGgufVariant;
setCheckpoint(checkpointId, statusRes.gguf_variant);
applyActiveModelStatusToStore(statusRes, {
previousCheckpoint: selectedCheckpoint,
+ previousGgufVariant,
});
// setModels(listRes...) above used catalog data, which omits audio
// capability. Re-apply live status so attach gates survive a refresh.
@@ -511,7 +522,11 @@ export function useChatModelRuntime() {
typeof selection === "string" ? false : selection.isDownloaded ?? false;
const model = models.find((entry) => entry.id === modelId);
const lora = loras.find((entry) => entry.id === modelId);
- const isGguf = explicitIsGguf ?? model?.isGguf ?? false;
+ // A native path-token selection is a local GGUF by construction (the
+ // native model intents only grant .gguf files), but its id is a display
+ // label that need not end in ".gguf" -- without this, Manual + Auto
+ // layers would pin the UI context instead of letting --fit size it.
+ const isGguf = explicitIsGguf ?? model?.isGguf ?? nativePathToken != null;
const loraIsAdapter = lora?.exportType === "lora";
const isLora =
explicitIsLora ?? model?.isLora ?? loraIsAdapter ?? false;
@@ -578,18 +593,27 @@ export function useChatModelRuntime() {
let trustRemoteCode = stateBeforeUnload.params.trustRemoteCode ?? false;
let approvedRemoteCodeFingerprint: string | null = null;
const maxSeqLength = stateBeforeUnload.params.maxSeqLength;
+ const previousActiveNativePathToken =
+ stateBeforeUnload.activeNativePathToken;
const previousIsGguf =
previousModel?.isGguf === true
|| previousVariant != null
+ || previousActiveNativePathToken != null
|| (previousCheckpoint?.toLowerCase().endsWith(".gguf") ?? false);
- const rollbackMaxSeqLength = previousIsGguf
- ? (stateBeforeUnload.ggufContextLength ?? 0)
- : maxSeqLength;
+ // Respect the rolled-back model's auto-layers mode: a Manual+Auto model
+ // with an unpinned (auto) context must reload with 0 (so --fit
+ // re-auto-sizes), not the positive context it happened to pick (which
+ // the backend would treat as a pin).
+ const rollbackMaxSeqLength = resolveFitMaxSeqLength(
+ previousIsGguf,
+ stateBeforeUnload.loadedGpuMemoryMode ?? "auto",
+ stateBeforeUnload.loadedGpuLayers ?? GPU_LAYERS_AUTO,
+ stateBeforeUnload.loadedCustomContextLength,
+ previousIsGguf ? (stateBeforeUnload.ggufContextLength ?? 0) : maxSeqLength,
+ );
const hfToken = stateBeforeUnload.hfToken || null;
const previousModelRequiresTrustRemoteCode =
stateBeforeUnload.modelRequiresTrustRemoteCode;
- const previousActiveNativePathToken =
- stateBeforeUnload.activeNativePathToken;
// Snapshot the load settings at click time, before the awaits below
// (validation, the trust dialog, unload). For a staged Load these knobs
// stay editable and a sheet-close revert (abandonStagedModel) can fire
@@ -598,11 +622,29 @@ export function useChatModelRuntime() {
// updates this snapshot in lock-step so non-staged loads are unchanged.
const loadChatTemplateOverride = stateBeforeUnload.chatTemplateOverride;
const loadKvCacheDtype = stateBeforeUnload.kvCacheDtype;
- const loadCustomContextLength = stateBeforeUnload.customContextLength;
+ // gpuMemoryMode is a standing preference (kept across a model switch);
+ // the rest are per-model knobs the reset below clears, so they are
+ // re-baselined there in lock-step with the store.
+ let loadCustomContextLength = stateBeforeUnload.customContextLength;
const loadGgufContextLength = stateBeforeUnload.ggufContextLength;
const loadTensorParallel = stateBeforeUnload.tensorParallel;
const loadActivePresetSource = stateBeforeUnload.activePresetSource;
const loadActiveGgufVariant = stateBeforeUnload.activeGgufVariant;
+ const loadGpuMemoryMode = stateBeforeUnload.gpuMemoryMode;
+ let loadGpuLayers = stateBeforeUnload.gpuLayers;
+ let loadNCpuMoe = stateBeforeUnload.nCpuMoe;
+ let loadSplitRatio = stateBeforeUnload.splitRatio;
+ // Reconcile the persisted pick against the GPUs present now, so a stale
+ // cross-host / now-hidden pick is dropped before /load rather than
+ // rejected there. Warm the device cache first: load-on-selection can
+ // run before any GPU hook mounted, and a cold cache would pass the
+ // pick through unvalidated. validateGpuIds derives from this too.
+ if (stateBeforeUnload.selectedGpuIds != null) {
+ await ensureGpuDeviceCache();
+ }
+ let loadSelectedGpuIds = reconcilePersistedGpuIds(
+ stateBeforeUnload.selectedGpuIds,
+ );
let loadSpeculativeType = stateBeforeUnload.speculativeType;
let loadSpecDraftNMax = stateBeforeUnload.specDraftNMax;
try {
@@ -615,16 +657,47 @@ export function useChatModelRuntime() {
// context can exceed maxSeqLength, so sizing on raw maxSeqLength could
// pass, unload, then have /load refuse it. Uses the click-time
// snapshot (same values loadModel uses below), so the two agree.
- const validateMaxSeqLength = resolveLoadMaxSeqLength({
- modelId,
- ggufVariant,
- customContextLength: loadCustomContextLength,
- ggufContextLength: loadGgufContextLength,
- currentCheckpoint,
- activeGgufVariant: loadActiveGgufVariant,
- maxSeqLength,
- presetSource: loadActivePresetSource,
- });
+ // Mirror what /load does on a cross-model switch: the reset below
+ // clears the per-model Auto-layers context pin + GPU pick, and
+ // Manual+Auto sizes context through resolveFitMaxSeqLength.
+ // gpuMemoryMode is a standing preference, kept across the switch.
+ // A same-repo quant switch (same checkpoint, different gguf_variant)
+ // is a different model for per-model knobs: the pinned context,
+ // gpuLayers, GPU pick, and MoE offload are scoped per variant, so
+ // treat a variant change like a model switch and re-baseline them.
+ const switchingModelOrVariant =
+ currentCheckpoint !== modelId ||
+ (loadActiveGgufVariant ?? null) !== (ggufVariant ?? null);
+ const resetsPerModelSettings = Boolean(
+ currentCheckpoint && switchingModelOrVariant && !keepSpeculative,
+ );
+ const validateCustomContextLength = resetsPerModelSettings
+ ? null
+ : loadCustomContextLength;
+ const validateGpuIds = resetsPerModelSettings
+ ? null
+ : loadSelectedGpuIds;
+ // The reset below re-baselines gpuLayers to Auto; mirror it here.
+ const validateGpuLayers = resetsPerModelSettings
+ ? GPU_LAYERS_AUTO
+ : loadGpuLayers;
+ const validateMaxSeqLength = resolveFitMaxSeqLength(
+ isGguf,
+ loadGpuMemoryMode,
+ validateGpuLayers,
+ validateCustomContextLength,
+ resolveLoadMaxSeqLength({
+ modelId,
+ ggufVariant,
+ isGguf,
+ customContextLength: validateCustomContextLength,
+ ggufContextLength: loadGgufContextLength,
+ currentCheckpoint,
+ activeGgufVariant: loadActiveGgufVariant,
+ maxSeqLength,
+ presetSource: loadActivePresetSource,
+ }),
+ );
const validation = await validateModel({
model_path: modelId,
nativePathLease: validateNativePathLease,
@@ -633,6 +706,8 @@ export function useChatModelRuntime() {
load_in_4bit: true,
is_lora: isLora,
gguf_variant: ggufVariant ?? null,
+ gpu_ids: validateGpuIds ?? undefined,
+ ...(isGguf ? { gpu_memory_mode: loadGpuMemoryMode } : {}),
});
// Upgrade consent runs before the security dialogs; Accept installs and the load continues.
if (validation.requires_transformers_upgrade) {
@@ -697,18 +772,52 @@ export function useChatModelRuntime() {
// keepSpeculative skips this for a staged Load: the user picked the
// mode for this model on the sidebar, so honor it (the backend still
// falls back at runtime if the model has no MTP head).
- if (currentCheckpoint && currentCheckpoint !== modelId && !keepSpeculative) {
+ if (resetsPerModelSettings) {
const persistedSpeculativeType = readPersistedSpeculativeType();
useChatRuntimeStore.setState({
speculativeType: persistedSpeculativeType,
loadedSpeculativeType: persistedSpeculativeType,
specDraftNMax: null,
loadedSpecDraftNMax: null,
+ // Per-model GPU knobs must not follow onto a different model
+ // (gpuMemoryMode is a standing preference and is kept).
+ selectedGpuIds: null,
+ gpuLayers: GPU_LAYERS_AUTO,
+ nCpuMoe: 0,
+ splitRatio: null,
+ // A Manual+Auto context pin is per-model; clear it so a different
+ // model loads at Auto/native, not the previous model's pin.
+ customContextLength: null,
});
loadSpeculativeType = persistedSpeculativeType;
loadSpecDraftNMax = null;
+ // Keep the click-time snapshot in lock-step with the store reset so
+ // the load below sizes against the cleared per-model knobs, not the
+ // previous model's (gpuMemoryMode is standing, so left as captured).
+ loadCustomContextLength = null;
+ loadSelectedGpuIds = null;
+ loadGpuLayers = GPU_LAYERS_AUTO;
+ loadNCpuMoe = 0;
+ loadSplitRatio = null;
}
+ // Pinning layers on the SAME model keeps the currently resolved
+ // context: with no explicit pin, a manual+pinned reload would send 0,
+ // which the backend's --fit off branch treats as the NATIVE context --
+ // far larger than the sheet shows when the load was fit-sized (Default
+ // or Manual + Auto layers may auto-reduce context to fit VRAM), a
+ // likely OOM. ggufContextLength is that resolved value; a model already
+ // at native reloads unchanged, so this is safe for any prior mode.
+ if (
+ isGguf &&
+ !switchingModelOrVariant &&
+ loadGpuMemoryMode === "manual" &&
+ loadGpuLayers >= 0 &&
+ loadCustomContextLength == null &&
+ (loadGgufContextLength ?? 0) > 0
+ ) {
+ loadCustomContextLength = loadGgufContextLength;
+ }
const effectiveMaxSeqLength = resolveLoadMaxSeqLength({
modelId,
ggufVariant,
@@ -720,13 +829,20 @@ export function useChatModelRuntime() {
maxSeqLength,
presetSource: loadActivePresetSource,
});
+ const loadMaxSeqLength = resolveFitMaxSeqLength(
+ isGguf,
+ loadGpuMemoryMode,
+ loadGpuLayers,
+ loadCustomContextLength,
+ effectiveMaxSeqLength,
+ );
const effectiveChatTemplateOverride =
loadChatTemplateOverride?.trim() ? loadChatTemplateOverride : null;
const loadResponse = await loadModel({
model_path: modelId,
nativePathLease: loadNativePathLease,
hf_token: hfToken,
- max_seq_length: effectiveMaxSeqLength,
+ max_seq_length: loadMaxSeqLength,
load_in_4bit: true,
is_lora: isLora,
gguf_variant: ggufVariant ?? null,
@@ -737,6 +853,11 @@ export function useChatModelRuntime() {
speculative_type: loadSpeculativeType,
spec_draft_n_max: loadSpecDraftNMax,
tensor_parallel: loadTensorParallel,
+ gpu_memory_mode: loadGpuMemoryMode,
+ gpu_layers: loadGpuLayers,
+ n_cpu_moe: loadNCpuMoe,
+ tensor_split: loadSplitRatio ?? undefined,
+ gpu_ids: loadSelectedGpuIds ?? undefined,
});
// If cancelled while loading, don't update UI to show
@@ -747,6 +868,9 @@ export function useChatModelRuntime() {
// preference now (the requested intent, not the resolved echo;
// saveSpeculativeType keeps only the universal auto/ngram/off).
saveSpeculativeType(loadSpeculativeType);
+ // Persist the GPU Memory mode only on a successful load (not on
+ // dropdown change), so an abandoned selection doesn't stick.
+ persistGpuMemoryModeOnLoad(loadResponse, loadGpuMemoryMode);
const currentParams = useChatRuntimeStore.getState().params;
setParams(
@@ -782,9 +906,13 @@ export function useChatModelRuntime() {
const reportedNativeCtx = loadResponse.is_gguf
? (loadResponse.native_context_length ?? null)
: null;
- // A successful reload has applied settings, so clear pending custom
- // context state and display the backend-reported effective context.
- const keepCustomCtx = null;
+ // Keep an explicit Manual+Auto context pin (so a later Apply doesn't
+ // revert it to Auto); other cases baseline on ggufContextLength.
+ const keepCustomCtx = resolveManualAutoCtxPin(
+ loadGpuMemoryMode,
+ loadGpuLayers,
+ loadCustomContextLength,
+ );
const reasoningAlwaysOn = loadResponse.reasoning_always_on ?? false;
const reasoningStyle = loadResponse.reasoning_style ?? "enable_thinking";
const supportsReasoning = loadResponse.supports_reasoning ?? false;
@@ -837,11 +965,13 @@ export function useChatModelRuntime() {
loadedKvCacheDtype: loadedKv,
tensorParallel: loadedTp,
loadedTensorParallel: loadedTp,
+ ...loadedGpuMemoryFields(loadResponse),
speculativeType: loadedSpec,
loadedSpeculativeType: loadedSpec,
specDraftNMax: loadResponse.spec_draft_n_max ?? null,
loadedSpecDraftNMax: loadResponse.spec_draft_n_max ?? null,
customContextLength: keepCustomCtx,
+ loadedCustomContextLength: keepCustomCtx,
defaultChatTemplate: loadResponse.chat_template ?? null,
chatTemplateOverride: effectiveChatTemplateOverride,
loadedChatTemplateOverride: effectiveChatTemplateOverride,
@@ -938,7 +1068,7 @@ export function useChatModelRuntime() {
}
}
try {
- await loadModel({
+ const rollbackResponse = await loadModel({
model_path: previousCheckpoint,
nativePathLease: rollbackNativePathLease,
hf_token: hfToken,
@@ -951,14 +1081,51 @@ export function useChatModelRuntime() {
// Resend the previous model's pinned approval so restoring it is not re-blocked.
approved_remote_code_fingerprint:
approvedRemoteCodeFingerprints.get(previousCheckpoint) ?? null,
+ chat_template_override:
+ stateBeforeUnload.loadedChatTemplateOverride,
+ cache_type_kv: stateBeforeUnload.loadedKvCacheDtype,
+ speculative_type:
+ stateBeforeUnload.loadedSpeculativeType,
+ spec_draft_n_max:
+ stateBeforeUnload.loadedSpecDraftNMax,
// Restore the previous model in the split mode it was running,
// not the default layer split.
tensor_parallel: stateBeforeUnload.loadedTensorParallel ?? false,
+ gpu_memory_mode: stateBeforeUnload.loadedGpuMemoryMode ?? "auto",
+ gpu_layers: stateBeforeUnload.loadedGpuLayers ?? -1,
+ n_cpu_moe: stateBeforeUnload.loadedNCpuMoe ?? 0,
+ tensor_split: stateBeforeUnload.loadedSplitRatio ?? undefined,
+ gpu_ids: stateBeforeUnload.loadedGpuIds ?? undefined,
});
+ const rollbackSpeculativeType = normalizeSpeculativeType(
+ rollbackResponse.speculative_type,
+ );
useChatRuntimeStore.setState({
activeNativePathToken: previousActiveNativePathToken ?? null,
- loadedSpeculativeType: null,
- loadedSpecDraftNMax: null,
+ loadedSpeculativeType: rollbackSpeculativeType,
+ loadedSpecDraftNMax:
+ rollbackResponse.spec_draft_n_max ?? null,
+ loadedKvCacheDtype: rollbackResponse.cache_type_kv ?? null,
+ loadedChatTemplateOverride:
+ stateBeforeUnload.loadedChatTemplateOverride,
+ // Re-baseline the GPU knobs from the rolled-back load's own
+ // response (the shared seeding every load path uses): the
+ // refresh() below can't do it, since the status reseed is
+ // gated off while modelLoading is still true. A failed staged
+ // Load stays staged for retry, so the staged hold applies.
+ ...loadedGpuMemoryFieldsUnlessStaged(rollbackResponse, {
+ tensorParallel: rollbackResponse.tensor_parallel ?? false,
+ loadedTensorParallel:
+ rollbackResponse.tensor_parallel ?? false,
+ // refresh() is held while modelLoading remains true, so
+ // restore the rolled-back model's context pin directly.
+ customContextLength:
+ stateBeforeUnload.loadedCustomContextLength,
+ }),
+ loadedTensorParallel:
+ rollbackResponse.tensor_parallel ?? false,
+ loadedCustomContextLength:
+ stateBeforeUnload.loadedCustomContextLength,
});
await refresh();
} catch {
diff --git a/studio/frontend/src/features/chat/hooks/use-staged-model-preparation.ts b/studio/frontend/src/features/chat/hooks/use-staged-model-preparation.ts
index d8076c720b..a3e7a2d264 100644
--- a/studio/frontend/src/features/chat/hooks/use-staged-model-preparation.ts
+++ b/studio/frontend/src/features/chat/hooks/use-staged-model-preparation.ts
@@ -7,7 +7,7 @@ import { useRepoDownload } from "@/features/hub/download-manager/use-repo-downlo
import type { DownloadJob } from "@/features/hub/download-manager/use-repo-download";
import { useLatestRef } from "@/features/hub/hooks/use-latest-ref";
-import { fetchGgufContextLength } from "../api/chat-api";
+import { fetchGgufStagedMetadata } from "../api/chat-api";
import {
isPendingGguf,
pendingSelectionMatches,
@@ -46,8 +46,16 @@ export function useStagedModelPreparation(opts?: {
const pendingDownloaded = useChatRuntimeStore(
(s) => s.pendingSelection?.isDownloaded ?? false,
);
- const pendingHasContext = useChatRuntimeStore(
- (s) => s.pendingSelection?.contextLength != null,
+ // "Already probed" must key off layerCount / moeLayerCount, which only the
+ // full header probe fills (it sets all three together, so either is a
+ // reliable marker). contextLength alone can be list-seeded from
+ // /gguf-variants, which returns no layer/MoE counts -- treating it as
+ // complete would skip the probe and leave the GPU Layers slider at its 256
+ // fallback and the MoE slider hidden until the model loads.
+ const pendingHasMetadata = useChatRuntimeStore(
+ (s) =>
+ s.pendingSelection?.layerCount != null ||
+ s.pendingSelection?.moeLayerCount != null,
);
const setPendingSelection = useChatRuntimeStore((s) => s.setPendingSelection);
const onAutoLoadRef = useLatestRef(opts?.onAutoLoad);
@@ -69,25 +77,31 @@ export function useStagedModelPreparation(opts?: {
if (!current?.id || !isPendingGguf(current)) return;
const { id, ggufVariant, nativePathToken } = current;
try {
- const contextLength = await fetchGgufContextLength({
- model_path: id,
- gguf_variant: ggufVariant,
- hf_token: useChatRuntimeStore.getState().hfToken || null,
- nativePathToken,
- });
+ const { contextLength, layerCount, moeLayerCount } =
+ await fetchGgufStagedMetadata({
+ model_path: id,
+ gguf_variant: ggufVariant,
+ hf_token: useChatRuntimeStore.getState().hfToken || null,
+ nativePathToken,
+ });
// Apply only if the same model is still staged (the user may have switched
// picks or loaded/cancelled while the request was in flight).
const latest = useChatRuntimeStore.getState().pendingSelection;
if (
latest &&
- contextLength != null &&
- pendingSelectionMatches(latest, { id, ggufVariant, nativePathToken })
+ pendingSelectionMatches(latest, { id, ggufVariant, nativePathToken }) &&
+ (contextLength != null || layerCount != null || moeLayerCount != null)
) {
- setPendingSelection({ ...latest, contextLength });
+ setPendingSelection({
+ ...latest,
+ contextLength,
+ layerCount,
+ moeLayerCount,
+ });
}
} catch {
- // Leave contextLength null: the context slider stays hidden and the user
- // can still load (context fills in from the load response afterwards).
+ // Leave metadata null: the context/MoE sliders stay hidden and the user
+ // can still load (they fill in from the load response afterwards).
}
}, [setPendingSelection]);
@@ -125,7 +139,7 @@ export function useStagedModelPreparation(opts?: {
if (
!pendingId ||
(!pendingIsGguf && !pendingIsHubRepo) ||
- pendingHasContext
+ pendingHasMetadata
) {
return;
}
@@ -146,7 +160,7 @@ export function useStagedModelPreparation(opts?: {
pendingIsGguf,
pendingIsHubRepo,
pendingDownloaded,
- pendingHasContext,
+ pendingHasMetadata,
startDownloadRef,
fetchMetadataRef,
]);
diff --git a/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts b/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts
index a4b5f848e2..69bb38bbbe 100644
--- a/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts
+++ b/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts
@@ -2,13 +2,17 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { getInferenceStatus } from "../api/chat-api";
-import { mergeBackendRecommendedInference } from "../presets/preset-policy";
+import {
+ mergeBackendRecommendedInference,
+ resolveManualAutoCtxPin,
+} from "../presets/preset-policy";
import { clampReasoningEffortToLevels } from "../provider-capabilities";
import {
CHAT_REASONING_ENABLED_KEY,
type ReasoningEffort,
type ReasoningStyle,
loadOptionalBool,
+ loadedGpuMemoryFields,
resolveToolsEnabledOnLoad,
useChatRuntimeStore,
} from "../stores/chat-runtime-store";
@@ -20,6 +24,10 @@ import type { ChatModelSummary } from "../types/runtime";
type LocalReasoningEffort = Extract
;
+function sameArray(a: T[] | null, b: T[] | null): boolean {
+ return JSON.stringify(a) === JSON.stringify(b);
+}
+
// Canonicalises backend / persisted speculative mode values onto the UI modes.
export function normalizeSpeculativeType(
v: string | null | undefined,
@@ -119,6 +127,10 @@ function ensureActiveModelInStoreList(
export type ApplyInferenceStatusOptions = {
previousCheckpoint?: string;
+ /** activeGgufVariant BEFORE the caller's setCheckpoint synced it to the
+ * status -- without it a variant-only switch underneath the tab reads as
+ * steady state and the hydration reseed keeps the old quant's baselines. */
+ previousGgufVariant?: string | null;
};
/** Mirror refresh() hydration so adopted CLI models get reasoning/tools flags. */
@@ -144,9 +156,13 @@ export function applyActiveModelStatusToStore(
);
}
+ const previousGgufVariant =
+ options.previousGgufVariant !== undefined
+ ? options.previousGgufVariant
+ : store.activeGgufVariant;
const hydratingExistingModel =
previousCheckpoint !== checkpointId ||
- store.activeGgufVariant !== (status.gguf_variant ?? null);
+ previousGgufVariant !== (status.gguf_variant ?? null);
const supportsReasoning = status.supports_reasoning ?? false;
const reasoningAlwaysOn = status.reasoning_always_on ?? false;
const reasoningStyle = status.reasoning_style ?? "enable_thinking";
@@ -185,6 +201,66 @@ export function applyActiveModelStatusToStore(
// While a load is in flight, performLoad owns the load params. Seeding them
// from a stale poll here would clobber the values the load dialog just set.
const seedLoadParams = !prevState.modelLoading;
+ // A Manual + Auto-layers load sent its positive context pin as max_seq_length,
+ // and status only exposes the RESOLVED context; re-seed the pin from the
+ // requested value (parity with the load paths' keepCustomCtx). Baselines
+ // unconditionally: anything but an applicable pin is null, so a previous
+ // model's pin can't survive a model change underneath and reload at the old length.
+ const gpuPin = status.is_gguf
+ ? resolveManualAutoCtxPin(
+ status.gpu_memory_mode ?? "auto",
+ status.gpu_layers ?? -1,
+ status.requested_context_length ?? null,
+ )
+ : null;
+ const incomingGpuMode = status.is_gguf
+ ? (status.gpu_memory_mode ?? "auto")
+ : null;
+ const incomingGpuLayers =
+ incomingGpuMode === "manual" ? (status.gpu_layers ?? null) : null;
+ const incomingNCpuMoe =
+ incomingGpuMode === "manual" ? (status.n_cpu_moe ?? null) : null;
+ const incomingSplit =
+ incomingGpuMode === "manual" ? (status.tensor_split ?? null) : null;
+ const incomingGpuIds = status.is_gguf ? (status.gpu_ids ?? null) : null;
+ const gpuStatusChanged =
+ prevState.loadedGpuMemoryMode !== incomingGpuMode ||
+ prevState.loadedGpuLayers !== incomingGpuLayers ||
+ prevState.loadedNCpuMoe !== incomingNCpuMoe ||
+ !sameArray(prevState.loadedSplitRatio, incomingSplit) ||
+ !sameArray(prevState.loadedGpuIds, incomingGpuIds) ||
+ prevState.loadedCustomContextLength !== gpuPin;
+ const gpuMemoryEditsPending =
+ (prevState.loadedGpuMemoryMode !== null &&
+ prevState.gpuMemoryMode !== prevState.loadedGpuMemoryMode) ||
+ (prevState.loadedGpuMemoryMode === "manual" &&
+ (prevState.gpuLayers !== prevState.loadedGpuLayers ||
+ prevState.nCpuMoe !== prevState.loadedNCpuMoe ||
+ !sameArray(prevState.splitRatio, prevState.loadedSplitRatio))) ||
+ prevState.customContextLength !== prevState.loadedCustomContextLength;
+ const gpuIdsEditPending = !sameArray(
+ prevState.selectedGpuIds,
+ prevState.loadedGpuIds,
+ );
+ const incomingGpuFields = loadedGpuMemoryFields(status);
+ // A same-model reload from another client advances every loaded baseline.
+ // Preserve each editable group only when this tab has an unapplied change.
+ const preserveSameModelEdits = gpuStatusChanged && !hydratingExistingModel;
+ const gpuStatusFields = {
+ ...incomingGpuFields,
+ customContextLength: gpuPin,
+ loadedCustomContextLength: gpuPin,
+ ...(preserveSameModelEdits &&
+ gpuMemoryEditsPending && {
+ gpuMemoryMode: prevState.gpuMemoryMode,
+ gpuLayers: prevState.gpuLayers,
+ nCpuMoe: prevState.nCpuMoe,
+ splitRatio: prevState.splitRatio,
+ customContextLength: prevState.customContextLength,
+ }),
+ ...(preserveSameModelEdits &&
+ gpuIdsEditPending && { selectedGpuIds: prevState.selectedGpuIds }),
+ };
useChatRuntimeStore.setState({
supportsReasoning,
@@ -215,30 +291,51 @@ export function applyActiveModelStatusToStore(
loadedIsMultimodal: isMultimodalResponse(status),
loadedIsDiffusion: status.is_diffusion ?? false,
specFallbackReason: status.spec_fallback_reason ?? null,
+ // The spec / KV seeds share the GPU-fields reseed mechanism below: a
+ // non-GGUF status leaves their loaded baselines null, so the "unseeded"
+ // guard re-fires every refresh -- hold them too while a staged pick's
+ // settings are being edited, or the refresh resets the staged edit.
+ // hydratingExistingModel reopens every load-param seed: when the active
+ // model changed underneath this tab (auto-switch, another client), the
+ // old model's baselines are stale and must adopt the new status.
...(seedLoadParams &&
- prevState.loadedSpeculativeType === null && {
+ prevState.pendingSelection == null &&
+ (prevState.loadedSpeculativeType === null || hydratingExistingModel) && {
speculativeType: currentSpecType,
loadedSpeculativeType: currentSpecType,
}),
...(seedLoadParams &&
+ prevState.pendingSelection == null &&
status.spec_draft_n_max !== undefined &&
- prevState.loadedSpecDraftNMax === null &&
- prevState.specDraftNMax === null && {
+ (hydratingExistingModel ||
+ (prevState.loadedSpecDraftNMax === null &&
+ prevState.specDraftNMax === null)) && {
specDraftNMax: status.spec_draft_n_max ?? null,
loadedSpecDraftNMax: status.spec_draft_n_max ?? null,
}),
...(seedLoadParams &&
+ prevState.pendingSelection == null &&
status.cache_type_kv !== undefined &&
- prevState.loadedKvCacheDtype === null && {
+ (prevState.loadedKvCacheDtype === null || hydratingExistingModel) && {
kvCacheDtype: status.cache_type_kv,
loadedKvCacheDtype: status.cache_type_kv,
}),
...(seedLoadParams &&
+ prevState.pendingSelection == null &&
status.tensor_parallel !== undefined &&
- prevState.loadedTensorParallel === null && {
+ (prevState.loadedTensorParallel === null || hydratingExistingModel) && {
tensorParallel: status.tensor_parallel,
loadedTensorParallel: status.tensor_parallel,
}),
+ // Re-seed on first hydration, model/variant changes, or a same-model backend
+ // placement change. gpuStatusFields preserves dirty local edits in the last
+ // case while advancing their loaded baselines.
+ ...(seedLoadParams &&
+ prevState.pendingSelection == null &&
+ (prevState.loadedGpuMemoryMode === null ||
+ hydratingExistingModel ||
+ gpuStatusChanged) &&
+ gpuStatusFields),
...(status.chat_template_override !== undefined &&
prevState.loadedChatTemplateOverride === null &&
prevState.chatTemplateOverride === null && {
@@ -298,7 +395,11 @@ export async function tryAdoptServerActiveModel(): Promise {
if (previousCheckpoint) {
return true;
}
+ const previousGgufVariant = useChatRuntimeStore.getState().activeGgufVariant;
store.setCheckpoint(checkpointId, status.gguf_variant);
- applyActiveModelStatusToStore(status, { previousCheckpoint });
+ applyActiveModelStatusToStore(status, {
+ previousCheckpoint,
+ previousGgufVariant,
+ });
return true;
}
diff --git a/studio/frontend/src/features/chat/presets/preset-policy.ts b/studio/frontend/src/features/chat/presets/preset-policy.ts
index f96ee91f1b..23d79a35e1 100644
--- a/studio/frontend/src/features/chat/presets/preset-policy.ts
+++ b/studio/frontend/src/features/chat/presets/preset-policy.ts
@@ -339,3 +339,34 @@ export function resolveLoadMaxSeqLength({
}
return maxSeqLength;
}
+
+/**
+ * Adjust a resolved max-seq-length for the GPU Memory mode. Under Manual + Auto
+ * layers (GGUF, gpuLayers < 0) llama.cpp's --fit owns context sizing, so send 0
+ * (the backend omits -c) unless the user pinned a length; every other case keeps
+ * the resolved fallback. Shared by every GGUF load path so they can't drift.
+ */
+export function resolveFitMaxSeqLength(
+ isGguf: boolean | null | undefined,
+ gpuMemoryMode: "auto" | "manual",
+ gpuLayers: number,
+ customContextLength: number | null,
+ fallback: number,
+): number {
+ if (!isGguf || gpuMemoryMode !== "manual" || gpuLayers >= 0) return fallback;
+ return customContextLength && customContextLength > 0 ? customContextLength : 0;
+}
+
+// A Manual + Auto-layers load sends its positive context pin as max_seq_length;
+// keep it across a status reseed/Apply so the model isn't reverted to auto-fit
+// sizing. Anything else (Auto mode, pinned layers, no pin) baselines to null.
+// The caller keeps its own isGguf/targetIsGguf guard inline.
+export function resolveManualAutoCtxPin(
+ gpuMemoryMode: "auto" | "manual",
+ gpuLayers: number,
+ customContextLength: number | null,
+): number | null {
+ return gpuMemoryMode === "manual" && gpuLayers < 0 && (customContextLength ?? 0) > 0
+ ? customContextLength
+ : null;
+}
diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsx
index a0813fe27b..31e50ee60c 100644
--- a/studio/frontend/src/features/chat/shared-composer.tsx
+++ b/studio/frontend/src/features/chat/shared-composer.tsx
@@ -84,6 +84,8 @@ import {
useTransformersUpgradeDialogStore,
} from "@/features/transformers-upgrade";
import { loadModel, validateModel } from "./api/chat-api";
+import { resolveFitMaxSeqLength, resolveManualAutoCtxPin } from "./presets/preset-policy";
+import { ensureGpuDeviceCache } from "@/hooks/use-gpu-info";
import {
parseExternalModelId,
providerTypeSupportsVision,
@@ -95,8 +97,11 @@ import {
usePlusMenuPrefsStore,
} from "./stores/plus-menu-prefs-store";
import {
+ loadedGpuMemoryFieldsUnlessStaged,
type ReasoningEffort,
+ reconcilePersistedGpuIds,
resolveLoadedSpeculativeSettings,
+ persistGpuMemoryModeOnLoad,
resolveSpeculativeSettingsForLoad,
saveSpeculativeType,
useChatRuntimeStore,
@@ -1037,10 +1042,32 @@ export function SharedComposer({
return parts[parts.length - 1] || id;
}
+ // Warm the device cache before the snapshot below reconciles the GPU
+ // pick: on a cold cache the reconcile passes a stale pick through.
+ if (store.selectedGpuIds != null) {
+ await ensureGpuDeviceCache();
+ }
+ // The GPU/offload knobs both compare loads must use, snapshotted at Send.
+ // ensureModelLoaded runs sequentially and the first load's response echo
+ // (loadedGpuMemoryFields) rewrites the live store -- a non-GGUF or Auto
+ // first model resets gpuLayers/nCpuMoe/split/pick to defaults -- so
+ // reading the store per load would hand model 2 the first model's echoed
+ // defaults instead of the settings the user pressed Send with.
+ const compareLoadKnobs = {
+ gpuMemoryMode: store.gpuMemoryMode,
+ gpuLayers: store.gpuLayers,
+ nCpuMoe: store.nCpuMoe,
+ splitRatio: store.splitRatio,
+ // Reconcile the pick against the GPUs present now, like the model-switch
+ // path: an early remember-restore can hold a stale cross-host pick that
+ // /load would reject (the device cache is populated by send time).
+ selectedGpuIds: reconcilePersistedGpuIds(store.selectedGpuIds),
+ tensorParallel: store.tensorParallel,
+ customContextLength: store.customContextLength,
+ };
// Set when an accepted transformers install unloaded the active model
// server-side; a later failure must then clear the stale checkpoint.
let upgradeUnloadedActive = false;
-
// Helper: load a model and update store checkpoint
async function ensureModelLoaded(
sel: CompareModelSelection,
@@ -1057,15 +1084,35 @@ export function SharedComposer({
if (isAlreadyActive) {
return "ready";
}
+ const targetIsGguf =
+ sel.id.toLowerCase().endsWith(".gguf") || sel.ggufVariant != null;
+ // Size validation exactly as the load below, so the training-guard
+ // preflight checks the footprint that actually loads (under Manual + Auto
+ // layers the load sends 0 / the pinned context, not raw maxSeqLength).
+ const compareMaxSeqLength = resolveFitMaxSeqLength(
+ targetIsGguf,
+ compareLoadKnobs.gpuMemoryMode,
+ compareLoadKnobs.gpuLayers,
+ compareLoadKnobs.customContextLength,
+ maxSeqLength,
+ );
const validation = await validateModel({
model_path: sel.id,
hf_token: currentStore.hfToken || null,
- max_seq_length: maxSeqLength,
+ max_seq_length: compareMaxSeqLength,
load_in_4bit: true,
is_lora: sel.isLora,
gguf_variant: sel.ggufVariant ?? null,
trust_remote_code: loadTrustRemoteCode,
chat_template_override: effectiveChatTemplateOverride,
+ // Scope the validate to the picked GPUs. GGUF-only, like the load
+ // below: a non-GGUF target must not inherit a hidden GGUF GPU pick.
+ ...(targetIsGguf
+ ? {
+ gpu_ids: compareLoadKnobs.selectedGpuIds ?? undefined,
+ gpu_memory_mode: compareLoadKnobs.gpuMemoryMode,
+ }
+ : {}),
});
// Upgrade dialog first (mirrors the primary load path).
if (validation.requires_transformers_upgrade) {
@@ -1114,7 +1161,7 @@ export function SharedComposer({
const resp = await loadModel({
model_path: sel.id,
hf_token: useChatRuntimeStore.getState().hfToken || null,
- max_seq_length: maxSeqLength,
+ max_seq_length: compareMaxSeqLength,
load_in_4bit: true,
is_lora: sel.isLora,
gguf_variant: sel.ggufVariant ?? null,
@@ -1123,10 +1170,25 @@ export function SharedComposer({
chat_template_override: effectiveChatTemplateOverride,
speculative_type: specSettings.speculativeType,
spec_draft_n_max: specSettings.specDraftNMax,
- // Honor the Tensor Parallelism toggle on compare loads too.
- tensor_parallel: currentStore.tensorParallel,
+ // Honor the Tensor Parallelism + GPU Memory choices on compare loads.
+ // GGUF-only, like the auto-load path: the picker is a GGUF control,
+ // so a non-GGUF target loads via HF auto-placement instead of being
+ // pinned to a leftover GGUF pick it can't even show.
+ tensor_parallel: compareLoadKnobs.tensorParallel,
+ ...(targetIsGguf
+ ? {
+ gpu_memory_mode: compareLoadKnobs.gpuMemoryMode,
+ gpu_layers: compareLoadKnobs.gpuLayers,
+ n_cpu_moe: compareLoadKnobs.nCpuMoe,
+ tensor_split: compareLoadKnobs.splitRatio ?? undefined,
+ gpu_ids: compareLoadKnobs.selectedGpuIds ?? undefined,
+ }
+ : {}),
});
saveSpeculativeType(specSettings.speculativeType);
+ // Persist the GPU Memory mode on a non-diffusion GGUF compare-load too,
+ // so an applied manual choice survives a restart.
+ persistGpuMemoryModeOnLoad(resp, compareLoadKnobs.gpuMemoryMode);
upgradeUnloadedActive = false;
const store = useChatRuntimeStore.getState();
store.setCheckpoint(
@@ -1136,6 +1198,17 @@ export function SharedComposer({
store.setModelRequiresTrustRemoteCode(
resp.requires_trust_remote_code ?? false,
);
+ // Keep an explicit Manual+Auto context pin the load just applied (so a
+ // later Apply/Reset doesn't silently revert the model to auto-fit
+ // sizing), mirroring the interactive path's keepCustomCtx. Non-GGUF
+ // compare loads don't send the pin, so their baseline clears.
+ const keepCustomCtx = targetIsGguf
+ ? resolveManualAutoCtxPin(
+ compareLoadKnobs.gpuMemoryMode,
+ compareLoadKnobs.gpuLayers,
+ compareLoadKnobs.customContextLength,
+ )
+ : null;
useChatRuntimeStore.setState({
supportsReasoning: resp.supports_reasoning ?? false,
reasoningAlwaysOn: resp.reasoning_always_on ?? false,
@@ -1144,6 +1217,32 @@ export function SharedComposer({
supportsTools: resp.supports_tools ?? false,
tensorParallel: resp.tensor_parallel ?? false,
loadedTensorParallel: resp.tensor_parallel ?? false,
+ customContextLength: keepCustomCtx,
+ loadedCustomContextLength: keepCustomCtx,
+ // Seed the loaded GGUF context (interactive/auto-load parity): the
+ // settings sheet keys the GGUF GPU controls off it for a direct .gguf
+ // with no variant, and a later Apply reads it as the resolved context.
+ ...(targetIsGguf
+ ? {
+ ggufContextLength: resp.context_length ?? 131072,
+ ggufMaxContextLength:
+ resp.max_context_length ?? resp.context_length ?? 131072,
+ ggufNativeContextLength: resp.native_context_length ?? null,
+ }
+ : { ggufContextLength: null }),
+ // Compare loads resolve by id (HF repo / local path), never through a
+ // native-path lease, so a token left by a previously loaded native
+ // GGUF is stale here -- isLoadedGguf keys off it, and a stale token
+ // would dress a non-GGUF compare load in GGUF controls. Mirror the
+ // interactive path, which writes it on every load success.
+ activeNativePathToken: null,
+ // Held under an open staged pick: setCheckpoint preserves a stage on
+ // the empty->active transition, so a compare load can complete with
+ // staged GPU edits still on screen.
+ ...loadedGpuMemoryFieldsUnlessStaged(resp),
+ // Drives the GPU Memory controls' diffusion gate; set alongside the
+ // GPU fields on every load path so the gate can't read stale.
+ loadedIsDiffusion: resp.is_diffusion ?? false,
loadedIsMultimodal: isMultimodalResponse(resp),
...resolveLoadedSpeculativeSettings(resp),
});
diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
index 192ce1ec69..5786947118 100644
--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
@@ -7,6 +7,10 @@ import {
mirrorHfTokenInto,
useHfTokenStore,
} from "@/features/hub";
+import {
+ cachedPinnableGpuIndices,
+ ensureGpuDeviceCache,
+} from "@/hooks/use-gpu-info";
import { toast } from "@/lib/toast";
import { create } from "zustand";
import { isExternalModelId, parseExternalModelId } from "../external-providers";
@@ -74,6 +78,7 @@ export const CHAT_RAG_AUTOINJECT_MIN_SCORE_KEY =
export const CHAT_RAG_OCR_KEY = "unsloth_chat_rag_ocr_scanned";
export const CHAT_RAG_CAPTION_KEY = "unsloth_chat_rag_caption_figures";
export const CHAT_SPECULATIVE_TYPE_KEY = "unsloth_chat_speculative_type";
+export const CHAT_GPU_MEMORY_MODE_KEY = "unsloth_chat_gpu_memory_mode";
// Persist only the model-agnostic intents (auto/ngram/off). MTP modes
// (mtp/mtp+ngram) and spec_draft_n_max stay session-only: a persisted MTP
@@ -497,6 +502,213 @@ export function saveSpeculativeType(value: string | null): void {
}
}
+// GPU Memory strategy is a standing preference (like speculative type), not a
+// per-model setting: a "manual" choice persists across model switches and reloads.
+export function readPersistedGpuMemoryMode(): "auto" | "manual" {
+ return loadString(CHAT_GPU_MEMORY_MODE_KEY, "auto") === "manual" ? "manual" : "auto";
+}
+
+export function saveGpuMemoryMode(value: "auto" | "manual"): void {
+ saveString(CHAT_GPU_MEMORY_MODE_KEY, value);
+}
+
+/** Persist the GPU Memory mode after a load, but only for a non-diffusion GGUF:
+ * non-GGUF has no such mode, and diffusion runs mode-agnostic (reports "auto"),
+ * so neither must clobber the standing manual preference. */
+export function persistGpuMemoryModeOnLoad(
+ resp: { is_gguf?: boolean; is_diffusion?: boolean },
+ mode: "auto" | "manual",
+): void {
+ if (resp.is_gguf && !resp.is_diffusion) saveGpuMemoryMode(mode);
+}
+
+// Manual-mode gpu_layers sentinel: -1 = Auto (hand layer + context sizing to
+// llama.cpp's --fit). The Manual default; "all on GPU" is the slider's max.
+export const GPU_LAYERS_AUTO = -1;
+
+// Round real-valued shares to integers summing exactly to `total`, giving the
+// leftover units to the largest fractional parts (largest-remainder method).
+function largestRemainder(shares: number[], total: number): number[] {
+ const out = shares.map((x) => Math.floor(x));
+ let rem = total - out.reduce((a, b) => a + b, 0);
+ const byFrac = shares
+ .map((x, i) => ({ i, frac: x - Math.floor(x) }))
+ .sort((a, b) => b.frac - a.frac);
+ for (let k = 0; rem > 0 && k < byFrac.length; k++, rem--) out[byFrac[k].i] += 1;
+ return out;
+}
+
+// Spread `total` layers across GPUs in proportion to `weights` (e.g. per-GPU
+// VRAM), as integers summing exactly to `total`; even split for all-zero/empty
+// weights. Default per-GPU layer split before the user edits it (mirrors
+// llama.cpp's free-VRAM default).
+export function distributeByWeight(total: number, weights: number[]): number[] {
+ if (weights.length === 0) return [];
+ const t = Math.max(0, Math.floor(total));
+ const sum = weights.reduce((a, b) => a + b, 0);
+ const w = sum > 0 ? weights : weights.map(() => 1);
+ const wSum = w.reduce((a, b) => a + b, 0);
+ return largestRemainder(
+ w.map((x) => (t * x) / wSum),
+ t,
+ );
+}
+
+// Set GPU `index` to `value` and rebalance the rest so per-GPU counts still sum
+// to `total`; others absorb the remainder in proportion to their counts (evenly
+// if all zero). The --tensor-split editor: counts are sent verbatim, and
+// llama.cpp gives each GPU exactly its count when gpu_layers == sum(counts).
+export function rebalanceSplit(
+ total: number,
+ counts: number[],
+ index: number,
+ value: number,
+): number[] {
+ const v = Math.max(0, Math.min(value, total));
+ const out = counts.slice();
+ const otherIdx = counts.map((_, i) => i).filter((i) => i !== index);
+ // No other GPU to absorb the remainder: this one holds everything.
+ if (otherIdx.length === 0) {
+ out[index] = total;
+ return out;
+ }
+ out[index] = v;
+ const dist = distributeByWeight(
+ total - v,
+ otherIdx.map((i) => counts[i]),
+ );
+ otherIdx.forEach((i, k) => (out[i] = dist[k]));
+ return out;
+}
+
+// Validate a persisted gpu_ids pick against the GPUs present right now, before
+// restoring it from remembered settings. Returns null (= automatic) when the
+// pick is stale (none of the saved ids exist, or the host can't pin a multi-GPU
+// set), so a saved [1] on a now-1-GPU host doesn't get sent and rejected with no
+// way to clear it. A null pick (= automatic) passes through unchanged, and an
+// unpopulated device cache leaves the pick alone (the backend still guards).
+export function reconcilePersistedGpuIds(
+ ids: number[] | null,
+): number[] | null {
+ if (ids == null) return ids;
+ const pinnable = cachedPinnableGpuIndices();
+ if (pinnable === null) return ids; // cache not ready: can't validate, keep it
+ const kept = ids.filter((i) => pinnable.includes(i));
+ return kept.length > 0 ? kept : null;
+}
+
+// Store fields derived from a load/status response's GPU-memory settings.
+// Shared by every load path so the manual-knob round-trip can't drift.
+export function loadedGpuMemoryFields(resp: {
+ is_gguf?: boolean;
+ is_diffusion?: boolean;
+ gpu_memory_mode?: "auto" | "manual";
+ gpu_layers?: number;
+ n_cpu_moe?: number;
+ tensor_split?: number[] | null;
+ n_layers?: number | null;
+ n_moe_layers?: number;
+ gpu_ids?: number[] | null;
+}) {
+ // GPU-memory state is meaningful only for a GGUF chat load. A non-GGUF response
+ // still carries gpu_memory_mode (its default "auto" is serialized), so gate on
+ // the authoritative is_gguf flag, not the field's presence -- otherwise loading
+ // a transformers model would reset the standing manual preference.
+ if (!resp.is_gguf) {
+ // Clear the GPU pick / offload baseline a prior GGUF load may have left, so it
+ // reflects the non-GGUF model (no pin) -- else a stale loadedGpuIds reads as
+ // dirty (gpuIdsDirty is ungated) and Reset restores it while the picker is
+ // hidden. gpuMemoryMode (the standing preference) is kept, but its loaded
+ // baseline clears to null so Reset preserves the preference, not a stale mode.
+ return {
+ selectedGpuIds: null,
+ loadedGpuIds: null,
+ loadedGpuMemoryMode: null,
+ gpuLayers: GPU_LAYERS_AUTO,
+ loadedGpuLayers: null,
+ nCpuMoe: 0,
+ loadedNCpuMoe: null,
+ splitRatio: null,
+ loadedSplitRatio: null,
+ ggufLayerCount: null,
+ moeLayerCount: null,
+ };
+ }
+ const mode = resp.gpu_memory_mode ?? "auto";
+ const gpuIds = resp.gpu_ids ?? null;
+ // Layer/MoE/split knobs apply (and are reported) only in manual mode; in auto
+ // the server ignores them, so don't seed the loaded baseline or the editable
+ // knobs with values it never applied. In manual, the server reports gpu_layers
+ // = -1 under Auto, which round-trips the slider back to its Auto position.
+ const manualKnobs =
+ mode === "manual"
+ ? {
+ loadedGpuLayers: resp.gpu_layers ?? null,
+ loadedNCpuMoe: resp.n_cpu_moe ?? null,
+ loadedSplitRatio: resp.tensor_split ?? null,
+ gpuLayers: resp.gpu_layers ?? GPU_LAYERS_AUTO,
+ nCpuMoe: resp.n_cpu_moe ?? 0,
+ splitRatio: resp.tensor_split ?? null,
+ }
+ : {
+ loadedGpuLayers: null,
+ loadedNCpuMoe: null,
+ loadedSplitRatio: null,
+ // Auto ignores these, so reset the editable knobs too (not just the
+ // loaded baseline) -- else a later switch back to Manual would snapshot
+ // and send a previous model's stale gpuLayers/nCpuMoe/split that this
+ // load never applied. Mirrors the non-GGUF branch above.
+ gpuLayers: GPU_LAYERS_AUTO,
+ nCpuMoe: 0,
+ splitRatio: null,
+ };
+ return {
+ // A diffusion GGUF runs mode-agnostic (pins all layers on one GPU, reports
+ // "auto"), so adopt everything a chat GGUF does EXCEPT the live standing
+ // preference -- the next chat load must still honor the user's manual choice.
+ // The loaded baseline is still "auto", but the UI hides mode controls for a
+ // loaded diffusion model so it can't read as dirty against the preference.
+ ...(resp.is_diffusion ? {} : { gpuMemoryMode: mode }),
+ loadedGpuMemoryMode: mode,
+ ggufLayerCount: resp.n_layers ?? null,
+ // MoE expert-layer count: the n_cpu_moe slider max, and 0 hides the slider.
+ moeLayerCount: resp.n_moe_layers ?? null,
+ // The picker reflects what loaded (the request sent the user's pick).
+ selectedGpuIds: gpuIds,
+ loadedGpuIds: gpuIds,
+ ...manualKnobs,
+ };
+}
+
+/** loadedGpuMemoryFields (plus any seedExtras), unless a staged pick is open.
+ *
+ * With a staged pick open (the load fired mid-staging), preserve its editable
+ * GPU knobs and seedExtras, but still advance every loaded baseline. Otherwise
+ * cancelling the stage restores its edits onto the newly loaded model. The
+ * status reseed cannot repair that while pendingSelection holds it off.
+ */
+export function loadedGpuMemoryFieldsUnlessStaged(
+ resp: Parameters[0],
+ seedExtras?: T,
+) {
+ const fields = loadedGpuMemoryFields(resp);
+ if (useChatRuntimeStore.getState().pendingSelection != null) {
+ return {
+ loadedGpuMemoryMode: fields.loadedGpuMemoryMode,
+ loadedGpuLayers: fields.loadedGpuLayers,
+ loadedNCpuMoe: fields.loadedNCpuMoe,
+ loadedSplitRatio: fields.loadedSplitRatio,
+ loadedGpuIds: fields.loadedGpuIds,
+ // These are metadata ceilings for the model that actually loaded, not
+ // editable values from the open stage. Advance them with the baselines
+ // so abandoning the stage cannot expose the previous model's limits.
+ ggufLayerCount: fields.ggufLayerCount,
+ moeLayerCount: fields.moeLayerCount,
+ };
+ }
+ return { ...fields, ...seedExtras };
+}
+
/** A local model staged for a deferred load (see `pendingSelection`). Shape is
* a subset of the load hook's `SelectedModelInput`, structurally assignable. */
export type PendingModelSelection = {
@@ -515,6 +727,13 @@ export type PendingModelSelection = {
* Scoped here (not the shared `ggufContextLength`) so a staged model's
* metadata never pollutes the currently-loaded model's context display. */
contextLength?: number | null;
+ /** Total layer count (GGUF block_count); the manual gpu-layers ceiling is
+ * this + 1 (llama.cpp counts the output layer as offloadable too);
+ * scoped here like contextLength. */
+ layerCount?: number | null;
+ /** MoE expert-layer count from the GGUF header (manual --n-cpu-moe ceiling);
+ * 0 for dense models, scoped here like contextLength. */
+ moeLayerCount?: number | null;
/** "Load on selection" on + un-cached GGUF: download via the manager (global
* indicator) without opening the sheet, then load once the download finishes. */
autoLoad?: boolean;
@@ -743,6 +962,32 @@ type ChatRuntimeStore = {
tensorParallel: boolean;
/** Backend-reported tensor-parallel state; null until first hydrated. */
loadedTensorParallel: boolean | null;
+ /** GPU memory strategy for GGUF loads. "auto" = Unsloth picks GPUs and context
+ * to fit; "manual" = you own the offload (gpuLayers < 0 = Auto/--fit, >= 0
+ * pins layers + nCpuMoe). */
+ gpuMemoryMode: "auto" | "manual";
+ /** Backend-reported gpu memory mode; null until first hydrated. */
+ loadedGpuMemoryMode: "auto" | "manual" | null;
+ /** Manual mode: layers to offload to GPU. -1 = Auto (--fit); >= model layer
+ * count = all. */
+ gpuLayers: number;
+ loadedGpuLayers: number | null;
+ /** Manual mode: MoE expert layers to keep on CPU (--n-cpu-moe); 0 = none. */
+ nCpuMoe: number;
+ loadedNCpuMoe: number | null;
+ /** Manual mode: per-GPU layer counts (--tensor-split), in GPU-in-use order;
+ * null = unset (llama.cpp splits by free VRAM). */
+ splitRatio: number[] | null;
+ /** Backend-reported per-GPU split ratio (--tensor-split); null = unset. */
+ loadedSplitRatio: number[] | null;
+ /** Model layer count (GGUF block_count); the manual gpu-layers ceiling is
+ * this + 1 (the output layer is offloadable too). */
+ ggufLayerCount: number | null;
+ /** MoE expert-layer count: the nCpuMoe slider max; 0/null hides the slider. */
+ moeLayerCount: number | null;
+ /** Picked physical GPU indices (null = use all / automatic). */
+ selectedGpuIds: number[] | null;
+ loadedGpuIds: number[] | null;
/** Persisted: when false, picking a local model stages it as
* `pendingSelection` (and opens settings) instead of loading immediately,
* so load settings can be set before the single load. */
@@ -766,6 +1011,9 @@ type ChatRuntimeStore = {
* per step, cleared when the run ends, never persisted into the transcript. */
activeDiffusionCanvas: DiffusionCanvasFrame | null;
customContextLength: number | null;
+ /** The pinned context the loaded model used (null = Auto), so dirty-tracking
+ * and a later fit Apply can tell an explicit pin apart from Auto. */
+ loadedCustomContextLength: number | null;
defaultChatTemplate: string | null;
chatTemplateOverride: string | null;
loadedChatTemplateOverride: string | null;
@@ -884,6 +1132,11 @@ type ChatRuntimeStore = {
* which skip the sheet but must still honor a saved config. */
applyRememberedLoadSettings: (settings: RememberedLoadSettings) => void;
setTensorParallel: (value: boolean) => void;
+ setGpuMemoryMode: (mode: "auto" | "manual") => void;
+ setGpuLayers: (value: number) => void;
+ setNCpuMoe: (value: number) => void;
+ setSplitRatio: (value: number[] | null) => void;
+ setSelectedGpuIds: (ids: number[] | null) => void;
setLoadOnSelection: (value: boolean) => void;
setExpandQuantizations: (value: boolean) => void;
setShowAllQuantizations: (value: boolean) => void;
@@ -1101,11 +1354,12 @@ function setScalarSettingVersion(
/** The "revert to the loaded model" baseline for the editable load knobs.
* Shared by resetModelSettingsToLoaded (full revert) and stageModel (which
- * overrides speculative to start a fresh pick from the standing default). */
+ * overrides speculative and the per-model GPU knobs to start a fresh pick). */
function loadedBaselineSettings(s: ChatRuntimeStore) {
const hasLoadedModel = Boolean(s.params.checkpoint);
return {
- customContextLength: null,
+ // Revert to the loaded model's pin (null = Auto), not a blanket Auto.
+ customContextLength: s.loadedCustomContextLength,
kvCacheDtype: s.loadedKvCacheDtype,
tensorParallel: s.loadedTensorParallel ?? false,
speculativeType: hasLoadedModel
@@ -1113,6 +1367,20 @@ function loadedBaselineSettings(s: ChatRuntimeStore) {
: readPersistedSpeculativeType(),
specDraftNMax: hasLoadedModel ? s.loadedSpecDraftNMax : null,
chatTemplateOverride: s.loadedChatTemplateOverride,
+ // GPU memory mode is a standing preference; revert to the loaded model's
+ // mode (or the persisted default when nothing is loaded). Manual knobs and
+ // the GPU pick are per-model and revert to their loaded baseline. A loaded
+ // model with no applicable mode -- diffusion ("auto" baseline) or non-GGUF
+ // (null baseline) -- keeps the live preference so Reset can't drop it.
+ gpuMemoryMode: !hasLoadedModel
+ ? readPersistedGpuMemoryMode()
+ : s.loadedIsDiffusion
+ ? s.gpuMemoryMode
+ : (s.loadedGpuMemoryMode ?? s.gpuMemoryMode),
+ gpuLayers: s.loadedGpuLayers ?? GPU_LAYERS_AUTO,
+ nCpuMoe: s.loadedNCpuMoe ?? 0,
+ splitRatio: s.loadedSplitRatio ?? null,
+ selectedGpuIds: s.loadedGpuIds,
};
}
@@ -1213,6 +1481,18 @@ export const useChatRuntimeStore = create((set, get) => ({
loadedSpecDraftNMax: null,
tensorParallel: false,
loadedTensorParallel: null,
+ gpuMemoryMode: readPersistedGpuMemoryMode(),
+ loadedGpuMemoryMode: null,
+ gpuLayers: GPU_LAYERS_AUTO,
+ loadedGpuLayers: null,
+ nCpuMoe: 0,
+ loadedNCpuMoe: null,
+ splitRatio: null,
+ loadedSplitRatio: null,
+ ggufLayerCount: null,
+ moeLayerCount: null,
+ selectedGpuIds: null,
+ loadedGpuIds: null,
loadOnSelection: loadBool(CHAT_LOAD_ON_SELECTION_KEY, true),
expandQuantizations: loadBool(CHAT_EXPAND_QUANTIZATIONS_KEY, false),
showAllQuantizations: loadBool(CHAT_SHOW_ALL_QUANTIZATIONS_KEY, true),
@@ -1221,6 +1501,7 @@ export const useChatRuntimeStore = create((set, get) => ({
loadedIsMultimodal: false,
loadedIsDiffusion: false,
customContextLength: null,
+ loadedCustomContextLength: null,
defaultChatTemplate: null,
chatTemplateOverride: null,
loadedChatTemplateOverride: null,
@@ -1455,9 +1736,23 @@ export const useChatRuntimeStore = create((set, get) => ({
loadedSpecDraftNMax: null,
tensorParallel: false,
loadedTensorParallel: null,
+ // Standing preference: survives unload, unlike the per-model knobs above.
+ gpuMemoryMode: readPersistedGpuMemoryMode(),
+ loadedGpuMemoryMode: null,
+ gpuLayers: GPU_LAYERS_AUTO,
+ loadedGpuLayers: null,
+ nCpuMoe: 0,
+ loadedNCpuMoe: null,
+ splitRatio: null,
+ loadedSplitRatio: null,
+ ggufLayerCount: null,
+ moeLayerCount: null,
+ selectedGpuIds: null,
+ loadedGpuIds: null,
loadedIsMultimodal: false,
loadedIsDiffusion: false,
customContextLength: null,
+ loadedCustomContextLength: null,
defaultChatTemplate: null,
chatTemplateOverride: null,
loadedChatTemplateOverride: null,
@@ -1753,17 +2048,67 @@ export const useChatRuntimeStore = create((set, get) => ({
setSpeculativeType: (speculativeType) => set({ speculativeType }),
setSpecDraftNMax: (specDraftNMax) => set({ specDraftNMax }),
setTensorParallel: (tensorParallel) => set({ tensorParallel }),
+ // Standing preference, but persisted only on a successful load (see
+ // use-chat-model-runtime), not on selection -- so an unapplied pick the user
+ // resets/abandons doesn't stick to the next session.
+ setGpuMemoryMode: (gpuMemoryMode) => set({ gpuMemoryMode }),
+ setGpuLayers: (gpuLayers) => set({ gpuLayers }),
+ setNCpuMoe: (nCpuMoe) => set({ nCpuMoe }),
+ setSplitRatio: (splitRatio) => set({ splitRatio }),
+ setSelectedGpuIds: (selectedGpuIds) => set({ selectedGpuIds }),
resetModelSettingsToLoaded: () => set((s) => loadedBaselineSettings(s)),
- applyRememberedLoadSettings: (settings) =>
+ applyRememberedLoadSettings: (settings) => {
+ const gpuCacheWasCold = cachedPinnableGpuIndices() === null;
+ const restoredGpuIds =
+ settings.selectedGpuIds !== undefined
+ ? reconcilePersistedGpuIds(settings.selectedGpuIds)
+ : undefined;
// Coalesce every field: a blob persisted by an older/newer build can omit
// keys, and a raw spread would push `undefined` into fields typed non-null.
+ // The GPU knobs are spread only when present, but first reset the per-model
+ // ones to defaults: this path (load-on-selection) starts from the loaded
+ // model's baseline and skips the model-switch reset, so a blob omitting
+ // gpuLayers/nCpuMoe/selectedGpuIds (older build) or splitRatio (never
+ // remembered) must not inherit the previous model's placement. gpuMemoryMode
+ // (standing preference) is NOT reset, only applied when the blob carries it;
+ // selectedGpuIds keeps a meaningful null (all GPUs), so it keys off undefined.
set({
+ gpuLayers: GPU_LAYERS_AUTO,
+ nCpuMoe: 0,
+ splitRatio: null,
+ selectedGpuIds: null,
customContextLength: settings.contextLength ?? null,
kvCacheDtype: settings.kvCacheDtype ?? null,
speculativeType: settings.speculativeType ?? "auto",
specDraftNMax: settings.specDraftNMax ?? null,
tensorParallel: settings.tensorParallel ?? false,
- }),
+ ...(settings.gpuMemoryMode != null && {
+ gpuMemoryMode: settings.gpuMemoryMode,
+ }),
+ ...(settings.gpuLayers != null && { gpuLayers: settings.gpuLayers }),
+ ...(settings.nCpuMoe != null && { nCpuMoe: settings.nCpuMoe }),
+ ...(restoredGpuIds !== undefined && {
+ // Reconcile against the GPUs present now (see reconcilePersistedGpuIds):
+ // a saved [1] on a 1-GPU host (or under relative/UUID visibility) would
+ // hide the picker yet still send gpu_ids, which the backend rejects.
+ selectedGpuIds: restoredGpuIds,
+ }),
+ });
+ // A cold cache makes the synchronous restore provisional. Reconcile again
+ // when the shared fetch completes, but only if this exact restored array is
+ // still current so a user edit, stage change, or load cannot be overwritten.
+ if (gpuCacheWasCold && restoredGpuIds != null) {
+ void ensureGpuDeviceCache().then(() => {
+ set((state) => {
+ if (state.selectedGpuIds !== restoredGpuIds) return state;
+ const reconciled = reconcilePersistedGpuIds(restoredGpuIds);
+ return reconciled === restoredGpuIds
+ ? state
+ : { selectedGpuIds: reconciled };
+ });
+ });
+ }
+ },
setLoadOnSelection: (loadOnSelection) => {
saveBool(CHAT_LOAD_ON_SELECTION_KEY, loadOnSelection);
set({ loadOnSelection });
@@ -1798,6 +2143,22 @@ export const useChatRuntimeStore = create((set, get) => ({
// Load's keepSpeculative) a forced MTP mode onto a model that may lack it.
speculativeType: readPersistedSpeculativeType(),
specDraftNMax: null,
+ // Keep the on-screen GPU Memory selection (loadedBaselineSettings would
+ // otherwise revert it to the loaded model's mode, dropping a Manual choice
+ // just made). Use the live store value, not the persisted one, which can
+ // lag a mode hydrated from an out-of-band load.
+ gpuMemoryMode: s.gpuMemoryMode,
+ // Per-model GPU knobs start from defaults too so a fresh pick doesn't
+ // inherit the loaded model's layer/MoE/split/GPU choices, matching the
+ // immediate-switch reset.
+ gpuLayers: GPU_LAYERS_AUTO,
+ nCpuMoe: 0,
+ splitRatio: null,
+ selectedGpuIds: null,
+ // Fresh pick starts at Auto context (loadedBaselineSettings would
+ // otherwise restore the current model's pin). Leaves the baseline
+ // intact, like the GPU knobs, so abandoning restores the loaded pin.
+ customContextLength: null,
};
});
},
diff --git a/studio/frontend/src/features/chat/types/api.ts b/studio/frontend/src/features/chat/types/api.ts
index d72c406fdd..c24ddde5f5 100644
--- a/studio/frontend/src/features/chat/types/api.ts
+++ b/studio/frontend/src/features/chat/types/api.ts
@@ -65,6 +65,18 @@ export interface LoadModelRequest {
* of by layer for GGUF models. Multi-GPU only; no effect on a single GPU.
*/
tensor_parallel?: boolean | null;
+ /** GPU memory strategy for GGUF models. "auto" (default): Unsloth selects GPUs
+ * and caps context to fit VRAM. "manual": you own the offload -- gpu_layers
+ * -1 (Auto) hands sizing to llama.cpp's --fit, >= 0 pins layers/n_cpu_moe. */
+ gpu_memory_mode?: "auto" | "manual";
+ /** Manual mode: layers to offload to GPU (--gpu-layers, --fit off); -1 = Auto (--fit). */
+ gpu_layers?: number;
+ /** Manual mode: MoE expert layers to keep on CPU (--n-cpu-moe); 0 = none. */
+ n_cpu_moe?: number;
+ /** Manual mode: relative model share per GPU (--tensor-split), in GPU order. */
+ tensor_split?: number[] | null;
+ /** Picked physical GPU indices (omit/empty = automatic). */
+ gpu_ids?: number[];
}
export interface ValidateModelResponse {
@@ -80,6 +92,13 @@ export interface ValidateModelResponse {
requires_security_review?: boolean;
/** Native context length from the local GGUF header; null until downloaded. */
context_length?: number | null;
+ /** Total layer count (GGUF block_count); the manual gpu-layers ceiling is
+ * this + 1 (llama.cpp counts the output layer as offloadable too); null
+ * until downloaded. */
+ layer_count?: number | null;
+ /** MoE expert-layer count from the GGUF header (manual --n-cpu-moe ceiling);
+ * 0 for dense models, null until downloaded. */
+ moe_layer_count?: number | null;
/** Architecture only shipped by a newer transformers; UI pauses on the upgrade dialog. */
requires_transformers_upgrade?: boolean;
/** Set only when requires_transformers_upgrade. */
@@ -159,6 +178,14 @@ export interface LoadModelResponse {
spec_draft_n_max?: number | null;
/** Whether tensor-parallel split (--split-mode tensor) is active. */
tensor_parallel?: boolean;
+ gpu_memory_mode?: "auto" | "manual";
+ gpu_layers?: number;
+ n_cpu_moe?: number;
+ tensor_split?: number[] | null;
+ n_layers?: number | null;
+ /** Model's MoE expert-layer count (the n_cpu_moe ceiling); 0 if not MoE. */
+ n_moe_layers?: number;
+ gpu_ids?: number[] | null;
}
export interface UnloadModelRequest {
@@ -203,6 +230,17 @@ export interface InferenceStatusResponse {
spec_draft_n_max?: number | null;
/** Whether tensor-parallel split (--split-mode tensor) is active. */
tensor_parallel?: boolean;
+ gpu_memory_mode?: "auto" | "manual";
+ gpu_layers?: number;
+ n_cpu_moe?: number;
+ tensor_split?: number[] | null;
+ /** n_ctx the active GGUF load was invoked with (0 = Auto); re-seeds a
+ * Manual + Auto-layers context pin on hydration. Null for non-GGUF. */
+ requested_context_length?: number | null;
+ gpu_ids?: number[] | null;
+ n_layers?: number | null;
+ /** Model's MoE expert-layer count (the n_cpu_moe ceiling); 0 if not MoE. */
+ n_moe_layers?: number;
/**
* Why MTP was disabled on the loaded model despite being requested.
* "binary_no_mtp" / "binary_outdated" -> updating llama.cpp would re-enable
diff --git a/studio/frontend/src/hooks/use-gpu-info.ts b/studio/frontend/src/hooks/use-gpu-info.ts
index 1e313acdf3..db2cc021be 100644
--- a/studio/frontend/src/hooks/use-gpu-info.ts
+++ b/studio/frontend/src/hooks/use-gpu-info.ts
@@ -15,6 +15,19 @@ export interface GpuInfo {
systemRamTotalGb: number
}
+export interface SystemGpuDevice {
+ index: number;
+ name: string;
+ memoryTotalGb: number;
+ /** Free VRAM at fetch time. Degrades to the total when the utilization
+ * probe had no usage data; 0 only when the total is unknown too. */
+ memoryFreeGb: number;
+ /** "physical" = `index` is a stable physical/PCI id safe to pin via gpu_ids;
+ * "relative" = an ordinal into a parent CUDA_VISIBLE_DEVICES mask, which the
+ * backend can't map back, so the picker must not offer it. */
+ physicalIndex: boolean;
+}
+
const DEFAULT_GPU: GpuInfo = {
available: false,
name: "Unknown",
@@ -25,70 +38,135 @@ const DEFAULT_GPU: GpuInfo = {
systemRamTotalGb: 0
};
-// Module-level cache so multiple components share one fetch.
-let cachedGpu: GpuInfo | null = null;
-let fetchPromise: Promise | null = null;
+// One module-level cache so every GPU hook shares a single /api/system fetch.
+let cachedSystem: SystemInfoResponse | null = null;
+let systemPromise: Promise | null = null;
-async function fetchGpuOnce(): Promise {
- if (cachedGpu) return cachedGpu;
- if (fetchPromise) return fetchPromise;
-
- fetchPromise = (async () => {
+async function fetchSystemOnce(): Promise {
+ if (cachedSystem) return cachedSystem;
+ if (systemPromise) return systemPromise;
+ systemPromise = (async () => {
try {
const res = await authFetch("/api/system");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
-
- const data = await res.json() as SystemInfoResponse;
- const gpuData = data?.gpu;
-
- // CPU/RAM exist even on hosts without a GPU, so populate them on every path.
- // No discrete GPU (e.g. Mac): still surface system RAM so memory math
- // (unified memory) has a budget to work with.
- const base = {
- cpuCore: data?.cpu?.physical_count ?? 0,
- cpuThread: data?.cpu?.logical_count ?? 0,
- systemRamAvailableGb: data?.memory?.available_gb ?? 0,
- systemRamTotalGb: data?.memory?.total_gb ?? 0,
- };
-
- const devices = gpuData?.devices ?? [];
- const info: GpuInfo =
- gpuData?.available && devices.length
- ? {
- ...base,
- available: true,
- name: devices[0]?.name ?? "Unknown",
- memoryTotalGb: devices.reduce((sum, d) => sum + (d.memory_total_gb ?? 0), 0),
- }
- : { ...DEFAULT_GPU, ...base };
- cachedGpu = info;
- return info;
+ cachedSystem = (await res.json()) as SystemInfoResponse;
+ return cachedSystem;
} catch {
- // Reset promise so subsequent calls retry (e.g. backend wasn't ready)
- fetchPromise = null;
- return DEFAULT_GPU;
+ systemPromise = null; // reset so a later call retries (backend not ready)
+ return null;
}
})();
+ return systemPromise;
+}
- return fetchPromise;
+function toGpuInfo(data: SystemInfoResponse | null): GpuInfo {
+ // CPU/RAM exist even on GPU-less hosts (e.g. Mac), so populate them on every
+ // path: unified-memory math still needs a RAM budget to work with.
+ const base = {
+ cpuCore: data?.cpu?.physical_count ?? 0,
+ cpuThread: data?.cpu?.logical_count ?? 0,
+ systemRamAvailableGb: data?.memory?.available_gb ?? 0,
+ systemRamTotalGb: data?.memory?.total_gb ?? 0,
+ };
+ const gpuData = data?.gpu;
+ const devices = gpuData?.devices ?? [];
+ if (!gpuData?.available || !devices.length) {
+ return { ...DEFAULT_GPU, ...base };
+ }
+ return {
+ ...base,
+ available: true,
+ name: devices[0]?.name ?? "Unknown",
+ memoryTotalGb: devices.reduce((sum, d) => sum + (d.memory_total_gb ?? 0), 0),
+ };
+}
+
+function toGpuDevices(data: SystemInfoResponse | null): SystemGpuDevice[] {
+ // Unpinnable configurations must hide every pick surface: XPU indices are
+ // torch-xpu ordinals no applicator speaks, and Vulkan-only builds pin ggml's
+ // own ordinals -- /load and /validate 400 picks on both, so the backend
+ // reports gpu.gguf_gpu_ids_supported and every gate keyed on physicalIndex
+ // (picker, persisted-pick reconcile) follows it. The device flavor lives on
+ // the TOP-LEVEL device_backend field; absent support info defaults to
+ // pinnable (older backend).
+ const pinnableBackend =
+ data?.device_backend !== "xpu" &&
+ data?.gpu?.gguf_gpu_ids_supported !== false;
+ return (data?.gpu?.devices ?? [])
+ .filter((d) => typeof d.index === "number")
+ .map((d) => ({
+ index: d.index as number,
+ name: d.name ?? `GPU ${d.index}`,
+ memoryTotalGb: d.memory_total_gb ?? 0,
+ memoryFreeGb: d.vram_free_gb ?? 0,
+ physicalIndex: pinnableBackend && d.index_kind === "physical",
+ }));
+}
+
+/** Aggregate GPU info from /api/system; shares one module-level fetch across all GPU hooks. */
+export function useGpuInfo(): GpuInfo {
+ const [gpu, setGpu] = useState(
+ cachedSystem ? toGpuInfo(cachedSystem) : DEFAULT_GPU,
+ );
+ useEffect(() => {
+ // No early return on cachedSystem: a consumer mounting as the cache fills
+ // (between render and effect) would otherwise stay stuck at the default.
+ let cancelled = false;
+ fetchSystemOnce().then((d) => {
+ if (!cancelled) setGpu(toGpuInfo(d));
+ });
+ return () => {
+ cancelled = true;
+ };
+ }, []);
+ return gpu;
+}
+
+/** All backend-visible GPUs (index, name, total VRAM); shares the same fetch. */
+export function useGpuDevices(): SystemGpuDevice[] {
+ const [devices, setDevices] = useState(
+ cachedSystem ? toGpuDevices(cachedSystem) : [],
+ );
+ useEffect(() => {
+ // No early return on cachedSystem: a consumer mounting as the cache fills
+ // (between render and effect) would otherwise stay stuck at the default.
+ let cancelled = false;
+ fetchSystemOnce().then((d) => {
+ if (!cancelled) setDevices(toGpuDevices(d));
+ });
+ return () => {
+ cancelled = true;
+ };
+ }, []);
+ return devices;
}
/**
- * Fetch GPU info from /api/system. Cached at module level, so only one request
- * is made no matter how many components call this hook.
+ * Await the shared /api/system fetch so cachedPinnableGpuIndices (and the
+ * store's reconcilePersistedGpuIds) can validate a persisted pick before a
+ * load path sends it -- on a cold cache the reconcile passes ids through
+ * unvalidated, and a stale cross-host pick then fails /load with the picker
+ * hidden. Resolves immediately once the module cache is warm; a failed fetch
+ * keeps the cache cold, preserving the "can't validate, backend guards"
+ * degradation.
*/
-export function useGpuInfo(): GpuInfo {
- const [gpu, setGpu] = useState(cachedGpu ?? DEFAULT_GPU);
+export async function ensureGpuDeviceCache(): Promise {
+ await fetchSystemOnce();
+}
- useEffect(() => {
- if (cachedGpu) return;
-
- let cancelled = false;
- fetchGpuOnce().then((info) => {
- if (!cancelled) setGpu(info);
- });
- return () => { cancelled = true; };
- }, []);
-
- return gpu;
-}
\ No newline at end of file
+/**
+ * Pinnable physical GPU indices from the already-fetched /api/system cache, for
+ * non-React code (the store) that needs to validate a persisted `gpu_ids` pick
+ * without triggering a fetch. Returns:
+ * - `null` when the cache isn't populated yet (caller can't validate, so keep
+ * the pick and let the backend guard reject a truly bad one);
+ * - `[]` when the host has no pinnable multi-GPU set (single GPU, or relative/
+ * UUID-masked indices) -- the picker is hidden, so any saved pick is stale;
+ * - the physical indices otherwise.
+ */
+export function cachedPinnableGpuIndices(): number[] | null {
+ if (!cachedSystem) return null;
+ const physical = toGpuDevices(cachedSystem).filter((d) => d.physicalIndex);
+ // Mirrors the sheet's showGpuPicker gate: only a 2+ physical-GPU host can pin.
+ return physical.length > 1 ? physical.map((d) => d.index) : [];
+}
diff --git a/studio/frontend/src/hooks/use-system.ts b/studio/frontend/src/hooks/use-system.ts
index a135cce86e..8cfe2bace4 100644
--- a/studio/frontend/src/hooks/use-system.ts
+++ b/studio/frontend/src/hooks/use-system.ts
@@ -40,6 +40,9 @@ export interface SystemInfoResponse {
gpu: {
available: boolean;
backend?: string;
+ /** Whether GGUF loads accept an explicit gpu_ids pick (false on XPU hosts
+ * and Vulkan-only builds, where /load and /validate 400 picks). */
+ gguf_gpu_ids_supported?: boolean;
backend_cuda_visible_devices?: string | null;
parent_visible_gpu_ids?: number[];
index_kind?: string;
From 03590f696e97401361d59d61e1b9b367238ea229 Mon Sep 17 00:00:00 2001
From: Daniel Han
Date: Sun, 19 Jul 2026 06:08:54 -0700
Subject: [PATCH 033/271] Give opencode real timeout headroom in Local Agent
Guides CI (#7235)
* Raise the opencode invoke timeout in Local Agent Guides CI
The connection (opencode) cell flakes with a 600s timeout reported as guide drift, but it is not a hang: in a passing run the same opencode run finishes in ~482s (08:12:31 to 08:20:33), right against the shared AGENT_INVOKE_TIMEOUT of 600s, so about one run in six drifts past the cap.
opencode is the slow outlier. The print-mode agents (claude -p, codex exec) run one turn against a minimal injected system prompt, while opencode run runs its own full turn with opencode's large system prompt plus a separate small_model call to name the session (start.py pins small_model to the same 4B the server hosts). On a CPU-served gemma-4-E4B that is about 8 minutes, leaving no margin under 600s.
Double opencode's per-invoke timeout in agent-guides-drive.sh and keep the tight 600s cap for the fast agents, so a genuine headless-TTY hang still fails quickly. 1200s stays well under the 40-minute job budget.
* Normalize the agent invoke timeout before doubling it for opencode
Strip an optional trailing 's' from AGENT_INVOKE_TIMEOUT so the opencode
arithmetic, and the "${TIMEOUT}s" timeout message, stay valid if a
timeout(1)-style suffix is ever configured.
* Only double the opencode timeout for a bare-integer seconds value
Guard the arithmetic so a GNU timeout(1) duration suffix (s/m/h/d, including
floats like 0.5s) is passed through unchanged instead of breaking the
expansion; timeout(1) parses those directly. Bare seconds still double.
---------
Co-authored-by: danielhanchen
---
.github/scripts/agent-guides-drive.sh | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/.github/scripts/agent-guides-drive.sh b/.github/scripts/agent-guides-drive.sh
index 2457f08407..b63ac94b93 100755
--- a/.github/scripts/agent-guides-drive.sh
+++ b/.github/scripts/agent-guides-drive.sh
@@ -36,6 +36,23 @@ AGENT="${2:?usage: agent-guides-drive.sh }"
# Determinism (seed/temp) is applied at the server level by
# serve-unsloth-run.sh --extra; agents inherit it through the API.
TIMEOUT="${AGENT_INVOKE_TIMEOUT:-180}"
+# opencode is the slow outlier. Unlike the print-mode agents (claude -p, codex
+# exec) it runs a full turn AND a separate small_model call to name the session,
+# so one connection reply takes ~8 min on a CPU-served 4B -- right at the shared
+# 600s cap, so the cell flaked when a run drifted past a ~480s success. Give it
+# headroom (still well under the 40-min job budget); the fast agents keep the
+# tight cap that still catches a real headless-TTY hang.
+case "$AGENT" in
+ opencode)
+ # Double it, but only for a bare-integer seconds value. A GNU timeout(1)
+ # duration suffix (s/m/h/d, including floats like 0.5s) is left unchanged so
+ # the arithmetic never sees a non-number; timeout(1) parses it directly.
+ case "$TIMEOUT" in
+ *[!0-9]*) ;;
+ *) TIMEOUT=$(( TIMEOUT * 2 )) ;;
+ esac
+ ;;
+esac
# Claude refuses --dangerously-skip-permissions outside a sandbox; the CI runner
# IS the sandbox, so declare it (mirrors unslothai/scripts launcher.sh). Harmless
From a9be36830eb4ec731dcd10008d2f6dc3bb102d40 Mon Sep 17 00:00:00 2001
From: Daniel Han
Date: Sun, 19 Jul 2026 06:19:29 -0700
Subject: [PATCH 034/271] Installer: allow torch 2.11.x on the CUDA install
path (fresh install + studio) (#6959)
* Studio: allow torch 2.11.x on the CUDA install path
The CUDA torch repair path (_ensure_cuda_torch) installs torch/torchvision/
torchaudio from an exclusive --index-url, so _CUDA_TORCH_PKG_SPEC decides
exactly which torch the Studio venv gets. It was capped at torch<2.11.0, so on
a cu128/cu130 host the venv resolved torch 2.10.x even though the CUDA indexes
now publish torch 2.11.0. That left the Studio venv a torch minor behind the
torch 2.11.0 Docker base image, so the CUDA dedup step would relink base libs
under a mismatched torch.
Raise the upper bound to <2.12.0 (torchvision <0.27.0, torchaudio <2.12.0) so
the CUDA install path lands on torch 2.11.x, matching the rocm7.2 spec and the
base image. The torchao selector already maps torch 2.11 -> torchao 0.17.0, and
_ensure_flash_attn degrades gracefully when no prebuilt wheel matches (Blackwell
skips it outright; non-Blackwell prints a warning and continues), so no other
pin needs to move.
Add test_cuda_torch_spec.py to lock the bound (torch 2.11.x in, 2.12.x out) and
assert the CUDA and rocm7.2 upper bounds stay in lockstep.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* test: use zip(strict=True) so a spec length mismatch fails loudly
* install.sh: widen the CUDA torch ceiling to <2.12.0 so a fresh install matches the base
Raising _CUDA_TORCH_PKG_SPEC alone was not enough: that spec only feeds
_ensure_cuda_torch(), the ROCm-poisoning repair path that early-returns on a
normal NVIDIA host. A fresh CUDA install (including the studio Docker build,
which runs `bash install.sh --local`) takes its torch from install.sh's
TORCH_CONSTRAINT, which was still capped at torch>=2.4,<2.11.0, so cu12x/cu13x
resolved torch 2.10.x and the venv landed a minor behind the torch 2.11.0 base
image.
Extend the existing `case "$TORCH_INDEX_URL"` block (which already relaxes
rocm7.2) with a `*/cu[0-9]*` branch that widens the ceiling to <2.12.0, keeping
the >=2.4 floor so an older CUDA index (e.g. cu118) that tops out below 2.11
still resolves. The CPU wheel and older ROCm tags stay on <2.11.0 (the glob
does not match /cpu). torchvision/torchaudio are bare on this install line and
resolve their compatible companions via wheel metadata, matching the rocm7.2
pattern.
Add behavioral tests (Python + shell) exercising the case block: cu118/124/126/
128/130 widen to <2.12.0, rocm7.2 stays 2.11.x, and /cpu plus older ROCm keep
the default <2.11.0.
* install.sh: key the CUDA torch widening off the index leaf, not the full URL
The `*/cu[0-9]*` glob matched a `cu` segment anywhere in TORCH_INDEX_URL,
so a custom UNSLOTH_PYTORCH_MIRROR whose base path contains e.g. cu128 but whose
final leaf is cpu or an older ROCm tag would still widen TORCH_CONSTRAINT to
<2.12.0, contradicting the block's own comment and letting a CPU / older-ROCm
mirror resolve torch 2.11.x. Match on _torch_index_leaf (the final path segment
the backend classification just above already computes) so only a real cu*/
rocm7.2 leaf is affected; cpu and older ROCm keep the default <2.11.0. Update
the Python + shell tests to mirror the leaf-anchored case and add regression
cases for a mirror base that contains cu128 but resolves to a cpu / rocm7.1 leaf.
* install: freeze the torch trio during the with-deps unsloth installs
Released unsloth wheels can pin an older torch than Step 1 installed
(unsloth 2026.7.2 declares torch<2.11.0), so the with-deps resolve from
PyPI silently downgrades the pinned +cuXXX torch trio to PyPI's default
wheel. The flavor guard cannot catch every such swap: PyPI's torch 2.10
default is itself cu128-flavored, so the cuXXX tag comparison still
matches while the version silently drops. Freeze the just-installed trio
with uv --overrides (overrides replace dependency requirements during
resolution), keeping torch 2.11.0+cuXXX in place while unsloth's other
dependencies resolve normally. Verified on the cu128 path: without the
override torch drops 2.11.0+cu128 -> 2.10.0; with it the trio survives
and unsloth 2026.7.2 + unsloth-zoo install cleanly.
* install: fold UV_OVERRIDE env files into the torch-trio overrides file
The CLI --overrides flag is the command-line form of UV_OVERRIDE, so
passing it replaced any overrides file already exported for the process;
macOS arm64 exports UV_OVERRIDE=overrides-darwin-arm64.txt for the same
generic install path and would have lost those pins. Concatenate any
UV_OVERRIDE files into the temp trio file so both keep applying.
* install: extend the torch-trio overrides guard to migrated installs
Four follow-ups to the Step-2 --overrides guard, all empirically verified:
1. The migrated-environment with-deps unsloth install resolved
unsloth>=2026.7.2 (which pins torch<2.11.0) without the overrides file,
so a migrated CUDA venv on torch 2.11 was silently downgraded -- the
exact bug this branch fixes on the fresh path. The overrides build is
now a function (_build_unsloth_torch_overrides, reading the trio
installed at call time) invoked by both with-deps paths; the migrated
no-torch path installs --no-deps and stays unguarded.
2. The overrides temp file is now cleaned by the EXIT trap (same pattern
as _UV_OVERRIDE_TMPDIR, pre-initialized empty so an inherited value can
never reach the trap's rm); previously any Step-2 failure leaked it.
3. Folding UV_OVERRIDE files used cat, which joins the last requirement of
a file lacking a trailing newline onto the next file's first requirement
(reproduced: idna==3.10certifi==2025.1.31 makes uv fail parsing).
4. Inherited torch/torchvision/torchaudio override lines are now filtered
out when folding: uv intersects duplicate overrides rather than
last-wins (verified on uv 0.10.12: direct conflict is unsatisfiable,
transitive conflict silently backtracks), so a conflicting inherited
trio pin would break the resolve the generated exact pins protect.
Both 3 and 4 are handled by a single newline-terminating awk filter
that preserves non-trio overrides (torchmetrics, torchao, ...).
test_unsloth_torch_override.sh extended: migrated-path coverage, trap
assertion, and a functional fold test (14 checks).
* installer: tighten comments
* install: keep the existing torch release when re-running the installer
Re-running `curl -fsSL https://unsloth.ai/install.sh | sh` over an existing
install rebuilds the venv for clean state, which silently moved users to the
newest torch in range (2.10 -> 2.11 once the constraint widened). A torch the
user already validated must survive an unsloth update.
Before the old venv is moved aside for rollback, its torch version is probed
(last stdout line only, so sitecustomize noise cannot corrupt it). After the
index leaf is chosen, _previous_torch_pin turns that version into a
torch==X.Y.Z pin, but only when it cannot do harm:
- cu*/cpu leaves only; rocm leaves keep their floors (rocm7.2 must land 2.11
for the Strix _grouped_mm fix) and the Radeon wheel-matching path is
untouched.
- The wheel's flavor tag must match the freshly chosen leaf, so a flavor
change (cpu -> cuda, cu126 -> cu130) still installs the correct new build.
- The base must look like a release, so probe noise never becomes a pin.
- UNSLOTH_TORCH_UPGRADE=1 opts out and restores the old always-newest
behavior; the substep line advertises it.
The supported range is kept in _PREV_FALLBACK_CONSTRAINT: if the exact
release is not resolvable from the chosen index (custom mirrors prune old
wheels), the install warns and falls back to the newest supported release
instead of failing the whole run. The later flavor-mismatch repair reuses
TORCH_CONSTRAINT, so a mid-install clobber is repaired back to the kept
release rather than the newest one.
Verified end to end: a venv seeded with torch 2.10.0+cu130 re-run through the
full installer finishes with torch 2.10.0+cu130 (previously 2.11.0+cu130).
Tests: tests/sh/test_previous_torch_pin.sh covers keep/flavor-change/rocm/
noise/opt-out plus wiring (probe ordering before venv replacement, fallback
present, SKIP_TORCH gate).
* install: constrain kept torch pins to the supported window
Review caught that _previous_torch_pin pinned the previous venv's torch on
flavor match alone, so a release outside the installer's active range (a
2.3.x manual install below the >=2.4 floor, or a 2.12.x manual upgrade above
the ceiling) replaced the bounds computed just above it and a rerun kept a
torch the installer otherwise deliberately excludes.
New _torch_release_in_window checks the probed base against the active
TORCH_CONSTRAINT ("torch>=A.B[,
---
install.sh | 168 +++++++++++++++++-
studio/backend/tests/test_cuda_torch_spec.py | 73 ++++++++
.../test_tokenizers_and_torch_constraint.py | 80 +++++++++
tests/sh/test_previous_torch_pin.sh | 101 +++++++++++
tests/sh/test_torch_constraint.sh | 14 ++
tests/sh/test_unsloth_torch_override.sh | 131 ++++++++++++++
6 files changed, 558 insertions(+), 9 deletions(-)
create mode 100644 studio/backend/tests/test_cuda_torch_spec.py
create mode 100644 tests/sh/test_previous_torch_pin.sh
create mode 100644 tests/sh/test_unsloth_torch_override.sh
diff --git a/install.sh b/install.sh
index 5972379d26..6076721540 100755
--- a/install.sh
+++ b/install.sh
@@ -472,11 +472,13 @@ _on_install_exit() {
_restore_studio_venv_replacement
fi
[ -n "${_UV_OVERRIDE_TMPDIR:-}" ] && rm -rf "$_UV_OVERRIDE_TMPDIR" 2>/dev/null || true
+ [ -n "${_UNSLOTH_TORCH_OVERRIDES:-}" ] && rm -f "$_UNSLOTH_TORCH_OVERRIDES" 2>/dev/null || true
exit "$_status"
}
-# Empty so an inherited value can never reach the trap's rm; only a temp dir
-# this script creates below (Apple Silicon, spaced path) is ever removed.
+# Empty so an inherited value never reaches the trap's rm; only temp paths this
+# script creates below (spaced-path dir, torch-trio overrides) are removed.
_UV_OVERRIDE_TMPDIR=""
+_UNSLOTH_TORCH_OVERRIDES=""
trap _on_install_exit EXIT
# ── Helper: download a URL to a file (supports curl and wget) ──
@@ -1821,6 +1823,8 @@ tauri_log "STEP" "Creating virtual environment"
mkdir -p "$STUDIO_HOME"
_MIGRATED=false
+# Empty so an inherited value can never masquerade as a probed torch version.
+_PREV_TORCH_VER=""
if [ -x "$VENV_DIR/bin/python" ]; then
# why: matching guard to the .venv branch below -- in env-mode
@@ -1838,6 +1842,12 @@ if [ -x "$VENV_DIR/bin/python" ]; then
echo " Move it aside or choose an empty UNSLOTH_STUDIO_HOME." >&2
exit 1
fi
+ # Record the existing venv's torch BEFORE the replacement moves it aside: a re-run
+ # rebuilds the venv for clean state, but must keep the torch release the user
+ # already has (see _previous_torch_pin below). Last line only: sitecustomize or
+ # import-hook noise on stdout must not corrupt the version.
+ _PREV_TORCH_VER=$("$VENV_DIR/bin/python" -c \
+ "import torch; print(torch.__version__)" 2>/dev/null | tail -n 1 || true)
# New layout already exists — replace only after preserving rollback copy.
substep "preserving existing environment for rollback..."
_start_studio_venv_replacement "$VENV_DIR"
@@ -2187,6 +2197,68 @@ _torch_flavor_tag() {
esac
}
+# Whether release base $1 (X.Y[.Z...]) falls inside constraint window $2
+# ("torch>=A.B[.C],="*",<"*) ;;
+ *) echo "no"; return ;;
+ esac
+ _trw_floor="${_trw_con#torch>=}"; _trw_floor="${_trw_floor%%,*}"
+ _trw_ceil="${_trw_con##*,<}"
+ _v_maj="${1%%.*}"; _v_rest="${1#*.}"; _v_min="${_v_rest%%.*}"
+ _f_maj="${_trw_floor%%.*}"; _f_rest="${_trw_floor#*.}"; _f_min="${_f_rest%%.*}"
+ _c_maj="${_trw_ceil%%.*}"; _c_rest="${_trw_ceil#*.}"; _c_min="${_c_rest%%.*}"
+ for _trw_n in "$_v_maj" "$_v_min" "$_f_maj" "$_f_min" "$_c_maj" "$_c_min"; do
+ case "$_trw_n" in ''|*[!0-9]*) echo "no"; return ;; esac
+ done
+ if [ "$_v_maj" -gt "$_f_maj" ] || { [ "$_v_maj" -eq "$_f_maj" ] && [ "$_v_min" -ge "$_f_min" ]; }; then
+ if [ "$_v_maj" -lt "$_c_maj" ] || { [ "$_v_maj" -eq "$_c_maj" ] && [ "$_v_min" -lt "$_c_min" ]; }; then
+ echo "yes"
+ return
+ fi
+ fi
+ echo "no"
+}
+
+# Whether a re-run should keep the previous venv's torch: echo "torch==X.Y.Z" when the
+# probed previous version ($1) has a flavor tag matching the freshly chosen cu*/cpu index
+# leaf ($2) AND sits inside the active constraint window ($3), else "". Re-running
+# `curl | sh` rebuilds the venv for clean state, but a healthy torch the user already
+# validated must not be silently moved to a newer release (2.10 -> 2.11); a flavor
+# change (cpu <-> cuda, cu126 -> cu130) still installs the correct new build, rocm
+# leaves keep their floors (rocm7.2 must land 2.11 for the Strix _grouped_mm fix), and
+# a release outside the window (2.3.x manual install, 2.12.x manual upgrade) is never
+# kept: the installer's own bounds win. Opt out with UNSLOTH_TORCH_UPGRADE=1 to get
+# the newest release.
+_previous_torch_pin() {
+ _ptp_ver="$1"
+ _ptp_leaf="$2"
+ _ptp_con="$3"
+ [ -n "$_ptp_ver" ] || { echo ""; return; }
+ [ "${UNSLOTH_TORCH_UPGRADE:-0}" = "1" ] && { echo ""; return; }
+ case "$_ptp_leaf" in
+ cu[0-9]*|cpu) ;;
+ *) echo ""; return ;;
+ esac
+ _ptp_base="${_ptp_ver%%+*}"
+ # The base must look like a release (probe noise / garbage must never become a pin).
+ case "$_ptp_base" in
+ [0-9]*.[0-9]*) ;;
+ *) echo ""; return ;;
+ esac
+ [ "$(_torch_release_in_window "$_ptp_base" "$_ptp_con")" = "yes" ] || { echo ""; return; }
+ if [ "$(_torch_flavor_tag "$_ptp_ver")" = "$_ptp_leaf" ]; then
+ echo "torch==$_ptp_base"
+ else
+ echo ""
+ fi
+}
+
# Expected tag from the index leaf ($1): cuXXX / cpu / rocm (rocmX.Y and gfx* ->
# rocm). Empty on an unknown leaf (odd mirror) so the repair safely no-ops.
_expected_torch_flavor_tag() {
@@ -2478,12 +2550,32 @@ case "$_torch_index_leaf" in
*) export UNSLOTH_TORCH_BACKEND="cuda" ;;
esac
-# rocm7.2 ships torch 2.11.0 -- adjust the constraint to allow it.
-# All other ROCm tags and CUDA stay within <2.11.0.
-case "$TORCH_INDEX_URL" in
- */rocm7.2) TORCH_CONSTRAINT="torch>=2.11.0,<2.12.0" ;;
+# rocm7.2 and the CUDA cu12x/cu13x indexes now ship torch 2.11.x, so widen the
+# ceiling to <2.12.0 (matches the base image and _CUDA_TORCH_PKG_SPEC in
+# studio/install_python_stack.py). Keep the >=2.4 floor so an older CUDA index
+# (e.g. cu118) still resolves. Match on _torch_index_leaf, not the full URL, so
+# a mirror whose base path contains cu*/rocm7.2 but resolves to a cpu/older-rocm
+# leaf keeps the default <2.11.0.
+case "$_torch_index_leaf" in
+ rocm7.2) TORCH_CONSTRAINT="torch>=2.11.0,<2.12.0" ;;
+ cu[0-9]*) TORCH_CONSTRAINT="torch>=2.4,<2.12.0" ;;
esac
+# Re-run over an existing install: keep the previous venv's torch release instead of
+# resolving the newest in range. The range stays in _PREV_FALLBACK_CONSTRAINT so the
+# install can fall back when the exact release is not on the chosen index (custom
+# mirrors may prune old wheels). Skipped for --no-torch (no previous probe runs).
+_PREV_TORCH_PIN=""
+_PREV_FALLBACK_CONSTRAINT="$TORCH_CONSTRAINT"
+if [ "$SKIP_TORCH" = false ]; then
+ _prev_pin=$(_previous_torch_pin "$_PREV_TORCH_VER" "$_torch_index_leaf" "$TORCH_CONSTRAINT")
+ if [ -n "$_prev_pin" ]; then
+ _PREV_TORCH_PIN="$_prev_pin"
+ TORCH_CONSTRAINT="$_prev_pin"
+ substep "existing install has torch $_PREV_TORCH_VER -- keeping it (set UNSLOTH_TORCH_UPGRADE=1 to get the newest release)"
+ fi
+fi
+
# Auto-detect GPU for AMD ROCm based
# get_torch_index_url must have chosen */rocm*
# (gfx in rocminfo or amd-smi list). Then require rocminfo "Marketing Name:.*Radeon".
@@ -2705,6 +2797,43 @@ esac
# ── Install unsloth directly into the venv (no activation needed) ──
tauri_log "STEP" "Installing PyTorch"
_VENV_PY="$VENV_DIR/bin/python"
+
+# A released unsloth wheel can pin an older torch (unsloth 2026.7.2 declares
+# torch<2.11.0); a with-deps PyPI resolve then downgrades the whole trio,
+# swapping the pinned +cuXXX/+rocm build for PyPI's default. The flavor guard
+# below misses this (PyPI's torch 2.10 default is itself cu128-flavored), so
+# freeze the trio via uv --overrides (overrides replace dependency requirements
+# during resolution) while unsloth's other deps resolve normally. Sets
+# _UNSLOTH_TORCH_OVERRIDES from the trio in the venv; every with-deps unsloth
+# install (migrated and fresh) must call this before resolving and rm it after.
+_build_unsloth_torch_overrides() {
+ _UNSLOTH_TORCH_OVERRIDES=""
+ [ "$SKIP_TORCH" = false ] || return 0
+ _torch_trio_pins=$("$_VENV_PY" -c "
+from importlib.metadata import version, PackageNotFoundError
+for _p in ('torch', 'torchvision', 'torchaudio'):
+ try:
+ print(_p + '==' + version(_p))
+ except PackageNotFoundError:
+ pass
+" 2>/dev/null) || _torch_trio_pins=""
+ case "$_torch_trio_pins" in
+ torch==*)
+ _UNSLOTH_TORCH_OVERRIDES=$(mktemp)
+ printf '%s\n' "$_torch_trio_pins" > "$_UNSLOTH_TORCH_OVERRIDES"
+ # The CLI --overrides flag replaces any UV_OVERRIDE env file (same
+ # uv setting; macOS arm64 exports one here), so fold its pins in.
+ # awk, not cat: it drops inherited torch-trio lines (uv intersects
+ # duplicate overrides, so a conflicting pin would make resolution
+ # unsatisfiable) and newline-terminates the last line so an
+ # unterminated file cannot join two requirements into one.
+ for _ov_file in ${UV_OVERRIDE:-}; do
+ [ -f "$_ov_file" ] && awk '!/^[[:space:]]*torch(vision|audio)?([[:space:]<>=!~;@[]|$)/' "$_ov_file" >> "$_UNSLOTH_TORCH_OVERRIDES"
+ done
+ ;;
+ esac
+}
+
if [ "$_MIGRATED" = true ]; then
# Migrated env: force-reinstall unsloth+unsloth-zoo to ensure clean state
# in the new venv location, while preserving existing torch/CUDA
@@ -2729,9 +2858,13 @@ if [ "$_MIGRATED" = true ]; then
else
# Pin mlx-lm away from 0.31.3 here too: a curl-piped migration has no
# overrides file, so UV_OVERRIDE is unset and this positional is the only cover.
+ _build_unsloth_torch_overrides
run_install_cmd_retry "install unsloth (migrated)" uv pip install --python "$_VENV_PY" \
+ ${_UNSLOTH_TORCH_OVERRIDES:+--overrides "$_UNSLOTH_TORCH_OVERRIDES"} \
--reinstall-package unsloth --reinstall-package unsloth-zoo \
"unsloth>=2026.7.3" "unsloth-zoo>=2026.7.3" ${_MLX_LM_EXCLUDE_ARG:-}
+ [ -n "$_UNSLOTH_TORCH_OVERRIDES" ] && rm -f "$_UNSLOTH_TORCH_OVERRIDES"
+ _UNSLOTH_TORCH_OVERRIDES=""
fi
if [ "$STUDIO_LOCAL_INSTALL" = true ]; then
substep "overlaying local repo (editable)..."
@@ -2913,8 +3046,20 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
fi
else
substep "installing PyTorch ($TORCH_INDEX_URL)..."
- run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL"
+ if [ -n "$_PREV_TORCH_PIN" ]; then
+ # Kept previous release: fall back to the supported range if the exact
+ # release is not resolvable from the chosen index (pruned mirror).
+ if ! run_install_cmd_retry "install PyTorch (kept release)" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
+ --default-index "$TORCH_INDEX_URL"; then
+ substep "[WARN] $_PREV_TORCH_PIN is not installable from $TORCH_INDEX_URL -- installing the newest supported release instead" "$C_WARN"
+ TORCH_CONSTRAINT="$_PREV_FALLBACK_CONSTRAINT"
+ run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
+ --default-index "$TORCH_INDEX_URL"
+ fi
+ else
+ run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
+ --default-index "$TORCH_INDEX_URL"
+ fi
fi
# AMD ROCm: install bitsandbytes (once, after torch, for all ROCm paths).
# Gate on SKIP_TORCH=false so a user running with --no-torch on a ROCm
@@ -2927,9 +3072,10 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
;;
esac
fi
- # Fresh: Step 2 - install unsloth, preserving pre-installed torch
+ # Fresh: Step 2 - install unsloth, preserving the torch Step 1 installed
tauri_log "STEP" "Installing Unsloth"
substep "installing unsloth (this may take a few minutes)..."
+ _build_unsloth_torch_overrides
if [ "$SKIP_TORCH" = true ]; then
# No-torch: install unsloth + unsloth-zoo with --no-deps, then
# runtime deps (typer, safetensors, transformers, etc.) with --no-deps.
@@ -2953,6 +3099,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
fi
elif [ "$STUDIO_LOCAL_INSTALL" = true ]; then
run_install_cmd_retry "install unsloth (local)" uv pip install --python "$_VENV_PY" \
+ ${_UNSLOTH_TORCH_OVERRIDES:+--overrides "$_UNSLOTH_TORCH_OVERRIDES"} \
--upgrade-package unsloth "unsloth>=2026.7.3" "unsloth-zoo>=2026.7.3"
substep "overlaying local repo (editable)..."
run_install_cmd "overlay local repo" uv pip install --python "$_VENV_PY" -e "$_REPO_ROOT" --no-deps
@@ -2962,8 +3109,11 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
"unsloth-zoo @ git+https://github.com/unslothai/unsloth-zoo"
else
run_install_cmd_retry "install unsloth" uv pip install --python "$_VENV_PY" \
+ ${_UNSLOTH_TORCH_OVERRIDES:+--overrides "$_UNSLOTH_TORCH_OVERRIDES"} \
--upgrade-package unsloth -- "$PACKAGE_NAME" ${_MLX_LM_EXCLUDE_ARG:-}
fi
+ [ -n "$_UNSLOTH_TORCH_OVERRIDES" ] && rm -f "$_UNSLOTH_TORCH_OVERRIDES"
+ _UNSLOTH_TORCH_OVERRIDES=""
# AMD ROCm: repair torch if the unsloth/unsloth-zoo install pulled in
# CUDA torch from PyPI, overwriting the ROCm wheels installed in Step 1.
if [ "$SKIP_TORCH" = false ]; then
diff --git a/studio/backend/tests/test_cuda_torch_spec.py b/studio/backend/tests/test_cuda_torch_spec.py
new file mode 100644
index 0000000000..928cef787e
--- /dev/null
+++ b/studio/backend/tests/test_cuda_torch_spec.py
@@ -0,0 +1,73 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Tests for _CUDA_TORCH_PKG_SPEC in install_python_stack.py.
+
+The CUDA repair path installs the torch trio from an exclusive --index-url (no
+PyPI fallback), so these pinned ranges decide which torch the venv gets. The
+upper bound is locked to the 2.11.x family to match the base image and rocm7.2
+spec and to keep the companions off a torch-2.12 wheel that would ABI-mismatch.
+"""
+
+from __future__ import annotations
+
+import sys
+from pathlib import Path
+
+import pytest
+from packaging.requirements import Requirement
+
+# install_python_stack.py lives at repo_root/studio/install_python_stack.py
+_INSTALL_SCRIPT = Path(__file__).resolve().parents[2] / "install_python_stack.py"
+
+
+def _load_module(monkeypatch):
+ """(Re-)import and return install_python_stack (mirrors test_torchao_select)."""
+ sys.modules.pop("install_python_stack", None)
+ monkeypatch.syspath_prepend(str(_INSTALL_SCRIPT.parent))
+ import install_python_stack
+
+ return install_python_stack
+
+
+def _spec_of(pkg_spec: str):
+ """Parse 'torch>=2.4,<2.12.0' into a packaging SpecifierSet."""
+ return Requirement(pkg_spec).specifier
+
+
+@pytest.mark.parametrize(
+ "index, allowed, rejected",
+ [
+ # torch: 2.11.x allowed (matches base image); 2.12.x excluded.
+ (0, ["2.11.0", "2.11.2", "2.10.0", "2.4.0"], ["2.12.0", "2.3.0", "1.13.1"]),
+ # torchvision: 0.26.x (torch 2.11 companion) allowed; 0.27.x (torch 2.12) out.
+ (1, ["0.26.0", "0.26.1", "0.19.0"], ["0.27.0", "0.18.0"]),
+ # torchaudio: same 2.11.x window as torch.
+ (2, ["2.11.0", "2.10.0", "2.4.0"], ["2.12.0", "2.3.0"]),
+ ],
+)
+def test_cuda_spec_bounds(monkeypatch, index, allowed, rejected):
+ mod = _load_module(monkeypatch)
+ spec = _spec_of(mod._CUDA_TORCH_PKG_SPEC[index])
+ for v in allowed:
+ assert spec.contains(v, prereleases = True), f"{v} should satisfy {spec}"
+ for v in rejected:
+ assert not spec.contains(v, prereleases = True), f"{v} should not satisfy {spec}"
+
+
+def test_cuda_spec_matches_rocm72_upper_bound(monkeypatch):
+ """CUDA and rocm7.2 target the same torch 2.11.x family, so their upper
+ bounds must stay in lockstep (bump both together at 2.12.x)."""
+ mod = _load_module(monkeypatch)
+ rocm72 = mod._ROCM_TORCH_PKG_SPECS["rocm7.2"]
+
+ def _upper(pkg_spec: str) -> str:
+ for clause in _spec_of(pkg_spec):
+ if clause.operator == "<":
+ return clause.version
+ raise AssertionError(f"no upper bound in {pkg_spec!r}")
+
+ for cuda_pkg, rocm_pkg in zip(mod._CUDA_TORCH_PKG_SPEC, rocm72, strict = True):
+ assert _upper(cuda_pkg) == _upper(
+ rocm_pkg
+ ), f"CUDA {cuda_pkg!r} upper bound must match rocm7.2 {rocm_pkg!r}"
diff --git a/tests/python/test_tokenizers_and_torch_constraint.py b/tests/python/test_tokenizers_and_torch_constraint.py
index 4322f0c7d6..c58808689b 100644
--- a/tests/python/test_tokenizers_and_torch_constraint.py
+++ b/tests/python/test_tokenizers_and_torch_constraint.py
@@ -69,6 +69,21 @@ class TestStructuralTorchConstraint:
def test_tightened_assignment_exists(self):
assert 'TORCH_CONSTRAINT="torch>=2.6,<2.11.0"' in self._sh
+ def test_cuda_constraint_widened_to_2_12(self):
+ """A fresh CUDA install widens the ceiling to <2.12.0 so cu12x/cu13x
+ land torch 2.11.x (matches the base image and _CUDA_TORCH_PKG_SPEC);
+ without it cu128/cu130 resolves torch 2.10.x."""
+ assert 'TORCH_CONSTRAINT="torch>=2.4,<2.12.0"' in self._sh
+
+ def test_cuda_case_widens_via_index_leaf(self):
+ """The cu* branch of the _torch_index_leaf case sets the widened
+ constraint (parallel to rocm7.2), anchored on the leaf."""
+ m = re.search(
+ r'cu\[0-9\]\*\)\s*TORCH_CONSTRAINT="torch>=2\.4,<2\.12\.0"',
+ self._sh,
+ )
+ assert m is not None, "CUDA (cu*) TORCH_CONSTRAINT widening case not found"
+
def test_variable_used_in_pip_install(self):
"""$TORCH_CONSTRAINT must appear in a uv pip install line."""
assert '"$TORCH_CONSTRAINT"' in self._sh
@@ -384,6 +399,71 @@ class TestTorchConstraintShell:
logged = log_file.read_text()
assert "torch>=2.4,<2.11.0" in logged, f"uv log: {logged}"
+ # Mirrors the _torch_index_leaf case in install.sh: rocm7.2 -> 2.11.x floor,
+ # CUDA -> widened <2.12.0 ceiling, else (CPU/older ROCm) -> default. Anchored
+ # on the final path segment, so a mirror base path containing cu*/rocm7.2 but
+ # ending in a cpu/older-rocm leaf keeps the default.
+ _INDEX_SNIPPET = textwrap.dedent(r"""
+ #!/bin/bash
+ set -e
+ TORCH_INDEX_URL="{index_url}"
+ TORCH_CONSTRAINT="torch>=2.4,<2.11.0"
+ _torch_index_leaf="${TORCH_INDEX_URL%/}"
+ _torch_index_leaf="${_torch_index_leaf##*/}"
+ case "$_torch_index_leaf" in
+ rocm7.2) TORCH_CONSTRAINT="torch>=2.11.0,<2.12.0" ;;
+ cu[0-9]*) TORCH_CONSTRAINT="torch>=2.4,<2.12.0" ;;
+ esac
+ echo "$TORCH_CONSTRAINT"
+ """).strip()
+
+ def _resolve_index(self, tmp_path: pathlib.Path, index_url: str) -> str:
+ script_file = tmp_path / "index_snippet.sh"
+ script_file.write_text(self._INDEX_SNIPPET.replace("{index_url}", index_url))
+ script_file.chmod(0o755)
+ result = subprocess.run(
+ ["bash", str(script_file)],
+ capture_output = True,
+ text = True,
+ timeout = 10,
+ )
+ assert result.returncode == 0, f"Script failed: {result.stderr}"
+ return result.stdout.strip()
+
+ @pytest.mark.parametrize("leaf", ["cu118", "cu124", "cu126", "cu128", "cu130"])
+ def test_cuda_index_widens_to_2_12(self, tmp_path, leaf):
+ url = f"https://download.pytorch.org/whl/{leaf}"
+ assert self._resolve_index(tmp_path, url) == "torch>=2.4,<2.12.0"
+
+ def test_rocm72_index_uses_211_floor(self, tmp_path):
+ url = "https://download.pytorch.org/whl/rocm7.2"
+ assert self._resolve_index(tmp_path, url) == "torch>=2.11.0,<2.12.0"
+
+ def test_cpu_index_keeps_default(self, tmp_path):
+ # /cpu must NOT match the */cu[0-9]* branch.
+ url = "https://download.pytorch.org/whl/cpu"
+ assert self._resolve_index(tmp_path, url) == "torch>=2.4,<2.11.0"
+
+ def test_older_rocm_index_keeps_default(self, tmp_path):
+ url = "https://download.pytorch.org/whl/rocm7.1"
+ assert self._resolve_index(tmp_path, url) == "torch>=2.4,<2.11.0"
+
+ def test_cuda_index_custom_mirror_widens(self, tmp_path):
+ url = "https://internal.example.com/pytorch/cu128"
+ assert self._resolve_index(tmp_path, url) == "torch>=2.4,<2.12.0"
+
+ @pytest.mark.parametrize(
+ "url",
+ [
+ "https://internal.example.com/pytorch/cu128/cpu",
+ "https://internal.example.com/cu128/whl/rocm7.1",
+ ],
+ )
+ def test_cuda_in_mirror_path_but_noncuda_leaf_keeps_default(self, tmp_path, url):
+ # A cu128 in the mirror base path must not widen when the leaf is cpu /
+ # older ROCm: the case anchors on _torch_index_leaf, not the whole URL.
+ assert self._resolve_index(tmp_path, url) == "torch>=2.4,<2.11.0"
+
# Group 3 -- E2E tokenizers fix (requires network, ~2-5 min)
@pytest.mark.e2e
diff --git a/tests/sh/test_previous_torch_pin.sh b/tests/sh/test_previous_torch_pin.sh
new file mode 100644
index 0000000000..253ede8a27
--- /dev/null
+++ b/tests/sh/test_previous_torch_pin.sh
@@ -0,0 +1,101 @@
+#!/bin/bash
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+# Unit tests for install.sh's _previous_torch_pin, which keeps the previous
+# venv's torch release on a re-run (curl | sh over an existing install) instead
+# of silently moving the user to a newer release. Helpers are extracted from
+# install.sh and sourced.
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+INSTALL_SH="$SCRIPT_DIR/../../install.sh"
+PASS=0
+FAIL=0
+
+# Extract _previous_torch_pin and its dependencies _torch_flavor_tag and
+# _torch_release_in_window.
+_FUNC_FILE=$(mktemp)
+{
+ sed -n '/^_torch_flavor_tag()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_torch_release_in_window()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_previous_torch_pin()/,/^}/p' "$INSTALL_SH"
+} > "$_FUNC_FILE"
+# shellcheck disable=SC1090
+. "$_FUNC_FILE"
+rm -f "$_FUNC_FILE"
+
+assert_eq() {
+ _label="$1"; _expected="$2"; _actual="$3"
+ if [ "$_actual" = "$_expected" ]; then
+ echo " PASS: $_label"; PASS=$((PASS + 1))
+ else
+ echo " FAIL: $_label (expected '$_expected', got '$_actual')"; FAIL=$((FAIL + 1))
+ fi
+}
+
+unset UNSLOTH_TORCH_UPGRADE
+
+echo "=== _previous_torch_pin: matching flavor keeps the release ==="
+assert_eq "cu126 wheel on cu126 leaf" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cu126' 'cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "cu130 wheel on cu130 leaf" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cu130' 'cu130' 'torch>=2.4,<2.12.0')"
+assert_eq "cpu wheel on cpu leaf" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cpu' 'cpu' 'torch>=2.4,<2.12.0')"
+assert_eq "untagged wheel on cpu leaf" "torch==2.10.0" "$(_previous_torch_pin '2.10.0' 'cpu' 'torch>=2.4,<2.12.0')"
+assert_eq "local suffix stripped" "torch==2.9.1" "$(_previous_torch_pin '2.9.1+cu128' 'cu128' 'torch>=2.4,<2.12.0')"
+
+echo "=== _previous_torch_pin: flavor change installs the new build ==="
+assert_eq "cu126 wheel on cu130 leaf" "" "$(_previous_torch_pin '2.10.0+cu126' 'cu130' 'torch>=2.4,<2.12.0')"
+assert_eq "cpu wheel on cu126 leaf" "" "$(_previous_torch_pin '2.10.0+cpu' 'cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "cu126 wheel on cpu leaf" "" "$(_previous_torch_pin '2.10.0+cu126' 'cpu' 'torch>=2.4,<2.12.0')"
+
+echo "=== _previous_torch_pin: rocm and unknown leaves never pin ==="
+assert_eq "rocm7.2 leaf keeps its floor" "" "$(_previous_torch_pin '2.11.0+rocm7.2' 'rocm7.2' 'torch>=2.4,<2.12.0')"
+assert_eq "gfx leaf keeps its floor" "" "$(_previous_torch_pin '2.11.0+rocm7.2' 'gfx120X-all' 'torch>=2.4,<2.12.0')"
+assert_eq "unknown mirror leaf" "" "$(_previous_torch_pin '2.10.0+cu126' 'simple' 'torch>=2.4,<2.12.0')"
+
+echo "=== _previous_torch_pin: probe noise never becomes a pin ==="
+assert_eq "empty version" "" "$(_previous_torch_pin '' 'cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "garbage version" "" "$(_previous_torch_pin 'not-a-version' 'cpu' 'torch>=2.4,<2.12.0')"
+assert_eq "traceback fragment" "" "$(_previous_torch_pin "ModuleNotFoundError: No module named 'torch'" 'cpu' 'torch>=2.4,<2.12.0')"
+
+echo "=== _previous_torch_pin: out-of-window releases never pin ==="
+assert_eq "2.3.x below the cu floor" "" "$(_previous_torch_pin '2.3.1+cu118' 'cu118' 'torch>=2.4,<2.12.0')"
+assert_eq "2.12.x above the cu ceiling" "" "$(_previous_torch_pin '2.12.0+cu130' 'cu130' 'torch>=2.4,<2.12.0')"
+assert_eq "floor boundary 2.4.0 kept" "torch==2.4.0" "$(_previous_torch_pin '2.4.0+cu126' 'cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "ceiling-adjacent 2.11.x kept" "torch==2.11.1" "$(_previous_torch_pin '2.11.1+cu130' 'cu130' 'torch>=2.4,<2.12.0')"
+assert_eq "cpu window excludes 2.11.x" "" "$(_previous_torch_pin '2.11.0+cpu' 'cpu' 'torch>=2.4,<2.11.0')"
+assert_eq "mac floor excludes 2.5.x" "" "$(_previous_torch_pin '2.5.1' 'cpu' 'torch>=2.6,<2.11.0')"
+assert_eq "malformed window never pins" "" "$(_previous_torch_pin '2.10.0+cu126' 'cu126' 'torch')"
+assert_eq "empty window never pins" "" "$(_previous_torch_pin '2.10.0+cu126' 'cu126' '')"
+
+echo "=== _torch_release_in_window ==="
+assert_eq "in window" "yes" "$(_torch_release_in_window '2.10.0' 'torch>=2.4,<2.12.0')"
+assert_eq "at floor" "yes" "$(_torch_release_in_window '2.4.0' 'torch>=2.4,<2.12.0')"
+assert_eq "below floor" "no" "$(_torch_release_in_window '2.3.1' 'torch>=2.4,<2.12.0')"
+assert_eq "at ceiling" "no" "$(_torch_release_in_window '2.12.0' 'torch>=2.4,<2.12.0')"
+assert_eq "next major" "no" "$(_torch_release_in_window '3.0.0' 'torch>=2.4,<2.12.0')"
+assert_eq "patch-level floor" "yes" "$(_torch_release_in_window '2.11.5' 'torch>=2.11.0,<2.12.0')"
+assert_eq "no ceiling -> no" "no" "$(_torch_release_in_window '2.10.0' 'torch>=2.4')"
+assert_eq "garbage minor -> no" "no" "$(_torch_release_in_window '2.x' 'torch>=2.4,<2.12.0')"
+
+echo "=== _previous_torch_pin: UNSLOTH_TORCH_UPGRADE=1 opts out ==="
+assert_eq "upgrade env set" "" "$(UNSLOTH_TORCH_UPGRADE=1 _previous_torch_pin '2.10.0+cu126' 'cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "upgrade env 0" "torch==2.10.0" "$(UNSLOTH_TORCH_UPGRADE=0 _previous_torch_pin '2.10.0+cu126' 'cu126' 'torch>=2.4,<2.12.0')"
+
+echo "=== install.sh wiring ==="
+# The probe must run against the OLD venv, before it is moved aside for rollback.
+_probe_line=$(grep -n '_PREV_TORCH_VER=\$(' "$INSTALL_SH" | head -1 | cut -d: -f1)
+_move_line=$(grep -n '_start_studio_venv_replacement "\$VENV_DIR"' "$INSTALL_SH" | head -1 | cut -d: -f1)
+assert_eq "probe exists" "yes" "$([ -n "$_probe_line" ] && echo yes)"
+assert_eq "probe before venv replacement" "yes" "$([ -n "$_probe_line" ] && [ -n "$_move_line" ] && [ "$_probe_line" -lt "$_move_line" ] && echo yes)"
+# A kept release that vanished from the index must fall back to the supported range.
+assert_eq "resolve-failure fallback wired" "yes" "$(grep -q 'TORCH_CONSTRAINT="\$_PREV_FALLBACK_CONSTRAINT"' "$INSTALL_SH" && echo yes)"
+assert_eq "pin gated on SKIP_TORCH" "yes" "$(grep -q 'if \[ "\$SKIP_TORCH" = false \]; then' "$INSTALL_SH" && echo yes)"
+
+echo ""
+if [ "$FAIL" -gt 0 ]; then
+ echo "$FAIL check(s) FAILED"
+ exit 1
+fi
+echo "All $PASS checks passed"
diff --git a/tests/sh/test_torch_constraint.sh b/tests/sh/test_torch_constraint.sh
index 293a709360..d60dfc9f90 100644
--- a/tests/sh/test_torch_constraint.sh
+++ b/tests/sh/test_torch_constraint.sh
@@ -108,6 +108,20 @@ assert_eq "\$TORCH_CONSTRAINT used in pip install" "yes" "$_has_var"
_hardcoded=$(grep -c '"torch>=2.4,<2.11.0"' "$INSTALL_SH" || true)
assert_eq "hardcoded torch>=2.4 appears exactly once" "1" "$_hardcoded"
+# A fresh CUDA install widens the ceiling to <2.12.0 so cu12x/cu13x land torch
+# 2.11.x (matches the base image and _CUDA_TORCH_PKG_SPEC).
+_cuda_widen=$(grep -c 'TORCH_CONSTRAINT="torch>=2.4,<2.12.0"' "$INSTALL_SH" || true)
+assert_eq "CUDA TORCH_CONSTRAINT widened to <2.12.0" "1" "$_cuda_widen"
+
+# Widening keys off the final leaf (_torch_index_leaf), not the full URL, so a
+# mirror base path with cu*/rocm7.2 but a cpu/older-rocm leaf is not mis-widened.
+_cuda_case=$(grep -c 'cu\[0-9\]\*)' "$INSTALL_SH" || true)
+_has_cuda_case=$([ "$_cuda_case" -ge 1 ] && echo "yes" || echo "no")
+assert_eq "cu* index case adjusts TORCH_CONSTRAINT" "yes" "$_has_cuda_case"
+_leaf_case=$(grep -c 'case "\$_torch_index_leaf" in' "$INSTALL_SH" || true)
+_has_leaf_constraint=$([ "$_leaf_case" -ge 2 ] && echo "yes" || echo "no")
+assert_eq "constraint case anchors on _torch_index_leaf" "yes" "$_has_leaf_constraint"
+
echo ""
echo "=== Structural: tokenizers in no-torch-runtime.txt ==="
diff --git a/tests/sh/test_unsloth_torch_override.sh b/tests/sh/test_unsloth_torch_override.sh
new file mode 100644
index 0000000000..7e8e3f5b5b
--- /dev/null
+++ b/tests/sh/test_unsloth_torch_override.sh
@@ -0,0 +1,131 @@
+#!/bin/bash
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+# Tests for the torch-trio --overrides guard on the Step-2 unsloth installs in
+# install.sh. A released unsloth wheel can pin an older torch (2026.7.2 declares
+# torch<2.11.0); without the overrides file a with-deps PyPI resolve downgrades
+# the trio Step 1 installed, and the flavor guard misses it (PyPI's torch 2.10
+# default is itself cu128-flavored). Same assertion pattern as test_torch_constraint.sh.
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+INSTALL_SH="$SCRIPT_DIR/../../install.sh"
+PASS=0
+FAIL=0
+
+assert_true() {
+ _label="$1"; _ok="$2"
+ if [ "$_ok" = "0" ]; then
+ echo " PASS: $_label"
+ PASS=$((PASS + 1))
+ else
+ echo " FAIL: $_label"
+ FAIL=$((FAIL + 1))
+ fi
+}
+
+echo "=== test_unsloth_torch_override ==="
+
+# 1. Every with-deps unsloth install carries the overrides expansion (local,
+# generic, migrated); the --no-deps no-torch paths need no guard.
+_local_block=$(grep -A2 '"install unsloth (local)"' "$INSTALL_SH")
+printf '%s' "$_local_block" | grep -q -- '--overrides "\$_UNSLOTH_TORCH_OVERRIDES"'
+assert_true "local (with-deps) unsloth install passes --overrides" "$?"
+
+_generic_block=$(grep -A2 '"install unsloth" uv pip install' "$INSTALL_SH")
+printf '%s' "$_generic_block" | grep -q -- '--overrides "\$_UNSLOTH_TORCH_OVERRIDES"'
+assert_true "generic (with-deps) unsloth install passes --overrides" "$?"
+
+_migrated_block=$(grep -A3 '"install unsloth (migrated)"' "$INSTALL_SH")
+printf '%s' "$_migrated_block" | grep -q -- '--overrides "\$_UNSLOTH_TORCH_OVERRIDES"'
+assert_true "migrated (with-deps) unsloth install passes --overrides" "$?"
+
+_no_torch_block=$(grep -A2 '"install unsloth (no-torch)"' "$INSTALL_SH")
+if printf '%s' "$_no_torch_block" | grep -q -- '--overrides'; then _rc=1; else _rc=0; fi
+assert_true "no-torch (--no-deps) unsloth install has no overrides" "$_rc"
+
+_migrated_nt_block=$(grep -A2 '"install unsloth (migrated no-torch)"' "$INSTALL_SH")
+if printf '%s' "$_migrated_nt_block" | grep -q -- '--overrides'; then _rc=1; else _rc=0; fi
+assert_true "migrated no-torch (--no-deps) unsloth install has no overrides" "$_rc"
+
+# 2. The overrides file is only built when SKIP_TORCH=false.
+grep -B2 '_torch_trio_pins=\$(' "$INSTALL_SH" | grep -q 'SKIP_TORCH" = false'
+assert_true "overrides file build is gated on SKIP_TORCH=false" "$?"
+
+# 3. The pin-collection snippet emits exact ==pins for the installed trio (run
+# the embedded python against this test's interpreter).
+_snippet=$(sed -n '/_torch_trio_pins=\$("\$_VENV_PY" -c "/,/^" 2>\/dev\/null)/p' "$INSTALL_SH" \
+ | sed '1s/.*-c "//' | sed '$d')
+_out=$(python3 -c "$_snippet" 2>&1) || true
+# torch may or may not be importable on the test host; the snippet must not
+# crash and every line it does emit must be an exact pkg==version pin.
+if [ -n "$_out" ]; then
+ printf '%s\n' "$_out" | grep -vqE '^(torch|torchvision|torchaudio)==.+$' && _rc=1 || _rc=0
+else
+ _rc=0
+fi
+assert_true "pin snippet emits only exact trio ==pins (or nothing)" "$_rc"
+
+# 4. The temp overrides file is cleaned up after Step 2.
+grep -q 'rm -f "\$_UNSLOTH_TORCH_OVERRIDES"' "$INSTALL_SH"
+assert_true "overrides temp file is removed after the unsloth installs" "$?"
+
+# 5. Any UV_OVERRIDE env file is folded in (the CLI --overrides flag would
+# otherwise replace it, dropping e.g. the macOS arm64 darwin overrides).
+grep -q 'for _ov_file in \${UV_OVERRIDE:-}' "$INSTALL_SH"
+assert_true "UV_OVERRIDE env files are merged into the overrides file" "$?"
+
+# 6. The EXIT trap also removes the overrides file, so a failed Step 2 (set -e
+# fires before the normal-path rm) cannot leak it.
+sed -n '/_on_install_exit() {/,/^}/p' "$INSTALL_SH" \
+ | grep -q 'rm -f "\$_UNSLOTH_TORCH_OVERRIDES"'
+assert_true "EXIT trap removes the overrides temp file on failure" "$?"
+
+# 7. The UV_OVERRIDE fold filters inherited files instead of cat-ing them (run
+# the extracted awk program on sample files): (a) inherited torch-trio lines
+# are dropped so the generated exact pins win (uv intersects duplicates);
+# (b) every line is newline-terminated so an unterminated file cannot join
+# two requirements into one.
+_awk_prog=$(sed -n "s/.*awk '\(.*\)' \"\$_ov_file\".*/\1/p" "$INSTALL_SH")
+[ -n "$_awk_prog" ]
+assert_true "UV_OVERRIDE fold uses the trio-filtering awk program" "$?"
+
+_ov_dir=$(mktemp -d)
+printf '%s' 'transformers>=4.57.6' > "$_ov_dir/ov1.txt" # no trailing newline
+cat > "$_ov_dir/ov2.txt" <<'EOF'
+# comment survives
+torch<2.11.0
+torchvision==0.25.0
+torchaudio!=2.11.0
+torchmetrics==1.0
+anyio<4.14.0
+EOF
+_merged="$_ov_dir/merged.txt"
+printf '%s\n' 'torch==2.11.0+cu128' > "$_merged"
+for _f in "$_ov_dir/ov1.txt" "$_ov_dir/ov2.txt"; do
+ awk "$_awk_prog" "$_f" >> "$_merged"
+done
+
+grep -qx 'transformers>=4.57.6' "$_merged"
+assert_true "no-trailing-newline override stays a separate requirement line" "$?"
+
+if grep -qx 'torchmetrics==1.0' "$_merged" && grep -qx 'anyio<4.14.0' "$_merged"; then
+ _rc=0
+else
+ _rc=1
+fi
+assert_true "unrelated inherited overrides are preserved" "$_rc"
+
+if grep -qE '^(torch|torchvision|torchaudio)([[:space:]<>=!~;@[]|$)' "$_merged" \
+ && [ "$(grep -cE '^(torch|torchvision|torchaudio)([[:space:]<>=!~;@[]|$)' "$_merged")" != "1" ]; then
+ _rc=1
+else
+ _rc=0
+fi
+grep -qx 'torch==2.11.0+cu128' "$_merged" || _rc=1
+assert_true "inherited torch-trio lines are dropped; generated pin wins" "$_rc"
+rm -rf "$_ov_dir"
+
+echo ""
+echo "Results: $PASS passed, $FAIL failed"
+[ "$FAIL" -eq 0 ] || exit 1
From b307823b1daf7013632340bceac5b2f70dbc04a8 Mon Sep 17 00:00:00 2001
From: Andrew Chen <48723787+chuenchen309@users.noreply.github.com>
Date: Sun, 19 Jul 2026 21:33:48 +0800
Subject: [PATCH 035/271] fix(chat_templates): bind loop_messages when
default_system_message is None (#7199)
* fix(chat_templates): bind loop_messages when default_system_message is None
construct_chat_template(default_system_message=None) built a system part that
binds loop_messages only inside the `{% if messages[0]['role'] == 'system' %}`
arm. The `Fix missing loop_messages` step right below then found no
unconditional `{% set loop_messages = messages %}`, concluded loop_messages was
missing, and rewrote `{% for message in loop_messages %}` back to
`{% for message in messages %}` -- undoing the `messages[1:]` skip.
A caller-supplied system message therefore reached the loop and tripped
raise_exception:
Only user and assistant roles are supported!
Add the `{% else %}` arm so loop_messages is always bound, mirroring the
default_system_message is not None branch minus the default text. That also
stops the rewrite from firing, since the unconditional binding is now present.
Renders before / after, same template, same inputs:
default_system_message input before after
None system msg raise_exception 'Be terse.\n### User: Hi\n'
None no system '### User: Hi\n' unchanged
'You are helpful.' system msg 'Be terse.\n### User: Hi\n' unchanged
'You are helpful.' no system 'You are helpful.\n...' unchanged
The rewrite still fires for templates with no {SYSTEM} part, which is what it
was there for -- verified unchanged.
Co-Authored-By: Claude Opus 4.8
* Scope loop_messages binding to {SYSTEM} templates for PR #7199
The None branch now only adds the else arm when system_part contains
{SYSTEM}, so a static prefix with no {SYSTEM} placeholder keeps raising on a
caller system message instead of silently dropping it. Strengthen the tests:
assert the default does not leak when a caller system message is present, and
add a regression test for the static prefix case.
---------
Co-authored-by: Claude Opus 4.8
Co-authored-by: danielhanchen
---
...test_construct_chat_template_validation.py | 90 +++++++++++++++++++
unsloth/chat_templates.py | 6 ++
2 files changed, 96 insertions(+)
diff --git a/tests/python/test_construct_chat_template_validation.py b/tests/python/test_construct_chat_template_validation.py
index 66d3d80920..53d281d435 100644
--- a/tests/python/test_construct_chat_template_validation.py
+++ b/tests/python/test_construct_chat_template_validation.py
@@ -104,3 +104,93 @@ def test_chat_template_does_not_leak_sentinel_when_section_starts_with_it(chat_t
)
assert "{INPUT}" not in jinja_template
assert "{OUTPUT}" not in jinja_template
+
+
+_SYSTEM_CHAT_TEMPLATE = (
+ "{SYSTEM}\n"
+ "### User: {INPUT}\n### Assistant: {OUTPUT}"
+ "### User: {INPUT}\n### Assistant: {OUTPUT}"
+)
+
+
+def _render(jinja_template, messages):
+ from jinja2.sandbox import ImmutableSandboxedEnvironment
+
+ env = ImmutableSandboxedEnvironment()
+ env.globals["raise_exception"] = lambda message: (_ for _ in ()).throw(RuntimeError(message))
+ return env.from_string(jinja_template).render(
+ messages = messages,
+ bos_token = "",
+ eos_token = " ",
+ add_generation_prompt = False,
+ )
+
+
+@pytest.mark.parametrize("default_system_message", [None, "You are helpful."])
+def test_system_message_is_consumed_by_the_system_part(default_system_message):
+ """A caller-supplied system message must be rendered by the system part and
+ skipped by the message loop, whatever `default_system_message` is.
+
+ With `default_system_message = None` the generated template used to bind
+ `loop_messages` only inside the `{% if %}` arm. The `Fix missing
+ loop_messages` step then saw no unconditional binding, rewrote the loop back
+ to `messages`, and the system message reached the loop and tripped
+ `raise_exception`.
+ """
+ _, jinja_template, _, _ = construct_chat_template(
+ tokenizer = _SuccessFakeTokenizer(),
+ chat_template = _SYSTEM_CHAT_TEMPLATE,
+ default_system_message = default_system_message,
+ extra_eos_tokens = [""],
+ )
+ rendered = _render(
+ jinja_template,
+ [
+ {"role": "system", "content": "Be terse."},
+ {"role": "user", "content": "Hi"},
+ ],
+ )
+ assert rendered.count("Be terse.") == 1
+ assert rendered.count("Hi") == 1
+ # A caller system message overrides the default; the default must not leak in.
+ if default_system_message is not None:
+ assert default_system_message not in rendered
+
+
+def test_absent_system_message_still_renders_without_default():
+ """`default_system_message = None` with no system message in the input must
+ keep working -- the `{% else %}` arm has to bind `loop_messages = messages`."""
+ _, jinja_template, _, _ = construct_chat_template(
+ tokenizer = _SuccessFakeTokenizer(),
+ chat_template = _SYSTEM_CHAT_TEMPLATE,
+ default_system_message = None,
+ extra_eos_tokens = [""],
+ )
+ rendered = _render(jinja_template, [{"role": "user", "content": "Hi"}])
+ assert "Hi" in rendered
+
+
+_NO_SYSTEM_CHAT_TEMPLATE = (
+ "PREAMBLE\n"
+ "### User: {INPUT}\n### Assistant: {OUTPUT}"
+ "### User: {INPUT}\n### Assistant: {OUTPUT}"
+)
+
+
+def test_static_prefix_without_system_still_rejects_system_message():
+ """A template with a static prefix but no {SYSTEM} placeholder cannot render a
+ caller system message, so it must still raise rather than silently drop it."""
+ _, jinja_template, _, _ = construct_chat_template(
+ tokenizer = _SuccessFakeTokenizer(),
+ chat_template = _NO_SYSTEM_CHAT_TEMPLATE,
+ default_system_message = None,
+ extra_eos_tokens = [""],
+ )
+ with pytest.raises(RuntimeError, match = "Only user and assistant roles are supported!"):
+ _render(
+ jinja_template,
+ [
+ {"role": "system", "content": "Be terse."},
+ {"role": "user", "content": "Hi"},
+ ],
+ )
diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py
index f47c78ba80..b857c34bcb 100644
--- a/unsloth/chat_templates.py
+++ b/unsloth/chat_templates.py
@@ -2652,6 +2652,12 @@ extra_eos_tokens = None,
"{{ '" + full_system + "' }}"\
"{% set loop_messages = messages %}"\
"{% endif %}"
+ elif "{SYSTEM}" in system_part:
+ # Only bind loop_messages when the template can render a caller system
+ # message. A static prefix with no {SYSTEM} must still raise, not drop it.
+ partial_system += "{% else %}"\
+ "{% set loop_messages = messages %}"\
+ "{% endif %}"
else:
partial_system += "{% endif %}"
From b3c0259cffdccb91362e7a16dc856632319f7304 Mon Sep 17 00:00:00 2001
From: Daniel Han
Date: Sun, 19 Jul 2026 07:55:06 -0700
Subject: [PATCH 036/271] Installer: preserve the previous torch release across
every flavor and vendor on re-runs (#7250)
* install: preserve the previous torch release across every flavor and vendor
A re-run of curl | sh over an existing install was supposed to keep the
user's validated torch release, but the pin required the old build's
local flavor tag to match the freshly chosen index leaf. That gate was
wrong in practice: a PyPI-sourced torch reports a BARE version (on Linux
the PyPI wheel IS a CUDA build), which classified as cpu and never
matched a cu leaf, so a healthy 2.10 on a cu130 host was silently moved
to 2.11 (reproduced end to end); the same happened for any flavor drift
such as cu128 to cu130 after a driver upgrade, and AMD ROCm leaves were
excluded from preservation entirely.
The rule is now release-based and flavor-agnostic: the probed previous
release is pinned whenever it sits inside the final constraint window,
and the pin installs from the freshly chosen index, so the flavor always
follows the machine (NVIDIA cu*, AMD rocm/gfx, Intel/CPU, mac) while the
release follows the user. The pin is evaluated AFTER every index and
constraint decision including the Strix reroute, so raised floors
(rocm7.2 / Strix gfx need torch 2.11 for the _grouped_mm fix) correctly
reject an older release and win. UNSLOTH_TORCH_UPGRADE=1 still opts out,
out-of-window releases are never kept, and probe noise never becomes a
pin.
The kept-release install with its range fallback (for indexes that do
not carry the exact release) is factored into
_install_torch_default_index and used by every --default-index torch
path: the default NVIDIA/CPU/mac path and all three ROCm-index
fallbacks, which previously bypassed the fallback. The Radeon-repo
direct-wheel path keeps its curated per-arch wheel set (those wheels are
already exact-pinned per rocm release).
Platform coverage: install.sh serves Linux, WSL (including the WoA
fallback), and macOS for all vendors; native Windows install.ps1 still
caps at <2.11.0 everywhere, so the silent 2.10-to-2.11 move cannot occur
there (2.11 alignment is a separate follow-up).
Verified: 35-check unit suite rewritten to the new spec (any-flavor
keep, floor rejection, noise, window edges, opt-out, wiring including
pin-after-reroute and helper coverage); end-to-end matrix against
sandboxed UNSLOTH_STUDIO_HOME installs on a cu130 host covering PyPI
bare, cu128 drift, cu130 same-flavor, out-of-window 2.3, the upgrade
opt-out, the hidden-GPU cpu leaf, and a fresh-install control.
* install: honor the kept torch release on the Radeon direct-wheel path
The Radeon repo path installs an explicit wheel trio selected by
_pick_radeon_wheel, bypassing --default-index, so the kept-release pin
only took effect when the listing failed and the install fell back to
the ROCm index. On a re-run over an in-window Radeon install the trio
search started at the newest common minor and silently moved the user
forward (2.9 to 2.10 whenever the repo offered both).
The trio search now starts at the kept release's minor when
_PREV_TORCH_PIN is set and the listing still offers a torch wheel for
that minor. Radeon wheels are patch-curated per rocm release, so the
minor is the unit of preservation there; the raised rocm7.2 / Strix
floors still win because the pin is window-checked against the final
constraint before this point, and gaps keep the existing downward
search / ROCm-index fallback.
Verified with a simulated listing carrying both a 2.9 and a 2.10 trio:
no pin selects the 2.10 trio, a kept 2.9 release selects the matched
2.9 / 0.24 / 2.9 trio, and an unavailable minor degrades to the newest
trio. Added a structural wiring check to test_previous_torch_pin.sh
(now 36 checks).
* install: tighten comments in the torch preservation paths
* install: exact kept release on the Radeon path, pin fallback in ROCm repairs
The minor-level clamp on the Radeon direct-wheel path still allowed
patch drift (a kept 2.10.0 could become 2.10.1 when the listing carried
both) and the downward gap search could settle below the kept minor,
both breaking the exact preservation guarantee the other vendor paths
honor. The kept release now gets an exact-first trio attempt before the
newest-trio search: pick the kept patch (else the newest patch of the
kept minor, for listings that pruned the exact patch) together with the
paired torchvision/torchaudio wheels for that minor. Any gap warns and
falls back to the unchanged newest-trio search, mirroring
_install_torch_default_index, so a rerun installs either the kept
release or the same set a fresh install would choose, never something
in between.
The two ROCm torch repair sites (torch overwritten by dependency
resolution, on the migrated and fresh paths) installed TORCH_CONSTRAINT
directly, so a pinned release missing from the generic ROCm index would
abort the rerun instead of falling back. Both now route through
_install_torch_default_index, which passes extra uv args through
(--force-reinstall) and clears the pin once the fallback fires so later
paths stay consistent.
Verified against synthetic listings: both patches listed keeps exactly
2.10.0; a kept minor missing vision/audio warns and yields the newest
complete trio rather than a silent undercut; a pruned patch stays on
the kept minor; no pin keeps the existing newest-trio behavior. Unit
suite now 39 checks, all passing.
* install: never pin nightly/dev/source torch builds on a rerun
A survey of published torch version strings (PyPI bare, +cpu, +cu116
through +cu132, +rocmX.Y and +rocmX.Y.Z, +xpu, nightly .devYYYYMMDD,
source a0+git, rc tags) showed one gap: nightly, dev, rc, and source
builds passed the loose release-shape check, producing a pin such as
torch==2.11.0.dev20250704 that no stable index carries. The range
fallback rescued the install, but it printed "keeping it" and then
burned a doomed resolve first. The base must now be a plain numeric
X.Y[.Z] release, so those builds skip the pin and go straight to the
newest supported release.
Added unit checks for +xpu and three-component +rocm7.2.1 tags (both
already preserved correctly) and for nightly, a0 source, and rc builds
(never pinned). Suite now 44 checks, all passing.
* install: pair kept-release companions, protect the flavor repair, note substitutions
Three fixes from a 12-way review pass over the preservation work:
The kept-release install left torchvision and torchaudio unconstrained
next to the exact torch pin. torchvision exact-pins its torch in wheel
metadata so it always paired correctly, but torchaudio no longer does:
a kept torch 2.9.0 on cu130 resolved torchaudio 2.11.0 (verified with
uv dry-runs). The helper now pairs both companions to the kept minor
(torchvision 0.minor+15, torchaudio 2.minor); if the index lacks the
paired set the existing range fallback fires. Verified resolving
correctly on cu130, cu126, and rocm6.4.
The wrong-flavor repair at the end of the install was the one remaining
default-index torch install outside the helper. It runs under set -e,
so a retained pin absent from the repair index (reachable when the
Radeon direct-wheel path installed the kept release and dependency
resolution later overwrote it) aborted the installer at the last step
instead of falling back. It now routes through the helper with its
reinstall flags passed through.
The Radeon kept-release path installed a same-series build silently
when the listing had pruned the exact patch; it now prints what it is
substituting.
Unit suite extended with wiring checks for all three (46 checks, all
passing).
---
install.sh | 180 +++++++++++++++++-----------
tests/sh/test_previous_torch_pin.sh | 106 ++++++++++------
2 files changed, 181 insertions(+), 105 deletions(-)
diff --git a/install.sh b/install.sh
index 6076721540..7918a2bd23 100755
--- a/install.sh
+++ b/install.sh
@@ -2225,37 +2225,67 @@ _torch_release_in_window() {
echo "no"
}
-# Whether a re-run should keep the previous venv's torch: echo "torch==X.Y.Z" when the
-# probed previous version ($1) has a flavor tag matching the freshly chosen cu*/cpu index
-# leaf ($2) AND sits inside the active constraint window ($3), else "". Re-running
-# `curl | sh` rebuilds the venv for clean state, but a healthy torch the user already
-# validated must not be silently moved to a newer release (2.10 -> 2.11); a flavor
-# change (cpu <-> cuda, cu126 -> cu130) still installs the correct new build, rocm
-# leaves keep their floors (rocm7.2 must land 2.11 for the Strix _grouped_mm fix), and
-# a release outside the window (2.3.x manual install, 2.12.x manual upgrade) is never
-# kept: the installer's own bounds win. Opt out with UNSLOTH_TORCH_UPGRADE=1 to get
-# the newest release.
+# Keep the previous venv's torch on a re-run: echo "torch==X.Y.Z" when the probed
+# version ($1) is inside the active constraint window ($2), else "". The RELEASE is kept
+# regardless of flavor tag; the pin installs from the freshly chosen index, so flavor
+# follows the machine (cpu <-> cuda, cu126 -> cu130, PyPI bare -> +cu130) while the
+# release follows the user. Gating on flavor was wrong: a PyPI torch reports a BARE
+# version (on Linux the PyPI wheel IS CUDA), misclassified "cpu", so a healthy 2.10 on a
+# cu130 host was moved to 2.11. Per-leaf floors still win (rocm7.2 / gfx >=2.11 for the
+# Strix _grouped_mm fix, out-of-window manual installs) and are never pinned; the caller's
+# _PREV_FALLBACK_CONSTRAINT installs the newest supported release when the index lacks the
+# exact one. Opt out with UNSLOTH_TORCH_UPGRADE=1.
_previous_torch_pin() {
_ptp_ver="$1"
- _ptp_leaf="$2"
- _ptp_con="$3"
+ _ptp_con="$2"
[ -n "$_ptp_ver" ] || { echo ""; return; }
[ "${UNSLOTH_TORCH_UPGRADE:-0}" = "1" ] && { echo ""; return; }
- case "$_ptp_leaf" in
- cu[0-9]*|cpu) ;;
- *) echo ""; return ;;
- esac
_ptp_base="${_ptp_ver%%+*}"
- # The base must look like a release (probe noise / garbage must never become a pin).
+ # Base must be a plain numeric release (X.Y[.Z]); probe noise and
+ # nightly/dev/source builds (2.11.0.dev20250704, 2.9.0a0) must never
+ # become a pin -- no stable index carries them, so pinning would only
+ # print "keeping it" and then burn a doomed resolve before falling back.
case "$_ptp_base" in
+ *[!0-9.]* | *..* | .* | *.) echo ""; return ;;
[0-9]*.[0-9]*) ;;
*) echo ""; return ;;
esac
[ "$(_torch_release_in_window "$_ptp_base" "$_ptp_con")" = "yes" ] || { echo ""; return; }
- if [ "$(_torch_flavor_tag "$_ptp_ver")" = "$_ptp_leaf" ]; then
- echo "torch==$_ptp_base"
+ echo "torch==$_ptp_base"
+}
+
+# Install torch from TORCH_INDEX_URL honoring a kept-release pin: with _PREV_TORCH_PIN
+# set, TORCH_CONSTRAINT is the exact previous release; fall back to the supported range
+# if the index lacks it (pruned mirror) rather than failing. Used by every --default-index
+# path (NVIDIA cu*, AMD rocm/gfx fallbacks, cpu/mac, ROCm repairs) so preservation is
+# uniform. Extra args (e.g. --force-reinstall) are passed through to uv.
+_install_torch_default_index() {
+ if [ -n "$_PREV_TORCH_PIN" ]; then
+ # Pair the companions with the kept torch minor: torchaudio no longer
+ # exact-pins torch in its metadata, so leaving it unconstrained resolves
+ # a newer mismatched build (a kept torch 2.9.0 pulled torchaudio 2.11.0).
+ _itdi_base="${_PREV_TORCH_PIN#torch==}"
+ _itdi_minor="${_itdi_base#*.}"
+ _itdi_minor="${_itdi_minor%%.*}"
+ _itdi_tv="torchvision"
+ _itdi_ta="torchaudio"
+ case "$_itdi_base" in
+ 2.*)
+ _itdi_tv="torchvision==0.$((_itdi_minor + 15)).*"
+ _itdi_ta="torchaudio==2.${_itdi_minor}.*"
+ ;;
+ esac
+ if ! run_install_cmd_retry "install PyTorch (kept release)" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" "$_itdi_tv" "$_itdi_ta" \
+ --default-index "$TORCH_INDEX_URL" "$@"; then
+ substep "[WARN] $_PREV_TORCH_PIN is not installable from $TORCH_INDEX_URL -- installing the newest supported release instead" "$C_WARN"
+ TORCH_CONSTRAINT="$_PREV_FALLBACK_CONSTRAINT"
+ _PREV_TORCH_PIN=""
+ run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
+ --default-index "$TORCH_INDEX_URL" "$@"
+ fi
else
- echo ""
+ run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
+ --default-index "$TORCH_INDEX_URL" "$@"
fi
}
@@ -2561,21 +2591,6 @@ case "$_torch_index_leaf" in
cu[0-9]*) TORCH_CONSTRAINT="torch>=2.4,<2.12.0" ;;
esac
-# Re-run over an existing install: keep the previous venv's torch release instead of
-# resolving the newest in range. The range stays in _PREV_FALLBACK_CONSTRAINT so the
-# install can fall back when the exact release is not on the chosen index (custom
-# mirrors may prune old wheels). Skipped for --no-torch (no previous probe runs).
-_PREV_TORCH_PIN=""
-_PREV_FALLBACK_CONSTRAINT="$TORCH_CONSTRAINT"
-if [ "$SKIP_TORCH" = false ]; then
- _prev_pin=$(_previous_torch_pin "$_PREV_TORCH_VER" "$_torch_index_leaf" "$TORCH_CONSTRAINT")
- if [ -n "$_prev_pin" ]; then
- _PREV_TORCH_PIN="$_prev_pin"
- TORCH_CONSTRAINT="$_prev_pin"
- substep "existing install has torch $_PREV_TORCH_VER -- keeping it (set UNSLOTH_TORCH_UPGRADE=1 to get the newest release)"
- fi
-fi
-
# Auto-detect GPU for AMD ROCm based
# get_torch_index_url must have chosen */rocm*
# (gfx in rocminfo or amd-smi list). Then require rocminfo "Marketing Name:.*Radeon".
@@ -2660,6 +2675,23 @@ case "$TORCH_INDEX_URL" in
fi
;;
esac
+# Re-run over an existing install: keep the previous venv's torch RELEASE; the fresh
+# index above supplies the right flavor for this machine. Evaluated HERE, after every
+# index/constraint decision including the Strix reroute, so the window checked is the
+# final one and a raised floor (rocm7.2 / Strix gfx) rejects an older release.
+# _PREV_FALLBACK_CONSTRAINT keeps the range so the install can fall back when the exact
+# release is not on the chosen index (mirrors may prune old wheels). Skipped for --no-torch.
+_PREV_TORCH_PIN=""
+_PREV_FALLBACK_CONSTRAINT="$TORCH_CONSTRAINT"
+if [ "$SKIP_TORCH" = false ]; then
+ _prev_pin=$(_previous_torch_pin "$_PREV_TORCH_VER" "$TORCH_CONSTRAINT")
+ if [ -n "$_prev_pin" ]; then
+ _PREV_TORCH_PIN="$_prev_pin"
+ TORCH_CONSTRAINT="$_prev_pin"
+ substep "existing install has torch $_PREV_TORCH_VER -- keeping it (set UNSLOTH_TORCH_UPGRADE=1 to get the newest release)"
+ fi
+fi
+
_TAURI_TORCH_INDEX_FAMILY=$(_tauri_torch_index_family "$TORCH_INDEX_URL")
if [ "$_amd_gpu_radeon" = true ] && [ "$SKIP_TORCH" = false ]; then
_TAURI_TORCH_INDEX_FAMILY="radeon"
@@ -2885,10 +2917,7 @@ if [ "$_MIGRATED" = true ]; then
_has_hip=$("$_VENV_PY" -c "import torch; print(getattr(torch.version,'hip','') or '')" 2>/dev/null || true)
if [ -z "$_has_hip" ]; then
substep "repairing ROCm torch (overwritten by dependency resolution)..."
- run_install_cmd_retry "repair ROCm torch" uv pip install --python "$_VENV_PY" \
- "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL" \
- --force-reinstall
+ _install_torch_default_index --force-reinstall
fi
;;
esac
@@ -2953,7 +2982,42 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
_ta_ver=$(_extract_version "$_ta_whl" "torchaudio")
_radeon_versions_match=false
- if [ -n "$_torch_ver" ] && [ -n "$_tv_ver" ] && [ -n "$_ta_ver" ]; then
+ # Kept release (_PREV_TORCH_PIN) wins here too: pick its exact
+ # patch (else the newest patch of its minor) plus the paired
+ # vision/audio wheels. Any gap falls back to the newest-trio
+ # search below, mirroring _install_torch_default_index, so a
+ # rerun never drifts to another release nor below the kept one.
+ if [ -n "$_PREV_TORCH_PIN" ]; then
+ _prev_kept_base="${_PREV_TORCH_PIN#torch==}"
+ _prev_kept_minor="${_prev_kept_base#*.}"
+ _prev_kept_minor="${_prev_kept_minor%%.*}"
+ case "$_prev_kept_minor" in
+ ''|*[!0-9]*) ;;
+ *)
+ _kept_torch=$(_pick_radeon_wheel "torch" "${_prev_kept_base}" 2>/dev/null) || _kept_torch=""
+ [ -z "$_kept_torch" ] && { _kept_torch=$(_pick_radeon_wheel "torch" "2.${_prev_kept_minor}." 2>/dev/null) || _kept_torch=""; }
+ _kept_tv=$(_pick_radeon_wheel "torchvision" "0.$((_prev_kept_minor + 15))." 2>/dev/null) || _kept_tv=""
+ _kept_ta=$(_pick_radeon_wheel "torchaudio" "2.${_prev_kept_minor}." 2>/dev/null) || _kept_ta=""
+ if [ -n "$_kept_torch" ] && [ -n "$_kept_tv" ] && [ -n "$_kept_ta" ]; then
+ _torch_whl=$_kept_torch
+ _tv_whl=$_kept_tv
+ _ta_whl=$_kept_ta
+ _tri_whl=""
+ _radeon_versions_match=true
+ # Say so when the listing pruned the exact patch
+ # and a same-series build is installed instead.
+ case "$(printf '%s' "${_kept_torch##*/}" | sed 's/%2[Bb]/+/g')" in
+ "torch-${_prev_kept_base}"[+-]*) ;;
+ *) substep "kept release ${_prev_kept_base} is not in the Radeon listing -- installing the closest 2.${_prev_kept_minor} series build instead" ;;
+ esac
+ else
+ substep "[WARN] Radeon repo lacks a complete wheel set for kept $_PREV_TORCH_PIN -- installing the newest compatible set instead" "$C_WARN"
+ fi
+ ;;
+ esac
+ fi
+ if [ "$_radeon_versions_match" != true ] && \
+ [ -n "$_torch_ver" ] && [ -n "$_tv_ver" ] && [ -n "$_ta_ver" ]; then
_torch_minor=${_torch_ver#*.}
_ta_minor=${_ta_ver#*.}
_tv_minor=${_tv_ver#*.}
@@ -3011,9 +3075,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
if [ -z "$_torch_whl" ] || [ -z "$_tv_whl" ] || [ -z "$_ta_whl" ] || \
[ "$_radeon_versions_match" != true ]; then
substep "[WARN] Radeon repo lacks a compatible wheel set for this Python; falling back to ROCm index ($TORCH_INDEX_URL)" "$C_WARN"
- run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" \
- "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL"
+ _install_torch_default_index
else
substep "installing PyTorch from Radeon repo (${_RADEON_BASE_URL})..."
# Pass explicit wheel URLs so the matched trio is
@@ -3034,32 +3096,15 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
fi
else
substep "[WARN] Radeon repo unavailable; falling back to ROCm index ($TORCH_INDEX_URL)" "$C_WARN"
- run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" \
- "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL"
+ _install_torch_default_index
fi
else
substep "[WARN] Radeon GPU detected but could not detect full ROCm version; falling back to ROCm index" "$C_WARN"
- run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" \
- "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL"
+ _install_torch_default_index
fi
else
substep "installing PyTorch ($TORCH_INDEX_URL)..."
- if [ -n "$_PREV_TORCH_PIN" ]; then
- # Kept previous release: fall back to the supported range if the exact
- # release is not resolvable from the chosen index (pruned mirror).
- if ! run_install_cmd_retry "install PyTorch (kept release)" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL"; then
- substep "[WARN] $_PREV_TORCH_PIN is not installable from $TORCH_INDEX_URL -- installing the newest supported release instead" "$C_WARN"
- TORCH_CONSTRAINT="$_PREV_FALLBACK_CONSTRAINT"
- run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL"
- fi
- else
- run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL"
- fi
+ _install_torch_default_index
fi
# AMD ROCm: install bitsandbytes (once, after torch, for all ROCm paths).
# Gate on SKIP_TORCH=false so a user running with --no-torch on a ROCm
@@ -3122,10 +3167,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
_has_hip=$("$_VENV_PY" -c "import torch; print(getattr(torch.version,'hip','') or '')" 2>/dev/null || true)
if [ -z "$_has_hip" ]; then
substep "repairing ROCm torch (overwritten by dependency resolution)..."
- run_install_cmd_retry "repair ROCm torch" uv pip install --python "$_VENV_PY" \
- "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL" \
- --force-reinstall
+ _install_torch_default_index --force-reinstall
fi
;;
esac
@@ -3164,9 +3206,7 @@ if [ "$SKIP_TORCH" = false ] && [ -n "${TORCH_INDEX_URL:-}" ]; then
if [ -n "$_installed_torch_tag" ] && [ "$_installed_torch_tag" != "$_expected_torch_tag" ] \
&& [ "$(_torch_index_repairable "$TORCH_INDEX_URL")" = "yes" ]; then
substep "PyTorch flavor mismatch (installed $_installed_torch_tag, need $_expected_torch_tag) -- reinstalling correct build..."
- run_install_cmd "reinstall PyTorch ($_expected_torch_tag)" uv pip install --python "$_VENV_PY" \
- "$TORCH_CONSTRAINT" torchvision torchaudio \
- --default-index "$TORCH_INDEX_URL" \
+ _install_torch_default_index \
--reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio
_installed_torch_ver=$("$_VENV_PY" -c "import torch; print(torch.__version__)" 2>/dev/null || true)
_installed_torch_tag=""
diff --git a/tests/sh/test_previous_torch_pin.sh b/tests/sh/test_previous_torch_pin.sh
index 253ede8a27..1bc0d1f27f 100644
--- a/tests/sh/test_previous_torch_pin.sh
+++ b/tests/sh/test_previous_torch_pin.sh
@@ -2,9 +2,12 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
# Unit tests for install.sh's _previous_torch_pin, which keeps the previous
-# venv's torch release on a re-run (curl | sh over an existing install) instead
-# of silently moving the user to a newer release. Helpers are extracted from
-# install.sh and sourced.
+# venv's torch RELEASE on a re-run instead of moving the user to a newer one.
+# The release is kept regardless of the old build's flavor tag (PyPI bare,
+# +cuXXX, +rocm, +cpu): the pin installs from the freshly chosen index, so the
+# flavor follows the machine while the release follows the user. Per-leaf
+# windows still win (rocm7.2 / Strix floors, out-of-window manual installs).
+# Helpers are extracted from install.sh and sourced.
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
@@ -12,12 +15,9 @@ INSTALL_SH="$SCRIPT_DIR/../../install.sh"
PASS=0
FAIL=0
-# Extract _previous_torch_pin and its dependencies _torch_flavor_tag and
-# _torch_release_in_window.
+# Extract _previous_torch_pin and its dependency _torch_release_in_window.
_FUNC_FILE=$(mktemp)
{
- sed -n '/^_torch_flavor_tag()/,/^}/p' "$INSTALL_SH"
- echo ""
sed -n '/^_torch_release_in_window()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_previous_torch_pin()/,/^}/p' "$INSTALL_SH"
@@ -37,37 +37,43 @@ assert_eq() {
unset UNSLOTH_TORCH_UPGRADE
-echo "=== _previous_torch_pin: matching flavor keeps the release ==="
-assert_eq "cu126 wheel on cu126 leaf" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cu126' 'cu126' 'torch>=2.4,<2.12.0')"
-assert_eq "cu130 wheel on cu130 leaf" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cu130' 'cu130' 'torch>=2.4,<2.12.0')"
-assert_eq "cpu wheel on cpu leaf" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cpu' 'cpu' 'torch>=2.4,<2.12.0')"
-assert_eq "untagged wheel on cpu leaf" "torch==2.10.0" "$(_previous_torch_pin '2.10.0' 'cpu' 'torch>=2.4,<2.12.0')"
-assert_eq "local suffix stripped" "torch==2.9.1" "$(_previous_torch_pin '2.9.1+cu128' 'cu128' 'torch>=2.4,<2.12.0')"
+echo "=== _previous_torch_pin: in-window releases are kept, any flavor ==="
+assert_eq "cu126 wheel" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "cu130 wheel" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cu130' 'torch>=2.4,<2.12.0')"
+assert_eq "cpu wheel" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+cpu' 'torch>=2.4,<2.12.0')"
+assert_eq "PyPI bare version (CUDA build on Linux)" "torch==2.10.0" "$(_previous_torch_pin '2.10.0' 'torch>=2.4,<2.12.0')"
+assert_eq "rocm wheel" "torch==2.10.0" "$(_previous_torch_pin '2.10.0+rocm6.4' 'torch>=2.4,<2.11.0')"
+assert_eq "rocm three-component tag" "torch==2.9.1" "$(_previous_torch_pin '2.9.1+rocm7.2.1' 'torch>=2.4,<2.12.0')"
+assert_eq "Intel xpu wheel" "torch==2.9.0" "$(_previous_torch_pin '2.9.0+xpu' 'torch>=2.4,<2.12.0')"
+assert_eq "local suffix stripped" "torch==2.9.1" "$(_previous_torch_pin '2.9.1+cu128' 'torch>=2.4,<2.12.0')"
-echo "=== _previous_torch_pin: flavor change installs the new build ==="
-assert_eq "cu126 wheel on cu130 leaf" "" "$(_previous_torch_pin '2.10.0+cu126' 'cu130' 'torch>=2.4,<2.12.0')"
-assert_eq "cpu wheel on cu126 leaf" "" "$(_previous_torch_pin '2.10.0+cpu' 'cu126' 'torch>=2.4,<2.12.0')"
-assert_eq "cu126 wheel on cpu leaf" "" "$(_previous_torch_pin '2.10.0+cu126' 'cpu' 'torch>=2.4,<2.12.0')"
-
-echo "=== _previous_torch_pin: rocm and unknown leaves never pin ==="
-assert_eq "rocm7.2 leaf keeps its floor" "" "$(_previous_torch_pin '2.11.0+rocm7.2' 'rocm7.2' 'torch>=2.4,<2.12.0')"
-assert_eq "gfx leaf keeps its floor" "" "$(_previous_torch_pin '2.11.0+rocm7.2' 'gfx120X-all' 'torch>=2.4,<2.12.0')"
-assert_eq "unknown mirror leaf" "" "$(_previous_torch_pin '2.10.0+cu126' 'simple' 'torch>=2.4,<2.12.0')"
+echo "=== _previous_torch_pin: raised floors reject older releases ==="
+# rocm7.2 / Strix gfx leaves raise TORCH_CONSTRAINT to >=2.11.0 BEFORE the pin
+# is evaluated, so an old 2.10 is out of window there and the floor wins.
+assert_eq "old 2.10 vs rocm7.2 floor" "" "$(_previous_torch_pin '2.10.0+rocm7.1' 'torch>=2.11.0,<2.12.0')"
+assert_eq "2.11 passes the rocm7.2 floor" "torch==2.11.0" "$(_previous_torch_pin '2.11.0+rocm7.2' 'torch>=2.11.0,<2.12.0')"
echo "=== _previous_torch_pin: probe noise never becomes a pin ==="
-assert_eq "empty version" "" "$(_previous_torch_pin '' 'cu126' 'torch>=2.4,<2.12.0')"
-assert_eq "garbage version" "" "$(_previous_torch_pin 'not-a-version' 'cpu' 'torch>=2.4,<2.12.0')"
-assert_eq "traceback fragment" "" "$(_previous_torch_pin "ModuleNotFoundError: No module named 'torch'" 'cpu' 'torch>=2.4,<2.12.0')"
+assert_eq "empty version" "" "$(_previous_torch_pin '' 'torch>=2.4,<2.12.0')"
+assert_eq "garbage version" "" "$(_previous_torch_pin 'not-a-version' 'torch>=2.4,<2.12.0')"
+assert_eq "traceback fragment" "" "$(_previous_torch_pin "ModuleNotFoundError: No module named 'torch'" 'torch>=2.4,<2.12.0')"
+
+echo "=== _previous_torch_pin: nightly / dev / source builds never pin ==="
+# No stable index carries these, so pinning would print "keeping it" and then
+# burn a doomed resolve before the range fallback rescues the install.
+assert_eq "nightly dev build" "" "$(_previous_torch_pin '2.11.0.dev20250704+cu128' 'torch>=2.4,<2.12.0')"
+assert_eq "source build a0 tag" "" "$(_previous_torch_pin '2.9.0a0+gitabc1234' 'torch>=2.4,<2.12.0')"
+assert_eq "release candidate" "" "$(_previous_torch_pin '2.11.0rc1+cu130' 'torch>=2.4,<2.12.0')"
echo "=== _previous_torch_pin: out-of-window releases never pin ==="
-assert_eq "2.3.x below the cu floor" "" "$(_previous_torch_pin '2.3.1+cu118' 'cu118' 'torch>=2.4,<2.12.0')"
-assert_eq "2.12.x above the cu ceiling" "" "$(_previous_torch_pin '2.12.0+cu130' 'cu130' 'torch>=2.4,<2.12.0')"
-assert_eq "floor boundary 2.4.0 kept" "torch==2.4.0" "$(_previous_torch_pin '2.4.0+cu126' 'cu126' 'torch>=2.4,<2.12.0')"
-assert_eq "ceiling-adjacent 2.11.x kept" "torch==2.11.1" "$(_previous_torch_pin '2.11.1+cu130' 'cu130' 'torch>=2.4,<2.12.0')"
-assert_eq "cpu window excludes 2.11.x" "" "$(_previous_torch_pin '2.11.0+cpu' 'cpu' 'torch>=2.4,<2.11.0')"
-assert_eq "mac floor excludes 2.5.x" "" "$(_previous_torch_pin '2.5.1' 'cpu' 'torch>=2.6,<2.11.0')"
-assert_eq "malformed window never pins" "" "$(_previous_torch_pin '2.10.0+cu126' 'cu126' 'torch')"
-assert_eq "empty window never pins" "" "$(_previous_torch_pin '2.10.0+cu126' 'cu126' '')"
+assert_eq "2.3.x below the cu floor" "" "$(_previous_torch_pin '2.3.1+cu118' 'torch>=2.4,<2.12.0')"
+assert_eq "2.12.x above the cu ceiling" "" "$(_previous_torch_pin '2.12.0+cu130' 'torch>=2.4,<2.12.0')"
+assert_eq "floor boundary 2.4.0 kept" "torch==2.4.0" "$(_previous_torch_pin '2.4.0+cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "ceiling-adjacent 2.11.x kept" "torch==2.11.1" "$(_previous_torch_pin '2.11.1+cu130' 'torch>=2.4,<2.12.0')"
+assert_eq "cpu window excludes 2.11.x" "" "$(_previous_torch_pin '2.11.0+cpu' 'torch>=2.4,<2.11.0')"
+assert_eq "mac floor excludes 2.5.x" "" "$(_previous_torch_pin '2.5.1' 'torch>=2.6,<2.11.0')"
+assert_eq "malformed window never pins" "" "$(_previous_torch_pin '2.10.0+cu126' 'torch')"
+assert_eq "empty window never pins" "" "$(_previous_torch_pin '2.10.0+cu126' '')"
echo "=== _torch_release_in_window ==="
assert_eq "in window" "yes" "$(_torch_release_in_window '2.10.0' 'torch>=2.4,<2.12.0')"
@@ -80,8 +86,8 @@ assert_eq "no ceiling -> no" "no" "$(_torch_release_in_window '2.10.0' 'tor
assert_eq "garbage minor -> no" "no" "$(_torch_release_in_window '2.x' 'torch>=2.4,<2.12.0')"
echo "=== _previous_torch_pin: UNSLOTH_TORCH_UPGRADE=1 opts out ==="
-assert_eq "upgrade env set" "" "$(UNSLOTH_TORCH_UPGRADE=1 _previous_torch_pin '2.10.0+cu126' 'cu126' 'torch>=2.4,<2.12.0')"
-assert_eq "upgrade env 0" "torch==2.10.0" "$(UNSLOTH_TORCH_UPGRADE=0 _previous_torch_pin '2.10.0+cu126' 'cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "upgrade env set" "" "$(UNSLOTH_TORCH_UPGRADE=1 _previous_torch_pin '2.10.0+cu126' 'torch>=2.4,<2.12.0')"
+assert_eq "upgrade env 0" "torch==2.10.0" "$(UNSLOTH_TORCH_UPGRADE=0 _previous_torch_pin '2.10.0+cu126' 'torch>=2.4,<2.12.0')"
echo "=== install.sh wiring ==="
# The probe must run against the OLD venv, before it is moved aside for rollback.
@@ -89,9 +95,39 @@ _probe_line=$(grep -n '_PREV_TORCH_VER=\$(' "$INSTALL_SH" | head -1 | cut -d: -f
_move_line=$(grep -n '_start_studio_venv_replacement "\$VENV_DIR"' "$INSTALL_SH" | head -1 | cut -d: -f1)
assert_eq "probe exists" "yes" "$([ -n "$_probe_line" ] && echo yes)"
assert_eq "probe before venv replacement" "yes" "$([ -n "$_probe_line" ] && [ -n "$_move_line" ] && [ "$_probe_line" -lt "$_move_line" ] && echo yes)"
+# The pin must be evaluated AFTER the last index/constraint decision (the Strix
+# reroute raises the floor), so a raised floor rejects an older kept release.
+_pin_line=$(grep -n '_prev_pin=\$(_previous_torch_pin' "$INSTALL_SH" | head -1 | cut -d: -f1)
+_strix_line=$(grep -n 'Strix Halo / Strix Point: force rocm7.2 wheels' "$INSTALL_SH" | head -1 | cut -d: -f1)
+assert_eq "pin evaluated after the Strix reroute" "yes" "$([ -n "$_pin_line" ] && [ -n "$_strix_line" ] && [ "$_pin_line" -gt "$_strix_line" ] && echo yes)"
# A kept release that vanished from the index must fall back to the supported range.
assert_eq "resolve-failure fallback wired" "yes" "$(grep -q 'TORCH_CONSTRAINT="\$_PREV_FALLBACK_CONSTRAINT"' "$INSTALL_SH" && echo yes)"
assert_eq "pin gated on SKIP_TORCH" "yes" "$(grep -q 'if \[ "\$SKIP_TORCH" = false \]; then' "$INSTALL_SH" && echo yes)"
+# Every --default-index torch install path must go through the kept-release
+# helper (definition + default path + three ROCm-index fallbacks + two ROCm
+# repairs + the flavor repair), so a pinned release missing from the index
+# never aborts a rerun.
+_helper_uses=$(grep -c '_install_torch_default_index' "$INSTALL_SH")
+assert_eq "kept-release helper used by all default-index paths" "yes" "$([ "$_helper_uses" -ge 8 ] && echo yes)"
+_repair_uses=$(grep -c '_install_torch_default_index --force-reinstall' "$INSTALL_SH")
+assert_eq "ROCm repairs routed through the kept-release helper" "yes" "$([ "$_repair_uses" -ge 2 ] && echo yes)"
+# The wrong-flavor repair must use the helper too (it runs under set -e, so a
+# direct uv call with an unresolvable pin would abort the whole installer).
+assert_eq "flavor repair routed through the kept-release helper" "yes" "$(grep -q '_install_torch_default_index \\' "$INSTALL_SH" && grep -q -- '--reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio' "$INSTALL_SH" && echo yes)"
+# The kept-release install must pair the companions with the kept minor:
+# torchaudio no longer exact-pins torch, so unconstrained it resolves a newer
+# mismatched build (verified: torch==2.9.0 pulled torchaudio 2.11.0 on cu130).
+assert_eq "kept-release install pairs torchvision/torchaudio to the kept minor" "yes" "$(grep -q 'torchaudio==2.\${_itdi_minor}.\*' "$INSTALL_SH" && grep -q 'torchvision==0.\$((_itdi_minor + 15)).\*' "$INSTALL_SH" && echo yes)"
+# The Radeon direct-wheel path must also honor the pin: an exact-first kept-trio
+# attempt (exact patch, else the kept minor's newest patch, with paired
+# vision/audio) runs BEFORE the newest-trio search, and the newest-trio search
+# only runs when that attempt did not produce a match, so a kept release can
+# neither drift to another patch/minor nor be undercut by the gap search.
+_radeon_kept_line=$(grep -n '_kept_torch=\$(_pick_radeon_wheel "torch" *"\${_prev_kept_base}"' "$INSTALL_SH" | head -1 | cut -d: -f1)
+_radeon_loop_line=$(grep -n 'Loop downwards to find the first complete matching trio' "$INSTALL_SH" | head -1 | cut -d: -f1)
+assert_eq "Radeon kept-trio attempt before the newest-trio search" "yes" "$([ -n "$_radeon_kept_line" ] && [ -n "$_radeon_loop_line" ] && [ "$_radeon_kept_line" -lt "$_radeon_loop_line" ] && echo yes)"
+assert_eq "Radeon newest-trio search gated on no kept match" "yes" "$(grep -q 'if \[ "\$_radeon_versions_match" != true \] &&' "$INSTALL_SH" && echo yes)"
+assert_eq "Radeon kept-trio gap falls back with a warning" "yes" "$(grep -q 'lacks a complete wheel set for kept' "$INSTALL_SH" && echo yes)"
echo ""
if [ "$FAIL" -gt 0 ]; then
From 17fd6c8ec6c3788c1da2f9e86452fc340e451444 Mon Sep 17 00:00:00 2001
From: Daniel Han
Date: Sun, 19 Jul 2026 17:19:15 -0700
Subject: [PATCH 037/271] studio: fix stale GGUF load-marker ordering test
after inheritance relocation (#7252)
#6414 moved the llama_extra_args inheritance out of the GGUF branch in
_load_model_impl into _guard_chat_load_against_training, which runs before the
branch, so 'if request.llama_extra_args is None' is no longer inside the
gguf_branch slice that test_load_marker_precedes_hub_guard_and_unload checks.
The assertion failed on that now-missing landmark even though the guarantee it
protects (the gguf_load_in_flight marker is entered before the hub-download
guard and the unload) is intact. Drop the relocated landmark from the ordering
so the test matches the current structure.
Co-authored-by: danielhanchen
---
studio/backend/tests/test_gguf_load_cache_reuse.py | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/studio/backend/tests/test_gguf_load_cache_reuse.py b/studio/backend/tests/test_gguf_load_cache_reuse.py
index 15d91cd324..62596fcc8a 100644
--- a/studio/backend/tests/test_gguf_load_cache_reuse.py
+++ b/studio/backend/tests/test_gguf_load_cache_reuse.py
@@ -728,9 +728,13 @@ class TestLoadHubDownloadExclusion:
source = (Path(__file__).resolve().parent.parent / "routes" / "inference.py").read_text()
gguf_branch = source[source.index("if config.is_gguf:") :]
+ # The gguf_load_in_flight marker must be entered before the hub-download
+ # guard and the unload so a concurrent load can't race the download
+ # manager. The llama_extra_args inheritance that used to sit between the
+ # marker and the guard now runs in _guard_chat_load_against_training, ahead
+ # of the GGUF branch, so it is no longer a landmark inside this slice.
assert (
gguf_branch.index("enter_context(gguf_load_in_flight")
- < gguf_branch.index("if request.llama_extra_args is None")
< gguf_branch.index("_hub_download_blocks_gguf_load")
< gguf_branch.index("unsloth_backend.unload_model")
)
From 8fab1c5310e6d4117a939f30c8d1546ffca023bd Mon Sep 17 00:00:00 2001
From: Lee Jackson <130007945+Imagineer99@users.noreply.github.com>
Date: Mon, 20 Jul 2026 05:03:50 +0100
Subject: [PATCH 038/271] Route OpenCode yolo aliases to native auto mode
(#7187)
Route --yolo to OpenCode native --auto for the default TUI and run; keep the config permission fallback for no-auto subcommands (including hidden console/generate) and for --mini, which ignores --auto.
---
unsloth_cli/commands/start.py | 115 ++++++++++++++++++--
unsloth_cli/tests/test_start.py | 185 +++++++++++++++++++++++++++++++-
2 files changed, 285 insertions(+), 15 deletions(-)
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.py
index 9257a7fbcb..8128447a02 100644
--- a/unsloth_cli/commands/start.py
+++ b/unsloth_cli/commands/start.py
@@ -149,8 +149,8 @@ _PERSIST_OPTION = typer.Option(
),
)
-# Per-agent CLI flag for "run tools without prompting". opencode and openclaw have no
-# such flag (config only) and are handled in their config writers, so they are absent.
+# Per-agent CLI flag for "run tools without prompting". OpenCode (native --auto is
+# command-scoped, handled below) and OpenClaw (config-only) are absent from this prefix map.
_YOLO_COMMAND_FLAGS = {
"claude": ["--dangerously-skip-permissions"],
"codex": ["--dangerously-bypass-approvals-and-sandbox"],
@@ -166,6 +166,84 @@ def _yolo_command_flags(agent: str, yolo: bool) -> list:
return _YOLO_COMMAND_FLAGS.get(agent, []) if yolo else []
+# Subcommands that reject --auto (OpenCode exposes it only on the default TUI and `run`),
+# so `opencode serve --auto` is never emitted. Includes console/generate, hidden from
+# `opencode --help` but still registered. Unknown first positionals are TUI paths -> --auto.
+_OPENCODE_NON_AUTO_SUBCOMMANDS = frozenset(
+ "completion acp mcp attach debug providers auth agent upgrade uninstall serve web "
+ "models stats export import github pr session plugin plug db console generate".split()
+)
+_OPENCODE_GLOBAL_BOOLEAN_OPTIONS = frozenset(
+ "-h --help -v --version --print-logs --pure --mdns".split()
+)
+_OPENCODE_GLOBAL_VALUE_OPTIONS = frozenset(
+ "--log-level --port --hostname --mdns-domain --cors".split()
+)
+_OPENCODE_NATIVE_AUTO_MIN_VERSION = (1, 17, 12)
+
+
+def _opencode_supports_native_auto() -> bool:
+ executable = shutil.which("opencode")
+ if executable is None:
+ # No local binary: a --no-launch recipe may run elsewhere, and _run installs the
+ # current release on launch -- either way assume native --auto is available.
+ return True
+ try:
+ output = subprocess.check_output(
+ [executable, "--version"],
+ text = True,
+ timeout = 10,
+ stderr = subprocess.DEVNULL,
+ )
+ except Exception:
+ return False
+ match = re.search(r"(\d+)\.(\d+)\.(\d+)", output)
+ return bool(match) and tuple(int(part) for part in match.groups()) >= (
+ _OPENCODE_NATIVE_AUTO_MIN_VERSION
+ )
+
+
+def _opencode_subcommand(args: list[str]) -> Optional[str]:
+ """Return an explicit OpenCode subcommand after supported global options."""
+ index = 0
+ while index < len(args):
+ arg = args[index]
+ if arg == "--":
+ return None
+ if arg in _OPENCODE_GLOBAL_BOOLEAN_OPTIONS:
+ index += 1
+ continue
+ if arg in _OPENCODE_GLOBAL_VALUE_OPTIONS:
+ index += 2
+ continue
+ if any(arg.startswith(f"{option}=") for option in _OPENCODE_GLOBAL_VALUE_OPTIONS):
+ index += 1
+ continue
+ # A non-global option (e.g. --session) is a TUI flag; stop before its value is
+ # mistaken for a subcommand.
+ if arg.startswith("-"):
+ return None
+ return arg
+ return None
+
+
+def _opencode_native_auto_args(args: list[str], yolo: bool) -> tuple[list[str], bool]:
+ """Add OpenCode's native --auto when the selected command supports it."""
+ routed = list(args)
+ if not yolo:
+ return routed, False
+ if _opencode_subcommand(routed) in _OPENCODE_NON_AUTO_SUBCOMMANDS:
+ return routed, False
+ separator = routed.index("--") if "--" in routed else len(routed)
+ # --mini's runMini TUI forces auto=false and never forwards --auto, so appending it is
+ # useless; fall back to the config permission block so --yolo still auto-approves.
+ if any(arg == "--mini" or arg.startswith("--mini=") for arg in routed[:separator]):
+ return routed, False
+ if "--auto" not in routed[:separator]:
+ routed.insert(separator, "--auto")
+ return routed, True
+
+
def _hermes_install_hint() -> str:
return _HERMES_WINDOWS_INSTALL_HINT if os.name == "nt" else _HERMES_POSIX_INSTALL_HINT
@@ -1465,10 +1543,10 @@ def write_opencode_config(
compaction["reserved"] = max(1, window // 10)
tools = ("edit", "bash", "webfetch")
if yolo:
- # OpenCode has no --yolo flag; auto-approve is the config `permission` block
- # (singular). Allow the prompting tools and paths outside the launch directory so
- # tool calls don't block on the TUI. This rides inline (OPENCODE_CONFIG_CONTENT) so
- # --yolo works even over a project config.
+ # Fallback for commands without native --auto and for the append-safe bare
+ # --no-launch command (subcommand unknown yet). Rides inline (OPENCODE_CONFIG_CONTENT)
+ # so it wins over a project config. TUI and `run` launches use --auto and call here
+ # with yolo=False, letting OpenCode preserve explicit deny rules.
session_permission = {t: "allow" for t in tools}
session_permission["external_directory"] = {"*": "allow"}
config["permission"] = dict(session_permission)
@@ -1807,11 +1885,20 @@ def opencode(
# --no-launch, where the printed command is consumed by drivers that append a
# subcommand such as `run `; a leading --model would land before that
# subcommand and break it. Those paths rely on the inline pin instead.
+ native_auto = False
+ route_native_auto = yolo and _opencode_supports_native_auto()
if ctx.args:
- command = ["opencode", *ctx.args]
+ opencode_args, native_auto = _opencode_native_auto_args(list(ctx.args), route_native_auto)
+ command = ["opencode", *opencode_args]
elif launch:
- command = ["opencode", "--model", opencode_model]
+ opencode_args, native_auto = _opencode_native_auto_args(
+ ["--model", opencode_model],
+ route_native_auto,
+ )
+ command = ["opencode", *opencode_args]
else:
+ # Append-safe base: `opencode --auto run ...` parses as the TUI with a project
+ # "run", not the run subcommand. Command unknown here, so keep the config fallback.
command = ["opencode"]
# opencode keeps sessions in ~/.local/share/opencode (never relocated), so resume
# already survives exit; reopen the last one by passing `opencode --continue` through.
@@ -1820,12 +1907,18 @@ def opencode(
# OPENCODE_CONFIG is an overlay (loaded between the user's global and project
# configs), so this adds the Unsloth provider/model for the session without
# changing the user's default model. Key lives in the config, not the env.
- session_permission = write_opencode_config(base, key, entry, config_path, yolo = yolo)
+ session_permission = write_opencode_config(
+ base,
+ key,
+ entry,
+ config_path,
+ yolo = yolo and not native_auto,
+ )
# A project's own opencode.json outranks OPENCODE_CONFIG, so the session model pin
# would silently lose to a repo config. Carry it in OPENCODE_CONFIG_CONTENT, which
# outranks project config; the API key stays in the private file, never the env.
- # Only --yolo carries a permission here (its allow must win over a project config);
- # a non-yolo session returns no permission, so the project's own rules are honored.
+ # Only the config fallback carries a permission. Native --auto omits it (auto-approve
+ # asks, keep explicit denies); a non-yolo session omits it too, honoring project rules.
# opencode filters every provider (a config-defined custom one included) through
# its enabled_providers allowlist and disabled_providers denylist, and a model pin
# does not bypass that gate -- a filtered provider resolves to ModelNotFoundError.
diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.py
index 2405ba0480..5b2806be12 100644
--- a/unsloth_cli/tests/test_start.py
+++ b/unsloth_cli/tests/test_start.py
@@ -2190,8 +2190,18 @@ def test_yolo_aliases_are_interchangeable(fake_studio, alias):
assert "--dangerously-bypass-approvals-and-sandbox" in codex.output
assert "--dangerously-skip-permissions" not in codex.output
+ opencode = CliRunner().invoke(
+ start.start_app,
+ ["opencode", alias, "--no-launch", "run", "hello"],
+ )
+ assert opencode.exit_code == 0, opencode.output
+ assert _launch_command(opencode.output) == ["opencode", "run", "hello", "--auto"]
+ assert "permission" not in _opencode_inline_config(opencode.output)
-def test_yolo_opencode_writes_permission_block(fake_studio, tmp_path):
+
+def test_yolo_opencode_bare_no_launch_uses_permission_fallback(fake_studio, tmp_path):
+ # A bare --no-launch recipe stays append-safe (callers add a subcommand later);
+ # `opencode --auto run ...` would select the TUI, not `run`, so keep the config fallback.
result = CliRunner().invoke(start.start_app, ["opencode", "--yolo", "--no-launch"])
assert result.exit_code == 0, result.output
config = json.loads((tmp_path / "agents" / "opencode" / "opencode.json").read_text())
@@ -2203,6 +2213,172 @@ def test_yolo_opencode_writes_permission_block(fake_studio, tmp_path):
}
+def test_yolo_opencode_run_uses_native_auto(fake_studio):
+ result = CliRunner().invoke(
+ start.start_app,
+ ["opencode", "--yolo", "--no-launch", "run", "hello"],
+ )
+ assert result.exit_code == 0, result.output
+ command = _launch_command(result.output)
+ assert command == ["opencode", "run", "hello", "--auto"]
+ assert "permission" not in _opencode_inline_config(result.output)
+
+
+def test_yolo_opencode_tui_resume_uses_native_auto(fake_studio):
+ result = CliRunner().invoke(
+ start.start_app,
+ ["opencode", "--yolo", "--no-launch", "--session", "sid"],
+ )
+ assert result.exit_code == 0, result.output
+ command = _launch_command(result.output)
+ assert command == ["opencode", "--session", "sid", "--auto"]
+ assert "permission" not in _opencode_inline_config(result.output)
+
+
+def test_no_yolo_opencode_run_omits_native_auto(fake_studio):
+ result = CliRunner().invoke(
+ start.start_app,
+ ["opencode", "--no-launch", "run", "hello"],
+ )
+ assert result.exit_code == 0, result.output
+ assert _launch_command(result.output) == ["opencode", "run", "hello"]
+ assert "permission" not in _opencode_inline_config(result.output)
+
+
+def test_yolo_opencode_bare_launch_uses_native_auto(fake_studio, monkeypatch):
+ monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/opencode")
+ monkeypatch.setattr(start, "_opencode_supports_native_auto", lambda: True)
+ captured = _capture_launch(monkeypatch, ["opencode", "--yolo"])
+ assert captured["command"][1:] == [
+ "--model",
+ f"{start._OPENCODE_PROVIDER}/{MODEL['id']}",
+ "--auto",
+ ]
+ assert "permission" not in json.loads(captured["env"]["OPENCODE_CONFIG_CONTENT"])
+
+
+def test_yolo_opencode_native_auto_clears_prior_config_fallback(fake_studio, tmp_path):
+ fallback = CliRunner().invoke(
+ start.start_app,
+ ["opencode", "--yolo", "--no-launch"],
+ )
+ assert fallback.exit_code == 0, fallback.output
+
+ native = CliRunner().invoke(
+ start.start_app,
+ ["opencode", "--yolo", "--no-launch", "run", "hello"],
+ )
+ assert native.exit_code == 0, native.output
+ assert _launch_command(native.output) == ["opencode", "run", "hello", "--auto"]
+ assert "permission" not in _opencode_inline_config(native.output)
+ config = json.loads((tmp_path / "agents" / "opencode" / "opencode.json").read_text())
+ assert config["permission"] == {
+ "edit": "ask",
+ "bash": "ask",
+ "webfetch": "ask",
+ "external_directory": {"*": "ask"},
+ }
+
+
+@pytest.mark.parametrize(
+ ("version", "expected"),
+ [
+ ("1.17.11", False),
+ ("1.17.12", True),
+ ("opencode 1.18.2", True),
+ ("development build", False),
+ ],
+)
+def test_opencode_native_auto_version_gate(monkeypatch, version, expected):
+ monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/opencode")
+ monkeypatch.setattr(start.subprocess, "check_output", lambda *args, **kwargs: version)
+ assert start._opencode_supports_native_auto() is expected
+
+
+def test_opencode_native_auto_assumes_current_without_local_binary(monkeypatch):
+ monkeypatch.setattr(start.shutil, "which", lambda _: None)
+ assert start._opencode_supports_native_auto() is True
+
+
+def test_yolo_opencode_old_version_uses_config_fallback(fake_studio, monkeypatch):
+ monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/opencode")
+ monkeypatch.setattr(start.subprocess, "check_output", lambda *args, **kwargs: "1.17.11")
+ result = CliRunner().invoke(
+ start.start_app,
+ ["opencode", "--yolo", "--no-launch", "run", "hello"],
+ )
+ assert result.exit_code == 0, result.output
+ assert _launch_command(result.output) == ["opencode", "run", "hello"]
+ assert _opencode_inline_config(result.output)["permission"] == {
+ "edit": "allow",
+ "bash": "allow",
+ "webfetch": "allow",
+ "external_directory": {"*": "allow"},
+ }
+
+
+@pytest.mark.parametrize(
+ ("args", "expected", "native"),
+ [
+ ([], ["--auto"], True),
+ (["run", "hello"], ["run", "hello", "--auto"], True),
+ (
+ ["run", "hello", "--", "--literal"],
+ ["run", "hello", "--auto", "--", "--literal"],
+ True,
+ ),
+ (["--print-logs", "run", "hello"], ["--print-logs", "run", "hello", "--auto"], True),
+ (["--session", "serve"], ["--session", "serve", "--auto"], True),
+ (["serve"], ["serve"], False),
+ (["--print-logs", "serve"], ["--print-logs", "serve"], False),
+ (["run", "--auto", "hello"], ["run", "--auto", "hello"], True),
+ # Hidden commands that reject --auto fall back like the visible utility ones.
+ (["generate"], ["generate"], False),
+ (["console", "login"], ["console", "login"], False),
+ # --mini ignores --auto (runMini forces auto=false), so use the config fallback.
+ (["--mini"], ["--mini"], False),
+ (["--session", "sid", "--mini"], ["--session", "sid", "--mini"], False),
+ ],
+)
+def test_opencode_native_auto_args(args, expected, native):
+ assert start._opencode_native_auto_args(args, True) == (expected, native)
+ assert start._opencode_native_auto_args(args, False) == (args, False)
+
+
+def test_yolo_opencode_non_agent_subcommand_uses_config_fallback(fake_studio):
+ result = CliRunner().invoke(
+ start.start_app,
+ ["opencode", "--yolo", "--no-launch", "serve"],
+ )
+ assert result.exit_code == 0, result.output
+ command = _launch_command(result.output)
+ assert command == ["opencode", "serve"]
+ assert _opencode_inline_config(result.output)["permission"] == {
+ "edit": "allow",
+ "bash": "allow",
+ "webfetch": "allow",
+ "external_directory": {"*": "allow"},
+ }
+
+
+@pytest.mark.parametrize("passthrough", (["generate"], ["console", "login"], ["--mini"]))
+def test_yolo_opencode_no_auto_command_uses_config_fallback(fake_studio, passthrough):
+ # generate/console are hidden and reject --auto, --mini ignores it: none get --auto,
+ # all keep the config permission fallback.
+ result = CliRunner().invoke(
+ start.start_app,
+ ["opencode", "--yolo", "--no-launch", *passthrough],
+ )
+ assert result.exit_code == 0, result.output
+ assert _launch_command(result.output) == ["opencode", *passthrough]
+ assert _opencode_inline_config(result.output)["permission"] == {
+ "edit": "allow",
+ "bash": "allow",
+ "webfetch": "allow",
+ "external_directory": {"*": "allow"},
+ }
+
+
def test_no_yolo_opencode_has_no_permission_block(fake_studio, tmp_path):
result = CliRunner().invoke(start.start_app, ["opencode", "--no-launch"])
assert result.exit_code == 0, result.output
@@ -2549,15 +2725,16 @@ def test_openclaw_non_yolo_preserves_full_mode(tmp_path):
def test_yolo_command_flags_unmapped_agent_is_empty():
- # Config-based agents (and any typo) must yield no flag, not a KeyError.
+ # Placement-aware/config-based agents (and any typo) must yield no prefix flag.
assert start._yolo_command_flags("opencode", True) == []
assert start._yolo_command_flags("openclaw", True) == []
assert start._yolo_command_flags("claude", True) == ["--dangerously-skip-permissions"]
assert start._yolo_command_flags("claude", False) == []
-def test_yolo_config_agents_add_no_command_flag(fake_studio):
- # opencode/openclaw auto-approve is config-only; nothing should leak onto argv.
+def test_yolo_config_fallbacks_add_no_legacy_command_flag(fake_studio):
+ # OpenClaw is config-only; OpenCode's append-safe bare recipe uses its config fallback.
+ # Neither should leak a legacy yolo/dangerous alias onto argv.
for agent in ("opencode", "openclaw"):
result = CliRunner().invoke(start.start_app, [agent, "--yolo", "--no-launch"])
assert result.exit_code == 0, result.output
From e0132b6d6c414cece2bced7eaf164eeebe088dd1 Mon Sep 17 00:00:00 2001
From: Lee Jackson <130007945+Imagineer99@users.noreply.github.com>
Date: Mon, 20 Jul 2026 05:04:28 +0100
Subject: [PATCH 039/271] Pin the Hermes remote installer and harden consent
(#7179)
Pin the fetched Hermes install.sh/install.ps1 and the checkout they perform to an immutable upstream commit, and distinguish pinned from unpinned sources in the consent warning.
---
unsloth_cli/commands/start.py | 54 +++++++++++++++++++++++++++------
unsloth_cli/tests/test_start.py | 38 ++++++++++++++++++-----
2 files changed, 75 insertions(+), 17 deletions(-)
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.py
index 8128447a02..6da31229f8 100644
--- a/unsloth_cli/commands/start.py
+++ b/unsloth_cli/commands/start.py
@@ -49,13 +49,22 @@ _HERMES_PROVIDER = "unsloth"
# the wizard's global API-key/model prompts would block the launch and point the
# user at a different (global) provider than the one Unsloth just configured.
# Both installers expose a skip flag: `-SkipSetup` (PowerShell) and
-# `--skip-setup` (POSIX; passed to the piped script via `bash -s --`).
+# `--skip-setup` (POSIX; passed to the piped script via `bash -s --`). Pin both
+# the fetched script and the repository checkout it performs to the same full
+# commit so a later change to either upstream branch cannot silently replace
+# code that Unsloth executes with the user's privileges.
+_HERMES_INSTALL_COMMIT = "f1af945f6c576eccb126fa955edc9be258b33020"
+_HERMES_INSTALL_BASE = (
+ "https://raw.githubusercontent.com/NousResearch/hermes-agent/"
+ f"{_HERMES_INSTALL_COMMIT}/scripts"
+)
_HERMES_WINDOWS_INSTALL_HINT = (
- "& ([scriptblock]::Create((irm https://hermes-agent.nousresearch.com/install.ps1))) -SkipSetup"
+ f"& ([scriptblock]::Create((irm {_HERMES_INSTALL_BASE}/install.ps1)))"
+ f" -SkipSetup -Commit {_HERMES_INSTALL_COMMIT}"
)
_HERMES_POSIX_INSTALL_HINT = (
- "curl -fsSL https://raw.githubusercontent.com/NousResearch/hermes-agent"
- "/main/scripts/install.sh | bash -s -- --skip-setup"
+ f"curl -fsSL {_HERMES_INSTALL_BASE}/install.sh | bash -s --"
+ f" --skip-setup --commit {_HERMES_INSTALL_COMMIT}"
)
# Hermes refuses to initialize when the model window is under 64,000 tokens; its
# error message points at the model.context_length / auxiliary.compression
@@ -1199,6 +1208,16 @@ def _install_source(install_hint: str) -> Optional[str]:
return match.group(0) if match else None
+def _pinned_raw_github_commit(source: str) -> Optional[str]:
+ """Return the immutable full commit in a raw GitHub URL, if present."""
+ match = re.match(
+ r"^https://raw\.githubusercontent\.com/[^/]+/[^/]+/([0-9a-f]{40})/",
+ source,
+ flags = re.IGNORECASE,
+ )
+ return match.group(1).lower() if match else None
+
+
def _install_agent(name: str, install_hint: str) -> Optional[str]:
# Missing agent under --launch: offer to run its documented install command, then
# re-resolve it on PATH. Consent-based (we never auto-run a remote install script
@@ -1212,12 +1231,27 @@ def _install_agent(name: str, install_hint: str) -> Optional[str]:
# and nothing checks a signature or hash on the fetched content. Naming the source
# turns a blind "yes" into informed consent.
source = _install_source(install_hint)
- warning = (
- f"This will download and RUN a script from {source} with your privileges"
- if source
- else f"This will RUN `{install_hint}` with your privileges"
- )
- typer.secho(f"{warning}; there is no signature or hash check.", fg = "yellow", err = True)
+ if source:
+ pinned_commit = _pinned_raw_github_commit(source)
+ if pinned_commit:
+ warning = (
+ "Security warning: This will download and execute a third-party script "
+ f"from {source} with your privileges. Unsloth pins this content to "
+ f"immutable upstream commit {pinned_commit}, but does not independently "
+ "verify or sandbox it. Continue only if you trust this source and commit."
+ )
+ else:
+ warning = (
+ "Security warning: This will download and execute an unverified third-party "
+ f"script from {source} with your privileges. Unsloth does not pin or verify "
+ "the downloaded content. Continue only if you trust this source."
+ )
+ else:
+ warning = (
+ f"This will RUN `{install_hint}` with your privileges; "
+ "there is no signature or hash check."
+ )
+ typer.secho(warning, fg = "yellow", err = True)
if not typer.confirm(f"Install `{name}` now with `{install_hint}`?", default = False):
return None
# Run each hint through the shell it is written for: PowerShell (irm | iex, or npm)
diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.py
index 5b2806be12..98bd9f8157 100644
--- a/unsloth_cli/tests/test_start.py
+++ b/unsloth_cli/tests/test_start.py
@@ -7,6 +7,7 @@ from __future__ import annotations
import json
import os
+import re
import shlex
import sys
import urllib.error
@@ -128,7 +129,7 @@ def test_install_agent_uses_powershell_on_windows(monkeypatch):
assert ran == [["powershell", "-NoProfile", "-Command", install_hint]]
-def test_install_agent_warns_and_names_remote_source(monkeypatch, capsys):
+def test_install_agent_warns_remote_installer_is_unverified_third_party(monkeypatch, capsys):
# Before the confirm, a remote installer must name the URL it fetches so the
# user consents to a specific source rather than blindly accepting.
monkeypatch.setattr(start.os, "name", "nt")
@@ -137,9 +138,23 @@ def test_install_agent_warns_and_names_remote_source(monkeypatch, capsys):
hint = "& ([scriptblock]::Create((irm https://hermes-agent.nousresearch.com/install.ps1))) -SkipSetup"
assert start._install_agent("hermes", hint) is None
err = capsys.readouterr().err
+ assert "Security warning" in err
+ assert "unverified third-party script" in err
assert "https://hermes-agent.nousresearch.com/install.ps1" in err
- assert "download and RUN" in err
- assert "signature or hash" in err
+ assert "Unsloth does not pin or verify the downloaded content" in err
+ assert "Continue only if you trust this source" in err
+
+
+def test_install_agent_reports_immutable_remote_installer_pin(monkeypatch, capsys):
+ monkeypatch.setattr(start.os, "name", "posix")
+ monkeypatch.setattr(start.sys, "stdin", SimpleNamespace(isatty = lambda: True))
+ monkeypatch.setattr(start.typer, "confirm", lambda *a, **k: False)
+ assert start._install_agent("hermes", start._HERMES_POSIX_INSTALL_HINT) is None
+ err = capsys.readouterr().err
+ assert start._HERMES_INSTALL_COMMIT in err
+ assert "immutable upstream commit" in err
+ assert "does not independently verify or sandbox it" in err
+ assert "does not pin or verify" not in err
def test_install_agent_warns_for_package_installer(monkeypatch, capsys):
@@ -160,8 +175,8 @@ def test_hermes_install_hint_is_windows_native_on_windows(monkeypatch):
# Scriptblock form so `-SkipSetup` reaches the installer and the interactive
# setup wizard is skipped during the unattended `unsloth start hermes` run.
assert start._hermes_install_hint() == (
- "& ([scriptblock]::Create((irm https://hermes-agent.nousresearch.com/install.ps1)))"
- " -SkipSetup"
+ f"& ([scriptblock]::Create((irm {start._HERMES_INSTALL_BASE}/install.ps1)))"
+ f" -SkipSetup -Commit {start._HERMES_INSTALL_COMMIT}"
)
@@ -170,11 +185,20 @@ def test_hermes_install_hint_is_bash_on_posix(monkeypatch):
# `bash -s -- --skip-setup` forwards the skip flag to the piped installer.
assert start._hermes_install_hint() == (
- "curl -fsSL https://raw.githubusercontent.com/NousResearch/hermes-agent"
- "/main/scripts/install.sh | bash -s -- --skip-setup"
+ f"curl -fsSL {start._HERMES_INSTALL_BASE}/install.sh | bash -s --"
+ f" --skip-setup --commit {start._HERMES_INSTALL_COMMIT}"
)
+def test_hermes_install_hints_pin_script_and_checkout_to_full_commit():
+ commit = start._HERMES_INSTALL_COMMIT
+ assert re.fullmatch(r"[0-9a-f]{40}", commit)
+ for hint in (start._HERMES_WINDOWS_INSTALL_HINT, start._HERMES_POSIX_INSTALL_HINT):
+ assert hint.count(commit) == 2
+ assert "/main/" not in hint
+ assert "hermes-agent.nousresearch.com" not in hint
+
+
def test_refresh_windows_path_noop_off_windows(monkeypatch):
monkeypatch.setattr(start.os, "name", "posix")
before = os.environ.get("PATH", "")
From 39497e6516bdc7d7edc2b09493b222c1dc2ce49c Mon Sep 17 00:00:00 2001
From: Lee Jackson <130007945+Imagineer99@users.noreply.github.com>
Date: Mon, 20 Jul 2026 05:05:22 +0100
Subject: [PATCH 040/271] Translate PWD for WSL-launched Windows agents (#7111)
Bridge PWD through WSLENV /p when launching a Windows npm shim from WSL so project-root discovery uses the live cwd. The no-launch recipe adds PWD/p without freezing PWD; the concrete cwd override applies only on direct launch.
---
unsloth_cli/commands/start.py | 16 ++++++++++++++--
unsloth_cli/tests/test_start.py | 16 ++++++++++++++--
2 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.py
index 6da31229f8..707c2b3c90 100644
--- a/unsloth_cli/commands/start.py
+++ b/unsloth_cli/commands/start.py
@@ -1274,6 +1274,16 @@ def _install_agent(name: str, install_hint: str) -> Optional[str]:
return executable
+def _wsl_shim_env(command: list, env: dict, unset_env: tuple) -> tuple[dict, tuple]:
+ wsl_env_bridge = _wsl_bridge_names(env, unset_env) if _wsl_windows_executable(command) else ()
+ if not wsl_env_bridge:
+ return env, wsl_env_bridge
+ # Bridge PWD via WSLENV (PWD/p) so the Windows shim finds its project root from the
+ # live cwd, not a stale inherited Linux PWD. Don't freeze env["PWD"]: a --no-launch
+ # recipe must translate the live PWD when run, not when generated; _launch overrides it.
+ return env, (*wsl_env_bridge, "PWD/p")
+
+
def _launch(
command: list,
env: dict,
@@ -1283,9 +1293,11 @@ def _launch(
executable = shutil.which(command[0]) or _install_agent(command[0], install_hint)
if executable is None:
_fail(f"`{command[0]}` not found on PATH. Install it with: {install_hint}")
- wsl_env_bridge = _wsl_bridge_names(env, unset_env) if _wsl_windows_executable(command) else ()
+ env, wsl_env_bridge = _wsl_shim_env(command, env, unset_env)
child_env = dict(os.environ)
if wsl_env_bridge:
+ # Override stale inherited PWD with the real cwd so the shim resolves the project root.
+ env = {**env, "PWD": os.getcwd()}
child_env["WSLENV"] = _merge_wslenv(child_env.get("WSLENV", ""), wsl_env_bridge)
for name in unset_env:
child_env[name] = ""
@@ -1353,8 +1365,8 @@ def _run(
if launch and clear_screen:
click.clear()
typer.echo(f"Unsloth {base} · model {entry['id']}")
- wsl_env_bridge = _wsl_bridge_names(env, unset_env) if _wsl_windows_executable(command) else ()
if not launch:
+ env, wsl_env_bridge = _wsl_shim_env(command, env, unset_env)
_print_env(env, command, unset_env = unset_env, wsl_env_bridge = wsl_env_bridge)
return
try:
diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.py
index 98bd9f8157..227918f63e 100644
--- a/unsloth_cli/tests/test_start.py
+++ b/unsloth_cli/tests/test_start.py
@@ -476,8 +476,10 @@ def test_connect_claude_launch_scrubs_conflicting_auth_env(fake_studio, monkeypa
reason = "WSL-from-Linux scenario (calling a Windows agent .exe from inside WSL); "
"os.name is 'posix' under WSL, so this path can't run on a native Windows runner.",
)
-def test_connect_claude_windows_shim_from_wsl_bridges_env(fake_studio, monkeypatch):
+def test_connect_claude_windows_shim_from_wsl_bridges_env(fake_studio, monkeypatch, tmp_path):
captured = {}
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setenv("PWD", "/stale/outer/repo")
monkeypatch.setenv("WSL_DISTRO_NAME", "Ubuntu")
monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-anthropic-stale")
monkeypatch.setenv("CLAUDE_CODE_OAUTH_TOKEN", "oauth-stale")
@@ -505,6 +507,8 @@ def test_connect_claude_windows_shim_from_wsl_bridges_env(fake_studio, monkeypat
assert captured["env"]["ANTHROPIC_AUTH_TOKEN"] == "sk-unsloth-feedfacefeedface"
assert captured["env"]["ANTHROPIC_BASE_URL"] == BASE
assert captured["env"]["ANTHROPIC_MODEL"] == MODEL["id"]
+ assert captured["env"]["PWD"] == str(tmp_path)
+ assert "PWD/p" in captured["env"]["WSLENV"].split(":")
for name in (
"ANTHROPIC_AUTH_TOKEN",
"ANTHROPIC_BASE_URL",
@@ -520,7 +524,11 @@ def test_connect_claude_windows_shim_from_wsl_bridges_env(fake_studio, monkeypat
reason = "WSL-from-Linux scenario (calling a Windows agent .exe from inside WSL); "
"os.name is 'posix' under WSL, so this path can't run on a native Windows runner.",
)
-def test_connect_claude_no_launch_windows_shim_from_wsl_prints_wslenv(fake_studio, monkeypatch):
+def test_connect_claude_no_launch_windows_shim_from_wsl_prints_wslenv(
+ fake_studio, monkeypatch, tmp_path
+):
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setenv("PWD", "/stale/outer/repo")
monkeypatch.setenv("WSL_DISTRO_NAME", "Ubuntu")
monkeypatch.setattr(
start.shutil, "which", lambda _: "/mnt/c/Users/samle/AppData/Roaming/npm/claude"
@@ -532,6 +540,10 @@ def test_connect_claude_no_launch_windows_shim_from_wsl_prints_wslenv(fake_studi
assert "export ANTHROPIC_API_KEY=" in result.output
assert "export CLAUDE_CODE_OAUTH_TOKEN=" in result.output
assert "export WSLENV=" in result.output
+ # PWD must NOT be frozen into the recipe (no `export PWD=`): WSLENV PWD/p translates the
+ # shell's live PWD at run time, so a recipe reused from another dir resolves the project root.
+ assert "export PWD=" not in result.output
+ assert "PWD/p" in result.output
assert "ANTHROPIC_AUTH_TOKEN" in result.output
assert "CLAUDE_CODE_OAUTH_TOKEN" in result.output
From 95d9970233ff3f248c9a5f89a987084e088623e9 Mon Sep 17 00:00:00 2001
From: Nilay <118994073+NilayYadav@users.noreply.github.com>
Date: Mon, 20 Jul 2026 12:42:42 +0530
Subject: [PATCH 041/271] persist llama.cpp KV cache across idle auto-unload
(slot save/restore) (#7204)
* Studio: persist llama.cpp KV cache across idle auto-unload (slot save/restore)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: address KV persistence review feedback
* Studio: guard KV restore on launch config
* Studio: fix KV resume purge race, fingerprint requested ctx, purge on disable
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: re-check idle/keep-KV settings after slot save, ns file identity
* Studio: shard-aware KV guard, honor user --no-cache-prompt, early save cap
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: honor LLAMA_ARG_CACHE_PROMPT env in slot-save guard
* Studio: derive prompt-cache state from final argv for slot saves
* Studio: stat LoRA/control-vector sidecars in KV restore fingerprint
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: parse csv and FNAME:SCALE sidecar syntax in KV fingerprint
* Studio: address codex review on idle-unload KV resume
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: harden slot-save cleanup, cap accounting, stale-KV guard, save timeout
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Studio: treat unavailable KV estimate as full-cap for slot-save disk check
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han
---
studio/backend/core/inference/llama_cpp.py | 293 +++++++++
.../backend/core/inference/llama_keepwarm.py | 138 +++-
.../core/inference/llama_server_args.py | 2 +
studio/backend/main.py | 3 +-
studio/backend/routes/inference.py | 2 +-
studio/backend/routes/settings.py | 21 +-
.../tests/test_llama_cpp_mtp_detection.py | 19 +
.../tests/test_llama_cpp_slot_resume.py | 494 +++++++++++++++
.../backend/tests/test_llama_server_args.py | 12 +
.../backend/tests/test_openai_auto_switch.py | 588 +++++++++++++++++-
.../utils/openai_auto_switch_settings.py | 61 +-
studio/backend/utils/paths/storage_roots.py | 5 +
.../settings/api/openai-auto-switch.ts | 19 +-
.../components/model-auto-switch-section.tsx | 26 +-
studio/frontend/src/i18n/locales/en.ts | 3 +
15 files changed, 1644 insertions(+), 42 deletions(-)
create mode 100644 studio/backend/tests/test_llama_cpp_slot_resume.py
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index d7c7eed518..d4cab81bdf 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -23,6 +23,7 @@ import subprocess
import sys
import threading
import time
+import uuid
from pathlib import Path
from typing import (
Callable,
@@ -453,6 +454,23 @@ def _hf_offline_if_dns_dead():
os.environ.pop("TRANSFORMERS_OFFLINE", None)
+try:
+ _SLOT_SAVE_MAX_BYTES = int(os.environ.get("UNSLOTH_SLOT_SAVE_MAX_BYTES") or (10 << 30))
+except ValueError:
+ _SLOT_SAVE_MAX_BYTES = 10 << 30
+
+# The idle loop holds the lifecycle gate across a slot save, so a newly arriving
+# request waits on the in-flight save's HTTP call. Bound it (was 120s) so a slow
+# or stuck save can't stall the next request for minutes; best-effort save just
+# falls back to a plain unload. Override with UNSLOTH_SLOT_SAVE_TIMEOUT (seconds).
+try:
+ _SLOT_SAVE_HTTP_TIMEOUT = float(os.environ.get("UNSLOTH_SLOT_SAVE_TIMEOUT") or 30.0)
+except ValueError:
+ _SLOT_SAVE_HTTP_TIMEOUT = 30.0
+if _SLOT_SAVE_HTTP_TIMEOUT <= 0:
+ _SLOT_SAVE_HTTP_TIMEOUT = 30.0
+
+
def _swa_cache_path() -> Path:
home = os.environ.get("UNSLOTH_STUDIO_HOME") or os.environ.get("STUDIO_HOME")
base = Path(home) if home else Path.home() / ".unsloth" / "studio"
@@ -2000,6 +2018,12 @@ class LlamaCppBackend:
self._llama_log_path: Optional[Path] = None
self._cancel_event = threading.Event()
self._api_key: Optional[str] = None
+ self._slot_save_dir: Optional[str] = None
+ self._slot_save_binary: Optional[tuple[str, int]] = None
+ # (gguf_identity, launch_fingerprint) snapshotted at load, so a later slot
+ # save can tell whether the model files were swapped on disk since load.
+ self._slot_loaded_identity: Optional[tuple] = None
+ self._prompt_cache_disabled: bool = False
# True once a probe has completed; cleared on transient failure.
self._is_audio: bool = False
self._audio_type: Optional[str] = None
@@ -2638,6 +2662,7 @@ class LlamaCppBackend:
"supports_ctx_checkpoints": False,
"supports_no_cache_prompt": False,
"supports_metrics": False,
+ "supports_slot_save": False,
}
try:
mtime = int(Path(bin_path).stat().st_mtime)
@@ -2658,6 +2683,7 @@ class LlamaCppBackend:
supports_ctx_checkpoints = False
supports_no_cache_prompt = False
supports_metrics = False
+ supports_slot_save = False
try:
probe_env = cls._llama_server_env_for_binary(bin_path)
result = subprocess.run(
@@ -2756,6 +2782,7 @@ class LlamaCppBackend:
supports_ctx_checkpoints = _is_real("--ctx-checkpoints")
supports_no_cache_prompt = _is_real("--no-cache-prompt")
supports_metrics = _is_real("--metrics")
+ supports_slot_save = _is_real("--slot-save-path")
except (OSError, subprocess.SubprocessError) as exc:
logger.debug(f"llama-server --help probe failed: {exc}")
@@ -2773,6 +2800,7 @@ class LlamaCppBackend:
"supports_ctx_checkpoints": supports_ctx_checkpoints,
"supports_no_cache_prompt": supports_no_cache_prompt,
"supports_metrics": supports_metrics,
+ "supports_slot_save": supports_slot_save,
}
cls._capability_cache[cache_key] = info
return info
@@ -7332,6 +7360,26 @@ class LlamaCppBackend:
# when the binary advertises it (older/custom binaries may not).
if server_caps.get("supports_metrics"):
cmd.append("--metrics")
+ self._slot_save_dir = None
+ self._slot_save_binary = None
+ self._prompt_cache_disabled = False
+ if server_caps.get("supports_slot_save"):
+ try:
+ from utils.paths.storage_roots import ( # noqa: WPS433
+ llama_slot_cache_root,
+ )
+
+ slot_dir = llama_slot_cache_root()
+ slot_dir.mkdir(parents = True, exist_ok = True)
+ # Saved KV encodes chat content; keep it from other local users.
+ with contextlib.suppress(OSError):
+ os.chmod(slot_dir, 0o700)
+ cmd.extend(["--slot-save-path", str(slot_dir)])
+ self._slot_save_dir = str(slot_dir)
+ self._slot_save_binary = (binary, Path(binary).stat().st_mtime_ns)
+ except OSError:
+ self._slot_save_dir = None
+ self._slot_save_binary = None
cmd.extend(
self._ctx_integrity_flags(
n_parallel,
@@ -7529,6 +7577,7 @@ class LlamaCppBackend:
unsupported_cache_flags.append("--ctx-checkpoints")
if server_caps.get("supports_no_cache_prompt"):
cmd.append("--no-cache-prompt")
+ self._prompt_cache_disabled = True
else:
unsupported_cache_flags.append("--no-cache-prompt")
if unsupported_cache_flags:
@@ -8105,6 +8154,15 @@ class LlamaCppBackend:
if not self._healthy:
return False
+ # Snapshot the files the server actually loaded. If a GGUF shard or a
+ # LoRA/control-vector sidecar is swapped on disk afterwards while the
+ # old weights stay mapped, save_slots_for_resume() compares against
+ # this and refuses to persist KV that a reload could misapply.
+ if self._slot_save_dir:
+ self._slot_loaded_identity = (
+ self._gguf_file_identity(self._gguf_path),
+ self._slot_launch_fingerprint(),
+ )
return True
def _build_speculative_flags(
@@ -8690,6 +8748,10 @@ class LlamaCppBackend:
self._effective_context_length = None
self._max_context_length = None
self._reset_effective_parallel_slots()
+ self._slot_save_dir = None
+ self._slot_save_binary = None
+ self._slot_loaded_identity = None
+ self._prompt_cache_disabled = False
self._chat_template = None
self._chat_template_override = None
self._supports_reasoning = False
@@ -9216,6 +9278,237 @@ class LlamaCppBackend:
return False
return True
+ def _slot_launch_fingerprint(self) -> tuple:
+ # KV validity keys on extra args, stat'd sidecar weights, effective ctx.
+ sidecars = []
+ for path in self._sidecar_weight_files():
+ try:
+ st = os.stat(path)
+ sidecars.append((path, st.st_size, st.st_mtime_ns))
+ except OSError:
+ sidecars.append((path, None, None))
+ return (
+ tuple(self._extra_args or ()),
+ tuple(sidecars),
+ self._requested_n_ctx,
+ self._effective_context_length,
+ getattr(self, "_cache_type_kv", None),
+ self.effective_parallel_slots,
+ )
+
+ def _gguf_file_identity(self, path) -> Optional[tuple]:
+ # (size, mtime_ns) per shard: a split GGUF keys KV validity on every sibling.
+ p = Path(path)
+ paths = [p]
+ m = _SHARD_FULL_RE.match(p.name)
+ if m:
+ prefix, _first, total = m.groups()
+ paths = [
+ p.with_name(f"{prefix}-{i:05d}-of-{total}{p.suffix}")
+ for i in range(1, int(total) + 1)
+ ]
+ try:
+ return tuple((sp.stat().st_size, sp.stat().st_mtime_ns) for sp in paths)
+ except OSError:
+ return None
+
+ _SIDECAR_WEIGHT_FLAGS = (
+ "--lora",
+ "--lora-scaled",
+ "--control-vector",
+ "--control-vector-scaled",
+ )
+
+ def _sidecar_weight_files(self) -> list[str]:
+ # llama.cpp: comma-separated paths, FNAME:SCALE on -scaled (older builds: FNAME SCALE).
+ args = [str(a).strip() for a in (self._extra_args or ())]
+ files: list[str] = []
+ for i, arg in enumerate(args):
+ flag, sep, inline = arg.partition("=")
+ if flag not in self._SIDECAR_WEIGHT_FLAGS:
+ continue
+ operand = inline if sep else (args[i + 1] if i + 1 < len(args) else "")
+ if not operand:
+ continue
+ candidates = [operand]
+ pieces = [p for p in operand.split(",") if p]
+ if len(pieces) > 1:
+ candidates.extend(pieces)
+ if flag.endswith("-scaled"):
+ for item in list(candidates):
+ # ":" tail is a scale; rpartition spares drive letters.
+ head, colon, tail = item.rpartition(":")
+ if not (colon and head):
+ continue
+ try:
+ float(tail)
+ except ValueError:
+ continue
+ candidates.append(head)
+ for cand in candidates:
+ if cand not in files:
+ files.append(cand)
+ return files
+
+ def _prompt_cache_off(self) -> bool:
+ # Caching off makes restores useless; last prompt-cache flag wins, env only when unset.
+ last = None
+ for arg in self._extra_args or ():
+ flag = arg.strip().split("=", 1)[0]
+ if flag in ("--cache-prompt", "--no-cache-prompt"):
+ last = flag
+ if last is not None:
+ return last == "--no-cache-prompt"
+ if self._prompt_cache_disabled:
+ return True
+ if os.environ.get("LLAMA_ARG_NO_CACHE_PROMPT") is not None:
+ return True
+ env = (os.environ.get("LLAMA_ARG_CACHE_PROMPT") or "").strip().lower()
+ return env in {"off", "disabled", "false", "0"}
+
+ def save_slots_for_resume(
+ self, should_abort: Optional[Callable[[], bool]] = None
+ ) -> Optional[dict]:
+ if (
+ not self.is_loaded
+ or not self._slot_save_dir
+ or not self._gguf_path
+ or self._prompt_cache_off()
+ ):
+ return None
+ save_dir = Path(self._slot_save_dir)
+ gguf_stat = self._gguf_file_identity(self._gguf_path)
+ if gguf_stat is None:
+ return None
+ launch = self._slot_launch_fingerprint()
+ # If the GGUF or a sidecar was swapped on disk while the original weights
+ # stayed mapped, the live KV belongs to the old weights but a reload would
+ # load the new file. Persisting it would let restore misapply stale KV.
+ if self._slot_loaded_identity is not None and self._slot_loaded_identity != (
+ gguf_stat,
+ launch,
+ ):
+ logger.debug("Skipping slot save: model files changed on disk since load")
+ return None
+ try:
+ estimate = self._estimate_kv_cache_bytes(
+ self._effective_context_length or self._context_length or 0,
+ self._cache_type_kv,
+ n_parallel = self.effective_parallel_slots,
+ )
+ # Skip before writing anything when the estimate alone blows the cap,
+ # rather than fully writing a slot and discarding it afterwards.
+ if estimate > _SLOT_SAVE_MAX_BYTES:
+ logger.debug(
+ "Skipping slot save: estimated %d bytes exceeds cap %d",
+ estimate,
+ _SLOT_SAVE_MAX_BYTES,
+ )
+ return None
+ # A 0 estimate means metadata was insufficient, not a zero-byte cache:
+ # a slot can still be many GiB, so demand room for the whole cap before
+ # trusting the post-write check.
+ required = (estimate if estimate > 0 else _SLOT_SAVE_MAX_BYTES) + (1 << 30)
+ if shutil.disk_usage(save_dir).free < required:
+ logger.debug("Skipping slot save: insufficient free disk")
+ return None
+ except Exception:
+ pass
+ token = uuid.uuid4().hex[:8]
+ entries: list[dict] = []
+ total_bytes = 0
+ for slot in range(self.effective_parallel_slots):
+ # A request pending mid-save waits on the gate; stop wasting its time.
+ if should_abort is not None and should_abort():
+ break
+ filename = f"resume-{token}-slot{slot}.bin"
+ path = save_dir / filename
+ try:
+ resp = httpx.post(
+ f"{self.base_url}/slots/{slot}",
+ params = {"action": "save"},
+ json = {"filename": filename},
+ headers = self._auth_headers,
+ timeout = _SLOT_SAVE_HTTP_TIMEOUT,
+ trust_env = False,
+ )
+ except Exception as e:
+ logger.debug(f"slot {slot} save failed: {e}")
+ with contextlib.suppress(OSError):
+ path.unlink()
+ break
+ if resp.status_code != 200:
+ logger.debug(f"slot {slot} save returned HTTP {resp.status_code}")
+ with contextlib.suppress(OSError):
+ path.unlink()
+ continue
+ try:
+ body = resp.json()
+ if not isinstance(body, dict):
+ raise ValueError("slot save response was not a JSON object")
+ n_saved = int(body.get("n_saved") or 0)
+ except Exception as e:
+ # A 200 that still wrote a file but returns a malformed body must
+ # clean up like the transport/HTTP error paths above, or the file
+ # (which holds chat KV) is orphaned until the next startup sweep.
+ logger.debug(f"slot {slot} save returned an invalid response: {e}")
+ with contextlib.suppress(OSError):
+ path.unlink()
+ continue
+ if n_saved <= 0:
+ with contextlib.suppress(OSError):
+ path.unlink()
+ continue
+ # Account by the bytes actually on disk, not the server-reported
+ # count, so the cap holds even if a custom binary under-reports.
+ try:
+ n_written = path.stat().st_size
+ except OSError:
+ n_written = 0
+ total_bytes += n_written
+ entries.append({"id": slot, "filename": filename, "n_saved": n_saved})
+ if total_bytes > _SLOT_SAVE_MAX_BYTES:
+ break # already over the cap; the discard below cleans up
+ if not entries:
+ return None
+ if total_bytes > _SLOT_SAVE_MAX_BYTES:
+ logger.debug(
+ "Discarding slot save: %d bytes exceeds cap %d",
+ total_bytes,
+ _SLOT_SAVE_MAX_BYTES,
+ )
+ for entry in entries:
+ with contextlib.suppress(OSError):
+ (save_dir / entry["filename"]).unlink()
+ return None
+ return {
+ "dir": self._slot_save_dir,
+ "binary": self._slot_save_binary,
+ "gguf": str(self._gguf_path),
+ "gguf_stat": gguf_stat,
+ "launch": launch,
+ "slots": entries,
+ }
+
+ def restore_slots_for_resume(self, manifest: dict) -> None:
+ if not self.is_loaded or not self._slot_save_dir:
+ return
+ for entry in manifest.get("slots") or []:
+ try:
+ resp = httpx.post(
+ f"{self.base_url}/slots/{int(entry['id'])}",
+ params = {"action": "restore"},
+ json = {"filename": str(entry["filename"])},
+ headers = self._auth_headers,
+ timeout = _SLOT_SAVE_HTTP_TIMEOUT,
+ trust_env = False,
+ )
+ except Exception as e:
+ logger.debug(f"slot restore failed: {e}")
+ break
+ if resp.status_code != 200:
+ logger.debug(f"slot {entry.get('id')} restore returned HTTP {resp.status_code}")
+
def _maybe_recover_from_mtp_crash(self, exc: Optional[BaseException] = None) -> bool:
"""Schedule one background reload without MTP after a mid-generation death.
diff --git a/studio/backend/core/inference/llama_keepwarm.py b/studio/backend/core/inference/llama_keepwarm.py
index 86a8c8a404..3380ebf5f5 100644
--- a/studio/backend/core/inference/llama_keepwarm.py
+++ b/studio/backend/core/inference/llama_keepwarm.py
@@ -15,6 +15,7 @@ import asyncio
import contextlib
import threading
import time
+from pathlib import Path
from loggers import get_logger
@@ -30,6 +31,8 @@ _last_active = time.monotonic()
# otherwise 503 against an empty backend can reload it (set on unload, cleared on
# reload). Storing the quant means the reload restores the exact freed variant.
_last_unloaded_model = None
+# Slot KV manifest saved by the idle unload; whoever pops it owns deleting its files.
+_kv_resume = None
# Guards inflight bumps against the idle-check-then-unload race, and blocks new
# inference from starting mid-swap. Process-wide, not per-loop: the backend slot is
# shared across every event loop in the process, so a per-loop gate would let a
@@ -161,11 +164,17 @@ def inference_lifecycle_gate():
return _unload_gate()
-def note_model_loaded() -> None:
- """Record a successful GGUF load: stamp activity and drop any reload stash so
- a manual load clears it synchronously, not only on the next idle poll."""
+def note_model_loaded(backend = None) -> None:
+ """Stamp activity and synchronously drop any reload stash."""
_note_activity()
+ resume = take_kv_resume()
_set_last_unloaded(None)
+ if resume is None:
+ return
+ if backend is not None:
+ restore_kv_resume(backend, resume)
+ else:
+ _delete_resume_files(resume)
def note_model_unloaded() -> None:
@@ -182,9 +191,81 @@ def get_last_unloaded_model():
def _set_last_unloaded(value) -> None:
- global _last_unloaded_model
+ global _last_unloaded_model, _kv_resume
+ stale = None
with _lock:
_last_unloaded_model = value
+ if value is None and _kv_resume is not None:
+ stale, _kv_resume = _kv_resume, None
+ if stale:
+ _delete_resume_files(stale)
+
+
+def _delete_resume_files(manifest) -> None:
+ try:
+ base = Path(manifest.get("dir") or "")
+ for entry in manifest.get("slots") or []:
+ with contextlib.suppress(OSError):
+ (base / str(entry.get("filename"))).unlink()
+ except Exception:
+ pass
+
+
+def _set_kv_resume(value) -> None:
+ global _kv_resume
+ stale = None
+ with _lock:
+ if _kv_resume is not None and _kv_resume is not value:
+ stale = _kv_resume
+ _kv_resume = value
+ if stale:
+ _delete_resume_files(stale)
+
+
+def take_kv_resume():
+ global _kv_resume
+ with _lock:
+ manifest, _kv_resume = _kv_resume, None
+ return manifest
+
+
+def purge_kv_resume() -> None:
+ resume = take_kv_resume()
+ if resume:
+ _delete_resume_files(resume)
+
+
+def restore_kv_resume(backend, manifest) -> None:
+ try:
+ gguf = manifest.get("gguf")
+ binary = manifest.get("binary")
+ current = getattr(backend, "_gguf_path", None)
+ same_gguf = bool(gguf and current) and Path(current).resolve() == Path(gguf).resolve()
+ if same_gguf:
+ # Same path is not enough: shards may have been rewritten meanwhile.
+ identity = getattr(backend, "_gguf_file_identity", None)
+ same_gguf = callable(identity) and identity(current) == manifest.get("gguf_stat")
+ if same_gguf:
+ # Nor the same file: launch overrides can invalidate KV numerics.
+ fingerprint = getattr(backend, "_slot_launch_fingerprint", None)
+ same_gguf = callable(fingerprint) and manifest.get("launch") == fingerprint()
+ if same_gguf and binary and binary == getattr(backend, "_slot_save_binary", None):
+ logger.info("Restoring saved slot KV onto the reloaded model")
+ backend.restore_slots_for_resume(manifest)
+ except Exception as exc:
+ logger.debug("slot restore after reload failed: %s", exc)
+ finally:
+ _delete_resume_files(manifest)
+
+
+def sweep_slot_save_dir() -> None:
+ try:
+ from utils.paths.storage_roots import llama_slot_cache_root
+ for path in llama_slot_cache_root().glob("resume-*.bin"):
+ with contextlib.suppress(OSError):
+ path.unlink()
+ except Exception:
+ pass
class LlamaKeepWarmMiddleware:
@@ -266,7 +347,10 @@ def _loaded_identity(backend):
async def idle_unload_loop(poll_seconds: float = 15.0) -> None:
"""Unload the loaded GGUF once idle past the configured TTL. Inert when off."""
- from utils.openai_auto_switch_settings import get_auto_unload_idle_seconds
+ from utils.openai_auto_switch_settings import (
+ get_auto_unload_idle_seconds,
+ get_auto_unload_keep_kv,
+ )
seen_model = None
while True:
@@ -281,17 +365,47 @@ async def idle_unload_loop(poll_seconds: float = 15.0) -> None:
# Track by (id, variant): a (re)loaded model -- including the same repo
# at a different quant -- counts as activity so it survives one TTL
# before its first request (loads bypass the activity middleware).
- current = _loaded_identity(backend)
- if current != seen_model:
- seen_model = current
- if current is not None:
- _note_activity()
- _set_last_unloaded(None) # a model is loaded; drop stale stash
async with _unload_gate():
+ # Purging the stash mid-reload would race the restore.
+ current = _loaded_identity(backend)
+ if current != seen_model:
+ seen_model = current
+ if current is not None:
+ _note_activity()
+ _set_last_unloaded(None) # a model is loaded; drop stale stash
if backend.is_loaded and _is_idle(ttl):
freed = _loaded_identity(backend)
- await asyncio.to_thread(backend.unload_model)
+ manifest = None
+ if get_auto_unload_keep_kv():
+ try:
+ manifest = await asyncio.to_thread(
+ backend.save_slots_for_resume,
+ lambda: not _is_idle(ttl),
+ )
+ except Exception as exc:
+ logger.debug("slot save before idle unload failed: %s", exc)
+ # Re-read settings: the save can outlive a settings change.
+ ttl = get_auto_unload_idle_seconds()
+ if ttl <= 0 or not _is_idle(ttl):
+ if manifest:
+ _delete_resume_files(manifest)
+ continue
+ if manifest and not get_auto_unload_keep_kv():
+ _delete_resume_files(manifest)
+ manifest = None
+ try:
+ await asyncio.to_thread(backend.unload_model)
+ except Exception:
+ # Failed unload means nothing will stash the manifest.
+ if manifest:
+ _delete_resume_files(manifest)
+ raise
_set_last_unloaded(freed) # let an alias request reload it
+ if manifest and freed:
+ _set_kv_resume({"identity": freed, **manifest})
+ logger.info("Idle auto-unload: saved slot KV for restore on reload")
+ elif manifest:
+ _delete_resume_files(manifest)
logger.info("Idle auto-unload: freed GGUF after %ss idle", ttl)
seen_model = None
except Exception as exc:
diff --git a/studio/backend/core/inference/llama_server_args.py b/studio/backend/core/inference/llama_server_args.py
index e72e10e071..7b42d2f40d 100644
--- a/studio/backend/core/inference/llama_server_args.py
+++ b/studio/backend/core/inference/llama_server_args.py
@@ -70,6 +70,8 @@ _DENYLIST_GROUPS: tuple[frozenset[str], ...] = (
# llama-server's own built-in tools flag would silently stack on top of
# Unsloth's --enable-tools / --disable-tools policy resolver.
frozenset({"--tools"}),
+ # Slot-state dir: Studio owns it for KV persistence across idle unload.
+ frozenset({"--slot-save-path"}),
)
_DENYLIST: frozenset[str] = frozenset().union(*_DENYLIST_GROUPS)
diff --git a/studio/backend/main.py b/studio/backend/main.py
index 81d4c16e52..a1ff4d60da 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -547,8 +547,9 @@ async def lifespan(app: FastAPI):
threading.Thread(target = _warm_rag_embedder, daemon = True, name = "rag-embedder-warm").start()
# Idle auto-unload loop (no-op unless the OpenAI auto-unload TTL is set).
- from core.inference.llama_keepwarm import idle_unload_loop
+ from core.inference.llama_keepwarm import idle_unload_loop, sweep_slot_save_dir
+ sweep_slot_save_dir()
app.state.idle_unload_task = asyncio.create_task(idle_unload_loop())
# Initialize RSA key pair for API key encryption (external providers).
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 136e4f7645..9c08ea4b79 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -4710,7 +4710,7 @@ async def _load_model_impl(request: LoadRequest, fastapi_request: Request, curre
# Clear any idle-unload reload stash now, not only on the next poll.
from core.inference.llama_keepwarm import note_model_loaded
- note_model_loaded()
+ await asyncio.to_thread(note_model_loaded, llama_backend)
# A plain load advertises its own identifier; auto-switch overwrites
# this with the repo id right after _load_model_impl returns.
llama_backend._openai_advertised_id = None
diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.py
index ab0fd2fd99..17e64df918 100644
--- a/studio/backend/routes/settings.py
+++ b/studio/backend/routes/settings.py
@@ -36,9 +36,10 @@ from utils.helper_precache_settings import (
)
from utils.coding_agents import CODING_AGENTS, detect_installed_coding_agents
from utils.openai_auto_switch_settings import (
- DEFAULT_AUTO_UNLOAD_IDLE_SECONDS,
+ DEFAULT_AUTO_UNLOAD_KEEP_KV,
DEFAULT_OPENAI_AUTO_SWITCH_ENABLED,
get_auto_unload_idle_seconds,
+ get_auto_unload_keep_kv,
get_model_overrides,
get_openai_auto_switch_enabled,
get_stored_auto_unload_idle_seconds,
@@ -90,7 +91,9 @@ class HelperPrecacheResponse(BaseModel):
class OpenAIAutoSwitchPayload(BaseModel):
enabled: bool
- auto_unload_idle_seconds: int = Field(default = DEFAULT_AUTO_UNLOAD_IDLE_SECONDS, ge = 0)
+ # None leaves the stored value untouched (partial updates can't clobber it).
+ auto_unload_idle_seconds: Optional[int] = Field(default = None, ge = 0)
+ auto_unload_keep_kv: Optional[bool] = None
class OpenAIAutoSwitchResponse(BaseModel):
@@ -101,6 +104,7 @@ class OpenAIAutoSwitchResponse(BaseModel):
# UNSLOTH_MODEL_IDLE_TTL set and nothing stored, this is true even while enabled
# is false, so the UI can show idle-unload as active instead of "needs enable".
idle_unload_active: bool = False
+ auto_unload_keep_kv: bool = DEFAULT_AUTO_UNLOAD_KEEP_KV
class ModelOverridePayload(BaseModel):
@@ -198,6 +202,7 @@ def get_openai_auto_switch(
enabled = get_openai_auto_switch_enabled(),
auto_unload_idle_seconds = get_stored_auto_unload_idle_seconds(),
idle_unload_active = get_auto_unload_idle_seconds() > 0,
+ auto_unload_keep_kv = get_auto_unload_keep_kv(),
)
@@ -206,8 +211,8 @@ def update_openai_auto_switch(
payload: OpenAIAutoSwitchPayload, current_subject: str = Depends(get_current_subject)
) -> OpenAIAutoSwitchResponse:
try:
- enabled, idle_seconds = set_openai_auto_switch(
- payload.enabled, payload.auto_unload_idle_seconds
+ enabled, idle_seconds, keep_kv = set_openai_auto_switch(
+ payload.enabled, payload.auto_unload_idle_seconds, payload.auto_unload_keep_kv
)
except ValueError as exc:
raise log_and_http_error(
@@ -217,10 +222,16 @@ def update_openai_auto_switch(
event = "settings.update_openai_auto_switch_failed",
log = logger,
) from exc
+ idle_unload_active = get_auto_unload_idle_seconds() > 0
+ if not keep_kv or not idle_unload_active:
+ # Keep-KV off or idle unload disabled: drop already-saved chat context too.
+ from core.inference.llama_keepwarm import purge_kv_resume
+ purge_kv_resume()
return OpenAIAutoSwitchResponse(
enabled = enabled,
auto_unload_idle_seconds = idle_seconds,
- idle_unload_active = get_auto_unload_idle_seconds() > 0,
+ idle_unload_active = idle_unload_active,
+ auto_unload_keep_kv = keep_kv,
)
diff --git a/studio/backend/tests/test_llama_cpp_mtp_detection.py b/studio/backend/tests/test_llama_cpp_mtp_detection.py
index 8fe04c0e39..68b706ebf9 100644
--- a/studio/backend/tests/test_llama_cpp_mtp_detection.py
+++ b/studio/backend/tests/test_llama_cpp_mtp_detection.py
@@ -741,6 +741,25 @@ def test_probe_reports_windows_cache_flags_absent_for_older_binary(tmp_path):
assert caps["supports_no_cache_prompt"] is False
+@_NEEDS_BASH
+def test_probe_detects_slot_save_path(tmp_path):
+ fake = _make_fake_llama_server(
+ tmp_path / "llama-server",
+ "--slot-save-path PATH path to save slot kv cache\n--threads N\n",
+ )
+ _clear_caps_cache()
+ caps = LlamaCppBackend.probe_server_capabilities(str(fake))
+ assert caps["supports_slot_save"] is True
+
+
+@_NEEDS_BASH
+def test_probe_reports_slot_save_absent_for_older_binary(tmp_path):
+ fake = _make_fake_llama_server(tmp_path / "llama-server", "--threads N\n")
+ _clear_caps_cache()
+ caps = LlamaCppBackend.probe_server_capabilities(str(fake))
+ assert caps["supports_slot_save"] is False
+
+
def test_build_ngram_mod_flags_new():
flags = _build_ngram_mod_flags({"ngram_mod_flavor": "new"})
assert flags == [
diff --git a/studio/backend/tests/test_llama_cpp_slot_resume.py b/studio/backend/tests/test_llama_cpp_slot_resume.py
new file mode 100644
index 0000000000..8b20c952c4
--- /dev/null
+++ b/studio/backend/tests/test_llama_cpp_slot_resume.py
@@ -0,0 +1,494 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import os
+from types import SimpleNamespace
+
+import core.inference.llama_cpp as llama_cpp
+from core.inference.llama_cpp import LlamaCppBackend
+
+
+def _resume_backend(tmp_path, n_slots = 1):
+ backend = LlamaCppBackend()
+ backend._healthy = True
+ # No-op lifecycle methods so the atexit cleanup can kill the fake quietly.
+ backend._process = SimpleNamespace(
+ poll = lambda: None,
+ terminate = lambda: None,
+ wait = lambda *a, **k: 0,
+ kill = lambda: None,
+ pid = 0,
+ )
+ backend._port = 8081
+ backend._slot_save_dir = str(tmp_path)
+ backend._slot_save_binary = ("/bin/llama-server", 1)
+ (tmp_path / "model.gguf").write_bytes(b"gguf")
+ backend._gguf_path = str(tmp_path / "model.gguf")
+ backend._effective_parallel_slots = n_slots
+ backend._estimate_kv_cache_bytes = lambda *a, **k: 0
+ return backend
+
+
+def _fake_disk(monkeypatch, free = 1 << 40):
+ monkeypatch.setattr(llama_cpp.shutil, "disk_usage", lambda _p: SimpleNamespace(free = free))
+
+
+class _Resp:
+ def __init__(
+ self,
+ status_code = 200,
+ body = None,
+ ):
+ self.status_code = status_code
+ self._body = body or {}
+
+ def json(self):
+ return self._body
+
+
+def test_save_returns_none_when_slot_save_disabled(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ backend._slot_save_dir = None
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is None
+
+
+def test_save_skipped_when_prompt_cache_disabled(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ backend._prompt_cache_disabled = True
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is None
+
+
+def test_save_skipped_when_insufficient_free_disk(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ backend._estimate_kv_cache_bytes = lambda *a, **k: 1 << 40
+ _fake_disk(monkeypatch, free = 1 << 20)
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is None
+
+
+def test_save_collects_manifest_across_slots(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path, n_slots = 2)
+ _fake_disk(monkeypatch)
+ calls = []
+
+ def fake_post(url, **kwargs):
+ calls.append((url, kwargs["params"], kwargs["json"]))
+ return _Resp(200, {"n_saved": 40, "n_written": 100})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ manifest = backend.save_slots_for_resume()
+ assert manifest is not None
+ assert manifest["dir"] == str(tmp_path)
+ assert manifest["binary"] == ("/bin/llama-server", 1)
+ assert manifest["gguf"] == str(tmp_path / "model.gguf")
+ st = os.stat(manifest["gguf"])
+ assert manifest["gguf_stat"] == ((st.st_size, st.st_mtime_ns),)
+ assert manifest["launch"] == backend._slot_launch_fingerprint()
+ assert [e["id"] for e in manifest["slots"]] == [0, 1]
+ assert all(e["n_saved"] == 40 for e in manifest["slots"])
+ assert [c[1] for c in calls] == [{"action": "save"}] * 2
+ assert "/slots/0" in calls[0][0] and "/slots/1" in calls[1][0]
+
+
+def test_save_unlinks_empty_slot_and_returns_none(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ _fake_disk(monkeypatch)
+
+ def fake_post(url, **kwargs):
+ (tmp_path / kwargs["json"]["filename"]).write_bytes(b"")
+ return _Resp(200, {"n_saved": 0, "n_written": 0})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ assert backend.save_slots_for_resume() is None
+ assert list(tmp_path.glob("resume-*.bin")) == [] # empty-slot file removed
+
+
+def test_save_cap_breach_discards_all_files(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path, n_slots = 2)
+ _fake_disk(monkeypatch)
+ monkeypatch.setattr(llama_cpp, "_SLOT_SAVE_MAX_BYTES", 150)
+
+ def fake_post(url, **kwargs):
+ (tmp_path / kwargs["json"]["filename"]).write_bytes(b"x" * 100)
+ return _Resp(200, {"n_saved": 40, "n_written": 100})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ assert backend.save_slots_for_resume() is None # 200 bytes > 150 cap
+ assert list(tmp_path.glob("resume-*.bin")) == []
+
+
+def test_save_transport_error_aborts_remaining_slots(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path, n_slots = 3)
+ _fake_disk(monkeypatch)
+ calls = []
+
+ def fake_post(url, **kwargs):
+ calls.append(url)
+ raise OSError("connection refused")
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ assert backend.save_slots_for_resume() is None
+ assert len(calls) == 1 # no retries against a dead server
+
+
+def test_save_transport_error_unlinks_partial_file(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ _fake_disk(monkeypatch)
+
+ def fake_post(url, **kwargs):
+ (tmp_path / kwargs["json"]["filename"]).write_bytes(b"partial")
+ raise OSError("timed out")
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ assert backend.save_slots_for_resume() is None
+ assert list(tmp_path.glob("resume-*.bin")) == []
+
+
+def test_fingerprint_tracks_lora_sidecar_rewrite(tmp_path):
+ backend = _resume_backend(tmp_path)
+ adapter = tmp_path / "adapter.gguf"
+ adapter.write_bytes(b"v1")
+ backend._extra_args = ["--lora", str(adapter)]
+
+ before = backend._slot_launch_fingerprint()
+ adapter.write_bytes(b"v2-different") # re-exported adapter, same path
+ assert backend._slot_launch_fingerprint() != before
+
+ backend._extra_args = [f"--lora={adapter}"]
+ assert backend._sidecar_weight_files() == [str(adapter)]
+ backend._extra_args = ["--lora-scaled", str(adapter), "0.5"]
+ assert backend._sidecar_weight_files() == [str(adapter)]
+ backend._extra_args = ["--control-vector", str(adapter), "--threads", "4"]
+ assert backend._sidecar_weight_files() == [str(adapter)]
+
+
+def test_sidecar_files_parse_csv_and_colon_scale(tmp_path):
+ backend = _resume_backend(tmp_path)
+ a, b = tmp_path / "a.gguf", tmp_path / "b.gguf"
+
+ backend._extra_args = ["--lora", f"{a},{b}"]
+ files = backend._sidecar_weight_files()
+ assert str(a) in files and str(b) in files
+
+ backend._extra_args = ["--lora-scaled", f"{a}:0.5"]
+ assert str(a) in backend._sidecar_weight_files()
+
+ backend._extra_args = ["--control-vector-scaled", f"{a}:1.0,{b}:2.0"]
+ files = backend._sidecar_weight_files()
+ assert str(a) in files and str(b) in files
+
+ # Windows drive letter must not be mistaken for a scale separator.
+ backend._extra_args = ["--lora-scaled", "C:\\adapters\\a.gguf:0.75"]
+ assert "C:\\adapters\\a.gguf" in backend._sidecar_weight_files()
+ backend._extra_args = ["--lora", "C:\\adapters\\a.gguf"]
+ assert backend._sidecar_weight_files() == ["C:\\adapters\\a.gguf"]
+
+
+def test_fingerprint_tracks_colon_scaled_adapter_rewrite(tmp_path):
+ backend = _resume_backend(tmp_path)
+ adapter = tmp_path / "adapter.gguf"
+ adapter.write_bytes(b"v1")
+ backend._extra_args = ["--lora-scaled", f"{adapter}:0.5"]
+
+ before = backend._slot_launch_fingerprint()
+ adapter.write_bytes(b"v2-different") # re-exported adapter, same path
+ assert backend._slot_launch_fingerprint() != before
+
+
+def test_fingerprint_tracks_effective_context_length(tmp_path):
+ backend = _resume_backend(tmp_path)
+ backend._effective_context_length = 8192
+
+ before = backend._slot_launch_fingerprint()
+ backend._effective_context_length = 4096 # auto-fit landed smaller on reload
+ assert backend._slot_launch_fingerprint() != before
+
+
+def test_gguf_file_identity_covers_split_shards(tmp_path):
+ backend = _resume_backend(tmp_path)
+ first = tmp_path / "m-00001-of-00002.gguf"
+ second = tmp_path / "m-00002-of-00002.gguf"
+ first.write_bytes(b"a")
+ second.write_bytes(b"bb")
+
+ before = backend._gguf_file_identity(str(first))
+ st1, st2 = os.stat(first), os.stat(second)
+ assert before == ((st1.st_size, st1.st_mtime_ns), (st2.st_size, st2.st_mtime_ns))
+
+ second.write_bytes(b"rewritten") # sibling changes, primary untouched
+ after = backend._gguf_file_identity(str(first))
+ assert after is not None and after != before
+ assert after[0] == before[0] # primary shard unchanged
+
+ second.unlink()
+ assert backend._gguf_file_identity(str(first)) is None # missing shard
+
+
+def test_save_skipped_when_user_disabled_prompt_cache(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ backend._extra_args = ["--no-cache-prompt"]
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is None
+
+
+def test_save_skipped_when_env_disables_prompt_cache(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ monkeypatch.setenv("LLAMA_ARG_CACHE_PROMPT", "0")
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is None
+ monkeypatch.delenv("LLAMA_ARG_CACHE_PROMPT")
+ monkeypatch.setenv("LLAMA_ARG_NO_CACHE_PROMPT", "1") # legacy negative form
+ assert backend.save_slots_for_resume() is None
+
+
+def test_explicit_cache_prompt_flag_overrides_env(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ monkeypatch.setenv("LLAMA_ARG_CACHE_PROMPT", "0")
+ backend._extra_args = ["--cache-prompt"] # CLI wins over env in llama.cpp
+ _fake_disk(monkeypatch)
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: _Resp(200, {"n_saved": 1, "n_written": 1}),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is not None
+
+
+def test_user_cache_prompt_overrides_studio_no_cache_flag(monkeypatch, tmp_path):
+ # User extras follow Studio's flags, so an explicit --cache-prompt wins.
+ backend = _resume_backend(tmp_path)
+ backend._prompt_cache_disabled = True
+ backend._extra_args = ["--cache-prompt"]
+ _fake_disk(monkeypatch)
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: _Resp(200, {"n_saved": 1, "n_written": 1}),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is not None
+ # Last flag wins when both appear in extras.
+ backend._extra_args = ["--cache-prompt", "--no-cache-prompt"]
+ assert backend.save_slots_for_resume() is None
+
+
+def test_save_stops_writing_once_cap_exceeded(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path, n_slots = 3)
+ _fake_disk(monkeypatch)
+ monkeypatch.setattr(llama_cpp, "_SLOT_SAVE_MAX_BYTES", 150)
+ calls = []
+
+ def fake_post(url, **kwargs):
+ calls.append(url)
+ (tmp_path / kwargs["json"]["filename"]).write_bytes(b"x" * 100)
+ return _Resp(200, {"n_saved": 1, "n_written": 100})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ assert backend.save_slots_for_resume() is None
+ assert len(calls) == 2 # cap blown after slot 1; slot 2 never attempted
+ assert list(tmp_path.glob("resume-*.bin")) == []
+
+
+def test_save_aborts_between_slots_when_no_longer_idle(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path, n_slots = 3)
+ _fake_disk(monkeypatch)
+ calls = []
+
+ def fake_post(url, **kwargs):
+ calls.append(url)
+ return _Resp(200, {"n_saved": 5, "n_written": 10})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ aborts = iter([False, True, True])
+ manifest = backend.save_slots_for_resume(should_abort = lambda: next(aborts))
+ assert len(calls) == 1 # slots 1 and 2 skipped
+ assert manifest is not None
+ assert [e["id"] for e in manifest["slots"]] == [0]
+
+
+def test_save_non_200_slot_is_skipped_but_others_kept(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path, n_slots = 2)
+ _fake_disk(monkeypatch)
+
+ def fake_post(url, **kwargs):
+ if "/slots/0" in url:
+ return _Resp(500)
+ return _Resp(200, {"n_saved": 5, "n_written": 10})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ manifest = backend.save_slots_for_resume()
+ assert manifest is not None
+ assert [e["id"] for e in manifest["slots"]] == [1]
+
+
+def test_restore_posts_each_slot_and_tolerates_failures(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ calls = []
+
+ def fake_post(url, **kwargs):
+ calls.append((url, kwargs["params"], kwargs["json"]))
+ return _Resp(500 if "/slots/0" in url else 200, {"n_restored": 5})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ backend.restore_slots_for_resume(
+ {
+ "slots": [
+ {"id": 0, "filename": "resume-a-slot0.bin", "n_saved": 5},
+ {"id": 1, "filename": "resume-a-slot1.bin", "n_saved": 5},
+ ]
+ }
+ )
+ assert [c[1] for c in calls] == [{"action": "restore"}] * 2
+ assert calls[0][2] == {"filename": "resume-a-slot0.bin"}
+
+
+def test_restore_transport_error_stops_early(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ calls = []
+
+ def fake_post(url, **kwargs):
+ calls.append(url)
+ raise OSError("connection refused")
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ backend.restore_slots_for_resume(
+ {"slots": [{"id": 0, "filename": "a.bin"}, {"id": 1, "filename": "b.bin"}]}
+ )
+ assert len(calls) == 1
+
+
+def test_save_deletes_orphan_on_malformed_response(monkeypatch, tmp_path):
+ # A 200 that writes a file but returns a non-numeric counter must be cleaned
+ # up like any other save failure, not left orphaned holding chat KV.
+ backend = _resume_backend(tmp_path)
+ _fake_disk(monkeypatch)
+
+ def fake_post(url, **kwargs):
+ (tmp_path / kwargs["json"]["filename"]).write_bytes(b"chat-kv")
+ return _Resp(200, {"n_saved": "not-an-int"})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ assert backend.save_slots_for_resume() is None
+ assert list(tmp_path.glob("resume-*.bin")) == []
+
+
+def test_save_deletes_orphan_on_non_dict_response(monkeypatch, tmp_path):
+ backend = _resume_backend(tmp_path)
+ _fake_disk(monkeypatch)
+
+ def fake_post(url, **kwargs):
+ (tmp_path / kwargs["json"]["filename"]).write_bytes(b"chat-kv")
+ return _Resp(200, ["unexpected", "list"])
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ assert backend.save_slots_for_resume() is None
+ assert list(tmp_path.glob("resume-*.bin")) == []
+
+
+def test_save_cap_uses_actual_file_size_not_reported_bytes(monkeypatch, tmp_path):
+ # A binary under-reporting n_written must not slip past the disk cap: the
+ # cap is enforced against the bytes actually on disk.
+ backend = _resume_backend(tmp_path)
+ _fake_disk(monkeypatch)
+ monkeypatch.setattr(llama_cpp, "_SLOT_SAVE_MAX_BYTES", 150)
+
+ def fake_post(url, **kwargs):
+ (tmp_path / kwargs["json"]["filename"]).write_bytes(b"x" * 200)
+ return _Resp(200, {"n_saved": 5, "n_written": 1}) # under-reported
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ assert backend.save_slots_for_resume() is None # 200 real bytes > 150 cap
+ assert list(tmp_path.glob("resume-*.bin")) == []
+
+
+def test_save_skipped_when_estimate_exceeds_cap(monkeypatch, tmp_path):
+ # An estimate over the cap skips before writing any slot at all.
+ backend = _resume_backend(tmp_path)
+ backend._estimate_kv_cache_bytes = lambda *a, **k: 1 << 40
+ monkeypatch.setattr(llama_cpp, "_SLOT_SAVE_MAX_BYTES", 1 << 20)
+ _fake_disk(monkeypatch)
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is None
+
+
+def test_save_skipped_when_model_file_changed_since_load(monkeypatch, tmp_path):
+ # The GGUF/sidecars were swapped on disk after the server loaded them, so the
+ # live KV belongs to the old weights: refuse to persist it (no POST at all).
+ backend = _resume_backend(tmp_path)
+ backend._slot_loaded_identity = ((("stale", 0),), ()) # != current identity
+ _fake_disk(monkeypatch)
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is None
+
+
+def test_save_proceeds_when_load_identity_matches(monkeypatch, tmp_path):
+ # Matching load-time snapshot: the save runs normally.
+ backend = _resume_backend(tmp_path)
+ backend._slot_loaded_identity = (
+ backend._gguf_file_identity(backend._gguf_path),
+ backend._slot_launch_fingerprint(),
+ )
+ _fake_disk(monkeypatch)
+
+ def fake_post(url, **kwargs):
+ (tmp_path / kwargs["json"]["filename"]).write_bytes(b"kv")
+ return _Resp(200, {"n_saved": 5, "n_written": 2})
+
+ monkeypatch.setattr(llama_cpp.httpx, "post", fake_post, raising = False)
+ manifest = backend.save_slots_for_resume()
+ assert manifest is not None
+ assert [e["id"] for e in manifest["slots"]] == [0]
+
+
+def test_save_skipped_when_estimate_unavailable_and_low_disk(monkeypatch, tmp_path):
+ # A 0 estimate means metadata was insufficient, not a zero-byte cache: the save
+ # must demand room for the whole cap, not just 1 GiB, on a low-disk host.
+ backend = _resume_backend(tmp_path)
+ backend._estimate_kv_cache_bytes = lambda *a, **k: 0 # metadata unavailable
+ monkeypatch.setattr(llama_cpp, "_SLOT_SAVE_MAX_BYTES", 8 << 30) # 8 GiB cap
+ _fake_disk(monkeypatch, free = 2 << 30) # 2 GiB free < 8 + 1 GiB required
+ monkeypatch.setattr(
+ llama_cpp.httpx,
+ "post",
+ lambda *a, **k: (_ for _ in ()).throw(AssertionError),
+ raising = False,
+ )
+ assert backend.save_slots_for_resume() is None
diff --git a/studio/backend/tests/test_llama_server_args.py b/studio/backend/tests/test_llama_server_args.py
index c6d16363f8..fa4ba71791 100644
--- a/studio/backend/tests/test_llama_server_args.py
+++ b/studio/backend/tests/test_llama_server_args.py
@@ -183,6 +183,8 @@ def test_non_flag_token_passes_through():
"--reranking",
# llama-server's own --tools clashes with Unsloth's tool policy.
"--tools",
+ # Slot-state dir: Studio owns it for KV persistence across idle unload.
+ "--slot-save-path",
],
)
def test_denylist_rejects_all_aliases(denied):
@@ -224,6 +226,16 @@ def test_denylist_rejects_equals_form():
validate_extra_args(["--port=9000"])
+def test_slot_save_path_is_managed_in_all_forms():
+ for args in (["--slot-save-path", "/tmp/x"], ["--slot-save-path=/tmp/x"], ["--slot-save-path"]):
+ with pytest.raises(ValueError, match = "--slot-save-path"):
+ validate_extra_args(args)
+ assert is_managed_flag("--slot-save-path") is True
+ assert is_managed_flag("--slot-save-path=/tmp/x") is True
+ # --slots (read-only diagnostics endpoint) stays a user choice.
+ assert is_managed_flag("--slots") is False
+
+
@pytest.mark.parametrize(
"padded",
[" --parallel", "--parallel ", "\t--parallel", " -np", "-np \n", "-np\t"],
diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.py
index c4c0ce15c9..1ee9ef36d3 100644
--- a/studio/backend/tests/test_openai_auto_switch.py
+++ b/studio/backend/tests/test_openai_auto_switch.py
@@ -8,6 +8,7 @@ tests/test_gguf_completion_usage.py.
"""
import asyncio
+import os
import pytest
@@ -18,6 +19,10 @@ from utils import openai_auto_switch_settings as settings
class _FakeBackend:
+ effective_parallel_slots = 1
+ _slot_save_binary = None
+ _gguf_path = None
+
def __init__(
self,
loaded_id = None,
@@ -29,6 +34,22 @@ class _FakeBackend:
self.hf_variant = hf_variant
self._openai_advertised_id = advertised_id
+ def save_slots_for_resume(self, should_abort = None):
+ return None
+
+ def restore_slots_for_resume(self, manifest):
+ return None
+
+ def _slot_launch_fingerprint(self):
+ return ((), None, None, 1)
+
+ def _gguf_file_identity(self, path):
+ try:
+ st = os.stat(path)
+ except OSError:
+ return None
+ return ((st.st_size, st.st_mtime_ns),)
+
class _LoadRecorder:
"""Stand-in for the load route: records calls and simulates a load."""
@@ -53,10 +74,15 @@ class _LoadRecorder:
from fastapi import HTTPException
raise HTTPException(status_code = 503, detail = "load failed")
self.backend.model_identifier = request.model_path
+ self.backend.hf_variant = getattr(request, "gguf_variant", None)
+ self.backend._gguf_path = request.model_path
self.backend.is_loaded = True
# Mirror _load_model_impl: a load advertises its own id until the
# auto-switch caller overwrites it with the repo id.
self.backend._openai_advertised_id = None
+ from core.inference import llama_keepwarm as kw
+
+ kw.note_model_loaded(self.backend)
return None
@@ -446,6 +472,75 @@ def test_idle_loop_unloads_after_ttl_and_stashes_for_reload(monkeypatch):
assert stash is not None and stash[0] == "unsloth/Idle-GGUF" and stash[1] == "Q4_K_M"
+def test_idle_loop_deletes_saved_kv_when_unload_fails(monkeypatch, tmp_path):
+ import time
+ from core.inference import llama_keepwarm as kw
+
+ monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)
+ monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: True)
+ kw._inflight = 0
+ kw._pending = 0
+ kw._last_active = time.monotonic() - 3600
+ kw._last_unloaded_model = None
+ kw._kv_resume = None
+
+ saved = tmp_path / "resume-abc-slot0.bin"
+ backend = _FakeBackend("unsloth/Idle-GGUF")
+ manifests = []
+
+ def _save(should_abort = None):
+ if manifests:
+ return None
+ saved.write_bytes(b"kv")
+ manifest = {"dir": str(tmp_path), "slots": [{"id": 0, "filename": saved.name}]}
+ manifests.append(manifest)
+ return manifest
+
+ def _unload():
+ raise RuntimeError("cuda teardown failed")
+
+ backend.save_slots_for_resume = _save
+ backend.unload_model = _unload
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
+
+ async def _drive():
+ task = asyncio.create_task(kw.idle_unload_loop(poll_seconds = 0.01))
+ for _ in range(200):
+ await asyncio.sleep(0.01)
+ if manifests and not saved.exists():
+ break
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
+
+ asyncio.run(_drive())
+ assert manifests and not saved.exists()
+ assert kw._kv_resume is None
+
+
+def test_disabling_idle_unload_purges_saved_kv(monkeypatch, tmp_path):
+ # PUT leaves keep-KV on but makes idle unload inactive: saved KV must go too.
+ import routes.settings as settings_route
+ from core.inference import llama_keepwarm as kw
+
+ saved = tmp_path / "resume-abc-slot0.bin"
+ saved.write_bytes(b"kv")
+ kw._kv_resume = {
+ "identity": ("m", None, "m"),
+ "dir": str(tmp_path),
+ "slots": [{"id": 0, "filename": saved.name}],
+ }
+ monkeypatch.setattr(settings_route, "set_openai_auto_switch", lambda *a: (False, 300, True))
+ monkeypatch.setattr(settings_route, "get_auto_unload_idle_seconds", lambda: 0)
+
+ payload = settings_route.OpenAIAutoSwitchPayload(enabled = False)
+ resp = settings_route.update_openai_auto_switch(payload, "tester")
+ assert resp.idle_unload_active is False and resp.auto_unload_keep_kv is True
+ assert kw._kv_resume is None and not saved.exists()
+
+
def test_audio_generate_is_tracked_as_inference_path():
# Direct GGUF TTS uses the llama backend and can outlive the idle TTL, so
# the keep-warm middleware must count it as in-flight inference.
@@ -2912,8 +3007,10 @@ def test_non_gguf_load_clears_reload_stash():
# A non-GGUF (Transformers/Unsloth) load must clear the stash like the GGUF
# branch, so it never lingers until the idle poll (or forever, idle-unload off).
import inspect
+
src = inspect.getsource(inference_route._load_model_impl)
- assert src.count("note_model_loaded()") >= 2
+ assert src.count("note_model_loaded()") >= 1 # non-GGUF branch
+ assert "to_thread(note_model_loaded, llama_backend)" in src # GGUF branch
def test_chat_rejects_malformed_tool_choice_before_switch(monkeypatch):
@@ -3121,6 +3218,495 @@ def test_responses_stream_hint_matches_toggle_regardless_of_active_model(monkeyp
assert "Model auto-switch" in non_gguf_loaded
+# ── idle-unload KV persistence (slot save/restore) ──────────────────
+
+
+def _seed_kv_manifest(
+ tmp_path,
+ identity = ("unsloth/A-GGUF", "Q4_K_M", "unsloth/A-GGUF"),
+ gguf = None,
+):
+ if gguf is None:
+ gguf_file = tmp_path / "model.gguf"
+ gguf_file.write_bytes(b"gguf")
+ gguf = str(gguf_file)
+ st = os.stat(gguf)
+ state_file = tmp_path / "resume-abc-slot0.bin"
+ state_file.write_bytes(b"kv")
+ return state_file, {
+ "identity": identity,
+ "dir": str(tmp_path),
+ "binary": ("/bin/llama-server", 111),
+ "gguf": gguf,
+ "gguf_stat": ((st.st_size, st.st_mtime_ns),),
+ "launch": ((), None, None, 1),
+ "slots": [{"id": 0, "filename": state_file.name, "n_saved": 42}],
+ }
+
+
+def _drive_idle_loop(
+ kw,
+ poll_seconds = 0.02,
+ run_for = 0.2,
+):
+ async def _drive():
+ task = asyncio.create_task(kw.idle_unload_loop(poll_seconds = poll_seconds))
+ await asyncio.sleep(run_for)
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
+
+ asyncio.run(_drive())
+
+
+def test_idle_unload_saves_slots_before_unload_and_stashes_manifest(monkeypatch, tmp_path):
+ import time
+ from core.inference import llama_keepwarm as kw
+
+ monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)
+ monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: True)
+ kw._inflight = 0
+ kw._pending = 0
+ kw._last_active = time.monotonic() - 3600
+ kw._last_unloaded_model = None
+ kw._kv_resume = None
+
+ events = []
+ backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")
+ manifest = {
+ "dir": str(tmp_path),
+ "binary": ("bin", 1),
+ "slots": [{"id": 0, "filename": "f.bin", "n_saved": 42}],
+ }
+
+ def _save(should_abort = None):
+ events.append("save")
+ return manifest
+
+ def _unload():
+ events.append("unload")
+ backend.is_loaded = False
+
+ backend.save_slots_for_resume = _save
+ backend.unload_model = _unload
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
+
+ _drive_idle_loop(kw)
+ # KV must be saved while the server is still alive, then exactly one unload.
+ assert events == ["save", "unload"]
+ assert kw.get_last_unloaded_model()[:2] == ("unsloth/Idle-GGUF", "Q4_K_M")
+ resume = kw.take_kv_resume()
+ assert resume is not None
+ assert resume["identity"][:2] == ("unsloth/Idle-GGUF", "Q4_K_M")
+ assert resume["slots"][0]["filename"] == "f.bin"
+
+
+def test_idle_save_failure_still_unloads_plain(monkeypatch):
+ import time
+ from core.inference import llama_keepwarm as kw
+
+ monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)
+ monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: True)
+ kw._inflight = 0
+ kw._pending = 0
+ kw._last_active = time.monotonic() - 3600
+ kw._last_unloaded_model = None
+ kw._kv_resume = None
+
+ unloads = []
+ backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")
+
+ def _save(should_abort = None):
+ raise RuntimeError("slot save exploded")
+
+ def _unload():
+ unloads.append(1)
+ backend.is_loaded = False
+
+ backend.save_slots_for_resume = _save
+ backend.unload_model = _unload
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
+
+ _drive_idle_loop(kw)
+ assert unloads == [1] # the save failure must not skip the unload
+ assert kw.get_last_unloaded_model() is not None
+ assert kw.take_kv_resume() is None
+
+
+def test_keep_kv_setting_off_skips_save(monkeypatch):
+ import time
+ from core.inference import llama_keepwarm as kw
+
+ monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)
+ monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: False)
+ kw._inflight = 0
+ kw._pending = 0
+ kw._last_active = time.monotonic() - 3600
+ kw._last_unloaded_model = None
+ kw._kv_resume = None
+
+ saves, unloads = [], []
+ backend = _FakeBackend("unsloth/Idle-GGUF")
+
+ def _unload():
+ unloads.append(1)
+ backend.is_loaded = False
+
+ backend.save_slots_for_resume = lambda *a, **k: saves.append(1)
+ backend.unload_model = _unload
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
+
+ _drive_idle_loop(kw)
+ assert saves == []
+ assert unloads == [1]
+ assert kw.take_kv_resume() is None
+
+
+def test_keep_kv_disabled_mid_save_discards_manifest(monkeypatch, tmp_path):
+ import time
+ from core.inference import llama_keepwarm as kw
+
+ keep = {"on": True}
+ monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 0.005)
+ monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: keep["on"])
+ kw._inflight = 0
+ kw._pending = 0
+ kw._last_active = time.monotonic() - 3600
+ kw._last_unloaded_model = None
+ kw._kv_resume = None
+
+ unloads = []
+ backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")
+ state_file = tmp_path / "resume-mid-slot0.bin"
+ state_file.write_bytes(b"kv")
+ manifest = {
+ "dir": str(tmp_path),
+ "binary": ("bin", 1),
+ "slots": [{"id": 0, "filename": state_file.name, "n_saved": 1}],
+ }
+
+ def _save(should_abort = None):
+ keep["on"] = False # user flips the toggle while the save runs
+ return manifest
+
+ def _unload():
+ unloads.append(1)
+ backend.is_loaded = False
+
+ backend.save_slots_for_resume = _save
+ backend.unload_model = _unload
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
+
+ _drive_idle_loop(kw)
+ assert unloads == [1] # still unloads; only the stash is dropped
+ assert kw.take_kv_resume() is None
+ assert not state_file.exists()
+
+
+def test_idle_ttl_disabled_mid_save_skips_unload(monkeypatch, tmp_path):
+ import time
+ from core.inference import llama_keepwarm as kw
+
+ ttl = {"v": 0.005}
+ monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: ttl["v"])
+ monkeypatch.setattr(settings, "get_auto_unload_keep_kv", lambda: True)
+ kw._inflight = 0
+ kw._pending = 0
+ kw._last_active = time.monotonic() - 3600
+ kw._last_unloaded_model = None
+ kw._kv_resume = None
+
+ unloads = []
+ backend = _FakeBackend("unsloth/Idle-GGUF", hf_variant = "Q4_K_M")
+ state_file = tmp_path / "resume-mid-slot0.bin"
+ state_file.write_bytes(b"kv")
+ manifest = {
+ "dir": str(tmp_path),
+ "binary": ("bin", 1),
+ "slots": [{"id": 0, "filename": state_file.name, "n_saved": 1}],
+ }
+
+ def _save(should_abort = None):
+ ttl["v"] = 0 # user turns idle unload off while the save runs
+ return manifest
+
+ backend.save_slots_for_resume = _save
+ backend.unload_model = lambda: unloads.append(1)
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
+
+ _drive_idle_loop(kw)
+ assert unloads == [] # the unload was cancelled by the setting change
+ assert kw.take_kv_resume() is None
+ assert not state_file.exists()
+
+
+def test_alias_reload_restores_slots_and_deletes_files(monkeypatch, tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ backend = _FakeBackend(None) # idle-unload emptied the backend
+ backend._slot_save_binary = ("/bin/llama-server", 111)
+ restored = []
+ backend.restore_slots_for_resume = lambda manifest: restored.append(manifest)
+
+ rec = _LoadRecorder(backend)
+ _wire(monkeypatch, enabled = True, resolves_to = None, backend = backend, recorder = rec)
+ monkeypatch.setattr(kw, "_inflight", 0)
+ state_file, manifest = _seed_kv_manifest(tmp_path)
+ monkeypatch.setattr(kw, "_last_unloaded_model", (manifest["gguf"], "Q4_K_M"))
+ monkeypatch.setattr(kw, "_kv_resume", manifest)
+
+ _run_hook("gpt-4o-mini")
+ assert len(rec.calls) == 1
+ assert len(restored) == 1 # same model + binary: restore ran
+ assert not state_file.exists() # state file deleted after the restore
+ assert kw._kv_resume is None
+
+
+def test_no_restore_when_different_model_loads(monkeypatch, tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ backend = _FakeBackend(None)
+ backend._slot_save_binary = ("/bin/llama-server", 111)
+ restored = []
+ backend.restore_slots_for_resume = lambda manifest: restored.append(manifest)
+ rec = _LoadRecorder(backend)
+ _wire(
+ monkeypatch,
+ enabled = True,
+ resolves_to = ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),
+ backend = backend,
+ recorder = rec,
+ )
+ monkeypatch.setattr(kw, "_inflight", 0)
+ state_file, manifest = _seed_kv_manifest(tmp_path) # manifest is for model A
+ monkeypatch.setattr(kw, "_kv_resume", manifest)
+
+ _run_hook("unsloth/B-GGUF")
+ assert len(rec.calls) == 1
+ assert restored == [] # different model: never restored
+ assert not state_file.exists() # but the stale files are gone
+ assert kw._kv_resume is None
+
+
+def test_restore_skipped_when_binary_changed(monkeypatch, tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ state_file, manifest = _seed_kv_manifest(tmp_path)
+ backend = _FakeBackend("unsloth/A-GGUF", hf_variant = "Q4_K_M")
+ backend._gguf_path = manifest["gguf"]
+ backend._slot_save_binary = ("/bin/llama-server", 222) # newer mtime
+ restored = []
+ backend.restore_slots_for_resume = lambda manifest: restored.append(manifest)
+
+ kw.restore_kv_resume(backend, manifest)
+ assert restored == []
+ assert not state_file.exists()
+
+
+def test_restore_skipped_when_launch_config_changed(tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ state_file, manifest = _seed_kv_manifest(tmp_path)
+ backend = _FakeBackend("unsloth/A-GGUF", hf_variant = "Q4_K_M")
+ backend._gguf_path = manifest["gguf"]
+ backend._slot_save_binary = ("/bin/llama-server", 111)
+ backend._slot_launch_fingerprint = lambda: (("--rope-freq-scale", "0.5"), None, None, 1)
+ restored = []
+ backend.restore_slots_for_resume = lambda manifest: restored.append(manifest)
+
+ kw.restore_kv_resume(backend, manifest)
+ assert restored == []
+ assert not state_file.exists()
+
+
+def test_restore_skipped_when_gguf_rewritten_in_place(tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ state_file, manifest = _seed_kv_manifest(tmp_path)
+ with open(manifest["gguf"], "wb") as fh:
+ fh.write(b"different weights") # same path, new content
+ backend = _FakeBackend("unsloth/A-GGUF", hf_variant = "Q4_K_M")
+ backend._gguf_path = manifest["gguf"]
+ backend._slot_save_binary = ("/bin/llama-server", 111)
+ restored = []
+ backend.restore_slots_for_resume = lambda manifest: restored.append(manifest)
+
+ kw.restore_kv_resume(backend, manifest)
+ assert restored == []
+ assert not state_file.exists()
+
+
+def test_note_model_unloaded_purges_manifest_and_files(tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ state_file, manifest = _seed_kv_manifest(tmp_path)
+ kw._set_last_unloaded(("org/A-GGUF", "Q4_K_M"))
+ kw._set_kv_resume(manifest)
+ kw.note_model_unloaded()
+ assert kw.get_last_unloaded_model() is None
+ assert kw.take_kv_resume() is None
+ assert not state_file.exists()
+
+
+def test_note_model_loaded_purges_manifest_and_files(tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ state_file, manifest = _seed_kv_manifest(tmp_path)
+ kw._set_last_unloaded(("org/A-GGUF", "Q4_K_M"))
+ kw._set_kv_resume(manifest)
+ kw.note_model_loaded()
+ assert kw.get_last_unloaded_model() is None
+ assert kw.take_kv_resume() is None
+ assert not state_file.exists()
+
+
+def test_new_idle_save_purges_previous_manifest_files(tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ old_file, old_manifest = _seed_kv_manifest(tmp_path)
+ kw._set_kv_resume(old_manifest)
+ new_file = tmp_path / "resume-def-slot0.bin"
+ new_file.write_bytes(b"kv2")
+ kw._set_kv_resume(
+ {
+ "identity": ("unsloth/B-GGUF", None, "unsloth/B-GGUF"),
+ "dir": str(tmp_path),
+ "binary": ("/bin/llama-server", 111),
+ "slots": [{"id": 0, "filename": new_file.name, "n_saved": 7}],
+ }
+ )
+ assert not old_file.exists() # replaced manifest's files purged
+ assert new_file.exists()
+ assert kw.take_kv_resume()["slots"][0]["filename"] == new_file.name
+
+
+def test_sweep_slot_save_dir_removes_only_resume_files(monkeypatch, tmp_path):
+ from core.inference import llama_keepwarm as kw
+ from utils.paths import storage_roots
+
+ monkeypatch.setattr(storage_roots, "llama_slot_cache_root", lambda: tmp_path)
+ stale = tmp_path / "resume-old-slot0.bin"
+ stale.write_bytes(b"kv")
+ other = tmp_path / "unrelated.txt"
+ other.write_text("keep")
+ kw.sweep_slot_save_dir()
+ assert not stale.exists()
+ assert other.exists()
+
+
+def test_keep_kv_setting_roundtrip_and_default(monkeypatch):
+ import storage.studio_db as db
+
+ store = {}
+ monkeypatch.setattr(db, "upsert_app_settings", lambda m: store.update(m))
+ monkeypatch.setattr(settings, "_cached_setting", lambda k, d = None: store.get(k, d))
+
+ assert settings.get_auto_unload_keep_kv() is True # default when never stored
+ assert settings.set_openai_auto_switch(True, 60, False)[2] is False
+ assert store[settings.AUTO_UNLOAD_KEEP_KV_SETTING_KEY] is False
+ assert settings.get_auto_unload_keep_kv() is False
+ # None leaves the stored value untouched (older clients can't reset it).
+ assert settings.set_openai_auto_switch(True, 60, None)[2] is False
+ assert store[settings.AUTO_UNLOAD_KEEP_KV_SETTING_KEY] is False
+ with pytest.raises(ValueError, match = "true or false"):
+ settings.set_openai_auto_switch(True, 60, "garbage")
+
+
+def test_stale_stash_cleanup_waits_for_lifecycle_gate(monkeypatch, tmp_path):
+ # The loop's stale-stash purge must wait on the gate a mid-reload holds.
+ import time
+ from core.inference import llama_keepwarm as kw
+
+ monkeypatch.setattr(settings, "get_auto_unload_idle_seconds", lambda: 3600)
+ kw._inflight = 0
+ kw._pending = 0
+ kw._last_active = time.monotonic()
+ backend = _FakeBackend("unsloth/New-GGUF")
+ monkeypatch.setattr(inference_route, "get_llama_cpp_backend", lambda: backend)
+ state_file, manifest = _seed_kv_manifest(tmp_path)
+ kw._kv_resume = manifest
+ kw._last_unloaded_model = ("unsloth/A-GGUF", "Q4_K_M")
+
+ assert kw._lifecycle_lock.acquire(blocking = False) # simulate in-flight reload
+ try:
+ _drive_idle_loop(kw)
+ assert kw._kv_resume is manifest # purge deferred while the gate is held
+ assert state_file.exists()
+ finally:
+ kw._lifecycle_lock.release()
+ _drive_idle_loop(kw)
+ assert kw._kv_resume is None # gate freed: genuinely stale stash purged
+ assert not state_file.exists()
+
+
+def test_put_route_disabling_keep_kv_purges_saved_state(monkeypatch, tmp_path):
+ import routes.settings as settings_route
+ import storage.studio_db as db
+ from core.inference import llama_keepwarm as kw
+
+ store = {}
+ monkeypatch.setattr(db, "upsert_app_settings", lambda m: store.update(m))
+ monkeypatch.setattr(settings, "_cached_setting", lambda k, d = None: store.get(k, d))
+ state_file, manifest = _seed_kv_manifest(tmp_path)
+ monkeypatch.setattr(kw, "_kv_resume", manifest)
+
+ payload = settings_route.OpenAIAutoSwitchPayload(enabled = True, auto_unload_keep_kv = False)
+ resp = settings_route.update_openai_auto_switch(payload, "tester")
+ assert resp.auto_unload_keep_kv is False
+ assert kw._kv_resume is None
+ assert not state_file.exists()
+
+
+def test_keep_kv_only_update_leaves_env_idle_ttl_active(monkeypatch):
+ # A keep-KV-only update must not materialize the env TTL as a stored value.
+ import routes.settings as settings_route
+ import storage.studio_db as db
+
+ store = {}
+ monkeypatch.setattr(db, "upsert_app_settings", lambda m: store.update(m))
+ monkeypatch.setattr(settings, "_cached_setting", lambda k, d = None: store.get(k, d))
+ monkeypatch.setenv(settings.MODEL_IDLE_TTL_ENV_VAR, "600")
+
+ assert settings_route.OpenAIAutoSwitchPayload(enabled = False).auto_unload_idle_seconds is None
+ enabled, idle, keep_kv = settings.set_openai_auto_switch(False, None, False)
+ assert settings.AUTO_UNLOAD_IDLE_SETTING_KEY not in store # idle untouched
+ assert settings.get_auto_unload_idle_seconds() == 600 # env TTL still active
+ assert (enabled, idle, keep_kv) == (False, 600, False)
+
+
+def test_load_impl_notes_loaded_with_backend_off_loop():
+ import inspect
+ src = inspect.getsource(inference_route._load_model_impl)
+ assert "to_thread(note_model_loaded, llama_backend)" in src
+
+
+def test_restore_matches_gguf_realpath_across_naming(tmp_path):
+ from core.inference import llama_keepwarm as kw
+
+ blob = tmp_path / "blob.gguf"
+ blob.write_bytes(b"gguf")
+ link = tmp_path / "snapshot.gguf"
+ try:
+ link.symlink_to(blob)
+ except OSError:
+ pytest.skip("symlinks unsupported on this host")
+
+ backend = _FakeBackend("/hf/snapshots/d7f5", hf_variant = None)
+ backend._gguf_path = str(link) # reload resolved the symlink spelling
+ backend._slot_save_binary = ("/bin/llama-server", 111)
+ restored = []
+ backend.restore_slots_for_resume = lambda manifest: restored.append(manifest)
+ state_file, manifest = _seed_kv_manifest(
+ tmp_path, identity = ("unsloth/A-GGUF", None, "unsloth/A-GGUF"), gguf = str(blob)
+ )
+
+ kw.restore_kv_resume(backend, manifest)
+ assert len(restored) == 1 # names differ, file identical: restore ran
+ assert not state_file.exists()
+
+
def test_setter_rejects_idle_below_floor(monkeypatch):
import storage.studio_db as db
diff --git a/studio/backend/utils/openai_auto_switch_settings.py b/studio/backend/utils/openai_auto_switch_settings.py
index 462435e5d5..7007440f4c 100644
--- a/studio/backend/utils/openai_auto_switch_settings.py
+++ b/studio/backend/utils/openai_auto_switch_settings.py
@@ -30,11 +30,13 @@ from typing import Any, Optional
OPENAI_AUTO_SWITCH_SETTING_KEY = "openai_api_auto_switch_model"
AUTO_UNLOAD_IDLE_SETTING_KEY = "openai_api_auto_unload_idle_seconds"
+AUTO_UNLOAD_KEEP_KV_SETTING_KEY = "openai_api_auto_unload_keep_kv"
MODEL_OVERRIDES_SETTING_KEY = "openai_api_auto_switch_overrides"
MODEL_IDLE_TTL_ENV_VAR = "UNSLOTH_MODEL_IDLE_TTL"
DEFAULT_OPENAI_AUTO_SWITCH_ENABLED = False
DEFAULT_AUTO_UNLOAD_IDLE_SECONDS = 0
+DEFAULT_AUTO_UNLOAD_KEEP_KV = True
MIN_AUTO_UNLOAD_IDLE_SECONDS = 60
_CACHE_TTL_S = 2.0
@@ -158,29 +160,54 @@ def get_auto_unload_idle_seconds() -> int:
return env if env is not None else 0
-def set_openai_auto_switch(enabled: Any, idle_seconds: Any) -> tuple[bool, int]:
- """Set both auto-switch flags in one transaction so a settings PUT can't leave
- one key updated and the other stale. Both values are coerced before any write,
- so an invalid value raises without persisting either."""
+def get_auto_unload_keep_kv() -> bool:
+ """Whether the idle unload persists slot KV to disk for restore on reload."""
+ parsed = _coerce_bool(_cached_setting(AUTO_UNLOAD_KEEP_KV_SETTING_KEY, None))
+ return parsed if parsed is not None else DEFAULT_AUTO_UNLOAD_KEEP_KV
+
+
+def set_openai_auto_switch(
+ enabled: Any,
+ idle_seconds: Any,
+ keep_kv: Any = None,
+) -> tuple[bool, int, bool]:
+ """One-transaction write; ``None`` leaves a stored value untouched."""
parsed_enabled = _coerce_bool(enabled)
if parsed_enabled is None:
raise ValueError("OpenAI auto-switch must be true or false.")
- parsed_idle = _coerce_int(idle_seconds)
- if parsed_idle is None:
- raise ValueError("Auto-unload idle seconds must be a non-negative integer.")
- if 0 < parsed_idle < MIN_AUTO_UNLOAD_IDLE_SECONDS:
- raise ValueError(
- f"Auto-unload idle seconds must be 0 (off) or at least "
- f"{MIN_AUTO_UNLOAD_IDLE_SECONDS}."
- )
+ parsed_idle = None
+ if idle_seconds is not None:
+ parsed_idle = _coerce_int(idle_seconds)
+ if parsed_idle is None:
+ raise ValueError("Auto-unload idle seconds must be a non-negative integer.")
+ if 0 < parsed_idle < MIN_AUTO_UNLOAD_IDLE_SECONDS:
+ raise ValueError(
+ f"Auto-unload idle seconds must be 0 (off) or at least "
+ f"{MIN_AUTO_UNLOAD_IDLE_SECONDS}."
+ )
+ parsed_keep_kv = None
+ if keep_kv is not None:
+ parsed_keep_kv = _coerce_bool(keep_kv)
+ if parsed_keep_kv is None:
+ raise ValueError("Keep KV on idle unload must be true or false.")
from storage.studio_db import upsert_app_settings
- upsert_app_settings(
- {OPENAI_AUTO_SWITCH_SETTING_KEY: parsed_enabled, AUTO_UNLOAD_IDLE_SETTING_KEY: parsed_idle}
- )
+ updates: dict[str, Any] = {OPENAI_AUTO_SWITCH_SETTING_KEY: parsed_enabled}
+ if parsed_idle is not None:
+ updates[AUTO_UNLOAD_IDLE_SETTING_KEY] = parsed_idle
+ if parsed_keep_kv is not None:
+ updates[AUTO_UNLOAD_KEEP_KV_SETTING_KEY] = parsed_keep_kv
+ upsert_app_settings(updates)
_invalidate(OPENAI_AUTO_SWITCH_SETTING_KEY)
- _invalidate(AUTO_UNLOAD_IDLE_SETTING_KEY)
- return parsed_enabled, parsed_idle
+ if parsed_idle is not None:
+ _invalidate(AUTO_UNLOAD_IDLE_SETTING_KEY)
+ if parsed_keep_kv is not None:
+ _invalidate(AUTO_UNLOAD_KEEP_KV_SETTING_KEY)
+ return (
+ parsed_enabled,
+ parsed_idle if parsed_idle is not None else get_stored_auto_unload_idle_seconds(),
+ parsed_keep_kv if parsed_keep_kv is not None else get_auto_unload_keep_kv(),
+ )
def get_model_overrides() -> dict[str, dict]:
diff --git a/studio/backend/utils/paths/storage_roots.py b/studio/backend/utils/paths/storage_roots.py
index 1faa2b1281..35b8c57e9b 100644
--- a/studio/backend/utils/paths/storage_roots.py
+++ b/studio/backend/utils/paths/storage_roots.py
@@ -61,6 +61,11 @@ def cache_root() -> Path:
return studio_root() / "cache"
+def llama_slot_cache_root() -> Path:
+ """Dir llama-server saves/restores slot KV state in across idle unloads."""
+ return cache_root() / "llama-slots"
+
+
def studio_bin_root() -> Path:
"""Dir for Unsloth-managed executables (the `unsloth` shim, downloaded tools like cloudflared)."""
return studio_root() / "bin"
diff --git a/studio/frontend/src/features/settings/api/openai-auto-switch.ts b/studio/frontend/src/features/settings/api/openai-auto-switch.ts
index 80ffc084d0..47bad56eab 100644
--- a/studio/frontend/src/features/settings/api/openai-auto-switch.ts
+++ b/studio/frontend/src/features/settings/api/openai-auto-switch.ts
@@ -11,6 +11,8 @@ export type OpenAIAutoSwitchSettings = {
// True when the idle-unload loop will actually unload (e.g. enabled via the
// UNSLOTH_MODEL_IDLE_TTL env var even while the toggle is off).
idleUnloadActive: boolean;
+ // Persist the KV cache to disk on idle unload and restore it on reload.
+ autoUnloadKeepKv: boolean;
};
type ApiOpenAIAutoSwitchSettings = {
@@ -21,6 +23,8 @@ type ApiOpenAIAutoSwitchSettings = {
default_enabled: boolean;
// biome-ignore lint/style/useNamingConvention: API schema
idle_unload_active?: boolean;
+ // biome-ignore lint/style/useNamingConvention: API schema
+ auto_unload_keep_kv?: boolean;
};
let cachedSettings: OpenAIAutoSwitchSettings | null = null;
@@ -34,6 +38,7 @@ function fromApi(
autoUnloadIdleSeconds: settings.auto_unload_idle_seconds,
defaultEnabled: settings.default_enabled,
idleUnloadActive: settings.idle_unload_active ?? false,
+ autoUnloadKeepKv: settings.auto_unload_keep_kv ?? true,
};
}
@@ -66,15 +71,23 @@ export async function loadOpenAIAutoSwitchSettings() {
export async function updateOpenAIAutoSwitchSettings(
enabled: boolean,
- autoUnloadIdleSeconds: number,
+ autoUnloadIdleSeconds?: number,
+ autoUnloadKeepKv?: boolean,
): Promise {
const res = await authFetch("/api/settings/openai-auto-switch", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
enabled,
- // biome-ignore lint/style/useNamingConvention: API schema
- auto_unload_idle_seconds: autoUnloadIdleSeconds,
+ // Omitted fields keep their stored value.
+ ...(autoUnloadIdleSeconds === undefined
+ ? {}
+ : // biome-ignore lint/style/useNamingConvention: API schema
+ { auto_unload_idle_seconds: autoUnloadIdleSeconds }),
+ ...(autoUnloadKeepKv === undefined
+ ? {}
+ : // biome-ignore lint/style/useNamingConvention: API schema
+ { auto_unload_keep_kv: autoUnloadKeepKv }),
}),
});
if (!res.ok) {
diff --git a/studio/frontend/src/features/settings/components/model-auto-switch-section.tsx b/studio/frontend/src/features/settings/components/model-auto-switch-section.tsx
index 32b3e53a2c..aa6857cff5 100644
--- a/studio/frontend/src/features/settings/components/model-auto-switch-section.tsx
+++ b/studio/frontend/src/features/settings/components/model-auto-switch-section.tsx
@@ -62,13 +62,18 @@ export function ModelAutoSwitchSection() {
const persist = async (
enabled: boolean,
- idleSeconds: number,
+ idleSeconds: number | undefined,
syncDraft = true,
+ keepKv?: boolean,
) => {
setIsSaving(true);
setError(null);
try {
- const saved = await updateOpenAIAutoSwitchSettings(enabled, idleSeconds);
+ const saved = await updateOpenAIAutoSwitchSettings(
+ enabled,
+ idleSeconds,
+ keepKv,
+ );
setSettings(saved);
if (syncDraft) {
setDraftIdleSeconds(String(saved.autoUnloadIdleSeconds));
@@ -107,6 +112,11 @@ export function ModelAutoSwitchSection() {
void persist(true, idleSeconds);
};
+ const handleKeepKvToggle = (keepKv: boolean) => {
+ if (!settings) return;
+ void persist(settings.enabled, undefined, false, keepKv);
+ };
+
return (
+ {settings?.idleUnloadActive ? (
+
+
+
+ ) : null}
);
}
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index de8ac17c29..cbddc9f0c2 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -232,6 +232,9 @@ export const en = {
loadError: "Failed to load model auto-switch settings.",
saveError: "Failed to save model auto-switch settings.",
idleError: "Enter 0 to keep the model loaded, or at least 60 seconds.",
+ keepKv: "Keep chat context across idle unload",
+ keepKvDescription:
+ "Save the model's KV cache to disk before an idle unload and restore it on reload, so resumed chats skip re-reading their history. Chat context is written to disk (up to 10 GB) until it is restored or cleaned up.",
},
previewSharing: {
sectionTitle: "Preview sharing",
From 9e334d552c77de8cc4b52ff1d2b13a93891d1366 Mon Sep 17 00:00:00 2001
From: alkinun
Date: Mon, 20 Jul 2026 10:23:37 +0300
Subject: [PATCH 042/271] Fix text-only VLM CPT packing truncation (#7211)
* Fix text-only VLM CPT packing truncation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Handle streaming vision datasets in packing
* Harden multimodal packing detection
* Preserve safe packing boundaries
* Scope stream packing checks to VLMs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Narrow VLM packing detection
* Align packing mode and eval safety
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add qwen3_5/qwen3_next to PADDING_FREE_BLOCKLIST to avoid packed-sequence contamination
* Detect hybrid linear-attention models structurally instead of by name for packing guard
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Install wrapped-packing setup at the signature, not the Zoo license comment
The _unsloth_wrapped_packing / _inspect setup block was injected by matching the
exact 'All Unsloth Zoo code licensed under LGPLv3' comment line in the sourced
sft_prepare_dataset. The unsloth_zoo dependency is only lower-bounded, so a newer
Zoo that moves or drops that header made the setup a silent no-op while the
truncation and pack_dataset rewrites still emitted references to those names,
raising NameError on every SFT dataset preparation.
Anchor the setup on the function signature instead (a structural location that
always exists) and fail loudly if it cannot be found, so the helper variables are
always defined before they are referenced across Zoo versions.
Adds a regression test that patches in a Zoo source without the license header.
* [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: Etherl <61019402+Etherll@users.noreply.github.com>
Co-authored-by: danielhanchen
---
studio/backend/core/training/trainer.py | 12 +-
tests/utils/test_packing.py | 371 +++++++++++++++++++++++-
unsloth/models/rl_replacements.py | 88 ++++--
unsloth/trainer.py | 177 ++++++++++-
4 files changed, 610 insertions(+), 38 deletions(-)
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.py
index 26720865f4..8e419849cb 100644
--- a/studio/backend/core/training/trainer.py
+++ b/studio/backend/core/training/trainer.py
@@ -3425,15 +3425,19 @@ class UnslothTrainer:
logger.info(
f"CPT: using UnslothTrainer with embedding_learning_rate={embedding_lr}\n"
)
+ cpt_args = _UnslothTrainingArguments(
+ embedding_learning_rate = embedding_lr,
+ **config_args,
+ )
+ if config_args.get("packing", False):
+ cpt_args.packing_strategy = "wrapped"
+ logger.info("CPT packing strategy: wrapped\n")
trainer_kwargs = {
"model": self.model,
"tokenizer": sft_tokenizer,
"train_dataset": dataset["dataset"],
"data_collator": data_collator,
- "args": _UnslothTrainingArguments(
- embedding_learning_rate = embedding_lr,
- **config_args,
- ),
+ "args": cpt_args,
}
if eval_dataset is not None:
trainer_kwargs["eval_dataset"] = eval_dataset
diff --git a/tests/utils/test_packing.py b/tests/utils/test_packing.py
index a8557d8533..98c29d9f0f 100644
--- a/tests/utils/test_packing.py
+++ b/tests/utils/test_packing.py
@@ -14,6 +14,7 @@
# along with this program. If not, see .
from unsloth import FastLanguageModel
+import unsloth.trainer as trainer_module
from unsloth.utils import attention_dispatch as attention_dispatch_utils
from unsloth.utils.packing import (
configure_padding_free,
@@ -29,7 +30,7 @@ from unittest.mock import patch
import pytest
import torch
-from datasets import Dataset
+from datasets import Dataset, IterableDataset
from trl import SFTConfig, SFTTrainer
from trl.trainer.sft_trainer import DataCollatorForLanguageModeling
@@ -160,6 +161,374 @@ def test_configure_padding_free():
assert config.remove_unused_columns is False
+def _patch_fake_sft_trainer():
+ class FakeSFTTrainer:
+ def __init__(self, *args, **kwargs):
+ self.model = args[0] if len(args) >= 1 else kwargs["model"]
+ self.args = args[1] if len(args) >= 2 else kwargs["args"]
+ self.data_collator = args[2] if len(args) >= 3 else kwargs.get("data_collator")
+
+ trainer_module._patch_sft_trainer_auto_packing(SimpleNamespace(SFTTrainer = FakeSFTTrainer))
+ return FakeSFTTrainer
+
+
+def _vlm_model():
+ return SimpleNamespace(
+ config = SimpleNamespace(
+ architectures = ["Gemma4ForConditionalGeneration"],
+ model_type = "gemma4",
+ vision_config = SimpleNamespace(),
+ ),
+ max_seq_length = 16,
+ )
+
+
+def _text_model():
+ return SimpleNamespace(
+ config = SimpleNamespace(
+ architectures = ["LlamaForCausalLM"],
+ model_type = "llama",
+ ),
+ max_seq_length = 16,
+ )
+
+
+class _CharacterTokenizer:
+ bos_token = None
+ eos_token = None
+ chat_template = None
+
+ def __call__(self, texts, **kwargs):
+ is_batched = isinstance(texts, list)
+ if not is_batched:
+ texts = [texts]
+ input_ids = [[ord(char) for char in text] for text in texts]
+ if kwargs.get("truncation") and kwargs.get("max_length") is not None:
+ input_ids = [ids[: kwargs["max_length"]] for ids in input_ids]
+ return {"input_ids": input_ids if is_batched else input_ids[0]}
+
+
+def test_vlm_text_dataset_allows_explicit_packing():
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+
+ trainer = fake_trainer(
+ model = _vlm_model(),
+ args = config,
+ processing_class = object(),
+ train_dataset = Dataset.from_dict({"text": ["text-only CPT sample"]}),
+ )
+
+ assert config.packing is True
+ assert config.padding_free is True
+ assert trainer.model._unsloth_allow_packed_overlength is True
+
+
+def test_vlm_without_processing_class_still_disables_packing():
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+
+ fake_trainer(
+ _vlm_model(),
+ config,
+ None,
+ Dataset.from_dict({"text": ["text-only sample"]}),
+ )
+
+ assert config.packing is False
+ assert config.padding_free is False
+
+
+@pytest.mark.parametrize(
+ ("model_type", "architecture"),
+ (
+ ("t5", "T5ForConditionalGeneration"),
+ ("bart", "BartForConditionalGeneration"),
+ ("whisper", "WhisperForConditionalGeneration"),
+ ("csm", "CsmForConditionalGeneration"),
+ ),
+)
+def test_nonvision_conditional_generation_keeps_packing(model_type, architecture):
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ model = SimpleNamespace(
+ config = SimpleNamespace(model_type = model_type, architectures = [architecture]),
+ max_seq_length = 16,
+ )
+
+ trainer = fake_trainer(
+ model,
+ config,
+ None,
+ Dataset.from_dict({"text": ["text-only sample"]}),
+ )
+
+ assert config.packing is True
+ assert config.padding_free is True
+ assert trainer.model._unsloth_allow_packed_overlength is True
+
+
+def test_vlm_vision_dataset_still_disables_packing():
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+
+ fake_trainer(
+ _vlm_model(),
+ config,
+ None,
+ Dataset.from_dict({"images": [None], "text": ["multimodal sample"]}),
+ None,
+ object(),
+ )
+
+ assert config.packing is False
+ assert config.padding_free is False
+
+
+@pytest.mark.parametrize(
+ "vision_column",
+ ("pixel_values", "pixel_attention_mask", "image_grid_thw"),
+)
+def test_vlm_preprocessed_vision_dataset_disables_packing(vision_column):
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+
+ fake_trainer(
+ model = _vlm_model(),
+ args = config,
+ processing_class = object(),
+ train_dataset = Dataset.from_dict({"input_ids": [[1]], vision_column: [None]}),
+ )
+
+ assert config.packing is False
+ assert config.padding_free is False
+
+
+@pytest.mark.parametrize("dict_eval", (False, True))
+def test_vlm_vision_eval_dataset_disables_packing(dict_eval):
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ eval_dataset = Dataset.from_dict({"input_ids": [[1]], "pixel_values": [None]})
+ if dict_eval:
+ eval_dataset = {"vision": eval_dataset}
+
+ fake_trainer(
+ model = _vlm_model(),
+ args = config,
+ processing_class = object(),
+ train_dataset = Dataset.from_dict({"text": ["text-only training sample"]}),
+ eval_dataset = eval_dataset,
+ )
+
+ assert config.packing is False
+ assert config.padding_free is False
+
+
+def test_vlm_streaming_vision_dataset_without_metadata_disables_packing():
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ dataset = IterableDataset.from_generator(
+ lambda: iter([{"images": [None], "text": "multimodal sample"}])
+ )
+ assert dataset.column_names is None
+
+ fake_trainer(
+ model = _vlm_model(),
+ args = config,
+ processing_class = object(),
+ train_dataset = dataset,
+ )
+
+ assert config.packing is False
+ assert config.padding_free is False
+ assert next(iter(dataset))["text"] == "multimodal sample"
+
+
+@pytest.mark.parametrize("data_collator", (None, object()))
+def test_stateful_stream_is_not_consumed_during_detection(data_collator):
+ class StatefulDataset:
+ def __init__(self):
+ self.rows = iter([{"text": "first"}, {"text": "second"}])
+
+ def __iter__(self):
+ return (row for row in self.rows)
+
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ dataset = StatefulDataset()
+
+ fake_trainer(
+ model = _vlm_model(),
+ args = config,
+ processing_class = object(),
+ data_collator = data_collator,
+ train_dataset = dataset,
+ )
+
+ assert config.packing is False
+ assert config.padding_free is False
+ assert next(iter(dataset))["text"] == "first"
+
+
+def test_text_model_stream_without_metadata_keeps_packing():
+ class StatefulDataset:
+ def __init__(self):
+ self.rows = iter([{"text": "first"}, {"text": "second"}])
+
+ def __iter__(self):
+ return (row for row in self.rows)
+
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ dataset = StatefulDataset()
+
+ trainer = fake_trainer(
+ model = _text_model(),
+ args = config,
+ processing_class = object(),
+ train_dataset = dataset,
+ )
+
+ assert config.packing is True
+ assert config.padding_free is True
+ assert trainer.model._unsloth_allow_packed_overlength is True
+ assert next(iter(dataset))["text"] == "first"
+
+
+def test_bfd_packing_truncates_before_packing(monkeypatch):
+ args = SimpleNamespace(
+ dataset_num_proc = 1,
+ dataset_text_field = "text",
+ max_length = 4,
+ packing_strategy = "bfd",
+ )
+ trainer = SimpleNamespace(model = None)
+ dataset = Dataset.from_dict({"prompt": ["abc"], "completion": ["defghij"]})
+ prepare_globals = SFTTrainer._prepare_dataset.__globals__
+
+ def passthrough_pack_dataset(dataset, seq_length, strategy, map_kwargs):
+ return dataset
+
+ monkeypatch.setitem(prepare_globals, "pack_dataset", passthrough_pack_dataset)
+ packed = SFTTrainer._prepare_dataset(
+ trainer,
+ dataset,
+ _CharacterTokenizer(),
+ args,
+ True,
+ None,
+ "train",
+ )
+
+ assert len(packed["input_ids"][0]) == args.max_length
+
+
+def test_wrapped_strategy_without_packing_still_truncates():
+ args = SimpleNamespace(
+ dataset_num_proc = 1,
+ dataset_text_field = "text",
+ max_length = 4,
+ packing_strategy = "wrapped",
+ )
+ trainer = SimpleNamespace(model = None)
+ dataset = Dataset.from_dict({"text": ["abcdefghi"]})
+
+ prepared = SFTTrainer._prepare_dataset(
+ trainer,
+ dataset,
+ _CharacterTokenizer(),
+ args,
+ False,
+ None,
+ "train",
+ )
+
+ assert len(prepared["input_ids"][0]) == args.max_length
+
+
+@pytest.mark.parametrize("legacy_api", (False, True))
+def test_wrapped_packing_preserves_overlength_tokens(monkeypatch, legacy_api):
+ args_kwargs = {
+ "dataset_num_proc": 1,
+ "dataset_text_field": "text",
+ "max_length": 4,
+ }
+ if not legacy_api:
+ args_kwargs["packing_strategy"] = "wrapped"
+ args = SimpleNamespace(**args_kwargs)
+ trainer = SimpleNamespace(model = None)
+ dataset = Dataset.from_dict({"text": ["abcdefghi"]})
+ prepare_globals = SFTTrainer._prepare_dataset.__globals__
+ pack_dataset = prepare_globals["pack_dataset"]
+
+ def legacy_pack_dataset(
+ dataset,
+ seq_length,
+ map_kwargs = None,
+ ):
+ return pack_dataset(dataset, seq_length, "wrapped", map_kwargs)
+
+ if legacy_api:
+ monkeypatch.setitem(prepare_globals, "pack_dataset", legacy_pack_dataset)
+
+ packed = SFTTrainer._prepare_dataset(
+ trainer,
+ dataset,
+ _CharacterTokenizer(),
+ args,
+ True,
+ None,
+ "train",
+ )
+
+ packed_ids = packed["input_ids"]
+ assert sum(len(input_ids) for input_ids in packed_ids) == 9
+ assert all(len(input_ids) <= args.max_length for input_ids in packed_ids)
+
+
+# Named to match the unsloth_zoo helper: sft_trainer_prepare_dataset sources it by
+# name and renames "def sft_prepare_dataset" -> "def _prepare_dataset". This fixture
+# deliberately omits the "All Unsloth Zoo code licensed under LGPLv3" header to emulate
+# a newer, compatible Zoo whose header moved (the dependency is only lower-bounded).
+def sft_prepare_dataset(
+ self, dataset, processing_class, args, packing, formatting_func, dataset_text_field
+):
+ do_truncation = True
+ # Mirror the Zoo call so the "truncation = do_truncation," injection anchor
+ # survives formatting (a bare tuple assignment gets rewritten to a paren form).
+ dataset = processing_class(
+ dataset,
+ truncation = do_truncation,
+ )
+ return dataset
+
+
+def test_wrapped_packing_setup_survives_missing_zoo_header(monkeypatch):
+ # Regression: the wrapped-packing setup used to anchor on the Zoo license comment,
+ # so a header change made it a no-op while the truncation reference still landed,
+ # NameError-ing every SFT dataset preparation. It must now install via the
+ # signature and always precede the reference.
+ import ast
+ import textwrap
+ import unsloth.models.rl_replacements as rlr
+
+ monkeypatch.setitem(rlr.RL_REPLACEMENTS, "sft_prepare_dataset", sft_prepare_dataset)
+
+ source = (
+ "def _prepare_dataset(self, dataset, processing_class, args, packing, "
+ "formatting_func, dataset_text_field):\n return dataset\n"
+ )
+ patched = rlr.sft_trainer_prepare_dataset("_prepare_dataset", source)
+
+ assert "_unsloth_wrapped_packing = packing" in patched
+ assert "import inspect as _inspect" in patched
+ assert "not _unsloth_wrapped_packing" in patched
+ assert patched.index("_unsloth_wrapped_packing = packing") < patched.index(
+ "truncation = do_truncation and not _unsloth_wrapped_packing"
+ )
+ ast.parse(textwrap.dedent(patched))
+
+
class _DummyChild(torch.nn.Module):
def __init__(self):
super().__init__()
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index ffb845b04f..b0709f7376 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -276,17 +276,13 @@ def dpo_trainer_vision_signature_columns(function_name, function):
_extra_columns = "".join(f' "{_k}",\n' for _k in _DPO_VISION_KEYS)
new_function = function.replace(
' "image_sizes",\n "token_type_ids",\n',
- f' "image_sizes",\n'
- f"{_extra_columns}"
- f' "token_type_ids",\n',
+ f' "image_sizes",\n{_extra_columns} "token_type_ids",\n',
)
if new_function != function:
return new_function
return function.replace(
' "image_sizes",\n "ref_chosen_logps",\n',
- f' "image_sizes",\n'
- f"{_extra_columns}"
- f' "ref_chosen_logps",\n',
+ f' "image_sizes",\n{_extra_columns} "ref_chosen_logps",\n',
)
@@ -458,6 +454,60 @@ def sft_trainer_prepare_dataset(function_name, function):
if matched:
# Use fast version!
function = inspect.getsource(fast_sft_prepare_dataset)
+ # why: install the wrapped-packing setup (and the `_inspect` import the
+ # truncation / pack_dataset rewrites below depend on) at the function
+ # signature, a structural anchor that always exists, rather than the
+ # unsloth_zoo license-comment line. That header is only lower-bounded, so a
+ # newer Zoo may move or drop it; anchoring there let the setup silently
+ # no-op while the references still landed, NameError-ing every SFT dataset
+ # preparation. Fail loudly if even the signature cannot be located.
+ _wrapped_packing_setup = (
+ " import inspect as _inspect\n"
+ " try:\n"
+ ' _unsloth_pack_has_strategy = "strategy" in _inspect.signature(pack_dataset).parameters\n'
+ " except Exception:\n"
+ " _unsloth_pack_has_strategy = True\n"
+ " _unsloth_wrapped_packing = packing and (\n"
+ ' getattr(args, "packing_strategy", None) == "wrapped"\n'
+ " or not _unsloth_pack_has_strategy\n"
+ " )\n"
+ )
+ function, _n_setup = re.subn(
+ r"(def sft_prepare_dataset\s*\(.*?\)\s*(?:->[^:\n]*)?:[ \t]*\n)",
+ lambda match: match.group(1) + _wrapped_packing_setup,
+ function,
+ count = 1,
+ flags = re.DOTALL,
+ )
+ if _n_setup != 1:
+ raise RuntimeError(
+ "Unsloth: failed to install wrapped-packing support into "
+ "sft_prepare_dataset (signature not found); please file a bug report."
+ )
+ function = function.replace(
+ "truncation = do_truncation,",
+ "truncation = do_truncation and not _unsloth_wrapped_packing,",
+ )
+ function = function.replace(
+ "if do_truncation and max_seq_length > 0:",
+ "if do_truncation and not _unsloth_wrapped_packing and max_seq_length > 0:",
+ )
+ function = function.replace(
+ """dataset = pack_dataset(
+ dataset.select_columns(used_column_names),
+ max_seq_length,
+ getattr(args, "packing_strategy", "bfd"),
+ map_kwargs,
+ )""",
+ """_pack_kwargs = {"map_kwargs": map_kwargs}
+ if "strategy" in _inspect.signature(pack_dataset).parameters:
+ _pack_kwargs["strategy"] = getattr(args, "packing_strategy", "bfd")
+ dataset = pack_dataset(
+ dataset.select_columns(used_column_names),
+ max_seq_length,
+ **_pack_kwargs,
+ )""",
+ )
function = function.split("\n")
function = "\n".join(" " * 4 + x for x in function)
function = function.replace("def sft_prepare_dataset", "def _prepare_dataset")
@@ -2120,19 +2170,21 @@ def grpo_trainer_compute_loss(function_name, function):
logits_to_keep,
batch_size = None,
compute_entropy = False,
- compute_efficient = False: self._get_per_token_logps(
- model, input_ids, attention_mask, logits_to_keep, compute_efficient
+ compute_efficient = False: (
+ self._get_per_token_logps(
+ model, input_ids, attention_mask, logits_to_keep, compute_efficient
+ )
+ if hasattr(self, "_get_per_token_logps")
+ else self._get_per_token_logps_and_entropies(
+ model,
+ input_ids,
+ attention_mask,
+ logits_to_keep,
+ batch_size,
+ compute_entropy,
+ compute_efficient,
+ )[0]
)
- if hasattr(self, "_get_per_token_logps")
- else self._get_per_token_logps_and_entropies(
- model,
- input_ids,
- attention_mask,
- logits_to_keep,
- batch_size,
- compute_entropy,
- compute_efficient,
- )[0]
) # logps
per_token_logps = get_logps_func(
diff --git a/unsloth/trainer.py b/unsloth/trainer.py
index 83cb1758f0..61d41aad21 100644
--- a/unsloth/trainer.py
+++ b/unsloth/trainer.py
@@ -100,6 +100,10 @@ PADDING_FREE_BLOCKLIST = {
"gemma2", # - gemma2: Uses slow_attention_softcapping which has torch.compile issues
"gpt_oss", # - gpt_oss: Uses Flex Attention which doesn't handle padding_free correctly
}
+# Hybrid linear-attention / state-space models (Qwen3.5, Qwen3-Next, ...) carry a
+# recurrent gated-delta state plus a causal conv1d. Sample packing / padding-free
+# flattens the batch, so those ops leak state across sequence boundaries. Detected
+# structurally by _is_hybrid_linear_attention_model rather than by model name.
def _should_pack(config) -> bool:
@@ -137,6 +141,132 @@ def _should_skip_auto_packing_error(exc: Exception) -> bool:
return any(msg in message for msg in _AUTO_PACK_SKIP_MESSAGES)
+_VISION_DATASET_KEYS = frozenset(
+ {
+ "image",
+ "images",
+ "image_grid_thw",
+ "image_position_ids",
+ "image_sizes",
+ "mm_token_type_ids",
+ "pixel_attention_mask",
+ "pixel_position_ids",
+ "pixel_values",
+ "pixel_values_videos",
+ "video",
+ "videos",
+ "video_grid_thw",
+ }
+)
+
+
+def _is_vlm_config(config, model_types = ()) -> bool:
+ if any(
+ hasattr(config, attr)
+ for attr in ("vision_config", "img_processor", "image_token_index", "projector_config")
+ ):
+ return True
+
+ architectures = getattr(config, "architectures", None) or ()
+ try:
+ from transformers.models.auto import modeling_auto
+
+ mappings = (
+ getattr(modeling_auto, "MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES", {}) or {},
+ getattr(modeling_auto, "MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES", {}) or {},
+ )
+ registry_types = set().union(*(mapping.keys() for mapping in mappings))
+ registry_classes = set().union(*(mapping.values() for mapping in mappings))
+ config_types = set(model_types or ())
+ model_type = getattr(config, "model_type", None)
+ if model_type is not None:
+ config_types.add(model_type)
+ if not config_types.isdisjoint(registry_types) or any(
+ architecture in registry_classes for architecture in architectures
+ ):
+ return True
+ except Exception:
+ pass
+ return any(
+ isinstance(architecture, str) and architecture.endswith("ForVisionText2Text")
+ for architecture in architectures
+ )
+
+
+def _is_vision_dataset(dataset, *, unknown_is_vision = False) -> bool:
+ if dataset is None:
+ return False
+ column_names = getattr(dataset, "column_names", None)
+ if column_names is not None:
+ return not _VISION_DATASET_KEYS.isdisjoint(column_names)
+ # Unknown-schema streams cannot be safely probed without potentially dropping a sample.
+ return unknown_is_vision
+
+
+def _is_vision_eval_dataset(dataset, *, unknown_is_vision = False) -> bool:
+ if isinstance(dataset, dict):
+ return any(
+ _is_vision_dataset(split, unknown_is_vision = unknown_is_vision)
+ for split in dataset.values()
+ )
+ return _is_vision_dataset(dataset, unknown_is_vision = unknown_is_vision)
+
+
+_HYBRID_CONFIG_MARKERS = (
+ "linear_conv_kernel_dim",
+ "linear_key_head_dim",
+ "linear_value_head_dim",
+ "full_attention_interval",
+)
+
+
+def _is_hybrid_linear_attention_model(model) -> bool:
+ """Detect models mixing linear-attention / state-space mixers (gated-delta,
+ Mamba-style) with a causal conv1d, e.g. Qwen3.5 / Qwen3-Next. Packing and
+ padding-free flatten the batch, and those recurrent + conv ops leak state
+ across sequence boundaries, so they must not be packed. Uses composite
+ structural evidence rather than a model-name match."""
+ if model is None:
+ return False
+
+ # Config-level: explicit hybrid layer schedule or linear-attn markers.
+ for config in (
+ getattr(model, "config", None),
+ getattr(getattr(model, "config", None), "text_config", None),
+ ):
+ if config is None:
+ continue
+ layer_types = getattr(config, "layer_types", None)
+ if isinstance(layer_types, (list, tuple)) and any(
+ isinstance(t, str) and "linear_attention" in t for t in layer_types
+ ):
+ return True
+ if any(hasattr(config, marker) for marker in _HYBRID_CONFIG_MARKERS):
+ return True
+
+ # Module-level: a mixer carrying a recurrent gated-delta op plus a conv1d.
+ named_modules = getattr(model, "named_modules", None)
+ if named_modules is None:
+ return False
+ seen = set()
+ for _, module in named_modules():
+ if id(module) in seen:
+ continue
+ seen.add(id(module))
+ cls = type(module).__name__
+ if not (
+ cls.endswith("GatedDeltaNet") or "LinearAttention" in cls or cls.endswith("Mamba2Mixer")
+ ):
+ continue
+ has_recurrent = any(
+ hasattr(module, attr)
+ for attr in ("chunk_gated_delta_rule", "recurrent_gated_delta_rule", "A_log")
+ )
+ if has_recurrent and hasattr(module, "conv1d"):
+ return True
+ return False
+
+
# Unsloth gradient accumulation fix:
from transformers import __version__ as transformers_version, ProcessorMixin
@@ -498,30 +628,43 @@ def _patch_sft_trainer_auto_packing(trl_module):
else:
config_arg = kwargs.get("args")
- model = kwargs.get("model")
- is_unsupported_model = False
+ model = args[0] if len(args) >= 1 else kwargs.get("model")
is_vlm = False
+ is_unsupported_model = False
+ is_hybrid = False
if model is not None:
model_config = getattr(model, "config", None)
if model_config is not None:
model_types = get_transformers_model_type(model_config)
is_unsupported_model = any(x in PADDING_FREE_BLOCKLIST for x in model_types)
+ is_vlm = _is_vlm_config(model_config, model_types)
+ is_hybrid = _is_hybrid_linear_attention_model(model)
- architectures = getattr(model_config, "architectures", None)
- if architectures is None:
- architectures = []
- is_vlm = any(x.endswith("ForConditionalGeneration") for x in architectures)
- is_vlm = is_vlm or hasattr(model_config, "vision_config")
-
- processing_class = kwargs.get("processing_class") or kwargs.get("tokenizer")
- data_collator = kwargs.get("data_collator")
+ processing_class = (
+ args[5] if len(args) >= 6 else kwargs.get("processing_class") or kwargs.get("tokenizer")
+ )
+ data_collator = args[2] if len(args) >= 3 else kwargs.get("data_collator")
+ train_dataset = args[3] if len(args) >= 4 else kwargs.get("train_dataset")
+ eval_dataset = args[4] if len(args) >= 5 else kwargs.get("eval_dataset")
+ is_processor = isinstance(processing_class, ProcessorMixin)
+ is_auto_processor_vlm = is_vlm and processing_class is None
+ is_vision_dataset = (
+ data_collator is None
+ and not is_processor
+ and (
+ _is_vision_dataset(train_dataset, unknown_is_vision = is_vlm)
+ or _is_vision_eval_dataset(eval_dataset, unknown_is_vision = is_vlm)
+ )
+ )
# Disable padding-free for VLMs / custom collators / blocklisted models
blocked = (
(data_collator is not None)
- or isinstance(processing_class, ProcessorMixin)
- or is_vlm
+ or is_processor
+ or is_auto_processor_vlm
+ or is_vision_dataset
or is_unsupported_model
+ or is_hybrid
or (
os.environ.get("UNSLOTH_RETURN_LOGITS", "0") == "1"
) # Disable padding free on forced logits
@@ -535,10 +678,14 @@ def _patch_sft_trainer_auto_packing(trl_module):
if blocked and requested_pack:
reason = "custom data collator"
- if data_collator is None and isinstance(processing_class, ProcessorMixin):
+ if data_collator is None and is_processor:
reason = "processor-based model"
- elif is_vlm:
- reason = "vision-language model"
+ elif is_auto_processor_vlm:
+ reason = "vision-language model with auto processor"
+ elif is_vision_dataset:
+ reason = "vision dataset"
+ elif is_hybrid:
+ reason = "hybrid linear-attention model"
elif is_unsupported_model:
reason = f"unsupported model type(s): {', '.join(model_types)}"
message = f"Unsloth: Sample packing skipped ({reason} detected)."
From cf912cbd881190f411147b8c93294408efe5f90c Mon Sep 17 00:00:00 2001
From: Naitik Pal
Date: Mon, 20 Jul 2026 13:03:56 +0530
Subject: [PATCH 043/271] feat(studio): add UNSLOTH_LLAMA_CPP_BACKEND env var
to force CPU fallback #7213 (#7228)
* test(studio): add e2e test for cpu-fallback overriding vulkan
* feat(studio): add UNSLOTH_LLAMA_CPP_BACKEND env var
* feat(studio): add UNSLOTH_LLAMA_CPP_BACKEND env var
* Preserve UNSLOTH_LLAMA_CPP_BACKEND=cpu across llama.cpp updates for PR #7228
The in-app updater rebuilt the installer command without --cpu-fallback and
only re-asserted Vulkan, so accepting a llama.cpp update after forcing CPU on
an Intel iGPU host re-ran host detection and routed back to the crashing Vulkan
bundle (#7213). Record install_kind in the prebuilt marker and re-assert
--cpu-fallback on update when the installed bundle is CPU.
Also make setup.sh's UNSLOTH_LLAMA_CPP_BACKEND check case-insensitive to match
setup.ps1, and add tests for the updater CPU preservation and the setup.sh flag
plumbing.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Trim and validate UNSLOTH_LLAMA_CPP_BACKEND, warn on unknown values for PR #7228
Trim surrounding whitespace and lowercase the value in both setup.sh and
setup.ps1, so values like ' cpu ' or 'CPU' still force the CPU-only prebuilt.
An unrecognized value (e.g. 'gpu') now prints a warning instead of silently
falling back to auto. Extend test_setup_llama_cpp_backend.py to cover both
scripts, including trimmed, empty and unknown values.
* Preserve arm64 CPU installs on update and honor CPU override in Windows prune for PR #7228
The update-path CPU preservation only matched install_kind ending in -cpu, so
arm64 CPU bundles (linux-arm64, windows-arm64) were re-routed to a GPU or source
build on update. Match the full set of CPU-only kinds instead.
Persisting install_kind also activated the previously inert Windows
mismatch-prune in setup.ps1: on a GPU host with UNSLOTH_LLAMA_CPP_BACKEND=cpu it
saw the windows-cpu marker as mismatched and deleted it every rerun. Normalize
the override once and make CPU expected so a deliberate CPU install is kept.
Extend the tests to cover both.
* Document legacy llama.cpp markers keep heal-to-GPU on update for PR #7228
Legacy prebuilt markers written before install_kind was persisted intentionally
do not force --cpu-fallback on update: the in-app updater lets them re-resolve
(heal to a GPU bundle) per the existing behavior from #6097, and only markers
that explicitly record a CPU install_kind are pinned to CPU. Add a comment and a
regression case documenting the boundary.
* Tighten llama.cpp CPU-fallback comments for PR #7228
* Fix Windows install-prune to keep valid Intel/fallback bundles for PR #7228
Persisting install_kind activated the setup.ps1 mismatch-prune, whose
expectedKinds was incomplete: the non-NVIDIA/non-AMD branch omitted
windows-vulkan (the Intel auto-route) and the GPU branches omitted the
windows-cpu/windows-arm64 fallback the installer uses when a GPU prebuilt is
missing. That made every setup rerun delete and re-download a valid Intel Vulkan
(or CPU-fallback) install. List all kinds the installer can produce per host so
only a bundle the host cannot run is pruned. Cover the full matrix in tests.
* Persist force_cpu marker flag so only forced CPU installs re-assert on update for PR #7228
* Add --force-cpu for deliberate CPU installs and warn on macOS for PR #7228
* Record force_cpu when reusing a matching CPU bundle for PR #7228
* Accept force_cpu keyword in installer test validator fakes for PR #7228
---------
Co-authored-by: danielhanchen
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han
---
.../tests/test_install_resolve_prebuilt.py | 95 +++++++++++
studio/backend/tests/test_llama_cpp_update.py | 58 ++++++-
.../tests/test_setup_llama_cpp_backend.py | 154 ++++++++++++++++++
studio/backend/utils/llama_cpp_update.py | 12 +-
studio/install_llama_prebuilt.py | 69 +++++++-
studio/setup.ps1 | 13 ++
studio/setup.sh | 20 +++
.../test_install_llama_prebuilt_logic.py | 4 +
8 files changed, 410 insertions(+), 15 deletions(-)
create mode 100644 studio/backend/tests/test_setup_llama_cpp_backend.py
diff --git a/studio/backend/tests/test_install_resolve_prebuilt.py b/studio/backend/tests/test_install_resolve_prebuilt.py
index e97ca47717..3ebad861ad 100644
--- a/studio/backend/tests/test_install_resolve_prebuilt.py
+++ b/studio/backend/tests/test_install_resolve_prebuilt.py
@@ -445,6 +445,101 @@ def test_route_to_vulkan_prebuilt_cpu_fallback_wins():
assert routed is host
+@pytest.mark.parametrize("cpu_flag", ["--cpu-fallback", "--force-cpu"])
+def test_resolve_prebuilt_cpu_fallback_overrides_intel_vulkan(monkeypatch, capsys, cpu_flag):
+ """Either CPU flag via CLI must suppress Vulkan even on an Intel GPU host: both
+ drop GPU detection (--force-cpu additionally persists, on the install path)."""
+ monkeypatch.setattr(
+ ilp,
+ "detect_host",
+ lambda: _host(is_linux = True, is_x86_64 = True, has_intel_gpu = True),
+ )
+ seen = {}
+
+ def _resolver(tag, host, repo, published_release_tag):
+ seen["host"] = host
+ seen["repo"] = repo
+ raise ilp.PrebuiltFallback("no asset")
+
+ monkeypatch.setattr(ilp, "resolve_simple_install_release_plans", _resolver)
+ monkeypatch.setattr(
+ sys,
+ "argv",
+ [
+ "install_llama_prebuilt.py",
+ "--resolve-prebuilt",
+ "latest",
+ cpu_flag,
+ "--output-format",
+ "json",
+ ],
+ )
+ assert ilp.main() == ilp.EXIT_SUCCESS
+ # The CPU flag must suppress Intel GPU, route to fork (not upstream Vulkan)
+ assert seen["host"].has_intel_gpu is False
+ assert seen["repo"] == FORK
+
+
+@pytest.mark.parametrize(
+ "flags, expect_force, expect_persist",
+ [
+ ([], False, False),
+ # Automatic/transient last resort (arm64 GPU-build recovery): drops GPU but
+ # does NOT persist, so a later update heals to a GPU bundle (#6097).
+ (["--cpu-fallback"], True, False),
+ # Deliberate CPU-only (UNSLOTH_LLAMA_CPP_BACKEND=cpu): drops GPU AND persists so
+ # the updater re-asserts it and never revives the Intel iGPU crash (#7213).
+ (["--force-cpu"], True, True),
+ (["--cpu-fallback", "--force-cpu"], True, True),
+ ],
+)
+def test_cli_cpu_flags_thread_force_and_persist(
+ monkeypatch, tmp_path, flags, expect_force, expect_persist
+):
+ captured = {}
+ monkeypatch.setattr(ilp, "install_prebuilt", lambda **kw: captured.update(kw))
+ monkeypatch.setattr(
+ sys,
+ "argv",
+ ["install_llama_prebuilt.py", "--install-dir", str(tmp_path / "llama.cpp"), *flags],
+ )
+ assert ilp.main() == ilp.EXIT_SUCCESS
+ assert captured["force_cpu"] is expect_force
+ assert captured["persist_force_cpu"] is expect_persist
+
+
+@pytest.mark.parametrize(
+ "existing, requested, expected",
+ [
+ # A deliberate --force-cpu on top of a naturally-installed CPU bundle (same
+ # asset, install skipped) must still flip the marker to true (#7213).
+ (False, True, True),
+ (None, True, True),
+ # No spurious writes when already in sync, and a released force syncs down.
+ (True, True, True),
+ (False, False, False),
+ (True, False, False),
+ ],
+)
+def test_sync_marker_force_cpu(tmp_path, existing, requested, expected):
+ marker = {"tag": "b9585", "asset": "llama-b9585-bin-ubuntu-x64.tar.gz"}
+ if existing is not None:
+ marker["force_cpu"] = existing
+ marker_path = tmp_path / "UNSLOTH_PREBUILT_INFO.json"
+ marker_path.write_text(json.dumps(marker))
+ ilp.sync_marker_force_cpu(tmp_path, requested)
+ written = json.loads(marker_path.read_text())
+ assert written["force_cpu"] is expected
+ # Unrelated fields are preserved.
+ assert written["asset"] == "llama-b9585-bin-ubuntu-x64.tar.gz"
+
+
+def test_sync_marker_force_cpu_missing_marker_is_noop(tmp_path):
+ # No marker (or unreadable) must not crash the reuse path.
+ ilp.sync_marker_force_cpu(tmp_path, True)
+ assert not (tmp_path / "UNSLOTH_PREBUILT_INFO.json").exists()
+
+
def test_route_to_vulkan_prebuilt_hidden_nvidia_not_rerouted():
# A mixed NVIDIA+Intel host that hid NVIDIA (CUDA_VISIBLE_DEVICES=""/-1):
# physical NVIDIA present but not usable. Must NOT auto-route to Vulkan, or
diff --git a/studio/backend/tests/test_llama_cpp_update.py b/studio/backend/tests/test_llama_cpp_update.py
index 83ea07a066..f12384231f 100644
--- a/studio/backend/tests/test_llama_cpp_update.py
+++ b/studio/backend/tests/test_llama_cpp_update.py
@@ -83,6 +83,7 @@ def _write_install(
repo: str = "unslothai/llama.cpp",
asset: str | None = None,
release_tag: str | None = None,
+ force_cpu: bool | None = None,
) -> str:
"""Create a fake prebuilt install and return the llama-server path."""
bin_dir = dir_ / "build" / "bin"
@@ -99,6 +100,8 @@ def _write_install(
}
if asset is not None:
marker["asset"] = asset
+ if force_cpu is not None:
+ marker["force_cpu"] = force_cpu
(dir_ / MARKER).write_text(json.dumps(marker))
return str(binary)
@@ -493,6 +496,47 @@ def test_start_update_preserves_vulkan_via_env(monkeypatch, tmp_path):
assert popen_kwargs["env"]["UNSLOTH_FORCE_VULKAN"] == "1"
+@pytest.mark.parametrize(
+ "force_cpu, expect_flag",
+ [
+ # A deliberate CPU install (marker force_cpu=True) re-asserts --force-cpu on
+ # update so detect_host on a GPU host cannot re-route and revive the crash
+ # (#7213); --force-cpu also re-persists the flag for the next update.
+ (True, True),
+ # A transient fallback (or a legacy marker without the flag) stays free to
+ # heal to a GPU bundle (#6097).
+ (False, False),
+ (None, False),
+ ],
+)
+def test_start_update_cpu_fallback_preserved_by_flag(monkeypatch, tmp_path, force_cpu, expect_flag):
+ asset = "llama-b9493-bin-ubuntu-x64.tar.gz"
+ install_dir = tmp_path / "llama.cpp"
+ binary = _write_install(install_dir, "b9493", asset = asset, force_cpu = force_cpu)
+ monkeypatch.setattr(upd, "_find_binary", lambda: binary)
+ monkeypatch.setattr(upd, "_installer_script", lambda: tmp_path / "install_llama_prebuilt.py")
+ monkeypatch.setattr(freshness, "_fetch_latest_release_tag", lambda repo, timeout = 5.0: "b9518")
+
+ captured: dict = {}
+
+ def _on_start(cmd):
+ captured["cmd"] = cmd
+ _write_install(install_dir, "b9518", asset = asset, force_cpu = force_cpu)
+
+ _patch_installer_popen(monkeypatch, lines = ["installed\n"], on_start = _on_start)
+
+ assert upd.start_update()["started"] is True
+ deadline = time.time() + 10
+ while time.time() < deadline:
+ job = upd.get_update_status()["job"]
+ if job["state"] in ("success", "error"):
+ break
+ time.sleep(0.05)
+ assert job["state"] == "success", job
+ assert ("--force-cpu" in captured["cmd"]) is expect_flag
+ assert "--cpu-fallback" not in captured["cmd"]
+
+
def test_start_update_reports_full_release_tag(monkeypatch, tmp_path):
install_dir = tmp_path / "llama.cpp"
binary = _write_install(install_dir, "b9595")
@@ -676,7 +720,7 @@ def test_install_cmd_rocm_marker_forwards_gfx(monkeypatch, tmp_path):
assert "--rocm-gfx" in cmd
assert cmd[cmd.index("--rocm-gfx") + 1] == "gfx110x"
assert "--has-rocm" not in cmd
- assert "--cpu-fallback" not in cmd
+ assert "--force-cpu" not in cmd
assert "--simple-policy" not in cmd
assert "--published-repo" in cmd and "unslothai/llama.cpp" in cmd
@@ -690,17 +734,17 @@ def test_install_cmd_fork_rocm_marker_forwards_has_rocm(monkeypatch, tmp_path):
def test_install_cmd_ggml_cpu_marker_has_no_cpu_fallback(monkeypatch, tmp_path):
- # Legacy CPU installs recorded a ggml-org marker (new installs use the fork).
- # Re-running into the same install-dir/repo reproduces the same CPU bundle;
- # --cpu-fallback (which force-drops GPU detection) is reserved for setup.sh's
- # arm64 rescue and must not appear here.
+ # Legacy CPU installs recorded a ggml-org marker (new installs use the fork) with
+ # no force_cpu field. Re-running into the same install-dir/repo reproduces the same
+ # CPU bundle; --force-cpu (the persisted-CPU re-assert) must not appear for a marker
+ # that never recorded a deliberate CPU choice, so it can still heal to GPU (#6097).
cmd = _capture_install_cmd(
monkeypatch,
tmp_path,
repo = "ggml-org/llama.cpp",
asset = "llama-b9334-bin-ubuntu-x64.tar.gz",
)
- assert "--cpu-fallback" not in cmd
+ assert "--force-cpu" not in cmd
assert "--rocm-gfx" not in cmd
assert "--has-rocm" not in cmd
assert "--simple-policy" not in cmd
@@ -714,7 +758,7 @@ def test_install_cmd_cuda_marker_minimal_and_backward_compatible(monkeypatch, tm
assert "--simple-policy" not in cmd
assert "--rocm-gfx" not in cmd
assert "--has-rocm" not in cmd
- assert "--cpu-fallback" not in cmd
+ assert "--force-cpu" not in cmd
def test_install_cmd_pins_offered_release_tag(monkeypatch, tmp_path):
diff --git a/studio/backend/tests/test_setup_llama_cpp_backend.py b/studio/backend/tests/test_setup_llama_cpp_backend.py
new file mode 100644
index 0000000000..36928c680c
--- /dev/null
+++ b/studio/backend/tests/test_setup_llama_cpp_backend.py
@@ -0,0 +1,154 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""setup.sh and setup.ps1 must map UNSLOTH_LLAMA_CPP_BACKEND=cpu to
+install_llama_prebuilt.py's --force-cpu so users can force the CPU-only prebuilt
+on GPU hosts (#7213). The match is case-insensitive and whitespace-trimmed, an
+unrecognized value warns instead of silently falling back, and macOS warns (no
+CPU-only bundle). Runs the real block extracted from each script so the tests
+track the shipped logic.
+"""
+
+import os
+import re
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+_STUDIO = Path(__file__).resolve().parents[2]
+_SETUP_SH = _STUDIO / "setup.sh"
+_SETUP_PS1 = _STUDIO / "setup.ps1"
+_SKIP_NO_BASH = pytest.mark.skipif(shutil.which("bash") is None, reason = "bash unavailable")
+_SKIP_NO_PWSH = pytest.mark.skipif(shutil.which("pwsh") is None, reason = "pwsh unavailable")
+
+
+def _backend_block() -> str:
+ text = _SETUP_SH.read_text(encoding = "utf-8")
+ m = re.search(r"_llama_backend=.*?esac", text, re.DOTALL)
+ assert m, "UNSLOTH_LLAMA_CPP_BACKEND block not found in setup.sh"
+ return m.group(0)
+
+
+def _run(value: str | None, system: str = "Linux") -> tuple[list[str], str]:
+ # Pass the value through env (not the script text) so whitespace survives, and
+ # stub the setup.sh logging helpers the unknown-value branch calls. system sets
+ # _HOST_SYSTEM so the macOS (Darwin) no-op branch can be exercised.
+ env = {k: v for k, v in os.environ.items() if k != "UNSLOTH_LLAMA_CPP_BACKEND"}
+ if value is not None:
+ env["UNSLOTH_LLAMA_CPP_BACKEND"] = value
+ harness = (
+ f'_PREBUILT_CMD=()\nC_WARN=""\n_HOST_SYSTEM="{system}"\n'
+ 'step() { printf "STEP: %s\\n" "$*" >&2; }\n'
+ f"{_backend_block()}\n"
+ 'printf "%s\\n" "${_PREBUILT_CMD[@]}"'
+ )
+ out = subprocess.run(
+ ["bash", "-c", harness], capture_output = True, text = True, env = env, check = True
+ )
+ return out.stdout.split(), out.stderr
+
+
+@_SKIP_NO_BASH
+@pytest.mark.parametrize("value", ["cpu", "CPU", "Cpu", " cpu ", "CPU\t"])
+def test_backend_cpu_appends_flag(value):
+ # A deliberate CPU choice persists, so it uses --force-cpu (not the transient
+ # --cpu-fallback the arm64 GPU-build recovery uses).
+ args, stderr = _run(value)
+ assert "--force-cpu" in args
+ assert "--cpu-fallback" not in args
+ assert "Ignoring" not in stderr
+
+
+@_SKIP_NO_BASH
+@pytest.mark.parametrize("value", ["cpu", "CPU", " cpu "])
+def test_backend_cpu_macos_warns_no_flag(value):
+ # macOS has no CPU-only bundle (the universal build already runs on CPU), so the
+ # override warns instead of writing a misleading forced-CPU marker.
+ args, stderr = _run(value, system = "Darwin")
+ assert "--force-cpu" not in args
+ assert "--cpu-fallback" not in args
+ assert "macOS" in stderr
+
+
+@_SKIP_NO_BASH
+@pytest.mark.parametrize("value", [None, "", "auto", "AUTO", " "])
+def test_backend_auto_no_flag_no_warn(value):
+ args, stderr = _run(value)
+ assert "--force-cpu" not in args
+ assert "Ignoring" not in stderr
+
+
+@_SKIP_NO_BASH
+@pytest.mark.parametrize("value", ["vulkan", "gpu", "cuda"])
+def test_backend_unknown_warns_and_no_flag(value):
+ args, stderr = _run(value)
+ assert "--force-cpu" not in args
+ assert "Ignoring" in stderr
+
+
+@_SKIP_NO_BASH
+def test_arm64_recovery_uses_transient_cpu_fallback():
+ # The arm64 Linux GPU-build recovery must stay transient (--cpu-fallback), never
+ # the persisted --force-cpu, so a later update can still heal to a GPU bundle (#6097).
+ text = _SETUP_SH.read_text(encoding = "utf-8")
+ m = re.search(r"_ARM64_CPU_CMD=\((.*?)\)", text, re.DOTALL)
+ assert m, "arm64 CPU recovery command not found in setup.sh"
+ block = m.group(1)
+ assert "--cpu-fallback" in block
+ assert "--force-cpu" not in block
+
+
+def _ps1_search(pattern: str, flags = 0) -> str:
+ m = re.search(pattern, _SETUP_PS1.read_text(encoding = "utf-8"), flags)
+ assert m, f"setup.ps1 block not found: {pattern}"
+ return m.group(0)
+
+
+def _run_ps1(value: str | None) -> str:
+ # The override is normalized (assign + warn) at the top of the prebuilt block and
+ # applied to $prebuiltArgs lower down; compose both real snippets.
+ normalize = _ps1_search(
+ r'\$llamaBackend = "\$\(\$env:UNSLOTH_LLAMA_CPP_BACKEND\)".*?Write-Host.*?\n\s*\}',
+ re.DOTALL,
+ )
+ apply_flag = _ps1_search(
+ r'if \(\$llamaBackend -eq "cpu"\) \{\s*\$prebuiltArgs \+= "--force-cpu"\s*\}'
+ )
+ env = {k: v for k, v in os.environ.items() if k != "UNSLOTH_LLAMA_CPP_BACKEND"}
+ if value is not None:
+ env["UNSLOTH_LLAMA_CPP_BACKEND"] = value
+ harness = f'$prebuiltArgs = @()\n{normalize}\n{apply_flag}\n"ARGS:" + ($prebuiltArgs -join ",")'
+ out = subprocess.run(
+ ["pwsh", "-NoProfile", "-Command", harness],
+ capture_output = True,
+ text = True,
+ env = env,
+ check = True,
+ )
+ return out.stdout
+
+
+@_SKIP_NO_PWSH
+@pytest.mark.parametrize("value", ["cpu", "CPU", "Cpu", " cpu ", "CPU\t"])
+def test_ps1_backend_cpu_appends_flag(value):
+ out = _run_ps1(value)
+ assert "--force-cpu" in out
+ assert "Ignoring" not in out
+
+
+@_SKIP_NO_PWSH
+@pytest.mark.parametrize("value", [None, "", "auto", "AUTO", " "])
+def test_ps1_backend_auto_no_flag_no_warn(value):
+ out = _run_ps1(value)
+ assert "--force-cpu" not in out
+ assert "Ignoring" not in out
+
+
+@_SKIP_NO_PWSH
+@pytest.mark.parametrize("value", ["vulkan", "gpu", "cuda"])
+def test_ps1_backend_unknown_warns_and_no_flag(value):
+ out = _run_ps1(value)
+ assert "--force-cpu" not in out
+ assert "Ignoring" in out
diff --git a/studio/backend/utils/llama_cpp_update.py b/studio/backend/utils/llama_cpp_update.py
index 31dbda63ea..67733bde35 100644
--- a/studio/backend/utils/llama_cpp_update.py
+++ b/studio/backend/utils/llama_cpp_update.py
@@ -479,6 +479,7 @@ def _run_update(
asset: Optional[str],
script: Path,
pin_release_tag: Optional[str] = None,
+ force_cpu: bool = False,
) -> None:
"""Worker: put the backend into a maintenance state, run the installer for
the latest prebuilt, then refresh caches so the next load uses the new build.
@@ -522,6 +523,12 @@ def _run_update(
if pin_release_tag:
cmd.extend(["--published-release-tag", pin_release_tag])
cmd.extend(_rocm_install_args(asset))
+ # Re-assert a deliberate CPU install (--force-cpu) so detect_host on a GPU host
+ # does not re-route to a GPU/Vulkan bundle and revive the crash (#7213). --force-cpu
+ # (not --cpu-fallback) also re-persists force_cpu, keeping the choice across future
+ # updates. A natural fallback (or a legacy marker without the flag) heals to GPU (#6097).
+ if force_cpu:
+ cmd.append("--force-cpu")
logger.info("llama update: installing", cmd = " ".join(cmd))
# Stream progress lines into job["progress"].
env = dict(os.environ, UNSLOTH_PROGRESS_PERCENT_STEP = "5")
@@ -671,6 +678,7 @@ def start_update() -> dict:
repo = marker.get("published_repo") or DEFAULT_PUBLISHED_REPO
from_tag = marker.get("tag") or marker.get("release_tag")
asset = marker.get("asset")
+ force_cpu = bool(marker.get("force_cpu"))
# Install exactly the release the banner offered: the installer's own
# "latest" is commit-date ordered and can lag the published_at pick
# above, reinstalling the current build in a loop (the #6219 class).
@@ -705,6 +713,8 @@ def start_update() -> dict:
repo = (res or {}).get("repo") or DEFAULT_PUBLISHED_REPO
from_tag = None
asset = (res or {}).get("asset")
+ # Source builds carry no forced-CPU marker, so nothing to preserve here.
+ force_cpu = False
# No pin: source-build detection resolves via --resolve-prebuilt latest,
# the same resolver the unpinned apply uses, so the two already agree.
pin_release_tag = None
@@ -735,7 +745,7 @@ def start_update() -> dict:
thread = threading.Thread(
target = _run_update,
- args = (install_dir, repo, asset, script, pin_release_tag),
+ args = (install_dir, repo, asset, script, pin_release_tag, force_cpu),
name = "llama-cpp-update",
daemon = True,
)
diff --git a/studio/install_llama_prebuilt.py b/studio/install_llama_prebuilt.py
index 9bbd0cb8be..b8182a534b 100644
--- a/studio/install_llama_prebuilt.py
+++ b/studio/install_llama_prebuilt.py
@@ -6450,6 +6450,7 @@ def write_prebuilt_metadata(
choice: AssetChoice,
approved_checksums: ApprovedReleaseChecksums,
prebuilt_fallback_used: bool,
+ force_cpu: bool = False,
) -> None:
source_asset_name, source_sha256 = selected_source_archive_metadata(
approved_checksums,
@@ -6474,6 +6475,10 @@ def write_prebuilt_metadata(
"release_tag": release_tag,
"published_repo": approved_checksums.repo,
"asset": choice.name,
+ # True only for a deliberate CPU choice (--force-cpu). The updater re-asserts it
+ # so a forced CPU install is not re-routed to a GPU bundle (#7213). An automatic
+ # --cpu-fallback (e.g. arm64 GPU-build recovery) stays False so it can heal to GPU.
+ "force_cpu": force_cpu,
"asset_sha256": choice.expected_sha256,
"source": choice.source_label,
# Binary-side repo/tag for non-fork sources (e.g. the ggml-org upstream
@@ -6501,6 +6506,24 @@ def write_prebuilt_metadata(
(install_dir / "UNSLOTH_PREBUILT_INFO.json").write_text(json.dumps(metadata, indent = 2) + "\n")
+def sync_marker_force_cpu(install_dir: Path, persist_force_cpu: bool) -> None:
+ """Sync only the force_cpu flag of an existing marker when the resolved bundle is
+ unchanged, so the install is skipped without a full metadata rewrite. A deliberate
+ --force-cpu on top of a naturally installed CPU bundle (same asset) must still be
+ recorded, else the updater will not re-assert it and can re-route the install to a
+ GPU/Vulkan bundle that revives the crash (#7213)."""
+ marker_path = install_dir / "UNSLOTH_PREBUILT_INFO.json"
+ try:
+ marker = json.loads(marker_path.read_text())
+ except (OSError, ValueError):
+ return
+ if not isinstance(marker, dict) or bool(marker.get("force_cpu")) == persist_force_cpu:
+ return
+ marker["force_cpu"] = persist_force_cpu
+ marker_path.write_text(json.dumps(marker, indent = 2) + "\n")
+ log(f"existing install reused; recorded force_cpu={persist_force_cpu} from this run")
+
+
def expected_install_fingerprint(
*,
llama_tag: str,
@@ -6746,6 +6769,7 @@ def validate_prebuilt_choice(
approved_checksums: ApprovedReleaseChecksums,
prebuilt_fallback_used: bool,
quantized_path: Path,
+ force_cpu: bool = False,
) -> tuple[Path, Path]:
source_repo, source_ref, source_archive, exact_source = preferred_source_archive(
approved_checksums, llama_tag
@@ -6786,6 +6810,7 @@ def validate_prebuilt_choice(
choice = choice,
approved_checksums = approved_checksums,
prebuilt_fallback_used = prebuilt_fallback_used,
+ force_cpu = force_cpu,
)
# Hashless external prebuilts are not in the approved-sha256
# manifest and rely on the functional smoke test as their only integrity gate,
@@ -6828,6 +6853,7 @@ def validate_prebuilt_attempts(
approved_checksums: ApprovedReleaseChecksums,
initial_fallback_used: bool = False,
existing_install_dir: Path | None = None,
+ force_cpu: bool = False,
) -> tuple[AssetChoice, Path, bool]:
attempt_list = list(attempts)
if not attempt_list:
@@ -6880,6 +6906,7 @@ def validate_prebuilt_attempts(
approved_checksums = approved_checksums,
prebuilt_fallback_used = tried_fallback,
quantized_path = quantized_path,
+ force_cpu = force_cpu,
)
except Exception as exc:
remove_tree(staging_dir)
@@ -6939,8 +6966,8 @@ def _route_to_vulkan_prebuilt(
"""Point a Vulkan-capable host at the upstream ggml-org Vulkan prebuilt.
The unsloth published repo ships only CUDA/ROCm/CPU assets, so Vulkan comes
- from UPSTREAM_REPO. Two triggers route here, both suppressed under
- --cpu-fallback (the explicit "give me CPU" last resort wins):
+ from UPSTREAM_REPO. Two triggers route here, both suppressed when a CPU flag
+ (--cpu-fallback or --force-cpu, folded into force_cpu) wins:
* UNSLOTH_FORCE_VULKAN forces Vulkan over the detected CUDA/ROCm backend;
* an auto-detected Intel GPU with NO physical NVIDIA/ROCm -- the purpose
of the has_intel_gpu probe, since the fork manifest ships no Vulkan asset.
@@ -7020,8 +7047,11 @@ def install_prebuilt(
override_has_rocm: bool = False,
override_rocm_gfx: str | None = None,
force_cpu: bool = False,
+ persist_force_cpu: bool = False,
instruction_cleanup_root: Path | None = None,
) -> None:
+ # force_cpu drops GPU detection (mechanism, both --cpu-fallback and --force-cpu);
+ # persist_force_cpu records the deliberate choice so the updater re-asserts it.
host = detect_host()
host = _apply_host_overrides(
host,
@@ -7072,6 +7102,9 @@ def install_prebuilt(
"existing llama.cpp install already matches selected release "
f"{current.release_tag} upstream_tag={current.llama_tag}; skipping download and install"
)
+ # Reused bundle is unchanged, but a fresh --force-cpu still must be
+ # recorded so the updater re-asserts it (#7213).
+ sync_marker_force_cpu(install_dir, persist_force_cpu)
return
with tempfile.TemporaryDirectory(prefix = "unsloth-llama-prebuilt-") as tmp:
work_dir = Path(tmp)
@@ -7092,6 +7125,7 @@ def install_prebuilt(
"existing llama.cpp install already matches fallback release "
f"{plan.release_tag} upstream_tag={plan.llama_tag}; skipping reinstall"
)
+ sync_marker_force_cpu(install_dir, persist_force_cpu)
return
log(
"selected "
@@ -7112,6 +7146,8 @@ def install_prebuilt(
initial_fallback_used = release_index > 0,
# Skip is gated per-attempt inside, so pass the dir always.
existing_install_dir = install_dir,
+ # Persist only the deliberate choice, not a transient fallback.
+ force_cpu = persist_force_cpu,
)
except ExistingInstallSatisfied:
return
@@ -7209,8 +7245,21 @@ def parse_args() -> argparse.Namespace:
default = False,
help = (
"Select the CPU prebuilt for this OS/arch even when a GPU is present. "
- "setup.sh uses this as a last resort for arm64 Linux GPU hosts whose "
- "source build failed (no arm64 CUDA prebuilt exists anywhere)."
+ "Automatic/transient: setup.sh uses this as a last resort for arm64 Linux "
+ "GPU hosts whose source build failed. Does NOT persist, so a later update "
+ "heals back to a GPU bundle once one is available (#6097). Use --force-cpu "
+ "for a deliberate CPU-only choice that survives updates."
+ ),
+ )
+ parser.add_argument(
+ "--force-cpu",
+ action = "store_true",
+ default = False,
+ help = (
+ "Deliberate CPU-only install (UNSLOTH_LLAMA_CPP_BACKEND=cpu). Drops GPU "
+ "detection like --cpu-fallback but also records force_cpu in the marker, so "
+ "the in-app updater re-asserts CPU and never re-routes to a GPU/Vulkan "
+ "bundle that would revive the Intel iGPU crash (#7213)."
),
)
resolve_group = parser.add_mutually_exclusive_group()
@@ -7333,16 +7382,19 @@ def main() -> int:
# Host-aware "is a prebuilt available" probe, no download. Every host now
# plans against the fork (args.published_repo defaults to it); an explicit
# --published-repo overrides. PrebuiltFallback == source build.
+ # Both flags drop GPU detection; --force-cpu additionally persists (install
+ # path only). The probe only needs the mechanism, so OR them.
+ _cpu_mechanism = args.cpu_fallback or args.force_cpu
host = _apply_host_overrides(
detect_host(),
override_has_rocm = args.has_rocm,
override_rocm_gfx = args.rocm_gfx,
- force_cpu = args.cpu_fallback,
+ force_cpu = _cpu_mechanism,
)
# Same Vulkan routing the install path applies, so the probe's answer
# matches what would install (an Intel/forced-Vulkan host -> upstream).
host, repo, release_tag = _route_to_vulkan_prebuilt(
- host, args.published_repo, args.published_release_tag or "", force_cpu = args.cpu_fallback
+ host, args.published_repo, args.published_release_tag or "", force_cpu = _cpu_mechanism
)
try:
_requested, plans = resolve_simple_install_release_plans(
@@ -7380,7 +7432,10 @@ def main() -> int:
published_release_tag = args.published_release_tag or "",
override_has_rocm = args.has_rocm,
override_rocm_gfx = args.rocm_gfx,
- force_cpu = args.cpu_fallback,
+ # Both drop GPU detection; only --force-cpu (deliberate) is recorded so the
+ # updater re-asserts it. --cpu-fallback stays transient and heals to GPU.
+ force_cpu = args.cpu_fallback or args.force_cpu,
+ persist_force_cpu = args.force_cpu,
instruction_cleanup_root = install_arg.absolute(),
)
return EXIT_SUCCESS
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index 98e801cd3c..f7d33a1142 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -32,6 +32,10 @@ $PackageDir = Split-Path -Parent $ScriptDir
# (no matching GitHub release), forces a source build, and causes HTTP 422
# errors. Only use "master" temporarily when the latest release is missing
# support for a new model architecture.
+#
+# UNSLOTH_LLAMA_CPP_BACKEND : "auto" (default) or "cpu". When "cpu", forces
+# the CPU-only prebuilt bundle on GPU hosts. Fixes Intel iGPU Vulkan
+# crashes (#7213).
$DefaultLlamaPrForce = ""
$DefaultLlamaSource = "https://github.com/ggml-org/llama.cpp"
$DefaultLlamaTag = "latest"
@@ -3367,6 +3371,15 @@ if ($LocalLlamaCppLinked) {
if ($env:UNSLOTH_LLAMA_RELEASE_TAG) {
$prebuiltArgs += @("--published-release-tag", $env:UNSLOTH_LLAMA_RELEASE_TAG)
}
+ # UNSLOTH_LLAMA_CPP_BACKEND=cpu (case-insensitive, whitespace-trimmed) forces the
+ # CPU-only prebuilt via --force-cpu (persisted so updates keep it). Fixes Intel
+ # iGPU Vulkan crash (#7213).
+ $llamaBackend = "$($env:UNSLOTH_LLAMA_CPP_BACKEND)".Trim().ToLowerInvariant()
+ if ($llamaBackend -eq "cpu") {
+ $prebuiltArgs += "--force-cpu"
+ } elseif ($llamaBackend -and $llamaBackend -ne "auto") {
+ Write-Host "[WARN] Ignoring UNSLOTH_LLAMA_CPP_BACKEND='$($env:UNSLOTH_LLAMA_CPP_BACKEND)' (expected 'auto' or 'cpu')" -ForegroundColor Yellow
+ }
$prevEAPPrebuilt = $ErrorActionPreference
$ErrorActionPreference = "Continue"
$previousNativeErrorPreference = $null
diff --git a/studio/setup.sh b/studio/setup.sh
index 8d47eecfda..df7178c662 100755
--- a/studio/setup.sh
+++ b/studio/setup.sh
@@ -36,6 +36,10 @@ fi
# forces a source build, and causes HTTP 422 errors.
# Only use "master" temporarily when the latest release
# is missing support for a new model architecture.
+#
+# UNSLOTH_LLAMA_CPP_BACKEND : "auto" (default) or "cpu". When "cpu", forces
+# the CPU-only prebuilt bundle on GPU hosts.
+# Fixes Intel iGPU Vulkan crashes (#7213).
# ──────────────────────────────────────────────────────────────────────────
_DEFAULT_LLAMA_PR_FORCE=""
_DEFAULT_LLAMA_SOURCE="https://github.com/ggml-org/llama.cpp"
@@ -1359,6 +1363,22 @@ else
# present so it can still attempt a prebuilt. Mirrors setup.ps1 behaviour.
_PREBUILT_CMD+=(--has-rocm)
fi
+ # UNSLOTH_LLAMA_CPP_BACKEND=cpu (case-insensitive, trimmed) forces the CPU-only
+ # prebuilt via --force-cpu, bypassing Vulkan/CUDA/ROCm. Fixes Intel iGPU crash (#7213).
+ # No effect on macOS: the universal bundle already runs on CPU (Metal is a runtime
+ # -ngl choice), so warn instead of writing a misleading forced-CPU marker.
+ _llama_backend="$(printf '%s' "${UNSLOTH_LLAMA_CPP_BACKEND:-auto}" | awk '{$1=$1; print tolower($0)}')"
+ case "$_llama_backend" in
+ cpu)
+ if [ "$_HOST_SYSTEM" = "Darwin" ]; then
+ step "llama.cpp" "UNSLOTH_LLAMA_CPP_BACKEND=cpu has no effect on macOS (universal build; use -ngl 0 at runtime for CPU-only)" "$C_WARN" >&2
+ else
+ _PREBUILT_CMD+=(--force-cpu)
+ fi
+ ;;
+ ""|auto) ;;
+ *) step "llama.cpp" "Ignoring UNSLOTH_LLAMA_CPP_BACKEND='$UNSLOTH_LLAMA_CPP_BACKEND' (expected 'auto' or 'cpu')" "$C_WARN" >&2 ;;
+ esac
_PREBUILT_LOG="$(mktemp)"
set +e
if _is_verbose; then
diff --git a/tests/studio/install/test_install_llama_prebuilt_logic.py b/tests/studio/install/test_install_llama_prebuilt_logic.py
index e995e5033e..9a094ddc0e 100644
--- a/tests/studio/install/test_install_llama_prebuilt_logic.py
+++ b/tests/studio/install/test_install_llama_prebuilt_logic.py
@@ -1348,6 +1348,7 @@ def test_install_prebuilt_falls_back_to_older_release_plan(
approved_checksums,
initial_fallback_used = False,
existing_install_dir = None,
+ force_cpu = False,
):
call_log.append((llama_tag, initial_fallback_used))
if llama_tag == "b9002":
@@ -2551,6 +2552,7 @@ def test_install_prebuilt_skips_when_older_release_fallback_matches_existing_ins
approved_checksums,
initial_fallback_used = False,
existing_install_dir = None,
+ force_cpu = False,
):
call_log.append(llama_tag)
raise PrebuiltFallback("validation failed for latest release")
@@ -2698,6 +2700,7 @@ def test_install_prebuilt_skips_same_release_fallback_attempt_when_installed(
approved_checksums,
prebuilt_fallback_used,
quantized_path,
+ force_cpu = False,
):
attempted_names.append(choice.name)
if choice.name == first_choice.name:
@@ -2824,6 +2827,7 @@ def test_install_prebuilt_same_tag_upstream_failure_uses_older_unsloth_release_p
approved_checksums,
initial_fallback_used = False,
existing_install_dir = None,
+ force_cpu = False,
):
attempted.append((llama_tag, release_tag, attempts[0].source_label))
if llama_tag == "b9002":
From 07272b9278eaa2813c30f3b12f712276ff97fa01 Mon Sep 17 00:00:00 2001
From: Daniel Han
Date: Mon, 20 Jul 2026 00:57:02 -0700
Subject: [PATCH 044/271] Experimental: correct varlen sample packing for
hybrid linear-attention models (#7249)
* Fix text-only VLM CPT packing truncation
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Handle streaming vision datasets in packing
* Harden multimodal packing detection
* Preserve safe packing boundaries
* Scope stream packing checks to VLMs
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Narrow VLM packing detection
* Align packing mode and eval safety
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add qwen3_5/qwen3_next to PADDING_FREE_BLOCKLIST to avoid packed-sequence contamination
* Detect hybrid linear-attention models structurally instead of by name for packing guard
* Add experimental varlen packing for hybrid linear-attention models
Feed seq_idx to the causal conv and cu_seqlens to the gated-delta scan so
sample packing / padding-free reset state at sequence boundaries for hybrid
linear-attention models (Qwen3.5, Qwen3-Next). Gated behind
UNSLOTH_EXPERIMENTAL_HYBRID_PACKING and fail-closed: when the flag is off or
the accelerated kernels (causal_conv1d + fla) are unavailable, the guard keeps
these models on the padded path.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Harden hybrid linear-attention varlen packing shim
Make patch_hybrid_linear_attention_varlen robust across transformers 4.57.6
through 5.x and TRL 0.22.2 through 1.x, following the import_fixes.py style:
- Read UNSLOTH_EXPERIMENTAL_HYBRID_PACKING at call time so the flag takes effect
when set after importing unsloth.
- Idempotent: repeat calls on a patched model return True without re-validating
the wrappers or double-wrapping; signatures are checked on captured originals.
- Prefer the authoritative packed_seq_lengths (via get_packed_info_from_kwargs)
over position_ids resets, handling pad_to_multiple_of trailing tokens.
- Suppress injection for cached forwards (use_cache / past_key_values) so
generation and eval are left on the untouched decode path.
- Validate every gated-delta module before mutating any (transactional).
- Bind position_ids / use_cache from both positional and keyword args.
- Verify dispatch at runtime (Unsloth wraps each module forward, so the mixer
source is not statically inspectable) and warn once if the shim is never hit.
- Emit one deduped diagnostic on each fail-closed path.
Add CPU unit tests covering the hybrid guard detection, the boundary builders,
and the shim (fail-closed, active, idempotent, cached no-op, runtime handshake).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Abort hybrid packing when the varlen shim is not fully dispatched
The runtime handshake used a single per-module hit flag written by both the conv
and scan wrappers, so a partial dispatch (only one kernel routed through
self.) passed the any() check and trained on contaminated data, and a
missing dispatch only logged a warning. Track conv and scan dispatch separately,
require both on every gated-delta module on the first packed forward, and raise
before loss/backward when either is missing (the batch is already flattened, so
there is no padded recovery at that point). Also skip an empty packed_seq_lengths
before it reaches max(), and document the position_ids fallback's left-pad
assumption.
Add tests for no-dispatch and partial (conv-only / scan-only) abort, the
packed_seq_lengths preference over a competing position_ids, MRoPE 3D position
ids, and the pad_to_multiple_of trailing-segment path through the metadata builder.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Import the hybrid packing patch from its submodule to satisfy the import-hoist lint
* Fail closed for hybrid packing on encoder-decoder, chunked-loss, and string-name models
The varlen shim only helps decoder-only hybrid models that run their mixer
through self. on a live nn.Module forward. Three cases slipped past
the guard:
- Encoder-decoder configs (is_encoder_decoder) reached the packing path even
though flattening a cross-attention batch is unsound. Block them explicitly.
- TRL's chunked_nll loss (the 1.x default) calls the backbone directly and
bypasses model.forward, so the per-instance forward wrapper that refreshes
the varlen stash never runs. Detect that path and keep the model padded.
- A string model_name reaches the trainer before the module exists, so the
instance shim has nothing to patch. Resolve the config up front and keep
string hybrids on the padded path.
Adds encoder-decoder / decoder-only / chunked-loss / string-model tests.
* Harden the SFT source-injection replacements and forward auth args for string models
The wrapped-packing injection rewrote the sourced unsloth_zoo sft_prepare_dataset
with str.replace anchored on the exact 'All Unsloth Zoo code licensed under
LGPLv3' comment. str.replace never raises on a missing anchor, so a supported
newer unsloth_zoo (the dependency is only lower-bounded) that moved that header
would silently drop the setup while the truncation and pack_dataset edits still
referenced _unsloth_wrapped_packing / _inspect, raising NameError on every SFT
dataset preparation.
- Install the setup at the sft_prepare_dataset signature via re.subn (a structural
anchor that always exists) and raise if even that is missing.
- Route the remaining edits through a _require_replace helper that fails loudly on a
missing required anchor (or warns once for an optional one), formalizing the
verify-then-replace idiom the DPO patchers in this file already use.
- Reuse the guarded _unsloth_pack_has_strategy at the pack_dataset call instead of
re-calling inspect.signature(pack_dataset) unguarded, so a non-introspectable
pack_dataset cannot crash there after the setup already handled it.
- _resolve_string_model_config now forwards token / use_auth_token / cache_dir /
code_revision, so a private hybrid resolves its config instead of falling through
as non-hybrid and enabling packing without the varlen shim.
Adds regression tests for the drift-resistant injection, the helper, and the
string-model auth forwarding.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Honor top-level SFTConfig.trust_remote_code when resolving a string model
TRL merges the top-level args.trust_remote_code into the load via
model_init_kwargs.setdefault("trust_remote_code", args.trust_remote_code) before
create_model_from_path, so a remote-code hybrid is commonly set with
SFTConfig(trust_remote_code=True) rather than inside model_init_kwargs. The config
probe only read model_init_kwargs, so AutoConfig could fail for such a model, leave
model_config None, and let the guard treat it as non-hybrid, enabling packing
without the varlen shim. Mirror TRL's setdefault (model_init_kwargs wins).
* Tighten hybrid-packing comments for concision
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: alkinun
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Etherl <61019402+Etherll@users.noreply.github.com>
---
tests/utils/test_packing.py | 579 +++++++++++++++++++++++++++---
unsloth/models/rl_replacements.py | 90 +++--
unsloth/trainer.py | 93 ++++-
unsloth/utils/packing.py | 302 ++++++++++++++++
4 files changed, 984 insertions(+), 80 deletions(-)
diff --git a/tests/utils/test_packing.py b/tests/utils/test_packing.py
index 98c29d9f0f..1b8bb65058 100644
--- a/tests/utils/test_packing.py
+++ b/tests/utils/test_packing.py
@@ -15,6 +15,7 @@
from unsloth import FastLanguageModel
import unsloth.trainer as trainer_module
+import unsloth.utils.packing as packing_module
from unsloth.utils import attention_dispatch as attention_dispatch_utils
from unsloth.utils.packing import (
configure_padding_free,
@@ -22,6 +23,7 @@ from unsloth.utils.packing import (
enable_padding_free_metadata,
enable_sample_packing,
mask_packed_sequence_boundaries,
+ patch_hybrid_linear_attention_varlen,
)
from contextlib import ExitStack
@@ -161,6 +163,327 @@ def test_configure_padding_free():
assert config.remove_unused_columns is False
+# --- Hybrid linear-attention guard + varlen shim (PR #7211 / #7249) ---------------
+
+
+def _hybrid_config_model():
+ # Qwen3.5 / Qwen3-Next style: explicit linear_attention layer schedule.
+ return SimpleNamespace(
+ config = SimpleNamespace(layer_types = ["linear_attention", "full_attention"])
+ )
+
+
+def _gemma3_model():
+ # Has layer_types but no linear_attention -> must NOT be flagged as hybrid.
+ return SimpleNamespace(
+ config = SimpleNamespace(
+ model_type = "gemma3", layer_types = ["sliding_attention", "full_attention"]
+ ),
+ )
+
+
+def _dense_qwen3_model():
+ return SimpleNamespace(
+ config = SimpleNamespace(model_type = "qwen3", architectures = ["Qwen3ForCausalLM"])
+ )
+
+
+class _FakeGatedDeltaNet(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.conv1d = torch.nn.Conv1d(4, 4, 3, groups = 4)
+ self.A_log = torch.nn.Parameter(torch.zeros(4))
+
+ def forward(self, hidden_states, **kwargs): # dispatch through self.
+ return self.chunk_gated_delta_rule(self.causal_conv1d_fn(hidden_states))
+
+
+class _FakeHybridModel(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.config = SimpleNamespace() # no markers -> forces module-level detection
+ self.linear_attn = _FakeGatedDeltaNet()
+
+
+def test_is_hybrid_linear_attention_detects_and_excludes():
+ is_hybrid = trainer_module._is_hybrid_linear_attention_model
+ assert is_hybrid(_hybrid_config_model()) is True
+ assert is_hybrid(_FakeHybridModel()) is True # module-structural evidence
+ assert is_hybrid(_text_model()) is False # Llama
+ assert is_hybrid(_gemma3_model()) is False # layer_types without linear_attention
+ assert is_hybrid(_dense_qwen3_model()) is False # dense Qwen3
+ assert is_hybrid(None) is False
+
+
+def test_varlen_from_position_ids():
+ cu, seq_idx = packing_module._varlen_from_position_ids(torch.tensor([[0, 1, 0, 0, 1, 2]]))
+ assert cu.tolist() == [0, 2, 3, 6]
+ assert seq_idx.tolist() == [[0, 0, 1, 2, 2, 2]]
+ assert (
+ packing_module._varlen_from_position_ids(torch.tensor([[0, 1, 2, 3]])) is None
+ ) # single sequence
+ assert packing_module._varlen_from_position_ids(torch.tensor([[1, 2, 3]])) is None # first != 0
+ assert (
+ packing_module._varlen_from_position_ids(torch.tensor([[0, 1], [0, 1]])) is None
+ ) # normal 2-row batch
+ assert packing_module._varlen_from_position_ids(None) is None
+
+
+def test_seq_idx_from_cu_seqlens_handles_trailing_pad():
+ cu = torch.tensor([0, 2, 5], dtype = torch.int32)
+ boundaries, seq_idx = packing_module._seq_idx_from_cu_seqlens(cu, total = 8) # pad_to_multiple_of
+ assert boundaries.tolist() == [0, 2, 5, 8]
+ assert seq_idx.tolist() == [[0, 0, 1, 1, 1, 2, 2, 2]]
+ boundaries2, _ = packing_module._seq_idx_from_cu_seqlens(cu, total = 5) # exact fit
+ assert boundaries2.tolist() == [0, 2, 5]
+ assert (
+ packing_module._seq_idx_from_cu_seqlens(torch.tensor([1, 2], dtype = torch.int32), total = 2)
+ is None
+ )
+ assert packing_module._seq_idx_from_cu_seqlens(cu, total = 3) is None # boundaries exceed total
+
+
+def test_hybrid_varlen_metadata_prefers_packed_seq_lengths():
+ # A competing position_ids would segment [0, 3, 6]; packed_seq_lengths must win.
+ kwargs = {
+ "input_ids": torch.zeros(1, 6, dtype = torch.long),
+ "packed_seq_lengths": torch.tensor([2, 1, 3], dtype = torch.int32),
+ "position_ids": torch.tensor([[0, 1, 2, 0, 1, 2]]),
+ }
+ cu, seq_idx = packing_module._hybrid_varlen_metadata(kwargs)
+ assert cu.tolist() == [0, 2, 3, 6]
+ assert seq_idx.tolist() == [[0, 0, 1, 2, 2, 2]]
+
+
+def test_hybrid_varlen_metadata_suppressed_when_cached():
+ base = {
+ "input_ids": torch.zeros(1, 6, dtype = torch.long),
+ "packed_seq_lengths": torch.tensor([2, 1, 3], dtype = torch.int32),
+ }
+ assert packing_module._hybrid_varlen_metadata({**base, "use_cache": True}) is None
+ assert packing_module._hybrid_varlen_metadata({**base, "past_key_values": object()}) is None
+
+
+def test_hybrid_varlen_metadata_none_for_plain_batch():
+ kwargs = {
+ "input_ids": torch.zeros(1, 4, dtype = torch.long),
+ "position_ids": torch.tensor([[0, 1, 2, 3]]),
+ }
+ assert packing_module._hybrid_varlen_metadata(kwargs) is None
+
+
+def _make_fake_kernels():
+ def causal_conv1d_fn(
+ x,
+ weight = None,
+ bias = None,
+ activation = None,
+ seq_idx = None,
+ ):
+ causal_conv1d_fn.calls.append(seq_idx)
+ return x
+
+ causal_conv1d_fn.calls = []
+
+ def chunk_gated_delta_rule(
+ q,
+ k = None,
+ v = None,
+ cu_seqlens = None,
+ **kw,
+ ):
+ chunk_gated_delta_rule.calls.append(cu_seqlens)
+ return q
+
+ chunk_gated_delta_rule.calls = []
+ return causal_conv1d_fn, chunk_gated_delta_rule
+
+
+class _ShimGatedDeltaNet(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.conv1d = torch.nn.Conv1d(4, 4, 3, groups = 4)
+ self.causal_conv1d_fn, self.chunk_gated_delta_rule = _make_fake_kernels()
+
+ def forward(self, hidden_states, **kwargs):
+ return self.chunk_gated_delta_rule(self.causal_conv1d_fn(hidden_states))
+
+
+class _ShimHybridModel(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.config = SimpleNamespace(layer_types = ["linear_attention", "full_attention"])
+ self.linear_attn = _ShimGatedDeltaNet()
+
+ def forward(
+ self,
+ input_ids = None,
+ position_ids = None,
+ packed_seq_lengths = None,
+ use_cache = None,
+ **kwargs,
+ ):
+ return self.linear_attn(input_ids.float())
+
+
+def test_patch_hybrid_varlen_flag_off(monkeypatch):
+ monkeypatch.delenv("UNSLOTH_EXPERIMENTAL_HYBRID_PACKING", raising = False)
+ model = _ShimHybridModel()
+ assert patch_hybrid_linear_attention_varlen(model) is False
+ assert not getattr(model, "_unsloth_varlen_forward_wrapped", False)
+
+
+def test_patch_hybrid_varlen_active_and_idempotent(monkeypatch):
+ monkeypatch.setenv("UNSLOTH_EXPERIMENTAL_HYBRID_PACKING", "1")
+ model = _ShimHybridModel()
+ conv_orig, scan_orig = (
+ model.linear_attn.causal_conv1d_fn,
+ model.linear_attn.chunk_gated_delta_rule,
+ )
+
+ assert patch_hybrid_linear_attention_varlen(model) is True
+ assert model._unsloth_varlen_forward_wrapped is True
+ assert model.linear_attn._unsloth_varlen_wrapped is True
+ assert patch_hybrid_linear_attention_varlen(model) is True # idempotent, no double-wrap
+
+ conv_orig.calls.clear()
+ scan_orig.calls.clear()
+ packing_module._HYBRID_WARNED.clear()
+ ids = torch.zeros(1, 6, dtype = torch.long)
+ model(
+ input_ids = ids,
+ packed_seq_lengths = torch.tensor([2, 1, 3], dtype = torch.int32),
+ use_cache = False,
+ )
+ assert conv_orig.calls[-1] is not None # seq_idx injected
+ assert scan_orig.calls[-1].tolist() == [0, 2, 3, 6] # cu_seqlens injected
+ assert not packing_module._HYBRID_WARNED # handshake passed, no rejection
+
+ conv_orig.calls.clear()
+ scan_orig.calls.clear()
+ model(
+ input_ids = ids, packed_seq_lengths = torch.tensor([2, 1, 3], dtype = torch.int32), use_cache = True
+ )
+ assert conv_orig.calls[-1] is None # cached forward -> no injection
+ assert scan_orig.calls[-1] is None
+
+
+def test_patch_hybrid_varlen_torch_fallback_fail_closed(monkeypatch):
+ monkeypatch.setenv("UNSLOTH_EXPERIMENTAL_HYBRID_PACKING", "1")
+ model = _ShimHybridModel()
+
+ def torch_chunk_gated_delta_rule(
+ q,
+ cu_seqlens = None,
+ **kw,
+ ):
+ return q
+
+ model.linear_attn.chunk_gated_delta_rule = torch_chunk_gated_delta_rule
+ assert patch_hybrid_linear_attention_varlen(model) is False
+ assert not getattr(model, "_unsloth_varlen_forward_wrapped", False)
+
+
+def test_patch_hybrid_varlen_bad_signature_fail_closed(monkeypatch):
+ monkeypatch.setenv("UNSLOTH_EXPERIMENTAL_HYBRID_PACKING", "1")
+ model = _ShimHybridModel()
+
+ def scan_no_cu(q, **kw): # missing cu_seqlens
+ return q
+
+ model.linear_attn.chunk_gated_delta_rule = scan_no_cu
+ assert patch_hybrid_linear_attention_varlen(model) is False
+
+
+def _hybrid_model_with_gdn(gdn_forward):
+ # Build a fake hybrid model whose gated-delta mixer forward is `gdn_forward`.
+ class _GatedDeltaNet(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.conv1d = torch.nn.Conv1d(4, 4, 3, groups = 4)
+ self.causal_conv1d_fn, self.chunk_gated_delta_rule = _make_fake_kernels()
+
+ forward = gdn_forward
+
+ class _Model(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.config = SimpleNamespace(layer_types = ["linear_attention", "full_attention"])
+ self.linear_attn = _GatedDeltaNet()
+
+ def forward(
+ self,
+ input_ids = None,
+ packed_seq_lengths = None,
+ use_cache = None,
+ **kwargs,
+ ):
+ return self.linear_attn(input_ids.float())
+
+ return _Model()
+
+
+def test_patch_hybrid_varlen_no_dispatch_aborts(monkeypatch):
+ # Dispatch is verified at runtime, not statically. A mixer that never calls
+ # self. installs the shim, but the first packed forward aborts (both
+ # boundary kernels are load-bearing).
+ monkeypatch.setenv("UNSLOTH_EXPERIMENTAL_HYBRID_PACKING", "1")
+ model = _hybrid_model_with_gdn(lambda self, hidden_states, **kw: hidden_states)
+ assert patch_hybrid_linear_attention_varlen(model) is True # kernels valid -> installs
+ with pytest.raises(RuntimeError, match = "both invoked"):
+ model(
+ input_ids = torch.zeros(1, 6),
+ packed_seq_lengths = torch.tensor([2, 1, 3], dtype = torch.int32),
+ use_cache = False,
+ )
+
+
+def test_patch_hybrid_varlen_partial_dispatch_aborts(monkeypatch):
+ # Only the conv fires; the scan would leak state. Both must be invoked, so abort.
+ monkeypatch.setenv("UNSLOTH_EXPERIMENTAL_HYBRID_PACKING", "1")
+ conv_only = _hybrid_model_with_gdn(
+ lambda self, hidden_states, **kw: self.causal_conv1d_fn(hidden_states)
+ )
+ assert patch_hybrid_linear_attention_varlen(conv_only) is True
+ with pytest.raises(RuntimeError, match = "both invoked"):
+ conv_only(
+ input_ids = torch.zeros(1, 6),
+ packed_seq_lengths = torch.tensor([2, 1, 3], dtype = torch.int32),
+ use_cache = False,
+ )
+
+ scan_only = _hybrid_model_with_gdn(
+ lambda self, hidden_states, **kw: self.chunk_gated_delta_rule(hidden_states)
+ )
+ assert patch_hybrid_linear_attention_varlen(scan_only) is True
+ with pytest.raises(RuntimeError, match = "both invoked"):
+ scan_only(
+ input_ids = torch.zeros(1, 6),
+ packed_seq_lengths = torch.tensor([2, 1, 3], dtype = torch.int32),
+ use_cache = False,
+ )
+
+
+def test_varlen_from_position_ids_mrope_3d():
+ pos = (
+ torch.tensor([[0, 1, 0, 0, 1, 2]]).unsqueeze(0).expand(3, 1, 6).clone()
+ ) # [3,1,T] text plane
+ cu, seq_idx = packing_module._varlen_from_position_ids(pos)
+ assert cu.tolist() == [0, 2, 3, 6]
+ assert seq_idx.tolist() == [[0, 0, 1, 2, 2, 2]]
+
+
+def test_hybrid_varlen_metadata_trailing_pad():
+ # packed_seq_lengths sum to 6 but the flattened input is 8 (pad_to_multiple_of).
+ kwargs = {
+ "input_ids": torch.zeros(1, 8, dtype = torch.long),
+ "packed_seq_lengths": torch.tensor([2, 1, 3], dtype = torch.int32),
+ }
+ cu, seq_idx = packing_module._hybrid_varlen_metadata(kwargs)
+ assert cu.tolist() == [0, 2, 3, 6, 8]
+ assert seq_idx.tolist() == [[0, 0, 1, 2, 2, 2, 3, 3]]
+
+
def _patch_fake_sft_trainer():
class FakeSFTTrainer:
def __init__(self, *args, **kwargs):
@@ -245,29 +568,101 @@ def test_vlm_without_processing_class_still_disables_packing():
("t5", "T5ForConditionalGeneration"),
("bart", "BartForConditionalGeneration"),
("whisper", "WhisperForConditionalGeneration"),
- ("csm", "CsmForConditionalGeneration"),
),
)
-def test_nonvision_conditional_generation_keeps_packing(model_type, architecture):
+def test_encoder_decoder_disables_packing(model_type, architecture):
+ # Text-only encoder-decoder models are not VLMs, but their bidirectional encoder
+ # attends across concatenated samples once padding-free drops attention_mask.
fake_trainer = _patch_fake_sft_trainer()
config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
model = SimpleNamespace(
- config = SimpleNamespace(model_type = model_type, architectures = [architecture]),
+ config = SimpleNamespace(
+ model_type = model_type,
+ architectures = [architecture],
+ is_encoder_decoder = True,
+ ),
max_seq_length = 16,
)
- trainer = fake_trainer(
- model,
- config,
- None,
- Dataset.from_dict({"text": ["text-only sample"]}),
+ trainer = fake_trainer(model, config, None, Dataset.from_dict({"text": ["text-only sample"]}))
+
+ assert config.packing is False
+ assert config.padding_free is False
+
+
+def test_decoder_only_conditional_generation_keeps_packing():
+ # CSM is decoder-only despite the ForConditionalGeneration name -> packing stays on.
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ model = SimpleNamespace(
+ config = SimpleNamespace(
+ model_type = "csm",
+ architectures = ["CsmForConditionalGeneration"],
+ is_encoder_decoder = False,
+ ),
+ max_seq_length = 16,
)
+ trainer = fake_trainer(model, config, None, Dataset.from_dict({"text": ["text-only sample"]}))
+
assert config.packing is True
assert config.padding_free is True
assert trainer.model._unsloth_allow_packed_overlength is True
+def _hybrid_trainer_model():
+ return SimpleNamespace(
+ config = SimpleNamespace(
+ model_type = "qwen3_next",
+ architectures = ["Qwen3NextForCausalLM"],
+ layer_types = ["linear_attention", "full_attention"],
+ ),
+ max_seq_length = 16,
+ )
+
+
+def test_hybrid_varlen_active_enables_packing(monkeypatch):
+ # Baseline: shim active + no forward bypass -> hybrid packing is allowed.
+ monkeypatch.setattr(trainer_module, "_chunked_loss_bypasses_forward", lambda config: False)
+ monkeypatch.setattr(trainer_module, "patch_hybrid_linear_attention_varlen", lambda model: True)
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ fake_trainer(_hybrid_trainer_model(), config, None, Dataset.from_dict({"text": ["x"]}))
+ assert config.packing is True
+ assert config.padding_free is True
+
+
+def test_hybrid_chunked_loss_stays_on_padded_path(monkeypatch):
+ # TRL's chunked-loss forward bypass leaves the varlen shim off -> block packing.
+ monkeypatch.setattr(trainer_module, "_chunked_loss_bypasses_forward", lambda config: True)
+ monkeypatch.setattr(trainer_module, "patch_hybrid_linear_attention_varlen", lambda model: True)
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ fake_trainer(_hybrid_trainer_model(), config, None, Dataset.from_dict({"text": ["x"]}))
+ assert config.packing is False
+ assert config.padding_free is False
+
+
+def test_string_hybrid_model_disables_packing(monkeypatch):
+ # A string model= is materialized after init; a hybrid string is blocked because the
+ # shim cannot patch a not-yet-built model.
+ monkeypatch.setattr(
+ trainer_module,
+ "_resolve_string_model_config",
+ lambda name, cfg: SimpleNamespace(
+ model_type = "qwen3_next",
+ architectures = ["Qwen3NextForCausalLM"],
+ layer_types = ["linear_attention", "full_attention"],
+ ),
+ )
+ monkeypatch.setattr(trainer_module, "patch_hybrid_linear_attention_varlen", lambda model: True)
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+ fake_trainer("Qwen/Qwen3-Next-80B-A3B", config, None, Dataset.from_dict({"text": ["x"]}))
+ assert config.packing is False
+ assert config.padding_free is False
+
+
def test_vlm_vision_dataset_still_disables_packing():
fake_trainer = _patch_fake_sft_trainer()
config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
@@ -486,49 +881,6 @@ def test_wrapped_packing_preserves_overlength_tokens(monkeypatch, legacy_api):
assert all(len(input_ids) <= args.max_length for input_ids in packed_ids)
-# Named to match the unsloth_zoo helper: sft_trainer_prepare_dataset sources it by
-# name and renames "def sft_prepare_dataset" -> "def _prepare_dataset". This fixture
-# deliberately omits the "All Unsloth Zoo code licensed under LGPLv3" header to emulate
-# a newer, compatible Zoo whose header moved (the dependency is only lower-bounded).
-def sft_prepare_dataset(
- self, dataset, processing_class, args, packing, formatting_func, dataset_text_field
-):
- do_truncation = True
- # Mirror the Zoo call so the "truncation = do_truncation," injection anchor
- # survives formatting (a bare tuple assignment gets rewritten to a paren form).
- dataset = processing_class(
- dataset,
- truncation = do_truncation,
- )
- return dataset
-
-
-def test_wrapped_packing_setup_survives_missing_zoo_header(monkeypatch):
- # Regression: the wrapped-packing setup used to anchor on the Zoo license comment,
- # so a header change made it a no-op while the truncation reference still landed,
- # NameError-ing every SFT dataset preparation. It must now install via the
- # signature and always precede the reference.
- import ast
- import textwrap
- import unsloth.models.rl_replacements as rlr
-
- monkeypatch.setitem(rlr.RL_REPLACEMENTS, "sft_prepare_dataset", sft_prepare_dataset)
-
- source = (
- "def _prepare_dataset(self, dataset, processing_class, args, packing, "
- "formatting_func, dataset_text_field):\n return dataset\n"
- )
- patched = rlr.sft_trainer_prepare_dataset("_prepare_dataset", source)
-
- assert "_unsloth_wrapped_packing = packing" in patched
- assert "import inspect as _inspect" in patched
- assert "not _unsloth_wrapped_packing" in patched
- assert patched.index("_unsloth_wrapped_packing = packing") < patched.index(
- "truncation = do_truncation and not _unsloth_wrapped_packing"
- )
- ast.parse(textwrap.dedent(patched))
-
-
class _DummyChild(torch.nn.Module):
def __init__(self):
super().__init__()
@@ -759,3 +1111,128 @@ def test_packing_sdpa(tmp_path):
if hasattr(trainer, "accelerator"):
trainer.accelerator.free_memory()
+
+
+# --- wrapped-packing source-injection robustness (reviewer.py / fork findings) --------
+
+
+# fmt: off
+# Named to match the unsloth_zoo helper (sourced by name, "def sft_prepare_dataset" ->
+# "def _prepare_dataset"). Deliberately OMITS the "licensed under LGPLv3" header to
+# emulate a newer Zoo whose header moved (dependency is only lower-bounded). Source only.
+def sft_prepare_dataset(
+ self, dataset, processing_class, args, packing, formatting_func, dataset_text_field
+):
+ do_truncation = True
+ max_seq_length = 4
+ used_column_names = ["text"]
+ map_kwargs = {}
+ dataset = processing_class(dataset, truncation = do_truncation,)
+ if do_truncation and max_seq_length > 0:
+ pass
+ if packing:
+ dataset = pack_dataset(
+ dataset.select_columns(used_column_names),
+ max_seq_length,
+ getattr(args, "packing_strategy", "bfd"),
+ map_kwargs,
+ )
+ return dataset
+# fmt: on
+
+
+def test_wrapped_packing_injection_is_drift_resistant(monkeypatch):
+ # Regression: the setup used to anchor on the Zoo license comment, so a header
+ # change silently no-op'd it while the truncation/pack edits still referenced its
+ # variables -> NameError on every SFT prep. It must now install via the signature
+ # before those references, and the pack edit must reuse the guarded
+ # _unsloth_pack_has_strategy instead of re-calling _inspect.signature(pack_dataset).
+ import ast
+ import textwrap
+ import unsloth.models.rl_replacements as rlr
+
+ monkeypatch.setitem(rlr.RL_REPLACEMENTS, "sft_prepare_dataset", sft_prepare_dataset)
+
+ source = (
+ "def _prepare_dataset(self, dataset, processing_class, args, packing, "
+ "formatting_func, dataset_text_field):\n return dataset\n"
+ )
+ patched = rlr.sft_trainer_prepare_dataset("_prepare_dataset", source)
+
+ # setup installed despite the missing header, and before it is referenced
+ assert "_unsloth_wrapped_packing = packing" in patched
+ assert "import inspect as _inspect" in patched
+ assert patched.index("_unsloth_wrapped_packing = packing") < patched.index(
+ "truncation = do_truncation and not _unsloth_wrapped_packing"
+ )
+ # the pack edit reuses the guarded flag (signature inspected exactly once, in setup)
+ assert "if _unsloth_pack_has_strategy:" in patched
+ assert patched.count("_inspect.signature(pack_dataset)") == 1
+ ast.parse(textwrap.dedent(patched))
+
+
+def test_require_replace_raises_on_missing_anchor():
+ from unsloth.models.rl_replacements import _require_replace
+
+ assert _require_replace("abc", "b", "B") == "aBc"
+ with pytest.raises(RuntimeError):
+ _require_replace("abc", "z", "Z", where = "unit test")
+ # an optional edit warns once and returns the source unchanged (no dangling ref)
+ assert _require_replace("abc", "z", "Z", required = False, where = "optional") == "abc"
+
+
+def test_resolve_string_model_config_forwards_token(monkeypatch):
+ import transformers
+
+ captured = {}
+
+ class _FakeAutoConfig:
+ @staticmethod
+ def from_pretrained(name, **kwargs):
+ captured.update(kwargs)
+ return SimpleNamespace(is_encoder_decoder = False)
+
+ monkeypatch.setattr(transformers, "AutoConfig", _FakeAutoConfig)
+
+ config_arg = SimpleNamespace(
+ model_init_kwargs = {
+ "token": "hf_secret",
+ "trust_remote_code": True,
+ "cache_dir": "/tmp/cache",
+ "torch_dtype": "bfloat16", # not a config arg -> must NOT be forwarded
+ }
+ )
+ result = trainer_module._resolve_string_model_config("org/private-hybrid", config_arg)
+
+ assert result is not None
+ assert captured.get("token") == "hf_secret"
+ assert captured.get("trust_remote_code") is True
+ assert captured.get("cache_dir") == "/tmp/cache"
+ assert "torch_dtype" not in captured
+
+
+def test_resolve_string_model_config_merges_top_level_trust_remote_code(monkeypatch):
+ import transformers
+
+ captured = {}
+
+ class _FakeAutoConfig:
+ @staticmethod
+ def from_pretrained(name, **kwargs):
+ captured.update(kwargs)
+ return SimpleNamespace(is_encoder_decoder = False)
+
+ monkeypatch.setattr(transformers, "AutoConfig", _FakeAutoConfig)
+
+ # SFTConfig(trust_remote_code=True) with no model_init_kwargs entry is honored
+ config_arg = SimpleNamespace(model_init_kwargs = {}, trust_remote_code = True)
+ trainer_module._resolve_string_model_config("org/remote-hybrid", config_arg)
+ assert captured.get("trust_remote_code") is True
+
+ # model_init_kwargs wins over the top-level flag (mirrors TRL's setdefault)
+ captured.clear()
+ config_arg = SimpleNamespace(
+ model_init_kwargs = {"trust_remote_code": False}, trust_remote_code = True
+ )
+ trainer_module._resolve_string_model_config("org/remote-hybrid", config_arg)
+ assert captured.get("trust_remote_code") is False
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index b0709f7376..4ef3af6add 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -437,6 +437,52 @@ RL_FUNCTIONS["dpo_trainer"].append(dpo_trainer_compute_loss_liger)
RL_EXTRA_ARGS["dpo_trainer"].append(dpo_trainer_data_collator_vision_keys)
+_WRAPPED_PACKING_SETUP = (
+ " import inspect as _inspect\n"
+ " try:\n"
+ ' _unsloth_pack_has_strategy = "strategy" in _inspect.signature(pack_dataset).parameters\n'
+ " except Exception:\n"
+ " _unsloth_pack_has_strategy = True\n"
+ " _unsloth_wrapped_packing = packing and (\n"
+ ' getattr(args, "packing_strategy", None) == "wrapped"\n'
+ " or not _unsloth_pack_has_strategy\n"
+ " )\n"
+)
+
+_WARNED_MISSING_ANCHORS = set()
+
+
+def _require_replace(
+ function,
+ old,
+ new,
+ *,
+ count = 1,
+ required = True,
+ where = "",
+):
+ """str.replace that never silently no-ops a load-bearing source edit.
+
+ Plain str.replace returns the source unchanged when the anchor is absent, so a
+ drifted anchor in a newer TRL / unsloth_zoo would skip the edit while later edits
+ still reference helper variables it should have introduced (NameError at runtime).
+ Fail loudly for a required edit, warn once and skip for an optional one, so a
+ drifted source can never corrupt the patched function silently.
+ """
+ if old not in function:
+ detail = f" ({where})" if where else ""
+ if required:
+ raise RuntimeError(
+ f"Unsloth: source anchor not found{detail}; the patched function is out "
+ "of sync with this TRL / unsloth_zoo version. Please file a bug report."
+ )
+ if where not in _WARNED_MISSING_ANCHORS:
+ _WARNED_MISSING_ANCHORS.add(where)
+ logger.warning(f"Unsloth: skipped an optional source edit{detail} (anchor not found).")
+ return function
+ return function.replace(old, new, count)
+
+
# Fix tokenizer double BOS
def sft_trainer_prepare_dataset(function_name, function):
if function_name != "_prepare_non_packed_dataloader" and function_name != "_prepare_dataset":
@@ -454,27 +500,14 @@ def sft_trainer_prepare_dataset(function_name, function):
if matched:
# Use fast version!
function = inspect.getsource(fast_sft_prepare_dataset)
- # why: install the wrapped-packing setup (and the `_inspect` import the
- # truncation / pack_dataset rewrites below depend on) at the function
- # signature, a structural anchor that always exists, rather than the
- # unsloth_zoo license-comment line. That header is only lower-bounded, so a
- # newer Zoo may move or drop it; anchoring there let the setup silently
- # no-op while the references still landed, NameError-ing every SFT dataset
- # preparation. Fail loudly if even the signature cannot be located.
- _wrapped_packing_setup = (
- " import inspect as _inspect\n"
- " try:\n"
- ' _unsloth_pack_has_strategy = "strategy" in _inspect.signature(pack_dataset).parameters\n'
- " except Exception:\n"
- " _unsloth_pack_has_strategy = True\n"
- " _unsloth_wrapped_packing = packing and (\n"
- ' getattr(args, "packing_strategy", None) == "wrapped"\n'
- " or not _unsloth_pack_has_strategy\n"
- " )\n"
- )
+ # why: anchor the wrapped-packing setup on the function signature -- a
+ # structural anchor that always exists -- not the unsloth_zoo license comment,
+ # which is only lower-bounded and a newer Zoo may move or drop. Anchoring there
+ # let the setup silently no-op while edits below referenced its variables,
+ # NameError-ing every SFT dataset prep. Fail loudly if the signature is missing.
function, _n_setup = re.subn(
r"(def sft_prepare_dataset\s*\(.*?\)\s*(?:->[^:\n]*)?:[ \t]*\n)",
- lambda match: match.group(1) + _wrapped_packing_setup,
+ lambda match: match.group(1) + _WRAPPED_PACKING_SETUP,
function,
count = 1,
flags = re.DOTALL,
@@ -484,15 +517,25 @@ def sft_trainer_prepare_dataset(function_name, function):
"Unsloth: failed to install wrapped-packing support into "
"sft_prepare_dataset (signature not found); please file a bug report."
)
- function = function.replace(
+ # why: route each edit through _require_replace so a drifted anchor fails
+ # loudly instead of leaving a dangling reference to the setup variables.
+ function = _require_replace(
+ function,
"truncation = do_truncation,",
"truncation = do_truncation and not _unsloth_wrapped_packing,",
+ where = "sft_prepare_dataset truncation flag",
)
- function = function.replace(
+ function = _require_replace(
+ function,
"if do_truncation and max_seq_length > 0:",
"if do_truncation and not _unsloth_wrapped_packing and max_seq_length > 0:",
+ where = "sft_prepare_dataset truncation guard",
)
- function = function.replace(
+ # why: reuse the guarded _unsloth_pack_has_strategy from the setup instead of
+ # re-calling _inspect.signature(pack_dataset) here -- the setup wraps that call
+ # in try/except, so a non-introspectable pack_dataset must not crash here.
+ function = _require_replace(
+ function,
"""dataset = pack_dataset(
dataset.select_columns(used_column_names),
max_seq_length,
@@ -500,13 +543,14 @@ def sft_trainer_prepare_dataset(function_name, function):
map_kwargs,
)""",
"""_pack_kwargs = {"map_kwargs": map_kwargs}
- if "strategy" in _inspect.signature(pack_dataset).parameters:
+ if _unsloth_pack_has_strategy:
_pack_kwargs["strategy"] = getattr(args, "packing_strategy", "bfd")
dataset = pack_dataset(
dataset.select_columns(used_column_names),
max_seq_length,
**_pack_kwargs,
)""",
+ where = "sft_prepare_dataset pack_dataset call",
)
function = function.split("\n")
function = "\n".join(" " * 4 + x for x in function)
diff --git a/unsloth/trainer.py b/unsloth/trainer.py
index 61d41aad21..1c30192301 100644
--- a/unsloth/trainer.py
+++ b/unsloth/trainer.py
@@ -17,6 +17,7 @@ import os
import psutil
import warnings
from dataclasses import dataclass, field
+from types import SimpleNamespace
from typing import Optional, List
from functools import wraps
@@ -32,6 +33,7 @@ from unsloth.utils import (
enable_padding_free_metadata,
enable_sample_packing,
)
+from unsloth.utils.packing import patch_hybrid_linear_attention_varlen
from unsloth_zoo.training_utils import (
unsloth_train as _unsloth_train,
)
@@ -101,9 +103,9 @@ PADDING_FREE_BLOCKLIST = {
"gpt_oss", # - gpt_oss: Uses Flex Attention which doesn't handle padding_free correctly
}
# Hybrid linear-attention / state-space models (Qwen3.5, Qwen3-Next, ...) carry a
-# recurrent gated-delta state plus a causal conv1d. Sample packing / padding-free
-# flattens the batch, so those ops leak state across sequence boundaries. Detected
-# structurally by _is_hybrid_linear_attention_model rather than by model name.
+# recurrent gated-delta state plus a causal conv1d that leak across sequence
+# boundaries once packing flattens the batch. Detected structurally by
+# _is_hybrid_linear_attention_model, not by model name.
def _should_pack(config) -> bool:
@@ -267,6 +269,57 @@ def _is_hybrid_linear_attention_model(model) -> bool:
return False
+def _resolve_string_model_config(model_name, config_arg):
+ """TRL materializes a string ``model=`` inside ``__init__``; resolve its config
+ up front so the packing guards run before the dataset is packed. Best-effort:
+ returns None if the config cannot be loaded."""
+ try:
+ from transformers import AutoConfig
+
+ init_kwargs = getattr(config_arg, "model_init_kwargs", None) or {}
+ # why: forward auth + cache args too. Dropping token/use_auth_token made a
+ # private hybrid fail to load (resolve as None) -> treated as non-hybrid ->
+ # packing enabled without the shim even though TRL later loads it with the token.
+ forward = {
+ key: init_kwargs[key]
+ for key in (
+ "trust_remote_code",
+ "revision",
+ "subfolder",
+ "token",
+ "use_auth_token",
+ "cache_dir",
+ "code_revision",
+ )
+ if key in init_kwargs
+ }
+ # why: TRL merges top-level args.trust_remote_code into the load via setdefault
+ # before create_model_from_path, so honor it here (model_init_kwargs wins), else
+ # a remote-code hybrid with SFTConfig(trust_remote_code=True) resolves as None
+ # and skips the guard.
+ top_level_trust_remote_code = getattr(config_arg, "trust_remote_code", None)
+ if top_level_trust_remote_code is not None:
+ forward.setdefault("trust_remote_code", top_level_trust_remote_code)
+ return AutoConfig.from_pretrained(model_name, **forward)
+ except Exception:
+ return None
+
+
+def _chunked_loss_bypasses_forward(config) -> bool:
+ """TRL's default ``loss_type="chunked_nll"`` patches the model forward and calls
+ the backbone directly, so a forward wrapper never runs. Detect it so hybrid
+ packing stays on the padded path instead of silently skipping the varlen shim."""
+ try:
+ import trl.trainer.sft_trainer as _sft_trainer
+ except Exception:
+ return False
+ if not hasattr(_sft_trainer, "_patch_chunked_ce_lm_head"):
+ return False # TRL has no chunked-CE path -> forward is not bypassed
+ if getattr(config, "use_liger_kernel", False):
+ return False # liger forces loss_type="nll" -> normal forward
+ return getattr(config, "loss_type", None) in (None, "chunked_nll")
+
+
# Unsloth gradient accumulation fix:
from transformers import __version__ as transformers_version, ProcessorMixin
@@ -632,13 +685,38 @@ def _patch_sft_trainer_auto_packing(trl_module):
is_vlm = False
is_unsupported_model = False
is_hybrid = False
+ is_encoder_decoder = False
+ hybrid_varlen_active = False
if model is not None:
model_config = getattr(model, "config", None)
+ if model_config is None and isinstance(model, str):
+ # TRL builds a string model inside __init__; resolve its config now.
+ model_config = _resolve_string_model_config(model, config_arg)
if model_config is not None:
model_types = get_transformers_model_type(model_config)
is_unsupported_model = any(x in PADDING_FREE_BLOCKLIST for x in model_types)
is_vlm = _is_vlm_config(model_config, model_types)
- is_hybrid = _is_hybrid_linear_attention_model(model)
+ is_encoder_decoder = bool(getattr(model_config, "is_encoder_decoder", False))
+ hybrid_target = (
+ SimpleNamespace(config = model_config)
+ if isinstance(model, str) and model_config is not None
+ else model
+ )
+ is_hybrid = _is_hybrid_linear_attention_model(hybrid_target)
+ # Hybrid models corrupt packed batches unless the gated-delta conv + scan
+ # reset at sequence boundaries. Enable the experimental varlen shim (flag +
+ # kernels) so packing stays correct, else keep them blocked. A string model
+ # (patched only after init) and TRL's chunked-loss forward bypass both leave
+ # the shim off, so hybrid packing falls back to the padded path.
+ if (
+ is_hybrid
+ and not isinstance(model, str)
+ and not _chunked_loss_bypasses_forward(config_arg)
+ ):
+ try:
+ hybrid_varlen_active = patch_hybrid_linear_attention_varlen(model)
+ except Exception:
+ hybrid_varlen_active = False
processing_class = (
args[5] if len(args) >= 6 else kwargs.get("processing_class") or kwargs.get("tokenizer")
@@ -664,7 +742,8 @@ def _patch_sft_trainer_auto_packing(trl_module):
or is_auto_processor_vlm
or is_vision_dataset
or is_unsupported_model
- or is_hybrid
+ or is_encoder_decoder
+ or (is_hybrid and not hybrid_varlen_active)
or (
os.environ.get("UNSLOTH_RETURN_LOGITS", "0") == "1"
) # Disable padding free on forced logits
@@ -684,7 +763,9 @@ def _patch_sft_trainer_auto_packing(trl_module):
reason = "vision-language model with auto processor"
elif is_vision_dataset:
reason = "vision dataset"
- elif is_hybrid:
+ elif is_encoder_decoder:
+ reason = "encoder-decoder model"
+ elif is_hybrid and not hybrid_varlen_active:
reason = "hybrid linear-attention model"
elif is_unsupported_model:
reason = f"unsupported model type(s): {', '.join(model_types)}"
diff --git a/unsloth/utils/packing.py b/unsloth/utils/packing.py
index dd0a1bfb62..f8d539fb93 100644
--- a/unsloth/utils/packing.py
+++ b/unsloth/utils/packing.py
@@ -17,8 +17,11 @@
from __future__ import annotations
+import inspect
import logging
+import os
from collections import OrderedDict
+from functools import wraps
from typing import Any, Iterable, Optional, Sequence, Tuple
import torch
@@ -218,6 +221,305 @@ def enable_padding_free_metadata(model, trainer):
collator._unsloth_padding_free_lengths_wrapped = True
+# --- Experimental: correct packing / padding-free for hybrid linear-attention ---
+# Qwen3.5 / Qwen3-Next mix a gated-delta recurrence with a causal conv1d. Packing
+# flattens the batch, and both ops leak state across sequence boundaries unless we
+# pass seq_idx (conv) and cu_seqlens (scan). Only the accelerated kernels accept
+# these, so we fail closed on the pure-torch fallbacks. Gated behind an env flag.
+#
+# Overrides only the per-module prefill kernels (causal_conv1d_fn /
+# chunk_gated_delta_rule), leaving decode untouched so generation is unaffected.
+# Recompute-safe under gradient checkpointing; never fires for cached forwards.
+# Feature-detect (never version-detect), fail closed, idempotent, one deduped
+# diagnostic when it declines to activate.
+_HYBRID_PACKING_ENV_VAR = "UNSLOTH_EXPERIMENTAL_HYBRID_PACKING"
+_HYBRID_LOGGER = logging.getLogger("unsloth.hybrid_packing")
+_HYBRID_WARNED: set = set()
+
+
+def _hybrid_packing_enabled() -> bool:
+ # Read at call time so setting the flag after `import unsloth` still takes effect.
+ return os.environ.get(_HYBRID_PACKING_ENV_VAR, "0").strip().lower() in {
+ "1",
+ "true",
+ "yes",
+ "on",
+ }
+
+
+def _hybrid_reject(reason: str) -> bool:
+ # One deduped diagnostic explaining why hybrid packing stayed on the padded path.
+ if reason not in _HYBRID_WARNED:
+ _HYBRID_WARNED.add(reason)
+ _HYBRID_LOGGER.warning(
+ "Unsloth: hybrid linear-attention packing disabled (padded path): %s.",
+ reason,
+ )
+ return False
+
+
+def _iter_gated_delta_modules(model):
+ modules, seen = [], set()
+ for module in model.modules():
+ if id(module) in seen:
+ continue
+ seen.add(id(module))
+ if type(module).__name__.endswith("GatedDeltaNet") and hasattr(module, "conv1d"):
+ modules.append(module)
+ return modules
+
+
+def _hybrid_varlen_kernels_available(gated_delta_modules) -> Optional[str]:
+ """None if every module can use the accelerated varlen path, else a short
+ reason string. All modules are validated before any are mutated; signatures
+ are read off the captured originals when already wrapped.
+
+ Dispatch (the mixer actually calling self.causal_conv1d_fn /
+ self.chunk_gated_delta_rule) is verified at RUNTIME by the forward-wrapper
+ handshake, not statically: Unsloth's compile-disable shim hides it from
+ inspect.getsource, and every supported transformers release dispatches
+ through the instance attribute."""
+ if not gated_delta_modules:
+ return "no gated-delta modules found"
+ for module in gated_delta_modules:
+ conv = getattr(module, "_unsloth_varlen_orig_conv", None) or getattr(
+ module,
+ "causal_conv1d_fn",
+ None,
+ )
+ scan = getattr(module, "_unsloth_varlen_orig_scan", None) or getattr(
+ module,
+ "chunk_gated_delta_rule",
+ None,
+ )
+ if conv is None or scan is None:
+ return "accelerated kernels missing (install causal_conv1d and fla)"
+ if getattr(scan, "__name__", "").startswith("torch_") or getattr(
+ conv,
+ "__name__",
+ "",
+ ).startswith("torch_"):
+ return "pure-torch kernel fallback in use"
+ try:
+ if "seq_idx" not in inspect.signature(conv).parameters:
+ return "conv kernel does not accept seq_idx"
+ if "cu_seqlens" not in inspect.signature(scan).parameters:
+ return "scan kernel does not accept cu_seqlens"
+ except (TypeError, ValueError):
+ return "kernel signature not introspectable"
+ return None
+
+
+def _varlen_from_position_ids(position_ids):
+ """(cu_seqlens int32[n+1], seq_idx int32[1,T]) for a flattened padding-free
+ batch, else None. Padding-free position_ids reset to 0 at each sequence start;
+ accepts only a validated single-row pack (normal batch or single sequence ->
+ None). Fallback used only when packed_seq_lengths is absent: it assumes
+ right-packed reset position_ids and would mis-segment a left-padded row, which
+ is why packed_seq_lengths is always preferred."""
+ if position_ids is None:
+ return None
+ pos = position_ids
+ if pos.dim() == 3: # MRoPE [n_planes, 1, T] -> text plane is index 0
+ pos = pos[0]
+ if pos.dim() != 2 or pos.shape[0] != 1:
+ return None
+ row = pos[0]
+ total = row.shape[0]
+ starts = (row == 0).nonzero(as_tuple = False).flatten()
+ if starts.numel() <= 1 or int(starts[0].item()) != 0:
+ return None
+ cu_seqlens = torch.cat(
+ [
+ starts.to(torch.int32),
+ torch.tensor([total], dtype = torch.int32, device = row.device),
+ ]
+ )
+ return _seq_idx_from_cu_seqlens(cu_seqlens, total)
+
+
+def _seq_idx_from_cu_seqlens(cu_seqlens, total):
+ """(cu_seqlens int32[n+1], seq_idx int32[1,total]) partitioning [0, total),
+ else None. Appends a trailing segment for pad_to_multiple_of zero tokens so the
+ boundaries always cover the full flattened length the kernels see."""
+ if cu_seqlens is None or cu_seqlens.numel() < 2 or int(cu_seqlens[0].item()) != 0:
+ return None
+ boundaries = cu_seqlens.to(torch.int32)
+ last = int(boundaries[-1].item())
+ if last > total:
+ return None
+ if last < total: # trailing pad tokens -> one final segment
+ boundaries = torch.cat(
+ [
+ boundaries,
+ torch.tensor([total], dtype = torch.int32, device = boundaries.device),
+ ]
+ )
+ lengths = boundaries[1:] - boundaries[:-1]
+ if not bool((lengths > 0).all()):
+ return None
+ seq_idx = torch.repeat_interleave(
+ torch.arange(lengths.numel(), dtype = torch.int32, device = boundaries.device),
+ lengths.to(torch.int64),
+ ).unsqueeze(0)
+ return boundaries, seq_idx
+
+
+def _hybrid_varlen_metadata(kwargs):
+ """Boundary metadata (cu_seqlens, seq_idx) for one flattened packed forward,
+ else None. Prefers the authoritative packed_seq_lengths, falls back to
+ reset-style position_ids. Returns None for cached forwards and non-packed
+ batches so decode / eval / normal batches are a strict no-op."""
+ if kwargs.get("use_cache"):
+ return None
+ if kwargs.get("past_key_values") is not None or kwargs.get("cache_params") is not None:
+ return None
+ total, device = None, None
+ for key in ("input_ids", "inputs_embeds", "position_ids"):
+ tensor = kwargs.get(key)
+ if tensor is not None and hasattr(tensor, "shape"):
+ total = tensor.shape[1] if key == "inputs_embeds" else tensor.shape[-1]
+ device = tensor.device
+ break
+ if total is None:
+ return None
+ psl = kwargs.get("packed_seq_lengths")
+ if psl is not None and getattr(psl, "numel", lambda: 1)() > 0: # skip empty (no max())
+ info = get_packed_info_from_kwargs(kwargs, device)
+ if info is not None:
+ _, cu_seqlens, _ = info
+ built = _seq_idx_from_cu_seqlens(cu_seqlens, total)
+ if built is not None:
+ return built
+ return _varlen_from_position_ids(kwargs.get("position_ids"))
+
+
+def patch_hybrid_linear_attention_varlen(model) -> bool:
+ """Feed seq_idx / cu_seqlens to the gated-delta conv + scan so packing and
+ padding-free reset state at sequence boundaries. Gated by
+ UNSLOTH_EXPERIMENTAL_HYBRID_PACKING and fail-closed. Returns True when the
+ varlen path is active, so the caller may allow packing for the model.
+ Idempotent: repeat calls on an already-patched model return True."""
+ if not _hybrid_packing_enabled():
+ return False
+ gated_delta_modules = _iter_gated_delta_modules(model)
+
+ # Idempotency: an already fully-patched model stays active without re-validation.
+ if (
+ getattr(model, "_unsloth_varlen_forward_wrapped", False)
+ and gated_delta_modules
+ and all(getattr(m, "_unsloth_varlen_wrapped", False) for m in gated_delta_modules)
+ ):
+ return True
+
+ reason = _hybrid_varlen_kernels_available(gated_delta_modules)
+ if reason is not None:
+ return _hybrid_reject(reason)
+
+ # Transactional: every module validated above, now wrap each and stash originals.
+ for module in gated_delta_modules:
+ if getattr(module, "_unsloth_varlen_wrapped", False):
+ continue
+ conv_orig, scan_orig = module.causal_conv1d_fn, module.chunk_gated_delta_rule
+ module._unsloth_varlen_orig_conv = conv_orig
+ module._unsloth_varlen_orig_scan = scan_orig
+
+ @wraps(conv_orig)
+ def conv_fn(
+ *args,
+ _orig = conv_orig,
+ _module = module,
+ **kwargs,
+ ):
+ varlen = getattr(_module, "_unsloth_varlen", None)
+ if varlen is not None:
+ _module._unsloth_varlen_conv_hit = True # runtime dispatch handshake
+ if kwargs.get("seq_idx") is None:
+ kwargs["seq_idx"] = varlen[1]
+ return _orig(*args, **kwargs)
+
+ @wraps(scan_orig)
+ def scan_fn(
+ *args,
+ _orig = scan_orig,
+ _module = module,
+ **kwargs,
+ ):
+ varlen = getattr(_module, "_unsloth_varlen", None)
+ if varlen is not None:
+ _module._unsloth_varlen_scan_hit = True
+ if kwargs.get("cu_seqlens") is None:
+ kwargs["cu_seqlens"] = varlen[0]
+ return _orig(*args, **kwargs)
+
+ module.causal_conv1d_fn = conv_fn
+ module.chunk_gated_delta_rule = scan_fn
+ module._unsloth_varlen = None
+ module._unsloth_varlen_wrapped = True
+
+ # Refresh the boundary stash on the outermost forward (once per step, outside
+ # gradient-checkpoint recompute, so it stays valid for recomputed inner
+ # forwards). Read from both positional and keyword args via the bound signature.
+ if not getattr(model, "_unsloth_varlen_forward_wrapped", False):
+ forward_orig = model.forward
+ try:
+ forward_sig = inspect.signature(forward_orig)
+ except (TypeError, ValueError):
+ forward_sig = None
+
+ @wraps(forward_orig)
+ def forward_with_varlen(*args, **kwargs):
+ try:
+ bound = dict(kwargs)
+ if forward_sig is not None and args:
+ bound.update(forward_sig.bind_partial(*args).arguments)
+ varlen = _hybrid_varlen_metadata(bound)
+ except Exception:
+ varlen = None
+ first_pack = varlen is not None and not getattr(
+ model,
+ "_unsloth_varlen_handshake_done",
+ False,
+ )
+ for module in gated_delta_modules:
+ module._unsloth_varlen = varlen
+ if first_pack:
+ module._unsloth_varlen_conv_hit = False
+ module._unsloth_varlen_scan_hit = False
+ out = forward_orig(*args, **kwargs)
+ # Runtime dispatch handshake: on the first packed forward, confirm BOTH
+ # boundary kernels ran for EVERY module. seq_idx (conv) and cu_seqlens
+ # (scan) are both load-bearing, so a partial/absent dispatch (a future
+ # version no longer routing through self.) leaves cross-sequence
+ # contamination. The batch is already flattened with no padded recovery,
+ # so abort before loss/backward rather than train on corrupted data.
+ if first_pack:
+ model._unsloth_varlen_handshake_done = True
+ missing = [
+ type(m).__name__
+ for m in gated_delta_modules
+ if not (
+ getattr(m, "_unsloth_varlen_conv_hit", False)
+ and getattr(m, "_unsloth_varlen_scan_hit", False)
+ )
+ ]
+ if missing:
+ for m in gated_delta_modules:
+ m._unsloth_varlen = None
+ _hybrid_reject("varlen conv/scan not both dispatched (dispatch changed?)")
+ raise RuntimeError(
+ "Unsloth: experimental hybrid packing cannot continue because the "
+ "varlen conv/scan wrappers were not both invoked for "
+ f"{sorted(set(missing))}. Unset UNSLOTH_EXPERIMENTAL_HYBRID_PACKING "
+ "to train these models on the padded path."
+ )
+ return out
+
+ model.forward = forward_with_varlen
+ model._unsloth_varlen_forward_wrapped = True
+ return True
+
+
def get_packed_info_from_kwargs(
kwargs: dict, device: torch.device
) -> Optional[Tuple[torch.Tensor, torch.Tensor, int]]:
From 3ab8dce97a95923b2b4e6741e9df9cba1a9baaca Mon Sep 17 00:00:00 2001
From: Daniel Han
Date: Mon, 20 Jul 2026 00:58:52 -0700
Subject: [PATCH 045/271] install: let UNSLOTH_TORCH_INDEX_FAMILY / _URL
override CUDA wheel detection (#6692)
* install: let UNSLOTH_TORCH_INDEX_FAMILY / _URL override CUDA wheel detection
get_torch_index_url (and the studio-update mirror _detect_cuda_torch_index_url)
chose the torch wheel family solely by probing the host GPU, with no override.
In a headless / container / CI build the host driver is visible via the
/proc/driver/nvidia/gpus fallback but nvidia-smi cannot report a CUDA version,
so the function fell back to its cu126 default and installed the wrong wheels
(e.g. a cu128 image got cu126 torch).
Add an explicit override checked before any probing, in both the shell installer
and the Python studio-update path:
- UNSLOTH_TORCH_INDEX_URL full index URL, used verbatim (wins)
- UNSLOTH_TORCH_INDEX_FAMILY family (cpu, cu128, rocm6.4, ...) appended to the
mirror base (UNSLOTH_PYTORCH_MIRROR still honoured)
This matches how the published GPU images select CUDA -- vLLM and SGLang take the
CUDA version from an explicit build ARG rather than detecting it, and the Unsloth
Docker base image already pins the cu128 index directly. Desktop installs are
unchanged: with no override set, detection runs exactly as before.
Adds test_get_torch_index_url.sh cases for the override (family, full URL,
precedence, mirror base, trailing-slash strip, empty-ignored).
* install: make the torch-index override authoritative across ROCm paths
Address review feedback on the override added in this PR so a pinned index is
honoured everywhere, not just in get_torch_index_url:
- Skip the WSL ROCm bootstrap (root privilege + large downloads, probes
/dev/dxg) when UNSLOTH_TORCH_INDEX_URL / _FAMILY is set; it previously ran
before the override was consulted.
- Skip the Radeon/Strix rerouting (which re-probes the GPU and overwrites the
resolved URL with repo.radeon.com / repo.amd.com) when the index is pinned, so
an explicit ROCm override (e.g. UNSLOTH_TORCH_INDEX_FAMILY=rocm6.4) is kept.
- install_python_stack.py: derive _TORCH_BACKEND from the override when
UNSLOTH_TORCH_BACKEND is unset (standalone studio update), so _ensure_rocm_torch
/ _ensure_cuda_torch repair to the requested family instead of re-detecting.
- Strip ALL leading/trailing slashes in the shell override to match the Python
side (avoids 404s on strict pip proxies).
Adds test cases for double-slash and leading/trailing-slash overrides.
* install: honor pinned torch index in CUDA/ROCm repair paths
Follow-up to the override work in this PR: the get_torch_index_url / install.sh
reroute already respect a pinned UNSLOTH_TORCH_INDEX_URL / _FAMILY, but the
Python repair helpers in install_python_stack.py still re-probed the GPU and
could overwrite the pinned family. Make the pin authoritative there too:
- _ensure_cuda_torch: an explicit cu* pin commits to CUDA wheels, so repair a
ROCm-poisoned venv even when no NVIDIA GPU is visible here (headless /
container / CI cross-install), instead of bailing on the GPU-presence gate.
- _ensure_rocm_torch: skip the AMD per-gfx (Strix) reroute when a ROCm index is
pinned, and in the generic reinstall path install from the pinned URL verbatim
rather than re-detecting the host ROCm version. gfx*/rocm7.2 indexes serve
torch 2.11+, so select the 2.11 package specs for a gfx leaf.
- install.sh: raise the torch constraint to 2.11 for */gfx* indexes too, matching
rocm7.2, so a pinned full-URL/family override that returns early keeps a valid
constraint.
Add _explicit_torch_index_url / _explicit_rocm_torch_index_url helpers and tests
covering the no-GPU CUDA pin repair and the explicit gfx index honored verbatim.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: honor torch-index override on the Windows installers too
The pinned-index work landed for install.sh and install_python_stack.py, but the
Windows installers still picked the wheel index from GPU probing. Extend the same
UNSLOTH_TORCH_INDEX_URL / _FAMILY contract so a pinned index wins on every platform:
- install.ps1: Get-TorchIndexUrl returns the pinned URL/family before nvidia-smi
probing; the AMD ROCm reroute is skipped when the index is pinned, so an explicit
cpu/cu* pin on an AMD host is not overwritten.
- studio/setup.ps1: add shared Get-PinnedTorchIndexUrl / Get-TorchIndexLeaf helpers;
the stale-venv check, the install selection and the AMD reroute all honor the pin,
and the CPU/CUDA install pulls from the resolved index URL.
- tests: parity test that all four installers read both override vars and the two
Windows installers gate the AMD reroute on the pinned flag.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: complete pinned-index handling for ROCm/Windows edge cases
Follow-ups to the override work flagged in review:
- install.ps1: a pinned gfx*/rocm>=7.2 index previously skipped the AMD reroute
that sets the torch>=2.11 floor, so the generic install used torch>=2.4,<2.11
and could resolve the known-bad _grouped_mm wheel. Route a pinned ROCm index
through the ROCm install path with the 2.11 floor + companions, and guard the
companion-spec lookup so a skipped reroute block cannot null-deref.
- studio/setup.ps1: the stale-venv check compared the installed flavor (cuXXX/cpu,
with +rocm misread as cpu) against the raw pinned leaf (gfx1151 / rocm6.4), so a
correct pinned ROCm venv was always marked stale. Classify +rocm wheels as the
generic 'rocm' flavor and normalize a pinned rocm*/gfx* leaf to 'rocm' before
comparing (cu* stays specific so cu126-vs-cu128 still rebuilds).
- install_python_stack.py: _ensure_cuda_torch now also reinstalls from a pinned
CUDA index when the venv carries a CPU wheel (headless CPU-venv-to-CUDA
cross-install via 'studio update'), not only when it finds a ROCm build.
- tests: parity assertions already cover all four installers honoring the override.
* install: finish pinned ROCm/CUDA edge cases on Windows + repair path
Follow-ups to the previous round:
- studio/setup.ps1: a pinned gfx*/rocm>=7.2 index now routes through the ROCm
install path with the 2.11 floor + companions (it previously fell through to the
CUDA branch with bare torch/torchvision/torchaudio against the ROCm index). The
CPU/CUDA fallback index is forced to the CPU wheel index when a ROCm index is
active, so a failed pinned-ROCm install does not retry the ROCm mirror.
- studio/setup.ps1: the stale-venv check no longer treats an unrecognized pinned
URL leaf (e.g. a PEP 503 mirror ending in /simple) as a torch flavor tag, which
was marking a correct venv stale; cu*/cpu/rocm/gfx leaves are still compared.
- install.ps1: the post-failure CPU fallback uses an explicit CPU index instead of
, which for a pinned ROCm index was the ROCm mirror itself (so the
'fallback' just retried the failing index and aborted the installer).
- install_python_stack.py: _ensure_cuda_torch now also reinstalls when the venv's
CUDA family differs from a pinned one (installed cu126 vs pinned cu128), not only
CPU->CUDA; the probe reports the installed cuXXX tag for the comparison.
* install: keep the ROCm to CPU fallback install inside the retry-helper window
The pinned-ROCm CPU fallback computes an explicit CPU index, but the comment
explaining why it cannot reuse $TorchIndexUrl pushed the actual
Invoke-InstallCommandRetry / --force-reinstall call more than 600 chars past the
"ROCm PyTorch install failed" message, so test_pr5940_followups's window check
no longer saw the retry helper. Move the CPU-index computation and its comment
above the failure substep so the retrying force-reinstall stays adjacent to the
message. No behavior change: same explicit CPU index, same retry, same
--force-reinstall.
* install: address #6692 review round 5 (ROCm/CPU pin edge cases)
setup.ps1:
- Stale-venv check: treat an AMD/ROCm host (HasROCm or a resolved gfx arch) with
no explicit pin as expecting "rocm", not "cpu", so a healthy +rocm venv is not
flagged stale (which made installer-managed setup exit and direct update rebuild).
- Pinned-ROCm install failure now routes into the force-reinstall CPU branch:
CuTag stays the rocm/gfx leaf on failure, so the condition also checks
ROCmCpuFallback; otherwise the CUDA branch installed from the CPU index without
--force-reinstall and kept the partial ROCm torch.
- Explicit ROCm pin compare no longer collapses gfx*/rocm* to a generic "rocm":
it compares the +rocmX.Y version (and the torch 2.11 line for gfx pins) so
changing the pinned family (e.g. rocm6.4 -> gfx1151) rebuilds and applies it.
install_python_stack.py:
- _ensure_rocm_torch: an explicit ROCm wheel-index pin now bypasses the
NVIDIA-present / no-AMD-GPU / unreadable-ROCm gates (headless/container/CI
cross-install), mirroring the explicit-CUDA-pin bypass in _ensure_cuda_torch.
- Add _ensure_cpu_torch: an explicit CPU pin (FAMILY=cpu or /cpu URL) now has a
repair path that reinstalls CPU torch over an existing CUDA/ROCm build on a
standalone update (which skips install.sh's flavor enforcement).
install.sh:
- Pin torchvision/torchaudio companions alongside torch for the rocm7.2 / per-gfx
index and the Strix reroute (those AMD indexes publish companions independently
and a bare name can resolve a torch-2.12-built wheel, an ABI mismatch).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch-index override: classify CUDA pin by leaf; trim blank shell overrides
_ensure_cuda_torch only overrode the NVIDIA-presence gate for *any* pinned index,
so a non-CUDA mirror URL (or a ROCm/CPU pin) on a non-NVIDIA host with ROCm torch
could force a CUDA reinstall over a working ROCm venv. Add
_explicit_cuda_torch_index_url() (leaf cu*), matching the ROCm/CPU helpers, and
gate on it instead.
install.sh::get_torch_index_url treated a whitespace-only UNSLOTH_TORCH_INDEX_URL
/ _FAMILY as authoritative (yielding an invalid index), unlike the Python .strip()
and PowerShell IsNullOrWhiteSpace paths; trim leading/trailing whitespace first.
* install: honor pinned torch index over CVD/GPU gates and fix leaf-based ROCm classification
- install_python_stack.py: an explicit cu* pin now clears the CUDA_VISIBLE_DEVICES
empty/-1 hide gate as well as the NVIDIA-presence gate, so
CVD=-1 UNSLOTH_TORCH_INDEX_FAMILY=cu128 studio update repairs to CUDA wheels
(parity with install.sh's get_torch_index_url override, which skips all GPU
probing). Unpinned CVD=-1 still skips.
- install_python_stack.py: _ensure_cpu_torch installs the bounded _CPU_TORCH_PKG_SPEC
instead of a bare torch/torchvision/torchaudio trio; the /cpu index now also
serves torch 2.11+, which is outside the supported <2.11 range.
- install.sh: the torch>=2.11 constraint case matches the index leaf (rocm7.2|gfx*)
instead of the whole URL, so a mirror base path containing a gfx/rocm7.2 segment
with a cu*/cpu family is not false-matched onto the 2.11 line.
- setup.ps1: the stale-venv check expects rocm torch only for arches the install
path maps to a repo.amd.com wheel index; an unmapped/unreadable arch installs
CPU, so a correct CPU venv is no longer marked stale.
- Tests for each of the above.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: tighten pinned torch-index override edge cases
- install.sh: trim whitespace-only UNSLOTH_TORCH_INDEX_URL/_FAMILY before the
_torch_index_pinned guard, matching get_torch_index_url, so a blank override no
longer skips the WSL bootstrap and Radeon/Strix reroutes while detection still
picks the normal index.
- install.sh / install.ps1 / setup.ps1 / install_python_stack.py: force the torch
2.11 floor only for the gfx families with the <2.11 _grouped_mm bug (gfx120X-all,
gfx1151, gfx1150). A pinned override to gfx110X-all/gfx90a/gfx908 stays on the
default range, matching the automatic AMD path.
- install_python_stack.py _ensure_cuda_torch: treat an untagged CUDA build under a
CUDA pin as a family mismatch (reinstall), and match cuXXX pins narrowly (cu +
digits) so a custom/current mirror leaf no longer forces CUDA over a CPU/ROCm venv.
- install_python_stack.py _ensure_rocm_torch: reinstall when an explicit ROCm pin
names a different ROCm family than the already-installed ROCm torch (the ROCm
analogue of the CUDA cuXXX mismatch repair).
Adds tests for each case.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: fix second-order edge cases in pinned torch-index ROCm/CUDA handling
Parse the ROCm torch probe positionally so an empty HIP marker is kept:
CPU/CUDA torch no longer reads as HIP, so the ROCm reinstall is not skipped.
Emit one "|" line (like the CUDA probe) for a robust parse.
Limit the gfx torch 2.11 expectation to the install allowlist
(gfx120X-all/gfx1151/gfx1150). A pinned gfx110X-all/gfx90a/gfx908 index stays
on the default <2.11 specs, so a correct 2.10+rocm wheel is no longer judged a
mismatch and force-reinstalled every update.
Distinguish an AMD per-arch wheel (three-part +rocmA.B.C) from a generic
pytorch.org wheel (two-part +rocmA.B): a gfx per-arch pin over a generic 2.11
wheel now reinstalls the per-arch wheel, while an already-installed per-arch
wheel is not re-flagged (no reinstall loop).
Mirror all of the above in setup.ps1 via new Test-RocmGfx211Leaf /
Test-CudaFamilyLeaf / Get-RocmPinStaleTags helpers, reused by both the
install-spec path and the stale-venv check so they cannot diverge again.
Require a digit after "cu" (^cu[0-9]) in setup.ps1, install.ps1 and install.sh
so a mirror leaf like /custom or /current is not branded CUDA and does not
rebuild the venv every run.
Add tests: CPU/CUDA probe -> has_hip_torch False; gfx110X-all pin + 2.10 wheel
not stale; gfx1151 pin + generic 2.11 wheel stale; gfx1151 pin + per-arch wheel
not stale; /custom and /current not CUDA; plus cross-language allowlist and
cu-digit parity guards, and a PowerShell unit test for the new setup.ps1 helpers.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix ROCm/gfx pin case normalization, ROCm-tag requirement, and CUDA-leaf classification
Normalize torch-index leaves to lowercase before the gfx*/rocm*/cu* allowlist
matches so the canonical gfx120X-all (capital X) gets the torch 2.11 floor in
install.sh (leaf, flavor and repairable helpers). Require an installed +rocm
local tag before a rocmX.Y or non-2.11 gfx pin is judged satisfied in
setup.ps1 Get-RocmPinStaleTags and the Python _rocm_pin_family_mismatch, so an
untagged CPU/CUDA wheel never leaves the pin unapplied. Classify a leaf as CUDA
only via ^cu[0-9]: the Python _TORCH_BACKEND derivation now uses
_is_cuda_family_leaf, and install.sh brands cuda only on cu[0-9]* (unset on an
unknown /current /custom mirror leaf) so the stack probes the GPU instead of
skipping ROCm repair. Add bash, Python and PowerShell tests for capital
gfx120X-all floor, current/custom not-cuda, and untagged-wheel ROCm pins.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: converge torch-index pin detection via a per-venv marker
Introduce a torch-index MARKER that records the exact wheel --index-url used
after each successful torch install, so `unsloth studio update` / repair makes
the "did the pinned index change?" decision by an EXACT string compare rather
than inferring it from the wheel +rocm/+cu version tag. The tag cannot encode
the AMD per-arch gfx family (two 2.11 gfx indexes both install +rocm7.13.0), so
the tag heuristic missed a gfx1151 -> gfx120X-all switch and a custom-URL swap.
Marker path is per-venv (.unsloth-torch-index), one line = the resolved index
URL, written atomically (temp + rename). Path, format and normalization are
shared across all four installers (install.sh, install_python_stack.py,
setup.ps1, install.ps1).
- Reapply gfx pins on a per-arch target change: the marker's exact compare
reinstalls when the pinned index differs, even when both wheels share a tag.
- Honor custom ROCm URL pins during repair: an explicit index whose leaf is not
rocm/gfx/cu/cpu (e.g. simple, current) now reinstalls torch VERBATIM from the
pin when it differs from the marker ("URL wins verbatim").
- Align the KNOWN-2.11 rocm/gfx set to exactly rocm7.2 plus the gfx allowlist
gfx120x-all/gfx1151/gfx1150 in every language; stop treating an unknown newer
rocm (rocm7.3, which does not exist) as the 2.11 line speculatively.
Backward compatible: with no marker (old venvs, torch installed out-of-band) the
existing +rocm/version-tag heuristics still decide, and a matching marker never
reinstall-loops. A cu128 CUDA pin stays a CUDA pin; custom and current leaves are
not CUDA. Adds marker tests (py/sh/ps) plus cross-installer parity checks.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: keep the torch-index marker additive to flavor validation
Three narrow fixes in the marker-based stale-venv detection:
- setup.ps1: a matching marker no longer overwrites the detected installed
flavor. The marker compare is now an additional rebuild trigger, so a stale
wheel (torch swapped to a +cpu build while the marker still records a cuXXX
pin) is still caught by the flavor check instead of being masked as up to date.
- setup.ps1: a supported AMD arch carrying CPU torch is no longer marked stale
and wiped. The downstream AMD Windows ROCm override upgrades CPU torch to ROCm
in place, so wiping first would delete the venv and abort with "Virtual
environment not found". Only a genuinely wrong CUDA wheel still rebuilds.
- install.sh: the Radeon --find-links path records its repo.radeon.com base in
the marker instead of the generic pytorch.org ROCm fallback index, so a later
pin to that generic family correctly reinstalls rather than comparing equal.
Mirrors install.ps1/setup.ps1, which already record the real AMD index.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: honor custom pins and repair pinned venvs in place
Four follow-ups to the torch-index marker work:
- install_python_stack.py: _ensure_cuda_torch/_ensure_rocm_torch now bail when an
explicit custom-index pin names no known torch family, so a verbatim URL override
(a private/simple mirror) is not clobbered by auto-detected CUDA/ROCm wheels
before _ensure_verbatim_torch_index applies it.
- install_python_stack.py: the ROCm marker is additive, not a substitute -- a
matching marker still runs the family/version check so a wheel swapped after the
marker was written is caught. Mirrors setup.ps1.
- setup.ps1: a stale venv under an explicit pin, whose torch still imports, is
repaired in place (force-reinstall torch from the pin in the dependency pass)
instead of wiped. The wipe path only delegates to install.ps1, so on a direct
update it stranded the user at "Virtual environment not found" instead of
applying the new pin. A broken venv or unpinned drift still wipes/delegates.
- install.ps1: when a pinned ROCm install fails over to a CPU base, the marker now
records the CPU index actually used instead of the ROCm pin, so the next managed
setup does not see CPU torch under a ROCm pin and abort as stale.
* setup.ps1: keep the ROCm CPU-fallback force line the pr5940 test guards
5c93ffd4 folded the pin-change force-reinstall into the ROCm CPU-fallback
condition on one line, so the exact literal that test_pr5940_followups.py checks
(if ($ROCmCpuFallback) { $cpuForce = @("--force-reinstall") }) no longer appeared
and the test failed. Split the two conditions into separate if lines: the ROCm
fallback line is restored verbatim and the pin-change force is its own line. Both
still set $cpuForce to the array, so @splat passes one arg.
* install: honor exact CUDA/custom index URL pins in the torch-index marker
Address three Codex review findings on the torch-index marker mechanism:
- install.sh: after the ROCm CPU repair reinstalls torch from the generic
$TORCH_INDEX_URL, record that as the marker source. A Radeon --find-links
install set _TORCH_MARKER_INDEX_URL to its repo.radeon.com base earlier, so
leaving it made the marker misreport Radeon wheels and a later Radeon pin would
compare equal and skip a needed reinstall.
- install_python_stack.py: _ensure_cuda_torch now consults the exact-URL marker
(_marker_pin_mismatch) when the installed +cuXXX tag matches the pinned leaf,
so a same-leaf CUDA mirror change (official cu128 to an internal cu128 mirror)
is reinstalled and re-recorded instead of skipped.
- _normalize_index_url / _normalize_family_leaf (install.sh, setup.ps1,
install_python_stack.py): lowercase only KNOWN wheel-family leaves (rocm/gfx/
cpu/cuXXX) so gfx120X-all still matches gfx120x-all, while a custom
(unknown-family) leaf keeps its case so a verbatim URL pin like /Current does
not compare equal to /current. Tests updated to assert the refined behavior.
* install: fix 3 torch-index marker edge cases (CPU mirror pin, Radeon leaf, migrated venv)
Addresses three review findings on the torch-index override path:
1. CPU index URL change on an already-CPU venv. _ensure_cpu_torch returned
early whenever torch was already a CPU build, so a standalone update that
moved the pin (official /cpu -> a private UNSLOTH_PYTORCH_MIRROR /cpu, same
+cpu tag) never reinstalled. It now consults the exact-URL marker and
reinstalls only when _marker_pin_mismatch reports a different index,
mirroring the CUDA/ROCm same-family handling. A matching marker (or none)
still leaves CPU torch untouched, so there is no reinstall loop.
2. Radeon find-links directory misclassified as a pip ROCm family. A
repo.radeon.com/.../rocm-rel-7.2.1 leaf starts with "rocm" but is a
find-links listing, not a pip --index-url. The old startswith(("rocm",
"gfx")) test routed it into a --index-url reinstall that fails against
find-links. New _is_pip_rocm_family_leaf gates on ^rocm\d / gfx (matching
install.sh's rocm[0-9]* and setup.ps1's ^(rocm[0-9]|gfx)), so a Radeon URL
routes to the verbatim/marker path instead.
3. Migrated venv rewriting its marker to a pin it did not install. install.sh
and install.ps1 write the marker unconditionally, so a migration that
preserves existing torch recorded the newly requested pin and a later
update then found a matching marker and skipped the reinstall the pin
needs (e.g. a per-arch gfx1151 -> gfx120X-all switch, identical +rocm tag).
Both now track _TORCH_INSTALLED_THIS_RUN and write the marker only when
torch was actually installed or repaired this run.
Also add Get-NormalizedFamilyLeaf to the setup.ps1 helper-extraction list in
test_torch_index_marker.ps1 (it was added to setup.ps1 and the shell test in an
earlier round but missed here) and add two unit tests covering findings 1 and 2.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: keep pinned torch repairs on the pinned index
Two fixes for explicit index pins (UNSLOTH_TORCH_INDEX_FAMILY / _URL):
1. install_python_stack.py's repair paths ran uv without clearing the
inherited uv index env vars. uv resolves the default index (--index-url
or --default-index) at the LOWEST priority, so a UV_INDEX or
UV_EXTRA_INDEX_URL mirror in the environment won for any package it
served: a cu128-pinned repair could install torch from the mirror and
then record the cu128 marker it never used. Verified empirically: with
UV_EXTRA_INDEX_URL=.../cu126 exported, uv pip install torch
--index-url .../cu128 resolves torch 2.13.0+cu126. Strip the four uv
index env vars for pinned-index commands only, mirroring the gate
install.sh, install.ps1 and setup.ps1 already have; non-pinned installs
keep the user's mirror.
2. install.ps1 routed any pinned leaf matching rocm* through the ROCm
--default-index path, so a custom find-links leaf like rocm-rel-7.2.1
was treated as a PEP 503 ROCm index and could silently fall back to CPU
torch on resolution failure. Require a digit after rocm, matching
install.sh's rocm[0-9]* and install_python_stack.py's ^rocm\d.
Adds parity + unit tests for both (11 new tests).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: keep pinned repairs off UV_TORCH_BACKEND and narrow setup.ps1's rocm pin match
Round 2 of the pinned-index hardening:
1. _build_uv_cmd converted UV_TORCH_BACKEND into --torch-backend before the
new env isolation could act, and uv's torch backend redirects torch
resolution to its own per-backend index even when --index-url is given
(verified: a cu128-pinned dry run with UV_TORCH_BACKEND=cpu resolves
torch 2.13.0+cpu). Pinned-index commands now never receive the flag and
UV_TORCH_BACKEND joins the stripped env vars, so uv cannot re-read it.
2. setup.ps1's pinned reroute had the same bare rocm* glob install.ps1 had:
a custom find-links leaf like rocm-rel-7.2.1 was routed through the ROCm
--index-url path instead of the verbatim unknown-pin path. Now requires
a digit after rocm, matching install.ps1, install.sh and
_is_pip_rocm_family_leaf.
3. The marker test's case-normalization checks used -eq, which is
case-insensitive in PowerShell, making them vacuous, and the unknown-leaf
expectation was written lowercased while the implementation deliberately
preserves custom-leaf case. Tightened to -ceq with the case-preserving
expected value.
Adds unit + parity tests for 1 and 2 (5 new tests).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: extend the pinned-index guards to every remaining surface
Round 3 of the pinned-index hardening, closing the same holes on the
surfaces the earlier rounds missed:
1. install.sh's pinned-install env scrub now clears UV_TORCH_BACKEND (uv's
torch backend redirects torch resolution to its own per-backend index
even against --default-index), and both PowerShell wrappers clear it in
their pinned-install scrubs, matching install_python_stack.py.
2. setup.ps1's marker stale check still classified any rocm* leaf as a
PyTorch ROCm family while the install selection is digit-gated, so a
custom rocm-current / rocm-rel-7.2.1 pin stale-compared as
not-rocm vs rocm and force-reinstalled on every studio update. The
stale check now uses the same ^rocm\d gate.
3. install_python_stack.py's pinned-command scrub also strips
PIP_EXTRA_INDEX_URL for the pip fallback: pip adds the env extra index
in addition to --index-url, so an inherited mirror could satisfy torch
off the pin while the marker recorded the pinned URL. PIP_INDEX_URL
needs no strip since the explicit --index-url flag overrides it.
Parity + unit tests extended (4 new tests).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: scrub find-links and carry the pinned scrub through pip fallbacks
Round 4 of the pinned-index hardening:
1. UV_FIND_LINKS joins every pinned-install scrub (install.sh, install.ps1,
setup.ps1, install_python_stack.py): uv's --find-links locations can
satisfy torch off the pinned index the same way an extra index does.
2. setup.ps1's Fast-Install restored the scrubbed vars in its finally
BEFORE the pip fallback ran, and never touched the pip env vars at all,
so a failed uv attempt fell back to python -m pip with an inherited
PIP_EXTRA_INDEX_URL / PIP_FIND_LINKS able to win over the pinned
--index-url. The scrub now wraps the whole function (uv attempt + pip
fallback) and includes the pip vars; restore happens after both.
3. install_python_stack.py's scrub also strips PIP_FIND_LINKS for its own
pip fallback, completing the PIP_EXTRA_INDEX_URL fix from round 3.
Parity tests extended (2 new tests).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: digit-gate rocm leaves in marker normalization and ROCm side effects
Round 5 of the pinned-index hardening (three custom-rocm-leaf edge cases):
1. _normalize_family_leaf lowercased every leaf starting with rocm, so a
custom mirror leaf like rocm-Current compared equal to its lowercase form
and a case-only pin change was skipped. URL paths can be case-sensitive.
The rocm prefix is now digit-gated (rocm[0-9]*, matching
_is_pip_rocm_family_leaf) in install.sh, setup.ps1 and
install_python_stack.py, so only true family leaves (rocm7.2) are
lowercased; a custom rocm-* leaf keeps its case.
2. setup.ps1 Test-MarkerPinMismatch compared normalized URLs with -ne, which
is case-insensitive in PowerShell, so a case-only marker change (Simple
vs simple) was treated as matching and the reinstall skipped. Now -cne.
3. install.sh gated the AMD bitsandbytes install and the "repair ROCm torch"
--default-index reinstall on a bare whole-URL rocm glob, so a custom
CPU/CUDA/private index whose leaf merely starts with rocm (rocm-current)
was force-repaired from the wrong ROCm-only path whenever torch.version.hip
was empty. Both now gate on _torch_index_is_rocm_family, computed once from
the digit-gated leaf (rocm[0-9]*/gfx*).
Tests: 4 new parity assertions plus 2 case-sensitivity marker checks.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: apply an explicit custom torch-index pin on the first update
Round 6: an explicitly-set custom (unknown-family) UNSLOTH_TORCH_INDEX_URL
was silently ignored on the first `studio update` of a venv that predates
the marker feature, on both platforms, because the no-marker case was
treated as "do nothing" and the version-tag heuristics cannot judge an
unknown leaf.
1. install_python_stack.py _ensure_verbatim_torch_index now reinstalls
verbatim when the marker is ABSENT (None), not only when it differs, and
short-circuits only when the marker already records this exact pin. It
then writes the marker, so every later update is a no-op. A user who did
not set the override gets pin=None and is untouched, so an out-of-band
torch install is never clobbered.
2. setup.ps1: for an unknown-family pin on a marker-less venv the stale-venv
check now sets PinChangedForceReinstall so the torch block reinstalls in
place from the pin. It deliberately does NOT set shouldRebuild, which
would wipe the venv and strand a direct `studio update`.
3. setup.sh (the Linux `studio update` entry point) skipped
install_python_stack.py entirely when unsloth was already current, so the
marker-driven reinstall (both the verbatim custom pin and the cu/rocm
flavor and family-change repair, e.g. gfx1151 to gfx120X-all) never ran.
It now forces the dependency pass when a torch-index pin env var is set;
the pass is idempotent and no-ops when the marker already matches. This
mirrors setup.ps1's stale-venv pre-check.
Tests: 3 new parity assertions.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* test: expect first-update reinstall for a no-marker custom index pin
Follow-up to d671d8fb2: _ensure_verbatim_torch_index now applies an
explicit unknown-family URL pin verbatim on the first update when the
marker is absent (instead of no-op), so the old
test_verbatim_custom_url_no_marker_is_noop assertion was stale. Rewritten
as test_verbatim_custom_url_no_marker_reinstalls_once: asserts the one
verbatim reinstall from the pinned URL, that the marker is written, and
that a second call with the pin still set is idempotent (no reinstall
loop).
* install: gate the pinned update pass on the marker and record a pin baseline
Round 8, two follow-ups to the round-6 first-update pin fix:
1. setup.sh forced the full dependency pass on EVERY `studio update` while a
torch-index pin stayed exported, even after the marker already recorded the
same pin, turning quick updates into the expensive pass every time. It now
probes install_python_stack.py --torch-pin-needs-apply (which reuses the
exact marker normalization) and forces the pass only when the pin is not yet
applied (marker absent or different); an already-applied persistent pin keeps
the fast path. A probe error fails safe toward running the pass. setup.ps1
gets the same probe in its fast path for parity.
2. A known-family full-URL pin on a venv predating the marker (e.g. an installed
cu128 build and UNSLOTH_TORCH_INDEX_URL pointing at a same-family mirror) left
the marker absent forever: the _ensure_* helpers deliberately do not force a
multi-GB reinstall of identical-family wheels on an old venv, so nothing
recorded the pin and every update re-entered the pass. _record_torch_index_pin_baseline
now records the resolved pin as a baseline after the ensure sequence when the
family already matches and no marker exists, so the pin is tracked (a later
genuine change is detected and applied) and the update loop is broken, without
the redundant reinstall.
Tests: 3 new baseline unit tests, 4 new parity assertions, and the CLI probe.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* setup.sh: keep the pin probe's exit 1 from killing the update under set -e
The --torch-pin-needs-apply probe deliberately exits 1 for the common
steady-state answer (pin already recorded, keep the fast path), but it ran
as a bare command under set -euo pipefail, so the whole studio update
aborted before the exit code was even captured. Absorb the status with
|| _PIN_NEEDS_APPLY=$? and pre-seed 0 so all three outcomes route as
documented: 0 runs the pass, 1 keeps the fast path, anything else fails
safe into the pass. Parity test asserts the guard.
* install: strip pin credentials, disable uv config discovery, bound verbatim installs
Four verified fix groups from a 12-reviewer audit of the torch-index
override feature, each reproduced before fixing:
1. Credential persistence: all four marker writers stored the raw pin URL,
so an authenticated pin (https://user:token@mirror/simple) persisted its
credentials in .unsloth-torch-index (mode 0644 under a default POSIX
umask) and install_python_stack.py printed pin URLs verbatim in repair
messages. Userinfo is now stripped before persisting and in every
log/substep that interpolates a pin, via lockstep helpers
(_strip_index_url_credentials in install.sh / install_python_stack.py,
Remove-IndexUrlCredentials in install.ps1 / setup.ps1). The three
normalizers strip too, so an OLD marker that already carries credentials
still compares equal to the same pin: no reinstall loop on upgrade.
Query strings deliberately stay in the marker; two indexes distinguished
only by query must not compare equal.
2. uv configuration discovery beat the explicit pin: with a discovered
uv.toml declaring torch-backend = "cpu" or a [[index]] entry, uv 0.10.12
resolves torch 2.13.0+cpu against an explicit --index-url/.../cu126 pin;
UV_NO_CONFIG=1 restores +cu126 (reproduced both ways). The pinned-install
scrub in all four installers now sets UV_NO_CONFIG=1 and drops
UV_CONFIG_FILE.
3. The verbatim custom-index update path installed a bare, unconstrained
torch trio while fresh installs from the same unknown-leaf pin apply the
supported range; _ensure_verbatim_torch_index now installs the bounded
trio spec, closing the fresh-vs-update asymmetry.
4. Query-bearing pins (.../cu128?token=x) classified by raw leaf split and
force-reinstalled on every update (the installed cu128 never equals
cu128?token=x). Query/fragment are now stripped before leaf
classification in all four implementations; the marker comparison keeps
the query per (1).
Rejected after verification (no change): the pin-baseline record cannot
produce a wrong later decision (every pin change still mismatches and
reinstalls from the new pin); the venv temp-file symlink scenarios require
an attacker who already owns the environment; pathological inputs like
" / cu128 / " have no realistic caller and fail loudly.
Parity, stack, rocm-support, marker (sh + ps1), pin-stale, index-url and
flavor suites all pass (455 python + full shell/ps1 batteries).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: harden custom-pin repair against clobber, broken torch, and pip config
Four follow-ups to the pinned-index audit fixes:
1. setup.ps1 routed an unknown-leaf custom pin through the CUDA branch with
a bare torch trio while install.ps1 (fresh) and the Python verbatim path
bound the supported range; the pinned unknown-leaf route now applies the
same torch>=2.4,<2.11.0 bound. Known cu* leaves and unpinned runs are
unchanged.
2. The final torch safety pass could not repair a clobbered unknown-family
pin: intermediate dependency steps can pull torch from PyPI (the pass
exists for exactly that reason), but the verbatim helper short-circuited
on marker==pin and no flavor tag exists to probe. The helper now keeps a
per-run snapshot of the installed trio (taken after a verbatim reinstall
or on the first matching-marker pass) and reinstalls from the pin when
the final pass sees the trio drifted. Probe failure skips the
comparison; a reinstall refreshes the snapshot, so no loop.
3. _record_torch_index_pin_baseline could freeze a known-family pin as
applied on a venv whose torch is missing or broken (every family helper
returns without reinstalling when its probe fails), making
--torch-pin-needs-apply report done forever. The baseline now probes the
installed flavor and records only on a match: a cuXXX pin requires the
matching +cuXXX tag, cpu requires a cpu build, rocm/gfx requires hip;
probe failure records nothing.
4. The pinned pip fallback stripped PIP_* env vars but user/site pip config
files still applied (a configured global.extra-index-url can satisfy
torch off the pin). PIP_CONFIG_FILE is now pointed at the null device
for pinned commands (pip loads no config files then), in
_install_env_for_cmd and setup.ps1's Fast-Install pinned scrub.
install.sh / install.ps1 have no pip fallback (uv-only), verified.
Tests: 7 new rocm_support tests (snapshot reset fixture), 1 stack test,
2 parity tests. Full battery green (464 python, sh and ps1 suites).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: complete the pin-repair coverage across the fast path and platforms
Three cross-platform follow-ups to the round-2 pin-repair fixes:
1. The --torch-pin-needs-apply probe only compared marker==pin, so a torch
trio clobbered to the wrong family (a cpu wheel replacing cu128 via a
later pip install) with a still-matching marker reported "already
applied" and the _ensure_{cuda,rocm,cpu} repair never ran on the Linux
fast path. The probe is now a testable _torch_pin_needs_apply() that also
checks the installed flavor against a known-family pin (via a shared
_torch_flavor_matches_pin() helper, so the baseline and the probe cannot
drift). An unknown-family pin has no flavor to validate and a failed
probe cannot prove drift, so both keep the fast path.
2. macOS ARM (real CPU/MPS torch, not NO_TORCH) never applied an unknown-
family custom pin on update: both the verbatim path and the baseline
returned on IS_MACOS while fresh install.sh honors the pin, so the marker
was never written and setup.sh forced the dependency pass on every update
forever. The guards are now IS_MAC_INTEL (Intel mac is already NO_TORCH),
and the final pass applies the pin on macOS ARM.
3. The round-2 final verbatim repair sat in the step-13 sequence guarded
not IS_WINDOWS, so on Windows a dependency step that clobbered torch after
the pin was applied was masked by the matching marker (setup.ps1 does not
re-validate the main venv's torch after calling this script -- verified).
Step 13 now runs the verbatim snapshot-drift repair on Windows and macOS
ARM too; the Linux-oriented cuda/rocm/cpu family helpers stay Linux-only.
Tests: 13 new rocm_support cases (flavor drift, macOS ARM, Windows repair),
parity updates. Full battery green (475 python, sh and ps1 suites).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: strip query tokens from the marker and tighten the pin-drift probe
Four follow-ups to the round-3 pin-repair fixes:
1. The credential stripper feeding the torch-index marker and the logged repair
messages dropped only user:pass@ userinfo, so a private feed that carries its
auth token in the query string (.../simple?token=SECRET) persisted the token
in the world-readable marker (mode 0644 under a default umask) and printed it
in substep output. All four strippers (install.sh, install.ps1,
studio/setup.ps1, install_python_stack.py) now drop the query and fragment
before building the sanitized URL. A query is not part of a PEP 503 index's
identity, so this also stops a rotated token from spuriously mismatching the
marker and forcing a needless reinstall.
2. The --torch-pin-needs-apply fast-path probe accepted an untagged CUDA build
(no +cuXXX local tag) under a specific cuXXX pin, but _ensure_cuda_torch
reinstalls exactly that build to enforce the pin. The probe was more lenient
than the repair, so the repair pass was skipped on the fast path.
_torch_flavor_matches_pin now reports a mismatch for an untagged build under a
cuXXX pin, forcing the pass.
3. The probe's ROCm branch accepted any HIP build for a rocm/gfx pin, while
_ensure_rocm_torch decides a reinstall with the per-arch
_rocm_pin_family_mismatch predicate (a generic +rocm7.2 wheel under a per-arch
gfx pin, or a wrong ROCm version, is a mismatch). The probe now reuses that
predicate, so it is as strict as the repair. This needs the installed torch
version, so _probe_torch_flavor now returns (marker, cutag, version) and
_torch_flavor_matches_pin takes the pin URL (extracting the leaf internally).
4. On Windows a known-family cu*/cpu pin is applied to the main venv by setup.ps1
before install_python_stack.py runs; a later dependency step can clobber it,
and the GPU-aware _ensure_{cuda,cpu}_torch self-skip on Windows while the
verbatim helper handles only unknown-family pins, so nothing repaired the
clobber (setup.ps1 does not re-validate the main venv's torch afterward,
verified). New _ensure_pinned_known_family_torch reinstalls a drifted cu*/cpu
pin in the step-13 Windows/macOS-ARM branch; rocm/gfx per-arch specs stay owned
by setup.ps1, unknown-family by the verbatim helper.
A speculative ROCm 2.11 floor was also raised but is unreachable: the rocm7.2
index publishes no 2.x wheel below 2.11.0, and an unknown newer rocm is not
floored speculatively.
Tests: query/fragment strip cases in the sh + ps1 marker suites and the Python
strip/marker tests; the tri-state helper and the probe/baseline harnesses moved
to the (marker, cutag, version) flavor with matching versions; new probe cases
(untagged CUDA, generic-rocm-under-gfx) and 8 _ensure_pinned_known_family_torch
tests; a four-way query-strip parity assertion. Full battery green (1150 python,
sh 26/26 marker, ps1 marker/flavor/pin-stale).
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: reinstall markerless gfx pins and cap custom-index updates at torch 2.11
Two follow-ups from the pin-marker audit:
1. A markerless venv with a gfx per-arch 2.11 pin trusted the wheel version
tag, which is byte-identical (+rocm7.13.0) across gfx120X-all / gfx1151 /
gfx1150. A pre-marker install holding one gfx arch's wheel that is now
pinned to a DIFFERENT gfx index was therefore never switched:
_rocm_pin_family_mismatch returns no-mismatch for any three-part +rocm
2.11 wheel, and _ensure_rocm_torch's absent-marker branch fell through to
that heuristic. _ensure_rocm_torch now forces a one-time reinstall when the
marker is absent AND the pin leaf is a 2.11 gfx per-arch index; the reinstall
writes the marker, so the next update compares exactly and does not loop
(the correctly-pinned no-reinstall guarantee then comes from the exact marker
compare, not the ambiguous tag). Non-gfx-2.11 pins (rocmX.Y, non-2.11 gfx)
stay on the tag heuristic -- their tags are distinguishable.
2. The verbatim custom-index update path used _CUDA_TORCH_PKG_SPEC (torch
<2.12.0) while a FRESH install of the same unknown leaf caps torch at
<2.11.0 (install.sh's default TORCH_CONSTRAINT, and setup.ps1's custom-pin
branch), so a private /simple mirror publishing torch 2.11 could upgrade a
`studio update` to a state the fresh installer never produces. Added
_CUSTOM_INDEX_TORCH_PKG_SPEC (torch>=2.4,<2.11.0), used only by the verbatim
path; companions stay pinned for the same exclusive --index-url ABI reason
as _CUDA_TORCH_PKG_SPEC (a bare name could pull a torch-2.12-built
torchvision). _CUDA_TORCH_PKG_SPEC is unchanged (known-family cu/cpu repair
correctly tracks install.sh's widened cu ceiling).
Tests: 2 new markerless-gfx cases (one-time reinstall + marker write + no-loop
second run, and the rocmX.Y absent-marker no-op), the pre-existing markerless
gfx no-reinstall test flipped to assert the one-time reinstall (it had encoded
the old tag-trusting behavior), and the custom-index bound assertions. 488
passed.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: a matching marker must not mask a broken, clobbered, or misclassified torch
Four round-6 follow-ups, all closing cases where a matching torch-index
marker wrongly vouched for a torch that is not actually the pinned one:
1. _is_cuda_family_leaf matched cu+digits by PREFIX (^cu[0-9]), so a custom
mirror leaf like cu128-private classified as CUDA family; the flavor check
then compared the installed cu128 tag to the whole leaf cu128-private and
forced a reinstall on EVERY update (never converging). The cu family is
now matched EXACTLY (re.fullmatch cu[0-9]+), so a cu-suffixed custom leaf
routes through the verbatim/unknown path with a stable marker. Mirrored in
install.sh (_normalize_family_leaf: strip cu, require an all-digit
remainder) and setup.ps1 / install.ps1 (^cu[0-9]+$).
2. _torch_pin_needs_apply returned False on a failed torch probe (missing or
unimportable) under a matching marker, so setup.sh kept the fast path and
a broken torch was never repaired. A failed probe now forces the pass: the
marker cannot vouch for a torch that does not import, forcing is idempotent,
and once torch imports again the probe succeeds and the forcing stops
(self-resolving). Reverses the round-4 conservative choice for this case.
3. _ensure_verbatim_torch_index snapshotted the installed trio on the first
pass with a matching marker and treated an unimportable torch (snapshot
None) as "no drift, skip", so a torch clobbered to a broken state before
the run was masked. A None snapshot now reapplies the pin. A torch
clobbered to a WORKING-but-wrong build under an unknown-family pin remains
undetectable from metadata (no flavor tag; reinstalling every update would
be the loop this avoids) and is documented as a known limitation.
4. The step-13 Windows final repair reran only the verbatim (unknown-family)
and known-family cu*/cpu paths, so a clobbered explicit rocm/gfx pin (the
wheel setup.ps1 installed from AMD's per-arch index) was left in place. The
branch now also runs _ensure_rocm_torch on Windows for an explicit rocm/gfx
pin; it has a Windows path and no-ops when torch already links HIP, so it
only reinstalls a genuinely clobbered ROCm venv (loop-safe).
Tests: the round-4 failed-probe-trusts-marker test flipped to force the pass;
new cases for the cu-suffix no-loop, the broken-torch verbatim reinstall, and
the Windows rocm final-repair structure; item-2 exact-cu parity assertions.
490 passed. sh/ps1 marker + flavor + pin-stale suites all green.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: repair Windows ROCm pins from the pinned URL and honor NO_TORCH
Four round-7 review items, two of them regressions in the round-6 work:
1. _torch_pin_needs_apply ignored UNSLOTH_NO_TORCH. With a torch-index env
var set and no marker, the failed-probe branch forced the dependency pass
on every `studio update`, and the pass (which also honors NO_TORCH) never
installs torch or writes a marker, so nothing could ever stop the forcing.
It now returns False immediately under NO_TORCH: the pin only matters once
torch is actually installed.
2. The step-13 Windows final repair (round-6) restored a clobbered explicit
rocm/gfx pin by calling _ensure_rocm_torch, whose Windows path reinstalls
from the arch AUTO-DETECTED via hipinfo, not from the pin. A user pinning a
different gfx family or a private mirror was restored from the wrong source
(and the wrong marker written), and a headless box was skipped entirely
(the arch probe returns nothing). The repair now goes through
_ensure_pinned_known_family_torch, which reinstalls from the PINNED url with
the same per-arch floor setup.ps1 uses (2.11-line gfx leaves) or a bare trio
(older arches, rocmN mirrors). It is gated on IS_WINDOWS since macOS ARM has
no ROCm, and the existing flavor check keeps it loop-safe (a matching HIP
wheel is left alone).
3. _ensure_verbatim_torch_index's broken-torch check (round-6) used
"_installed_trio_snapshot() is None", but that helper reports a REMOVED torch
as "torch==absent" (a non-None tuple) and a broken import as the stale
on-disk version, so a missing or unimportable torch under a matching marker
was read as "no drift" and skipped. The matching-marker path now confirms
torch health with an import probe (_probe_torch_flavor): a torch that does
not import reapplies the pin, while a healthy torch keeps the snapshot-based
intra-run drift detection.
4. A unit test for _ensure_cpu_torch did not pin NO_TORCH False like its
siblings, so a suite run with UNSLOTH_NO_TORCH=1 in the environment made the
guard return early and the reinstall assertions fail spuriously.
Tests: the round-6 broken-torch verbatim test re-encodes the non-None
"torch==absent" snapshot case (the exact state the old "is None" check missed);
new Windows-ROCm pinned-repair cases (reinstall from the pin, per-arch floor vs
bare spec, matching-wheel no-op, off-Windows no-op); a NO_TORCH fast-path probe
case; the parity test now asserts the Windows final branch does not auto-detect
the ROCm index and that the helper reinstalls from the explicit pin. 494 passed.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: floor the rocm7.2 index in the Windows pin repair; isolate marker tests
Three round-8 review items, two of them downstream of the round-7 changes:
1. _ensure_pinned_known_family_torch gave a rocm index leaf a bare
torch/torchvision/torchaudio trio while flooring only gfx* leaves, so a
Windows venv clobbered under an explicit rocm7.2 pin could reinstall an
unbounded or ABI-mismatched trio from that exclusive --index-url. It now
mirrors the spec the initial ROCm paths pin: the rocm7.2 floor for 2.11-line
gfx leaves and rocm leaves that serve torch 2.11, the <2.11 default for
older rocm versions, and a bare trio only for older gfx per-arch leaves
(which publish no floor), matching _ROCM_TORCH_PKG_SPECS / _ensure_rocm_torch.
2. test_verbatim_custom_url_no_marker_reinstalls_once called
_ensure_verbatim_torch_index twice; the second call now hits the
matching-marker health probe, and with pip_install mocked torch never becomes
importable, so in a no-torch environment _probe_torch_flavor returned None and
forced another reinstall, failing the idempotence assertion. The test now pins
a healthy flavor so the idempotence check is about the marker, not ambient
torch.
3. The TestEnsureRocmTorchMarker fixture patched os.environ per test but not
_TORCH_BACKEND, which install_python_stack.py computes once at import from
UNSLOTH_TORCH_BACKEND. A runner starting with a cuda/cpu backend made
_ensure_rocm_torch early-return and skip the mocked repair these tests
exercise. The fixture now neutralizes _TORCH_BACKEND so the marker tests are
independent of the caller's installer-pin environment.
Tests: the Windows floor-spec test now asserts a rocm7.2 mirror pin uses the
rocm7.2 floor (not bare), plus a new rocm7.1 case that must fall back to the
<2.11 default; the marker suite passes under a hostile
UNSLOTH_TORCH_BACKEND=cuda / UNSLOTH_TORCH_INDEX_URL env. 495 passed.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: apply same-flavor pin repoints, keep ROCm fallback nonfatal, bound custom companions
Four round-9 review items, two of them regressions in the round-7 pin helper:
1. _ensure_pinned_known_family_torch returned as satisfied whenever the installed
flavor matched the pin, so a same-flavor SOURCE change (one /cpu or /cu128
mirror to another, or a gfx1151 -> gfx120x-all per-arch switch, both carrying
the same wheel tag) was never applied, while _torch_pin_needs_apply kept forcing
the pass on the marker mismatch forever. It now also reinstalls when the marker
records a DIFFERENT index of the same flavor, rewriting the marker so the next
update matches (no loop), exactly as the Linux _ensure_{cuda,cpu}_torch helpers
do. An absent marker on an already-matching venv is still left to the baseline
recorder (no forced reinstall of a correct pre-marker venv).
2. That helper reinstalled a Windows ROCm pin with the FATAL pip_install, so when
setup.ps1 had taken its CPU fallback (the pinned AMD index unavailable), the
final repair re-hit the same missing index and aborted the whole install. The
ROCm reinstall is now nonfatal (pip_install_try): on failure it leaves the CPU
base in place and writes no ROCm marker, so the install completes -- matching
_ensure_rocm_torch's Windows path. cu*/cpu pins stay fatal (authoritative source).
3. install.sh left torchvision/torchaudio bare for a pinned custom/unknown-leaf
index (a private /simple mirror), unlike the Python update path's
_CUSTOM_INDEX_TORCH_PKG_SPEC, so a mirror also exposing newer companion wheels
could resolve a torch-2.12-built torchvision against the capped <2.11 torch. It
now bounds the companions (torchvision>=0.19,<0.26.0 / torchaudio>=2.4,<2.11.0)
for a custom leaf, gated on an empty _expected_torch_flavor_tag so known families
keep their curated bare/floored companions.
4. install.sh's _expected_torch_flavor_tag matched cu[0-9]* by prefix, so a custom
leaf like cu128-private classified as the cu128 family and force-reinstalled a
correct +cu128 wheel on every run. It now requires exact cu+digits (routing the
suffixed leaf to the custom path), matching the Python re.fullmatch(cu[0-9]+) and
PowerShell, and feeding item 3's custom-leaf detection.
Tests: new cases for the same-flavor marker-change reinstall, the nonfatal ROCm
fallback (no marker on failure), the rocm7.2/older-rocm floor selection now split
across the nonfatal path, cu-suffixed custom leaves in test_torch_flavor.sh, and the
custom-leaf companion bounds in test_torch_constraint.sh. 497 python + 143 shell
assertions pass; the marker suite still passes under a hostile
UNSLOTH_TORCH_BACKEND=cuda env.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: bound custom-pin companions on the Windows setup path; isolate pin-probe tests
Two round-10 review items:
1. setup.ps1's custom/unknown-leaf pin branch capped only torch ($cudaTorchSpec)
and still asked the exclusive index for bare torchvision/torchaudio, so a
private mirror that also serves newer companion wheels could install a
torch<2.11 wheel alongside a torchvision>=0.26 / torchaudio>=2.11 built for a
newer torch ABI, after which the marker records the pin as applied. It now
bounds the whole trio (torch>=2.4,<2.11.0 / torchvision>=0.19,<0.26.0 /
torchaudio>=2.4,<2.11.0) for a pinned non-cu-family leaf, matching install.sh,
install.ps1's fresh pinned install, and install_python_stack.py's
_CUSTOM_INDEX_TORCH_PKG_SPEC. This completes the companion-bounds fix across all
three installers; known cu* leaves keep bare specs (the family index bounds them).
2. The _torch_pin_needs_apply probe tests did not pin NO_TORCH False, so a test
process launched with UNSLOTH_NO_TORCH=1 short-circuited the probe (the round-7
guard) and returned False for cases that expect the pass to run. The _needs_apply
helper now patches NO_TORCH (default False) around the call, and the dedicated
no-torch case passes no_torch=True explicitly.
Tests: the cross-platform parity test now asserts setup.ps1 bounds the full trio
(not just torch) for a custom leaf; the pin-probe suite passes under a hostile
UNSLOTH_NO_TORCH=1 environment. setup.ps1 parses clean; 497 python + shell suites
green.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: bound custom rocm-* pins, redact diag tokens, snapshot custom pins before base update
Three round-11 review items, all reproduced before fixing:
1. install.sh's custom-index companion bounds gated on _expected_torch_flavor_tag
returning empty, but that helper returned "rocm" for ANY rocm* leaf, so a custom
mirror whose leaf starts with rocm but is not a pip family (a private rocm-current
mirror, a Radeon find-links rocm-rel-7.2.1) escaped the bounds and installed bare
torchvision/torchaudio. It now digit-gates rocm to rocm[0-9]* (matching the Python
_is_pip_rocm_family_leaf ^rocm\d), so those custom leaves return "" and the <2.11
companion caps apply; real rocm7.2 / gfx per-arch indexes still classify as rocm.
2. _tauri_torch_index_family classified by the raw last path segment, so a pinned URL
carrying auth in the query (.../rocm7.2?token=SECRET) had the token echoed verbatim
into the emitted [TAURI:DIAG] line. It now strips query/fragment before classifying
(mirroring the marker/log credential stripping), so no token reaches the diagnostic
output; as a side effect .../cu128?token=x now classifies as cu128 instead of auto.
3. On studio update, the core package step (a newer unsloth can require a torch the
custom pin does not satisfy, pulling a default PyPI trio) runs BEFORE the step-2b
verbatim check, which then recorded the already-clobbered trio as the baseline for a
matching marker and left the pin unapplied. A new _capture_verbatim_baseline() records
the pre-clobber trio before the core step, so the verbatim pass detects the drift and
reapplies the pin. Captures only for a matching custom pin with importable torch; a
mismatched/absent marker or broken torch is left to _ensure_verbatim_torch_index.
Tests: _expected_torch_flavor_tag rocm-current / rocm-rel cases; _tauri_torch_index_family
token/fragment redaction with a no-leak regression guard; _capture_verbatim_baseline
record/skip cases plus an end-to-end clobber-detection scenario; a structural guard that
the capture runs before the core step. 501 python + shell suites pass; install.sh bash -n
clean, shellcheck unchanged from base.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: match rocm family leaves exactly, enforce the rocm7.2 torch line, repair a broken pinned torch
A pinned index is a pip ROCm --index-url family only when its leaf is an exact
rocm / rocm. (rocm7.2) or a gfx* per-arch leaf. The prior
^rocm[0-9] prefix match also caught suffixed private-mirror leaves (rocm7.2-private,
rocm7-current), routing them through the ROCm/companion-family path instead of the
verbatim pin: the companion bounds were skipped and, on a pre-marker venv with a
compatible +rocm wheel, the pin was never applied. Match the family exactly through one
shared helper at every site:
- install_python_stack.py: _is_pip_rocm_family_leaf (re.fullmatch), plus the two other
loose gates it feeds (_normalize_family_leaf, _torch_flavor_matches_pin).
- install.sh: a new _is_pip_rocm_family_leaf routes _expected_torch_flavor_tag,
_torch_index_repairable, _normalize_family_leaf and the ROCm side-effect gate.
- setup.ps1: a new Test-PipRocmFamilyLeaf routes Get-NormalizedFamilyLeaf and both
pinned reroutes; install.ps1 anchors its reroute regex.
_rocm_pin_family_mismatch (and its setup.ps1 mirror Get-RocmPinStaleTags) compared only
the ROCm version, so a +rocm7.2 wheel whose torch release drifted off the 2.11 line
(2.12/2.13 from an out-of-band upgrade or a custom rocm7.2 mirror) satisfied the family
check while violating _ROCM_TORCH_PKG_SPECS['rocm7.2'] (torch>=2.11,<2.12). Flag it stale
so the repair reinstalls to floor; >=2.11 alone is not enough, so the release is compared
exactly against the 2.11 line for a KNOWN-2.11 rocm pin.
_ensure_pinned_known_family_torch returned on a failed import probe, but
_torch_pin_needs_apply forces the dependency pass on that same failed probe: a broken
torch under a known-family pin was left in place and the pass was forced on every update.
Treat an unimportable torch as drift and reinstall the pinned trio (the spec and marker
derive from the pinned leaf, not the absent flavor); once it lands the probe succeeds and
the fast path returns.
Tests: exact-match cases across test_torch_flavor.sh, test_rocm_support.py,
test_cross_platform_parity.py and the two .ps1 helper suites; the rocm7.2 release-line
and broken-probe-reinstall cases; extraction lists updated for the new helpers.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* install: anchor the PS pinned-ROCm floor gate and bound install.ps1 custom-pin companions
Round 12 made every family CLASSIFIER exact, but the Windows install-flow floor gate reads
$_pinRocm211 directly from the raw pinned leaf with an unanchored -match '^rocm(\d+)\.(\d+)'
BEFORE any exact classification runs. A suffixed custom leaf (rocm7.2-private) matches that
rocm7.2 prefix, so it takes the 2.11-floor branch and is force-routed through the ROCm
install path before the exact-match elseif can send it to the verbatim install. Anchor the
match ($) in both install.ps1 and setup.ps1 so only an exact rocmX.Y leaf is floored; a
suffixed or newer-suffix leaf falls through to the verbatim path. The Python floor
selection is already exact (dict lookups gated on _is_pip_rocm_family_leaf), so only the two
PS scripts needed this.
install.ps1's custom (non-cu-family) pinned-torch install bounded torch>=2.4,<2.11.0 but
left torchvision/torchaudio bare, so a private mirror serving newer companions could pull a
wheel built for a newer torch ABI while the marker records the pin as applied. Bound both
companions (torchvision>=0.19,<0.26.0 / torchaudio>=2.4,<2.11.0) when the leaf is not a
cu