Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix Windows installer torch index override (#6972)
alkinun216a1fad3356
diff --git a/install.ps1 b/install.ps1index 696f4e613a6..0797cd38684 100644--- a/install.ps1+++ b/install.ps1@@ -469,6 +469,17 @@ function Install-UnslothStudio {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.+ $savedUvIndex = $null+ if ($Command.ToString() -match '--default-index') {+ $savedUvIndex = @{}+ foreach ($n in 'UV_DEFAULT_INDEX', 'UV_INDEX_URL', 'UV_INDEX', 'UV_EXTRA_INDEX_URL') {+ $savedUvIndex[$n] = [Environment]::GetEnvironmentVariable($n)+ Remove-Item "Env:$n" -ErrorAction SilentlyContinue+ }+ }$prevEap = $ErrorActionPreference$ErrorActionPreference = "Continue"try {@@ -488,6 +499,7 @@ function Install-UnslothStudio {return [int]$LASTEXITCODE} finally {$ErrorActionPreference = $prevEap+ if ($savedUvIndex) { foreach ($n in $savedUvIndex.Keys) { if ($null -ne $savedUvIndex[$n]) { Set-Item "Env:$n" $savedUvIndex[$n] } } }}}@@ -2200,7 +2212,7 @@ exit 0# 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" }- $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (AMD ROCm)" { uv pip install --python $VenvPython --force-reinstall --index-url $ROCmIndexUrl $torchSpec $visionSpec $audioSpec }+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (AMD ROCm)" { uv pip install --python $VenvPython --force-reinstall --default-index $ROCmIndexUrl $torchSpec $visionSpec $audioSpec }if ($torchInstallExit -ne 0) {# Transient AMD-index failure: fall back to a CPU base so the install# still completes; Studio setup retries ROCm afterwards.@@ -2209,7 +2221,7 @@ exit 0# 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 --index-url $TorchIndexUrl }+ $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 }if ($torchInstallExit -ne 0) {Write-Host "[ERROR] Failed to install PyTorch (ROCm and CPU base both failed, exit code $torchInstallExit)" -ForegroundColor Redreturn (Exit-InstallFailure "Failed to install PyTorch (exit code $torchInstallExit)" $torchInstallExit)@@ -2223,7 +2235,7 @@ 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 --index-url $TorchIndexUrl }+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch" { uv pip install --python $VenvPython "torch>=2.4,<2.11.0" torchvision torchaudio --default-index $TorchIndexUrl }if ($torchInstallExit -ne 0) {Write-Host "[ERROR] Failed to install PyTorch (exit code $torchInstallExit)" -ForegroundColor Redreturn (Exit-InstallFailure "Failed to install PyTorch (exit code $torchInstallExit)" $torchInstallExit)@@ -2306,7 +2318,7 @@ exit 0# keeps a stale torch==X+cpu against a CUDA index and setup.ps1 then loops on# "torch cpu != required cuXXX". Reinstall the right triplet when a GPU build is# expected: CUDA from $TorchIndexUrl, ROCm from $ROCmIndexUrl (repo.amd.com gfx*- # is a PEP 503 index uv resolves via --index-url, same URL the fresh ROCm install+ # is a PEP 503 index uv resolves via --default-index, same URL the fresh ROCm install# above uses). --no-torch / CPU-only hosts (expected cpu) are no-ops.if (-not $SkipTorch) {$expectedTorchTag = Get-ExpectedTorchFlavorTag -TorchIndexUrl $TorchIndexUrl -ROCmIndexUrl $ROCmIndexUrl@@ -2322,7 +2334,7 @@ exit 0$visionSpec = if ($ROCmGfxArch -and $torchvisionFloorMap.ContainsKey($ROCmGfxArch)) { $torchvisionFloorMap[$ROCmGfxArch] } else { "torchvision" }$audioSpec = if ($ROCmGfxArch -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 --index-url $ROCmIndexUrl $rocmSpec $visionSpec $audioSpec }+ $torchFixExit = Invoke-InstallCommand { uv pip install --python $VenvPython --force-reinstall --default-index $ROCmIndexUrl $rocmSpec $visionSpec $audioSpec }if ($torchFixExit -ne 0) {Write-Host "[ERROR] Failed to reinstall PyTorch with the correct ROCm build (exit code $torchFixExit)" -ForegroundColor Redreturn (Exit-InstallFailure "Failed to reinstall PyTorch (ROCm) (exit code $torchFixExit)" $torchFixExit)@@ -2331,7 +2343,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 --index-url $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 torchaudio --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 Redreturn (Exit-InstallFailure "Failed to reinstall PyTorch ($expectedTorchTag) (exit code $torchFixExit)" $torchFixExit)diff --git a/install.sh b/install.shindex 0acc9ec0be0..3f4ea923872 100755--- a/install.sh+++ b/install.sh@@ -159,6 +159,12 @@ run_maybe_quiet() {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.+ case " $* " in+ *" --default-index "*) set -- env -u UV_DEFAULT_INDEX -u UV_INDEX_URL -u UV_INDEX -u UV_EXTRA_INDEX_URL "$@" ;;+ esacif _is_verbose; then"$@" && return 0_rc=$?@@ -2190,9 +2196,9 @@ _expected_torch_flavor_tag() {esac}-# Whether index ($1) supports a plain --index-url reinstall. pytorch.org cuXXX /+# Whether index ($1) supports a plain --default-index reinstall. pytorch.org cuXXX /# rocmX.Y AND the repo.amd.com gfx* indexes are all PEP 503 simple indexes that uv-# resolves (torch + every transitive dep) via --index-url -- the same URLs the+# resolves (torch + every transitive dep) via --default-index -- the same URLs the# 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() {@@ -2744,7 +2750,7 @@ if [ "$_MIGRATED" = true ]; thensubstep "repairing ROCm torch (overwritten by dependency resolution)..."run_install_cmd_retry "repair ROCm torch" uv pip install --python "$_VENV_PY" \"$TORCH_CONSTRAINT" torchvision torchaudio \- --index-url "$TORCH_INDEX_URL" \+ --default-index "$TORCH_INDEX_URL" \--force-reinstallfi;;@@ -2870,7 +2876,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; thensubstep "[WARN] Radeon repo lacks a compatible wheel set for this Python; falling back to ROCm index ($TORCH_INDEX_URL)" "$C_WARN"run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" \"$TORCH_CONSTRAINT" torchvision torchaudio \- --index-url "$TORCH_INDEX_URL"+ --default-index "$TORCH_INDEX_URL"elsesubstep "installing PyTorch from Radeon repo (${_RADEON_BASE_URL})..."# Pass explicit wheel URLs so the matched trio is@@ -2893,18 +2899,18 @@ elif [ -n "$TORCH_INDEX_URL" ]; thensubstep "[WARN] Radeon repo unavailable; falling back to ROCm index ($TORCH_INDEX_URL)" "$C_WARN"run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" \"$TORCH_CONSTRAINT" torchvision torchaudio \- --index-url "$TORCH_INDEX_URL"+ --default-index "$TORCH_INDEX_URL"fielsesubstep "[WARN] Radeon GPU detected but could not detect full ROCm version; falling back to ROCm index" "$C_WARN"run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" \"$TORCH_CONSTRAINT" torchvision torchaudio \- --index-url "$TORCH_INDEX_URL"+ --default-index "$TORCH_INDEX_URL"fielsesubstep "installing PyTorch ($TORCH_INDEX_URL)..."run_install_cmd_retry "install PyTorch" uv pip install --python "$_VENV_PY" "$TORCH_CONSTRAINT" torchvision torchaudio \- --index-url "$TORCH_INDEX_URL"+ --default-index "$TORCH_INDEX_URL"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@@ -2964,7 +2970,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; thensubstep "repairing ROCm torch (overwritten by dependency resolution)..."run_install_cmd_retry "repair ROCm torch" uv pip install --python "$_VENV_PY" \"$TORCH_CONSTRAINT" torchvision torchaudio \- --index-url "$TORCH_INDEX_URL" \+ --default-index "$TORCH_INDEX_URL" \--force-reinstallfi;;@@ -2999,14 +3005,14 @@ if [ "$SKIP_TORCH" = false ] && [ -n "${TORCH_INDEX_URL:-}" ]; then_installed_torch_ver=$("$_VENV_PY" -c "import torch; print(torch.__version__)" 2>/dev/null || true)_installed_torch_tag=""[ -n "$_installed_torch_ver" ] && _installed_torch_tag=$(_torch_flavor_tag "$_installed_torch_ver")- # Repair when flavor is wrong AND the index is plain --index-url reinstallable+ # Repair when flavor is wrong AND the index is plain --default-index reinstallable# (cuXXX / rocmX.Y / repo.amd.com gfx*); an unknown mirror leaf -> warn only.if [ -n "$_installed_torch_tag" ] && [ "$_installed_torch_tag" != "$_expected_torch_tag" ] \&& [ "$(_torch_index_repairable "$TORCH_INDEX_URL")" = "yes" ]; thensubstep "PyTorch flavor mismatch (installed $_installed_torch_tag, need $_expected_torch_tag) -- reinstalling correct build..."run_install_cmd "reinstall PyTorch ($_expected_torch_tag)" uv pip install --python "$_VENV_PY" \"$TORCH_CONSTRAINT" torchvision torchaudio \- --index-url "$TORCH_INDEX_URL" \+ --default-index "$TORCH_INDEX_URL" \--reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio_installed_torch_ver=$("$_VENV_PY" -c "import torch; print(torch.__version__)" 2>/dev/null || true)_installed_torch_tag=""@@ -3017,7 +3023,7 @@ if [ "$SKIP_TORCH" = false ] && [ -n "${TORCH_INDEX_URL:-}" ]; thensubstep "[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 --index-url $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 torchaudio --default-index $TORCH_INDEX_URL --reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio" "$C_WARN"fififidiff --git a/studio/setup.ps1 b/studio/setup.ps1index 07dcb173354..db01a1ecad6 100644--- a/studio/setup.ps1+++ b/studio/setup.ps1@@ -2621,7 +2621,18 @@ function Fast-Install {param([Parameter(ValueFromRemainingArguments=$true)]$Args_)if ($UseUv) {$VenvPy = (Get-Command python).Source- $result = & uv pip install --python $VenvPy @Args_ 2>&1+ # 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+ }+ }+ 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 }}& python -m pip install @Args_ 2>&1diff --git a/tests/python/test_tokenizers_and_torch_constraint.py b/tests/python/test_tokenizers_and_torch_constraint.pyindex 7390d7be9bd..4322f0c7d6f 100644--- a/tests/python/test_tokenizers_and_torch_constraint.py+++ b/tests/python/test_tokenizers_and_torch_constraint.py@@ -14,6 +14,7 @@ _TESTS_DIR = pathlib.Path(__file__).resolve().parent.parent # tests/_REPO_ROOT = _TESTS_DIR.parent # unsloth/_INSTALL_SH = _REPO_ROOT / "install.sh"_INSTALL_PS1 = _REPO_ROOT / "install.ps1"+_SETUP_PS1 = _REPO_ROOT / "studio" / "setup.ps1"_NO_TORCH_RT = _REPO_ROOT / "studio" / "backend" / "requirements" / "no-torch-runtime.txt"@@ -109,6 +110,56 @@ class TestStructuralInstallPs1Unchanged:assert '"torch>=2.4,<2.11.0"' in self._ps1+class TestInstallPs1UvDefaultIndex:+ """Installer-managed torch indexes must override inherited uv defaults."""++ _ps1 = _read(_INSTALL_PS1)++ def test_torch_installs_use_default_index(self):+ assert "--default-index $TorchIndexUrl" in self._ps1+ assert "--default-index $ROCmIndexUrl" in self._ps1++ def test_torch_installs_do_not_use_deprecated_index_url(self):+ assert "--index-url $TorchIndexUrl" not in self._ps1+ assert "--index-url $ROCmIndexUrl" not in self._ps1++ def test_torch_installs_neutralize_all_uv_index_env_vars(self):+ # Extra-index vars outrank --default-index, so pinned installs must clear them.+ for var in ("UV_DEFAULT_INDEX", "UV_INDEX_URL", "UV_INDEX", "UV_EXTRA_INDEX_URL"):+ assert var in self._ps1+ assert 'Remove-Item "Env:$n"' in self._ps1+++class TestSetupPs1FastInstallIndex:+ """setup.ps1 Fast-Install must neutralize inherited uv indexes when pinning."""++ _ps1 = _read(_SETUP_PS1)++ def test_fast_install_clears_all_uv_index_env_vars(self):+ for var in ("UV_DEFAULT_INDEX", "UV_INDEX_URL", "UV_INDEX", "UV_EXTRA_INDEX_URL"):+ assert var in self._ps1+ # Must truly remove the vars (child sees no value), not set them empty.+ assert 'Remove-Item "Env:$n"' in self._ps1+++class TestInstallShUvDefaultIndex:+ """Linux/Mac installer torch indexes must override inherited uv defaults."""++ _sh = _read(_INSTALL_SH)++ def test_torch_installs_use_default_index(self):+ assert '--default-index "$TORCH_INDEX_URL"' in self._sh++ def test_torch_installs_do_not_use_deprecated_index_url(self):+ assert '--index-url "$TORCH_INDEX_URL"' not in self._sh++ def test_torch_installs_neutralize_all_uv_index_env_vars(self):+ # --default-index installs run with all uv index env vars unset via `env -u`.+ assert (+ "env -u UV_DEFAULT_INDEX -u UV_INDEX_URL -u UV_INDEX -u UV_EXTRA_INDEX_URL" in self._sh+ )++# Group 2 -- Shell snippet tests (bash subprocess, mocked python)class TestTorchConstraintShell:"""Test the TORCH_CONSTRAINT block via bash with mocked python minor versions."""diff --git a/tests/sh/test_mac_intel_compat.sh b/tests/sh/test_mac_intel_compat.shindex 3c3bbfaa5f7..8a0ff4b641e 100644--- a/tests/sh/test_mac_intel_compat.sh+++ b/tests/sh/test_mac_intel_compat.sh@@ -312,7 +312,7 @@ if [ "$SKIP_TORCH" = true ]; thenelseecho "==> Installing PyTorch ($TORCH_INDEX_URL)..."uv pip install --python "$_VENV_PY" "torch>=2.4,<2.11.0" torchvision torchaudio \- --index-url "$TORCH_INDEX_URL"+ --default-index "$TORCH_INDEX_URL"fiTORCH_EOF