diff --git a/.github/scripts/run-studio-permission-browser.sh b/.github/scripts/run-studio-permission-browser.sh
new file mode 100755
index 0000000000..2007789035
--- /dev/null
+++ b/.github/scripts/run-studio-permission-browser.sh
@@ -0,0 +1,69 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+set -euo pipefail
+
+port="${1:?usage: $0 PORT BROWSER [CHANNEL]}"
+browser="${2:?usage: $0 PORT BROWSER [CHANNEL]}"
+channel="${3:-}"
+slug="$browser${channel:+-$channel}"
+artifact_dir="logs/playwright-permissions-$slug"
+server_log="logs/studio-permissions-$slug.log"
+studio_home="${UNSLOTH_STUDIO_HOME:-$HOME/.unsloth/studio}"
+set --
+if [ -n "${STUDIO_PERMISSION_FRONTEND:-}" ]; then
+ set -- -f "$STUDIO_PERMISSION_FRONTEND"
+fi
+
+mkdir -p "$artifact_dir"
+unsloth studio reset-password
+UNSLOTH_API_ONLY=1 unsloth studio -H 127.0.0.1 -p "$port" "$@" \
+ >"$server_log" 2>&1 &
+studio_pid=$!
+
+cleanup() {
+ kill "$studio_pid" 2>/dev/null || true
+ wait "$studio_pid" 2>/dev/null || true
+}
+trap cleanup EXIT
+
+healthy=0
+for _ in $(seq 1 180); do
+ if curl -fs "http://127.0.0.1:$port/api/health" >/dev/null; then
+ healthy=1
+ break
+ fi
+ if ! kill -0 "$studio_pid" 2>/dev/null; then
+ tail -100 "$server_log" || true
+ exit 1
+ fi
+ sleep 1
+done
+if [ "$healthy" -ne 1 ]; then
+ tail -100 "$server_log" || true
+ exit 1
+fi
+
+old_password=$(cat "$studio_home/auth/.bootstrap_password")
+new_password="CIPerm-$(python -c 'import secrets; print(secrets.token_urlsafe(16))')"
+if [ "${GITHUB_ACTIONS:-}" = "true" ]; then
+ echo "::add-mask::$old_password"
+ echo "::add-mask::$new_password"
+fi
+
+export BASE_URL="http://127.0.0.1:$port"
+export STUDIO_OLD_PW="$old_password"
+export STUDIO_NEW_PW="$new_password"
+export STUDIO_UI_STRICT=1
+export STUDIO_UI_PERMISSION_ONLY=1
+export STUDIO_UI_WALL_TIMEOUT_S=240
+export STUDIO_PLAYWRIGHT_BROWSER="$browser"
+export PW_ART_DIR="$artifact_dir"
+if [ -n "$channel" ]; then
+ export STUDIO_PLAYWRIGHT_CHANNEL="$channel"
+else
+ unset STUDIO_PLAYWRIGHT_CHANNEL || true
+fi
+
+python tests/studio/playwright_chat_ui.py
diff --git a/.github/workflows/studio-mac-ui-smoke.yml b/.github/workflows/studio-mac-ui-smoke.yml
index 378e8ee5a6..7375e9bcbf 100644
--- a/.github/workflows/studio-mac-ui-smoke.yml
+++ b/.github/workflows/studio-mac-ui-smoke.yml
@@ -19,6 +19,7 @@ on:
- 'install.sh'
- 'pyproject.toml'
- 'tests/studio/**'
+ - '.github/scripts/run-studio-permission-browser.sh'
- '.github/workflows/studio-mac-ui-smoke.yml'
push:
branches: [main, pip]
@@ -96,7 +97,7 @@ jobs:
- name: Assert llama.cpp loads on this macOS
run: bash .github/scripts/assert-llama-loads.sh
- - name: Install Playwright + Chromium
+ - name: Install Playwright browsers
# No --with-deps on Mac: that flag installs Linux apt packages.
# GitHub-hosted macos-14 ships the system frameworks Chromium
# needs already.
@@ -112,7 +113,7 @@ jobs:
# in-script retry recover from any residual flakes.
run: |
pip install 'playwright>=1.55,<1.58'
- python -m playwright install chromium
+ python -m playwright install chromium webkit
- name: Patch Playwright pipeTransport.js to tolerate malformed JSON
# In Playwright 1.55-1.58, pipeTransport.js does
@@ -244,6 +245,10 @@ jobs:
kill "${STUDIO_PID}" 2>/dev/null || true
sleep 2
+ - name: Cross-browser permission controls
+ run: |
+ bash .github/scripts/run-studio-permission-browser.sh 18895 webkit
+
- name: Reset auth + boot Unsloth for extra UI tests (port 18897)
run: |
unsloth studio reset-password
@@ -343,5 +348,7 @@ jobs:
logs/studio_extra.log
logs/install.log
logs/playwright
+ logs/playwright-permissions-*
logs/playwright_extra
+ logs/studio-permissions-*.log
retention-days: 7
diff --git a/.github/workflows/studio-ui-smoke.yml b/.github/workflows/studio-ui-smoke.yml
index b6d6d7d6e2..30280c281e 100644
--- a/.github/workflows/studio-ui-smoke.yml
+++ b/.github/workflows/studio-ui-smoke.yml
@@ -27,6 +27,7 @@ on:
# The Playwright test files themselves -- a PR that ONLY edits
# the test must still trigger UI CI.
- 'tests/studio/**'
+ - '.github/scripts/run-studio-permission-browser.sh'
- '.github/workflows/studio-ui-smoke.yml'
push:
branches: [main, pip]
@@ -107,13 +108,10 @@ jobs:
set -o pipefail
bash install.sh --local --no-torch 2>&1 | tee logs/install.log
- - name: Install Playwright + Chromium
+ - name: Install Playwright browsers
run: |
pip install 'playwright>=1.45'
- # --with-deps installs the OS-level runtime libs Chromium
- # needs (libnss3, libxkbcommon, etc.). About 30 s on a
- # warm runner.
- python -m playwright install --with-deps chromium
+ python -m playwright install --with-deps chromium firefox webkit
- name: Reset auth + boot Unsloth
run: |
@@ -182,6 +180,12 @@ jobs:
kill "${STUDIO_PID}" 2>/dev/null || true
sleep 2
+ - name: Cross-browser permission controls
+ run: |
+ bash .github/scripts/run-studio-permission-browser.sh 18893 firefox
+ bash .github/scripts/run-studio-permission-browser.sh 18893 webkit
+ bash .github/scripts/run-studio-permission-browser.sh 18893 chromium chrome
+
# The chat UI test ends by clicking the Shutdown menuitem, which
# leaves the server dead. The extra UI test (Compare / Recipes /
# Export / Unsloth / Settings) needs a fresh Unsloth, so we boot a
@@ -297,6 +301,8 @@ jobs:
logs/install.log
logs/server-logs/
logs/playwright
+ logs/playwright-permissions-*
logs/playwright_extra
logs/playwright_ime
+ logs/studio-permissions-*.log
retention-days: 7
diff --git a/.github/workflows/studio-windows-ui-smoke.yml b/.github/workflows/studio-windows-ui-smoke.yml
index 12d7475b53..f401f7be44 100644
--- a/.github/workflows/studio-windows-ui-smoke.yml
+++ b/.github/workflows/studio-windows-ui-smoke.yml
@@ -19,6 +19,7 @@ on:
- 'install.ps1'
- 'pyproject.toml'
- 'tests/studio/**'
+ - '.github/scripts/run-studio-permission-browser.sh'
- '.github/workflows/studio-windows-ui-smoke.yml'
push:
branches: [main, pip]
@@ -345,6 +346,10 @@ jobs:
kill "${STUDIO_PID}" 2>/dev/null || true
sleep 2
+ - name: Edge permission controls
+ run: |
+ bash .github/scripts/run-studio-permission-browser.sh 18895 chromium msedge
+
- name: Reset auth + boot Unsloth for extra UI tests (port 18897)
run: |
unsloth studio reset-password
@@ -402,5 +407,7 @@ jobs:
logs/studio_extra.log
logs/install.log
logs/playwright
+ logs/playwright-permissions-*
logs/playwright_extra
+ logs/studio-permissions-*.log
retention-days: 7
diff --git a/README.md b/README.md
index 085c7718e5..6aa8f4f4c3 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,7 @@ Unsloth Studio lets you run and train models locally.
Features •
+ News •
Quickstart •
Notebooks •
Documentation
@@ -47,15 +48,44 @@ Unsloth Studio (Beta) lets you run and train text, [audio](https://unsloth.ai/do
* [Auto set inference settings](https://unsloth.ai/docs/new/studio/chat#auto-parameter-tuning) and customize chat templates.
* We work directly with teams behind [gpt-oss](https://docs.unsloth.ai/new/gpt-oss-how-to-run-and-fine-tune#unsloth-fixes-for-gpt-oss), [Qwen3](https://www.reddit.com/r/LocalLLaMA/comments/1kaodxu/qwen3_unsloth_dynamic_ggufs_128k_context_bug_fixes/), [Llama 4](https://github.com/ggml-org/llama.cpp/pull/12889), [Mistral](https://huggingface.co/mistralai/Mistral-Medium-3.5-128B/discussions/18), [Gemma 1-3](https://news.ycombinator.com/item?id=39671146), and [Phi-4](https://unsloth.ai/blog/phi4), where we’ve fixed bugs that improve model accuracy.
* Chat with images, audio, PDFs, code, DOCX and more. [Connect API providers](https://unsloth.ai/docs/integrations/connections) (OpenAI, Anthropic) or servers (vLLM, Ollama).
+* [**Compare any two models**](https://unsloth.ai/docs/new/studio/chat#model-arena) side by side with the same prompt.
+* **OpenAI/Anthropic-compatible APIs**: Serve local models through `/v1/chat/completions`, `/v1/responses` and `/v1/messages`.
+* **Connect local models to agents**: Use `unsloth start` with Claude Code, Codex, Hermes and more.
+* **Web/PDF search** can read PDF papers, manuals and other PDF results.
+* **GGUF hardware controls**: Choose GPUs/layers, offload MoE experts, use multi-GPU or Tensor Parallelism.
+* The opt-in **MCP control endpoint** lets AI clients manage models, training, recipes and exports.
### Training
-* Train and RL **500+ models** up to **2x faster** with up to **70% less VRAM**, with no accuracy loss.
-* Custom Triton and mathematical **kernels**. See some collabs we did with [PyTorch](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/fp8-reinforcement-learning) and [Hugging Face](https://unsloth.ai/docs/new/faster-moe).
+* Train and RL **500+ models** up to **2x faster** with **70% less VRAM**; MoE up to **12x faster**.
+* Train and run RL on [AMD GPUs](https://unsloth.ai/docs/basics/amd) across Windows, WSL and Linux.
* **Data Recipes**: [Auto-create datasets](https://unsloth.ai/docs/new/studio/data-recipe) from **PDF, CSV, DOCX** etc. Edit data in a visual-node workflow.
-* **[Reinforcement Learning](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide)** (RL): The most efficient [RL](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide) library, using **80% less VRAM** for GRPO, [FP8](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/fp8-reinforcement-learning) etc.
-* Supports full fine-tuning, RL, pretraining, 4-bit, 16-bit and, FP8 training.
+* **[Reinforcement Learning](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide)** uses **80% less VRAM** for GRPO, FP8 and vision RL, with 7x longer contexts.
+* [**Long-context training**](https://unsloth.ai/docs/new/3x-faster-training-packing): **3x faster**, 30% less VRAM and 500K+ context.
+* Supports LoRA/QLoRA, full fine-tuning, RL, pretraining, 4-bit, 16-bit and FP8.
+* Custom Triton and mathematical **kernels** built with PyTorch and Hugging Face.
* **Observability**: Monitor training live, track loss and GPU usage and customize graphs.
* [Multi-GPU](https://unsloth.ai/docs/basics/multi-gpu-training-with-unsloth) training is supported, with major improvements coming soon.
+## 🚀 Unsloth Start
+
+[Unsloth Start](https://unsloth.ai/docs/integrations/unsloth-start) connects [Claude Code](https://unsloth.ai/docs/basics/claude-code), [Codex](https://unsloth.ai/docs/basics/codex) and other agents to local models with one command.
+
+Start Unsloth, load a model, open your project folder, then run:
+
+```bash
+unsloth start claude
+```
+
+Replace `claude` with any supported agent:
+
+| Agent | Command |
+| --- | --- |
+| Claude Code | `unsloth start claude` |
+| OpenAI Codex | `unsloth start codex` |
+| Hermes Agent | `unsloth start hermes` |
+| OpenClaw | `unsloth start openclaw` |
+| OpenCode | `unsloth start opencode` |
+| Pi Coding Agent | `unsloth start pi` |
+
## 📥 Install
Unsloth can be used in two ways: through **[Unsloth Studio](https://unsloth.ai/docs/new/studio/)**, the web UI, or through **Unsloth Core**, the code-based version. Each has different requirements.
@@ -65,7 +95,8 @@ 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). Unsloth Studio support is out soon.
+* **AMD:** Training, RL, chat and deployment work on Windows, WSL and Linux. [Read the AMD guide](https://unsloth.ai/docs/basics/amd).
+* **Vulkan:** GGUF inference is supported on [compatible GPUs, including Intel GPUs](https://github.com/unslothai/unsloth/pull/5819).
* **Multi-GPU:** Available now, with a major upgrade on the way
#### macOS, Linux, WSL:
@@ -122,7 +153,7 @@ You can use the same Docker image as Unsloth Studio.
#### AMD, Intel:
For RTX 50x, B200, 6000 GPUs: `uv pip install unsloth --torch-backend=auto`. Read our guides for: [Blackwell](https://unsloth.ai/docs/blog/fine-tuning-llms-with-blackwell-rtx-50-series-and-unsloth) and [DGX Spark](https://unsloth.ai/docs/blog/fine-tuning-llms-with-nvidia-dgx-spark-and-unsloth).
-To install Unsloth on **AMD** and **Intel** GPUs, follow our [AMD Guide](https://unsloth.ai/docs/get-started/install/amd) and [Intel Guide](https://unsloth.ai/docs/get-started/install/intel).
+To install Unsloth on **AMD** and **Intel** GPUs, follow our [AMD Guide](https://unsloth.ai/docs/basics/amd) and [Intel Guide](https://unsloth.ai/docs/get-started/install/intel).
## 📒 Free Notebooks
@@ -148,13 +179,20 @@ Read our [guide](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide). Ad
- See detailed documentation for Unsloth [here](https://unsloth.ai/docs)
## 🦥 Unsloth News
-- **Connections**: Connect any API provider (OpenAI, Anthropic) or server (vLLM, Ollama). [Guide](https://unsloth.ai/docs/integrations/connections)
-- **MTP**: Run Qwen3.6 MTP in Unsloth. MTP settings are autoset specific to your hardware. [Guide](https://unsloth.ai/docs/models/qwen3.6#mtp-guide)
-- **API inference endpoint**: Deploy and run local LLMs in Claude Code, Codex tools. [Guide](https://unsloth.ai/docs/basics/api)
-- **Qwen3.6**: Qwen3.6-35B-A3B can now be trained and run in Unsloth Studio. [Blog](https://unsloth.ai/docs/models/qwen3.6)
-- **Gemma 4**: Run and train Google’s new models directly in Unsloth. [Blog](https://unsloth.ai/docs/models/gemma-4)
+- **AMD training**: Train, run RL, chat and deploy on AMD GPUs across Windows, WSL and Linux. [Guide](https://unsloth.ai/docs/basics/amd)
+- **GGUF hardware controls**: Choose GPU/layer placement, offload MoE experts and use multi-GPU or Tensor Parallelism. [#6414](https://github.com/unslothai/unsloth/pull/6414)
+- **Local models for any agent**: Use `unsloth start` with Claude Code, Codex, Hermes, OpenCode, OpenClaw, Pi and more through Unsloth's OpenAI- and Anthropic-compatible APIs. [Guide](https://unsloth.ai/docs/basics/api)
+- **MCP control endpoint**: Let compatible clients manage models, training, recipes, checkpoints and exports. [#7191](https://github.com/unslothai/unsloth/pull/7191)
+- **Local inference reliability**: Resume long chats faster, recover stalled downloads and reuse existing GGUF files. [#7204](https://github.com/unslothai/unsloth/pull/7204) • [#6858](https://github.com/unslothai/unsloth/pull/6858) • [#7209](https://github.com/unslothai/unsloth/pull/7209)
+- **New models**: [Qwen-AgentWorld](https://huggingface.co/unsloth/Qwen-AgentWorld-35B-A3B-GGUF), [Ornith](https://huggingface.co/unsloth/models?search=ornith), [Kimi K2.7 Code](https://unsloth.ai/docs/models/kimi-k2.7-code) and [MiniMax M3](https://unsloth.ai/docs/models/minimax-m3)
+- **GLM-5.2**: Run Z.ai's 744B-parameter, 1M-context open model locally with Unsloth Dynamic GGUFs. [Guide](https://unsloth.ai/docs/models/glm-5.2)
+- **DeepSeek-V4**: Run DeepSeek-V4-Flash locally with corrected multi-turn and tool-calling behavior. [Guide](https://unsloth.ai/docs/models/deepseek-v4)
+- **DiffusionGemma**: Run and fine-tune Google's diffusion language model with 1.8x faster inference in Unsloth Studio. [Guide](https://unsloth.ai/docs/models/diffusiongemma)
+- **Qwen3.6**: Run and train Qwen3.6 with MTP for 1.4-2.2x faster inference and NVFP4 quants for supported GPUs. [Guide](https://unsloth.ai/docs/models/qwen3.6)
+- **Gemma 4**: Run and train Gemma 4 text, image and audio models with QAT, MTP, GGUF and MLX support. [Guide](https://unsloth.ai/docs/models/gemma-4)
+- **MCP servers**: Connect local models to files, apps, databases and external tools through Model Context Protocol. [Guide](https://unsloth.ai/docs/basics/mcp)
+- **Connections**: Mix local models with API providers (OpenAI, Anthropic) or servers (vLLM, Ollama) in the same interface. [Guide](https://unsloth.ai/docs/integrations/connections)
- **Introducing Unsloth Studio**: our new web UI for running and training LLMs. [Blog](https://unsloth.ai/docs/new/studio)
-- **Qwen3.5** - 0.8B, 2B, 4B, 9B, 27B, 35-A3B, 112B-A10B are now supported. [Guide + notebooks](https://unsloth.ai/docs/models/qwen3.5/fine-tune)
- Train **MoE LLMs 12x faster** with 35% less VRAM - DeepSeek, GLM, Qwen and gpt-oss. [Blog](https://unsloth.ai/docs/new/faster-moe)
- **Embedding models**: Unsloth now supports ~1.8-3.3x faster embedding fine-tuning. [Blog](https://unsloth.ai/docs/new/embedding-finetuning) • [Notebooks](https://unsloth.ai/docs/get-started/unsloth-notebooks#embedding-models)
- New **7x longer context RL** vs. all other setups, via our new batching algorithms. [Blog](https://unsloth.ai/docs/new/grpo-long-context)
diff --git a/install.ps1 b/install.ps1
index df49414620..a525d4df56 100644
--- a/install.ps1
+++ b/install.ps1
@@ -53,7 +53,8 @@ function Install-UnslothStudio {
param([string]$TorchIndexUrl)
if ($SkipTorch) { return "none" }
if ([string]::IsNullOrWhiteSpace($TorchIndexUrl)) { return "none" }
- $leaf = ($TorchIndexUrl.TrimEnd('/') -split '/')[-1].ToLowerInvariant()
+ # Drop query/fragment first so a token-authenticated pin classifies by family.
+ $leaf = (($TorchIndexUrl -split '[?#]', 2)[0].TrimEnd('/') -split '/')[-1].ToLowerInvariant()
if (@("cpu", "cu118", "cu124", "cu126", "cu128", "cu130") -contains $leaf) { return $leaf }
if ($leaf -match '^rocm[0-9]+\.[0-9]+$') { return $leaf }
return "auto"
@@ -62,7 +63,8 @@ function Install-UnslothStudio {
function Get-TauriGpuBranch {
param([string]$TorchIndexFamily)
if ($SkipTorch) { return "no_torch" }
- if ($TorchIndexFamily -like "cu*") { return "cuda" }
+ # Require a digit after "cu" so /current or /custom isn't branded CUDA (parity ^cu[0-9]).
+ if ($TorchIndexFamily -match '^cu[0-9]') { return "cuda" }
if ($TorchIndexFamily -like "rocm*") { return "rocm" }
if ($TorchIndexFamily -eq "cpu") { return "cpu" }
return "unknown"
@@ -467,22 +469,35 @@ function Install-UnslothStudio {
}
}
+ # Redact index-URL credentials (userinfo + ?query= + #fragment) from captured installer
+ # output before printing on failure; uv/pip errors echo the failing --index-url verbatim.
+ # Mirrors the other installers. Verbose mode streams uncaptured, so it isn't redacted.
+ function Redact-InstallOutput {
+ param([string]$Text)
+ if (-not $Text) { return $Text }
+ $Text = $Text -replace '(https?://)[^/@\s`]+@', '$1@'
+ $Text = $Text -replace '([?&][^=\s&`]+)=[^\s`]+', '$1='
+ # A #token=... fragment is as sensitive as a query; URL-anchored.
+ return $Text -replace '(https?://[^\s`#]+)#[^\s`]+', '$1#'
+ }
+
# Run native commands quietly by default to match install.sh behavior.
# Full command output is shown only when --verbose / UNSLOTH_VERBOSE=1.
function Invoke-InstallCommand {
param(
[Parameter(Mandatory = $true)][ScriptBlock]$Command
)
- # Installer-pinned index installs (torch) must beat an inherited uv mirror
- # (#6898): when the command pins an index, clear every uv index env var so
- # it wins, then restore in finally. Other installs keep the user's mirror.
+ # Installer-pinned index installs (torch) must beat an inherited uv mirror (#6898):
+ # for --default-index, clear the uv index env vars (restore in finally) and set
+ # UV_NO_CONFIG=1 so a uv.toml/pyproject index can't outrank the CLI pin (uv 0.10).
$savedUvIndex = $null
if ($Command.ToString() -match '--default-index') {
$savedUvIndex = @{}
- foreach ($n in 'UV_DEFAULT_INDEX', 'UV_INDEX_URL', 'UV_INDEX', 'UV_EXTRA_INDEX_URL') {
+ foreach ($n in 'UV_DEFAULT_INDEX', 'UV_INDEX_URL', 'UV_INDEX', 'UV_EXTRA_INDEX_URL', 'UV_TORCH_BACKEND', 'UV_FIND_LINKS', 'UV_CONFIG_FILE', 'UV_NO_CONFIG') {
$savedUvIndex[$n] = [Environment]::GetEnvironmentVariable($n)
Remove-Item "Env:$n" -ErrorAction SilentlyContinue
}
+ $env:UV_NO_CONFIG = '1'
}
$prevEap = $ErrorActionPreference
$ErrorActionPreference = "Continue"
@@ -493,17 +508,23 @@ function Install-UnslothStudio {
# Merge stderr into stdout so progress/warning output stays visible
# without flipping $? on successful native commands (PS 5.1 treats
# stderr records as errors that set $? = $false even on exit code 0).
- & $Command 2>&1 | Out-Host
+ # Redact per record: uv echoes index URLs (credentials and all) in
+ # its errors, and verbose mode must not bypass the quiet path's
+ # redaction. ForEach-Object/Out-Host leave $LASTEXITCODE untouched.
+ & $Command 2>&1 | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
} else {
$output = & $Command 2>&1 | Out-String
if ($LASTEXITCODE -ne 0) {
- Write-Host $output -ForegroundColor Red
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Red
}
}
return [int]$LASTEXITCODE
} finally {
$ErrorActionPreference = $prevEap
- if ($savedUvIndex) { foreach ($n in $savedUvIndex.Keys) { if ($null -ne $savedUvIndex[$n]) { Set-Item "Env:$n" $savedUvIndex[$n] } } }
+ if ($savedUvIndex) {
+ Remove-Item "Env:UV_NO_CONFIG" -ErrorAction SilentlyContinue
+ foreach ($n in $savedUvIndex.Keys) { if ($null -ne $savedUvIndex[$n]) { Set-Item "Env:$n" $savedUvIndex[$n] } }
+ }
}
}
@@ -1960,10 +1981,31 @@ exit 0
# On an AMD GPU (no NVIDIA), surface the optional WSL-ROCm driver hint.
if (-not $HasNvidiaSmi -and ($ROCmGfxArch -or $ROCmGpuLabel)) { Show-AmdWslDriverHint }
+ # Trim trailing slashes from the URL PATH only, preserving ?query / #fragment: a whole-URL
+ # TrimEnd corrupts a token ending in "/", a single strip leaves .../cu128// empty. Shared.
+ function Trim-IndexPathSlashes {
+ param([string]$Url)
+ $value = $Url.Trim()
+ $idx = $value.IndexOfAny([char[]]@('?', '#'))
+ if ($idx -lt 0) {
+ return $value.TrimEnd('/')
+ }
+ return $value.Substring(0, $idx).TrimEnd('/') + $value.Substring($idx)
+ }
+
# ── Choose the correct PyTorch index URL based on driver CUDA version ──
# Mirrors Get-PytorchCudaTag in setup.ps1.
function Get-TorchIndexUrl {
$baseUrl = if ($env:UNSLOTH_PYTORCH_MIRROR) { $env:UNSLOTH_PYTORCH_MIRROR.TrimEnd('/') } else { "https://download.pytorch.org/whl" }
+ # Explicit pin -- skip ALL GPU probing (headless / CI / cross-install).
+ # UNSLOTH_TORCH_INDEX_URL wins (full URL, verbatim); _FAMILY is the leaf appended
+ # to the mirror base. Matches install.sh / install_python_stack.py.
+ if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_URL)) {
+ return (Trim-IndexPathSlashes $env:UNSLOTH_TORCH_INDEX_URL)
+ }
+ if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_FAMILY)) {
+ return "$baseUrl/$($env:UNSLOTH_TORCH_INDEX_FAMILY.Trim().Trim('/'))"
+ }
if (-not $NvidiaSmiExe) { return "$baseUrl/cpu" }
try {
$output = Invoke-NvidiaSmiBounded $NvidiaSmiExe
@@ -1984,6 +2026,25 @@ exit 0
return "$baseUrl/cu126"
}
+ # Strip userinfo AND query/fragment so an authenticated pin never leaks. Shared with
+ # _strip_index_url_credentials (install.sh / py / setup.ps1).
+ function Remove-IndexUrlCredentials {
+ param([string]$Url)
+ $sep = $Url.IndexOf('://')
+ if ($sep -lt 0) { return $Url }
+ $scheme = $Url.Substring(0, $sep)
+ $rest = $Url.Substring($sep + 3)
+ # Drop query / fragment (may hold auth tokens).
+ $q = $rest.IndexOfAny([char[]]('?', '#'))
+ if ($q -ge 0) { $rest = $rest.Substring(0, $q) }
+ $slash = $rest.IndexOf('/')
+ $authority = if ($slash -ge 0) { $rest.Substring(0, $slash) } else { $rest }
+ $at = $authority.LastIndexOf('@')
+ $host_ = if ($at -ge 0) { $authority.Substring($at + 1) } else { $authority }
+ if ($slash -ge 0) { return "${scheme}://${host_}$($rest.Substring($slash))" }
+ return "${scheme}://${host_}"
+ }
+
# ── Torch flavor helpers (to repair a stale CPU / wrong-CUDA wheel) ──
# torch.__version__ -> flavor tag (cuXXX / rocm / cpu); untagged wheel = cpu,
# matching setup.ps1's stale-venv parse.
@@ -2002,11 +2063,13 @@ exit 0
param([string]$TorchIndexUrl, [string]$ROCmIndexUrl)
if (-not [string]::IsNullOrWhiteSpace($ROCmIndexUrl)) { return 'rocm' }
if ([string]::IsNullOrWhiteSpace($TorchIndexUrl)) { return $null }
- $leaf = ($TorchIndexUrl.TrimEnd('/') -split '/')[-1].ToLowerInvariant()
+ # Drop query/fragment first so .../cu128?token=x classifies as cu128 (else it reinstalls every run).
+ $leaf = (($TorchIndexUrl -split '[?#]', 2)[0].TrimEnd('/') -split '/')[-1].ToLowerInvariant()
if ($leaf -match '^cu\d+$') { return $leaf }
if ($leaf -eq 'cpu') { return 'cpu' }
if ($leaf -match '^rocm') { return 'rocm' }
- if ($leaf -match '^gfx') { return 'rocm' }
+ # gfx must be followed by a digit (an architecture leaf); gfx-private is custom.
+ if ($leaf -match '^gfx[0-9]') { return 'rocm' }
return $null
}
@@ -2041,6 +2104,10 @@ exit 0
} catch { return $null }
}
+ # An explicit pin is authoritative: the AMD ROCm reroute below must not rewrite it
+ # (e.g. a deliberate cpu pin on an AMD host).
+ $TorchIndexPinned = (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_URL)) -or `
+ (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_FAMILY))
$TorchIndexUrl = Get-TorchIndexUrl
# ── GPU arch → newest compatible Windows ROCm wheel release ──
@@ -2052,7 +2119,9 @@ exit 0
# Override with UNSLOTH_ROCM_WINDOWS_MIRROR for air-gapped / mirror installs.
$ROCmIndexUrl = $null
$ROCmTorchFloor = $null
- if (($HasROCm -or $ROCmGfxArch) -and $TorchIndexUrl -like "*/cpu" -and -not $SkipTorch) {
+ $PinnedRocmVisionSpec = $null
+ $PinnedRocmAudioSpec = $null
+ if (-not $TorchIndexPinned -and ($HasROCm -or $ROCmGfxArch) -and $TorchIndexUrl -like "*/cpu" -and -not $SkipTorch) {
$amdIndexBase = if ($env:UNSLOTH_ROCM_WINDOWS_MIRROR) { $env:UNSLOTH_ROCM_WINDOWS_MIRROR.TrimEnd('/') } else { "https://repo.amd.com/rocm/whl" }
$archFamilyMap = @{
"gfx1201" = "gfx120X-all"; "gfx1200" = "gfx120X-all" # RDNA 4
@@ -2102,6 +2171,32 @@ exit 0
}
}
+ # A gfx*/rocm pin skips the auto-reroute above, but the generic CPU/CUDA install below
+ # would use torch>=2.4,<2.11 and pull a known-bad wheel on the gfx115x/gfx120x/rocm>=7.2
+ # indexes (the _grouped_mm bug). Route a pinned ROCm index through the ROCm path.
+ if ($TorchIndexPinned -and -not $ROCmIndexUrl -and -not $SkipTorch) {
+ $_pinLeaf = (($TorchIndexUrl -split '[?#]', 2)[0].TrimEnd('/') -split '/')[-1].ToLower()
+ $_pinRocm211 = $false
+ # Anchor ($) so a suffixed custom leaf (rocm7.2-private) falls through to verbatim.
+ if ($_pinLeaf -match '^rocm(\d+)\.(\d+)$') {
+ # Only KNOWN-2.11 rocm (rocm7.2) gets the floor. Matches Test-RocmKnown211Version.
+ $_pinRocm211 = ([int]$Matches[1] -eq 7 -and [int]$Matches[2] -eq 2)
+ }
+ # Only the 2.11-allowlist gfx arches need the floor; others publish <2.11 and stay bare.
+ $_pinGfx211 = @('gfx120x-all', 'gfx1151', 'gfx1150') -contains $_pinLeaf
+ if ($_pinGfx211 -or $_pinRocm211) {
+ $ROCmIndexUrl = $TorchIndexUrl
+ $ROCmTorchFloor = "torch>=2.11.0,<2.12.0"
+ $PinnedRocmVisionSpec = "torchvision>=0.26.0,<0.27.0"
+ $PinnedRocmAudioSpec = "torchaudio>=2.11.0,<2.12.0"
+ substep "pinned ROCm index ($_pinLeaf) -- enforcing $ROCmTorchFloor" "Cyan"
+ } elseif ($_pinLeaf -match '^gfx[0-9]' -or $_pinLeaf -match '^rocm[0-9]+(\.[0-9]+)?$') {
+ # Other gfx / older rocm (<=7.1) ship torch <2.11; route via the ROCm path with
+ # bare specs. Only EXACT rocm/gfx* are families; a suffixed leaf is verbatim.
+ $ROCmIndexUrl = $TorchIndexUrl
+ }
+ }
+
if ($ROCmIndexUrl) {
$TorchIndexFamily = "rocm"
} else {
@@ -2164,14 +2259,14 @@ exit 0
}
if ($_Migrated) {
- # Migrated env: force-reinstall unsloth+unsloth-zoo to ensure clean state
- # in the new venv location, while preserving existing torch/CUDA
+ # Migrated env: force-reinstall unsloth+unsloth-zoo for a clean state, preserving
+ # existing torch/CUDA unless the flavor repair below re-lands it.
Write-TauriLog "STEP" "Installing unsloth"
substep "upgrading unsloth in migrated environment..."
if ($SkipTorch) {
# No-torch: install unsloth + unsloth-zoo with --no-deps, then
# runtime deps (typer, safetensors, transformers, etc.) with --no-deps.
- $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (migrated no-torch)" { uv pip install --python $VenvPython --no-deps --reinstall-package unsloth --reinstall-package unsloth-zoo "unsloth>=2026.7.3" "unsloth-zoo>=2026.7.3" }
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (migrated no-torch)" { uv pip install --python $VenvPython --no-deps --reinstall-package unsloth --reinstall-package unsloth-zoo "unsloth>=2026.7.4" "unsloth-zoo>=2026.7.4" }
if ($baseInstallExit -eq 0) {
# Resolve pydantic WITH deps so pip pins pydantic-core
# to the matching version (no-torch-runtime.txt below
@@ -2185,7 +2280,7 @@ exit 0
}
}
} else {
- $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (migrated)" { uv pip install --python $VenvPython --reinstall-package unsloth --reinstall-package unsloth-zoo "unsloth>=2026.7.3" "unsloth-zoo>=2026.7.3" }
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (migrated)" { uv pip install --python $VenvPython --reinstall-package unsloth --reinstall-package unsloth-zoo "unsloth>=2026.7.4" "unsloth-zoo>=2026.7.4" }
}
if ($baseInstallExit -ne 0) {
Write-Host "[ERROR] Failed to install unsloth (exit code $baseInstallExit)" -ForegroundColor Red
@@ -2210,22 +2305,24 @@ exit 0
substep "skipping PyTorch (--no-torch flag set)." "Yellow"
} elseif ($ROCmIndexUrl) {
Write-TauriLog "STEP" "Installing PyTorch (AMD ROCm Windows)"
- substep "installing PyTorch from $ROCmIndexUrl..."
+ substep "installing PyTorch from $(Remove-IndexUrlCredentials $ROCmIndexUrl)..."
$torchSpec = if ($ROCmTorchFloor) { $ROCmTorchFloor } else { "torch" }
# Pin the companions to match $torchSpec; bare names can resolve an
# ABI-incompatible torchvision/torchaudio on AMD's per-arch index.
- $visionSpec = if ($ROCmGfxArch -and $torchvisionFloorMap.ContainsKey($ROCmGfxArch)) { $torchvisionFloorMap[$ROCmGfxArch] } else { "torchvision" }
- $audioSpec = if ($ROCmGfxArch -and $torchaudioFloorMap.ContainsKey($ROCmGfxArch)) { $torchaudioFloorMap[$ROCmGfxArch] } else { "torchaudio" }
+ $visionSpec = if ($PinnedRocmVisionSpec) { $PinnedRocmVisionSpec } elseif ($ROCmGfxArch -and $torchvisionFloorMap -and $torchvisionFloorMap.ContainsKey($ROCmGfxArch)) { $torchvisionFloorMap[$ROCmGfxArch] } else { "torchvision" }
+ $audioSpec = if ($PinnedRocmAudioSpec) { $PinnedRocmAudioSpec } elseif ($ROCmGfxArch -and $torchaudioFloorMap -and $torchaudioFloorMap.ContainsKey($ROCmGfxArch)) { $torchaudioFloorMap[$ROCmGfxArch] } else { "torchaudio" }
$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; Unsloth setup retries ROCm afterwards.
+ # Transient AMD-index failure: fall back to a CPU base (Unsloth setup retries
+ # ROCm). Use an explicit CPU index -- for a pinned ROCm index $TorchIndexUrl IS
+ # the ROCm mirror, so reusing it would just retry it.
+ $CpuFallbackIndexUrl = if ($env:UNSLOTH_PYTORCH_MIRROR) { "$($env:UNSLOTH_PYTORCH_MIRROR.TrimEnd('/'))/cpu" } else { "https://download.pytorch.org/whl/cpu" }
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
# the companions -- a mismatched venv the flavor-repair block won't fix.
- $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (CPU fallback)" { uv pip install --python $VenvPython --force-reinstall "torch>=2.4,<2.11.0" torchvision torchaudio --default-index $TorchIndexUrl }
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (CPU fallback)" { uv pip install --python $VenvPython --force-reinstall "torch>=2.4,<2.11.0" "torchvision>=0.19,<0.26.0" "torchaudio>=2.4,<2.11.0" --default-index $CpuFallbackIndexUrl }
if ($torchInstallExit -ne 0) {
Write-Host "[ERROR] Failed to install PyTorch (ROCm and CPU base both failed, exit code $torchInstallExit)" -ForegroundColor Red
return (Exit-InstallFailure "Failed to install PyTorch (exit code $torchInstallExit)" $torchInstallExit)
@@ -2238,8 +2335,14 @@ exit 0
}
} else {
Write-TauriLog "STEP" "Installing PyTorch"
- substep "installing PyTorch ($TorchIndexUrl)..."
- $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch" { uv pip install --python $VenvPython "torch>=2.4,<2.11.0" torchvision torchaudio --default-index $TorchIndexUrl }
+ substep "installing PyTorch ($(Remove-IndexUrlCredentials $TorchIndexUrl))..."
+ # Bound the companions to the capped torch on EVERY index, cu
+ # families included: torchaudio 2.11 dropped its exact torch pin from
+ # the wheel metadata, so a bare companion next to torch<2.11 can
+ # resolve a mismatched 2.11.0 build. Mirrors install.sh.
+ $_pinVisionSpec = "torchvision>=0.19,<0.26.0"
+ $_pinAudioSpec = "torchaudio>=2.4,<2.11.0"
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch" { uv pip install --python $VenvPython "torch>=2.4,<2.11.0" $_pinVisionSpec $_pinAudioSpec --default-index $TorchIndexUrl }
if ($torchInstallExit -ne 0) {
Write-Host "[ERROR] Failed to install PyTorch (exit code $torchInstallExit)" -ForegroundColor Red
return (Exit-InstallFailure "Failed to install PyTorch (exit code $torchInstallExit)" $torchInstallExit)
@@ -2251,7 +2354,7 @@ exit 0
if ($SkipTorch) {
# No-torch: install unsloth + unsloth-zoo with --no-deps, then
# runtime deps (typer, safetensors, transformers, etc.) with --no-deps.
- $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (no-torch)" { uv pip install --python $VenvPython --no-deps --upgrade-package unsloth --upgrade-package unsloth-zoo "unsloth>=2026.7.3" "unsloth-zoo>=2026.7.3" }
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (no-torch)" { uv pip install --python $VenvPython --no-deps --upgrade-package unsloth --upgrade-package unsloth-zoo "unsloth>=2026.7.4" "unsloth-zoo>=2026.7.4" }
if ($baseInstallExit -eq 0) {
# Same pydantic-with-deps trick as the migrated branch.
$baseInstallExit = Invoke-InstallCommandRetry -Label "install pydantic" { uv pip install --python $VenvPython pydantic }
@@ -2263,7 +2366,7 @@ exit 0
}
}
} elseif ($StudioLocalInstall) {
- $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (local)" { uv pip install --python $VenvPython --upgrade-package unsloth "unsloth>=2026.7.3" "unsloth-zoo>=2026.7.3" }
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (local)" { uv pip install --python $VenvPython --upgrade-package unsloth "unsloth>=2026.7.4" "unsloth-zoo>=2026.7.4" }
} else {
$baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth" { uv pip install --python $VenvPython --upgrade-package unsloth -- "$PackageName" }
}
@@ -2291,7 +2394,7 @@ exit 0
Write-TauriLog "STEP" "Installing unsloth"
substep "installing unsloth (this may take a few minutes)..."
if ($StudioLocalInstall) {
- $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (auto torch backend)" { uv pip install --python $VenvPython "unsloth-zoo>=2026.7.3" "unsloth>=2026.7.3" --torch-backend=auto }
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (auto torch backend)" { uv pip install --python $VenvPython "unsloth-zoo>=2026.7.4" "unsloth>=2026.7.4" --torch-backend=auto }
if ($baseInstallExit -ne 0) {
Write-Host "[ERROR] Failed to install unsloth (exit code $baseInstallExit)" -ForegroundColor Red
return (Exit-InstallFailure "Failed to install unsloth (exit code $baseInstallExit)" $baseInstallExit)
@@ -2335,8 +2438,8 @@ exit 0
$rocmSpec = if ($ROCmTorchFloor) { $ROCmTorchFloor } else { "torch" }
# Pin companions like the fresh ROCm path (bare names can pull an
# ABI-incompatible torchvision/torchaudio from the per-arch index).
- $visionSpec = if ($ROCmGfxArch -and $torchvisionFloorMap.ContainsKey($ROCmGfxArch)) { $torchvisionFloorMap[$ROCmGfxArch] } else { "torchvision" }
- $audioSpec = if ($ROCmGfxArch -and $torchaudioFloorMap.ContainsKey($ROCmGfxArch)) { $torchaudioFloorMap[$ROCmGfxArch] } else { "torchaudio" }
+ $visionSpec = if ($PinnedRocmVisionSpec) { $PinnedRocmVisionSpec } elseif ($ROCmGfxArch -and $torchvisionFloorMap -and $torchvisionFloorMap.ContainsKey($ROCmGfxArch)) { $torchvisionFloorMap[$ROCmGfxArch] } else { "torchvision" }
+ $audioSpec = if ($PinnedRocmAudioSpec) { $PinnedRocmAudioSpec } elseif ($ROCmGfxArch -and $torchaudioFloorMap -and $torchaudioFloorMap.ContainsKey($ROCmGfxArch)) { $torchaudioFloorMap[$ROCmGfxArch] } else { "torchaudio" }
substep "PyTorch flavor mismatch (installed $installedTorchTag, need ROCm) -- reinstalling correct build..." "Yellow"
$torchFixExit = Invoke-InstallCommand { uv pip install --python $VenvPython --force-reinstall --default-index $ROCmIndexUrl $rocmSpec $visionSpec $audioSpec }
if ($torchFixExit -ne 0) {
@@ -2347,7 +2450,7 @@ exit 0
} elseif ($expectedTorchTag -ne 'rocm') {
# CUDA: stale +cpu (or wrong cuXXX) against a CUDA index -> reinstall triplet.
substep "PyTorch flavor mismatch (installed $installedTorchTag, need $expectedTorchTag) -- reinstalling correct build..." "Yellow"
- $torchFixExit = Invoke-InstallCommand { uv pip install --python $VenvPython "torch>=2.4,<2.11.0" torchvision torchaudio --default-index $TorchIndexUrl --reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio }
+ $torchFixExit = Invoke-InstallCommand { uv pip install --python $VenvPython "torch>=2.4,<2.11.0" "torchvision>=0.19,<0.26.0" "torchaudio>=2.4,<2.11.0" --default-index $TorchIndexUrl --reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio }
if ($torchFixExit -ne 0) {
Write-Host "[ERROR] Failed to reinstall PyTorch with the correct CUDA build (exit code $torchFixExit)" -ForegroundColor Red
return (Exit-InstallFailure "Failed to reinstall PyTorch ($expectedTorchTag) (exit code $torchFixExit)" $torchFixExit)
diff --git a/install.sh b/install.sh
index 7918a2bd23..0acccdf049 100755
--- a/install.sh
+++ b/install.sh
@@ -159,18 +159,58 @@ run_maybe_quiet() {
fi
}
+# Trim trailing slashes from the URL PATH only, preserving ?query / #fragment: a whole-URL
+# strip corrupts a token ending in "/", a single strip leaves .../cu128// empty. Shared.
+_trim_index_path_slashes() {
+ _tips_v="$1"
+ case "$_tips_v" in
+ *[?#]*)
+ _tips_head="${_tips_v%%[?#]*}"
+ _tips_tail="${_tips_v#"$_tips_head"}"
+ ;;
+ *)
+ _tips_head="$_tips_v"
+ _tips_tail=""
+ ;;
+ esac
+ while [ -n "$_tips_head" ] && [ "${_tips_head%/}" != "$_tips_head" ]; do
+ _tips_head="${_tips_head%/}"
+ done
+ printf '%s%s' "$_tips_head" "$_tips_tail"
+}
+
+# Redact index-URL credentials (userinfo + ?query= + #fragment) from captured installer
+# output before printing on failure; uv/pip errors echo the failing --index-url verbatim.
+# Mirrors the other installers. Verbose mode streams uncaptured, so it isn't redacted.
+_redact_install_output() {
+ sed -E \
+ -e 's#(https?://)[^/@[:space:]`]+@#\1@#g' \
+ -e 's#([?&][^=[:space:]&`]+)=[^[:space:]`]+#\1=#g' \
+ -e 's|(https?://[^[:space:]`#]+)#[^[:space:]`]+|\1#|g' \
+ "$@"
+}
+
run_install_cmd() {
_label="$1"
shift
- # Installer-pinned index installs (torch) must beat an inherited uv mirror
- # (#6898): when we pass --default-index, neutralize every uv index env var so
- # the pinned index wins. Other installs keep the user's mirror.
+ # Installer-pinned index installs (torch) must beat an inherited uv mirror (#6898):
+ # for --default-index, neutralize the uv index/backend/config vars (UV_TORCH_BACKEND
+ # redirects torch; UV_NO_CONFIG=1 + dropping UV_CONFIG_FILE stops a uv.toml/pyproject
+ # index outranking the CLI pin, uv 0.10).
case " $* " in
- *" --default-index "*) set -- env -u UV_DEFAULT_INDEX -u UV_INDEX_URL -u UV_INDEX -u UV_EXTRA_INDEX_URL "$@" ;;
+ *" --default-index "*) set -- env -u UV_DEFAULT_INDEX -u UV_INDEX_URL -u UV_INDEX -u UV_EXTRA_INDEX_URL -u UV_TORCH_BACKEND -u UV_FIND_LINKS -u UV_CONFIG_FILE UV_NO_CONFIG=1 "$@" ;;
esac
if _is_verbose; then
- "$@" && return 0
- _rc=$?
+ # Stream through the redactor: uv echoes index URLs (credentials and
+ # all) in its errors, and verbose mode previously bypassed the
+ # redaction the quiet path applies. The rc file preserves the
+ # command's exit code across the pipe without relying on pipefail
+ # (this script runs under plain sh).
+ _rcf=$(mktemp)
+ { "$@" 2>&1; printf '%s' "$?" > "$_rcf"; } | _redact_install_output
+ _rc=$(cat "$_rcf" 2>/dev/null || echo 1)
+ rm -f "$_rcf"
+ [ "${_rc:-1}" -eq 0 ] 2>/dev/null && return 0
step "error" "$_label failed (exit code $_rc)" "$C_ERR" >&2
return "$_rc"
fi
@@ -178,7 +218,7 @@ run_install_cmd() {
"$@" >"$_log" 2>&1 && { rm -f "$_log"; return 0; }
_rc=$?
step "error" "$_label failed (exit code $_rc)" "$C_ERR" >&2
- cat "$_log" >&2
+ _redact_install_output "$_log" >&2
rm -f "$_log"
return $_rc
}
@@ -257,7 +297,7 @@ _install_bnb_rocm() {
fi
_bnb_rc=$?
if _is_verbose; then
- cat "$_bnb_log" >&2
+ _redact_install_output "$_bnb_log" >&2
fi
rm -f "$_bnb_log"
step "warning" "$_label (pre-release) failed (exit code $_bnb_rc)" "$C_WARN" >&2
@@ -310,6 +350,11 @@ _tauri_torch_index_family() {
return
fi
_diag_url="${1:-}"
+ # Strip query/fragment AND a trailing slash before classifying (like _torch_index_url_leaf):
+ # a token isn't echoed into [TAURI:DIAG], and .../cu128/?token=x still classifies as cu128.
+ _diag_url="${_diag_url%%\?*}"
+ _diag_url="${_diag_url%%#*}"
+ _diag_url="${_diag_url%/}"
case "$_diag_url" in
*/cu118) echo "cu118" ;;
*/cu124) echo "cu124" ;;
@@ -343,7 +388,8 @@ _tauri_gpu_branch() {
return
fi
case "$_diag_family" in
- cu*) echo "cuda" ;;
+ # Require a digit after cu so /current or /custom isn't branded CUDA (parity ^cu[0-9]).
+ cu[0-9]*) echo "cuda" ;;
rocm*)
if [ "$_diag_radeon" = true ]; then
echo "rocm_radeon"
@@ -1575,6 +1621,12 @@ _has_usable_nvidia_gpu() {
# the STUDIO_HOME mkdir/venv so the origin distro is untouched.
_maybe_reroute_strixhalo_to_2404() {
[ "${OS:-}" = "wsl" ] || return 0
+ # An explicit index pin skips every GPU-driven reroute (same contract as
+ # the later Radeon/Strix guard): the pin is honored in THIS distro rather
+ # than probing the GPU and switching distributions. Whitespace-only
+ # overrides do not gate (parity with get_torch_index_url).
+ _rr_pin=$(printf '%s' "${UNSLOTH_TORCH_INDEX_URL:-}${UNSLOTH_TORCH_INDEX_FAMILY:-}" | tr -d '[:space:]')
+ [ -n "$_rr_pin" ] && return 0
[ "${SKIP_TORCH:-false}" = "false" ] || return 0
[ "${UNSLOTH_SKIP_ROCM_WSL_SETUP:-0}" = "1" ] && return 0
[ "${UNSLOTH_WSL_REROUTED:-0}" = "1" ] && return 0
@@ -1636,6 +1688,10 @@ _maybe_reroute_strixhalo_to_2404() {
# Forward explicit ROCm-bootstrap consent (e.g. Tauri) so the child auto-enables the
# GPU instead of falling back to the desktop-app prompt path.
[ "${UNSLOTH_ROCM_WSL_AUTO:-0}" = "1" ] && _rr_exports="$_rr_exports; export UNSLOTH_ROCM_WSL_AUTO=1"
+ # Forward a pinned torch index into the rerouted distro; dropping it would
+ # silently revert the child install to auto-detection.
+ [ -n "${UNSLOTH_TORCH_INDEX_URL:-}" ] && _rr_exports="$_rr_exports; export UNSLOTH_TORCH_INDEX_URL=$(_rr_q "$UNSLOTH_TORCH_INDEX_URL")"
+ [ -n "${UNSLOTH_TORCH_INDEX_FAMILY:-}" ] && _rr_exports="$_rr_exports; export UNSLOTH_TORCH_INDEX_FAMILY=$(_rr_q "$UNSLOTH_TORCH_INDEX_FAMILY")"
[ "$_SKIP_AUTOSTART" = true ] && _rr_exports="$_rr_exports; export UNSLOTH_SKIP_AUTOSTART=1"
_rr_args=""
[ "$PACKAGE_NAME" != "unsloth" ] && _rr_args="$_rr_args --package $(_rr_q "$PACKAGE_NAME")"
@@ -2001,6 +2057,15 @@ if [ "$SKIP_TORCH" = false ] && [ "$OS" = "macos" ] && [ "$_ARCH" = "arm64" ]; t
TORCH_CONSTRAINT="torch>=2.6,<2.11.0"
fi
fi
+# Companion (torchvision/torchaudio) constraints, bounded to torch's window.
+# torchaudio 2.11 dropped its exact torch pin, so a bare companion next to a
+# <2.11-capped torch resolves torchaudio 2.11 (verified: cpu leaf installed
+# torch 2.10.0+cpu with torchaudio 2.11.0+cpu). torchvision still exact-pins
+# torch and self-corrects, but is bounded for symmetry. Widened alongside the
+# cu* torch window below; the torch-2.11 AMD paths (rocm7.2 / per-gfx / Strix)
+# pin their own trio.
+TORCHVISION_CONSTRAINT="torchvision>=0.19,<0.26.0"
+TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<2.11.0"
# ── Resolve repo root (for --local installs) ──
_REPO_ROOT="$(cd "$(dirname "$0" 2>/dev/null || echo ".")" && pwd)"
@@ -2069,6 +2134,24 @@ _has_amd_rocm_gpu() {
get_torch_index_url() {
_base="${UNSLOTH_PYTORCH_MIRROR:-https://download.pytorch.org/whl}"
_base="${_base%/}"
+ # Explicit override -- skip ALL GPU probing (headless / container / CI / cross-install).
+ # UNSLOTH_TORCH_INDEX_URL wins (full URL, verbatim); _FAMILY is the leaf (cpu, cu128, ...)
+ # appended to the mirror base. Trim whitespace so a whitespace-only value is unset.
+ _url="${UNSLOTH_TORCH_INDEX_URL:-}"
+ _url="${_url#"${_url%%[![:space:]]*}"}"; _url="${_url%"${_url##*[![:space:]]}"}"
+ if [ -n "$_url" ]; then
+ # Trim trailing PATH slashes (a multi-slash path 404s on strict pip proxies) while
+ # preserving a ?query/#fragment token (a whole-URL strip would eat a "/"-ending token).
+ _url=$(_trim_index_path_slashes "$_url")
+ echo "$_url"; return
+ fi
+ _family="${UNSLOTH_TORCH_INDEX_FAMILY:-}"
+ _family="${_family#"${_family%%[![:space:]]*}"}"; _family="${_family%"${_family##*[![:space:]]}"}"
+ if [ -n "$_family" ]; then
+ while [ "${_family#/}" != "$_family" ]; do _family="${_family#/}"; done
+ while [ "${_family%/}" != "$_family" ]; do _family="${_family%/}"; done
+ echo "$_base/$_family"; return
+ fi
# macOS: always CPU (no CUDA support)
case "$(uname -s)" in Darwin) echo "$_base/cpu"; return ;; esac
# Try nvidia-smi -- require the binary to actually list a usable GPU.
@@ -2197,6 +2280,45 @@ _torch_flavor_tag() {
esac
}
+# Final path segment of a wheel index URL ($1), lowercased, query/fragment stripped first
+# so a token-authenticated pin (.../cu128?token=x) classifies as cu128 (else it reinstalls
+# every update). Classification only. Shared with the py / ps1 leaf extractors.
+_torch_index_url_leaf() {
+ _tl_u="${1%%\?*}"
+ _tl_u="${_tl_u%%#*}"
+ # Strip ALL trailing slashes, not one: .../rocm7.2// must yield rocm7.2, not an empty leaf.
+ while [ -n "$_tl_u" ] && [ "${_tl_u%/}" != "$_tl_u" ]; do
+ _tl_u="${_tl_u%/}"
+ done
+ printf '%s' "${_tl_u##*/}" | tr '[:upper:]' '[:lower:]'
+}
+
+# True (exit 0) when a lowercased leaf is an EXACT pip ROCm family: rocm[.]
+# or a gfx ARCHITECTURE leaf (gfx followed by a digit: gfx90a, gfx1151, gfx120x-all). A leaf
+# that merely starts with rocm/gfx (rocm7.2-private, gfx-private) is a custom verbatim pin.
+# Matches the py / ps1 sides.
+_is_pip_rocm_family_leaf() {
+ case "$1" in
+ gfx[0-9]*) return 0 ;;
+ rocm[0-9]*)
+ # Exact rocm[.]: both major and minor must be non-empty all-digits
+ # (rocm7., rocm7.2.1, rocm7.2-private are all custom pins, not a family).
+ _rocm_rest="${1#rocm}"
+ case "$_rocm_rest" in
+ *.*.*) return 1 ;;
+ *.*)
+ _rocm_minor="${_rocm_rest#*.}"
+ case "${_rocm_rest%%.*}" in "" | *[!0-9]*) return 1 ;; esac
+ case "$_rocm_minor" in "" | *[!0-9]*) return 1 ;; esac
+ ;;
+ *[!0-9]*) return 1 ;;
+ esac
+ return 0
+ ;;
+ *) return 1 ;;
+ esac
+}
+
# Whether release base $1 (X.Y[.Z...]) falls inside constraint window $2
# ("torch>=A.B[.C],
# rocm). Empty on an unknown leaf (odd mirror) so the repair safely no-ops.
_expected_torch_flavor_tag() {
- _u="${1%/}"
- _leaf="${_u##*/}"
+ _leaf=$(_torch_index_url_leaf "$1")
case "$_leaf" in
- cu[0-9]*) echo "$_leaf" ;;
- cpu) echo "cpu" ;;
- rocm*|gfx*) echo "rocm" ;;
- *) echo "" ;;
+ cu[0-9]*)
+ # Exact cu + digits only; a cu*-suffixed leaf (cu128-private) -> "" (custom),
+ # else a correct +cu128 wheel is force-reinstalled every run.
+ case "${_leaf#cu}" in
+ *[!0-9]*) echo "" ;;
+ *) echo "$_leaf" ;;
+ esac
+ ;;
+ cpu) echo "cpu" ;;
+ # Exact rocm/gfx families only; a custom rocm*-suffixed leaf -> "" (custom).
+ *)
+ if _is_pip_rocm_family_leaf "$_leaf"; then echo "rocm"; else echo ""; fi
+ ;;
esac
}
@@ -2308,14 +2438,42 @@ _expected_torch_flavor_tag() {
# fresh-install paths above already use -- so a stale wheel is auto-repairable.
# Unknown/odd-mirror leaves -> no, so we warn rather than risk a wrong reinstall.
_torch_index_repairable() {
- _u="${1%/}"
- _leaf="${_u##*/}"
+ _leaf=$(_torch_index_url_leaf "$1")
case "$_leaf" in
- cu[0-9]*|rocm[0-9]*|gfx*) echo "yes" ;;
- *) echo "no" ;;
+ cu[0-9]*) echo "yes" ;;
+ # Only EXACT rocm/gfx families resolve via --default-index; a suffixed leaf is verbatim.
+ *)
+ if _is_pip_rocm_family_leaf "$_leaf"; then echo "yes"; else echo "no"; fi
+ ;;
esac
}
+# Remove credentials from a wheel index URL ($1) so an authenticated pin never leaks:
+# drops userinfo AND query/fragment; scheme/host/path stay exact. Shared with py / ps1.
+_strip_index_url_credentials() {
+ _sic_url="$1"
+ case "$_sic_url" in
+ *://*) ;;
+ *) printf '%s' "$_sic_url"; return ;;
+ esac
+ _sic_scheme="${_sic_url%%://*}"
+ _sic_rest="${_sic_url#*://}"
+ # Drop query / fragment (may hold auth tokens).
+ _sic_rest="${_sic_rest%%\?*}"
+ _sic_rest="${_sic_rest%%#*}"
+ _sic_auth="${_sic_rest%%/*}"
+ # Drop user:pass@ userinfo if present.
+ case "$_sic_auth" in
+ *@*) _sic_host="${_sic_auth##*@}" ;;
+ *) _sic_host="$_sic_auth" ;;
+ esac
+ if [ "$_sic_auth" = "$_sic_rest" ]; then
+ printf '%s://%s' "$_sic_scheme" "$_sic_host"
+ else
+ printf '%s://%s/%s' "$_sic_scheme" "$_sic_host" "${_sic_rest#*/}"
+ fi
+}
+
get_radeon_wheel_url() {
# Only meaningful on Linux. Picks a repo.radeon.com base URL whose listing
# contains torch wheels. Tries paths like rocm-rel-7.2.1/, rocm-rel-7.2/,
@@ -2561,7 +2719,19 @@ _maybe_bootstrap_rocm_wsl() {
[ -n "$_rw_tmp" ] && rm -f "$_rw_tmp"
return 0
}
-_maybe_bootstrap_rocm_wsl || true
+# When the caller pins the wheel index (UNSLOTH_TORCH_INDEX_URL / _FAMILY), honour it
+# everywhere: skip the WSL ROCm bootstrap and the Radeon/Strix reroute below (which would
+# re-probe the GPU and overwrite the pin). Trim whitespace first (parity with
+# get_torch_index_url): a whitespace-only override is unset there, so must not flip this true.
+_torch_index_pinned=false
+_ti_url_trim="${UNSLOTH_TORCH_INDEX_URL:-}"
+_ti_url_trim="${_ti_url_trim#"${_ti_url_trim%%[![:space:]]*}"}"; _ti_url_trim="${_ti_url_trim%"${_ti_url_trim##*[![:space:]]}"}"
+_ti_family_trim="${UNSLOTH_TORCH_INDEX_FAMILY:-}"
+_ti_family_trim="${_ti_family_trim#"${_ti_family_trim%%[![:space:]]*}"}"; _ti_family_trim="${_ti_family_trim%"${_ti_family_trim##*[![:space:]]}"}"
+if [ -n "$_ti_url_trim" ] || [ -n "$_ti_family_trim" ]; then
+ _torch_index_pinned=true
+fi
+[ "$_torch_index_pinned" = true ] || _maybe_bootstrap_rocm_wsl || true
TORCH_INDEX_URL=$(get_torch_index_url)
@@ -2572,29 +2742,74 @@ TORCH_INDEX_URL=$(get_torch_index_url)
# whose base path happens to contain "rocm" or "gfx" must not mislabel a
# cu*/cpu index as ROCm (radeon repo URLs end in rocm-rel-X.Y/, Strix
# overrides in gfxNNNN/, so the trailing slash is stripped first).
-_torch_index_leaf="${TORCH_INDEX_URL%/}"
+# Lowercase the leaf so every gfx*/rocm*/cu* arm matches regardless of case (canonical AMD
+# RDNA4 leaf is gfx120X-all). CUDA is branded only on a real cu[0-9]* leaf, so a mirror
+# leaf (/current) does NOT commit a CUDA backend; an unknown leaf leaves the var unset so
+# the stack probes the GPU. Query/fragment dropped first, then ALL trailing slashes (in
+# lockstep with the shared _torch_index_url_leaf extractor).
+_torch_index_leaf="${TORCH_INDEX_URL%%\?*}"
+_torch_index_leaf="${_torch_index_leaf%%#*}"
+# Strip ALL trailing slashes, not one: .../cu128// must yield cu128, not an empty leaf.
+while [ -n "$_torch_index_leaf" ] && [ "${_torch_index_leaf%/}" != "$_torch_index_leaf" ]; do
+ _torch_index_leaf="${_torch_index_leaf%/}"
+done
_torch_index_leaf="${_torch_index_leaf##*/}"
+_torch_index_leaf=$(printf '%s' "$_torch_index_leaf" | tr '[:upper:]' '[:lower:]')
case "$_torch_index_leaf" in
rocm*|gfx*) export UNSLOTH_TORCH_BACKEND="rocm" ;;
cpu) export UNSLOTH_TORCH_BACKEND="cpu" ;;
- *) export UNSLOTH_TORCH_BACKEND="cuda" ;;
+ cu[0-9]*) export UNSLOTH_TORCH_BACKEND="cuda" ;;
+ # Unknown leaf (odd mirror, /current): unset so a stale inherited value can't leak and
+ # the stack probes the GPU.
+ *) unset UNSLOTH_TORCH_BACKEND ;;
esac
-# 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.
+# Whether TORCH_INDEX_URL names an actual pip ROCm family (rocm* / gfx*), gating the
+# ROCm-only side effects below (AMD bitsandbytes, ROCm-torch repair). Digit-gated so a leaf
+# merely STARTING with "rocm" isn't force-repaired from the wrong path.
+if _is_pip_rocm_family_leaf "$_torch_index_leaf"; then
+ _torch_index_is_rocm_family=true
+else
+ _torch_index_is_rocm_family=false
+fi
+
+# rocm7.2 and the per-gfx indexes with the _grouped_mm <2.11 bug (gfx120X-all, gfx1151,
+# gfx1150) ship torch 2.11.0 -- raise the floor (also covers a pinned override that skipped
+# the Strix reroute). Pin the companions too: the per-gfx index publishes them independently
+# and a bare name can resolve a 2.12 ABI-mismatched wheel. Match on the FINAL leaf so a
+# custom mirror with a gfx/rocm7.2 path segment but a cu*/cpu family isn't forced.
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" ;;
+ rocm7.2|gfx120x-all|gfx1151|gfx1150)
+ TORCH_CONSTRAINT="torch>=2.11.0,<2.12.0"
+ TORCHVISION_CONSTRAINT="torchvision>=0.26.0,<0.27.0"
+ TORCHAUDIO_CONSTRAINT="torchaudio>=2.11.0,<2.12.0"
+ ;;
+ # CUDA cu12x/cu13x indexes ship torch 2.11.x: widen the ceiling to <2.12.0 (matches
+ # _CUDA_TORCH_PKG_SPEC) and widen the companions with it so the trio stays paired.
+ cu[0-9]*)
+ TORCH_CONSTRAINT="torch>=2.4,<2.12.0"
+ TORCHVISION_CONSTRAINT="torchvision>=0.19,<0.27.0"
+ TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<2.12.0"
+ ;;
esac
+# A pinned custom/unknown-leaf index (/simple, /current, /cu128-private) has no curated
+# companion set, so bound torchvision/torchaudio to the same <2.11 range the Python path pins
+# (else a mirror with newer companions resolves a 2.12 ABI-mismatched wheel). Known families
+# keep their curated companions above (_expected_torch_flavor_tag returns "" only for custom).
+if [ "$_torch_index_pinned" = true ] && \
+ [ -z "$(_expected_torch_flavor_tag "$TORCH_INDEX_URL")" ]; then
+ TORCHVISION_CONSTRAINT="torchvision>=0.19,<0.26.0"
+ TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<2.11.0"
+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".
+# Skipped when the index is pinned: an explicit override must not be rerouted to the
+# Radeon/Strix repos by GPU probing.
_amd_gpu_radeon=false
+if [ "$_torch_index_pinned" = false ]; then
case "$TORCH_INDEX_URL" in
*/rocm*)
if _has_amd_rocm_gpu && command -v rocminfo >/dev/null 2>&1 && \
@@ -2671,10 +2886,14 @@ case "$TORCH_INDEX_URL" in
done
TORCH_INDEX_URL="${_amd_strix_base}/${_strix_gfx}/"
TORCH_CONSTRAINT="torch>=2.11.0,<2.12.0"
+ # Pin companions to 2.11 (per-gfx index publishes them independently).
+ TORCHVISION_CONSTRAINT="torchvision>=0.26.0,<0.27.0"
+ TORCHAUDIO_CONSTRAINT="torchaudio>=2.11.0,<2.12.0"
_amd_gpu_radeon=false
fi
;;
esac
+fi # _torch_index_pinned guard (Radeon + Strix reroute)
# 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
@@ -2821,7 +3040,7 @@ case "$TORCH_INDEX_URL" in
if [ "$_amd_gpu_radeon" = true ]; then
substep "wheels: repo.radeon.com (Radeon)"
else
- substep "wheels: $TORCH_INDEX_URL"
+ substep "wheels: $(_strip_index_url_credentials "$TORCH_INDEX_URL")"
fi
;;
esac
@@ -2867,8 +3086,8 @@ for _p in ('torch', 'torchvision', 'torchaudio'):
}
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
+ # Migrated env: force-reinstall unsloth+unsloth-zoo for a clean state, preserving
+ # existing torch/CUDA unless the ROCm repair below fires.
substep "upgrading unsloth in migrated environment..."
if [ "$SKIP_TORCH" = true ]; then
# No-torch: install unsloth + unsloth-zoo with --no-deps (current
@@ -2877,7 +3096,7 @@ if [ "$_MIGRATED" = true ]; then
# to prevent transitive torch resolution.
run_install_cmd_retry "install unsloth (migrated no-torch)" uv pip install --python "$_VENV_PY" --no-deps \
--reinstall-package unsloth --reinstall-package unsloth-zoo \
- "unsloth>=2026.7.3" "unsloth-zoo>=2026.7.3"
+ "unsloth>=2026.7.4" "unsloth-zoo>=2026.7.4"
# Resolve pydantic WITH deps so pip pins pydantic-core to the
# matching version (no-torch-runtime.txt below is --no-deps).
# All transitive deps are torch-free.
@@ -2894,7 +3113,7 @@ if [ "$_MIGRATED" = true ]; then
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:-}
+ "unsloth>=2026.7.4" "unsloth-zoo>=2026.7.4" ${_MLX_LM_EXCLUDE_ARG:-}
[ -n "$_UNSLOTH_TORCH_OVERRIDES" ] && rm -f "$_UNSLOTH_TORCH_OVERRIDES"
_UNSLOTH_TORCH_OVERRIDES=""
fi
@@ -2909,18 +3128,14 @@ if [ "$_MIGRATED" = true ]; then
# AMD ROCm: install bitsandbytes even in migrated environments so
# existing ROCm installs gain the AMD bitsandbytes build without a
# fresh reinstall.
- if [ "$SKIP_TORCH" = false ]; then
- case "$TORCH_INDEX_URL" in
- */rocm*|*/gfx*)
- _install_bnb_rocm "install bitsandbytes (AMD)" "$_VENV_PY"
- # Repair ROCm torch if overwritten during migrated install
- _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)..."
- _install_torch_default_index --force-reinstall
- fi
- ;;
- esac
+ if [ "$SKIP_TORCH" = false ] && [ "$_torch_index_is_rocm_family" = true ]; then
+ _install_bnb_rocm "install bitsandbytes (AMD)" "$_VENV_PY"
+ # Repair ROCm torch if overwritten during migrated install
+ _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)..."
+ _install_torch_default_index --force-reinstall
+ fi
fi
elif [ -n "$TORCH_INDEX_URL" ]; then
# Fresh: Step 1 - install torch from explicit index (skip when --no-torch or Intel Mac)
@@ -3074,7 +3289,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"
+ substep "[WARN] Radeon repo lacks a compatible wheel set for this Python; falling back to ROCm index ($(_strip_index_url_credentials "$TORCH_INDEX_URL"))" "$C_WARN"
_install_torch_default_index
else
substep "installing PyTorch from Radeon repo (${_RADEON_BASE_URL})..."
@@ -3095,7 +3310,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
fi
fi
else
- substep "[WARN] Radeon repo unavailable; falling back to ROCm index ($TORCH_INDEX_URL)" "$C_WARN"
+ substep "[WARN] Radeon repo unavailable; falling back to ROCm index ($(_strip_index_url_credentials "$TORCH_INDEX_URL"))" "$C_WARN"
_install_torch_default_index
fi
else
@@ -3103,19 +3318,15 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
_install_torch_default_index
fi
else
- substep "installing PyTorch ($TORCH_INDEX_URL)..."
+ substep "installing PyTorch ($(_strip_index_url_credentials "$TORCH_INDEX_URL"))..."
_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
# host stays in GGUF-only mode rather than pulling in bitsandbytes,
# which is only useful once torch is present for training.
- if [ "$SKIP_TORCH" = false ]; then
- case "$TORCH_INDEX_URL" in
- */rocm*|*/gfx*)
- _install_bnb_rocm "install bitsandbytes (AMD)" "$_VENV_PY"
- ;;
- esac
+ if [ "$SKIP_TORCH" = false ] && [ "$_torch_index_is_rocm_family" = true ]; then
+ _install_bnb_rocm "install bitsandbytes (AMD)" "$_VENV_PY"
fi
# Fresh: Step 2 - install unsloth, preserving the torch Step 1 installed
tauri_log "STEP" "Installing Unsloth"
@@ -3126,7 +3337,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
# runtime deps (typer, safetensors, transformers, etc.) with --no-deps.
run_install_cmd_retry "install unsloth (no-torch)" uv pip install --python "$_VENV_PY" --no-deps \
--upgrade-package unsloth --upgrade-package unsloth-zoo \
- "unsloth>=2026.7.3" "unsloth-zoo>=2026.7.3"
+ "unsloth>=2026.7.4" "unsloth-zoo>=2026.7.4"
# Same pydantic-with-deps trick as the migrated branch.
run_install_cmd_retry "install pydantic (with deps for compatible core)" \
uv pip install --python "$_VENV_PY" pydantic
@@ -3145,7 +3356,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
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"
+ --upgrade-package unsloth "unsloth>=2026.7.4" "unsloth-zoo>=2026.7.4"
substep "overlaying local repo (editable)..."
run_install_cmd "overlay local repo" uv pip install --python "$_VENV_PY" -e "$_REPO_ROOT" --no-deps
substep "overlaying unsloth-zoo from git main..."
@@ -3161,23 +3372,19 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
_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
- case "$TORCH_INDEX_URL" in
- */rocm*|*/gfx*)
- _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)..."
- _install_torch_default_index --force-reinstall
- fi
- ;;
- esac
+ if [ "$SKIP_TORCH" = false ] && [ "$_torch_index_is_rocm_family" = 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)..."
+ _install_torch_default_index --force-reinstall
+ fi
fi
else
# Fallback: GPU detection failed to produce a URL -- let uv resolve torch
tauri_log "STEP" "Installing Unsloth"
substep "installing unsloth (this may take a few minutes)..."
if [ "$STUDIO_LOCAL_INSTALL" = true ]; then
- run_install_cmd_retry "install unsloth (auto torch backend)" uv pip install --python "$_VENV_PY" "unsloth-zoo>=2026.7.3" "unsloth>=2026.7.3" --torch-backend=auto
+ run_install_cmd_retry "install unsloth (auto torch backend)" uv pip install --python "$_VENV_PY" "unsloth-zoo>=2026.7.4" "unsloth>=2026.7.4" --torch-backend=auto
substep "overlaying local repo (editable)..."
run_install_cmd "overlay local repo" uv pip install --python "$_VENV_PY" -e "$_REPO_ROOT" --no-deps
substep "overlaying unsloth-zoo from git main..."
@@ -3217,7 +3424,7 @@ if [ "$SKIP_TORCH" = false ] && [ -n "${TORCH_INDEX_URL:-}" ]; then
substep "[WARN] PyTorch is CPU-only but a $_expected_torch_tag GPU build was expected for this machine." "$C_WARN"
substep "[WARN] Training and GPU inference will run on CPU until this is fixed." "$C_WARN"
substep "[WARN] Re-run this installer, or reinstall the GPU build manually:" "$C_WARN"
- substep "[WARN] uv pip install --python \"$_VENV_PY\" \"$TORCH_CONSTRAINT\" torchvision torchaudio --default-index $TORCH_INDEX_URL --reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio" "$C_WARN"
+ substep "[WARN] uv pip install --python \"$_VENV_PY\" \"$TORCH_CONSTRAINT\" \"$TORCHVISION_CONSTRAINT\" \"$TORCHAUDIO_CONSTRAINT\" --default-index $(_strip_index_url_credentials "$TORCH_INDEX_URL") --reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio" "$C_WARN"
fi
fi
fi
diff --git a/pyproject.toml b/pyproject.toml
index 7b8fdd100d..071258eb8f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -74,7 +74,7 @@ triton = [
]
huggingfacenotorch = [
- "unsloth_zoo>=2026.7.3",
+ "unsloth_zoo>=2026.7.4",
"wheel>=0.42.0",
"packaging",
"numpy",
@@ -95,7 +95,7 @@ huggingfacenotorch = [
]
huggingface = [
"unsloth[huggingfacenotorch]",
- "unsloth_zoo>=2026.7.3",
+ "unsloth_zoo>=2026.7.4",
"torchvision",
"unsloth[triton]",
]
@@ -580,7 +580,7 @@ colab-ampere-torch220 = [
"flash-attn>=2.6.3 ; ('linux' in sys_platform)",
]
colab-new = [
- "unsloth_zoo>=2026.7.3",
+ "unsloth_zoo>=2026.7.4",
"packaging",
"tyro",
"transformers>=4.51.3,!=4.52.0,!=4.52.1,!=4.52.2,!=4.52.3,!=4.53.0,!=4.54.0,!=4.55.0,!=4.55.1,!=4.57.0,!=4.57.4,!=4.57.5,!=5.0.0,!=5.1.0,<=5.5.0",
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index d7c7eed518..2c7433f7a4 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,
@@ -7515,8 +7563,9 @@ class LlamaCppBackend:
else:
self._api_key = None
- # Windows + full offload: disable KV checkpoints (WDDM/PCI-E
- # overhead). CPU/partial offload keeps prompt caching. #5692.
+ # Windows + full offload: drop the host-RAM KV checkpoints that cause
+ # WDDM/PCI-E overhead, but keep prompt caching (in-VRAM prefix reuse) so
+ # a repeated prompt is not re-prefilled on every request. #5692.
if sys.platform == "win32" and full_offload_tuning_active:
unsupported_cache_flags: list[str] = []
if server_caps.get("supports_cache_ram"):
@@ -7527,10 +7576,6 @@ class LlamaCppBackend:
cmd.extend(["--ctx-checkpoints", "0"])
else:
unsupported_cache_flags.append("--ctx-checkpoints")
- if server_caps.get("supports_no_cache_prompt"):
- cmd.append("--no-cache-prompt")
- else:
- unsupported_cache_flags.append("--no-cache-prompt")
if unsupported_cache_flags:
logger.info(
"Skipping unsupported Windows cache flags for llama-server: %s",
@@ -8105,6 +8150,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 +8744,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 +9274,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.
@@ -9614,6 +9903,75 @@ class LlamaCppBackend:
except Exception:
logger.debug("Could not close httpx client", exc_info = True)
+ @staticmethod
+ def _install_cancel_aware_read(
+ client: "httpx.Client",
+ cancel_event: threading.Event,
+ response: Optional["httpx.Response"] = None,
+ poll_s: float = 0.2,
+ ) -> None:
+ """Wrap the httpcore stream so the reader interrupts its own blocked recv() on cancel.
+
+ A cross-thread socket shutdown wakes a parked recv() on POSIX but not on
+ Windows (Winsock), so read in short slices and poll cancel_event between them
+ (plain or TLS); slice timeouts are swallowed so a slow-but-alive stream survives.
+ httpcore snapshots request.extensions["timeout"]["read"] once at body start, so
+ given ``response`` we re-read the live value per call to honor the post-first-token
+ stall timeout instead of the long prefill timeout."""
+ import httpcore
+
+ def _live_read_timeout() -> Optional[float]:
+ if response is None:
+ return None
+ try:
+ ext = response.request.extensions.get("timeout")
+ if isinstance(ext, dict):
+ value = ext.get("read")
+ if isinstance(value, (int, float)):
+ return float(value)
+ except Exception:
+ pass
+ return None
+
+ try:
+ pool = getattr(getattr(client, "_transport", None), "_pool", None)
+ for connection in list(getattr(pool, "_connections", []) or []):
+ inner = getattr(connection, "_connection", None)
+ stream = getattr(inner, "_network_stream", None)
+ if stream is None or getattr(stream, "_unsloth_cancel_wrapped", False):
+ continue
+ orig_read = stream.read
+
+ def read(
+ max_bytes,
+ timeout = None,
+ _orig = orig_read,
+ ):
+ live = _live_read_timeout()
+ effective = live if live is not None else timeout
+ deadline = None if effective is None else time.monotonic() + effective
+ while True:
+ if cancel_event.is_set():
+ raise httpcore.ReadError("stream cancelled by user")
+ if deadline is None:
+ step = poll_s
+ else:
+ remaining = deadline - time.monotonic()
+ if remaining <= 0:
+ raise httpcore.ReadTimeout("read operation timed out")
+ step = min(poll_s, remaining)
+ try:
+ return _orig(max_bytes, timeout = step)
+ except httpcore.ReadTimeout:
+ if deadline is not None and time.monotonic() >= deadline:
+ raise
+ continue # slow but alive: keep reading
+
+ stream.read = read
+ stream._unsloth_cancel_wrapped = True
+ except Exception:
+ logger.debug("Could not install cancel-aware read", exc_info = True)
+
@staticmethod
@contextlib.contextmanager
def _stream_with_retry(
@@ -9671,6 +10029,11 @@ class LlamaCppBackend:
headers = headers,
) as response:
_response_ref[0] = response
+ if cancel_event is not None:
+ # Portable mid-stream cancel: the reader polls cancel itself, so
+ # Stop interrupts a stalled read where the watcher's Windows socket
+ # shutdown does not. Pass response to honor the live stall timeout.
+ LlamaCppBackend._install_cancel_aware_read(client, cancel_event, response)
if cancel_event is not None and cancel_event.is_set():
raise _LlamaStreamCancelled
yield response
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/core/inference/orchestrator.py b/studio/backend/core/inference/orchestrator.py
index eaa474d9b8..75ef9c2399 100644
--- a/studio/backend/core/inference/orchestrator.py
+++ b/studio/backend/core/inference/orchestrator.py
@@ -54,9 +54,8 @@ class GenStreamError(str):
"""A stream chunk carrying a real backend/generation error, not model text.
Subclasses str so existing display/logging consumers are unaffected, while
- callers that must abort a distributed run on error (raise_on_streamed_error)
- can distinguish a real error from model output whose visible text starts with
- "Error:" by checking isinstance(chunk, GenStreamError).
+ callers can distinguish a real error from model output whose visible text
+ starts with "Error:" by checking isinstance(chunk, GenStreamError).
"""
__slots__ = ("public",)
diff --git a/studio/backend/core/rag/store.py b/studio/backend/core/rag/store.py
index f9128d1715..1165b6bb0e 100644
--- a/studio/backend/core/rag/store.py
+++ b/studio/backend/core/rag/store.py
@@ -158,6 +158,16 @@ def list_documents(conn: sqlite3.Connection, scope: str) -> list[dict]:
return [dict(r) for r in rows]
+def list_all_documents(conn: sqlite3.Connection) -> list[dict]:
+ """Every uploaded document across all scopes (KBs, threads, projects)."""
+ rows = conn.execute(
+ "SELECT id, scope, kb_id, thread_id, project_id, filename, sha256, status, error, "
+ "num_chunks, stored_path, created_at "
+ "FROM documents ORDER BY created_at DESC"
+ ).fetchall()
+ return [dict(r) for r in rows]
+
+
def get_document(conn: sqlite3.Connection, document_id: str) -> dict | None:
row = conn.execute("SELECT * FROM documents WHERE id=?", (document_id,)).fetchone()
return dict(row) if row else None
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/studio/backend/hub/routes/__init__.py b/studio/backend/hub/routes/__init__.py
index e9579635b0..7c5cfb9b3c 100644
--- a/studio/backend/hub/routes/__init__.py
+++ b/studio/backend/hub/routes/__init__.py
@@ -5,8 +5,10 @@
from hub.routes.inventory import router as inventory_router
from hub.routes.datasets import router as datasets_router
+from hub.routes.token import router as token_router
__all__ = [
"inventory_router",
"datasets_router",
+ "token_router",
]
diff --git a/studio/backend/hub/routes/token.py b/studio/backend/hub/routes/token.py
new file mode 100644
index 0000000000..1b7ad733a2
--- /dev/null
+++ b/studio/backend/hub/routes/token.py
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Hugging Face token validation endpoint."""
+
+from __future__ import annotations
+
+import asyncio
+from typing import Literal, Optional
+
+from fastapi import APIRouter, Depends, Request
+from pydantic import BaseModel
+
+from auth.authentication import get_current_subject
+from hub.dependencies import get_hf_token
+from utils.client_ip import client_ip
+from utils.hf_token_validation import validate_hf_token
+
+
+router = APIRouter()
+
+
+class HfTokenValidationResponse(BaseModel):
+ status: Literal["missing", "valid", "invalid", "rate_limited", "unavailable"]
+ retry_after_seconds: Optional[int] = None
+
+
+@router.post("/token/validate", response_model = HfTokenValidationResponse)
+async def validate_token(
+ request: Request,
+ hf_token: Optional[str] = Depends(get_hf_token),
+ current_subject: str = Depends(get_current_subject),
+):
+ if not hf_token:
+ return HfTokenValidationResponse(status = "missing")
+ result = await asyncio.to_thread(
+ validate_hf_token,
+ hf_token,
+ rate_key = f"{current_subject}:{client_ip(request)}",
+ )
+ return HfTokenValidationResponse(
+ status = result.status,
+ retry_after_seconds = result.retry_after_seconds,
+ )
diff --git a/studio/backend/main.py b/studio/backend/main.py
index 81d4c16e52..f686e29bf5 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -312,6 +312,7 @@ from routes.preview import router as preview_router
from hub.routes import (
inventory_router as hub_inventory_router,
datasets_router as hub_datasets_router,
+ token_router as hub_token_router,
)
from hub.schemas.downloads import TransportCapabilities
from hub.utils.download_registry import (
@@ -547,8 +548,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).
@@ -992,6 +994,7 @@ app.include_router(rag_router, prefix = "/api/rag", tags = ["rag"])
app.include_router(training_history_router, prefix = "/api/train", tags = ["training-history"])
app.include_router(hub_inventory_router, prefix = "/api/hub", tags = ["hub"])
app.include_router(hub_datasets_router, prefix = "/api/hub/datasets", tags = ["hub"])
+app.include_router(hub_token_router, prefix = "/api/hub", tags = ["hub"])
# Re-wrap client-error responses on the /v1/* surface into OpenAI/Anthropic
# error envelopes; non-/v1 paths keep FastAPI's default {"detail": ...} shape.
@@ -1148,11 +1151,15 @@ def _get_cached_system_gpu_info(logger) -> dict[str, Any]:
util = util_devices.get(idx, {})
total_vram = util.get("vram_total_gb") or dev.get("memory_total_gb") or 0
- used_vram = util.get("vram_used_gb") or 0
+ # Keep None (usage unknown, e.g. Windows ROCm perf counter) so the UI
+ # shows unknown, not a fabricated 0 used / full free.
+ used_vram = util.get("vram_used_gb")
enriched_dev = dict(dev)
enriched_dev["vram_used_gb"] = used_vram
- enriched_dev["vram_free_gb"] = round(total_vram - used_vram, 2) if total_vram else 0
+ enriched_dev["vram_free_gb"] = (
+ round(total_vram - used_vram, 2) if total_vram and used_vram is not None else None
+ )
enriched_dev["vram_utilization_pct"] = util.get("vram_utilization_pct")
enriched_devices.append(enriched_dev)
diff --git a/studio/backend/routes/chat_history.py b/studio/backend/routes/chat_history.py
index 7a27a58a52..24b6dfb36d 100644
--- a/studio/backend/routes/chat_history.py
+++ b/studio/backend/routes/chat_history.py
@@ -5,7 +5,7 @@
Chat history API routes backed by studio.db.
"""
-from typing import Any, Literal, Optional
+from typing import Annotated, Any, Literal, Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel, ConfigDict, Field, ValidationError
@@ -19,13 +19,16 @@ from storage.studio_db import (
clear_chat_history,
count_chat_threads,
count_forks_for_message,
+ delete_chat_attachment,
delete_chat_threads,
delete_chat_project,
ensure_chat_project_workspace,
fork_chat_thread,
+ get_chat_attachment,
get_chat_project,
get_chat_thread,
get_chat_message,
+ list_chat_attachments_page,
list_chat_projects,
list_chat_legacy_imports,
list_chat_settings,
@@ -279,6 +282,131 @@ async def delete_threads(
return {"status": "deleted"}
+@router.get("/attachments")
+def list_attachments(
+ limit: Annotated[int, Query(ge = 1, le = 100)] = 50,
+ offset: Annotated[int, Query(ge = 0)] = 0,
+ current_subject: str = Depends(get_current_subject),
+) -> dict:
+ """One bounded page of chat uploads for the settings Data tab."""
+ attachments, next_offset = list_chat_attachments_page(limit = limit, offset = offset)
+ return {"attachments": attachments, "nextOffset": next_offset}
+
+
+def _decode_attachment_base64(payload: str) -> bytes:
+ """Strict base64 decode of a stored payload.
+
+ Normalizes first: strips whitespace, fixes padding, accepts the URL-safe
+ alphabet. validate=False would silently drop bad characters and serve
+ corrupted bytes instead of failing, so raise 422 on anything else.
+ """
+ import base64
+
+ normalized = "".join(payload.split())
+ altchars = b"-_" if ("-" in normalized or "_" in normalized) else None
+ normalized += "=" * (-len(normalized) % 4)
+ try:
+ return base64.b64decode(normalized, altchars = altchars, validate = True)
+ except Exception as exc: # noqa: BLE001 - corrupt stored payload
+ raise HTTPException(status_code = 422, detail = "Attachment data is corrupt") from exc
+
+
+_AUDIO_FORMAT_MEDIA_TYPES = {
+ "mp3": "audio/mpeg",
+ "wav": "audio/wav",
+ "ogg": "audio/ogg",
+ "flac": "audio/flac",
+}
+
+
+def _safe_image_media_type(media_type: str) -> str:
+ """Clamp a data-URL media type to something inert to render.
+
+ Imported chats store image parts verbatim, so the embedded type can be
+ text/html or image/svg+xml; echoing those would execute markup with the
+ app origin when opened. Anything not a plain raster type downloads as
+ bytes instead.
+ """
+ lowered = media_type.strip().lower()
+ if lowered.startswith("image/") and lowered != "image/svg+xml":
+ return lowered
+ return "application/octet-stream"
+
+
+@router.get("/attachments/{message_id}/{attachment_id}/file")
+def get_attachment_file(
+ message_id: str,
+ attachment_id: str,
+ current_subject: str = Depends(get_current_subject),
+):
+ """Serve one attachment's stored content: image or audio bytes, or
+ extracted text."""
+ import urllib.parse
+
+ from fastapi.responses import Response
+
+ attachment = get_chat_attachment(message_id, attachment_id)
+ if attachment is None:
+ raise HTTPException(status_code = 404, detail = "Attachment not found")
+
+ attachment_content_type = attachment.get("contentType")
+ texts: list[str] = []
+ for part in attachment.get("content") or []:
+ if not isinstance(part, dict):
+ continue
+ image = part.get("image")
+ if isinstance(image, str) and image[:5].lower() == "data:":
+ header, _, payload = image.partition(",")
+ media_type = _safe_image_media_type(
+ header[5:].split(";", 1)[0] or "application/octet-stream"
+ )
+ if "base64" not in header.lower():
+ # RFC 2397 non-base64 form stores percent-encoded bytes.
+ data = urllib.parse.unquote_to_bytes(payload)
+ return Response(content = data, media_type = media_type)
+ data = _decode_attachment_base64(payload)
+ return Response(content = data, media_type = media_type)
+ # Audio parts: the attachment adapter stores {data, format} with raw
+ # base64; compare chats store a bare base64 string.
+ audio = part.get("audio")
+ if isinstance(audio, dict) or (isinstance(audio, str) and audio):
+ if isinstance(audio, dict):
+ payload = audio.get("data")
+ audio_format = audio.get("format")
+ else:
+ payload = audio.rsplit(",", 1)[-1]
+ audio_format = None
+ if isinstance(payload, str) and payload:
+ data = _decode_attachment_base64(payload)
+ media_type = (
+ attachment_content_type
+ if isinstance(attachment_content_type, str)
+ and attachment_content_type.startswith("audio/")
+ else _AUDIO_FORMAT_MEDIA_TYPES.get(
+ str(audio_format or "").lower(), "application/octet-stream"
+ )
+ )
+ return Response(content = data, media_type = media_type)
+ text = part.get("text")
+ if isinstance(text, str) and text:
+ texts.append(text)
+ if texts:
+ return Response(content = "\n".join(texts), media_type = "text/plain; charset=utf-8")
+ raise HTTPException(status_code = 404, detail = "Attachment has no stored content")
+
+
+@router.delete("/attachments/{message_id}/{attachment_id}")
+def delete_attachment(
+ message_id: str,
+ attachment_id: str,
+ current_subject: str = Depends(get_current_subject),
+) -> dict:
+ """Remove one attachment from its chat message."""
+ if not delete_chat_attachment(message_id, attachment_id):
+ raise HTTPException(status_code = 404, detail = "Attachment not found")
+ return {"ok": True}
+
+
@router.get("/projects", response_model = ChatProjectListResponse)
async def list_projects(
include_archived: bool = Query(False), current_subject: str = Depends(get_current_subject)
@@ -409,7 +537,7 @@ async def get_thread_message(
@router.put("/threads/{thread_id}/messages/{message_id}", response_model = ChatMessage)
-async def save_thread_message(
+def save_thread_message(
thread_id: str,
message_id: str,
payload: ChatMessage,
@@ -432,7 +560,7 @@ async def save_thread_message(
@router.put("/threads/{thread_id}/messages", response_model = ChatMessageListResponse)
-async def replace_thread_messages(
+def replace_thread_messages(
thread_id: str,
payload: ChatMessageSyncRequest,
current_subject: str = Depends(get_current_subject),
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 136e4f7645..afd942e9a5 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -1778,7 +1778,7 @@ from core.inference.providers import get_base_url
from core.inference.external_provider import ExternalProviderClient
from core.inference.chat_templates import resolve_effective_chat_template_override
from storage import providers_db
-from utils.utils import safe_error_detail, log_and_http_error
+from utils.utils import is_hf_authentication_error, safe_error_detail, log_and_http_error
import io
import base64
@@ -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
@@ -5244,6 +5244,14 @@ async def validate_model(
raise HTTPException(status_code = 400, detail = str(e))
except Exception as e:
redacted_msg = redact_native_paths(str(e))
+ if is_hf_authentication_error(e):
+ raise HTTPException(
+ status_code = 400,
+ detail = (
+ "Hugging Face authentication failed. Check or clear the token "
+ "in Settings, and confirm access to this gated repository."
+ ),
+ )
if _is_unsupported_nvfp4_inference_error(redacted_msg):
logger.warning(
"NVFP4 inference is not supported yet while validating '%s'",
diff --git a/studio/backend/routes/rag.py b/studio/backend/routes/rag.py
index e20fea74a3..392a4e0d02 100644
--- a/studio/backend/routes/rag.py
+++ b/studio/backend/routes/rag.py
@@ -318,6 +318,39 @@ def list_project_documents(project_id: str, subject: str = Depends(get_current_s
conn.close()
+@router.get("/documents")
+def list_all_uploaded_documents(subject: str = Depends(get_current_subject)) -> dict:
+ """Every uploaded file across chats, projects, and knowledge bases (settings
+ Data tab)."""
+ _require_rag()
+ conn = rag_db.get_connection()
+ try:
+ docs = store.list_all_documents(conn)
+ kb_names = {kb["id"]: kb["name"] for kb in store.list_kbs(conn)}
+ finally:
+ conn.close()
+
+ from storage.studio_db import list_chat_projects
+
+ project_names = {p["id"]: p["name"] for p in list_chat_projects(include_archived = True)}
+
+ out = []
+ for doc in docs:
+ view = _doc_view(doc)
+ stored_path = doc.get("stored_path")
+ size = None
+ if stored_path:
+ try:
+ size = os.path.getsize(stored_path)
+ except OSError:
+ size = None
+ view["sizeBytes"] = size
+ view["kbName"] = kb_names.get(doc.get("kb_id"))
+ view["projectName"] = project_names.get(doc.get("project_id"))
+ out.append(view)
+ return {"documents": out}
+
+
@router.delete("/documents/{document_id}")
def delete_document(document_id: str, subject: str = Depends(get_current_subject)) -> dict:
_require_rag()
@@ -424,8 +457,10 @@ _CONTENT_TYPES = {
".txt": "text/plain; charset=utf-8",
".md": "text/markdown; charset=utf-8",
".markdown": "text/markdown; charset=utf-8",
- ".html": "text/html; charset=utf-8",
- ".htm": "text/html; charset=utf-8",
+ # Served as plain text, never text/html: an uploaded HTML document rendered
+ # same-origin would execute its scripts with access to the app's storage.
+ ".html": "text/plain; charset=utf-8",
+ ".htm": "text/plain; charset=utf-8",
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
}
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/storage/studio_db.py b/studio/backend/storage/studio_db.py
index 4e0c711b69..d889894d04 100644
--- a/studio/backend/storage/studio_db.py
+++ b/studio/backend/storage/studio_db.py
@@ -7,6 +7,7 @@ Like auth/storage.py (module-level functions, raw sqlite3, per-function
connections) plus WAL mode and PRAGMA foreign_keys = ON for CASCADE deletes.
"""
+import hashlib
import json
import logging
import os
@@ -100,6 +101,7 @@ _schema_lock = threading.Lock()
_schema_ready = False
_SQLITE_IN_CHUNK_SIZE = 900
_PROJECT_WORKSPACE_SUBDIRS = ("sandbox",)
+_CHAT_ATTACHMENT_INVENTORY_VERSION = 1
def _project_slug(name: str) -> str:
@@ -313,6 +315,141 @@ def _ensure_schema(conn: sqlite3.Connection) -> None:
)
"""
)
+ tombstone_schema = """
+ CREATE TABLE chat_attachment_tombstones (
+ thread_id TEXT NOT NULL REFERENCES chat_threads(id) ON DELETE CASCADE,
+ message_id TEXT NOT NULL,
+ attachment_id TEXT NOT NULL,
+ deleted_at INTEGER NOT NULL,
+ PRIMARY KEY(thread_id, message_id, attachment_id)
+ ) WITHOUT ROWID
+ """
+ tombstone_table = conn.execute(
+ """
+ SELECT 1 FROM sqlite_master
+ WHERE type = 'table' AND name = 'chat_attachment_tombstones'
+ """
+ ).fetchone()
+ if tombstone_table is None:
+ conn.execute(tombstone_schema)
+ else:
+ tombstone_columns = {
+ row[1] for row in conn.execute("PRAGMA table_info(chat_attachment_tombstones)")
+ }
+ tombstone_fk_targets = {
+ row[2] for row in conn.execute("PRAGMA foreign_key_list(chat_attachment_tombstones)")
+ }
+ if "thread_id" not in tombstone_columns or "chat_threads" not in tombstone_fk_targets:
+ # The first implementation cascaded through chat_messages, which
+ # erased deletion knowledge during pruneMissing. Rebuild once,
+ # retaining every tombstone whose owning thread still exists.
+ conn.execute("SAVEPOINT migrate_chat_attachment_tombstones")
+ try:
+ conn.execute(
+ "ALTER TABLE chat_attachment_tombstones "
+ "RENAME TO chat_attachment_tombstones_legacy"
+ )
+ conn.execute(tombstone_schema)
+ if "thread_id" in tombstone_columns:
+ conn.execute(
+ """
+ INSERT OR IGNORE INTO chat_attachment_tombstones
+ (thread_id, message_id, attachment_id, deleted_at)
+ SELECT legacy.thread_id, legacy.message_id,
+ legacy.attachment_id, legacy.deleted_at
+ FROM chat_attachment_tombstones_legacy legacy
+ JOIN chat_threads thread ON thread.id = legacy.thread_id
+ """
+ )
+ else:
+ conn.execute(
+ """
+ INSERT OR IGNORE INTO chat_attachment_tombstones
+ (thread_id, message_id, attachment_id, deleted_at)
+ SELECT message.thread_id, legacy.message_id,
+ legacy.attachment_id, legacy.deleted_at
+ FROM chat_attachment_tombstones_legacy legacy
+ JOIN chat_messages message ON message.id = legacy.message_id
+ """
+ )
+ conn.execute("DROP TABLE chat_attachment_tombstones_legacy")
+ conn.execute("RELEASE SAVEPOINT migrate_chat_attachment_tombstones")
+ except Exception:
+ conn.execute("ROLLBACK TO SAVEPOINT migrate_chat_attachment_tombstones")
+ conn.execute("RELEASE SAVEPOINT migrate_chat_attachment_tombstones")
+ raise
+ conn.execute(
+ """
+ CREATE TABLE IF NOT EXISTS chat_attachment_inventory (
+ message_id TEXT NOT NULL REFERENCES chat_messages(id) ON DELETE CASCADE,
+ attachment_id TEXT NOT NULL,
+ name TEXT NOT NULL,
+ type TEXT,
+ content_type TEXT,
+ size_bytes INTEGER,
+ PRIMARY KEY(message_id, attachment_id)
+ ) WITHOUT ROWID
+ """
+ )
+ conn.execute(
+ """
+ CREATE TABLE IF NOT EXISTS chat_attachment_inventory_state (
+ singleton INTEGER NOT NULL PRIMARY KEY CHECK(singleton = 1),
+ inventory_version INTEGER NOT NULL DEFAULT 0,
+ dirty INTEGER NOT NULL DEFAULT 1,
+ backfilled_at INTEGER NOT NULL
+ )
+ """
+ )
+ inventory_state_columns = {
+ row[1] for row in conn.execute("PRAGMA table_info(chat_attachment_inventory_state)")
+ }
+ if "inventory_version" not in inventory_state_columns:
+ conn.execute(
+ "ALTER TABLE chat_attachment_inventory_state "
+ "ADD COLUMN inventory_version INTEGER NOT NULL DEFAULT 0"
+ )
+ if "dirty" not in inventory_state_columns:
+ conn.execute(
+ "ALTER TABLE chat_attachment_inventory_state "
+ "ADD COLUMN dirty INTEGER NOT NULL DEFAULT 1"
+ )
+ conn.execute(
+ """
+ CREATE TRIGGER IF NOT EXISTS chat_attachment_inventory_dirty_insert
+ AFTER INSERT ON chat_messages
+ BEGIN
+ INSERT INTO chat_attachment_inventory_state
+ (singleton, inventory_version, dirty, backfilled_at)
+ VALUES (1, 0, 1, 0)
+ ON CONFLICT(singleton) DO UPDATE SET dirty = 1;
+ END
+ """
+ )
+ conn.execute(
+ """
+ CREATE TRIGGER IF NOT EXISTS chat_attachment_inventory_dirty_update
+ AFTER UPDATE ON chat_messages
+ BEGIN
+ INSERT INTO chat_attachment_inventory_state
+ (singleton, inventory_version, dirty, backfilled_at)
+ VALUES (1, 0, 1, 0)
+ ON CONFLICT(singleton) DO UPDATE SET dirty = 1;
+ END
+ """
+ )
+ conn.execute(
+ """
+ CREATE TRIGGER IF NOT EXISTS chat_attachment_inventory_dirty_delete
+ AFTER DELETE ON chat_messages
+ BEGIN
+ INSERT INTO chat_attachment_inventory_state
+ (singleton, inventory_version, dirty, backfilled_at)
+ VALUES (1, 0, 1, 0)
+ ON CONFLICT(singleton) DO UPDATE SET dirty = 1;
+ END
+ """
+ )
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_chat_threads_model_type_created_at ON chat_threads(model_type, created_at)"
)
@@ -391,6 +528,21 @@ def _ensure_schema(conn: sqlite3.Connection) -> None:
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_prompt_lists_created_at ON prompt_lists(created_at)"
)
+ inventory_state = conn.execute(
+ """
+ SELECT inventory_version, dirty
+ FROM chat_attachment_inventory_state
+ WHERE singleton = 1
+ """
+ ).fetchone()
+ if (
+ inventory_state is None
+ or inventory_state["inventory_version"] != _CHAT_ATTACHMENT_INVENTORY_VERSION
+ or inventory_state["dirty"]
+ ):
+ _rebuild_chat_attachment_inventory(conn)
+ _mark_chat_attachment_inventory_clean(conn)
+ conn.commit()
def _prompt_entry_from_row(row: sqlite3.Row) -> dict:
@@ -1219,7 +1371,14 @@ def delete_chat_threads(ids: list[str]) -> None:
return
conn = get_connection()
try:
+ conn.execute("BEGIN IMMEDIATE")
+ _ensure_chat_attachment_inventory_current(conn)
+ conn.executemany(
+ "DELETE FROM chat_attachment_tombstones WHERE thread_id = ?",
+ [(id,) for id in ids],
+ )
conn.executemany("DELETE FROM chat_threads WHERE id = ?", [(id,) for id in ids])
+ _mark_chat_attachment_inventory_clean(conn)
conn.commit()
finally:
conn.close()
@@ -1228,7 +1387,11 @@ def delete_chat_threads(ids: list[str]) -> None:
def clear_chat_history() -> None:
conn = get_connection()
try:
+ conn.execute("BEGIN IMMEDIATE")
+ _ensure_chat_attachment_inventory_current(conn)
+ conn.execute("DELETE FROM chat_attachment_tombstones")
conn.execute("DELETE FROM chat_threads")
+ _mark_chat_attachment_inventory_clean(conn)
conn.commit()
finally:
conn.close()
@@ -1354,6 +1517,7 @@ def delete_chat_project(id: str, delete_files: bool = False) -> Optional[dict]:
conn = get_connection()
try:
conn.execute("BEGIN IMMEDIATE")
+ _ensure_chat_attachment_inventory_current(conn)
row = conn.execute("SELECT * FROM chat_projects WHERE id = ?", (id,)).fetchone()
if row is None:
conn.rollback()
@@ -1361,6 +1525,7 @@ def delete_chat_project(id: str, delete_files: bool = False) -> Optional[dict]:
project = _chat_project_from_row(row)
conn.execute("DELETE FROM chat_threads WHERE project_id = ?", (id,))
conn.execute("DELETE FROM chat_projects WHERE id = ?", (id,))
+ _mark_chat_attachment_inventory_clean(conn)
conn.commit()
if delete_files:
_delete_project_workspace(project)
@@ -1483,15 +1648,285 @@ def _recompute_chat_thread_updated_at(conn: sqlite3.Connection, thread_id: str)
)
+_CONTENT_PART_ID_PREFIX = "content-part-sha256-"
+_URI_SCHEME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:")
+
+
+def _is_locally_stored_blob(value: str) -> bool:
+ """True for data URIs or bare base64, never external/blob URI references."""
+ candidate = value.lstrip()
+ if not candidate:
+ return False
+ if candidate[:5].lower() == "data:":
+ return True
+ if candidate.startswith(("//", "\\\\")):
+ return False
+ return _URI_SCHEME_RE.match(candidate) is None
+
+
+def _managed_content_part_payload(part: dict) -> Optional[tuple[str, Any]]:
+ """Return the locally stored blob payload used to identify a content part."""
+ image = part.get("image")
+ if isinstance(image, str) and image[:5].lower() == "data:":
+ return "image", image
+
+ audio = part.get("audio")
+ if isinstance(audio, str) and _is_locally_stored_blob(audio):
+ return "audio", audio
+ if isinstance(audio, dict):
+ data = audio.get("data")
+ if isinstance(data, str) and _is_locally_stored_blob(data):
+ return "audio", audio
+ return None
+
+
+def _content_part_id(part: dict) -> Optional[str]:
+ """Stable managed id derived from blob data, without mutating inference content."""
+ payload = _managed_content_part_payload(part)
+ if payload is None:
+ return None
+ canonical = json.dumps(
+ payload,
+ ensure_ascii = False,
+ separators = (",", ":"),
+ sort_keys = True,
+ ).encode("utf-8")
+ return f"{_CONTENT_PART_ID_PREFIX}{hashlib.sha256(canonical).hexdigest()}"
+
+
+def _chat_attachment_tombstones_for_messages(
+ conn: sqlite3.Connection, thread_id: str, message_ids: list[str]
+) -> dict[str, set[str]]:
+ tombstones = {message_id: set() for message_id in message_ids}
+ unique_ids = list(dict.fromkeys(message_ids))
+ for start in range(0, len(unique_ids), _SQLITE_IN_CHUNK_SIZE):
+ chunk = unique_ids[start : start + _SQLITE_IN_CHUNK_SIZE]
+ placeholders = ",".join("?" for _ in chunk)
+ rows = conn.execute(
+ f"""
+ SELECT message_id, attachment_id
+ FROM chat_attachment_tombstones
+ WHERE thread_id = ? AND message_id IN ({placeholders})
+ """,
+ (thread_id, *chunk),
+ ).fetchall()
+ for row in rows:
+ tombstones[row["message_id"]].add(row["attachment_id"])
+ return tombstones
+
+
+def _reconcile_chat_message_uploads(message: dict, tombstones: set[str]) -> dict:
+ """Strip uploads previously deleted through the Data tab from a stale write."""
+ if not tombstones:
+ return message
+
+ reconciled = dict(message)
+ attachments = message.get("attachments")
+ if isinstance(attachments, list):
+ reconciled["attachments"] = [
+ attachment
+ for attachment in attachments
+ if not (isinstance(attachment, dict) and str(attachment.get("id") or "") in tombstones)
+ ]
+
+ content = message.get("content")
+ if isinstance(content, list):
+ reconciled["content"] = [
+ part
+ for part in content
+ if not (isinstance(part, dict) and (_content_part_id(part) or "") in tombstones)
+ ]
+ return reconciled
+
+
+def _chat_attachment_metadata_text(value, fallback: Optional[str] = None) -> Optional[str]:
+ """Keep untyped legacy/import metadata safe for SQLite binding."""
+ if value is None:
+ return fallback
+ if isinstance(value, str):
+ return value or fallback
+ if isinstance(value, (bool, int, float)):
+ return str(value)
+ # Objects and arrays are not useful display metadata and sqlite3 rejects
+ # binding them directly.
+ return fallback
+
+
+def _chat_attachment_inventory_entries(
+ attachments_json: Optional[str],
+ content_json: Optional[str],
+ tombstones: Optional[set[str]] = None,
+) -> list[dict]:
+ tombstones = tombstones or set()
+ attachments = _json_loads(attachments_json, None)
+ if not isinstance(attachments, list):
+ attachments = []
+ attachments = [
+ attachment
+ for attachment in attachments
+ if isinstance(attachment, dict) and attachment.get("id")
+ ]
+ attachments.extend(_content_part_attachments(content_json))
+
+ entries: list[dict] = []
+ seen: set[str] = set()
+ for attachment in attachments:
+ attachment_id = str(attachment["id"])
+ if attachment_id in seen or attachment_id in tombstones:
+ continue
+ seen.add(attachment_id)
+ entries.append(
+ {
+ "id": attachment_id,
+ "name": _chat_attachment_metadata_text(attachment.get("name"), "attachment"),
+ "type": _chat_attachment_metadata_text(attachment.get("type")),
+ "contentType": _chat_attachment_metadata_text(attachment.get("contentType")),
+ "sizeBytes": _chat_attachment_size_bytes(attachment),
+ }
+ )
+ return entries
+
+
+def _replace_chat_attachment_inventory(
+ conn: sqlite3.Connection,
+ message_id: str,
+ attachments_json: Optional[str],
+ content_json: Optional[str],
+ tombstones: Optional[set[str]] = None,
+) -> None:
+ conn.execute("DELETE FROM chat_attachment_inventory WHERE message_id = ?", (message_id,))
+ entries = _chat_attachment_inventory_entries(
+ attachments_json,
+ content_json,
+ tombstones,
+ )
+ conn.executemany(
+ """
+ INSERT INTO chat_attachment_inventory
+ (message_id, attachment_id, name, type, content_type, size_bytes)
+ VALUES (?, ?, ?, ?, ?, ?)
+ """,
+ [
+ (
+ message_id,
+ entry["id"],
+ entry["name"],
+ entry["type"],
+ entry["contentType"],
+ entry["sizeBytes"],
+ )
+ for entry in entries
+ ],
+ )
+
+
+def _mark_chat_attachment_inventory_clean(conn: sqlite3.Connection) -> None:
+ conn.execute(
+ """
+ INSERT INTO chat_attachment_inventory_state
+ (singleton, inventory_version, dirty, backfilled_at)
+ VALUES (1, ?, 0, ?)
+ ON CONFLICT(singleton) DO UPDATE SET
+ inventory_version = excluded.inventory_version,
+ dirty = 0,
+ backfilled_at = excluded.backfilled_at
+ """,
+ (
+ _CHAT_ATTACHMENT_INVENTORY_VERSION,
+ int(datetime.now(timezone.utc).timestamp() * 1000),
+ ),
+ )
+
+
+def _rebuild_chat_attachment_inventory(conn: sqlite3.Connection) -> None:
+ """Rebuild after schema upgrade or a write from an older Studio build."""
+ conn.execute("DELETE FROM chat_attachment_inventory")
+ tombstones: dict[tuple[str, str], set[str]] = {}
+ for row in conn.execute(
+ "SELECT thread_id, message_id, attachment_id FROM chat_attachment_tombstones"
+ ).fetchall():
+ tombstones.setdefault((row["thread_id"], row["message_id"]), set()).add(
+ row["attachment_id"]
+ )
+ rows = conn.execute(
+ "SELECT id, thread_id, attachments_json, content_json FROM chat_messages"
+ ).fetchall()
+ for row in rows:
+ _replace_chat_attachment_inventory(
+ conn,
+ row["id"],
+ row["attachments_json"],
+ row["content_json"],
+ tombstones.get((row["thread_id"], row["id"]), set()),
+ )
+
+
+def _ensure_chat_attachment_inventory_current(conn: sqlite3.Connection) -> None:
+ state = conn.execute(
+ """
+ SELECT inventory_version, dirty
+ FROM chat_attachment_inventory_state
+ WHERE singleton = 1
+ """
+ ).fetchone()
+ if (
+ state is not None
+ and state["inventory_version"] == _CHAT_ATTACHMENT_INVENTORY_VERSION
+ and not state["dirty"]
+ ):
+ return
+
+ owns_transaction = not conn.in_transaction
+ if owns_transaction:
+ conn.execute("BEGIN IMMEDIATE")
+ try:
+ state = conn.execute(
+ """
+ SELECT inventory_version, dirty
+ FROM chat_attachment_inventory_state
+ WHERE singleton = 1
+ """
+ ).fetchone()
+ if (
+ state is None
+ or state["inventory_version"] != _CHAT_ATTACHMENT_INVENTORY_VERSION
+ or state["dirty"]
+ ):
+ _rebuild_chat_attachment_inventory(conn)
+ _mark_chat_attachment_inventory_clean(conn)
+ if owns_transaction:
+ conn.commit()
+ except Exception:
+ if owns_transaction:
+ conn.rollback()
+ raise
+
+
def upsert_chat_message(message: dict) -> dict:
conn = get_connection()
try:
conn.execute("BEGIN IMMEDIATE")
+ _ensure_chat_attachment_inventory_current(conn)
_raise_if_chat_message_thread_conflicts(
conn,
message["threadId"],
[message["id"]],
)
+ tombstones = _chat_attachment_tombstones_for_messages(
+ conn,
+ message["threadId"],
+ [message["id"]],
+ )
+ reconciled = _reconcile_chat_message_uploads(
+ message,
+ tombstones.get(message["id"], set()),
+ )
+ content_json = json.dumps(reconciled.get("content", []))
+ attachments_json = (
+ json.dumps(reconciled.get("attachments"))
+ if reconciled.get("attachments") is not None
+ else None
+ )
conn.execute(
"""
INSERT INTO chat_messages
@@ -1507,23 +1942,32 @@ def upsert_chat_message(message: dict) -> dict:
WHERE excluded.thread_id = chat_messages.thread_id
""",
(
- message["id"],
- message["threadId"],
- message.get("parentId"),
- message["role"],
- json.dumps(message.get("content", [])),
- json.dumps(message.get("attachments"))
- if message.get("attachments") is not None
+ reconciled["id"],
+ reconciled["threadId"],
+ reconciled.get("parentId"),
+ reconciled["role"],
+ content_json,
+ attachments_json,
+ json.dumps(reconciled.get("metadata"))
+ if reconciled.get("metadata") is not None
else None,
- json.dumps(message.get("metadata"))
- if message.get("metadata") is not None
- else None,
- int(message["createdAt"]),
+ int(reconciled["createdAt"]),
),
)
- _bump_chat_thread_updated_at(conn, message["threadId"], int(message["createdAt"]))
+ _replace_chat_attachment_inventory(
+ conn,
+ reconciled["id"],
+ attachments_json,
+ content_json,
+ )
+ _bump_chat_thread_updated_at(
+ conn,
+ reconciled["threadId"],
+ int(reconciled["createdAt"]),
+ )
+ _mark_chat_attachment_inventory_clean(conn)
conn.commit()
- return message
+ return reconciled
except Exception:
conn.rollback()
raise
@@ -1539,13 +1983,28 @@ def sync_chat_messages(
conn = get_connection()
try:
conn.execute("BEGIN IMMEDIATE")
+ _ensure_chat_attachment_inventory_current(conn)
_raise_if_chat_message_thread_conflicts(
conn,
thread_id,
[m["id"] for m in messages],
)
- if prune_missing:
- conn.execute("DELETE FROM chat_messages WHERE thread_id = ?", (thread_id,))
+ tombstones = _chat_attachment_tombstones_for_messages(
+ conn,
+ thread_id,
+ [m["id"] for m in messages],
+ )
+ reconciled_messages = [
+ _reconcile_chat_message_uploads(m, tombstones.get(m["id"], set())) for m in messages
+ ]
+ serialized_messages = [
+ (
+ m,
+ json.dumps(m.get("content", [])),
+ json.dumps(m.get("attachments")) if m.get("attachments") is not None else None,
+ )
+ for m in reconciled_messages
+ ]
conn.executemany(
"""
INSERT INTO chat_messages
@@ -1566,20 +2025,46 @@ def sync_chat_messages(
thread_id,
m.get("parentId"),
m["role"],
- json.dumps(m.get("content", [])),
- json.dumps(m.get("attachments")) if m.get("attachments") is not None else None,
+ content_json,
+ attachments_json,
json.dumps(m.get("metadata")) if m.get("metadata") is not None else None,
int(m["createdAt"]),
)
- for m in messages
+ for m, content_json, attachments_json in serialized_messages
],
)
- if prune_missing:
- _recompute_chat_thread_updated_at(conn, thread_id)
- elif messages:
- _bump_chat_thread_updated_at(
- conn, thread_id, max(int(m["createdAt"]) for m in messages)
+ for m, content_json, attachments_json in serialized_messages:
+ _replace_chat_attachment_inventory(
+ conn,
+ m["id"],
+ attachments_json,
+ content_json,
)
+ if prune_missing:
+ retained_ids = {m["id"] for m in reconciled_messages}
+ existing_ids = {
+ row["id"]
+ for row in conn.execute(
+ "SELECT id FROM chat_messages WHERE thread_id = ?",
+ (thread_id,),
+ ).fetchall()
+ }
+ missing_ids = sorted(existing_ids - retained_ids)
+ for start in range(0, len(missing_ids), _SQLITE_IN_CHUNK_SIZE):
+ chunk = missing_ids[start : start + _SQLITE_IN_CHUNK_SIZE]
+ placeholders = ",".join("?" for _ in chunk)
+ conn.execute(
+ f"DELETE FROM chat_messages WHERE thread_id = ? AND id IN ({placeholders})",
+ (thread_id, *chunk),
+ )
+ _recompute_chat_thread_updated_at(conn, thread_id)
+ elif reconciled_messages:
+ _bump_chat_thread_updated_at(
+ conn,
+ thread_id,
+ max(int(m["createdAt"]) for m in reconciled_messages),
+ )
+ _mark_chat_attachment_inventory_clean(conn)
conn.commit()
return list_chat_messages(thread_id)
except ChatMessageConflictError:
@@ -1613,6 +2098,7 @@ def fork_chat_thread(
conn = get_connection()
try:
conn.execute("BEGIN IMMEDIATE")
+ _ensure_chat_attachment_inventory_current(conn)
src = conn.execute(
"SELECT * FROM chat_threads WHERE id = ?", (source_thread_id,)
).fetchone()
@@ -1686,6 +2172,14 @@ def fork_chat_thread(
for row in ancestry
],
)
+ for row in ancestry:
+ _replace_chat_attachment_inventory(
+ conn,
+ id_map[row["id"]],
+ row["attachments_json"],
+ row["content_json"],
+ )
+ _mark_chat_attachment_inventory_clean(conn)
conn.commit()
thread_row = conn.execute(
"SELECT * FROM chat_threads WHERE id = ?", (new_thread_id,)
@@ -1744,6 +2238,279 @@ def get_chat_message(thread_id: str, message_id: str) -> Optional[dict]:
conn.close()
+def _blob_part_base64_len(part: dict) -> int:
+ """Base64 payload length of an image or audio content part, or 0."""
+ image = part.get("image")
+ if isinstance(image, str) and image[:5].lower() == "data:":
+ return len(image.rsplit(",", 1)[-1])
+ audio = part.get("audio")
+ if isinstance(audio, str) and _is_locally_stored_blob(audio):
+ return len(audio.rsplit(",", 1)[-1])
+ if isinstance(audio, dict):
+ data = audio.get("data")
+ if isinstance(data, str) and _is_locally_stored_blob(data):
+ return len(data)
+ return 0
+
+
+def _chat_attachment_size_bytes(attachment: dict) -> Optional[int]:
+ """Approximate stored size of one attachment's content parts.
+
+ Image and audio parts hold base64 payloads (decoded bytes ~= 3/4 of the
+ encoded length); text parts count their character length. None when there
+ is no sizable content (e.g. a stripped/legacy attachment).
+ """
+ total = 0
+ found = False
+ for part in attachment.get("content") or []:
+ if not isinstance(part, dict):
+ continue
+ blob_len = _blob_part_base64_len(part)
+ if blob_len > 0:
+ total += (blob_len * 3) // 4
+ found = True
+ continue
+ text = part.get("text")
+ if isinstance(text, str) and text:
+ total += len(text.encode("utf-8", errors = "ignore"))
+ found = True
+ return total if found else None
+
+
+def _content_part_attachments(content_json: Optional[str]) -> list[dict]:
+ """Managed local blobs stored in content_json, with stable payload ids.
+
+ Exact duplicate blobs intentionally share one inventory id. Deleting that
+ id removes every identical copy, avoiding ambiguous index-based addressing.
+ """
+ content = _json_loads(content_json, None)
+ if not isinstance(content, list):
+ return []
+ out: list[dict] = []
+ seen: set[str] = set()
+ for part in content:
+ if not isinstance(part, dict):
+ continue
+ attachment_id = _content_part_id(part)
+ payload = _managed_content_part_payload(part)
+ if attachment_id is None or payload is None or attachment_id in seen:
+ continue
+ seen.add(attachment_id)
+ kind, value = payload
+ content_type = None
+ if kind == "image" and isinstance(value, str):
+ content_type = value[5:].split(";", 1)[0].split(",", 1)[0] or None
+ out.append(
+ {
+ "id": attachment_id,
+ "type": kind,
+ "name": "Chat image" if kind == "image" else "Chat audio",
+ "contentType": content_type,
+ "content": [part],
+ }
+ )
+ return out
+
+
+def list_chat_attachments_page(
+ limit: int = 50, offset: int = 0
+) -> tuple[list[dict], Optional[int]]:
+ """One bounded page from the normalized attachment inventory."""
+ if not 1 <= limit <= 100:
+ raise ValueError("limit must be between 1 and 100")
+ if offset < 0:
+ raise ValueError("offset must be non-negative")
+
+ conn = get_connection()
+ try:
+ _ensure_chat_attachment_inventory_current(conn)
+ rows = conn.execute(
+ """
+ SELECT i.attachment_id, i.name, i.type, i.content_type,
+ i.size_bytes, m.id AS message_id, m.thread_id,
+ m.created_at, t.title AS thread_title, t.pair_id
+ FROM chat_attachment_inventory i
+ JOIN chat_messages m ON m.id = i.message_id
+ LEFT JOIN chat_threads t ON t.id = m.thread_id
+ ORDER BY m.created_at DESC, m.id ASC, i.attachment_id ASC
+ LIMIT ? OFFSET ?
+ """,
+ (limit + 1, offset),
+ ).fetchall()
+ finally:
+ conn.close()
+
+ has_more = len(rows) > limit
+ page_rows = rows[:limit]
+ attachments = [
+ {
+ "id": row["attachment_id"],
+ "messageId": row["message_id"],
+ "threadId": row["thread_id"],
+ "pairId": row["pair_id"],
+ "threadTitle": row["thread_title"],
+ "name": row["name"],
+ "type": row["type"],
+ "contentType": row["content_type"],
+ "sizeBytes": row["size_bytes"],
+ "createdAt": row["created_at"],
+ }
+ for row in page_rows
+ ]
+ return attachments, offset + limit if has_more else None
+
+
+def list_chat_attachments() -> list[dict]:
+ """Compatibility helper returning the full normalized inventory."""
+ attachments: list[dict] = []
+ offset = 0
+ while True:
+ page, next_offset = list_chat_attachments_page(limit = 100, offset = offset)
+ attachments.extend(page)
+ if next_offset is None:
+ return attachments
+ offset = next_offset
+
+
+def get_chat_attachment(message_id: str, attachment_id: str) -> Optional[dict]:
+ """One attachment record (full content) from a message, or None."""
+ conn = get_connection()
+ try:
+ row = conn.execute(
+ """
+ SELECT message.attachments_json, message.content_json,
+ EXISTS(
+ SELECT 1 FROM chat_attachment_tombstones tombstone
+ WHERE tombstone.thread_id = message.thread_id
+ AND tombstone.message_id = message.id
+ AND tombstone.attachment_id = ?
+ ) AS tombstoned
+ FROM chat_messages message
+ WHERE message.id = ?
+ """,
+ (attachment_id, message_id),
+ ).fetchone()
+ finally:
+ conn.close()
+ if row is None or row["tombstoned"]:
+ return None
+ attachments = _json_loads(row["attachments_json"], None)
+ if isinstance(attachments, list):
+ for attachment in attachments:
+ if isinstance(attachment, dict) and str(attachment.get("id") or "") == attachment_id:
+ return attachment
+ if attachment_id.startswith(_CONTENT_PART_ID_PREFIX):
+ for attachment in _content_part_attachments(row["content_json"]):
+ if attachment["id"] == attachment_id:
+ return attachment
+ return None
+
+
+def _record_chat_attachment_tombstone(
+ conn: sqlite3.Connection, thread_id: str, message_id: str, attachment_id: str
+) -> None:
+ conn.execute(
+ """
+ INSERT INTO chat_attachment_tombstones
+ (thread_id, message_id, attachment_id, deleted_at)
+ VALUES (?, ?, ?, ?)
+ ON CONFLICT(thread_id, message_id, attachment_id) DO UPDATE SET
+ deleted_at = excluded.deleted_at
+ """,
+ (
+ thread_id,
+ message_id,
+ attachment_id,
+ int(datetime.now(timezone.utc).timestamp() * 1000),
+ ),
+ )
+
+
+def delete_chat_attachment(message_id: str, attachment_id: str) -> bool:
+ """Remove one stored upload from a message.
+
+ The tombstone is retained while the thread exists, so pruning and later
+ recreating the same message id cannot restore the deleted upload. If an
+ ordinary attachment id collides with a content-blob id, both are deleted as
+ one managed item.
+ """
+ conn = get_connection()
+ try:
+ conn.execute("BEGIN IMMEDIATE")
+ _ensure_chat_attachment_inventory_current(conn)
+ row = conn.execute(
+ """
+ SELECT thread_id, attachments_json, content_json
+ FROM chat_messages WHERE id = ?
+ """,
+ (message_id,),
+ ).fetchone()
+ if row is None:
+ conn.rollback()
+ return False
+
+ attachments = _json_loads(row["attachments_json"], None)
+ updated_attachments_json = row["attachments_json"]
+ deleted_attachment = False
+ if isinstance(attachments, list):
+ remaining_attachments = [
+ attachment
+ for attachment in attachments
+ if not (
+ isinstance(attachment, dict)
+ and str(attachment.get("id") or "") == attachment_id
+ )
+ ]
+ deleted_attachment = len(remaining_attachments) != len(attachments)
+ if deleted_attachment:
+ updated_attachments_json = json.dumps(remaining_attachments)
+
+ content = _json_loads(row["content_json"], None)
+ updated_content_json = row["content_json"]
+ deleted_content = False
+ if attachment_id.startswith(_CONTENT_PART_ID_PREFIX) and isinstance(content, list):
+ remaining_content = [
+ part
+ for part in content
+ if not (isinstance(part, dict) and _content_part_id(part) == attachment_id)
+ ]
+ deleted_content = len(remaining_content) != len(content)
+ if deleted_content:
+ updated_content_json = json.dumps(remaining_content)
+
+ if not deleted_attachment and not deleted_content:
+ conn.rollback()
+ return False
+ conn.execute(
+ """
+ UPDATE chat_messages
+ SET attachments_json = ?, content_json = ?
+ WHERE id = ?
+ """,
+ (updated_attachments_json, updated_content_json, message_id),
+ )
+ _record_chat_attachment_tombstone(
+ conn,
+ row["thread_id"],
+ message_id,
+ attachment_id,
+ )
+ _replace_chat_attachment_inventory(
+ conn,
+ message_id,
+ updated_attachments_json,
+ updated_content_json,
+ )
+ _mark_chat_attachment_inventory_clean(conn)
+ conn.commit()
+ return True
+ except Exception:
+ conn.rollback()
+ raise
+ finally:
+ conn.close()
+
+
def list_chat_messages_for_threads(thread_ids: list[str]) -> list[dict]:
if not thread_ids:
return []
diff --git a/studio/backend/tests/test_chat_attachments.py b/studio/backend/tests/test_chat_attachments.py
new file mode 100644
index 0000000000..459587ca9e
--- /dev/null
+++ b/studio/backend/tests/test_chat_attachments.py
@@ -0,0 +1,634 @@
+# 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 base64
+import json
+import os
+import sqlite3
+import sys
+
+import pytest
+from fastapi import HTTPException
+
+_backend = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, _backend)
+
+from routes import chat_history
+from storage import studio_db
+from utils.paths import studio_db_path
+
+PNG_BYTES = base64.b64decode(
+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
+)
+PNG_DATA_URL = "data:image/png;base64," + base64.b64encode(PNG_BYTES).decode("ascii")
+
+
+def _reset_studio_db(tmp_path, monkeypatch):
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))
+ monkeypatch.setenv("UNSLOTH_STUDIO_PROJECTS_HOME", str(tmp_path / "Projects"))
+ monkeypatch.setattr(studio_db, "_schema_ready", False)
+
+
+def _thread(
+ thread_id: str = "thread-1",
+ title: str = "Test Chat",
+ pair_id: str | None = None,
+) -> dict:
+ return {
+ "id": thread_id,
+ "title": title,
+ "modelType": "base",
+ "modelId": "test-model",
+ "pairId": pair_id,
+ "archived": False,
+ "createdAt": 1_700_000_000_000,
+ }
+
+
+def _message(
+ message_id: str,
+ created_at: int = 1_700_000_000_000,
+ attachments = None,
+ thread_id: str = "thread-1",
+) -> dict:
+ message = {
+ "id": message_id,
+ "threadId": thread_id,
+ "parentId": None,
+ "role": "user",
+ "content": [{"type": "text", "text": "hello"}],
+ "createdAt": created_at,
+ }
+ if attachments is not None:
+ message["attachments"] = attachments
+ return message
+
+
+def _image_attachment(attachment_id: str = "att-1", name: str = "photo.png") -> dict:
+ return {
+ "id": attachment_id,
+ "type": "image",
+ "name": name,
+ "contentType": "image/png",
+ "content": [{"type": "image", "image": PNG_DATA_URL}],
+ "status": {"type": "complete"},
+ }
+
+
+def _seed(
+ tmp_path,
+ monkeypatch,
+ attachments,
+ message_id: str = "msg-1",
+):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ studio_db.upsert_chat_message(_message(message_id, attachments = attachments))
+
+
+def _set_raw_attachments_json(message_id: str, raw: str) -> None:
+ conn = sqlite3.connect(studio_db_path())
+ try:
+ conn.execute(
+ "UPDATE chat_messages SET attachments_json = ? WHERE id = ?",
+ (raw, message_id),
+ )
+ conn.commit()
+ finally:
+ conn.close()
+
+
+def _raw_attachments_json(message_id: str):
+ conn = sqlite3.connect(studio_db_path())
+ try:
+ row = conn.execute(
+ "SELECT attachments_json FROM chat_messages WHERE id = ?",
+ (message_id,),
+ ).fetchone()
+ return row[0] if row is not None else None
+ finally:
+ conn.close()
+
+
+# ---------------------------------------------------------------------------
+# Storage: list_chat_attachments
+# ---------------------------------------------------------------------------
+
+
+def test_list_chat_attachments_empty_db(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ assert studio_db.list_chat_attachments() == []
+
+
+def test_list_chat_attachments_round_trip(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_image_attachment()])
+ records = studio_db.list_chat_attachments()
+ assert len(records) == 1
+ record = records[0]
+ assert record["id"] == "att-1"
+ assert record["messageId"] == "msg-1"
+ assert record["threadId"] == "thread-1"
+ assert record["threadTitle"] == "Test Chat"
+ assert record["name"] == "photo.png"
+ assert record["type"] == "image"
+ assert record["contentType"] == "image/png"
+ assert record["createdAt"] == 1_700_000_000_000
+ # Base64 length estimate is within padding error of the decoded size.
+ assert abs(record["sizeBytes"] - len(PNG_BYTES)) <= 2
+
+
+def test_list_chat_attachments_counts_text_utf8(tmp_path, monkeypatch):
+ text = "héllo wörld é世界"
+ attachment = {
+ "id": "att-txt",
+ "type": "document",
+ "name": "notes.txt",
+ "content": [{"type": "text", "text": text}],
+ }
+ _seed(tmp_path, monkeypatch, [attachment])
+ records = studio_db.list_chat_attachments()
+ assert records[0]["sizeBytes"] == len(text.encode("utf-8"))
+
+
+def test_list_chat_attachments_no_content_size_is_none(tmp_path, monkeypatch):
+ attachment = {"id": "att-empty", "name": "ghost.bin", "content": []}
+ _seed(tmp_path, monkeypatch, [attachment])
+ records = studio_db.list_chat_attachments()
+ assert records[0]["sizeBytes"] is None
+ assert records[0]["name"] == "ghost.bin"
+
+
+def test_list_chat_attachments_defaults_missing_name(tmp_path, monkeypatch):
+ attachment = {"id": "att-noname", "content": []}
+ _seed(tmp_path, monkeypatch, [attachment])
+ assert studio_db.list_chat_attachments()[0]["name"] == "attachment"
+
+
+def test_list_chat_attachments_sanitizes_structured_metadata(tmp_path, monkeypatch):
+ attachment = {
+ "id": "att-weird",
+ "name": {"nested": "name"},
+ "type": ["image"],
+ "contentType": {"mime": "image/png"},
+ "content": [],
+ }
+ _seed(tmp_path, monkeypatch, [attachment])
+ record = studio_db.list_chat_attachments()[0]
+ assert record["name"] == "attachment"
+ assert record["type"] is None
+ assert record["contentType"] is None
+
+
+def test_list_chat_attachments_skips_malformed_rows(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ for i, raw in enumerate(
+ [
+ "not json at all",
+ '{"id": "att-obj"}',
+ "null",
+ "[]",
+ '[{"noid": true}, "just a string", 42]',
+ '[{"id": ""}]',
+ ]
+ ):
+ message_id = f"msg-bad-{i}"
+ studio_db.upsert_chat_message(_message(message_id))
+ _set_raw_attachments_json(message_id, raw)
+ studio_db.upsert_chat_message(_message("msg-good", attachments = [_image_attachment("att-ok")]))
+ records = studio_db.list_chat_attachments()
+ assert [r["id"] for r in records] == ["att-ok"]
+
+
+def test_list_chat_attachments_orders_newest_first(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ studio_db.upsert_chat_message(
+ _message("msg-old", 1_700_000_000_000, [_image_attachment("att-old")])
+ )
+ studio_db.upsert_chat_message(
+ _message("msg-new", 1_700_000_100_000, [_image_attachment("att-new")])
+ )
+ assert [r["id"] for r in studio_db.list_chat_attachments()] == ["att-new", "att-old"]
+
+
+def test_list_chat_attachments_survives_missing_thread_row(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ studio_db.upsert_chat_message(_message("msg-1", attachments = [_image_attachment()]))
+ conn = sqlite3.connect(studio_db_path())
+ try:
+ conn.execute("DELETE FROM chat_threads WHERE id = 'thread-1'")
+ conn.commit()
+ finally:
+ conn.close()
+ records = studio_db.list_chat_attachments()
+ assert len(records) == 1
+ assert records[0]["threadTitle"] is None
+
+
+def test_list_chat_attachments_includes_compare_pair_id(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread(pair_id = "pair-1"))
+ studio_db.upsert_chat_message(_message("msg-compare", attachments = [_image_attachment()]))
+ record = studio_db.list_chat_attachments()[0]
+ assert record["threadId"] == "thread-1"
+ assert record["pairId"] == "pair-1"
+
+
+def test_list_chat_attachments_gone_after_thread_delete(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_image_attachment()])
+ studio_db.delete_chat_threads(["thread-1"])
+ assert studio_db.list_chat_attachments() == []
+
+
+# ---------------------------------------------------------------------------
+# Storage: get_chat_attachment / delete_chat_attachment
+# ---------------------------------------------------------------------------
+
+
+def test_get_chat_attachment_found_and_missing(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_image_attachment()])
+ attachment = studio_db.get_chat_attachment("msg-1", "att-1")
+ assert attachment is not None
+ assert attachment["content"][0]["image"] == PNG_DATA_URL
+ assert studio_db.get_chat_attachment("msg-1", "att-missing") is None
+ assert studio_db.get_chat_attachment("msg-missing", "att-1") is None
+
+
+def test_delete_chat_attachment_keeps_others(tmp_path, monkeypatch):
+ _seed(
+ tmp_path,
+ monkeypatch,
+ [_image_attachment("att-1"), _image_attachment("att-2", "other.png")],
+ )
+ assert studio_db.delete_chat_attachment("msg-1", "att-1") is True
+ assert studio_db.get_chat_attachment("msg-1", "att-1") is None
+ assert studio_db.get_chat_attachment("msg-1", "att-2") is not None
+ assert [r["id"] for r in studio_db.list_chat_attachments()] == ["att-2"]
+
+
+def test_delete_last_chat_attachment_stores_empty_list(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_image_attachment()])
+ assert studio_db.delete_chat_attachment("msg-1", "att-1") is True
+ # '[]' rather than NULL: a NULL attachments field reads back as missing
+ # and triggers the legacy IndexedDB backfill, resurrecting the deleted
+ # attachment on the next chat load.
+ assert _raw_attachments_json("msg-1") == "[]"
+ assert studio_db.list_chat_attachments() == []
+ # The message itself must survive with its content intact.
+ message = studio_db.get_chat_message("thread-1", "msg-1")
+ assert message is not None
+ assert message["content"] == [{"type": "text", "text": "hello"}]
+ assert message["attachments"] == []
+
+
+def test_delete_chat_attachment_missing_targets(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_image_attachment()])
+ assert studio_db.delete_chat_attachment("msg-missing", "att-1") is False
+ assert studio_db.delete_chat_attachment("msg-1", "att-missing") is False
+ _set_raw_attachments_json("msg-1", "not json")
+ assert studio_db.delete_chat_attachment("msg-1", "att-1") is False
+
+
+# ---------------------------------------------------------------------------
+# Routes: /attachments endpoints (real storage, direct calls)
+# ---------------------------------------------------------------------------
+
+
+def test_list_attachments_route(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_image_attachment()])
+ result = chat_history.list_attachments(current_subject = "unsloth")
+ assert [a["id"] for a in result["attachments"]] == ["att-1"]
+
+
+def test_attachment_file_serves_image_bytes(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_image_attachment()])
+ response = chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert response.body == PNG_BYTES
+ assert response.media_type == "image/png"
+
+
+def test_attachment_file_tolerates_whitespace_in_base64(tmp_path, monkeypatch):
+ encoded = base64.b64encode(PNG_BYTES).decode("ascii")
+ wrapped = "\n".join(encoded[i : i + 8] for i in range(0, len(encoded), 8))
+ attachment = _image_attachment()
+ attachment["content"] = [{"type": "image", "image": "data:image/png;base64," + wrapped}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ response = chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert response.body == PNG_BYTES
+
+
+def test_attachment_file_corrupt_base64_is_422(tmp_path, monkeypatch):
+ attachment = _image_attachment()
+ attachment["content"] = [{"type": "image", "image": "data:image/png;base64,%%%"}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ with pytest.raises(HTTPException) as excinfo:
+ chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert excinfo.value.status_code == 422
+
+
+def test_attachment_file_accepts_urlsafe_base64(tmp_path, monkeypatch):
+ data = bytes(range(251, 256)) * 3 # encodes to characters remapped by urlsafe
+ payload = base64.urlsafe_b64encode(data).decode("ascii")
+ assert "-" in payload or "_" in payload
+ attachment = _image_attachment()
+ attachment["content"] = [{"type": "image", "image": "data:image/png;base64," + payload}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ response = chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert response.body == data
+
+
+def test_attachment_file_accepts_missing_padding(tmp_path, monkeypatch):
+ payload = base64.b64encode(PNG_BYTES).decode("ascii").rstrip("=")
+ attachment = _image_attachment()
+ attachment["content"] = [{"type": "image", "image": "data:image/png;base64," + payload}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ response = chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert response.body == PNG_BYTES
+
+
+def test_attachment_file_serves_percent_encoded_data_url(tmp_path, monkeypatch):
+ attachment = _image_attachment()
+ attachment["content"] = [{"type": "image", "image": "data:text/plain,hello%20world"}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ response = chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert response.body == b"hello world"
+ # Non-image data URL types are clamped so markup never renders same-origin.
+ assert response.media_type == "application/octet-stream"
+
+
+def test_attachment_file_serves_text_parts(tmp_path, monkeypatch):
+ attachment = {
+ "id": "att-txt",
+ "type": "document",
+ "name": "notes.txt",
+ "content": [
+ {"type": "text", "text": "first"},
+ {"type": "text", "text": "second"},
+ ],
+ }
+ _seed(tmp_path, monkeypatch, [attachment])
+ response = chat_history.get_attachment_file("msg-1", "att-txt", current_subject = "unsloth")
+ assert response.body.decode("utf-8") == "first\nsecond"
+ assert response.media_type.startswith("text/plain")
+
+
+def test_attachment_file_no_content_is_404(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [{"id": "att-empty", "name": "ghost", "content": []}])
+ with pytest.raises(HTTPException) as excinfo:
+ chat_history.get_attachment_file("msg-1", "att-empty", current_subject = "unsloth")
+ assert excinfo.value.status_code == 404
+
+
+def test_attachment_file_missing_message_is_404(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ with pytest.raises(HTTPException) as excinfo:
+ chat_history.get_attachment_file("nope", "att-1", current_subject = "unsloth")
+ assert excinfo.value.status_code == 404
+
+
+def test_attachment_file_non_data_url_image_is_404(tmp_path, monkeypatch):
+ attachment = _image_attachment()
+ attachment["content"] = [{"type": "image", "image": "https://example.com/a.png"}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ with pytest.raises(HTTPException) as excinfo:
+ chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert excinfo.value.status_code == 404
+
+
+def test_attachment_file_defaults_media_type(tmp_path, monkeypatch):
+ payload = base64.b64encode(b"raw-bytes").decode("ascii")
+ attachment = _image_attachment()
+ attachment["content"] = [{"type": "image", "image": "data:;base64," + payload}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ response = chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert response.body == b"raw-bytes"
+ assert response.media_type == "application/octet-stream"
+
+
+def test_attachment_file_svg_media_type(tmp_path, monkeypatch):
+ svg = b" "
+ payload = base64.b64encode(svg).decode("ascii")
+ attachment = _image_attachment()
+ attachment["content"] = [{"type": "image", "image": "data:image/svg+xml;base64," + payload}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ response = chat_history.get_attachment_file("msg-1", "att-1", current_subject = "unsloth")
+ assert response.body == svg
+ # SVG can carry scripts, so it downloads as bytes instead of rendering.
+ assert response.media_type == "application/octet-stream"
+
+
+def test_delete_attachment_route_then_404(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_image_attachment()])
+ result = chat_history.delete_attachment("msg-1", "att-1", current_subject = "unsloth")
+ assert result == {"ok": True}
+ with pytest.raises(HTTPException) as excinfo:
+ chat_history.delete_attachment("msg-1", "att-1", current_subject = "unsloth")
+ assert excinfo.value.status_code == 404
+
+
+# ---------------------------------------------------------------------------
+# Audio attachments (adapter {data, format} and compare-chat bare base64)
+# ---------------------------------------------------------------------------
+
+WAV_BYTES = b"RIFF$\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00"
+WAV_B64 = base64.b64encode(WAV_BYTES).decode("ascii")
+
+
+def _audio_attachment(attachment_id: str = "att-audio") -> dict:
+ return {
+ "id": attachment_id,
+ "type": "file",
+ "name": "clip.wav",
+ "contentType": "audio/wav",
+ "content": [{"type": "audio", "audio": {"data": WAV_B64, "format": "wav"}}],
+ "status": {"type": "complete"},
+ }
+
+
+def test_audio_attachment_lists_with_size(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_audio_attachment()])
+ records = studio_db.list_chat_attachments()
+ assert len(records) == 1
+ assert records[0]["id"] == "att-audio"
+ assert abs(records[0]["sizeBytes"] - len(WAV_BYTES)) <= 2
+
+
+def test_audio_attachment_file_serves_bytes(tmp_path, monkeypatch):
+ _seed(tmp_path, monkeypatch, [_audio_attachment()])
+ response = chat_history.get_attachment_file("msg-1", "att-audio", current_subject = "unsloth")
+ assert response.body == WAV_BYTES
+ assert response.media_type == "audio/wav"
+
+
+def test_audio_attachment_media_type_from_format(tmp_path, monkeypatch):
+ attachment = _audio_attachment()
+ attachment["contentType"] = None
+ attachment["content"] = [{"type": "audio", "audio": {"data": WAV_B64, "format": "mp3"}}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ response = chat_history.get_attachment_file("msg-1", "att-audio", current_subject = "unsloth")
+ assert response.media_type == "audio/mpeg"
+
+
+def test_audio_attachment_corrupt_payload_is_422(tmp_path, monkeypatch):
+ attachment = _audio_attachment()
+ attachment["content"] = [{"type": "audio", "audio": {"data": "%%%", "format": "wav"}}]
+ _seed(tmp_path, monkeypatch, [attachment])
+ with pytest.raises(HTTPException) as excinfo:
+ chat_history.get_attachment_file("msg-1", "att-audio", current_subject = "unsloth")
+ assert excinfo.value.status_code == 422
+
+
+# ---------------------------------------------------------------------------
+# Compare-chat uploads stored as message content parts
+# ---------------------------------------------------------------------------
+
+
+def _compare_message(message_id: str = "msg-cmp") -> dict:
+ return {
+ "id": message_id,
+ "threadId": "thread-1",
+ "parentId": None,
+ "role": "user",
+ "content": [
+ {"type": "image", "image": PNG_DATA_URL},
+ {"type": "audio", "audio": WAV_B64},
+ {"type": "text", "text": "compare these"},
+ ],
+ "createdAt": 1_700_000_000_000,
+ }
+
+
+def _seed_compare(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ studio_db.upsert_chat_message(_compare_message())
+
+
+_CONTENT_PART_PREFIX = "content-part-sha256-"
+
+
+def _content_part_id_for(message_id: str, kind: str) -> str:
+ """Resolve the stable content-hash id for a message's stored blob.
+
+ Content-part ids are SHA-256 hashes of the blob payload, not array
+ indices, so tests look them up from the listing instead of hardcoding an
+ index that would shift when an earlier part is deleted.
+ """
+ for record in studio_db.list_chat_attachments():
+ if record["messageId"] == message_id and record["type"] == kind:
+ return record["id"]
+ raise AssertionError(f"no {kind} content-part upload for {message_id}")
+
+
+def test_content_part_uploads_are_listed(tmp_path, monkeypatch):
+ _seed_compare(tmp_path, monkeypatch)
+ records = studio_db.list_chat_attachments()
+ # Ids are stable content hashes, not array indices.
+ assert all(r["id"].startswith(_CONTENT_PART_PREFIX) for r in records)
+ assert {r["type"] for r in records} == {"image", "audio"}
+ image = next(r for r in records if r["type"] == "image")
+ assert image["contentType"] == "image/png"
+ assert abs(image["sizeBytes"] - len(PNG_BYTES)) <= 2
+ audio = next(r for r in records if r["type"] == "audio")
+ assert audio["type"] == "audio"
+
+
+def test_content_part_file_serves_image_bytes(tmp_path, monkeypatch):
+ _seed_compare(tmp_path, monkeypatch)
+ image_id = _content_part_id_for("msg-cmp", "image")
+ response = chat_history.get_attachment_file("msg-cmp", image_id, current_subject = "unsloth")
+ assert response.body == PNG_BYTES
+ assert response.media_type == "image/png"
+
+
+def test_content_part_delete_keeps_text(tmp_path, monkeypatch):
+ _seed_compare(tmp_path, monkeypatch)
+ image_id = _content_part_id_for("msg-cmp", "image")
+ assert studio_db.delete_chat_attachment("msg-cmp", image_id) is True
+ message = studio_db.get_chat_message("thread-1", "msg-cmp")
+ types = [p["type"] for p in message["content"]]
+ assert types == ["audio", "text"]
+ # The surviving audio blob keeps its own stable hash id after the delete.
+ remaining = studio_db.list_chat_attachments()
+ assert [r["type"] for r in remaining] == ["audio"]
+ assert remaining[0]["id"].startswith(_CONTENT_PART_PREFIX)
+ assert remaining[0]["id"] != image_id
+
+
+def test_content_part_delete_rejects_non_blob(tmp_path, monkeypatch):
+ _seed_compare(tmp_path, monkeypatch)
+ # The text part is not a stored upload, so it never gets an id: only the
+ # image and audio blobs are addressable.
+ assert len(studio_db.list_chat_attachments()) == 2
+ # A well-formed but unknown content-hash id, and malformed ids, all no-op.
+ assert studio_db.delete_chat_attachment("msg-cmp", _CONTENT_PART_PREFIX + "0" * 64) is False
+ assert studio_db.delete_chat_attachment("msg-cmp", "content-part-99") is False
+ assert studio_db.delete_chat_attachment("msg-cmp", "content-part-x") is False
+
+
+def test_text_only_messages_not_listed_as_uploads(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ # The word "image" inside text must not create phantom upload rows.
+ message = _message("msg-txt")
+ message["content"] = [{"type": "text", "text": 'discussing an "image" and "audio" here'}]
+ studio_db.upsert_chat_message(message)
+ assert studio_db.list_chat_attachments() == []
+
+
+def test_remote_image_urls_are_not_listed_as_uploads(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ message = _message("msg-remote")
+ message["content"] = [
+ {"type": "image", "image": "https://example.com/cat.png"},
+ {"type": "text", "text": "look at this"},
+ ]
+ studio_db.upsert_chat_message(message)
+ # No stored bytes: nothing to list, open, or delete.
+ assert studio_db.list_chat_attachments() == []
+ assert studio_db.get_chat_attachment("msg-remote", "content-part-0") is None
+ assert studio_db.delete_chat_attachment("msg-remote", "content-part-0") is False
+ stored = studio_db.get_chat_message("thread-1", "msg-remote")
+ assert [p["type"] for p in stored["content"]] == ["image", "text"]
+
+
+def test_html_data_url_serves_as_octet_stream(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ html_b64 = base64.b64encode(b"").decode()
+ message = _message("msg-html")
+ message["content"] = [
+ {"type": "image", "image": f"data:text/html;base64,{html_b64}"},
+ ]
+ studio_db.upsert_chat_message(message)
+ attachment_id = _content_part_id_for("msg-html", "image")
+ response = chat_history.get_attachment_file(
+ "msg-html", attachment_id, current_subject = "unsloth"
+ )
+ # Never echo a script-capable media type back under the app origin.
+ assert response.media_type == "application/octet-stream"
+ assert response.body == b""
+
+
+def test_svg_data_url_serves_as_octet_stream(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ svg_b64 = base64.b64encode(b" ").decode()
+ message = _message("msg-svg")
+ message["content"] = [
+ {"type": "image", "image": f"data:image/svg+xml;base64,{svg_b64}"},
+ ]
+ studio_db.upsert_chat_message(message)
+ attachment_id = _content_part_id_for("msg-svg", "image")
+ response = chat_history.get_attachment_file("msg-svg", attachment_id, current_subject = "unsloth")
+ assert response.media_type == "application/octet-stream"
+
+
+def test_png_data_url_keeps_its_media_type(tmp_path, monkeypatch):
+ _seed_compare(tmp_path, monkeypatch)
+ image_id = _content_part_id_for("msg-cmp", "image")
+ response = chat_history.get_attachment_file("msg-cmp", image_id, current_subject = "unsloth")
+ assert response.media_type == "image/png"
diff --git a/studio/backend/tests/test_hf_token_validation.py b/studio/backend/tests/test_hf_token_validation.py
new file mode 100644
index 0000000000..31b30fc37d
--- /dev/null
+++ b/studio/backend/tests/test_hf_token_validation.py
@@ -0,0 +1,165 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Focused coverage for cached, rate-limited HF token validation."""
+
+from __future__ import annotations
+
+from pathlib import Path
+import sys
+
+import httpx
+import pytest
+
+
+_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
+if _BACKEND_DIR not in sys.path:
+ sys.path.insert(0, _BACKEND_DIR)
+
+import utils.hf_token_validation as validation
+
+
+@pytest.fixture(autouse = True)
+def _reset_validation_state():
+ validation.reset_hf_token_validation_state()
+ yield
+ validation.reset_hf_token_validation_state()
+
+
+def test_cached_token_does_not_spend_another_attempt(monkeypatch):
+ calls = []
+
+ def _check(token):
+ calls.append(token)
+ return validation.TokenValidationResult(status = "valid")
+
+ monkeypatch.setattr(validation, "_check_remote", _check)
+ first = validation.validate_hf_token("hf_valid", rate_key = "user:ip")
+ second = validation.validate_hf_token("hf_valid", rate_key = "user:ip")
+
+ assert first.status == second.status == "valid"
+ assert calls == ["hf_valid"]
+
+
+def test_three_uncached_attempts_per_hour(monkeypatch):
+ monkeypatch.setattr(
+ validation,
+ "_check_remote",
+ lambda _token: validation.TokenValidationResult(status = "invalid"),
+ )
+
+ for index in range(3):
+ result = validation.validate_hf_token(f"hf_bad_{index}", rate_key = "user:ip")
+ assert result.status == "invalid"
+
+ limited = validation.validate_hf_token("hf_bad_4", rate_key = "user:ip")
+ assert limited.status == "rate_limited"
+ assert limited.retry_after_seconds is not None
+ assert limited.retry_after_seconds > 0
+
+ other_user = validation.validate_hf_token("hf_other", rate_key = "other:ip")
+ assert other_user.status == "invalid"
+
+
+def test_window_rolls_forward(monkeypatch):
+ clock = {"now": 100.0}
+ monkeypatch.setattr(validation.time, "monotonic", lambda: clock["now"])
+ monkeypatch.setattr(validation, "_MAX_ATTEMPTS", 1)
+ monkeypatch.setattr(validation, "_WINDOW_SECONDS", 10.0)
+ monkeypatch.setattr(
+ validation,
+ "_check_remote",
+ lambda _token: validation.TokenValidationResult(status = "invalid"),
+ )
+
+ assert validation.validate_hf_token("hf_a", rate_key = "user:ip").status == "invalid"
+ assert validation.validate_hf_token("hf_b", rate_key = "user:ip").status == "rate_limited"
+ clock["now"] += 11.0
+ assert validation.validate_hf_token("hf_b", rate_key = "user:ip").status == "invalid"
+
+
+@pytest.mark.parametrize(
+ ("status_code", "expected"),
+ [(200, "valid"), (401, "invalid"), (429, "rate_limited"), (500, "unavailable")],
+)
+def test_remote_status_classification(monkeypatch, status_code, expected):
+ response = httpx.Response(
+ status_code,
+ request = httpx.Request("GET", "https://huggingface.co/api/whoami-v2"),
+ headers = {"Retry-After": "42"} if status_code == 429 else None,
+ )
+
+ class _Session:
+ def get(self, url, *, headers, timeout):
+ assert url == "https://huggingface.co/api/whoami-v2"
+ assert headers["authorization"] == "Bearer hf_test"
+ assert timeout == validation._REMOTE_TIMEOUT_SECONDS
+ return response
+
+ monkeypatch.setattr(validation, "get_session", lambda: _Session())
+ result = validation._check_remote("hf_test")
+ assert result.status == expected
+ if status_code == 429:
+ assert result.retry_after_seconds == 42
+
+
+def test_wrapped_http_401_is_invalid(monkeypatch):
+ response = httpx.Response(
+ 401,
+ request = httpx.Request("GET", "https://huggingface.co/api/whoami-v2"),
+ )
+
+ class _Session:
+ def get(self, _url, **_kwargs):
+ error = RuntimeError("Invalid user token.")
+ error.response = response
+ raise error
+
+ monkeypatch.setattr(validation, "get_session", lambda: _Session())
+ assert validation._check_remote("hf_test").status == "invalid"
+
+
+def test_remote_timeout_is_bounded_and_unavailable(monkeypatch):
+ class _Session:
+ def get(self, _url, *, headers, timeout):
+ assert headers["authorization"] == "Bearer hf_test"
+ assert timeout == validation._REMOTE_TIMEOUT_SECONDS
+ raise TimeoutError("timed out")
+
+ monkeypatch.setattr(validation, "get_session", lambda: _Session())
+ assert validation._check_remote("hf_test").status == "unavailable"
+
+
+def test_raw_token_is_not_retained(monkeypatch):
+ monkeypatch.setattr(
+ validation,
+ "_check_remote",
+ lambda _token: validation.TokenValidationResult(status = "valid"),
+ )
+ token = "hf_do_not_store_this_value"
+ validation.validate_hf_token(token, rate_key = "user:ip")
+
+ assert token not in repr(validation._cache)
+ assert token not in repr(validation._attempts)
+
+
+def test_unexpected_remote_exception_releases_singleflight(monkeypatch):
+ calls = 0
+ monkeypatch.setattr(validation, "_INFLIGHT_WAIT_SECONDS", 0.0)
+
+ def _check(_token):
+ nonlocal calls
+ calls += 1
+ if calls == 1:
+ raise RuntimeError("unexpected failure")
+ return validation.TokenValidationResult(status = "valid")
+
+ monkeypatch.setattr(validation, "_check_remote", _check)
+
+ with pytest.raises(RuntimeError, match = "unexpected failure"):
+ validation.validate_hf_token("hf_test", rate_key = "user:ip")
+
+ result = validation.validate_hf_token("hf_test", rate_key = "user:ip")
+ assert result.status == "valid"
+ assert calls == 2
+ assert validation._inflight == {}
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_mtp_detection.py b/studio/backend/tests/test_llama_cpp_mtp_detection.py
index 8fe04c0e39..1d15647967 100644
--- a/studio/backend/tests/test_llama_cpp_mtp_detection.py
+++ b/studio/backend/tests/test_llama_cpp_mtp_detection.py
@@ -9,6 +9,7 @@ the _already_in_target_state mirror that prevents needless reloads.
from __future__ import annotations
+import ast
import inspect
import os
import struct
@@ -345,10 +346,62 @@ def test_windows_full_offload_flags_use_current_llama_server_args():
stale_checkpoint_flag = "--checkpoint-" + "every-n-tokens"
assert '"--cache-ram"' in src
assert '"--ctx-checkpoints"' in src
- assert '"--no-cache-prompt"' in src
+ # Prompt caching stays on (in-VRAM prefix reuse); #5692 only needed the host-RAM
+ # checkpoints (--cache-ram / --ctx-checkpoints) disabled, not prompt reuse.
+ assert '"--no-cache-prompt"' not in src
assert stale_checkpoint_flag not in src
+# Backend-wide guard: Unsloth must never inject --no-cache-prompt into a llama-server
+# command. It disables in-VRAM prompt-prefix reuse, re-prefilling every repeated prompt
+# (#5692 only needed --cache-ram / --ctx-checkpoints off; #7260 dropped the stray flag).
+# Detecting it (_is_real) or honouring a user-supplied one (_prompt_cache_off) is fine.
+_NO_CACHE_PROMPT_FLAG = "--no-cache-prompt"
+_LIST_MUTATORS = frozenset({"append", "extend", "insert"})
+
+
+def _has_flag_literal(node: ast.AST) -> bool:
+ return any(
+ isinstance(n, ast.Constant) and n.value == _NO_CACHE_PROMPT_FLAG for n in ast.walk(node)
+ )
+
+
+def _no_cache_prompt_injections(source: str, filename: str) -> list[tuple[str, int]]:
+ """(file, lineno) for each spot adding --no-cache-prompt to a list."""
+ hits: list[tuple[str, int]] = []
+ for node in ast.walk(ast.parse(source, filename = filename)):
+ # cmd.append/extend/insert(... flag ...) or cmd += [... flag ...]
+ if (
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Attribute)
+ and node.func.attr in _LIST_MUTATORS
+ and any(_has_flag_literal(a) for a in node.args)
+ ) or (
+ isinstance(node, ast.AugAssign)
+ and isinstance(node.op, ast.Add)
+ and _has_flag_literal(node.value)
+ ):
+ hits.append((filename, node.lineno))
+ return hits
+
+
+def test_unsloth_never_injects_no_cache_prompt_into_any_command():
+ root = Path(_BACKEND_DIR)
+ files = [p for p in root.rglob("*.py") if "tests" not in p.relative_to(root).parts]
+ violations: list[tuple[str, int]] = []
+ for path in files:
+ try:
+ violations += _no_cache_prompt_injections(path.read_text(encoding = "utf-8"), str(path))
+ except (OSError, UnicodeDecodeError, SyntaxError):
+ continue
+ assert files, "no backend source files were scanned"
+ assert violations == [], (
+ "Unsloth must never add --no-cache-prompt to a llama-server command "
+ "(it disables prompt-prefix reuse); detecting or honouring a user-supplied "
+ f"one is fine. Offending sites: {violations}"
+ )
+
+
def test_load_model_sets_threads_once():
src = inspect.getsource(LlamaCppBackend.load_model)
assert src.count('cmd.extend(["--threads", str(') == 1
@@ -741,6 +794,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_cpp_stall_timeout.py b/studio/backend/tests/test_llama_cpp_stall_timeout.py
new file mode 100644
index 0000000000..da36f75e8e
--- /dev/null
+++ b/studio/backend/tests/test_llama_cpp_stall_timeout.py
@@ -0,0 +1,125 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Regression test for the post-first-token stall timeout in the cancel-aware read.
+
+httpcore snapshots ``request.extensions["timeout"]["read"]`` once at body start, so
+when ``_iter_text_cancellable`` lowers it after the first token, a one-token-then-silent
+server hangs for the full prefill window. The fix re-reads the live extensions timeout
+per call; a fake clock and always-silent stream check the read gives up after the live
+stall timeout, not the stale prefill one.
+"""
+
+from __future__ import annotations
+
+import inspect
+import sys
+import threading
+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)
+
+# Mirror sibling tests' stubbing so the module imports without fastapi.
+_loggers_stub = _types.ModuleType("loggers")
+_loggers_stub.get_logger = lambda name: __import__("logging").getLogger(name)
+sys.modules.setdefault("loggers", _loggers_stub)
+sys.modules.setdefault("structlog", _types.ModuleType("structlog"))
+
+import httpcore # noqa: E402
+
+from core.inference import llama_cpp as llama_cpp_mod # noqa: E402
+from core.inference.llama_cpp import LlamaCppBackend # noqa: E402
+
+_PREFILL_TIMEOUT = 1200.0 # what httpcore snapshots from the prefill timeout
+_STALL_TIMEOUT = 120.0 # the post-first-token stall timeout the wrapper must honor
+
+
+class _Obj:
+ pass
+
+
+def _install(response, clock, silent_stream):
+ """Wire fake client/pool so _install_cancel_aware_read finds the stream; return the wrapped stream.read."""
+ inner = _Obj()
+ inner._network_stream = silent_stream
+ connection = _Obj()
+ connection._connection = inner
+ pool = _Obj()
+ pool._connections = [connection]
+ transport = _Obj()
+ transport._pool = pool
+ client = _Obj()
+ client._transport = transport
+
+ cancel_event = threading.Event() # never set: we test the stall path, not cancel
+ sig = inspect.signature(LlamaCppBackend._install_cancel_aware_read)
+ if "response" in sig.parameters:
+ # Fixed signature: wrapper reads the live extensions timeout.
+ LlamaCppBackend._install_cancel_aware_read(client, cancel_event, response)
+ else:
+ # Pre-fix signature: no response, so the stall assertion fails (proves the bug).
+ LlamaCppBackend._install_cancel_aware_read(client, cancel_event)
+ return silent_stream.read
+
+
+def test_stall_timeout_honored_after_first_token(monkeypatch):
+ clock = {"t": 0.0}
+ monkeypatch.setattr(llama_cpp_mod.time, "monotonic", lambda: clock["t"])
+
+ # One token then silence: every read times out, advancing fake time by its timeout.
+ def silent_read(max_bytes, timeout = None):
+ clock["t"] += timeout if timeout is not None else 0.0
+ raise httpcore.ReadTimeout("slice timed out on silence")
+
+ stream = _Obj()
+ stream.read = silent_read
+
+ # First token seen: the live read timeout is lowered to the stall timeout.
+ request = _Obj()
+ request.extensions = {"timeout": {"read": _STALL_TIMEOUT}}
+ response = _Obj()
+ response.request = request
+
+ wrapped_read = _install(response, clock, stream)
+
+ # httpcore still passes the stale prefill timeout it snapshotted at body start.
+ with pytest.raises(httpcore.ReadTimeout):
+ wrapped_read(65536, timeout = _PREFILL_TIMEOUT)
+
+ # Must give up ~stall timeout after the last token, not the prefill window.
+ assert clock["t"] <= _STALL_TIMEOUT * 1.5, (
+ f"stall timeout not honored: waited {clock['t']}s "
+ f"(expected ~{_STALL_TIMEOUT}s, not {_PREFILL_TIMEOUT}s)"
+ )
+ assert clock["t"] >= _STALL_TIMEOUT * 0.5
+
+
+def test_prefill_timeout_used_when_no_live_override(monkeypatch):
+ """Without a lowered live timeout, the wrapper honors the passed prefill timeout, so the normal first-token wait is unchanged."""
+ clock = {"t": 0.0}
+ monkeypatch.setattr(llama_cpp_mod.time, "monotonic", lambda: clock["t"])
+
+ def silent_read(max_bytes, timeout = None):
+ clock["t"] += timeout if timeout is not None else 0.0
+ raise httpcore.ReadTimeout("slice timed out on silence")
+
+ stream = _Obj()
+ stream.read = silent_read
+
+ # No timeout extension: wrapper falls back to httpcore's passed timeout.
+ request = _Obj()
+ request.extensions = {}
+ response = _Obj()
+ response.request = request
+
+ wrapped_read = _install(response, clock, stream)
+
+ with pytest.raises(httpcore.ReadTimeout):
+ wrapped_read(65536, timeout = _PREFILL_TIMEOUT)
+
+ assert clock["t"] >= _PREFILL_TIMEOUT * 0.9
diff --git a/studio/backend/tests/test_llama_cpp_update.py b/studio/backend/tests/test_llama_cpp_update.py
index 43a4eb5f29..478f06ea10 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)
@@ -546,6 +549,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")
@@ -729,7 +773,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
@@ -743,17 +787,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
@@ -767,7 +811,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_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/tests/test_rocm_windows_vram_7072.py b/studio/backend/tests/test_rocm_windows_vram_7072.py
new file mode 100644
index 0000000000..b4079831b7
--- /dev/null
+++ b/studio/backend/tests/test_rocm_windows_vram_7072.py
@@ -0,0 +1,361 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Regression tests for issue #7072 -- "VRAM Usage in System Tab is wrong".
+
+Reporter: dual AMD (Radeon PRO W7900 ~48GB + W7500 8GB), Windows 10, ROCm 7.13,
+torch 2.11.0+rocm7.13. On Windows without a HIP SDK, amd-smi is permanently
+disabled (avoids a UAC/DiskPart prompt) and hipMemGetInfo returns free==total
+(used 0). Two symptoms followed:
+
+ * System tab (/api/system -> get_visible_gpu_utilization) showed ~0 VRAM used
+ on every GPU (torch mem_get_info free==total quirk; ROCm/ROCm#1909).
+ * get_gpu_utilization()'s Windows fallback SUMMED "GPU Adapter Memory\\Dedicated
+ Usage" across all adapters into ONE fake device with only GPU 0's total, so
+ the second GPU never appeared.
+
+The fix reads the per-adapter (LUID-instanced) Dedicated Usage performance
+counter -- Task Manager's source -- for per-GPU used, takes per-GPU total from
+torch device properties, and guards the free==total mem_get_info quirk. CI has no
+AMD GPU/Windows, so torch, the performance counter, and platform are all mocked.
+"""
+
+from __future__ import annotations
+
+import subprocess
+import sys
+import types
+
+import pytest
+
+from utils.hardware import hardware as hw
+
+GB = 1024**3
+MiB = 1024**2
+
+
+# ----------------------------------------------------------------------------- #
+# Fakes
+# ----------------------------------------------------------------------------- #
+def _fake_torch(
+ devices,
+ *,
+ free_equals_total = False,
+ used_per_device = None,
+):
+ """Build a fake `torch` module. devices: list of (name, total_bytes)."""
+ dev = list(devices)
+
+ class _Props:
+ def __init__(self, name, total):
+ self.name = name
+ self.total_memory = total
+
+ def get_device_properties(i):
+ name, total = dev[i]
+ return _Props(name, total)
+
+ def mem_get_info(i):
+ _, total = dev[i]
+ if free_equals_total:
+ return (total, total)
+ used = used_per_device[i] if used_per_device is not None else 0
+ return (total - used, total)
+
+ t = types.ModuleType("torch")
+ t.__version__ = "2.11.0+rocm7.13"
+ t.version = types.SimpleNamespace(hip = "7.13", cuda = None)
+ t.cuda = types.SimpleNamespace(
+ is_available = lambda: len(dev) > 0,
+ device_count = lambda: len(dev),
+ current_device = lambda: 0,
+ get_device_properties = get_device_properties,
+ mem_get_info = mem_get_info,
+ memory_allocated = lambda i: 0,
+ memory_reserved = lambda i: 0,
+ )
+ return t
+
+
+def _adapter_output(adapters):
+ if not adapters:
+ return "__NONE__\n"
+ return "".join(f"{name}|{int(used)}\n" for name, used in adapters)
+
+
+def _subprocess_run(*, adapter_output = "__NONE__\n", util_output = "12.0\n"):
+ def fake_run(cmd, *a, **k):
+ joined = " ".join(cmd) if isinstance(cmd, list) else str(cmd)
+ if "GPU Adapter Memory" in joined and "InstanceName" in joined:
+ out = adapter_output
+ elif "engtype_3D" in joined or "GPU Engine" in joined:
+ out = util_output
+ else:
+ out = "-1\n"
+ return subprocess.CompletedProcess(args = cmd, returncode = 0, stdout = out, stderr = "")
+
+ return fake_run
+
+
+@pytest.fixture
+def win_rocm(monkeypatch):
+ """Configure the hardware module as a Windows ROCm host with 2 visible GPUs."""
+ monkeypatch.setattr(hw, "get_device", lambda: hw.DeviceType.CUDA)
+ monkeypatch.setattr(hw, "IS_ROCM", True)
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ monkeypatch.setattr(hw.sys, "platform", "win32")
+ monkeypatch.setattr(hw, "_smi_query", lambda *a, **k: None) # amd-smi disabled
+ # Visible set via HIP mask so we don't shell out to amd-smi for the count.
+ monkeypatch.setenv("HIP_VISIBLE_DEVICES", "0,1")
+ monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising = False)
+ monkeypatch.delenv("ROCR_VISIBLE_DEVICES", raising = False)
+ return monkeypatch
+
+
+REPORTER_ADAPTERS = [
+ ("luid_0x00000000_0x0000d1e2_phys_0", 40.0 * GB), # W7900, model loaded
+ ("luid_0x00000000_0x0000e34a_phys_0", 0.5 * GB), # W7500, idle
+ ("luid_0x00000000_0x0000f001_phys_0", 3 * MiB), # Basic Render Driver
+]
+DEVICES = [("AMD Radeon PRO W7900", 48 * GB), ("AMD Radeon PRO W7500", 8 * GB)]
+
+
+# ----------------------------------------------------------------------------- #
+# System tab (get_visible_gpu_utilization) -- the reporter's screenshot
+# ----------------------------------------------------------------------------- #
+def test_system_tab_shows_per_gpu_used(win_rocm, monkeypatch):
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch(DEVICES, free_equals_total = True))
+ monkeypatch.setattr(
+ hw.subprocess, "run", _subprocess_run(adapter_output = _adapter_output(REPORTER_ADAPTERS))
+ )
+
+ devices = hw.get_visible_gpu_utilization()["devices"]
+ by_idx = {d["index"]: d for d in devices}
+ assert len(devices) == 2
+ assert by_idx[0]["vram_total_gb"] == 48.0
+ assert by_idx[0]["vram_used_gb"] == pytest.approx(40.0, abs = 0.01) # not 0
+ assert by_idx[1]["vram_total_gb"] == 8.0 # own total
+ # The 3 MiB Basic Render Driver counter makes this a hidden-adapter case: only
+ # the 40 GiB is forced onto the 48 GiB card; the idle card reads Unknown.
+ assert by_idx[1]["vram_used_gb"] is None
+ assert by_idx[1]["vram_utilization_pct"] is None
+ assert all(
+ d["vram_used_gb"] <= d["vram_total_gb"] for d in devices if d["vram_used_gb"] is not None
+ )
+
+
+def test_gpu_utilization_does_not_collapse(win_rocm, monkeypatch):
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch(DEVICES, free_equals_total = True))
+ monkeypatch.setattr(
+ hw.subprocess, "run", _subprocess_run(adapter_output = _adapter_output(REPORTER_ADAPTERS))
+ )
+
+ result = hw.get_gpu_utilization()
+ devices = result["devices"]
+ assert sorted(d["index"] for d in devices) == [0, 1] # both GPUs, no collapse
+ assert {d["vram_total_gb"] for d in devices} == {48.0, 8.0}
+ assert result["vram_total_gb"] == 48.0 # legacy primary mirror preserved
+
+
+def test_localized_counter_reports_unknown_not_zero(win_rocm, monkeypatch):
+ monkeypatch.setitem(sys.modules, "torch", _fake_torch(DEVICES, free_equals_total = True))
+ monkeypatch.setattr(hw.subprocess, "run", _subprocess_run(adapter_output = "__NONE__\n"))
+
+ devices = hw.get_visible_gpu_utilization()["devices"]
+ assert len(devices) == 2 # both still shown with correct totals
+ assert {d["vram_total_gb"] for d in devices} == {48.0, 8.0}
+ assert all(d["vram_used_gb"] is None for d in devices) # unknown, not fake 0
+ assert all(d["vram_utilization_pct"] is None for d in devices)
+
+
+# ----------------------------------------------------------------------------- #
+# mem_get_info free==total guard scoping
+# ----------------------------------------------------------------------------- #
+def test_mem_get_info_guard_scopes_to_windows_rocm(monkeypatch):
+ torch_mod = _fake_torch(DEVICES, free_equals_total = True)
+ monkeypatch.setattr(hw, "get_device", lambda: hw.DeviceType.CUDA)
+ monkeypatch.setitem(sys.modules, "torch", torch_mod)
+
+ # Windows ROCm -> used unknown (None), total kept.
+ monkeypatch.setattr(hw, "IS_ROCM", True)
+ monkeypatch.setattr(hw.sys, "platform", "win32")
+ win = hw._torch_get_per_device_info([0, 1])
+ assert [d["used_gb"] for d in win] == [None, None]
+ assert [d["total_gb"] for d in win] == [48.0, 8.0]
+
+ # Linux ROCm -> unchanged numeric used.
+ monkeypatch.setattr(hw.sys, "platform", "linux")
+ assert [d["used_gb"] for d in hw._torch_get_per_device_info([0, 1])] == [0.0, 0.0]
+
+ # Windows NVIDIA -> guard must not fire.
+ monkeypatch.setattr(hw, "IS_ROCM", False)
+ monkeypatch.setattr(hw.sys, "platform", "win32")
+ assert [d["used_gb"] for d in hw._torch_get_per_device_info([0, 1])] == [0.0, 0.0]
+
+
+# ----------------------------------------------------------------------------- #
+# Per-adapter attribution helpers (pure unit)
+# ----------------------------------------------------------------------------- #
+def test_match_adapter_pairs_and_clamps():
+ assert hw._match_adapter_used_to_devices([40 * GB, 0.5 * GB], [48 * GB, 8 * GB]) == [
+ 40 * GB,
+ 0.5 * GB,
+ ]
+ assert hw._match_adapter_used_to_devices([100 * GB], [48 * GB]) == [48 * GB] # clamp
+ assert hw._match_adapter_used_to_devices([40 * GB], [48 * GB, 8 * GB]) == [40 * GB, None]
+
+
+def test_match_adapter_reports_unknown_when_more_active_than_visible():
+ # More adapters actively using VRAM than are visible (a GPU outside the mask):
+ # attribution would fabricate a value, so report unknown for every device.
+ assert hw._match_adapter_used_to_devices([40 * GB, 0.5 * GB], [8 * GB]) == [None]
+
+
+def test_match_adapter_reports_unknown_when_hidden_high_use_adapter_survives_filter():
+ # Idle 8 GiB card (10 MiB noise) beside a hidden 48 GiB card at 40 GiB: the
+ # 40 GiB can't fit the 8 GiB device, so clamping there would fabricate. Unknown.
+ assert hw._match_adapter_used_to_devices([40 * GB, 10 * MiB], [8 * GB]) == [None]
+ # Order of the counters must not matter.
+ assert hw._match_adapter_used_to_devices([10 * MiB, 40 * GB], [8 * GB]) == [None]
+
+
+def test_match_adapter_reports_unknown_for_placeholder_fallback():
+ # Every counter below the 64 MiB floor plus a placeholder: no LUID-to-ordinal
+ # mapping tells placeholder from idle GPU, so report unknown, not fabricate.
+ # Single visible 8 GiB card idle (10 MiB) beside a 50 MiB placeholder counter.
+ assert hw._match_adapter_used_to_devices([50 * MiB, 10 * MiB], [8 * GB]) == [None]
+ # Order of the counters must not matter.
+ assert hw._match_adapter_used_to_devices([10 * MiB, 50 * MiB], [8 * GB]) == [None]
+ # Two idle visible GPUs plus a placeholder: all three counters below the floor.
+ assert hw._match_adapter_used_to_devices([50 * MiB, 10 * MiB, 5 * MiB], [48 * GB, 8 * GB]) == [
+ None,
+ None,
+ ]
+
+
+def test_match_adapter_reports_unknown_when_usage_not_capacity_ordered():
+ # 8 GiB card at 7 GiB beside a 48 GiB card at 5 GiB: the bigger usage still fits
+ # the smaller card, so both pairings are feasible -> unknown.
+ assert hw._match_adapter_used_to_devices([7 * GB, 5 * GB], [8 * GB, 48 * GB]) == [None, None]
+ # Device order must not matter (same physical situation, ordinals flipped).
+ assert hw._match_adapter_used_to_devices([7 * GB, 5 * GB], [48 * GB, 8 * GB]) == [None, None]
+ # Same-capacity cards with unequal usage are equally unattributable.
+ assert hw._match_adapter_used_to_devices([12 * GB, 8 * GB], [24 * GB, 24 * GB]) == [None, None]
+ # A single usage that fits both cards can sit on either -> unknown.
+ assert hw._match_adapter_used_to_devices([5 * GB], [48 * GB, 8 * GB]) == [None, None]
+ # But a capacity-forced assignment (usage exceeds the smaller card) is kept:
+ # 40 GiB can only be the 48 GiB card, so it is not fabrication.
+ assert hw._match_adapter_used_to_devices([40 * GB], [48 * GB, 8 * GB]) == [40 * GB, None]
+
+
+def test_match_adapter_reports_unknown_when_hidden_usage_fits_visible_card():
+ # A survivor that merely *fits* a visible card must not be pinned onto it. Two
+ # cards (48/8 GiB) at 40 GiB / 10 MiB beside a hidden 6 GiB adapter: the 6 GiB
+ # fits the idle 8 GiB card but isn't forced -> Unknown; only 40 GiB is forced.
+ assert hw._match_adapter_used_to_devices([40 * GB, 10 * MiB, 6 * GB], [48 * GB, 8 * GB]) == [
+ 40 * GB,
+ None,
+ ]
+ # Counter order must not matter.
+ assert hw._match_adapter_used_to_devices([6 * GB, 40 * GB, 10 * MiB], [48 * GB, 8 * GB]) == [
+ 40 * GB,
+ None,
+ ]
+ # A single visible card with a hidden adapter is never attributable: a fitting
+ # survivor could be the hidden GPU's while the visible card is idle.
+ assert hw._match_adapter_used_to_devices([6 * GB, 10 * MiB], [8 * GB]) == [None]
+
+
+def test_match_adapter_capacity_forced_matrix():
+ """Exhaustive hidden-adapter matrix for the capacity-forced rule.
+
+ A value is emitted only when the supra-threshold counters number exactly the
+ visible devices AND a device's ranked usage strictly exceeds every smaller
+ card's capacity. Otherwise (a visible card idle, a merely-fitting usage, or the
+ smallest card) every device reports unknown.
+ """
+ m = hw._match_adapter_used_to_devices
+ # -- exactly-n supra-threshold counters, capacity-forced survivors are kept - #
+ # Both visible cards have a real reading (the 3 MiB is a placeholder): 40 GiB
+ # forced onto the 48 GiB card, 0.5 GiB not forced -> None.
+ assert m([40 * GB, 0.5 * GB, 3 * MiB], [48 * GB, 8 * GB]) == [40 * GB, None]
+ # Three visible cards all active (supra-threshold) + placeholder: 40 > 24 and
+ # 20 > 8, both forced; the 8 GiB card is not forced -> None.
+ assert m([40 * GB, 20 * GB, 5 * GB, 3 * MiB], [48 * GB, 24 * GB, 8 * GB]) == [
+ 40 * GB,
+ 20 * GB,
+ None,
+ ]
+ # -- fewer supra-threshold counters than visible cards -> all unknown ------ #
+ # A visible card is idle, so even a "forced" 40 could be the hidden GPU's.
+ assert m([40 * GB, 3 * MiB, 3 * MiB], [48 * GB, 8 * GB]) == [None, None]
+ assert m([40 * GB, 10 * MiB, 10 * MiB], [48 * GB, 8 * GB]) == [None, None]
+ assert m([40 * GB, 20 * GB, 3 * MiB, 3 * MiB], [48 * GB, 24 * GB, 8 * GB]) == [
+ None,
+ None,
+ None,
+ ]
+ # Middle usage (6 GiB) fits both the 24 and 8 GiB cards, and only two cards are
+ # active for three visible -> not a bijection -> all unknown.
+ assert m([40 * GB, 6 * GB, 3 * MiB, 3 * MiB], [48 * GB, 24 * GB, 8 * GB]) == [
+ None,
+ None,
+ None,
+ ]
+ # -- hidden larger than every visible card -> all unknown ----------------- #
+ assert m([40 * GB, 10 * MiB], [8 * GB]) == [None]
+ assert m([48 * GB, 3 * MiB, 3 * MiB], [24 * GB, 8 * GB]) == [None, None]
+ # -- more active adapters than visible cards -> all unknown --------------- #
+ assert m([40 * GB, 7 * GB, 6 * GB, 3 * MiB], [48 * GB, 8 * GB]) == [None, None]
+ assert m([40 * GB, 7 * GB, 6 * GB, 3 * MiB, 3 * MiB], [48 * GB, 8 * GB]) == [None, None]
+ # -- every counter below the noise floor (placeholder fallback) -> unknown - #
+ assert m([50 * MiB, 10 * MiB], [8 * GB]) == [None]
+ assert m([50 * MiB, 10 * MiB, 5 * MiB], [48 * GB, 8 * GB]) == [None, None]
+ # -- equal-capacity cards with a hidden adapter: nothing is forced -------- #
+ assert m([40 * GB, 40 * GB, 3 * MiB], [48 * GB, 48 * GB]) == [None, None]
+ assert m([40 * GB, 30 * GB, 3 * MiB], [48 * GB, 48 * GB]) == [None, None]
+
+
+def test_perf_counter_parser_and_sentinel(monkeypatch):
+ monkeypatch.setattr(hw.platform, "system", lambda: "Windows")
+ monkeypatch.setattr(
+ hw.subprocess, "run", _subprocess_run(adapter_output = _adapter_output(REPORTER_ADAPTERS))
+ )
+ parsed = hw._rocm_windows_perf_counter_vram_by_adapter()
+ assert parsed is not None and len(parsed) == 3
+ assert parsed[0][0].startswith("luid_")
+ monkeypatch.setattr(hw.subprocess, "run", _subprocess_run(adapter_output = "__NONE__\n"))
+ assert hw._rocm_windows_perf_counter_vram_by_adapter() is None
+
+
+# ----------------------------------------------------------------------------- #
+# Unified-memory (Strix Halo APU) total reconciliation (Codex #7238)
+# ----------------------------------------------------------------------------- #
+def test_unified_memory_adopts_torch_total_even_when_used_unknown():
+ """Windows ROCm unified-memory APU: torch's used is None but its total (the full
+ GTT pool) is authoritative. The correction must still adopt the larger total;
+ used stays at amd-smi's figure when torch's is unknown."""
+ metrics = {"vram_total_gb": 8.0, "vram_used_gb": 2.0, "vram_utilization_pct": 25.0}
+ hw._apply_unified_memory_correction(metrics, {"total_gb": 124.0, "used_gb": None, "index": 0})
+ assert metrics["vram_total_gb"] == 124.0 # full unified pool, not the 8 GB carve-out
+ assert metrics["vram_used_gb"] == 2.0 # amd-smi used preserved (torch's was None)
+ assert metrics["vram_utilization_pct"] == pytest.approx(round(2.0 / 124.0 * 100, 1))
+
+
+def test_unified_memory_overwrites_used_when_torch_used_known():
+ """When torch reports both a larger total and a known used, both are adopted
+ and utilization is recomputed against the corrected total (unchanged path)."""
+ metrics = {"vram_total_gb": 8.0, "vram_used_gb": 2.0, "vram_utilization_pct": 25.0}
+ hw._apply_unified_memory_correction(metrics, {"total_gb": 124.0, "used_gb": 40.0, "index": 0})
+ assert metrics["vram_total_gb"] == 124.0
+ assert metrics["vram_used_gb"] == 40.0
+ assert metrics["vram_utilization_pct"] == pytest.approx(round(40.0 / 124.0 * 100, 1))
+
+
+def test_unified_memory_no_op_when_torch_total_not_larger():
+ """A discrete GPU where torch total does not exceed amd-smi's is left untouched."""
+ metrics = {"vram_total_gb": 48.0, "vram_used_gb": 10.0, "vram_utilization_pct": 20.8}
+ hw._apply_unified_memory_correction(metrics, {"total_gb": 48.0, "used_gb": None, "index": 0})
+ assert metrics["vram_total_gb"] == 48.0
+ assert metrics["vram_used_gb"] == 10.0
+ assert metrics["vram_utilization_pct"] == 20.8
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/tests/test_utils.py b/studio/backend/tests/test_utils.py
index 64a3c62156..741f19c67a 100644
--- a/studio/backend/tests/test_utils.py
+++ b/studio/backend/tests/test_utils.py
@@ -38,7 +38,7 @@ from utils.hardware import (
DeviceType,
)
import utils.hardware.hardware as _hw_module
-from utils.utils import format_error_message
+from utils.utils import format_error_message, is_hf_authentication_error
# ========== Helpers ==========
@@ -439,6 +439,20 @@ class TestFormatErrorMessage:
msg = format_error_message(err, "any/model")
assert "invalid" in msg.lower()
+ def test_hf_authentication_error_follows_wrapped_401(self):
+ response = type("Response", (), {"status_code": 401})()
+ auth_error = Exception("request failed")
+ auth_error.response = response
+ wrapper = RuntimeError("model validation failed")
+ wrapper.__cause__ = auth_error
+ assert is_hf_authentication_error(wrapper) is True
+
+ def test_hf_authentication_error_does_not_treat_429_as_invalid(self):
+ response = type("Response", (), {"status_code": 429})()
+ rate_error = Exception("too many requests")
+ rate_error.response = response
+ assert is_hf_authentication_error(rate_error) is False
+
# --- OOM on CUDA ---
@needs_torch
diff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.py
index adc9a54aab..9fef53e65e 100644
--- a/studio/backend/utils/hardware/hardware.py
+++ b/studio/backend/utils/hardware/hardware.py
@@ -538,21 +538,31 @@ def _torch_get_physical_gpu_count() -> Optional[int]:
def _torch_get_per_device_info(device_indices: list[int]) -> list[Dict[str, Any]]:
- """Query torch for per-GPU name, total VRAM, and used VRAM."""
+ """Query torch for per-GPU name, total VRAM, and used VRAM.
+
+ ``used_gb`` is ``None`` on Windows ROCm when ``hipMemGetInfo`` reports
+ ``free == total`` (ROCm/ROCm#1909): that 0 means unknown, not empty.
+ """
mod, _ = _torch_get_device_module()
if mod is None:
return []
+ # free==total is a Windows-ROCm-only quirk.
+ _win_rocm = sys.platform == "win32" and IS_ROCM
devices = []
for ordinal, phys_idx in enumerate(device_indices):
try:
# torch ordinals are 0-based relative to CUDA_VISIBLE_DEVICES.
props = mod.get_device_properties(ordinal)
total_bytes = props.total_memory
+ used_bytes: Optional[int]
# Prefer mem_get_info (system-wide) so auto-select sees other consumers.
if hasattr(mod, "mem_get_info"):
free_bytes, total_bytes = mod.mem_get_info(ordinal)
used_bytes = total_bytes - free_bytes
+ # free==total is the broken-API sentinel, not an idle GPU.
+ if _win_rocm and free_bytes == total_bytes:
+ used_bytes = None
else:
used_bytes = mod.memory_allocated(ordinal)
devices.append(
@@ -561,7 +571,7 @@ def _torch_get_per_device_info(device_indices: list[int]) -> list[Dict[str, Any]
"visible_ordinal": ordinal,
"name": props.name,
"total_gb": round(total_bytes / (1024**3), 2),
- "used_gb": round(used_bytes / (1024**3), 2),
+ "used_gb": round(used_bytes / (1024**3), 2) if used_bytes is not None else None,
}
)
except Exception as e:
@@ -724,20 +734,30 @@ def _rocm_linux_sysfs_vram_gb() -> tuple[Optional[float], Optional[float]]:
return None, None
-def _rocm_windows_perf_counter_vram_gb() -> tuple[Optional[float], Optional[float]]:
- """Query system-wide dedicated GPU VRAM via Windows Performance Counters.
+# ── Windows AMD/ROCm per-adapter VRAM (issue #7072) ──────────────────────────
+# amd-smi is disabled and hipMemGetInfo reports free==total, so read used from the
+# per-LUID "GPU Adapter Memory" perf counters and take each total from torch, so
+# every GPU shows instead of one fake device with GPU 0's total.
+# Placeholder adapters (Basic Render Driver / idle iGPU) drop only when they would
+# outnumber the real torch devices.
+_ROCM_WIN_ADAPTER_MIN_BYTES = 64 * 1024 * 1024 # 64 MiB
- Same data source as Task Manager, so cross-process usage is accurate.
- Works for any GPU vendor without amd-smi or nvidia-smi.
- Returns (used_gb, total_gb) or (None, None) on failure.
+
+def _rocm_windows_perf_counter_vram_by_adapter() -> Optional[list[tuple[str, float]]]:
+ """Per-adapter dedicated VRAM usage on Windows via Performance Counters.
+
+ Returns ``[(instance_name, used_bytes)]`` (one per LUID-named adapter), or
+ ``None`` when the counter is unavailable/localized/empty so callers fall back.
"""
if platform.system() != "Windows":
- return None, None
+ return None
try:
+ # Emit "|" per sample, or a __NONE__ sentinel.
ps = (
"$s=(Get-Counter '\\GPU Adapter Memory(*)\\Dedicated Usage'"
" -ErrorAction SilentlyContinue).CounterSamples;"
- "if($s){($s|Measure-Object CookedValue -Sum).Sum}else{-1}"
+ "if($s){$s|ForEach-Object{'{0}|{1}' -f $_.InstanceName,[int64]$_.CookedValue}}"
+ "else{'__NONE__'}"
)
r = subprocess.run(
["powershell", "-NoProfile", "-NonInteractive", "-Command", ps],
@@ -746,16 +766,167 @@ def _rocm_windows_perf_counter_vram_gb() -> tuple[Optional[float], Optional[floa
timeout = 5,
)
if r.returncode != 0 or not r.stdout.strip():
- return None, None
- used_bytes = float(r.stdout.strip())
- if used_bytes < 0:
- return None, None
- import torch as _torch
-
- total_bytes = _torch.cuda.get_device_properties(0).total_memory
- return round(used_bytes / (1024**3), 2), round(total_bytes / (1024**3), 2)
+ return None
+ adapters: list[tuple[str, float]] = []
+ for line in r.stdout.splitlines():
+ line = line.strip()
+ if not line or line == "__NONE__" or "|" not in line:
+ continue
+ instance, _, raw = line.rpartition("|")
+ try:
+ used = float(raw.strip())
+ except (ValueError, TypeError):
+ continue
+ if used < 0:
+ continue
+ adapters.append((instance.strip(), used))
+ return adapters or None
except Exception:
- return None, None
+ return None
+
+
+def _match_adapter_used_to_devices(
+ adapter_useds: list[float], device_totals: list[float]
+) -> list[Optional[float]]:
+ """Attribute per-adapter used bytes to torch devices by capacity ranking.
+
+ Windows shares no key between LUID counters and torch ordinals, so usages are
+ ranked against device totals and each is trusted only when capacity *forces* it
+ (it exceeds every smaller device); an ambiguous ranking reports unknown
+ (``None``) rather than fabricate a per-index free.
+
+ Extra counters mean a hidden/display adapter, and the noise filter may have
+ dropped a real reading, so values are emitted only when the supra-threshold
+ counters number EXACTLY the visible devices AND capacity forces the mapping;
+ otherwise every device is unknown. Best-effort but correct for the common
+ loaded-card case (#7072). Returns a list aligned to ``device_totals``.
+ """
+ n = len(device_totals)
+ if n == 0:
+ return []
+ useds = sorted(adapter_useds, reverse = True)
+ ranked_positions = sorted(range(n), key = lambda i: -device_totals[i])
+ ranked_totals = [device_totals[pos] for pos in ranked_positions]
+ assigned: list[Optional[float]]
+ # More counters than devices -> a hidden/display adapter (check before noise filter).
+ if len(useds) > n:
+ non_trivial = [u for u in useds if u >= _ROCM_WIN_ADAPTER_MIN_BYTES]
+ if len(non_trivial) != n:
+ # Not a clean bijection (a masked GPU is busy or a visible card idle):
+ # no counter maps to a specific card, so report unknown.
+ return [None] * n
+ # Exactly n supra-threshold counters: extras were placeholders, so a
+ # capacity-ranked bijection is plausible.
+ useds = non_trivial
+ ranked_useds = [useds[rank] for rank in range(n)]
+ # A usage above its ranked capacity is a hidden larger GPU; clamping onto the
+ # smaller card would fabricate a fully-used reading.
+ for rank in range(n):
+ if ranked_useds[rank] > ranked_totals[rank]:
+ return [None] * n
+ # Capacity forces the mapping only when the usage exceeds the next-smaller
+ # capacity; the smallest card and merely-fitting usages stay unknown.
+ # Keeps 40 GiB over 48/8 GiB -> [40, None].
+ assigned = [None] * n
+ for rank, pos in enumerate(ranked_positions):
+ if rank + 1 < n and ranked_useds[rank] > ranked_totals[rank + 1]:
+ assigned[pos] = min(ranked_useds[rank], device_totals[pos])
+ return assigned
+ # No hidden adapters: every counter is a visible card, so ranking is a permutation.
+ ranked_useds = [useds[rank] if rank < len(useds) else 0.0 for rank in range(n)]
+ # Ambiguous if a strictly larger usage also fits the next smaller card: the two
+ # could be swapped without breaking capacity, so ranking can't tell them apart.
+ for rank in range(n - 1):
+ upper, lower = ranked_useds[rank], ranked_useds[rank + 1]
+ if upper > lower and upper <= ranked_totals[rank + 1]:
+ return [None] * n
+ assigned = [None] * n
+ for rank, pos in enumerate(ranked_positions):
+ if rank < len(useds):
+ assigned[pos] = min(useds[rank], device_totals[pos])
+ return assigned
+
+
+def _rocm_windows_per_device_vram(device_indices: list[int]) -> list[Dict[str, Any]]:
+ """Per-GPU VRAM on Windows AMD/ROCm: total from torch properties (reliable),
+ used from the per-adapter Dedicated Usage counter.
+
+ Returns ``{index, visible_ordinal, name, used_gb, total_gb}`` per visible GPU
+ (``used_gb`` may be ``None`` when the counter is unavailable), or ``[]`` when
+ torch can't enumerate devices so callers fall through to the torch last resort.
+ """
+ if platform.system() != "Windows":
+ return []
+ mod, _ = _torch_get_device_module()
+ if mod is None:
+ return []
+ # Totals/names from torch properties (mem_get_info's free==total quirk zeroes used).
+ dev_meta: list[Dict[str, Any]] = []
+ for ordinal, phys_idx in enumerate(device_indices):
+ try:
+ props = mod.get_device_properties(ordinal)
+ dev_meta.append(
+ {
+ "index": phys_idx,
+ "visible_ordinal": ordinal,
+ "name": props.name,
+ "total_bytes": int(props.total_memory),
+ }
+ )
+ except Exception as e:
+ logger.debug("torch property probe failed for ordinal %d: %s", ordinal, e)
+ if not dev_meta:
+ return []
+
+ adapters = _rocm_windows_perf_counter_vram_by_adapter()
+ if adapters:
+ assigned = _match_adapter_used_to_devices(
+ [used for _, used in adapters],
+ [d["total_bytes"] for d in dev_meta],
+ )
+ else:
+ # Counter unavailable: show every GPU with a correct total, used unknown.
+ assigned = [None] * len(dev_meta)
+
+ devices: list[Dict[str, Any]] = []
+ for meta, used_bytes in zip(dev_meta, assigned):
+ total_gb = round(meta["total_bytes"] / (1024**3), 2)
+ used_gb = round(used_bytes / (1024**3), 2) if used_bytes is not None else None
+ devices.append(
+ {
+ "index": meta["index"],
+ "visible_ordinal": meta["visible_ordinal"],
+ "name": meta["name"],
+ "used_gb": used_gb,
+ "total_gb": total_gb,
+ }
+ )
+ return devices
+
+
+def _rocm_windows_device_payload_entry(
+ device: DeviceType, dev: Dict[str, Any], gpu_util_pct: Optional[float]
+) -> Dict[str, Any]:
+ """Build a ``get_gpu_utilization`` device entry from a per-device VRAM dict."""
+ total_gb = dev["total_gb"]
+ used_gb = dev["used_gb"]
+ return {
+ "available": True,
+ "backend": _backend_label(device),
+ "index": dev["index"],
+ "visible_ordinal": dev["visible_ordinal"],
+ "name": dev.get("name", "Unknown"),
+ "gpu_utilization_pct": gpu_util_pct,
+ "temperature_c": None,
+ "vram_used_gb": used_gb,
+ "vram_total_gb": total_gb,
+ "vram_utilization_pct": round((used_gb / total_gb) * 100, 1)
+ if total_gb and total_gb > 0 and used_gb is not None
+ else None,
+ "power_draw_w": None,
+ "power_limit_w": None,
+ "power_utilization_pct": None,
+ }
def _gpu_utilization_payload(
@@ -821,30 +992,24 @@ def get_gpu_utilization() -> Dict[str, Any]:
index_kind = result.get("index_kind"),
)
- # Fallback Windows ROCm
+ # Fallback Windows ROCm: per-adapter VRAM attribution (issue #7072), so
+ # every visible GPU is shown instead of a sum collapsed onto one device.
if IS_ROCM and platform.system() == "Windows":
- _win_used, _win_total = _rocm_windows_perf_counter_vram_gb()
- if _win_used is not None and _win_total is not None:
- _win_util = _rocm_windows_perf_counter_gpu_util_pct()
+ _win_ids = _get_parent_visible_gpu_spec().get("numeric_ids")
+ if not _win_ids:
+ _win_ids = list(range(_torch_get_physical_gpu_count() or 0))
+ _win_devices = _rocm_windows_per_device_vram(_win_ids)
+ if _win_devices:
+ # A single visible GPU can own the aggregate 3D-engine utilization;
+ # across several GPUs the sum isn't per-device, so leave it unset.
+ _win_util = (
+ _rocm_windows_perf_counter_gpu_util_pct() if len(_win_devices) == 1 else None
+ )
return _gpu_utilization_payload(
device,
[
- {
- "available": True,
- "backend": _backend_label(device),
- "index": 0,
- "visible_ordinal": 0,
- "gpu_utilization_pct": _win_util,
- "temperature_c": None,
- "vram_used_gb": _win_used,
- "vram_total_gb": _win_total,
- "vram_utilization_pct": round((_win_used / _win_total) * 100, 1)
- if _win_total > 0
- else None,
- "power_draw_w": None,
- "power_limit_w": None,
- "power_utilization_pct": None,
- }
+ _rocm_windows_device_payload_entry(device, _wd, _win_util)
+ for _wd in _win_devices
],
)
@@ -901,7 +1066,7 @@ def get_gpu_utilization() -> Dict[str, Any]:
"vram_used_gb": _used,
"vram_total_gb": _total,
"vram_utilization_pct": round((_used / _total) * 100, 1)
- if _total > 0
+ if _total > 0 and _used is not None
else None,
"power_draw_w": None,
"power_limit_w": None,
@@ -995,19 +1160,27 @@ def _apply_unified_memory_correction(
endpoints stay in sync on AMD iGPUs with unified memory.
"""
torch_total_gb = torch_info["total_gb"]
+ torch_used_gb = torch_info.get("used_gb")
smi_total_gb = device_metrics.get("vram_total_gb") or 0.0
+ # torch sees the full unified (GTT) pool; amd-smi only the dedicated carve-out.
+ # Adopt torch's larger total regardless of used: on Windows ROCm torch_used is
+ # None (free==total sentinel) but its total stays authoritative. Overwrite used
+ # only when torch's is known, then recompute utilization against whatever remains.
if torch_total_gb > smi_total_gb:
- torch_used_gb = torch_info["used_gb"]
device_metrics["vram_total_gb"] = torch_total_gb
- device_metrics["vram_used_gb"] = torch_used_gb
+ if torch_used_gb is not None:
+ device_metrics["vram_used_gb"] = torch_used_gb
+ _used_for_pct = device_metrics.get("vram_used_gb")
device_metrics["vram_utilization_pct"] = (
- round((torch_used_gb / torch_total_gb) * 100, 1) if torch_total_gb > 0 else None
+ round((_used_for_pct / torch_total_gb) * 100, 1)
+ if torch_total_gb > 0 and _used_for_pct is not None
+ else None
)
logger.debug(
- "ROCm unified memory: replaced amd-smi VRAM (%.2f GB) with "
- "torch mem_get_info total (%.2f GB) for device %s",
- smi_total_gb,
+ "ROCm unified memory: adopted torch mem_get_info total (%.2f GB) over "
+ "amd-smi (%.2f GB) for device %s",
torch_total_gb,
+ smi_total_gb,
torch_info.get("index"),
)
@@ -1067,6 +1240,49 @@ def get_visible_gpu_utilization() -> Dict[str, Any]:
_reconcile_rocm_unified_memory(result, numeric_ids)
return result
+ # Windows AMD/ROCm (issue #7072): the System tab's VRAM source. The torch
+ # fallback below would report used==0 (free==total), so read per-adapter
+ # Dedicated Usage instead; total from torch properties.
+ if IS_ROCM and platform.system() == "Windows":
+ win_numeric_ids = parent_visible_spec.get("numeric_ids")
+ if win_numeric_ids:
+ win_ids = win_numeric_ids
+ win_index_kind = "physical"
+ else:
+ win_ids = list(range(_torch_get_physical_gpu_count() or 0))
+ win_index_kind = "relative"
+ win_devices = _rocm_windows_per_device_vram(win_ids)
+ if win_devices:
+ devices = []
+ for wd in win_devices:
+ total = wd["total_gb"]
+ used = wd["used_gb"]
+ devices.append(
+ {
+ "index": wd["index"],
+ "index_kind": win_index_kind,
+ "visible_ordinal": wd["visible_ordinal"],
+ "name": wd.get("name"),
+ "gpu_utilization_pct": None,
+ "temperature_c": None,
+ "vram_used_gb": used,
+ "vram_total_gb": total,
+ "vram_utilization_pct": round((used / total) * 100, 1)
+ if total and total > 0 and used is not None
+ else None,
+ "power_draw_w": None,
+ "power_limit_w": None,
+ "power_utilization_pct": None,
+ }
+ )
+ return {
+ "available": True,
+ "backend": _backend_label(device),
+ "parent_visible_gpu_ids": win_numeric_ids or [],
+ "devices": devices,
+ "index_kind": win_index_kind,
+ }
+
# Torch-based fallback for CUDA (nvidia-smi unavailable, AMD ROCm) and XPU (Intel)
if device in (DeviceType.CUDA, DeviceType.XPU):
parent_ids = get_parent_visible_gpu_ids()
@@ -1094,7 +1310,7 @@ def get_visible_gpu_utilization() -> Dict[str, Any]:
"vram_used_gb": used,
"vram_total_gb": total,
"vram_utilization_pct": round((used / total) * 100, 1)
- if total > 0
+ if total > 0 and used is not None
else None,
"power_draw_w": None,
"power_limit_w": None,
diff --git a/studio/backend/utils/hf_token_validation.py b/studio/backend/utils/hf_token_validation.py
new file mode 100644
index 0000000000..7247c6e756
--- /dev/null
+++ b/studio/backend/utils/hf_token_validation.py
@@ -0,0 +1,208 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Cached, rate-limited Hugging Face token validation."""
+
+from __future__ import annotations
+
+import hashlib
+import threading
+import time
+from collections import deque
+from dataclasses import dataclass
+from typing import Literal
+
+from huggingface_hub import HfApi
+from huggingface_hub.utils import build_hf_headers, get_session
+
+
+TokenValidationStatus = Literal["valid", "invalid", "rate_limited", "unavailable"]
+
+
+@dataclass(frozen = True)
+class TokenValidationResult:
+ status: TokenValidationStatus
+ retry_after_seconds: int | None = None
+
+
+_WINDOW_SECONDS = 3600.0
+_MAX_ATTEMPTS = 3
+_CACHE_TTL_SECONDS = 3600.0
+_TEMPORARY_CACHE_TTL_SECONDS = 15.0
+_MAX_BUCKETS = 4096
+_MAX_CACHE_ENTRIES = 4096
+_INFLIGHT_WAIT_SECONDS = 30.0
+_REMOTE_TIMEOUT_SECONDS = 10.0
+
+_attempts: dict[str, deque[float]] = {}
+_cache: dict[str, tuple[float, TokenValidationResult]] = {}
+_inflight: dict[str, threading.Event] = {}
+_lock = threading.Lock()
+
+
+def _fingerprint(token: str) -> str:
+ return hashlib.sha256(token.encode("utf-8")).hexdigest()
+
+
+def _prune_attempts(bucket: deque[float], now: float) -> None:
+ while bucket and now - bucket[0] >= _WINDOW_SECONDS:
+ bucket.popleft()
+
+
+def _prune_locked(now: float) -> None:
+ for key in list(_attempts):
+ bucket = _attempts[key]
+ _prune_attempts(bucket, now)
+ if not bucket:
+ del _attempts[key]
+ for key, (expires_at, _result) in list(_cache.items()):
+ if expires_at <= now:
+ del _cache[key]
+
+
+def _cached_locked(fingerprint: str, now: float) -> TokenValidationResult | None:
+ cached = _cache.get(fingerprint)
+ if cached is None:
+ return None
+ expires_at, result = cached
+ if expires_at <= now:
+ del _cache[fingerprint]
+ return None
+ return result
+
+
+def _retry_after(bucket: deque[float], now: float) -> int:
+ return max(1, int(_WINDOW_SECONDS - (now - bucket[0])) + 1)
+
+
+def _reserve_attempt_locked(rate_key: str, now: float) -> TokenValidationResult | None:
+ bucket = _attempts.get(rate_key)
+ if bucket is None:
+ if len(_attempts) >= _MAX_BUCKETS:
+ _prune_locked(now)
+ if len(_attempts) >= _MAX_BUCKETS:
+ return TokenValidationResult(
+ status = "rate_limited",
+ retry_after_seconds = max(1, int(_WINDOW_SECONDS)),
+ )
+ bucket = _attempts[rate_key] = deque()
+ _prune_attempts(bucket, now)
+ if len(bucket) >= _MAX_ATTEMPTS:
+ return TokenValidationResult(
+ status = "rate_limited",
+ retry_after_seconds = _retry_after(bucket, now),
+ )
+ bucket.append(now)
+ return None
+
+
+def _http_status(response: object | None) -> int | None:
+ status = getattr(response, "status_code", None)
+ try:
+ return int(status) if status is not None else None
+ except (TypeError, ValueError):
+ return None
+
+
+def _remote_retry_after(response: object | None) -> int | None:
+ headers = getattr(response, "headers", None)
+ if not headers:
+ return None
+ raw = headers.get("Retry-After")
+ try:
+ return max(1, int(float(raw))) if raw is not None else None
+ except (TypeError, ValueError):
+ return None
+
+
+def _classify_response(response: object | None) -> TokenValidationResult:
+ status = _http_status(response)
+ if status is not None and 200 <= status < 300:
+ return TokenValidationResult(status = "valid")
+ if status == 401:
+ return TokenValidationResult(status = "invalid")
+ if status == 429:
+ return TokenValidationResult(
+ status = "rate_limited",
+ retry_after_seconds = _remote_retry_after(response),
+ )
+ return TokenValidationResult(status = "unavailable")
+
+
+def _check_remote(token: str) -> TokenValidationResult:
+ api = HfApi()
+ try:
+ # HfApi.whoami has no timeout parameter in the pinned Hub client.
+ # Use its session and headers against the same whoami endpoint.
+ response = get_session().get(
+ f"{api.endpoint}/api/whoami-v2",
+ headers = build_hf_headers(token = token),
+ timeout = _REMOTE_TIMEOUT_SECONDS,
+ )
+ except Exception as exc:
+ # huggingface-hub 0.36.x can wrap a 401 as requests.HTTPError.
+ return _classify_response(getattr(exc, "response", None))
+ return _classify_response(response)
+
+
+def validate_hf_token(token: str, *, rate_key: str) -> TokenValidationResult:
+ """Validate ``token`` without retaining it, sharing results across callers.
+
+ Cached checks do not consume the caller's three-per-hour network budget. A
+ single-flight event also prevents simultaneously mounted UI surfaces from
+ sending duplicate ``whoami`` requests for the same token.
+ """
+ normalized = token.strip()
+ if not normalized:
+ return TokenValidationResult(status = "invalid")
+ token_fingerprint = _fingerprint(normalized)
+ owner_event: threading.Event | None = None
+
+ try:
+ while True:
+ now = time.monotonic()
+ with _lock:
+ cached = _cached_locked(token_fingerprint, now)
+ if cached is not None:
+ return cached
+ waiting = _inflight.get(token_fingerprint)
+ if waiting is None:
+ limited = _reserve_attempt_locked(rate_key, now)
+ if limited is not None:
+ return limited
+ owner_event = threading.Event()
+ _inflight[token_fingerprint] = owner_event
+ break
+ if not waiting.wait(_INFLIGHT_WAIT_SECONDS):
+ return TokenValidationResult(status = "unavailable")
+
+ result = _check_remote(normalized)
+ now = time.monotonic()
+ ttl = (
+ _CACHE_TTL_SECONDS
+ if result.status in ("valid", "invalid")
+ else max(_TEMPORARY_CACHE_TTL_SECONDS, float(result.retry_after_seconds or 0))
+ )
+ with _lock:
+ if len(_cache) >= _MAX_CACHE_ENTRIES:
+ _prune_locked(now)
+ if len(_cache) < _MAX_CACHE_ENTRIES:
+ _cache[token_fingerprint] = (now + ttl, result)
+ return result
+ finally:
+ if owner_event is not None:
+ with _lock:
+ event = _inflight.get(token_fingerprint)
+ if event is owner_event:
+ _inflight.pop(token_fingerprint, None)
+ event.set()
+
+
+def reset_hf_token_validation_state() -> None:
+ """Clear process state for test isolation."""
+ with _lock:
+ for event in _inflight.values():
+ event.set()
+ _inflight.clear()
+ _attempts.clear()
+ _cache.clear()
diff --git a/studio/backend/utils/llama_cpp_update.py b/studio/backend/utils/llama_cpp_update.py
index ee1d415272..f5032bb102 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")
@@ -676,6 +683,7 @@ def start_update(expected_tag: Optional[str] = None) -> 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).
@@ -719,6 +727,8 @@ def start_update(expected_tag: Optional[str] = None) -> dict:
# install tag check. Unpinned only if the resolver reported no release tag.
pin_release_tag = (res or {}).get("release_tag") or None
resolved_tag = src.get("latest_tag")
+ # Source builds carry no forced-CPU marker, so nothing to preserve here.
+ force_cpu = False
# Install exactly the build the caller confirmed: a release published since
# confirmation moves latest above, so abort rather than swap an unconfirmed build.
@@ -759,7 +769,7 @@ def start_update(expected_tag: Optional[str] = None) -> 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/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/backend/utils/utils.py b/studio/backend/utils/utils.py
index 3818253ac9..31f5f31bee 100644
--- a/studio/backend/utils/utils.py
+++ b/studio/backend/utils/utils.py
@@ -123,6 +123,26 @@ def without_hf_auth():
os.environ.pop("HF_HUB_DISABLE_IMPLICIT_TOKEN", None)
+def is_hf_authentication_error(error: Exception) -> bool:
+ """Return whether an exception chain contains a definitive HF auth failure."""
+ seen: set[int] = set()
+ current: BaseException | None = error
+ while current is not None and id(current) not in seen:
+ seen.add(id(current))
+ response = getattr(current, "response", None)
+ status = getattr(response, "status_code", None)
+ try:
+ if status is not None and int(status) == 401:
+ return True
+ except (TypeError, ValueError):
+ pass
+ message = str(current).lower()
+ if "invalid user token" in message or "invalid hf token" in message:
+ return True
+ current = current.__cause__ or current.__context__
+ return False
+
+
def format_error_message(error: Exception, model_name: str) -> str:
"""
Format a user-friendly error message for common load issues.
diff --git a/studio/frontend/src/app/routes/__root.tsx b/studio/frontend/src/app/routes/__root.tsx
index ba56ce7525..e23892e020 100644
--- a/studio/frontend/src/app/routes/__root.tsx
+++ b/studio/frontend/src/app/routes/__root.tsx
@@ -16,6 +16,7 @@ import {
type ChatSearch,
} from "@/features/chat";
import { RemoteCodeConsentDialog } from "@/features/security";
+import { HfTokenWarningDialog } from "@/features/hf-auth";
import { TransformersUpgradeDialog } from "@/features/transformers-upgrade";
import { useTrainingUnloadGuard } from "@/features/training";
import { useExportRuntimeLifecycle } from "@/features/export";
@@ -230,6 +231,7 @@ function RootLayout() {
{!isAuthFlowRoute && }
+
{hideNavbar ? (
diff --git a/studio/frontend/src/components/app-sidebar.tsx b/studio/frontend/src/components/app-sidebar.tsx
index b6e218793a..b8601b00f6 100644
--- a/studio/frontend/src/components/app-sidebar.tsx
+++ b/studio/frontend/src/components/app-sidebar.tsx
@@ -969,10 +969,10 @@ export function AppSidebar() {
))}
- {/* Bulk export and import live in Settings -> Chat -> Data. */}
+ {/* Bulk export and import live in Settings -> Data. */}
- useSettingsDialogStore.getState().openDialog("chat")
+ useSettingsDialogStore.getState().openDialog("data")
}
>
Export all chats…
diff --git a/studio/frontend/src/components/assistant-ui/attachment.tsx b/studio/frontend/src/components/assistant-ui/attachment.tsx
index 98ebe5ab5f..b26840cc02 100644
--- a/studio/frontend/src/components/assistant-ui/attachment.tsx
+++ b/studio/frontend/src/components/assistant-ui/attachment.tsx
@@ -7,6 +7,7 @@
import { TooltipIconButton } from "@/components/assistant-ui/tooltip-icon-button";
import {
Dialog,
+ DialogClose,
DialogContent,
DialogTitle,
DialogTrigger,
@@ -27,12 +28,7 @@ import {
import { AudioWave01Icon, File02Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { PlusIcon, XIcon } from "lucide-react";
-import {
- type FC,
- type PropsWithChildren,
- useEffect,
- useState,
-} from "react";
+import { type FC, type PropsWithChildren, useEffect, useState } from "react";
import { useShallow } from "zustand/shallow";
const useFileSrc = (file: File | undefined): string | undefined => {
@@ -83,7 +79,7 @@ const AttachmentPreview: FC = ({ src }) => {
src={src}
alt="Preview"
className={cn(
- "block h-auto max-h-[80vh] w-auto max-w-full object-contain",
+ "block h-auto max-h-[90dvh] w-auto max-w-[92vw] object-contain",
isLoaded
? "aui-attachment-preview-image-loaded"
: "aui-attachment-preview-image-loading invisible",
@@ -108,12 +104,23 @@ const AttachmentPreviewDialog: FC = ({ children }) => {
>
{children}
-
+ {/* Chrome-free lightbox: the image floats on the dimmed backdrop with
+ no dialog panel, and the close button sits in the screen corner. */}
+
Image Attachment Preview
-
-
+ {/* Clicking the backdrop (anywhere off the image) closes the preview. */}
+
+
+
+
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 ff4bd4bebd..84119cc992 100644
--- a/studio/frontend/src/components/assistant-ui/model-selector/pickers.tsx
+++ b/studio/frontend/src/components/assistant-ui/model-selector/pickers.tsx
@@ -47,7 +47,7 @@ import {
import { useOnlineStatus } from "@/features/hub/hooks/use-online-status";
import { isHiddenModelId } from "@/features/hub/lib/hidden-models";
import { classifyUnslothSupport } from "@/features/hub/lib/unsloth-support";
-import { useHfTokenStore } from "@/features/hub/stores/hf-token-store";
+import { hfApiToken, useHfTokenStore } from "@/features/hub/stores/hf-token-store";
import {
downloadManager,
jobKeyOf,
@@ -67,6 +67,8 @@ import {
Download01Icon,
Flag01Icon,
Folder02Icon,
+ PinIcon,
+ PinOffIcon,
RemoveCircleIcon,
Search01Icon,
ViewIcon,
@@ -99,6 +101,11 @@ import {
loadedAt,
useModelLoadTimes,
} from "./model-usage";
+import {
+ pinKey,
+ pinnedQuantEntries,
+ usePinnedModelsStore,
+} from "./pinned-models";
import {
type FormatFilter,
estimateQuantBytes,
@@ -384,10 +391,64 @@ function CapabilityIcons({ caps }: { caps: ModelCapabilities }) {
);
}
+function normalizeModelIdForPicker(modelId: string): string {
+ const trimmed = modelId.trim();
+ const slashPath = trimmed.replace(/\\/g, "/").replace(/\/+$/, "");
+ const caseInsensitive =
+ !/^(\/|\.{1,2}\/|~\/)/.test(slashPath) ||
+ /^[A-Za-z]:\//.test(slashPath) ||
+ slashPath.startsWith("//") ||
+ /^\/mnt\/[A-Za-z](?:\/|$)/.test(slashPath);
+ return caseInsensitive ? slashPath.toLowerCase() : slashPath;
+}
+
+function modelIdsMatchForPicker(
+ left: string | null | undefined,
+ right: string | null | undefined,
+): boolean {
+ return Boolean(
+ left &&
+ right &&
+ normalizeModelIdForPicker(left) === normalizeModelIdForPicker(right),
+ );
+}
+
+function normalizeGgufVariantForPicker(variant: string | null | undefined) {
+ return variant?.trim().toLowerCase() ?? "";
+}
+
+function ggufVariantsMatchForPicker(
+ left: string | null | undefined,
+ right: string | null | undefined,
+): boolean {
+ return (
+ normalizeGgufVariantForPicker(left) ===
+ normalizeGgufVariantForPicker(right)
+ );
+}
+
+function isRuntimeLoadedModel(
+ loadedModelId: string | undefined,
+ activeGgufVariant: string | null | undefined,
+ modelId: string,
+ variantPolicy: "none" | "required" | "ignore",
+): boolean {
+ if (!modelIdsMatchForPicker(loadedModelId, modelId)) return false;
+ if (variantPolicy === "ignore") return true;
+ const hasActiveGgufVariant = !ggufVariantsMatchForPicker(
+ activeGgufVariant,
+ null,
+ );
+ return variantPolicy === "required"
+ ? hasActiveGgufVariant
+ : !hasActiveGgufVariant;
+}
+
function ModelRow({
label,
meta,
selected,
+ loaded = false,
onClick,
vramStatus,
vramEst,
@@ -405,6 +466,8 @@ function ModelRow({
label: string;
meta?: string | null;
selected?: boolean;
+ /** Override badge state when authoritative runtime state is available. */
+ loaded?: boolean;
onClick: () => void;
vramStatus?: VramFitStatus | null;
vramEst?: number;
@@ -494,7 +557,7 @@ function ModelRow({
)}
- {selected && (
+ {loaded && (
)}
- {downloaded && !selected && (
+ {downloaded && !loaded && (
void;
}) {
+ const pinnedKeys = usePinnedModelsStore((s) => s.pinned);
+ const togglePinnedQuant = usePinnedModelsStore((s) => s.togglePinned);
const onUpdateVariant = variantActions?.onUpdate;
const updateVariantTitle = variantActions?.updateTitle ?? "Update cached model?";
const renderUpdateVariantDescription = variantActions?.renderUpdateDescription;
@@ -946,7 +1015,7 @@ function GgufVariantExpander({
{v.downloaded ? (
<>
-
+
downloaded
{v.update_available ? (
@@ -1000,6 +1069,43 @@ function GgufVariantExpander({
onUpdated={() => setRefreshKey((key) => key + 1)}
/>
)}
+ {v.downloaded && allowPin && (
+
+
+ togglePinnedQuant(repoId, v.quant)}
+ aria-label={
+ pinnedKeys.includes(pinKey(repoId, v.quant))
+ ? `Unpin ${repoId} ${v.quant}`
+ : `Pin ${repoId} ${v.quant}`
+ }
+ aria-pressed={pinnedKeys.includes(pinKey(repoId, v.quant))}
+ className={cn(
+ "shrink-0 rounded-md p-1 transition-colors hover:bg-black/5 hover:text-foreground dark:hover:bg-white/10",
+ pinnedKeys.includes(pinKey(repoId, v.quant))
+ ? "text-foreground/80"
+ : "text-muted-foreground/60",
+ )}
+ >
+
+
+
+
+ {pinnedKeys.includes(pinKey(repoId, v.quant))
+ ? "Unpin quant"
+ : "Pin quant to the top"}
+
+
+ )}
{v.downloaded && (
onDeleteVariant(v.quant)}
+ onConfirm={async () => {
+ await onDeleteVariant(v.quant);
+ // Drop the pin too: a pinned row for a deleted file
+ // would try to load something that no longer exists.
+ if (pinnedKeys.includes(pinKey(repoId, v.quant))) {
+ togglePinnedQuant(repoId, v.quant);
+ }
+ }}
/>
)}
@@ -1057,6 +1170,17 @@ let _lmStudioCache: LocalModelInfo[] = [];
let _localDirCache: LocalModelInfo[] = [];
let _customFolderCache: LocalModelInfo[] = [];
let _scanFoldersCache: ScanFolderInfo[] = [];
+let _onDeviceCachesReady = false;
+let _cachedGgufRequestVersion = 0;
+let _cachedModelsRequestVersion = 0;
+let _localModelsRequestVersion = 0;
+const _onDeviceCacheListeners = new Set<(settled?: boolean) => void>();
+
+const ON_DEVICE_CACHE_TIMEOUT_MS = 30_000;
+
+function notifyOnDeviceCachesChanged(settled = false): void {
+ for (const listener of _onDeviceCacheListeners) listener(settled);
+}
/** True when any on-device model (downloaded GGUF, cached repo, LM Studio, or
* custom-folder model) is known. Reads the module caches, which persist across
@@ -1273,6 +1397,8 @@ export function HubModelPicker({
// Live model id from the runtime store (backend-mirrored active_model), not the dropdown
// highlight which can be a staged pick. Disables the update action for it.
const loadedModelId = useChatRuntimeStore((s) => s.params.checkpoint);
+ // Loaded GGUF quant of the active model; marks the matching pinned row.
+ const activeGgufVariant = useChatRuntimeStore((s) => s.activeGgufVariant);
// Last-loaded timestamps power the "Recent" sort (vs "Downloaded" = file date).
const loadTimes = useModelLoadTimes(value);
// Fade the list's top edge once scrolled, and its bottom edge while more
@@ -1285,7 +1411,8 @@ export function HubModelPicker({
// Shared Hub search stack (the same hooks the Hub page uses) so the picker
// and Hub run one implementation. Scoped to unsloth like the old listing.
const online = useOnlineStatus();
- const accessToken = hfToken || undefined;
+ // Sanitize to anonymous on a malformed token, matching the Hub page.
+ const accessToken = hfApiToken(hfToken);
// Recommended section: a live unsloth listing sorted by the dropdown. The
// same sort drives the search results so the dropdown works while searching.
const [recommendedSort, setRecommendedSort] =
@@ -1396,6 +1523,7 @@ export function HubModelPicker({
[expandQuantizations],
);
+ const [pinnedCollapsed, setPinnedCollapsed] = useState(false);
const [downloadedCollapsed, setDownloadedCollapsed] = useState(false);
const [otherModelsCollapsed, setOtherModelsCollapsed] = useState(false);
const [customFoldersCollapsed, setCustomFoldersCollapsed] = useState(false);
@@ -1453,8 +1581,7 @@ export function HubModelPicker({
useState(_cachedGgufCache);
const [cachedModels, setCachedModels] =
useState(_cachedModelsCache);
- const alreadyCached =
- _cachedGgufCache.length > 0 || _cachedModelsCache.length > 0;
+ const alreadyCached = _onDeviceCachesReady || hasDownloadedModels();
const [cachedReady, setCachedReady] = useState(alreadyCached);
const [updateConflictKey, setUpdateConflictKey] = useState(
null,
@@ -1489,6 +1616,25 @@ export function HubModelPicker({
const [customFolderModels, setCustomFolderModels] =
useState(_customFolderCache);
+ useEffect(() => {
+ const syncModuleCaches = (settled = false) => {
+ setCachedGguf(_cachedGgufCache);
+ setCachedModels(_cachedModelsCache);
+ setLmStudioModels(_lmStudioCache);
+ setLocalDirModels(_localDirCache);
+ setCustomFolderModels(_customFolderCache);
+ setCachedReady(
+ (ready) =>
+ ready || settled || _onDeviceCachesReady || hasDownloadedModels(),
+ );
+ };
+ _onDeviceCacheListeners.add(syncModuleCaches);
+ syncModuleCaches();
+ return () => {
+ _onDeviceCacheListeners.delete(syncModuleCaches);
+ };
+ }, []);
+
// Custom scan folders management
const [scanFolders, setScanFolders] =
useState(_scanFoldersCache);
@@ -1499,23 +1645,94 @@ export function HubModelPicker({
const [showFolderBrowser, setShowFolderBrowser] = useState(false);
const [recommendedFolders, setRecommendedFolders] = useState([]);
+ const applyLocalModels = useCallback(
+ (res: Awaited>) => {
+ const lm = sortLmStudio(
+ res.models.filter((m) => m.source === "lmstudio"),
+ );
+ _lmStudioCache = lm;
+ setLmStudioModels(lm);
+ const ld = res.models.filter((m) => m.source === "models_dir");
+ _localDirCache = ld;
+ setLocalDirModels(ld);
+ const cf = res.models.filter((m) => m.source === "custom");
+ _customFolderCache = cf;
+ setCustomFolderModels(cf);
+ notifyOnDeviceCachesChanged();
+ },
+ [],
+ );
+
+ const refreshColdOnDeviceCaches = useCallback(() => {
+ const ggufRequestVersion = ++_cachedGgufRequestVersion;
+ const modelsRequestVersion = ++_cachedModelsRequestVersion;
+ const localRequestVersion = ++_localModelsRequestVersion;
+ let ggufResult: Awaited> | undefined;
+ let modelsResult: Awaited> | undefined;
+ let localResult: Awaited> | undefined;
+ let released = false;
+
+ const ggufRequest = listCachedGguf().then(
+ (value) => { if (!released) ggufResult = value; },
+ () => {},
+ );
+ const modelsRequest = listCachedModels(hfToken || undefined).then(
+ (value) => { if (!released) modelsResult = value; },
+ () => {},
+ );
+ const localRequest = listLocalModels().then(
+ (value) => {
+ localResult = value;
+ if (released && localRequestVersion === _localModelsRequestVersion) {
+ if (ggufResult !== undefined && modelsResult !== undefined) _onDeviceCachesReady = true;
+ applyLocalModels(value);
+ }
+ },
+ () => {},
+ );
+ const isCurrent = () =>
+ ggufRequestVersion === _cachedGgufRequestVersion &&
+ modelsRequestVersion === _cachedModelsRequestVersion &&
+ localRequestVersion === _localModelsRequestVersion;
+ const publish = (invalidate = false) => {
+ if (!isCurrent()) return;
+ if (invalidate) {
+ released = true;
+ ++_cachedGgufRequestVersion;
+ ++_cachedModelsRequestVersion;
+ }
+ if (ggufResult !== undefined) {
+ _cachedGgufCache = ggufResult;
+ setCachedGguf(ggufResult);
+ }
+ if (modelsResult !== undefined) {
+ _cachedModelsCache = modelsResult;
+ setCachedModels(modelsResult);
+ }
+ if (localResult !== undefined) applyLocalModels(localResult);
+ if (ggufResult !== undefined && modelsResult !== undefined && localResult !== undefined) {
+ _onDeviceCachesReady = true;
+ }
+ notifyOnDeviceCachesChanged(true);
+ };
+ const timeout = window.setTimeout(() => publish(true), ON_DEVICE_CACHE_TIMEOUT_MS);
+ void Promise.all([ggufRequest, modelsRequest, localRequest]).then(() => {
+ window.clearTimeout(timeout);
+ publish();
+ });
+ }, [applyLocalModels, hfToken]);
+
const refreshLocalModelsList = useCallback(() => {
+ if (!_onDeviceCachesReady && !hasDownloadedModels()) return refreshColdOnDeviceCaches();
+ const requestVersion = ++_localModelsRequestVersion;
listLocalModels()
.then((res) => {
- const lm = sortLmStudio(
- res.models.filter((m) => m.source === "lmstudio"),
- );
- _lmStudioCache = lm;
- setLmStudioModels(lm);
- const ld = res.models.filter((m) => m.source === "models_dir");
- _localDirCache = ld;
- setLocalDirModels(ld);
- const cf = res.models.filter((m) => m.source === "custom");
- _customFolderCache = cf;
- setCustomFolderModels(cf);
+ if (requestVersion === _localModelsRequestVersion) {
+ applyLocalModels(res);
+ }
})
.catch(() => {});
- }, []);
+ }, [applyLocalModels, refreshColdOnDeviceCaches]);
const refreshScanFolders = useCallback(() => {
listScanFolders()
@@ -1594,20 +1811,27 @@ export function HubModelPicker({
);
const refreshCachedLists = useCallback(() => {
+ if (!_onDeviceCachesReady && !hasDownloadedModels()) return refreshColdOnDeviceCaches();
+ const ggufRequestVersion = ++_cachedGgufRequestVersion;
listCachedGguf()
.then((v) => {
+ if (ggufRequestVersion !== _cachedGgufRequestVersion) return;
_cachedGgufCache = v;
setCachedGguf(v);
+ notifyOnDeviceCachesChanged();
})
.catch(() => {});
+ const modelsRequestVersion = ++_cachedModelsRequestVersion;
listCachedModels(hfToken || undefined)
.then((v) => {
+ if (modelsRequestVersion !== _cachedModelsRequestVersion) return;
_cachedModelsCache = v;
setCachedModels(v);
+ notifyOnDeviceCachesChanged();
})
.catch(() => {});
refreshLocalModelsList();
- }, [hfToken, refreshLocalModelsList]);
+ }, [hfToken, refreshColdOnDeviceCaches, refreshLocalModelsList]);
// Updates run as managed downloads (Downloads panel: progress + Cancel), not a blocking
// call. The worker pulls only changed blobs, so the cached copy stays usable until done.
@@ -1635,36 +1859,100 @@ export function HubModelPicker({
);
useEffect(() => {
- // Always refresh LM Studio + custom folder models (not gated by alreadyCached).
- refreshLocalModelsList();
refreshScanFolders();
listRecommendedFolders()
.then(setRecommendedFolders)
.catch(() => {});
- // Always refetch cached GGUF/model lists. The module-level caches render
- // instantly with stale data (no spinner flash), but newly downloaded
- // repos need a fresh backend hit. cachedReady=alreadyCached initially,
- // so the background refresh is invisible when we already had data.
- let done = 0;
- const check = () => {
- if (++done >= 2) setCachedReady(true);
+ // Publish downloaded and local rows as one bounded snapshot. Existing data
+ // stays visible during background refreshes, and a failed source keeps its
+ // last successful cache instead of clearing or durably marking it ready.
+ const controller = new AbortController();
+ const timeout = window.setTimeout(
+ () => controller.abort(),
+ ON_DEVICE_CACHE_TIMEOUT_MS,
+ );
+ const aborted = new Promise((_, reject) => {
+ controller.signal.addEventListener(
+ "abort",
+ () => reject(controller.signal.reason),
+ { once: true },
+ );
+ });
+ const bounded = (request: Promise) =>
+ Promise.race([request, aborted]);
+ let cancelled = false;
+ const ggufRequestVersion = ++_cachedGgufRequestVersion;
+ const modelsRequestVersion = ++_cachedModelsRequestVersion;
+ const localRequestVersion = ++_localModelsRequestVersion;
+ const localRequest = listLocalModels();
+
+ void Promise.allSettled([
+ bounded(listCachedGguf(controller.signal)),
+ bounded(listCachedModels(hfToken || undefined, controller.signal)),
+ bounded(localRequest),
+ ]).then(([ggufResult, modelsResult, localResult]) => {
+ window.clearTimeout(timeout);
+ if (cancelled) return;
+
+ const ggufIsCurrent =
+ ggufRequestVersion === _cachedGgufRequestVersion;
+ const modelsAreCurrent =
+ modelsRequestVersion === _cachedModelsRequestVersion;
+ const localIsCurrent =
+ localRequestVersion === _localModelsRequestVersion;
+
+ if (ggufResult.status === "fulfilled" && ggufIsCurrent) {
+ _cachedGgufCache = ggufResult.value;
+ setCachedGguf(ggufResult.value);
+ notifyOnDeviceCachesChanged();
+ }
+ if (modelsResult.status === "fulfilled" && modelsAreCurrent) {
+ _cachedModelsCache = modelsResult.value;
+ setCachedModels(modelsResult.value);
+ notifyOnDeviceCachesChanged();
+ }
+ if (localResult.status === "fulfilled" && localIsCurrent) {
+ applyLocalModels(localResult.value);
+ }
+ if (localResult.status === "rejected" && controller.signal.aborted) {
+ void localRequest.then((value) => {
+ if (cancelled || localRequestVersion !== _localModelsRequestVersion) return;
+ if (ggufResult.status === "fulfilled" && modelsResult.status === "fulfilled") _onDeviceCachesReady = true;
+ applyLocalModels(value);
+ }).catch(() => {});
+ }
+ const snapshotIsCurrent =
+ ggufIsCurrent && modelsAreCurrent && localIsCurrent;
+ if (
+ ggufResult.status === "fulfilled" &&
+ modelsResult.status === "fulfilled" &&
+ localResult.status === "fulfilled" &&
+ snapshotIsCurrent
+ ) {
+ _onDeviceCachesReady = true;
+ }
+ notifyOnDeviceCachesChanged(snapshotIsCurrent);
+ });
+
+ return () => {
+ cancelled = true;
+ window.clearTimeout(timeout);
+ controller.abort();
+ queueMicrotask(() => {
+ if (
+ ggufRequestVersion === _cachedGgufRequestVersion &&
+ modelsRequestVersion === _cachedModelsRequestVersion &&
+ localRequestVersion === _localModelsRequestVersion
+ ) {
+ ++_cachedGgufRequestVersion;
+ ++_cachedModelsRequestVersion;
+ ++_localModelsRequestVersion;
+ notifyOnDeviceCachesChanged(true);
+ }
+ });
};
- listCachedGguf()
- .then((v) => {
- _cachedGgufCache = v;
- setCachedGguf(v);
- })
- .catch(() => {})
- .finally(check);
- listCachedModels(hfToken || undefined)
- .then((v) => {
- _cachedModelsCache = v;
- setCachedModels(v);
- })
- .catch(() => {})
- .finally(check);
- }, [hfToken, refreshLocalModelsList, refreshScanFolders]);
+ }, [applyLocalModels, hfToken, refreshScanFolders]);
// Hide downloaded models from the recommended list. Case-insensitive
// since the HF cache lowercases repo IDs.
@@ -1964,6 +2252,110 @@ export function HubModelPicker({
// logic must use this (not visibleCachedModels) or the picker can go blank.
const visibleCachedModelRows = chatOnly ? [] : visibleCachedModels;
+ // Pinned entries surface in their own section above the Unsloth heading.
+ // GGUF quants pin individually and their repo stays listed below; non-GGUF
+ // repos pin whole and leave the Unsloth / Other models groups.
+ const pinnedIds = usePinnedModelsStore((s) => s.pinned);
+ const togglePinned = usePinnedModelsStore((s) => s.togglePinned);
+ const pinnedSet = useMemo(() => new Set(pinnedIds), [pinnedIds]);
+
+ // Candidate pins whose repo still exists in the managed cache. Per-quant
+ // validation below is required because deleting one variant can leave a
+ // sibling quant (and therefore the repo row) cached.
+ const pinnedQuantCandidates = useMemo(() => {
+ // The existence check ignores the text query (but keeps the format filter)
+ // so a pinned quant stays findable by its quant name even when the repo id
+ // does not match the query; querying visibleCachedGguf here would drop the
+ // repo before the later `${repoId} ${quant}` predicate could surface it.
+ const cached = new Set(
+ sortedCachedGguf
+ .filter((c) => matchesFormatFilter(c.repo_id, true, formatFilter))
+ .map((c) => c.repo_id),
+ );
+ return pinnedQuantEntries(pinnedIds).filter((entry) =>
+ cached.has(entry.repoId),
+ );
+ }, [pinnedIds, sortedCachedGguf, formatFilter]);
+ const pinnedQuantValidationKey = useMemo(() => {
+ const cacheByRepo = new Map(
+ sortedCachedGguf.map((repo) => [repo.repo_id, repo]),
+ );
+ return pinnedQuantCandidates
+ .map((entry) => {
+ const cached = cacheByRepo.get(entry.repoId);
+ return `${pinKey(entry.repoId, entry.quant)}@${cached?.size_bytes ?? 0}:${cached?.last_modified ?? 0}`;
+ })
+ .join("\u0000");
+ }, [pinnedQuantCandidates, sortedCachedGguf]);
+ const [pinnedQuantValidation, setPinnedQuantValidation] = useState<{
+ key: string;
+ downloaded: ReadonlySet;
+ }>({ key: "", downloaded: new Set() });
+
+ useEffect(() => {
+ let cancelled = false;
+ const repoIds = Array.from(
+ new Set(pinnedQuantCandidates.map((entry) => entry.repoId)),
+ );
+ if (repoIds.length === 0) return;
+
+ void Promise.all(
+ repoIds.map(async (repoId) => {
+ try {
+ const response = await listGgufVariants(
+ repoId,
+ hfToken || undefined,
+ );
+ return normalizeGgufVariantsResponse(response).variants
+ .filter((variant) => variant.downloaded === true)
+ .map((variant) => pinKey(repoId, variant.quant));
+ } catch {
+ // If the backend cannot verify a quant, hiding the direct-load row
+ // is safer than claiming a missing file is downloaded.
+ return [];
+ }
+ }),
+ ).then((groups) => {
+ if (!cancelled) {
+ setPinnedQuantValidation({
+ key: pinnedQuantValidationKey,
+ downloaded: new Set(groups.flat()),
+ });
+ }
+ });
+
+ return () => {
+ cancelled = true;
+ };
+ }, [hfToken, pinnedQuantCandidates, pinnedQuantValidationKey]);
+ const downloadedPinnedQuantKeys = useMemo>(
+ () =>
+ pinnedQuantValidation.key === pinnedQuantValidationKey
+ ? pinnedQuantValidation.downloaded
+ : new Set(),
+ [pinnedQuantValidation, pinnedQuantValidationKey],
+ );
+
+ // Verified downloaded quants, in pin order and filtered by repo id or quant.
+ const pinnedQuants = useMemo(() => {
+ const q = normalizeForSearch(debouncedQuery.trim());
+ return pinnedQuantCandidates.filter(
+ (entry) =>
+ downloadedPinnedQuantKeys.has(pinKey(entry.repoId, entry.quant)) &&
+ (!q ||
+ normalizeForSearch(`${entry.repoId} ${entry.quant}`).includes(q)),
+ );
+ }, [
+ debouncedQuery,
+ downloadedPinnedQuantKeys,
+ pinnedQuantCandidates,
+ ]);
+
+ const pinnedCachedModelRows = useMemo(
+ () => visibleCachedModelRows.filter((c) => pinnedSet.has(pinKey(c.repo_id))),
+ [visibleCachedModelRows, pinnedSet],
+ );
+
// Split downloaded models so non-Unsloth repos get their own "Other models"
// section above Fine-tuned.
const unslothCachedGguf = useMemo(
@@ -1975,12 +2367,18 @@ export function HubModelPicker({
[visibleCachedGguf],
);
const unslothCachedModelRows = useMemo(
- () => visibleCachedModelRows.filter((c) => isUnslothRepoId(c.repo_id)),
- [visibleCachedModelRows],
+ () =>
+ visibleCachedModelRows.filter(
+ (c) => isUnslothRepoId(c.repo_id) && !pinnedSet.has(pinKey(c.repo_id)),
+ ),
+ [visibleCachedModelRows, pinnedSet],
);
const otherCachedModelRows = useMemo(
- () => visibleCachedModelRows.filter((c) => !isUnslothRepoId(c.repo_id)),
- [visibleCachedModelRows],
+ () =>
+ visibleCachedModelRows.filter(
+ (c) => !isUnslothRepoId(c.repo_id) && !pinnedSet.has(pinKey(c.repo_id)),
+ ),
+ [visibleCachedModelRows, pinnedSet],
);
// Param counts come straight off the unsloth listings the picker already
@@ -2076,6 +2474,25 @@ export function HubModelPicker({
const hubOptionKeys = useMemo(() => {
const keys: string[] = [];
+ // Pinned rows sit above the Unsloth heading on the On Device tab.
+ if (
+ section === "downloaded" &&
+ cachedReady &&
+ !pinnedCollapsed &&
+ (pinnedQuants.length > 0 || pinnedCachedModelRows.length > 0)
+ ) {
+ keys.push(
+ ...pinnedQuants.map((entry) =>
+ makeModelOptionKey("pinned-quant", pinKey(entry.repoId, entry.quant)),
+ ),
+ );
+ keys.push(
+ ...pinnedCachedModelRows.map((model) =>
+ makeModelOptionKey("downloaded-model", model.repo_id),
+ ),
+ );
+ }
+
// Downloaded (Unsloth) rows (query-filtered) on the On Device tab only.
if (
section === "downloaded" &&
@@ -2126,12 +2543,12 @@ export function HubModelPicker({
}
// Fine-tuned models sit below downloaded, above custom folders.
- if (section === "downloaded" && !fineTunedCollapsed) {
+ if (section === "downloaded" && cachedReady && !fineTunedCollapsed) {
keys.push(...fineTunedRows.map((m) => makeModelOptionKey("lora", m.id)));
}
// Custom folders sit right below the downloaded models on On Device.
- if (section === "downloaded" && !customFoldersCollapsed) {
+ if (section === "downloaded" && cachedReady && !customFoldersCollapsed) {
keys.push(
...sortedCustomFolderModels.map((model) =>
makeModelOptionKey("custom-folder", model.id),
@@ -2139,7 +2556,7 @@ export function HubModelPicker({
);
}
- if (section === "downloaded" && !lmStudioCollapsed) {
+ if (section === "downloaded" && cachedReady && !lmStudioCollapsed) {
keys.push(
...sortedLmStudio.map((model) =>
makeModelOptionKey("lm-studio", model.id),
@@ -2147,7 +2564,7 @@ export function HubModelPicker({
);
}
- if (section === "downloaded" && !localDirCollapsed) {
+ if (section === "downloaded" && cachedReady && !localDirCollapsed) {
keys.push(
...sortedLocalDir.map((model) =>
makeModelOptionKey("local-dir", model.id),
@@ -2167,6 +2584,9 @@ export function HubModelPicker({
chatOnly,
sortedCustomFolderModels,
customFoldersCollapsed,
+ pinnedQuants,
+ pinnedCachedModelRows,
+ pinnedCollapsed,
downloadedCollapsed,
fineTunedRows,
fineTunedCollapsed,
@@ -2337,6 +2757,7 @@ export function HubModelPicker({
const showDownloaded = section === "downloaded";
const showCustom = section === "downloaded";
const showRecommendedSection = !showHfSection && section === "recommended";
+ const onDeviceCacheLoading = showDownloaded && !cachedReady;
const downloadedEmpty =
visibleCachedGguf.length === 0 &&
visibleCachedModelRows.length === 0 &&
@@ -2475,6 +2896,151 @@ export function HubModelPicker({
selected && "bg-[#ececec] dark:bg-[var(--sidebar-accent)]",
);
+ // Pin toggle at a row's right edge: hidden until the row is hovered (or the
+ // button is focused), always visible while pinned so pinned rows read as such.
+ // `small` matches the compact quant-row action sizing; it also skips the
+ // hide-until-hover classes since small pins render inside a hover-gated group.
+ const renderPinAction = (
+ repoId: string,
+ quant?: string,
+ opts?: { className?: string; small?: boolean },
+ ) => {
+ const pinned = pinnedSet.has(pinKey(repoId, quant));
+ const target = quant ? `${repoId} ${quant}` : repoId;
+ return (
+
+
+ {
+ e.stopPropagation();
+ togglePinned(repoId, quant);
+ }}
+ aria-label={pinned ? `Unpin ${target}` : `Pin ${target}`}
+ aria-pressed={pinned}
+ className={cn(
+ "shrink-0 rounded-md transition-colors hover:bg-black/5 dark:hover:bg-white/10",
+ opts?.small ? "p-1" : "p-1.5",
+ pinned
+ ? "text-foreground/80 hover:text-foreground"
+ : "text-muted-foreground/60 hover:text-foreground",
+ !pinned &&
+ !opts?.small &&
+ "opacity-0 focus-visible:opacity-100 group-hover:opacity-100 group-focus-within:opacity-100",
+ opts?.className,
+ )}
+ >
+
+
+
+
+ {pinned
+ ? quant
+ ? "Unpin quant"
+ : "Unpin model"
+ : quant
+ ? "Pin quant to the top"
+ : "Pin model to the top"}
+
+
+ );
+ };
+
+ // A pinned quant: repo name with the quant as a grey chip. One click loads
+ // that quant directly, no expansion needed.
+ const renderPinnedQuantRow = (entry: { repoId: string; quant: string }) => {
+ const optionKey = makeModelOptionKey(
+ "pinned-quant",
+ pinKey(entry.repoId, entry.quant),
+ );
+ const { owner, name } = splitRepoLabel(entry.repoId);
+ const isSelected = value === entry.repoId && activeGgufVariant === entry.quant;
+ const isLoaded =
+ modelIdsMatchForPicker(loadedModelId, entry.repoId) &&
+ !ggufVariantsMatchForPicker(activeGgufVariant, null) &&
+ ggufVariantsMatchForPicker(activeGgufVariant, entry.quant);
+ return (
+
+
+ onSelect(entry.repoId, {
+ source: "hub",
+ isLora: false,
+ ggufVariant: entry.quant,
+ isDownloaded: true,
+ })
+ }
+ className={cn(
+ "flex min-w-0 flex-1 items-center gap-2 rounded-full px-2 py-1.5 text-left text-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/45",
+ downloadedRowButtonClassName,
+ )}
+ title={`${entry.repoId} (${entry.quant})`}
+ >
+
+ {owner ? (
+
+ {owner}
+ /
+
+ ) : null}
+ {name}
+
+
+ {entry.quant}
+
+ {isLoaded && (
+
+ )}
+
+
+ {renderPinAction(entry.repoId, entry.quant, { small: true })}
+
+
+ This will remove{" "}
+
+ {entry.repoId} ({entry.quant})
+ {" "}
+ from disk. You can re-download it later.
+ >
+ }
+ successMessage={`Deleted ${entry.repoId} ${entry.quant}`}
+ buttonClassName="p-1"
+ iconClassName="size-3"
+ disabled={deleteDisabled}
+ onConfirm={async () => {
+ await deleteCachedModel(entry.repoId, entry.quant);
+ refreshCachedLists();
+ // The file is gone, so drop its pin too.
+ togglePinned(entry.repoId, entry.quant);
+ }}
+ />
+
+
+ );
+ };
+
// Shared row renderers so Downloaded (Unsloth) and Other models render alike.
const renderDownloadedGgufRow = (c: (typeof visibleCachedGguf)[number]) => {
const optionKey = makeModelOptionKey("downloaded-gguf", c.repo_id);
@@ -2489,6 +3055,12 @@ export function HubModelPicker({
meta="GGUF"
showVision={c.has_vision ?? visionByRepo[c.repo_id]}
selected={isSelected}
+ loaded={isRuntimeLoadedModel(
+ loadedModelId,
+ activeGgufVariant,
+ c.repo_id,
+ "required",
+ )}
optionProps={hubModelList.getOptionProps(optionKey, isSelected)}
onClick={() => toggleGgufExpanded(c.repo_id)}
onArrowDownIntoChildren={
@@ -2506,6 +3078,7 @@ export function HubModelPicker({
reportVision(c.repo_id, v)}
onSelect={onSelect}
hfToken={hfToken || undefined}
@@ -2523,6 +3096,7 @@ export function HubModelPicker({
await deleteCachedModel(c.repo_id, quant);
refreshCachedLists();
},
+ deleteDisabled,
}}
/>
)}
@@ -2547,6 +3121,12 @@ export function HubModelPicker({
c.size_bytes,
)}`}
selected={isSelected}
+ loaded={isRuntimeLoadedModel(
+ loadedModelId,
+ activeGgufVariant,
+ c.repo_id,
+ "none",
+ )}
optionProps={hubModelList.getOptionProps(
optionKey,
isSelected,
@@ -2562,6 +3142,7 @@ export function HubModelPicker({
className={downloadedRowButtonClassName}
/>
+ {renderPinAction(c.repo_id)}
deleteCachedModel(c.repo_id)}
+ disabled={deleteDisabled}
+ onConfirm={async () => {
+ await deleteCachedModel(c.repo_id);
+ if (pinnedSet.has(pinKey(c.repo_id))) {
+ togglePinned(c.repo_id);
+ }
+ }}
onDeleted={refreshCachedLists}
/>
@@ -2721,11 +3308,8 @@ export function HubModelPicker({
)
) : (
<>
- {/* First-load spinner only when nothing cached is shown yet. */}
- {showDownloaded &&
- !cachedReady &&
- !showHfSection &&
- downloadedEmpty ? (
+ {/* First-load spinner while downloaded/local scans are resolving. */}
+ {onDeviceCacheLoading ? (
@@ -2749,12 +3333,37 @@ export function HubModelPicker({
) : null}
+ {/* Pinned quants and models sit above the Unsloth heading so
+ favorites are always first. Filtered by the query like the
+ sections below. */}
+ {showDownloaded &&
+ (pinnedQuants.length > 0 ||
+ pinnedCachedModelRows.length > 0) ? (
+ <>
+ }
+ collapsed={pinnedCollapsed}
+ onToggle={() => setPinnedCollapsed((v) => !v)}
+ >
+ Pinned
+
+ {!pinnedCollapsed && pinnedQuants.map(renderPinnedQuantRow)}
+ {!pinnedCollapsed &&
+ pinnedCachedModelRows.map(renderDownloadedModelRow)}
+ >
+ ) : null}
+
{/* Downloaded (Unsloth) stays visible (filtered) while searching. */}
{showDownloaded &&
+ cachedReady &&
(unslothCachedGguf.length > 0 ||
unslothCachedModelRows.length > 0) ? (
<>
0 ||
+ pinnedCachedModelRows.length > 0
+ }
collapsed={downloadedCollapsed}
onToggle={() => setDownloadedCollapsed((v) => !v)}
action={
@@ -2840,7 +3449,7 @@ export function HubModelPicker({
{/* Other models: non-Unsloth downloads, grouped just above
Fine-tuned. Shown only when such models exist. */}
- {showDownloaded && hasOtherModels ? (
+ {showDownloaded && cachedReady && hasOtherModels ? (
) : null}
- {/* Fine-tuned models: a section above Custom Folders. Always shown on
- On Device so the train shortcut always has a target, with an empty
- state when none exist. */}
- {section === "downloaded" ? (
+ {/* Fine-tuned models: shown after the On Device scans resolve so
+ downloaded sections do not reorder during startup. */}
+ {section === "downloaded" && cachedReady ? (
<>
) : null}
- {showCustom ? (
+ {showCustom && cachedReady ? (
<>
) : null}
- {section === "downloaded" && sortedLmStudio.length > 0 ? (
+ {section === "downloaded" &&
+ cachedReady &&
+ sortedLmStudio.length > 0 ? (
<>
) : null}
- {section === "downloaded" && sortedLocalDir.length > 0 ? (
+ {section === "downloaded" &&
+ cachedReady &&
+ sortedLocalDir.length > 0 ? (
<>
)}
@@ -3497,6 +4148,12 @@ export function HubModelPicker({
: (vram?.detail ?? extractParamLabel(id))
}
selected={value === id}
+ loaded={isRuntimeLoadedModel(
+ loadedModelId,
+ activeGgufVariant,
+ id,
+ isKnownGgufRepo(id) ? "required" : "none",
+ )}
optionProps={hubModelList.getOptionProps(
optionKey,
value === id,
@@ -3546,6 +4203,7 @@ export function HubModelPicker({
await deleteCachedModel(id, quant);
refreshCachedLists();
},
+ deleteDisabled,
}}
/>
)}
@@ -3586,6 +4244,12 @@ export function HubModelPicker({
.join(" · ")
}
selected={value === id}
+ loaded={isRuntimeLoadedModel(
+ loadedModelId,
+ activeGgufVariant,
+ id,
+ isSearchGguf ? "required" : "none",
+ )}
optionProps={hubModelList.getOptionProps(
optionKey,
value === id,
@@ -3637,6 +4301,7 @@ export function HubModelPicker({
await deleteCachedModel(id, quant);
refreshCachedLists();
},
+ deleteDisabled,
}}
/>
)}
@@ -3687,6 +4352,8 @@ export function HubModelPicker({
function FineTunedRows({
adapters,
value,
+ loadedModelId,
+ activeGgufVariant,
onSelect,
onModelsChange,
deleteDisabled = false,
@@ -3697,6 +4364,8 @@ function FineTunedRows({
}: {
adapters: LoraModelOption[];
value?: string;
+ loadedModelId?: string;
+ activeGgufVariant?: string | null;
onSelect: (id: string, meta: ModelSelectorChangeMeta) => void;
onModelsChange?: (deletedModel?: DeletedModelRef) => void;
deleteDisabled?: boolean;
@@ -3753,6 +4422,12 @@ function FineTunedRows({
label={adapter.name}
meta={meta}
selected={value === adapter.id}
+ loaded={isRuntimeLoadedModel(
+ loadedModelId,
+ activeGgufVariant,
+ adapter.id,
+ isLocalGgufDir || isExportedGguf ? "required" : "none",
+ )}
optionProps={loraModelList.getOptionProps(
optionKey,
value === adapter.id,
diff --git a/studio/frontend/src/components/assistant-ui/model-selector/pinned-models.ts b/studio/frontend/src/components/assistant-ui/model-selector/pinned-models.ts
new file mode 100644
index 0000000000..4835c4c0cf
--- /dev/null
+++ b/studio/frontend/src/components/assistant-ui/model-selector/pinned-models.ts
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Pinned models for the model selector's On Device list, persisted in
+// localStorage so pins survive reloads. GGUF quants pin individually
+// (repoId + quant); non-GGUF repos pin as a whole. Pinned entries surface
+// in a "Pinned" section above the Unsloth/Downloaded group.
+
+import { create } from "zustand";
+
+const KEY = "unsloth_pinned_models";
+
+// Entries are stored as strings: "repoId" pins a whole (non-GGUF) repo,
+// "repoId::quant" pins one GGUF quant. Neither part contains "::".
+export function pinKey(repoId: string, quant?: string): string {
+ return quant ? `${repoId}::${quant}` : repoId;
+}
+
+export interface PinnedQuantEntry {
+ repoId: string;
+ quant: string;
+}
+
+/** The pinned GGUF quants, in pin order. Plain repo pins are excluded. */
+export function pinnedQuantEntries(pinned: string[]): PinnedQuantEntry[] {
+ const out: PinnedQuantEntry[] = [];
+ for (const key of pinned) {
+ const sep = key.indexOf("::");
+ if (sep <= 0) continue;
+ const repoId = key.slice(0, sep);
+ const quant = key.slice(sep + 2);
+ if (repoId && quant) out.push({ repoId, quant });
+ }
+ return out;
+}
+
+function readPinned(): string[] {
+ try {
+ const raw = JSON.parse(localStorage.getItem(KEY) ?? "[]");
+ return Array.isArray(raw)
+ ? raw.filter((v): v is string => typeof v === "string")
+ : [];
+ } catch {
+ return [];
+ }
+}
+
+function writePinned(pinned: string[]): void {
+ try {
+ localStorage.setItem(KEY, JSON.stringify(pinned));
+ } catch {
+ // Ignore unavailable storage; pins stay session-only.
+ }
+}
+
+interface PinnedModelsState {
+ pinned: string[];
+ togglePinned: (repoId: string, quant?: string) => void;
+}
+
+export const usePinnedModelsStore = create((set) => ({
+ pinned: readPinned(),
+ togglePinned: (repoId, quant) =>
+ set((state) => {
+ const key = pinKey(repoId, quant);
+ const next = state.pinned.includes(key)
+ ? state.pinned.filter((id) => id !== key)
+ : [...state.pinned, key];
+ writePinned(next);
+ return { pinned: next };
+ }),
+}));
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 235dcb3c3d..32fbd61e09 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -1434,13 +1434,10 @@ const Composer: FC<{
const artifactsEnabled = useChatRuntimeStore((s) => s.artifactsEnabled);
const mcpEnabledForChat = useChatRuntimeStore((s) => s.mcpEnabledForChat);
const ragEnabled = useChatRuntimeStore((s) => s.ragEnabled);
- const permissionMode = useChatRuntimeStore((s) => s.permissionMode);
- // More than 4 pills: collapse to icons only. Search and Code always show; the
- // permission pill shows in every mode except "off" (it renders null there);
- // Images, RAG, Canvas and MCP are conditional.
+ // More than 4 pills: collapse to icons only. Search, Code, and permissions
+ // always show; Images, RAG, Canvas and MCP are conditional.
const pillsCompact =
- 2 +
- (permissionMode !== "off" ? 1 : 0) +
+ 3 +
(ragEnabled ? 1 : 0) +
(supportsBuiltinImageGeneration ? 1 : 0) +
(artifactsEnabled ? 1 : 0) +
@@ -1556,20 +1553,6 @@ const Composer: FC<{
const t = setTimeout(() => writeComposerDraft(draftKey, composerText), 300);
return () => clearTimeout(t);
}, [composerText, draftKey]);
- // Two-row layout shows once the input wraps or a tool is on. Tools can
- // pre-select before a model loads, so an active toggle expands it either way.
- // Keep the composer expanded whenever the permission pill is visible.
- const composerExpanded =
- isMultiline ||
- hasAttachments ||
- hasPendingAudio ||
- toolsEnabled ||
- codeToolsEnabled ||
- imageToolsEnabled ||
- ragEnabled ||
- artifactsEnabled ||
- mcpEnabledForChat ||
- permissionMode !== "off";
// react-textarea-autosize re-measures only on value change or window resize,
// not on the width swap from expanding, so it keeps the taller height and
// leaves a stray blank row. Nudge a resize whenever input width changes.
@@ -1856,27 +1839,25 @@ const Composer: FC<{
- {/* Permission-level pill: always visible, even while the pill row
- is collapsed; opens the permission level dropdown. */}
+ {/* Permission-level pill: always visible and opens the permission
+ level dropdown. */}
- {composerExpanded ? (
- <>
-
-
-
-
- {artifactsEnabled ?
: null}
- {mcpEnabledForChat ? (
-
- ) : null}
- >
+
+
+
+
+ {artifactsEnabled ?
: null}
+ {mcpEnabledForChat ? (
+
) : null}
sum + (device.memory_total_gb ?? 0),
0,
);
- const vramUsed = devices.reduce(
- (sum, device) => sum + (device.vram_used_gb ?? 0),
- 0,
- );
+ // null usage = unknown (e.g. Windows ROCm perf counter): treating it as 0
+ // fabricates a 0-used readout, so the aggregate is unknown if any device is.
+ const vramUsageKnown =
+ devices.length > 0 &&
+ devices.every((device) => Number.isFinite(device.vram_used_gb));
+ const vramUsed = vramUsageKnown
+ ? devices.reduce((sum, device) => sum + (device.vram_used_gb ?? 0), 0)
+ : 0;
const vramPercent = clampPercent(
- vramTotal > 0 ? (vramUsed / vramTotal) * 100 : 0,
+ vramUsageKnown && vramTotal > 0 ? (vramUsed / vramTotal) * 100 : 0,
);
+ const unknownLabel = t("settings.resources.environment.unknown");
const hasGpu = (systemInfo.gpu?.available ?? false) && devices.length > 0;
@@ -164,17 +169,20 @@ export function FloatingMonitor() {
- {Math.round(vramPercent)}%
+ {vramUsageKnown ? `${Math.round(vramPercent)}%` : "--"}
- {formatGiB(vramUsed)} / {formatGiB(vramTotal)}
+ {vramUsageKnown ? formatGiB(vramUsed) : unknownLabel} /{" "}
+ {formatGiB(vramTotal)}
diff --git a/studio/frontend/src/components/ui/tooltip.tsx b/studio/frontend/src/components/ui/tooltip.tsx
index cbc1a09a8f..91745af5ec 100644
--- a/studio/frontend/src/components/ui/tooltip.tsx
+++ b/studio/frontend/src/components/ui/tooltip.tsx
@@ -67,9 +67,14 @@ function TooltipTrigger({
const handleClick = useCallback(
(e: React.MouseEvent) => {
+ // Run the composed handler first: when this trigger wraps another Radix
+ // trigger (e.g. DialogTrigger around an attachment tile), that trigger's
+ // action is skipped if the event is already default-prevented.
+ onClick?.(e);
+ // preventDefault keeps Radix Tooltip's internal close-on-click from
+ // undoing the tap-toggle below (its composed handler checks it).
e.preventDefault();
toggle?.();
- onClick?.(e);
},
[toggle, onClick],
);
diff --git a/studio/frontend/src/features/chat/api/chat-api.ts b/studio/frontend/src/features/chat/api/chat-api.ts
index 0f6af38033..475bd0f801 100644
--- a/studio/frontend/src/features/chat/api/chat-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-api.ts
@@ -2,7 +2,12 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { authFetch } from "@/features/auth";
+import { prepareHfTokenForUse } from "@/features/hf-auth";
+// These helpers are deliberately API-layer-only and are not part of their
+// features' React-facing public barrels.
+// eslint-disable-next-line no-restricted-imports
import { hubTokenHeader } from "@/features/hub/lib/hub-token-header";
+// eslint-disable-next-line no-restricted-imports
import { consumeNativePathToken } from "@/features/native-intents/api";
import { formatFastApiDetail } from "@/lib/format-fastapi-error";
import type {
@@ -104,11 +109,14 @@ export async function getApiMonitorEntry(id: string): Promise {
export async function loadModel(
payload: LoadModelRequest,
): Promise {
+ const preparedToken = await prepareHfTokenForUse(payload.hf_token);
+ if (!preparedToken.proceed) throw new Error("Model load cancelled.");
const response = await authFetch("/api/inference/load", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
...payload,
+ hf_token: preparedToken.token,
native_path_lease: payload.nativePathLease ?? null,
nativePathLease: undefined,
}),
@@ -119,13 +127,15 @@ export async function loadModel(
export async function validateModel(
payload: LoadModelRequest,
): Promise {
+ const preparedToken = await prepareHfTokenForUse(payload.hf_token);
+ if (!preparedToken.proceed) throw new Error("Model load cancelled.");
const response = await authFetch("/api/inference/validate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model_path: payload.model_path,
native_path_lease: payload.nativePathLease ?? null,
- hf_token: payload.hf_token,
+ hf_token: preparedToken.token,
gguf_variant: payload.gguf_variant ?? null,
// Intended load settings so validate's preflight matches the follow-up
// /load. Default placement is sized against the selected GPUs.
@@ -324,13 +334,17 @@ interface LocalModelListResponse {
models: LocalModelInfo[];
}
-export async function listLocalModels(): Promise {
- const response = await authFetch("/api/models/local");
+export async function listLocalModels(
+ signal?: AbortSignal,
+): Promise {
+ const response = await authFetch("/api/models/local", { signal });
return parseJsonOrThrow(response);
}
-export async function listCachedGguf(): Promise {
- const response = await authFetch("/api/models/cached-gguf");
+export async function listCachedGguf(
+ signal?: AbortSignal,
+): Promise {
+ const response = await authFetch("/api/models/cached-gguf", { signal });
const data = await parseJsonOrThrow<{ cached: CachedGgufRepo[] }>(response);
return data.cached;
}
@@ -345,9 +359,11 @@ export interface CachedModelRepo {
export async function listCachedModels(
hfToken?: string | null,
+ signal?: AbortSignal,
): Promise {
const response = await authFetch("/api/models/cached-models", {
headers: hubTokenHeader(hfToken),
+ signal,
});
const data = await parseJsonOrThrow<{ cached: CachedModelRepo[] }>(response);
return data.cached;
@@ -437,6 +453,73 @@ export async function listChatThreads(
return Array.isArray(data.threads) ? data.threads : [];
}
+/** One chat message attachment, as listed for the settings uploaded-files view. */
+export interface ChatAttachmentRecord {
+ id: string;
+ messageId: string;
+ threadId: string;
+ pairId?: string | null;
+ threadTitle?: string | null;
+ name: string;
+ type?: string | null;
+ contentType?: string | null;
+ sizeBytes?: number | null;
+ createdAt?: number | null;
+}
+
+export interface ChatAttachmentPage {
+ attachments: ChatAttachmentRecord[];
+ nextOffset: number | null;
+}
+
+export async function listChatAttachments(
+ offset = 0,
+ limit = 50,
+): Promise {
+ const params = new URLSearchParams({
+ limit: String(limit),
+ offset: String(offset),
+ });
+ const response = await authFetch(`/api/chat/attachments?${params}`);
+ const data = await parseJsonOrThrow<{
+ attachments: ChatAttachmentRecord[];
+ nextOffset: number | null;
+ }>(response);
+ return {
+ attachments: Array.isArray(data.attachments) ? data.attachments : [],
+ nextOffset:
+ typeof data.nextOffset === "number" && Number.isFinite(data.nextOffset)
+ ? data.nextOffset
+ : null,
+ };
+}
+
+/** Stored attachment content (image bytes or extracted text) as a Blob. */
+export async function fetchChatAttachmentBlob(
+ messageId: string,
+ attachmentId: string,
+): Promise {
+ const response = await authFetch(
+ `/api/chat/attachments/${encodeURIComponent(messageId)}/${encodeURIComponent(attachmentId)}/file`,
+ );
+ if (!response.ok) {
+ const body = await response.json().catch(() => null);
+ throw new Error(parseErrorText(response.status, body));
+ }
+ return response.blob();
+}
+
+export async function deleteChatAttachment(
+ messageId: string,
+ attachmentId: string,
+): Promise {
+ const response = await authFetch(
+ `/api/chat/attachments/${encodeURIComponent(messageId)}/${encodeURIComponent(attachmentId)}`,
+ { method: "DELETE" },
+ );
+ await parseJsonOrThrow<{ ok: boolean }>(response);
+}
+
export async function getChatThread(
threadId: string,
): Promise {
@@ -960,7 +1043,8 @@ export async function* streamChatCompletions(
parsed.type === "reasoning_summary"
) {
yield {
- _reasoningDurationMs: (parsed as { duration_ms?: number }).duration_ms,
+ _reasoningDurationMs: (parsed as { duration_ms?: number })
+ .duration_ms,
} as unknown as OpenAIChatChunk;
separatorIndex = buffer.search(/\r?\n\r?\n/);
continue;
diff --git a/studio/frontend/src/features/chat/bypass-permissions-menu-item.tsx b/studio/frontend/src/features/chat/bypass-permissions-menu-item.tsx
index b35317b2fa..8c9a528e66 100644
--- a/studio/frontend/src/features/chat/bypass-permissions-menu-item.tsx
+++ b/studio/frontend/src/features/chat/bypass-permissions-menu-item.tsx
@@ -20,16 +20,12 @@ import {
DropdownMenuSubTrigger,
} from "@/components/ui/dropdown-menu";
import { useChatRuntimeStore } from "@/features/chat/stores/chat-runtime-store";
-import { PermissionModeMenuItems } from "./permission-mode-select";
+import {
+ FULL_ACCESS_WARNING,
+ PermissionModeMenuItems,
+} from "./permission-mode-select";
-// "Bypass permissions" entry for the composer "+" -> More menu. Like the MCP
-// pill, it opens a submenu where the user picks the permission level (Ask for
-// approval / Approve for me / Full access). Picking Full access demands the
-// danger warning; the other levels apply immediately. The menu closes normally
-// on select (no preventDefault) -- the warning dialog lives outside the menu
-// (BypassPermissionsConfirmDialog, mounted once at the chat-page root and
-// driven by the store), so it survives the menu unmounting and the "+"/More
-// popovers don't stay frozen.
+// Tool permissions entry for the composer "+" menu.
export function BypassPermissionsMenuItem() {
const permissionMode = useChatRuntimeStore((s) => s.permissionMode);
const setBypassConfirmOpen = useChatRuntimeStore(
@@ -44,7 +40,7 @@ export function BypassPermissionsMenuItem() {
}
>
- Bypass permissions
+ Tool permissions
Enable Full access?
- Full access (Bypass permissions) is dangerous since the AI model
- might delete, corrupt your machine, and or cause real world damage
- to you or the world - only accept if you are certain
+ {FULL_ACCESS_WARNING}
diff --git a/studio/frontend/src/features/chat/chat-settings-sheet.tsx b/studio/frontend/src/features/chat/chat-settings-sheet.tsx
index b848702f79..f8cf8c04da 100644
--- a/studio/frontend/src/features/chat/chat-settings-sheet.tsx
+++ b/studio/frontend/src/features/chat/chat-settings-sheet.tsx
@@ -733,11 +733,11 @@ export function ChatSettingsPanel({
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;
+ // The picker must keep one GPU selected.
+ const singleGpuInUse = gpusInUse.length <= 1;
+ // TP needs at least two GPUs because tensor split is a no-op on one and may
+ // abort. Auto layers hides TP because --fit aborts under --split-mode tensor.
+ const tpDisabled = singleGpuInUse;
// 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
@@ -1547,7 +1547,7 @@ export function ChatSettingsPanel({
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.
+ every GPU. At least one GPU must stay selected.
@@ -1567,7 +1567,10 @@ export function ChatSettingsPanel({
checked={isGpuChecked(d.index)}
onCheckedChange={() => toggleGpu(d.index)}
data-test-id={`gpu-pick-${d.index}`}
- disabled={modelControlsDisabled}
+ disabled={
+ modelControlsDisabled ||
+ (isGpuChecked(d.index) && singleGpuInUse)
+ }
/>
))}
@@ -2443,13 +2446,14 @@ function ConfirmToolCallsToggle() {
When on, every local Unsloth tool call pauses for your approval
before it runs (the "Ask for approval" level). When off, tool calls
- run without prompts inside the sandbox (the "Off" level).
+ run without prompts inside the sandbox (the "Run automatically"
+ level).
Provider-hosted tools are not gated here.
{permissionMode === "full" ? (
- Overridden by Full access (Bypass permissions)
+ Overridden by Full access
) : null}
@@ -2470,11 +2474,11 @@ function BypassPermissionsToggle() {
- Bypass permissions
+ Tool permissions
- How Unsloth approves tool calls before they run. Full access is
- dangerous: it disables confirmations and the code sandbox.
+ Choose how Unsloth approves tool calls before they run. Full access
+ disables confirmations and the code sandbox.
{/* Full width, styled like the panel selects/preset input. */}
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 bfb3eeb14c..a08bd5fa54 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
@@ -217,6 +217,40 @@ export async function archiveChatItem(
notifyChatHistoryUpdated();
}
+export async function archiveAllChatItems(
+ activeId?: string,
+ onSelect?: (view: { mode: "single"; newThreadNonce: string }) => void,
+): Promise
{
+ const threads = await listStoredChatThreads({ includeArchived: true });
+ // Boolean() mirrors groupThreads: legacy records may have archived
+ // undefined/null, which must count as "not archived".
+ const toArchive = threads.filter((t) => !t.archived);
+ if (toArchive.length === 0) return 0;
+
+ for (const t of toArchive) cancelIfRunning(t.id);
+
+ await Promise.all(
+ toArchive.map((t) => updateStoredChatThread(t.id, { archived: true })),
+ );
+
+ // Reset only when this action archived the active single thread or compare
+ // pair. An already-archived chat opened from the archive is not in
+ // toArchive and must stay open.
+ const archivedActive =
+ activeId !== undefined &&
+ toArchive.some(
+ (thread) => thread.id === activeId || thread.pairId === activeId,
+ );
+ if (archivedActive) {
+ useChatRuntimeStore.getState().setActiveThreadId(null);
+ onSelect?.({ mode: "single", newThreadNonce: crypto.randomUUID() });
+ }
+
+ notifyChatHistoryUpdated();
+ // Report sidebar items, not raw threads: a compare pair reads as one chat.
+ return groupThreads(toArchive).length;
+}
+
export async function unarchiveChatItem(item: SidebarItem): Promise {
const threadIds: string[] =
item.type === "single"
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index a8b5fc23ad..b0059b57b1 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -3,10 +3,15 @@
export { ChatPage, validateChatSearch, type ChatSearch } from "./chat-page";
export {
+ deleteChatAttachment,
+ fetchChatAttachmentBlob,
getInferenceStatus,
+ listChatAttachments,
listGgufVariants,
listLocalModels,
loadModel,
+ type ChatAttachmentPage,
+ type ChatAttachmentRecord,
type LocalModelInfo,
} from "./api/chat-api";
export type { GgufVariantDetail } from "./types/api";
@@ -17,6 +22,10 @@ export {
type Preset,
} from "./chat-settings-sheet";
export { useChatRuntimeStore } from "./stores/chat-runtime-store";
+export {
+ CHAT_RAG_CAPTION_KEY,
+ CHAT_RAG_OCR_KEY,
+} from "./stores/chat-runtime-store";
export {
preferFullToolOutput,
toolOutputKey,
@@ -46,12 +55,16 @@ export { setTrainingCompareHandoff } from "./lib/training-compare-handoff";
export type { ProjectRecord } from "./types";
export { clearAllChats, countAllChats } from "./utils/clear-all-chats";
export { listStoredChatThreads } from "./utils/chat-history-storage";
+export { emitChatAttachmentDeleted } from "./utils/chat-attachment-events";
export { ArtifactCard } from "./artifacts/artifact-card";
export {
useChatArtifactsStore,
useSelectedChatArtifact,
} from "./artifacts/store";
-export { downloadChatExport } from "./utils/export-chat-history";
+export {
+ downloadChatExport,
+ downloadArchivedChatExport,
+} from "./utils/export-chat-history";
export {
clearNewChatDraft,
composerDraftKey,
@@ -60,10 +73,14 @@ export {
} from "./utils/composer-draft";
export {
EXPORT_FORMATS_LIST,
+ buildFineTuneJsonl,
bulkExportConversationsByScope,
+ exportFineTuneJsonl,
importConversationsFromFile,
+ type FineTuneFormat,
} from "./prompt-storage/prompt-storage-dialog";
export {
+ archiveAllChatItems,
archiveChatItem,
deleteChatItem,
renameChatItem,
diff --git a/studio/frontend/src/features/chat/permission-mode-select.tsx b/studio/frontend/src/features/chat/permission-mode-select.tsx
index a9cb8ce5d1..e6c89cf54a 100644
--- a/studio/frontend/src/features/chat/permission-mode-select.tsx
+++ b/studio/frontend/src/features/chat/permission-mode-select.tsx
@@ -7,7 +7,6 @@ import {
CircleOff,
Hand,
ShieldCheck,
- XIcon,
} from "lucide-react";
import { useState } from "react";
@@ -39,9 +38,8 @@ import {
} from "./stores/chat-runtime-store";
/**
- * Permission levels for the Bypass permissions dropdowns (General settings,
- * chat settings sheet, composer "+" menu). Off sits last as the toggle that
- * turns the feature off entirely.
+ * Permission levels for tool calls. Full access stays last because it disables
+ * both approval prompts and the code sandbox.
*/
export const PERMISSION_MODE_OPTIONS: readonly {
value: PermissionMode;
@@ -61,6 +59,12 @@ export const PERMISSION_MODE_OPTIONS: readonly {
description: "Only ask for actions detected as potentially unsafe",
icon: ShieldCheck,
},
+ {
+ value: "off",
+ label: "Run automatically",
+ description: "Run tool calls without approval prompts inside the sandbox",
+ icon: CircleOff,
+ },
{
value: "full",
label: "Full access",
@@ -68,14 +72,11 @@ export const PERMISSION_MODE_OPTIONS: readonly {
"Unrestricted: no approval prompts and the code sandbox is disabled",
icon: CircleAlert,
},
- {
- value: "off",
- label: "Off",
- description: "Turn off bypass permissions",
- icon: CircleOff,
- },
] as const;
+export const FULL_ACCESS_WARNING =
+ "Full access lets tool calls run without approval prompts or the code sandbox. They can modify or delete files, run commands, and make network requests. Enable it only when you trust the current task.";
+
export function permissionModeOption(mode: PermissionMode) {
return (
PERMISSION_MODE_OPTIONS.find((option) => option.value === mode) ??
@@ -100,10 +101,10 @@ export function PermissionModeMenuItems({
{
- // Reselecting the active level toggles the feature off.
if (option.value === permissionMode) {
- setPermissionMode("off");
- } else if (option.value === "full") {
+ return;
+ }
+ if (option.value === "full") {
onRequestFullAccess();
} else {
setPermissionMode(option.value);
@@ -154,9 +155,7 @@ export function FullAccessConfirmDialog({
Enable Full access?
- Full access (Bypass permissions) is dangerous since the AI model
- might delete, corrupt your machine, and or cause real world damage
- to you or the world - only accept if you are certain
+ {FULL_ACCESS_WARNING}
@@ -260,15 +259,10 @@ export function PermissionModeComposerPill({
const setBypassConfirmOpen = useChatRuntimeStore(
(s) => s.setBypassConfirmOpen,
);
- const setPermissionMode = useChatRuntimeStore((s) => s.setPermissionMode);
const active = permissionModeOption(permissionMode);
const ActiveIcon = active.icon;
const fullAccess = permissionMode === "full";
- // Off means the feature is off: no pill (re-enable via the "+" menu or
- // settings, like the pre-levels bypass badge).
- if (permissionMode === "off") return null;
-
return (
@@ -278,30 +272,11 @@ export function PermissionModeComposerPill({
data-pill-label={active.label}
data-active={fullAccess ? "true" : "false"}
data-variant={fullAccess ? "danger" : undefined}
- data-keep-label="true"
aria-label="Permission level for tool calls"
title={`${active.label}: ${active.description}`}
>
- {/* The icon doubles as an off switch (mirrors the MCP pill): hover
- swaps it to an X; clicking it turns bypass permissions Off (no
- prompts, sandbox on) without opening the menu. data-keep-label
- exempts this pill from compact icon-only mode, so the off switch
- stays clickable even while the other pills are collapsed. */}
- {
- e.stopPropagation();
- }}
- onClick={(e) => {
- e.stopPropagation();
- setPermissionMode("off");
- }}
- className="composer-pill-glyph cursor-pointer"
- >
+
-
{active.label}
(messages: T[]): T[] {
+function orderByParentChain(
+ messages: T[],
+ options: {
+ /** Append messages off the selected chain (abandoned branches) at the
+ * end. Full exports keep everything; fine-tune conversion must not,
+ * since alternate replies would merge into one conversation. */
+ includeSiblings?: boolean;
+ } = {},
+): T[] {
+ const { includeSiblings = true } = options;
const byId = new Map(messages.map((m) => [m.id, m]));
const childrenOf = new Map();
for (const m of messages) {
@@ -207,7 +216,9 @@ function orderByParentChain(messages: T[]): T[] {
byId.delete(next.id);
}
- for (const [, m] of byId) result.push(m);
+ if (includeSiblings) {
+ for (const [, m] of byId) result.push(m);
+ }
return result;
}
@@ -543,6 +554,214 @@ export async function exportProjectConversations(
);
}
+// ── Fine-tuning export ─────────────────────────────────────────────────────
+// One JSONL line per conversation: {"messages": [{"role", "content"}]} with
+// string-only content in system/user/assistant turns. Unsloth's training tab
+// detects this as ChatML natively (no column mapping, no standardization) and
+// it works with train-on-completions masking, which only trains on assistant
+// turns. Reasoning, tool calls, and images are dropped: clean SFT targets.
+
+export type FineTuneMessage = {
+ role: "system" | "user" | "assistant";
+ content: string;
+};
+
+const FINE_TUNE_ROLES = new Set(["system", "user", "assistant"]);
+
+/** Plain text of a message: text blocks plus text-type attachment parts. */
+function messageToPlainText(msg: {
+ content: unknown;
+ attachments?: unknown;
+}): string {
+ const parts: string[] = [];
+ const collect = (blocks: unknown) => {
+ // Legacy and imported histories can store content as a plain string.
+ if (typeof blocks === "string") {
+ if (blocks.trim()) parts.push(blocks);
+ return;
+ }
+ if (!Array.isArray(blocks)) return;
+ for (const b of blocks) {
+ if (!b || typeof b !== "object") {
+ continue;
+ }
+ const block = b as Record;
+ if (block.type === "text" && typeof block.text === "string" && block.text) {
+ parts.push(block.text);
+ }
+ }
+ };
+ collect(msg.content);
+ if (Array.isArray(msg.attachments)) {
+ for (const attachment of msg.attachments as Array<{ content?: unknown }>) {
+ collect(attachment?.content);
+ }
+ }
+ return parts.join("\n\n").trim();
+}
+
+/** Merge consecutive same-role turns so chat templates format cleanly. */
+function mergeSameRoleTurns(turns: FineTuneMessage[]): FineTuneMessage[] {
+ const merged: FineTuneMessage[] = [];
+ for (const turn of turns) {
+ const last = merged[merged.length - 1];
+ if (last && last.role === turn.role) {
+ last.content += `\n\n${turn.content}`;
+ } else {
+ merged.push({ ...turn });
+ }
+ }
+ return merged;
+}
+
+/** Conversation turns for fine-tuning, or null when the thread has no
+ * usable user + assistant exchange. Consecutive same-role turns merge,
+ * assistant turns before the first user turn drop (an assistant target
+ * with no prompt teaches nothing), and trailing non-assistant turns drop
+ * so chat templates format cleanly. */
+function messagesToFineTuneTurns(
+ messages: Array<{ role: unknown; content: unknown; attachments?: unknown }>,
+): FineTuneMessage[] | null {
+ const raw: FineTuneMessage[] = [];
+ for (const msg of messages) {
+ const role = msg.role as FineTuneMessage["role"];
+ if (!FINE_TUNE_ROLES.has(role)) continue;
+ const content = messageToPlainText(msg);
+ if (!content) continue;
+ raw.push({ role, content });
+ }
+ const firstUser = raw.findIndex((t) => t.role === "user");
+ if (firstUser === -1) return null;
+ const turns = mergeSameRoleTurns(
+ raw.filter((t, i) => i >= firstUser || t.role === "system"),
+ );
+ while (turns.length > 0 && turns[turns.length - 1].role !== "assistant") {
+ turns.pop();
+ }
+ const hasUser = turns.some((t) => t.role === "user");
+ const hasAssistant = turns.some((t) => t.role === "assistant");
+ return hasUser && hasAssistant ? turns : null;
+}
+
+export type FineTuneExportResult = {
+ lines: string[];
+ conversations: number;
+ skipped: number;
+};
+
+/** Dataset shapes the Train tab detects without column mapping. */
+export type FineTuneFormat = "openai" | "sharegpt" | "alpaca";
+
+const SHAREGPT_FROM: Record = {
+ system: "system",
+ user: "human",
+ assistant: "gpt",
+};
+
+/** JSONL lines for one conversation in the chosen format. Alpaca is
+ * single-turn, so each user to assistant pair becomes its own record with
+ * the system prompt and earlier exchange carried in the input field. */
+function turnsToFineTuneLines(
+ turns: FineTuneMessage[],
+ format: FineTuneFormat,
+): string[] {
+ if (format === "sharegpt") {
+ return [
+ JSON.stringify({
+ conversations: turns.map((t) => ({
+ from: SHAREGPT_FROM[t.role],
+ value: t.content,
+ })),
+ }),
+ ];
+ }
+ if (format === "alpaca") {
+ const lines: string[] = [];
+ const context: string[] = [];
+ let system = "";
+ let pendingUser: string | null = null;
+ for (const t of turns) {
+ if (t.role === "system") {
+ system = system ? `${system}\n\n${t.content}` : t.content;
+ continue;
+ }
+ if (t.role === "user") {
+ pendingUser = t.content;
+ continue;
+ }
+ if (pendingUser === null) continue;
+ const inputParts = [];
+ if (system) inputParts.push(system);
+ if (context.length > 0) inputParts.push(context.join("\n"));
+ lines.push(
+ JSON.stringify({
+ instruction: pendingUser,
+ input: inputParts.join("\n\n"),
+ output: t.content,
+ }),
+ );
+ context.push(`User: ${pendingUser}`, `Assistant: ${t.content}`);
+ pendingUser = null;
+ }
+ return lines;
+ }
+ return [JSON.stringify({ messages: turns })];
+}
+
+/** Every non-archived chat (Recents and Projects) as training-ready JSONL. */
+export async function buildFineTuneJsonl(
+ format: FineTuneFormat = "openai",
+): Promise {
+ const threads = await listStoredChatThreads({ includeArchived: false });
+ const ids = [...new Set(threads.map((t) => t.id))];
+ const lines: string[] = [];
+ let conversations = 0;
+ let skipped = 0;
+ for (const id of ids) {
+ const raw = await listStoredChatMessages(id);
+ const hasParentIds = raw.some(
+ (m) => (m as { parentId?: unknown }).parentId != null,
+ );
+ // Chain only: retries/regenerations leave sibling branches, and mixing
+ // alternate replies into one conversation corrupts the training targets.
+ const ordered = hasParentIds
+ ? (orderByParentChain(raw, { includeSiblings: false }) as typeof raw)
+ : raw;
+ const turns = messagesToFineTuneTurns(ordered);
+ const converted = turns ? turnsToFineTuneLines(turns, format) : [];
+ if (converted.length === 0) {
+ skipped += 1;
+ continue;
+ }
+ conversations += 1;
+ lines.push(...converted);
+ }
+ return { lines, conversations, skipped };
+}
+
+/** Download the fine-tuning JSONL; returns the conversation count. */
+export async function exportFineTuneJsonl(
+ format: FineTuneFormat = "openai",
+): Promise {
+ const { lines, conversations, skipped } = await buildFineTuneJsonl(format);
+ if (conversations === 0) {
+ toast.info("No chats with a user and assistant exchange to export.");
+ return 0;
+ }
+ const suffix = format === "openai" ? "" : `-${format}`;
+ downloadBlob(
+ lines.join("\n"),
+ `chat-finetune${suffix}-${exportTs()}.jsonl`,
+ "application/x-ndjson",
+ );
+ if (skipped > 0) {
+ toast.success(
+ `Exported ${conversations} conversation${conversations === 1 ? "" : "s"} (${skipped} without a full exchange skipped).`,
+ );
+ }
+ return conversations;
+}
+
// role:"tool" results are absorbed into the preceding assistant tool-call
// part's `result` field rather than becoming separate records.
function oaiMessagesToRecords(
diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsx
index 019d0bfd8a..4a8740ac9b 100644
--- a/studio/frontend/src/features/chat/runtime-provider.tsx
+++ b/studio/frontend/src/features/chat/runtime-provider.tsx
@@ -56,6 +56,11 @@ import { AudioAttachmentAdapter } from "./audio-attachment-adapter";
import { useChatRuntimeStore } from "./stores/chat-runtime-store";
import { ToolPaneScopeContext, toolPaneScope } from "./tool-output-scope";
import type { MessageRecord, ModelType, ThreadRecord } from "./types";
+import {
+ chatContentPartAttachmentIdFromSignature,
+ chatContentPartAttachmentSignature,
+ onChatAttachmentDeleted,
+} from "./utils/chat-attachment-events";
import {
deleteStoredChatThreads,
ensureStoredChatThread,
@@ -890,6 +895,168 @@ function useStudioRuntimeAdapters(
): StudioRuntimeAdapters {
const aui = useAui();
+ // Mirror Data-tab attachment deletions into the loaded thread. The in-memory
+ // repository otherwise keeps the attachment, and a later repo-to-storage sync
+ // (e.g. deleting a message in the thread) would write it back.
+ useEffect(() => {
+ let active = true;
+ let pendingDeletion = Promise.resolve();
+ const unsubscribe = onChatAttachmentDeleted((event) => {
+ pendingDeletion = pendingDeletion.then(async () => {
+ if (!active) return;
+ const { messageId, attachmentId } = event;
+ try {
+ const thread = aui.thread();
+ if (attachmentId.startsWith("content-part-sha256-")) {
+ for (let attempt = 0; attempt < 3 && active; attempt += 1) {
+ const exported = thread.export();
+ const target = exported.messages.find(
+ (item) => item.message.id === messageId,
+ );
+ if (!target || !Array.isArray(target.message.content)) return;
+ const content = target.message.content;
+
+ const signatures = content.map((part) =>
+ chatContentPartAttachmentSignature(part),
+ );
+ const ids = await Promise.all(
+ signatures.map((signature) =>
+ signature === null
+ ? null
+ : chatContentPartAttachmentIdFromSignature(signature),
+ ),
+ );
+ const targetAttachments = (
+ target.message as {
+ attachments?: readonly { id: string }[];
+ }
+ ).attachments;
+ const hasTargetAttachment =
+ Array.isArray(targetAttachments) &&
+ targetAttachments.some(
+ (attachment) => attachment.id === attachmentId,
+ );
+ if (
+ (!ids.includes(attachmentId) && !hasTargetAttachment) ||
+ !active
+ ) {
+ return;
+ }
+
+ // Preserve any messages added or streamed while WebCrypto ran.
+ // Retry if the target's managed content itself changed.
+ const latest = thread.export();
+ const latestTarget = latest.messages.find(
+ (item) => item.message.id === messageId,
+ );
+ const latestContent = latestTarget?.message.content;
+ if (!Array.isArray(latestContent)) return;
+ const latestSignatures = latestContent.map((part) =>
+ chatContentPartAttachmentSignature(part),
+ );
+ if (
+ signatures.length !== latestSignatures.length ||
+ signatures.some(
+ (signature, index) => signature !== latestSignatures[index],
+ )
+ ) {
+ continue;
+ }
+
+ const messages = latest.messages.map((item) => {
+ if (item.message.id !== messageId) return item;
+ const attachments = (
+ item.message as {
+ attachments?: readonly { id: string }[];
+ }
+ ).attachments;
+ return {
+ ...item,
+ message: {
+ ...item.message,
+ content: latestContent.filter(
+ (_, index) => ids[index] !== attachmentId,
+ ),
+ ...(Array.isArray(attachments)
+ ? {
+ attachments: attachments.filter(
+ (attachment) =>
+ attachment.id !== attachmentId,
+ ),
+ }
+ : {}),
+ } as typeof item.message,
+ };
+ });
+ if (active) thread.import({ ...latest, messages });
+ return;
+ }
+ return;
+ }
+
+ const exported = thread.export();
+ let changed = false;
+ const messages = exported.messages.map((item) => {
+ if (item.message.id !== messageId) return item;
+ const message = item.message;
+ const attachments = (
+ message as { attachments?: readonly { id: string }[] }
+ ).attachments;
+ if (
+ Array.isArray(attachments) &&
+ attachments.some(
+ (attachment) => attachment.id === attachmentId,
+ )
+ ) {
+ changed = true;
+ return {
+ ...item,
+ message: {
+ ...message,
+ attachments: attachments.filter(
+ (attachment) => attachment.id !== attachmentId,
+ ),
+ } as typeof message,
+ };
+ }
+ if (/^content-part-[0-9]+$/.test(attachmentId)) {
+ // Legacy synthetic id for a blob stored as a message content part.
+ const idx = Number(attachmentId.slice("content-part-".length));
+ const content = message.content;
+ if (
+ !Array.isArray(content) ||
+ !Number.isInteger(idx) ||
+ idx < 0 ||
+ idx >= content.length
+ ) {
+ return item;
+ }
+ const part = content[idx] as { type?: string };
+ if (part?.type !== "image" && part?.type !== "audio") return item;
+ changed = true;
+ return {
+ ...item,
+ message: {
+ ...message,
+ content: content.filter((_, i) => i !== idx),
+ } as typeof message,
+ };
+ }
+ return item;
+ });
+ if (changed && active) thread.import({ ...exported, messages });
+ } catch {
+ // No active thread mounted: storage already holds the truth.
+ }
+ });
+ return pendingDeletion;
+ });
+ return () => {
+ active = false;
+ unsubscribe();
+ };
+ }, [aui]);
+
const history = useMemo(
() => ({
async load() {
diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsx
index 31e50ee60c..461eef99b2 100644
--- a/studio/frontend/src/features/chat/shared-composer.tsx
+++ b/studio/frontend/src/features/chat/shared-composer.tsx
@@ -619,7 +619,6 @@ export function SharedComposer({
const artifactsEnabled = useChatRuntimeStore((s) => s.artifactsEnabled);
const setArtifactsEnabled = useChatRuntimeStore((s) => s.setArtifactsEnabled);
const showCanvasMenuItem = useChatRuntimeStore((s) => s.showCanvasMenuItem);
- const permissionMode = useChatRuntimeStore((s) => s.permissionMode);
const mcpEnabledForChat = useChatRuntimeStore((s) => s.mcpEnabledForChat);
const setMcpEnabledForChat = useChatRuntimeStore(
(s) => s.setMcpEnabledForChat,
@@ -790,15 +789,12 @@ export function SharedComposer({
// can still be pre-selected, matching Web search/Code/MCP.
const ragDisabled = modelLoaded && (isExternalModel || !supportsTools);
const showRagPill = !isExternalModel;
- // Above 4 pills, collapse to icons only to cut clutter. Compare, Search and
- // Code always show; the permission pill shows in every mode except "off"
- // (it renders null there); the rest are conditional.
- const permissionPillVisible = permissionMode !== "off";
+ // Above 4 pills, collapse to icons only. Compare, Search, Code, and
+ // permissions always show; the rest are conditional.
const pillsCompact =
- 3 +
- (permissionPillVisible ? 1 : 0) +
+ 4 +
(showImagePill ? 1 : 0) +
- (showRagPill && ragEnabled && !ragDisabled ? 1 : 0) +
+ (showRagPill && ragEnabled ? 1 : 0) +
(showWebFetchPill ? 1 : 0) +
(artifactsEnabled ? 1 : 0) +
(mcpEnabledForChat ? 1 : 0) >
diff --git a/studio/frontend/src/features/chat/utils/archived-chat-export.ts b/studio/frontend/src/features/chat/utils/archived-chat-export.ts
new file mode 100644
index 0000000000..834dfdcf5f
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/archived-chat-export.ts
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Minimal views over the `unknown[]` export fields we filter on.
+type ExportThreadView = {
+ id?: string;
+ archived?: boolean;
+ projectId?: string | null;
+};
+type ExportMessageView = { threadId?: string };
+type ExportProjectView = { id?: string };
+
+// Full chat-export backup shape, kept structural so the pure filter below
+// stays decoupled from the storage layer that produces it.
+export interface ChatExportData {
+ exportedAt?: string;
+ version?: number;
+ threadCount: number;
+ projects?: unknown[];
+ threads: unknown[];
+ messages: unknown[];
+}
+
+// Restrict a full chat export to archived threads, their messages and the
+// projects those threads belong to. Pure: never mutates the input, and keeps
+// the original thread/message objects so the backup re-imports unchanged.
+export function filterArchivedChatExport(
+ full: T,
+): { data: T; archivedCount: number } {
+ const archivedThreads = (full.threads as ExportThreadView[]).filter(
+ (thread) => thread.archived === true,
+ );
+ const archivedThreadIds = new Set(
+ archivedThreads
+ .map((thread) => thread.id)
+ .filter((id): id is string => typeof id === "string"),
+ );
+ const messages = (full.messages as ExportMessageView[]).filter(
+ (message) =>
+ typeof message.threadId === "string" &&
+ archivedThreadIds.has(message.threadId),
+ );
+ const referencedProjectIds = new Set(
+ archivedThreads
+ .map((thread) => thread.projectId)
+ .filter((id): id is string => typeof id === "string"),
+ );
+ const projects = (full.projects as ExportProjectView[] | undefined)?.filter(
+ (project) =>
+ typeof project.id === "string" && referencedProjectIds.has(project.id),
+ );
+ return {
+ data: {
+ ...full,
+ threadCount: archivedThreads.length,
+ projects: projects ?? [],
+ threads: archivedThreads as unknown[],
+ messages: messages as unknown[],
+ },
+ archivedCount: archivedThreads.length,
+ };
+}
diff --git a/studio/frontend/src/features/chat/utils/chat-attachment-events.ts b/studio/frontend/src/features/chat/utils/chat-attachment-events.ts
new file mode 100644
index 0000000000..dbde157890
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/chat-attachment-events.ts
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+/**
+ * Notifies loaded chat runtimes when the Data tab deletes a stored attachment.
+ * Without this, the active thread's in-memory repository still holds the
+ * attachment, and any later repo-to-storage sync (e.g. deleting a message in
+ * that thread) writes it back, undoing the deletion.
+ */
+
+import forge from "node-forge";
+
+export type ChatAttachmentDeletedEvent = {
+ messageId: string;
+ attachmentId: string;
+};
+
+const CONTENT_PART_ID_PREFIX = "content-part-sha256-";
+const URI_SCHEME_RE = /^[A-Za-z][A-Za-z0-9+.-]*:/;
+
+function isLocallyStoredBlob(value: string): boolean {
+ const candidate = value.trimStart();
+ if (!candidate) return false;
+ if (candidate.slice(0, 5).toLowerCase() === "data:") return true;
+ if (candidate.startsWith("//") || candidate.startsWith("\\\\")) {
+ return false;
+ }
+ return !URI_SCHEME_RE.test(candidate);
+}
+
+function stableJson(value: unknown): string {
+ if (Array.isArray(value)) {
+ return `[${value
+ .map((item) => (item === undefined ? "null" : stableJson(item)))
+ .join(",")}]`;
+ }
+ if (value && typeof value === "object") {
+ const record = value as Record;
+ return `{${Object.keys(record)
+ .filter((key) => record[key] !== undefined)
+ .sort()
+ .map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`)
+ .join(",")}}`;
+ }
+ return JSON.stringify(value) ?? "null";
+}
+
+/** Canonical payload used to detect whether an async hash still describes the
+ * current message content. */
+export function chatContentPartAttachmentSignature(
+ part: unknown,
+): string | null {
+ if (!part || typeof part !== "object") return null;
+ const record = part as Record;
+ let payload: ["image" | "audio", unknown] | null = null;
+ if (
+ typeof record.image === "string" &&
+ record.image.slice(0, 5).toLowerCase() === "data:"
+ ) {
+ payload = ["image", record.image];
+ } else if (
+ typeof record.audio === "string" &&
+ isLocallyStoredBlob(record.audio)
+ ) {
+ payload = ["audio", record.audio];
+ } else if (record.audio && typeof record.audio === "object") {
+ const data = (record.audio as Record).data;
+ if (typeof data === "string" && isLocallyStoredBlob(data)) {
+ payload = ["audio", record.audio];
+ }
+ }
+ if (!payload) return null;
+
+ return stableJson(payload);
+}
+
+/** Mirrors the backend's stable content-part identity without adding private
+ * metadata to the message payload sent to inference. */
+export async function chatContentPartAttachmentIdFromSignature(
+ signature: string,
+): Promise {
+ let hex: string | null = null;
+ const subtle = globalThis.crypto?.subtle;
+ if (subtle) {
+ try {
+ const digest = await subtle.digest(
+ "SHA-256",
+ new TextEncoder().encode(signature),
+ );
+ hex = Array.from(new Uint8Array(digest), (byte) =>
+ byte.toString(16).padStart(2, "0"),
+ ).join("");
+ } catch {
+ // Fall through to the pure-JS implementation below. Some embedded
+ // browsers expose crypto.subtle but reject it outside a secure context.
+ }
+ }
+ if (hex === null) {
+ const digest = forge.md.sha256.create();
+ digest.update(signature, "utf8");
+ hex = digest.digest().toHex();
+ }
+ return `${CONTENT_PART_ID_PREFIX}${hex}`;
+}
+
+type Listener = (event: ChatAttachmentDeletedEvent) => void | Promise;
+
+const listeners = new Set();
+
+export function onChatAttachmentDeleted(listener: Listener): () => void {
+ listeners.add(listener);
+ return () => {
+ listeners.delete(listener);
+ };
+}
+
+export function emitChatAttachmentDeleted(
+ event: ChatAttachmentDeletedEvent,
+): void {
+ for (const listener of [...listeners]) {
+ void listener(event);
+ }
+}
diff --git a/studio/frontend/src/features/chat/utils/download-json.ts b/studio/frontend/src/features/chat/utils/download-json.ts
new file mode 100644
index 0000000000..0c5ce00ff9
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/download-json.ts
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Save `data` as a pretty-printed JSON file via a temporary object URL. Uses
+// only the standard Blob/anchor download path so it works in every browser.
+export function triggerJsonDownload(data: unknown, filename: string): void {
+ const blob = new Blob([JSON.stringify(data, null, 2)], {
+ type: "application/json",
+ });
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ a.href = url;
+ a.download = filename;
+ document.body.appendChild(a);
+ a.click();
+ a.remove();
+ URL.revokeObjectURL(url);
+}
diff --git a/studio/frontend/src/features/chat/utils/export-chat-history.ts b/studio/frontend/src/features/chat/utils/export-chat-history.ts
index 5faf4dc08a..b4bc64d053 100644
--- a/studio/frontend/src/features/chat/utils/export-chat-history.ts
+++ b/studio/frontend/src/features/chat/utils/export-chat-history.ts
@@ -1,21 +1,34 @@
// 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 { filterArchivedChatExport } from "./archived-chat-export";
import { buildStoredChatExport } from "./chat-history-storage";
+import { triggerJsonDownload } from "./download-json";
export const buildChatExport = buildStoredChatExport;
+function dateStamp(): string {
+ // Date only (no colons) so the filename is valid on every OS.
+ return new Date().toISOString().slice(0, 10);
+}
+
export async function downloadChatExport(): Promise {
const data = await buildChatExport();
- const blob = new Blob([JSON.stringify(data, null, 2)], {
- type: "application/json",
- });
- const url = URL.createObjectURL(blob);
- const a = document.createElement("a");
- a.href = url;
- a.download = `unsloth-chats-${new Date().toISOString().slice(0, 10)}.json`;
- document.body.appendChild(a);
- a.click();
- a.remove();
- URL.revokeObjectURL(url);
+ triggerJsonDownload(data, `unsloth-chats-${dateStamp()}.json`);
+}
+
+// Full backup restricted to archived chats. Returns the archived thread count.
+export async function buildArchivedChatExport() {
+ return filterArchivedChatExport(await buildChatExport());
+}
+
+// Download only the archived chats. Returns how many were exported; skips the
+// download entirely when there are none.
+export async function downloadArchivedChatExport(): Promise {
+ const { data, archivedCount } = await buildArchivedChatExport();
+ if (archivedCount === 0) {
+ return 0;
+ }
+ triggerJsonDownload(data, `unsloth-archived-chats-${dateStamp()}.json`);
+ return archivedCount;
}
diff --git a/studio/frontend/src/features/export/export-page.tsx b/studio/frontend/src/features/export/export-page.tsx
index 4cd7958fb8..51e9f209dd 100644
--- a/studio/frontend/src/features/export/export-page.tsx
+++ b/studio/frontend/src/features/export/export-page.tsx
@@ -44,6 +44,7 @@ import {
import { usePlatformStore } from "@/config/env";
import { useHubModelSearch } from "@/features/hub/hooks/use-hub-model-search";
import { confirmRemoteCodeIfNeeded } from "@/features/security";
+import { prepareHfTokenForUse } from "@/features/hf-auth";
import { GuidedTour, useGuidedTourController } from "@/features/tour";
import {
type LocalModelInfo,
@@ -724,11 +725,17 @@ export function ExportPage() {
const checkpointPath = selectedCp?.path ?? null;
const pushToHub = destination === "hub";
+ const preparedToken = await prepareHfTokenForUse(hfToken, {
+ allowAnonymous: !pushToHub,
+ });
+ if (!preparedToken.proceed) return;
+ const actionHfToken = preparedToken.token ?? "";
+
const repoId =
pushToHub && hfUsername && modelName
? `${hfUsername}/${modelName}`
: undefined;
- const token = pushToHub && hfToken ? hfToken : undefined;
+ const token = pushToHub && actionHfToken ? actionHfToken : undefined;
// The GGUF method with the LoRA target reuses the LoRA-adapter export path.
const effectiveMethod: ExportMethod = ggufAsLora ? "lora" : exportMethod;
const emitLoraGguf =
@@ -747,7 +754,7 @@ export function ExportPage() {
if (sourceMode !== "checkpoint") {
const remoteCodeOk = await confirmRemoteCodeIfNeeded({
modelName: source,
- hfToken: hfToken || null,
+ hfToken: actionHfToken || null,
// An HF source can need trust_remote_code via its YAML default with no
// auto_map to review; signal it so a YAML-only model does not export
// with it false.
@@ -767,7 +774,7 @@ export function ExportPage() {
modelSource,
trustRemoteCode,
approvedRemoteCodeFingerprint,
- loadToken: hfToken || null,
+ loadToken: actionHfToken || null,
exportMethod: effectiveMethod,
isAdapter: adapterExport,
quantLevels,
diff --git a/studio/frontend/src/features/hf-auth/api.ts b/studio/frontend/src/features/hf-auth/api.ts
new file mode 100644
index 0000000000..ab4b049566
--- /dev/null
+++ b/studio/frontend/src/features/hf-auth/api.ts
@@ -0,0 +1,44 @@
+// 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 { authFetch } from "@/features/auth";
+// This header helper is API-layer-only and is not part of the feature's
+// React-facing public barrel.
+// eslint-disable-next-line no-restricted-imports
+import { hubTokenHeader } from "@/features/hub/lib/hub-token-header";
+
+export type HfTokenValidationStatus =
+ | "missing"
+ | "valid"
+ | "invalid"
+ | "rate_limited"
+ | "unavailable";
+
+export interface HfTokenValidationResult {
+ status: HfTokenValidationStatus;
+ retryAfterSeconds: number | null;
+}
+
+export async function validateHfToken(
+ token: string | null | undefined,
+): Promise {
+ const normalized = token?.trim() ?? "";
+ if (!normalized) {
+ return { status: "missing", retryAfterSeconds: null };
+ }
+ const response = await authFetch("/api/hub/token/validate", {
+ method: "POST",
+ headers: hubTokenHeader(normalized),
+ });
+ if (!response.ok) {
+ return { status: "unavailable", retryAfterSeconds: null };
+ }
+ const body = (await response.json()) as {
+ status?: HfTokenValidationStatus;
+ retry_after_seconds?: number | null;
+ };
+ return {
+ status: body.status ?? "unavailable",
+ retryAfterSeconds: body.retry_after_seconds ?? null,
+ };
+}
diff --git a/studio/frontend/src/features/hf-auth/confirm-token.ts b/studio/frontend/src/features/hf-auth/confirm-token.ts
new file mode 100644
index 0000000000..e1f6e6c705
--- /dev/null
+++ b/studio/frontend/src/features/hf-auth/confirm-token.ts
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// These stores are used outside React and are not part of their features'
+// React-facing public barrels.
+// eslint-disable-next-line no-restricted-imports
+import { useHfTokenStore } from "@/features/hub/stores/hf-token-store";
+// eslint-disable-next-line no-restricted-imports
+import { useSettingsDialogStore } from "@/features/settings/stores/settings-dialog-store";
+import { validateHfToken } from "./api";
+import { useHfTokenWarningStore } from "./store";
+
+export interface PreparedHfToken {
+ proceed: boolean;
+ token: string | null;
+}
+
+interface PrepareHfTokenOptions {
+ allowAnonymous?: boolean;
+}
+
+// A caller can retain the pre-dialog payload while the shared store is cleared.
+// Remember that one-session choice so a follow-up /load does not prompt again
+// after its preceding /validate already continued anonymously.
+const anonymousForSession = new Set();
+
+export async function prepareHfTokenForUse(
+ token: string | null | undefined,
+ options: PrepareHfTokenOptions = {},
+): Promise {
+ const normalized = token?.trim() ?? "";
+ if (!normalized) return { proceed: true, token: null };
+ const allowAnonymous = options.allowAnonymous ?? true;
+ if (allowAnonymous && anonymousForSession.has(normalized)) {
+ return { proceed: true, token: null };
+ }
+
+ let validation;
+ try {
+ validation = await validateHfToken(normalized);
+ } catch {
+ // Validation is advisory. Let the real operation retain its own error.
+ return { proceed: true, token: normalized };
+ }
+ if (validation.status !== "invalid") {
+ // A connectivity failure or rate limit cannot prove that a token is bad.
+ // Let the real operation proceed and retain its repository-specific error.
+ return { proceed: true, token: normalized };
+ }
+
+ const decision = await useHfTokenWarningStore
+ .getState()
+ .requestDecision(allowAnonymous);
+ if (decision === "anonymous") {
+ anonymousForSession.add(normalized);
+ useHfTokenStore.getState().clearToken();
+ return { proceed: true, token: null };
+ }
+ if (decision === "replace") {
+ useSettingsDialogStore.getState().openDialog("general");
+ }
+ return { proceed: false, token: normalized };
+}
diff --git a/studio/frontend/src/features/hf-auth/hf-token-warning-dialog.tsx b/studio/frontend/src/features/hf-auth/hf-token-warning-dialog.tsx
new file mode 100644
index 0000000000..4370c39693
--- /dev/null
+++ b/studio/frontend/src/features/hf-auth/hf-token-warning-dialog.tsx
@@ -0,0 +1,66 @@
+// 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 {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+} from "@/components/ui/alert-dialog";
+import { Button } from "@/components/ui/button";
+import { AlertTriangle } from "lucide-react";
+import { useHfTokenWarningStore } from "./store";
+
+export function HfTokenWarningDialog() {
+ const open = useHfTokenWarningStore((state) => state.open);
+ const allowAnonymous = useHfTokenWarningStore(
+ (state) => state.allowAnonymous,
+ );
+ const resolve = useHfTokenWarningStore((state) => state.resolve);
+
+ return (
+ {
+ if (!next) resolve("cancel");
+ }}
+ >
+
+
+
+
+
+
Hugging Face token is invalid
+
+ {allowAnonymous
+ ? "Hugging Face rejected the saved token. Replace it to access private or gated repositories, or continue without it for public and fully downloaded models."
+ : "Hugging Face rejected the saved token. Replace it before uploading to the Hub."}
+
+
+
+
+
+ resolve("cancel")}>
+ Cancel
+
+
+ {allowAnonymous ? (
+
resolve("anonymous")}>
+ Continue without token
+
+ ) : null}
+
resolve("replace")}>
+ Replace token
+
+
+
+
+
+ );
+}
diff --git a/studio/frontend/src/features/hf-auth/index.ts b/studio/frontend/src/features/hf-auth/index.ts
new file mode 100644
index 0000000000..e8bcb48193
--- /dev/null
+++ b/studio/frontend/src/features/hf-auth/index.ts
@@ -0,0 +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
+
+export { validateHfToken } from "./api";
+export type {
+ HfTokenValidationResult,
+ HfTokenValidationStatus,
+} from "./api";
+export { prepareHfTokenForUse } from "./confirm-token";
+export { HfTokenWarningDialog } from "./hf-token-warning-dialog";
diff --git a/studio/frontend/src/features/hf-auth/store.ts b/studio/frontend/src/features/hf-auth/store.ts
new file mode 100644
index 0000000000..faa2543a8a
--- /dev/null
+++ b/studio/frontend/src/features/hf-auth/store.ts
@@ -0,0 +1,33 @@
+// 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 { create } from "zustand";
+
+export type HfTokenWarningDecision = "anonymous" | "replace" | "cancel";
+type Resolver = (decision: HfTokenWarningDecision) => void;
+
+let pendingResolver: Resolver | null = null;
+
+interface HfTokenWarningStore {
+ open: boolean;
+ allowAnonymous: boolean;
+ requestDecision: (allowAnonymous: boolean) => Promise;
+ resolve: (decision: HfTokenWarningDecision) => void;
+}
+
+export const useHfTokenWarningStore = create((set) => ({
+ open: false,
+ allowAnonymous: true,
+ requestDecision: (allowAnonymous) =>
+ new Promise((resolve) => {
+ pendingResolver?.("cancel");
+ pendingResolver = resolve;
+ set({ open: true, allowAnonymous });
+ }),
+ resolve: (decision) => {
+ const resolver = pendingResolver;
+ pendingResolver = null;
+ set({ open: false, allowAnonymous: true });
+ resolver?.(decision);
+ },
+}));
diff --git a/studio/frontend/src/features/hub/catalog/model-inspector.tsx b/studio/frontend/src/features/hub/catalog/model-inspector.tsx
index 63cb34678b..5a6ae1615a 100644
--- a/studio/frontend/src/features/hub/catalog/model-inspector.tsx
+++ b/studio/frontend/src/features/hub/catalog/model-inspector.tsx
@@ -59,6 +59,13 @@ import { ModelReadme } from "./model-readme";
import { OwnerAvatar } from "./owner-avatar";
import { AccessChip, CapabilityPill } from "./shared";
+// HF pipeline_tag values authoritative for embedding-only repos; capability
+// labels (code/vision/audio) can leak onto them via name or tags.
+const EMBEDDING_PIPELINE_TAGS: ReadonlySet = new Set([
+ "feature-extraction",
+ "sentence-similarity",
+]);
+
function ViewRepositoryButton({
repoId,
isDataset,
@@ -531,11 +538,27 @@ export const ModelInspector = memo(function ModelInspector({
? formatCompact(model.totalParams)
: "N/A";
const unslothSupported = unslothSupport.status !== "unsupported";
+ // Embedding-only non-GGUF repos have no generative head, so keep them out of
+ // the Run gate. Prefer the pipeline tag, else the capability heuristic.
+ const isEmbeddingOnly =
+ !model.isGguf &&
+ model.capabilities.some((c) => c.key === "embedding") &&
+ (EMBEDDING_PIPELINE_TAGS.has(model.pipelineTag?.toLowerCase() ?? "") ||
+ !model.capabilities.some(
+ (c) =>
+ c.key === "conversational" ||
+ c.key === "tools" ||
+ c.key === "reasoning" ||
+ c.key === "code" ||
+ c.key === "vision" ||
+ c.key === "audio",
+ ));
// Chat-only hosts (no supported GPU / usable MLX) run inference only through
// llama.cpp, so only GGUF is loadable.
const canRunModel =
!isDataset &&
(model.runtimeCapabilities?.canChat ?? true) &&
+ !isEmbeddingOnly &&
(model.isGguf || (!chatOnly && unslothSupported));
const canTrainModel =
!isDataset &&
diff --git a/studio/frontend/src/features/rag/api/rag-api.ts b/studio/frontend/src/features/rag/api/rag-api.ts
index 20800230b5..c0bad1a9d5 100644
--- a/studio/frontend/src/features/rag/api/rag-api.ts
+++ b/studio/frontend/src/features/rag/api/rag-api.ts
@@ -10,6 +10,7 @@ import type {
KnowledgeBase,
PreviewTarget,
RagDocument,
+ UploadedDocument,
} from "../types/rag";
const RAG_BASE = "/api/rag";
@@ -194,10 +195,25 @@ export function invalidateProjectSources(projectId: string): void {
projectSourcesCache.delete(projectId);
}
-export function deleteDocument(documentId: string): Promise<{ ok: boolean }> {
- return ragRequest(`/documents/${encodeURIComponent(documentId)}`, {
- method: "DELETE",
- });
+export async function listAllDocuments(): Promise {
+ const data = await ragRequest<{ documents: UploadedDocument[] }>(
+ "/documents",
+ );
+ return data.documents ?? [];
+}
+
+export async function deleteDocument(
+ documentId: string,
+ projectId?: string | null,
+): Promise<{ ok: boolean }> {
+ const result = await ragRequest<{ ok: boolean }>(
+ `/documents/${encodeURIComponent(documentId)}`,
+ {
+ method: "DELETE",
+ },
+ );
+ if (projectId) invalidateProjectSources(projectId);
+ return result;
}
export function getJob(jobId: string): Promise {
@@ -237,7 +253,8 @@ export async function* streamJobEvents(
const dataLines: string[] = [];
for (const line of rawEvent.split(/\r?\n/)) {
- if (line.startsWith("data:")) dataLines.push(line.slice(5).trimStart());
+ if (line.startsWith("data:"))
+ dataLines.push(line.slice(5).trimStart());
}
if (dataLines.length > 0) {
const dataText = dataLines.join("\n");
diff --git a/studio/frontend/src/features/rag/components/use-rag-documents.ts b/studio/frontend/src/features/rag/components/use-rag-documents.ts
index 8e756b2782..8d6433d8c3 100644
--- a/studio/frontend/src/features/rag/components/use-rag-documents.ts
+++ b/studio/frontend/src/features/rag/components/use-rag-documents.ts
@@ -2,12 +2,11 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { useCallback, useEffect, useRef, useState } from "react";
-import { useChatRuntimeStore } from "@/features/chat";
-
import {
CHAT_RAG_CAPTION_KEY,
CHAT_RAG_OCR_KEY,
-} from "@/features/chat/stores/chat-runtime-store";
+ useChatRuntimeStore,
+} from "@/features/chat";
import { toast } from "@/lib/toast";
import {
deleteDocument,
@@ -60,9 +59,7 @@ export function useRagDocuments(
if (ids.size === 0) return false;
const docs = documentsRef.current.filter((d) => ids.has(d.id));
if (docs.length === 0) return false; // sig tracked but doc gone -> allow re-upload
- return docs.some(
- (d) => d.status !== "completed" || (d.numChunks ?? 0) > 0,
- );
+ return docs.some((d) => d.status !== "completed" || (d.numChunks ?? 0) > 0);
}, []);
// True while upload() runs, so the scope-change effect can tell a real switch
// from lazy thread materialization mid-upload (which must not reset).
@@ -80,9 +77,7 @@ export function useRagDocuments(
const patchDoc = useCallback(
(documentId: string, patch: Partial) => {
setDocuments((rows) =>
- rows.map((row) =>
- row.id === documentId ? { ...row, ...patch } : row,
- ),
+ rows.map((row) => (row.id === documentId ? { ...row, ...patch } : row)),
);
},
[],
@@ -176,49 +171,61 @@ export function useRagDocuments(
[patchDoc],
);
- const refresh = useCallback(async (opts?: { quiet?: boolean }) => {
- if (!scope) return;
- if (!opts?.quiet) setLoading(true);
- try {
- // Merge server truth with local progress so a refresh mid-index keeps a
- // live "running %" chip. Failed docs hidden (toast warned at upload).
- const rows = (await lister()).filter((row) => row.status !== "failed");
- setDocuments((prev) => {
- const merged = rows.map((row) => {
- const tracked = prev.find((p) => p.id === row.id);
- return tracked && tracked.progress != null && row.status !== "completed"
- ? { ...row, progress: tracked.progress }
- : row;
+ const refresh = useCallback(
+ async (opts?: { quiet?: boolean }) => {
+ if (!scope) return;
+ if (!opts?.quiet) setLoading(true);
+ try {
+ // Merge server truth with local progress so a refresh mid-index keeps a
+ // live "running %" chip. Failed docs hidden (toast warned at upload).
+ const rows = (await lister()).filter((row) => row.status !== "failed");
+ setDocuments((prev) => {
+ const merged = rows.map((row) => {
+ const tracked = prev.find((p) => p.id === row.id);
+ return tracked &&
+ tracked.progress != null &&
+ row.status !== "completed"
+ ? { ...row, progress: tracked.progress }
+ : row;
+ });
+ // Keep optimistic chips (not yet listed) so a refresh racing an upload
+ // can't make them vanish.
+ const serverIds = new Set(rows.map((row) => row.id));
+ const pendingLocal = prev.filter(
+ (row) => row.id.startsWith("pending_") && !serverIds.has(row.id),
+ );
+ return [...merged, ...pendingLocal];
});
- // Keep optimistic chips (not yet listed) so a refresh racing an upload
- // can't make them vanish.
- const serverIds = new Set(rows.map((row) => row.id));
- const pendingLocal = prev.filter(
- (row) => row.id.startsWith("pending_") && !serverIds.has(row.id),
- );
- return [...merged, ...pendingLocal];
- });
- } catch (err) {
- toast.error("Failed to load documents", {
- description: err instanceof Error ? err.message : String(err),
- });
- } finally {
- if (!opts?.quiet) setLoading(false);
- }
- }, [scope, lister]);
+ } catch (err) {
+ toast.error("Failed to load documents", {
+ description: err instanceof Error ? err.message : String(err),
+ });
+ } finally {
+ if (!opts?.quiet) setLoading(false);
+ }
+ },
+ [scope, lister],
+ );
// A real switch (thread/KB swap) resets + reloads; first acquiring a scope just
// loads. Skip both during materialization mid-upload (scope null -> new thread
// while upload() runs) so we don't abort tracking or wipe optimistic chips.
useEffect(() => {
+ const jobs = trackedJobs.current;
const prev = prevScopeKeyRef.current;
prevScopeKeyRef.current = scopeKey;
if (prev !== null && prev !== scopeKey) {
- for (const controller of trackedJobs.current.values()) controller.abort();
- trackedJobs.current.clear();
+ for (const controller of jobs.values()) controller.abort();
+ jobs.clear();
sigByDocId.current.clear();
+ // Scope changes intentionally clear the old scope before fetching the new
+ // one. Keep this synchronous so React StrictMode's setup/cleanup replay
+ // cannot cancel the only refresh after prevScopeKeyRef has advanced.
setDocuments([]);
- if (scope) void refresh();
+ if (scope) {
+ // eslint-disable-next-line react-hooks/set-state-in-effect
+ void refresh();
+ }
} else if (prev === null && scope && !uploadInFlightRef.current) {
void refresh();
}
@@ -226,8 +233,8 @@ export function useRagDocuments(
// Preserve in-flight tracking when cleanup is the materialization flip,
// not a real switch/unmount.
if (uploadInFlightRef.current) return;
- for (const controller of trackedJobs.current.values()) controller.abort();
- trackedJobs.current.clear();
+ for (const controller of jobs.values()) controller.abort();
+ jobs.clear();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [scopeKey]);
@@ -260,21 +267,41 @@ export function useRagDocuments(
// otherwise backend env defaults own the ingest policy.
const state = useChatRuntimeStore.getState();
const hasLocal = (key: string) =>
- typeof window !== "undefined" && window.localStorage.getItem(key) !== null;
- const ocr = hasLocal(CHAT_RAG_OCR_KEY) ? state.ragOcrScanned : undefined;
+ typeof window !== "undefined" &&
+ window.localStorage.getItem(key) !== null;
+ const ocr = hasLocal(CHAT_RAG_OCR_KEY)
+ ? state.ragOcrScanned
+ : undefined;
const caption = hasLocal(CHAT_RAG_CAPTION_KEY)
? state.ragCaptionFigures
: undefined;
const result =
activeScope.type === "kb"
- ? await uploadKnowledgeBaseDocument(activeScope.kbId, file, ocr, caption)
+ ? await uploadKnowledgeBaseDocument(
+ activeScope.kbId,
+ file,
+ ocr,
+ caption,
+ )
: activeScope.type === "project"
- ? await uploadProjectDocument(activeScope.projectId, file, ocr, caption)
- : await uploadThreadDocument(activeScope.threadId, file, ocr, caption);
+ ? await uploadProjectDocument(
+ activeScope.projectId,
+ file,
+ ocr,
+ caption,
+ )
+ : await uploadThreadDocument(
+ activeScope.threadId,
+ file,
+ ocr,
+ caption,
+ );
sigByDocId.current.set(result.documentId, fileSignature(file));
if (seenIds.has(result.documentId)) {
setDocuments((rows) => rows.filter((row) => row.id !== tempId));
- toast.info(`${result.filename || file.name} is already indexed - skipping`);
+ toast.info(
+ `${result.filename || file.name} is already indexed - skipping`,
+ );
return;
}
seenIds.add(result.documentId);
@@ -341,7 +368,9 @@ export function useRagDocuments(
]);
const resolved =
- overrideScope instanceof Promise ? await overrideScope : overrideScope;
+ overrideScope instanceof Promise
+ ? await overrideScope
+ : overrideScope;
const activeScope = resolved ?? scope;
if (!activeScope) {
// Materialization failed: drop the chips so they don't hang "pending".
@@ -377,7 +406,10 @@ export function useRagDocuments(
const prevSig = sigByDocId.current.get(documentId);
sigByDocId.current.delete(documentId);
try {
- await deleteDocument(documentId);
+ await deleteDocument(
+ documentId,
+ scope?.type === "project" ? scope.projectId : undefined,
+ );
} catch (err) {
setDocuments(prev);
if (prevSig !== undefined) sigByDocId.current.set(documentId, prevSig);
@@ -386,7 +418,7 @@ export function useRagDocuments(
});
}
},
- [documents],
+ [documents, scope],
);
return { documents, loading, uploading, refresh, upload, remove };
diff --git a/studio/frontend/src/features/rag/index.ts b/studio/frontend/src/features/rag/index.ts
index 9e35e345ee..b06c7e09cc 100644
--- a/studio/frontend/src/features/rag/index.ts
+++ b/studio/frontend/src/features/rag/index.ts
@@ -5,4 +5,9 @@ export { KnowledgeBaseComposerButton } from "./components/knowledge-base-compose
export { KnowledgeBaseDialog } from "./components/knowledge-base-dialog";
export { RetrievalSettingsSection } from "./components/retrieval-settings-section";
export { ThreadDocumentsBar } from "./components/thread-documents-bar";
-export type { KnowledgeBase, RagDocument } from "./types/rag";
+export {
+ deleteDocument,
+ getDocumentFileUrl,
+ listAllDocuments,
+} from "./api/rag-api";
+export type { KnowledgeBase, RagDocument, UploadedDocument } from "./types/rag";
diff --git a/studio/frontend/src/features/rag/types/rag.ts b/studio/frontend/src/features/rag/types/rag.ts
index 1277500ae6..9922c740af 100644
--- a/studio/frontend/src/features/rag/types/rag.ts
+++ b/studio/frontend/src/features/rag/types/rag.ts
@@ -24,6 +24,13 @@ export interface RagDocument {
createdAt?: string | null;
}
+/** RagDocument enriched for the global uploaded-files list (settings Data tab). */
+export interface UploadedDocument extends RagDocument {
+ sizeBytes?: number | null;
+ kbName?: string | null;
+ projectName?: string | null;
+}
+
export interface DocumentUploadResult {
documentId: string;
jobId: string;
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/archived-chats-dialog.tsx b/studio/frontend/src/features/settings/components/archived-chats-dialog.tsx
index 836b4d25d4..c4932ccc5b 100644
--- a/studio/frontend/src/features/settings/components/archived-chats-dialog.tsx
+++ b/studio/frontend/src/features/settings/components/archived-chats-dialog.tsx
@@ -12,18 +12,12 @@ import {
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import {
- Dialog,
- DialogContent,
- DialogHeader,
- DialogTitle,
-} from "@/components/ui/dialog";
-import {
+ type SidebarItem,
deleteChatItem,
unarchiveChatItem,
useChatPreferencesStore,
useChatRuntimeStore,
useChatSidebarItems,
- type SidebarItem,
} from "@/features/chat";
import { toast } from "@/lib/toast";
import { ArchiveRestoreIcon, Delete02Icon } from "@hugeicons/core-free-icons";
@@ -40,13 +34,7 @@ function formatCreatedAt(ms: number): string {
});
}
-export function ArchivedChatsDialog({
- open,
- onOpenChange,
-}: {
- open: boolean;
- onOpenChange: (open: boolean) => void;
-}) {
+export function ArchivedChatsView() {
const { archivedItems } = useChatSidebarItems({ requireMessages: false });
const navigate = useNavigate();
const closeSettings = useSettingsDialogStore((s) => s.closeDialog);
@@ -74,7 +62,6 @@ export function ArchivedChatsDialog({
search:
item.type === "single" ? { thread: item.id } : { compare: item.id },
});
- onOpenChange(false);
closeSettings();
}
@@ -114,72 +101,66 @@ export function ArchivedChatsDialog({
}
return (
-
-
-
- Archived chats
-
-
- {archivedItems.length === 0 ? (
-
- No archived chats.
-
- ) : (
-
-
- Name
- Date created
-
-
- {archivedItems.map((item) => (
-
+ {archivedItems.length === 0 ? (
+
+ No archived chats.
+
+ ) : (
+
+
+ Name
+ Date created
+
+
+ {archivedItems.map((item) => (
+
+ openChat(item)}
+ className="min-w-0 flex-1 truncate text-left text-primary hover:underline"
+ title={item.title}
>
+ {item.title}
+
+
+ {formatCreatedAt(item.createdAt)}
+
+
openChat(item)}
- className="min-w-0 flex-1 truncate text-left text-primary hover:underline"
- title={item.title}
+ onClick={() => void handleUnarchive(item)}
+ aria-label="Unarchive chat"
+ title="Unarchive"
+ className="inline-flex size-7 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
>
- {item.title}
+
-
- {formatCreatedAt(item.createdAt)}
-
-
- void handleUnarchive(item)}
- aria-label="Unarchive chat"
- title="Unarchive"
- className="inline-flex size-7 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
- >
-
-
- requestDelete(item)}
- aria-label="Delete chat"
- title="Delete"
- className="inline-flex size-7 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-destructive/10 hover:text-destructive"
- >
-
-
-
-
- ))}
-
- )}
-
+
requestDelete(item)}
+ aria-label="Delete chat"
+ title="Delete"
+ className="inline-flex size-7 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-destructive/10 hover:text-destructive"
+ >
+
+
+
+
+ ))}
+
+ )}
-
+
);
}
diff --git a/studio/frontend/src/features/settings/components/finetune-recipe.ts b/studio/frontend/src/features/settings/components/finetune-recipe.ts
new file mode 100644
index 0000000000..c98f422a35
--- /dev/null
+++ b/studio/frontend/src/features/settings/components/finetune-recipe.ts
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Settings Data tab glue: turn chat history into a fine-tuning JSONL, stage
+// it as a Data Recipe seed upload, and open a new recipe on that file.
+
+import { type FineTuneFormat, buildFineTuneJsonl } from "@/features/chat";
+import { saveRecipe } from "@/features/data-recipes/data/recipes-db";
+import { createEmptyRecipePayload } from "@/features/recipe-studio";
+import { inspectSeedUpload } from "@/features/recipe-studio/api";
+import { uploadTrainingDataset } from "@/features/training/api/datasets-api";
+import { useTrainingConfigStore } from "@/features/training/stores/training-config-store";
+import { toast } from "@/lib/toast";
+
+/** btoa cannot handle code points above latin-1, so encode UTF-8 bytes. */
+function base64FromString(value: string): string {
+ const bytes = new TextEncoder().encode(value);
+ let binary = "";
+ const CHUNK = 0x8000;
+ for (let i = 0; i < bytes.length; i += CHUNK) {
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
+ }
+ return btoa(binary);
+}
+
+/** Builds the JSONL, uploads it as a local recipe seed, and saves a new
+ * recipe whose seed block points at the file. Returns the recipe id, or
+ * null when there is nothing to export. */
+export async function createFineTuneRecipeFromChats(
+ format: FineTuneFormat = "openai",
+): Promise {
+ const { lines, conversations } = await buildFineTuneJsonl(format);
+ if (conversations === 0) {
+ toast.info("No chats with a user and assistant exchange to export.");
+ return null;
+ }
+
+ const dateLabel = new Date().toISOString().slice(0, 10);
+ const suffix = format === "openai" ? "" : `-${format}`;
+ const filename = `chat-finetune${suffix}-${dateLabel}.jsonl`;
+ const inspected = await inspectSeedUpload({
+ filename,
+ // biome-ignore lint/style/useNamingConvention: api schema
+ content_base64: base64FromString(lines.join("\n")),
+ // biome-ignore lint/style/useNamingConvention: api schema
+ preview_size: 10,
+ });
+
+ const payload = createEmptyRecipePayload();
+ payload.recipe.seed_config = {
+ source: {
+ // biome-ignore lint/style/useNamingConvention: api schema
+ seed_type: "local",
+ path: inspected.resolved_path,
+ },
+ // biome-ignore lint/style/useNamingConvention: api schema
+ sampling_strategy: "ordered",
+ // biome-ignore lint/style/useNamingConvention: api schema
+ selection_strategy: null,
+ };
+ payload.ui.nodes = [{ id: "seed", x: 0, y: 0, width: 400 }];
+ payload.ui.seed_source_type = "local";
+ payload.ui.seed_columns = inspected.columns;
+ payload.ui.seed_preview_rows = inspected.preview_rows ?? [];
+ payload.ui.local_file_name = filename;
+
+ const record = await saveRecipe({
+ name: `Chat fine-tuning ${dateLabel}`,
+ payload,
+ });
+ return record.id;
+}
+
+/** Builds the JSONL, uploads it as a training dataset, and selects it in the
+ * Train tab's config store so the Train page opens with it loaded. Returns
+ * false when there is nothing to export. */
+export async function loadFineTuneDatasetInTrainTab(
+ format: FineTuneFormat = "openai",
+): Promise {
+ const { lines, conversations } = await buildFineTuneJsonl(format);
+ if (conversations === 0) {
+ toast.info("No chats with a user and assistant exchange to export.");
+ return false;
+ }
+
+ const dateLabel = new Date().toISOString().slice(0, 10);
+ const suffix = format === "openai" ? "" : `-${format}`;
+ const file = new File(
+ [lines.join("\n")],
+ `chat-finetune${suffix}-${dateLabel}.jsonl`,
+ { type: "application/x-ndjson" },
+ );
+ const uploaded = await uploadTrainingDataset(file);
+ // Selecting also kicks off the dataset format check, so the Train tab
+ // shows the detected format as soon as it mounts.
+ useTrainingConfigStore.getState().selectLocalDataset(uploaded.stored_path);
+ return true;
+}
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/features/settings/components/uploaded-files-dialog.tsx b/studio/frontend/src/features/settings/components/uploaded-files-dialog.tsx
new file mode 100644
index 0000000000..3368f07ff2
--- /dev/null
+++ b/studio/frontend/src/features/settings/components/uploaded-files-dialog.tsx
@@ -0,0 +1,644 @@
+// 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 {
+ AlertDialog,
+ AlertDialogAction,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
+} from "@/components/ui/alert-dialog";
+import { Spinner } from "@/components/ui/spinner";
+import {
+ type ChatAttachmentRecord,
+ deleteChatAttachment,
+ emitChatAttachmentDeleted,
+ fetchChatAttachmentBlob,
+ listChatAttachments,
+} from "@/features/chat";
+import {
+ deleteDocument,
+ getDocumentFileUrl,
+ listAllDocuments,
+ type UploadedDocument,
+} from "@/features/rag";
+import { toast } from "@/lib/toast";
+import {
+ ArrowUpRight01Icon,
+ Delete02Icon,
+ File02Icon,
+} from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
+import { useNavigate } from "@tanstack/react-router";
+import { type ReactNode, useEffect, useRef, useState } from "react";
+import { useSettingsDialogStore } from "../stores/settings-dialog-store";
+
+function formatUploadedAt(value: string | number | null | undefined): string {
+ if (value === null || value === undefined || value === "") return "-";
+ // Chat attachments carry ms epoch numbers; RAG documents carry SQLite
+ // ISO-ish strings (no timezone). Unparseable strings fall through raw.
+ const parsed = new Date(value);
+ if (Number.isNaN(parsed.getTime())) return String(value);
+ return parsed.toLocaleDateString(undefined, {
+ year: "numeric",
+ month: "long",
+ day: "numeric",
+ });
+}
+
+function formatSize(bytes: number | null | undefined): string {
+ if (bytes === null || bytes === undefined) return "-";
+ if (bytes < 1024) return `${bytes} B`;
+ const units = ["KB", "MB", "GB"];
+ let value = bytes;
+ let unit = "B";
+ for (const next of units) {
+ if (value < 1024) break;
+ value /= 1024;
+ unit = next;
+ }
+ return `${value >= 10 ? Math.round(value) : value.toFixed(1)} ${unit}`;
+}
+
+function ragLocationLabel(doc: UploadedDocument): string {
+ if (doc.kbId) return doc.kbName ? `KB · ${doc.kbName}` : "Knowledge base";
+ if (doc.projectId) {
+ return doc.projectName ? `Project · ${doc.projectName}` : "Project";
+ }
+ if (doc.threadId) return "Chat files (RAG)";
+ return "-";
+}
+
+/** Short uppercase file-type label from the filename extension, falling back
+ * to the content-type subtype (e.g. "image/webp" gives WEBP). */
+function fileTypeLabel(
+ name: string,
+ contentType?: string | null,
+): string | null {
+ const dot = name.lastIndexOf(".");
+ const ext = dot > 0 ? name.slice(dot + 1).trim() : "";
+ if (ext && ext.length <= 5) return ext.toUpperCase();
+ const subtype = contentType?.split("/")[1]?.split("+")[0]?.trim();
+ return subtype && subtype.length <= 10 ? subtype.toUpperCase() : null;
+}
+
+/** Lazy image thumbnail for a chat attachment; a file icon until it loads.
+ * The stored blob only downloads once the row scrolls into view, so a long
+ * history of screenshots does not fetch every image on open. */
+function ChatImageThumb({
+ messageId,
+ attachmentId,
+}: {
+ messageId: string;
+ attachmentId: string;
+}) {
+ const [src, setSrc] = useState(null);
+ const [visible, setVisible] = useState(false);
+ const holderRef = useRef(null);
+
+ useEffect(() => {
+ const el = holderRef.current;
+ if (!el) return;
+ if (typeof IntersectionObserver === "undefined") {
+ return;
+ }
+ const observer = new IntersectionObserver((entries) => {
+ if (entries.some((entry) => entry.isIntersecting)) {
+ setVisible(true);
+ observer.disconnect();
+ }
+ });
+ observer.observe(el);
+ return () => observer.disconnect();
+ }, []);
+
+ useEffect(() => {
+ if (!visible) return;
+ let cancelled = false;
+ let url: string | null = null;
+ fetchChatAttachmentBlob(messageId, attachmentId)
+ .then((blob) => {
+ if (cancelled) return;
+ url = URL.createObjectURL(blob);
+ setSrc(url);
+ })
+ .catch(() => {
+ // Keep the file icon on failure.
+ });
+ return () => {
+ cancelled = true;
+ if (url) URL.revokeObjectURL(url);
+ };
+ }, [visible, messageId, attachmentId]);
+
+ if (!src) {
+ return (
+
+
+
+ );
+ }
+ return ;
+}
+
+function FileIconThumb() {
+ return (
+
+ );
+}
+
+/** One display row: a RAG document or a chat message attachment. */
+interface UploadedFileRow {
+ key: string;
+ source: "rag" | "chat";
+ name: string;
+ location: string;
+ sizeBytes?: number | null;
+ createdAt?: string | number | null;
+ failed?: boolean;
+ /** Epoch ms for sorting; rows with unknown dates sort last. */
+ sortTime: number;
+ typeLabel: string | null;
+ /** Image rows render a thumbnail; others show a file icon. */
+ thumb: ReactNode;
+ /** Chat rows link back to their thread. */
+ threadId?: string | null;
+ /** Compare-chat rows navigate by pair id instead of opening one pane alone. */
+ pairId?: string | null;
+ open: () => Promise;
+ remove: () => Promise;
+ deleteDescription: string;
+}
+
+function toSortTime(value: string | number | null | undefined): number {
+ if (value === null || value === undefined || value === "") return 0;
+ const parsed = new Date(value).getTime();
+ return Number.isNaN(parsed) ? 0 : parsed;
+}
+
+// Safari and Firefox block window.open after an await (the user gesture is
+// gone), so open a blank tab synchronously and point it at the URL once
+// resolved. A blocked synchronous open is surfaced instead of silently losing
+// the file after the asynchronous URL lookup.
+async function openResolvedUrl(resolve: () => Promise): Promise {
+ const win = window.open("", "_blank");
+ if (!win) {
+ throw new Error(
+ "Your browser blocked the new tab. Allow popups and retry.",
+ );
+ }
+ win.opener = null;
+ let url: string;
+ try {
+ url = await resolve();
+ } catch (err) {
+ win.close();
+ throw err;
+ }
+ win.location.replace(url);
+}
+
+function ragRow(doc: UploadedDocument): UploadedFileRow {
+ return {
+ key: `rag-${doc.id}`,
+ source: "rag",
+ name: doc.filename,
+ location: ragLocationLabel(doc),
+ sizeBytes: doc.sizeBytes,
+ createdAt: doc.createdAt,
+ failed: doc.status === "failed",
+ sortTime: toSortTime(doc.createdAt),
+ typeLabel: fileTypeLabel(doc.filename),
+ // RAG uploads are documents (pdf, txt, md, docx, html), not images.
+ thumb: ,
+ open: () => openResolvedUrl(() => getDocumentFileUrl(doc.id)),
+ remove: async () => {
+ await deleteDocument(doc.id, doc.projectId);
+ },
+ deleteDescription:
+ "The file and its indexed content are removed. This cannot be undone.",
+ };
+}
+
+function chatAttachmentRow(att: ChatAttachmentRecord): UploadedFileRow {
+ const isImage =
+ att.type === "image" || Boolean(att.contentType?.startsWith("image/"));
+ return {
+ key: `chat-${att.messageId}-${att.id}`,
+ source: "chat",
+ name: att.name,
+ location: att.threadTitle ? `Chat · ${att.threadTitle}` : "Chat",
+ sizeBytes: att.sizeBytes,
+ createdAt: att.createdAt,
+ sortTime: toSortTime(att.createdAt),
+ typeLabel: fileTypeLabel(att.name, att.contentType),
+ threadId: att.threadId,
+ pairId: att.pairId,
+ thumb: isImage ? (
+
+ ) : (
+
+ ),
+ open: () =>
+ openResolvedUrl(async () => {
+ const blob = await fetchChatAttachmentBlob(att.messageId, att.id);
+ const url = URL.createObjectURL(blob);
+ // Give the new tab time to load the blob before revoking.
+ setTimeout(() => URL.revokeObjectURL(url), 60_000);
+ return url;
+ }),
+ remove: async () => {
+ await deleteChatAttachment(att.messageId, att.id);
+ // Patch any loaded runtime copy so a later repo sync cannot write the
+ // deleted attachment back to storage.
+ emitChatAttachmentDeleted({
+ messageId: att.messageId,
+ attachmentId: att.id,
+ });
+ },
+ deleteDescription:
+ "The attachment is removed from its chat message; the message text is kept. This cannot be undone.",
+ };
+}
+
+type SourceLoad = {
+ status: "loading" | "ready" | "error";
+ data: T;
+ error: string | null;
+};
+
+function errorMessage(error: unknown, fallback: string): string {
+ return error instanceof Error ? error.message : fallback;
+}
+
+/** Inline settings page listing uploaded files from each available source. */
+export function UploadedFilesView() {
+ const [ragFiles, setRagFiles] = useState>({
+ status: "loading",
+ data: [],
+ error: null,
+ });
+ const [chatFiles, setChatFiles] = useState<
+ SourceLoad
+ >({ status: "loading", data: [], error: null });
+ const [chatNextOffset, setChatNextOffset] = useState(null);
+ const [loadingMore, setLoadingMore] = useState(false);
+ const [confirmingDelete, setConfirmingDelete] =
+ useState(null);
+ const navigate = useNavigate();
+
+ const rows = [
+ ...ragFiles.data.map(ragRow),
+ ...chatFiles.data.map(chatAttachmentRow),
+ ].sort((a, b) => b.sortTime - a.sortTime);
+
+ // Jump to the chat thread the attachment lives in, closing the settings
+ // dialog so the thread is actually visible.
+ function goToChat(row: UploadedFileRow) {
+ if (!row.threadId) return;
+ useSettingsDialogStore.getState().closeDialog();
+ if (row.pairId) {
+ void navigate({ to: "/chat", search: { compare: row.pairId } });
+ } else {
+ void navigate({ to: "/chat", search: { thread: row.threadId } });
+ }
+ }
+
+ useEffect(() => {
+ let cancelled = false;
+ void listAllDocuments().then(
+ (data) => {
+ if (!cancelled) setRagFiles({ status: "ready", data, error: null });
+ },
+ (error: unknown) => {
+ if (!cancelled) {
+ setRagFiles({
+ status: "error",
+ data: [],
+ error: errorMessage(error, "Failed to load RAG documents"),
+ });
+ }
+ },
+ );
+ void listChatAttachments().then(
+ (page) => {
+ if (!cancelled) {
+ setChatFiles({
+ status: "ready",
+ data: page.attachments,
+ error: null,
+ });
+ setChatNextOffset(page.nextOffset);
+ }
+ },
+ (error: unknown) => {
+ if (!cancelled) {
+ setChatFiles({
+ status: "error",
+ data: [],
+ error: errorMessage(error, "Failed to load chat attachments"),
+ });
+ }
+ },
+ );
+ return () => {
+ cancelled = true;
+ };
+ }, []);
+
+ function retryRagFiles() {
+ setRagFiles((current) => ({ ...current, status: "loading", error: null }));
+ void listAllDocuments().then(
+ (data) => setRagFiles({ status: "ready", data, error: null }),
+ (error: unknown) =>
+ setRagFiles((current) => ({
+ ...current,
+ status: "error",
+ error: errorMessage(error, "Failed to load RAG documents"),
+ })),
+ );
+ }
+
+ async function loadChatPage(offset: number, append: boolean) {
+ setLoadingMore(true);
+ setChatFiles((current) => ({ ...current, status: "loading", error: null }));
+ try {
+ const page = await listChatAttachments(offset);
+ setChatFiles((current) => ({
+ status: "ready",
+ data: append
+ ? [
+ ...current.data,
+ ...page.attachments.filter(
+ (incoming) =>
+ !current.data.some(
+ (existing) =>
+ existing.id === incoming.id &&
+ existing.messageId === incoming.messageId,
+ ),
+ ),
+ ]
+ : page.attachments,
+ error: null,
+ }));
+ setChatNextOffset(page.nextOffset);
+ } catch (error) {
+ setChatFiles((current) => ({
+ ...current,
+ status: "error",
+ error: errorMessage(error, "Failed to load chat attachments"),
+ }));
+ } finally {
+ setLoadingMore(false);
+ }
+ }
+
+ function retryChatFiles() {
+ const append = chatFiles.data.length > 0 && chatNextOffset !== null;
+ void loadChatPage(append ? chatNextOffset : 0, append);
+ }
+
+ async function handleOpen(row: UploadedFileRow) {
+ try {
+ await row.open();
+ } catch (err) {
+ toast.error("Failed to open file", {
+ description: err instanceof Error ? err.message : undefined,
+ });
+ }
+ }
+
+ async function handleDelete(row: UploadedFileRow) {
+ // Offset pages and destructive mutations must not race: a deletion shifts
+ // the boundary used by an in-flight page request.
+ if (loadingMore) return;
+ try {
+ await row.remove();
+ if (row.source === "rag") {
+ setRagFiles((current) => ({
+ ...current,
+ data: current.data.filter((doc) => `rag-${doc.id}` !== row.key),
+ }));
+ } else {
+ setChatFiles((current) => ({
+ ...current,
+ data: current.data.filter(
+ (attachment) =>
+ `chat-${attachment.messageId}-${attachment.id}` !== row.key,
+ ),
+ }));
+ // Offset pagination is relative to the current server inventory. A
+ // deletion before the next page shifts every later row back by one.
+ setChatNextOffset((current) =>
+ current === null ? null : Math.max(0, current - 1),
+ );
+ }
+ toast.success("File deleted");
+ } catch (err) {
+ toast.error("Failed to delete file", {
+ description: err instanceof Error ? err.message : undefined,
+ });
+ }
+ }
+
+ return (
+
+ {ragFiles.status === "error" ? (
+
+ RAG documents unavailable: {ragFiles.error}
+
+ Retry
+
+
+ ) : null}
+ {chatFiles.status === "error" ? (
+
+ Chat attachments unavailable: {chatFiles.error}
+
+ Retry
+
+
+ ) : null}
+
+ {rows.length === 0 &&
+ (ragFiles.status === "loading" || chatFiles.status === "loading") ? (
+
+
+
+ ) : rows.length === 0 &&
+ ragFiles.status !== "error" &&
+ chatFiles.status !== "error" ? (
+
+ No uploaded files.
+
+ ) : rows.length > 0 ? (
+
+
+ Name
+ Location
+ Uploaded
+
+
+ {rows.map((row) => (
+
+ {/* Clicking the file jumps to its chat; files without one
+ open directly. The theme scales rounded-md up to a near
+ circle at this size, so the thumb pins a small radius. */}
+
+ row.threadId ? goToChat(row) : void handleOpen(row)
+ }
+ title={
+ row.threadId ? `Go to ${row.location}` : `Open ${row.name}`
+ }
+ className="group/name flex min-w-0 flex-1 basis-[calc(100%-5rem)] items-center gap-2.5 overflow-hidden text-left sm:basis-auto"
+ >
+
+ {row.thumb}
+
+
+
+ {/* Floor keeps the name visible when the chip and fixed
+ columns squeeze the cell at narrow widths. */}
+
+ {row.name}
+
+ {row.typeLabel ? (
+
+ {row.typeLabel}
+
+ ) : null}
+ {row.failed ? (
+
+ failed
+
+ ) : null}
+
+
+ {formatSize(row.sizeBytes)}
+
+
+
+ {row.threadId ? (
+ goToChat(row)}
+ title={`Go to ${row.location}`}
+ className="order-3 w-full truncate pl-10 text-left text-muted-foreground underline-offset-2 transition-colors hover:text-foreground hover:underline sm:order-none sm:w-36 sm:pl-0"
+ >
+ {row.location}
+
+ ) : (
+
+ {row.location}
+
+ )}
+
+ {formatUploadedAt(row.createdAt)}
+
+
+ void handleOpen(row)}
+ aria-label={`Open ${row.name}`}
+ title="Open"
+ className="inline-flex size-7 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
+ >
+
+
+ setConfirmingDelete(row)}
+ aria-label={`Delete ${row.name}`}
+ title="Delete"
+ className="inline-flex size-7 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-destructive/10 hover:text-destructive disabled:cursor-wait disabled:opacity-50"
+ >
+
+
+
+
+ ))}
+ {chatNextOffset !== null ? (
+
+ void loadChatPage(chatNextOffset, true)}
+ className="rounded-md border border-border px-3 py-1.5 text-sm font-medium hover:bg-muted disabled:cursor-wait disabled:opacity-60"
+ >
+ {loadingMore ? "Loading..." : "Load more chat attachments"}
+
+
+ ) : null}
+
+ ) : null}
+
+
{
+ if (!o) setConfirmingDelete(null);
+ }}
+ >
+
+
+ Delete file
+
+ Delete{" "}
+
+ "{confirmingDelete?.name}"
+
+ ? {confirmingDelete?.deleteDescription}
+
+
+
+ Cancel
+ {
+ const row = confirmingDelete;
+ setConfirmingDelete(null);
+ if (row) void handleDelete(row);
+ }}
+ >
+ Delete
+
+
+
+
+
+ );
+}
diff --git a/studio/frontend/src/features/settings/settings-dialog.tsx b/studio/frontend/src/features/settings/settings-dialog.tsx
index 08588be8fe..d98d1b8ac0 100644
--- a/studio/frontend/src/features/settings/settings-dialog.tsx
+++ b/studio/frontend/src/features/settings/settings-dialog.tsx
@@ -15,6 +15,7 @@ import {
Cancel01Icon,
CloudIcon,
CpuIcon,
+ DatabaseSettingIcon,
Globe02Icon,
HelpCircleIcon,
Message01Icon,
@@ -43,6 +44,7 @@ import { ApiKeysTab } from "./tabs/api-keys-tab";
import { AppearanceTab } from "./tabs/appearance-tab";
import { ChatTab } from "./tabs/chat-tab";
import { ConnectionsTab } from "./tabs/connections-tab";
+import { DataTab } from "./tabs/data-tab";
import { GeneralTab } from "./tabs/general-tab";
import { ProfileTab } from "./tabs/profile-tab";
import { ResourcesTab } from "./tabs/resources-tab";
@@ -93,6 +95,12 @@ const TABS: TabDef[] = [
iconComponent: MicIcon,
badgeKey: "common.new",
},
+ {
+ id: "data",
+ labelKey: "settings.tabs.data",
+ icon: DatabaseSettingIcon,
+ badgeKey: "common.new",
+ },
{ id: "about", labelKey: "settings.tabs.about", icon: HelpCircleIcon },
];
@@ -112,6 +120,8 @@ function renderTab(tab: SettingsTab) {
return ;
case "connections":
return ;
+ case "data":
+ return ;
case "api-keys":
return ;
case "about":
@@ -210,6 +220,7 @@ export function SettingsDialog() {
chat: null,
voice: null,
connections: null,
+ data: null,
"api-keys": null,
about: null,
});
diff --git a/studio/frontend/src/features/settings/settings-search.ts b/studio/frontend/src/features/settings/settings-search.ts
index 786ee11d0a..c19b27c732 100644
--- a/studio/frontend/src/features/settings/settings-search.ts
+++ b/studio/frontend/src/features/settings/settings-search.ts
@@ -85,12 +85,19 @@ export const SETTINGS_SEARCH_INDEX: Record = {
"settings.chat.artifacts.title",
"settings.chat.artifacts.collapseHtmlBlocks",
"settings.chat.artifacts.allowNetworkAccess",
- "settings.chat.data",
+ "settings.chat.modelDisclaimer",
+ ],
+ // Chat data management moved to the Data tab; keep these rows findable there.
+ data: [
+ "settings.data.fineTuneExport",
+ "settings.data.archivedChats",
+ "settings.data.archiveAllChats",
+ "settings.data.confirmBeforeDeleting",
+ "settings.data.uploadedFiles",
+ "settings.chat.exportHistory",
"settings.chat.exportConversations",
"settings.chat.importChats",
"settings.chat.clearAllChats",
- "settings.chat.exportHistory",
- "settings.chat.modelDisclaimer",
],
"api-keys": [
"settings.apiKeys.title",
diff --git a/studio/frontend/src/features/settings/stores/settings-dialog-store.ts b/studio/frontend/src/features/settings/stores/settings-dialog-store.ts
index b9e9c75b14..51908a5ad0 100644
--- a/studio/frontend/src/features/settings/stores/settings-dialog-store.ts
+++ b/studio/frontend/src/features/settings/stores/settings-dialog-store.ts
@@ -11,6 +11,7 @@ export type SettingsTab =
| "chat"
| "voice"
| "connections"
+ | "data"
| "api-keys"
| "about";
@@ -30,7 +31,7 @@ interface SettingsDialogState {
// explicitly via onCloseAutoFocus.
opener: HTMLElement | null;
// Set when something asks to jump straight to the archived chats list (the
- // archive toast). ChatTab consumes it to open the dialog, then clears it.
+ // archive toast). DataTab uses it as its initial subpage, then clears it.
archivedChatsRequested: boolean;
openDialog: (tab?: SettingsTab, options?: OpenDialogOptions) => void;
openArchivedChats: () => void;
@@ -66,6 +67,7 @@ function loadInitialTab(): SettingsTab {
"chat",
"voice",
"connections",
+ "data",
"api-keys",
"about",
];
@@ -90,7 +92,7 @@ export const useSettingsDialogStore = create((set) => ({
openArchivedChats: () =>
set({
open: true,
- activeTab: "chat",
+ activeTab: "data",
scrollTarget: null,
archivedChatsRequested: true,
opener: captureOpener(),
diff --git a/studio/frontend/src/features/settings/tabs/chat-tab.tsx b/studio/frontend/src/features/settings/tabs/chat-tab.tsx
index d5c9f10a4f..f7f3bccad6 100644
--- a/studio/frontend/src/features/settings/tabs/chat-tab.tsx
+++ b/studio/frontend/src/features/settings/tabs/chat-tab.tsx
@@ -1,43 +1,16 @@
// 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 { Button } from "@/components/ui/button";
-import {
- Dialog,
- DialogContent,
- DialogDescription,
- DialogFooter,
- DialogHeader,
- DialogTitle,
-} from "@/components/ui/dialog";
-import {
- DropdownMenu,
- DropdownMenuContent,
- DropdownMenuItem,
- DropdownMenuSeparator,
- DropdownMenuSub,
- DropdownMenuSubContent,
- DropdownMenuSubTrigger,
- DropdownMenuTrigger,
-} from "@/components/ui/dropdown-menu";
import { Switch } from "@/components/ui/switch";
import {
- EXPORT_FORMATS_LIST,
type PlusMenuItemId,
- bulkExportConversationsByScope,
- clearAllChats,
- countAllChats,
- downloadChatExport,
- importConversationsFromFile,
useChatPreferencesStore,
useChatRuntimeStore,
usePlusMenuPrefsStore,
} from "@/features/chat";
import { useT } from "@/i18n";
-import { toast } from "@/lib/toast";
import {
Bookmark02Icon,
- Delete02Icon,
Download01Icon,
FileDatabaseIcon,
Folder01Icon,
@@ -45,19 +18,16 @@ import {
PencilRulerIcon,
Settings02Icon,
ShieldBanIcon,
- Upload01Icon,
} from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { Columns2Icon, PlusIcon } from "lucide-react";
-import { useEffect, useRef, useState } from "react";
+import { useEffect } from "react";
import type { ReactNode } from "react";
-import { ArchivedChatsDialog } from "../components/archived-chats-dialog";
import { SettingsRow } from "../components/settings-row";
import {
SettingsGroupDivider,
SettingsSection,
} from "../components/settings-section";
-import { useSettingsDialogStore } from "../stores/settings-dialog-store";
// Adjustable "+" menu items shown in settings, in display order. Icons mirror
// the ones used in the composer + menu itself.
@@ -140,7 +110,7 @@ const PLUS_MENU_SETTINGS: {
},
{
id: "bypassPermissions",
- label: "Bypass permissions",
+ label: "Tool permissions",
icon: (
state.pins);
const togglePlusPin = usePlusMenuPrefsStore((state) => state.togglePin);
- const [confirmOpen, setConfirmOpen] = useState(false);
- const [archivedOpen, setArchivedOpen] = useState(false);
- const [count, setCount] = useState(null);
- const archivedChatsRequested = useSettingsDialogStore(
- (s) => s.archivedChatsRequested,
- );
- const consumeArchivedChatsRequest = useSettingsDialogStore(
- (s) => s.consumeArchivedChatsRequest,
- );
-
- // Open the archived list when the archive toast asked to jump here.
- useEffect(() => {
- if (!archivedChatsRequested) return;
- setArchivedOpen(true);
- consumeArchivedChatsRequest();
- }, [archivedChatsRequested, consumeArchivedChatsRequest]);
- const [exporting, setExporting] = useState(false);
- const [clearing, setClearing] = useState(false);
const autoTitle = useChatRuntimeStore((state) => state.autoTitle);
const setAutoTitle = useChatRuntimeStore((state) => state.setAutoTitle);
const showCanvasMenuItem = useChatRuntimeStore(
@@ -212,12 +164,6 @@ export function ChatTab() {
const setShowAllQuantizations = useChatRuntimeStore(
(state) => state.setShowAllQuantizations,
);
- const confirmDeleteChats = useChatPreferencesStore(
- (state) => state.confirmDeleteChats,
- );
- const setConfirmDeleteChats = useChatPreferencesStore(
- (state) => state.setConfirmDeleteChats,
- );
const showModelDisclaimer = useChatPreferencesStore(
(state) => state.showModelDisclaimer,
);
@@ -232,95 +178,9 @@ export function ChatTab() {
);
useEffect(() => {
- void countAllChats().then(setCount);
void hydratePersistedSettings();
}, [hydratePersistedSettings]);
- const handleExport = async () => {
- setExporting(true);
- try {
- await downloadChatExport();
- } finally {
- setExporting(false);
- }
- };
-
- const importInputRef = useRef(null);
- const handleImport = async (file: File) => {
- try {
- const imported = await importConversationsFromFile(file, null);
- if (imported === 0) {
- toast.info(t("settings.chat.importNoConversations"));
- } else {
- toast.success(
- imported === 1
- ? t("settings.chat.importedOneChat")
- : t("settings.chat.importedChatCount", { count: imported }),
- );
- setCount(await countAllChats().catch(() => count));
- }
- } catch {
- toast.error(t("settings.chat.importFailed"));
- }
- };
-
- const handleClear = async () => {
- setClearing(true);
- try {
- const result = await clearAllChats();
- const clearedCount = result.deletedThreadIds.length;
- const hasFailedStore =
- result.backend === "failed" || result.legacy === "failed";
- if (!hasFailedStore && result.failedThreadIds.length === 0) {
- setCount(0);
- setConfirmOpen(false);
- toast.success(
- clearedCount === 0
- ? t("settings.chat.clearedAllChats")
- : clearedCount === 1
- ? t("settings.chat.clearedOneChat")
- : t("settings.chat.clearedChatCount", { count: clearedCount }),
- );
- return;
- }
-
- const fallbackRemaining =
- result.failedThreadIds.length > 0
- ? result.failedThreadIds.length
- : (count ?? 0);
- const remaining = await countAllChats().catch(() => fallbackRemaining);
- setCount(remaining);
- setConfirmOpen(false);
- toast.warning(t("settings.chat.someChatsCouldNotBeCleared"), {
- description:
- result.failedThreadIds.length > 0
- ? clearedCount === 1 && result.failedThreadIds.length === 1
- ? t("settings.chat.oneChatClearedRemainOne")
- : clearedCount === 1
- ? t("settings.chat.oneChatClearedRemain", {
- remainingCount: result.failedThreadIds.length,
- })
- : result.failedThreadIds.length === 1
- ? t("settings.chat.chatsClearedRemainOne", { clearedCount })
- : t("settings.chat.chatsClearedRemain", {
- clearedCount,
- remainingCount: result.failedThreadIds.length,
- })
- : remaining === 1
- ? t("settings.chat.storageClearFailedOne")
- : t("settings.chat.storageClearFailed", { count: remaining }),
- });
- } catch (error) {
- const remaining = await countAllChats().catch(() => count);
- setCount(remaining);
- toast.error(t("settings.chat.failedToClearChats"), {
- description: error instanceof Error ? error.message : undefined,
- });
- } finally {
- setClearing(false);
- }
- };
-
return (
@@ -347,7 +207,7 @@ export function ChatTab() {
Q4_K_M
-
+
downloaded
16 GB
@@ -481,191 +341,6 @@ export function ChatTab() {
/>
-
-
-
- setArchivedOpen(true)}
- >
- Manage
-
-
-
-
-
-
-
-
-
-
- {exporting
- ? t("settings.chat.exportingAction")
- : t("settings.chat.exportAction")}
-
-
-
-
-
-
-
-
- {t("settings.chat.exportConversationsAction")}
-
-
-
- {(
- [
- { scope: "recents", label: "exportScopeRecents" },
- { scope: "all", label: "exportScopeAll" },
- ] as const
- ).map(({ scope, label }) => (
-
-
-
- {t(`settings.chat.${label}`)}
-
-
- {EXPORT_FORMATS_LIST.map(({ fmt, label: fmtLabel }) => (
-
- void bulkExportConversationsByScope(scope, fmt, true)
- }
- >
- {fmtLabel} {t("settings.chat.exportCombinedSuffix")}
-
- ))}
-
- {EXPORT_FORMATS_LIST.map(({ fmt, label: fmtLabel }) => (
-
- void bulkExportConversationsByScope(scope, fmt, false)
- }
- >
- {fmtLabel} {t("settings.chat.exportPerChatSuffix")}
-
- ))}
-
-
- ))}
-
-
-
-
-
- importInputRef.current?.click()}
- >
-
- {t("settings.chat.importChatsAction")}
-
- {
- const file = e.target.files?.[0];
- e.target.value = "";
- if (file) void handleImport(file);
- }}
- />
-
-
-
- setConfirmOpen(true)}
- disabled={count === 0}
- className="text-destructive hover:text-destructive hover:border-destructive/60"
- >
-
- {t("settings.chat.clearChatsAction")}
-
-
-
-
-
-
-
-
-
-
- {count === 1
- ? t("settings.chat.clearOneChatTitle")
- : t("settings.chat.clearChatsTitle", { count: count ?? 0 })}
-
-
- {t("settings.chat.clearChatsConfirmDescription")}
-
-
-
- setConfirmOpen(false)}>
- {t("common.cancel")}
-
-
- {clearing
- ? t("settings.chat.clearingAction")
- : count === 1
- ? t("settings.chat.clearOneChatAction")
- : t("settings.chat.clearChatCountAction", {
- count: count ?? 0,
- })}
-
-
-
-
);
}
diff --git a/studio/frontend/src/features/settings/tabs/data-tab.tsx b/studio/frontend/src/features/settings/tabs/data-tab.tsx
new file mode 100644
index 0000000000..dc9e707ea0
--- /dev/null
+++ b/studio/frontend/src/features/settings/tabs/data-tab.tsx
@@ -0,0 +1,727 @@
+// 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 { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuSeparator,
+ DropdownMenuSub,
+ DropdownMenuSubContent,
+ DropdownMenuSubTrigger,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu";
+import { Spinner } from "@/components/ui/spinner";
+import { Switch } from "@/components/ui/switch";
+import { usePlatformStore } from "@/config/env";
+import {
+ EXPORT_FORMATS_LIST,
+ type FineTuneFormat,
+ archiveAllChatItems,
+ bulkExportConversationsByScope,
+ clearAllChats,
+ countAllChats,
+ downloadArchivedChatExport,
+ downloadChatExport,
+ exportFineTuneJsonl,
+ importConversationsFromFile,
+ useChatPreferencesStore,
+ useChatRuntimeStore,
+ useChatSidebarItems,
+} from "@/features/chat";
+import { useT } from "@/i18n";
+import {
+ ChevronDownStandardIcon,
+ ChevronRightStandardIcon,
+} from "@/lib/chevron-icons";
+import { toast } from "@/lib/toast";
+import {
+ Archive02Icon,
+ ArrowLeft01Icon,
+ Delete02Icon,
+ Download01Icon,
+ Tick02Icon,
+ Upload01Icon,
+} from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
+import { useNavigate, useRouterState } from "@tanstack/react-router";
+import { useEffect, useRef, useState } from "react";
+import { ArchivedChatsView } from "../components/archived-chats-dialog";
+import {
+ createFineTuneRecipeFromChats,
+ loadFineTuneDatasetInTrainTab,
+} from "../components/finetune-recipe";
+import { SettingsRow } from "../components/settings-row";
+import { SettingsSection } from "../components/settings-section";
+import { UploadedFilesView } from "../components/uploaded-files-dialog";
+import { useSettingsDialogStore } from "../stores/settings-dialog-store";
+
+export function DataTab() {
+ const t = useT();
+ const navigate = useNavigate();
+ const archivedChatsRequested = useSettingsDialogStore(
+ (s) => s.archivedChatsRequested,
+ );
+ const consumeArchivedChatsRequest = useSettingsDialogStore(
+ (s) => s.consumeArchivedChatsRequest,
+ );
+ const [confirmOpen, setConfirmOpen] = useState(false);
+ const [archiveConfirmOpen, setArchiveConfirmOpen] = useState(false);
+ // Subpages swap the Data tab body instead of opening nested dialogs.
+ const [subpage, setSubpage] = useState<"main" | "archived" | "files">(
+ archivedChatsRequested ? "archived" : "main",
+ );
+ const [count, setCount] = useState(null);
+ const [exporting, setExporting] = useState(false);
+ const [archivedExporting, setArchivedExporting] = useState(false);
+ // Gates the archived subpage Export button.
+ const { archivedItems } = useChatSidebarItems({ requireMessages: false });
+ const [clearing, setClearing] = useState(false);
+ const [archiving, setArchiving] = useState(false);
+ const [fineTuneExporting, setFineTuneExporting] = useState(false);
+ const [openingRecipe, setOpeningRecipe] = useState(false);
+ const [loadingTraining, setLoadingTraining] = useState(false);
+ // Chat-only hosts redirect /studio back to /chat, so loading a dataset in
+ // the Train tab would upload it and then strand the user; gate the action
+ // the same way the sidebar gates Train.
+ const chatOnly = usePlatformStore((s) => s.isChatOnly());
+ const [fineTuneAction, setFineTuneAction] = useState<
+ "train" | "recipes" | "export"
+ >(chatOnly ? "export" : "train");
+ // Chat Completions (OpenAI messages) is the only export format we ship.
+ const fineTuneFormat: FineTuneFormat = "openai";
+
+ // The MLX self-heal can flip chat-only while the dialog is open.
+ useEffect(() => {
+ if (chatOnly) {
+ setFineTuneAction((a) => (a === "train" ? "export" : a));
+ }
+ }, [chatOnly]);
+ // Requests can arrive after Data is already mounted (for example from the
+ // archive-all toast), so always switch before consuming the flag.
+ useEffect(() => {
+ if (!archivedChatsRequested) return;
+ let cancelled = false;
+ queueMicrotask(() => {
+ if (cancelled) return;
+ setSubpage("archived");
+ consumeArchivedChatsRequest();
+ });
+ return () => {
+ cancelled = true;
+ };
+ }, [archivedChatsRequested, consumeArchivedChatsRequest]);
+
+ const confirmDeleteChats = useChatPreferencesStore(
+ (state) => state.confirmDeleteChats,
+ );
+ const setConfirmDeleteChats = useChatPreferencesStore(
+ (state) => state.setConfirmDeleteChats,
+ );
+
+ const storeThreadId = useChatRuntimeStore((s) => s.activeThreadId);
+ // Open chat id from the route (single thread or compare pair), mirroring
+ // ArchivedChatsView: compare panes only live in the search params.
+ const openChatId = useRouterState({
+ select: (s) => {
+ if (!s.location.pathname.startsWith("/chat")) return undefined;
+ const search = s.location.search as Record;
+ return search.thread ?? search.compare ?? storeThreadId ?? undefined;
+ },
+ });
+
+ useEffect(() => {
+ void countAllChats().then(setCount);
+ }, []);
+
+ const handleExport = async () => {
+ setExporting(true);
+ try {
+ await downloadChatExport();
+ } finally {
+ setExporting(false);
+ }
+ };
+
+ const handleExportArchived = async () => {
+ setArchivedExporting(true);
+ try {
+ const exported = await downloadArchivedChatExport();
+ toast.success(
+ exported === 0
+ ? t("settings.data.noArchivedChatsToExport")
+ : exported === 1
+ ? t("settings.data.exportedOneArchivedChat")
+ : t("settings.data.exportedArchivedChatCount", { count: exported }),
+ );
+ } catch (error) {
+ toast.error(t("settings.data.failedToExportArchivedChats"), {
+ description: error instanceof Error ? error.message : undefined,
+ });
+ } finally {
+ setArchivedExporting(false);
+ }
+ };
+
+ const importInputRef = useRef(null);
+ const handleImport = async (file: File) => {
+ try {
+ const imported = await importConversationsFromFile(file, null);
+ if (imported === 0) {
+ toast.info(t("settings.chat.importNoConversations"));
+ } else {
+ toast.success(
+ imported === 1
+ ? t("settings.chat.importedOneChat")
+ : t("settings.chat.importedChatCount", { count: imported }),
+ );
+ setCount(await countAllChats().catch(() => count));
+ }
+ } catch {
+ toast.error(t("settings.chat.importFailed"));
+ }
+ };
+
+ const handleArchiveAll = async () => {
+ setArchiving(true);
+ try {
+ const archived = await archiveAllChatItems(openChatId, (view) => {
+ navigate({ to: "/chat", search: { new: view.newThreadNonce } });
+ });
+ setArchiveConfirmOpen(false);
+ toast.success(
+ archived === 0
+ ? t("settings.data.noChatsToArchive")
+ : archived === 1
+ ? t("settings.data.archivedOneChat")
+ : t("settings.data.archivedChatCount", { count: archived }),
+ );
+ } catch (error) {
+ toast.error(t("settings.data.failedToArchiveChats"), {
+ description: error instanceof Error ? error.message : undefined,
+ });
+ } finally {
+ setArchiving(false);
+ }
+ };
+
+ const handleFineTuneExport = async () => {
+ setFineTuneExporting(true);
+ try {
+ await exportFineTuneJsonl(fineTuneFormat);
+ } catch (error) {
+ toast.error(t("settings.data.fineTuneExportFailed"), {
+ description: error instanceof Error ? error.message : undefined,
+ });
+ } finally {
+ setFineTuneExporting(false);
+ }
+ };
+
+ const handleOpenInRecipes = async () => {
+ setOpeningRecipe(true);
+ try {
+ const recipeId = await createFineTuneRecipeFromChats(fineTuneFormat);
+ if (!recipeId) return;
+ useSettingsDialogStore.getState().closeDialog();
+ void navigate({ to: "/data-recipes/$recipeId", params: { recipeId } });
+ } catch (error) {
+ toast.error(t("settings.data.fineTuneRecipeFailed"), {
+ description: error instanceof Error ? error.message : undefined,
+ });
+ } finally {
+ setOpeningRecipe(false);
+ }
+ };
+
+ const handleUseInTraining = async () => {
+ setLoadingTraining(true);
+ try {
+ const loaded = await loadFineTuneDatasetInTrainTab(fineTuneFormat);
+ if (!loaded) return;
+ useSettingsDialogStore.getState().closeDialog();
+ void navigate({ to: "/studio" });
+ } catch (error) {
+ toast.error(t("settings.data.fineTuneTrainFailed"), {
+ description: error instanceof Error ? error.message : undefined,
+ });
+ } finally {
+ setLoadingTraining(false);
+ }
+ };
+
+ const fineTuneActionLabels = {
+ train: t("settings.data.fineTuneTrainAction"),
+ recipes: t("settings.data.fineTuneOpenRecipesAction"),
+ export: t("settings.data.fineTuneExportAction"),
+ } as const;
+ const fineTuneBusy = loadingTraining || openingRecipe || fineTuneExporting;
+ const runFineTuneAction = () => {
+ if (fineTuneAction === "train") {
+ if (chatOnly) return;
+ void handleUseInTraining();
+ } else if (fineTuneAction === "recipes") void handleOpenInRecipes();
+ else void handleFineTuneExport();
+ };
+
+ const handleClear = async () => {
+ setClearing(true);
+ try {
+ const result = await clearAllChats();
+ const clearedCount = result.deletedThreadIds.length;
+ const hasFailedStore =
+ result.backend === "failed" || result.legacy === "failed";
+ if (!hasFailedStore && result.failedThreadIds.length === 0) {
+ setCount(0);
+ setConfirmOpen(false);
+ toast.success(
+ clearedCount === 0
+ ? t("settings.chat.clearedAllChats")
+ : clearedCount === 1
+ ? t("settings.chat.clearedOneChat")
+ : t("settings.chat.clearedChatCount", { count: clearedCount }),
+ );
+ return;
+ }
+
+ const fallbackRemaining =
+ result.failedThreadIds.length > 0
+ ? result.failedThreadIds.length
+ : (count ?? 0);
+ const remaining = await countAllChats().catch(() => fallbackRemaining);
+ setCount(remaining);
+ setConfirmOpen(false);
+ toast.warning(t("settings.chat.someChatsCouldNotBeCleared"), {
+ description:
+ result.failedThreadIds.length > 0
+ ? clearedCount === 1 && result.failedThreadIds.length === 1
+ ? t("settings.chat.oneChatClearedRemainOne")
+ : clearedCount === 1
+ ? t("settings.chat.oneChatClearedRemain", {
+ remainingCount: result.failedThreadIds.length,
+ })
+ : result.failedThreadIds.length === 1
+ ? t("settings.chat.chatsClearedRemainOne", { clearedCount })
+ : t("settings.chat.chatsClearedRemain", {
+ clearedCount,
+ remainingCount: result.failedThreadIds.length,
+ })
+ : remaining === 1
+ ? t("settings.chat.storageClearFailedOne")
+ : t("settings.chat.storageClearFailed", { count: remaining }),
+ });
+ } catch (error) {
+ const remaining = await countAllChats().catch(() => count);
+ setCount(remaining);
+ toast.error(t("settings.chat.failedToClearChats"), {
+ description: error instanceof Error ? error.message : undefined,
+ });
+ } finally {
+ setClearing(false);
+ }
+ };
+
+ if (subpage === "archived") {
+ return (
+
+
+ setSubpage("main")}
+ aria-label={`Back to ${t("settings.data.title")}`}
+ className="inline-flex size-7 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
+ >
+
+
+
+ {t("settings.data.title")}
+
+
+
+
+
+ {t("settings.data.archivedChats")}
+
+
+ {t("settings.data.archivedChatsDescription")}
+
+
+ {archivedItems.length > 0 && (
+
+ {archivedExporting ? (
+
+ ) : (
+
+ )}
+ {archivedExporting
+ ? t("settings.data.exportingArchivedChats")
+ : t("settings.data.exportArchivedChats")}
+
+ )}
+
+
+
+ );
+ }
+
+ if (subpage === "files") {
+ return (
+
+
+ setSubpage("main")}
+ aria-label={`Back to ${t("settings.data.title")}`}
+ className="inline-flex size-7 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
+ >
+
+
+
+ {t("settings.data.title")}
+
+
+
+
+ {t("settings.data.uploadedFiles")}
+
+
+ {t("settings.data.uploadedFilesDescription")}
+
+
+
+
+ );
+ }
+
+ return (
+
+
+
+
+
+
+
+
+ {/* Fixed width so switching actions never resizes the row. */}
+
+
+ {fineTuneActionLabels[fineTuneAction]}
+
+
+
+
+
+ {(["export", "train", "recipes"] as const).map((action) => (
+ setFineTuneAction(action)}
+ >
+
+ {fineTuneActionLabels[action]}
+
+ {fineTuneAction === action ? (
+
+ ) : null}
+
+ ))}
+
+
+
+ {fineTuneBusy ? (
+
+ ) : (
+
+ )}
+
+
+
+
+
+ setSubpage("archived")}
+ >
+ {t("settings.data.manageAction")}
+
+
+
+
+ setArchiveConfirmOpen(true)}
+ >
+
+ {t("settings.data.archiveAllAction")}
+
+
+
+
+
+
+
+
+
+
+ {exporting
+ ? t("settings.chat.exportingAction")
+ : t("settings.chat.exportAction")}
+
+
+
+
+
+
+
+
+ {t("settings.chat.exportConversationsAction")}
+
+
+
+ {(
+ [
+ { scope: "recents", label: "exportScopeRecents" },
+ { scope: "all", label: "exportScopeAll" },
+ ] as const
+ ).map(({ scope, label }) => (
+
+
+
+ {t(`settings.chat.${label}`)}
+
+
+ {EXPORT_FORMATS_LIST.map(({ fmt, label: fmtLabel }) => (
+
+ void bulkExportConversationsByScope(scope, fmt, true)
+ }
+ >
+ {fmtLabel} {t("settings.chat.exportCombinedSuffix")}
+
+ ))}
+
+ {EXPORT_FORMATS_LIST.map(({ fmt, label: fmtLabel }) => (
+
+ void bulkExportConversationsByScope(scope, fmt, false)
+ }
+ >
+ {fmtLabel} {t("settings.chat.exportPerChatSuffix")}
+
+ ))}
+
+
+ ))}
+
+
+
+
+
+ setConfirmOpen(true)}
+ disabled={count === 0}
+ className="text-destructive hover:text-destructive hover:border-destructive/60"
+ >
+
+ {t("settings.chat.clearChatsAction")}
+
+
+
+
+ importInputRef.current?.click()}
+ >
+
+ {t("settings.chat.importChatsAction")}
+
+ {
+ const file = e.target.files?.[0];
+ e.target.value = "";
+ if (file) void handleImport(file);
+ }}
+ />
+
+
+
+
+
+ setSubpage("files")}
+ >
+ {t("settings.data.manageAction")}
+
+
+
+
+
+
+
+ {t("settings.data.archiveAllChatsTitle")}
+
+ {t("settings.data.archiveAllChatsConfirmDescription")}
+
+
+
+ setArchiveConfirmOpen(false)}
+ >
+ {t("common.cancel")}
+
+
+ {archiving
+ ? t("settings.data.archivingAction")
+ : t("settings.data.archiveAllAction")}
+
+
+
+
+
+
+
+
+
+ {count === 1
+ ? t("settings.chat.clearOneChatTitle")
+ : t("settings.chat.clearChatsTitle", { count: count ?? 0 })}
+
+
+ {t("settings.chat.clearChatsConfirmDescription")}
+
+
+
+ setConfirmOpen(false)}>
+ {t("common.cancel")}
+
+
+ {clearing
+ ? t("settings.chat.clearingAction")
+ : count === 1
+ ? t("settings.chat.clearOneChatAction")
+ : t("settings.chat.clearChatCountAction", {
+ count: count ?? 0,
+ })}
+
+
+
+
+
+ );
+}
diff --git a/studio/frontend/src/features/settings/tabs/general-tab.tsx b/studio/frontend/src/features/settings/tabs/general-tab.tsx
index 73a33fd155..d5a85c01c6 100644
--- a/studio/frontend/src/features/settings/tabs/general-tab.tsx
+++ b/studio/frontend/src/features/settings/tabs/general-tab.tsx
@@ -21,6 +21,7 @@ import {
setShowLlamaUpdateBanner,
useShowLlamaUpdateBanner,
} from "@/hooks/use-llama-update-pref";
+import { useHfTokenValidation } from "@/hooks";
import { LOCALE_STORAGE_KEY, useT } from "@/i18n";
import { isTauri } from "@/lib/api-base";
import { copyToClipboard } from "@/lib/copy-to-clipboard";
@@ -216,11 +217,18 @@ export function GeneralTab() {
if (trimmed !== hfToken) setHfToken(trimmed);
};
+ const clearHfToken = () => {
+ draftRef.current = "";
+ setDraftToken("");
+ setHfToken("");
+ };
+
// Show an "accepted" tick once a non-empty token has been committed to the
// store and the field still matches it (i.e. not mid-edit). Gives the user
// feedback that a pasted token was saved.
const tokenSaved =
draftToken.trim().length > 0 && draftToken.trim() === (hfToken ?? "");
+ const tokenValidation = useHfTokenValidation(hfToken ?? "");
useEffect(() => {
let cancelled = false;
@@ -498,46 +506,70 @@ export function GeneralTab() {
label={t("settings.general.huggingFaceToken")}
description={t("settings.general.huggingFaceTokenDescription")}
>
-
-
setDraftToken(e.target.value)}
- onBlur={commitToken}
- className={cn(
- "h-8 w-full font-mono text-xs",
- tokenSaved ? "pr-14" : "pr-8",
- )}
- />
- {tokenSaved ? (
- // Decorative: pointer-events-none lets clicks reach the input
- // underneath so the field still focuses anywhere.
-
+
+
+ setDraftToken(e.target.value)}
+ onBlur={commitToken}
+ className={cn(
+ "h-8 w-full font-mono text-xs",
+ tokenSaved ? "pr-14" : "pr-8",
+ )}
+ />
+ {tokenSaved ? (
+ // Decorative: pointer-events-none lets clicks reach the input
+ // underneath so the field still focuses anywhere.
+
+
+
+ ) : null}
+ setShowToken((s) => !s)}
+ className="absolute right-1.5 top-1/2 flex size-5 -translate-y-1/2 items-center justify-center rounded text-muted-foreground transition-colors hover:text-foreground"
+ aria-label={
+ showToken
+ ? t("settings.general.hideToken")
+ : t("settings.general.showToken")
+ }
+ tabIndex={-1}
+ >
+ {showToken ? (
+
+ ) : (
+
+ )}
+
+
+
-
-
+ Clear
+
+
+ {tokenValidation.isChecking ? (
+ Checking token…
+ ) : tokenValidation.error ? (
+
+ {tokenValidation.error}
+
) : null}
- setShowToken((s) => !s)}
- className="absolute right-1.5 top-1/2 flex size-5 -translate-y-1/2 items-center justify-center rounded text-muted-foreground transition-colors hover:text-foreground"
- aria-label={
- showToken
- ? t("settings.general.hideToken")
- : t("settings.general.showToken")
- }
- tabIndex={-1}
- >
- {showToken ? (
-
- ) : (
-
- )}
-
{/* The desktop app authenticates via desktop auto-auth with a generated
diff --git a/studio/frontend/src/features/settings/tabs/resources-tab.tsx b/studio/frontend/src/features/settings/tabs/resources-tab.tsx
index 9a359f0f38..eb7fa03cf9 100644
--- a/studio/frontend/src/features/settings/tabs/resources-tab.tsx
+++ b/studio/frontend/src/features/settings/tabs/resources-tab.tsx
@@ -90,8 +90,11 @@ function MetricTile({
label: string;
value: string;
detail: string;
- percent: number;
+ // null = usage unknown (e.g. Windows ROCm perf counter): show a dash and
+ // empty bar rather than a fabricated 0%.
+ percent: number | null;
}) {
+ const percentKnown = isFiniteNumber(percent);
const safePercent = clampPercent(percent);
return (
@@ -102,10 +105,10 @@ function MetricTile({
- {formatPercent(safePercent)}
+ {percentKnown ? formatPercent(safePercent) : "--"}
@@ -117,7 +120,7 @@ function MetricTile({
sum + (device.memory_total_gb ?? 0),
0,
);
- const vramUsed = devices.reduce(
- (sum, device) => sum + (device.vram_used_gb ?? 0),
- 0,
- );
- const vramFree = devices.reduce(
- (sum, device) =>
- sum +
- (device.vram_free_gb ??
- Math.max(0, (device.memory_total_gb ?? 0) - (device.vram_used_gb ?? 0))),
- 0,
- );
- const vramPercent = vramTotal > 0 ? (vramUsed / vramTotal) * 100 : 0;
+ // null usage = unknown (e.g. Windows ROCm perf counter): treating it as 0
+ // fabricates a 0-used total, so the aggregate is unknown if any device is.
+ const vramUsageKnown =
+ devices.length > 0 &&
+ devices.every((device) => isFiniteNumber(device.vram_used_gb));
+ const vramUsed = vramUsageKnown
+ ? devices.reduce((sum, device) => sum + (device.vram_used_gb ?? 0), 0)
+ : null;
+ const vramFree = vramUsageKnown
+ ? devices.reduce(
+ (sum, device) =>
+ sum +
+ (device.vram_free_gb ??
+ Math.max(
+ 0,
+ (device.memory_total_gb ?? 0) - (device.vram_used_gb ?? 0),
+ )),
+ 0,
+ )
+ : null;
+ const vramPercent =
+ vramUsageKnown && isFiniteNumber(vramUsed) && vramTotal > 0
+ ? (vramUsed / vramTotal) * 100
+ : 0;
return {
devices,
@@ -218,6 +233,7 @@ export function ResourcesTab() {
vramUsed,
vramFree,
vramPercent,
+ vramUsageKnown,
};
}, [systemInfo]);
@@ -259,6 +275,7 @@ export function ResourcesTab() {
: modelsFolderLoaded
? t("settings.resources.environment.unknown")
: t("common.loading");
+ const unknownLabel = t("settings.resources.environment.unknown");
return (
@@ -327,17 +344,21 @@ export function ResourcesTab() {
label={t("settings.resources.liveMonitor.vram")}
value={
hasGpu
- ? `${formatGiB(metrics.vramUsed)} / ${formatGiB(metrics.vramTotal)}`
+ ? metrics.vramUsageKnown
+ ? `${formatGiB(metrics.vramUsed)} / ${formatGiB(metrics.vramTotal)}`
+ : `${unknownLabel} / ${formatGiB(metrics.vramTotal)}`
: t("settings.resources.liveMonitor.noGpu")
}
detail={
hasGpu
- ? t("settings.resources.liveMonitor.free", {
- value: formatGiB(metrics.vramFree),
- })
+ ? metrics.vramUsageKnown
+ ? t("settings.resources.liveMonitor.free", {
+ value: formatGiB(metrics.vramFree),
+ })
+ : unknownLabel
: backendLabel
}
- percent={metrics.vramPercent}
+ percent={metrics.vramUsageKnown ? metrics.vramPercent : null}
/>
@@ -346,13 +367,33 @@ export function ResourcesTab() {
{hasGpu ? (
metrics.devices.map((device, index) => {
const ordinal = deviceOrdinal(device);
- const total = device.memory_total_gb ?? 0;
- const used = device.vram_used_gb ?? 0;
- const free = device.vram_free_gb ?? Math.max(0, total - used);
+ // Preserve null (unknown, e.g. Windows ROCm perf counter); coercing
+ // to 0 would render a fabricated 0 used / full free.
+ const total = device.memory_total_gb ?? null;
+ const used = device.vram_used_gb ?? null;
+ const free =
+ device.vram_free_gb ??
+ (isFiniteNumber(total) && isFiniteNumber(used)
+ ? Math.max(0, total - used)
+ : null);
const percent =
device.vram_utilization_pct ??
- (total > 0 ? (used / total) * 100 : null);
+ (isFiniteNumber(total) && total > 0 && isFiniteNumber(used)
+ ? (used / total) * 100
+ : null);
const safePercent = clampPercent(percent);
+ const usedText = isFiniteNumber(used)
+ ? formatGiB(used)
+ : unknownLabel;
+ const freeText = isFiniteNumber(free)
+ ? formatGiB(free)
+ : unknownLabel;
+ const totalText = isFiniteNumber(total)
+ ? formatGiB(total)
+ : unknownLabel;
+ const percentText = isFiniteNumber(percent)
+ ? formatPercent(safePercent)
+ : unknownLabel;
return (
- {formatPercent(safePercent)}{" "}
+ {percentText}{" "}
{t("settings.resources.gpu.vramUtilization")}
@@ -382,17 +423,17 @@ export function ResourcesTab() {
{t("settings.resources.gpu.used", {
- value: formatGiB(used),
+ value: usedText,
})}
{t("settings.resources.gpu.free", {
- value: formatGiB(free),
+ value: freeText,
})}
{t("settings.resources.gpu.total", {
- value: formatGiB(total),
+ value: totalText,
})}
diff --git a/studio/frontend/src/features/training/api/train-api.ts b/studio/frontend/src/features/training/api/train-api.ts
index 609e4f8591..c5f09c8bd0 100644
--- a/studio/frontend/src/features/training/api/train-api.ts
+++ b/studio/frontend/src/features/training/api/train-api.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { authFetch } from "@/features/auth";
+import { prepareHfTokenForUse } from "@/features/hf-auth";
import { readFastApiError } from "@/lib/format-fastapi-error";
import type {
TrainingStartRequest,
@@ -30,10 +31,12 @@ async function parseJson
(response: Response): Promise {
export async function startTraining(
payload: TrainingStartRequest,
): Promise {
+ const preparedToken = await prepareHfTokenForUse(payload.hf_token);
+ if (!preparedToken.proceed) throw new Error("Training start cancelled.");
const response = await authFetch("/api/train/start", {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify(payload),
+ body: JSON.stringify({ ...payload, hf_token: preparedToken.token }),
});
return parseJson(response);
}
diff --git a/studio/frontend/src/features/training/hooks/use-training-actions.ts b/studio/frontend/src/features/training/hooks/use-training-actions.ts
index 2f04656c23..0d32e066d5 100644
--- a/studio/frontend/src/features/training/hooks/use-training-actions.ts
+++ b/studio/frontend/src/features/training/hooks/use-training-actions.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { primeNativeNotificationPermission } from "@/lib/native-notifications";
+import { prepareHfTokenForUse } from "@/features/hf-auth";
import { confirmRemoteCodeIfNeeded } from "@/features/security";
import { useCallback } from "react";
import { toast } from "@/lib/toast";
@@ -43,7 +44,7 @@ export function useTrainingActions() {
const startError = useTrainingRuntimeStore((state) => state.startError);
const startTrainingRun = useCallback(async (): Promise => {
- const config = useTrainingConfigStore.getState();
+ let config = useTrainingConfigStore.getState();
const runtimeStore = useTrainingRuntimeStore.getState();
const dialogStore = useDatasetPreviewDialogStore.getState();
@@ -54,6 +55,13 @@ export function useTrainingActions() {
return false;
}
+ const preparedToken = await prepareHfTokenForUse(config.hfToken);
+ if (!preparedToken.proceed) return false;
+ if ((preparedToken.token ?? "") !== config.hfToken) {
+ config.setHfToken(preparedToken.token ?? "");
+ config = useTrainingConfigStore.getState();
+ }
+
primeNativeNotificationPermission().catch(() => undefined);
runtimeStore.setStartResources(
@@ -226,6 +234,13 @@ export function useTrainingActions() {
resume_from_checkpoint: outputDir,
} as TrainingStartRequest;
+ const preparedToken = await prepareHfTokenForUse(payload.hf_token);
+ if (!preparedToken.proceed) {
+ runtimeStore.setStarting(false);
+ return false;
+ }
+ payload.hf_token = preparedToken.token;
+
runtimeStore.setStartResources(
payload.model_name,
payload.hf_dataset,
diff --git a/studio/frontend/src/hooks/use-hf-token-validation.ts b/studio/frontend/src/hooks/use-hf-token-validation.ts
index 11f56a2151..e0ed02cca2 100644
--- a/studio/frontend/src/hooks/use-hf-token-validation.ts
+++ b/studio/frontend/src/hooks/use-hf-token-validation.ts
@@ -1,8 +1,8 @@
// 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 { whoAmI } from "@huggingface/hub";
-import { useCallback, useEffect, useRef, useState } from "react";
+import { validateHfToken } from "@/features/hf-auth";
+import { useEffect, useRef, useState } from "react";
import { useDebouncedValue } from "./use-debounced-value";
export interface HfTokenValidationState {
@@ -17,6 +17,20 @@ const INITIAL: HfTokenValidationState = {
isChecking: false,
};
+interface CompletedValidation extends HfTokenValidationState {
+ token: string;
+}
+
+const NO_COMPLETED_VALIDATION: CompletedValidation = {
+ ...INITIAL,
+ token: "",
+};
+
+// Current user access tokens contain 34 characters after the hf_ prefix.
+// Action-time validation still accepts legacy shapes without spending quota
+// on every intermediate value typed into a live form field.
+const COMPLETE_HF_TOKEN = /^hf_[A-Za-z0-9]{34}$/;
+
/**
* Validates the HF token via the whoami-v2 API, debounced to avoid excessive
* requests while typing. isValid is null until checked.
@@ -26,39 +40,73 @@ export function useHfTokenValidation(token: string): HfTokenValidationState {
token.trim().replace(/^["']+|["']+$/g, ""),
500,
);
- const [state, setState] = useState(INITIAL);
+ const [completed, setCompleted] = useState(
+ NO_COMPLETED_VALIDATION,
+ );
const versionRef = useRef(0);
-
- const runCheck = useCallback(async (t: string) => {
- if (!t) {
- setState({ isValid: null, error: null, isChecking: false });
- return;
- }
-
- const v = ++versionRef.current;
- setState((prev) => ({ ...prev, isChecking: true, error: null }));
-
- try {
- await whoAmI({ accessToken: t });
- if (versionRef.current !== v) return;
- setState({ isValid: true, error: null, isChecking: false });
- } catch {
- if (versionRef.current !== v) return;
- setState({
- isValid: false,
- error: "invalid or expired token",
- isChecking: false,
- });
- }
- }, []);
+ const shouldValidate = COMPLETE_HF_TOKEN.test(debouncedToken);
useEffect(() => {
- if (!debouncedToken) {
- setState(INITIAL);
+ if (!shouldValidate) {
+ versionRef.current += 1;
return;
}
- runCheck(debouncedToken);
- }, [debouncedToken, runCheck]);
+ const version = ++versionRef.current;
+ void validateHfToken(debouncedToken).then(
+ (result) => {
+ if (versionRef.current !== version) return;
+ if (result.status === "valid") {
+ setCompleted({
+ token: debouncedToken,
+ isValid: true,
+ error: null,
+ isChecking: false,
+ });
+ } else if (result.status === "invalid") {
+ setCompleted({
+ token: debouncedToken,
+ isValid: false,
+ error: "invalid or expired token",
+ isChecking: false,
+ });
+ } else if (result.status === "rate_limited") {
+ const wait = result.retryAfterSeconds
+ ? ` Try again in about ${Math.ceil(result.retryAfterSeconds / 60)} minute(s).`
+ : " Try again later.";
+ setCompleted({
+ token: debouncedToken,
+ isValid: null,
+ error: `Token verification is rate limited.${wait}`,
+ isChecking: false,
+ });
+ } else {
+ setCompleted({
+ token: debouncedToken,
+ isValid: null,
+ error: "Could not verify the token. Check your connection and try again.",
+ isChecking: false,
+ });
+ }
+ },
+ () => {
+ if (versionRef.current !== version) return;
+ setCompleted({
+ token: debouncedToken,
+ isValid: null,
+ error: "Could not verify the token. Check your connection and try again.",
+ isChecking: false,
+ });
+ },
+ );
+ }, [debouncedToken, shouldValidate]);
- return state;
+ if (!shouldValidate) return INITIAL;
+ if (completed.token !== debouncedToken) {
+ return { isValid: null, error: null, isChecking: true };
+ }
+ return {
+ isValid: completed.isValid,
+ error: completed.error,
+ isChecking: false,
+ };
}
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index de8ac17c29..3e1c1e5a76 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -99,6 +99,7 @@ export const en = {
chat: "Chat",
voice: "Voice",
connections: "Connections",
+ data: "Data",
apiKeys: "API",
about: "About",
},
@@ -232,6 +233,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",
@@ -254,7 +258,7 @@ export const en = {
},
permissions: {
sectionTitle: "Permissions",
- bypassLabel: "Bypass permissions",
+ bypassLabel: "Tool permissions",
bypassDescription:
"How Unsloth approves chat tool calls (terminal, python, web, MCP) before they run. Full access disables approvals and the code sandbox.",
},
@@ -508,7 +512,7 @@ export const en = {
},
chat: {
title: "Chat",
- description: "Manage chat history stored on this device.",
+ description: "Customize how chat behaves on this device.",
modelDisclaimer: "Show model disclaimer",
modelDisclaimerDescription:
'Show "LLMs can make mistakes" under the chat box.',
@@ -577,6 +581,53 @@ export const en = {
"A storage clear failed; {count} chats may remain. Please retry.",
failedToClearChats: "Failed to clear chats",
},
+ data: {
+ title: "Data",
+ description:
+ "Manage chat history and uploaded files stored on this device.",
+ archivedChats: "Archived chats",
+ archivedChatsDescription: "View and manage chats you have archived.",
+ manageAction: "Manage",
+ exportArchivedChats: "Export",
+ exportingArchivedChats: "Exporting...",
+ exportedOneArchivedChat: "Exported 1 archived chat",
+ exportedArchivedChatCount: "Exported {count} archived chats",
+ noArchivedChatsToExport: "No archived chats to export.",
+ failedToExportArchivedChats: "Failed to export archived chats",
+ archiveAllChats: "Archive all chats",
+ archiveAllChatsDescription:
+ "Move every chat in Recents and Projects to the archive.",
+ noChatsToArchive: "No chats to archive.",
+ archiveAllAction: "Archive all",
+ archivingAction: "Archiving...",
+ archiveAllChatsTitle: "Archive all chats?",
+ archiveAllChatsConfirmDescription:
+ "Moves every chat on this device to the archive. Archived chats stay available and can be unarchived at any time.",
+ archivedAllChats: "Archived all chats",
+ archivedOneChat: "Archived 1 chat",
+ archivedChatCount: "Archived {count} chats",
+ failedToArchiveChats: "Failed to archive chats",
+ confirmBeforeDeleting: "Confirm before deleting",
+ confirmBeforeDeletingDescription:
+ "Ask for confirmation before a chat is deleted. Turn off to delete instantly.",
+ filesSection: "Files",
+ uploadedFiles: "Uploaded files",
+ uploadedFilesDescription:
+ "View and manage files uploaded to chats, projects, and knowledge bases.",
+ fineTuneExport: "Use chats as training data",
+ fineTuneExportDescription:
+ "Create a fine-tuning JSONL dataset from your chats. Load it in Train, refine in Recipes, or export it.",
+ fineTuneExportAction: "Export JSONL",
+ fineTuneRunAction: "Run",
+ fineTuneExportingAction: "Exporting...",
+ fineTuneOpenRecipesAction: "Open in Recipes",
+ fineTuneOpeningRecipesAction: "Opening...",
+ fineTuneTrainAction: "Load in Train tab",
+ fineTuneTrainingAction: "Loading...",
+ fineTuneExportFailed: "Failed to export training data",
+ fineTuneRecipeFailed: "Failed to open chats in Recipes",
+ fineTuneTrainFailed: "Failed to load dataset in the Train tab",
+ },
connections: {
title: "Connections",
description: "Manage providers and external connections.",
diff --git a/studio/frontend/src/index.css b/studio/frontend/src/index.css
index a43e3d974b..b06dc03fba 100644
--- a/studio/frontend/src/index.css
+++ b/studio/frontend/src/index.css
@@ -1532,12 +1532,12 @@ html[data-chat-font] .aui-root {
}
/* Hovering an active pill swaps the icon for an X (click to turn off). */
- .composer-pill-btn[data-active="true"]:hover .composer-pill-glyph > :not(.composer-pill-x),
+ .composer-pill-btn[data-active="true"]:has(.composer-pill-x):hover .composer-pill-glyph > :not(.composer-pill-x),
.unsloth-thinking-pill[data-active="true"]:hover .composer-pill-glyph > :not(.composer-pill-x) {
@apply opacity-0;
}
- .composer-pill-btn[data-active="true"]:hover .composer-pill-x,
+ .composer-pill-btn[data-active="true"]:has(.composer-pill-x):hover .composer-pill-x,
.unsloth-thinking-pill[data-active="true"]:hover .composer-pill-x {
@apply opacity-100;
}
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/install_python_stack.py b/studio/install_python_stack.py
index 95c9356d4a..9921b83543 100644
--- a/studio/install_python_stack.py
+++ b/studio/install_python_stack.py
@@ -44,11 +44,10 @@ IS_MAC_INTEL = IS_MACOS and platform.machine() == "x86_64"
IS_MAC_ARM = IS_MACOS and platform.machine() == "arm64"
IS_LINUX = sys.platform.startswith("linux")
-# DiskPart-prompt suppression: amd-smi auto-elevates on Windows, popping a
-# UAC/DiskPart prompt mid-install. This installer only spawns probes and pip/uv
-# (none need elevation), so set __COMPAT_LAYER=RunAsInvoker process-wide -- every
-# amd-smi subprocess then runs un-elevated, no per-call guard needed. setup.ps1
-# keeps per-call guards since it ALSO spawns winget installers that need elevation.
+# amd-smi auto-elevates on Windows (UAC/DiskPart prompt mid-install). This installer
+# only spawns probes and pip/uv (no elevation), so set __COMPAT_LAYER=RunAsInvoker
+# process-wide; amd-smi then runs un-elevated. setup.ps1 keeps per-call guards (it
+# also spawns winget installers that need elevation).
if IS_WINDOWS:
os.environ.setdefault("__COMPAT_LAYER", "RunAsInvoker")
# torchcodec ships wheels only for manylinux_2_28_x86_64, macosx_12_0_arm64,
@@ -74,6 +73,14 @@ _ROCM_TORCH_INDEX: dict[tuple[int, int], str] = {
(6, 0): "rocm6.0",
}
+# AMD per-arch leaves needing the torch 2.11 floor (the _grouped_mm <2.11 bug).
+# Mirrors *FloorMap in install.ps1 / setup.ps1; other arches ship <2.11 and stay bare.
+_ROCM_GFX_TORCH211_LEAVES: frozenset[str] = frozenset({"gfx120x-all", "gfx1151", "gfx1150"})
+
+# pytorch.org rocmX.Y indexes KNOWN to ship torch 2.11 (rocm7.2 only today); don't
+# floor an unknown newer rocm speculatively. Match install.sh / setup.ps1 / install.ps1.
+_ROCM_KNOWN_TORCH211_VERSIONS: frozenset[tuple[int, int]] = frozenset({(7, 2)})
+
# Per-tag pip specs; rocm7.2 ships torch 2.11.0 (older tags cap at 2.10.x).
_ROCM_TORCH_PKG_SPECS: dict[str, tuple[str, str, str]] = {
"rocm7.2": (
@@ -81,18 +88,16 @@ _ROCM_TORCH_PKG_SPECS: dict[str, tuple[str, str, str]] = {
"torchvision>=0.26.0,<0.27.0",
"torchaudio>=2.11.0,<2.12.0",
),
- # Default for rocm7.1 and earlier: torch 2.x below 2.11
+ # rocm7.1 and earlier: torch 2.x below 2.11
"_default": (
"torch>=2.4,<2.11.0",
"torchvision>=0.19,<0.26.0",
"torchaudio>=2.4,<2.11.0",
),
}
-# Windows AMD per-arch companion pins for the repo.amd.com index, mirroring the
-# install.ps1 / setup.ps1 floor maps (gfx120X and Strix Halo/Point use the rocm7.2
-# torch 2.11 trio). Pinning the companions keeps AMD's per-arch index -- which
-# publishes each independently -- from resolving an ABI-mismatched one. Unlisted
-# arches have no published floor, so stay bare. Bump with the PS maps at 2.12.x.
+# Windows AMD per-arch companion pins for the repo.amd.com index (mirrors the install.ps1 /
+# setup.ps1 floor maps): pinning stops the per-arch index (each published independently) from
+# resolving an ABI-mismatched companion. Unlisted arches have no floor, so stay bare.
_WINDOWS_ROCM_TORCH_PKG_SPECS: dict[str, tuple[str, str, str]] = {
"gfx1201": _ROCM_TORCH_PKG_SPECS["rocm7.2"],
"gfx1200": _ROCM_TORCH_PKG_SPECS["rocm7.2"],
@@ -103,19 +108,79 @@ _PYTORCH_WHL_BASE = (
os.environ.get("UNSLOTH_PYTORCH_MIRROR") or "https://download.pytorch.org/whl"
).rstrip("/")
-# CUDA torch repair specs (see _ensure_cuda_torch). torch 2.11 is allowed: its
-# torchao 0.17 cpp kernels load cleanly (0.16 crashes on cu130), and the flash-attn
-# / causal-conv1d / mamba torch2.10 wheels load and pass their upstream suites on
-# 2.11 (see wheel_utils._PREBUILT_WHEEL_TORCH_MM). torchvision/torchaudio are pinned
-# (not bare) because the install uses an exclusive --index-url (no PyPI fallback), so
-# a bare name could resolve one built against a different torch major (e.g. 0.27 for
-# torch 2.12) and fail at runtime with an ABI mismatch.
+
+def _strip_index_url_credentials(url: str) -> str:
+ """Strip userinfo (user:password@) AND query/fragment from a wheel index URL.
+
+ An authenticated pin must not leak credentials in printed output; query/fragment
+ may hold tokens and aren't part of the PEP 503 index identity. Host/path stay
+ exact. MUST match install.sh / setup.ps1 / install.ps1.
+ """
+ scheme, sep, rest = url.partition("://")
+ if not sep:
+ return url
+ rest = rest.split("?", 1)[0].split("#", 1)[0] # drop query / fragment
+ authority, slash, tail = rest.partition("/")
+ host = authority.rpartition("@")[2] # drop user:pass@ userinfo
+ return f"{scheme}://{host}{slash}{tail}"
+
+
+_URL_USERINFO_RE = re.compile(r"(https?://)[^/@\s`]+@")
+_URL_QUERY_VALUE_RE = re.compile(r"([?&][^=\s&`]+)=[^\s`]+")
+# URL-anchored so a bare "#..." (a shell comment in tool output) is never touched.
+_URL_FRAGMENT_RE = re.compile(r"(https?://[^\s`#]+)#[^\s`]+")
+
+
+def _redact_install_output(output: "bytes | str") -> str:
+ """Redact index-URL credentials (userinfo + query values + fragments) from captured
+ installer output before printing. uv/pip failure text embeds the failing --index-url
+ verbatim, which can carry a user:token@, ?token= or #token= secret. MUST match
+ install.sh / setup.ps1 / install.ps1's output sanitizers."""
+ text = output.decode(errors = "replace") if isinstance(output, bytes) else output
+ text = _URL_USERINFO_RE.sub(r"\1@", text)
+ text = _URL_QUERY_VALUE_RE.sub(r"\1=", text)
+ return _URL_FRAGMENT_RE.sub(r"\1#", text)
+
+
+def _trim_index_path_slashes(url: str) -> str:
+ """Trim trailing slashes from the URL PATH only, preserving ?query / #fragment. A
+ whole-URL rstrip("/") corrupts a token that ends in "/" (e.g. base64 ...abc/) and a
+ single-slash strip leaves .../cu128// classifying as an empty leaf. MUST match
+ install.sh / setup.ps1 / install.ps1."""
+ value = url.strip()
+ match = re.fullmatch(r"([^?#]*)([?#].*)?", value)
+ if match is None:
+ return value.rstrip("/")
+ return match.group(1).rstrip("/") + (match.group(2) or "")
+
+
+def _torch_index_leaf(url: str) -> str:
+ """Final URL path segment, lowercased, query/fragment removed first.
+
+ So a token-authenticated pin (.../cu128?token=x) classifies as cu128 (a raw leaf
+ keeps the query, never equals the +cu128 tag, and force-reinstalls every update).
+ CLASSIFICATION only; the install keeps the full URL. MUST match install.sh /
+ setup.ps1 / install.ps1.
+ """
+ path = url.split("?", 1)[0].split("#", 1)[0]
+ return path.rstrip("/").rsplit("/", 1)[-1].lower()
+
+
+# CUDA torch repair specs (see _ensure_cuda_torch). torch 2.11 is allowed (torchao
+# 0.17 cpp loads cleanly, and the flash-attn/causal-conv1d/mamba wheels pass on 2.11).
+# torchvision/torchaudio are pinned (not bare) so the exclusive --index-url can't
+# resolve one built against a different torch major -> ABI mismatch.
_CUDA_TORCH_PKG_SPEC: tuple[str, str, str] = (
"torch>=2.4,<2.12.0",
"torchvision>=0.19,<0.27.0",
"torchaudio>=2.4,<2.12.0",
)
+# CPU torch repair specs (see _ensure_cpu_torch). Same bounds/reasoning as CUDA: the
+# /cpu index also serves newer torch, so a bare trio could resolve out of range or ABI-
+# mismatched.
+_CPU_TORCH_PKG_SPEC: tuple[str, str, str] = _CUDA_TORCH_PKG_SPEC
+
# torchao's cpp extensions are pinned to ONE torch release AND CUDA major. A torch
# mismatch just skips the cpp kernels (slow Python fallback); a CUDA mismatch fails
# to import ("libcudart.so.12: cannot open shared object file"). The torch pin is a
@@ -408,9 +473,8 @@ def _detect_rocm_version() -> tuple[int, int] | None:
try:
with open(path) as fh:
parts = fh.read().strip().split("-")[0].split(".")
- # Explicit length guard so we don't rely on the broad except
- # below to swallow IndexError when the version file has a
- # single component (e.g. "6\n" on a partial install).
+ # Explicit length guard: don't rely on the broad except below to
+ # swallow IndexError on a single-component version (e.g. "6\n").
if len(parts) >= 2:
return int(parts[0]), int(parts[1])
except Exception:
@@ -455,11 +519,10 @@ def _detect_rocm_version() -> tuple[int, int] | None:
except Exception:
pass
- # Distro package-manager fallbacks. Package-managed ROCm installs can
- # expose GPUs via rocminfo/amd-smi but lack /opt/rocm/.info/version and
- # hipconfig, so probe dpkg (Debian/Ubuntu) and rpm (RHEL/Fedora/SUSE)
- # for the rocm-core version. Matches install.sh::get_torch_index_url so
- # `unsloth studio update` behaves like a fresh `curl | sh` install.
+ # Distro package-manager fallbacks: package-managed ROCm can expose GPUs via
+ # rocminfo/amd-smi but lack /opt/rocm/.info/version and hipconfig, so probe
+ # dpkg (Debian/Ubuntu) and rpm (RHEL/Fedora/SUSE) for the rocm-core version.
+ # Matches install.sh::get_torch_index_url so `studio update` == fresh install.
for cmd in (
["dpkg-query", "-W", "-f=${Version}\n", "rocm-core"],
["rpm", "-q", "--qf", "%{VERSION}\n", "rocm-core"],
@@ -561,11 +624,10 @@ def _detect_windows_gfx_arch() -> str | None:
stderr = subprocess.DEVNULL,
timeout = 10,
)
- # Accept partial output even when hipinfo crashes (e.g. exit code
- # 0xC0000005 / STATUS_ACCESS_VIOLATION on some RDNA 4 hosts): if
- # gcnArchName is present in stdout the device was enumerated before
- # the crash, so the arch is trustworthy. Ignoring it causes a
- # silent CPU PyTorch fallback (issue #6043).
+ # Accept partial output even when hipinfo crashes (e.g. 0xC0000005 /
+ # STATUS_ACCESS_VIOLATION on some RDNA 4 hosts): a gcnArchName in stdout
+ # means the device was enumerated pre-crash, so the arch is trustworthy.
+ # Ignoring it causes a silent CPU PyTorch fallback (issue #6043).
text = result.stdout.decode(errors = "replace")
# findall gets every gcnArchName line so multi-GPU hosts are
# enumerable and HIP_VISIBLE_DEVICES selects correctly.
@@ -706,9 +768,8 @@ def _detect_bnb_rocm_dll_ver() -> str | None:
m = re.search(r"libbitsandbytes_rocm(\d+)\.dll", os.path.basename(dll))
if m:
all_vers.append(m.group(1))
- # Pick the highest numeric suffix so e.g. "713" wins over "72" when both
- # variants are present. Glob order is not guaranteed, so always sort
- # rather than stopping at the first match.
+ # Highest numeric suffix wins (e.g. "713" over "72"); glob order is not
+ # guaranteed, so sort rather than take the first match.
return max(all_vers, key = lambda v: int(v)) if all_vers else None
@@ -825,17 +886,14 @@ def _has_rocm_gpu() -> bool:
if result.returncode == 0 and result.stdout.strip():
if check_fn(result.stdout):
return True
- # sysfs KFD topology fallback (Linux only) -- matches install.sh's
- # runtime-only detection. On minimal package-managed installs (no
- # rocminfo / no amd-smi tools), the kernel exposes AMD GPUs via
- # /sys/class/kfd so `studio update` can still detect and repair.
+ # sysfs KFD topology fallback (Linux only) -- matches install.sh's runtime-only
+ # detection. On minimal package-managed installs (no rocminfo / amd-smi), the
+ # kernel exposes AMD GPUs via /sys/class/kfd so `studio update` can still repair.
#
- # Guard: reject any KFD node whose properties file reports a non-AMD
- # vendor. With the NVIDIA open kernel module (driver 560+), NVIDIA GPUs
- # can register KFD topology nodes with a non-zero gpu_id; those nodes
- # have vendor_id 4318 (0x10DE) rather than the AMD value 4098 (0x1002).
- # Without this check the fallback returns True on NVIDIA-only systems,
- # causing _ensure_rocm_torch to install ROCm wheels on NVIDIA hardware.
+ # Guard: reject any KFD node whose properties file reports a non-AMD vendor. The
+ # NVIDIA open kernel module (driver 560+) registers KFD nodes with a non-zero
+ # gpu_id and vendor_id 4318 (0x10DE), not the AMD 4098 (0x1002); without this
+ # check the fallback returns True on NVIDIA-only hosts, installing ROCm wheels.
if sys.platform != "win32":
try:
kfd_nodes = "/sys/class/kfd/kfd/topology/nodes"
@@ -849,12 +907,10 @@ def _has_rocm_gpu() -> bool:
continue
if not gpu_id or gpu_id == "0": # gpu_id 0 = CPU node
continue
- # Require AMD vendor_id 4098 (0x1002) in the properties file.
- # KFD properties files exist on every kernel that exposes
- # /sys/class/kfd, so absence of the file means we cannot
- # confirm AMD ownership -- skip the node rather than risk a
- # false positive (e.g. NVIDIA open driver KFD nodes that
- # lack a properties file on some kernel versions).
+ # Require AMD vendor_id 4098 (0x1002). KFD properties files exist
+ # on every kernel exposing /sys/class/kfd, so a missing file means
+ # AMD ownership is unconfirmed -- skip the node rather than risk a
+ # false positive (e.g. NVIDIA open-driver KFD nodes lacking it).
props_path = os.path.join(kfd_nodes, entry, "properties")
try:
with open(props_path) as fh:
@@ -981,13 +1037,10 @@ def _install_bnb_windows_rocm() -> bool:
)
if not _ok:
return False
- # After install: detect the actual ROCm DLL suffix shipped in the wheel and
- # set BNB_ROCM_VERSION so bitsandbytes loads the correct DLL regardless of
- # what torch.version.hip reports. The wheel may ship an older suffix (e.g.
- # "72") while torch reports a newer HIP version (e.g. 7.13); the env var
- # override ensures bitsandbytes does not fail looking for a non-existent DLL.
- # The worker subprocess inherits this env var automatically.
- # Fall back to "72" if detection fails (e.g. install was a no-op / dry-run).
+ # Detect the actual ROCm DLL suffix in the wheel and set BNB_ROCM_VERSION so bnb
+ # loads the right DLL regardless of torch.version.hip (the wheel may ship "72"
+ # while torch reports 7.13). The worker subprocess inherits it; fall back to "72"
+ # if detection fails (e.g. a no-op / dry-run install).
_env_ver = os.environ.get("BNB_ROCM_VERSION")
_env_is_persisted_default = (
os.environ.get(_BNB_ROCM_VERSION_SOURCE_ENV) == _BNB_ROCM_VERSION_SOURCE_SITECUSTOMIZE
@@ -1002,13 +1055,11 @@ def _install_bnb_windows_rocm() -> bool:
_persist_detected_version = True
if _persist_detected_version:
_persist_bnb_rocm_version(_ver)
- # Make hipInfo.exe (shipped into the venv Scripts dir by the AMD torch
- # wheel) resolvable via PATH for this process and every child python the
- # installer spawns (import checks, precompile): bitsandbytes runs
- # `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
- # Unsloth nor the installer's child processes ever do.
+ # Make hipInfo.exe (shipped into venv Scripts by the AMD torch wheel) resolvable
+ # via PATH for this process and every child python (import checks, precompile):
+ # bitsandbytes runs hipinfo.exe at import to detect the GPU arch and logs a scary
+ # (harmless) ERROR + WARNING when it is missing. Scripts is on PATH only for an
+ # activated venv, which neither Unsloth nor the installer's children 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"
@@ -1020,13 +1071,18 @@ def _install_bnb_windows_rocm() -> bool:
def _detect_cuda_torch_index_url() -> str:
"""Return the pytorch.org CUDA wheel index URL for the host's NVIDIA driver.
- Mirrors install.sh::get_torch_index_url's CUDA ladder so `studio update`
- repairs to the same wheel family a fresh `curl | sh` install would pick.
- Probes nvidia-smi (PATH, then /usr/bin/nvidia-smi) and parses both the
- legacy "CUDA Version:" and the newer "CUDA UMD Version:" spellings.
- Defaults to cu126 when nvidia-smi is missing or the version is unreadable
- (e.g. NVIDIA detected only via the /proc/driver/nvidia/gpus fallback).
+ Mirrors install.sh::get_torch_index_url's CUDA ladder so `studio update` repairs
+ to the same wheel family a fresh install would pick. Honours the explicit
+ overrides first (UNSLOTH_TORCH_INDEX_URL / _FAMILY) so a headless / CI install
+ never lets the host GPU decide. Otherwise probes nvidia-smi (parsing both "CUDA
+ Version:" and "CUDA UMD Version:"), defaulting to cu126 when unreadable.
"""
+ _override_url = os.environ.get("UNSLOTH_TORCH_INDEX_URL", "").strip()
+ if _override_url:
+ return _trim_index_path_slashes(_override_url)
+ _override_family = os.environ.get("UNSLOTH_TORCH_INDEX_FAMILY", "").strip()
+ if _override_family:
+ return f"{_PYTORCH_WHL_BASE}/{_override_family.strip('/')}"
exe = shutil.which("nvidia-smi")
if not exe and os.path.isfile("/usr/bin/nvidia-smi"):
exe = "/usr/bin/nvidia-smi"
@@ -1061,6 +1117,157 @@ def _detect_cuda_torch_index_url() -> str:
return f"{_PYTORCH_WHL_BASE}/{tag}"
+def _explicit_torch_index_url() -> "str | None":
+ """The wheel index URL pinned via UNSLOTH_TORCH_INDEX_URL / _FAMILY, else None.
+
+ Lets the CUDA/ROCm repair helpers honour the exact pinned family/URL instead
+ of re-probing the GPU. Mirrors install.sh::get_torch_index_url's override.
+ """
+ url = os.environ.get("UNSLOTH_TORCH_INDEX_URL", "").strip()
+ if url:
+ return _trim_index_path_slashes(url)
+ family = os.environ.get("UNSLOTH_TORCH_INDEX_FAMILY", "").strip()
+ if family:
+ return f"{_PYTORCH_WHL_BASE}/{family.strip('/')}"
+ return None
+
+
+def _is_pip_rocm_family_leaf(leaf: str) -> bool:
+ """True when a lowercased leaf names a pip --index-url ROCm family: an EXACT
+ rocm[.] leaf or a gfx leaf. A suffixed leaf (rocm-rel-7.2.1,
+ rocm7.2-private) starts with "rocm" but is a custom pin the verbatim path owns, so
+ match EXACTLY. Mirrors install.sh / setup.ps1.
+ """
+ # gfx must be followed by a digit (gfx90a, gfx1151, gfx120X-all): a gfx-prefixed
+ # custom leaf (gfx-private) is a verbatim pin, like rocm7.2-private.
+ return bool(re.fullmatch(r"rocm\d+(?:\.\d+)?", leaf)) or bool(re.match(r"gfx\d", leaf))
+
+
+def _explicit_rocm_torch_index_url() -> "str | None":
+ """The pinned wheel index URL when it names a pip ROCm family (rocm/gfx*), else None."""
+ url = _explicit_torch_index_url()
+ if url is None:
+ return None
+ return url if _is_pip_rocm_family_leaf(_torch_index_leaf(url)) else None
+
+
+def _rocm_pin_family_mismatch(pin_url: str, installed_ver: str) -> bool:
+ """True when an explicit ROCm pin names a different ROCm family than the installed
+ ROCm torch, so the pin needs a reinstall. Mirrors setup.ps1's stale-venv comparison;
+ same three pin-leaf cases as _ensure_rocm_torch. A same-family pin is NOT a mismatch.
+ """
+ leaf = _torch_index_leaf(pin_url)
+ # Pinned ROCm version. The family classifier accepts a major-only rocm leaf too,
+ # so parse the minor as optional; a major-only pin compares on the major alone.
+ _pin_rocm = re.match(r"^rocm(\d+)(?:\.(\d+))?", leaf)
+ _pin_major = int(_pin_rocm.group(1)) if _pin_rocm else None
+ _pin_ver = (
+ (int(_pin_rocm.group(1)), int(_pin_rocm.group(2)))
+ if _pin_rocm and _pin_rocm.group(2) is not None
+ else None
+ )
+ # Installed +rocmX.Y version; a THREE-part +rocmA.B.C tag is the AMD per-arch
+ # (repo.amd.com/gfx*) signature vs a two-part pytorch.org wheel.
+ _inst_rocm = re.search(r"\+rocm(\d+)\.(\d+)", installed_ver)
+ _inst_ver = (int(_inst_rocm.group(1)), int(_inst_rocm.group(2))) if _inst_rocm else None
+ _inst_is_perarch = re.search(r"\+rocm\d+\.\d+\.\d+", installed_ver) is not None
+ # A ROCm build MUST carry a +rocm tag; an untagged wheel never satisfies a ROCm pin.
+ _inst_has_rocm = re.search(r"\+rocm", installed_ver) is not None
+ # Installed torch RELEASE (before "+") is 2.11+.
+ _inst_rel = re.match(r"^(\d+)\.(\d+)", installed_ver)
+ _inst_is_211 = (
+ (int(_inst_rel.group(1)), int(_inst_rel.group(2))) >= (2, 11) if _inst_rel else False
+ )
+
+ if leaf.startswith("gfx"):
+ # 2.11-allowlist arches expect the AMD per-arch wheel (three-part +rocmA.B.C,
+ # torch 2.11+); a generic or pre-2.11 build is a mismatch.
+ if leaf in _ROCM_GFX_TORCH211_LEAVES:
+ return not (_inst_is_211 and _inst_is_perarch)
+ # Non-2.11 gfx leaf (<2.11 specs): mismatch on an untagged wheel or torch 2.11+.
+ return (not _inst_has_rocm) or _inst_is_211
+
+ # Major-only rocm pin (rocm7): compare majors only -- a +rocm6.4 wheel under a rocm7
+ # pin is a mismatch, any +rocm7.x wheel satisfies it (there is no pinned minor to
+ # compare, and the 2.11-line fallback below would invert both verdicts).
+ if _pin_major is not None and _pin_ver is None:
+ if _inst_ver is not None:
+ return _inst_ver[0] != _pin_major
+ # Untagged wheel never satisfies a ROCm pin; a +rocm tag with an unreadable
+ # version is accepted (matches the lenient unreadable fallback below).
+ return not _inst_has_rocm
+
+ # rocmX.Y pin. Only KNOWN-2.11 rocm is the 2.11 line (no speculative floor).
+ _pin_is_211 = _pin_ver in _ROCM_KNOWN_TORCH211_VERSIONS if _pin_ver is not None else False
+ if _pin_ver is not None and _inst_ver is not None:
+ # Both readable: exact (major, minor) compare (rocm7.2 pin over +rocm7.13.x ->
+ # mismatch, reinstall the pinned wheel).
+ if _pin_ver != _inst_ver:
+ return True
+ # Same family: a KNOWN-2.11 pin whose release drifted off 2.11 (2.12+rocm7.2)
+ # violates the spec -> reinstall to floor (exact compare, not >=2.11).
+ if _pin_is_211 and _inst_rel is not None:
+ if (int(_inst_rel.group(1)), int(_inst_rel.group(2))) != (2, 11):
+ return True
+ return False
+ # rocm pin, unreadable installed version: compare on the 2.11 line, but an untagged
+ # wheel never satisfies a rocmX.Y pin -> mismatch.
+ if not _inst_has_rocm:
+ return True
+ return _pin_is_211 != _inst_is_211
+
+
+def _explicit_cpu_torch_index_url() -> "str | None":
+ """The pinned wheel index URL when it names the CPU family (leaf == cpu), else None.
+
+ An explicit CPU pin (UNSLOTH_TORCH_INDEX_FAMILY=cpu or a URL ending in /cpu)
+ is authoritative -- see _ensure_cpu_torch.
+ """
+ url = _explicit_torch_index_url()
+ if url is None:
+ return None
+ return url if _torch_index_leaf(url) == "cpu" else None
+
+
+def _is_cuda_family_leaf(leaf: str) -> bool:
+ """True only for a real CUDA wheel-family leaf: "cu" + digits (cu118, cu128, ...).
+
+ A bare startswith("cu") would match "custom"/"current". The match is EXACT so
+ "cu128-private" is NOT a family leaf and routes to the verbatim path instead.
+ """
+ return re.fullmatch(r"cu[0-9]+", leaf) is not None
+
+
+def _explicit_cuda_torch_index_url() -> "str | None":
+ """The pinned wheel index URL when it names a CUDA family (leaf cuXXX), else None.
+
+ Mirrors _explicit_rocm/cpu_torch_index_url so _ensure_cuda_torch only treats a
+ *CUDA* pin as authority to override the NVIDIA-presence gate (an arbitrary mirror
+ or a ROCm/CPU pin must not force a CUDA reinstall on a non-NVIDIA host).
+ """
+ url = _explicit_torch_index_url()
+ if url is None:
+ return None
+ return url if _is_cuda_family_leaf(_torch_index_leaf(url)) else None
+
+
+def _explicit_unknown_family_torch_index_url() -> "str | None":
+ """The pinned index URL when its leaf names NO known torch family, else None.
+
+ Known = rocm* / gfx* / cpu / cuXXX. Anything else (a private mirror /simple,
+ /current) is UNKNOWN: version-tag heuristics can't judge it, so the family
+ repair helpers must leave it alone (the install applied it verbatim).
+ Matches install.sh / setup.ps1 / install.ps1.
+ """
+ url = _explicit_torch_index_url()
+ if url is None:
+ return None
+ leaf = _torch_index_leaf(url)
+ if _is_pip_rocm_family_leaf(leaf) or leaf == "cpu" or _is_cuda_family_leaf(leaf):
+ return None
+ return url
+
+
def _ensure_cuda_torch() -> None:
"""Repair a venv whose torch is a ROCm build on an NVIDIA host.
@@ -1073,44 +1280,47 @@ def _ensure_cuda_torch() -> None:
Only repairs when torch actually links against HIP/ROCm. Healthy CUDA
torch and deliberate CPU-only torch are left untouched.
"""
- # Respect an explicit backend choice from install.sh: only "" (standalone
- # `studio update`) or "cuda" should ever force CUDA wheels. "rocm"/"cpu"
- # (or any unrecognised value) are deliberate and must not be overridden.
+ # Respect install.sh's backend: only "" (standalone update) or "cuda" force CUDA
+ # wheels; "rocm"/"cpu"/unrecognised are deliberate.
if _TORCH_BACKEND not in ("", "cuda"):
return
- # No CUDA torch on macOS; Windows venv/torch lifecycle is owned by
- # install.ps1 (and the KFD poisoning bug is Linux-only), so skip both.
+ # An explicit unknown-family pin was applied VERBATIM at install time; leave it alone.
+ if _explicit_unknown_family_torch_index_url() is not None:
+ return
+ # No CUDA torch on macOS; Windows torch is owned by install.ps1 (KFD bug is Linux-only).
if IS_MACOS or IS_WINDOWS or NO_TORCH:
return
# Never undo a deliberate ROCm install (setup.ps1 sets this marker).
if os.environ.get("UNSLOTH_ROCM_TORCH_INSTALLED") == "1":
return
- # CUDA_VISIBLE_DEVICES="" / "-1" deliberately hides the NVIDIA GPU (for
- # example a mixed AMD+NVIDIA host that runs ROCm torch on the AMD card);
- # never force CUDA wheels over that choice.
+ # An explicit CUDA pin (headless / CI cross-install) commits to CUDA wheels and skips ALL
+ # GPU probing, so it clears both the CUDA_VISIBLE_DEVICES hide gate and the NVIDIA gate below.
+ _cuda_pinned = _explicit_cuda_torch_index_url() is not None
+ # CUDA_VISIBLE_DEVICES="" / "-1" deliberately hides the NVIDIA GPU; never force CUDA
+ # wheels over that unless a CUDA index is pinned.
_cvd = os.environ.get("CUDA_VISIBLE_DEVICES")
- if _cvd is not None and _cvd.strip() in ("", "-1"):
+ if not _cuda_pinned and _cvd is not None and _cvd.strip() in ("", "-1"):
return
- # Only NVIDIA hosts should carry CUDA torch. _has_usable_nvidia_gpu()
- # covers the /proc/driver/nvidia/gpus fallback when nvidia-smi is absent.
- if not _has_usable_nvidia_gpu():
+ # Only NVIDIA hosts carry CUDA torch (the CUDA pin overrides this gate too).
+ if not _cuda_pinned and not _has_usable_nvidia_gpu():
return
- # Classify the installed torch: "hip" (ROCm build -- the poisoning
- # signature), "cuda" (healthy), or "cpu" (deliberate CPU wheel). A
- # non-zero exit means torch is missing or un-importable; the base install
- # step handles that, so leave it alone.
+ # Classify the installed torch: "hip" (ROCm poisoning signature), "cuda" (healthy),
+ # or "cpu". A non-zero exit means torch is missing/un-importable: without a pin the
+ # base install owns it, but a pinned CUDA index reinstalls it below.
try:
probe = subprocess.run(
[
sys.executable,
"-c",
(
- "import torch; "
+ "import torch, re; "
"hip = getattr(torch.version, 'hip', '') or ''; "
"cuda = getattr(torch.version, 'cuda', '') or ''; "
"ver = getattr(torch, '__version__', '').lower(); "
- "print('hip' if (hip or 'rocm' in ver) else ('cuda' if cuda else 'cpu'))"
+ "m = re.search(r'\\+(cu\\d+)', ver); "
+ "marker = 'hip' if (hip or 'rocm' in ver) else ('cuda' if cuda else 'cpu'); "
+ "print(marker + '|' + (m.group(1) if m else ''))"
),
],
stdout = subprocess.PIPE,
@@ -1120,22 +1330,60 @@ def _ensure_cuda_torch() -> None:
except (OSError, subprocess.TimeoutExpired):
return
if probe.returncode != 0:
+ # torch present but can't import. Without a pin the base install owns it; but an
+ # explicit CUDA pin forces this pass (failed probe) and the base update won't
+ # reinstall an already-installed torch, so reinstall from the pin (self-resolving).
+ if not _cuda_pinned:
+ return
+ index_url = _detect_cuda_torch_index_url()
+ _torch_pkg, _vision_pkg, _audio_pkg = _CUDA_TORCH_PKG_SPEC
+ print(
+ f" torch cannot import but an explicit CUDA index is pinned -- reinstalling "
+ f"CUDA torch from {_strip_index_url_credentials(index_url)}"
+ )
+ pip_install(
+ "CUDA torch repair",
+ "--force-reinstall",
+ "--no-cache-dir",
+ _torch_pkg,
+ _vision_pkg,
+ _audio_pkg,
+ "--index-url",
+ index_url,
+ constrain = False,
+ )
return
- # Take the last non-empty stdout line: stray output from sitecustomize or
- # an import hook must not mask the marker (fail-closed either way).
+ # Last non-empty line: stray sitecustomize/import-hook output must not mask the marker.
_marker_lines = [
line.strip() for line in probe.stdout.decode(errors = "replace").splitlines() if line.strip()
]
- if not _marker_lines or _marker_lines[-1] != "hip":
- return # healthy CUDA torch, or a deliberate CPU wheel -- leave as-is
+ if not _marker_lines:
+ return
+ _marker, _, _installed_cu = _marker_lines[-1].partition("|")
+ # Reinstall CUDA torch on a ROCm build on an NVIDIA host (poisoning signature), or when a
+ # CUDA index is pinned but the venv has the wrong family (CPU or a different cuXXX). A
+ # healthy match, or a CPU wheel with no CUDA pin, is left alone.
+ _pin = _explicit_torch_index_url()
+ _pin_leaf = _torch_index_leaf(_pin) if _pin else ""
+ _pinned_cuda = _is_cuda_family_leaf(_pin_leaf)
+ if _marker == "hip":
+ _why = "torch is a ROCm build on an NVIDIA host"
+ elif _marker == "cpu" and _pinned_cuda:
+ _why = "torch is a CPU build but an explicit CUDA index is pinned"
+ elif _marker == "cuda" and _pinned_cuda and _installed_cu != _pin_leaf:
+ # Installed cuXXX differs from the pin. An untagged build (empty) counts too:
+ # the family can't be confirmed, so reinstall to enforce it (idempotent).
+ _installed_desc = _installed_cu if _installed_cu else "an untagged CUDA build"
+ _why = f"torch is {_installed_desc} but the pinned CUDA index is {_pin_leaf}"
+ else:
+ return # healthy CUDA torch matching the pin, or a deliberate CPU wheel
index_url = _detect_cuda_torch_index_url()
_torch_pkg, _vision_pkg, _audio_pkg = _CUDA_TORCH_PKG_SPEC
print(
- f" torch is a ROCm build on an NVIDIA host -- reinstalling "
- f"CUDA torch from {index_url}\n"
- f" (set UNSLOTH_TORCH_BACKEND=rocm to keep a deliberate ROCm torch "
- f"on a mixed AMD+NVIDIA host)"
+ f" {_why} -- reinstalling CUDA torch from {_strip_index_url_credentials(index_url)}\n"
+ f" (set UNSLOTH_TORCH_BACKEND=rocm or cpu to keep a deliberate "
+ f"non-CUDA torch)"
)
pip_install(
"CUDA torch repair",
@@ -1150,6 +1398,90 @@ def _ensure_cuda_torch() -> None:
)
+def _ensure_cpu_torch() -> None:
+ """Reinstall CPU torch when an explicit CPU pin is set but the venv has a GPU build.
+
+ Counterpart to _ensure_cuda/rocm_torch for the explicit-CPU case (those treat a CPU
+ backend as a skip, so a standalone `studio update` would ignore the authoritative CPU
+ pin). Only fires for an EXPLICIT pin.
+ """
+ if NO_TORCH:
+ return
+ pin = _explicit_cpu_torch_index_url()
+ if pin is None:
+ return
+
+ # Classify the installed torch family. A non-zero exit means torch is missing or
+ # un-importable: the explicit CPU pin reinstalls it below.
+ try:
+ probe = subprocess.run(
+ [
+ sys.executable,
+ "-c",
+ (
+ "import torch, re; "
+ "hip = getattr(torch.version, 'hip', '') or ''; "
+ "cuda = getattr(torch.version, 'cuda', '') or ''; "
+ "ver = getattr(torch, '__version__', '').lower(); "
+ "gpu = bool(hip) or 'rocm' in ver or bool(cuda) or bool(re.search(r'\\+cu\\d+', ver)); "
+ "print('gpu' if gpu else 'cpu')"
+ ),
+ ],
+ stdout = subprocess.PIPE,
+ stderr = subprocess.DEVNULL,
+ timeout = 90,
+ )
+ except (OSError, subprocess.TimeoutExpired):
+ return
+ if probe.returncode != 0:
+ # torch present but can't import. The explicit CPU pin forces this pass (failed
+ # probe) and the base update won't reinstall an already-installed torch, so
+ # reinstall from the pin (self-resolving, no loop).
+ _torch_pkg, _vision_pkg, _audio_pkg = _CPU_TORCH_PKG_SPEC
+ print(
+ f" torch cannot import but an explicit CPU index is pinned -- reinstalling "
+ f"CPU torch from {_strip_index_url_credentials(pin)}"
+ )
+ pip_install(
+ "CPU torch repair",
+ "--force-reinstall",
+ "--no-cache-dir",
+ _torch_pkg,
+ _vision_pkg,
+ _audio_pkg,
+ "--index-url",
+ pin,
+ constrain = False,
+ )
+ return
+ _lines = [
+ line.strip() for line in probe.stdout.decode(errors = "replace").splitlines() if line.strip()
+ ]
+ if not _lines:
+ return # unreadable -- the base install step handles a missing torch
+ if _lines[-1] != "gpu":
+ return # already a CPU build
+
+ print(
+ " torch is a GPU build but an explicit CPU index is pinned -- reinstalling "
+ f"CPU torch from {_strip_index_url_credentials(pin)}"
+ )
+ # Pin the supported torch<2.11 family (the /cpu index now serves 2.11+, so a bare
+ # trio could resolve out of range or ABI-mismatched).
+ _torch_pkg, _vision_pkg, _audio_pkg = _CPU_TORCH_PKG_SPEC
+ pip_install(
+ "CPU torch repair",
+ "--force-reinstall",
+ "--no-cache-dir",
+ _torch_pkg,
+ _vision_pkg,
+ _audio_pkg,
+ "--index-url",
+ pin,
+ constrain = False,
+ )
+
+
def _ensure_rocm_torch() -> None:
"""Reinstall torch with ROCm wheels when the venv received CPU-only torch.
@@ -1160,16 +1492,15 @@ def _ensure_rocm_torch() -> None:
Uses pip_install() to respect uv, constraints, and --python targeting.
"""
global _rocm_windows_torch_installed
- # install.sh sets UNSLOTH_TORCH_BACKEND to the resolved wheel family
- # ("cuda", "rocm", "cpu"). Skip ROCm operations entirely when install.sh
- # already selected a non-ROCm backend -- this is the authoritative signal
- # and avoids re-running GPU detection in a subprocess that may see a
- # different environment (different PATH, CUDA_VISIBLE_DEVICES, etc.).
+ # install.sh's resolved backend is authoritative: skip ROCm when it already chose a
+ # non-ROCm family (avoids re-detecting in a subprocess that may see a different env).
if _TORCH_BACKEND in ("cuda", "cpu"):
return
- # setup.ps1 sets this after installing AMD wheels; skip the probe only when
- # torch is actually importable as ROCm. If the venv was wiped between runs,
- # the stale env-var would suppress a needed reinstall.
+ # An explicit unknown-family pin was applied VERBATIM at install time; leave it alone.
+ if _explicit_unknown_family_torch_index_url() is not None:
+ return
+ # setup.ps1 sets this after installing AMD wheels; skip only when torch is actually
+ # importable as ROCm (a wiped venv leaves a stale env-var that must not suppress it).
if os.environ.get("UNSLOTH_ROCM_TORCH_INSTALLED") == "1":
_torch_ok = False
try:
@@ -1193,9 +1524,8 @@ def _ensure_rocm_torch() -> None:
pass
if _torch_ok:
_rocm_windows_torch_installed = True
- # setup.ps1 already installed ROCm torch, but we still need the AMD
- # Windows BNB wheel here -- the PyPI bitsandbytes wheel ships only
- # CUDA DLLs and fails to load on ROCm.
+ # ROCm torch is already installed, but the AMD Windows BNB wheel is still
+ # needed (the PyPI bitsandbytes ships only CUDA DLLs, fails on ROCm).
_install_bnb_windows_rocm()
return
# torch was wiped between runs; fall through to the full install path
@@ -1203,10 +1533,15 @@ def _ensure_rocm_torch() -> None:
return
if IS_WINDOWS:
- if _has_usable_nvidia_gpu():
+ # An explicit ROCm-family pin commits to ROCm wheels regardless of the visible
+ # GPU and overrides the public per-arch index (mirrors the Linux pin handling
+ # below): after a pinned setup.ps1 install fails to CPU, this repair must retry
+ # the PINNED index, not repo.amd.com.
+ _win_rocm_pin = _explicit_rocm_torch_index_url()
+ if _win_rocm_pin is None and _has_usable_nvidia_gpu():
return
gfx_arch = _detect_windows_gfx_arch()
- if not gfx_arch:
+ if not gfx_arch and _win_rocm_pin is None:
return # no AMD GPU visible via hipinfo
# Probe whether torch already links against HIP.
_torch_already_rocm = False
@@ -1231,23 +1566,24 @@ def _ensure_rocm_torch() -> None:
except (OSError, subprocess.TimeoutExpired):
pass
if not _torch_already_rocm:
- index_url = _windows_rocm_index_url(gfx_arch)
+ index_url = _win_rocm_pin or _windows_rocm_index_url(gfx_arch)
if index_url is None:
print(f" No AMD Windows torch index for GPU arch {gfx_arch} -- skipping")
return
- print(f" {gfx_arch} (Windows) -- installing torch from {index_url}")
- # Pin companions for the arches install.ps1/setup.ps1 pin (gfx120X /
- # Strix) so the per-arch index resolves an ABI-consistent trio; other
- # arches stay bare (no published floor), matching the PowerShell side.
+ print(
+ f" {gfx_arch or 'pinned ROCm index'} (Windows) -- installing torch from "
+ f"{_strip_index_url_credentials(index_url)}"
+ )
+ # Pin companions for the arches install.ps1/setup.ps1 pin (gfx120X / Strix)
+ # so the per-arch index resolves an ABI-consistent trio; other arches stay bare.
_torch_pkg, _vision_pkg, _audio_pkg = _WINDOWS_ROCM_TORCH_PKG_SPECS.get(
gfx_arch, ("torch", "torchvision", "torchaudio")
)
- # Nonfatal: a transient AMD-index failure must not abort the whole
- # install once the PowerShell side has fallen back to CPU torch.
- # --force-reinstall resolves before uninstalling, so a failed index
- # leaves the existing build intact; keep it and let the user retry.
+ # Nonfatal: a transient AMD-index failure must not abort the install.
+ # --force-reinstall resolves before uninstalling, so a failed index keeps the
+ # existing build intact; let the user retry.
if not pip_install_try(
- f"ROCm torch (Windows, {gfx_arch})",
+ f"ROCm torch (Windows, {gfx_arch or 'pinned'})",
"--force-reinstall",
"--index-url",
index_url,
@@ -1257,7 +1593,7 @@ def _ensure_rocm_torch() -> None:
constrain = False,
):
print(
- f" Warning: AMD Windows ROCm torch install failed for {gfx_arch}; "
+ f" Warning: AMD Windows ROCm torch install failed for {gfx_arch or 'the pinned index'}; "
"keeping the existing torch build. Re-run 'unsloth studio update' "
"later to retry ROCm."
)
@@ -1280,26 +1616,30 @@ def _ensure_rocm_torch() -> None:
# ── Linux x86_64 only: PyTorch ROCm wheels are not published for aarch64 ──
if platform.machine().lower() not in {"x86_64", "amd64"}:
return
- # NVIDIA takes precedence on mixed hosts -- but only if a GPU is usable
- if _has_usable_nvidia_gpu():
- return
- # Use _has_rocm_gpu() (rocminfo / amd-smi GPU data rows) as the
- # authoritative "is this an AMD ROCm host?" signal. The old gate required
- # /opt/rocm or hipcc to exist, which breaks runtime-only ROCm installs
- # (minimal package-managed installs, Radeon software) that ship
- # amd-smi/rocminfo without /opt/rocm or hipcc, leaving `unsloth studio
- # update` unable to repair a CPU-only venv on those systems.
- if not _has_rocm_gpu():
- return # no AMD GPU visible
+ # An explicit ROCm pin commits to ROCm wheels regardless of the visible GPU (headless / CI).
+ # Mirror _ensure_cuda_torch: skip the NVIDIA/no-AMD/unreadable gates.
+ _rocm_pin = _explicit_rocm_torch_index_url()
+ if _rocm_pin is None:
+ # NVIDIA takes precedence on mixed hosts (only if a GPU is usable).
+ if _has_usable_nvidia_gpu():
+ return
+ # _has_rocm_gpu() (rocminfo / amd-smi rows) is the authoritative AMD-host signal;
+ # the old /opt/rocm-or-hipcc gate broke runtime-only ROCm installs.
+ if not _has_rocm_gpu():
+ return # no AMD GPU visible
ver = _detect_rocm_version()
if ver is None:
- print(" ROCm detected but version unreadable -- skipping torch reinstall")
- return
+ if _rocm_pin is None:
+ print(" ROCm detected but version unreadable -- skipping torch reinstall")
+ return
+ # Explicit pin: the pinned leaf drives the install, so an unreadable host version
+ # is fine (sentinel keeps ver comparisons defined).
+ ver = (0, 0)
- # Probe whether torch already links against HIP (ROCm already working).
- # Do NOT skip for CUDA-only builds: they are unusable on AMD-only hosts
- # (the NVIDIA check above already handled mixed AMD+NVIDIA setups).
+ # Probe whether torch links against HIP, capturing the installed ROCm tag for pin-mismatch
+ # detection. Emit ONE "|" line: marker (HIP version, "rocm" sentinel,
+ # or empty for CPU/CUDA) before "|", wheel version after.
try:
probe = subprocess.run(
[
@@ -1309,10 +1649,10 @@ def _ensure_rocm_torch() -> None:
"import torch; "
"hip=getattr(torch.version,'hip','') or ''; "
"ver=getattr(torch,'__version__','').lower(); "
- # Print the HIP version when present (back-compat), else a
- # "rocm" sentinel when only torch.__version__ flags ROCm
- # (AMD SDK / Radeon wheels). Empty string = CPU/CUDA.
- "print(hip if hip else ('rocm' if 'rocm' in ver else ''))"
+ # HIP version if present, else a "rocm" sentinel when only the
+ # version string flags ROCm; empty marker = CPU/CUDA torch.
+ "marker=hip if hip else ('rocm' if 'rocm' in ver else ''); "
+ "print(marker + '|' + ver)"
),
],
stdout = subprocess.PIPE,
@@ -1321,29 +1661,42 @@ def _ensure_rocm_torch() -> None:
)
except (OSError, subprocess.TimeoutExpired):
probe = None
- has_hip_torch = (
- probe is not None and probe.returncode == 0 and probe.stdout.decode().strip() != ""
+ # Last non-empty line, split on the FIRST "|" so the empty HIP field is preserved.
+ _marker_lines = (
+ [ln.strip() for ln in probe.stdout.decode(errors = "replace").splitlines() if ln.strip()]
+ if (probe is not None and probe.returncode == 0)
+ else []
+ )
+ _hip_marker, _sep, _installed_torch_ver = (
+ _marker_lines[-1].partition("|") if _marker_lines else ("", "", "")
+ )
+ # A "|"-delimited line is required; without it treat HIP as absent -> reinstall.
+ has_hip_torch = bool(_sep) and _hip_marker != ""
+
+ # An explicit ROCm pin whose family differs from the installed torch must reinstall, else a
+ # rocm7.2/gfx* pin over an older +rocm6.4/7.1 build never applies. Version-tag heuristic
+ # only: a same-tag per-arch switch (gfx1151 -> gfx120X-all, both +rocm7.13.0) isn't detectable.
+ _rocm_pin_mismatch = (
+ _rocm_pin_family_mismatch(_rocm_pin, _installed_torch_ver)
+ if (has_hip_torch and _rocm_pin is not None)
+ else False
)
- rocm_torch_ready = has_hip_torch
+ rocm_torch_ready = has_hip_torch and not _rocm_pin_mismatch
- # Strix Halo / Strix Point (gfx1151 / gfx1150) segfault under ROCm 7.1
- # in torch._grouped_mm. AMD's per-gfx repo ships torch 2.11.0+rocm7.13.0
- # with the real fix, so route those hosts there instead of the generic
- # pytorch.org rocm7.1 wheel. Mirrors install.sh's Strix override.
- # On mixed hosts (Strix iGPU + non-Strix dGPU), route to the AMD per-gfx
- # index only when HIP's runtime GPU is the Strix one -- else the dGPU gets
- # an incompatible wheel. Use HIP_VISIBLE_DEVICES for the runtime target.
+ # Strix Halo / Point (gfx1151 / gfx1150) segfault under ROCm 7.1 in torch._grouped_mm;
+ # AMD's per-gfx repo ships 2.11.0+rocm7.13.0 with the fix, so route those hosts there
+ # (mirrors install.sh). On mixed hosts, reroute only when HIP's runtime GPU is the Strix one.
_strix_override_url: "str | None" = None
_strix_override_pkgs: "tuple[str, str, str] | None" = None
- if ver < (7, 2):
+ # An explicit ROCm pin is authoritative: never auto-reroute it.
+ if ver < (7, 2) and _explicit_rocm_torch_index_url() is None:
gfx_codes = _detect_amd_gfx_codes()
_strix_gfx = {"gfx1151", "gfx1150"}
_detected_strix = _strix_gfx.intersection(gfx_codes)
if _detected_strix:
- # Pick the runtime-visible GPU: use the HIP_VISIBLE_DEVICES index
- # into gfx_codes, else default to the first GPU. Skip the override
- # unless the resolved GPU is Strix.
+ # Runtime-visible GPU (HIP_VISIBLE_DEVICES index into gfx_codes, else first);
+ # skip the override unless it's Strix.
_runtime_gfx = gfx_codes[_pick_visible_index(len(gfx_codes))] if gfx_codes else None
if _runtime_gfx in _strix_gfx:
_selected_gfx = _runtime_gfx
@@ -1353,12 +1706,8 @@ def _ensure_rocm_torch() -> None:
_strix_override_url = f"{_amd_mirror}/{_selected_gfx}/"
_strix_override_pkgs = (
"torch>=2.11.0,<2.12.0",
- # Pin torchvision/torchaudio to the 2.11.x-compatible range.
- # The install uses --index-url (exclusive, no PyPI fallback),
- # so bare unversioned names risk resolving an AMD-index build
- # targeting a different torch major (e.g. 0.27 built against
- # torch 2.12), which fails at runtime with an ABI/version
- # mismatch. Matches _ROCM_TORCH_CONSTRAINT["rocm7.2"].
+ # Pin companions to the 2.11.x range: the exclusive --index-url could
+ # otherwise resolve a build for a different torch major (ABI mismatch).
"torchvision>=0.26.0,<0.27.0",
"torchaudio>=2.11.0,<2.12.0",
)
@@ -1378,14 +1727,15 @@ def _ensure_rocm_torch() -> None:
f" skipping AMD per-gfx index override.\n"
)
- # Strix override on ROCm 7.1 must fire even when has_hip_torch is True --
- # an existing torch with `torch.version.hip == "7.1"` is exactly the broken
- # combo the override repairs, so skipping it leaves users on the known
- # _grouped_mm segfault.
+ # The Strix override must fire even when has_hip_torch is True: an existing
+ # torch.version.hip == "7.1" is exactly the broken combo it repairs.
if _strix_override_url is not None and _strix_override_pkgs is not None:
index_url = _strix_override_url
_torch_pkg, _vision_pkg, _audio_pkg = _strix_override_pkgs
- print(f" Strix ROCm 7.1 override -- installing torch from {index_url}")
+ print(
+ f" Strix ROCm 7.1 override -- installing torch from "
+ f"{_strip_index_url_credentials(index_url)}"
+ )
pip_install(
"ROCm torch (Strix arch-specific)",
"--force-reinstall",
@@ -1398,24 +1748,38 @@ def _ensure_rocm_torch() -> None:
constrain = False,
)
rocm_torch_ready = True
- elif not has_hip_torch:
- # Select best matching wheel tag (newest ROCm version <= installed)
- tag = next(
- (
- t
- for (maj, mn), t in sorted(_ROCM_TORCH_INDEX.items(), reverse = True)
- if ver >= (maj, mn)
- ),
- None,
- )
- if tag is None:
- print(f" No PyTorch wheel for ROCm {ver[0]}.{ver[1]} -- " f"skipping torch reinstall")
+ elif not has_hip_torch or _rocm_pin_mismatch:
+ # Reinstall when torch is not ROCm yet, OR a ROCm build's family differs from a pin.
+ # Honour a ROCm pin verbatim; else pick the newest wheel tag <= host.
+ _override_idx = _explicit_rocm_torch_index_url()
+ if _override_idx is not None:
+ index_url = _override_idx
+ tag = _torch_index_leaf(index_url)
else:
- index_url = f"{_PYTORCH_WHL_BASE}/{tag}"
- print(f" ROCm {ver[0]}.{ver[1]} -- installing torch from {index_url}")
- _torch_pkg, _vision_pkg, _audio_pkg = _ROCM_TORCH_PKG_SPECS.get(
- tag, _ROCM_TORCH_PKG_SPECS["_default"]
+ tag = next(
+ (
+ t
+ for (maj, mn), t in sorted(_ROCM_TORCH_INDEX.items(), reverse = True)
+ if ver >= (maj, mn)
+ ),
+ None,
)
+ if tag is None:
+ print(f" No PyTorch wheel for ROCm {ver[0]}.{ver[1]} -- skipping torch reinstall")
+ else:
+ if _override_idx is None:
+ index_url = f"{_PYTORCH_WHL_BASE}/{tag}"
+ print(f" ROCm torch -- installing from {_strip_index_url_credentials(index_url)}")
+ # Only the _grouped_mm-bug gfx arches need the 2.11 spec; other gfx indexes ship
+ # <2.11 and stay on the default range (matches install.ps1 / setup.ps1).
+ if tag in _ROCM_GFX_TORCH211_LEAVES:
+ _torch_pkg, _vision_pkg, _audio_pkg = _ROCM_TORCH_PKG_SPECS["rocm7.2"]
+ elif tag.startswith("gfx"):
+ _torch_pkg, _vision_pkg, _audio_pkg = _ROCM_TORCH_PKG_SPECS["_default"]
+ else:
+ _torch_pkg, _vision_pkg, _audio_pkg = _ROCM_TORCH_PKG_SPECS.get(
+ tag, _ROCM_TORCH_PKG_SPECS["_default"]
+ )
pip_install(
f"ROCm torch ({tag})",
"--force-reinstall",
@@ -1504,11 +1868,26 @@ def _infer_no_torch() -> bool:
NO_TORCH = _infer_no_torch()
-# UNSLOTH_TORCH_BACKEND is set by install.sh after get_torch_index_url() so
-# that this script knows which torch variant was selected without re-running
-# GPU detection. Values: "cuda", "rocm", or "cpu". Empty means unknown
-# (standalone `unsloth studio update` runs, where we re-detect normally).
+# UNSLOTH_TORCH_BACKEND is set by install.sh after get_torch_index_url() ("cuda", "rocm",
+# "cpu"; empty = standalone `studio update`, where we re-detect).
_TORCH_BACKEND: str = os.environ.get("UNSLOTH_TORCH_BACKEND", "").lower()
+# Standalone update with an explicit pin: derive the backend from the override (classify on
+# the final URL/family segment, mirroring install.sh) instead of re-probing the GPU.
+if not _TORCH_BACKEND:
+ _idx_override = (
+ os.environ.get("UNSLOTH_TORCH_INDEX_URL", "").strip()
+ or os.environ.get("UNSLOTH_TORCH_INDEX_FAMILY", "").strip()
+ )
+ _idx_leaf = _torch_index_leaf(_idx_override)
+ if _idx_leaf.startswith(("rocm", "gfx")):
+ _TORCH_BACKEND = "rocm"
+ elif _idx_leaf == "cpu":
+ _TORCH_BACKEND = "cpu"
+ elif _is_cuda_family_leaf(_idx_leaf):
+ # Require a digit after "cu" so /current or /custom is NOT branded CUDA (a wrong backend
+ # makes _ensure_rocm_torch return early on AMD hosts). An unknown leaf keeps "" so the
+ # helpers probe the GPU.
+ _TORCH_BACKEND = "cuda"
def _torch_step_label(suffix: str) -> str:
@@ -1724,12 +2103,15 @@ def run(
cmd,
stdout = subprocess.PIPE if quiet else None,
stderr = subprocess.STDOUT if quiet else None,
+ env = _install_env_for_cmd(cmd),
**_windows_hidden_subprocess_kwargs(),
)
if result.returncode != 0:
_step("error", f"{label} failed (exit code {result.returncode})", _red)
if result.stdout:
- print(result.stdout.decode(errors = "replace"))
+ # Redact before printing: the failing pip command may carry a pinned --index-url
+ # with userinfo/?token= creds, so raw pip error text would leak them.
+ print(_redact_install_output(result.stdout))
sys.exit(result.returncode)
return result
@@ -1737,15 +2119,13 @@ def run(
# Packages to skip on Windows (require special build steps)
WINDOWS_SKIP_PACKAGES = {"triton_kernels"}
-# Packages to skip when torch is unavailable (Intel Mac GGUF-only mode).
-# These either *are* torch extensions or have unconditional
-# ``Requires-Dist: torch``, so installing them would pull torch back in.
-# ``librosa`` is here too despite not requiring torch: upstream ``llvmlite``
-# dropped its macOS x86_64 wheel between 0.42.0 and 0.46.0+ (see
-# https://pypi.org/project/llvmlite/0.47.0/#files -- only
-# macosx_arm64 / manylinux / win_amd64 remain), so on Intel Mac the
-# librosa -> numba -> llvmlite chain triggers a from-source build that fails
-# in CI and on hosts without LLVM 14/15 headers. Tracked in unslothai/unsloth#5046.
+# Packages to skip when torch is unavailable (Intel Mac GGUF-only mode). These
+# either *are* torch extensions or have unconditional ``Requires-Dist: torch``, so
+# installing them pulls torch back in. ``librosa`` is here despite not requiring
+# torch: upstream ``llvmlite`` dropped its macOS x86_64 wheel (0.46.0+ ships only
+# macosx_arm64 / manylinux / win_amd64), so on Intel Mac the librosa -> numba ->
+# llvmlite chain triggers a from-source build that fails without LLVM 14/15 headers.
+# Tracked in unslothai/unsloth#5046.
NO_TORCH_SKIP_PACKAGES = {
"torch-stoi",
"timm",
@@ -1767,7 +2147,8 @@ def _build_flash_attn_wheel_url(env: dict[str, str]) -> str | None:
def _print_optional_install_failure(label: str, result: subprocess.CompletedProcess[str]) -> None:
_step("warning", f"{label} failed (exit code {result.returncode})", _cyan)
if result.stdout:
- print(result.stdout.strip())
+ # Redact any pinned --index-url credentials before printing captured output.
+ print(_redact_install_output(result.stdout).strip())
def _flash_attn_install_disabled() -> bool:
@@ -1913,15 +2294,60 @@ def _build_uv_cmd(args: tuple[str, ...]) -> list[str]:
# Colab and similar).
cmd.extend(["--python", sys.executable])
cmd.extend(_translate_pip_args_for_uv(args))
- # Torch is pre-installed by install.sh/setup.ps1. Do not add
- # --torch-backend by default -- it can cause solver dead-ends on CPU-only
- # machines. Callers that need it can set UV_TORCH_BACKEND.
+ # Torch is pre-installed, so don't add --torch-backend by default (solver dead-ends on
+ # CPU-only machines); callers can set UV_TORCH_BACKEND. Never add it to a pinned-index
+ # command: uv's torch backend redirects torch to its own per-backend index, defeating the pin.
_tb = os.environ.get("UV_TORCH_BACKEND", "")
- if _tb:
+ if _tb and not _is_pinned_index_cmd(cmd):
cmd.append(f"--torch-backend={_tb}")
return cmd
+# uv resolves --index-url / --default-index at LOWEST priority, so an inherited UV_INDEX /
+# UV_EXTRA_INDEX_URL mirror wins and a pinned torch repair silently ignores the pin.
+# Neutralise these for pinned installs (as install.sh #6898 / install.ps1 / setup.ps1 do).
+# UV_TORCH_BACKEND redirects torch; PIP_* matter for the pip FALLBACK; UV_CONFIG_FILE is
+# stripped + UV_NO_CONFIG=1 (a discovered uv.toml outranks the CLI pin, uv 0.10).
+_UV_INDEX_ENV_VARS = (
+ "UV_CONFIG_FILE",
+ "UV_DEFAULT_INDEX",
+ "UV_INDEX_URL",
+ "UV_INDEX",
+ "UV_EXTRA_INDEX_URL",
+ "UV_TORCH_BACKEND",
+ "UV_FIND_LINKS",
+ "PIP_EXTRA_INDEX_URL",
+ "PIP_FIND_LINKS",
+ # PIP_NO_INDEX=1 makes the pip fallback ignore ALL indexes (defeating --index-url);
+ # PIP_INDEX_URL is dropped too so a stale mirror env can't outrank the pin.
+ "PIP_NO_INDEX",
+ "PIP_INDEX_URL",
+)
+
+
+def _is_pinned_index_cmd(cmd: "list[str] | tuple[str, ...]") -> bool:
+ """True when the command pins an index via --index-url / --default-index."""
+ return any(arg in ("--index-url", "--default-index") for arg in cmd)
+
+
+def _install_env_for_cmd(cmd: "list[str]") -> "dict[str, str] | None":
+ """Return an env with the uv index vars stripped for a pinned-index install.
+
+ None (inherit env) when the command does NOT pin an index, so ordinary installs honour
+ the user's mirror. For pinned commands, the uv index/backend vars are removed,
+ UV_NO_CONFIG=1 set (a discovered uv.toml outranks the CLI pin), and PIP_CONFIG_FILE
+ pointed at os.devnull for the pip fallback. Mirrors install.sh's gate (#6898).
+ """
+ if not _is_pinned_index_cmd(cmd):
+ return None
+ env = os.environ.copy()
+ for name in _UV_INDEX_ENV_VARS:
+ env.pop(name, None)
+ env["UV_NO_CONFIG"] = "1"
+ env["PIP_CONFIG_FILE"] = os.devnull
+ return env
+
+
def pip_install_try(
label: str,
*args: str,
@@ -1948,11 +2374,13 @@ def pip_install_try(
cmd,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
+ env = _install_env_for_cmd(cmd),
)
if result.returncode == 0:
return True
if VERBOSE and result.stdout:
- print(result.stdout.decode(errors = "replace"))
+ # pip/uv echo index URLs (credentials included) in failure output.
+ print(_redact_install_output(result.stdout))
return False
@@ -2000,13 +2428,14 @@ def pip_install(
uv_cmd,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
+ env = _install_env_for_cmd(uv_cmd),
**_windows_hidden_subprocess_kwargs(),
)
if result.returncode == 0:
return
print(_red(f" uv failed, falling back to pip..."))
if result.stdout:
- print(result.stdout.decode(errors = "replace"))
+ print(_redact_install_output(result.stdout))
pip_cmd = _build_pip_cmd(args) + constraint_args_pip + req_args_pip
run(f"{label} (pip)" if USE_UV else label, pip_cmd)
@@ -2054,10 +2483,9 @@ def install_python_stack() -> int:
global USE_UV, _STEP, _TOTAL
_STEP = 0
- # install.sh (which already installed unsloth) sets SKIP_STUDIO_BASE=1 to
- # avoid reinstalling base packages. "unsloth studio update" does NOT set it,
- # so base packages (unsloth + unsloth-zoo) are reinstalled to pick up new
- # versions.
+ # install.sh sets SKIP_STUDIO_BASE=1 to avoid reinstalling base packages;
+ # `studio update` does NOT, so unsloth + unsloth-zoo are reinstalled to pick
+ # up new versions.
skip_base = os.environ.get("SKIP_STUDIO_BASE", "0") == "1"
# --package installs a different package name (for testing).
package_name = os.environ.get("STUDIO_PACKAGE_NAME", "unsloth")
@@ -2067,9 +2495,9 @@ def install_python_stack() -> int:
if IS_MACOS:
base_total -= 1 # triton step is skipped on macOS
if not IS_MACOS and not NO_TORCH:
- base_total += 1 # ROCm torch check (line 1526) -- all non-macOS platforms
+ base_total += 1 # ROCm torch check (step 2b), non-macOS
if not IS_WINDOWS:
- base_total += 2 # flash-attn (line 1620) + ROCm torch final (line 1705) -- Linux only
+ base_total += 2 # flash-attn + torch final repair (step 13), Linux
_TOTAL = (base_total - 1) if skip_base else base_total
# 1. Try uv for faster installs (before pip upgrade -- uv venvs don't
@@ -2134,9 +2562,8 @@ def install_python_stack() -> int:
if skip_base:
pass
elif NO_TORCH:
- # No-torch update path: install unsloth + unsloth-zoo with --no-deps
- # (PyPI metadata still declares torch as a hard dep), then runtime deps
- # with --no-deps (avoids transitive torch).
+ # No-torch update path: install unsloth + unsloth-zoo, then runtime deps,
+ # both with --no-deps (PyPI metadata declares torch a hard dep; avoid it).
_progress("base packages (no torch)")
pip_install(
f"Updating {package_name} + unsloth-zoo (no-torch mode)",
@@ -2149,10 +2576,9 @@ def install_python_stack() -> int:
package_name,
"unsloth-zoo",
)
- # Resolve pydantic WITH deps so pip pins pydantic-core to the exact
- # version pydantic's metadata declares. Under --no-deps pip picks the
- # latest of each and trips pydantic's _ensure_pydantic_core_version
- # check. Transitive deps are torch-free.
+ # Resolve pydantic WITH deps so pip pins pydantic-core to the exact version
+ # its metadata declares (under --no-deps pip picks the latest of each and
+ # trips pydantic's _ensure_pydantic_core_version check). Deps are torch-free.
pip_install(
"Installing pydantic (with deps for compatible core)",
"--no-cache-dir",
@@ -2244,6 +2670,7 @@ def install_python_stack() -> int:
_progress(_torch_step_label("check"))
_ensure_cuda_torch()
_ensure_rocm_torch()
+ _ensure_cpu_torch()
# Windows + AMD GPU: warn if ROCm torch was not installed (wrong Python
# version or unknown ROCm version).
@@ -2309,11 +2736,10 @@ def install_python_stack() -> int:
req = REQ_ROOT / "extras-no-deps.txt",
)
- # 4. Overrides (torchao) -- force-reinstall. The torchao version is chosen to
- # match the torch installed in the venv so its C++ extensions load (see
- # _select_torchao_spec). Skip when torch is unavailable (e.g. Intel Mac
- # GGUF-only mode): torchao requires torch. Also skipped on Windows ROCm
- # (no working build; see below).
+ # 4. Overrides (torchao) -- force-reinstall to a version matching the venv's
+ # torch so its C++ extensions load (see _select_torchao_spec). Skipped when
+ # torch is unavailable (Intel Mac GGUF-only) and on Windows ROCm (no working
+ # build; see below).
if NO_TORCH:
_progress("dependency overrides (skipped, no torch)")
elif _rocm_windows_torch_installed or _installed_torch_is_windows_rocm():
@@ -2430,14 +2856,12 @@ def install_python_stack() -> int:
[sys.executable, str(SINGLE_ENV / "patch_metadata.py")],
)
- # 13. AMD ROCm: final torch repair. Several steps above can pull in CUDA
- # torch from PyPI (base packages, extras, overrides, studio deps, etc.).
- # Running the repair last ensures ROCm torch is in place at runtime,
- # whichever intermediate step clobbered it.
+ # 13. Final torch repair. Steps above can pull CUDA torch from PyPI, so repair last.
if not IS_WINDOWS and not IS_MACOS and not NO_TORCH:
_progress(_torch_step_label("final"))
_ensure_cuda_torch()
_ensure_rocm_torch()
+ _ensure_cpu_torch()
# 14. Final check (silent; third-party conflicts are expected)
subprocess.run(
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index 98e801cd3c..f523b9ff14 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"
@@ -427,6 +431,167 @@ function Get-PytorchCudaTag {
return "cu126"
}
+# Trim trailing slashes from the URL PATH only, preserving ?query / #fragment: a whole-URL
+# TrimEnd corrupts a token ending in "/", a single strip leaves .../cu128// empty. Shared.
+function Trim-IndexPathSlashes {
+ param([string]$Url)
+ $value = $Url.Trim()
+ $idx = $value.IndexOfAny([char[]]@('?', '#'))
+ if ($idx -lt 0) {
+ return $value.TrimEnd('/')
+ }
+ return $value.Substring(0, $idx).TrimEnd('/') + $value.Substring($idx)
+}
+
+# Explicit torch-index pin (UNSLOTH_TORCH_INDEX_URL / _FAMILY), shared by the stale-venv check
+# and install selection so a pinned index wins over GPU probing (parity with the other
+# installers). URL is verbatim; _FAMILY is the leaf joined to the mirror base.
+function Get-PinnedTorchIndexUrl {
+ if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_URL)) {
+ return (Trim-IndexPathSlashes $env:UNSLOTH_TORCH_INDEX_URL)
+ }
+ if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_FAMILY)) {
+ $base = if ($env:UNSLOTH_PYTORCH_MIRROR) { $env:UNSLOTH_PYTORCH_MIRROR.TrimEnd('/') } else { "https://download.pytorch.org/whl" }
+ return "$base/$($env:UNSLOTH_TORCH_INDEX_FAMILY.Trim().Trim('/'))"
+ }
+ return $null
+}
+
+# Last path segment of a wheel index URL, query/fragment dropped first so a token-authenticated
+# pin (.../cu128?token=x) classifies as cu128 (else it reinstalls every update). Classification
+# only. Shared with the py / install.sh leaf extractors.
+function Get-TorchIndexLeaf {
+ param([string]$Url)
+ if ([string]::IsNullOrWhiteSpace($Url)) { return $null }
+ $path = ($Url -split '[?#]', 2)[0]
+ if ([string]::IsNullOrWhiteSpace($path)) { return $null }
+ return ($path.TrimEnd('/') -split '/')[-1].ToLowerInvariant()
+}
+
+# Redact index-URL credentials (userinfo + ?query= + #fragment) from captured installer
+# output before printing on failure; uv/pip errors echo the failing --index-url verbatim.
+# Mirrors the other installers. Verbose mode streams uncaptured, so it isn't redacted.
+function Redact-InstallOutput {
+ param([string]$Text)
+ if (-not $Text) { return $Text }
+ $Text = $Text -replace '(https?://)[^/@\s`]+@', '$1@'
+ $Text = $Text -replace '([?&][^=\s&`]+)=[^\s`]+', '$1='
+ # A #token=... fragment is as sensitive as a query; URL-anchored.
+ return $Text -replace '(https?://[^\s`#]+)#[^\s`]+', '$1#'
+}
+
+# AMD per-arch leaves needing the torch 2.11 floor (the _grouped_mm <2.11 bug). MUST match
+# the install-spec path below and the other installers; other leaves ship <2.11 and stay default.
+function Test-RocmGfx211Leaf {
+ param([string]$Leaf)
+ return @('gfx120x-all', 'gfx1151', 'gfx1150') -contains $Leaf
+}
+
+# rocmX.Y versions KNOWN to ship torch 2.11: rocm7.2 only today. Do NOT floor an unknown newer
+# rocm speculatively. MUST match _ROCM_KNOWN_TORCH211_VERSIONS and the rocm7.2 leaf elsewhere.
+function Test-RocmKnown211Version {
+ param([int]$Major, [int]$Minor)
+ return ($Major -eq 7 -and $Minor -eq 2)
+}
+
+# True only for a real CUDA family leaf: "cu" + digits (cu118, cu128, ...). A bare -like 'cu*'
+# would match "custom"/"current" and rebuild the venv every run. Mirrors _is_cuda_family_leaf.
+function Test-CudaFamilyLeaf {
+ param([string]$Leaf)
+ if ([string]::IsNullOrWhiteSpace($Leaf)) { return $false }
+ # EXACT cu+digits: cu128-private routes through the unknown-leaf path instead.
+ return $Leaf -match '^cu[0-9]+$'
+}
+
+# True only for a real pip ROCm family leaf: EXACT rocm[.] or a gfx leaf. A leaf
+# that merely STARTS with rocm (rocm-rel-7.2.1, rocm7.2-private) is a custom pin the verbatim
+# path owns, so anchor the match. Mirrors _is_pip_rocm_family_leaf / install.sh.
+function Test-PipRocmFamilyLeaf {
+ param([string]$Leaf)
+ if ([string]::IsNullOrWhiteSpace($Leaf)) { return $false }
+ # gfx must be followed by a digit (an architecture leaf); gfx-private is custom.
+ return ($Leaf -match '^gfx[0-9]') -or ($Leaf -match '^rocm[0-9]+(\.[0-9]+)?$')
+}
+
+# Stale-venv ROCm comparison for a pinned gfx*/rocm* index. Returns @{ Expected; Installed } so
+# the caller rebuilds when they differ. Mirrors _rocm_pin_family_mismatch (same rocmX.Y / gfx
+# cases). An untagged (no +rocm) wheel never satisfies a ROCm pin -> stale.
+function Get-RocmPinStaleTags {
+ param([string]$PinLeaf, [string]$TorchVersion)
+ $_pinRocm = [regex]::Match($PinLeaf, '^rocm(\d+)\.(\d+)')
+ $_pinVer = if ($_pinRocm.Success) { "$($_pinRocm.Groups[1].Value).$($_pinRocm.Groups[2].Value)" } else { $null }
+ # The family classifier accepts a major-only rocm leaf too (rocm7).
+ $_pinMajorOnly = [regex]::Match($PinLeaf, '^rocm(\d+)$')
+ # Installed rocm version and whether the wheel is a per-arch (three-part) build.
+ $_instRocm = [regex]::Match($TorchVersion, '\+rocm(\d+)\.(\d+)')
+ $_instVer = if ($_instRocm.Success) { "$($_instRocm.Groups[1].Value).$($_instRocm.Groups[2].Value)" } else { $null }
+ $_instPerArch = [regex]::IsMatch($TorchVersion, '\+rocm\d+\.\d+\.\d+')
+ # A ROCm build MUST carry a +rocm tag; an untagged wheel can't satisfy any ROCm pin.
+ $_instHasRocm = [regex]::IsMatch($TorchVersion, '\+rocm')
+ $_instRel = [regex]::Match($TorchVersion, '^(\d+)\.(\d+)')
+ $_instIs211 = $false
+ if ($_instRel.Success) {
+ $_instIs211 = ([int]$_instRel.Groups[1].Value -gt 2) -or ([int]$_instRel.Groups[1].Value -eq 2 -and [int]$_instRel.Groups[2].Value -ge 11)
+ }
+
+ if ($PinLeaf -like 'gfx*') {
+ if (Test-RocmGfx211Leaf $PinLeaf) {
+ # Expect the AMD per-arch (three-part) 2.11 wheel: satisfied only when BOTH
+ # a 2.11 release AND a three-part rocm tag are installed.
+ $installed = if ($_instIs211 -and $_instPerArch) { "rocm-perarch(torch>=2.11)" } else { "rocm-generic-or-old" }
+ return @{ Expected = "rocm-perarch(torch>=2.11)"; Installed = $installed }
+ }
+ # Non-2.11 gfx leaf (<2.11 spec): stale on an untagged wheel or a 2.11+ build.
+ $installed = if (-not $_instHasRocm) { "not-rocm" } elseif ($_instIs211) { "rocm(torch>=2.11)" } else { "rocm(torch<2.11)" }
+ return @{
+ Expected = "rocm(torch<2.11)"
+ Installed = $installed
+ }
+ }
+
+ # Major-only rocm pin (rocm7): compare majors only -- a +rocm6.4 wheel under a rocm7
+ # pin is stale, any +rocm7.x wheel satisfies it (no pinned minor to compare, and the
+ # 2.11-line fallback below would invert both verdicts). Mirrors _rocm_pin_family_mismatch.
+ if ($_pinMajorOnly.Success) {
+ $_pinMaj = [int]$_pinMajorOnly.Groups[1].Value
+ if ($_instVer) {
+ $_instMaj = [int]$_instRocm.Groups[1].Value
+ $expected = if ($_instMaj -eq $_pinMaj) { "rocm$_instVer" } else { "rocm$_pinMaj.x" }
+ return @{ Expected = $expected; Installed = "rocm$_instVer" }
+ }
+ # Untagged wheel never satisfies a ROCm pin; a +rocm tag with an unreadable
+ # version is accepted (matches the lenient unreadable fallback below).
+ $installed = if ($_instHasRocm) { "rocm" } else { "not-rocm" }
+ return @{ Expected = "rocm"; Installed = $installed }
+ }
+
+ # rocmX.Y pin.
+ if ($_pinVer -and $_instVer) {
+ # Both readable: exact compare. When they match AND the pin is KNOWN-2.11, the
+ # installed release must also be 2.11 (a +rocm7.2 wheel drifted to 2.12 shares the
+ # tag but violates the spec), so fold the release into the tag. Mirrors _rocm_pin_family_mismatch.
+ $_pinKnown211 = Test-RocmKnown211Version -Major ([int]$_pinRocm.Groups[1].Value) -Minor ([int]$_pinRocm.Groups[2].Value)
+ $_instOn211 = $_instRel.Success -and [int]$_instRel.Groups[1].Value -eq 2 -and [int]$_instRel.Groups[2].Value -eq 11
+ if ($_pinKnown211 -and -not $_instOn211) {
+ return @{ Expected = "rocm$_pinVer(torch2.11)"; Installed = "rocm$_instVer(torch-off-2.11)" }
+ }
+ return @{ Expected = "rocm$_pinVer"; Installed = "rocm$_instVer" }
+ }
+ $_pinNeeds211 = $false
+ if ($_pinRocm.Success) {
+ # Only KNOWN-2.11 rocm (rocm7.2) is on the 2.11 line (no speculative floor).
+ # Matches _ROCM_KNOWN_TORCH211_VERSIONS.
+ $_pinNeeds211 = Test-RocmKnown211Version -Major ([int]$_pinRocm.Groups[1].Value) -Minor ([int]$_pinRocm.Groups[2].Value)
+ }
+ # Fallback (installed rocm version unreadable): compare on the 2.11 line; an untagged
+ # wheel never satisfies a rocmX.Y pin -> stale.
+ $installed = if (-not $_instHasRocm) { "not-rocm" } elseif ($_instIs211) { "rocm(torch>=2.11)" } else { "rocm(torch<2.11)" }
+ return @{
+ Expected = if ($_pinNeeds211) { "rocm(torch>=2.11)" } else { "rocm(torch<2.11)" }
+ Installed = $installed
+ }
+}
+
# VS generator -> MSBuild BuildCustomizations dir; toolset tracks the VS major
# (18->v180, 17->v170), defaulting to v170 when unparseable.
function Get-VcBuildCustomizationsDir {
@@ -809,11 +974,14 @@ function Invoke-SetupCommand {
# Merge stderr into stdout so progress/warning output stays visible
# without flipping $? on successful native commands (PS 5.1 treats
# stderr records as errors that set $? = $false even on exit code 0).
- & $Command 2>&1 | Out-Host
+ # Redact per record: uv/pip echo index URLs (credentials and all) in
+ # their errors, and verbose mode must not bypass the quiet path's
+ # redaction. ForEach-Object/Out-Host leave $LASTEXITCODE untouched.
+ & $Command 2>&1 | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
} else {
$output = & $Command 2>&1 | Out-String
if ($LASTEXITCODE -ne 0) {
- Write-Host $output -ForegroundColor Red
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Red
}
}
return [int]$LASTEXITCODE
@@ -2531,6 +2699,8 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
$VenvPyExe = Join-Path $VenvDir "Scripts\python.exe"
$installedTorchTag = $null
$shouldRebuild = $false
+ # Set when a stale venv under a pin is repaired in place (force-reinstall) not wiped.
+ $script:PinChangedForceReinstall = $false
if (Test-Path -LiteralPath $VenvPyExe) {
try {
@@ -2547,10 +2717,14 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
if ($finished -and $proc.ExitCode -eq 0 -and $torchVer) {
if ($torchVer -match '\+(cu\d+)') {
$installedTorchTag = $Matches[1]
+ } elseif ($torchVer -match '\+rocm') {
+ # Any +rocm / gfx wheel -> generic "rocm" flavor (the exact version is
+ # repaired later by install_python_stack.py; here we only need the flavor).
+ $installedTorchTag = "rocm"
} elseif ($torchVer -match '\+cpu') {
$installedTorchTag = "cpu"
} else {
- # Untagged wheel (plain "2.x.y" from PyPI) -- treat as cpu
+ # Untagged wheel (plain "2.x.y" from PyPI) -> cpu.
$installedTorchTag = "cpu"
}
} else {
@@ -2566,12 +2740,71 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
}
if (-not $shouldRebuild) {
- $expectedTorchTag = if ($HasNvidiaSmi) { Get-PytorchCudaTag } else { "cpu" }
- if ($installedTorchTag -and $installedTorchTag -ne $expectedTorchTag) {
+ $_pinnedIdx = Get-PinnedTorchIndexUrl
+ $_expectedKnown = $true
+ if ($_pinnedIdx) {
+ $_pinLeaf = Get-TorchIndexLeaf $_pinnedIdx
+ # Digit-gated like the install selection: a custom rocm-* leaf (rocm-current /
+ # rocm-rel-7.2.1) is NOT a ROCm family and must not be stale-compared.
+ if (Test-PipRocmFamilyLeaf $_pinLeaf) {
+ # Don't collapse a pinned ROCm/gfx leaf to a generic "rocm" (would mask a family
+ # change, rocm6.4 -> gfx1151). Get-RocmPinStaleTags uses the SAME 2.11 allowlist
+ # as the install path, so a gfx110X-all/gfx90a/gfx908 pin on a <2.11 wheel is NOT stale.
+ $_rocmTags = Get-RocmPinStaleTags -PinLeaf $_pinLeaf -TorchVersion $torchVer
+ $expectedTorchTag = $_rocmTags.Expected
+ $installedTorchTag = $_rocmTags.Installed
+ } elseif ((Test-CudaFamilyLeaf $_pinLeaf) -or $_pinLeaf -eq 'cpu') {
+ # cu*/cpu leaves stay specific so a cu126-vs-cu128 mismatch rebuilds;
+ # /custom and /current fall through to the unknown-index branch below.
+ $expectedTorchTag = $_pinLeaf
+ } else {
+ # Custom index whose leaf is not a torch flavor (a /simple mirror): the
+ # flavor can't be inferred, so never treat the venv as stale over it.
+ $_expectedKnown = $false
+ $expectedTorchTag = $installedTorchTag
+ }
+ } elseif ($HasNvidiaSmi) {
+ $expectedTorchTag = Get-PytorchCudaTag
+ } elseif ($HasROCm -or $script:ROCmGfxArch) {
+ # AMD/ROCm host with no explicit pin: an existing +rocm wheel is correct (gfx arch
+ # counts even when $HasROCm is false). But only the arches the install path maps to a
+ # repo.amd.com index get ROCm torch; an unmapped arch installs CPU, so expect "cpu"
+ # for those or a correct CPU venv rebuilds every update.
+ $_rocmWheelArches = @(
+ "gfx1201", "gfx1200", # RDNA 4
+ "gfx1151", "gfx1150", # RDNA 3.5 (Strix Halo/Point)
+ "gfx1103", "gfx1102", "gfx1101", "gfx1100", # RDNA 3
+ "gfx90a", "gfx908" # MI200 / MI100
+ )
+ if ($script:ROCmGfxArch -and ($_rocmWheelArches -contains $script:ROCmGfxArch)) {
+ # A correct +rocm wheel is not stale. A CPU wheel on a supported AMD arch is
+ # NOT wiped either (the AMD Windows ROCm override below upgrades it in place);
+ # expect "cpu" for that case. A wrong CUDA wheel still rebuilds.
+ if ($installedTorchTag -eq "cpu") {
+ $expectedTorchTag = "cpu"
+ } else {
+ $expectedTorchTag = "rocm"
+ }
+ } else {
+ $expectedTorchTag = "cpu"
+ }
+ } else {
+ $expectedTorchTag = "cpu"
+ }
+ if ($_expectedKnown -and $installedTorchTag -and $installedTorchTag -ne $expectedTorchTag) {
$shouldRebuild = $true
}
}
+ # A stale venv under a pin whose torch still imports is repaired IN PLACE (the dependency
+ # pass force-reinstalls from the pin). The rebuild path wipes the venv and would strand a
+ # direct `studio update`; only a broken venv or an unpinned drift wipes.
+ if ($shouldRebuild -and $_pinnedIdx -and $installedTorchTag) {
+ substep "Torch-index pin changed ($installedTorchTag) -- reinstalling torch from the pin in place." "Cyan"
+ $script:PinChangedForceReinstall = $true
+ $shouldRebuild = $false
+ }
+
if ($shouldRebuild) {
$reason = if ($installedTorchTag) { "torch $installedTorchTag != required $expectedTorchTag" } else { "torch could not be imported" }
if ($InstallerManagedSetup) {
@@ -2649,23 +2882,41 @@ if (Get-Command uv -ErrorAction SilentlyContinue) {
# Helper: install a package, preferring uv with pip fallback
function Fast-Install {
param([Parameter(ValueFromRemainingArguments=$true)]$Args_)
- if ($UseUv) {
- $VenvPy = (Get-Command python).Source
- # An explicit --index-url must win. Inherited uv index env vars otherwise
- # override it and pull CPU torch over the CUDA/ROCm build (#6898), so drop
- # them only for index-pinned installs; mirrors still apply elsewhere.
- $saved = @{}
- if (@($Args_) -contains '--index-url') {
- foreach ($n in 'UV_DEFAULT_INDEX', 'UV_INDEX_URL', 'UV_INDEX', 'UV_EXTRA_INDEX_URL') {
- $saved[$n] = [Environment]::GetEnvironmentVariable($n)
- Remove-Item "Env:$n" -ErrorAction SilentlyContinue
- }
+ # An explicit --index-url must win: inherited uv index vars otherwise pull CPU torch over
+ # the CUDA/ROCm build (#6898), so drop them for pinned installs (scrub covers the whole
+ # function since the pip fallback honours PIP_* too). UV_TORCH_BACKEND / UV_FIND_LINKS also
+ # reroute; UV_NO_CONFIG=1 (+ dropping UV_CONFIG_FILE) stops a uv.toml index outranking the
+ # pin (uv 0.10); PIP_NO_INDEX / PIP_INDEX_URL would defeat the pinned --index-url in pip.
+ $saved = @{}
+ $pinned = @($Args_) -contains '--index-url'
+ if ($pinned) {
+ foreach ($n in 'UV_DEFAULT_INDEX', 'UV_INDEX_URL', 'UV_INDEX', 'UV_EXTRA_INDEX_URL',
+ 'UV_TORCH_BACKEND', 'UV_FIND_LINKS', 'PIP_EXTRA_INDEX_URL', 'PIP_FIND_LINKS',
+ 'PIP_NO_INDEX', 'PIP_INDEX_URL',
+ 'UV_CONFIG_FILE', 'UV_NO_CONFIG', 'PIP_CONFIG_FILE') {
+ $saved[$n] = [Environment]::GetEnvironmentVariable($n)
+ Remove-Item "Env:$n" -ErrorAction SilentlyContinue
}
- try { $result = & uv pip install --python $VenvPy @Args_ 2>&1 }
- finally { foreach ($n in $saved.Keys) { if ($null -ne $saved[$n]) { Set-Item "Env:$n" $saved[$n] } } }
- if ($LASTEXITCODE -eq 0) { return }
+ $env:UV_NO_CONFIG = '1'
+ # A `pip config` global.extra-index-url still adds indexes to the pip FALLBACK;
+ # PIP_CONFIG_FILE = 'nul' (Windows devnull) loads NO config (uv ignores pip config).
+ $env:PIP_CONFIG_FILE = 'nul'
+ }
+ try {
+ if ($UseUv) {
+ $VenvPy = (Get-Command python).Source
+ $result = & uv pip install --python $VenvPy @Args_ 2>&1
+ if ($LASTEXITCODE -eq 0) { return }
+ }
+ & python -m pip install @Args_ 2>&1
+ }
+ finally {
+ if ($pinned) {
+ Remove-Item "Env:UV_NO_CONFIG" -ErrorAction SilentlyContinue
+ Remove-Item "Env:PIP_CONFIG_FILE" -ErrorAction SilentlyContinue
+ }
+ foreach ($n in $saved.Keys) { if ($null -ne $saved[$n]) { Set-Item "Env:$n" $saved[$n] } }
}
- & python -m pip install @Args_ 2>&1
}
# ── Check if Python deps need updating ──
@@ -2748,6 +2999,10 @@ sys.exit(0 if (major, minor) >= (4, 14) else 1)
# pip install unsloth 2>&1 | Out-Null
# }
+# A torch-index pin change repairs in place: force the dependency pass so the torch install
+# below force-reinstalls from the new pin (else the fast path keeps the old wheel).
+if ($script:PinChangedForceReinstall) { $SkipPythonDeps = $false }
+
if (-not $SkipPythonDeps) {
if ($script:UnslothVerbose) {
@@ -2775,7 +3030,13 @@ $env:TORCHINDUCTOR_CACHE_DIR = $TorchCacheDir
[Environment]::SetEnvironmentVariable('TORCHINDUCTOR_CACHE_DIR', $TorchCacheDir, 'User')
substep "TORCHINDUCTOR_CACHE_DIR set to $TorchCacheDir (avoids MAX_PATH issues)"
-if ($HasNvidiaSmi) {
+# Explicit pin (URL or family) wins over GPU probing and suppresses the AMD reroute below;
+# matches install.sh / install.ps1 / install_python_stack.py.
+$PinnedTorchIndexUrl = Get-PinnedTorchIndexUrl
+$TorchIndexPinned = [bool]$PinnedTorchIndexUrl
+if ($PinnedTorchIndexUrl) {
+ $CuTag = Get-TorchIndexLeaf $PinnedTorchIndexUrl
+} elseif ($HasNvidiaSmi) {
$CuTag = Get-PytorchCudaTag
} else {
$CuTag = "cpu"
@@ -2796,7 +3057,7 @@ $ROCmIndexUrl = $null
# 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") {
+if (-not $TorchIndexPinned -and ($HasROCm -or $ROCmGfxArch) -and $CuTag -eq "cpu") {
$amdIndexBase = if ($env:UNSLOTH_ROCM_WINDOWS_MIRROR) { $env:UNSLOTH_ROCM_WINDOWS_MIRROR.TrimEnd('/') } else { "https://repo.amd.com/rocm/whl" }
$archFamilyMap = @{
"gfx1201" = "gfx120X-all"; "gfx1200" = "gfx120X-all" # RDNA 4
@@ -2846,8 +3107,45 @@ if (($HasROCm -or $ROCmGfxArch) -and $CuTag -eq "cpu") {
}
}
+# A pinned gfx*/rocm index skips the auto-reroute above; route it through the ROCm install path
+# with the same floor/companions the unpinned AMD path uses (mirrors install.ps1), else the CUDA
+# branch installs bare torch and resolves a known-bad wheel for gfx115x/gfx120x/rocm>=7.2.
+if ($TorchIndexPinned -and -not $ROCmIndexUrl -and $PinnedTorchIndexUrl) {
+ $_pinLeaf = Get-TorchIndexLeaf $PinnedTorchIndexUrl
+ $_pinRocm211 = $false
+ # Anchor the match ($) so a suffixed custom leaf (rocm7.2-private) falls through to the
+ # verbatim install instead of being floored by its rocm7.2 prefix.
+ if ($_pinLeaf -match '^rocm(\d+)\.(\d+)$') {
+ # Only KNOWN-2.11 rocm (rocm7.2) gets the floor (no speculative floor). Matches
+ # Test-RocmKnown211Version / _ROCM_KNOWN_TORCH211_VERSIONS.
+ $_pinRocm211 = Test-RocmKnown211Version -Major ([int]$Matches[1]) -Minor ([int]$Matches[2])
+ }
+ # Only the 2.11 gfx arches need the floor; others publish <2.11 and stay bare. Reuse
+ # Test-RocmGfx211Leaf so this allowlist and the stale-venv check never diverge.
+ $_pinGfx211 = Test-RocmGfx211Leaf $_pinLeaf
+ if ($_pinGfx211 -or $_pinRocm211) {
+ $ROCmIndexUrl = $PinnedTorchIndexUrl
+ $ROCmTorchSpec = "torch>=2.11.0,<2.12.0"
+ $ROCmVisionSpec = "torchvision>=0.26.0,<0.27.0"
+ $ROCmAudioSpec = "torchaudio>=2.11.0,<2.12.0"
+ substep "pinned ROCm index ($_pinLeaf) -- enforcing $ROCmTorchSpec" "Cyan"
+ } elseif (Test-PipRocmFamilyLeaf $_pinLeaf) {
+ # Other gfx / older rocm (<=7.1) ship torch <2.11; route via the ROCm path with
+ # bare specs. Only EXACT rocm and gfx* are --index-url families; a suffixed
+ # leaf stays on the verbatim path. Mirrors install.ps1 / _is_pip_rocm_family_leaf.
+ $ROCmIndexUrl = $PinnedTorchIndexUrl
+ $ROCmTorchSpec = "torch"
+ $ROCmVisionSpec = "torchvision"
+ $ROCmAudioSpec = "torchaudio"
+ }
+}
+
$PyTorchWhlBase = if ($env:UNSLOTH_PYTORCH_MIRROR) { $env:UNSLOTH_PYTORCH_MIRROR.TrimEnd('/') } else { "https://download.pytorch.org/whl" }
+# A full URL pin is used verbatim; a family pin already set $CuTag. A pinned ROCm install
+# goes through $ROCmIndexUrl; on failure the fallback uses the CPU index, not the ROCm pin.
+$TorchInstallIndexUrl = if ($ROCmIndexUrl) { "$PyTorchWhlBase/cpu" } elseif ($PinnedTorchIndexUrl) { $PinnedTorchIndexUrl } else { "$PyTorchWhlBase/$CuTag" }
+
$ROCmCpuFallback = $false
if ($ROCmIndexUrl) {
substep "installing PyTorch (AMD ROCm, $ROCmGfxArch)..."
@@ -2855,7 +3153,7 @@ if ($ROCmIndexUrl) {
substep " enforcing $ROCmTorchSpec $ROCmVisionSpec $ROCmAudioSpec (known _grouped_mm bug in older wheels)" "Cyan"
}
if ($script:UnslothVerbose) {
- Fast-Install $ROCmTorchSpec $ROCmVisionSpec $ROCmAudioSpec --force-reinstall --index-url $ROCmIndexUrl
+ Fast-Install $ROCmTorchSpec $ROCmVisionSpec $ROCmAudioSpec --force-reinstall --index-url $ROCmIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
$torchInstallExit = $LASTEXITCODE
$output = ""
} else {
@@ -2864,7 +3162,7 @@ if ($ROCmIndexUrl) {
}
if ($torchInstallExit -ne 0) {
Write-Host "[WARN] AMD ROCm PyTorch install failed -- falling back to CPU" -ForegroundColor Yellow
- Write-Host $output -ForegroundColor Yellow
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Yellow
$ROCmIndexUrl = $null
$ROCmCpuFallback = $true
} else {
@@ -2874,42 +3172,70 @@ if ($ROCmIndexUrl) {
}
}
-if (-not $ROCmIndexUrl -and $CuTag -eq "cpu") {
+if (-not $ROCmIndexUrl -and ($CuTag -eq "cpu" -or $ROCmCpuFallback)) {
substep "installing PyTorch (CPU-only)..."
- # After an AMD ROCm fallback, force-reinstall so a partially-installed ROCm torch
- # (which still satisfies the CPU torch>= range) is replaced by the CPU build. Skip
- # the forced reinstall on a genuine CPU-only host so the common path stays fast.
- # Build the array directly: an if-expression collapses @("x") to a scalar string,
- # which @splat would then enumerate char-by-char into broken single-letter args.
+ # After an AMD ROCm fallback, force-reinstall so a partial ROCm torch (which satisfies the
+ # CPU torch>= range) is replaced by the CPU build; skip on a genuine CPU host to stay fast.
+ # $ROCmCpuFallback matters when a PINNED ROCm index failed ($CuTag is still the rocm leaf).
+ # Build the array directly: an if-expression collapses @("x") to a scalar @splat would
+ # enumerate char-by-char.
$cpuForce = @()
if ($ROCmCpuFallback) { $cpuForce = @("--force-reinstall") }
+ # --force-reinstall on a pin change: a stale +cu / +rocm wheel still satisfies the CPU
+ # torch>= range, so uv would keep it and only swap companions.
+ if ($script:PinChangedForceReinstall) { $cpuForce = @("--force-reinstall") }
+ # A PINNED cpu index installs the bounded trio (parity with _CPU_TORCH_PKG_SPEC): the /cpu
+ # index serves newer torch, and _ensure_cpu_torch keeps any CPU build, so a bare trio could
+ # land an unsupported version. Unpinned CPU hosts keep the bare trio (pre-pin behavior).
+ $cpuTorchSpec = "torch"; $cpuVisionSpec = "torchvision"; $cpuAudioSpec = "torchaudio"
+ if ($TorchIndexPinned) {
+ $cpuTorchSpec = "torch>=2.4,<2.12.0"
+ $cpuVisionSpec = "torchvision>=0.19,<0.27.0"
+ $cpuAudioSpec = "torchaudio>=2.4,<2.12.0"
+ }
if ($script:UnslothVerbose) {
- Fast-Install torch torchvision torchaudio @cpuForce --index-url "$PyTorchWhlBase/cpu"
+ Fast-Install $cpuTorchSpec $cpuVisionSpec $cpuAudioSpec @cpuForce --index-url $TorchInstallIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
$torchInstallExit = $LASTEXITCODE
$output = ""
} else {
- $output = Fast-Install torch torchvision torchaudio @cpuForce --index-url "$PyTorchWhlBase/cpu" | Out-String
+ $output = Fast-Install $cpuTorchSpec $cpuVisionSpec $cpuAudioSpec @cpuForce --index-url $TorchInstallIndexUrl | Out-String
$torchInstallExit = $LASTEXITCODE
}
if ($torchInstallExit -ne 0) {
Write-Host "[FAILED] PyTorch install failed (exit code $torchInstallExit)" -ForegroundColor Red
- Write-Host $output -ForegroundColor Red
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Red
exit 1
}
} elseif (-not $ROCmIndexUrl) {
substep "installing PyTorch with CUDA support ($CuTag)..."
substep "(This download is ~2.8 GB -- may take a few minutes)"
+ # --force-reinstall on a pin change: an installed cuXXX wheel satisfies the bare torch
+ # requirement (PEP 440 ignores the +cuXXX tag), so without it a changed CUDA pin (cu126
+ # -> cu128) never applies.
+ $cudaForce = @()
+ if ($script:PinChangedForceReinstall) { $cudaForce = @("--force-reinstall") }
+ # An unknown-leaf custom pin (/simple, /current) routes here with $CuTag as that leaf. Bound
+ # the trio like the fresh custom-pin paths so a mirror can't pull an ABI-newer companion
+ # against the capped torch. Known cu* leaves keep bare specs.
+ $cudaTorchSpec = "torch"
+ $cudaVisionSpec = "torchvision"
+ $cudaAudioSpec = "torchaudio"
+ if ($TorchIndexPinned -and -not (Test-CudaFamilyLeaf $CuTag)) {
+ $cudaTorchSpec = "torch>=2.4,<2.11.0"
+ $cudaVisionSpec = "torchvision>=0.19,<0.26.0"
+ $cudaAudioSpec = "torchaudio>=2.4,<2.11.0"
+ }
if ($script:UnslothVerbose) {
- Fast-Install torch torchvision torchaudio --index-url "$PyTorchWhlBase/$CuTag"
+ Fast-Install $cudaTorchSpec $cudaVisionSpec $cudaAudioSpec @cudaForce --index-url $TorchInstallIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
$torchInstallExit = $LASTEXITCODE
$output = ""
} else {
- $output = Fast-Install torch torchvision torchaudio --index-url "$PyTorchWhlBase/$CuTag" | Out-String
+ $output = Fast-Install $cudaTorchSpec $cudaVisionSpec $cudaAudioSpec @cudaForce --index-url $TorchInstallIndexUrl | Out-String
$torchInstallExit = $LASTEXITCODE
}
if ($torchInstallExit -ne 0) {
Write-Host "[FAILED] PyTorch CUDA install failed (exit code $torchInstallExit)" -ForegroundColor Red
- Write-Host $output -ForegroundColor Red
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Red
exit 1
}
@@ -2925,7 +3251,7 @@ if (-not $ROCmIndexUrl -and $CuTag -eq "cpu") {
}
if ($tritonInstallExit -ne 0) {
substep "Triton install failed -- torch.compile may not work" "Yellow"
- Write-Host $output -ForegroundColor Yellow
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Yellow
} else {
substep "Triton for Windows installed (enables torch.compile)"
}
@@ -3022,7 +3348,7 @@ foreach ($pkg in @("transformers==5.3.0", "huggingface_hub==1.8.0", "hf_xet==1.4
}
if ($t5PkgExit -ne 0) {
Write-Host "[FAIL] Could not install $pkg into .venv_t5_530/" -ForegroundColor Red
- Write-Host $output -ForegroundColor Red
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Red
$ErrorActionPreference = $prevEAP_t5
exit 1
}
@@ -3057,7 +3383,7 @@ foreach ($pkg in @("transformers==5.5.0", "huggingface_hub==1.8.0", "hf_xet==1.4
}
if ($t5PkgExit -ne 0) {
Write-Host "[FAIL] Could not install $pkg into .venv_t5_550/" -ForegroundColor Red
- Write-Host $output -ForegroundColor Red
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Red
$ErrorActionPreference = $prevEAP_t5
exit 1
}
@@ -3092,7 +3418,7 @@ foreach ($pkg in @("transformers==5.10.2", "huggingface_hub==1.8.0", "hf_xet==1.
}
if ($t5PkgExit -ne 0) {
Write-Host "[FAIL] Could not install $pkg into .venv_t5_510/" -ForegroundColor Red
- Write-Host $output -ForegroundColor Red
+ Write-Host (Redact-InstallOutput $output) -ForegroundColor Red
$ErrorActionPreference = $prevEAP_t5
exit 1
}
@@ -3367,6 +3693,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/python/test_cross_platform_parity.py b/tests/python/test_cross_platform_parity.py
index 666ea7ce10..b3a9b99c55 100644
--- a/tests/python/test_cross_platform_parity.py
+++ b/tests/python/test_cross_platform_parity.py
@@ -10,6 +10,8 @@ import pytest
REPO_ROOT = Path(__file__).resolve().parents[2]
INSTALL_SH = REPO_ROOT / "install.sh"
INSTALL_PS1 = REPO_ROOT / "install.ps1"
+SETUP_PS1 = REPO_ROOT / "studio" / "setup.ps1"
+STACK_PY = REPO_ROOT / "studio" / "install_python_stack.py"
class TestNoTorchBackendAutoInInstallSh:
@@ -180,3 +182,607 @@ class TestUvBytecodeCompileTimeout:
assert (
'$env:UV_COMPILE_BYTECODE_TIMEOUT = "180"' in text
), "install.ps1 should default UV_COMPILE_BYTECODE_TIMEOUT"
+
+
+class TestTorchIndexOverrideParity:
+ """Every installer must honor UNSLOTH_TORCH_INDEX_URL / _FAMILY so a pinned wheel
+ index wins over GPU probing on all platforms (no asymmetric, per-OS coverage)."""
+
+ @pytest.mark.parametrize(
+ "path",
+ [INSTALL_SH, INSTALL_PS1, SETUP_PS1, STACK_PY],
+ ids = ["install.sh", "install.ps1", "setup.ps1", "install_python_stack.py"],
+ )
+ def test_installer_reads_override_env(self, path):
+ text = path.read_text(encoding = "utf-8")
+ for var in ("UNSLOTH_TORCH_INDEX_URL", "UNSLOTH_TORCH_INDEX_FAMILY"):
+ assert var in text, f"{path.name} does not honor {var}"
+
+ @pytest.mark.parametrize(
+ "path",
+ [INSTALL_PS1, SETUP_PS1],
+ ids = ["install.ps1", "setup.ps1"],
+ )
+ def test_amd_reroute_guarded_when_pinned(self, path):
+ # The AMD ROCm reroute must be skipped when the index is explicitly pinned,
+ # so an explicit cpu / cu* / rocm pin on an AMD host is not overwritten.
+ text = path.read_text(encoding = "utf-8")
+ assert (
+ "TorchIndexPinned" in text
+ ), f"{path.name} should gate the AMD ROCm reroute on a pinned-index flag"
+
+ def test_cuda_pin_overrides_cvd_hide_gate(self):
+ # A pinned cu* index skips ALL host-GPU probing, so the CUDA repair must clear the
+ # CUDA_VISIBLE_DEVICES hide gate too (else the GPU-less CI case bails).
+ text = STACK_PY.read_text(encoding = "utf-8")
+ m = re.search(r"def _ensure_cuda_torch\(\).*?(?=\ndef )", text, re.DOTALL)
+ assert m, "could not locate _ensure_cuda_torch"
+ body = m.group(0)
+ assert "_cuda_pinned" in body, (
+ "_ensure_cuda_torch should compute a CUDA-pin flag so the pin can "
+ "override the CVD hide gate"
+ )
+ assert re.search(
+ r"if not _cuda_pinned and _cvd is not None", body
+ ), "the CVD hide gate must be bypassed when a CUDA index is pinned"
+
+ def test_cpu_repair_pins_supported_torch_range(self):
+ # The explicit-CPU repair must use the bounded CPU/CUDA spec, not a bare trio (the
+ # /cpu index serves torch 2.11+, so a bare install could resolve out of range).
+ text = STACK_PY.read_text(encoding = "utf-8")
+ m = re.search(r"def _ensure_cpu_torch\(\).*?(?=\ndef )", text, re.DOTALL)
+ assert m, "could not locate _ensure_cpu_torch"
+ body = m.group(0)
+ assert "_CPU_TORCH_PKG_SPEC" in body, (
+ "_ensure_cpu_torch should install the bounded _CPU_TORCH_PKG_SPEC, "
+ "not a bare torch/torchvision/torchaudio trio"
+ )
+
+ def test_setup_ps1_stale_check_gates_rocm_on_supported_arch(self):
+ # The stale check must expect ROCm torch only for arches the install path maps to a
+ # repo.amd.com index; expecting "rocm" for an unmapped arch marks a good CPU venv stale.
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ assert "_rocmWheelArches" in text, (
+ "setup.ps1 stale check should restrict the ROCm expected-tag to the "
+ "supported gfx wheel arches"
+ )
+
+
+class TestGfx211AllowlistParity:
+ """The gfx per-arch 2.11-floor leaves (gfx120X-all / gfx1151 / gfx1150) must be the
+ SAME set in every installer and its stale/mismatch check. When they diverged, a
+ pinned gfx110X-all / gfx90a / gfx908 wheel (<2.11) was force-reinstalled every update."""
+
+ EXPECTED = {"gfx120x-all", "gfx1151", "gfx1150"}
+
+ def test_install_sh_allowlist(self):
+ text = INSTALL_SH.read_text(encoding = "utf-8").lower()
+ # install.sh: the TORCH_CONSTRAINT case (rocm7.2|gfx120x-all|gfx1151|gfx1150).
+ m = re.search(r"rocm7\.2\|gfx120x-all\|gfx1151\|gfx1150", text)
+ assert m, "install.sh gfx-2.11 allowlist case not found / changed"
+
+ def test_install_ps1_allowlist(self):
+ text = INSTALL_PS1.read_text(encoding = "utf-8").lower()
+ m = re.search(r"@\('gfx120x-all',\s*'gfx1151',\s*'gfx1150'\)", text)
+ assert m, "install.ps1 $_pinGfx211 allowlist not found / changed"
+
+ def test_setup_ps1_defines_single_allowlist_helper(self):
+ # setup.ps1 must define the allowlist once (Test-RocmGfx211Leaf) and reuse it, so
+ # the stale check and install spec can't disagree.
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ assert (
+ "function Test-RocmGfx211Leaf" in text
+ ), "setup.ps1 should define a single Test-RocmGfx211Leaf allowlist helper"
+ assert re.search(
+ r"@\('gfx120x-all',\s*'gfx1151',\s*'gfx1150'\)", text.lower()
+ ), "Test-RocmGfx211Leaf should hold the gfx-2.11 allowlist"
+ assert "$_pinGfx211 = Test-RocmGfx211Leaf" in text, (
+ "setup.ps1 install-spec path should reuse Test-RocmGfx211Leaf, not "
+ "re-hardcode the allowlist (they must not diverge)"
+ )
+
+ def test_stack_py_allowlist(self):
+ text = STACK_PY.read_text(encoding = "utf-8").lower()
+ assert (
+ '"gfx120x-all", "gfx1151", "gfx1150"' in text
+ ), "install_python_stack.py _ROCM_GFX_TORCH211_LEAVES not found / changed"
+
+
+class TestCudaLeafDigitParity:
+ """A wheel-family leaf is CUDA only when it is "cu" + digits (cu118/cu128/...).
+ A bare cu* glob wrongly catches mirror leaves like /custom or /current; when
+ that happened the venv was marked stale and rebuilt on every run. Every
+ installer must require a digit after "cu" in its family/CUDA classification."""
+
+ def test_stack_py_requires_cu_digit(self):
+ text = STACK_PY.read_text(encoding = "utf-8")
+ # EXACT cu+digits: a custom leaf like cu128-private must route to the
+ # verbatim/unknown path, not be compared against the installed +cu128 tag.
+ assert re.search(
+ r'r"cu\[0-9\]\+"', text
+ ), "install_python_stack.py _is_cuda_family_leaf must fullmatch cu[0-9]+"
+
+ def test_setup_ps1_requires_cu_digit(self):
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ # EXACT cu+digits: cu128-private must not classify as CUDA (it would become
+ # the expected tag and rebuild the venv on every update).
+ assert re.search(
+ r"'\^cu\[0-9\]\+\$'", text
+ ), "setup.ps1 Test-CudaFamilyLeaf must match ^cu[0-9]+$, not a cu* prefix"
+ # The stale-venv branch must go through the digit-guarded helper.
+ assert (
+ "Test-CudaFamilyLeaf $_pinLeaf" in text
+ ), "setup.ps1 stale check should classify CUDA via Test-CudaFamilyLeaf"
+
+ def test_install_ps1_requires_cu_digit_in_gpu_branch(self):
+ text = INSTALL_PS1.read_text(encoding = "utf-8")
+ assert re.search(
+ r"'\^cu\[0-9\]'", text
+ ), "install.ps1 Get-TauriGpuBranch must require a digit after cu"
+
+ def test_install_sh_requires_cu_digit_in_gpu_branch(self):
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ # The _tauri_gpu_branch cuda case must be cu[0-9]*, not a bare cu*.
+ assert re.search(
+ r"cu\[0-9\]\*\)\s*echo \"cuda\"", text
+ ), "install.sh _tauri_gpu_branch cuda case must be cu[0-9]*, not cu*"
+
+ def test_install_sh_backend_export_requires_cu_digit(self):
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ # Brand CUDA only on cu[0-9]*; a bare catch-all *) -> cuda would mis-brand
+ # /current, /custom pins and skip ROCm repair on AMD hosts.
+ assert re.search(
+ r'cu\[0-9\]\*\)\s*export UNSLOTH_TORCH_BACKEND="cuda"', text
+ ), "install.sh backend export must brand cuda only on cu[0-9]*"
+ # An unknown leaf must NOT commit a cuda backend (it unsets instead).
+ assert re.search(
+ r"\*\)\s*unset UNSLOTH_TORCH_BACKEND", text
+ ), "install.sh backend export must unset (not force cuda) on an unknown leaf"
+
+ def test_install_sh_lowercases_backend_leaf(self):
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ # The leaf feeding both the backend case and the 2.11 floor case must be
+ # lowercased so the canonical gfx120X-all (capital X) matches.
+ assert re.search(
+ r"_torch_index_leaf=\$\(printf '%s' \"\$_torch_index_leaf\" \| tr '\[:upper:\]' '\[:lower:\]'\)",
+ text,
+ ), "install.sh must lowercase _torch_index_leaf before the gfx/rocm/cu case matches"
+
+
+class TestKnown211SetParity:
+ """The KNOWN-2.11 rocm/gfx set must be identical across all four installers:
+ exactly {rocm7.2} plus the gfx allowlist {gfx120x-all, gfx1151, gfx1150}.
+ rocm7.3 / torch 2.12 do not exist, so no side may floor them speculatively."""
+
+ def test_install_sh_known_211_leaf_is_rocm72_and_gfx_allowlist(self):
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ # The 2.11 floor case matches exactly rocm7.2 + the three gfx leaves.
+ assert re.search(
+ r"rocm7\.2\|gfx120x-all\|gfx1151\|gfx1150\)", text
+ ), "install.sh 2.11 floor must be exactly rocm7.2|gfx120x-all|gfx1151|gfx1150"
+ # No speculative rocm7.3 anywhere.
+ assert "rocm7.3" not in text, "install.sh must not reference a non-existent rocm7.3"
+
+ def test_python_known_211_versions_is_only_rocm72(self):
+ text = STACK_PY.read_text(encoding = "utf-8")
+ assert "_ROCM_KNOWN_TORCH211_VERSIONS" in text
+ # The frozenset literal is exactly {(7, 2)}.
+ m = re.search(r"_ROCM_KNOWN_TORCH211_VERSIONS[^=]*=\s*frozenset\(\{([^}]*)\}\)", text)
+ assert m is not None, "install_python_stack.py must define _ROCM_KNOWN_TORCH211_VERSIONS"
+ assert "(7, 2)" in m.group(1)
+ assert "7, 3" not in m.group(1) and "7, 1" not in m.group(1)
+
+ def test_setup_ps1_known_211_helper_is_only_rocm72(self):
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ assert "Test-RocmKnown211Version" in text
+ # The predicate is Major -eq 7 -and Minor -eq 2 (only rocm7.2).
+ assert re.search(
+ r"Test-RocmKnown211Version[\s\S]{0,400}\$Major -eq 7 -and \$Minor -eq 2", text
+ ), "setup.ps1 Test-RocmKnown211Version must accept only rocm7.2"
+
+ def test_install_ps1_pin_floor_is_only_rocm72(self):
+ text = INSTALL_PS1.read_text(encoding = "utf-8")
+ # The pinned-ROCm install-spec floor must be Major -eq 7 -and Minor -eq 2,
+ # not the speculative >= 2 that would floor a non-existent rocm7.3.
+ assert re.search(
+ r"\$_pinRocm211 = \(\[int\]\$Matches\[1\] -eq 7 -and \[int\]\$Matches\[2\] -eq 2\)",
+ text,
+ ), "install.ps1 pinned-ROCm floor must be rocm7.2 only (no speculative >= 2)"
+
+ def test_ps1_pin_floor_gate_is_anchored(self):
+ """The floor-selection gate that reads $_pinRocm211 from the raw leaf must anchor
+ the rocm match ($), or a suffixed custom leaf (rocm7.2-private) matches the rocm7.2
+ prefix, takes the 2.11-floor branch, and is force-routed through the ROCm path
+ before the exact-match elseif can send it to the verbatim install (Codex P2)."""
+ for path, label in ((INSTALL_PS1, "install.ps1"), (SETUP_PS1, "setup.ps1")):
+ text = path.read_text(encoding = "utf-8")
+ assert "-match '^rocm(\\d+)\\.(\\d+)$'" in text, (
+ f"{label} floor gate must anchor the rocm match (^rocm(\\d+)\\.(\\d+)$) so a "
+ "suffixed custom leaf is not floored/routed as rocm7.2"
+ )
+ assert (
+ "-match '^rocm(\\d+)\\.(\\d+)'\n" not in text
+ ), f"{label} floor gate must not use the unanchored ^rocm(\\d+)\\.(\\d+) prefix"
+
+ def test_install_ps1_bounds_unknown_leaf_pinned_torch(self):
+ """install.ps1's pinned-torch install must bound BOTH companions on EVERY
+ index, cu families included: torchaudio 2.11 dropped its exact torch
+ pin from the wheel metadata, so a bare companion beside torch<2.11 can
+ resolve a mismatched 2.11.0 build (Codex P2, then unconditional per the
+ torchaudio 2.11 unpinning)."""
+ text = INSTALL_PS1.read_text(encoding = "utf-8")
+ assert (
+ '$_pinVisionSpec = "torchvision>=0.19,<0.26.0"' in text
+ ), "install.ps1 custom-pin install must bound torchvision (>=0.19,<0.26.0)"
+ assert (
+ '$_pinAudioSpec = "torchaudio>=2.4,<2.11.0"' in text
+ ), "install.ps1 custom-pin install must bound torchaudio (>=2.4,<2.11.0)"
+ # No cu-family exemption: the bounds apply unconditionally.
+ assert (
+ "$_pinCuLeaf" not in text
+ ), "install.ps1 must bound companions on every index (no cu-family exemption)"
+ # The bounded companions must actually be passed to the install command.
+ assert re.search(
+ r'"torch>=2\.4,<2\.11\.0" \$_pinVisionSpec \$_pinAudioSpec --default-index \$TorchIndexUrl',
+ text,
+ ), "install.ps1 custom-pin install must pass the bounded companion specs to uv"
+
+ def test_gfx_allowlist_matches_across_installers(self):
+ # The gfx 2.11 allowlist {gfx120x-all, gfx1151, gfx1150} must appear in each.
+ gfx = ("gfx120x-all", "gfx1151", "gfx1150")
+ for path, label in (
+ (INSTALL_SH, "install.sh"),
+ (INSTALL_PS1, "install.ps1"),
+ (SETUP_PS1, "setup.ps1"),
+ (STACK_PY, "install_python_stack.py"),
+ ):
+ low = path.read_text(encoding = "utf-8").lower()
+ for g in gfx:
+ assert g in low, f"{label} missing gfx 2.11 allowlist member {g}"
+
+
+class TestPinnedRocmLeafDigitParity:
+ """A pinned index is a pip ROCm --default-index family only when its leaf is an
+ EXACT rocm+digits (rocm7 / rocm7.2) or gfx*. A ^rocm[0-9] PREFIX (or a bare rocm*
+ glob) wrongly catches a custom mirror / find-links leaf (rocm-current /
+ rocm-rel-7.2.1) AND a suffixed private-mirror leaf (rocm7.2-private / rocm7-current),
+ routing it through the ROCm install path (which silently falls back to CPU on
+ failure) or skipping the custom-index companion bounds, instead of the verbatim
+ --default-index install. All installers must match the family EXACTLY: Python and
+ install.sh via a shared _is_pip_rocm_family_leaf, setup.ps1 via Test-PipRocmFamilyLeaf,
+ install.ps1 via an anchored ^rocm[0-9]+(\\.[0-9]+)?$ reroute."""
+
+ def test_install_ps1_pinned_reroute_requires_rocm_digit(self):
+ text = INSTALL_PS1.read_text(encoding = "utf-8")
+ # The pinned gfx*/rocm reroute must match rocm EXACTLY (anchored), so a suffixed
+ # rocm7.2-private / rocm-current falls through to the verbatim --default-index path.
+ assert "-match '^rocm[0-9]+(\\.[0-9]+)?$'" in text, (
+ "install.ps1 pinned-index reroute must anchor the rocm match "
+ "(^rocm[0-9]+(\\.[0-9]+)?$), not a bare -like 'rocm*' or an unanchored ^rocm\\d"
+ )
+ # Neither the broad glob nor the unanchored prefix may drive that reroute.
+ assert (
+ "-like 'rocm*'" not in text
+ ), "install.ps1 must not route a pinned index on a bare -like 'rocm*' glob"
+ assert (
+ "-match '^rocm\\d'" not in text
+ ), "install.ps1 must not route a pinned index on an unanchored -match '^rocm\\d'"
+
+ def test_setup_ps1_pinned_reroute_requires_rocm_digit(self):
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ # setup.ps1 routes every family decision through Test-PipRocmFamilyLeaf, which
+ # anchors the rocm match so a suffixed custom leaf stays on the verbatim path.
+ assert (
+ "function Test-PipRocmFamilyLeaf" in text
+ ), "setup.ps1 must define Test-PipRocmFamilyLeaf (the exact rocm/gfx family gate)"
+ assert "'^rocm[0-9]+(\\.[0-9]+)?$'" in text, (
+ "setup.ps1 Test-PipRocmFamilyLeaf must anchor the rocm match "
+ "(^rocm[0-9]+(\\.[0-9]+)?$) so rocm7.2-private / rocm-current stay verbatim"
+ )
+ pinned_block = text[text.find("$_pinGfx211 = Test-RocmGfx211Leaf") :][:2000]
+ assert (
+ "-like 'rocm*'" not in pinned_block
+ ), "setup.ps1 pinned reroute must not route on a bare -like 'rocm*' glob"
+
+ def test_install_sh_repairable_requires_rocm_digit(self):
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ # _torch_index_repairable routes rocm/gfx through the exact-match helper.
+ assert (
+ "_is_pip_rocm_family_leaf" in text
+ ), "install.sh must define/use _is_pip_rocm_family_leaf for the exact rocm gate"
+ # gfx needs a following digit: gfx-private / gfxfoo are custom verbatim pins.
+ assert re.search(
+ r'case "\$1" in\n\s*gfx\[0-9\]\*\) return 0', text
+ ), "install.sh _is_pip_rocm_family_leaf must treat only gfx* as a family"
+ assert not re.search(
+ r'case "\$1" in\n\s*gfx\*\) return 0', text
+ ), "install.sh _is_pip_rocm_family_leaf must not family-match a bare gfx* glob"
+
+ def test_stack_py_pip_rocm_family_requires_digit(self):
+ text = STACK_PY.read_text(encoding = "utf-8")
+ assert re.search(
+ r'fullmatch\(r"rocm\\d\+\(\?:\\\.\\d\+\)\?", leaf\)', text
+ ), "install_python_stack.py _is_pip_rocm_family_leaf must fullmatch rocm\\d+(?:\\.\\d+)?"
+ # The unanchored prefix must be gone from the family/flavor gates.
+ assert (
+ 're.match(r"^rocm\\d"' not in text
+ ), "install_python_stack.py must not gate a family on an unanchored re.match(^rocm\\d)"
+
+ def test_install_sh_rocm_side_effects_digit_gated(self):
+ """The AMD bitsandbytes + 'repair ROCm torch' side effects must fire only on
+ an EXACT ROCm family (rocm7.2/gfx*), not a bare */rocm* whole-URL glob nor a
+ ^rocm[0-9] prefix that catches a custom CPU/CUDA index like /rocm-current or a
+ suffixed /rocm7.2-private and force-repairs it from the wrong --default-index."""
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ assert (
+ 'if _is_pip_rocm_family_leaf "$_torch_index_leaf"; then\n _torch_index_is_rocm_family=true'
+ in text
+ ), "install.sh must set _torch_index_is_rocm_family from the exact-match helper"
+ assert (
+ '[ "$_torch_index_is_rocm_family" = true ]' in text
+ ), "install.sh ROCm bnb/repair hooks must gate on _torch_index_is_rocm_family"
+ assert (
+ "*/rocm*|*/gfx*)\n _install_bnb_rocm" not in text
+ ), "install.sh must not gate _install_bnb_rocm on a bare */rocm* whole-URL glob"
+
+
+class TestPinnedIndexClearsUvEnvParity:
+ """Every installer must neutralise the uv index env vars for a pinned torch
+ install (#6898). uv treats the default index (--index-url / --default-index) as
+ lowest priority, so an inherited UV_INDEX / UV_EXTRA_INDEX_URL mirror would win
+ under uv's first-index strategy and pull torch from the wrong index -- after
+ which the pinned wheel index is silently never used."""
+
+ UV_VARS = ("UV_DEFAULT_INDEX", "UV_INDEX_URL", "UV_INDEX", "UV_EXTRA_INDEX_URL")
+
+ def test_install_sh_clears_uv_index_vars(self):
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ assert (
+ "env -u UV_DEFAULT_INDEX -u UV_INDEX_URL -u UV_INDEX -u UV_EXTRA_INDEX_URL" in text
+ ), "install.sh run_install_cmd must clear the uv index vars for --default-index installs"
+
+ def test_install_ps1_clears_uv_index_vars(self):
+ text = INSTALL_PS1.read_text(encoding = "utf-8")
+ for var in self.UV_VARS:
+ assert var in text, f"install.ps1 must clear {var} for pinned installs"
+
+ def test_setup_ps1_clears_uv_index_vars(self):
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ for var in self.UV_VARS:
+ assert var in text, f"setup.ps1 must clear {var} for pinned installs"
+
+ def test_stack_py_clears_uv_index_vars(self):
+ text = STACK_PY.read_text(encoding = "utf-8")
+ assert "_install_env_for_cmd" in text, (
+ "install_python_stack.py must scrub inherited uv index vars for pinned "
+ "installs via _install_env_for_cmd (parity with install.sh #6898)"
+ )
+ for var in self.UV_VARS:
+ assert var in text, f"install_python_stack.py must clear {var} for pinned installs"
+
+ def test_all_installers_clear_uv_torch_backend(self):
+ """uv's torch backend redirects torch resolution to its own per-backend
+ index even against an explicit pin, so every installer's pinned-install
+ scrub must clear UV_TORCH_BACKEND too."""
+ sh = INSTALL_SH.read_text(encoding = "utf-8")
+ assert "-u UV_TORCH_BACKEND" in sh, "install.sh pinned scrub must clear UV_TORCH_BACKEND"
+ for path in (INSTALL_PS1, SETUP_PS1):
+ text = path.read_text(encoding = "utf-8")
+ assert (
+ "'UV_TORCH_BACKEND'" in text
+ ), f"{path.name} pinned scrub must clear UV_TORCH_BACKEND"
+ stack = STACK_PY.read_text(encoding = "utf-8")
+ assert (
+ '"UV_TORCH_BACKEND",' in stack
+ ), "install_python_stack.py strip tuple must include UV_TORCH_BACKEND"
+
+ def test_stack_py_strips_pip_extra_index_for_pip_fallback(self):
+ """The pip fallback honours PIP_EXTRA_INDEX_URL (pip adds it IN ADDITION
+ to --index-url), so the pinned-command scrub must strip it."""
+ stack = STACK_PY.read_text(encoding = "utf-8")
+ assert (
+ '"PIP_EXTRA_INDEX_URL",' in stack
+ ), "install_python_stack.py strip tuple must include PIP_EXTRA_INDEX_URL"
+
+ def test_all_installers_scrub_find_links(self):
+ """uv's --find-links (env UV_FIND_LINKS) adds candidate locations that can
+ satisfy torch off a pinned index; every pinned-install scrub must clear it."""
+ sh = INSTALL_SH.read_text(encoding = "utf-8")
+ assert "-u UV_FIND_LINKS" in sh
+ for path in (INSTALL_PS1, SETUP_PS1):
+ assert "'UV_FIND_LINKS'" in path.read_text(encoding = "utf-8"), path.name
+ stack = STACK_PY.read_text(encoding = "utf-8")
+ assert '"UV_FIND_LINKS",' in stack and '"PIP_FIND_LINKS",' in stack
+
+ def test_setup_ps1_scrub_covers_pip_fallback(self):
+ """setup.ps1's Fast-Install must keep the scrub active through the pip
+ fallback (pip honours PIP_EXTRA_INDEX_URL / PIP_FIND_LINKS in addition to
+ --index-url); restoring the vars before the fallback reopens the hole."""
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ fi = text[text.find("function Fast-Install") :][:2500]
+ assert "'PIP_EXTRA_INDEX_URL'" in fi and "'PIP_FIND_LINKS'" in fi
+ # the pip fallback must sit INSIDE the try whose finally restores the vars
+ assert fi.find("python -m pip install") < fi.find(
+ "finally"
+ ), "pip fallback must run before the scrub is restored"
+
+ def test_all_installers_disable_uv_config_for_pinned_installs(self):
+ """A DISCOVERED uv.toml / pyproject [tool.uv] outranks the CLI pin
+ (verified with uv 0.10: [pip] torch-backend = "cpu" and a non-default
+ [[index]] both resolve torch+cpu against an explicit --index-url /
+ --default-index cu126 pin; UV_NO_CONFIG=1 restores the pin). Every
+ installer's pinned scrub must set UV_NO_CONFIG=1 and drop UV_CONFIG_FILE."""
+ sh = INSTALL_SH.read_text(encoding = "utf-8")
+ assert "-u UV_CONFIG_FILE UV_NO_CONFIG=1" in sh, (
+ "install.sh run_install_cmd must set UV_NO_CONFIG=1 and drop "
+ "UV_CONFIG_FILE for --default-index installs"
+ )
+ for path in (INSTALL_PS1, SETUP_PS1):
+ text = path.read_text(encoding = "utf-8")
+ assert "'UV_CONFIG_FILE'" in text, f"{path.name} must drop UV_CONFIG_FILE"
+ assert (
+ "$env:UV_NO_CONFIG = '1'" in text
+ ), f"{path.name} must set UV_NO_CONFIG=1 for pinned installs"
+ stack = STACK_PY.read_text(encoding = "utf-8")
+ assert (
+ '"UV_CONFIG_FILE",' in stack
+ ), "install_python_stack.py strip tuple must include UV_CONFIG_FILE"
+ assert (
+ 'env["UV_NO_CONFIG"] = "1"' in stack
+ ), "_install_env_for_cmd must set UV_NO_CONFIG=1 for pinned installs"
+
+ def test_pip_fallbacks_disable_pip_config_files(self):
+ """The pip FALLBACK (uv missing/failed) honours user/site pip config files
+ even with the PIP_* env vars stripped: `pip config set
+ global.extra-index-url` still adds indexes to a pinned install. pip loads
+ NO configuration files when PIP_CONFIG_FILE is the platform devnull, so
+ the two installers that HAVE a pip fallback (install_python_stack.py and
+ setup.ps1's Fast-Install) must set it in their pinned scrub. install.sh
+ and install.ps1 are uv-only (no python -m pip fallback) and need no
+ equivalent."""
+ stack = STACK_PY.read_text(encoding = "utf-8")
+ assert 'env["PIP_CONFIG_FILE"] = os.devnull' in stack, (
+ "_install_env_for_cmd must point PIP_CONFIG_FILE at os.devnull for "
+ "pinned installs (pip fallback isolation)"
+ )
+ setup = SETUP_PS1.read_text(encoding = "utf-8")
+ assert "$env:PIP_CONFIG_FILE = 'nul'" in setup, (
+ "setup.ps1 Fast-Install pinned scrub must point PIP_CONFIG_FILE at nul "
+ "(Windows devnull) so the pip fallback ignores user/site pip config"
+ )
+ assert (
+ "'PIP_CONFIG_FILE'" in setup
+ ), "setup.ps1 must save/restore PIP_CONFIG_FILE around the pinned scrub"
+
+ def test_setup_ps1_bounds_unknown_leaf_pinned_torch(self):
+ """A first-time/changed unknown-leaf custom pin routes through setup.ps1's
+ CUDA branch; install.ps1's fresh pinned install, install.sh, and the Python
+ verbatim path bound the WHOLE trio, so the Windows update path must too -- a
+ private mirror serving newer torch OR newer companions must not lift the venv
+ above the supported range under the pin."""
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ # The custom-leaf branch bounds torch AND both companions (parity with the
+ # other installers' custom-pin trio bounds), gated on a non-cu-family leaf.
+ for spec in (
+ '$cudaTorchSpec = "torch>=2.4,<2.11.0"',
+ '$cudaVisionSpec = "torchvision>=0.19,<0.26.0"',
+ '$cudaAudioSpec = "torchaudio>=2.4,<2.11.0"',
+ ):
+ assert spec in text, f"setup.ps1 must bound the custom-leaf trio: {spec}"
+ assert (
+ "if ($TorchIndexPinned -and -not (Test-CudaFamilyLeaf $CuTag)) {" in text
+ ), "the custom-leaf trio bounds must be gated on a pinned non-cu-family leaf"
+ assert (
+ "Fast-Install $cudaTorchSpec $cudaVisionSpec $cudaAudioSpec" in text
+ ), "setup.ps1's CUDA branch must install via the bounded spec variables"
+
+ def test_setup_ps1_bounds_pinned_cpu_torch(self):
+ """setup.ps1's CPU branch must bound the trio under an explicit pin (parity with
+ _CPU_TORCH_PKG_SPEC): the /cpu index serves newer torch, and _ensure_cpu_torch
+ keeps any CPU build, so a bare pinned trio could land an unsupported version.
+ An unpinned CPU host keeps the bare trio (pre-pin behavior unchanged)."""
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ for spec in (
+ '$cpuTorchSpec = "torch>=2.4,<2.12.0"',
+ '$cpuVisionSpec = "torchvision>=0.19,<0.27.0"',
+ '$cpuAudioSpec = "torchaudio>=2.4,<2.12.0"',
+ ):
+ assert spec in text, f"setup.ps1 must bound the pinned CPU trio: {spec}"
+ assert (
+ "if ($TorchIndexPinned) {" in text
+ ), "the CPU trio bounds must be gated on an explicit pin"
+ assert (
+ "Fast-Install $cpuTorchSpec $cpuVisionSpec $cpuAudioSpec @cpuForce" in text
+ ), "setup.ps1's CPU branch must install via the spec variables"
+ # The ceilings mirror the Python repair spec exactly.
+ stack = STACK_PY.read_text(encoding = "utf-8")
+ spec_block = re.search(r"_CUDA_TORCH_PKG_SPEC[^(]*\(\s*(.*?)\)", stack, re.DOTALL)
+ assert spec_block and '"torch>=2.4,<2.12.0"' in spec_block.group(1), (
+ "_CPU_TORCH_PKG_SPEC (via _CUDA_TORCH_PKG_SPEC) must keep the torch<2.12 "
+ "ceiling the setup.ps1 pinned CPU branch mirrors"
+ )
+
+ def test_setup_ps1_stale_check_requires_rocm_digit(self):
+ """The stale-venv check must use the same EXACT rocm/gfx gate as the install
+ selection (Test-PipRocmFamilyLeaf), or a custom rocm-* / suffixed rocm7.2-private
+ leaf is stale-compared as a family and force-reinstalls on every studio update."""
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ anchor = text.find("$_pinLeaf = Get-TorchIndexLeaf $_pinnedIdx")
+ assert anchor >= 0, "setup.ps1 stale check must classify the pinned leaf"
+ stale = text[anchor:][:2500]
+ assert (
+ "Test-PipRocmFamilyLeaf" in stale
+ ), "setup.ps1 stale check must gate rocm leaves via the exact Test-PipRocmFamilyLeaf"
+ assert (
+ stale.count("-like 'rocm*'") == 0
+ ), "setup.ps1 stale check must not use a bare -like 'rocm*' glob"
+ assert (
+ "-match '^rocm\\d'" not in stale
+ ), "setup.ps1 stale check must not use an unanchored -match '^rocm\\d'"
+
+
+class TestIndexPathSlashTrimParity:
+ """Every installer must trim trailing PATH slashes only on the verbatim
+ UNSLOTH_TORCH_INDEX_URL override, preserving a ?query/#fragment token: a whole-URL
+ strip corrupts a base64 token ending in "/", a single strip leaves a double-slash leaf
+ empty. The helper must be DEFINED and WIRED into the override return in all four."""
+
+ def test_helper_defined_in_all_installers(self):
+ assert "def _trim_index_path_slashes(" in STACK_PY.read_text(encoding = "utf-8")
+ assert "_trim_index_path_slashes()" in INSTALL_SH.read_text(encoding = "utf-8")
+ assert "function Trim-IndexPathSlashes" in INSTALL_PS1.read_text(encoding = "utf-8")
+ assert "function Trim-IndexPathSlashes" in SETUP_PS1.read_text(encoding = "utf-8")
+
+ def test_helper_wired_into_override_in_all_installers(self):
+ assert "_trim_index_path_slashes(url)" in STACK_PY.read_text(encoding = "utf-8")
+ assert '_url=$(_trim_index_path_slashes "$_url")' in INSTALL_SH.read_text(encoding = "utf-8")
+ assert "Trim-IndexPathSlashes $env:UNSLOTH_TORCH_INDEX_URL" in INSTALL_PS1.read_text(
+ encoding = "utf-8"
+ )
+ assert "Trim-IndexPathSlashes $env:UNSLOTH_TORCH_INDEX_URL" in SETUP_PS1.read_text(
+ encoding = "utf-8"
+ )
+
+
+class TestInstallOutputRedactionParity:
+ """uv/pip failure text embeds the failing --index-url verbatim, so a captured install
+ log dumped on error can leak a user:token@ or ?token= secret. Every installer must
+ DEFINE a redaction helper and WIRE it into the captured-output print path."""
+
+ def test_helper_defined_in_all_installers(self):
+ assert "def _redact_install_output(" in STACK_PY.read_text(encoding = "utf-8")
+ assert "_redact_install_output()" in INSTALL_SH.read_text(encoding = "utf-8")
+ assert "function Redact-InstallOutput" in INSTALL_PS1.read_text(encoding = "utf-8")
+ assert "function Redact-InstallOutput" in SETUP_PS1.read_text(encoding = "utf-8")
+
+ def test_helper_wired_into_failure_print(self):
+ # install.sh dumps the captured log through the redactor on failure.
+ assert '_redact_install_output "$_log"' in INSTALL_SH.read_text(encoding = "utf-8")
+ # Both ps1 installers redact the captured $output before Write-Host on non-zero exit.
+ assert (
+ "Write-Host (Redact-InstallOutput $output) -ForegroundColor Red"
+ in INSTALL_PS1.read_text(encoding = "utf-8")
+ )
+ assert (
+ "Write-Host (Redact-InstallOutput $output) -ForegroundColor Red"
+ in SETUP_PS1.read_text(encoding = "utf-8")
+ )
+ # Python redacts the captured stdout before printing.
+ assert "_redact_install_output(" in STACK_PY.read_text(encoding = "utf-8")
+
+
+class TestPipNoIndexScrubParity:
+ """The plain-pip fallback honours PIP_*: PIP_NO_INDEX=1 makes it ignore ALL indexes
+ (defeating the pinned --index-url) and PIP_INDEX_URL replaces the pin. The two installers
+ that HAVE a plain-pip fallback (Python + setup.ps1) must scrub both for a pinned install.
+ install.sh / install.ps1 are uv-only (--default-index), which ignores pip config/env."""
+
+ def test_python_scrubs_pip_no_index_and_pip_index_url(self):
+ text = STACK_PY.read_text(encoding = "utf-8")
+ assert '"PIP_NO_INDEX"' in text
+ assert '"PIP_INDEX_URL"' in text
+
+ def test_setup_ps1_scrubs_pip_no_index_and_pip_index_url(self):
+ text = SETUP_PS1.read_text(encoding = "utf-8")
+ assert "'PIP_NO_INDEX'" in text
+ assert "'PIP_INDEX_URL'" in text
diff --git a/tests/python/test_install_python_stack.py b/tests/python/test_install_python_stack.py
index 9015ff8c9d..3a12e53f95 100644
--- a/tests/python/test_install_python_stack.py
+++ b/tests/python/test_install_python_stack.py
@@ -54,6 +54,24 @@ class TestBuildUvCmdTorchBackend:
a.startswith("--torch-backend") for a in cmd
), f"Empty UV_TORCH_BACKEND should not add flag, got: {cmd}"
+ def test_uv_torch_backend_skipped_for_pinned_index(self):
+ """A pinned-index command must NOT get --torch-backend: uv's torch backend
+ redirects torch resolution to its own per-backend index even when
+ --index-url is given (verified: cu128 pin + backend cpu installs
+ torch+cpu), defeating the pin."""
+ for pin_flag in ("--index-url", "--default-index"):
+ with mock.patch.dict(os.environ, {"UV_TORCH_BACKEND": "cpu"}):
+ cmd = self._call(("torch", pin_flag, "https://download.pytorch.org/whl/cu128"))
+ assert not any(
+ a.startswith("--torch-backend") for a in cmd
+ ), f"{pin_flag} command must not carry --torch-backend, got: {cmd}"
+
+ def test_uv_torch_backend_kept_for_unpinned(self):
+ """Non-pinned commands still honour UV_TORCH_BACKEND."""
+ with mock.patch.dict(os.environ, {"UV_TORCH_BACKEND": "cpu"}):
+ cmd = self._call(("somepackage",))
+ assert "--torch-backend=cpu" in cmd
+
class TestUvSafePath:
"""_uv_safe_path hands uv a space-free `-c`/`-r` path (issue #6503)."""
@@ -148,3 +166,119 @@ class TestUvSafePathHardening:
assert " " not in value
assert Path(value).read_text() == "transformers>=4.57.6\n"
+
+
+class TestPinnedIndexClearsUvEnv:
+ """A pinned torch install (--index-url / --default-index) must neutralise an
+ inherited UV_INDEX / UV_EXTRA_INDEX_URL so the pinned wheel index wins.
+
+ uv treats the default index (--index-url / --default-index) as LOWEST priority,
+ so an inherited UV_INDEX / UV_EXTRA_INDEX_URL (a corporate/CPU mirror) would be
+ searched first and, under uv's default first-index strategy, resolve torch from
+ the wrong mirror -- after which the marker records a wheel index that was never
+ used. install.sh (#6898), install.ps1 and setup.ps1 already clear these for
+ pinned installs; install_python_stack must match (parity across all installers).
+ """
+
+ UV_VARS = ("UV_DEFAULT_INDEX", "UV_INDEX_URL", "UV_INDEX", "UV_EXTRA_INDEX_URL")
+
+ def test_pinned_index_url_strips_uv_index_vars(self):
+ cmd = [
+ "uv",
+ "pip",
+ "install",
+ "--force-reinstall",
+ "torch",
+ "torchvision",
+ "torchaudio",
+ "--index-url",
+ "https://download.pytorch.org/whl/cu128",
+ ]
+ with mock.patch.dict(
+ os.environ,
+ {
+ "UV_INDEX": "https://mirror.corp/simple",
+ "UV_EXTRA_INDEX_URL": "https://mirror.corp/extra",
+ "UV_INDEX_URL": "https://mirror.corp/root",
+ "UV_DEFAULT_INDEX": "https://mirror.corp/default",
+ },
+ ):
+ env = ips._install_env_for_cmd(cmd)
+ assert env is not None, "a --index-url install must run with a scrubbed env"
+ for var in self.UV_VARS:
+ assert var not in env, f"{var} must be cleared for a pinned-index install"
+
+ def test_pinned_default_index_strips_uv_index_vars(self):
+ # --default-index must be gated too (matches install.sh / install.ps1).
+ cmd = ["uv", "pip", "install", "torch", "--default-index", "https://x/cu126"]
+ with mock.patch.dict(os.environ, {"UV_INDEX": "https://mirror.corp/simple"}):
+ env = ips._install_env_for_cmd(cmd)
+ assert env is not None
+ assert "UV_INDEX" not in env
+
+ def test_non_pinned_install_keeps_user_mirror(self):
+ # A plain install (no --index-url) must NOT scrub the env, so a user's mirror
+ # still applies to base packages.
+ cmd = ["uv", "pip", "install", "unsloth", "unsloth-zoo"]
+ with mock.patch.dict(os.environ, {"UV_INDEX": "https://mirror.corp/simple"}):
+ env = ips._install_env_for_cmd(cmd)
+ assert env is None, "non-pinned installs must inherit the caller env unchanged"
+
+ def test_scrubbed_env_preserves_other_vars(self):
+ cmd = ["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"]
+ with mock.patch.dict(
+ os.environ,
+ {"UV_INDEX": "https://mirror.corp/simple", "PATH_SENTINEL_XYZ": "keepme"},
+ ):
+ env = ips._install_env_for_cmd(cmd)
+ assert env is not None
+ assert env.get("PATH_SENTINEL_XYZ") == "keepme", "only uv index vars are removed"
+
+ def test_pinned_cmd_strips_pip_extra_index_url(self):
+ """PIP_EXTRA_INDEX_URL is stripped for pinned commands so the pip
+ fallback cannot satisfy torch from an inherited extra index."""
+ with mock.patch.dict(os.environ, {"PIP_EXTRA_INDEX_URL": "https://mirror/simple"}):
+ env = ips._install_env_for_cmd(
+ ["pip", "install", "torch", "--index-url", "https://x/cu128"]
+ )
+ assert env is not None and "PIP_EXTRA_INDEX_URL" not in env
+
+ def test_pinned_cmd_strips_uv_torch_backend(self):
+ """UV_TORCH_BACKEND is stripped for pinned commands so uv cannot read it
+ from the environment and reroute torch off the pinned index."""
+ with mock.patch.dict(os.environ, {"UV_TORCH_BACKEND": "cpu"}):
+ env = ips._install_env_for_cmd(
+ ["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"]
+ )
+ assert env is not None and "UV_TORCH_BACKEND" not in env
+
+ def test_pinned_cmd_disables_uv_config_discovery(self):
+ """A DISCOVERED uv.toml / pyproject [tool.uv] outranks the CLI pin too
+ (verified with uv 0.10: [pip] torch-backend = "cpu" and a non-default
+ [[index]] both resolve torch+cpu against an explicit --index-url /
+ --default-index cu126 pin). Pinned commands must run with UV_NO_CONFIG=1
+ and without an inherited UV_CONFIG_FILE."""
+ with mock.patch.dict(os.environ, {"UV_CONFIG_FILE": "/etc/uv/uv.toml"}):
+ env = ips._install_env_for_cmd(
+ ["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"]
+ )
+ assert env is not None
+ assert env.get("UV_NO_CONFIG") == "1"
+ assert "UV_CONFIG_FILE" not in env
+
+ def test_pinned_cmd_disables_pip_config_files(self):
+ """The pip FALLBACK honours user/site pip config files (pip config set
+ global.extra-index-url) even with the PIP_* env vars stripped; pip loads
+ NO configuration files when PIP_CONFIG_FILE is os.devnull. Harmless for
+ uv, decisive for the fallback."""
+ env = ips._install_env_for_cmd(
+ ["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"]
+ )
+ assert env is not None
+ assert env.get("PIP_CONFIG_FILE") == os.devnull
+
+ def test_non_pinned_cmd_keeps_uv_config_discovery(self):
+ """Non-pinned installs inherit the caller env unchanged, so a user's uv
+ configuration still applies to base packages."""
+ env = ips._install_env_for_cmd(["uv", "pip", "install", "unsloth"])
+ assert env is None
diff --git a/tests/run_all.sh b/tests/run_all.sh
index d03f4c4d4f..a31103a85b 100755
--- a/tests/run_all.sh
+++ b/tests/run_all.sh
@@ -15,6 +15,7 @@ sh "$TESTS_DIR/sh/test_resolve_cuda_archs.sh"
sh "$TESTS_DIR/sh/test_strixhalo_wsl_reroute.sh"
sh "$TESTS_DIR/sh/test_uninstall_shared_icon.sh"
sh "$TESTS_DIR/sh/test_torch_flavor.sh"
+sh "$TESTS_DIR/sh/test_redact_install_output.sh"
sh "$TESTS_DIR/sh/test_install_uv_override_space.sh"
echo ""
diff --git a/tests/sh/test_get_torch_index_url.sh b/tests/sh/test_get_torch_index_url.sh
index 6656142625..23902097ef 100755
--- a/tests/sh/test_get_torch_index_url.sh
+++ b/tests/sh/test_get_torch_index_url.sh
@@ -23,6 +23,8 @@ _FAKE_SMI_DIR=$(mktemp -d)
echo ""
sed -n '/^_has_usable_nvidia_gpu()/,/^}/p' "$INSTALL_SH"
echo ""
+ sed -n '/^_trim_index_path_slashes()/,/^}/p' "$INSTALL_SH"
+ echo ""
sed -n '/^get_torch_index_url()/,/^}/p' "$INSTALL_SH"
} | sed "s|/usr/bin/nvidia-smi|$_FAKE_SMI_DIR/nvidia-smi-absent|g" \
> "$_FUNC_FILE"
@@ -379,6 +381,61 @@ _result=$(run_func "$_dir" " -1 ")
assert_eq "CVD=' -1 ' hides NVIDIA -> cpu" "https://download.pytorch.org/whl/cpu" "$_result"
rm -rf "$_dir"
+# --- explicit overrides (headless / container / CI; no GPU probing) ----------
+# 39) UNSLOTH_TORCH_INDEX_FAMILY pins the family with no GPU present (not the cpu fallback).
+_result=$(UNSLOTH_TORCH_INDEX_FAMILY="cu128" run_func "none")
+assert_eq "family override (no GPU) -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
+
+# 40) Family override beats real detection: an nvidia-smi 12.6 host still gets cu128
+# (the Docker-build case -- builder sees the host driver but publishes a cu128 image).
+_dir=$(make_mock_smi "12.6")
+_result=$(UNSLOTH_TORCH_INDEX_FAMILY="cu128" run_func "$_dir")
+assert_eq "family override beats detected 12.6 -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
+rm -rf "$_dir"
+
+# 41) UNSLOTH_TORCH_INDEX_URL is used verbatim and wins over detection.
+_dir=$(make_mock_smi "12.6")
+_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu999" run_func "$_dir")
+assert_eq "url override beats detection -> verbatim" "https://mirror.example.com/whl/cu999" "$_result"
+rm -rf "$_dir"
+
+# 42) Family override is appended to UNSLOTH_PYTORCH_MIRROR (mirror still honoured).
+_result=$(UNSLOTH_PYTORCH_MIRROR="https://mirror.example.com/whl" UNSLOTH_TORCH_INDEX_FAMILY="cu128" run_func "none")
+assert_eq "mirror + family override -> mirror/cu128" "https://mirror.example.com/whl/cu128" "$_result"
+
+# 43) Trailing slash in UNSLOTH_TORCH_INDEX_URL is stripped.
+_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128/" run_func "none")
+assert_eq "url override trailing slash stripped" "https://mirror.example.com/whl/cu128" "$_result"
+
+# 44) URL override takes precedence over family override.
+_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu130" UNSLOTH_TORCH_INDEX_FAMILY="cu128" run_func "none")
+assert_eq "url override beats family override -> url" "https://mirror.example.com/whl/cu130" "$_result"
+
+# 45) An empty override is ignored (falls through to normal detection).
+_result=$(UNSLOTH_TORCH_INDEX_FAMILY="" UNSLOTH_TORCH_INDEX_URL="" run_func "none")
+assert_eq "empty overrides ignored -> detected cpu" "https://download.pytorch.org/whl/cpu" "$_result"
+
+# 46) ALL trailing slashes are stripped from a URL override (not just one).
+_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128///" run_func "none")
+assert_eq "url override double slash stripped" "https://mirror.example.com/whl/cu128" "$_result"
+
+# 47) Leading and trailing slashes stripped from a family override.
+_result=$(UNSLOTH_TORCH_INDEX_FAMILY="//cu128//" run_func "none")
+assert_eq "family override slashes stripped" "https://download.pytorch.org/whl/cu128" "$_result"
+
+# 48) A ?query token that ends in "/" is PRESERVED: only PATH slashes are trimmed, so a
+# base64 token ending in "/" is not corrupted (path-only trim, not whole-URL rstrip).
+_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128?token=ab12cd/" run_func "none")
+assert_eq "url override preserves query token slash" "https://mirror.example.com/whl/cu128?token=ab12cd/" "$_result"
+
+# 49) Double PATH slash before a query is collapsed while the query survives intact.
+_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128//?token=ab12cd/" run_func "none")
+assert_eq "url override path slash trimmed, query kept" "https://mirror.example.com/whl/cu128?token=ab12cd/" "$_result"
+
+# 50) A #fragment ending in "/" is likewise preserved.
+_result=$(UNSLOTH_TORCH_INDEX_URL="https://mirror.example.com/whl/cu128#anchor/" run_func "none")
+assert_eq "url override preserves fragment slash" "https://mirror.example.com/whl/cu128#anchor/" "$_result"
+
rm -f "$_FUNC_FILE"
rm -rf "$_FAKE_SMI_DIR"
rm -rf "$_TOOLS_DIR"
diff --git a/tests/sh/test_redact_install_output.sh b/tests/sh/test_redact_install_output.sh
new file mode 100755
index 0000000000..0f10122aea
--- /dev/null
+++ b/tests/sh/test_redact_install_output.sh
@@ -0,0 +1,89 @@
+#!/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 _redact_install_output helper. uv/pip failure text embeds the
+# failing --index-url verbatim, so a captured install log dumped on error can leak a
+# user:token@ or ?token= secret. The helper redacts both before printing. Mirrors
+# _redact_install_output (install_python_stack.py) / Redact-InstallOutput (install.ps1 /
+# setup.ps1).
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+INSTALL_SH="$SCRIPT_DIR/../../install.sh"
+PASS=0
+FAIL=0
+
+_FUNC_FILE=$(mktemp)
+sed -n '/^_redact_install_output()/,/^}/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
+}
+
+# Redact from a file (the actual call site passes a captured-log tempfile).
+redact_str() {
+ _rs_tmp=$(mktemp)
+ printf '%s\n' "$1" > "$_rs_tmp"
+ _rs_out=$(_redact_install_output "$_rs_tmp")
+ rm -f "$_rs_tmp"
+ printf '%s' "$_rs_out"
+}
+
+echo "=== _redact_install_output ==="
+assert_eq "userinfo user:token@ redacted" \
+ "ERROR: failed https://@download.pytorch.org/whl/cu128" \
+ "$(redact_str 'ERROR: failed https://alice:s3cr3t@download.pytorch.org/whl/cu128')"
+
+assert_eq "bare-token@ userinfo redacted" \
+ "fetch https://@host/whl/cu128 failed" \
+ "$(redact_str 'fetch https://ghp_deadbeef@host/whl/cu128 failed')"
+
+assert_eq "single ?token= query redacted" \
+ "url https://host/whl/cu128?token= unreachable" \
+ "$(redact_str 'url https://host/whl/cu128?token=abcd1234 unreachable')"
+
+assert_eq "multiple query values redacted" \
+ "https://host/whl/cu128?token=&channel=" \
+ "$(redact_str 'https://host/whl/cu128?token=abcd1234&channel=beta')"
+
+assert_eq "http (not https) userinfo redacted" \
+ "http://@host/simple" \
+ "$(redact_str 'http://u:p@host/simple')"
+
+assert_eq "fragment token redacted" \
+ "ERROR: could not fetch https://mirror.local/whl/cu128# (403)" \
+ "$(redact_str 'ERROR: could not fetch https://mirror.local/whl/cu128#token=SECRET123 (403)')"
+
+assert_eq "query and fragment both redacted" \
+ "https://host/whl/cu128?token=# done" \
+ "$(redact_str 'https://host/whl/cu128?token=abc#sig=xyz done')"
+
+# Non-secret text is untouched (no false positives on ordinary log lines).
+assert_eq "plain line untouched" \
+ "Resolved 42 packages in 1.2s" \
+ "$(redact_str 'Resolved 42 packages in 1.2s')"
+assert_eq "plain url without creds untouched" \
+ "downloading https://download.pytorch.org/whl/cu128/torch-2.8.0.whl" \
+ "$(redact_str 'downloading https://download.pytorch.org/whl/cu128/torch-2.8.0.whl')"
+assert_eq "bare hash comment untouched" \
+ "# retrying with --no-cache-dir" \
+ "$(redact_str '# retrying with --no-cache-dir')"
+
+# Regression guard: no secret substring survives.
+_leak=$(redact_str 'https://alice:s3cr3t@host/whl/cu128?token=SUPERSECRET#frag=ALSOSECRET')
+case "$_leak" in
+ *s3cr3t*|*SUPERSECRET*|*ALSOSECRET*) assert_eq "no secret leak" "clean" "leaked:$_leak" ;;
+ *) assert_eq "no secret leak" "clean" "clean" ;;
+esac
+
+echo ""
+echo "Results: $PASS passed, $FAIL failed"
+[ "$FAIL" -eq 0 ]
diff --git a/tests/sh/test_torch_constraint.sh b/tests/sh/test_torch_constraint.sh
index d60dfc9f90..bfafbd161b 100644
--- a/tests/sh/test_torch_constraint.sh
+++ b/tests/sh/test_torch_constraint.sh
@@ -108,6 +108,25 @@ 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"
+# Companions must be bounded to torch's window everywhere: the <2.11 bound appears
+# twice (default assignments + the pinned custom-leaf block), never bare. torchaudio
+# 2.11 dropped its exact torch pin, so a bare companion next to a <2.11-capped torch
+# resolves a mismatched 2.11 build.
+_count=$(grep -c 'TORCHVISION_CONSTRAINT="torchvision>=0.19,<0.26.0"' "$INSTALL_SH" || true)
+assert_eq "torchvision bounded (<0.26) at default + custom-leaf" "2" "$_count"
+_count=$(grep -c 'TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<2.11.0"' "$INSTALL_SH" || true)
+assert_eq "torchaudio bounded (<2.11) at default + custom-leaf" "2" "$_count"
+_count=$(grep -c 'TORCHVISION_CONSTRAINT="torchvision"$' "$INSTALL_SH" || true)
+assert_eq "no bare torchvision companion remains" "0" "$_count"
+_count=$(grep -c 'TORCHAUDIO_CONSTRAINT="torchaudio"$' "$INSTALL_SH" || true)
+assert_eq "no bare torchaudio companion remains" "0" "$_count"
+# The cu* widen must carry the companions with it (torch <2.12 with torchaudio <2.11
+# would cap a mismatched pair the other way).
+assert_eq "cu widen pairs torchaudio (<2.12)" "1" "$(grep -c 'TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<2.12.0"' "$INSTALL_SH" || true)"
+_gated=$(grep -c '_expected_torch_flavor_tag "$TORCH_INDEX_URL"' "$INSTALL_SH" || true)
+_has_gate=$([ "$_gated" -ge 1 ] && echo "yes" || echo "no")
+assert_eq "custom-companion bound gated on empty flavor tag" "yes" "$_has_gate"
+
# 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)
@@ -285,6 +304,61 @@ bash -c "
_uv_got2=$(cat "$_UV_LOG2" 2>/dev/null || echo "")
assert_contains "mock uv arm64+py312 receives torch>=2.4" "$_uv_got2" "torch>=2.4,<2.11.0"
+# ======================================================================
+# ROCm 2.11 floor: leaf is lowercased before the gfx*/rocm* allowlist match
+# ======================================================================
+echo ""
+echo "=== ROCm 2.11 floor case (leaf normalization) ==="
+
+# Structural: install.sh lowercases _torch_index_leaf before the floor case, so the
+# canonical gfx120X-all (capital X) matches gfx120x-all.
+_has_lc=$(grep -c '_torch_index_leaf=$(printf .* | tr .\[:upper:\]. .\[:lower:\].)' "$INSTALL_SH" || true)
+_has_lc_ok=$([ "$_has_lc" -ge 1 ] && echo "yes" || echo "no")
+assert_eq "install.sh lowercases _torch_index_leaf" "yes" "$_has_lc_ok"
+
+# Runtime: replicate install.sh's normalization + floor case and assert both gfx120X-all
+# and gfx120x-all get the floor, while non-2.11 leaves keep the default.
+run_floor_case() {
+ _url="$1"
+ bash -c '
+ TORCH_CONSTRAINT="torch>=2.4,<2.11.0"
+ TORCHVISION_CONSTRAINT="torchvision"
+ TORCHAUDIO_CONSTRAINT="torchaudio"
+ _torch_index_leaf="${1%/}"
+ _torch_index_leaf="${_torch_index_leaf##*/}"
+ _torch_index_leaf=$(printf "%s" "$_torch_index_leaf" | tr "[:upper:]" "[:lower:]")
+ case "$_torch_index_leaf" in
+ rocm7.2|gfx120x-all|gfx1151|gfx1150)
+ TORCH_CONSTRAINT="torch>=2.11.0,<2.12.0"
+ TORCHVISION_CONSTRAINT="torchvision>=0.26.0,<0.27.0"
+ TORCHAUDIO_CONSTRAINT="torchaudio>=2.11.0,<2.12.0"
+ ;;
+ esac
+ echo "$TORCH_CONSTRAINT"
+ ' _ "$_url"
+}
+
+assert_eq "gfx120X-all (capital) -> 2.11 floor" "torch>=2.11.0,<2.12.0" \
+ "$(run_floor_case 'https://repo.amd.com/rocm/whl/gfx120X-all')"
+assert_eq "gfx120X-all trailing slash -> 2.11 floor" "torch>=2.11.0,<2.12.0" \
+ "$(run_floor_case 'https://repo.amd.com/rocm/whl/gfx120X-all/')"
+assert_eq "gfx120x-all (lowercase) -> 2.11 floor" "torch>=2.11.0,<2.12.0" \
+ "$(run_floor_case 'https://repo.amd.com/rocm/whl/gfx120x-all')"
+assert_eq "gfx1151 -> 2.11 floor" "torch>=2.11.0,<2.12.0" \
+ "$(run_floor_case 'https://repo.amd.com/rocm/whl/gfx1151')"
+assert_eq "gfx1150 -> 2.11 floor" "torch>=2.11.0,<2.12.0" \
+ "$(run_floor_case 'https://repo.amd.com/rocm/whl/gfx1150')"
+assert_eq "rocm7.2 -> 2.11 floor" "torch>=2.11.0,<2.12.0" \
+ "$(run_floor_case 'https://download.pytorch.org/whl/rocm7.2')"
+assert_eq "gfx110X-all -> default (no floor)" "torch>=2.4,<2.11.0" \
+ "$(run_floor_case 'https://repo.amd.com/rocm/whl/gfx110X-all')"
+assert_eq "rocm6.4 -> default (no floor)" "torch>=2.4,<2.11.0" \
+ "$(run_floor_case 'https://download.pytorch.org/whl/rocm6.4')"
+assert_eq "cu128 -> default (no floor)" "torch>=2.4,<2.11.0" \
+ "$(run_floor_case 'https://download.pytorch.org/whl/cu128')"
+assert_eq "cpu -> default (no floor)" "torch>=2.4,<2.11.0" \
+ "$(run_floor_case 'https://download.pytorch.org/whl/cpu')"
+
# ======================================================================
# Summary
# ======================================================================
diff --git a/tests/sh/test_torch_flavor.sh b/tests/sh/test_torch_flavor.sh
index ead2c4164f..55da2c0f07 100755
--- a/tests/sh/test_torch_flavor.sh
+++ b/tests/sh/test_torch_flavor.sh
@@ -11,14 +11,21 @@ INSTALL_SH="$SCRIPT_DIR/../../install.sh"
PASS=0
FAIL=0
-# Extract the three helper functions from install.sh and source them.
+# Extract the helper functions from install.sh and source them
+# (_torch_index_url_leaf is the shared leaf extractor the classifiers call).
_FUNC_FILE=$(mktemp)
{
sed -n '/^_torch_flavor_tag()/,/^}/p' "$INSTALL_SH"
echo ""
+ sed -n '/^_torch_index_url_leaf()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_is_pip_rocm_family_leaf()/,/^}/p' "$INSTALL_SH"
+ echo ""
sed -n '/^_expected_torch_flavor_tag()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_torch_index_repairable()/,/^}/p' "$INSTALL_SH"
+ echo ""
+ sed -n '/^_tauri_torch_index_family()/,/^}/p' "$INSTALL_SH"
} > "$_FUNC_FILE"
# shellcheck disable=SC1090
. "$_FUNC_FILE"
@@ -56,6 +63,26 @@ assert_eq "amd gfx index" "rocm" "$(_expected_torch_flavor_tag 'https://re
assert_eq "mirror cu130 leaf" "cu130" "$(_expected_torch_flavor_tag 'https://my.mirror/pytorch/whl/cu130')"
assert_eq "unrecognized leaf" "" "$(_expected_torch_flavor_tag 'https://my.mirror/whl/simple')"
assert_eq "empty url" "" "$(_expected_torch_flavor_tag '')"
+# Query/fragment dropped before classification: .../cu128?token=x classifies as cu128,
+# not an opaque leaf that reinstalls every run.
+assert_eq "query-bearing cu128" "cu128" "$(_expected_torch_flavor_tag 'https://m/whl/cu128?token=x')"
+assert_eq "fragment-bearing cpu" "cpu" "$(_expected_torch_flavor_tag 'https://m/whl/cpu#frag')"
+# A cu-suffixed CUSTOM leaf (cu128-private, cu128x) is NOT the cu128 family (exact
+# cu+digits only). Mirrors Python re.fullmatch(cu[0-9]+) / PowerShell.
+assert_eq "cu-suffix custom leaf" "" "$(_expected_torch_flavor_tag 'https://m/whl/cu128-private')"
+assert_eq "cu-alnum custom leaf" "" "$(_expected_torch_flavor_tag 'https://m/whl/cu128x')"
+assert_eq "bare cu digits stays" "cu126" "$(_expected_torch_flavor_tag 'https://m/whl/cu126')"
+# A custom leaf merely STARTING with rocm (rocm-current, rocm-rel-7.2.1) is NOT a pip
+# rocm family -> "" (custom); real families (rocm7.2) and gfx indexes stay "rocm".
+assert_eq "custom rocm-current" "" "$(_expected_torch_flavor_tag 'https://mirror/whl/rocm-current')"
+assert_eq "radeon rocm-rel leaf" "" "$(_expected_torch_flavor_tag 'https://repo.radeon.com/rocm/manylinux/rocm-rel-7.2.1')"
+assert_eq "real rocm7.2 stays" "rocm" "$(_expected_torch_flavor_tag 'https://download.pytorch.org/whl/rocm7.2')"
+# A rocm-SUFFIX private mirror (rocm7.2-private, rocm7-current) is a custom pin ->
+# "" (custom); match the family exactly, not the prefix.
+assert_eq "suffixed rocm7.2-private" "" "$(_expected_torch_flavor_tag 'https://co.internal/whl/rocm7.2-private')"
+assert_eq "suffixed rocm7-current" "" "$(_expected_torch_flavor_tag 'https://co.internal/whl/rocm7-current')"
+assert_eq "two-dot rocm7.2.1" "" "$(_expected_torch_flavor_tag 'https://co.internal/whl/rocm7.2.1')"
+assert_eq "bare rocm7 stays" "rocm" "$(_expected_torch_flavor_tag 'https://download.pytorch.org/whl/rocm7')"
echo "=== _torch_index_repairable ==="
assert_eq "cu130 repairable" "yes" "$(_torch_index_repairable 'https://download.pytorch.org/whl/cu130')"
@@ -64,6 +91,66 @@ assert_eq "gfx repairable" "yes" "$(_torch_index_repairable 'https://repo.
assert_eq "gfx1151 repairable" "yes" "$(_torch_index_repairable 'https://repo.amd.com/rocm/whl/gfx1151/')"
assert_eq "cpu NOT repairable" "no" "$(_torch_index_repairable 'https://download.pytorch.org/whl/cpu')"
assert_eq "unknown NOT repair" "no" "$(_torch_index_repairable 'https://my.mirror/whl/simple')"
+# A suffixed rocm leaf is a verbatim pin, not a --default-index repairable family.
+assert_eq "rocm-private NOT repair" "no" "$(_torch_index_repairable 'https://co.internal/whl/rocm7.2-private')"
+
+echo "=== _is_pip_rocm_family_leaf ==="
+assert_family() {
+ _label="$1"; _expected="$2"; _leaf="$3"
+ if _is_pip_rocm_family_leaf "$_leaf"; then _actual="yes"; else _actual="no"; fi
+ assert_eq "$_label" "$_expected" "$_actual"
+}
+assert_family "rocm7.2 family" "yes" "rocm7.2"
+assert_family "rocm6.4 family" "yes" "rocm6.4"
+assert_family "bare rocm7 family" "yes" "rocm7"
+assert_family "gfx120x-all family" "yes" "gfx120x-all"
+assert_family "gfx1151 family" "yes" "gfx1151"
+assert_family "rocm7.2-private custom" "no" "rocm7.2-private"
+assert_family "rocm7-current custom" "no" "rocm7-current"
+assert_family "rocm-current custom" "no" "rocm-current"
+assert_family "rocm-rel-7.2.1 custom" "no" "rocm-rel-7.2.1"
+assert_family "rocm7.2.1 custom" "no" "rocm7.2.1"
+# A trailing dot (rocm7.) or leading/double dot is NOT a family: both major and minor must
+# be non-empty all-digits, matching Python re.fullmatch(rocm\d+(?:\.\d+)?). Bash previously
+# accepted rocm7. via a bare %/-style trim while Python rejected it (validator asymmetry).
+assert_family "rocm7. trailing-dot custom" "no" "rocm7."
+assert_family "rocm.7 leading-dot custom" "no" "rocm.7"
+assert_family "rocm7..2 double-dot custom" "no" "rocm7..2"
+assert_family "cpu not rocm" "no" "cpu"
+assert_family "cu128 not rocm" "no" "cu128"
+assert_family "simple not rocm" "no" "simple"
+
+echo "=== _torch_index_url_leaf (ALL trailing slashes stripped -> non-empty leaf) ==="
+# A double (or triple) trailing slash must yield the real leaf, not an empty string that
+# fails every classifier arm. Python .rstrip("/") drops them all; bash must match (a bare
+# %/ left .../cu128// classifying as "").
+assert_eq "double slash cu128 leaf" "cu128" "$(_torch_index_url_leaf 'https://m/whl/cu128//')"
+assert_eq "triple slash rocm7.2 leaf" "rocm7.2" "$(_torch_index_url_leaf 'https://m/whl/rocm7.2///')"
+assert_eq "double slash + token leaf" "cu128" "$(_torch_index_url_leaf 'https://m/whl/cu128//?token=x')"
+assert_eq "single slash cu128 leaf" "cu128" "$(_torch_index_url_leaf 'https://m/whl/cu128/')"
+# The classifier that consumes the leaf must therefore still tag a double-slash index.
+assert_eq "double-slash cu128 tag" "cu128" "$(_expected_torch_flavor_tag 'https://m/whl/cu128//')"
+assert_eq "double-slash rocm7.2 tag" "rocm" "$(_expected_torch_flavor_tag 'https://m/whl/rocm7.2//')"
+
+echo "=== _tauri_torch_index_family (credential redaction) ==="
+# A token/fragment must be stripped BEFORE classification so it never reaches the
+# [TAURI:DIAG] line (the family is the last path segment, which else carries the query).
+SKIP_TORCH=false
+assert_eq "token stripped from rocm" "rocm7.2" "$(_tauri_torch_index_family 'https://mirror/whl/rocm7.2?token=SECRET')"
+assert_eq "token-bearing cu classifies" "cu128" "$(_tauri_torch_index_family 'https://m/whl/cu128?token=x')"
+assert_eq "fragment stripped cpu" "cpu" "$(_tauri_torch_index_family 'https://m/whl/cpu#frag')"
+assert_eq "plain rocm7.2 unchanged" "rocm7.2" "$(_tauri_torch_index_family 'https://download.pytorch.org/whl/rocm7.2')"
+# A trailing slash must be stripped too, or the */cu128 and */cpu arms miss .../cu128/
+# and it falls through to "auto".
+assert_eq "trailing slash cu128" "cu128" "$(_tauri_torch_index_family 'https://download.pytorch.org/whl/cu128/')"
+assert_eq "slash + token cu128" "cu128" "$(_tauri_torch_index_family 'https://m/whl/cu128/?token=x')"
+assert_eq "trailing slash cpu" "cpu" "$(_tauri_torch_index_family 'https://m/whl/cpu/')"
+# Regression guard: no secret token substring may survive in any classification.
+_leak=$(_tauri_torch_index_family 'https://mirror/whl/rocm7.2?token=SECRET')
+case "$_leak" in
+ *SECRET*|*token*) assert_eq "no token leak in family" "clean" "leaked:$_leak" ;;
+ *) assert_eq "no token leak in family" "clean" "clean" ;;
+esac
echo ""
echo "Results: $PASS passed, $FAIL failed"
diff --git a/tests/studio/install/test_cuda_repair.py b/tests/studio/install/test_cuda_repair.py
index cea4383268..c6d2b95316 100644
--- a/tests/studio/install/test_cuda_repair.py
+++ b/tests/studio/install/test_cuda_repair.py
@@ -64,15 +64,23 @@ def _run_cuda_repair(
rocm_marker = False,
smi_path = "/usr/bin/nvidia-smi",
cvd = None,
+ index_family = None,
+ index_url = None,
):
"""Invoke _ensure_cuda_torch under a fully mocked host; return the pip mock.
- cvd controls CUDA_VISIBLE_DEVICES: None removes it from the env, any string sets it."""
+ cvd controls CUDA_VISIBLE_DEVICES: None removes it from the env, any string sets it.
+ index_family sets UNSLOTH_TORCH_INDEX_FAMILY (the explicit wheel-index pin).
+ index_url sets UNSLOTH_TORCH_INDEX_URL (the full-URL pin form)."""
env = {}
if rocm_marker:
env["UNSLOTH_ROCM_TORCH_INSTALLED"] = "1"
if cvd is not None:
env["CUDA_VISIBLE_DEVICES"] = cvd
+ if index_family is not None:
+ env["UNSLOTH_TORCH_INDEX_FAMILY"] = index_family
+ if index_url is not None:
+ env["UNSLOTH_TORCH_INDEX_URL"] = index_url
def _which(name, *a, **k):
if name == "nvidia-smi":
@@ -99,6 +107,10 @@ def _run_cuda_repair(
stack_mod.os.environ.pop("UNSLOTH_ROCM_TORCH_INSTALLED", None)
if cvd is None:
stack_mod.os.environ.pop("CUDA_VISIBLE_DEVICES", None)
+ if index_family is None:
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_FAMILY", None)
+ if index_url is None:
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_URL", None)
_ensure_cuda_torch()
return mock_pip
@@ -123,11 +135,74 @@ class TestCudaRepairFires:
assert mock_pip.call_args.kwargs["constrain"] is False
def test_rocm_in_version_string_triggers_repair(self):
- # AMD SDK / Radeon wheels may encode rocm in __version__ without
- # torch.version.hip; the probe prints "hip" for both.
+ # AMD SDK / Radeon wheels may encode rocm in __version__ without torch.version.hip;
+ # the probe prints "hip" for both.
mock_pip = _run_cuda_repair(torch_state = "hip")
assert mock_pip.call_count == 1
+ def test_no_gpu_but_explicit_cuda_pin_repairs(self):
+ # Headless / CI cross-install: an explicit cu* pin commits to CUDA wheels with no
+ # NVIDIA GPU visible, so a ROCm-poisoned venv is still repaired to the pinned family.
+ mock_pip = _run_cuda_repair(
+ nvidia = False,
+ backend = "cuda",
+ index_family = "cu128",
+ torch_state = "hip",
+ )
+ assert mock_pip.call_count == 1
+ assert "cu128" in _index_url(mock_pip)
+
+ def test_cvd_hidden_but_explicit_cuda_pin_repairs(self):
+ # CVD=-1/"" hides the GPU, but an explicit cu* pin skips ALL host-GPU probing, so the
+ # CVD hide gate must not suppress the repair (GPU-less CI: CVD=-1, FAMILY=cu128).
+ for _cvd in ("-1", ""):
+ mock_pip = _run_cuda_repair(
+ nvidia = False,
+ backend = "cuda",
+ cvd = _cvd,
+ index_family = "cu128",
+ torch_state = "hip",
+ )
+ assert mock_pip.call_count == 1
+ assert "cu128" in _index_url(mock_pip)
+
+ def test_tagged_cuda_mismatch_repairs(self):
+ # A healthy CUDA torch whose +cuXXX differs from the pin is repaired.
+ mock_pip = _run_cuda_repair(
+ index_family = "cu128",
+ torch_state = "cuda|cu126",
+ cuda_version = "12.8",
+ )
+ assert mock_pip.call_count == 1
+ assert "cu128" in _index_url(mock_pip)
+
+ def test_untagged_cuda_build_under_pin_repairs(self):
+ # An untagged CUDA build (no +cuXXX tag -> empty installed cu) can't be confirmed
+ # to match the pin, so the pin is enforced with a reinstall.
+ mock_pip = _run_cuda_repair(
+ index_family = "cu128",
+ torch_state = "cuda", # marker cuda, empty installed cu
+ cuda_version = "12.8",
+ )
+ assert mock_pip.call_count == 1
+ assert "cu128" in _index_url(mock_pip)
+
+ def test_broken_probe_with_cuda_pin_repairs(self):
+ # torch present but unimportable under a CUDA pin: the base update won't repair a
+ # broken already-installed torch, so reinstall from the pin instead of stranding it.
+ mock_pip = _run_cuda_repair(torch_state = "hip", torch_rc = 1, index_family = "cu128")
+ assert mock_pip.call_count == 1
+ assert "cu128" in _index_url(mock_pip)
+
+ def test_broken_probe_with_cuda_url_pin_repairs(self):
+ mock_pip = _run_cuda_repair(
+ torch_state = "cpu",
+ torch_rc = 1,
+ index_url = "https://mirror.local/cu128",
+ )
+ assert mock_pip.call_count == 1
+ assert "https://mirror.local/cu128" in _index_url(mock_pip)
+
# No-op cases.
@@ -157,8 +232,9 @@ class TestCudaRepairSkips:
mock_pip = _run_cuda_repair(nvidia = False, torch_state = "hip")
mock_pip.assert_not_called()
- def test_torch_missing_skips(self):
- # Non-zero probe exit = torch missing / un-importable.
+ def test_torch_missing_no_pin_skips(self):
+ # Non-zero probe exit = torch missing/un-importable. With NO CUDA pin the base
+ # install owns it, so leave it alone (a pinned build reinstalls).
mock_pip = _run_cuda_repair(torch_state = "hip", torch_rc = 1)
mock_pip.assert_not_called()
@@ -191,6 +267,109 @@ class TestCudaRepairSkips:
mock_pip = _run_cuda_repair(cvd = "0", torch_state = "hip")
assert mock_pip.call_count == 1
+ def test_matching_tagged_cuda_pin_no_repair(self):
+ # Healthy CUDA torch whose +cuXXX already matches the pin: no reinstall.
+ mock_pip = _run_cuda_repair(
+ index_family = "cu128",
+ torch_state = "cuda|cu128",
+ cuda_version = "12.8",
+ )
+ mock_pip.assert_not_called()
+
+ def test_custom_mirror_leaf_not_treated_as_cuda_pin(self):
+ # A mirror leaf starting with "cu" but not cuXXX (.../custom, .../current) must
+ # NOT be treated as a CUDA pin, so it can't bypass the NVIDIA gate.
+ for _leaf in ("custom", "current"):
+ mock_pip = _run_cuda_repair(
+ nvidia = False,
+ backend = "cuda",
+ index_url = f"https://mymirror.example/{_leaf}",
+ torch_state = "hip",
+ )
+ mock_pip.assert_not_called()
+
+ def test_explicit_cuda_family_leaf_helper(self):
+ # _explicit_cuda_torch_index_url matches cuXXX narrowly, not any cu* leaf.
+ import contextlib
+
+ def _with(url):
+ with patch.dict(stack_mod.os.environ, {"UNSLOTH_TORCH_INDEX_URL": url}, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_FAMILY", None)
+ return stack_mod._explicit_cuda_torch_index_url()
+
+ assert _with("https://download.pytorch.org/whl/cu128") is not None
+ assert _with("https://download.pytorch.org/whl/cu126") is not None
+ assert _with("https://mymirror.example/custom") is None
+ assert _with("https://mymirror.example/current") is None
+ assert _with("https://download.pytorch.org/whl/cpu") is None
+ with contextlib.suppress(Exception):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_URL", None)
+
+
+class TestTorchBackendDerivationFromPin:
+ """The module-level _TORCH_BACKEND derivation (standalone `studio update`
+ with no install.sh-set UNSLOTH_TORCH_BACKEND) must classify the pinned index
+ leaf via _is_cuda_family_leaf (^cu[0-9]), NOT a bare startswith("cu"). A
+ full-override URL ending in /current or /custom must fall through to backend
+ "" (probe the GPU) so _ensure_rocm_torch() still repairs a wrong/CPU torch on
+ AMD hosts, instead of being wrongly branded "cuda" and returning early."""
+
+ @staticmethod
+ def _derive(env):
+ # Re-run the module's import-time derivation, using its own _is_cuda_family_leaf
+ # so this stays in lockstep.
+ idx_override = (
+ env.get("UNSLOTH_TORCH_INDEX_URL", "").strip()
+ or env.get("UNSLOTH_TORCH_INDEX_FAMILY", "").strip()
+ )
+ backend = env.get("UNSLOTH_TORCH_BACKEND", "").lower()
+ if not backend:
+ leaf = idx_override.rstrip("/").rsplit("/", 1)[-1].lower()
+ if leaf.startswith(("rocm", "gfx")):
+ backend = "rocm"
+ elif leaf == "cpu":
+ backend = "cpu"
+ elif stack_mod._is_cuda_family_leaf(leaf):
+ backend = "cuda"
+ return backend
+
+ def test_cu128_pin_is_cuda(self):
+ assert (
+ self._derive({"UNSLOTH_TORCH_INDEX_URL": "https://download.pytorch.org/whl/cu128"})
+ == "cuda"
+ )
+
+ def test_cu128_family_is_cuda(self):
+ assert self._derive({"UNSLOTH_TORCH_INDEX_FAMILY": "cu128"}) == "cuda"
+
+ def test_current_leaf_not_cuda(self):
+ # ^cu[0-9] rejects /current -> backend stays "" (probe GPU), so an AMD host still
+ # repairs a CPU/wrong torch instead of short-circuiting.
+ assert self._derive({"UNSLOTH_TORCH_INDEX_URL": "https://mymirror.example/current"}) == ""
+
+ def test_custom_leaf_not_cuda(self):
+ assert self._derive({"UNSLOTH_TORCH_INDEX_URL": "https://mymirror.example/custom"}) == ""
+
+ def test_rocm_and_gfx_pins_are_rocm(self):
+ assert self._derive({"UNSLOTH_TORCH_INDEX_FAMILY": "rocm7.2"}) == "rocm"
+ assert (
+ self._derive({"UNSLOTH_TORCH_INDEX_URL": "https://repo.amd.com/rocm/whl/gfx120X-all"})
+ == "rocm"
+ )
+
+ def test_cpu_pin_is_cpu(self):
+ assert self._derive({"UNSLOTH_TORCH_INDEX_FAMILY": "cpu"}) == "cpu"
+
+ def test_source_uses_helper_not_bare_startswith(self):
+ # Guard against a regression back to elif _idx_leaf.startswith("cu").
+ src = _STACK_PATH.read_text(encoding = "utf-8")
+ assert (
+ "elif _is_cuda_family_leaf(_idx_leaf):" in src
+ ), "_TORCH_BACKEND derivation must classify CUDA via _is_cuda_family_leaf"
+ assert (
+ 'elif _idx_leaf.startswith("cu"):' not in src
+ ), "_TORCH_BACKEND derivation must not use a bare startswith('cu')"
+
# CUDA index ladder.
diff --git a/tests/studio/install/test_gpu_detection_followups.py b/tests/studio/install/test_gpu_detection_followups.py
index f5ad9566d7..d2fd7ae8db 100644
--- a/tests/studio/install/test_gpu_detection_followups.py
+++ b/tests/studio/install/test_gpu_detection_followups.py
@@ -284,7 +284,9 @@ class TestBackendExportLeafClassification:
def test_export_block_uses_leaf(self, install_src):
anchor = install_src.find("_torch_index_leaf=")
assert anchor >= 0, "backend export must classify on the final path segment"
- window = install_src[anchor : anchor + 500]
+ # Window spans the leaf-normalization prelude (query/frag drop + all-slash trim loop)
+ # through the export case arms.
+ window = install_src[anchor : anchor + 900]
assert 'export UNSLOTH_TORCH_BACKEND="rocm"' in window
assert 'export UNSLOTH_TORCH_BACKEND="cpu"' in window
assert 'export UNSLOTH_TORCH_BACKEND="cuda"' in window
@@ -459,3 +461,130 @@ class TestHiddenCvdNotUsable:
cvd,
)
assert out == expected
+
+
+class TestRedactInstallOutput:
+ """_redact_install_output scrubs index-URL credentials from a captured install log
+ before it is printed on failure (uv/pip embeds the failing --index-url verbatim)."""
+
+ def test_userinfo_redacted(self):
+ out = stack_mod._redact_install_output(
+ "ERROR: failed https://alice:s3cr3t@download.pytorch.org/whl/cu128"
+ )
+ assert out == "ERROR: failed https://@download.pytorch.org/whl/cu128"
+
+ def test_bytes_input_decoded_and_redacted(self):
+ out = stack_mod._redact_install_output(b"fetch https://ghp_deadbeef@host/whl/cu128 failed")
+ assert out == "fetch https://@host/whl/cu128 failed"
+
+ def test_query_values_redacted(self):
+ out = stack_mod._redact_install_output(
+ "url https://host/whl/cu128?token=abcd1234&channel=beta unreachable"
+ )
+ assert out == "url https://host/whl/cu128?token=&channel= unreachable"
+
+ def test_fragment_redacted(self):
+ out = stack_mod._redact_install_output(
+ "ERROR: could not fetch https://mirror.local/whl/cu128#token=SECRET123 (403)"
+ )
+ assert out == "ERROR: could not fetch https://mirror.local/whl/cu128# (403)"
+
+ def test_query_and_fragment_both_redacted(self):
+ out = stack_mod._redact_install_output("https://host/whl/cu128?token=abc#sig=xyz done")
+ assert out == "https://host/whl/cu128?token=# done"
+
+ def test_bare_hash_comment_untouched(self):
+ # The fragment redaction is URL-anchored: a shell comment in tool output survives.
+ assert (
+ stack_mod._redact_install_output("# retrying with --no-cache-dir")
+ == "# retrying with --no-cache-dir"
+ )
+
+ def test_plain_line_untouched(self):
+ assert (
+ stack_mod._redact_install_output("Resolved 42 packages in 1.2s")
+ == "Resolved 42 packages in 1.2s"
+ )
+
+ def test_no_secret_substring_survives(self):
+ out = stack_mod._redact_install_output(
+ "https://alice:s3cr3t@host/whl/cu128?token=SUPERSECRET#frag=ALSOSECRET"
+ )
+ assert "s3cr3t" not in out and "SUPERSECRET" not in out and "ALSOSECRET" not in out
+
+
+class TestTrimIndexPathSlashes:
+ """_trim_index_path_slashes strips trailing PATH slashes only; a ?query/#fragment token
+ ending in "/" must survive (a whole-URL rstrip would corrupt a base64 token)."""
+
+ def test_double_path_slash_collapsed(self):
+ assert stack_mod._trim_index_path_slashes("https://h/whl/cu128//") == "https://h/whl/cu128"
+
+ def test_query_token_slash_preserved(self):
+ assert (
+ stack_mod._trim_index_path_slashes("https://h/whl/cu128?token=ab12cd/")
+ == "https://h/whl/cu128?token=ab12cd/"
+ )
+
+ def test_path_slash_trimmed_query_kept(self):
+ assert (
+ stack_mod._trim_index_path_slashes("https://h/whl/cu128//?token=ab12cd/")
+ == "https://h/whl/cu128?token=ab12cd/"
+ )
+
+ def test_fragment_slash_preserved(self):
+ assert (
+ stack_mod._trim_index_path_slashes("https://h/whl/cu128#anchor/")
+ == "https://h/whl/cu128#anchor/"
+ )
+
+
+class TestRocmFamilyLeafParity:
+ """_is_pip_rocm_family_leaf must match re.fullmatch(rocm\\d+(?:\\.\\d+)?): a trailing dot
+ (rocm7.) is a CUSTOM pin, not a family (the historical bash/py validator asymmetry)."""
+
+ @pytest.mark.parametrize(
+ "leaf, expected",
+ [
+ ("rocm7", True),
+ ("rocm7.2", True),
+ ("gfx1151", True),
+ ("rocm7.", False),
+ ("rocm.7", False),
+ ("rocm7..2", False),
+ ("rocm7.2.1", False),
+ ("rocm7.2-private", False),
+ ("cpu", False),
+ ("cu128", False),
+ ],
+ )
+ def test_family_classification(self, leaf, expected):
+ assert stack_mod._is_pip_rocm_family_leaf(leaf) is expected
+
+
+class TestTorchIndexLeafAllSlashes:
+ """_torch_index_leaf drops query/fragment then strips ALL trailing slashes, so a
+ double-slash index still yields the real leaf (not an empty string)."""
+
+ @pytest.mark.parametrize(
+ "url, expected",
+ [
+ ("https://m/whl/cu128//", "cu128"),
+ ("https://m/whl/rocm7.2///", "rocm7.2"),
+ ("https://m/whl/cu128//?token=x", "cu128"),
+ ("https://m/whl/cu128/", "cu128"),
+ ],
+ )
+ def test_leaf_never_empty_on_double_slash(self, url, expected):
+ assert stack_mod._torch_index_leaf(url) == expected
+
+
+class TestUvIndexEnvVarsScrub:
+ """The pinned-install env scrub must drop PIP_NO_INDEX (which makes the pip fallback
+ ignore ALL indexes, defeating the pin) and PIP_INDEX_URL (replaces the pinned index)."""
+
+ def test_pip_no_index_scrubbed(self):
+ assert "PIP_NO_INDEX" in stack_mod._UV_INDEX_ENV_VARS
+
+ def test_pip_index_url_scrubbed(self):
+ assert "PIP_INDEX_URL" in stack_mod._UV_INDEX_ENV_VARS
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":
diff --git a/tests/studio/install/test_pr5940_followups.py b/tests/studio/install/test_pr5940_followups.py
index d6dc8b2f8e..dd2a7ec487 100644
--- a/tests/studio/install/test_pr5940_followups.py
+++ b/tests/studio/install/test_pr5940_followups.py
@@ -784,7 +784,7 @@ def test_install_python_stack_windows_rocm_repair_pins_and_is_nonfatal():
assert re.search(
r'"' + gfx + r'":\s*_ROCM_TORCH_PKG_SPECS\["rocm7\.2"\]', text
), f"{gfx} must pin to the rocm7.2 trio like install.ps1/setup.ps1"
- i = text.find('f"ROCm torch (Windows, {gfx_arch})"')
+ i = text.find("f\"ROCm torch (Windows, {gfx_arch or 'pinned'})\"")
assert i != -1, "Windows ROCm repair pip call not found"
# The nearest preceding call must be the nonfatal pip_install_try, not pip_install.
j = text.rfind("pip_install_try(", 0, i)
diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.py
index e7ac0ec82d..b94578369d 100644
--- a/tests/studio/install/test_rocm_support.py
+++ b/tests/studio/install/test_rocm_support.py
@@ -569,19 +569,27 @@ class TestEnsureRocmTorch:
_ensure_rocm_torch()
mock_pip.assert_not_called()
+ @patch.object(stack_mod, "IS_WINDOWS", False)
+ @patch.object(stack_mod, "pip_install_try", return_value = True)
@patch.object(stack_mod, "pip_install")
@patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
@patch.object(stack_mod, "_has_rocm_gpu", return_value = True)
@patch.object(stack_mod, "_detect_rocm_version", return_value = (7, 1))
- def test_torch_already_has_cuda_skips(self, mock_ver, mock_gpu, mock_nvidia, mock_pip):
- """If torch already has CUDA, should skip ROCm reinstall."""
+ def test_cuda_torch_on_amd_host_reinstalls(
+ self, mock_ver, mock_gpu, mock_nvidia, mock_pip, mock_pip_try
+ ):
+ """A CUDA-only torch build is unusable on an AMD-only host, so it must be
+ reinstalled to ROCm (has_hip_torch is driven by the empty HIP marker, not
+ by treating the CUDA version string as a HIP marker)."""
mock_probe = MagicMock()
mock_probe.returncode = 0
- mock_probe.stdout = b"12.6\n" # CUDA version
+ # Single-line probe: empty HIP marker before "|" for a CUDA build.
+ mock_probe.stdout = b"|2.10.0+cu126\n"
with patch("os.path.isdir", return_value = True):
with patch("subprocess.run", return_value = mock_probe):
_ensure_rocm_torch()
- mock_pip.assert_not_called()
+ assert mock_pip.call_count == 1
+ assert "rocm7.1" in str(mock_pip.call_args_list[0])
@patch.object(stack_mod, "pip_install")
@patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
@@ -591,12 +599,31 @@ class TestEnsureRocmTorch:
"""If torch already has HIP, should skip ROCm reinstall."""
mock_probe = MagicMock()
mock_probe.returncode = 0
- mock_probe.stdout = b"7.1.12345\n" # HIP version
+ mock_probe.stdout = b"7.1.12345|2.10.0+rocm7.1\n" # HIP marker + version
with patch("os.path.isdir", return_value = True):
with patch("subprocess.run", return_value = mock_probe):
_ensure_rocm_torch()
mock_pip.assert_not_called()
+ @patch.object(stack_mod, "IS_WINDOWS", False)
+ @patch.object(stack_mod, "pip_install")
+ @patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
+ @patch.object(stack_mod, "_has_rocm_gpu", return_value = True)
+ @patch.object(stack_mod, "_detect_rocm_version", return_value = (7, 1))
+ def test_cpu_torch_probe_line_not_read_as_hip(self, mock_ver, mock_gpu, mock_nvidia, mock_pip):
+ """A CPU build's probe line ("|2.10.0+cpu") must not read as HIP: the version
+ after the "|" separator is data, not a HIP marker, so has_hip_torch stays False
+ and the reinstall fires."""
+ mock_probe = MagicMock()
+ mock_probe.returncode = 0
+ mock_probe.stdout = b"|2.10.0+cpu\n"
+ with patch("os.path.isdir", return_value = True):
+ with patch("subprocess.run", return_value = mock_probe):
+ with patch.object(stack_mod, "pip_install_try", return_value = True):
+ _ensure_rocm_torch()
+ assert mock_pip.call_count == 1
+ assert "rocm7.1" in str(mock_pip.call_args_list[0])
+
@patch.object(stack_mod, "IS_WINDOWS", False)
@patch.object(stack_mod, "pip_install_try", return_value = True)
@patch.object(stack_mod, "pip_install")
@@ -680,6 +707,295 @@ class TestEnsureRocmTorch:
torch_call = mock_pip.call_args_list[0]
assert "rocm7.2" in str(torch_call)
+ @patch.object(stack_mod, "IS_WINDOWS", False)
+ @patch.object(stack_mod, "pip_install_try", return_value = True)
+ @patch.object(stack_mod, "pip_install")
+ @patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
+ @patch.object(stack_mod, "_has_rocm_gpu", return_value = True)
+ @patch.object(stack_mod, "_detect_rocm_version", return_value = (6, 4))
+ def test_explicit_gfx_index_honored_and_skips_strix_reroute(
+ self, mock_ver, mock_gpu, mock_nvidia, mock_pip, mock_pip_try
+ ):
+ """An explicit gfx wheel-index pin is authoritative: install from it verbatim
+ with torch 2.11, and never re-probe gfx codes to second-guess it (host ROCm 6.4
+ would otherwise pick the rocm6.4 wheel / trigger the Strix re-route)."""
+ mock_probe = MagicMock()
+ mock_probe.returncode = 0
+ mock_probe.stdout = b"\n" # cpu torch -> reinstall
+ env = {"UNSLOTH_TORCH_INDEX_URL": "https://repo.amd.com/rocm/whl/gfx1151"}
+ with patch.dict(stack_mod.os.environ, env, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_FAMILY", None)
+ with patch("os.path.isdir", return_value = True):
+ with patch("subprocess.run", return_value = mock_probe):
+ # Would raise if the Strix block ran (it is skipped on an explicit pin).
+ with patch.object(
+ stack_mod, "_detect_amd_gfx_codes", side_effect = AssertionError
+ ):
+ _ensure_rocm_torch()
+ assert mock_pip.call_count == 1
+ torch_call = str(mock_pip.call_args_list[0])
+ assert "gfx1151" in torch_call
+ assert "torch>=2.11.0,<2.12.0" in torch_call
+
+ def test_rocm_pin_family_mismatch_helper(self):
+ """_rocm_pin_family_mismatch: exact rocm compare, else the 2.11 line."""
+ f = stack_mod._rocm_pin_family_mismatch
+ base = "https://download.pytorch.org/whl"
+ amd = "https://repo.amd.com/rocm/whl"
+ # Exact rocm version comparison.
+ assert f(f"{base}/rocm7.2", "2.11.0+rocm7.2") is False
+ assert f(f"{base}/rocm7.2", "2.10.0+rocm6.4") is True
+ assert f(f"{base}/rocm6.4", "2.10.0+rocm6.4") is False
+ # rocm7.2 is KNOWN-2.11. A +rocm7.2 wheel whose RELEASE drifted off 2.11 shares the
+ # tag but violates the spec -> mismatch (a plain version compare would accept it).
+ assert f(f"{base}/rocm7.2", "2.12.0+rocm7.2") is True
+ assert f(f"{base}/rocm7.2", "2.13.0+rocm7.2") is True
+ assert f(f"{base}/rocm7.2", "2.11.5+rocm7.2") is False # patch on 2.11 is in-spec
+ # An UNKNOWN newer rocm (not on the 2.11 allowlist) is not floored to 2.11, so a
+ # matching rocm version at any release line is NOT a mismatch on this branch.
+ assert f(f"{base}/rocm8.0", "2.12.0+rocm8.0") is False
+ # gfx pin (2.11 line) vs installed release line.
+ assert f(f"{amd}/gfx1151", "2.10.0+rocm6.4") is True
+ assert f(f"{amd}/gfx1151", "2.11.0+rocm7.13.0") is False
+ # rocm7.2 pin vs an untagged (no +rocm) wheel: a CPU/CUDA build never
+ # satisfies a ROCm pin, regardless of its release line -> always a mismatch.
+ assert f(f"{base}/rocm7.2", "2.10.0") is True
+ assert f(f"{base}/rocm7.2", "2.11.0") is True
+ assert f(f"{base}/rocm6.4", "2.10.0") is True
+ # A 2.11-allowlist gfx pin over a GENERIC (two-part +rocm7.2) 2.11 wheel mismatches:
+ # the user wants AMD's per-arch (three-part) wheel, not the generic one.
+ assert f(f"{amd}/gfx1151", "2.11.0+rocm7.2") is True
+ assert f(f"{amd}/gfx120X-all", "2.11.0+rocm7.2") is True
+ # ...but an already-installed per-arch (three-part) wheel is NOT re-flagged
+ # (no reinstall loop once the correct gfx wheel is present).
+ assert f(f"{amd}/gfx120X-all", "2.11.0+rocm7.13.0") is False
+ assert f(f"{amd}/gfx1150", "2.11.0+rocm7.13.0") is False
+ # A NON-2.11 gfx pin (gfx110X-all/gfx90a/gfx908) tracks the default <2.11 spec: a
+ # correct 2.10+rocm wheel is NOT a mismatch, a 2.11 build is.
+ assert f(f"{amd}/gfx110X-all", "2.10.0+rocm6.4") is False
+ assert f(f"{amd}/gfx90a", "2.10.0+rocm6.3") is False
+ assert f(f"{amd}/gfx908", "2.10.0+rocm7.0") is False
+ assert f(f"{amd}/gfx110X-all", "2.11.0+rocm7.2") is True
+ # A non-2.11 gfx pin over an untagged (no +rocm) wheel is a mismatch even
+ # when torch is already <2.11: a CPU/CUDA build never satisfies the ROCm pin.
+ assert f(f"{amd}/gfx110X-all", "2.10.0") is True
+ assert f(f"{amd}/gfx90a", "2.10.0") is True
+ # A major-only rocm pin (rocm7) compares on the major alone: rocm6.x mismatches,
+ # any rocm7.x satisfies it, an untagged wheel never does, a bare +rocm is lenient.
+ assert f(f"{base}/rocm7", "2.10.0+rocm6.4") is True
+ assert f(f"{base}/rocm7", "2.11.0+rocm7.2") is False
+ assert f(f"{base}/rocm7", "2.11.0+rocm7.13.0") is False
+ assert f(f"{base}/rocm7", "2.10.0") is True
+ assert f(f"{base}/rocm7", "2.10.0+rocm") is False
+
+ @patch.object(stack_mod, "IS_WINDOWS", False)
+ @patch.object(stack_mod, "pip_install_try", return_value = True)
+ @patch.object(stack_mod, "pip_install")
+ @patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
+ @patch.object(stack_mod, "_has_rocm_gpu", return_value = True)
+ @patch.object(stack_mod, "_detect_rocm_version", return_value = (7, 2))
+ def test_rocm_pin_mismatch_over_installed_rocm_reinstalls(
+ self, mock_ver, mock_gpu, mock_nvidia, mock_pip, mock_pip_try
+ ):
+ """A rocm7.2 pin over an already-installed OLDER +rocm6.4 build must reinstall,
+ even though has_hip_torch is True (the ROCm analogue of the CUDA cuXXX mismatch)."""
+ mock_probe = MagicMock()
+ mock_probe.returncode = 0
+ # HIP marker present (has_hip_torch=True) + installed +rocm6.4 wheel.
+ mock_probe.stdout = b"6.4.12345|2.10.0+rocm6.4\n"
+ env = {"UNSLOTH_TORCH_INDEX_FAMILY": "rocm7.2"}
+ with patch.dict(stack_mod.os.environ, env, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_URL", None)
+ with patch("os.path.isdir", return_value = True):
+ with patch("subprocess.run", return_value = mock_probe):
+ _ensure_rocm_torch()
+ torch_call = str(mock_pip.call_args_list[0])
+ assert "rocm7.2" in torch_call
+ assert "torch>=2.11.0,<2.12.0" in torch_call
+
+ @patch.object(stack_mod, "IS_WINDOWS", False)
+ @patch.object(stack_mod, "pip_install_try", return_value = True)
+ @patch.object(stack_mod, "pip_install")
+ @patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
+ @patch.object(stack_mod, "_has_rocm_gpu", return_value = True)
+ @patch.object(stack_mod, "_detect_rocm_version", return_value = (6, 4))
+ def test_gfx_pin_over_installed_pre211_rocm_reinstalls(
+ self, mock_ver, mock_gpu, mock_nvidia, mock_pip, mock_pip_try
+ ):
+ """A gfx* pin (2.11 line) over an installed pre-2.11 +rocm6.4 build reinstalls."""
+ mock_probe = MagicMock()
+ mock_probe.returncode = 0
+ mock_probe.stdout = b"6.4.12345|2.10.0+rocm6.4\n"
+ env = {"UNSLOTH_TORCH_INDEX_URL": "https://repo.amd.com/rocm/whl/gfx1151"}
+ with patch.dict(stack_mod.os.environ, env, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_FAMILY", None)
+ with patch("os.path.isdir", return_value = True):
+ with patch("subprocess.run", return_value = mock_probe):
+ with patch.object(
+ stack_mod, "_detect_amd_gfx_codes", side_effect = AssertionError
+ ):
+ _ensure_rocm_torch()
+ torch_call = str(mock_pip.call_args_list[0])
+ assert "gfx1151" in torch_call
+ assert "torch>=2.11.0,<2.12.0" in torch_call
+
+ @patch.object(stack_mod, "IS_WINDOWS", False)
+ @patch.object(stack_mod, "pip_install_try", return_value = True)
+ @patch.object(stack_mod, "pip_install")
+ @patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
+ @patch.object(stack_mod, "_has_rocm_gpu", return_value = True)
+ @patch.object(stack_mod, "_detect_rocm_version", return_value = (7, 2))
+ def test_rocm_pin_matches_installed_no_torch_reinstall(
+ self, mock_ver, mock_gpu, mock_nvidia, mock_pip, mock_pip_try
+ ):
+ """A rocm7.2 pin over an already-matching +rocm7.2 build must NOT reinstall torch
+ (no false reinstall of a correct ROCm venv)."""
+ mock_probe = MagicMock()
+ mock_probe.returncode = 0
+ mock_probe.stdout = b"7.2.12345|2.11.0+rocm7.2\n"
+ env = {"UNSLOTH_TORCH_INDEX_FAMILY": "rocm7.2"}
+ with patch.dict(stack_mod.os.environ, env, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_URL", None)
+ with patch("os.path.isdir", return_value = True):
+ with patch("subprocess.run", return_value = mock_probe):
+ _ensure_rocm_torch()
+ # No torch reinstall: any pip_install call must not target a torch index.
+ for _call in mock_pip.call_args_list:
+ _args = [str(a) for a in _call.args]
+ if "--index-url" in _args:
+ _url = _args[_args.index("--index-url") + 1]
+ assert "rocm7.2" not in _url or "torch" not in " ".join(
+ _args
+ ), "torch must not be reinstalled when the pin already matches"
+ # A torch reinstall would pass torch>=... as a positional; assert none did.
+ assert not any(
+ any(str(a).startswith("torch") for a in _c.args) for _c in mock_pip.call_args_list
+ )
+
+ @patch.object(stack_mod, "IS_WINDOWS", False)
+ @patch.object(stack_mod, "pip_install_try", return_value = True)
+ @patch.object(stack_mod, "pip_install")
+ @patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
+ @patch.object(stack_mod, "_has_rocm_gpu", return_value = True)
+ @patch.object(stack_mod, "_detect_rocm_version", return_value = (6, 4))
+ def test_non211_gfx_pin_over_210_rocm_no_reinstall(
+ self, mock_ver, mock_gpu, mock_nvidia, mock_pip, mock_pip_try
+ ):
+ """A gfx110X-all pin (NOT in the 2.11 allowlist) over a correct 2.10+rocm
+ wheel must NOT be flagged stale -- the install path uses the default <2.11
+ specs for that arch, so re-flagging would reinstall-loop on every update."""
+ mock_probe = MagicMock()
+ mock_probe.returncode = 0
+ mock_probe.stdout = b"6.4.12345|2.10.0+rocm6.4\n"
+ env = {"UNSLOTH_TORCH_INDEX_URL": "https://repo.amd.com/rocm/whl/gfx110X-all"}
+ with patch.dict(stack_mod.os.environ, env, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_FAMILY", None)
+ with patch("os.path.isdir", return_value = True):
+ with patch("subprocess.run", return_value = mock_probe):
+ _ensure_rocm_torch()
+ # has_hip_torch True + no mismatch -> torch must NOT be reinstalled.
+ assert not any(
+ any(str(a).startswith("torch") for a in _c.args) for _c in mock_pip.call_args_list
+ )
+
+ @patch.object(stack_mod, "IS_WINDOWS", False)
+ @patch.object(stack_mod, "pip_install_try", return_value = True)
+ @patch.object(stack_mod, "pip_install")
+ @patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = False)
+ @patch.object(stack_mod, "_has_rocm_gpu", return_value = True)
+ @patch.object(stack_mod, "_detect_rocm_version", return_value = (7, 2))
+ def test_gfx_pin_over_generic_rocm211_reinstalls(
+ self, mock_ver, mock_gpu, mock_nvidia, mock_pip, mock_pip_try
+ ):
+ """A gfx1151 pin over a GENERIC (two-part +rocm7.2) 2.11 wheel must reinstall
+ the AMD per-arch wheel -- even though both are torch 2.11, the generic wheel
+ is not the per-arch build the user pinned (Strix stays off the generic wheel)."""
+ mock_probe = MagicMock()
+ mock_probe.returncode = 0
+ mock_probe.stdout = b"7.2.12345|2.11.0+rocm7.2\n"
+ env = {"UNSLOTH_TORCH_INDEX_URL": "https://repo.amd.com/rocm/whl/gfx1151"}
+ with patch.dict(stack_mod.os.environ, env, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_FAMILY", None)
+ with patch("os.path.isdir", return_value = True):
+ with patch("subprocess.run", return_value = mock_probe):
+ with patch.object(
+ stack_mod, "_detect_amd_gfx_codes", side_effect = AssertionError
+ ):
+ _ensure_rocm_torch()
+ torch_call = str(mock_pip.call_args_list[0])
+ assert "gfx1151" in torch_call
+ assert "torch>=2.11.0,<2.12.0" in torch_call
+
+ def test_radeon_url_not_classified_as_pip_rocm_family(self):
+ """A repo.radeon.com find-links dir (leaf rocm-rel-7.2.1) starts with "rocm" but is
+ NOT a pip --index-url ROCm family: it must route to the verbatim path, not a
+ --index-url reinstall that fails against a find-links listing."""
+ leaf_f = stack_mod._is_pip_rocm_family_leaf
+ # Real pip ROCm families (download.pytorch.org/whl/rocmX.Y, repo.amd.com gfx).
+ assert leaf_f("rocm7.2") is True
+ assert leaf_f("rocm6.4") is True
+ assert leaf_f("gfx120x-all") is True
+ assert leaf_f("gfx1151") is True
+ # A bare rocm (no minor) is still an exact family.
+ assert leaf_f("rocm7") is True
+ # A Radeon find-links dir leaf, a custom mirror, cpu and cuda are NOT pip rocm.
+ assert leaf_f("rocm-rel-7.2.1") is False
+ assert leaf_f("simple") is False
+ assert leaf_f("current") is False
+ assert leaf_f("cpu") is False
+ assert leaf_f("cu128") is False
+ # A rocm-SUFFIX private mirror shares the family prefix but is a custom pin
+ # the verbatim path owns: a ^rocm\d PREFIX match would wrongly treat it as a
+ # --index-url family. Match EXACTLY.
+ assert leaf_f("rocm7.2-private") is False
+ assert leaf_f("rocm7-current") is False
+ assert leaf_f("rocm7.2.1") is False # two-part local suffix -> custom, not rocm7.2
+
+ radeon = "https://repo.radeon.com/rocm/manylinux/rocm-rel-7.2.1"
+ pip_rocm = "https://download.pytorch.org/whl/rocm7.2"
+ amd_gfx = "https://repo.amd.com/rocm/whl/gfx120X-all"
+
+ def _classify(url, fn):
+ with patch.dict(stack_mod.os.environ, {"UNSLOTH_TORCH_INDEX_URL": url}, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_FAMILY", None)
+ return fn()
+
+ rocm_fn = stack_mod._explicit_rocm_torch_index_url
+ unk_fn = stack_mod._explicit_unknown_family_torch_index_url
+ # Real pip rocm/gfx pins ARE a ROCm family (reinstallable via --index-url) and
+ # are NOT "unknown".
+ assert _classify(pip_rocm, rocm_fn) == pip_rocm
+ assert _classify(amd_gfx, rocm_fn) == amd_gfx
+ assert _classify(pip_rocm, unk_fn) is None
+ assert _classify(amd_gfx, unk_fn) is None
+ # The Radeon find-links URL is NOT a pip ROCm family (so _ensure_rocm_torch skips
+ # it) and IS unknown, so the family repair helpers leave it alone.
+ assert _classify(radeon, rocm_fn) is None
+ assert _classify(radeon, unk_fn) == radeon
+
+ # A rocm-suffix private mirror routes the same way: NOT a pip rocm family,
+ # IS an unknown-family (verbatim) pin.
+ suffixed = "https://co.internal/whl/rocm7.2-private"
+ assert _classify(suffixed, rocm_fn) is None
+ assert _classify(suffixed, unk_fn) == suffixed
+
+ @patch.object(stack_mod, "pip_install")
+ def test_ensure_cpu_torch_broken_probe_reinstalls(self, mock_pip):
+ """_ensure_cpu_torch: torch present but unimportable (probe exit != 0) under an
+ explicit CPU pin must reinstall from the pin, not return -- the base update does
+ not repair a broken installed torch, so returning would strand it (Codex P2)."""
+ mock_probe = MagicMock()
+ mock_probe.returncode = 1 # torch present but cannot import
+ mock_probe.stdout = b""
+ env = {"UNSLOTH_TORCH_INDEX_URL": "https://mirror.local/cpu"}
+ with patch.dict(stack_mod.os.environ, env, clear = False):
+ stack_mod.os.environ.pop("UNSLOTH_TORCH_INDEX_FAMILY", None)
+ with patch("subprocess.run", return_value = mock_probe):
+ with patch.object(stack_mod, "NO_TORCH", False):
+ stack_mod._ensure_cpu_torch()
+ assert mock_pip.call_count == 1
+ assert "https://mirror.local/cpu" in str(mock_pip.call_args)
+
@patch.object(stack_mod, "IS_WINDOWS", False)
@patch.object(stack_mod, "pip_install_try", return_value = True)
@patch.object(stack_mod, "pip_install")
@@ -732,7 +1048,7 @@ class TestEnsureRocmTorch:
mock_pip.assert_not_called()
-# TEST: install_python_stack.py -- _has_rocm_gpu KFD sysfs vendor_id guard
+# TEST: install_python_stack.py -- torch-index MARKER mechanism (PR #6692)
class TestHasRocmGpuKfdVendorGuard:
@@ -1716,9 +2032,8 @@ class TestDetectWindowsGfxArch:
assert result == "gfx1200"
def test_returns_arch_on_crash_with_gcnarchname_in_output(self):
- # Regression #6043: hipinfo may crash (0xC0000005 on RDNA 4) after
- # printing gcnArchName. Accept the arch whenever gcnArchName is in
- # stdout, regardless of exit code (previously a CPU fallback).
+ # Regression #6043: hipinfo may crash (0xC0000005 on RDNA 4) after printing
+ # gcnArchName. Accept the arch whenever gcnArchName is in stdout, any exit code.
mock_result = MagicMock()
mock_result.returncode = -1073741819 # 0xC0000005 STATUS_ACCESS_VIOLATION
mock_result.stdout = b"gcnArchName : gfx1200\nsome other line\n"
@@ -2360,6 +2675,49 @@ class TestWindowsRocmTorchaoGuard:
assert not any("torchao" in arg for arg in installed_specs)
+class TestProgressStepCountMatchesTotal:
+ """The progress bar must reach exactly _TOTAL: every _progress() step is counted in
+ base_total. Regression for a repair step added without incrementing base_total,
+ which pushed _STEP past _TOTAL (Codex P2)."""
+
+ def _run_stack(self, tmp_path, *, is_windows, is_macos, is_mac_arm):
+ unstructured_plugin = tmp_path / "unstructured"
+ github_plugin = tmp_path / "github"
+ unstructured_plugin.mkdir()
+ github_plugin.mkdir()
+ sub = MagicMock()
+ sub.returncode = 0
+ sub.stdout = ""
+ with (
+ patch.dict(os.environ, {"SKIP_STUDIO_BASE": "1"}),
+ patch.object(stack_mod, "IS_WINDOWS", is_windows),
+ patch.object(stack_mod, "IS_MACOS", is_macos),
+ patch.object(stack_mod, "IS_MAC_ARM", is_mac_arm),
+ patch.object(stack_mod, "NO_TORCH", False),
+ patch.object(stack_mod, "_rocm_windows_torch_installed", False),
+ patch.object(stack_mod, "_bootstrap_uv", return_value = False),
+ patch.object(stack_mod, "_installed_torch_is_windows_rocm", return_value = False),
+ patch.object(stack_mod, "_has_usable_nvidia_gpu", return_value = True),
+ patch.object(stack_mod, "_repair_bad_anyio"),
+ patch.object(stack_mod, "_ensure_cuda_torch"),
+ patch.object(stack_mod, "_ensure_rocm_torch"),
+ patch.object(stack_mod, "_ensure_cpu_torch"),
+ patch.object(stack_mod, "LOCAL_DD_UNSTRUCTURED_PLUGIN", unstructured_plugin),
+ patch.object(stack_mod, "LOCAL_DD_GITHUB_PLUGIN", github_plugin),
+ patch.object(stack_mod.subprocess, "run", return_value = sub),
+ ):
+ assert stack_mod.install_python_stack() == 0
+ return stack_mod._STEP, stack_mod._TOTAL
+
+ def test_windows_progress_reaches_total(self, tmp_path):
+ step, total = self._run_stack(tmp_path, is_windows = True, is_macos = False, is_mac_arm = False)
+ assert step == total, f"Windows progress {step} != total {total} (final step uncounted)"
+
+ def test_linux_progress_reaches_total(self, tmp_path):
+ step, total = self._run_stack(tmp_path, is_windows = False, is_macos = False, is_mac_arm = False)
+ assert step == total, f"Linux progress {step} != total {total}"
+
+
# TEST: worker.py -- Windows ROCm patches (source-level checks)
@@ -2846,6 +3204,24 @@ class TestStrixRocm71Override:
source = _INSTALL_SH_PATH.read_text(encoding = "utf-8")
assert "TORCH_CONSTRAINT" in source and "2.11" in source
+ def test_torch_constraint_211_matches_leaf_not_whole_url(self):
+ """The 2.11 constraint case must match the index LEAF, not the whole URL.
+
+ A custom UNSLOTH_PYTORCH_MIRROR whose base path contains a gfx/rocm7.2
+ segment (e.g. https://mirror.local/gfx-cache) with a cu*/cpu family must
+ not be pushed to the torch 2.11 line -- same leaf-only reasoning the
+ UNSLOTH_TORCH_BACKEND classification uses.
+ """
+ source = _INSTALL_SH_PATH.read_text(encoding = "utf-8")
+ # The 2.11 constraint block must switch on $_torch_index_leaf, not the full
+ # $TORCH_INDEX_URL (a */gfx* match false-positives on a mirror base path). Only the
+ # _grouped_mm-bug gfx families (gfx120X-all / gfx1151 / gfx1150) are pushed to 2.11;
+ # a bare gfx* would also floor gfx110X-all/gfx90a/gfx908, left bare on purpose.
+ assert 'case "$_torch_index_leaf" in\n rocm7.2|gfx120x-all|gfx1151|gfx1150)' in source, (
+ "the torch>=2.11 constraint must match the specific gfx leaves that need "
+ "it (rocm7.2|gfx120x-all|gfx1151|gfx1150), not a bare gfx* or the whole URL"
+ )
+
def test_amd_rocm_mirror_env_var_respected(self):
"""install.sh must honour UNSLOTH_AMD_ROCM_MIRROR for air-gapped installs."""
source = _INSTALL_SH_PATH.read_text(encoding = "utf-8")
@@ -2934,9 +3310,9 @@ class TestServerStartupRocmFixes:
assert '"BNB_ROCM_VERSION" not in os.environ' in source
# ── hipInfo.exe PATH prepend (bitsandbytes arch-probe fix) ────────────────
- # bnb's get_rocm_gpu_arch() runs hipinfo.exe via PATH at import; the AMD
- # wheel ships it in venv Scripts (on PATH only for activated venvs), so
- # without the prepend bnb logs "[WinError 2]" when launched directly.
+ # bnb's get_rocm_gpu_arch() runs hipinfo.exe via PATH at import; the AMD wheel ships it
+ # in venv Scripts (on PATH only for activated venvs), so without the prepend bnb logs
+ # "[WinError 2]" when launched directly.
def test_main_py_prepends_hipinfo_dir_to_path(self):
"""main.py must make hipInfo.exe resolvable before bnb imports."""
@@ -3178,11 +3554,10 @@ class TestRocmGfxForwarding:
assert '$HelperReleaseRepo = "unslothai/llama.cpp"' in source
assert "$HelperReleaseRepo = if (" not in source
- # The text pins above guard the literal. The tests below *execute* the real
- # routing line from setup.sh / setup.ps1 and assert the resolved release repo,
- # so a refactor that reintroduces a conditional (or a ggml-org branch) is still
- # caught. Inputs are varied -- CPU-only, inferred/forwarded gfx, usable NVIDIA --
- # to prove no host slips back onto ggml-org. No GPU, no tooling, no network.
+ # The text pins above guard the literal. The tests below execute the real routing line
+ # from setup.sh / setup.ps1 and assert the resolved release repo, so a refactor that
+ # reintroduces a conditional (or a ggml-org branch) is still caught. Inputs vary
+ # (CPU-only, inferred/forwarded gfx, usable NVIDIA) to prove no host hits ggml-org.
@staticmethod
def _resolve_setup_sh_repo(
@@ -3277,8 +3652,8 @@ class TestRocmGfxForwarding:
# TEST: _pick_rocm_gfx_target -- visible-device selection from rocminfo output.
-# Honours CUDA/HIP_VISIBLE_DEVICES so a mixed-arch host installs the prebuilt
-# for the selected GPU, not GPU 0.
+# Honours CUDA/HIP_VISIBLE_DEVICES so a mixed-arch host installs the prebuilt for the
+# selected GPU, not GPU 0.
_pick_rocm_gfx_target = prebuilt_mod._pick_rocm_gfx_target
@@ -3454,7 +3829,11 @@ class TestWslRerouteNvidiaGuard:
source = _INSTALL_SH_PATH.read_text(encoding = "utf-8")
start = source.find("_maybe_reroute_strixhalo_to_2404()")
assert start != -1
- body = source[start : start + 1200]
+ # Slice the WHOLE function body (to its closing brace at column 0), not a
+ # fixed-length window: preamble growth must not push the signals out of view.
+ end = source.find("\n}", start)
+ assert end != -1
+ body = source[start:end]
nv = body.find("_has_usable_nvidia_gpu")
wmi = body.find("_wsl_amd_gpu_name")
assert nv != -1, "reroute must consult _has_usable_nvidia_gpu before deciding to reroute"
diff --git a/tests/studio/playwright_chat_ui.py b/tests/studio/playwright_chat_ui.py
index 35b18756ff..b00b45f97a 100644
--- a/tests/studio/playwright_chat_ui.py
+++ b/tests/studio/playwright_chat_ui.py
@@ -52,6 +52,14 @@ TURN_TIMEOUT_MS = int(os.environ.get("STUDIO_UI_TURN_TIMEOUT_MS", "180000"))
# Wall-clock cap for the whole script (healthy run is 5-9 min).
WALL_TIMEOUT_S = float(os.environ.get("STUDIO_UI_WALL_TIMEOUT_S", "720"))
+# Run only bootstrap plus permission controls for fast cross-browser checks.
+PERMISSION_ONLY = os.environ.get("STUDIO_UI_PERMISSION_ONLY", "0") == "1"
+
+# Default stays Chromium for CI. Local runs can select firefox/webkit or a
+# Chromium channel such as chrome/msedge.
+PLAYWRIGHT_BROWSER = os.environ.get("STUDIO_PLAYWRIGHT_BROWSER", "chromium").lower()
+PLAYWRIGHT_CHANNEL = os.environ.get("STUDIO_PLAYWRIGHT_CHANNEL") or None
+
# Per-fetch budget; /api/inference/load is the slowest (cold-cache GGUF load).
FETCH_TIMEOUT_MS = int(os.environ.get("STUDIO_UI_FETCH_TIMEOUT_MS", "30000"))
LOAD_FETCH_TIMEOUT_MS = int(os.environ.get("STUDIO_UI_LOAD_TIMEOUT_MS", "180000"))
@@ -116,6 +124,126 @@ def soft_fail(m):
info(f"WARN (strict-off): {m}")
+def exercise_permission_mode_controls(page, shoot):
+ """Exercise labels, migration, persistence, confirmation, and focus."""
+ step("permission levels: labels, persistence, confirmation, and focus")
+ pill = page.locator('button[aria-label="Permission level for tool calls"]:visible').first
+ expect(pill).to_be_visible()
+
+ def expect_mode(label):
+ expect(pill).to_have_attribute("data-pill-label", label)
+ expect(pill).to_contain_text(label)
+
+ def open_menu():
+ pill.click()
+ menu = page.get_by_role("menu").last
+ expect(menu).to_be_visible()
+ return menu
+
+ def choose(label):
+ menu = open_menu()
+ item = menu.get_by_role("menuitem").filter(has_text = label).first
+ expect(item).to_be_visible()
+ item.click()
+
+ # Fresh profiles default to Approve for me.
+ expect_mode("Approve for me")
+ menu = open_menu()
+ for label in (
+ "Ask for approval",
+ "Approve for me",
+ "Run automatically",
+ "Full access",
+ ):
+ expect(menu.get_by_role("menuitem").filter(has_text = label).first).to_be_visible()
+ if menu.get_by_text("Off", exact = True).count() != 0:
+ fail("legacy Off label is still visible")
+ if menu.locator('[role="menuitem"] button, [role="menuitem"] [role="button"]').count():
+ fail("permission menu contains nested interactive controls")
+ page.keyboard.press("Escape")
+ expect(pill).to_be_focused()
+
+ # The active row is a no-op and must not open the Full access dialog.
+ choose("Approve for me")
+ expect_mode("Approve for me")
+ expect(page.get_by_role("alertdialog")).to_have_count(0)
+
+ # Pointer and compact-layout coverage.
+ page.set_viewport_size({"width": 390, "height": 844})
+ expect(pill).to_be_visible()
+ box = pill.bounding_box()
+ if box is None or box["x"] < 0 or box["x"] + box["width"] > 390:
+ fail(f"permission pill is clipped in compact layout: {box!r}")
+ page.set_viewport_size({"width": 1280, "height": 900})
+
+ # Legacy setting migration: true -> ask, false -> off, absent -> auto.
+ migration_cases = (
+ ("true", "Ask for approval"),
+ ("false", "Run automatically"),
+ (None, "Approve for me"),
+ )
+ for legacy_value, expected_label in migration_cases:
+ page.evaluate(
+ """(legacyValue) => {
+ localStorage.removeItem("unsloth_chat_permission_mode");
+ if (legacyValue === null) {
+ localStorage.removeItem("unsloth_chat_confirm_tool_calls");
+ } else {
+ localStorage.setItem(
+ "unsloth_chat_confirm_tool_calls",
+ legacyValue,
+ );
+ }
+ }""",
+ legacy_value,
+ )
+ page.reload(wait_until = "domcontentloaded")
+ expect(pill).to_be_visible()
+ expect_mode(expected_label)
+
+ choose("Run automatically")
+ expect_mode("Run automatically")
+ expect(page.locator('button[data-pill-label="Search"]:visible').first).to_be_visible()
+ expect(page.locator('button[data-pill-label="Code"]:visible').first).to_be_visible()
+ stored = page.evaluate("() => localStorage.getItem('unsloth_chat_permission_mode')")
+ if stored != "off":
+ fail(f"Run automatically persisted {stored!r}, expected 'off'")
+
+ # Full access requires explicit consent and never overwrites persistence.
+ choose("Full access")
+ dialog = page.get_by_role("alertdialog")
+ expect(dialog).to_be_visible()
+ expect(dialog.get_by_role("heading", name = "Enable Full access?")).to_be_visible()
+ expect(dialog).to_contain_text("the code sandbox")
+ dialog.get_by_role("button", name = "Cancel").click()
+ expect(dialog).to_be_hidden()
+ expect_mode("Run automatically")
+
+ choose("Full access")
+ expect(dialog).to_be_visible()
+ dialog.get_by_role("button", name = "I understand").click()
+ expect_mode("Full access")
+ expect(pill).to_have_attribute("data-variant", "danger")
+ active_icon = pill.locator(".composer-pill-glyph > :first-child")
+ pill.hover()
+ page.wait_for_timeout(200)
+ icon_opacity = float(active_icon.evaluate("el => getComputedStyle(el).opacity"))
+ if icon_opacity < 0.5:
+ fail(f"Full access icon disappeared on hover (opacity={icon_opacity})")
+ stored = page.evaluate("() => localStorage.getItem('unsloth_chat_permission_mode')")
+ if stored != "off":
+ fail(f"Full access overwrote persisted mode with {stored!r}")
+
+ page.reload(wait_until = "domcontentloaded")
+ expect(pill).to_be_visible()
+ expect_mode("Run automatically")
+
+ # Leave the full chat smoke in the fresh-install default.
+ choose("Approve for me")
+ expect_mode("Approve for me")
+ shoot("04-permission-levels")
+
+
def login_via_api(pw):
req = urllib.request.Request(
f"{BASE}/api/auth/login",
@@ -145,11 +273,17 @@ with sync_playwright() as p:
# DB is still migrating; this 30s probe catches that gap before we
# sink 60s into a change-password timeout. Diagnostic only.
wait_for_health(BASE, timeout = 30.0, info = info)
- # Chromium launch args: see `tests/studio/_playwright_robust.py`.
- browser = p.chromium.launch(
- headless = True,
- args = chromium_launch_args(),
- )
+ if PLAYWRIGHT_BROWSER not in ("chromium", "firefox", "webkit"):
+ fail(f"unsupported STUDIO_PLAYWRIGHT_BROWSER={PLAYWRIGHT_BROWSER!r}")
+ browser_type = getattr(p, PLAYWRIGHT_BROWSER)
+ launch_kwargs = {"headless": True}
+ if PLAYWRIGHT_BROWSER == "chromium":
+ launch_kwargs["args"] = chromium_launch_args()
+ if PLAYWRIGHT_CHANNEL:
+ launch_kwargs["channel"] = PLAYWRIGHT_CHANNEL
+ elif PLAYWRIGHT_CHANNEL:
+ fail("STUDIO_PLAYWRIGHT_CHANNEL requires chromium")
+ browser = browser_type.launch(**launch_kwargs)
ctx = browser.new_context(
viewport = {"width": 1280, "height": 900},
# Reduce motion so view-transition animations don't intercept
@@ -364,6 +498,15 @@ with sync_playwright() as p:
raise last_err
shoot("03-chat-loaded")
+ exercise_permission_mode_controls(page, shoot)
+ if PERMISSION_ONLY:
+ info(
+ "permission-only run passed "
+ f"(browser={PLAYWRIGHT_BROWSER}, channel={PLAYWRIGHT_CHANNEL or 'bundled'})"
+ )
+ browser.close()
+ sys.exit(0)
+
# /api/models/list and /api/inference/load need a bearer; the
# frontend stores it under "unsloth_auth_token" (auth/session.ts).
token = robust_evaluate(
diff --git a/tests/studio/test_setup_pin_stale.ps1 b/tests/studio/test_setup_pin_stale.ps1
new file mode 100644
index 0000000000..2c92ae317f
--- /dev/null
+++ b/tests/studio/test_setup_pin_stale.ps1
@@ -0,0 +1,114 @@
+#!/usr/bin/env pwsh
+# 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 test for studio/setup.ps1's pinned-torch-index stale-venv helpers
+# (Test-RocmGfx211Leaf, Test-CudaFamilyLeaf, Get-RocmPinStaleTags). Pure helpers,
+# AST-extracted and run in-process. Mirrors the Python _rocm_pin_family_mismatch /
+# _is_cuda_family_leaf tests.
+# Run: pwsh -NoProfile -File tests/studio/test_setup_pin_stale.ps1
+
+$ErrorActionPreference = "Stop"
+$setupPath = [System.IO.Path]::Combine($PSScriptRoot, "..", "..", "studio", "setup.ps1")
+$setupPath = (Resolve-Path $setupPath).Path
+
+# --- Parse setup.ps1 (also serves as a syntax gate) and extract the helpers ---
+$tokens = $null; $errors = $null
+$ast = [System.Management.Automation.Language.Parser]::ParseFile($setupPath, [ref]$tokens, [ref]$errors)
+if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "setup.ps1 has parse errors" }
+
+foreach ($name in @("Test-RocmGfx211Leaf", "Test-RocmKnown211Version", "Test-CudaFamilyLeaf", "Get-RocmPinStaleTags")) {
+ $fn = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name
+ }, $true)
+ if ($fn.Count -ne 1) { throw "expected exactly one $name in setup.ps1, found $($fn.Count)" }
+ # Pure helpers (no exit / external calls) -- safe to define in this scope.
+ Invoke-Expression $fn[0].Extent.Text
+}
+
+$failures = 0
+function Check($name, $cond) {
+ if ($cond) { Write-Host " PASS $name" }
+ else { Write-Host " FAIL $name" -ForegroundColor Red; $script:failures++ }
+}
+
+# A pinned gfx/rocm index is stale when Expected != Installed.
+function IsStale($leaf, $ver) {
+ $t = Get-RocmPinStaleTags -PinLeaf $leaf -TorchVersion $ver
+ return $t.Expected -ne $t.Installed
+}
+
+Write-Host "Test-RocmGfx211Leaf (the 2.11 gfx allowlist)"
+Check "gfx1151 -> true" (Test-RocmGfx211Leaf "gfx1151")
+Check "gfx1150 -> true" (Test-RocmGfx211Leaf "gfx1150")
+Check "gfx120x-all -> true" (Test-RocmGfx211Leaf "gfx120x-all")
+Check "gfx110x-all -> false" (-not (Test-RocmGfx211Leaf "gfx110x-all"))
+Check "gfx90a -> false" (-not (Test-RocmGfx211Leaf "gfx90a"))
+Check "gfx908 -> false" (-not (Test-RocmGfx211Leaf "gfx908"))
+
+Write-Host "Test-CudaFamilyLeaf (^cu[0-9])"
+Check "cu118 -> true" (Test-CudaFamilyLeaf "cu118")
+Check "cu128 -> true" (Test-CudaFamilyLeaf "cu128")
+Check "cu130 -> true" (Test-CudaFamilyLeaf "cu130")
+Check "custom -> false" (-not (Test-CudaFamilyLeaf "custom"))
+Check "current -> false" (-not (Test-CudaFamilyLeaf "current"))
+Check "cpu -> false" (-not (Test-CudaFamilyLeaf "cpu"))
+Check "empty -> false" (-not (Test-CudaFamilyLeaf ""))
+
+Write-Host "Get-RocmPinStaleTags (mirror of _rocm_pin_family_mismatch)"
+# Exact rocm version comparison.
+Check "rocm7.2 pin + 2.11.0+rocm7.2 -> not stale" (-not (IsStale "rocm7.2" "2.11.0+rocm7.2"))
+Check "rocm7.2 pin + 2.10.0+rocm6.4 -> stale" (IsStale "rocm7.2" "2.10.0+rocm6.4")
+Check "rocm6.4 pin + 2.10.0+rocm6.4 -> not stale" (-not (IsStale "rocm6.4" "2.10.0+rocm6.4"))
+# rocm7.2 is a KNOWN-2.11 index. A +rocm7.2 wheel whose RELEASE drifted off 2.11 shares
+# the tag but violates the spec -> stale (mirror of _rocm_pin_family_mismatch).
+Check "rocm7.2 pin + 2.12.0+rocm7.2 -> stale" (IsStale "rocm7.2" "2.12.0+rocm7.2")
+Check "rocm7.2 pin + 2.13.0+rocm7.2 -> stale" (IsStale "rocm7.2" "2.13.0+rocm7.2")
+Check "rocm7.2 pin + 2.11.5+rocm7.2 -> not stale" (-not (IsStale "rocm7.2" "2.11.5+rocm7.2"))
+# An UNKNOWN newer rocm (off the 2.11 allowlist) isn't floored, so a matching version at
+# any release line is NOT stale on this exact-compare branch.
+Check "rocm8.0 pin + 2.12.0+rocm8.0 -> not stale" (-not (IsStale "rocm8.0" "2.12.0+rocm8.0"))
+# An untagged (no +rocm) wheel never satisfies a ROCm pin -> always stale.
+Check "rocm7.2 pin + 2.10.0 (untagged) -> stale" (IsStale "rocm7.2" "2.10.0")
+Check "rocm7.2 pin + 2.11.0 (untagged) -> stale" (IsStale "rocm7.2" "2.11.0")
+Check "rocm6.4 pin + 2.10.0 (untagged) -> stale" (IsStale "rocm6.4" "2.10.0")
+# 2.11-allowlist gfx pin: per-arch (three-part) wheel is satisfied, generic is stale.
+Check "gfx1151 pin + 2.11.0+rocm7.13.0 -> not stale" (-not (IsStale "gfx1151" "2.11.0+rocm7.13.0"))
+Check "gfx1150 pin + 2.11.0+rocm7.13.0 -> not stale" (-not (IsStale "gfx1150" "2.11.0+rocm7.13.0"))
+Check "gfx120x-all pin + 2.11.0+rocm7.13.0 -> not stale" (-not (IsStale "gfx120x-all" "2.11.0+rocm7.13.0"))
+Check "gfx1151 pin + 2.11.0+rocm7.2 (generic) -> stale" (IsStale "gfx1151" "2.11.0+rocm7.2")
+Check "gfx1151 pin + 2.10.0+rocm6.4 -> stale" (IsStale "gfx1151" "2.10.0+rocm6.4")
+# Non-2.11 gfx pin (gfx110X-all/gfx90a/gfx908): a valid <2.11 wheel is NOT stale.
+Check "gfx110x-all pin + 2.10.0+rocm6.4 -> not stale" (-not (IsStale "gfx110x-all" "2.10.0+rocm6.4"))
+Check "gfx90a pin + 2.10.0+rocm6.3 -> not stale" (-not (IsStale "gfx90a" "2.10.0+rocm6.3"))
+Check "gfx908 pin + 2.10.0+rocm7.0 -> not stale" (-not (IsStale "gfx908" "2.10.0+rocm7.0"))
+Check "gfx110x-all pin + 2.11.0+rocm7.2 -> stale" (IsStale "gfx110x-all" "2.11.0+rocm7.2")
+# Non-2.11 gfx pin over an untagged wheel: never satisfies the pin -> stale, so the
+# explicit ROCm index is applied even when torch is already <2.11.
+Check "gfx110x-all pin + 2.10.0 (untagged) -> stale" (IsStale "gfx110x-all" "2.10.0")
+Check "gfx90a pin + 2.10.0 (untagged) -> stale" (IsStale "gfx90a" "2.10.0")
+# Capital gfx120X-all is lowercased by Get-TorchIndexLeaf before this helper, so the
+# 2.11-allowlist branch fires and a generic/untagged wheel is stale.
+Check "gfx120x-all pin + 2.11.0+rocm7.2 (generic) -> stale" (IsStale "gfx120x-all" "2.11.0+rocm7.2")
+Check "gfx120x-all pin + 2.10.0 (untagged) -> stale" (IsStale "gfx120x-all" "2.10.0")
+
+# Major-only rocm pin (rocm7): majors compared alone; mirrors _rocm_pin_family_mismatch.
+Check "rocm7 pin + 2.10.0+rocm6.4 -> stale" (IsStale "rocm7" "2.10.0+rocm6.4")
+Check "rocm7 pin + 2.11.0+rocm7.2 -> not stale" (-not (IsStale "rocm7" "2.11.0+rocm7.2"))
+Check "rocm7 pin + 2.11.0+rocm7.13.0 -> not stale" (-not (IsStale "rocm7" "2.11.0+rocm7.13.0"))
+Check "rocm7 pin + 2.10.0 (untagged) -> stale" (IsStale "rocm7" "2.10.0")
+Check "rocm7 pin + 2.10.0+rocm (unreadable) -> not stale" (-not (IsStale "rocm7" "2.10.0+rocm"))
+
+Write-Host "Test-RocmKnown211Version + KNOWN-2.11 fallback (rocm7.2 only; no speculative rocm7.3)"
+Check "rocm7.2 -> known 2.11" (Test-RocmKnown211Version -Major 7 -Minor 2)
+Check "rocm7.1 -> not known" (-not (Test-RocmKnown211Version -Major 7 -Minor 1))
+Check "rocm7.3 -> not known" (-not (Test-RocmKnown211Version -Major 7 -Minor 3))
+Check "rocm8.0 -> not known" (-not (Test-RocmKnown211Version -Major 8 -Minor 0))
+# Unreadable-installed fallback: a rocm7.3 pin (unknown -> <2.11 line) over a <2.11 +rocm
+# wheel with an unreadable version is NOT stale; rocm7.2 (KNOWN-2.11) over the same wheel
+# IS stale (#2534 alignment).
+Check "rocm7.3 pin + 2.10.0+rocm (unreadable ver) -> not stale" (-not (IsStale "rocm7.3" "2.10.0+rocm"))
+Check "rocm7.2 pin + 2.10.0+rocm (unreadable ver) -> stale" (IsStale "rocm7.2" "2.10.0+rocm")
+
+Write-Host ""
+if ($failures -gt 0) { Write-Host "$failures check(s) FAILED" -ForegroundColor Red; exit 1 }
+Write-Host "All checks passed" -ForegroundColor Green
diff --git a/tests/studio/test_torch_flavor.ps1 b/tests/studio/test_torch_flavor.ps1
index 50be4814b0..f2cc55c21c 100644
--- a/tests/studio/test_torch_flavor.ps1
+++ b/tests/studio/test_torch_flavor.ps1
@@ -15,7 +15,7 @@ $tokens = $null; $errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($installPath, [ref]$tokens, [ref]$errors)
if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "install.ps1 has parse errors" }
-foreach ($name in @("ConvertTo-TorchFlavorTag", "Get-ExpectedTorchFlavorTag")) {
+foreach ($name in @("ConvertTo-TorchFlavorTag", "Get-ExpectedTorchFlavorTag", "Trim-IndexPathSlashes", "Redact-InstallOutput")) {
$fn = $ast.FindAll({ param($n)
$n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name
}, $true)
@@ -49,6 +49,19 @@ Check "mirror cu130 leaf -> cu130" ((Get-ExpectedTorchFlavorTag -TorchIndexUrl
Check "unrecognized leaf -> null" ($null -eq (Get-ExpectedTorchFlavorTag -TorchIndexUrl "https://my.mirror/whl/simple"))
Check "empty url -> null" ($null -eq (Get-ExpectedTorchFlavorTag -TorchIndexUrl ""))
+Write-Host "Trim-IndexPathSlashes (install.ps1 parity: path-only, token-preserving)"
+Check "double path slash collapsed" ((Trim-IndexPathSlashes "https://h/whl/cu128//") -eq "https://h/whl/cu128")
+Check "single trailing slash trimmed" ((Trim-IndexPathSlashes "https://h/whl/cu128/") -eq "https://h/whl/cu128")
+Check "query token slash preserved" ((Trim-IndexPathSlashes "https://h/whl/cu128?token=ab12cd/") -eq "https://h/whl/cu128?token=ab12cd/")
+Check "path slash trimmed, query kept" ((Trim-IndexPathSlashes "https://h/whl/cu128//?token=ab12cd/") -eq "https://h/whl/cu128?token=ab12cd/")
+
+Write-Host "Redact-InstallOutput (install.ps1 parity: credential redaction)"
+Check "userinfo redacted" ((Redact-InstallOutput "ERROR https://alice:s3cr3t@download.pytorch.org/whl/cu128") -eq "ERROR https://@download.pytorch.org/whl/cu128")
+Check "query value redacted" ((Redact-InstallOutput "https://host/whl/cu128?token=abcd1234&channel=beta") -eq "https://host/whl/cu128?token=&channel=")
+Check "fragment token redacted" ((Redact-InstallOutput "ERROR https://mirror.local/whl/cu128#token=SECRET123 (403)") -eq "ERROR https://mirror.local/whl/cu128# (403)")
+Check "bare hash comment untouched" ((Redact-InstallOutput "# retrying with --no-cache-dir") -eq "# retrying with --no-cache-dir")
+Check "plain line untouched" ((Redact-InstallOutput "Resolved 42 packages in 1.2s") -eq "Resolved 42 packages in 1.2s")
+
Write-Host ""
if ($failures -gt 0) { Write-Host "$failures check(s) FAILED" -ForegroundColor Red; exit 1 }
Write-Host "All checks passed" -ForegroundColor Green
diff --git a/tests/studio/test_torch_index_pin_hardening.ps1 b/tests/studio/test_torch_index_pin_hardening.ps1
new file mode 100644
index 0000000000..b8edf3da25
--- /dev/null
+++ b/tests/studio/test_torch_index_pin_hardening.ps1
@@ -0,0 +1,78 @@
+#!/usr/bin/env pwsh
+# 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 setup.ps1's torch-index pin-hardening helpers: Trim-IndexPathSlashes
+# (path-only slash trim, token-preserving), Redact-InstallOutput (credential redaction of
+# captured install logs), Get-TorchIndexLeaf (ALL trailing slashes stripped) and
+# Test-PipRocmFamilyLeaf (rocm7. is a custom pin, not a family). Pure helpers, AST-extracted
+# and run in-process. Run: pwsh -NoProfile -File tests/studio/test_torch_index_pin_hardening.ps1
+
+$ErrorActionPreference = "Stop"
+$setupPath = [System.IO.Path]::Combine($PSScriptRoot, "..", "..", "studio", "setup.ps1")
+$setupPath = (Resolve-Path $setupPath).Path
+$setupText = Get-Content -Raw $setupPath
+
+# --- Parse setup.ps1 (also a syntax gate) and extract the pure helpers ---
+$tokens = $null; $errors = $null
+$ast = [System.Management.Automation.Language.Parser]::ParseFile($setupPath, [ref]$tokens, [ref]$errors)
+if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "setup.ps1 has parse errors" }
+
+foreach ($name in @("Trim-IndexPathSlashes", "Redact-InstallOutput", "Get-TorchIndexLeaf", "Test-PipRocmFamilyLeaf")) {
+ $fn = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name
+ }, $true)
+ if ($fn.Count -ne 1) { throw "expected exactly one $name in setup.ps1, found $($fn.Count)" }
+ Invoke-Expression $fn[0].Extent.Text
+}
+
+$failures = 0
+function Check($name, $cond) {
+ if ($cond) { Write-Host " PASS $name" }
+ else { Write-Host " FAIL $name" -ForegroundColor Red; $script:failures++ }
+}
+
+Write-Host "Trim-IndexPathSlashes (path-only, token-preserving)"
+Check "double path slash collapsed" ((Trim-IndexPathSlashes "https://h/whl/cu128//") -eq "https://h/whl/cu128")
+Check "single trailing slash trimmed" ((Trim-IndexPathSlashes "https://h/whl/cu128/") -eq "https://h/whl/cu128")
+Check "no slash unchanged" ((Trim-IndexPathSlashes "https://h/whl/cu128") -eq "https://h/whl/cu128")
+Check "query token slash preserved" ((Trim-IndexPathSlashes "https://h/whl/cu128?token=ab12cd/") -eq "https://h/whl/cu128?token=ab12cd/")
+Check "path slash trimmed, query kept" ((Trim-IndexPathSlashes "https://h/whl/cu128//?token=ab12cd/") -eq "https://h/whl/cu128?token=ab12cd/")
+Check "fragment slash preserved" ((Trim-IndexPathSlashes "https://h/whl/cu128#anchor/") -eq "https://h/whl/cu128#anchor/")
+
+Write-Host "Redact-InstallOutput (credential redaction)"
+Check "userinfo redacted" ((Redact-InstallOutput "ERROR https://alice:s3cr3t@download.pytorch.org/whl/cu128") -eq "ERROR https://@download.pytorch.org/whl/cu128")
+Check "bare-token@ redacted" ((Redact-InstallOutput "fetch https://ghp_deadbeef@host/whl/cu128 failed") -eq "fetch https://@host/whl/cu128 failed")
+Check "single query value redacted" ((Redact-InstallOutput "url https://host/whl/cu128?token=abcd1234 unreachable") -eq "url https://host/whl/cu128?token= unreachable")
+Check "multiple query values redacted" ((Redact-InstallOutput "https://host/whl/cu128?token=abcd1234&channel=beta") -eq "https://host/whl/cu128?token=&channel=")
+Check "fragment token redacted" ((Redact-InstallOutput "ERROR https://mirror.local/whl/cu128#token=SECRET123 (403)") -eq "ERROR https://mirror.local/whl/cu128# (403)")
+Check "query and fragment both redacted" ((Redact-InstallOutput "https://host/whl/cu128?token=abc#sig=xyz done") -eq "https://host/whl/cu128?token=# done")
+Check "bare hash comment untouched" ((Redact-InstallOutput "# retrying with --no-cache-dir") -eq "# retrying with --no-cache-dir")
+Check "plain line untouched" ((Redact-InstallOutput "Resolved 42 packages in 1.2s") -eq "Resolved 42 packages in 1.2s")
+$leak = Redact-InstallOutput "https://alice:s3cr3t@host/whl/cu128?token=SUPERSECRET#frag=ALSOSECRET"
+Check "no secret substring survives" (($leak -notmatch "s3cr3t") -and ($leak -notmatch "SUPERSECRET") -and ($leak -notmatch "ALSOSECRET"))
+
+Write-Host "Get-TorchIndexLeaf (ALL trailing slashes stripped)"
+Check "double slash cu128 -> cu128" ((Get-TorchIndexLeaf "https://m/whl/cu128//") -eq "cu128")
+Check "triple slash rocm7.2 -> rocm7.2" ((Get-TorchIndexLeaf "https://m/whl/rocm7.2///") -eq "rocm7.2")
+Check "double slash + token -> cu128" ((Get-TorchIndexLeaf "https://m/whl/cu128//?token=x") -eq "cu128")
+Check "single slash cu128 -> cu128" ((Get-TorchIndexLeaf "https://m/whl/cu128/") -eq "cu128")
+
+Write-Host "Test-PipRocmFamilyLeaf (rocm7. is a custom pin, not a family)"
+Check "rocm7 family" (Test-PipRocmFamilyLeaf "rocm7")
+Check "rocm7.2 family" (Test-PipRocmFamilyLeaf "rocm7.2")
+Check "gfx1151 family" (Test-PipRocmFamilyLeaf "gfx1151")
+Check "rocm7. trailing-dot NOT family" (-not (Test-PipRocmFamilyLeaf "rocm7."))
+Check "rocm.7 leading-dot NOT family" (-not (Test-PipRocmFamilyLeaf "rocm.7"))
+Check "rocm7.2.1 two-dot NOT family" (-not (Test-PipRocmFamilyLeaf "rocm7.2.1"))
+Check "rocm7.2-private NOT family" (-not (Test-PipRocmFamilyLeaf "rocm7.2-private"))
+Check "cu128 NOT family" (-not (Test-PipRocmFamilyLeaf "cu128"))
+
+Write-Host "Fast-Install pinned-install env scrub (source assertion)"
+# The pip fallback honours PIP_*; PIP_NO_INDEX=1 would make it ignore the pinned --index-url
+# and PIP_INDEX_URL would replace it, so both must be scrubbed for a pinned install.
+Check "PIP_NO_INDEX scrubbed" ($setupText -match "'PIP_NO_INDEX'")
+Check "PIP_INDEX_URL scrubbed" ($setupText -match "'PIP_INDEX_URL'")
+
+Write-Host ""
+if ($failures -gt 0) { Write-Host "$failures check(s) FAILED" -ForegroundColor Red; exit 1 }
+Write-Host "All checks passed" -ForegroundColor Green
diff --git a/tests/utils/test_packing.py b/tests/utils/test_packing.py
index a8557d8533..1b8bb65058 100644
--- a/tests/utils/test_packing.py
+++ b/tests/utils/test_packing.py
@@ -14,6 +14,8 @@
# along with this program. If not, see .
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,
@@ -21,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
@@ -29,7 +32,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 +163,724 @@ 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):
+ 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"),
+ ),
+)
+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],
+ is_encoder_decoder = True,
+ ),
+ max_seq_length = 16,
+ )
+
+ 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)
+
+ 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)
+
+
class _DummyChild(torch.nn.Module):
def __init__(self):
super().__init__()
@@ -390,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/_utils.py b/unsloth/models/_utils.py
index 3d41b505df..57169fa3de 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-__version__ = "2026.7.3"
+__version__ = "2026.7.4"
__all__ = [
"SUPPORTS_BFLOAT16",
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index ffb845b04f..4ef3af6add 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',
)
@@ -441,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":
@@ -458,6 +500,58 @@ def sft_trainer_prepare_dataset(function_name, function):
if matched:
# Use fast version!
function = inspect.getsource(fast_sft_prepare_dataset)
+ # 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,
+ 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."
+ )
+ # 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 = _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",
+ )
+ # 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,
+ getattr(args, "packing_strategy", "bfd"),
+ map_kwargs,
+ )""",
+ """_pack_kwargs = {"map_kwargs": map_kwargs}
+ 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)
function = function.replace("def sft_prepare_dataset", "def _prepare_dataset")
@@ -2120,19 +2214,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..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,
)
@@ -100,6 +102,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 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:
@@ -137,6 +143,183 @@ 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
+
+
+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
@@ -498,30 +681,69 @@ 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
+ 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_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
- 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_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
@@ -535,10 +757,16 @@ 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_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)}"
message = f"Unsloth: Sample packing skipped ({reason} detected)."
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]]:
diff --git a/unsloth_cli/_inference.py b/unsloth_cli/_inference.py
index 551bef4787..a2b0f9c04f 100644
--- a/unsloth_cli/_inference.py
+++ b/unsloth_cli/_inference.py
@@ -235,7 +235,7 @@ def collect_stream(stream, show_thinking: bool) -> str:
def raise_on_streamed_error(stream):
# Match real backend errors by type (GenStreamError), not the "Error:" text
# prefix, so a completion whose text opens with "Error:" is not misread as a
- # failure that aborts a distributed run.
+ # backend failure.
try:
ensure_studio_backend_path()
from core.inference.orchestrator import GenStreamError
diff --git a/unsloth_cli/commands/chat.py b/unsloth_cli/commands/chat.py
index bba5fab08e..fdc5577700 100644
--- a/unsloth_cli/commands/chat.py
+++ b/unsloth_cli/commands/chat.py
@@ -331,7 +331,7 @@ def chat(
enable_thinking = show_thinking,
use_adapter = use_adapter,
)
- return raise_on_streamed_error(stream) if is_mlx_distributed else stream
+ return raise_on_streamed_error(stream)
if should_print:
console.print()
diff --git a/unsloth_cli/commands/inference.py b/unsloth_cli/commands/inference.py
index 524d8fd015..7df05cef94 100644
--- a/unsloth_cli/commands/inference.py
+++ b/unsloth_cli/commands/inference.py
@@ -111,15 +111,12 @@ def inference(
repetition_penalty = repetition_penalty,
enable_thinking = think,
)
- if is_mlx_distributed:
- stream = raise_on_streamed_error(stream)
+ stream = raise_on_streamed_error(stream)
if rank == 0:
typer.echo("Assistant:")
try:
stream_to_stdout(stream, show_thinking = think)
except RuntimeError as exc:
- if not is_mlx_distributed:
- raise
typer.echo(f"Error: {exc}", err = True)
raise typer.Exit(code = 1)
else:
diff --git a/unsloth_cli/commands/start.py b/unsloth_cli/commands/start.py
index 707c2b3c90..3d73df65be 100644
--- a/unsloth_cli/commands/start.py
+++ b/unsloth_cli/commands/start.py
@@ -192,7 +192,7 @@ _OPENCODE_NATIVE_AUTO_MIN_VERSION = (1, 17, 12)
def _opencode_supports_native_auto() -> bool:
- executable = shutil.which("opencode")
+ executable = _which_with_install_dirs("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.
@@ -826,7 +826,7 @@ def _resolve_model(
)
if requested and match is None:
typer.echo(
- f"Ensuring {requested} is loaded with the requested settings…"
+ f"Loading {requested} - please wait…"
if load_has_overrides
else f"Loading {requested} on the Unsloth server (this can take a while)…"
)
@@ -902,17 +902,21 @@ def _require_gguf_for_codex(base: str, key: str, model_id: str) -> None:
_DYNAMIC_SECTIONS_FLAG = "--exclude-dynamic-system-prompt-sections"
-# Session overlay applied via `claude --settings`; suppresses the attribution header
-# for THIS run only (no ~/.claude write) so llama.cpp KV-cache reuse is preserved. It
-# reinforces the CLAUDE_CODE_ATTRIBUTION_HEADER env var on builds that read the setting
-# only from settings.json.
-_CLAUDE_SETTINGS_OVERLAY = '{"env":{"CLAUDE_CODE_ATTRIBUTION_HEADER":"0"}}'
+
+
+def _claude_settings_overlay(model_id: str) -> str:
+ # Session-only `claude --settings` overlay (command-line tier, no ~/.claude write):
+ # suppress the attribution header, and pin availableModels to the served model so a
+ # user allowlist can't reject it. The pin must be non-empty; [] is ignored.
+ return json.dumps(
+ {"env": {"CLAUDE_CODE_ATTRIBUTION_HEADER": "0"}, "availableModels": [model_id]}
+ )
def _claude_version() -> Optional[tuple]:
# None = no local `claude` (a --no-launch printout for another machine; assume a
# current build). An unparseable version is treated as too old for the new flags.
- executable = shutil.which("claude")
+ executable = _which_with_install_dirs("claude")
if executable is None:
return None
try:
@@ -929,16 +933,14 @@ def _claude_version() -> Optional[tuple]:
return (0,)
-def _claude_flags() -> list:
- # Both knobs preserve llama.cpp KV-cache reuse: --exclude-dynamic-system-prompt-sections
- # moves per-session context out of the system prompt, and --settings suppresses the
- # attribution header for this session only (no persistent ~/.claude write; the env var
- # sets it too). Claude Code < 2.1.98 aborts on unknown flags, so gate on the version;
- # no local binary means a printout for another machine, so assume a current build.
+def _claude_flags(model_id: str) -> list:
+ # KV-cache-preserving flags: move per-session context out of the system prompt and pass
+ # the session overlay. claude < 2.1.98 rejects unknown flags; no local binary means a
+ # printout for another machine, so assume a current build.
version = _claude_version()
if version is not None and version < (2, 1, 98):
return []
- return [_DYNAMIC_SECTIONS_FLAG, "--settings", _CLAUDE_SETTINGS_OVERLAY]
+ return [_DYNAMIC_SECTIONS_FLAG, "--settings", _claude_settings_overlay(model_id)]
def _merge_codex_config(existing: str, base: str) -> str:
@@ -971,7 +973,7 @@ _CODEX_MODEL_CATALOG_MIN_VERSION = (0, 110, 0)
def _codex_supports_model_catalog() -> bool:
- executable = shutil.which("codex")
+ executable = _which_with_install_dirs("codex")
if executable is None:
# A --no-launch recipe may be copied to another machine; assume a current Codex.
return True
@@ -1202,6 +1204,53 @@ def _refresh_windows_path() -> None:
os.environ["PATH"] = os.pathsep.join(entries)
+def _augment_path_with_install_dirs() -> None:
+ # Append known install dirs to PATH so a freshly installed agent resolves without a new
+ # shell: some installers write the binary but not PATH (claude drops ~/.local/bin and
+ # only prints a note; npm -g shims land in %APPDATA%\npm). Appended, so precedence holds.
+ try:
+ home = Path.home()
+ except (RuntimeError, OSError):
+ return
+ candidates = [home / ".local" / "bin"]
+ if os.name == "nt":
+ appdata = os.environ.get("APPDATA")
+ if appdata:
+ candidates.append(Path(appdata) / "npm")
+ current = os.environ.get("PATH")
+ if current is None:
+ # PATH unset: shutil.which() and exec*p* fall back to os.defpath (e.g. /bin:/usr/bin), so
+ # keep that default instead of collapsing to just the install dirs (which would hide a
+ # system-installed agent and strip the launched child's normal PATH). An explicitly empty
+ # PATH is left as-is: like shutil.which, it means "search nothing", not os.defpath.
+ current = os.defpath
+ seen = {os.path.normcase(entry) for entry in current.split(os.pathsep) if entry}
+ additions = [
+ str(directory)
+ for directory in candidates
+ if directory.is_dir() and os.path.normcase(str(directory)) not in seen
+ ]
+ if additions:
+ os.environ["PATH"] = os.pathsep.join([current, *additions] if current else additions)
+
+
+def _which_with_install_dirs(name: str) -> Optional[str]:
+ # shutil.which(name), but searching the known agent install dirs too, so a version probe
+ # resolves the same binary _launch() will (it augments PATH before it runs). Without this an
+ # agent present only in ~/.local/bin / %APPDATA%\npm is missed, wrongly assumed current, and
+ # launched with flags an older build rejects. PATH is restored afterward: only _launch()
+ # should persist the augmentation for the child process.
+ original = os.environ.get("PATH")
+ _augment_path_with_install_dirs()
+ try:
+ return shutil.which(name)
+ finally:
+ if original is None:
+ os.environ.pop("PATH", None)
+ else:
+ os.environ["PATH"] = original
+
+
def _install_source(install_hint: str) -> Optional[str]:
"""The first http(s) URL an install hint fetches, or None (e.g. an npm install)."""
match = re.search(r"https?://[^\s'\")]+", install_hint)
@@ -1254,17 +1303,35 @@ def _install_agent(name: str, install_hint: str) -> Optional[str]:
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)
- # on Windows, /bin/sh (curl | bash, or npm) everywhere else.
+ # Run each hint through its shell: PowerShell on Windows, /bin/sh elsewhere.
+ # -ExecutionPolicy Bypass is process-scoped (nothing persistent) so npm's npm.ps1 and
+ # irm | iex run under the Windows default Restricted policy instead of failing with a
+ # PSSecurityException.
if os.name == "nt":
- install_command = ["powershell", "-NoProfile", "-Command", install_hint]
+ install_command = [
+ "powershell",
+ "-NoProfile",
+ "-ExecutionPolicy",
+ "Bypass",
+ "-Command",
+ install_hint,
+ ]
else:
install_command = ["/bin/sh", "-c", install_hint]
if subprocess.run(install_command).returncode != 0:
- _fail(f"Install command failed. Run it yourself, then re-run: {install_hint}")
- # The installer just wrote PATH to the registry (Windows); pull it into this
- # process so the freshly installed agent resolves without a shell restart.
+ message = f"Install command failed. Run it yourself, then re-run: {install_hint}"
+ if os.name == "nt":
+ # A hand-run retry can still hit the policy; point at the one-time per-user fix.
+ message += (
+ "\nIf it fails because running scripts is disabled (PSSecurityException), "
+ "allow local scripts for your user, then retry:\n"
+ " Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned"
+ )
+ _fail(message)
+ # Resolve the freshly installed agent without a shell restart: pull registry PATH
+ # (Windows) plus well-known install dirs the installer may not have added to PATH.
_refresh_windows_path()
+ _augment_path_with_install_dirs()
executable = shutil.which(name)
if executable is None:
_fail(
@@ -1290,6 +1357,9 @@ def _launch(
install_hint: str,
unset_env: tuple = (),
) -> NoReturn:
+ # Resolve well-known install dirs (e.g. ~/.local/bin) first, so an already-installed
+ # agent not yet on PATH is found instead of prompting a needless reinstall.
+ _augment_path_with_install_dirs()
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}")
@@ -1780,7 +1850,7 @@ def claude(
"claude",
"--model",
model_id,
- *_claude_flags(),
+ *_claude_flags(model_id),
*_yolo_command_flags("claude", yolo),
*ctx.args,
]
diff --git a/unsloth_cli/tests/test_inference_chat.py b/unsloth_cli/tests/test_inference_chat.py
index ae6f8dcfd4..07eed01e97 100644
--- a/unsloth_cli/tests/test_inference_chat.py
+++ b/unsloth_cli/tests/test_inference_chat.py
@@ -873,6 +873,104 @@ def test_chat_compare_on_mlx_loads_base_model_side_by_side(monkeypatch):
assert set(closed) == {"tuned", "base"}
+@pytest.mark.parametrize(
+ ("chunk_kind", "expected_exit"),
+ [
+ ("answer", 0),
+ ("model_text_error", 0),
+ ("real_error", 1),
+ ],
+)
+def test_inference_local_handles_stream(monkeypatch, chunk_kind, expected_exit):
+ from unsloth_cli.commands import inference as infermod
+ from unsloth_cli._inference import ensure_studio_backend_path
+
+ ensure_studio_backend_path()
+ from core.inference.orchestrator import GenStreamError
+
+ chunks = {
+ "answer": ["answer"],
+ "model_text_error": ["Error: printed by the model, not a backend failure"],
+ "real_error": [GenStreamError("Error: generation failed")],
+ }[chunk_kind]
+ closed = []
+
+ class _FakeBackend:
+ def stream(self, messages, **kwargs):
+ return iter(chunks)
+
+ def close(self):
+ closed.append(True)
+
+ monkeypatch.setattr(
+ infermod,
+ "connect_studio_server",
+ lambda *_a, **_k: (_ for _ in ()).throw(AssertionError("server disabled")),
+ )
+ monkeypatch.setattr(infermod, "load_chat_backend", lambda *a, **k: _FakeBackend())
+
+ result = CliRunner().invoke(
+ _inference_app(),
+ ["fake-model", "hello", "--no-server"],
+ )
+
+ assert result.exit_code == expected_exit, result.output
+ assert closed == [True]
+ if chunk_kind == "real_error":
+ assert result.stdout == "Assistant:\n"
+ assert result.stderr == "Error: generation failed\n"
+ else:
+ assert chunks[0] in result.output
+
+
+@pytest.mark.parametrize("chunk_kind", ["answer", "model_text_error", "real_error"])
+def test_chat_local_handles_stream(monkeypatch, chunk_kind):
+ from unsloth_cli._inference import ensure_studio_backend_path
+
+ ensure_studio_backend_path()
+ from core.inference.orchestrator import GenStreamError
+
+ first_chunk = {
+ "answer": "answer",
+ "model_text_error": "Error: printed by the model, not a backend failure",
+ "real_error": GenStreamError("Error: generation failed"),
+ }[chunk_kind]
+ calls, closed = [], []
+
+ class _FakeChatBackend:
+ def stream(self, messages, **kwargs):
+ calls.append([dict(message) for message in messages])
+ return iter([first_chunk if len(calls) == 1 else "second answer"])
+
+ def close(self):
+ closed.append(True)
+
+ monkeypatch.setattr(chatmod, "resolve_model_config", lambda *a, **k: _FakeConfig())
+ monkeypatch.setattr(chatmod, "connect_studio_server", lambda *a, **k: None)
+ monkeypatch.setattr(chatmod, "load_chat_backend", lambda *a, **k: _FakeChatBackend())
+ monkeypatch.setattr(chatmod, "_compare_needs_second_model", lambda: False)
+
+ result = CliRunner().invoke(
+ _chat_app(),
+ ["fake-model"],
+ input = "first\nsecond\n/exit\n",
+ )
+
+ assert result.exit_code == 0, result.output
+ assert closed == [True]
+ if chunk_kind == "real_error":
+ assert calls[1] == [{"role": "user", "content": "second"}]
+ assert "(error: generation failed)" in result.output
+ assert "Error: generation failed" not in result.output
+ else:
+ assert calls[1] == [
+ {"role": "user", "content": "first"},
+ {"role": "assistant", "content": first_chunk},
+ {"role": "user", "content": "second"},
+ ]
+ assert first_chunk in result.output
+
+
@pytest.mark.parametrize(
("chunk_kind", "expected_exit"),
[
diff --git a/unsloth_cli/tests/test_start.py b/unsloth_cli/tests/test_start.py
index 227918f63e..1e03d390d1 100644
--- a/unsloth_cli/tests/test_start.py
+++ b/unsloth_cli/tests/test_start.py
@@ -61,36 +61,73 @@ def _fake_claude(monkeypatch, version_output: str) -> None:
)
+def _path_aware_which(binaries: dict):
+ # A shutil.which fake that resolves a name only when its directory is on PATH at call time.
+ # Lets a test prove a version probe augments PATH before resolving: an agent present only in
+ # an install dir (~/.local/bin, %APPDATA%\npm) must still be found and version-checked.
+ def _which(name):
+ directory = binaries.get(name)
+ if directory is None:
+ return None
+ entries = os.environ.get("PATH", "").split(os.pathsep)
+ # os.path.join (not Path()) so this works when a test has flipped os.name to "nt": under
+ # a simulated os.name, pathlib would build the non-native flavour and raise.
+ return os.path.join(str(directory), name) if str(directory) in entries else None
+
+ return _which
+
+
+def _simulate_windows(monkeypatch) -> None:
+ # Exercise the `os.name == "nt"` branch on any host. Flipping os.name alone makes pathlib
+ # pick the non-native flavour (WindowsPath on POSIX, PosixPath on Windows) when a Path is
+ # constructed, which raises; pin Path to the host-native class (captured before the flip)
+ # so the branch logic runs without that crash. Keeps these tests green on Linux/Mac/WSL too.
+ monkeypatch.setattr(start, "Path", type(Path()))
+ monkeypatch.setattr(start.os, "name", "nt")
+
+
def test_claude_flags_passed_to_supported_claude(monkeypatch):
_fake_claude(monkeypatch, "2.1.98 (Claude Code)\n")
- assert start._claude_flags() == [
+ assert start._claude_flags(MODEL["id"]) == [
"--exclude-dynamic-system-prompt-sections",
"--settings",
- start._CLAUDE_SETTINGS_OVERLAY,
+ start._claude_settings_overlay(MODEL["id"]),
]
def test_claude_flags_skipped_on_old_claude(monkeypatch):
_fake_claude(monkeypatch, "2.0.14 (Claude Code)\n")
- assert start._claude_flags() == []
+ assert start._claude_flags(MODEL["id"]) == []
def test_claude_flags_skipped_on_unparseable_version(monkeypatch):
_fake_claude(monkeypatch, "weird build string\n")
- assert start._claude_flags() == []
+ assert start._claude_flags(MODEL["id"]) == []
def test_claude_flags_detected_when_version_not_first_token(monkeypatch):
# The X.Y.Z is pulled from anywhere in the output, so a format change (version not
# the first token) doesn't silently drop the optimization flags.
_fake_claude(monkeypatch, "claude version 2.1.98\n")
- assert start._claude_flags() == [
+ assert start._claude_flags(MODEL["id"]) == [
"--exclude-dynamic-system-prompt-sections",
"--settings",
- start._CLAUDE_SETTINGS_OVERLAY,
+ start._claude_settings_overlay(MODEL["id"]),
]
+def test_claude_settings_overlay_pins_served_model():
+ # The session overlay must pin availableModels to the served model: a user's allowlist
+ # in ~/.claude/settings.json otherwise rejects the Unsloth --model ("restricted by your
+ # organization's settings"), and no env var can bypass it. The override must be a
+ # NON-EMPTY array to take effect (an empty [] is ignored and the user's list still
+ # applies), so it lists exactly this model, for this session only.
+ overlay = json.loads(start._claude_settings_overlay(MODEL["id"]))
+ assert overlay["availableModels"] == [MODEL["id"]]
+ # The attribution-header suppression is preserved alongside it.
+ assert overlay["env"]["CLAUDE_CODE_ATTRIBUTION_HEADER"] == "0"
+
+
def test_install_agent_prompts_then_installs(monkeypatch):
# TTY + yes: run the documented install command, then re-resolve the now-present binary.
monkeypatch.setattr(start.os, "name", "posix")
@@ -126,7 +163,52 @@ def test_install_agent_uses_powershell_on_windows(monkeypatch):
executable = start._install_agent("hermes", install_hint)
assert executable == r"C:\Users\samle\bin\hermes.exe"
- assert ran == [["powershell", "-NoProfile", "-Command", install_hint]]
+ # -ExecutionPolicy Bypass (process-scoped) lets npm's npm.ps1 wrapper and irm|iex
+ # scripts run even when the machine policy is the Windows default Restricted.
+ assert ran == [
+ ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", install_hint]
+ ]
+
+
+def test_install_agent_windows_failure_hints_execution_policy(monkeypatch, capsys):
+ # A failed install on Windows points the user at the per-user execution-policy fix:
+ # our subprocess bypasses the policy, but their own shell may still block npm.ps1
+ # (PSSecurityException) when they run the install by hand.
+ monkeypatch.setattr(start.os, "name", "nt")
+ monkeypatch.setattr(start.sys, "stdin", SimpleNamespace(isatty = lambda: True))
+ monkeypatch.setattr(start.typer, "confirm", lambda *a, **k: True)
+ monkeypatch.setattr(
+ start.subprocess,
+ "run",
+ lambda *a, **k: SimpleNamespace(returncode = 1),
+ )
+ monkeypatch.setattr(start.shutil, "which", lambda _: None)
+
+ with pytest.raises(start.typer.Exit):
+ start._install_agent("codex", "npm install -g @openai/codex")
+
+ err = capsys.readouterr().err
+ assert "Install command failed" in err
+ assert "Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned" in err
+
+
+def test_install_agent_posix_failure_omits_execution_policy_hint(monkeypatch, capsys):
+ # The execution-policy hint is Windows-only; a POSIX install failure must not mention it.
+ monkeypatch.setattr(start.os, "name", "posix")
+ monkeypatch.setattr(start.sys, "stdin", SimpleNamespace(isatty = lambda: True))
+ monkeypatch.setattr(start.typer, "confirm", lambda *a, **k: True)
+ monkeypatch.setattr(
+ start.subprocess,
+ "run",
+ lambda *a, **k: SimpleNamespace(returncode = 1),
+ )
+
+ with pytest.raises(start.typer.Exit):
+ start._install_agent("codex", "npm install -g @openai/codex")
+
+ err = capsys.readouterr().err
+ assert "Install command failed" in err
+ assert "Set-ExecutionPolicy" not in err
def test_install_agent_warns_remote_installer_is_unverified_third_party(monkeypatch, capsys):
@@ -254,6 +336,172 @@ def test_refresh_windows_path_merges_registry_hives(monkeypatch):
]
+def test_augment_path_adds_existing_local_bin(monkeypatch, tmp_path):
+ # Claude's installer drops its binary in ~/.local/bin but only *suggests* adding it to
+ # PATH, so Unsloth appends it in-process to resolve the freshly installed agent.
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata")) # skip the npm candidate
+ monkeypatch.setenv("PATH", str(tmp_path / "existing"))
+ start._augment_path_with_install_dirs()
+ entries = os.environ["PATH"].split(os.pathsep)
+ assert str(local_bin) in entries
+ # Appended (lowest precedence), so it never shadows an existing PATH entry.
+ assert entries[-1] == str(local_bin)
+
+
+def test_augment_path_skips_missing_and_duplicate_dirs(monkeypatch, tmp_path):
+ # A non-existent ~/.local/bin is not added; an already-present one is not duplicated.
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path) # no .local/bin created yet
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata")) # skip the npm candidate
+ monkeypatch.setenv("PATH", str(tmp_path / "existing"))
+ start._augment_path_with_install_dirs()
+ assert os.environ["PATH"] == str(tmp_path / "existing")
+
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setenv("PATH", os.pathsep.join([str(tmp_path / "existing"), str(local_bin)]))
+ start._augment_path_with_install_dirs()
+ assert os.environ["PATH"].split(os.pathsep).count(str(local_bin)) == 1
+
+
+def test_augment_path_adds_npm_global_bin_on_windows(monkeypatch, tmp_path):
+ # npm -g shims (codex/opencode/pi) land in %APPDATA%\npm on Windows; add it so a freshly
+ # installed npm agent resolves even when that dir isn't on PATH yet.
+ _simulate_windows(monkeypatch)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path) # no ~/.local/bin created
+ npm_dir = tmp_path / "Roaming" / "npm"
+ npm_dir.mkdir(parents = True)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "Roaming"))
+ monkeypatch.setenv("PATH", str(tmp_path / "existing"))
+ start._augment_path_with_install_dirs()
+ assert str(npm_dir) in os.environ["PATH"].split(os.pathsep)
+
+
+def test_which_with_install_dirs_finds_agent_and_restores_path(monkeypatch, tmp_path):
+ # The probe helper resolves against the augmented PATH but must NOT persist it: only
+ # _launch() should mutate PATH for the child process. Here `claude` is only in ~/.local/bin.
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata")) # skip the npm candidate
+ original = str(tmp_path / "existing")
+ monkeypatch.setenv("PATH", original) # local_bin NOT on PATH yet
+ monkeypatch.setattr(start.shutil, "which", _path_aware_which({"claude": local_bin}))
+ assert start._which_with_install_dirs("claude") == str(local_bin / "claude")
+ assert os.environ["PATH"] == original # restored, no global pollution
+
+
+def test_claude_flags_probes_old_agent_only_in_install_dir(monkeypatch, tmp_path):
+ # Regression: the version probe must augment PATH before resolving, so an OLD claude present
+ # only in ~/.local/bin (not yet on PATH) is detected as old and the unsupported flags are
+ # dropped -- the same binary _launch() will run. Before the fix the probe saw no binary,
+ # assumed a current build, and emitted flags the old claude rejects.
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata"))
+ monkeypatch.setenv("PATH", str(tmp_path / "existing"))
+ monkeypatch.setattr(start.shutil, "which", _path_aware_which({"claude": local_bin}))
+ monkeypatch.setattr(
+ start.subprocess, "run", lambda *a, **k: SimpleNamespace(stdout = "2.0.14 (Claude Code)\n")
+ )
+ assert start._claude_flags(MODEL["id"]) == []
+
+
+def test_claude_flags_detects_supported_agent_only_in_install_dir(monkeypatch, tmp_path):
+ # The counterpart: a SUPPORTED claude present only in ~/.local/bin is now resolved and gets
+ # the flags, instead of being missed and (coincidentally) also assumed current.
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata"))
+ monkeypatch.setenv("PATH", str(tmp_path / "existing"))
+ monkeypatch.setattr(start.shutil, "which", _path_aware_which({"claude": local_bin}))
+ monkeypatch.setattr(
+ start.subprocess, "run", lambda *a, **k: SimpleNamespace(stdout = "2.1.98 (Claude Code)\n")
+ )
+ assert start._claude_flags(MODEL["id"]) == [
+ "--exclude-dynamic-system-prompt-sections",
+ "--settings",
+ start._claude_settings_overlay(MODEL["id"]),
+ ]
+
+
+def test_claude_flags_probes_npm_install_dir_on_windows(monkeypatch, tmp_path):
+ # npm -g shims land in %APPDATA%\npm on Windows; an old claude there (not on PATH) must still
+ # be version-checked so the unsupported flags are dropped.
+ _simulate_windows(monkeypatch)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path) # no ~/.local/bin created
+ npm_dir = tmp_path / "Roaming" / "npm"
+ npm_dir.mkdir(parents = True)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "Roaming"))
+ monkeypatch.setenv("PATH", str(tmp_path / "existing"))
+ monkeypatch.setattr(start.shutil, "which", _path_aware_which({"claude": npm_dir}))
+ monkeypatch.setattr(
+ start.subprocess, "run", lambda *a, **k: SimpleNamespace(stdout = "2.0.14 (Claude Code)\n")
+ )
+ assert start._claude_flags(MODEL["id"]) == []
+
+
+def test_codex_catalog_probes_old_codex_only_in_install_dir(monkeypatch, tmp_path):
+ # Same ordering fix for codex: an old codex present only in an install dir is detected so the
+ # model-catalog config is omitted (the old binary can't consume it).
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata"))
+ monkeypatch.setenv("PATH", str(tmp_path / "existing"))
+ monkeypatch.setattr(start.shutil, "which", _path_aware_which({"codex": local_bin}))
+ monkeypatch.setattr(start.subprocess, "check_output", lambda *a, **k: "codex-cli 0.109.0")
+ assert start._codex_supports_model_catalog() is False
+
+
+def test_opencode_native_auto_probes_old_opencode_only_in_install_dir(monkeypatch, tmp_path):
+ # Same ordering fix for opencode: an old opencode present only in an install dir is detected
+ # so native --auto is not assumed (the old binary rejects it).
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata"))
+ monkeypatch.setenv("PATH", str(tmp_path / "existing"))
+ monkeypatch.setattr(start.shutil, "which", _path_aware_which({"opencode": local_bin}))
+ monkeypatch.setattr(start.subprocess, "check_output", lambda *a, **k: "1.17.11")
+ assert start._opencode_supports_native_auto() is False
+
+
+def test_augment_path_preserves_defpath_when_path_unset(monkeypatch, tmp_path):
+ # PATH unset: shutil.which() and exec*p* fall back to os.defpath (e.g. /bin:/usr/bin), so the
+ # augmentation must keep those default dirs instead of collapsing to just the install dir
+ # (which would hide a system-installed agent and strip the launched child's normal PATH).
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata"))
+ monkeypatch.delenv("PATH", raising = False)
+ start._augment_path_with_install_dirs()
+ entries = os.environ["PATH"].split(os.pathsep)
+ for default_dir in os.defpath.split(os.pathsep):
+ if default_dir:
+ assert default_dir in entries
+ assert str(local_bin) in entries
+
+
+def test_which_with_install_dirs_keeps_defpath_when_path_unset(monkeypatch, tmp_path):
+ # With PATH unset, a system agent on os.defpath (e.g. /usr/bin) must still resolve; the
+ # install-dir augmentation must not drop the default search path. PATH is restored to unset.
+ local_bin = tmp_path / ".local" / "bin"
+ local_bin.mkdir(parents = True)
+ monkeypatch.setattr(start.Path, "home", lambda: tmp_path)
+ monkeypatch.setenv("APPDATA", str(tmp_path / "no-appdata"))
+ monkeypatch.delenv("PATH", raising = False)
+ sysdir = next(part for part in reversed(os.defpath.split(os.pathsep)) if part)
+ monkeypatch.setattr(start.shutil, "which", _path_aware_which({"claude": Path(sysdir)}))
+ assert start._which_with_install_dirs("claude") == os.path.join(sysdir, "claude")
+ assert "PATH" not in os.environ
+
+
def test_install_agent_declined_returns_none(monkeypatch):
# TTY + no: never runs anything; caller falls back to the print-hint failure.
monkeypatch.setattr(start.sys, "stdin", SimpleNamespace(isatty = lambda: True))
@@ -451,7 +699,7 @@ def test_connect_claude_launch_scrubs_conflicting_auth_env(fake_studio, monkeypa
monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-anthropic-stale")
monkeypatch.setenv("CLAUDE_CODE_OAUTH_TOKEN", "oauth-stale")
monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/claude")
- monkeypatch.setattr(start, "_claude_flags", lambda: [])
+ monkeypatch.setattr(start, "_claude_flags", lambda *a, **k: [])
def run(command, env):
captured["command"] = command
@@ -486,7 +734,7 @@ def test_connect_claude_windows_shim_from_wsl_bridges_env(fake_studio, monkeypat
monkeypatch.setattr(
start.shutil, "which", lambda _: "/mnt/c/Users/samle/AppData/Roaming/npm/claude"
)
- monkeypatch.setattr(start, "_claude_flags", lambda: [])
+ monkeypatch.setattr(start, "_claude_flags", lambda *a, **k: [])
def run(command, env):
captured["command"] = command
@@ -2175,8 +2423,9 @@ def test_powershell_quote_single_quotes_json():
# keeps the embedded double quotes literal (list2cmdline's backslashes would not).
assert start._powershell_quote("--settings") == "--settings"
assert start._powershell_quote("unsloth/gemma-4-26B") == "unsloth/gemma-4-26B"
- quoted = start._powershell_quote(start._CLAUDE_SETTINGS_OVERLAY)
- assert quoted == "'" + start._CLAUDE_SETTINGS_OVERLAY + "'"
+ overlay = start._claude_settings_overlay("unsloth/gemma-4-26B")
+ quoted = start._powershell_quote(overlay)
+ assert quoted == "'" + overlay + "'"
assert "\\" not in quoted # no cmd.exe backslash escaping
assert start._powershell_quote("a'b") == "'a''b'" # embedded quote doubled
@@ -2811,7 +3060,7 @@ def test_claude_launch_does_not_clear(fake_studio, monkeypatch):
calls = []
monkeypatch.setattr(start.click, "clear", lambda: calls.append("clear"))
monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/claude")
- monkeypatch.setattr(start, "_claude_flags", lambda: [])
+ monkeypatch.setattr(start, "_claude_flags", lambda *a, **k: [])
monkeypatch.setattr(start.subprocess, "run", lambda command, env: SimpleNamespace(returncode = 0))
result = CliRunner().invoke(start.start_app, ["claude"])
assert result.exit_code == 0, result.output
@@ -2988,7 +3237,7 @@ def test_persist_bare_opencode_launch_has_no_resume_token(fake_studio, monkeypat
def test_persist_bare_claude_launch_has_no_resume_token(fake_studio, monkeypatch):
monkeypatch.setattr(start.shutil, "which", lambda _: "/usr/local/bin/claude")
- monkeypatch.setattr(start, "_claude_flags", lambda: [])
+ monkeypatch.setattr(start, "_claude_flags", lambda *a, **k: [])
captured = _capture_launch(monkeypatch, ["claude", "--persist"])
assert "--continue" not in captured["command"]
assert captured["command"][1:] == ["--model", MODEL["id"]]
@@ -3152,7 +3401,7 @@ def test_native_resume_flag_passes_through_unchanged(fake_studio, monkeypatch):
# `--resume ` (e.g. `unsloth start claude --resume `) still flows
# 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: [])
+ monkeypatch.setattr(start, "_claude_flags", lambda *a, **k: [])
captured = _capture_launch(monkeypatch, ["claude", "--resume", "some-session-guid"])
assert captured["command"][-2:] == ["--resume", "some-session-guid"]
# Unsloth never auto-appends its own resume token when the user drives resume.