Skip to content

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.

Windows torch release preservation and a torch 2.11 default across installers (#7256)

Daniel Han617975fbe183
diff --git a/install.ps1 b/install.ps1
index e5e3495c053..d68328456a5 100644
--- a/install.ps1
+++ b/install.ps1
@@ -312,6 +312,8 @@ function Install-UnslothStudio {
)
if ($Code -eq 0) { $Code = 1 }
Write-TauriLog "ERROR_DEFAULT" $Message
+ # Under `irm | iex` the session survives a failure; a leaked handoff would re-pin it.
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
if (Get-Command Restore-StudioVenvRollback -CommandType Function -ErrorAction SilentlyContinue) {
Restore-StudioVenvRollback
}
@@ -683,8 +685,6 @@ function Install-UnslothStudio {
if ($env:UNSLOTH_NO_TORCH -in @('1', 'true', 'yes', 'on')) { $SkipTorch = $true }
if ($env:UNSLOTH_SKIP_AUTOSTART -in @('1', 'true', 'yes', 'on')) { $SkipAutostart = $true }
- # Propagate to child processes so they also respect verbose mode.
- # Process-scoped -- does not persist.
if ($script:UnslothVerbose) {
$env:UNSLOTH_VERBOSE = '1'
}
@@ -705,9 +705,7 @@ function Install-UnslothStudio {
# UNSLOTH_PYTHON pins the version (mirrors install.sh --python); default 3.13.
$PythonVersion = if ($env:UNSLOTH_PYTHON) { $env:UNSLOTH_PYTHON } else { "3.13" }
- # python.org fallback patch, used only when winget is unavailable/broken AND
- # the live python.org listing can't be fetched. The installer URL scheme is
- # stable so an older patch still installs. Bump alongside $PythonVersion.
+ # python.org fallback patch when winget and the live listing fail; bump with $PythonVersion.
$PythonFallbackFullVersion = "3.13.13"
# Patch releases the stack cannot run; mirrors PYTHON_SKIP in install.sh.
# Windows resolves an installed interpreter and hands uv its path rather
@@ -720,10 +718,7 @@ function Install-UnslothStudio {
# able to complete, over a package it will not install.
if ($SkipTorch) { $PythonSkip = @() }
- # Resolve install destinations. Priority: UNSLOTH_STUDIO_HOME, then
- # STUDIO_HOME alias, then USERPROFILE-redirect, then default.
- # Reject whitespace-only values so " " is treated as unset (matches the
- # Python resolvers' .strip()), preventing install/runtime layout drift.
+ # Whitespace-only == unset (matches the Python resolvers' .strip()).
$envOverrideVar = $null
$envOverride = $null
if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) {
@@ -1180,8 +1175,7 @@ public static class UnslothStudioFinalPathV2
}
- # LOCALAPPDATA may be unset in service / CI contexts; Join-Path would abort
- # under ErrorActionPreference=Stop without this guard.
+ # LOCALAPPDATA may be unset in service / CI contexts; guard Join-Path under Stop.
$defaultDataDir = if ($env:LOCALAPPDATA -and -not [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) {
Join-Path $env:LOCALAPPDATA "Unsloth Studio"
} else { $null }
@@ -1192,8 +1186,7 @@ public static class UnslothStudioFinalPathV2
$envOverride = (Join-Path $env:USERPROFILE $envOverride.Substring(1).TrimStart('/','\'))
}
try {
- # .NET API: New-Item -Path treats brackets as wildcards and has no
- # -LiteralPath in PS 5.1, so a root like C:\studio[abc] would fail.
+ # .NET API: New-Item -Path treats brackets as wildcards (no -LiteralPath on PS 5.1).
[System.IO.Directory]::CreateDirectory($envOverride) | Out-Null
$StudioHome = (Resolve-Path -LiteralPath $envOverride).Path
} catch {
@@ -1204,8 +1197,7 @@ public static class UnslothStudioFinalPathV2
}
$probe = Join-Path $StudioHome (".unsloth-write-probe-" + [guid]::NewGuid())
try {
- # WriteAllText: literal-path safe + closes handle so Remove-Item works.
- [System.IO.File]::WriteAllText($probe, "")
+ [System.IO.File]::WriteAllText($probe, "") # literal-path safe + closes handle
Remove-Item -LiteralPath $probe -Force -ErrorAction SilentlyContinue
} catch {
Write-StudioLine "ERROR: $envOverrideVar=$StudioHome is not writable." -ForegroundColor Red
@@ -1311,8 +1303,7 @@ public static class UnslothStudioFinalPathV2
}
# ── Helper: safely add a directory to the persistent User PATH ──
- # Direct registry access preserves REG_EXPAND_SZ (avoids dotnet/runtime#1442).
- # Append (default) keeps existing tools first; Prepend for must-win entries.
+ # Direct registry access preserves REG_EXPAND_SZ (dotnet/runtime#1442).
function Add-ToUserPath {
param(
[Parameter(Mandatory = $true)][string]$Directory,
@@ -1642,20 +1633,15 @@ public static class UnslothStudioFinalPathV2
if (-not $Text) { return $Text }
$Text = $Text -replace '(https?://)[^/@\s`]+@', '$1<redacted>@'
$Text = $Text -replace '([?&][^=\s&`]+)=[^&#\s`]+', '$1=<redacted>'
- # A #token=... fragment is as sensitive as a query; URL-anchored.
return $Text -replace '(https?://[^\s`#]+)#[^\s`]+', '$1#<redacted>'
}
- # 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,
[string]$Label = "install command"
)
- # 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).
+ # A pinned index must beat an inherited uv mirror (#6898); UV_NO_CONFIG=1 blocks uv.toml.
$savedUvIndex = $null
if ($Command.ToString() -match '--default-index') {
$savedUvIndex = @{}
@@ -1711,15 +1697,11 @@ public static class UnslothStudioFinalPathV2
}
}
- # Retry Invoke-InstallCommand on transient uv download failures with backoff.
- # Returns the last exit code on permanent failure so rollback still fires.
function Invoke-InstallCommandRetry {
param(
[Parameter(Mandatory = $true, Position = 0)][ScriptBlock]$Command,
[string]$Label = "install step"
)
- # Sanitize overrides to a default of 3 (a typo must not disable retries; =1 disables).
- # TryParse with bounds avoids an Int32 overflow throw. Bounds: 1..100 retries, 0..3600s.
$maxAttempts = 3
$parsedAttempts = 0
if ([int]::TryParse($env:UNSLOTH_INSTALL_RETRIES, [ref]$parsedAttempts) -and $parsedAttempts -ge 1 -and $parsedAttempts -le 100) {
@@ -2126,15 +2108,7 @@ public static class UnslothStudioFinalPathV2
[System.IO.Directory]::CreateDirectory($appDir) | Out-Null
}
- # Same-install discriminator: per-install opaque id written once at
- # install time and read by both this launcher and the backend
- # (/api/health). Replaces the older sha256(resolved $StudioHome)
- # scheme to (a) avoid leaking the install path on -H 0.0.0.0
- # deployments and (b) sidestep launcher/backend canonicalization
- # drift (Resolve-Path vs Path.resolve() junction handling). Lives
- # at $StudioHome\share\ (not $appDir) so the backend can find it
- # via _STUDIO_ROOT_RESOLVED / "share" / "studio_install_id"
- # regardless of mode. 32 bytes of crypto random -> 64 hex chars.
+ # Per-install opaque id for launcher and backend, so neither compares install paths.
$_studioIdDir = Join-Path $StudioHome "share"
if (-not (Test-Path -LiteralPath $_studioIdDir)) {
[System.IO.Directory]::CreateDirectory($_studioIdDir) | Out-Null
@@ -2183,11 +2157,7 @@ public static class UnslothStudioFinalPathV2
}
}
- # Env-mode: persist UNSLOTH_STUDIO_HOME (and llama path) so fresh
- # shells don't need to re-export, and bake per-install $portFile /
- # $mutexName so concurrent custom-root launchers cannot serialize
- # through one global mutex on 8888..8908. Default installs get an
- # empty prefix to match pre-PR behavior.
+ # Bake per-install $portFile / $mutexName so custom-root launchers do not share a mutex.
$studioHomeExport = if ($StudioRedirectMode -eq 'env') {
# Reuse the preflight's managed path for legacy-home overrides.
$_llamaPath = Get-ManagedLlamaCppDir
@@ -2455,20 +2425,13 @@ exit 0
# shortcuts instead point straight at powershell.exe running
# launch-studio.ps1 with a hidden window (selected below).
- # Delete any launch-studio.vbs left by a pre-hardening install. New
- # installs no longer generate it, but an upgrade that merely stopped
- # generating it would leave the exact file AV flags on disk, so remove
- # it explicitly. Covers default and env-mode installs (same $appDir).
+ # Drop any launch-studio.vbs left by a pre-hardening install (AV-flagged shape).
$legacyLauncherVbs = Join-Path $appDir "launch-studio.vbs"
if (Test-Path -LiteralPath $legacyLauncherVbs) {
Remove-Item -LiteralPath $legacyLauncherVbs -Force -ErrorAction SilentlyContinue
}
- # Prefer bundled icon from local clone/dev installs.
- # If not available, best-effort download from raw GitHub.
- # We only attach the icon if the resulting file has a valid ICO header.
- # Snapshot the existing icon first so we can tell whether it actually
- # changed and gate the heavier icon-cache refresh on a real change.
+ # Attach only on a valid ICO header; snapshot the icon so the refresh runs on a change.
$preIconHash = $null
if (Test-Path -LiteralPath $iconPath) {
try { $preIconHash = (Get-FileHash -LiteralPath $iconPath -Algorithm SHA256).Hash } catch {}
@@ -2508,9 +2471,6 @@ exit 0
}
}
- # Did the icon content actually change vs the previous install?
- # Only a real change (or a first/removed icon) should trigger the heavy
- # refresh; a no-op reinstall with no icon at all must not.
$iconChanged = $false
if ($hasValidIcon) {
if (-not $preIconHash) {
@@ -2526,17 +2486,13 @@ exit 0
$iconChanged = $true
}
- # Env-mode: skip persistent Desktop / Start Menu .lnk shortcuts
- # that may point at a deleted workspace; launcher + icon stay.
+ # Env-mode: skip .lnk shortcuts (may point at a deleted workspace); launcher stays.
if ($StudioRedirectMode -eq 'env') {
substep "wrote launcher at $launcherPs1 (persistent shortcuts skipped in env-override mode)"
return
}
- # Whether this is effectively a first install (no pre-existing .lnk).
- # Used to gate the heavier icon-cache refresh below so a no-op reinstall
- # does not repeatedly clear caches / restart StartMenuExperienceHost --
- # a behavioral cluster AV heuristics can score as dropper-like.
+ # Gates the heavy refresh: clearing caches on a no-op reinstall looks like a dropper.
$firstInstall = -not (
($desktopLink -and (Test-Path -LiteralPath $desktopLink)) -or
($startMenuLink -and (Test-Path -LiteralPath $startMenuLink))
@@ -2610,13 +2566,7 @@ exit 0
}
if ($createdShortcutCount -gt 0) {
substep "Created Unsloth Studio shortcut"
- # Always do the cheap, non-disruptive per-item refresh so a
- # rewritten same-name .lnk renders with its new target/icon
- # immediately (a same-name .lnk recreated across reinstalls keeps
- # Explorer's cached per-item icon). The reliable fix (no explorer
- # restart) is a per-item SHChangeNotify SHCNE_UPDATEITEM +
- # SHCNF_PATHW per .lnk; the global SHCNE_ASSOCCHANGED broadcast
- # alone does NOT recover a stale item.
+ # Per-item SHChangeNotify: the global broadcast misses a rewritten same-name .lnk.
try {
Add-Type -Namespace UnslothShell -Name IconRefresh -MemberDefinition '[System.Runtime.InteropServices.DllImport("shell32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] public static extern void SHChangeNotify(int eventId, uint flags, string item1, System.IntPtr item2);' -ErrorAction SilentlyContinue
# SHCNE_UPDATEITEM (0x00002000) + SHCNF_PATHW (0x0005) per shortcut
@@ -2626,21 +2576,10 @@ exit 0
# SHCNE_ASSOCCHANGED (0x08000000) global refresh (belt-and-suspenders)
[UnslothShell.IconRefresh]::SHChangeNotify(0x08000000, 0, $null, [System.IntPtr]::Zero)
} catch {}
- # Heavier on-disk icon-cache clear + StartMenuExperienceHost tile
- # rebuild only when the icon actually changed or this is a first
- # install. Running "clear icon cache + kill StartMenuExperienceHost"
- # on every no-op reinstall is a dropper-like behavioral cluster and
- # is unnecessary when the icon is unchanged (the per-item notify
- # above already refreshes the rewritten shortcut).
if ($firstInstall -or $iconChanged) {
try { & "$env:SystemRoot\System32\ie4uinit.exe" -ClearIconCache 2>$null } catch {}
try { & "$env:SystemRoot\System32\ie4uinit.exe" -show 2>$null } catch {}
- # Win11's Start Menu (StartMenuExperienceHost) keeps its OWN
- # pre-rendered tile-icon cache that ie4uinit/explorer restart do NOT
- # invalidate, so a rewritten same-name shortcut shows the old tile
- # until the host restarts. Drop only the render caches (NEVER
- # start2.bin -- the pinned layout) and let the host rebuild.
- # Best-effort; Win10 has no such host (Test-Path skips it).
+ # Win11 caches tile icons past ie4uinit; never drop start2.bin (the layout).
try {
$smehTemp = Join-Path $env:LOCALAPPDATA "Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\TempState"
if (Test-Path -LiteralPath $smehTemp) {
@@ -3200,19 +3139,7 @@ exit 0
substep "Get it from https://aka.ms/getwinget if Python / uv are not already on PATH." "Yellow"
}
- # ── Helper: detect a working Python 3.11-3.13 on the system ──
- # Returns the version string (e.g. "3.13") or "" if none found.
- # Uses try-catch + stderr redirection so that App Execution Alias stubs
- # (WindowsApps) and other non-functional executables are probed safely
- # without triggering $ErrorActionPreference = "Stop".
- #
- # Skips Anaconda/Miniconda Python: conda-bundled CPython ships modified
- # DLL search paths that break torch's c10.dll loading on Windows.
- # Standalone CPython (python.org, winget, uv) does not have this issue.
- #
- # NOTE: A venv created from conda Python inherits conda's base_prefix
- # even if the venv path does not contain "conda". We check both the
- # executable path AND sys.base_prefix to catch this.
+ # ── Detect Python 3.11-3.13, skipping conda and venvs whose base_prefix is conda. ──
$script:CondaSkipPattern = '(?i)(conda|miniconda|anaconda|miniforge|mambaforge)'
function Test-IsCondaPython {
@@ -3255,9 +3182,7 @@ exit 0
# py.exe resolves to the standard CPython install, not conda.
# Prefer the requested $PythonVersion, then newest-first fallback.
$minors = @($PythonVersion) + (@("3.13", "3.12", "3.11") | Where-Object { $_ -ne $PythonVersion })
- # Enumerate every py.exe on PATH with -All (Windows PowerShell 5.1
- # returns only the first launcher without it) and search each for a
- # supported, non-conda interpreter.
+ # -All: Windows PowerShell 5.1 returns only the first launcher without it.
foreach ($pyLauncher in @(Get-Command py -All -CommandType Application -ErrorAction SilentlyContinue)) {
if ($pyLauncher.Source -match $script:CondaSkipPattern) { continue }
foreach ($minor in $minors) {
@@ -3281,13 +3206,7 @@ exit 0
} catch {}
}
}
- # Try python3 / python via Get-Command -All to look past stubs that
- # might shadow a real Python further down PATH.
- # Skip WindowsApps entries: the App Execution Alias stubs live there
- # and can open the Microsoft Store as a side effect. Legitimate Store
- # Python is already detected via the py launcher above (Store packages
- # include py since Python 3.11).
- # Skip Anaconda/Miniconda: check both path and sys.base_prefix.
+ # -All looks past shadowing stubs. Skip WindowsApps (alias stubs) and conda.
foreach ($name in @("python3", "python")) {
foreach ($cmd in @(Get-Command $name -All -ErrorAction SilentlyContinue)) {
if (-not $cmd.Source) { continue }
@@ -3357,12 +3276,7 @@ exit 0
return $null
}
- # ── Fallback: install CPython directly from python.org ──
- # Used when winget is unavailable or fails (notably msstore cert-pinning error
- # 0x8a15005e, which aborts `winget install` unless --source winget is given).
- # Downloads the official installer and runs it silently as a per-user install
- # (no UAC), putting python.exe + the py launcher on PATH. Mirrors the uv ->
- # astral.sh fallback below. Returns @{ Version; Path } or $null.
+ # ── Fallback when winget fails (msstore 0x8a15005e): silent per-user python.org, no UAC. ──
function Install-PythonFromPythonOrg {
# $Arch overrides the host arch, to pull x64 onto an ARM64 box.
param([string]$Arch = "")
@@ -3379,10 +3293,7 @@ exit 0
return $null
}
- # Resolve the latest $PythonVersion.x patch from the python.org listing,
- # falling back to a same-minor version if the listing cannot be fetched.
- # Use the pinned full version only when it matches the requested minor so a
- # non-default UNSLOTH_PYTHON (e.g. 3.12) doesn't silently install 3.13.
+ # The pinned full version applies only to a matching minor, so 3.12 cannot install 3.13.
$full = if ($PythonFallbackFullVersion -like "$PythonVersion.*") { $PythonFallbackFullVersion } else { "$PythonVersion.0" }
try {
$listing = [string](Invoke-RestMethod -Uri "https://www.python.org/ftp/python/" -UseBasicParsing -TimeoutSec 20)
@@ -3428,8 +3339,7 @@ exit 0
"InstallAllUsers=0",
"PrependPath=1",
"Include_launcher=1",
- # Launcher per-user too: Include_launcher defaults InstallLauncherAllUsers=1,
- # which needs admin and would break this non-admin per-user fallback.
+ # Launcher per-user too: Include_launcher defaults to all-users, which needs admin.
"InstallLauncherAllUsers=0",
"Include_pip=1",
"AssociateFiles=0",
@@ -3493,7 +3403,6 @@ exit 0
}
# ── Install Python if no compatible version (3.11-3.13) found ──
- # Find-CompatiblePython returns @{ Version = "3.13"; Path = "C:\...\python.exe" } or $null.
Write-TauriLog "STEP" "Installing Python"
$DetectedPython = Remove-SkippedPython (Find-CompatiblePython)
@@ -3506,13 +3415,7 @@ exit 0
$wingetExit = $null
if ($script:WingetAvailable) {
- # --source winget avoids the msstore source, which can fail with
- # cert-pinning error 0x8a15005e and abort the whole `winget install`
- # (winget then demands --source). Python and uv both live in the
- # winget source, so pinning it is correct and faster.
- #
- # Lower ErrorActionPreference so winget stderr (progress/warnings) is
- # not a terminating error on PS 5.1 (native stderr is ErrorRecord).
+ # --source winget avoids msstore, whose cert-pinning (0x8a15005e) can abort the install.
$prevEAP = $ErrorActionPreference
$ErrorActionPreference = "Continue"
try {
@@ -3526,10 +3429,7 @@ exit 0
$DetectedPython = Remove-SkippedPython (Find-CompatiblePython)
if (-not $DetectedPython) {
- # Python still not functional after winget -- force reinstall.
- # This handles both real failures AND "already installed" codes where
- # winget thinks Python is present but it's not actually on PATH
- # (e.g. user partially uninstalled, or installed via a different method).
+ # Covers real failures and "already installed" codes where Python is not on PATH.
substep "Python not found on PATH after winget. Retrying with --force..." "Yellow"
$ErrorActionPreference = "Continue"
try {
@@ -3542,9 +3442,6 @@ exit 0
}
}
- # Fall back to python.org if winget is unavailable OR couldn't install a
- # working Python (missing/broken winget, msstore cert errors --source
- # winget can't fix). Keeps the install automatic instead of failing out.
if (-not $DetectedPython) {
if ($script:WingetAvailable) {
substep "winget could not install Python -- falling back to python.org..." "Yellow"
@@ -3893,8 +3790,7 @@ exit 0
}
}
- # A freshly installed uv can sit later on PATH than an older one (active
- # venv, Scoop/pipx shim). Prefer a just-installed uv from a known location.
+ # A freshly installed uv can sit behind an older one on PATH; prefer known install locations.
if (-not (Test-UvVersionOk)) {
$origPath = $env:PATH
foreach ($d in @($script:UvInstallDestDir, $env:UV_INSTALL_DIR, $env:XDG_BIN_HOME,
@@ -3914,14 +3810,11 @@ exit 0
return (Exit-InstallFailure "uv could not be installed")
}
- # When bytecode compilation is enabled, large installs can exceed uv's 60s
- # default on slow machines. Default to 180s, preserving overrides ("0" disables).
+ # Bytecode compilation can exceed uv's 60s default on slow machines ("0" disables).
if (-not $env:UV_COMPILE_BYTECODE_TIMEOUT) {
$env:UV_COMPILE_BYTECODE_TIMEOUT = "180"
}
- # uv >= 0.8.16 retries HTTP/2 streaming body errors; raise retries and read
- # timeout for large wheel downloads. User-provided values are preserved.
if (-not $env:UV_HTTP_RETRIES) {
$env:UV_HTTP_RETRIES = "5"
}
@@ -3929,9 +3822,7 @@ exit 0
$env:UV_HTTP_TIMEOUT = "180"
}
- # ── Create venv (migrate old layout if possible, otherwise fresh) ──
- # Pass the resolved executable path to uv so it does not re-resolve
- # a version string back to a conda interpreter.
+ # ── Create the venv; hand uv the resolved exe path so it does not re-resolve back to conda. ──
Write-TauriLog "STEP" "Creating virtual environment"
if (-not (Test-Path -LiteralPath $StudioHome)) {
# .NET API: New-Item -Path treats brackets as wildcards.
@@ -3944,6 +3835,9 @@ exit 0
$script:StudioVenvRollbackTarget = $VenvDir
$script:StudioVenvRollbackActive = $false
$script:StudioVenvRollbackPartial = $false
+ # Reset per run: under `irm | iex` the script scope IS the caller's session.
+ $script:PrevTorchVer = ""
+ $script:PrevTorchPin = $null
function Test-VenvPythonReady {
param([Parameter(Mandatory = $true)][string]$PythonExe)
@@ -4256,6 +4150,35 @@ exit 0
}
}
+ # Bounded probe (async-drained, 30s) so a wedged "import torch" cannot stall the installer.
+ function Get-InstalledTorchVersionRaw {
+ param([string]$PythonExe)
+ if (-not $PythonExe -or -not (Test-Path -LiteralPath $PythonExe)) { return $null }
+ try {
+ $psi = New-Object System.Diagnostics.ProcessStartInfo
+ $psi.FileName = $PythonExe
+ # Dist metadata, not "import torch": a broken DLL would drop the pin (as in install.sh).
+ $psi.Arguments = '-c "import importlib.metadata as m; print(m.version(''torch''))"'
+ $psi.RedirectStandardOutput = $true
+ $psi.RedirectStandardError = $true
+ $psi.UseShellExecute = $false
+ $psi.CreateNoWindow = $true
+ $proc = [System.Diagnostics.Process]::Start($psi)
+ # Drain BOTH streams async before WaitForExit, or a wedged import deadlocks.
+ $outTask = $proc.StandardOutput.ReadToEndAsync()
+ $errTask = $proc.StandardError.ReadToEndAsync()
+ $finished = $proc.WaitForExit(30000)
+ if (-not $finished) { try { $proc.Kill() } catch {}; return $null }
+ $out = $outTask.GetAwaiter().GetResult()
+ [void]$errTask.GetAwaiter().GetResult()
+ if ($proc.ExitCode -ne 0) { return $null }
+ # Last non-empty line only, so stdout noise cannot corrupt the pin.
+ $lines = @($out -split "`r?`n" | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" })
+ if ($lines.Count -eq 0) { return $null }
+ return $lines[-1]
+ } catch { return $null }
+ }
+
$studioVenvReplacementCommitted = $false
try {
# Replace occupied venvs even when python.exe is missing, as in #9479.
@@ -4279,6 +4202,10 @@ exit 0
Write-StudioLine " Move it aside or choose an empty UNSLOTH_STUDIO_HOME." -ForegroundColor Yellow
throw "Refusing to delete non-Unsloth venv at $VenvDir"
}
+ # Record the release before the rollback move; only the new-layout replace needs it.
+ if (-not $SkipTorch) {
+ $script:PrevTorchVer = Get-InstalledTorchVersionRaw -PythonExe $VenvPython
+ }
# New layout already exists -- replace only after preserving rollback copy.
substep "preserving existing environment for rollback..."
try {
@@ -4383,15 +4310,11 @@ exit 0
[Parameter(Position = 1)][string[]]$SmiArgs = @(),
[int]$TimeoutSec = 30
)
- # RunAsInvoker blocks the auto-elevation/UAC prompt; the timeout bounds a
- # flaky amd-smi that can otherwise spin for minutes (30s mirrors amd.py).
+ # RunAsInvoker blocks auto-elevation; the timeout bounds a flaky amd-smi (30s, as amd.py).
$prevCompat = [Environment]::GetEnvironmentVariable('__COMPAT_LAYER', 'Process')
$env:__COMPAT_LAYER = 'RunAsInvoker'
try {
- # [Process]::Start, NOT Start-Process -PassThru: the latter leaves
- # .ExitCode $null after WaitForExit on PS 5.1, so $LASTEXITCODE (checked
- # by callers) reads non-zero and kills detection. Async reads drain the
- # pipes (no deadlock); amd-smi args have no spaces so a plain join is safe.
+ # [Process]::Start, not Start-Process: PS 5.1 leaves .ExitCode $null and detection dies.
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $Exe
$psi.Arguments = ($SmiArgs -join ' ')
@@ -4421,11 +4344,7 @@ exit 0
}
}
- # ── Helper: run nvidia-smi under a timeout ──
- # A wedged NVIDIA driver can make nvidia-smi block during init or after a
- # reset; WaitForExit bounds it (mirrors Invoke-AmdSmiNoElevate) so detection
- # cannot hang the installer. No RunAsInvoker compat layer: nvidia-smi does
- # not auto-elevate. Returns combined stdout+stderr; "" on timeout/failure.
+ # ── Bound nvidia-smi so a wedged driver cannot hang the installer. ──
function Invoke-NvidiaSmiBounded {
param(
[Parameter(Mandatory = $true, Position = 0)][string]$Exe,
@@ -4460,10 +4379,7 @@ exit 0
}
}
- # ── Helper: nvidia-smi -L lists at least one real GPU ──
- # Exit code 0 alone is not enough: a stale/driverless nvidia-smi can exit 0
- # while listing no GPU, which would mark an AMD host NVIDIA and suppress
- # ROCm detection. Require a "GPU <n>:" data row.
+ # ── A driverless nvidia-smi exits 0 listing no GPU, so require a "GPU <n>:" row. ──
function Test-NvidiaSmiHasGpu {
param([Parameter(Mandatory = $true)][string]$Exe)
$out = Invoke-NvidiaSmiBounded $Exe @('-L')
@@ -4501,15 +4417,10 @@ exit 0
# it are outside that gate, so an NVIDIA host would leave it undefined.
$ROCmUnsupportedGfxArch = $null
if (-not $HasNvidiaSmi) {
- # hipinfo: PATH first, then HIP_PATH/ROCM_PATH bin fallback (mirrors NVIDIA smi path resolution).
- # AMD HIP SDK sets HIP_PATH but may not add the bin dir to PATH depending on install type.
- # Ignore the venv hipInfo.exe (AMD wheel, on PATH): not a HIP SDK, so
- # amd-smi would still auto-elevate. Cf. _path_inside_venv().
+ # Ignore the venv hipInfo.exe: an AMD wheel, not a HIP SDK, so amd-smi still auto-elevates.
function Test-HipinfoIsVenvInternal {
param([AllowNull()][string]$HipinfoPath)
if ([string]::IsNullOrWhiteSpace($HipinfoPath)) { return $false }
- # Also derive the venv from the setup python + default Unsloth home, so
- # the venv hipInfo is caught when VenvDir/VIRTUAL_ENV are unset.
$venvRoots = @()
if ($env:VIRTUAL_ENV) { $venvRoots += $env:VIRTUAL_ENV }
$vd = Get-Variable -Name VenvDir -ValueOnly -ErrorAction SilentlyContinue
@@ -4518,15 +4429,11 @@ exit 0
try { $venvRoots += (Split-Path -Parent (Split-Path -Parent $env:UNSLOTH_SETUP_PYTHON)) } catch {}
}
if ($env:USERPROFILE) { $venvRoots += (Join-Path $env:USERPROFILE ".unsloth\studio\unsloth_studio") }
- # A custom Unsloth home (UNSLOTH_STUDIO_HOME / STUDIO_HOME alias) moves the
- # venv off the default path; seed it too or its hipInfo escapes the filter.
$studioHomeEnv = if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) { $env:UNSLOTH_STUDIO_HOME.Trim() } elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) { $env:STUDIO_HOME.Trim() } else { $null }
if ($studioHomeEnv) {
- # Expand a leading ~ like the canonical resolver; else GetFullPath
- # keeps the literal ~ (cwd-relative) and the hipInfo escapes the filter.
+ # Expand a leading ~ like the canonical resolver; GetFullPath keeps it literal.
if (($studioHomeEnv -eq "~" -or $studioHomeEnv -like "~/*" -or $studioHomeEnv -like "~\*") -and -not [string]::IsNullOrWhiteSpace($env:USERPROFILE)) {
- # A bare "~" leaves an empty child path; Join-Path rejects that on
- # PS 5.1, so use USERPROFILE directly and only join a real remainder.
+ # A bare "~" leaves an empty child path, which Join-Path rejects on PS 5.1.
$studioHomeRest = $studioHomeEnv.Substring(1).TrimStart('/', '\')
$studioHomeEnv = if ($studioHomeRest) { Join-Path $env:USERPROFILE $studioHomeRest } else { $env:USERPROFILE }
}
@@ -4536,8 +4443,7 @@ exit 0
foreach ($root in $venvRoots) {
if ([string]::IsNullOrWhiteSpace($root)) { continue }
try { $r = [System.IO.Path]::GetFullPath($root).TrimEnd('\', '/') } catch { continue }
- # Skip a bare drive root (e.g. a non-venv UNSLOTH_SETUP_PYTHON like
- # C:\Python311\python.exe yields C:) -- it would match every path on that drive.
+ # Skip a bare drive root; it would match every path on that drive.
if ($r -match '^[a-zA-Z]:$') { continue }
if ($hip.Equals($r, [System.StringComparison]::OrdinalIgnoreCase) -or
$hip.StartsWith($r + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) {
@@ -4546,15 +4452,11 @@ exit 0
}
return $false
}
- # Scan all hipinfo and keep the first non-venv one (the venv copy from the
- # bnb fix could shadow a real HIP SDK's). -CommandType Application matches
- # only real executables, not a user alias/function named hipinfo.
+ # -CommandType Application skips an alias or function named hipinfo.
$hipinfoExe = Get-Command hipinfo -CommandType Application -All -ErrorAction SilentlyContinue |
Where-Object { -not (Test-HipinfoIsVenvInternal $_.Source) } |
Select-Object -First 1
if (-not $hipinfoExe) {
- # Iterate the env roots (mirrors the Python list) and take the first non-venv
- # bin\hipinfo.exe, so a venv-internal HIP_PATH can't mask a real SDK in ROCM_PATH.
$hipMissingLabel = $null; $hipMissingRoot = $null; $hipMissingCandidate = $null
foreach ($hipEnvLabel in @("HIP_PATH", "HIP_PATH_57", "ROCM_PATH")) {
$hipRoot = [Environment]::GetEnvironmentVariable($hipEnvLabel)
@@ -4607,14 +4509,7 @@ exit 0
}
} catch {}
}
- # On hosts without a working HIP runtime amd-smi elevates a child at runtime,
- # popping a UAC/DiskPart prompt RunAsInvoker can't suppress (manifest is
- # asInvoker). So only probe when a HIP SDK is present (hipinfo found ->
- # un-elevated) or the user opts in; else fall through to WMI name inference
- # (enough to pick ROCm wheels + the ROCm llama.cpp prebuilt).
- # An explicit opt-out (UNSLOTH_ENABLE_AMD_SMI=0/false/no/off) wins over the
- # HIP-SDK heuristic: a HIP SDK binary with a broken runtime can still pop the
- # prompt, so $HipSdkInstalled must NOT silently re-enable it.
+ # amd-smi pops an unsuppressable UAC prompt without HIP, so probe only with a SDK or opt-in.
$amdSmiOptOut = $env:UNSLOTH_ENABLE_AMD_SMI -match '^(?i)(0|false|no|off)$'
$amdSmiAllowed = (-not $amdSmiOptOut) -and ($HipSdkInstalled -or ($env:UNSLOTH_ENABLE_AMD_SMI -match '^(?i)(1|true|yes|on)$'))
if (-not $HasROCm -and $amdSmiAllowed) {
@@ -4624,8 +4519,7 @@ exit 0
$smiOut = Invoke-AmdSmiNoElevate $amdSmiExe.Source @('list')
if ($LASTEXITCODE -eq 0 -and $smiOut -match "(?im)^GPU\s*[:\[]\s*\d") {
$HasROCm = $true
- # Mirror the hipinfo path: collect all gfx tokens in enumeration
- # order and pick the runtime-visible one via HIP_VISIBLE_DEVICES.
+ # gfx tokens in enumeration order, picked via HIP_VISIBLE_DEVICES.
$_smiVisIdx = if ($env:HIP_VISIBLE_DEVICES -match '^\d') { [int]($env:HIP_VISIBLE_DEVICES -split ',')[0] } elseif ($env:ROCR_VISIBLE_DEVICES -match '^\d') { [int]($env:ROCR_VISIBLE_DEVICES -split ',')[0] } else { 0 }
# Attempt 1: newer amd-smi versions embed the gfx arch in list output.
$_smiGfxTokens = @([regex]::Matches($smiOut, "(?i)\b(gfx\d+[a-z]?)\b") | ForEach-Object { $_.Groups[1].Value.ToLower() })
@@ -4633,8 +4527,7 @@ exit 0
$ROCmGfxArch = if ($_smiVisIdx -lt $_smiGfxTokens.Count) { $_smiGfxTokens[$_smiVisIdx] } else { $_smiGfxTokens[0] }
$ROCmGpuLabel = "AMD ROCm ($ROCmGfxArch)"
} else {
- # Attempt 2: 'static --asic' exposes ASIC details on ROCm 6+,
- # including the GFX target needed for wheel index selection.
+ # Attempt 2: 'static --asic' exposes the GFX target on ROCm 6+.
$smiAsicOut = ""
try { $smiAsicOut = Invoke-AmdSmiNoElevate $amdSmiExe.Source @('static','--asic') } catch {}
$_asicGfxTokens = @([regex]::Matches($smiAsicOut, "(?i)\b(gfx\d+[a-z]?)\b") | ForEach-Object { $_.Groups[1].Value.ToLower() })
@@ -4714,9 +4607,6 @@ exit 0
$ROCmGpuLabel = "AMD ROCm ($ROCmGfxArch)"
substep "gfx arch from UNSLOTH_ROCM_GFX_ARCH env override: $ROCmGfxArch" "Cyan"
}
- # 2. Best-effort name → arch lookup from marketing name (amd-smi / WMI).
- # Targets only arches the ROCm prebuilts cover
- # (gfx120X/110X/1151/1150/103X); unknown names fall back to CPU.
elseif ($ROCmGpuLabel) {
$nameArchTable = @(
@{ P = "9070|9080|R9700"; A = "gfx1201" } # RDNA 4 (Navi 48: RX 9070 XT / 9070 GRE / 9070 / 9080, Radeon AI PRO R9700)
@@ -4782,9 +4672,7 @@ exit 0
}
}
}
- # Capture ROCm version for wheel selection (hipconfig, then amd-smi).
- # Run whenever the HIP SDK binary is present, not just when the device is accessible --
- # hipconfig --version works even when hipinfo reports no ROCm device (driver issue).
+ # Run whenever the HIP SDK is present: hipconfig --version works without a ROCm device.
if ($HasROCm -or $HipSdkInstalled) {
$hipConfigExe = Get-Command hipconfig -ErrorAction SilentlyContinue
if (-not $hipConfigExe) {
@@ -4824,12 +4712,7 @@ exit 0
}
}
- # ── Optional WSL-ROCm driver hint ────────────────────────────────────────
- # An AMD GPU can also be used inside WSL2, but only with Adrenalin >= 26.2.2
- # (first production ROCDXG/WSL release); native Windows GPU works with any
- # recent driver. We can't auto-install it (AMD referrer-gates downloads, no
- # winget package), so just point at AMD's page. Shown only when the installed
- # driver predates 26.2.2 (Feb 2026); suppress with UNSLOTH_SKIP_AMD_DRIVER_HINT=1.
+ # ── WSL2 needs AMD Adrenalin >= 26.2.2; AMD referrer-gates it, so only link their page. ──
function Show-AmdWslDriverHint {
if ($env:UNSLOTH_SKIP_AMD_DRIVER_HINT) { return }
try {
@@ -4846,15 +4729,12 @@ exit 0
$drvDate = [Management.ManagementDateTimeConverter]::ToDateTime([string]$amd.DriverDate)
}
} catch {}
- # Older than 26.2.2 (Feb 2026) => can't expose the GPU to WSL ROCm.
- # Unreadable date => still show the hint (informational, suppressible).
if ($drvDate -and $drvDate -ge (Get-Date '2026-02-01')) { return }
substep "Tip: to use this GPU inside WSL too, install AMD Adrenalin 26.2.2+ (for WSL2)." "Cyan"
substep " Your current driver predates it; native Windows GPU is unaffected. Get it from AMD:" "Cyan"
substep " https://www.amd.com/en/resources/support-articles/release-notes/RN-RAD-WIN-26-2-2.html" "Cyan"
substep " Then reboot and run this installer inside an Ubuntu-24.04 WSL distro." "Cyan"
- # If WSL isn't installed yet, point at the command that provisions it
- # (best-effort; wsl.exe absent => no WSL).
+ # wsl.exe absent means WSL is not installed; point at the command that provisions it.
$hasWsl = $false
try { $hasWsl = [bool](Get-Command wsl.exe -ErrorAction SilentlyContinue) } catch {}
if (-not $hasWsl) {
@@ -5082,8 +4962,6 @@ exit 0
substep " Ensure the ROCm compute driver is installed alongside the display driver:" "Yellow"
substep " https://rocm.docs.amd.com/en/latest/deploy/windows/index.html" "Yellow"
} elseif ($ROCmGfxArch) {
- # Known arch: Unsloth setup installs AMD's bundled-runtime ROCm PyTorch wheels
- # (repo.amd.com), which ship their own runtime -- HIP SDK optional.
step "gpu" "AMD ROCm ($ROCmGfxArch)" "Cyan"
substep "Detected: $ROCmGpuLabel" "Cyan"
substep "GPU PyTorch uses AMD's bundled-runtime ROCm wheels -- HIP SDK not required (optional)." "Cyan"
@@ -5133,8 +5011,7 @@ 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.
+ # Trim the URL PATH only: a whole-URL TrimEnd corrupts a token ending in "/".
function Trim-IndexPathSlashes {
param([string]$Url)
$value = $Url.Trim()
@@ -5183,9 +5060,8 @@ exit 0
function Get-CudaFamilyCappedForPreTuring {
param([string]$Family, [string]$SmiExe)
if ($Family -notin @('cu128', 'cu130')) { return $Family }
- # Windows pins torch<2.11, whose cu128 still ships sm_70, so only cu130
- # strands a Volta here. Raise to 75 when that pin reaches 2.11.
- $legacyFloorSm = if ($Family -eq 'cu128') { 70 } else { 75 }
+ # torch 2.11.0+cu128 dropped Volta, so cu128 now strands a pre-Turing host as cu130 does.
+ $legacyFloorSm = 75
switch (Get-NvidiaCu126Verdict $SmiExe $legacyFloorSm) {
'cu126' {
substep "pre-Turing NVIDIA GPUs (sm_<75) are present -- selecting cu126, because PyTorch 2.11's $Family wheels start at sm_75" "Yellow"
@@ -5203,9 +5079,7 @@ exit 0
# 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.
+ # An explicit pin skips ALL GPU probing. Matches install.sh / install_python_stack.py.
if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_URL)) {
return (Trim-IndexPathSlashes $env:UNSLOTH_TORCH_INDEX_URL)
}
@@ -5215,9 +5089,6 @@ exit 0
if (-not $NvidiaSmiExe) { return "$baseUrl/cpu" }
try {
$output = Invoke-NvidiaSmiBounded $NvidiaSmiExe
- # Newer NVIDIA drivers (e.g. 610.x on Windows) print
- # "CUDA UMD Version: X.Y" instead of the legacy "CUDA Version: X.Y".
- # Accept both spellings so we don't fall through to the cu126 default.
if ($output -match 'CUDA(?: UMD)? Version:\s+(\d+)\.(\d+)') {
$major = [int]$Matches[1]; $minor = [int]$Matches[2]
if ($major -ge 13) { $family = "cu130" }
@@ -5233,8 +5104,6 @@ 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)
# Ordinal, not culture-aware: on non-English locales (e.g. th-TH) linguistic
@@ -5281,8 +5150,6 @@ exit 0
return 'cpu'
}
- # Expected tag from the index leaf: cuXXX / cpu / rocm ($ROCmIndexUrl or a
- # gfx* leaf -> rocm). $null on an unknown leaf (odd mirror) so repair no-ops.
function Get-ExpectedTorchFlavorTag {
param([string]$TorchIndexUrl, [string]$ROCmIndexUrl)
if (-not [string]::IsNullOrWhiteSpace($ROCmIndexUrl)) { return 'rocm' }
@@ -5480,8 +5347,58 @@ exit 0
return $false
}
- # An explicit pin is authoritative: the AMD ROCm reroute below must not rewrite it
- # (e.g. a deliberate cpu pin on an AMD host).
+ # ── Torch release preservation (twin of _previous_torch_pin, PR 7250); UPGRADE=1 opts out. ──
+
+ # Strips ONLY the +local tag; the anchored numeric match keeps dev/rc/alpha from pinning.
+ function ConvertTo-TorchNumericRelease {
+ param([string]$TorchVersion)
+ if ([string]::IsNullOrWhiteSpace($TorchVersion)) { return $null }
+ $publicBase = ($TorchVersion.Trim() -split '\+', 2)[0]
+ if ($publicBase -notmatch '^(\d+)\.(\d+)(?:\.(\d+))?$') { return $null }
+ try {
+ $major = [int]$Matches[1]; $minor = [int]$Matches[2]
+ if ($Matches[3]) { $patch = [int]$Matches[3] } else { $patch = 0 }
+ $normalized = New-Object System.Version($major, $minor, $patch)
+ } catch { return $null }
+ return [pscustomobject]@{
+ PublicBase = $publicBase; Major = $major; Minor = $minor; Patch = $patch; Version = $normalized
+ }
+ }
+
+ # Fails closed on any other shape (exact pins, bare torch), so preservation defers.
+ function Test-TorchReleaseInWindow {
+ param(
+ [Parameter(Mandatory = $true)]$Release,
+ [Parameter(Mandatory = $true)][string]$Constraint
+ )
+ if ($Constraint -notmatch '^torch>=(\d+(?:\.\d+){0,2}),<(\d+(?:\.\d+){0,2})$') { return $false }
+ $floor = ConvertTo-TorchNumericRelease $Matches[1]
+ $ceiling = ConvertTo-TorchNumericRelease $Matches[2]
+ if (-not $floor -or -not $ceiling) { return $false }
+ return ($Release.Version -ge $floor.Version -and $Release.Version -lt $ceiling.Version)
+ }
+
+ # $null when nothing is kept: unstable version, UPGRADE=1, or outside the final window.
+ function Get-PreviousTorchPin {
+ param(
+ [string]$TorchVersion,
+ # Named -Constraint: the Windows port keeps no shell-style constraint variable.
+ [Parameter(Mandatory = $true)][string]$Constraint
+ )
+ if ($env:UNSLOTH_TORCH_UPGRADE -eq '1') { return $null }
+ $release = ConvertTo-TorchNumericRelease $TorchVersion
+ if (-not $release) { return $null }
+ if (-not (Test-TorchReleaseInWindow -Release $release -Constraint $Constraint)) { return $null }
+ if ($release.Major -ne 2) { return $null }
+ $visionMinor = $release.Minor + 15
+ return [pscustomobject]@{
+ Release = $release
+ TorchSpec = "torch==$($release.PublicBase)"
+ VisionSpec = "torchvision==0.$visionMinor.*"
+ AudioSpec = "torchaudio==2.$($release.Minor).*"
+ }
+ }
+
$TorchIndexPinned = (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_URL)) -or `
(-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_FAMILY))
$TorchIndexUrl = Get-TorchIndexUrl
@@ -5523,9 +5440,7 @@ exit 0
"gfx1151" = "torch>=2.11.0,<2.12.0"; "gfx1150" = "torch>=2.11.0,<2.12.0"
"gfx1152" = "torch>=2.11.0,<2.12.0"
}
- # Companion ranges track the torch ceiling so pip resolves a consistent
- # trio on AMD's per-arch index (each published independently). Mirrors
- # setup.ps1 / install_python_stack.py; bump all three together for 2.12.x.
+ # Companions track the torch ceiling for a consistent trio on AMD's per-arch index.
$torchvisionFloorMap = @{
"gfx1201" = "torchvision>=0.26.0,<0.27.0"; "gfx1200" = "torchvision>=0.26.0,<0.27.0"
"gfx1151" = "torchvision>=0.26.0,<0.27.0"; "gfx1150" = "torchvision>=0.26.0,<0.27.0"
@@ -5554,9 +5469,7 @@ 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.
+ # A gfx*/rocm pin skips the reroute; CPU/CUDA would pull a _grouped_mm-broken wheel there.
if ($TorchIndexPinned -and -not $ROCmIndexUrl -and -not $SkipTorch) {
$_pinLeaf = (($TorchIndexUrl -split '[?#]', 2)[0].TrimEnd('/') -split '/')[-1].ToLower()
$_pinRocm211 = $false
@@ -5574,8 +5487,7 @@ exit 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<digits>/gfx* are families; a suffixed leaf is verbatim.
+ # Other gfx / older rocm (<=7.1) ship torch <2.11: bare specs. Only EXACT leaves count.
$ROCmIndexUrl = $TorchIndexUrl
}
}
@@ -5592,8 +5504,6 @@ exit 0
if (-not $SkipTorch -and -not $ROCmIndexUrl -and $TorchIndexUrl -like "*/cpu") {
Write-StudioLine ""
if ($ROCmGfxArch) {
- # Only an unmapped arch reaches here (a mapped one set $ROCmIndexUrl
- # above). No ROCm torch wheels for this arch (e.g. RDNA2 gfx103X) -> CPU.
substep "Installing CPU PyTorch -- no ROCm PyTorch wheels are available for $ROCmGfxArch." "Yellow"
substep "PyTorch (training and Transformers inference) runs on CPU on this GPU." "Yellow"
} else {
@@ -5633,23 +5543,9 @@ exit 0
}
# ── Install PyTorch first, then unsloth separately ──
- #
- # Why two steps?
- # `uv pip install unsloth --torch-backend=cpu` on Windows resolves to
- # unsloth==2024.8 (a pre-CLI release with no unsloth.exe) because the
- # cpu-only solver cannot satisfy newer unsloth's dependencies.
- # Installing torch first from the explicit CUDA index, then upgrading
- # unsloth in a second step, avoids this solver dead-end.
- #
- # Why --upgrade-package instead of --upgrade?
- # `--upgrade unsloth` re-resolves ALL dependencies including torch,
- # pulling torch from default PyPI and stripping the +cuXXX suffix
- # that step 1 installed (e.g. torch 2.5.1+cu124 -> 2.10.0 with no
- # CUDA suffix). `--upgrade-package unsloth` upgrades ONLY unsloth
- # to the latest version while preserving the already-pinned torch
- # CUDA wheels. Missing dependencies (transformers, trl, peft, etc.)
- # are still pulled in because they are new, not upgrades.
- #
+ # Two steps: `uv pip install unsloth --torch-backend=cpu` resolves to the pre-CLI
+ # unsloth==2024.8, so install torch from the explicit index first. --upgrade-package (not
+ # --upgrade) so upgrading unsloth cannot re-resolve torch from PyPI and strip the +cuXXX.
# ── Helper: find no-torch-runtime.txt ──
function Find-NoTorchRuntimeFile {
if ($StudioLocalInstall -and (Test-Path (Join-Path $RepoRoot "studio\backend\requirements\no-torch-runtime.txt"))) {
@@ -5661,13 +5557,70 @@ exit 0
return $installed
}
+ # ── Freeze the trio: a released unsloth wheel can downgrade it. The caller removes it. ──
+ function New-UnslothTorchOverridesFile {
+ param([string]$PythonExe)
+ if ($SkipTorch) { return $null }
+ $pins = & $PythonExe -c "from importlib.metadata import version, PackageNotFoundError`nfor _p in ('torch', 'torchvision', 'torchaudio'):`n try:`n print(_p + '==' + version(_p))`n except PackageNotFoundError:`n pass" 2>$null
+ $lines = @($pins | Where-Object { $_ -match '^torch' })
+ if ($lines.Count -eq 0 -or $lines[0] -notmatch '^torch==') { return $null }
+ # --overrides replaces any UV_OVERRIDE env file, so fold caller files in, minus their trio.
+ $ovDirs = @()
+ if ($env:UV_OVERRIDE) {
+ foreach ($ovFile in ($env:UV_OVERRIDE -split '\s+' | Where-Object { $_ })) {
+ if (Test-Path -LiteralPath $ovFile -PathType Leaf) {
+ $ovFull = (Convert-Path -LiteralPath $ovFile)
+ # ReadAllLines, not Get-Content: PS 5.1 decodes a BOM-less file with the ANSI
+ # code page, turning a non-ASCII path into mojibake the UTF-8 write preserves.
+ $lines += @([System.IO.File]::ReadAllLines($ovFull) | Where-Object {
+ $_ -notmatch '^\s*torch(vision|audio)?([\s<>=!~;@[]|$)'
+ })
+ $ovDirs += (Split-Path -Parent $ovFull)
+ }
+ }
+ }
+ # uv resolves an override's relative references against THAT file's dir, so write the
+ # merge beside the caller's override; %TEMP% when there is none, several dirs, or read-only.
+ $f = $null
+ $ovDirs = @($ovDirs | Sort-Object -Unique)
+ if ($ovDirs.Count -eq 1) {
+ try {
+ $candidate = Join-Path $ovDirs[0] ("unsloth-torch-overrides-" + [guid]::NewGuid().ToString("N") + ".txt")
+ New-Item -ItemType File -Path $candidate -ErrorAction Stop | Out-Null
+ $f = $candidate
+ } catch { $f = $null }
+ }
+ if (-not $f) { $f = [System.IO.Path]::GetTempFileName() }
+ # Track it the moment it exists: WriteAllText can throw and the outer cleanup would have
+ # no path to remove, leaving a partial copy, authenticated URLs included, on disk.
+ $script:TorchOverridesFile = $f
+ # This copy can hold credentials in a direct URL, and New-Item takes the directory's
+ # inherited ACL, not the source file's. Twin of install.sh's umask 077 plus chmod 600.
+ try {
+ $_acl = Get-Acl -LiteralPath $f
+ $_acl.SetAccessRuleProtection($true, $false)
+ $_me = [System.Security.Principal.WindowsIdentity]::GetCurrent().User
+ $_acl.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule(
+ $_me, "FullControl", "Allow")))
+ Set-Acl -LiteralPath $f -AclObject $_acl
+ } catch { } # best effort, and a no-op off Windows
+ # UTF-8 without a BOM, not -Encoding ascii, which rewrites a non-ASCII path as "?".
+ # WriteAllText adds no trailing newline: terminate it or two requirements join.
+ try {
+ [System.IO.File]::WriteAllText($f, (($lines -join "`n") + "`n"), (New-Object System.Text.UTF8Encoding($false)))
+ } catch {
+ Remove-Item -LiteralPath $f -Force -ErrorAction SilentlyContinue
+ $script:TorchOverridesFile = $null
+ throw
+ }
+ return $f
+ }
+
$_desktopMinVer = if ($env:UNSLOTH_DESKTOP_BACKEND_VERSION) { $env:UNSLOTH_DESKTOP_BACKEND_VERSION.Trim() } else { "" }
$_unslothDesktopInstallSpec = if ($_desktopMinVer) { "unsloth>=$_desktopMinVer" } else { $null }
$_unslothReleaseInstallSpec = if ($_unslothDesktopInstallSpec) { $_unslothDesktopInstallSpec } else { "unsloth>=2026.9.2" }
if ($_Migrated) {
- # 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) {
@@ -5708,35 +5661,62 @@ exit 0
}
}
} elseif ($TorchIndexUrl -or $ROCmIndexUrl) {
+ # Bounded default trio (torch 2.11 line), hoisted so release preservation uses it as the
+ # route window. torchaudio 2.11 dropped its torch pin, so a bare companion can drift.
+ $_pinTorchSpec = "torch>=2.4,<2.12.0"
+ $_pinVisionSpec = "torchvision>=0.19,<0.27.0"
+ $_pinAudioSpec = "torchaudio>=2.4,<2.12.0"
+ # Release preservation, after every floor choice. Exported as UNSLOTH_KEPT_TORCH.
+ $script:PrevTorchPin = $null
+ # An interrupted run leaks a stale pin, so clear before deciding.
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ if (-not $SkipTorch -and $script:PrevTorchVer) {
+ $_routeWindow = $_pinTorchSpec
+ # Vet the kept release against the XPU window, or a kept 2.5 becomes an unsatisfiable pin.
+ if ((Get-TorchIndexLeafName $TorchIndexUrl) -eq "xpu") { $_routeWindow = (Get-XpuTorchSpecs -Platform (Get-VenvPlatformTag -PythonExe $VenvPython))[0] }
+ if ($ROCmIndexUrl -and $ROCmTorchFloor) { $_routeWindow = $ROCmTorchFloor }
+ $script:PrevTorchPin = Get-PreviousTorchPin -TorchVersion $script:PrevTorchVer -Constraint $_routeWindow
+ if ($script:PrevTorchPin) {
+ $env:UNSLOTH_KEPT_TORCH = $script:PrevTorchPin.Release.PublicBase
+ substep "existing install has torch $script:PrevTorchVer -- keeping it (set UNSLOTH_TORCH_UPGRADE=1 to get the newest release)"
+ }
+ }
if ($SkipTorch) {
substep "skipping PyTorch (--no-torch flag set)." "Yellow"
} elseif ($ROCmIndexUrl) {
Write-TauriLog "STEP" "Installing PyTorch (AMD ROCm Windows)"
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.
+ # Pin companions to $torchSpec: bare names can resolve an ABI-incompatible pair.
$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)" { & $script:UvExe pip install --python $VenvPython --force-reinstall --default-index $ROCmIndexUrl $torchSpec $visionSpec $audioSpec }
+ if ($script:PrevTorchPin) {
+ $_keptTorch = $script:PrevTorchPin.TorchSpec; $_keptVision = $script:PrevTorchPin.VisionSpec; $_keptAudio = $script:PrevTorchPin.AudioSpec
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (kept release)" { & $script:UvExe pip install --python $VenvPython --force-reinstall $_keptTorch $_keptVision $_keptAudio --default-index $ROCmIndexUrl }
+ if ($torchInstallExit -ne 0) {
+ substep "[WARN] $_keptTorch is not installable from $(Remove-IndexUrlCredentials $ROCmIndexUrl) -- installing the newest supported release instead" "Yellow"
+ $script:PrevTorchPin = $null
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ }
+ }
+ if (-not $script:PrevTorchPin) {
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (AMD ROCm)" { & $script:UvExe 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 (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.
+ # Explicit CPU index: under a ROCm pin $TorchIndexUrl IS the mirror that failed.
$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)" { & $script:UvExe 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 }
+ # No kept-release attempt: the ROCm attempts resolve or clear the pin first.
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (CPU fallback)" { & $script:UvExe pip install --python $VenvPython --force-reinstall "torch>=2.4,<2.12.0" "torchvision>=0.19,<0.27.0" "torchaudio>=2.4,<2.12.0" --default-index $CpuFallbackIndexUrl }
if ($torchInstallExit -ne 0) {
Write-StudioLine "[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)
}
- # CPU base is in; drop the ROCm expectation so the flavor-repair
- # block below won't retry the just-failed index and abort. setup.ps1
- # reinstalls ROCm afterwards (recomputes its own index URL).
+ # Drop the ROCm expectation so the flavor repair does not retry the failed index.
$ROCmIndexUrl = $null
$ROCmTorchFloor = $null
}
@@ -5756,12 +5736,26 @@ exit 0
# rather than abort -- the pin-only route reaches this branch on an arm64 interpreter.
$VenvPlatform = Get-VenvPlatformTag -PythonExe $VenvPython
$_xpuSpecs = Get-XpuTorchSpecs -Platform $VenvPlatform
- $_xpuCpuSpecs = @("torch>=2.4,<2.11.0", "torchvision>=0.19,<0.26.0", "torchaudio>=2.4,<2.11.0")
+ $_xpuCpuSpecs = @("torch>=2.4,<2.12.0", "torchvision>=0.19,<0.27.0", "torchaudio>=2.4,<2.12.0")
if ($VenvPlatform -eq "win-arm64") {
substep "windows on arm: skipping torchaudio (upstream publishes no win_arm64 wheel)."
- $_xpuCpuSpecs = @("torch>=2.4,<2.11.0", "torchvision>=0.19,<0.26.0")
+ $_xpuCpuSpecs = @("torch>=2.4,<2.12.0", "torchvision>=0.19,<0.27.0")
+ }
+ # The kept trio follows $_xpuSpecs' shape so win-arm64 still omits torchaudio.
+ if ($script:PrevTorchPin) {
+ $_keptTorch = $script:PrevTorchPin.TorchSpec; $_keptVision = $script:PrevTorchPin.VisionSpec; $_keptAudio = $script:PrevTorchPin.AudioSpec
+ $_keptXpuSpecs = @($_keptTorch, $_keptVision)
+ if ($_xpuSpecs.Count -ge 3) { $_keptXpuSpecs += $_keptAudio }
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (kept release)" { & $script:UvExe pip install --python $VenvPython --force-reinstall @_keptXpuSpecs --default-index $TorchIndexUrl }
+ if ($torchInstallExit -ne 0) {
+ substep "[WARN] $_keptTorch is not installable from $(Remove-IndexUrlCredentials $TorchIndexUrl) -- installing the newest supported release instead" "Yellow"
+ $script:PrevTorchPin = $null
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ }
+ }
+ if (-not $script:PrevTorchPin) {
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (Intel XPU)" { & $script:UvExe pip install --python $VenvPython --force-reinstall @_xpuSpecs --default-index $TorchIndexUrl }
}
- $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (Intel XPU)" { & $script:UvExe pip install --python $VenvPython --force-reinstall @_xpuSpecs --default-index $TorchIndexUrl }
if ($torchInstallExit -ne 0) {
# Transient XPU-index failure: fall back to CPU base.
$CpuFallbackIndexUrl = if ($env:UNSLOTH_PYTORCH_MIRROR) { "$($env:UNSLOTH_PYTORCH_MIRROR.TrimEnd('/'))/cpu" } else { "https://download.pytorch.org/whl/cpu" }
@@ -5794,15 +5788,27 @@ exit 0
# 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"
- $_torchSpecs = @("torch>=2.4,<2.11.0", $_pinVisionSpec, $_pinAudioSpec)
+ # The hoisted trio, so the vetted route window and the installed specs cannot drift.
+ $_torchSpecs = @($_pinTorchSpec, $_pinVisionSpec, $_pinAudioSpec)
if ($VenvPlatform -eq "win-arm64") {
substep "windows on arm: skipping torchaudio (upstream publishes no"
substep "win_arm64 wheel); torch and torchvision install normally."
- $_torchSpecs = @("torch>=2.4,<2.11.0", $_pinVisionSpec)
+ $_torchSpecs = @($_pinTorchSpec, $_pinVisionSpec)
+ }
+ if ($script:PrevTorchPin) {
+ $_keptTorch = $script:PrevTorchPin.TorchSpec; $_keptVision = $script:PrevTorchPin.VisionSpec; $_keptAudio = $script:PrevTorchPin.AudioSpec
+ $_keptSpecs = @($_keptTorch, $_keptVision)
+ if ($_torchSpecs.Count -ge 3) { $_keptSpecs += $_keptAudio }
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch (kept release)" { & $script:UvExe pip install --python $VenvPython @_keptSpecs --default-index $TorchIndexUrl }
+ if ($torchInstallExit -ne 0) {
+ substep "[WARN] $_keptTorch is not installable from $(Remove-IndexUrlCredentials $TorchIndexUrl) -- installing the newest supported release instead" "Yellow"
+ $script:PrevTorchPin = $null
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ }
+ }
+ if (-not $script:PrevTorchPin) {
+ $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch" { & $script:UvExe pip install --python $VenvPython @_torchSpecs --default-index $TorchIndexUrl }
}
- $torchInstallExit = Invoke-InstallCommandRetry -Label "install PyTorch" { & $script:UvExe pip install --python $VenvPython @_torchSpecs --default-index $TorchIndexUrl }
if ($torchInstallExit -ne 0) {
Write-StudioLine "[ERROR] Failed to install PyTorch (exit code $torchInstallExit)" -ForegroundColor Red
return (Exit-InstallFailure "Failed to install PyTorch (exit code $torchInstallExit)" $torchInstallExit)
@@ -5826,10 +5832,25 @@ exit 0
}
}
} elseif ($StudioLocalInstall) {
- $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (local)" { & $script:UvExe pip install --python $VenvPython --upgrade-package unsloth "$_unslothReleaseInstallSpec" "unsloth-zoo>=2026.9.1" }
+ # Freeze the trio so this with-deps resolve cannot downgrade the pinned build.
+ $script:TorchOverridesFile = New-UnslothTorchOverridesFile -PythonExe $VenvPython
+ if ($script:TorchOverridesFile) {
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (local)" { & $script:UvExe pip install --python $VenvPython --upgrade-package unsloth --overrides $script:TorchOverridesFile "$_unslothReleaseInstallSpec" "unsloth-zoo>=2026.9.1" }
+ Remove-Item -LiteralPath $script:TorchOverridesFile -Force -ErrorAction SilentlyContinue
+ $script:TorchOverridesFile = $null
+ } else {
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth (local)" { & $script:UvExe pip install --python $VenvPython --upgrade-package unsloth "$_unslothReleaseInstallSpec" "unsloth-zoo>=2026.9.1" }
+ }
} else {
$_unslothPkg = if ($PackageName -eq "unsloth" -and $_unslothDesktopInstallSpec) { $_unslothDesktopInstallSpec } else { $PackageName }
- $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth" { & $script:UvExe pip install --python $VenvPython --upgrade-package unsloth -- "$_unslothPkg" }
+ $script:TorchOverridesFile = New-UnslothTorchOverridesFile -PythonExe $VenvPython
+ if ($script:TorchOverridesFile) {
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth" { & $script:UvExe pip install --python $VenvPython --upgrade-package unsloth --overrides $script:TorchOverridesFile -- "$_unslothPkg" }
+ Remove-Item -LiteralPath $script:TorchOverridesFile -Force -ErrorAction SilentlyContinue
+ $script:TorchOverridesFile = $null
+ } else {
+ $baseInstallExit = Invoke-InstallCommandRetry -Label "install unsloth" { & $script:UvExe pip install --python $VenvPython --upgrade-package unsloth -- "$_unslothPkg" }
+ }
}
if ($baseInstallExit -ne 0) {
Write-StudioLine "[ERROR] Failed to install unsloth (exit code $baseInstallExit)" -ForegroundColor Red
@@ -5928,42 +5949,70 @@ sys.exit(2 if conflict else (0 if installed else 1))
substep "[WARN] installed $PackageName version could not be determined" "Yellow"
}
- # ── Enforce the installed torch flavor matches the detected GPU build ──
- # PEP 440 ignores the +cpu/+cuXXX/+rocm local label in a version range, so uv
- # 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 --default-index, same URL the fresh ROCm install
- # above uses). --no-torch / CPU-only hosts (expected cpu) are no-ops.
+ # ── PEP 440 ignores the +cpu/+cuXXX local label, so uv keeps a stale torch+cpu against a CUDA
+ # index. Reinstall the triplet when a GPU build is expected; --no-torch is a no-op. ──
if (-not $SkipTorch) {
$expectedTorchTag = Get-ExpectedTorchFlavorTag -TorchIndexUrl $TorchIndexUrl -ROCmIndexUrl $ROCmIndexUrl
if ($expectedTorchTag -and $expectedTorchTag -ne 'cpu') {
$installedTorchTag = Get-InstalledTorchTag -PythonExe $VenvPython
if ($installedTorchTag -and $installedTorchTag -ne $expectedTorchTag) {
if ($expectedTorchTag -eq 'rocm' -and $ROCmIndexUrl) {
- # AMD: a migrated venv can keep a stale CPU torch the fresh ROCm path
- # would have force-reinstalled. Repair from the same repo.amd.com index.
+ # A migrated venv can keep a stale CPU torch the fresh ROCm path would replace.
$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 ($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" }
+ # Kept-release substitution, as install.sh's _install_torch_default_index does.
+ $_rocmKept = $false
+ if ($script:PrevTorchPin) {
+ $_origRocmSpec = $rocmSpec; $_origVisionSpec = $visionSpec; $_origAudioSpec = $audioSpec
+ $rocmSpec = $script:PrevTorchPin.TorchSpec; $visionSpec = $script:PrevTorchPin.VisionSpec; $audioSpec = $script:PrevTorchPin.AudioSpec
+ $_rocmKept = $true
+ }
substep "PyTorch flavor mismatch (installed $installedTorchTag, need ROCm) -- reinstalling correct build..." "Yellow"
$torchFixExit = Invoke-InstallCommand -Label "reinstall PyTorch (ROCm)" { & $script:UvExe pip install --python $VenvPython --force-reinstall --default-index $ROCmIndexUrl $rocmSpec $visionSpec $audioSpec }
+ if ($torchFixExit -ne 0 -and $_rocmKept) {
+ substep "[WARN] $rocmSpec is not installable from $(Remove-IndexUrlCredentials $ROCmIndexUrl) -- installing the newest supported release instead" "Yellow"
+ $rocmSpec = $_origRocmSpec; $visionSpec = $_origVisionSpec; $audioSpec = $_origAudioSpec
+ $script:PrevTorchPin = $null
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ $torchFixExit = Invoke-InstallCommand -Label "reinstall PyTorch (ROCm)" { & $script:UvExe pip install --python $VenvPython --force-reinstall --default-index $ROCmIndexUrl $rocmSpec $visionSpec $audioSpec }
+ }
if ($torchFixExit -ne 0) {
Write-StudioLine "[ERROR] Failed to reinstall PyTorch with the correct ROCm build (exit code $torchFixExit)" -ForegroundColor Red
return (Exit-InstallFailure "Failed to reinstall PyTorch (ROCm) (exit code $torchFixExit)" $torchFixExit)
}
$installedTorchTag = Get-InstalledTorchTag -PythonExe $VenvPython
} elseif ($expectedTorchTag -ne 'rocm') {
- # CUDA: stale +cpu (or wrong cuXXX) against a CUDA index -> reinstall triplet.
+ # CUDA: reinstall the triplet with the default 2.11-line trio.
+ $_fixTorchSpec = "torch>=2.4,<2.12.0"; $_fixVisionSpec = "torchvision>=0.19,<0.27.0"; $_fixAudioSpec = "torchaudio>=2.4,<2.12.0"
+ $_cudaKept = $false
+ if ($script:PrevTorchPin) {
+ $_origFixTorchSpec = $_fixTorchSpec; $_origFixVisionSpec = $_fixVisionSpec; $_origFixAudioSpec = $_fixAudioSpec
+ $_fixTorchSpec = $script:PrevTorchPin.TorchSpec; $_fixVisionSpec = $script:PrevTorchPin.VisionSpec; $_fixAudioSpec = $script:PrevTorchPin.AudioSpec
+ $_cudaKept = $true
+ }
substep "PyTorch flavor mismatch (installed $installedTorchTag, need $expectedTorchTag) -- reinstalling correct build..." "Yellow"
- # Same trio builder as the XPU install above: a migrated win-arm64 venv
- # reaches THIS path, and asking for a torchaudio the index has no wheel for
- # fails the repair before setup.ps1's ARM-aware fallback.
- $_fixSpecs = if ($expectedTorchTag -eq 'xpu') { Get-XpuTorchSpecs -Platform (Get-VenvPlatformTag -PythonExe $VenvPython) }
- else { @("torch>=2.4,<2.11.0", "torchvision>=0.19,<0.26.0", "torchaudio>=2.4,<2.11.0") }
+ # A migrated win-arm64 venv lands here; torchaudio is unpublished for it.
+ $_fixSpecs = @($_fixTorchSpec, $_fixVisionSpec, $_fixAudioSpec)
+ $_origFixSpecs = if ($_cudaKept) { @($_origFixTorchSpec, $_origFixVisionSpec, $_origFixAudioSpec) } else { $_fixSpecs }
+ if ($expectedTorchTag -eq 'xpu') {
+ $_origFixSpecs = Get-XpuTorchSpecs -Platform (Get-VenvPlatformTag -PythonExe $VenvPython)
+ if ($_cudaKept) {
+ $_fixSpecs = @($_fixTorchSpec, $_fixVisionSpec)
+ if ($_origFixSpecs.Count -ge 3) { $_fixSpecs += $_fixAudioSpec }
+ } else {
+ $_fixSpecs = $_origFixSpecs
+ }
+ }
$torchFixExit = Invoke-InstallCommand -Label "reinstall PyTorch ($expectedTorchTag)" { & $script:UvExe pip install --python $VenvPython @_fixSpecs --default-index $TorchIndexUrl --reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio }
+ if ($torchFixExit -ne 0 -and $_cudaKept) {
+ substep "[WARN] $_fixTorchSpec is not installable from $(Remove-IndexUrlCredentials $TorchIndexUrl) -- installing the newest supported release instead" "Yellow"
+ $_fixTorchSpec = $_origFixTorchSpec; $_fixVisionSpec = $_origFixVisionSpec; $_fixAudioSpec = $_origFixAudioSpec
+ $_fixSpecs = $_origFixSpecs
+ $script:PrevTorchPin = $null
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ $torchFixExit = Invoke-InstallCommand -Label "reinstall PyTorch ($expectedTorchTag)" { & $script:UvExe pip install --python $VenvPython @_fixSpecs --default-index $TorchIndexUrl --reinstall-package torch --reinstall-package torchvision --reinstall-package torchaudio }
+ }
if ($torchFixExit -ne 0) {
Write-StudioLine "[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)
@@ -6176,8 +6225,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
}
# Tauri desktop app bundles its own frontend — skip Node/npm/frontend build
$env:SKIP_STUDIO_FRONTEND = if ($TauriMode) { "1" } else { "0" }
- # Always set STUDIO_LOCAL_INSTALL explicitly to avoid stale values from
- # a previous --local run in the same PowerShell session.
+ # Always set explicitly: a stale value from a previous --local run would leak.
if ($StudioLocalInstall) {
$env:STUDIO_LOCAL_INSTALL = "1"
$env:STUDIO_LOCAL_REPO = $RepoRoot
@@ -6185,11 +6233,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
$env:STUDIO_LOCAL_INSTALL = "0"
Remove-Item Env:STUDIO_LOCAL_REPO -ErrorAction SilentlyContinue
}
- # Use 'studio setup' (not 'studio update') because 'update' pops
- # SKIP_STUDIO_BASE, which would cause redundant package reinstallation
- # and bypass the fast-path version check from PR #4667.
- # Propagate UNSLOTH_STUDIO_HOME only for env-override installs; otherwise
- # an inherited value would put llama.cpp in the wrong place.
+ # 'setup', not 'update': update pops SKIP_STUDIO_BASE and skips the #4667 fast path.
$previousUnslothStudioHome = $env:UNSLOTH_STUDIO_HOME
$hadPreviousUnslothStudioHome = ($null -ne $previousUnslothStudioHome)
$previousTauriMode = $env:UNSLOTH_TAURI_MODE
@@ -6210,10 +6254,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
$env:UNSLOTH_LOCAL_LLAMA_CPP_DIR = (Resolve-Path -LiteralPath $WithLlamaCppDir).Path
}
$env:UNSLOTH_INSTALL_ROLLBACK_MANAGED = "1"
- # Hand the venv interpreter to setup.ps1 so it reuses the Python we already
- # resolved and built the venv with, instead of re-probing the system (which
- # can trip over an unsupported `python` 3.14 or a Store stub on PATH even
- # though the venv is fine). setup.ps1 Test-Path-guards this before use.
+ # Hand setup.ps1 the venv interpreter so it does not re-probe a PATH led by a stub.
$env:UNSLOTH_SETUP_PYTHON = Join-Path $VenvDir "Scripts\python.exe"
# Installer already owns the runtime mutex; the child inherits it rather
# than deadlocking trying to reacquire it.
@@ -6289,6 +6330,16 @@ sys.exit(2 if conflict else (0 if installed else 1))
if ($null -eq $setupExit) {
return (Exit-InstallFailure (Write-ApplicationControlBlocked -Path $VenvPython))
}
+ # Clear the handoff so a later update cannot re-pin; warn, never abort, on a series change.
+ if ($script:PrevTorchPin) {
+ $_keptSeries = "$($script:PrevTorchPin.Release.Major).$($script:PrevTorchPin.Release.Minor)"
+ $_nowVer = Get-InstalledTorchVersionRaw -PythonExe $VenvPython
+ $_nowRelease = ConvertTo-TorchNumericRelease $_nowVer
+ if ($_nowRelease -and "$($_nowRelease.Major).$($_nowRelease.Minor)" -ne $_keptSeries) {
+ Write-StudioLine "[WARN] kept torch $($script:PrevTorchVer) but the environment now has torch $_nowVer" -ForegroundColor Red
+ }
+ }
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
if ($setupExit -ne 0) {
if (-not $TauriMode) {
Write-StudioLine "[ERROR] unsloth studio setup failed (exit code $setupExit)" -ForegroundColor Red
@@ -6297,11 +6348,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
}
Clear-TauriInstallError "studio setup completed"
- # ── Expose `unsloth` via a shim dir containing only unsloth.exe ──
- # We do NOT add the venv Scripts dir to PATH (it also holds python.exe
- # and pip.exe, which would hijack the user's system interpreter).
- # Hardlink preferred; falls back to copy if cross-volume or non-NTFS.
- #
+ # ── Shim dir holds only unsloth.exe; the venv Scripts python.exe would hijack the system. ──
# Remove the legacy venv Scripts PATH entry that older installers wrote.
$LegacyScriptsDir = Join-Path $VenvDir "Scripts"
try {
@@ -6336,9 +6383,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
$ShimDir = Join-Path $StudioHome "bin"
[System.IO.Directory]::CreateDirectory($ShimDir) | Out-Null
$ShimExe = Join-Path $ShimDir "unsloth.exe"
- # Fatal preflight outside the lock-handling try/catch -- a directory at
- # the shim path must not be downgraded to "Continuing with the existing
- # launcher", or the install finishes with no usable shim.
+ # Outside the lock try/catch: a directory here must not degrade to "existing launcher".
if (Test-Path -LiteralPath $ShimExe -PathType Container) {
Write-StudioLine "[ERROR] Cannot create unsloth launcher: $ShimExe is a directory." -ForegroundColor Red
Write-StudioLine " Move or remove it manually, then re-run the installer." -ForegroundColor Yellow
@@ -6349,10 +6394,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
try {
if (Test-Path -LiteralPath $ShimExe) { Remove-Item -LiteralPath $ShimExe -Force -ErrorAction Stop }
try {
- # New-Item -ItemType HardLink does NOT accept -LiteralPath in any
- # PowerShell version, so use -Path. Wildcards in $ShimExe (e.g.
- # brackets in custom roots) glob-expand here and fall through to
- # the Copy-Item -LiteralPath fallback below.
+ # New-Item -ItemType HardLink takes no -LiteralPath, so brackets in $ShimExe glob-expand.
New-Item -ItemType HardLink -Path $ShimExe -Target $UnslothExe -ErrorAction Stop | Out-Null
} catch {
Copy-Item -LiteralPath $UnslothExe -Destination $ShimExe -Force -ErrorAction Stop # fallback: copy
@@ -6424,12 +6466,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
# New-StudioShortcuts gates the .lnk shortcuts on env-mode internally.
New-StudioShortcuts -ManagedPythonPath $VenvPython
- # Warn if another 'unsloth' wins on PATH (different venv, system pip).
- # Mirrors install.sh; absolute path is still the most reliable launch.
- # Uses content-hash equality (Get-FileHash) so hardlinks, symlinks, and
- # identical copies of the installer's shim don't false-trigger. CommandType
- # Application restricts the probe to real executables (skips aliases,
- # functions, scripts).
+ # Compare content hashes so hardlinks and identical copies do not false-trigger.
try {
$_pathCmd = Get-Command unsloth -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
if ($_pathCmd) {
@@ -6462,9 +6499,6 @@ sys.exit(2 if conflict else (0 if installed else 1))
# Diagnostic only; never block install on a probe failure.
}
- # In interactive terminals, ask the user before starting Unsloth unless the
- # caller explicitly disabled the post-install prompt.
- # In non-interactive environments (CI, Docker) just print instructions.
$IsInteractive = (-not $SkipAutostart) -and [Environment]::UserInteractive -and (-not [Console]::IsInputRedirected)
if ($IsInteractive) {
Write-StudioLine ""
@@ -6511,8 +6545,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
}
} else {
step "launch" "manual commands:"
- # Single-quote the printed paths so $-vars / backticks in custom roots
- # do not reparse when the user pastes the command.
+ # Single-quote printed paths so $-vars in custom roots survive a copy-paste.
$_actLiteral = "'" + ((Join-Path $VenvDir "Scripts\Activate.ps1") -replace "'", "''") + "'"
# Activating the venv puts Scripts\unsloth.exe first and PATHEXT prefers .EXE,
# so every bare `unsloth` below is unusable where the policy denies it. Same
@@ -6555,4 +6588,16 @@ sys.exit(2 if conflict else (0 if installed else 1))
}
}
-Install-UnslothStudio @args
+# Under `irm | iex` the script scope IS the caller's session; an earlier value must not leak.
+$script:TorchOverridesFile = $null
+try {
+ Install-UnslothStudio @args
+} finally {
+ # UNSLOTH_KEPT_TORCH is a process-scoped handoff, and the session outlives the installer.
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ # The generated overrides file copies the caller's UV_OVERRIDE contents; never leave it.
+ if ($script:TorchOverridesFile) {
+ Remove-Item -LiteralPath $script:TorchOverridesFile -Force -ErrorAction SilentlyContinue
+ $script:TorchOverridesFile = $null
+ }
+}
diff --git a/install.sh b/install.sh
index f95c3e38509..ded02f4292d 100755
--- a/install.sh
+++ b/install.sh
@@ -66,9 +66,6 @@ _SHORTCUTS_ONLY=false
_next_is_package=false
_next_is_python=false
_next_is_llama_cpp_dir=false
-# Seed from the environment so a caller who exports UNSLOTH_LOCAL_LLAMA_CPP_DIR
-# (the documented piped-install style) is honored; the --with-llama-cpp-dir
-# flag below overrides it when given.
_WITH_LLAMA_CPP_DIR="${UNSLOTH_LOCAL_LLAMA_CPP_DIR:-}"
for arg in "$@"; do
if [ "$_next_is_package" = true ]; then
@@ -107,8 +104,7 @@ if [ "$_VERBOSE" = true ]; then
export UNSLOTH_VERBOSE=1
fi
-# Custom Unsloth roots are not supported with --tauri (desktop app still
-# resolves ~/.unsloth/studio). Pass through if the override == legacy default.
+# Custom Unsloth roots are unsupported with --tauri unless override == legacy default.
if [ "$TAURI_MODE" = true ]; then
_tauri_override_var=""
_tauri_override="${UNSLOTH_STUDIO_HOME:-}"
@@ -125,8 +121,7 @@ if [ "$TAURI_MODE" = true ]; then
"~") _tauri_override="$HOME" ;;
"~/"*) _tauri_override="$HOME/${_tauri_override#'~/'}" ;;
esac
- # Canonicalize both sides (CDPATH=, -P) so a CDPATH-set env or
- # symlinked $HOME doesn't break the legacy-equality comparison.
+ # Canonicalize both sides so CDPATH / a symlinked $HOME cannot break equality.
if [ -d "$_tauri_override" ]; then
_tauri_override_abs=$(CDPATH= cd -P -- "$_tauri_override" 2>/dev/null && pwd -P) \
|| _tauri_override_abs="$_tauri_override"
@@ -169,8 +164,6 @@ 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
@@ -189,9 +182,6 @@ _trim_index_path_slashes() {
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<redacted>@#g' \
@@ -245,19 +235,12 @@ _uv_download_markers() {
run_install_cmd() {
_label="$1"
shift
- # 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).
+ # For --default-index, clear inherited uv index vars so a uv.toml cannot outrank the CLI pin.
case " $* " in
*" --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
- # 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).
+ # Stream through the redactor; the rc file carries the exit code (no pipefail in sh).
_rcf=$(mktemp)
tauri_stream_log stdout "OUTPUT_CLEAR" "$_label"
{
@@ -306,16 +289,12 @@ run_install_cmd() {
return $_rc
}
-# Retry run_install_cmd on transient uv download failures with backoff. Returns
-# the last exit code on permanent failure so the set -e rollback trap still fires.
+# Returns the last exit code so the set -e rollback trap still fires.
: "${UNSLOTH_INSTALL_RETRIES:=3}"
: "${UNSLOTH_INSTALL_RETRY_DELAY:=3}"
run_install_cmd_retry() {
_ricr_label="$1"
- # Sanitize overrides to a default of 3 (a typo must not disable retries; =1 disables).
- # Length guard precedes the numeric test so a huge value can't overflow `[ -ge ]`.
- # 0?* rejects leading-zero delays ("08"/"09" break the later $((delay*2)) as octal);
- # bare "0" stays valid. Bounds: 1..100 retries, 0..3600s base delay.
+ # Default 3, bounds 1..100 retries and 0..3600s delay; 0?* rejects octal delays.
case "$UNSLOTH_INSTALL_RETRIES" in
''|*[!0-9]*|0) _ricr_max=3 ;;
*) if [ "${#UNSLOTH_INSTALL_RETRIES}" -le 3 ] && [ "$UNSLOTH_INSTALL_RETRIES" -ge 1 ] 2>/dev/null && [ "$UNSLOTH_INSTALL_RETRIES" -le 100 ] 2>/dev/null; then _ricr_max=$UNSLOTH_INSTALL_RETRIES; else _ricr_max=3; fi ;;
@@ -326,8 +305,7 @@ run_install_cmd_retry() {
esac
_ricr_attempt=1
while :; do
- # AND-OR (not `if`) preserves the real failure code: $? after a non-taken
- # `if` is 0 in sh/dash/bash, which would break the rollback path.
+ # AND-OR (not `if`) preserves the real failure code for the rollback path.
run_install_cmd "$@" && return 0
_ricr_rc=$?
if [ "$_ricr_attempt" -ge "$_ricr_max" ]; then
@@ -473,8 +451,7 @@ if [ "$_next_is_llama_cpp_dir" = true ]; then
exit 1
fi
-# Validate --package to prevent injection into shell/Python commands.
-# Must start with a letter/digit (rejects leading dashes that uv would parse as flags).
+# Validate --package: a leading letter/digit stops uv parsing it as a flag.
case "$PACKAGE_NAME" in
[!a-zA-Z0-9]*)
echo "❌ ERROR: --package name must start with a letter or digit." >&2
@@ -531,8 +508,6 @@ _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%/}"
@@ -550,8 +525,7 @@ _tauri_torch_index_family() {
rocm[0-9]*.[0-9]*) echo "$_diag_family" ;;
*) echo "auto" ;;
esac ;;
- # AMD arch-specific index (e.g. repo.amd.com/rocm/whl/gfx1151/) --
- # used for Strix Halo/Point where torch 2.11+rocm7.13 has the real fix.
+ # AMD arch-specific index (Strix Halo/Point; torch 2.11+rocm7.13 has the real fix).
*repo.amd.com/rocm/whl/gfx*|*rocm/whl/gfx*) echo "rocm7.13" ;;
"") echo "none" ;;
*) echo "auto" ;;
@@ -570,7 +544,7 @@ _tauri_gpu_branch() {
return
fi
case "$_diag_family" in
- # Require a digit after cu so /current or /custom isn't branded CUDA (parity ^cu[0-9]).
+ # Require a digit after cu so /current or /custom is not branded CUDA.
cu[0-9]*) echo "cuda" ;;
rocm*)
if [ "$_diag_radeon" = true ]; then
@@ -588,9 +562,6 @@ _tauri_gpu_branch() {
PYTHON_VERSION="" # resolved after platform detection
-# Resolve install destinations: env override, HOME-redirect (best-effort
-# via getent/dscl), or default. Env-var priority: UNSLOTH_STUDIO_HOME wins
-# over STUDIO_HOME (the more specific signal beats the generic alias).
_resolve_studio_destinations() {
_override_var=""
_override="${UNSLOTH_STUDIO_HOME:-}"
@@ -600,10 +571,9 @@ _resolve_studio_destinations() {
_override="${STUDIO_HOME:-}"
[ -n "$_override" ] && _override_var="STUDIO_HOME"
fi
- # Strip surrounding whitespace so " " is treated as unset (matches the
- # Python resolvers' .strip()), preventing install/runtime layout drift.
+ # Strip surrounding whitespace so " " is treated as unset (matches Python .strip()).
_override=$(printf '%s' "$_override" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
- # Tilde expansion: env vars are not subject to it when quoted on assignment.
+ # Tilde expansion: quoted env vars aren't subject to it on assignment.
case "$_override" in
"~") _override="$HOME" ;;
"~/"*) _override="$HOME/${_override#'~/'}" ;;
@@ -624,8 +594,6 @@ _resolve_studio_destinations() {
elif [ "$(uname)" = "Darwin" ] && command -v dscl >/dev/null 2>&1; then
_default_home=$(dscl . -read "/Users/${USER:-$(whoami)}" NFSHomeDirectory 2>/dev/null | awk '{print $2}')
fi
- # Canonicalize both sides so a trailing slash on $HOME (or symlink mismatch
- # with passwd-DB output) doesn't misfire the redirection branch.
_home_canon="$HOME"
if [ -d "$_home_canon" ]; then
_home_canon=$(CDPATH= cd -P -- "$_home_canon" 2>/dev/null && pwd -P) || _home_canon="$HOME"
@@ -1085,10 +1053,6 @@ _css_read_valid_install_id() (
)
# ── Helper: create desktop shortcuts and launcher script ──
-# Usage: create_studio_shortcuts <unsloth_exe> <os>
-# Creates ~/.local/share/unsloth/launch-studio.sh (shared launcher),
-# plus platform-specific shortcuts (Linux .desktop / macOS .app bundle /
-# WSL Windows Desktop+Start Menu .lnk).
create_studio_shortcuts() {
_css_exe="$1"
_css_os="$2"
@@ -1110,17 +1074,7 @@ create_studio_shortcuts() {
mkdir -p "$_css_data_dir"
- # Same-install discriminator: per-install opaque id written once at install
- # time and read by both this launcher and the backend (/api/health). Replaces
- # the older sha256(canonical $STUDIO_HOME) scheme to (a) avoid leaking the
- # install path on -H 0.0.0.0 deployments and (b) sidestep launcher/backend
- # canonicalization drift (cd -P vs Path.resolve() symlink/junction handling).
- # Lives at $STUDIO_HOME/share/ (not $DATA_DIR) so the backend can find it
- # via _STUDIO_ROOT_RESOLVED / "share" / "studio_install_id" regardless of
- # mode (in env-mode $STUDIO_HOME/share == $DATA_DIR; in default mode they
- # diverge but the backend only knows the studio_root). 32 bytes of urandom
- # -> 64 hex chars, byte-compatible with the prior digest so launcher
- # placeholder, _check_health, and tests stay length-agnostic.
+ # Per-install opaque id for launcher + backend, under $STUDIO_HOME/share/ for studio_root.
_css_id_dir="$STUDIO_HOME/share"
mkdir -p "$_css_id_dir"
_css_id_file="$_css_id_dir/studio_install_id"
@@ -1183,8 +1137,6 @@ create_studio_shortcuts() {
[ "$_STUDIO_HOME_REDIRECT" = "env" ] && _css_is_env_mode=true
# ── Write launcher script ──
- # Single-quoted heredoc; @@DATA_DIR@@, @@STUDIO_ROOT_ID@@, and
- # @@INSTALLED_IS_ENV_MODE@@ are substituted via sed below.
cat > "$_css_launcher" << 'LAUNCHER_EOF'
#!/usr/bin/env bash
# Unsloth Studio Launcher
@@ -1543,20 +1495,15 @@ else
fi
LAUNCHER_EOF
- # why: bake non-user-controlled placeholders FIRST so a literal
- # `@@STUDIO_ROOT_ID@@` inside $DATA_DIR cannot be rewritten below.
+ # Bake fixed placeholders FIRST so a literal @@STUDIO_ROOT_ID@@ in $DATA_DIR survives.
sed -e "s|@@STUDIO_ROOT_ID@@|$_css_studio_root_id|g" \
-e "s|@@INSTALLED_IS_ENV_MODE@@|$_css_is_env_mode|g" \
"$_css_launcher" > "$_css_launcher.tmp" \
&& mv "$_css_launcher.tmp" "$_css_launcher"
- # Env-mode bakes an absolute DATA_DIR (root fixed at install time);
- # default / HOME-redirect keeps the literal $HOME/.local/share/unsloth
- # so behavior is byte-identical to pre-override.
+ # Env-mode bakes an absolute DATA_DIR; otherwise keep $HOME/.local/share/unsloth.
if [ "$_STUDIO_HOME_REDIRECT" = "env" ]; then
- # Two-stage escape: (1) `'` -> `'\''` for shell single-quote embedding,
- # (2) backslash/&/| escape so the value survives the s|...|VALUE| sed
- # below. Verified end-to-end with apostrophes, spaces, &, |, $.
+ # Two-stage escape: single-quote embedding, then backslash/&/| for the sed below.
_sq_escaped=$(printf '%s' "$DATA_DIR" | sed "s/'/'\\\\''/g")
_sed_safe=$(printf '%s' "$_sq_escaped" | sed 's/[\\&|]/\\&/g')
sed "s|@@DATA_DIR@@|$_sed_safe|g" "$_css_launcher" > "$_css_launcher.tmp" \
@@ -1569,16 +1516,11 @@ LAUNCHER_EOF
chmod +x "$_css_launcher"
- # studio.conf: exe path + (env-mode only) persisted env vars so fresh
- # shells launch the right install without re-exporting.
_css_quoted_exe=$(printf '%s' "$_css_exe" | sed "s/'/'\\\\''/g")
{
printf '%s\n' "UNSLOTH_EXE='$_css_quoted_exe'"
if [ "$_STUDIO_HOME_REDIRECT" = "env" ]; then
- # When an override resolves to the legacy default, llama.cpp
- # still lives at ~/.unsloth/llama.cpp (one shared build).
- # Canonicalize the legacy side so a symlinked $HOME doesn't
- # break the comparison.
+ # An override resolving to the legacy default shares ~/.unsloth/llama.cpp.
_css_legacy_studio="$HOME/.unsloth/studio"
if [ -d "$_css_legacy_studio" ]; then
_css_legacy_studio=$(CDPATH= cd -P -- "$_css_legacy_studio" 2>/dev/null && pwd -P) \
@@ -1592,8 +1534,7 @@ LAUNCHER_EOF
_css_quoted_home=$(printf '%s' "$STUDIO_HOME" | sed "s/'/'\\\\''/g")
_css_quoted_llama=$(printf '%s' "$_css_llama_path" | sed "s/'/'\\\\''/g")
printf '%s\n' "export UNSLOTH_STUDIO_HOME='$_css_quoted_home'"
- # UNSLOTH_LLAMA_CPP_PATH is a pre-existing user-controlled
- # llama.cpp dir override; only default it if unset.
+ # UNSLOTH_LLAMA_CPP_PATH is user-controlled; only default it if unset.
printf '%s\n' 'if [ -z "${UNSLOTH_LLAMA_CPP_PATH:-}" ]; then'
printf '%s\n' " export UNSLOTH_LLAMA_CPP_PATH='$_css_quoted_llama'"
printf '%s\n' 'fi'
@@ -1601,7 +1542,6 @@ LAUNCHER_EOF
} > "$_css_data_dir/studio.conf"
# ── Icon: try bundled, then download ──
- # rounded-512.png used for both Linux and macOS icons
_css_script_dir=""
if [ -n "${0:-}" ] && [ -f "$0" ]; then
_css_script_dir=$(cd "$(dirname "$0")" 2>/dev/null && pwd) || true
@@ -1670,9 +1610,7 @@ LAUNCHER_EOF
fi
# ── Platform-specific shortcuts ──
- # Env-mode installs are workspace-scoped: skip persistent desktop /
- # Start-Menu / dock launchers that may point at a deleted workspace.
- # Runtime launcher + studio.conf + icon are still written above.
+ # Env-mode is workspace-scoped: skip launchers that may point at a deleted workspace.
if [ "$_STUDIO_HOME_REDIRECT" = "env" ]; then
substep "wrote launcher at $_css_launcher (persistent shortcuts skipped in env-override mode)"
return 0
@@ -1783,9 +1721,7 @@ DESKTOP_EOF
</plist>
PLIST_EOF
- # Executable stub: same single-quoted-heredoc + sed-substitute
- # pattern as launch-studio.sh so $-vars in $_css_data_dir don't
- # expand at .app launch time.
+ # Single-quoted heredoc + sed so $-vars in $_css_data_dir do not expand at launch.
_css_sq_dir=$(printf '%s' "$_css_data_dir" | sed "s/'/'\\\\''/g")
_css_sed_dir=$(printf '%s' "$_css_sq_dir" | sed 's/[\\&|]/\\&/g')
cat > "$_css_macos_dir/launch-studio" << 'STUB_EOF'
@@ -1838,13 +1774,8 @@ STUB_EOF
elif [ "$_css_os" = "wsl" ]; then
# ── WSL: create Windows Desktop and Start Menu shortcuts ──
- # Detect current WSL distro for targeted shortcut
_css_distro="${WSL_DISTRO_NAME:-}"
- # Build the wsl.exe arguments.
- # Double-quote distro name and launcher path for Windows command line
- # parsing so values with spaces (e.g. "Ubuntu Preview") are kept as
- # single arguments.
_css_wsl_args=""
if [ -n "$_css_distro" ]; then
_css_wsl_args="-d \"$_css_distro\" "
@@ -1868,8 +1799,7 @@ STUB_EOF
# Escape single quotes for PowerShell single-quoted string embedding
_css_sc_args_ps=$(printf '%s' "$_css_sc_args" | sed "s/'/''/g")
- # DISTINCT shortcut name so the WSL launcher never clobbers a native
- # install's "Unsloth Studio.lnk" in the same folder. Per-distro suffix.
+ # Per-distro name so the WSL launcher never clobbers a native "Unsloth Studio.lnk".
if [ -n "$_css_distro" ]; then
_css_lnk_name="Unsloth Studio (WSL - ${_css_distro}).lnk"
else
@@ -1966,8 +1896,6 @@ WSLPS1_EOF
fi
rm -f "$_css_ps1_tmp"
fi
- # If WSL interop is disabled (powershell.exe "Exec format error"), the
- # shortcut wasn't created; tell the user how to launch / re-enable it.
if [ "$_css_created" -ne 1 ]; then
substep "Couldn't create the Windows shortcut (WSL interop may be disabled)." "$C_WARN"
substep " Launch Unsloth from Windows: wsl -d \"$_css_distro\" -- bash -lc 'unsloth studio'" "$C_WARN"
@@ -2018,8 +1946,7 @@ MAC_INTEL=false
# an Apple Silicon Mac here, not an Intel one.
_MAC_ROSETTA=false
if [ "$OS" = "macos" ] && [ "$_ARCH" = "x86_64" ]; then
- # Guard against Apple Silicon running under Rosetta (reports x86_64).
- # sysctl hw.optional.arm64 returns "1" on Apple Silicon even in Rosetta.
+ # Apple Silicon under Rosetta reports x86_64; hw.optional.arm64 stays "1".
if [ "$(sysctl -in hw.optional.arm64 2>/dev/null || echo 0)" = "1" ]; then
_MAC_ROSETTA=true
echo ""
@@ -2060,8 +1987,7 @@ fi
if [ "$OS" = "macos" ] && [ "$_ARCH" = "arm64" ]; then
_OVERRIDES_FILE="$(cd "$(dirname "$0" 2>/dev/null || echo ".")" && pwd)/studio/backend/requirements/single-env/overrides-darwin-arm64.txt"
if [ -f "$_OVERRIDES_FILE" ]; then
- # uv splits UV_OVERRIDE on whitespace, so a repo path with whitespace
- # truncates it and aborts every later uv call (issue #6503). Hand uv a copy.
+ # uv splits UV_OVERRIDE on whitespace; hand uv a copy in a whitespace-free temp dir.
case "$_OVERRIDES_FILE" in
*[[:space:]]*)
_UV_OVERRIDE_TMP_ROOT=${TMPDIR:-/tmp}
@@ -2093,9 +2019,7 @@ elif [ "$OS" = "macos" ]; then
fi
tauri_diag_marker "$_TAURI_INITIAL_GPU_BRANCH" "none"
-# AMD GPU name from the Windows host via WMI, or empty. Discrete cards aren't in
-# /proc/cpuinfo, so ask Windows. Cached ("-" = negative), self-contained, bounded
-# to 10s. Defined here so the reroute below can use it before _run_bounded exists.
+# AMD GPU name from the Windows host via WMI (discrete cards are not in /proc/cpuinfo).
_WSL_AMD_GPU_NAME_CACHE=""
_wsl_amd_gpu_name() {
if [ -n "$_WSL_AMD_GPU_NAME_CACHE" ]; then
@@ -2114,11 +2038,6 @@ _wsl_amd_gpu_name() {
}
# ── Bounded command runner ──
-# Runs a command under a 10s timeout when the `timeout` binary is available,
-# otherwise runs it unbounded. Keeps a wedged nvidia-smi (blocking during
-# driver init or after a reset) from hanging the installer: a timed-out probe
-# exits nonzero and is treated exactly like a failed probe. No-op semantics on
-# hosts without `timeout` (e.g. macOS) or when the probe is healthy.
_run_bounded() {
if command -v timeout >/dev/null 2>&1; then
timeout 10 "$@"
@@ -2127,10 +2046,7 @@ _run_bounded() {
fi
}
-# Returns 0 (true) when CUDA_VISIBLE_DEVICES is set to "" or "-1", i.e. every
-# NVIDIA device is deliberately hidden (mixed AMD+NVIDIA hosts steering work to
-# the AMD card). Unset means all devices visible. nvidia-smi ignores this env
-# var, so the probes below cannot see the distinction on their own.
+# True when CUDA_VISIBLE_DEVICES is "" or "-1"; nvidia-smi ignores it.
_cvd_hides_nvidia() {
[ "${CUDA_VISIBLE_DEVICES+set}" = "set" ] || return 1
_cvd_trim=$(printf '%s' "$CUDA_VISIBLE_DEVICES" | tr -d '[:space:]')
@@ -2138,13 +2054,7 @@ _cvd_hides_nvidia() {
}
# ── NVIDIA usable-GPU helper ──
-# Returns 0 (true) if an NVIDIA GPU is present and usable.
-# Primary probe: nvidia-smi -L. Fallback: /proc/driver/nvidia/gpus/ sysfs,
-# which the NVIDIA driver populates on Linux regardless of nvidia-smi state
-# -- handles PATH gaps, subprocess timeouts, and driver init races that
-# could otherwise cause nvidia-smi to fail and silence NVIDIA detection.
-# A GPU hidden via CUDA_VISIBLE_DEVICES=""/-1 counts as NOT usable (matches
-# install_llama_prebuilt.py has_usable_nvidia), so AMD/CPU routing still runs.
+# nvidia-smi -L primary, /proc/driver/nvidia/gpus/ fallback; a hidden GPU is NOT usable.
_has_usable_nvidia_gpu() {
if _cvd_hides_nvidia; then
return 1
@@ -2160,7 +2070,6 @@ _has_usable_nvidia_gpu() {
return 0
fi
fi
- # Fallback: NVIDIA driver exposes one subdir per GPU under this path.
if [ -d /proc/driver/nvidia/gpus ] && \
[ -n "$(ls -A /proc/driver/nvidia/gpus 2>/dev/null)" ]; then
return 0
@@ -2168,25 +2077,16 @@ _has_usable_nvidia_gpu() {
return 1
}
-# Strix Halo ROCm-on-WSL only targets Ubuntu 24.04. On a newer distro (e.g. 26.04)
-# with a 24.04 distro present, re-run the install there and stop; else fall through
-# to CPU + the `wsl --install` hint below (never auto-create a distro). Runs before
-# the STUDIO_HOME mkdir/venv so the origin distro is untouched.
+# Strix Halo ROCm-on-WSL needs Ubuntu 24.04: re-run in one, else CPU. Never create a distro.
_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).
+ # An explicit index pin skips every GPU-driven reroute; whitespace-only does not gate.
_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
[ -e /dev/dxg ] || return 0
- # A usable NVIDIA GPU (common on hybrid AMD+NVIDIA hosts) means the CUDA path works on
- # this distro, so don't reroute for AMD. _has_usable_nvidia_gpu (moved above) honors
- # CUDA_VISIBLE_DEVICES=""/-1 and the /proc/driver/nvidia fallback for PATH/timeout gaps.
if _has_usable_nvidia_gpu; then return 0; fi
# Strix APUs show in /proc/cpuinfo; discrete cards don't, so also try WMI. Either reroutes.
if ! grep -qiE 'Ryzen AI Max|Radeon 80[0-9][05]S|Strix Halo' /proc/cpuinfo 2>/dev/null \
@@ -2199,18 +2099,10 @@ _maybe_reroute_strixhalo_to_2404() {
fi
_rr_ver=""
[ -r /etc/os-release ] && _rr_ver=$(. /etc/os-release 2>/dev/null; printf '%s' "${VERSION_ID:-}")
- # The bootstrap (scripts/install_rocm_wsl_strixhalo.sh) dies on any VERSION_ID but
- # 24.04 and pins the noble repo, so 24.04 is the sole GPU-supported target; leave a
- # 24.04 user alone. (Working ROCm on other versions was caught by librocdxg above.)
case "$_rr_ver" in 24.04) return 0 ;; esac
- # Distro is now unsupported. If we can't reroute to a 24.04 target, stay CPU-only
- # AND skip the later origin-distro ROCm bootstrap (it ignores distro version, so it
- # would otherwise install ROCm into 26.04 etc.).
+ # Without a 24.04 reroute target, stay CPU-only AND skip the origin-distro ROCm bootstrap.
command -v wsl.exe >/dev/null 2>&1 || { UNSLOTH_SKIP_ROCM_WSL_SETUP=1; return 0; }
- # Route only to an installed Ubuntu-24.04 (bootstrap's only target). Match the whole
- # line (one distro per line from wsl.exe -l -q), not a substring, so "Ubuntu-24.04-test"
- # can't masquerade as it and then fail `wsl -d`.
- # || true: no match is expected, not an error (script runs under set -e).
+ # Whole-line match so "Ubuntu-24.04-test" cannot masquerade; no match is fine.
_rr_distros=$(wsl.exe -l -q 2>/dev/null | tr -d '\000\r')
_rr_target=$(printf '%s\n' "$_rr_distros" | grep -ixF "Ubuntu-24.04" | head -n1) || true
[ -n "$_rr_target" ] || {
@@ -2229,20 +2121,13 @@ _maybe_reroute_strixhalo_to_2404() {
substep "This is a --local install; re-run it from $_rr_target instead:" "$C_WARN"
substep " wsl -d $_rr_target -- bash -lc 'cd <your checkout> && ./install.sh --local'" "$C_WARN"
substep "Continuing CPU-only in Ubuntu ${_rr_ver:-this distro} for now." "$C_WARN"
- # Unsupported distro, can't reroute a --local checkout: skip the origin ROCm bootstrap.
UNSLOTH_SKIP_ROCM_WSL_SETUP=1
return 0
fi
- # Forward the caller's options/env (custom package/python/home) so the rerouted
- # install matches what was asked for, not a default install.
_rr_q() { printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"; }
_rr_exports="set -o pipefail; export UNSLOTH_WSL_REROUTED=1"
[ "$_STUDIO_HOME_REDIRECT" = "env" ] && _rr_exports="$_rr_exports; export UNSLOTH_STUDIO_HOME=$(_rr_q "$STUDIO_HOME")"
- # 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"
@@ -2265,17 +2150,14 @@ _maybe_reroute_strixhalo_to_2404() {
if [ "$_rr_rc" -eq 0 ]; then
exit 0
fi
- # In Tauri mode the child uses exit 2 ([TAURI:NEED_SUDO]) to ask the desktop app to
- # elevate for the target distro; the child already printed the NEED_SUDO line, so
- # propagate the code instead of masking it as a reroute failure and dropping to CPU.
+ # Tauri child exit 2 ([TAURI:NEED_SUDO]) asks the desktop app to elevate.
if [ "$TAURI_MODE" = true ] && [ "$_rr_rc" -eq 2 ]; then
exit 2
fi
substep "Could not auto-continue in $_rr_target; run it yourself:" "$C_WARN"
substep " wsl -d $_rr_target -- bash -lc 'curl -fsSL https://unsloth.ai/install.sh | sh'"
substep "Continuing CPU-only in Ubuntu ${_rr_ver:-this distro} for now." "$C_WARN"
- # Reroute failed; don't let the later bootstrap install ROCm into this unsupported
- # distro -- stay CPU-only.
+ # Reroute failed; keep the later bootstrap from installing ROCm into this distro.
UNSLOTH_SKIP_ROCM_WSL_SETUP=1
return 0
}
@@ -2456,25 +2338,16 @@ UV_MIN_VERSION="0.9.3"
# missing flags it is about to be handed (--default-index, --torch-backend).
UV_OFFLINE_MIN_VERSION="0.8.16"
-# When bytecode compilation is enabled, large installs can exceed uv's 60s default on slow machines. Default to 180s, preserving overrides ("0" disables).
+# Large bytecode-compiled installs can exceed uv's 60s default; use 180s ("0" disables).
: "${UV_COMPILE_BYTECODE_TIMEOUT:=180}"
export UV_COMPILE_BYTECODE_TIMEOUT
-# uv >= 0.8.16 retries HTTP/2 streaming body errors; raise retries and read
-# timeout for large wheel downloads. ":=" preserves any user override.
: "${UV_HTTP_RETRIES:=5}"
export UV_HTTP_RETRIES
: "${UV_HTTP_TIMEOUT:=180}"
export UV_HTTP_TIMEOUT
-# macOS: trust the system Keychain so uv uses SecureTransport instead of rustls.
-# Required behind TLS-inspecting proxies (Cisco Umbrella, Zscaler, etc.) which
-# present their own CA certificate. rustls (uv's default) ignores the Keychain
-# and rejects intercepted connections with "invalid peer certificate: UnknownIssuer".
-# Set both vars: UV_SYSTEM_CERTS is the modern one (uv >= 0.11), UV_NATIVE_TLS the
-# legacy one understood by uv 0.8.16-0.10.x, which the installer keeps if already
-# present (UV_MIN_VERSION) and which ignores UV_SYSTEM_CERTS. Mirror the choice onto
-# both so it works on either uv. Opt out with UV_SYSTEM_CERTS=0.
+# macOS: trust the Keychain via UV_SYSTEM_CERTS (uv >= 0.11) / UV_NATIVE_TLS; 0 opts out.
if [ "$OS" = "macos" ]; then
: "${UV_SYSTEM_CERTS:=1}"
: "${UV_NATIVE_TLS:=$UV_SYSTEM_CERTS}"
@@ -2886,7 +2759,8 @@ if [ -x "$VENV_DIR/bin/python" ] || _dir_has_entries "$VENV_DIR"; then
_PREV_TORCH_VER=$(sed -n "s/^__version__ = '\([^']*\)'.*/\1/p" "$_prev_tv" | head -n 1)
break
done
- [ -n "$_PREV_TORCH_VER" ] || _PREV_TORCH_VER=$("$VENV_DIR/bin/python" -c \
+ # _run_bounded the fallback: without version.py it hits `import torch`, which can wedge.
+ [ -n "$_PREV_TORCH_VER" ] || _PREV_TORCH_VER=$(_run_bounded "$VENV_DIR/bin/python" -c \
"import torch; print(torch.__version__)" 2>/dev/null | tail -n 1 || true)
# New layout already exists — replace only after preserving rollback copy.
substep "preserving existing environment for rollback..."
@@ -2898,10 +2772,7 @@ if [ -x "$VENV_DIR/bin/python" ] || _dir_has_entries "$VENV_DIR"; then
exit 1
fi
elif [ "$_STUDIO_HOME_REDIRECT" != "env" ] && [ -x "$STUDIO_HOME/.venv/bin/python" ]; then
- # Old layout exists — validate before migrating.
- # Skip in env-mode so we don't rm -rf an unrelated .venv at the
- # workspace root (e.g. user's existing project Python venv).
- # In no-torch mode, a missing torch package is expected; validate Python only.
+ # Old layout: validate before migrating (env-mode skips it); no-torch checks Python only.
substep "found legacy Unsloth environment, validating..."
_legacy_ok=false
if [ "$SKIP_TORCH" = true ]; then
@@ -2944,8 +2815,6 @@ torch.testing.assert_close(torch.unique(E), torch.tensor((20,), device=E.device,
fi
fi
-# If an Intel Mac has a stale 3.13 venv from a previous failed install, recreate
-# (skip when the user explicitly chose a version via --python)
if [ "$SKIP_TORCH" = true ] && [ "$MAC_INTEL" = true ] && [ -z "$_USER_PYTHON" ] && [ -x "$VENV_DIR/bin/python" ]; then
_PY_MM=$("$VENV_DIR/bin/python" -c \
"import sys; print('{}.{}'.format(*sys.version_info[:2]))" 2>/dev/null || echo "")
@@ -3103,26 +2972,12 @@ if [ ! -x "$VENV_DIR/bin/python" ]; then
fi
fi
-# Mark the freshly-created venv as Unsloth-owned so a partial install can be
-# repaired by re-running install.sh; the env-mode deletion guard above accepts
-# this marker as the primary sentinel.
+# Mark the freshly-created venv as Unsloth-owned (env-mode deletion guard's primary sentinel).
if [ -x "$VENV_DIR/bin/python" ]; then
: > "$VENV_DIR/.unsloth-studio-owned" 2>/dev/null || true
fi
-# Guard against two independent Apple Silicon venv problems, in order:
-# 1. uv may create the venv from a cached x86_64 (Rosetta) Python when a
-# same-version x86_64 build is already cached (often because uv itself
-# is an x86_64 build). That venv reports x86_64 to wheel resolvers, and
-# PyTorch ships no macOS wheels on the CPU index for any architecture,
-# so the torch install can never resolve. Recreate it with an
-# arch-explicit arm64 CPython.
-# 2. Python 3.13.8 has a known torch import bug.
-# The two are independent: a venv may be x86_64 and, once recreated, still
-# land on 3.13.8. So we re-inspect the interpreter between the checks instead
-# of chaining them with elif, guaranteeing both invariants hold on whatever
-# venv we end up with. Skip both when the user explicitly chose an interpreter
-# via --python.
+# Two independent Apple Silicon venv guards (Rosetta x86_64, the 3.13.8 torch bug); re-inspect, not elif.
if [ -z "$_USER_PYTHON" ] && [ "$OS" = "macos" ] && [ "$_ARCH" = "arm64" ]; then
_inspect_venv() {
"$VENV_DIR/bin/python" -c \
@@ -3132,10 +2987,7 @@ if [ -z "$_USER_PYTHON" ] && [ "$OS" = "macos" ] && [ "$_ARCH" = "arm64" ]; then
_info=$(_inspect_venv)
_VENV_ARCH=${_info%% *}
_PY_VER=${_info##* }
- # If the interpreter could not be executed (an x86_64 venv python on a Mac
- # without Rosetta installed), the probe above yields an empty arch. Fall
- # back to reading the binary's Mach-O arch statically so the x86_64
- # recreate below still triggers instead of letting uv fail later.
+ # An unexecutable x86_64 venv python (no Rosetta) yields no arch; read the Mach-O header.
if [ -z "$_VENV_ARCH" ] && [ -x "$VENV_DIR/bin/python" ]; then
# uv symlinks bin/python to the base interpreter, so dereference with
# file -L (lipo already follows the link). Trailing || true keeps the
@@ -3204,25 +3056,23 @@ if [ -x "$VENV_DIR/bin/python" ]; then
substep "${VENV_DIR}"
fi
-# Default torch constraint -- tightened for Python 3.13+ on arm64 macOS
-# (torch <2.6 has no cp313 macOS arm64 wheels)
-TORCH_CONSTRAINT="torch>=2.4,<2.11.0"
+# Default range admits torch 2.11 (verified on cpu/cu126/cu128/cu130/rocm7.1+/mac arm64).
+# Bump all three ceilings when the next minor is validated; the curated ROCm floors below stay literal.
+_TORCH_CEILING="2.12.0"
+_TORCHVISION_CEILING="0.27.0"
+_TORCHAUDIO_CEILING="2.12.0"
+# Default torch constraint; tightened for Python 3.13+ on arm64 macOS (no cp313 wheels below 2.6).
+TORCH_CONSTRAINT="torch>=2.4,<${_TORCH_CEILING}"
if [ "$SKIP_TORCH" = false ] && [ "$OS" = "macos" ] && [ "$_ARCH" = "arm64" ]; then
_PY_MINOR=$("$VENV_DIR/bin/python" -c \
"import sys; print(sys.version_info.minor)" 2>/dev/null || echo "0")
if [ "$_PY_MINOR" -ge 13 ] 2>/dev/null; then
- TORCH_CONSTRAINT="torch>=2.6,<2.11.0"
+ TORCH_CONSTRAINT="torch>=2.6,<${_TORCH_CEILING}"
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"
+# Companions bounded to torch's window: torchaudio 2.11 dropped its torch pin, so it can drift.
+TORCHVISION_CONSTRAINT="torchvision>=0.19,<${_TORCHVISION_CEILING}"
+TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<${_TORCHAUDIO_CEILING}"
# ── Resolve repo root (for --local installs) ──
_REPO_ROOT="$(cd "$(dirname "$0" 2>/dev/null || echo ".")" && pwd)"
@@ -3252,10 +3102,7 @@ _find_no_torch_runtime() {
}
# ── AMD ROCm GPU detection helper ──
-# WSL2 ROCDXG: the system rocminfo enumerates the GPU over /dev/dxg only when
-# HSA_ENABLE_DXG_DETECTION=1 (a no-op on bare metal), and /opt/rocm/bin can be
-# off PATH outside login shells (the profile.d drop-in). Seed both before any
-# rocminfo probe or a ROCDXG WSL host is misdetected as CPU-only.
+# WSL2 ROCDXG: rocminfo needs HSA_ENABLE_DXG_DETECTION=1 and /opt/rocm/bin on PATH; seed both.
_ensure_rocm_probe_env() {
export HSA_ENABLE_DXG_DETECTION="${HSA_ENABLE_DXG_DETECTION:-1}"
if ! command -v rocminfo >/dev/null 2>&1 && [ -x /opt/rocm/bin/rocminfo ]; then
@@ -3263,12 +3110,6 @@ _ensure_rocm_probe_env() {
fi
}
-# Returns 0 if an AMD GPU is present. Checks rocminfo, amd-smi, then sysfs
-# KFD topology (env-var-independent fallback for when HIP/ROCR_VISIBLE_DEVICES hides devices).
-# Always returns 1 (false) when an NVIDIA GPU is present: blocks every
-# detection path (rocminfo, amd-smi, KFD sysfs) from producing a false
-# positive on NVIDIA-only or NVIDIA-primary hosts, even when ROCm tools
-# are co-installed.
_has_amd_rocm_gpu() {
_ensure_rocm_probe_env
if _has_usable_nvidia_gpu; then
@@ -3850,14 +3691,11 @@ _detect_rocm_version_tag() {
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.
+ # An explicit override skips ALL GPU probing: the URL is verbatim, _FAMILY is its leaf.
_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).
+ # Trim trailing path slashes (they 404 on strict proxies), keeping ?query/#fragment.
_url=$(_trim_index_path_slashes "$_url")
echo "$_url"; return
fi
@@ -3870,10 +3708,7 @@ get_torch_index_url() {
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.
- # Presence of the binary alone (container leftovers, stale driver
- # packages) is not sufficient: otherwise an AMD-only host would
- # silently install CUDA wheels.
+ # Require nvidia-smi to list a usable GPU; the binary alone installs CUDA wheels on AMD.
_smi=""
_nvidia_detected=0
if _has_usable_nvidia_gpu; then
@@ -3885,10 +3720,6 @@ get_torch_index_url() {
fi
fi
if [ "$_nvidia_detected" -eq 0 ]; then
- # No NVIDIA GPU -- check for AMD ROCm GPU.
- # PyTorch only publishes ROCm wheels for linux-x86_64; skip the
- # ROCm branch entirely on aarch64 / arm64 / other architectures
- # so non-x86_64 Linux hosts fall back cleanly to CPU wheels.
case "$(uname -m)" in
x86_64|amd64) : ;;
*) echo "$_base/cpu"; return ;;
@@ -3946,7 +3777,6 @@ get_torch_index_url() {
*) _rocm_tag="" ;; # reject malformed (empty, garbled, or major=0)
esac
if [ -n "$_rocm_tag" ]; then
- # Minimum supported: ROCm 6.0 (no PyTorch wheels exist for older)
case "$_rocm_tag" in
rocm[1-5].*)
echo "[WARN] ROCm $_rocm_tag detected but PyTorch ROCm wheels require ROCm 6.0+ -- falling back to CPU-only PyTorch" >&2
@@ -3957,9 +3787,7 @@ get_torch_index_url() {
echo "[WARN] UNSLOTH_TORCH_INDEX_URL=<full index URL> (takes precedence, used verbatim)" >&2
echo "$_base/cpu"; return ;;
esac
- # Supported tags; 6.5+ clips to rocm6.4, 7.3+ caps to rocm7.2.
- # PyTorch publishes major.minor URLs only (no patch level), so
- # rocm7.2.1 / rocm6.0.2 / etc. must normalise to rocm7.2 / rocm6.0.
+ # Normalise to major.minor; 6.5+ clips to rocm6.4, 7.3+ caps to rocm7.2.
case "$_rocm_tag" in
rocm6.0|rocm6.0.*) echo "$_base/rocm6.0" ;;
rocm6.1|rocm6.1.*) echo "$_base/rocm6.1" ;;
@@ -3970,11 +3798,8 @@ get_torch_index_url() {
rocm7.1|rocm7.1.*) echo "$_base/rocm7.1" ;;
rocm7.2|rocm7.2.*) echo "$_base/rocm7.2" ;;
rocm6.*)
- # ROCm 6.5+ (no published PyTorch wheels): clip down
- # to the last supported 6.x wheel set.
echo "$_base/rocm6.4" ;;
*)
- # ROCm 7.3+ (future): cap to rocm7.2 (latest known)
echo "$_base/rocm7.2" ;;
esac
return
@@ -4011,15 +3836,7 @@ get_torch_index_url() {
echo "[WARN] Version sources checked: amd-smi, /opt/rocm/.info/version, hipconfig, dpkg, rpm (Debian runtime package: libhsa-runtime64-1)." >&2
echo "$_base/cpu"; return
fi
- # Parse CUDA version from nvidia-smi output (POSIX-safe, no grep -P).
- # Newer NVIDIA drivers (e.g. 610.x) print "CUDA UMD Version: X.Y" instead
- # of the legacy "CUDA Version: X.Y"; accept both with two BRE expressions
- # (POSIX sed does not support "?" without -E). The two patterns are
- # mutually exclusive per line, so head -1 picks the first emitted match.
- # Bound the call (a wedged nvidia-smi would otherwise hang here) and force
- # the C locale for stable parsing. LC_ALL is exported inside this command
- # substitution subshell so it reaches nvidia-smi through _run_bounded
- # without depending on `env`; the export is scoped to the subshell.
+ # CUDA version from nvidia-smi: accept "CUDA Version:" and the newer "CUDA UMD Version:".
_cuda_ver=$(export LC_ALL=C; _run_bounded "$_smi" 2>/dev/null \
| sed -n \
-e 's/.*CUDA UMD Version:[[:space:]]*\([0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' \
@@ -4055,29 +3872,23 @@ _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.
+# Lowercased final path segment of an index URL, query/fragment stripped. Shared with py / ps1.
_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.
+ # Strip ALL trailing slashes: .../rocm7.2// must yield rocm7.2, not empty.
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<digits>[.<digits>]
-# 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.
+# True for an EXACT ROCm family leaf; one that merely starts with rocm/gfx is a custom pin.
_is_pip_rocm_family_leaf() {
case "$1" in
gfx[0-9]*) return 0 ;;
rocm[0-9]*)
- # Exact rocm<digits>[.<digits>]: both major and minor must be non-empty all-digits
- # (rocm7., rocm7.2.1, rocm7.2-private are all custom pins, not a family).
+ # Major/minor both all-digits (rocm7., rocm7.2.1, rocm7.2-private are custom).
_rocm_rest="${1#rocm}"
case "$_rocm_rest" in
*.*.*) return 1 ;;
@@ -4094,11 +3905,6 @@ _is_pip_rocm_family_leaf() {
esac
}
-# Whether release base $1 (X.Y[.Z...]) falls inside constraint window $2
-# ("torch>=A.B[.C],<D.E.F"). Compares at major.minor granularity, which is exact
-# for the windows this script uses (ceilings are always X.Y.0); a non-.0 ceiling
-# would only make this conservative (excludes the whole ceiling minor). Anything
-# unparseable answers "no" so the caller fails toward the supported range.
_torch_release_in_window() {
_trw_con="$2"
case "$_trw_con" in
@@ -4122,26 +3928,14 @@ _torch_release_in_window() {
echo "no"
}
-# Keep the previous venv's torch on a re-run: echo "torch==X.Y.Z" when the probed
-# version ($1) is inside the active constraint window ($2), else "". The RELEASE is kept
-# regardless of flavor tag; the pin installs from the freshly chosen index, so flavor
-# follows the machine (cpu <-> cuda, cu126 -> cu130, PyPI bare -> +cu130) while the
-# release follows the user. Gating on flavor was wrong: a PyPI torch reports a BARE
-# version (on Linux the PyPI wheel IS CUDA), misclassified "cpu", so a healthy 2.10 on a
-# cu130 host was moved to 2.11. Per-leaf floors still win (rocm7.2 / gfx >=2.11 for the
-# Strix _grouped_mm fix, out-of-window manual installs) and are never pinned; the caller's
-# _PREV_FALLBACK_CONSTRAINT installs the newest supported release when the index lacks the
-# exact one. Opt out with UNSLOTH_TORCH_UPGRADE=1.
+# Keep the previous torch RELEASE when inside the window; UNSLOTH_TORCH_UPGRADE=1 opts out.
_previous_torch_pin() {
_ptp_ver="$1"
_ptp_con="$2"
[ -n "$_ptp_ver" ] || { echo ""; return; }
[ "${UNSLOTH_TORCH_UPGRADE:-0}" = "1" ] && { echo ""; return; }
_ptp_base="${_ptp_ver%%+*}"
- # Base must be a plain numeric release (X.Y[.Z]); probe noise and
- # nightly/dev/source builds (2.11.0.dev20250704, 2.9.0a0) must never
- # become a pin -- no stable index carries them, so pinning would only
- # print "keeping it" and then burn a doomed resolve before falling back.
+ # Base must be a plain numeric release; nightly/dev builds never become a pin.
case "$_ptp_base" in
*[!0-9.]* | *..* | .* | *.) echo ""; return ;;
[0-9]*.[0-9]*) ;;
@@ -4151,16 +3945,9 @@ _previous_torch_pin() {
echo "torch==$_ptp_base"
}
-# Install torch from TORCH_INDEX_URL honoring a kept-release pin: with _PREV_TORCH_PIN
-# set, TORCH_CONSTRAINT is the exact previous release; fall back to the supported range
-# if the index lacks it (pruned mirror) rather than failing. Used by every --default-index
-# path (NVIDIA cu*, AMD rocm/gfx fallbacks, cpu/mac, ROCm repairs) so preservation is
-# uniform. Extra args (e.g. --force-reinstall) are passed through to uv.
_install_torch_default_index() {
if [ -n "$_PREV_TORCH_PIN" ]; then
- # Pair the companions with the kept torch minor: torchaudio no longer
- # exact-pins torch in its metadata, so leaving it unconstrained resolves
- # a newer mismatched build (a kept torch 2.9.0 pulled torchaudio 2.11.0).
+ # Pair companions with the kept torch minor (torchaudio no longer exact-pins torch).
_itdi_base="${_PREV_TORCH_PIN#torch==}"
_itdi_minor="${_itdi_base#*.}"
_itdi_minor="${_itdi_minor%%.*}"
@@ -4192,8 +3979,7 @@ _expected_torch_flavor_tag() {
_leaf=$(_torch_index_url_leaf "$1")
case "$_leaf" in
cu[0-9]*)
- # Exact cu + digits only; a cu*-suffixed leaf (cu128-private) -> "" (custom),
- # else a correct +cu128 wheel is force-reinstalled every run.
+ # Exact cu + digits only; a cu128-private leaf is custom.
case "${_leaf#cu}" in
*[!0-9]*) echo "" ;;
*) echo "$_leaf" ;;
@@ -4242,8 +4028,7 @@ _torch_index_repairable() {
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.
+# Drops userinfo AND query/fragment. Shared with py / ps1.
_strip_index_url_credentials() {
_sic_url="$1"
case "$_sic_url" in
@@ -4317,16 +4102,11 @@ get_radeon_wheel_url() {
}
# ── Radeon repo wheel selection helpers ──────────────────────────────────────
-# Fetches the Radeon repo directory listing once into _RADEON_LISTING (global).
-# _RADEON_PYTAG holds the CPython tag for the running interpreter (e.g. cp312).
-# _RADEON_BASE_URL holds the base URL for relative-href resolution.
_RADEON_LISTING=""
_RADEON_PYTAG=""
_RADEON_BASE_URL=""
_radeon_fetch_listing() {
- # Usage: _radeon_fetch_listing BASE_URL
- # Populates _RADEON_LISTING, _RADEON_PYTAG, _RADEON_BASE_URL.
_RADEON_BASE_URL="$1"
_RADEON_PYTAG=$("$_VENV_PY" -c "
import sys
@@ -4341,15 +4121,6 @@ print('cp{}{}'.format(sys.version_info.major, sys.version_info.minor))
}
_pick_radeon_wheel() {
- # Usage: _pick_radeon_wheel PACKAGE_NAME [VERSION_PREFIX]
- # Scans $_RADEON_LISTING for the newest wheel whose filename starts exactly
- # with PACKAGE_NAME- (and optionally VERSION_PREFIX) and matches _RADEON_PYTAG + linux_x86_64.
- # Prints the full URL (resolving relative hrefs against _RADEON_BASE_URL).
- #
- # POSIX-compliant pipeline: all href parsing, filtering, and version
- # selection is done inside a single awk script rather than reaching
- # for GNU extensions (grep -o, sort -V) that would break under BSD
- # or BusyBox coreutils.
_pkg="$1"
_ver_prefix="${2:-}"
[ -n "$_RADEON_LISTING" ] || return 1
@@ -4403,16 +4174,7 @@ _pick_radeon_wheel() {
}
# ── ROCm-on-WSL bootstrap for AMD Strix Halo (gfx1151) ───────────────────────
-# No-op everywhere except: WSL + GPU wanted + no usable GPU yet + /dev/dxg +
-# Strix Halo APU. Every other config (NVIDIA, native-Linux ROCm, macOS, Windows,
-# CPU, non-Strix WSL) skips it and normal detection runs unchanged. NEVER aborts
-# the installer -- always returns 0. Runs the idempotent helper (ROCm 7.2 +
-# librocdxg), then sources the env it persisted so detection finds the GPU.
-# Export the ROCm-on-WSL env into this process and persist it to /etc/profile.d
-# so non-login Unsloth/llama launches inherit it. Idempotent (writes only when
-# the drop-in is missing); no-op without librocdxg, so never fires off WSL.
-# /etc/profile.d is root-owned -- sudo-tee when not root, else ROCm vanishes
-# after this shell on a non-root reinstall. Best-effort either way.
+# Idempotent, no-op without librocdxg, best-effort; sudo-tee when not root.
_persist_rocm_wsl_dropin() {
[ -e /opt/rocm/lib/librocdxg.so ] || [ -e /opt/rocm/lib64/librocdxg.so ] || return 0
_rw_rocm=/opt/rocm
@@ -4439,23 +4201,15 @@ _persist_rocm_wsl_dropin() {
fi
}
-# _wsl_amd_gpu_name is defined earlier so both the reroute and this bootstrap can use it.
_maybe_bootstrap_rocm_wsl() {
[ "${OS:-}" = "wsl" ] || return 0
[ "${SKIP_TORCH:-false}" = "false" ] || return 0
[ "${UNSLOTH_SKIP_ROCM_WSL_SETUP:-0}" = "1" ] && return 0
- # Leave any already-usable GPU completely alone (NVIDIA, or working ROCm).
if _has_usable_nvidia_gpu; then return 0; fi
- # Usable ROCm = rocminfo enumerates a real GPU agent: gfx[1-9] (excludes gfx000,
- # the CPU agent) and not the "gfx11-generic" fallback. awk consumes all input so
- # rocminfo isn't SIGPIPE'd like `grep -q` under pipefail.
+ # Usable ROCm = a real gfx[1-9] agent, not gfx000; awk drains input so rocminfo survives.
_ensure_rocm_probe_env
if command -v rocminfo >/dev/null 2>&1 && \
rocminfo 2>/dev/null | awk '/Name:[[:space:]]*gfx[1-9]/ && !/generic/{found=1} END{exit !found}'; then
- # rocminfo may work only via the transient env _ensure_rocm_probe_env
- # just set, which dies with the installer. Persist the drop-in so login
- # shells (Unsloth, llama.cpp) inherit it -- else a reinstall over an
- # existing /opt/rocm (uninstall keeps ROCm but drops it) loses the GPU.
_persist_rocm_wsl_dropin
return 0
fi
@@ -4469,15 +4223,12 @@ _maybe_bootstrap_rocm_wsl() {
fi
command -v bash >/dev/null 2>&1 || return 0
- # Fast path: already configured (librocdxg present) but launched from a
- # non-login shell so the persisted env wasn't loaded -- just load it.
if [ -e /opt/rocm/lib/librocdxg.so ] || [ -e /opt/rocm/lib64/librocdxg.so ]; then
if [ -r /etc/profile.d/unsloth-rocm-wsl.sh ]; then
# shellcheck disable=SC1091
. /etc/profile.d/unsloth-rocm-wsl.sh || true
else
- # librocdxg present but the env drop-in is gone (e.g. an Unsloth
- # uninstall removed it while keeping shared ROCm). Restore the env.
+ # librocdxg present but the env drop-in is gone (uninstall dropped it).
_persist_rocm_wsl_dropin
fi
return 0
@@ -4571,10 +4322,7 @@ _maybe_bootstrap_rocm_wsl() {
[ -n "$_rw_tmp" ] && rm -f "$_rw_tmp"
return 0
}
-# 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.
+# A pinned wheel index skips the WSL ROCm bootstrap and the Radeon/Strix reroute below.
_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:]]}"}"
@@ -4751,7 +4499,6 @@ fi
# 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
@@ -4772,8 +4519,7 @@ case "$_torch_index_leaf" in
rocm*|gfx*) export UNSLOTH_TORCH_BACKEND="rocm" ;;
cpu) export UNSLOTH_TORCH_BACKEND="cpu" ;;
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.
+ # Unknown leaf: unset so a stale value cannot leak and the stack probes the GPU.
*) unset UNSLOTH_TORCH_BACKEND ;;
esac
@@ -4802,24 +4548,13 @@ 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.
+# rocm7.2 and per-gfx indexes ship torch 2.11.0: raise the floor, matching the FINAL leaf only.
case "$_torch_index_leaf" in
rocm7.2|gfx120x-all|gfx1151|gfx1150|gfx1152)
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"
- ;;
# Floor 2.6, not the generic 2.4: unsloth/models/_utils.py raises at import for an XPU
# device below it, so a mirror serving an older +xpu wheel would install something that
# cannot run. Reached only through an explicit pin (no Intel autodetect on this side).
@@ -4830,21 +4565,6 @@ case "$_torch_index_leaf" in
;;
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
# On the LEAF, like every other index classifier here: the AMD per-arch mirror is
@@ -4971,14 +4691,8 @@ case "$_torch_index_leaf" in
echo " [WARN] _grouped_mm segfault, moe_utils.py:167, and later Strix kernel bugs)," >&2
echo " [WARN] and is more reliable than the rocm7.2 index or an offline Radeon repo." >&2
echo "" >&2
- # AMD's arch-specific index serves torch 2.11.0+rocm7.13.0 which has AMD's
- # actual fix for the gfx1151/gfx1150 _grouped_mm kernel bug -- preferred
- # over the pytorch.org rocm7.2 fallback because it exercises the real GPU
- # kernel path. Set UNSLOTH_AMD_ROCM_MIRROR to override for air-gapped installs.
+ # AMD's arch index carries the real _grouped_mm fix (torch 2.11.0+rocm7.13.0).
_amd_strix_base="${UNSLOTH_AMD_ROCM_MIRROR:-https://repo.amd.com/rocm/whl}"
- # Strip ALL trailing slashes to match Python's .rstrip("/") -- a
- # double-/triple-slash mirror URL would otherwise produce 404s on
- # strict pip proxies (artifactory, sonatype).
while [ "${_amd_strix_base%/}" != "$_amd_strix_base" ]; do
_amd_strix_base="${_amd_strix_base%/}"
done
@@ -5050,8 +4764,7 @@ case "$_torch_index_leaf" in
_amd_gfx906_base="${_amd_gfx906_base%/}"
done
TORCH_INDEX_URL="${_amd_gfx906_base}/rocm6.3"
- # Reset to the default (<2.11) window: a rocm7.2 pick raised the floor
- # to 2.11 above, which the rocm6.3 index (torch <= 2.9.x) cannot satisfy.
+ # Cap below <2.12: a rocm7.2 pick floors at 2.11, which rocm6.3 (<= 2.9.x) cannot meet.
TORCH_CONSTRAINT="torch>=2.4,<2.11.0"
TORCHVISION_CONSTRAINT="torchvision>=0.19,<0.26.0"
TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<2.11.0"
@@ -5060,12 +4773,6 @@ case "$_torch_index_leaf" in
;;
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
-# final one and a raised floor (rocm7.2 / Strix gfx) rejects an older release.
-# _PREV_FALLBACK_CONSTRAINT keeps the range so the install can fall back when the exact
-# release is not on the chosen index (mirrors may prune old wheels). Skipped for --no-torch.
_PREV_TORCH_PIN=""
_PREV_FALLBACK_CONSTRAINT="$TORCH_CONSTRAINT"
if [ "$SKIP_TORCH" = false ]; then
@@ -5261,9 +4968,7 @@ elif case "$TORCH_INDEX_URL" in */rocm*|*/gfx*) true ;; *) false ;; esac; then
substep "gfx arch from UNSLOTH_ROCM_GFX_ARCH env override: $_gpu_disp_gfx"
# Name-based arch inference when tools don't report gfx (mirrors install.ps1 nameArchTable)
elif [ -z "$_gpu_disp_gfx" ] && [ -n "$_gpu_disp_mkt" ]; then
- # Kept in sync with the nameArchTable in install.ps1 / setup.ps1.
- # gfx1102 matched BEFORE gfx1100 so the spaceless "RX 7700S" lands on
- # gfx1102 (bash case has no negative lookahead like the PS tables).
+ # In sync with install.ps1 nameArchTable; gfx1102 before gfx1100 ("RX 7700S").
case "$_gpu_disp_mkt" in
*9070*|*9080*|*"R9700"*) _gpu_disp_gfx="gfx1201" ;; # RDNA 4 (Navi 48: RX 9070 / 9080, Radeon AI PRO R9700)
*9060*) _gpu_disp_gfx="gfx1200" ;; # RDNA 4 (Navi 44)
@@ -5470,17 +5175,50 @@ for _p in ('torch', 'torchvision', 'torchaudio'):
" 2>/dev/null) || _torch_trio_pins=""
case "$_torch_trio_pins" in
torch==*)
- _UNSLOTH_TORCH_OVERRIDES=$(mktemp)
+ # uv resolves an override's relative includes (-r nested.txt) against THAT file's dir,
+ # so merge beside the caller's override when they share one writable dir, else mktemp.
+ # Globbing is off for both walks below: uv reads the literal name, so an ov[1].txt
+ # would otherwise make them iterate a sibling ov1.txt.
+ _ov_glob=on
+ case $- in *f*) _ov_glob=off ;; esac
+ set -f
+ _ov_dir=""
+ _ov_dir_ok=1
+ for _ov_file in ${UV_OVERRIDE:-}; do
+ [ -f "$_ov_file" ] || continue
+ _ov_this=$(CDPATH= cd -- "$(dirname -- "$_ov_file")" 2>/dev/null && pwd) || _ov_this=""
+ if [ -z "$_ov_this" ]; then
+ _ov_dir_ok=0
+ break
+ elif [ -z "$_ov_dir" ]; then
+ _ov_dir=$_ov_this
+ elif [ "$_ov_dir" != "$_ov_this" ]; then
+ _ov_dir_ok=0
+ break
+ fi
+ done
+ _UNSLOTH_TORCH_OVERRIDES=""
+ if [ "$_ov_dir_ok" = 1 ] && [ -n "$_ov_dir" ] && [ -w "$_ov_dir" ]; then
+ _UNSLOTH_TORCH_OVERRIDES="$_ov_dir/.unsloth-torch-overrides.$$.txt"
+ # 0600, as the mktemp fallback: the merge copies inherited requirements in, which
+ # can include an authenticated direct URL. The chmod is not redundant with the
+ # umask, as `: >` truncates a recycled-PID file without changing its mode.
+ if (umask 077; : > "$_UNSLOTH_TORCH_OVERRIDES") 2>/dev/null; then
+ chmod 600 "$_UNSLOTH_TORCH_OVERRIDES" 2>/dev/null || true
+ else
+ _UNSLOTH_TORCH_OVERRIDES=""
+ fi
+ fi
+ [ -n "$_UNSLOTH_TORCH_OVERRIDES" ] || _UNSLOTH_TORCH_OVERRIDES=$(mktemp)
printf '%s\n' "$_torch_trio_pins" > "$_UNSLOTH_TORCH_OVERRIDES"
- # The CLI --overrides flag replaces any UV_OVERRIDE env file (same
- # uv setting; macOS arm64 exports one here), so fold its pins in.
- # awk, not cat: it drops inherited torch-trio lines (uv intersects
- # duplicate overrides, so a conflicting pin would make resolution
- # unsatisfiable) and newline-terminates the last line so an
- # unterminated file cannot join two requirements into one.
+ # --overrides replaces any UV_OVERRIDE env file, so fold its pins in; awk drops the
+ # inherited trio. tolower: a caller's "Torch<2.11" would be unsatisfiable to uv.
for _ov_file in ${UV_OVERRIDE:-}; do
- [ -f "$_ov_file" ] && awk '!/^[[:space:]]*torch(vision|audio)?([[:space:]<>=!~;@[]|$)/' "$_ov_file" >> "$_UNSLOTH_TORCH_OVERRIDES"
+ [ -f "$_ov_file" ] && awk '!(tolower($0) ~ /^[[:space:]]*torch(vision|audio)?([[:space:]<>=!~;@[]|$)/)' "$_ov_file" >> "$_UNSLOTH_TORCH_OVERRIDES"
done
+ # `if`, not `[ ... ] && set +f`: last command of the arm, so under `set -e` a false
+ # test would make the whole function exit non-zero.
+ if [ "$_ov_glob" = on ]; then set +f; fi
;;
esac
}
@@ -5492,15 +5230,11 @@ fi
_unsloth_release_install_spec="${_unsloth_desktop_install_spec:-unsloth>=2026.9.2}"
if [ "$_MIGRATED" = true ]; then
- # Migrated env: force-reinstall unsloth+unsloth-zoo for a clean state, preserving
- # existing torch/CUDA unless the ROCm repair below fires.
+ # Migrated env: force-reinstall unsloth+unsloth-zoo, keeping torch unless the ROCm repair fires.
_gfx906_bnb_snapshot
substep "upgrading unsloth in migrated environment..."
if [ "$SKIP_TORCH" = true ]; then
- # No-torch: install unsloth + unsloth-zoo with --no-deps (current
- # PyPI metadata still declares torch as a hard dep), then install
- # runtime deps (typer, safetensors, transformers, etc.) with --no-deps
- # to prevent transitive torch resolution.
+ # No-torch: --no-deps throughout (PyPI metadata still hard-deps torch).
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_release_install_spec" "unsloth-zoo>=2026.9.1"
@@ -5530,9 +5264,6 @@ if [ "$_MIGRATED" = true ]; then
--no-deps --reinstall-package unsloth-zoo \
"unsloth-zoo @ git+https://github.com/unslothai/unsloth-zoo"
fi
- # 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 ] && [ "$_torch_index_is_rocm_family" = true ]; then
if _is_gfx906_bnb_skip; then
substep "gfx906: skipping prebuilt bitsandbytes (no gfx906 kernels); build from source for 4-bit QLoRA -- https://docs.unsloth.ai/get-started/install-and-update/amd" "$C_WARN"
@@ -5568,31 +5299,13 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
fi
if [ "$_radeon_listing_ok" = true ]; then
- # Require torch, torchvision, torchaudio wheels to all resolve
- # from the Radeon listing. The repo often publishes multiple
- # generations simultaneously, so picking the highest-version
- # for each package independently can assemble a mismatched trio
- # (e.g. torch 2.10 + torchvision 0.24). To prevent this,
- # we identify the highest common minor version and downpair
- # wheels if necessary to ensure a compatible set.
+ # Independent highest picks can mismatch; downpair to the highest common minor.
_torch_whl=$(_pick_radeon_wheel "torch" 2>/dev/null) || _torch_whl=""
_tv_whl=$(_pick_radeon_wheel "torchvision" 2>/dev/null) || _tv_whl=""
_ta_whl=$(_pick_radeon_wheel "torchaudio" 2>/dev/null) || _ta_whl=""
_tri_whl=$(_pick_radeon_wheel "triton" 2>/dev/null) || _tri_whl=""
- # Check that torch and torchaudio share the same X.Y public
- # version prefix, and that torchvision's minor correctly
- # pairs with torch's minor (torchvision = torch.minor + 15
- # since torch 2.4 -> torchvision 0.19 -> torch 2.9 ->
- # torchvision 0.24).
- #
- # URL-decode each wheel name so %2B -> + before version
- # extraction. Real Radeon wheel hrefs are percent-encoded
- # (torch-2.10.0%2Brocm7.2.0...), so a plain [+-] terminator
- # in the sed regex below would never match and
- # _radeon_versions_match would stay false for every real
- # listing, silently forcing a fallback to the generic
- # ROCm index.
+ # Verify the X.Y pairing (vision = torch.minor + 15); URL-decode %2B first.
_extract_version() {
_whl=$1
_pkg=$2
@@ -5607,11 +5320,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
_ta_ver=$(_extract_version "$_ta_whl" "torchaudio")
_radeon_versions_match=false
- # Kept release (_PREV_TORCH_PIN) wins here too: pick its exact
- # patch (else the newest patch of its minor) plus the paired
- # vision/audio wheels. Any gap falls back to the newest-trio
- # search below, mirroring _install_torch_default_index, so a
- # rerun never drifts to another release nor below the kept one.
+ # Kept release wins here too: exact patch, else newest of its minor, paired.
if [ -n "$_PREV_TORCH_PIN" ]; then
_prev_kept_base="${_PREV_TORCH_PIN#torch==}"
_prev_kept_minor="${_prev_kept_base#*.}"
@@ -5629,8 +5338,6 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
_ta_whl=$_kept_ta
_tri_whl=""
_radeon_versions_match=true
- # Say so when the listing pruned the exact patch
- # and a same-series build is installed instead.
case "$(printf '%s' "${_kept_torch##*/}" | sed 's/%2[Bb]/+/g')" in
"torch-${_prev_kept_base}"[+-]*) ;;
*) substep "kept release ${_prev_kept_base} is not in the Radeon listing -- installing the closest 2.${_prev_kept_minor} series build instead" ;;
@@ -5653,8 +5360,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
[ "$_tv_equiv_minor" -lt "$_target_minor" ] && _target_minor=$_tv_equiv_minor
[ "$_ta_minor" -lt "$_target_minor" ] && _target_minor=$_ta_minor
- # Loop downwards to find the first complete matching trio.
- # This avoids aborting if the repo has gaps.
+ # Loop downwards to find the first complete matching trio (repo gaps).
_attempts=0
while [ "$_attempts" -lt 5 ] && [ "$_target_minor" -ge 0 ]; do
_expected_tv_minor=$((_target_minor + 15))
@@ -5677,8 +5383,6 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
_c_tv_major=${_c_tv_ver%%.*}
_c_tv_minor=${_c_tv_ver#*.}
- # Strict X.Y validation: allow patch versions to differ (e.g. torch 2.9.1 + vision 0.24.0)
- # as long as the Major and Minor pairing is correct.
if [ "$_c_torch_major" = "$_c_ta_major" ] && \
[ "$_c_torch_minor" = "$_c_ta_minor" ] && \
[ "$_c_tv_major" = "0" ] && \
@@ -5703,12 +5407,7 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
_install_torch_default_index
else
substep "installing PyTorch from Radeon repo (${_RADEON_BASE_URL})..."
- # Pass explicit wheel URLs so the matched trio is
- # installed together. --find-links lets uv discover
- # the Radeon listing for any local lookup, and PyPI
- # (not disabled) provides transitive deps like
- # filelock / sympy / networkx which are not in the
- # Radeon listing.
+ # Explicit wheel URLs install the trio together; --find-links exposes the listing.
if [ -n "$_tri_whl" ]; then
run_install_cmd_retry "install triton + PyTorch" uv pip install --python "$_VENV_PY" \
--find-links "$_RADEON_BASE_URL" \
@@ -5731,10 +5430,6 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
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 ] && [ "$_torch_index_is_rocm_family" = true ]; then
if _is_gfx906_bnb_skip; then
substep "gfx906: skipping prebuilt bitsandbytes (no gfx906 kernels); build from source for 4-bit QLoRA -- https://docs.unsloth.ai/get-started/install-and-update/amd" "$C_WARN"
@@ -5748,8 +5443,6 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
substep "installing unsloth (this may take a few minutes)..."
_build_unsloth_torch_overrides
if [ "$SKIP_TORCH" = true ]; then
- # No-torch: install unsloth + unsloth-zoo with --no-deps, then
- # runtime deps (typer, safetensors, transformers, etc.) with --no-deps.
run_install_cmd_retry "install unsloth (no-torch)" uv pip install --python "$_VENV_PY" --no-deps \
--upgrade-package unsloth --upgrade-package unsloth-zoo \
"$_unsloth_release_install_spec" "unsloth-zoo>=2026.9.1"
@@ -5789,8 +5482,6 @@ elif [ -n "$TORCH_INDEX_URL" ]; then
fi
[ -n "$_UNSLOTH_TORCH_OVERRIDES" ] && rm -f "$_UNSLOTH_TORCH_OVERRIDES"
_UNSLOTH_TORCH_OVERRIDES=""
- # AMD ROCm: repair torch if the unsloth/unsloth-zoo install pulled in
- # CUDA torch from PyPI, overwriting the ROCm wheels installed in Step 1.
if [ "$SKIP_TORCH" = false ] && [ "$_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
@@ -5860,10 +5551,7 @@ else
fi
# ── Enforce the installed torch flavor matches the detected GPU build ──
-# PEP 440 ignores the +cpu/+cuXXX/+rocm local label in a version range, so uv
-# keeps a stale torch==X+cpu against a GPU index and the venv silently trains on
-# CPU. Reinstall the right wheel triplet when a GPU build is expected; if it
-# can't be reinstalled, warn loudly. --no-torch / CPU-only / macOS: no-op.
+# PEP 440 ignores the +cpu/+cuXXX local label, so uv keeps a stale torch+cpu against a GPU index.
if [ "$SKIP_TORCH" = false ] && [ -n "${TORCH_INDEX_URL:-}" ]; then
_expected_torch_tag=$(_expected_torch_flavor_tag "$TORCH_INDEX_URL")
# Only act when a GPU build is expected (cuXXX / rocm); cpu and unknown skip.
@@ -5871,8 +5559,7 @@ if [ "$SKIP_TORCH" = false ] && [ -n "${TORCH_INDEX_URL:-}" ]; then
_installed_torch_ver=$(_installed_torch_version_for_tag "$_expected_torch_tag")
_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 --default-index reinstallable
- # (cuXXX / rocmX.Y / repo.amd.com gfx*); an unknown mirror leaf -> warn only.
+ # Repair only when flavor is wrong AND the index is --default-index reinstallable.
if [ -n "$_installed_torch_tag" ] && [ "$_installed_torch_tag" != "$_expected_torch_tag" ] \
&& [ "$(_torch_index_repairable "$TORCH_INDEX_URL")" = "yes" ]; then
substep "PyTorch flavor mismatch (installed $_installed_torch_tag, need $_expected_torch_tag) -- reinstalling correct build..."
@@ -5932,8 +5619,6 @@ fi
# ── Run studio setup ──
tauri_log "STEP" "Running Unsloth setup"
-# When --local, use the repo's own setup.sh directly.
-# Otherwise, find it inside the installed package.
SETUP_SH=""
if [ "$STUDIO_LOCAL_INSTALL" = true ] && [ -f "$_REPO_ROOT/studio/setup.sh" ]; then
SETUP_SH="$_REPO_ROOT/studio/setup.sh"
@@ -5978,8 +5663,6 @@ _SKIP_FRONTEND=0
if [ "$TAURI_MODE" = true ]; then
_SKIP_FRONTEND=1
fi
-# Prepend UNSLOTH_STUDIO_HOME=$STUDIO_HOME to "$@" for env-override installs
-# without word-splitting on whitespace paths.
_run_setup_with_studio_home() {
if [ "$_STUDIO_HOME_REDIRECT" = "env" ]; then
UNSLOTH_STUDIO_HOME="$STUDIO_HOME" "$@"
@@ -6006,11 +5689,7 @@ if [ "$STUDIO_LOCAL_INSTALL" = true ]; then
UNSLOTH_TAURI_MODE="$TAURI_MODE" \
bash "$SETUP_SH" </dev/null || _SETUP_EXIT=$?
else
- # Explicitly reset STUDIO_LOCAL_INSTALL / STUDIO_LOCAL_REPO so a stale
- # value inherited from the parent shell (e.g. a previous --local run in
- # the same session) does not silently flip a normal install onto the
- # local-dev path in setup.sh and install_python_stack.py. Mirrors the
- # reset already done in install.ps1 for PowerShell.
+ # Reset STUDIO_LOCAL_* so a stale value cannot flip a normal install onto the local-dev path.
_run_setup_with_studio_home env \
SKIP_STUDIO_BASE="$_SKIP_BASE" \
SKIP_STUDIO_FRONTEND="$_SKIP_FRONTEND" \
@@ -6031,11 +5710,8 @@ if [ "$_SETUP_EXIT" -eq 0 ]; then
fi
# ── Make 'unsloth' available via $_LOCAL_BIN (resolved earlier) ──
-# Env-mode: $_LOCAL_BIN is $STUDIO_HOME/bin; skip shell-rc PATH append so we
-# don't pollute the user's profile with a workspace-scoped path.
mkdir -p "$_LOCAL_BIN"
-# ln -sf into an existing dir creates link inside it. Refuse to delete a
-# real directory at the shim path -- that could destroy unrelated user data.
+# Refuse to delete a real directory at the shim path (could destroy user data).
_shim_path="$_LOCAL_BIN/unsloth"
if [ -d "$_shim_path" ] && [ ! -L "$_shim_path" ]; then
echo "ERROR: $_shim_path is a directory; refusing to delete it." >&2
@@ -6208,14 +5884,11 @@ fi
# end of the PATH persistence block
# Non-Tauri installs keep shortcuts even if setup reports failure.
-# create_studio_shortcuts gates persistent menu shortcuts on env-mode;
-# launcher + studio.conf + icon are always written.
if [ "$TAURI_MODE" != true ]; then
create_studio_shortcuts "$VENV_ABS_BIN/unsloth" "$OS"
fi
# If setup.sh failed, report and exit now.
-# PATH and shortcuts are already set up so the user can fix and retry.
if [ "$_SETUP_EXIT" -ne 0 ]; then
echo ""
if [ "$TAURI_MODE" = true ]; then
@@ -6233,18 +5906,11 @@ if [ "$TAURI_MODE" = true ]; then
exit 0
fi
-# Warn if another 'unsloth' wins on PATH (different venv, system pip, etc).
-# Users typing `unsloth studio` later would hit that binary instead of the
-# one just installed; the runtime now falls back via UNSLOTH_STUDIO_HOME
-# but the absolute path is still the most reliable launch.
-# Uses the venv python (just created above) for path canonicalization so
-# this works on macOS (BSD readlink has no -f) as well as Linux/WSL.
+# Warn if another 'unsloth' wins on PATH; canonicalize via python (BSD readlink lacks -f).
_installed_bin="$VENV_DIR/bin/unsloth"
_path_unsloth=$(command -v unsloth 2>/dev/null || true)
if [ -n "$_path_unsloth" ] && [ -x "$VENV_DIR/bin/python" ]; then
- # Canonicalize via the venv python (BSD readlink lacks -f on macOS).
- # If either side fails to resolve, skip the check entirely rather than
- # comparing raw paths (which would false-trigger on symlink targets).
+ # If either side fails to resolve, skip the check rather than compare raw paths.
_canon() {
"$VENV_DIR/bin/python" -c \
'import os, sys; print(os.path.realpath(sys.argv[1]))' \
@@ -6270,9 +5936,6 @@ printf " ${C_TITLE}%s${C_RST}\n" "Unsloth Studio installed!"
printf " ${C_DIM}%s${C_RST}\n" "$RULE"
echo ""
-# In interactive terminals, ask the user before starting Unsloth unless the
-# caller explicitly disabled the post-install prompt.
-# In non-interactive environments (Docker, CI, cloud-init) just print instructions.
if [ "$_SKIP_AUTOSTART" != true ] && [ -t 1 ]; then
echo ""
# No readable answer (closed/EOF tty) defaults to no; Enter is still yes.
diff --git a/studio/install_python_stack.py b/studio/install_python_stack.py
index 3e7bffd7c8f..313a0142615 100644
--- a/studio/install_python_stack.py
+++ b/studio/install_python_stack.py
@@ -54,15 +54,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")
-# 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).
+# amd-smi auto-elevates on Windows (UAC/DiskPart); RunAsInvoker keeps probes un-elevated.
if IS_WINDOWS:
os.environ.setdefault("__COMPAT_LAYER", "RunAsInvoker")
-# torchcodec ships wheels only for manylinux_2_28_x86_64, macosx_12_0_arm64,
-# and win_amd64. On other hosts the audio extras must be filtered out (the
-# extras-no-deps step would otherwise fail), regardless of NO_TORCH.
+# torchcodec ships wheels only for manylinux_2_28_x86_64, macosx_12_0_arm64 and win_amd64.
PLATFORM_LACKS_TORCHCODEC_WHEEL = (
(IS_LINUX and platform.machine() in {"aarch64", "arm64"})
or (IS_WINDOWS and platform.machine().lower() in {"arm64", "aarch64"})
@@ -87,12 +82,10 @@ def _is_windows_arm64() -> bool:
# ── ROCm / AMD GPU support ─────────────────────────────────────────────────────
-# Detected ROCm (major, minor) -> best PyTorch wheel tag on
-# download.pytorch.org. Checked newest-first (>=).
_ROCM_TORCH_INDEX: dict[tuple[int, int], str] = {
(7, 2): "rocm7.2", # torch 2.11.0
- (7, 1): "rocm7.1", # torch 2.10.0
- (7, 0): "rocm7.0",
+ (7, 1): "rocm7.1", # torch 2.11.0
+ (7, 0): "rocm7.0", # torch 2.10.0
(6, 4): "rocm6.4",
(6, 3): "rocm6.3",
(6, 2): "rocm6.2",
@@ -183,27 +176,31 @@ _ROCM_GFX_TORCH211_LEAVES: frozenset[str] = frozenset(
{"gfx120x-all", "gfx1151", "gfx1150", "gfx1152"}
)
-# 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.
+# rocmX.Y indexes KNOWN to ship torch 2.11; never floor an unknown newer rocm.
_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).
+# Per-tag repair specs; must land on the same wheels a fresh install.sh run does.
_ROCM_TORCH_PKG_SPECS: dict[str, tuple[str, str, str]] = {
+ # Floored at 2.11 (the _grouped_mm bug), matching install.sh's rocm7.2|gfx* case.
"rocm7.2": (
"torch>=2.11.0,<2.12.0",
"torchvision>=0.26.0,<0.27.0",
"torchaudio>=2.11.0,<2.12.0",
),
- # rocm7.1 and earlier: torch 2.x below 2.11
+ # rocm7.1 also serves 2.11, so a <2.11 cap would force-reinstall 2.10 over it.
+ "rocm7.1": (
+ "torch>=2.4,<2.12.0",
+ "torchvision>=0.19,<0.27.0",
+ "torchaudio>=2.4,<2.12.0",
+ ),
+ # rocm7.0 and earlier top out below 2.11, so the ceiling stays literal.
"_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 (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 AMD per-arch pins for repo.amd.com, stopping an ABI-mismatched companion.
_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"],
@@ -280,27 +277,22 @@ def _torch_index_leaf(url: str) -> str:
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 repair specs (see _ensure_cuda_torch); companions pinned against an 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 repair specs (see _ensure_cpu_torch); the /cpu index also serves newer torch.
_CPU_TORCH_PKG_SPEC: tuple[str, str, str] = _CUDA_TORCH_PKG_SPEC
-# Byte-identical to the non-XPU arm of install.ps1's $_fixSpecs, NOT _CUDA_TORCH_PKG_SPEC:
+# Byte-identical to the non-XPU arm of install.ps1's $_fix*Spec scalars, NOT _CUDA_TORCH_PKG_SPEC:
# `studio update` must repair to the same wheels install.ps1 does.
_TORCH_FLAVOR_REPAIR_PKG_SPEC: tuple[str, str, str] = (
- "torch>=2.4,<2.11.0",
- "torchvision>=0.19,<0.26.0",
- "torchaudio>=2.4,<2.11.0",
+ "torch>=2.4,<2.12.0",
+ "torchvision>=0.19,<0.27.0",
+ "torchaudio>=2.4,<2.12.0",
)
# torchao's cpp extensions are pinned to ONE torch release AND CUDA major. A torch
@@ -343,8 +335,7 @@ def _select_torchao_spec(torch_version: str | None) -> str:
release = str(torch_version).split("+", 1)[0] # drop +cu130/+rocm6.4/+cpu
parts = release.split(".")
try:
- # Strip any pre-release/dev suffix from the minor (e.g. '10rc1' -> '10'),
- # matching wheel_utils.probe_torch_wheel_env.
+ # Strip a pre-release suffix from the minor ('10rc1' -> '10').
minor_str = re.sub(r"[^0-9].*", "", parts[1]) if len(parts) > 1 else ""
major, minor = int(parts[0]), int(minor_str)
except (IndexError, ValueError):
@@ -511,9 +502,7 @@ def _installed_torch_is_windows_rocm() -> bool:
return bool(_hip) or "rocm" in _ver or "rocmsdk" in _ver
-# constraints.txt caps new anyio resolutions at <4.14 (#6483), but an install
-# from before the cap existed can already be stuck at 4.14+, which later
-# constrained installs won't touch since it already satisfies mcp/fastmcp.
+# constraints.txt caps anyio <4.14 (#6483), but a pre-cap install stuck at 4.14+ is untouched.
_ANYIO_BAD_FLOOR = (4, 14)
@@ -555,8 +544,6 @@ _ROCM_WINDOWS_INDEX_BASE = (
os.environ.get("UNSLOTH_ROCM_WINDOWS_MIRROR") or "https://repo.amd.com/rocm/whl"
)
-# gfx arch → AMD index arch-family suffix; each family is a separate
-# pip index on repo.amd.com.
_GFX_TO_AMD_INDEX_ARCH: dict[str, str] = {
"gfx1201": "gfx120X-all",
"gfx1200": "gfx120X-all", # RDNA 4
@@ -592,9 +579,7 @@ _BNB_ROCM_PRERELEASE_URLS: dict[str, str] = {
"download/continuous-release_main/"
"bitsandbytes-1.33.7.preview-py3-none-manylinux_2_24_aarch64.whl"
),
- # Windows ROCm wheel ships libbitsandbytes_rocm{VER}.dll. BNB's HIP
- # auto-detect may mismatch the DLL suffix, so we scan the wheel and set
- # BNB_ROCM_VERSION in _install_bnb_windows_rocm() and worker.py.
+ # The Windows ROCm wheel ships libbitsandbytes_rocm{VER}.dll; BNB_ROCM_VERSION must match.
"win_amd64": (
"https://github.com/bitsandbytes-foundation/bitsandbytes/releases/"
"download/continuous-release_main/"
@@ -642,8 +627,7 @@ def _path_inside_venv(path: str) -> bool:
try:
# realpath (not abspath): resolve symlinks/8.3 names so an aliased venv matches.
_root = os.path.normcase(os.path.realpath(sys.prefix))
- # Guard a root-dir prefix (C:\ or /): commonpath would match every path on
- # it. A venv is never at root, so treat that as outside.
+ # A root prefix (C:\ or /) would commonpath-match everything; a venv is never at root.
if os.path.dirname(_root) == _root:
return False
return os.path.normcase(os.path.commonpath([os.path.realpath(path), _root])) == _root
@@ -681,9 +665,7 @@ def _amd_smi_allowed() -> bool:
return True
if flag in ("0", "false", "no", "off"):
return False
- # A real HIP SDK lets amd-smi run un-elevated; hipinfo-on-PATH is the proxy.
- # Ignore the venv hipInfo.exe (AMD wheel via bnb fix): not a HIP SDK, doesn't
- # stop amd-smi's DiskPart UAC.
+ # hipinfo-on-PATH proxies a real HIP SDK; the venv hipInfo.exe is not one.
if _external_hipinfo_on_path():
return True
for _var in ("HIP_PATH", "HIP_PATH_57", "ROCM_PATH"):
@@ -732,17 +714,13 @@ def _detect_rocm_version_uncached() -> tuple[int, int] | None:
try:
with open(path, encoding = "utf-8") as fh:
parts = fh.read().strip().split("-")[0].split(".")
- # 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:
_record("ROCm version file", int(parts[0]), int(parts[1]))
break
except Exception:
pass
- # Try amd-smi version (outputs "... | ROCm version: X.Y.Z").
- # Gated off on Windows w/o a HIP SDK (avoids the UAC/DiskPart prompt);
- # hipconfig below covers that case.
+ # amd-smi version ("ROCm version: X.Y.Z"); off on Windows without a HIP SDK (UAC prompt).
amd_smi = shutil.which("amd-smi") if _amd_smi_allowed() else None
if amd_smi:
try:
@@ -1065,10 +1043,7 @@ def _detect_windows_gfx_arch() -> str | None:
hipinfo = _candidate
break
if not hipinfo:
- # 2b. AMD torch wheels ship hipInfo.exe into the venv Scripts dir
- # (next to python.exe); resolvable even on driver-only hosts with no
- # SDK install at all. Lets `studio update` re-detect the arch on a
- # venv that already has the AMD wheel.
+ # 2b. AMD torch wheels drop hipInfo.exe into venv Scripts, so driver-only hosts re-detect.
_venv_hipinfo = os.path.join(os.path.dirname(sys.executable), "hipInfo.exe")
if os.path.isfile(_venv_hipinfo):
hipinfo = _venv_hipinfo
@@ -1080,10 +1055,7 @@ def _detect_windows_gfx_arch() -> str | None:
stderr = subprocess.DEVNULL,
timeout = 10,
)
- # 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).
+ # Accept partial output when hipinfo crashes (0xC0000005 on some RDNA 4, #6043).
text = result.stdout.decode(errors = "replace")
# findall gets every gcnArchName line so multi-GPU hosts are
# enumerable and HIP_VISIBLE_DEVICES selects correctly.
@@ -1100,9 +1072,6 @@ def _detect_windows_gfx_arch() -> str | None:
except Exception:
pass
- # 3. amd-smi fallback -- runtime-only Radeon installs ship amd-smi but no hipinfo.
- # Gated off on Windows w/o a HIP SDK (avoids the UAC/DiskPart prompt); the arch
- # arrives via --rocm-gfx / name inference there, so this is only needed when safe.
amd_smi = shutil.which("amd-smi") if _amd_smi_allowed() else None
if amd_smi:
for _args in (("static", "--asic"), ("list",)):
@@ -1255,10 +1224,7 @@ def _detect_windows_gfx_arch() -> str | None:
return None
-# GPU marketing-name → gfx arch table, mirroring setup.ps1's $nameArchTable.
-# Most-specific first; first match wins. Covers only arches the ROCm
-# prebuilts / AMD Windows torch indexes support; unknown names return None
-# (callers then fall back cleanly to CPU).
+# GPU marketing-name -> gfx arch (mirrors setup.ps1's $nameArchTable), most-specific first.
_WIN_GPU_NAME_ARCH_TABLE: "list[tuple[str, str]]" = [
# RDNA 4 (Navi 48: Radeon RX 9070 XT / 9070 GRE / 9070 / 9080, Radeon AI PRO R9700).
# R9700 is listed separately: its name holds neither 9070 nor 9080, so it matched
@@ -1606,8 +1572,7 @@ 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))
- # Highest numeric suffix wins (e.g. "713" over "72"); glob order is not
- # guaranteed, so sort rather than take the first match.
+ # Highest numeric suffix wins ("713" over "72"); glob order is not guaranteed.
return max(all_vers, key = lambda v: int(v)) if all_vers else None
@@ -1676,8 +1641,7 @@ def _persist_bnb_rocm_version(version: str) -> bool:
existing = (
sitecustomize_path.read_text(encoding = "utf-8") if sitecustomize_path.exists() else ""
)
- # Strip all managed regions, including one whose END marker was lost to
- # an interrupted write, then append exactly one fresh block.
+ # Strip all managed regions (even END-less, from an interrupted write), append one block.
pattern = re.compile(
rf"{re.escape(_BNB_ROCM_SITECUSTOMIZE_BEGIN)}.*?"
rf"(?:{re.escape(_BNB_ROCM_SITECUSTOMIZE_END)}\n?|\Z)",
@@ -1717,10 +1681,7 @@ def _has_rocm_gpu() -> bool:
if _has_usable_nvidia_gpu():
return False
for cmd, check_fn in (
- # rocminfo: look for a real gfx GPU id (3-4 chars, nonzero first digit).
- # gfx000 is the CPU agent; ROCm 6.1+ also emits generic ISA lines like
- # "gfx11-generic"/"gfx9-4-generic" with only 1-2 digits before the dash,
- # which must not be treated as a real GPU.
+ # rocminfo: real gfx GPU ids only (gfx000 = CPU agent, "gfx11-generic" = ISA line).
(
["rocminfo"],
lambda out: bool(re.search(r"gfx[1-9][0-9a-z]{2,3}", out.lower())),
@@ -1734,8 +1695,6 @@ def _has_rocm_gpu() -> bool:
exe = shutil.which(cmd[0])
if not exe:
continue
- # Skip amd-smi on Windows w/o a HIP SDK (avoids the UAC/DiskPart prompt);
- # rely on rocminfo / the sysfs fallback there.
if cmd[0] == "amd-smi" and not _amd_smi_allowed():
continue
try:
@@ -1754,14 +1713,8 @@ 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 / 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. 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.
+ # sysfs KFD fallback for hosts without rocminfo/amd-smi. Non-AMD vendors are rejected: the
+ # NVIDIA open kernel module also registers KFD nodes.
if sys.platform != "win32":
try:
kfd_nodes = "/sys/class/kfd/kfd/topology/nodes"
@@ -1775,10 +1728,7 @@ 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). 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).
+ # Require AMD vendor_id 4098 (0x1002); a missing properties file stays unconfirmed.
props_path = os.path.join(kfd_nodes, entry, "properties")
try:
with open(props_path, encoding = "utf-8") as fh:
@@ -1826,8 +1776,7 @@ def _has_usable_nvidia_gpu() -> bool:
return True
except Exception:
pass
- # Fallback: the NVIDIA driver exposes one subdirectory per GPU under
- # /proc/driver/nvidia/gpus/ on Linux regardless of nvidia-smi state.
+ # Fallback: /proc/driver/nvidia/gpus/ has one subdir per GPU whatever nvidia-smi does.
if sys.platform != "win32":
try:
gpu_dir = "/proc/driver/nvidia/gpus"
@@ -2578,10 +2527,7 @@ def _install_bnb_windows_rocm() -> bool:
)
if not _ok:
return False
- # 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).
+ # BNB_ROCM_VERSION from the DLL suffix (the wheel may ship "72" while torch reports 7.13).
_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
@@ -2596,11 +2542,7 @@ def _install_bnb_windows_rocm() -> bool:
_persist_detected_version = True
if _persist_detected_version:
_persist_bnb_rocm_version(_ver)
- # 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.
+ # venv Scripts (hipInfo.exe from the AMD torch wheel) on PATH, else bnb logs a stray ERROR.
_scripts_dir = os.path.dirname(sys.executable)
if os.path.isfile(os.path.join(_scripts_dir, "hipInfo.exe")) and not shutil.which(
"hipinfo.exe"
@@ -2784,8 +2726,7 @@ def _is_pip_rocm_family_leaf(leaf: str) -> bool:
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.
+ # gfx must be followed by a digit; a gfx-private custom leaf is a verbatim pin.
return bool(re.fullmatch(r"rocm\d+(?:\.\d+)?", leaf)) or bool(re.match(r"gfx\d", leaf))
@@ -2965,8 +2906,7 @@ def _ensure_cuda_torch() -> None:
GPUs (a pre-Turing box that the driver-only ladder sent to cu128/cu130).
Healthy CUDA torch and deliberate CPU-only torch are left untouched.
"""
- # Respect install.sh's backend: only "" (standalone update) or "cuda" force CUDA
- # wheels; "rocm"/"cpu"/unrecognised are deliberate.
+ # Respect install.sh's backend: only "" (standalone update) or "cuda" force CUDA wheels.
if _TORCH_BACKEND not in ("", "cuda"):
return
# An explicit unknown-family pin was applied VERBATIM at install time; leave it alone.
@@ -2978,11 +2918,9 @@ def _ensure_cuda_torch() -> None:
# Never undo a deliberate ROCm install (setup.ps1 sets this marker).
if os.environ.get("UNSLOTH_ROCM_TORCH_INSTALLED") == "1":
return
- # 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.
+ # An explicit CUDA pin commits to CUDA wheels and skips ALL GPU gates 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.
+ # CUDA_VISIBLE_DEVICES="" / "-1" hides the GPU; honour it unless a CUDA index is pinned.
_cvd = os.environ.get("CUDA_VISIBLE_DEVICES")
if not _cuda_pinned and _cvd is not None and _cvd.strip() in ("", "-1"):
return
@@ -3045,8 +2983,7 @@ def _ensure_cuda_torch() -> None:
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 cuXXX differs from the pin; an untagged build counts too (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}"
elif _marker == "cuda" and not _pinned_cuda:
@@ -3476,8 +3413,6 @@ def _ensure_cpu_torch() -> None:
" 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",
@@ -4325,8 +4260,7 @@ def _ensure_rocm_torch() -> None:
# 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).
+ # setup.ps1's marker; trust it only when torch imports as ROCm (a wiped venv leaves it stale).
if os.environ.get("UNSLOTH_ROCM_TORCH_INSTALLED") == "1":
_ran, _importable, _version, _hip, _cuda = _probe_torch_runtime()
_torch_ok = _ran and _importable and (bool(_hip) or "rocm" in (_version or "").lower())
@@ -4341,10 +4275,7 @@ def _ensure_rocm_torch() -> None:
return
if IS_WINDOWS:
- # 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.
+ # An explicit ROCm pin overrides the per-arch index: retry the PINNED one, not repo.amd.com.
_win_rocm_pin = _explicit_rocm_torch_index_url()
if _win_rocm_pin is None and _has_usable_nvidia_gpu():
return
@@ -4378,8 +4309,6 @@ def _ensure_rocm_torch() -> None:
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")
)
@@ -4405,10 +4334,7 @@ def _ensure_rocm_torch() -> None:
"later to retry ROCm."
)
return
- # ROCm torch is installed (or already was); flag it so later phases
- # do not overwrite it with the generic CPU torch wheel. BNB is a
- # separate dependency -- a BNB install failure must NOT roll back the
- # torch ROCm install.
+ # Flag ROCm torch installed so later phases keep it; a BNB failure must not roll it back.
_rocm_windows_torch_installed = True
# Always install AMD Windows bitsandbytes, even when torch was already a
# ROCm build, so `studio update` repairs a broken bnb.
@@ -4423,8 +4349,7 @@ 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
- # 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.
+ # An explicit ROCm pin commits to ROCm wheels whatever the visible GPU (headless / CI).
_rocm_pin = _explicit_rocm_torch_index_url()
_inferred_linux_gfx = (
_infer_linux_amd_gfx_arch() if (_rocm_pin is None and not IS_WINDOWS) else None
@@ -4477,9 +4402,7 @@ def _ensure_rocm_torch() -> None:
_hip_marker = _hip if _hip else ("rocm" if "rocm" in _installed_torch_ver else "")
has_hip_torch = _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.
+ # A ROCm pin of another family reinstalls; a same-tag per-arch switch is undetectable.
_rocm_pin_mismatch = (
_rocm_pin_family_mismatch(_rocm_pin, _installed_torch_ver)
if (has_hip_torch and _rocm_pin is not None)
@@ -4588,8 +4511,6 @@ def _ensure_rocm_torch() -> None:
_arch_index_url = _amd_arch_index_url(_selected_gfx)
_arch_index_pkgs = (
"torch>=2.11.0,<2.12.0",
- # 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",
)
@@ -4942,10 +4863,7 @@ def _ensure_rocm_torch() -> None:
[sys.executable, "-m", "pip", "uninstall", "-y", "bitsandbytes"],
capture_output = True,
)
- # Install bitsandbytes only when torch links against ROCm. Prefers the
- # continuous-release_main wheel (bnb PR #1887 4-bit GEMV fix), falling back
- # to PyPI when the pre-release wheel won't install. Use pip for the
- # pre-release wheel because uv rejects its filename/metadata version mismatch.
+ # bitsandbytes only when torch links ROCm; the pre-release wheel (bnb #1887) needs pip, not uv.
elif rocm_torch_ready:
_bnb_url = _bnb_rocm_prerelease_url()
_bnb_installed = False
@@ -4988,9 +4906,6 @@ def _ensure_rocm_torch() -> None:
)
-# _uv_safe_path is imported from backend.utils.uv_path_safety (shared with mlx_repair).
-
-
def _windows_hidden_subprocess_kwargs() -> dict[str, object]:
"""Return Windows-only subprocess kwargs that suppress console windows."""
if not IS_WINDOWS:
@@ -5052,8 +4967,6 @@ _RECORDED_TORCH_TAG_PINNED = install_manifest.recorded_torch_flavor_was_pinned()
# 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()
@@ -5069,9 +4982,7 @@ if not _TORCH_BACKEND:
# on an authoritative XPU pin -- see _ensure_xpu_torch.
_TORCH_BACKEND = "xpu"
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.
+ # Require a digit after "cu" so /current or /custom is not branded CUDA.
_TORCH_BACKEND = "cuda"
@@ -5093,15 +5004,9 @@ def _torch_step_label(suffix: str) -> str:
# -- Verbosity control ----------------------------------------------------------
-# By default the installer shows a minimal in-place one-line progress bar.
-# Set UNSLOTH_VERBOSE=1 to restore full per-step output:
-# CLI: unsloth studio setup --verbose
-# Linux/Mac: UNSLOTH_VERBOSE=1 ./studio/setup.sh
-# Windows: $env:UNSLOTH_VERBOSE="1" ; .\studio\setup.ps1
VERBOSE: bool = os.environ.get("UNSLOTH_VERBOSE", "0") == "1"
-# Progress bar state -- updated by _progress() per install step.
-# Update _TOTAL if you add/remove steps in install_python_stack().
+# Progress bar state; update _TOTAL when adding/removing steps in install_python_stack().
_STEP: int = 0
_TOTAL: int = 0 # set at runtime in install_python_stack() based on platform
_PROGRESS_LINE_ACTIVE: bool = False
@@ -5123,9 +5028,7 @@ if IS_MAC_ARM and _MLX_OVERRIDES.is_file() and "UV_OVERRIDE" not in os.environ:
os.environ["UV_OVERRIDE"] = _uv_safe_path(_MLX_OVERRIDES)
# -- Unicode-safe printing ---------------------------------------------
-# On Windows the console encoding may be a legacy code page (e.g. CP1252)
-# that cannot represent glyphs like ✅ or ❌. _safe_print() degrades to ASCII
-# equivalents so the installer never crashes over a status glyph.
+# Windows consoles may be a legacy code page (CP1252); _safe_print() degrades glyphs to ASCII.
_UNICODE_TO_ASCII: dict[str, str] = {
"\u2705": "[OK]", # ✅
@@ -5390,13 +5293,7 @@ def _purge_recordless_distributions(output: "bytes | str | None") -> list[str]:
# 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 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.
+# Skipped without torch (Intel Mac GGUF-only), plus librosa, whose numba chain fails (#5046).
NO_TORCH_SKIP_PACKAGES = {
"torch-stoi",
"timm",
@@ -5576,8 +5473,7 @@ def _bootstrap_uv() -> bool:
global UV_NEEDS_SYSTEM
if not shutil.which("uv"):
return False
- # Probe: try a dry-run install targeting the current Python explicitly.
- # Without --python, uv can ignore the activated venv on some platforms.
+ # Explicit --python: uv can ignore the activated venv on some platforms.
probe = subprocess.run(
["uv", "pip", "install", "--dry-run", "--python", sys.executable, "pip"],
stdout = subprocess.PIPE,
@@ -6371,25 +6267,16 @@ def _build_uv_cmd(args: tuple[str, ...]) -> list[str]:
cmd = ["uv", "pip", "install"]
if UV_NEEDS_SYSTEM:
cmd.append("--system")
- # Always pass --python so uv targets the right environment. Without it, uv
- # can ignore an activated venv and install into the system Python (seen on
- # Colab and similar).
cmd.extend(["--python", sys.executable])
cmd.extend(_translate_pip_args_for_uv(args))
- # 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.
+ # No --torch-backend by default, and never on a pinned index: it would defeat the pin.
_tb = os.environ.get("UV_TORCH_BACKEND", "")
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 ranks --index-url LOWEST, so inherited index vars defeat a pinned repair; neutralise them.
_UV_INDEX_ENV_VARS = (
"UV_CONFIG_FILE",
"UV_DEFAULT_INDEX",
@@ -6400,8 +6287,7 @@ _UV_INDEX_ENV_VARS = (
"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 would defeat --index-url; PIP_INDEX_URL dropped so a mirror cannot outrank it.
"PIP_NO_INDEX",
"PIP_INDEX_URL",
)
@@ -7041,8 +6927,6 @@ def install_python_stack() -> int:
# absent torch as a stale venv, and tries to delete the running environment.
install_manifest.set_no_torch_marker(NO_TORCH)
- # 1. Try uv for faster installs (before pip upgrade -- uv venvs don't
- # include pip by default).
USE_UV = _bootstrap_uv()
# 2. Ensure pip is available (uv venvs from install.sh omit pip).
@@ -7060,8 +6944,7 @@ def install_python_stack() -> int:
],
)
else:
- # pip may not exist yet (uv-created venvs omit it). Try ensurepip,
- # then upgrade. Direct upgrade only when pip is already present.
+ # uv-created venvs omit pip: ensurepip, else direct upgrade.
_has_pip = (
subprocess.run(
[sys.executable, "-m", "pip", "--version"],
@@ -7124,8 +7007,7 @@ def install_python_stack() -> int:
# install.sh / install.ps1 already installed both core distributions.
pass
elif NO_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).
+ # No-torch update path: --no-deps throughout (PyPI metadata makes torch a hard dep).
_progress("base packages (no torch)")
desktop_min_ver = os.environ.get("UNSLOTH_DESKTOP_BACKEND_VERSION", "").strip()
unsloth_spec = (
@@ -7144,9 +7026,7 @@ def install_python_stack() -> int:
unsloth_spec,
"unsloth-zoo",
)
- # 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.
+ # pydantic WITH deps (all torch-free) so pip pins a matching pydantic-core.
pip_install(
"Installing pydantic (with deps for compatible core)",
"--no-cache-dir",
@@ -7184,9 +7064,6 @@ def install_python_stack() -> int:
package_name,
)
else:
- # Update path: upgrade only unsloth + unsloth-zoo, preserving existing
- # torch/CUDA installs. Torch is pre-installed by install.sh/setup.ps1;
- # --upgrade-package targets only base pkgs.
_progress("base packages")
desktop_min_ver = os.environ.get("UNSLOTH_DESKTOP_BACKEND_VERSION", "").strip()
unsloth_spec = (
@@ -7221,9 +7098,7 @@ def install_python_stack() -> int:
req = base_requirements,
)
- # 2b. AMD ROCm: reinstall torch with HIP wheels if the host has ROCm but the
- # venv got CPU-only torch (common when pip resolves torch from PyPI).
- # Must follow base packages so torch is present for inspection.
+ # 2b. Torch repair (wrong-family / CPU-only); must follow base packages so torch is present.
if not IS_MACOS and not NO_TORCH:
_progress(_torch_step_label("check"))
_ensure_cuda_torch()
@@ -7234,8 +7109,6 @@ def install_python_stack() -> int:
# CPU pin over an XPU venv would leave XPU triton under a CPU torch.
_ensure_xpu_triton()
- # Windows + AMD GPU: warn if ROCm torch was not installed (wrong Python
- # version or unknown ROCm version).
if IS_WINDOWS and not NO_TORCH and not _has_usable_nvidia_gpu():
# Validate actual AMD GPU presence (not just tool existence).
import re as _re_win
@@ -7251,10 +7124,7 @@ def install_python_stack() -> int:
_wexe = shutil.which(_wcmd[0])
if not _wexe:
continue
- # Skip amd-smi on Windows w/o a HIP SDK (avoids the UAC/DiskPart
- # prompt), as _has_rocm_gpu()/_detect_amd_gfx_codes do. The only loss
- # is the best-effort "AMD GPU detected" note; ROCm-torch state below
- # comes from the install itself.
+ # Skip amd-smi without a HIP SDK (UAC prompt); only a best-effort note is lost.
if _wcmd[0] == "amd-smi" and not _amd_smi_allowed():
continue
try:
@@ -7306,9 +7176,7 @@ def install_python_stack() -> int:
if NO_TORCH:
_progress("dependency overrides (skipped, no torch)")
elif _rocm_windows_torch_installed or _installed_torch_is_windows_rocm():
- # No working Windows ROCm torchao build: it imports an absent c10d backend
- # and crashes transformers.quantizers. Unsloth stubs it at runtime, so
- # installing it only ships a package that crashes on import -- skip it.
+ # No working Windows ROCm torchao build (crashes on import; stubbed at runtime).
_progress("dependency overrides (skipped, Windows ROCm)")
_note("Windows ROCm -- skipping torchao (no working build; stubbed at runtime)")
else:
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index 1ff9b43aa30..3c1a020c33d 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -141,13 +141,7 @@ $DefaultLlamaSource = "https://github.com/ggml-org/llama.cpp"
$DefaultLlamaTag = "latest"
$DefaultLlamaForceCompileRef = "master"
-# Corporate-mirror / proxy escape hatch for the frontend npm/bun install (#6491).
-# studio/frontend/.npmrc pins registry=https://registry.npmjs.org/ as a supply-chain
-# lock, which overrides a corporate user's ~/.npmrc proxy and causes 403s behind a
-# firewall. UNSLOTH_NPM_REGISTRY is a deliberate opt-in: when set we splat it as
-# `--registry <url>` into every npm/bun install. `--registry` is the highest-precedence
-# override for BOTH tools and leaves min-release-age / save-exact in force. Empty array
-# (the default) splats to nothing, so normal installs are unchanged.
+# UNSLOTH_NPM_REGISTRY: opt-in --registry splat past the frontend .npmrc lock (corporate proxies).
$NpmRegistryArgs = @()
if ($env:UNSLOTH_NPM_REGISTRY) {
$NpmRegistryArgs = @('--registry', $env:UNSLOTH_NPM_REGISTRY)
@@ -161,14 +155,10 @@ foreach ($a in $args) {
break
}
}
-# Propagate to child processes (e.g. install_python_stack.py) so they
-# also respect verbose mode. Process-scoped -- does not persist.
if ($script:UnslothVerbose) {
$env:UNSLOTH_VERBOSE = '1'
}
$script:LlamaCppDegraded = $false
-# CUDA toolkit state, published by Resolve-CudaToolkit. Only the Phase 4 source
-# build consumes these; the prebuilt path leaves them at these defaults.
$script:CudaToolkitReady = $false
$script:NvccPath = $null
$script:CudaToolkitRoot = $null
@@ -198,9 +188,6 @@ $IsPipInstall = -not (Test-Path $FrontendDir)
# Helper functions
# ─────────────────────────────────────────────
-# Reload ALL environment variables from registry.
-# Picks up changes made by installers (winget, msi, etc.) including
-# Path, CUDA_PATH, CUDA_PATH_V*, and any other vars they set.
function Refresh-Environment {
foreach ($level in @('Machine', 'User')) {
$vars = [System.Environment]::GetEnvironmentVariables($level)
@@ -240,9 +227,7 @@ function Refresh-Environment {
$env:Path = $unique -join ";"
}
-# ── Helper: safely add a directory to the persistent User PATH ──
-# Direct registry access preserves REG_EXPAND_SZ (avoids dotnet/runtime#1442).
-# Append (default) keeps existing tools first; Prepend for must-win entries.
+# Direct registry access preserves REG_EXPAND_SZ (dotnet/runtime#1442).
function Add-ToUserPath {
param(
[Parameter(Mandatory = $true)][string]$Directory,
@@ -309,8 +294,7 @@ function Add-ToUserPath {
return $false
}
$regKey.SetValue('Path', $newPath, [Microsoft.Win32.RegistryValueKind]::ExpandString)
- # Broadcast WM_SETTINGCHANGE via dummy env-var roundtrip.
- # [NullString]::Value avoids PS 7.5+/.NET 9 $null-to-"" coercion.
+ # Broadcast WM_SETTINGCHANGE via a dummy env-var roundtrip ([NullString]::Value).
try {
$d = "UnslothPathRefresh_$([guid]::NewGuid().ToString('N').Substring(0,8))"
[Environment]::SetEnvironmentVariable($d, '1', 'User')
@@ -603,8 +587,6 @@ function Get-InstalledLlamaPrebuiltRelease {
return $message
}
-# Find nvcc on PATH, CUDA_PATH, or standard toolkit dirs.
-# Returns the path to nvcc.exe, or $null if not found.
function Find-Nvcc {
param([string]$MaxVersion = "")
@@ -675,11 +657,8 @@ function Write-CudaDriverToolkitMismatch {
substep "Or let Unsloth use the prebuilt CUDA bundle; it does not need the local toolkit." $Color
}
-# Detect CUDA Compute Capability via nvidia-smi.
-# Returns e.g. "80" for A100 (8.0), "89" for RTX 4090 (8.9), etc.
-# Returns $null if detection fails.
function Get-CudaComputeCapability {
- # Use the resolved absolute path ($NvidiaSmiExe) to survive Refresh-Environment
+ # $NvidiaSmiExe is an absolute path that survives Refresh-Environment.
$smiExe = if ($script:NvidiaSmiExe) { $script:NvidiaSmiExe } else {
$cmd = Get-Command nvidia-smi -ErrorAction SilentlyContinue
if ($cmd) { $cmd.Source } else { $null }
@@ -687,13 +666,9 @@ function Get-CudaComputeCapability {
if (-not $smiExe) { return $null }
try {
- # Bounded: a wedged nvidia-smi must not hang setup after the initial
- # -L probe succeeded (the helper merges stderr after stdout, so the
- # first line is still the compute_cap value).
$raw = Invoke-NvidiaSmiBounded $smiExe @('--query-gpu=compute_cap', '--format=csv,noheader')
if ($LASTEXITCODE -ne 0 -or -not $raw) { return $null }
- # nvidia-smi may return multiple GPUs; take the first one
$cap = ($raw -split "`n")[0].Trim()
if ($cap -match '^(\d+)\.(\d+)$') {
$major = $Matches[1]
@@ -705,11 +680,7 @@ function Get-CudaComputeCapability {
return $null
}
-# Check if an nvcc binary supports a given sm_ architecture.
-# Uses `nvcc --list-gpu-code` which outputs sm_* tokens (--list-gpu-arch
-# outputs compute_* tokens instead). Available since CUDA 11.6.
-# Returns $false if the flag isn't supported (old toolkit) — safer to reject
-# and fall back to scanning/PTX than to assume support and fail later.
+# --list-gpu-code emits sm_* (CUDA 11.6+), --list-gpu-arch compute_*; $false falls back to PTX.
function Test-NvccArchSupport {
param([string]$NvccExe, [string]$Arch)
try {
@@ -721,8 +692,6 @@ function Test-NvccArchSupport {
}
}
-# Given an nvcc binary, return the highest sm_ architecture it supports.
-# Returns e.g. "90" for CUDA 12.4. Returns $null if detection fails.
function Get-NvccMaxArch {
param([string]$NvccExe)
try {
@@ -827,9 +796,8 @@ function Get-NvidiaCu126Verdict {
function Get-CudaFamilyCappedForPreTuring {
param([string]$Family, [string]$SmiExe)
if ($Family -notin @('cu128', 'cu130')) { return $Family }
- # Windows pins torch<2.11, whose cu128 still ships sm_70, so only cu130
- # strands a Volta here. Raise to 75 when that pin reaches 2.11.
- $legacyFloorSm = if ($Family -eq 'cu128') { 70 } else { 75 }
+ # torch 2.11.0+cu128 dropped Volta, so cu128 now strands a pre-Turing host as cu130 does.
+ $legacyFloorSm = 75
$verdict = Get-NvidiaCu126Verdict $SmiExe $legacyFloorSm
if (-not $verdict) { return $Family }
# This runs twice per setup; announce once without polluting pipeline output.
@@ -861,13 +829,7 @@ function Get-PytorchCudaTag {
if (-not $smiExe) { return "cu126" }
try {
- # Bounded: a wedged nvidia-smi must not hang setup. The helper merges
- # stderr into the returned string, matching the old 2>&1 | Out-String
- # shape (plain 2>$null leaks ErrorRecord objects in PS 5.1).
$output = Invoke-NvidiaSmiBounded $smiExe
- # Newer NVIDIA drivers (e.g. 610.x on Windows) print
- # "CUDA UMD Version: X.Y" instead of the legacy "CUDA Version: X.Y".
- # Accept both spellings so we don't fall through to the cu126 default.
if ($output -match 'CUDA(?: UMD)? Version:\s+(\d+)\.(\d+)') {
$major = [int]$Matches[1]
$minor = [int]$Matches[2]
@@ -885,8 +847,6 @@ 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()
@@ -897,9 +857,6 @@ function Trim-IndexPathSlashes {
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)
@@ -911,9 +868,7 @@ function Get-PinnedTorchIndexUrl {
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.
+# Query/fragment dropped first, so .../cu128?token=x classifies as cu128 and does not reinstall.
function Get-TorchIndexLeaf {
param([string]$Url)
if ([string]::IsNullOrWhiteSpace($Url)) { return $null }
@@ -922,64 +877,49 @@ function Get-TorchIndexLeaf {
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.
+# Redact index-URL credentials from captured output, which echoes --index-url verbatim.
function Redact-InstallOutput {
param([string]$Text)
if (-not $Text) { return $Text }
$Text = $Text -replace '(https?://)[^/@\s`]+@', '$1<redacted>@'
$Text = $Text -replace '([?&][^=\s&`]+)=[^&#\s`]+', '$1=<redacted>'
- # A #token=... fragment is as sensitive as a query; URL-anchored.
return $Text -replace '(https?://[^\s`#]+)#[^\s`]+', '$1#<redacted>'
}
-# 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.
+# _grouped_mm bug: these leaves need the torch 2.11 floor. Must match the other installers.
function Test-RocmGfx211Leaf {
param([string]$Leaf)
return @('gfx120x-all', 'gfx1151', 'gfx1150', 'gfx1152') -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.
+# rocm7.2 only; never floor an unknown newer rocm. Matches _ROCM_KNOWN_TORCH211_VERSIONS.
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.
+# A bare 'cu*' glob would match "custom"/"current" and rebuild the venv every run.
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<digits>[.<digits>] 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.
+# Anchored: rocm-rel-7.2.1 merely starts with rocm and is a custom pin the verbatim path owns.
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<d> leaf too (rocm7).
- $_pinMajorOnly = [regex]::Match($PinLeaf, '^rocm(\d+)$')
- # Installed rocm version and whether the wheel is a per-arch (three-part) build.
+ $_pinMajorOnly = [regex]::Match($PinLeaf, '^rocm(\d+)$') # major-only rocm<d> leaf (rocm7)
$_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+')
+ $_instPerArch = [regex]::IsMatch($TorchVersion, '\+rocm\d+\.\d+\.\d+') # per-arch three-part build
# 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+)')
@@ -990,8 +930,6 @@ function Get-RocmPinStaleTags {
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 }
}
@@ -1003,9 +941,6 @@ function Get-RocmPinStaleTags {
}
}
- # 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) {
@@ -1013,17 +948,14 @@ function Get-RocmPinStaleTags {
$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).
+ # An untagged wheel never satisfies a ROCm pin; an unreadable +rocm version is accepted.
$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.
+ # A +rocm7.2 wheel drifted to 2.12 shares the tag but violates the spec, so fold both.
$_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) {
@@ -1033,12 +965,8 @@ function Get-RocmPinStaleTags {
}
$_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)" }
@@ -1320,8 +1248,7 @@ function Test-CmakeSupportsGenerator {
}
function Test-CmakeListsGenerator {
- # Does `cmake --help` actually list the generator? A VS-bundled cmake can drive
- # VS 2026 below the 4.2 floor, so probe rather than trust the version. (#6473)
+ # Probe rather than trust the version: a VS-bundled cmake can drive VS 2026 below 4.2. (#6473)
param([Parameter(Mandatory)][string]$Generator)
$help = & cmake --help 2>$null | Out-String
if (-not $help) { return $false }
@@ -1340,8 +1267,6 @@ function Test-CmakeCanDriveGenerator {
}
function Add-DefaultCmakeToPath {
- # Prepend the default CMake dir so a freshly winget-installed cmake wins over an
- # older one already on PATH. $true if found. (#6473)
$cmakeDefaults = @(
"$env:ProgramFiles\CMake\bin",
"${env:ProgramFiles(x86)}\CMake\bin",
@@ -1358,10 +1283,7 @@ function Add-DefaultCmakeToPath {
}
function Get-FallbackVsGenerator {
- # Newest pre-2026 VS whose generator the current cmake can drive, for when the
- # VS 2026 generator is unusable (old/offline cmake) but an older toolchain exists.
- # vswhere first (catches non-default roots like D:\), then Program Files; matches
- # Find-VsBuildTools. Returns @{ Generator; InstallPath } or $null. (#6473)
+ # Newest pre-2026 VS whose generator this cmake can drive. vswhere first, for roots like D:\.
$knownEditions = @('BuildTools', 'Community', 'Professional', 'Enterprise', 'Preview')
# install path if it holds a usable cl.exe, else $null
@@ -1418,9 +1340,7 @@ function Get-FallbackVsGenerator {
return $null
}
-# VS version label -> cmake generator. vswhere's productLineVersion is the year for
-# VS <= 2022 but the internal major "18" for VS 2026, and dir names use either form,
-# so accept both. (VS 2026 detection adapted from @LeoBorcherding's #6038.)
+# productLineVersion is the year for VS <= 2022 but "18" for VS 2026; dir names use either form.
function Resolve-VsGeneratorFromLabel {
param([string]$Label)
if (-not $Label) { return $null }
@@ -1483,10 +1403,7 @@ function Find-VsBuildTools {
return $null
}
-# Install CMake + VS Build Tools, deferred here from Phase 1 so the prebuilt path
-# never pays for a multi-GB install. Called only when a source build is committed.
-# CMake is best-effort (build skips downstream if absent); VS Build Tools are
-# required, so exit 1 with guidance if missing. No-ops for VS when already detected.
+# Deferred from Phase 1 so the prebuilt path never pays for the multi-GB install.
function Ensure-BuildToolsForLlamaSourceBuild {
# CMake
if ($null -eq (Get-Command cmake -ErrorAction SilentlyContinue)) {
@@ -1729,12 +1646,7 @@ function Invoke-SetupCommand {
# Reset to avoid stale values from prior native commands.
$global:LASTEXITCODE = 0
if ($script:UnslothVerbose -and -not $AlwaysQuiet) {
- # 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).
- # 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.
+ # PS 5.1 turns stderr records into $? = $false even on exit 0; redact per record.
& $Command 2>&1 | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
} else {
$output = & $Command 2>&1 | Out-String
@@ -1850,10 +1762,6 @@ function substep {
}
function Show-NpmRegistryHint {
- # Print actionable guidance when a frontend/OXC npm/bun install fails and the
- # registry lock is the likely cause (corporate firewall/proxy). No-op once the
- # user has opted in via UNSLOTH_NPM_REGISTRY. We never switch registries
- # automatically -- we only guide.
if ($env:UNSLOTH_NPM_REGISTRY) { return }
$mirror = $env:NPM_CONFIG_REGISTRY
if (-not $mirror) {
@@ -2018,11 +1926,7 @@ if (-not $StageRoot) {
# ============================================
# 1a. GPU detection
# ============================================
-# ── Helper: run nvidia-smi under a timeout ──
-# A wedged NVIDIA driver can make nvidia-smi block during init or after a reset;
-# WaitForExit bounds it (mirrors Invoke-AmdSmiNoElevate below) so detection
-# cannot hang setup. No RunAsInvoker compat layer: nvidia-smi does not
-# auto-elevate. Returns combined stdout+stderr; "" on timeout/failure.
+# A wedged driver can block nvidia-smi during init or after a reset; WaitForExit bounds it.
function Invoke-NvidiaSmiBounded {
param(
[Parameter(Mandatory = $true, Position = 0)][string]$Exe,
@@ -2057,10 +1961,7 @@ function Invoke-NvidiaSmiBounded {
}
}
-# ── Helper: nvidia-smi -L lists at least one real GPU ──
-# Exit code 0 alone is not enough: a stale/driverless nvidia-smi can exit 0
-# while listing no GPU, which would mark an AMD host NVIDIA and suppress ROCm
-# detection. Require a "GPU <n>:" data row.
+# A driverless nvidia-smi exits 0 listing no GPU, so require a "GPU <n>:" row.
function Test-NvidiaSmiHasGpu {
param([Parameter(Mandatory = $true)][string]$Exe)
$out = Invoke-NvidiaSmiBounded $Exe @('-L')
@@ -2076,8 +1977,6 @@ try {
$NvidiaSmiExe = $nvSmiCmd.Source
}
} catch {}
-# Fallback: nvidia-smi may not be on PATH even though a GPU + driver exist.
-# Check the default install location and the Windows driver store.
if (-not $HasNvidiaSmi) {
$nvSmiDefaults = @(
"$env:ProgramFiles\NVIDIA Corporation\NVSMI\nvidia-smi.exe",
@@ -2096,26 +1995,17 @@ if (-not $HasNvidiaSmi) {
}
}
}
-# ── Helper: run amd-smi without triggering a UAC elevation prompt ──
-# amd-smi on Windows auto-elevates to read GPU/APU memory, surfacing a confusing
-# DiskPart UAC prompt mid-install (Unsloth backend amd.py hits the same). RunAsInvoker
-# forces it (and helpers it spawns) to run un-elevated; on failure the WMI name ->
-# gfx fallback still resolves the arch.
+# amd-smi auto-elevates to read GPU memory, popping a DiskPart UAC prompt; RunAsInvoker stops it.
function Invoke-AmdSmiNoElevate {
param(
[Parameter(Mandatory = $true, Position = 0)][string]$Exe,
[Parameter(Position = 1)][string[]]$SmiArgs = @(),
[int]$TimeoutSec = 30
)
- # RunAsInvoker blocks the auto-elevation/UAC prompt; the timeout bounds a flaky
- # amd-smi that can otherwise spin for minutes (30s mirrors the backend amd.py).
$prevCompat = [Environment]::GetEnvironmentVariable('__COMPAT_LAYER', 'Process')
$env:__COMPAT_LAYER = 'RunAsInvoker'
try {
- # [Process]::Start, NOT Start-Process -PassThru: the latter leaves .ExitCode
- # $null after WaitForExit on PS 5.1, so $LASTEXITCODE (checked by callers)
- # reads non-zero and kills detection. Async reads drain the pipes (no
- # deadlock); amd-smi args have no spaces so a plain join is safe.
+ # [Process]::Start, NOT Start-Process -PassThru, which leaves .ExitCode $null on PS 5.1.
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $Exe
$psi.Arguments = ($SmiArgs -join ' ')
@@ -2263,16 +2153,11 @@ function Resolve-ShadowingGfxPick {
return $discreteArch
}
if (-not $HasNvidiaSmi) {
- # hipinfo: PATH first, then HIP_PATH/ROCM_PATH bin fallback (mirrors NVIDIA smi path resolution).
- # AMD HIP SDK sets HIP_PATH but may not add the bin dir to PATH depending on install type.
- # Ignore the venv hipInfo.exe (AMD wheel, on PATH): not a HIP SDK, so amd-smi
- # would still auto-elevate. Cf. _path_inside_venv().
+ # Ignore the venv hipInfo.exe: an AMD wheel, not a HIP SDK, so amd-smi still auto-elevates.
function Test-HipinfoIsVenvInternal {
param([AllowNull()][string]$HipinfoPath)
if ([string]::IsNullOrWhiteSpace($HipinfoPath)) { return $false }
- # VenvDir/VIRTUAL_ENV can be unset this early (the update flow probes before
- # VenvDir is set), so also derive the venv from the setup python + default
- # Unsloth home, else the venv hipInfo isn't caught.
+ # VenvDir/VIRTUAL_ENV can be unset this early, so also derive it from the setup python.
$venvRoots = @()
if ($env:VIRTUAL_ENV) { $venvRoots += $env:VIRTUAL_ENV }
$vd = Get-Variable -Name VenvDir -ValueOnly -ErrorAction SilentlyContinue
@@ -2281,15 +2166,11 @@ if (-not $HasNvidiaSmi) {
try { $venvRoots += (Split-Path -Parent (Split-Path -Parent $env:UNSLOTH_SETUP_PYTHON)) } catch {}
}
if ($env:USERPROFILE) { $venvRoots += (Join-Path $env:USERPROFILE ".unsloth\studio\unsloth_studio") }
- # A custom Unsloth home (UNSLOTH_STUDIO_HOME / STUDIO_HOME alias) moves the
- # venv off the default path; seed it too or its hipInfo escapes the filter.
$studioHomeEnv = if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) { $env:UNSLOTH_STUDIO_HOME.Trim() } elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) { $env:STUDIO_HOME.Trim() } else { $null }
if ($studioHomeEnv) {
- # Expand a leading ~ like the canonical resolver below; else GetFullPath
- # keeps the literal ~ (cwd-relative) and the hipInfo escapes the filter.
+ # Expand a leading ~ like the canonical resolver; GetFullPath keeps it literal.
if (($studioHomeEnv -eq "~" -or $studioHomeEnv -like "~/*" -or $studioHomeEnv -like "~\*") -and -not [string]::IsNullOrWhiteSpace($env:USERPROFILE)) {
- # A bare "~" leaves an empty child path; Join-Path rejects that on
- # PS 5.1, so use USERPROFILE directly and only join a real remainder.
+ # A bare "~" leaves an empty child path Join-Path rejects on PS 5.1.
$studioHomeRest = $studioHomeEnv.Substring(1).TrimStart('/', '\')
$studioHomeEnv = if ($studioHomeRest) { Join-Path $env:USERPROFILE $studioHomeRest } else { $env:USERPROFILE }
}
@@ -2299,8 +2180,7 @@ if (-not $HasNvidiaSmi) {
foreach ($root in $venvRoots) {
if ([string]::IsNullOrWhiteSpace($root)) { continue }
try { $r = [System.IO.Path]::GetFullPath($root).TrimEnd('\', '/') } catch { continue }
- # Skip a bare drive root (e.g. a non-venv UNSLOTH_SETUP_PYTHON like
- # C:\Python311\python.exe yields C:) -- it would match every path on that drive.
+ # Skip a bare drive root; it would match every path on that drive.
if ($r -match '^[a-zA-Z]:$') { continue }
if ($hip.Equals($r, [System.StringComparison]::OrdinalIgnoreCase) -or
$hip.StartsWith($r + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) {
@@ -2309,15 +2189,11 @@ if (-not $HasNvidiaSmi) {
}
return $false
}
- # Scan all hipinfo and keep the first non-venv one (the venv copy from the
- # bnb fix could shadow a real HIP SDK's). -CommandType Application matches
- # only real executables, not a user alias/function named hipinfo.
+ # -CommandType Application skips an alias or function named hipinfo.
$hipinfoExe = Get-Command hipinfo -CommandType Application -All -ErrorAction SilentlyContinue |
Where-Object { -not (Test-HipinfoIsVenvInternal $_.Source) } |
Select-Object -First 1
if (-not $hipinfoExe) {
- # Iterate the env roots (mirrors the Python list) and take the first non-venv
- # bin\hipinfo.exe, so a venv-internal HIP_PATH can't mask a real SDK in ROCM_PATH.
$hipMissingLabel = $null; $hipMissingRoot = $null; $hipMissingCandidate = $null
foreach ($hipEnvLabel in @("HIP_PATH", "HIP_PATH_57", "ROCM_PATH")) {
$hipRoot = [Environment]::GetEnvironmentVariable($hipEnvLabel)
@@ -2345,8 +2221,7 @@ if (-not $HasNvidiaSmi) {
try {
$hipOut = & $hipinfoExe.Source 2>&1 | Out-String
if ($hipOut -match "(?i)gcnArchName") {
- # hipinfo can crash after printing gcnArchName (#6043).
- # Once the arch is printed, keep the ROCm wheel path.
+ # hipinfo can crash after printing gcnArchName (#6043); keep the ROCm path.
$HasROCm = $true
$_hipAllArches = @([regex]::Matches($hipOut, "(?im)^\s*gcnArchName\s*:\s*(\S+)") | ForEach-Object { ($_.Groups[1].Value -split ':')[0].Trim().ToLower() })
if ($_hipAllArches.Count -gt 0) {
@@ -2364,9 +2239,6 @@ if (-not $HasNvidiaSmi) {
substep "[INFO] hipinfo exited with code $LASTEXITCODE but reported gcnArchName -- treating as ROCm-capable (see #6043)" "Cyan"
}
} elseif ($LASTEXITCODE -ne 0) {
- # hipinfo ran but returned a HIP runtime error without any gcnArchName
- # output (e.g. "no ROCm-capable device detected"), or crashed before
- # printing device info.
$firstLine = ($hipOut -split '\r?\n' | Where-Object { $_.Trim() } | Select-Object -First 1)
substep "[WARN] hipinfo returned a HIP runtime error (exit $LASTEXITCODE)" "Yellow"
substep " $firstLine" "Yellow"
@@ -2374,18 +2246,7 @@ if (-not $HasNvidiaSmi) {
}
} catch {}
}
- # amd-smi fallback: HIP runtime present but hipinfo unavailable (no full HIP SDK).
- # 'list' confirms GPU visibility, 'static --asic' extracts the gfx arch hipinfo
- # would give. Critical for Strix Halo (gfx1151) and other HIP-runtime-only iGPUs.
- #
- # BUT on hosts without a working HIP runtime amd-smi elevates a child at runtime,
- # popping a UAC/DiskPart prompt RunAsInvoker can't suppress (its manifest is
- # asInvoker; even 'amd-smi version' hangs). So only probe when a HIP SDK is present
- # (hipinfo found -> un-elevated) or the user opts in; else fall through to WMI name
- # inference (enough to pick ROCm wheels + the ROCm llama.cpp prebuilt).
- # An explicit opt-out (UNSLOTH_ENABLE_AMD_SMI=0/false/no/off) wins over the HIP-SDK
- # heuristic: a HIP SDK binary with a broken runtime can still pop the prompt, so
- # $HipSdkInstalled must NOT silently re-enable it.
+ # amd-smi can pop an unsuppressable UAC prompt, so probe only with a HIP SDK or on opt-in.
$amdSmiOptOut = $env:UNSLOTH_ENABLE_AMD_SMI -match '^(?i)(0|false|no|off)$'
$amdSmiAllowed = (-not $amdSmiOptOut) -and ($HipSdkInstalled -or ($env:UNSLOTH_ENABLE_AMD_SMI -match '^(?i)(1|true|yes|on)$'))
if (-not $HasROCm -and $amdSmiAllowed) {
@@ -2395,16 +2256,7 @@ if (-not $HasNvidiaSmi) {
$smiOut = Invoke-AmdSmiNoElevate $amdSmiExe.Source @('list')
if ($LASTEXITCODE -eq 0 -and $smiOut -match "(?im)^GPU\s*[:\[]\s*\d") {
$HasROCm = $true
- # Attempt 1: newer amd-smi versions embed the gfx arch in list output.
- # Collect ALL gfx tokens in output order so that on mixed-arch systems
- # we can honour HIP_VISIBLE_DEVICES / ROCR_VISIBLE_DEVICES and pick the
- # arch for the *runtime-visible* GPU rather than always the first one.
- # Do NOT deduplicate: a dual same-arch system (e.g. two gfx1151 APUs)
- # must produce a 2-element array so HIP_VISIBLE_DEVICES=1 selects the
- # second GPU rather than triggering a false out-of-range warning.
- # Note: this mapping assumes amd-smi lists GPUs in the same order as
- # HIP enumerates them (both follow PCI bus order in practice); it may
- # give the wrong arch when GPU indices are non-contiguous (very rare).
+ # ALL gfx tokens in output order, no dedup, so HIP_VISIBLE_DEVICES indexes in.
$allGfxArches = @([regex]::Matches($smiOut, '(?i)\b(gfx\d+[a-z]?)\b') |
ForEach-Object { $_.Groups[1].Value.ToLower() })
if ($allGfxArches.Count -gt 0) {
@@ -2415,8 +2267,7 @@ if (-not $HasNvidiaSmi) {
$script:ROCmGfxArch = Resolve-ShadowingGfxPick $script:ROCmGfxArch $allGfxArches
$ROCmGpuLabel = "AMD ROCm ($script:ROCmGfxArch)"
} else {
- # Attempt 2: 'static --asic' exposes ASIC details on ROCm 6+,
- # including the GFX target needed for wheel index selection.
+ # Attempt 2: 'static --asic' exposes the GFX target on ROCm 6+.
$smiAsicOut = ""
try { $smiAsicOut = Invoke-AmdSmiNoElevate $amdSmiExe.Source @('static','--asic') } catch {}
# Every token, not just the first: this branch is reached precisely
@@ -2438,11 +2289,7 @@ if (-not $HasNvidiaSmi) {
} catch {}
}
}
- # WMI fallback: AMD GPU in device list but no HIP SDK → guide the user.
- # WMI gives a marketing name (e.g. "AMD Radeon 890M") but never a gfx arch.
- # $HasROCm is intentionally NOT set here — we cannot confirm ROCm runtime
- # support without hipinfo or amd-smi. The name is saved to $ROCmGpuLabel
- # so the name-based inference below can still attempt an arch lookup.
+ # WMI gives a marketing name, never a gfx arch, so $HasROCm stays false.
if (-not $HasROCm) {
try {
# Keep every AMD adapter, not just the first: WMI orders controllers as the
@@ -2516,9 +2363,7 @@ if (-not $HasNvidiaSmi) {
$ROCmGpuLabel = "AMD ROCm ($script:ROCmGfxArch)"
substep "gfx arch from UNSLOTH_ROCM_GFX_ARCH env override: $script:ROCmGfxArch" "Cyan"
}
- # 2. Best-effort name → arch lookup (amd-smi / WMI). Most-specific first,
- # first match wins. Covers only arches the ROCm prebuilts support
- # (gfx120X/110X/1151/1150/103X); unknown names fall back cleanly to CPU.
+ # 2. Name -> arch, most-specific first; unknown names fall back to CPU.
elseif ($ROCmGpuLabel) {
$nameArchTable = @(
@{ P = "9070|9080|R9700"; A = "gfx1201" } # RDNA 4 (Navi 48: Radeon RX 9070 XT / 9070 GRE / 9070 / 9080, Radeon AI PRO R9700)
@@ -2840,7 +2685,6 @@ if ($LongPathsEnabled) {
Write-StudioLine "Windows Long Paths not enabled (required for Triton compilation and deep dependency paths)." -ForegroundColor Yellow
Write-StudioLine " Requesting admin access to fix..." -ForegroundColor Yellow
try {
- # Spawn an elevated process to set the registry key (triggers UAC prompt)
$proc = Start-Process -FilePath "reg.exe" `
-ArgumentList 'add "HKLM\SYSTEM\CurrentControlSet\Control\FileSystem" /v LongPathsEnabled /t REG_DWORD /d 1 /f' `
-Verb RunAs -Wait -PassThru -ErrorAction Stop
@@ -2939,8 +2783,7 @@ Ensure-VCRedist
# ============================================
# 1c. CMake (only needed for a llama.cpp SOURCE build -- detection only)
# ============================================
-# Detection only: the prebuilt path needs no compiler, so do not install or exit
-# here. Ensure-BuildToolsForLlamaSourceBuild installs CMake if a source build runs.
+# Detection only; a source build gets CMake from Ensure-BuildToolsForLlamaSourceBuild.
$HasCmake = $null -ne (Get-Command cmake -ErrorAction SilentlyContinue)
if ($HasCmake) {
step "cmake" "$(cmake --version | Select-Object -First 1)"
@@ -2951,8 +2794,6 @@ if ($HasCmake) {
# ============================================
# 1d. Visual Studio Build Tools (only needed for a llama.cpp SOURCE build -- detection only)
# ============================================
-# Detection only: detect VS for a possible source build, but never install or exit
-# here. Install is deferred to Ensure-BuildToolsForLlamaSourceBuild.
$CmakeGenerator = $null
$VsInstallPath = $null
$vsResult = Find-VsBuildTools
@@ -2969,28 +2810,19 @@ if ($vsResult) {
# ============================================
# 1e. CUDA Toolkit (nvcc for llama.cpp build + env vars)
# ============================================
-# Defined here but invoked lazily right before a Phase 4 source build; the
-# prebuilt llama.cpp path needs no local toolkit. With -RequireOrExit a source
-# build is committed, so hard-fail if no driver-compatible toolkit can be found
-# or installed. Without it, detection is best-effort and only sets the flag.
+# Lazy: the prebuilt path needs no local toolkit. -RequireOrExit hard-fails when none is found.
function Resolve-CudaToolkit {
param([switch]$RequireOrExit)
-# Toolkit major must be <= the driver's max CUDA major (nvidia-smi "CUDA Version: X.Y");
-# a newer-major toolkit fails at runtime ("ggml_cuda_init: failed to initialize CUDA").
+# Toolkit major must be <= the driver's max CUDA major; a newer-major toolkit fails at runtime.
$DriverMaxCuda = $null
try {
- # Bounded: source-build toolkit resolution must not hang on a wedged smi.
- # test_resolve_cuda_toolkit.ps1 extracts this function alone into a child
- # pwsh (no Invoke-NvidiaSmiBounded in scope) and stubs nvidia-smi with a
- # .ps1 script, so fall back to direct invocation when the bounded runner
- # is unavailable; production setup.ps1 always has it defined.
+ # test_resolve_cuda_toolkit.ps1 extracts this function alone, without Invoke-NvidiaSmiBounded.
$smiOut = if (Get-Command Invoke-NvidiaSmiBounded -ErrorAction SilentlyContinue) {
Invoke-NvidiaSmiBounded $NvidiaSmiExe
} else {
& $NvidiaSmiExe 2>&1 | Out-String
}
- # Newer drivers report "CUDA UMD Version: X.Y" instead of "CUDA Version: X.Y"; accept both.
if ($smiOut -match "CUDA(?: UMD)? Version:\s+([\d]+)\.([\d]+)") {
$DriverMaxCuda = "$($Matches[1]).$($Matches[2])"
substep "driver supports up to CUDA $DriverMaxCuda"
@@ -3003,12 +2835,7 @@ if ($CudaArch) {
substep "GPU Compute Capability = $($CudaArch.Insert($CudaArch.Length-1, '.')) (sm_$CudaArch)"
}
-# -- Find a toolkit that's compatible with the driver AND the GPU --
-# Strategy: prefer the toolkit at CUDA_PATH (user's existing setup) if it's
-# compatible with the driver AND supports the GPU architecture. Only fall back
-# to scanning side-by-side installs if CUDA_PATH is missing, points to an
-# incompatible version, or can't compile for the GPU. This avoids
-# header/binary mismatches when multiple toolkits are installed.
+# Prefer CUDA_PATH when driver-compatible; scanning side-by-side installs avoids mismatches.
$IncompatibleToolkit = $null
$NvccPath = $null
@@ -3058,8 +2885,6 @@ if ($DriverMaxCuda) {
}
}
} else {
- # No side-by-side match: a major-compatible toolkit may still be on
- # PATH/CUDA_PATH/a custom dir; use it, else record it as too-new.
$AnyNvcc = Find-Nvcc
if ($AnyNvcc) {
$NvccOut = & $AnyNvcc --version 2>&1 | Out-String
@@ -3171,15 +2996,11 @@ if (-not $NvccPath) {
$CudaToolkitRoot = Split-Path (Split-Path $NvccPath -Parent) -Parent
# CUDA_PATH: used by cmake's find_package(CUDAToolkit)
[Environment]::SetEnvironmentVariable('CUDA_PATH', $CudaToolkitRoot, 'Process')
-# CudaToolkitDir: the MSBuild property that CUDA .targets checks directly
-# Trailing backslash required -- the .targets file appends subpaths to it
+# CudaToolkitDir: MSBuild property the CUDA .targets checks; trailing backslash required.
[Environment]::SetEnvironmentVariable('CudaToolkitDir', "$CudaToolkitRoot\", 'Process')
-# Always persist CUDA_PATH to User registry so the compatible toolkit is used
-# in future sessions (overwrites any existing value pointing to a newer, incompatible version)
[Environment]::SetEnvironmentVariable('CUDA_PATH', $CudaToolkitRoot, 'User')
substep "Persisted CUDA_PATH=$CudaToolkitRoot to user environment"
-# Clear all versioned CUDA_PATH_V* env vars in this process to prevent
-# cmake/MSBuild from discovering a conflicting CUDA installation.
+# Clear versioned CUDA_PATH_V* vars so cmake/MSBuild cannot find a conflicting install.
$cudaPathVars = @([Environment]::GetEnvironmentVariables('Process').Keys | Where-Object { $_ -match '^CUDA_PATH_V' })
foreach ($v in $cudaPathVars) {
[Environment]::SetEnvironmentVariable($v, $null, 'Process')
@@ -3201,10 +3022,7 @@ if (Add-ToUserPath -Directory $nvccBinDir -Position 'Prepend') {
substep "Persisted CUDA bin dir to user PATH"
}
-# -- Ensure CUDA ↔ Visual Studio integration files exist --
-# When CUDA is installed before VS Build Tools (or VS is reinstalled after CUDA),
-# the MSBuild .targets/.props files that let VS compile .cu files are missing.
-# cmake fails with "No CUDA toolset found". Fix: copy from CUDA extras dir.
+# CUDA installed before VS Build Tools leaves .targets missing: "No CUDA toolset found".
if ($VsInstallPath -and $CudaToolkitRoot) {
$vsCustomizations = Get-VcBuildCustomizationsDir -VsInstallPath $VsInstallPath -Generator $CmakeGenerator
$cudaExtras = Join-Path $CudaToolkitRoot "extras\visual_studio_integration\MSBuildExtensions"
@@ -3216,7 +3034,6 @@ if ($VsInstallPath -and $CudaToolkitRoot) {
Copy-Item "$cudaExtras\*" $vsCustomizations -Force -ErrorAction Stop
substep "CUDA VS integration files installed"
} catch {
- # Direct copy failed (needs admin). Try elevated copy via Start-Process.
try {
$copyCmd = "Copy-Item '$cudaExtras\*' '$vsCustomizations' -Force"
Start-Process powershell -ArgumentList "-NoProfile -Command $copyCmd" -Verb RunAs -Wait -ErrorAction Stop
@@ -3243,8 +3060,6 @@ step "cuda" $NvccPath
substep "CUDA_PATH = $CudaToolkitRoot"
substep "CudaToolkitDir = $CudaToolkitRoot\"
-# $CudaArch was detected earlier (before toolkit selection) so it could
-# influence which toolkit we picked. Just log the final state here.
if (-not $CudaArch) {
substep "could not detect compute capability -- cmake will use defaults" "Yellow"
}
@@ -3259,8 +3074,6 @@ if ($HasROCm -and -not $script:ROCmUnsupportedGfxArch) {
$rocmVerLabel = if ($script:ROCmVersionFull) { "ROCm $script:ROCmVersionFull" } elseif ($script:ROCmVersion) { "ROCm $script:ROCmVersion" } else { "ROCm (version unknown)" }
step "rocm" $rocmVerLabel
} elseif ($script:ROCmGfxArch) {
- # GPU training/inference works via AMD's bundled-runtime ROCm PyTorch wheels;
- # the HIP SDK is optional (only the system ROCm toolchain).
step "rocm" "GPU via bundled ROCm wheels ($script:ROCmGfxArch) -- HIP SDK optional" "Cyan"
} elseif ($script:ROCmUnsupportedGfxArch) {
# Naming the HIP SDK here would read as "install it and this resolves"; on a
@@ -3273,8 +3086,6 @@ if ($HasROCm -and -not $script:ROCmUnsupportedGfxArch) {
# ============================================
# 1f. Node.js / npm (skip if pip-installed or Tauri -- only needed for frontend build)
# ============================================
-# Frontend and OXC share this Node floor. The helper returns:
-# system | bundled | skip.
function Get-NodeDecision {
param(
[string]$NodeVersion, # `node -v` output, e.g. v22.17.1 (or empty)
@@ -3357,11 +3168,7 @@ if (-not $IsPipInstall) {
}
$NodeDir = Join-Path $NodeParent "node"
- # Probe system node/npm without letting a missing/broken command abort setup.
- # Under $ErrorActionPreference = "Stop" a bare `node -v` for an absent node
- # throws a terminating error `2>$null` cannot swallow, and a present-but-broken
- # shim throws too. Guard with Get-Command (node/npm independently) + try/catch;
- # empty version => Get-NodeDecision returns "bundled".
+ # Under -ErrorAction Stop a bare `node -v` throws past 2>$null; empty means "bundled".
$SysNodeVersion = try { if (Get-Command node -ErrorAction SilentlyContinue) { (node -v 2>$null) } else { "" } } catch { "" }
$SysNpmVersion = try { if (Get-Command npm -ErrorAction SilentlyContinue) { (npm -v 2>$null) } else { "" } } catch { "" }
$NodeSource = Get-NodeDecision -NodeVersion "$SysNodeVersion" -NpmVersion "$SysNpmVersion" -SkipInstall "$($env:UNSLOTH_SKIP_NODE_INSTALL)"
@@ -3372,8 +3179,6 @@ if ($IsPipInstall) {
} elseif ($SkipFrontend) {
step "frontend" "bundled (Tauri)"
} else {
- # Stale npm used to trigger system Node changes. Keep this process-local
- # and provision only when the build or OXC needs Node.
if ($NodeSource -eq "system") {
substep "Node $SysNodeVersion and npm $SysNpmVersion already meet requirements (system)."
} elseif ($NodeSource -eq "bundled") {
@@ -3383,9 +3188,7 @@ if ($IsPipInstall) {
}
}
-# Conda CPython ships modified DLL search paths that break torch's c10.dll
-# loading on Windows; a venv made from conda Python inherits its base_prefix,
-# so check the executable path AND sys.base_prefix.
+# Conda CPython breaks torch's c10.dll, and a venv from it inherits base_prefix, so check both.
$CondaSkipPattern = '(?i)(conda|miniconda|anaconda|miniforge|mambaforge)'
function Test-IsConda {
param([string]$Exe)
@@ -3397,18 +3200,12 @@ function Test-IsConda {
return $false
}
-# 1g. Python (>= 3.11 and < 3.14). Prefer the interpreter install.ps1 already
-# resolved and built the venv with (UNSLOTH_SETUP_PYTHON), or the existing
-# venv python, before re-probing a system where a 3.14 or a WindowsApps stub
-# ahead on PATH would trip the gate. setup.ps1 only updates packages in that
-# venv, so the handoff is safe to reuse once validated.
+# 1g. Python (>= 3.11, < 3.14). Prefer the validated venv python over a stub-led PATH probe.
function Resolve-ReusedSetupPython {
if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_SETUP_PYTHON) -and
(Test-Path -LiteralPath $env:UNSLOTH_SETUP_PYTHON)) {
return $env:UNSLOTH_SETUP_PYTHON
}
- # Standalone `unsloth studio setup/update` (install.ps1 did not run): derive
- # the venv python from the studio root, mirroring the resolver below.
$root = if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_STUDIO_HOME)) { $env:UNSLOTH_STUDIO_HOME.Trim() }
elseif (-not [string]::IsNullOrWhiteSpace($env:STUDIO_HOME)) { $env:STUDIO_HOME.Trim() }
else { Join-Path $env:USERPROFILE ".unsloth\studio" }
@@ -3465,10 +3262,7 @@ if ($ReusedSetupPython) {
}
}
-# Fall back to every py.exe on PATH (all-users and per-user launchers can both
-# register). -All is required: Windows PowerShell 5.1 returns only the first
-# launcher without it, and the PowerShell 7 multi-match array breaks the call
-# operator if used directly.
+# -All is required: PS 5.1 returns only the first py launcher without it.
$PyLaunchers = if ($PythonOk) { @() } else { @(Get-Command py -All -CommandType Application -ErrorAction SilentlyContinue) }
foreach ($PyLauncher in $PyLaunchers) {
@@ -3478,9 +3272,7 @@ foreach ($PyLauncher in $PyLaunchers) {
$out = & $PyLauncher.Source "-$minor" --version 2>&1 | Out-String
if ($out -match 'Python (3\.\d+\.\d+)') {
$DetectedPyVer = $Matches[1]
- # Make `python` resolvable for the rest of setup. Without this,
- # py-launcher-only installs (no python.exe on PATH) pass the gate
- # and then crash on the first bare `python` call below.
+ # Make `python` resolvable: a py-launcher-only install would crash on bare `python`.
try {
$resolvedExe = (& $PyLauncher.Source "-$minor" -c "import sys; print(sys.executable)" 2>$null | Select-Object -First 1)
if ($resolvedExe -and (Test-Path $resolvedExe)) {
@@ -3541,7 +3333,6 @@ $ScriptsDir = python -c "import os, sysconfig; p = sysconfig.get_path('scripts',
if ($LASTEXITCODE -eq 0 -and $ScriptsDir -and (Test-Path $ScriptsDir)) {
# Append (not Prepend) -- this dir has other pip scripts; shim handles unsloth.
if (Add-ToUserPath -Directory $ScriptsDir) {
- # Also add to current process so it's available immediately
$ProcessPathEntries = $env:PATH.Split(';')
if (-not ($ProcessPathEntries | Where-Object { $_.TrimEnd('\') -eq $ScriptsDir })) {
$env:PATH = "$ScriptsDir;$env:PATH"
@@ -3599,9 +3390,7 @@ if ($SkipFrontend) {
}
}
-# Provision Node when the frontend build OR the OXC runtime install needs it (the
-# OXC `npm install` runs whenever its dir exists, regardless of dist staleness);
-# never eagerly. System Node is used read-only; the isolated one is ours.
+# Provision Node only when the frontend build or OXC needs it; system Node stays read-only.
$NeedNodeForSetup = (-not $IsPipInstall) -and ($NeedFrontendBuild -or (Test-Path $OxcValidatorDir))
if ($NeedNodeForSetup) {
if ($NodeSource -eq "skip") {
@@ -3613,8 +3402,7 @@ if ($NeedNodeForSetup) {
substep "install a suitable Node + npm, or unset UNSLOTH_SKIP_NODE_INSTALL to let Unsloth manage an isolated Node" "Yellow"
} elseif ($NodeSource -eq "bundled") {
New-Item -ItemType Directory -Force -Path $NodeParent -ErrorAction SilentlyContinue | Out-Null
- # Minimal ownership guard for a custom-home dir (the full Unsloth-owned
- # helpers are defined later); never os.replace over a user-owned dir.
+ # Minimal ownership guard; never os.replace over a user-owned dir.
if ($NodeOverride -and (Test-Path -LiteralPath $NodeDir -PathType Container)) {
$nodeOwnedMarker = Join-Path $NodeDir ".unsloth-studio-owned"
$nodeMeta = Join-Path $NodeDir "UNSLOTH_NODE_PREBUILT_INFO.json"
@@ -3625,8 +3413,7 @@ if ($NeedNodeForSetup) {
}
}
substep "installing isolated Node (system Node/npm left untouched)..."
- # The main Python resolver runs later; bare `python` may be a Store stub or
- # absent this early, so prefer the validated handed-off/venv Python.
+ # Prefer the validated handed-off/venv Python: bare `python` may be a Store stub here.
$NodeInstallPython = if ($ValidatedSetupPython) { $ValidatedSetupPython } else { "python" }
$nodeOut = & $NodeInstallPython "$PSScriptRoot\install_node_prebuilt.py" --install-dir $NodeDir 2>&1 | Out-String
$nodeExit = $LASTEXITCODE
@@ -3643,8 +3430,6 @@ if ($NeedNodeForSetup) {
if ($NodeOverride -and (Test-Path -LiteralPath $NodeDir -PathType Container)) {
New-Item -ItemType File -Force -Path (Join-Path $NodeDir ".unsloth-studio-owned") -ErrorAction SilentlyContinue | Out-Null
}
- # Windows Node zip ships node.exe + npm.cmd at the root; prepend it (this
- # process only) so node/npm/bun resolve here for the build.
$env:PATH = "$NodeDir;" + $env:PATH
# Keep npm and module resolution inside the isolated Node.
$env:NPM_CONFIG_PREFIX = $NodeDir
@@ -3660,8 +3445,7 @@ if ($NeedNodeForSetup) {
Invoke-SetupCommand { npm install -g bun --allow-scripts=bun @NpmRegistryArgs } | Out-Null
$ErrorActionPreference = $prevEAP_bun
Refresh-Environment
- # Refresh-Environment rebuilds PATH (Machine;User;current), demoting the
- # isolated-Node prepend; re-prepend so it wins for the build and OXC step.
+ # Refresh-Environment demotes the isolated-Node prepend; re-prepend so it wins.
$env:PATH = "$NodeDir;" + $env:PATH
$env:NPM_CONFIG_PREFIX = $NodeDir
$env:npm_config_prefix = $NodeDir
@@ -3673,8 +3457,6 @@ if ($NeedNodeForSetup) {
}
}
} else {
- # system Node already satisfies requirements; use it as-is. We do NOT
- # install global packages (bun) here -- the build falls back to npm.
step "node" "$SysNodeVersion | npm $SysNpmVersion (system)"
}
}
@@ -3682,11 +3464,7 @@ if ($NeedFrontendBuild -and -not $IsPipInstall) {
Write-StudioLine ""
substep "building frontend..."
- # ── Tailwind v4 .gitignore workaround ──
- # Tailwind v4's oxide scanner respects .gitignore in parent directories.
- # Python venvs create a .gitignore with "*" (ignore everything), which
- # prevents Tailwind from scanning .tsx source files for class names.
- # Temporarily hide any such .gitignore during the build, then restore it.
+ # Tailwind v4's oxide scanner respects parent .gitignore, and a venv's "*" hides the .tsx files.
$HiddenGitignores = @()
$WalkDir = (Get-Item $FrontendDir).Parent.FullName
while ($WalkDir -and $WalkDir -ne [System.IO.Path]::GetPathRoot($WalkDir)) {
@@ -3703,28 +3481,20 @@ if ($NeedFrontendBuild -and -not $IsPipInstall) {
$WalkDir = Split-Path $WalkDir -Parent
}
- # Use bun if available (faster install), fall back to npm.
- # Bun is used only as package manager; Node runs the actual build (Vite 8).
$prevEAP_npm = $ErrorActionPreference
$ErrorActionPreference = "Continue"
Push-Location $FrontendDir
$UseBun = $null -ne (Get-Command bun -ErrorAction SilentlyContinue)
- # bun's package cache can become corrupt -- packages get stored with only
- # metadata but no actual content (bin/, lib/). When this happens bun install
- # exits 0 but leaves binaries missing. We validate after install and clear
- # the cache + retry once before falling back to npm.
+ # A corrupt bun cache still exits 0, so validate, clear it, retry once, then fall back to npm.
if ($UseBun) {
Write-StudioLine " Using bun for package install (faster)" -ForegroundColor DarkGray
$bunExit = Invoke-SetupCommand { bun install @NpmRegistryArgs }
- # On Windows, .bin/ entries vary by package manager:
- # npm → tsc, tsc.cmd, tsc.ps1
- # bun → tsc.exe, tsc.bunx
+ # .bin/ entries vary by manager (npm: tsc/.cmd/.ps1; bun: .exe/.bunx).
$hasTsc = (Test-Path "node_modules\.bin\tsc") -or (Test-Path "node_modules\.bin\tsc.cmd") -or (Test-Path "node_modules\.bin\tsc.exe") -or (Test-Path "node_modules\.bin\tsc.bunx")
$hasVite = (Test-Path "node_modules\.bin\vite") -or (Test-Path "node_modules\.bin\vite.cmd") -or (Test-Path "node_modules\.bin\vite.exe") -or (Test-Path "node_modules\.bin\vite.bunx")
if ($bunExit -eq 0 -and $hasTsc -and $hasVite) {
- # bun install succeeded and critical binaries are present
} elseif ($bunExit -eq 0) {
Write-StudioLine " bun install exited 0 but critical binaries are missing, clearing cache and retrying..." -ForegroundColor Yellow
if (Test-Path "node_modules") {
@@ -3762,7 +3532,6 @@ if ($NeedFrontendBuild -and -not $IsPipInstall) {
}
}
- # Always use npm to run the build (Node runtime — avoids bun Windows runtime issues)
$buildExit = Invoke-SetupCommand { npm run build }
if ($buildExit -ne 0) {
Pop-Location
@@ -3806,8 +3575,6 @@ if ((Test-Path $OxcValidatorDir) -and $NodeSource -ne "skip" -and (Get-Command n
$ErrorActionPreference = $prevEAP_oxc
step "oxc runtime" "installed"
} elseif ((Test-Path $OxcValidatorDir) -and $NodeSource -ne "skip") {
- # No npm on PATH (e.g. a pip install with no system Node and no isolated Node
- # provisioned). Skip rather than abort; the runtime resolver degrades. Mirrors setup.sh.
substep "OXC validator runtime skipped (no npm found); code validation degrades until Node is available" "Yellow"
}
@@ -3822,14 +3589,8 @@ Remove-AgentInstructionFiles -Roots @(
Write-StudioLine ""
substep "setting up Python environment..."
-# Find Python -- skip Anaconda/Miniconda distributions ($CondaSkipPattern and
-# Test-IsConda are defined above the 1g gate). Standalone CPython (python.org,
-# winget, uv) does not have conda's torch c10.dll loading issue.
$PythonCmd = $null
-# 0. Reuse the interpreter install.ps1 already resolved and built the venv with
-# (UNSLOTH_SETUP_PYTHON, or the existing venv python) before probing the
-# system -- it is already validated as supported and non-conda.
if ($ReusedSetupPython) {
try {
$out = & $ReusedSetupPython --version 2>&1 | Out-String
@@ -3842,10 +3603,6 @@ if ($ReusedSetupPython) {
} catch { }
}
-# 1. Try the Python Launcher (py.exe) first -- most reliable on Windows.
-# Enumerate every launcher with -All (Windows PowerShell 5.1 returns only
-# the first match without it) and search each for a supported, non-conda
-# interpreter.
$PyLaunchersResolve = if ($PythonCmd) { @() } else { @(Get-Command py -All -CommandType Application -ErrorAction SilentlyContinue) }
foreach ($pyLauncher in $PyLaunchersResolve) {
if ($pyLauncher.Source -match $CondaSkipPattern) { continue }
@@ -3855,8 +3612,7 @@ foreach ($pyLauncher in $PyLaunchersResolve) {
if ($out -match 'Python 3\.(\d+)') {
$pyMinor = [int]$Matches[1]
if ($pyMinor -ge 11 -and $pyMinor -le 13) {
- # Resolve the actual executable path so venv creation
- # does not re-resolve back to a conda interpreter.
+ # Resolve the actual executable so venv creation does not re-resolve to conda.
$resolvedExe = (& $pyLauncher.Source "-$minor" -c "import sys; print(sys.executable)" 2>$null | Out-String).Trim()
if ($resolvedExe -and (Test-Path $resolvedExe) -and -not (Test-IsConda $resolvedExe)) {
$PythonCmd = $resolvedExe
@@ -3869,8 +3625,6 @@ foreach ($pyLauncher in $PyLaunchersResolve) {
if ($PythonCmd) { break }
}
-# 2. Fall back to scanning python3.x / python3 / python on PATH.
-# Use Get-Command -All to look past conda entries.
if (-not $PythonCmd) {
foreach ($candidate in @("python3.13", "python3.12", "python3.11", "python3", "python")) {
foreach ($cmdInfo in @(Get-Command $candidate -All -ErrorAction SilentlyContinue)) {
@@ -4169,9 +3923,7 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
# 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.
+ # Keep the pinned leaf specific: a generic "rocm" masks a rocm6.4 -> gfx1151 change.
$_rocmTags = Get-RocmPinStaleTags -PinLeaf $_pinLeaf -TorchVersion $torchVer
$expectedTorchTag = $_rocmTags.Expected
$installedTorchTag = $_rocmTags.Installed
@@ -4180,8 +3932,7 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
# is caught; /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.
+ # Custom index whose leaf is not a torch flavor (/simple mirror): never stale.
$_expectedKnown = $false
$expectedTorchTag = $installedTorchTag
}
@@ -4219,9 +3970,7 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
}
}
- # 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.
+ # A stale venv whose torch still imports is repaired in place; only a broken venv wipes.
if ($shouldRebuild -and $_pinnedIdx -and $installedTorchTag) {
substep "Torch-index pin changed ($installedTorchTag) -- reinstalling torch from the pin in place." "Cyan"
$script:PinChangedForceReinstall = $true
@@ -4424,10 +4173,7 @@ if (-not (Test-Path -LiteralPath $VenvDir)) {
}
}
-# pip and python write to stderr even on success (progress bars, warnings).
-# With $ErrorActionPreference = "Stop" (set at top of script), PS 5.1
-# converts stderr lines into terminating ErrorRecords, breaking output.
-# Lower to "Continue" for the pip/python section.
+# pip/python write to stderr even on success, which -ErrorAction Stop makes terminating on PS 5.1.
$prevEAP = $ErrorActionPreference
$ErrorActionPreference = "Continue"
@@ -4742,11 +4488,7 @@ Assert-VenvActivated -VenvDir $VenvDir
# Helper: install a package, preferring uv with pip fallback
function Fast-Install {
param([Parameter(ValueFromRemainingArguments=$true)]$Args_)
- # 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.
+ # An explicit --index-url must win: inherited index vars pull CPU torch over GPU (#6898).
$saved = @{}
$pinned = @($Args_) -contains '--index-url'
if ($pinned) {
@@ -4758,8 +4500,7 @@ function Fast-Install {
Remove-Item "Env:$n" -ErrorAction SilentlyContinue
}
$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).
+ # PIP_CONFIG_FILE = 'nul' stops a `pip config` extra-index-url reaching the pip fallback.
$env:PIP_CONFIG_FILE = 'nul'
}
try {
@@ -4843,9 +4584,7 @@ sys.exit(2 if conflict else (0 if version else 1))
} elseif ($InstalledVer -and $LatestVer -and ($InstalledVer -eq $LatestVer)) {
step "python" "$_PkgName $InstalledVer is up to date"
$SkipPythonDeps = $true
- # A pre-#6483-fix install can be stuck on anyio>=4.14 even though
- # $_PkgName itself is current; the fast path above would otherwise
- # never reach install_python_stack's anyio repair (#6797).
+ # A pre-#6483 install stuck on anyio>=4.14 would skip the repair (#6797), so force it.
$_anyioBad = $false
try {
& python -c "
@@ -5052,12 +4791,7 @@ if ($script:UnslothVerbose) {
Fast-Install --upgrade pip | Out-Null
}
-# Pre-install PyTorch with CUDA support.
-# On Windows, the default PyPI torch wheel is CPU-only.
-# We need PyTorch's CUDA index to get GPU-enabled wheels.
-# PyTorch bundles its own CUDA runtime, so this works regardless
-# of whether the CUDA Toolkit is installed yet.
-# The CUDA tag is chosen based on the driver's max supported CUDA version.
+# The default Windows PyPI torch wheel is CPU-only; CUDA-index wheels bundle their own runtime.
# Triton/inductor filenames are long and can hit Windows MAX_PATH (260). With long
# paths on, cache under Unsloth home; else use a short drive-root dir for headroom.
@@ -5073,8 +4807,6 @@ if (-not $StageRoot) {
}
substep "TORCHINDUCTOR_CACHE_DIR set to $TorchCacheDir (avoids MAX_PATH issues)"
-# 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) {
@@ -5105,21 +4837,10 @@ if ($PinnedTorchIndexUrl) {
$CuTag = "cpu"
}
-# ── GPU arch → newest compatible Windows ROCm wheel release ──
-# Wheels bundle their own ROCm runtime; the installed HIP SDK version does
-# not constrain which release to use. Always picks the newest release that
-# supports the GPU architecture.
-# ── AMD Windows ROCm torch override ──────────────────────────────────────────
-# Uses AMD's arch-specific pip index (repo.amd.com/rocm/whl/{arch}/).
-# Wheels bundle their own ROCm runtime; HIP SDK version is irrelevant.
+# AMD's arch-specific pip index (repo.amd.com/rocm/whl/{arch}/); wheels bundle their runtime.
$ROCmGfxArch = $script:ROCmGfxArch
$ROCmIndexUrl = $null
-# Install AMD ROCm PyTorch wheels when ROCm is confirmed OR a gfx arch is known
-# (name-inferred on Adrenalin-only hosts). The per-arch wheels bundle the runtime
-# (rocm-sdk-libraries-<gfx>), so torch.cuda.is_available() is True without a HIP
-# 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.
+# Also on a name-inferred gfx: the wheels bundle the runtime, so no HIP SDK is needed.
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" }
# gfx120X and Strix have a null _grouped_mm kernel on torch <2.11.0.
@@ -5130,13 +4851,7 @@ if (-not $TorchIndexPinned -and ($HasROCm -or $ROCmGfxArch) -and $CuTag -eq "cpu
"gfx1151" = "torch>=2.11.0,<2.12.0"; "gfx1150" = "torch>=2.11.0,<2.12.0"
"gfx1152" = "torch>=2.11.0,<2.12.0"
}
- # Companion ranges for torchvision/torchaudio -- must stay in sync with the
- # torch ceiling so pip can always find a consistent trio on AMD's per-arch
- # index. AMD publishes each package independently and may add a newer
- # torchvision (e.g. 0.27 for torch 2.12) before removing 0.26, which would
- # cause pip to resolve an ABI-incompatible set if these are left bare.
- # Matches _ROCM_TORCH_PKG_SPECS["rocm7.2"] in install_python_stack.py.
- # Bump all three ceilings together when torch 2.12.x is validated.
+ # Companions bounded to the torch ceiling for a consistent trio (AMD publishes each alone).
$torchvisionFloorMap = @{
"gfx1201" = "torchvision>=0.26.0,<0.27.0"; "gfx1200" = "torchvision>=0.26.0,<0.27.0"
"gfx1151" = "torchvision>=0.26.0,<0.27.0"; "gfx1150" = "torchvision>=0.26.0,<0.27.0"
@@ -5154,33 +4869,23 @@ if (-not $TorchIndexPinned -and ($HasROCm -or $ROCmGfxArch) -and $CuTag -eq "cpu
if ($archFamily) {
$ROCmIndexUrl = "$amdIndexBase/$archFamily/"
} elseif ($ROCmGfxArch) {
- # GPU arch detected but not in the supported wheel map — warn explicitly
- # so the user knows why they are getting CPU PyTorch instead of ROCm.
substep "[WARN] AMD GPU ($ROCmGfxArch) not in supported arch list -- falling back to CPU-only PyTorch" "Yellow"
substep " Supported: gfx1200/1201 (RDNA 4), gfx1150/1151/1152 (RDNA 3.5), gfx1100-1103 (RDNA 3), gfx1030-1036 (RDNA 2), gfx90a, gfx908" "Yellow"
} else {
- # HIP SDK present ($HasROCm=true via amd-smi) but gcnArchName was not
- # readable — warn rather than silently falling back to CPU PyTorch.
substep "[WARN] AMD GPU detected (HIP SDK present) but GPU arch could not be read -- falling back to CPU-only PyTorch" "Yellow"
substep " Arch detection requires hipinfo to report gcnArchName. Re-install the HIP SDK if this is unexpected." "Yellow"
}
}
-# 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.
+# A pinned gfx*/rocm index skips the reroute; CUDA would resolve a known-bad wheel there.
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.
+ # Anchor ($) so a suffixed custom leaf (rocm7.2-private) falls through to verbatim install.
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.
+ # Reuse Test-RocmGfx211Leaf so this allowlist and the stale-venv check cannot diverge.
$_pinGfx211 = Test-RocmGfx211Leaf $_pinLeaf
if ($_pinGfx211 -or $_pinRocm211) {
$ROCmIndexUrl = $PinnedTorchIndexUrl
@@ -5189,9 +4894,6 @@ if ($TorchIndexPinned -and -not $ROCmIndexUrl -and $PinnedTorchIndexUrl) {
$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<digits> 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"
@@ -5201,8 +4903,7 @@ if ($TorchIndexPinned -and -not $ROCmIndexUrl -and $PinnedTorchIndexUrl) {
$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.
+# On a ROCm-install failure the fallback uses the CPU index, not the ROCm pin.
$TorchInstallIndexUrl = if ($ROCmIndexUrl) { "$PyTorchWhlBase/cpu" } elseif ($PinnedTorchIndexUrl) { $PinnedTorchIndexUrl } else { "$PyTorchWhlBase/$CuTag" }
# Declared outside the guard: the bitsandbytes and Triton passes read it from outside too, and
@@ -5227,16 +4928,35 @@ if ($ROCmIndexUrl) {
if ($ROCmTorchSpec -ne "torch") {
substep " enforcing $ROCmTorchSpec $ROCmVisionSpec $ROCmAudioSpec (known _grouped_mm bug in older wheels)" "Cyan"
}
- # Built above the verbose branch: a splat assigned inside it is unset on the other.
- $_rocmTrio = @($ROCmTorchSpec, $ROCmVisionSpec, $ROCmAudioSpec)
- if ($WinArm64NoAudio) { $_rocmTrio = @($ROCmTorchSpec, $ROCmVisionSpec) }
- if ($script:UnslothVerbose) {
- Fast-Install @_rocmTrio --force-reinstall --index-url $ROCmIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
- $torchInstallExit = $LASTEXITCODE
- $output = ""
- } else {
- $output = Fast-Install @_rocmTrio --force-reinstall --index-url $ROCmIndexUrl | Out-String
- $torchInstallExit = $LASTEXITCODE
+ # Release preservation: keep UNSLOTH_KEPT_TORCH unless it conflicts with a >=2.11 floor.
+ $_rocmKeptActive = $false
+ $_rocmOrigTorch = $ROCmTorchSpec; $_rocmOrigVision = $ROCmVisionSpec; $_rocmOrigAudio = $ROCmAudioSpec
+ if ($env:UNSLOTH_KEPT_TORCH -match '^\d+\.\d+(\.\d+)?$') {
+ $_keptMinor = [int](($env:UNSLOTH_KEPT_TORCH -split '\.')[1])
+ if (-not ($ROCmTorchSpec -match 'torch>=2\.11' -and $_keptMinor -lt 11)) {
+ $ROCmTorchSpec = "torch==$($env:UNSLOTH_KEPT_TORCH)"
+ $ROCmVisionSpec = "torchvision==0.$($_keptMinor + 15).*"
+ $ROCmAudioSpec = "torchaudio==2.$($_keptMinor).*"
+ $_rocmKeptActive = $true
+ }
+ }
+ while ($true) {
+ # Built here, not in the verbose branch (a splat assigned there is unset on the other path).
+ $_rocmTrio = @($ROCmTorchSpec, $ROCmVisionSpec, $ROCmAudioSpec)
+ if ($WinArm64NoAudio) { $_rocmTrio = @($ROCmTorchSpec, $ROCmVisionSpec) }
+ if ($script:UnslothVerbose) {
+ Fast-Install @_rocmTrio --force-reinstall --index-url $ROCmIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
+ $torchInstallExit = $LASTEXITCODE
+ $output = ""
+ } else {
+ $output = Fast-Install @_rocmTrio --force-reinstall --index-url $ROCmIndexUrl | Out-String
+ $torchInstallExit = $LASTEXITCODE
+ }
+ if ($torchInstallExit -eq 0 -or -not $_rocmKeptActive) { break }
+ substep "[WARN] torch==$($env:UNSLOTH_KEPT_TORCH) not installable on this ROCm index -- using the supported release" "Yellow"
+ $ROCmTorchSpec = $_rocmOrigTorch; $ROCmVisionSpec = $_rocmOrigVision; $ROCmAudioSpec = $_rocmOrigAudio
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ $_rocmKeptActive = $false
}
if ($torchInstallExit -ne 0) {
Write-StudioLine "[WARN] AMD ROCm PyTorch install failed -- falling back to CPU" -ForegroundColor Yellow
@@ -5244,7 +4964,7 @@ if ($ROCmIndexUrl) {
$ROCmIndexUrl = $null
$ROCmCpuFallback = $true
} else {
- # Tell install_python_stack.py to skip probe + suppress manual-install warning.
+ # Tell install_python_stack.py to skip the probe and the manual-install warning.
$env:UNSLOTH_ROCM_TORCH_INSTALLED = "1"
substep "GPU ROCm PyTorch installed ($ROCmGfxArch) -- training and GPU inference will use the GPU" "Cyan"
}
@@ -5299,11 +5019,7 @@ if ($XpuIndexUrl) {
if (-not $ROCmIndexUrl -and -not $XpuIndexUrl -and ($CuTag -eq "cpu" -or $ROCmCpuFallback -or $XpuCpuFallback)) {
substep "installing PyTorch (CPU-only)..."
- # 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.
+ # force-reinstall after a ROCm fallback; build the array directly, as an @splat enumerates chars.
$cpuForce = @()
if ($ROCmCpuFallback) { $cpuForce = @("--force-reinstall") }
# Same after an Intel XPU fallback: a partial +xpu torch satisfies the CPU torch>= range.
@@ -5315,8 +5031,7 @@ if (-not $ROCmIndexUrl -and -not $XpuIndexUrl -and ($CuTag -eq "cpu" -or $ROCmCp
# rescued from disk and still reads "cpu", so the range is satisfied and it is kept.
if ($script:TorchImportDefinitivelyFailed) { $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).
+ # index serves newer torch and _ensure_cpu_torch keeps any CPU build. Unpinned keeps the bare trio.
$cpuTorchSpec = "torch"; $cpuVisionSpec = "torchvision"; $cpuAudioSpec = "torchaudio"
if ($TorchIndexPinned) {
$cpuTorchSpec = "torch>=2.4,<2.12.0"
@@ -5329,15 +5044,32 @@ if (-not $ROCmIndexUrl -and -not $XpuIndexUrl -and ($CuTag -eq "cpu" -or $ROCmCp
$cpuVisionSpec = "torchvision>=0.19,<0.27.0"
$cpuAudioSpec = "torchaudio>=2.4,<2.12.0"
}
- $_torchTrio = @($cpuTorchSpec, $cpuVisionSpec, $cpuAudioSpec)
- if ($WinArm64NoAudio) { $_torchTrio = @($cpuTorchSpec, $cpuVisionSpec) }
- if ($script:UnslothVerbose) {
- Fast-Install @_torchTrio @cpuForce --index-url $TorchInstallIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
- $torchInstallExit = $LASTEXITCODE
- $output = ""
- } else {
- $output = Fast-Install @_torchTrio @cpuForce --index-url $TorchInstallIndexUrl | Out-String
- $torchInstallExit = $LASTEXITCODE
+ # Release preservation: the kept release resolves to the right cpu build off the /cpu index.
+ $_cpuKeptActive = $false
+ $_cpuOrigTorch = $cpuTorchSpec; $_cpuOrigVision = $cpuVisionSpec; $_cpuOrigAudio = $cpuAudioSpec
+ if ($env:UNSLOTH_KEPT_TORCH -match '^\d+\.\d+(\.\d+)?$') {
+ $_keptMinor = [int](($env:UNSLOTH_KEPT_TORCH -split '\.')[1])
+ $cpuTorchSpec = "torch==$($env:UNSLOTH_KEPT_TORCH)"
+ $cpuVisionSpec = "torchvision==0.$($_keptMinor + 15).*"
+ $cpuAudioSpec = "torchaudio==2.$($_keptMinor).*"
+ $_cpuKeptActive = $true
+ }
+ while ($true) {
+ $_torchTrio = @($cpuTorchSpec, $cpuVisionSpec, $cpuAudioSpec)
+ if ($WinArm64NoAudio) { $_torchTrio = @($cpuTorchSpec, $cpuVisionSpec) }
+ if ($script:UnslothVerbose) {
+ Fast-Install @_torchTrio @cpuForce --index-url $TorchInstallIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
+ $torchInstallExit = $LASTEXITCODE
+ $output = ""
+ } else {
+ $output = Fast-Install @_torchTrio @cpuForce --index-url $TorchInstallIndexUrl | Out-String
+ $torchInstallExit = $LASTEXITCODE
+ }
+ if ($torchInstallExit -eq 0 -or -not $_cpuKeptActive) { break }
+ substep "[WARN] torch==$($env:UNSLOTH_KEPT_TORCH) not installable from the CPU index -- using the supported release" "Yellow"
+ $cpuTorchSpec = $_cpuOrigTorch; $cpuVisionSpec = $_cpuOrigVision; $cpuAudioSpec = $_cpuOrigAudio
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ $_cpuKeptActive = $false
}
if ($torchInstallExit -ne 0) {
Write-StudioLine "[FAILED] PyTorch install failed (exit code $torchInstallExit)" -ForegroundColor Red
@@ -5347,9 +5079,7 @@ if (-not $ROCmIndexUrl -and -not $XpuIndexUrl -and ($CuTag -eq "cpu" -or $ROCmCp
} elseif (-not $ROCmIndexUrl -and -not $XpuIndexUrl) {
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.
+ # PEP 440 ignores +cuXXX, so a cu126 -> cu128 change never applies without --force-reinstall.
$cudaForce = @()
if ($script:PinChangedForceReinstall -or $script:TorchImportDefinitivelyFailed) {
$cudaForce = @("--force-reinstall")
@@ -5361,21 +5091,37 @@ if (-not $ROCmIndexUrl -and -not $XpuIndexUrl -and ($CuTag -eq "cpu" -or $ROCmCp
$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"
- }
- # A custom pin whose leaf is not cpu (a corporate /simple mirror) lands an ARM64 host
- # here, so this branch drops torchaudio too.
- $_cudaTrio = @($cudaTorchSpec, $cudaVisionSpec, $cudaAudioSpec)
- if ($WinArm64NoAudio) { $_cudaTrio = @($cudaTorchSpec, $cudaVisionSpec) }
- if ($script:UnslothVerbose) {
- Fast-Install @_cudaTrio @cudaForce --index-url $TorchInstallIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
- $torchInstallExit = $LASTEXITCODE
- $output = ""
- } else {
- $output = Fast-Install @_cudaTrio @cudaForce --index-url $TorchInstallIndexUrl | Out-String
- $torchInstallExit = $LASTEXITCODE
+ $cudaTorchSpec = "torch>=2.4,<2.12.0"
+ $cudaVisionSpec = "torchvision>=0.19,<0.27.0"
+ $cudaAudioSpec = "torchaudio>=2.4,<2.12.0"
+ }
+ # Release preservation: keep the same release across re-runs (+cuXXX follows this index).
+ $_cudaKeptActive = $false
+ $_cudaOrigTorch = $cudaTorchSpec; $_cudaOrigVision = $cudaVisionSpec; $_cudaOrigAudio = $cudaAudioSpec
+ if ($env:UNSLOTH_KEPT_TORCH -match '^\d+\.\d+(\.\d+)?$') {
+ $_keptMinor = [int](($env:UNSLOTH_KEPT_TORCH -split '\.')[1])
+ $cudaTorchSpec = "torch==$($env:UNSLOTH_KEPT_TORCH)"
+ $cudaVisionSpec = "torchvision==0.$($_keptMinor + 15).*"
+ $cudaAudioSpec = "torchaudio==2.$($_keptMinor).*"
+ $_cudaKeptActive = $true
+ }
+ while ($true) {
+ # A custom pin whose leaf is not cpu lands an ARM64 host here, so drop torchaudio.
+ $_cudaTrio = @($cudaTorchSpec, $cudaVisionSpec, $cudaAudioSpec)
+ if ($WinArm64NoAudio) { $_cudaTrio = @($cudaTorchSpec, $cudaVisionSpec) }
+ if ($script:UnslothVerbose) {
+ Fast-Install @_cudaTrio @cudaForce --index-url $TorchInstallIndexUrl | ForEach-Object { Redact-InstallOutput "$_" } | Out-Host
+ $torchInstallExit = $LASTEXITCODE
+ $output = ""
+ } else {
+ $output = Fast-Install @_cudaTrio @cudaForce --index-url $TorchInstallIndexUrl | Out-String
+ $torchInstallExit = $LASTEXITCODE
+ }
+ if ($torchInstallExit -eq 0 -or -not $_cudaKeptActive) { break }
+ substep "[WARN] torch==$($env:UNSLOTH_KEPT_TORCH) not installable from this CUDA index -- using the supported release" "Yellow"
+ $cudaTorchSpec = $_cudaOrigTorch; $cudaVisionSpec = $_cudaOrigVision; $cudaAudioSpec = $_cudaOrigAudio
+ Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
+ $_cudaKeptActive = $false
}
if ($torchInstallExit -ne 0) {
Write-StudioLine "[FAILED] PyTorch CUDA install failed (exit code $torchInstallExit)" -ForegroundColor Red
@@ -5383,7 +5129,7 @@ if (-not $ROCmIndexUrl -and -not $XpuIndexUrl -and ($CuTag -eq "cpu" -or $ROCmCp
Exit-SetupFailure "PyTorch CUDA installation failed (exit code $torchInstallExit)"
}
- # Install Triton for Windows (enables torch.compile -- without it training can hang)
+ # Triton for Windows enables torch.compile (without it training can hang).
substep "installing Triton for Windows..."
if ($script:UnslothVerbose) {
Fast-Install "triton-windows<3.7"
@@ -5803,10 +5549,7 @@ if (-not (Test-Path -LiteralPath $UnslothHome)) { [System.IO.Directory]::CreateD
$NeedLlamaSourceBuild = $false
$SkipPrebuiltInstall = $false
$RequestedLlamaTag = if ($env:UNSLOTH_LLAMA_TAG) { $env:UNSLOTH_LLAMA_TAG } else { $DefaultLlamaTag }
-# Every host installs the fork's app-* prebuilts now: GPU Windows (CUDA / ROCm)
-# already did, and the fork now also ships the CPU bundles for Windows x64 and
-# arm64 (windows-cpu / windows-arm64). ggml-org artifacts are no longer used by
-# default. Mirrors setup.sh's routing.
+# Every host installs the fork's app-* prebuilts; ggml-org artifacts are no longer the default.
$HelperReleaseRepo = "unslothai/llama.cpp"
$LlamaPr = if ($env:UNSLOTH_LLAMA_PR) { $env:UNSLOTH_LLAMA_PR.Trim() } else { "" }
@@ -5850,9 +5593,7 @@ function Invoke-LlamaHelper {
}
try {
- # Capture all output (stdout + stderr) so that PowerShell does not
- # convert stderr lines into visible ErrorRecord objects. Separate
- # stdout from stderr afterwards.
+ # Capture all output (2>&1) so PS does not make ErrorRecords; split afterwards.
$allOutput = & python "$PSScriptRoot\install_llama_prebuilt.py" @Arguments 2>&1
$exitCode = $LASTEXITCODE
$stdoutLines = @()
@@ -5918,10 +5659,7 @@ if ($LocalLlamaCppSrc) {
Exit-SetupFailure "UNSLOTH_LOCAL_LLAMA_CPP_DIR does not exist: $LocalLlamaCppSrc"
}
$ResolvedLocal = (Resolve-Path -LiteralPath $LocalLlamaCppSrc).Path
- # Reusing a local dir disables both the prebuilt download and the source
- # build, so a runnable llama-server.exe must already be present. Accept any
- # layout LlamaCppBackend._layout_candidates() resolves (root-level, build\bin,
- # or build\bin\Release) so the flag never rejects a tree Unsloth could run.
+ # A local dir disables prebuilt + source build, so accept any layout the backend resolves.
$LocalLlamaServerFound = $false
$LocalIsCanonical = ($ResolvedLocal -eq $LlamaCppDir)
foreach ($_cand in @(
@@ -5953,21 +5691,11 @@ if ($LocalLlamaCppSrc) {
substep "UNSLOTH_LOCAL_LLAMA_CPP_DIR points to the canonical install location with nothing built there yet; running the normal install" "Yellow"
}
} else {
- # Fail clearly rather than junction an unbuilt or wrong-platform checkout
- # and leave Unsloth with no usable binary.
if (-not $LocalLlamaServerFound) {
step "llama.cpp" "no llama-server.exe under $ResolvedLocal (looked for .\llama-server.exe, .\build\bin and .\build\bin\Release) -- build llama.cpp there first, or drop --with-llama-cpp-dir" "Red"
Exit-SetupFailure "No llama-server.exe was found under $ResolvedLocal"
}
- # If the target is already a junction/symlink (e.g. a previous
- # --with-llama-cpp-dir run), delete only the link via DirectoryInfo.Delete().
- # Remove-Item -Recurse -Force on a reparse point can traverse the link and
- # wipe the user's real llama.cpp directory on PowerShell 5.1. Dropping the
- # stale link here also keeps the custom-home ownership check below idempotent.
- # Use Get-Item -Force (not Test-Path): a *broken* junction whose target was
- # moved/deleted makes Test-Path return false, which would leave the dangling
- # link in place and make mklink below fail; Get-Item still resolves it so we
- # can remove it and relink to a new valid directory.
+ # Delete only the link: -Recurse can traverse a reparse point and wipe the real dir.
$existing = Get-Item -LiteralPath $LlamaCppDir -Force -ErrorAction SilentlyContinue
if ($existing -and ($existing.Attributes -band [System.IO.FileAttributes]::ReparsePoint)) {
# A link reads Present, so the probe below cannot cover a denied
@@ -6036,26 +5764,13 @@ if ($LocalLlamaCppLinked) {
}
if ($llamaDirState -eq "Readable") {
substep "Existing llama.cpp install detected -- validating staged prebuilt update before replacement"
- # If the existing install is the wrong kind (e.g. windows-cpu on a ROCm
- # machine that should have windows-rocm), remove it so the installer is
- # forced to download the correct variant rather than skipping on tag match.
$existingMetaPath = Join-Path $LlamaCppDir "UNSLOTH_PREBUILT_INFO.json"
# Readable state leaves only marker presence to decide here.
if (Test-PathQuiet -Path $existingMetaPath -PathType Leaf) {
try {
$existingMeta = Get-Content -LiteralPath $existingMetaPath -Raw | ConvertFrom-Json
$existingKind = $existingMeta.install_kind
- # A ROCm host may legitimately carry the fork's windows-rocm bundle
- # or the upstream windows-hip fallback, so accept either and never
- # treat a valid ROCm install as mismatched. A name-inferred gfx
- # arch (Adrenalin-only, no confirmed runtime) still counts as
- # ROCm-capable -- the ROCm prebuilt bundles its own runtime,
- # mirroring the --rocm-gfx forward below. The CPU branch covers both
- # the x64 windows-cpu and arm64 windows-arm64 bundles (Windows arm64
- # has no GPU prebuilt). NOTE: this block is currently inert --
- # write_prebuilt_metadata does not persist an install_kind key, so
- # $existingKind is always null; keep $expectedKinds in sync with the
- # kinds install_llama_prebuilt.py installs before relying on it.
+ # ROCm hosts carry windows-rocm or -hip; CPU covers -cpu and -arm64. Inert for now.
$expectedKinds = if ($HasROCm -or $script:ROCmGfxArch) { @("windows-rocm", "windows-hip") } elseif ($HasNvidiaSmi) { @("windows-cuda") } else { @("windows-cpu", "windows-arm64") }
if ($existingKind -and ($existingKind -notin $expectedKinds)) {
substep "Removing mismatched llama.cpp install (found '$existingKind', need one of: $($expectedKinds -join ', '))..."
@@ -6067,9 +5782,7 @@ if ($LocalLlamaCppLinked) {
}
}
substep "installing prebuilt llama.cpp bundle (preferred path)..."
- # why: install_llama_prebuilt.py uses os.replace(), which would displace
- # an unrelated $env:UNSLOTH_STUDIO_HOME\llama.cpp before the source-build
- # ownership check below ever runs.
+ # install_llama_prebuilt.py's os.replace() would displace a custom-home llama.cpp first.
if ($StudioHomeIsCustom) {
Assert-StudioOwnedOrAbsent -Path $LlamaCppDir -Label "llama.cpp install"
}
@@ -6082,11 +5795,7 @@ if ($LocalLlamaCppLinked) {
if ($HasROCm) {
$prebuiltArgs += "--has-rocm"
}
- # Forward the resolved gfx arch so the per-gfx ROCm prebuilt is picked even
- # when the installer's probe can't confirm the runtime (amd-smi-only /
- # Adrenalin-only, name-inferred arch). --rocm-gfx is authoritative and
- # implies ROCm in install_llama_prebuilt.py, so the GPU prebuilt is selected
- # even with $HasROCm false. Gating on $HasROCm gave Strix Halo / 8060S CPU.
+ # --rocm-gfx implies ROCm, so the GPU prebuilt is picked with $HasROCm false.
if ($script:ROCmGfxArch) {
$prebuiltArgs += @("--rocm-gfx", $script:ROCmGfxArch)
}
@@ -6127,7 +5836,6 @@ if ($LocalLlamaCppLinked) {
}
try {
if ($script:UnslothVerbose) {
- # Show live output in verbose mode while still capturing for error log
$prebuiltLog = Join-Path $env:TEMP "unsloth-prebuilt-$PID.log"
& python @prebuiltArgs 2>&1 | Tee-Object -FilePath $prebuiltLog | Out-Host
$prebuiltExit = $LASTEXITCODE
@@ -6309,12 +6017,9 @@ if ($StageRoot -and $NeedLlamaSourceBuild) {
# ==========================================================================
# PHASE 3.5: Install OpenSSL dev (for HTTPS support in llama-server)
# ==========================================================================
-# llama-server needs OpenSSL to download models from HuggingFace via -hf.
-# ShiningLight.OpenSSL.Dev includes headers + libs that cmake can find.
$OpenSslAvailable = $false
if ($NeedLlamaSourceBuild) {
- # Check if OpenSSL dev is already installed (look for include dir)
$OpenSslRoots = @(
'C:\Program Files\OpenSSL-Win64',
'C:\Program Files\OpenSSL',
@@ -6358,14 +6063,6 @@ if ($NeedLlamaSourceBuild) {
# ==========================================================================
# PHASE 4: Build llama.cpp with CUDA for GGUF inference + export
# ==========================================================================
-# Builds at ~/.unsloth/llama.cpp — a single shared location under the user's
-# home directory. This is used by both the inference server and the GGUF
-# export pipeline (unsloth-zoo).
-# We build:
-# - llama-server: for GGUF model inference (with HTTPS if OpenSSL available)
-# - llama-quantize: for GGUF export quantization
-# Prerequisites git, cmake, VS Build Tools were installed in Phase 1; the CUDA
-# Toolkit is resolved lazily just below via Resolve-CudaToolkit (source build only).
$OriginalLlamaCppDir = $LlamaCppDir
$BuildDir = Join-Path $LlamaCppDir "build"
$LlamaServerBin = Join-Path $BuildDir "bin\Release\llama-server.exe"
@@ -6373,8 +6070,7 @@ $LlamaServerBin = Join-Path $BuildDir "bin\Release\llama-server.exe"
$HasCmakeForBuild = $null -ne (Get-Command cmake -ErrorAction SilentlyContinue)
$HasGitForBuild = $null -ne (Get-Command git -ErrorAction SilentlyContinue)
-# Check if existing llama-server matches current GPU mode. A CUDA-built binary
-# on a now-CPU-only machine (or vice versa) needs to be rebuilt.
+# A CUDA binary on a now-CPU-only machine, or the reverse, must be rebuilt.
$NeedRebuild = $false
# A forced compile, a pinned PR or a custom source skips the prebuilt path and its
# phase 3.4 denial guard, so this can be the first probe to read inside the tree.
@@ -6405,10 +6101,6 @@ if ($llamaBinState -eq "Present") {
}
}
-# Install build tools now (last resort) rather than eagerly in Phase 1, so the
-# prebuilt path stays fast. Same condition as the if/elseif chain below: a source
-# build runs only when needed and no usable binary is already present. A linked
-# local dir sets $NeedLlamaSourceBuild = $false, so this no-ops for that path.
$WillBuildLlamaFromSource = $NeedLlamaSourceBuild -and `
-not ((Test-PathQuiet $LlamaServerBin "Leaf") -and -not $NeedRebuild -and $RequestedLlamaTag -ne "master")
if ($WillBuildLlamaFromSource) {
@@ -6463,7 +6155,6 @@ if ($LocalLlamaCppLinked) {
} elseif (-not $HasCmakeForBuild) {
Write-StudioLine ""
if (-not $HasNvidiaSmi) {
- # CPU-only machines depend entirely on llama-server for GGUF chat -- cmake is required
substep "CMake is required to build llama-server for GGUF chat mode." "Yellow"
substep "Continuing setup without llama.cpp build." "Yellow"
substep "Install CMake from https://cmake.org/download/ and re-run setup." "Yellow"
@@ -6473,25 +6164,21 @@ if ($LocalLlamaCppLinked) {
substep "Install CMake from https://cmake.org/download/ and re-run setup." "Yellow"
$script:LlamaCppDegraded = $true
} else {
- # Finalize the VS generator (gate/fallback below) BEFORE Resolve-CudaToolkit,
- # which copies the CUDA .targets into the current generator's dir; a later swap
- # would strand them. The CMake 4.2 gate for VS 2026 is checked only here, in the
- # source-build path, so a VS 2026 + cmake < 4.2 host can still use the prebuilt. (#6473)
+ # Finalize the VS generator BEFORE Resolve-CudaToolkit, which copies the CUDA .targets into
+ # the current generator's dir. The CMake 4.2 gate for VS 2026 is checked only here, so a
+ # VS 2026 + cmake < 4.2 host can still use the prebuilt. (#6473)
if ($CmakeGenerator -match 'Visual Studio 18\b') {
if (-not (Test-CmakeCanDriveGenerator -Generator $CmakeGenerator)) {
$cmakeVerObj = Get-CmakeVersion
$cmakeVerStr = if ($cmakeVerObj) { $cmakeVerObj.ToString() } else { '0.0' }
substep "CMake $cmakeVerStr cannot drive the Visual Studio 2026 generator (need 4.2+ or a VS-bundled cmake) -- updating via winget..." "Yellow"
if ($null -ne (Get-Command winget -ErrorAction SilentlyContinue)) {
- # upgrade first (fast if Kitware.CMake is already a winget app), then
- # prepend the default dir so the new cmake wins over an older one on PATH
try {
Invoke-SetupCommand { winget upgrade Kitware.CMake --source winget --accept-package-agreements --accept-source-agreements } | Out-Null
Refresh-Environment
} catch { substep "CMake winget upgrade failed: $($_.Exception.Message)" "Yellow" }
Add-DefaultCmakeToPath | Out-Null
- # upgrade no-ops if the cmake came from Scoop/Chocolatey/VS, not the
- # Kitware winget package; install it so a 4.2+ cmake is available
+ # upgrade no-ops for a Scoop/Chocolatey/VS cmake; install to get a 4.2+ one.
if (-not (Test-CmakeCanDriveGenerator -Generator $CmakeGenerator)) {
try {
Invoke-SetupCommand { winget install Kitware.CMake --source winget --accept-package-agreements --accept-source-agreements } | Out-Null
@@ -6501,9 +6188,6 @@ if ($LocalLlamaCppLinked) {
}
}
if (-not (Test-CmakeCanDriveGenerator -Generator $CmakeGenerator)) {
- # cmake still cannot drive VS 2026; before failing, fall back to an
- # older installed VS whose generator it can drive (e.g. VS 2022 + old
- # cmake on an offline box keeps building)
$fallback = Get-FallbackVsGenerator
if ($fallback) {
substep "CMake cannot drive $CmakeGenerator; falling back to $($fallback.Generator)" "Yellow"
@@ -6519,18 +6203,14 @@ if ($LocalLlamaCppLinked) {
substep "CMake can drive the $CmakeGenerator generator"
}
- # CUDA resolved here (fail fast if none), after the final VS generator so its
- # .targets land in the toolset cmake actually uses.
+ # After the final VS generator, so the CUDA .targets land in the toolset cmake uses.
if ($HasNvidiaSmi) { Resolve-CudaToolkit -RequireOrExit }
Write-StudioLine ""
if ($HasNvidiaSmi) {
substep "building llama.cpp with CUDA support..."
} elseif ($HasROCm -or $script:ROCmGfxArch) {
- # AMD GPU present but in the CPU-only source-build fallback: a HIP source
- # build needs the full HIP SDK + ROCm clang toolchain. AMD GPU acceleration
- # comes from the per-gfx ROCm prebuilt (bundles the runtime, no SDK) -- reaching
- # here means it couldn't be installed. Warn loudly, don't ship a slow CPU build.
+ # A HIP source build needs the full HIP SDK; the per-gfx ROCm prebuilt already failed here.
$_amdArch = if ($script:ROCmGfxArch) { $script:ROCmGfxArch } else { "ROCm" }
substep "[WARN] AMD GPU ($_amdArch) detected, but the GPU-accelerated ROCm" "Yellow"
substep " llama.cpp prebuilt could not be installed -- falling back to a CPU build." "Yellow"
@@ -6547,19 +6227,13 @@ if ($LocalLlamaCppLinked) {
# Start total build timer
$totalSw = [System.Diagnostics.Stopwatch]::StartNew()
- # Native commands (git, cmake) write to stderr even on success.
- # With $ErrorActionPreference = "Stop" (set at top of script), PS 5.1
- # converts stderr lines into terminating ErrorRecords, breaking output.
- # Lower to "Continue" for the build section.
$prevEAP = $ErrorActionPreference
$ErrorActionPreference = "Continue"
$BuildOk = $true
$FailedStep = ""
- # Re-sanitize CUDA_PATH_V* vars — Refresh-Environment (called during
- # Node/Python installs above) may have repopulated conflicting versioned
- # vars from the Machine registry.
+ # Refresh-Environment may have repopulated conflicting CUDA_PATH_V* from the registry.
if ($HasNvidiaSmi -and $CudaToolkitRoot) {
$cudaPathVars2 = @([Environment]::GetEnvironmentVariables('Process').Keys | Where-Object { $_ -match '^CUDA_PATH_V' })
foreach ($v2 in $cudaPathVars2) {
@@ -6703,9 +6377,7 @@ if ($LocalLlamaCppLinked) {
}
}
}
- # why: in-place git-sync (the temp-dir clone path calls Mark-StudioOwned
- # at swap-time) must mark the existing tree so a subsequent prebuilt
- # update path's Assert-StudioOwnedOrAbsent does not exit on the same root.
+ # In-place git-sync must mark the tree, else a later Assert-StudioOwnedOrAbsent exits.
if ($BuildOk -and $StudioHomeIsCustom) {
Mark-StudioOwned -Path $LlamaCppDir
}
@@ -6815,7 +6487,6 @@ if ($LocalLlamaCppLinked) {
'-G', $CmakeGenerator,
'-Wno-dev'
)
- # Tell cmake exactly where VS is (bypasses registry lookup)
if ($VsInstallPath) {
$CmakeArgs += "-DCMAKE_GENERATOR_INSTANCE=$VsInstallPath"
}
@@ -6835,21 +6506,15 @@ if ($LocalLlamaCppLinked) {
$CmakeArgs += '-DCMAKE_EXE_LINKER_FLAGS=/NODEFAULTLIB:LIBCMT'
# CUDA flags -- only if GPU available, otherwise explicitly disable
if ($HasNvidiaSmi -and $NvccPath) {
- # UNSLOTH_LLAMA_CUDA_ARCHS (e.g. "120" or "89;86") forces the build
- # arch and wins over detection, matching setup.sh.
+ # UNSLOTH_LLAMA_CUDA_ARCHS ("120" or "89;86") forces the build arch, as setup.sh does.
$CudaArchOverride = if ($env:UNSLOTH_LLAMA_CUDA_ARCHS) { ($env:UNSLOTH_LLAMA_CUDA_ARCHS -replace '\s', '') } else { '' }
if ((-not $CudaArch) -and (-not $CudaArchOverride)) {
- # No detectable compute capability (#5854): -DGGML_CUDA=ON with no
- # arch builds a PTX-only binary, so build CPU instead. Mirrors the
- # Linux fix; set UNSLOTH_LLAMA_CUDA_ARCHS=120 to force a CUDA build.
+ # No compute capability (#5854): -DGGML_CUDA=ON with no arch builds PTX-only.
substep "could not detect a CUDA compute capability; building CPU llama.cpp instead of a PTX-only binary (set UNSLOTH_LLAMA_CUDA_ARCHS=120 to force a CUDA build)." "Yellow"
$CmakeArgs += '-DGGML_CUDA=OFF'
} else {
$CmakeArgs += '-DGGML_CUDA=ON'
- # Accept a host MSVC newer than nvcc's whitelist; a fresh toolkit
- # (e.g. CUDA 13.3) otherwise aborts with "#error -- unsupported
- # Microsoft Visual Studio version!". Mirrors the Linux fix. Via env
- # (covers the configure probe + build), after Refresh-Environment, idempotent.
+ # Accept a host MSVC newer than nvcc's whitelist, which would otherwise abort.
$nvccAllowFlag = '-allow-unsupported-compiler'
if ([string]::IsNullOrEmpty($env:NVCC_PREPEND_FLAGS)) {
$env:NVCC_PREPEND_FLAGS = $nvccAllowFlag
@@ -6868,8 +6533,7 @@ if ($LocalLlamaCppLinked) {
if (Test-NvccArchSupport -NvccExe $NvccPath -Arch $CudaArch) {
$CmakeArgs += "-DCMAKE_CUDA_ARCHITECTURES=$CudaArch"
} else {
- # GPU arch too new for this toolkit -- fall back to highest supported.
- # PTX forward-compatibility will JIT-compile for the actual GPU at runtime.
+ # Arch too new for this toolkit: fall back to the highest supported (PTX JITs).
$maxArch = Get-NvccMaxArch -NvccExe $NvccPath
if ($maxArch) {
$CmakeArgs += "-DCMAKE_CUDA_ARCHITECTURES=$maxArch"
@@ -6933,8 +6597,7 @@ if ($LocalLlamaCppLinked) {
}
# -- Step E: Build the DiffusionGemma visual server (optional, best-effort) --
- # An example target present on llama.cpp PR #24423; lets Unsloth serve
- # DiffusionGemma GGUFs without DG_VISUAL_BIN. No-op when not configured.
+ # Example target from llama.cpp PR #24423.
if ($BuildOk) {
$null = cmake --build $BuildDir --config Release --target llama-diffusion-gemma-visual-server -j $NumCpu 2>&1 | Out-String
}
@@ -7043,10 +6706,7 @@ substep "(add -H 0.0.0.0 for LAN / cloud access; exposes the raw port only, not
substep "(add -H 0.0.0.0 --cloudflare for a public Cloudflare HTTPS link, or --secure to keep the raw port private; anyone with the API key can run code)"
Write-StudioLine ""
-# Match studio/setup.sh: exit non-zero for degraded llama.cpp when called
-# from install.ps1 (SKIP_STUDIO_BASE=1) so the installer can detect the
-# failure. Direct 'unsloth studio update' does not set SKIP_STUDIO_BASE,
-# so it keeps degraded installs successful.
+# Exit non-zero for degraded llama.cpp only under install.ps1; 'studio update' stays successful.
if ($script:LlamaCppDegraded -and $env:SKIP_STUDIO_BASE -eq "1") {
# Tauri mode reports instead of aborting, exactly as setup.sh does. install.ps1
# turns any non-zero status from here into Exit-InstallFailure, and install.rs
diff --git a/tests/python/test_cross_platform_parity.py b/tests/python/test_cross_platform_parity.py
index 00999ece72e..5148ef318cb 100644
--- a/tests/python/test_cross_platform_parity.py
+++ b/tests/python/test_cross_platform_parity.py
@@ -515,30 +515,42 @@ class TestKnown211SetParity:
), 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<digits> 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)."""
+ """install.ps1's pinned-torch install must bound the whole trio on EVERY
+ index with the default torch 2.11 line (<2.12 trio, matching install.sh's
+ ceiling-composed default and _CUDA_TORCH_PKG_SPEC): torchaudio 2.11
+ dropped its exact torch pin from the wheel metadata, so a bare companion
+ beside a capped torch can resolve a mismatched build."""
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)"
+ '$_pinTorchSpec = "torch>=2.4,<2.12.0"' in text
+ ), "install.ps1 default install must use the torch 2.11 line (<2.12.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.
+ '$_pinVisionSpec = "torchvision>=0.19,<0.27.0"' in text
+ ), "install.ps1 must pair torchvision <0.27.0 with torch <2.12"
+ assert (
+ '$_pinAudioSpec = "torchaudio>=2.4,<2.12.0"' in text
+ ), "install.ps1 must pair torchaudio <2.12.0 with torch <2.12"
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.
+ # No stale 2.10-line DEFAULT remains. Checked against the default trio's own assignments,
+ # not a blanket "<2.11.0 appears nowhere": Get-XpuTorchSpecs keeps a curated sub-2.11 cap.
+ for _stale in (
+ '$_pinTorchSpec = "torch>=2.4,<2.11.0"',
+ '$_pinVisionSpec = "torchvision>=0.19,<0.26.0"',
+ '$_pinAudioSpec = "torchaudio>=2.4,<2.11.0"',
+ '$_torchSpecs = @("torch>=2.4,<2.11.0"',
+ ):
+ assert (
+ _stale not in text
+ ), f"install.ps1 must not retain the <2.11.0 default torch line: {_stale}"
# Specs are splatted, so check both halves: the list is built, and it is passed.
assert (
- '$_torchSpecs = @("torch>=2.4,<2.11.0", $_pinVisionSpec, $_pinAudioSpec)' in text
- ), "install.ps1 custom-pin install must build the bounded spec list"
+ "$_torchSpecs = @($_pinTorchSpec, $_pinVisionSpec, $_pinAudioSpec)" in text
+ ), "install.ps1 pinned install must build the bounded trio spec list"
assert (
"@_torchSpecs --default-index $TorchIndexUrl" in text
- ), "install.ps1 custom-pin install must pass the bounded companion specs to uv"
+ ), "install.ps1 pinned install must pass the bounded trio specs to uv"
def test_gfx_allowlist_matches_across_installers(self):
# The gfx 2.11 allowlist {gfx120x-all, gfx1151, gfx1150} must appear in each.
@@ -827,9 +839,9 @@ class TestPinnedIndexClearsUvEnvParity:
# 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"',
+ '$cudaTorchSpec = "torch>=2.4,<2.12.0"',
+ '$cudaVisionSpec = "torchvision>=0.19,<0.27.0"',
+ '$cudaAudioSpec = "torchaudio>=2.4,<2.12.0"',
):
assert spec in text, f"setup.ps1 must bound the custom-leaf trio: {spec}"
assert (
diff --git a/tests/python/test_tokenizers_and_torch_constraint.py b/tests/python/test_tokenizers_and_torch_constraint.py
index 57733774e55..5886208d034 100644
--- a/tests/python/test_tokenizers_and_torch_constraint.py
+++ b/tests/python/test_tokenizers_and_torch_constraint.py
@@ -65,45 +65,46 @@ class TestStructuralTorchConstraint:
_sh = _read(_INSTALL_SH)
def test_default_assignment_exists(self):
- assert 'TORCH_CONSTRAINT="torch>=2.4,<2.11.0"' in self._sh
+ """The default range composes the per-file ceiling variable, so the
+ supported line (torch 2.11 today) is bumped in one place."""
+ assert '_TORCH_CEILING="2.12.0"' in self._sh
+ assert 'TORCH_CONSTRAINT="torch>=2.4,<${_TORCH_CEILING}"' in self._sh
def test_tightened_assignment_exists(self):
- assert 'TORCH_CONSTRAINT="torch>=2.6,<2.11.0"' in self._sh
+ assert 'TORCH_CONSTRAINT="torch>=2.6,<${_TORCH_CEILING}"' in self._sh
- def test_cuda_constraint_widened_to_2_12(self):
- """A fresh CUDA install widens the ceiling to <2.12.0 so cu12x/cu13x
- land torch 2.11.x (matches the base image and _CUDA_TORCH_PKG_SPEC);
- without it cu128/cu130 resolves torch 2.10.x."""
- assert 'TORCH_CONSTRAINT="torch>=2.4,<2.12.0"' in self._sh
-
- def test_cuda_case_widens_via_index_leaf(self):
- """The cu* branch of the _torch_index_leaf case sets the widened
- constraint (parallel to rocm7.2), anchored on the leaf."""
- m = re.search(
- r'cu\[0-9\]\*\)\s*TORCH_CONSTRAINT="torch>=2\.4,<2\.12\.0"',
- self._sh,
- )
- assert m is not None, "CUDA (cu*) TORCH_CONSTRAINT widening case not found"
+ def test_companion_ceilings_composed(self):
+ """Companions bound to the same window via their own ceiling vars."""
+ assert '_TORCHVISION_CEILING="0.27.0"' in self._sh
+ assert '_TORCHAUDIO_CEILING="2.12.0"' in self._sh
+ assert 'TORCHVISION_CONSTRAINT="torchvision>=0.19,<${_TORCHVISION_CEILING}"' in self._sh
+ assert 'TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<${_TORCHAUDIO_CEILING}"' in self._sh
def test_variable_used_in_pip_install(self):
"""$TORCH_CONSTRAINT must appear in a uv pip install line."""
assert '"$TORCH_CONSTRAINT"' in self._sh
def test_hardcoded_torch_constraint_only_on_assignments(self):
- """The hard-coded torch>=2.4,<2.11.0 string must only appear on
- TORCH_CONSTRAINT= assignment lines, never on a pip/uv install line
- (those must reference $TORCH_CONSTRAINT). Two assignments are expected:
- the default, and the gfx906 (MI50) reroute that restores the default
- <2.11 window after the rocm7.2 floor bump raised it to 2.11."""
- hits = [ln for ln in self._sh.splitlines() if '"torch>=2.4,<2.11.0"' in ln]
- assert hits, "default constraint literal missing from install.sh"
- for ln in hits:
- assert (
- "TORCH_CONSTRAINT=" in ln
- ), f"torch>=2.4,<2.11.0 hardcoded off a TORCH_CONSTRAINT= assignment: {ln.strip()!r}"
- assert (
- "pip install" not in ln
- ), f"torch>=2.4,<2.11.0 hardcoded on a pip install line: {ln.strip()!r}"
+ """The default range is composed from the ceiling vars, so the supported
+ line is bumped in one place. A hard-coded range may still appear on a
+ curated per-index TORCH_CONSTRAINT= override -- the gfx906 (MI50) reroute
+ caps below 2.11 because the rocm6.3 index tops out at torch 2.9.x -- but
+ never on a pip/uv install line (those must reference $TORCH_CONSTRAINT)."""
+ for literal in ('"torch>=2.4,<2.11.0"', '"torch>=2.4,<2.12.0"'):
+ for ln in self._sh.splitlines():
+ if literal not in ln:
+ continue
+ assert (
+ "TORCH_CONSTRAINT=" in ln
+ ), f"{literal} hardcoded off a TORCH_CONSTRAINT= assignment: {ln.strip()!r}"
+ assert (
+ "pip install" not in ln
+ ), f"{literal} hardcoded on a pip install line: {ln.strip()!r}"
+
+ def test_gfx906_reroute_caps_below_211(self):
+ """The gfx906 / MI50 reroute must keep its literal sub-2.11 cap: the
+ rocm6.3 index it routes to serves no torch 2.11 wheel."""
+ assert self._sh.count('TORCH_CONSTRAINT="torch>=2.4,<2.11.0"') == 1
def test_tightening_guarded_by_skip_torch(self):
"""The block must check SKIP_TORCH=false."""
@@ -133,7 +134,7 @@ class TestStructuralInstallPs1Unchanged:
assert "$TorchConstraint" not in self._ps1
def test_hardcoded_torch_constraint_present(self):
- assert '"torch>=2.4,<2.11.0"' in self._ps1
+ assert '"torch>=2.4,<2.12.0"' in self._ps1
class TestInstallPs1UvDefaultIndex:
diff --git a/tests/python/test_windows_xformers_installer.py b/tests/python/test_windows_xformers_installer.py
index 2ba4db6c52d..80c7eae4dd6 100644
--- a/tests/python/test_windows_xformers_installer.py
+++ b/tests/python/test_windows_xformers_installer.py
@@ -250,11 +250,17 @@ def test_installer_never_installs_an_unpinned_xformers():
def test_xformers_step_runs_after_the_torch_flavor_repair():
"""The repair can reinstall torch from a different index; selecting the wheel before it
- would pin against a torch build that is about to be replaced."""
+ would pin against a torch build that is about to be replaced.
+
+ Anchored on code, not on comment prose. The previous anchors were the section
+ headers, and a comment-tightening pass that inserted one word into
+ "Enforce the installed torch flavor" turned this red without changing any
+ behaviour, which is a failure of the test rather than of the installer.
+ """
source = _source()
- repair = source.index("Enforce the installed torch flavor matches the detected GPU build")
- xformers = source.index("Pin xFormers to the wheel built for the torch")
- overlay = source.index("CI only: overlay a source checkout")
+ repair = source.index("$expectedTorchTag = Get-ExpectedTorchFlavorTag")
+ xformers = source.index("xformers==$_xfVersion")
+ overlay = source.index("UNSLOTH_CI_SOURCE_OVERLAY")
assert repair < xformers < overlay
diff --git a/tests/sh/test_mac_intel_compat.sh b/tests/sh/test_mac_intel_compat.sh
index 697040de70b..cfc62793af8 100644
--- a/tests/sh/test_mac_intel_compat.sh
+++ b/tests/sh/test_mac_intel_compat.sh
@@ -574,6 +574,7 @@ _GUARD_FILE=$(mktemp)
# The guard calls _python_is_skipped, _discard_venv_for_recreate and _uv_venv_arm64,
# so the skip list, its reader, and the replacement helpers have to come along or the
# version check silently never fires and the recreate loses the venv it was handed.
+# The awk anchors on the "independent Apple Silicon venv" substring; keep that wording in install.sh.
{
printf 'substep() { :; }\n'
sed -n '/^PYTHON_SKIP=/p' "$INSTALL_SH"
@@ -582,7 +583,7 @@ _GUARD_FILE=$(mktemp)
sed -n '/^_start_studio_venv_replacement()/,/^}/p' "$INSTALL_SH"
sed -n '/^_discard_venv_for_recreate()/,/^}/p' "$INSTALL_SH"
sed -n '/^_uv_venv_arm64()/,/^}/p' "$INSTALL_SH"
- awk '/Guard against two independent Apple Silicon venv problems/{f=1} f{print} f&&/^fi$/{exit}' \
+ awk '/independent Apple Silicon venv/{f=1} f{print} f&&/^fi$/{exit}' \
"$INSTALL_SH"
} > "$_GUARD_FILE"
for _needed in _python_skip_applies _python_is_skipped _start_studio_venv_replacement _discard_venv_for_recreate _uv_venv_arm64; do
diff --git a/tests/sh/test_previous_torch_pin.sh b/tests/sh/test_previous_torch_pin.sh
index 7231187f63d..98cbccf687c 100644
--- a/tests/sh/test_previous_torch_pin.sh
+++ b/tests/sh/test_previous_torch_pin.sh
@@ -93,10 +93,15 @@ echo "=== the preservation probe reads off disk, not through the interpreter ===
# `import torch` can block forever on a wedged Intel driver, and this probe runs before
# setup.sh's bounded ones. Executed, not grepped: the stub interpreter records being called.
_PREVBLK=$(mktemp)
-awk '/^ _PREV_TORCH_VER=""$/{on=1} on{print} on && /tail -n 1 \|\| true\)$/{exit}' \
- "$INSTALL_SH" > "$_PREVBLK"
+# The interpreter fallback is wrapped in _run_bounded, so the helper has to come along too.
+{
+ sed -n '/^_run_bounded()/,/^}/p' "$INSTALL_SH"
+ awk '/^ _PREV_TORCH_VER=""$/{on=1} on{print} on && /tail -n 1 \|\| true\)$/{exit}' \
+ "$INSTALL_SH"
+} > "$_PREVBLK"
grep -q 'version.py' "$_PREVBLK" || { echo "FATAL: probe block not extracted"; exit 1; }
grep -q 'import torch' "$_PREVBLK" || { echo "FATAL: extraction lost the fallback"; exit 1; }
+grep -q '^_run_bounded()' "$_PREVBLK" || { echo "FATAL: could not extract _run_bounded"; exit 1; }
_prev_probe() { # $1 = torch label to put on disk ("" for no version.py)
_d=$(mktemp -d)
diff --git a/tests/sh/test_torch_constraint.sh b/tests/sh/test_torch_constraint.sh
index ada95c2620c..c742c902e17 100644
--- a/tests/sh/test_torch_constraint.sh
+++ b/tests/sh/test_torch_constraint.sh
@@ -76,11 +76,12 @@ run_constraint_snippet() {
OS=\"$_os\"
_ARCH=\"$_arch\"
VENV_DIR=\"$_venv_dir\"
- TORCH_CONSTRAINT=\"torch>=2.4,<2.11.0\"
+ _TORCH_CEILING=\"2.12.0\"
+ TORCH_CONSTRAINT=\"torch>=2.4,<\${_TORCH_CEILING}\"
if [ \"\$SKIP_TORCH\" = false ] && [ \"\$OS\" = \"macos\" ] && [ \"\$_ARCH\" = \"arm64\" ]; then
_PY_MINOR=\$(\"\$VENV_DIR/bin/python\" -c \"import sys; print(sys.version_info.minor)\" 2>/dev/null || echo \"0\")
if [ \"\$_PY_MINOR\" -ge 13 ] 2>/dev/null; then
- TORCH_CONSTRAINT=\"torch>=2.6,<2.11.0\"
+ TORCH_CONSTRAINT=\"torch>=2.6,<\${_TORCH_CEILING}\"
fi
fi
echo \"\$TORCH_CONSTRAINT\"
@@ -94,13 +95,22 @@ echo "=== Structural: TORCH_CONSTRAINT in install.sh ==="
_SH_CONTENT=$(cat "$INSTALL_SH")
+# The supported line is centralized in per-file ceiling vars, so a 2.12 bump is three lines.
+_count=$(grep -c '_TORCH_CEILING="2.12.0"' "$INSTALL_SH" || true)
+assert_eq "torch ceiling variable defined once" "1" "$_count"
+_count=$(grep -c '_TORCHVISION_CEILING="0.27.0"' "$INSTALL_SH" || true)
+assert_eq "torchvision ceiling variable defined once" "1" "$_count"
+_count=$(grep -c '_TORCHAUDIO_CEILING="2.12.0"' "$INSTALL_SH" || true)
+assert_eq "torchaudio ceiling variable defined once" "1" "$_count"
+
# Each hardware branch assigns its own triple, so counting every occurrence made
# adding a branch (gfx906 in #7354) a test edit. The default is the one assigned at
# top level; a branch's is always indented, so anchor on that instead of counting.
-_count=$(grep -c '^TORCH_CONSTRAINT="torch>=2.4,<2.11.0"$' "$INSTALL_SH" || true)
-assert_eq "default TORCH_CONSTRAINT assignment exists" "1" "$_count"
+# The default composes _TORCH_CEILING, so the column-0 anchor goes on the composed form.
+_count=$(grep -c '^TORCH_CONSTRAINT="torch>=2.4,<${_TORCH_CEILING}"$' "$INSTALL_SH" || true)
+assert_eq "default TORCH_CONSTRAINT assignment exists at top level" "1" "$_count"
-_count=$(grep -c 'TORCH_CONSTRAINT="torch>=2.6,<2.11.0"' "$INSTALL_SH" || true)
+_count=$(grep -c 'TORCH_CONSTRAINT="torch>=2.6,<${_TORCH_CEILING}"' "$INSTALL_SH" || true)
_has=$([ "$_count" -ge 1 ] && echo "yes" || echo "no")
assert_eq "tightened TORCH_CONSTRAINT assignment exists" "yes" "$_has"
@@ -113,30 +123,36 @@ assert_eq "\$TORCH_CONSTRAINT used in pip install" "yes" "$_has_var"
_literal=$(grep -cE 'uv pip install .*"torch>=' "$INSTALL_SH" || true)
assert_eq "no pip install hardcodes a torch pin" "0" "$_literal"
+# The same rule over the whole file, catching a default range copied outside a TORCH_CONSTRAINT=
+# assignment. Curated per-index overrides may still cap literally (gfx906 / MI50 on rocm6.3).
+_hardcoded=$(grep -E '"torch>=2\.4,<2\.11\.0"|"torch>=2\.4,<2\.12\.0"' "$INSTALL_SH" \
+ | grep -c -v '^[[:space:]]*TORCH_CONSTRAINT=' || true)
+assert_eq "no hardcoded default torch range off a TORCH_CONSTRAINT= assignment" "0" "$_hardcoded"
+
+# The gfx906 / MI50 reroute keeps its own sub-2.11 cap. Existence, not a count: more may appear.
+_count=$(grep -cE '^[[:space:]]+TORCH_CONSTRAINT="torch>=2\.4,<2\.11\.0"$' "$INSTALL_SH" || true)
+_has_sub211_cap=$([ "$_count" -ge 1 ] && echo "yes" || echo "no")
+assert_eq "gfx906 reroute caps torch below 2.11 for the rocm6.3 index" "yes" "$_has_sub211_cap"
+
# Companions must be bounded to torch's window everywhere, 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. Every assignment, not a fixed number of them.
+# a mismatched 2.11 build. Every assignment, not a fixed number: a literal on the curated per-index pins, the composed ceiling on the defaults.
_total=$(grep -cE '^[[:space:]]*TORCHVISION_CONSTRAINT="' "$INSTALL_SH" || true)
-_bounded=$(grep -cE '^[[:space:]]*TORCHVISION_CONSTRAINT="torchvision>=[0-9][0-9.]*,<[0-9][0-9.]*"$' "$INSTALL_SH" || true)
+_bounded=$(grep -cE '^[[:space:]]*TORCHVISION_CONSTRAINT="torchvision>=[0-9][0-9.]*,<([0-9][0-9.]*|[$][{]_TORCHVISION_CEILING[}])"$' "$INSTALL_SH" || true)
assert_eq "every torchvision constraint is upper-bounded" "$_total" "$_bounded"
_total=$(grep -cE '^[[:space:]]*TORCHAUDIO_CONSTRAINT="' "$INSTALL_SH" || true)
-_bounded=$(grep -cE '^[[:space:]]*TORCHAUDIO_CONSTRAINT="torchaudio>=[0-9][0-9.]*,<[0-9][0-9.]*"$' "$INSTALL_SH" || true)
+_bounded=$(grep -cE '^[[:space:]]*TORCHAUDIO_CONSTRAINT="torchaudio>=[0-9][0-9.]*,<([0-9][0-9.]*|[$][{]_TORCHAUDIO_CEILING[}])"$' "$INSTALL_SH" || true)
assert_eq "every torchaudio constraint is upper-bounded" "$_total" "$_bounded"
+
+# The top-level companion defaults compose their ceiling variable, so a bump stays one line.
+_count=$(grep -c '^TORCHVISION_CONSTRAINT="torchvision>=0.19,<${_TORCHVISION_CEILING}"$' "$INSTALL_SH" || true)
+assert_eq "torchvision default composes the ceiling" "1" "$_count"
+_count=$(grep -c '^TORCHAUDIO_CONSTRAINT="torchaudio>=2.4,<${_TORCHAUDIO_CEILING}"$' "$INSTALL_SH" || true)
+assert_eq "torchaudio default composes the ceiling" "1" "$_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)
-assert_eq "CUDA TORCH_CONSTRAINT widened to <2.12.0" "1" "$_cuda_widen"
# Widening keys off the final leaf (_torch_index_leaf), not the full URL, so a
# mirror base path with cu*/rocm7.2 but a cpu/older-rocm leaf is not mis-widened.
@@ -187,7 +203,7 @@ _PS1_CONTENT=$(cat "$INSTALL_PS1")
_ps1_has_var=$(echo "$_PS1_CONTENT" | grep -c 'TORCH_CONSTRAINT\|TorchConstraint' || true)
assert_eq "install.ps1 has no TORCH_CONSTRAINT variable" "0" "$_ps1_has_var"
-_ps1_hardcoded=$(echo "$_PS1_CONTENT" | grep -c '"torch>=2.4,<2.11.0"' || true)
+_ps1_hardcoded=$(echo "$_PS1_CONTENT" | grep -c '"torch>=2.4,<2.12.0"' || true)
_ps1_has_hc=$([ "$_ps1_hardcoded" -ge 1 ] && echo "yes" || echo "no")
assert_eq "install.ps1 has hardcoded torch constraint" "yes" "$_ps1_has_hc"
@@ -202,55 +218,55 @@ trap 'rm -rf "$TMPDIR_BASE"' EXIT
# 1. arm64 macOS py3.13 -> tightened
_result=$(run_constraint_snippet false macos arm64 13 "$TMPDIR_BASE/v1")
-assert_eq "arm64+macos+py313 -> tightened" "torch>=2.6,<2.11.0" "$_result"
+assert_eq "arm64+macos+py313 -> tightened" "torch>=2.6,<2.12.0" "$_result"
# 2. arm64 macOS py3.14 -> tightened (future-proofed)
_result=$(run_constraint_snippet false macos arm64 14 "$TMPDIR_BASE/v2")
-assert_eq "arm64+macos+py314 -> tightened" "torch>=2.6,<2.11.0" "$_result"
+assert_eq "arm64+macos+py314 -> tightened" "torch>=2.6,<2.12.0" "$_result"
# 3. arm64 macOS py3.12 -> default
_result=$(run_constraint_snippet false macos arm64 12 "$TMPDIR_BASE/v3")
-assert_eq "arm64+macos+py312 -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "arm64+macos+py312 -> default" "torch>=2.4,<2.12.0" "$_result"
# 4. arm64 macOS py3.11 -> default
_result=$(run_constraint_snippet false macos arm64 11 "$TMPDIR_BASE/v4")
-assert_eq "arm64+macos+py311 -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "arm64+macos+py311 -> default" "torch>=2.4,<2.12.0" "$_result"
# 5. Linux x86_64 py3.13 -> default (Linux unaffected)
_result=$(run_constraint_snippet false linux x86_64 13 "$TMPDIR_BASE/v5")
-assert_eq "linux+x86_64+py313 -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "linux+x86_64+py313 -> default" "torch>=2.4,<2.12.0" "$_result"
# 6. Linux aarch64 py3.13 -> default (guard checks OS=macos)
_result=$(run_constraint_snippet false linux aarch64 13 "$TMPDIR_BASE/v6")
-assert_eq "linux+aarch64+py313 -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "linux+aarch64+py313 -> default" "torch>=2.4,<2.12.0" "$_result"
# 7. Intel Mac x86_64 py3.12 -> default (arch mismatch)
_result=$(run_constraint_snippet false macos x86_64 12 "$TMPDIR_BASE/v7")
-assert_eq "macos+x86_64+py312 -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "macos+x86_64+py312 -> default" "torch>=2.4,<2.12.0" "$_result"
# 8. SKIP_TORCH=true arm64 macOS py3.13 -> block skipped, default
_result=$(run_constraint_snippet true macos arm64 13 "$TMPDIR_BASE/v8")
-assert_eq "SKIP_TORCH=true -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "SKIP_TORCH=true -> default" "torch>=2.4,<2.12.0" "$_result"
# 9. WSL py3.13 -> default
_result=$(run_constraint_snippet false wsl x86_64 13 "$TMPDIR_BASE/v9")
-assert_eq "wsl+py313 -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "wsl+py313 -> default" "torch>=2.4,<2.12.0" "$_result"
# 10. py_minor=0 (failed query fallback) -> default
_result=$(run_constraint_snippet false macos arm64 0 "$TMPDIR_BASE/v10")
-assert_eq "py_minor=0 fallback -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "py_minor=0 fallback -> default" "torch>=2.4,<2.12.0" "$_result"
# 11. Boundary: py_minor=12 -> NOT tightened
_result=$(run_constraint_snippet false macos arm64 12 "$TMPDIR_BASE/v11")
-assert_eq "boundary py_minor=12 -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "boundary py_minor=12 -> default" "torch>=2.4,<2.12.0" "$_result"
# 12. Boundary: py_minor=13 -> tightened
_result=$(run_constraint_snippet false macos arm64 13 "$TMPDIR_BASE/v12")
-assert_eq "boundary py_minor=13 -> tightened" "torch>=2.6,<2.11.0" "$_result"
+assert_eq "boundary py_minor=13 -> tightened" "torch>=2.6,<2.12.0" "$_result"
# 13. Intel Mac py3.13 -> default (arch=x86_64, not arm64)
_result=$(run_constraint_snippet false macos x86_64 13 "$TMPDIR_BASE/v13")
-assert_eq "macos+x86_64+py313 -> default" "torch>=2.4,<2.11.0" "$_result"
+assert_eq "macos+x86_64+py313 -> default" "torch>=2.4,<2.12.0" "$_result"
# ======================================================================
# Mock uv integration
diff --git a/tests/sh/test_unsloth_torch_override.sh b/tests/sh/test_unsloth_torch_override.sh
index 84e52ca2874..0438bcedda8 100644
--- a/tests/sh/test_unsloth_torch_override.sh
+++ b/tests/sh/test_unsloth_torch_override.sh
@@ -137,6 +137,94 @@ grep -qx 'torch==2.11.0+cu128' "$_merged" || _rc=1
assert_true "inherited torch-trio lines are dropped; generated pin wins" "$_rc"
rm -rf "$_ov_dir"
+# 5. The beside-the-caller override must not be world-readable: the merge copies every inherited
+# requirement in, and a direct URL can carry credentials. The mktemp fallback is already 0600.
+_creation=$(sed -n '/_UNSLOTH_TORCH_OVERRIDES="$_ov_dir\/.unsloth-torch-overrides/,/^ fi$/p' "$INSTALL_SH")
+# `if`, not a bare pipeline: under `set -e` a failing grep would abort the suite, not report.
+if printf '%s' "$_creation" | grep -q 'umask 077'; then _rc=0; else _rc=1; fi
+assert_true "the adjacent override file is created under umask 077" "$_rc"
+
+if printf '%s' "$_creation" | grep -q 'chmod 600'; then _rc=0; else _rc=1; fi
+assert_true "and chmod'd too, since \`: >\` truncates without changing an existing mode" "$_rc"
+
+# Drive the real construct: a recycled-PID file is what the umask alone cannot fix.
+_mode_dir=$(mktemp -d)
+(
+ umask 022
+ _f="$_mode_dir/.unsloth-torch-overrides.$$.txt"
+ if (umask 077; : > "$_f") 2>/dev/null; then chmod 600 "$_f" 2>/dev/null || true; fi
+ stat -c %a "$_f" > "$_mode_dir/fresh"
+
+ _s="$_mode_dir/stale.txt"
+ : > "$_s"; chmod 644 "$_s"
+ if (umask 077; : > "$_s") 2>/dev/null; then
+ stat -c %a "$_s" > "$_mode_dir/stale_umask_only"
+ chmod 600 "$_s" 2>/dev/null || true
+ fi
+ stat -c %a "$_s" > "$_mode_dir/stale_both"
+)
+if [ "$(cat "$_mode_dir/fresh")" = "600" ]; then _rc=0; else _rc=1; fi
+assert_true "a freshly created override file is 0600 under umask 022" "$_rc"
+if [ "$(cat "$_mode_dir/stale_umask_only")" = "644" ]; then _rc=0; else _rc=1; fi
+assert_true "umask alone leaves a stale file 0644, so the chmod is load-bearing" "$_rc"
+if [ "$(cat "$_mode_dir/stale_both")" = "600" ]; then _rc=0; else _rc=1; fi
+assert_true "umask plus chmod brings a stale file back to 0600" "$_rc"
+rm -rf "$_mode_dir"
+
+# 6. UV_OVERRIDE is split unquoted, so field splitting is followed by globbing. uv reads the
+# literal name (uv 0.11.32: "ov[1].txt" beside "ov1.txt" resolves the bracketed file), so
+# without `set -f` both walks iterate the sibling and the merge carries wrong requirements.
+_arm=$(sed -n '/^ torch==\*)$/,/^ ;;$/p' "$INSTALL_SH")
+_arm_body=$(printf '%s\n' "$_arm" | sed '1d;$d')
+[ -n "$_arm_body" ]
+assert_true "the torch==* overrides arm was extracted from install.sh" "$?"
+
+_glob_dir=$(mktemp -d)
+printf 'idna==3.6\n' > "$_glob_dir/ov[1].txt"
+printf 'idna==3.7\n' > "$_glob_dir/ov1.txt"
+_glob_out=$(
+ UV_OVERRIDE="$_glob_dir/ov[1].txt"
+ _torch_trio_pins='torch==2.11.0+cu128'
+ eval "$_arm_body"
+ cat "$_UNSLOTH_TORCH_OVERRIDES"
+ rm -f "$_UNSLOTH_TORCH_OVERRIDES"
+)
+if printf '%s\n' "$_glob_out" | grep -qx 'idna==3.6'; then _rc=0; else _rc=1; fi
+assert_true "a glob-metachar override path is read literally, not via its sibling" "$_rc"
+if printf '%s\n' "$_glob_out" | grep -qx 'idna==3.7'; then _rc=1; else _rc=0; fi
+assert_true "the unrelated sibling that the pattern matches is never folded in" "$_rc"
+if printf '%s\n' "$_glob_out" | grep -qx 'torch==2.11.0+cu128'; then _rc=0; else _rc=1; fi
+assert_true "the generated trio pin still leads the merged file" "$_rc"
+rm -rf "$_glob_dir"
+
+# set -f stops globbing but NOT field splitting, and uv takes UV_OVERRIDE as a space-separated
+# list, so multi-file must keep working. Quoting it instead would fold only the first file.
+_multi_dir=$(mktemp -d)
+printf 'idna==3.6\n' > "$_multi_dir/a.txt"
+printf 'certifi==2024.1.1\n' > "$_multi_dir/b.txt"
+_multi_out=$(
+ UV_OVERRIDE="$_multi_dir/a.txt $_multi_dir/b.txt"
+ _torch_trio_pins='torch==2.11.0+cu128'
+ eval "$_arm_body"
+ cat "$_UNSLOTH_TORCH_OVERRIDES"
+ rm -f "$_UNSLOTH_TORCH_OVERRIDES"
+)
+if printf '%s\n' "$_multi_out" | grep -qx 'idna==3.6' &&
+ printf '%s\n' "$_multi_out" | grep -qx 'certifi==2024.1.1'; then _rc=0; else _rc=1; fi
+assert_true "every file of a space-separated UV_OVERRIDE is still folded in" "$_rc"
+rm -rf "$_multi_dir"
+
+# The guard must not leak: callers below rely on globbing (_dir_has_entries walks "$1"/*).
+_glob_state=$(
+ UV_OVERRIDE=""
+ _torch_trio_pins='torch==2.11.0+cu128'
+ eval "$_arm_body"
+ rm -f "$_UNSLOTH_TORCH_OVERRIDES"
+ case $- in *f*) echo off ;; *) echo on ;; esac
+)
+if [ "$_glob_state" = on ]; then _rc=0; else _rc=1; fi
+assert_true "pathname expansion is restored after the arm" "$_rc"
+
echo ""
echo "Results: $PASS passed, $FAIL failed"
[ "$FAIL" -eq 0 ] || exit 1
diff --git a/tests/studio/install/test_cuda_repair.py b/tests/studio/install/test_cuda_repair.py
index 28127b6216f..1882cbc4919 100644
--- a/tests/studio/install/test_cuda_repair.py
+++ b/tests/studio/install/test_cuda_repair.py
@@ -859,8 +859,8 @@ class TestExpectedTorchFlavorRepairs:
def test_the_repair_uses_install_ps1s_bounded_trio(self):
_ok, mock_pip = _run_flavor_invariant(repaired = "2.10.0+cu124")
call_args = [str(a) for a in mock_pip.call_args.args]
- # NOT the <2.12 Linux range: an unbounded trio resolves back to the 2.11 wheel.
- for spec in ("torch>=2.4,<2.11.0", "torchvision>=0.19,<0.26.0", "torchaudio>=2.4,<2.11.0"):
+ # The bounded trio install.ps1 repairs with; Windows now shares the Linux <2.12 window.
+ for spec in ("torch>=2.4,<2.12.0", "torchvision>=0.19,<0.27.0", "torchaudio>=2.4,<2.12.0"):
assert spec in call_args
def test_untagged_pypi_wheel_is_repaired(self):
diff --git a/tests/studio/install/test_pr5940_followups.py b/tests/studio/install/test_pr5940_followups.py
index 91b5e480c40..976dabe9df4 100644
--- a/tests/studio/install/test_pr5940_followups.py
+++ b/tests/studio/install/test_pr5940_followups.py
@@ -489,7 +489,11 @@ def test_install_ps1_rocm_cpu_fallback_uses_retry():
text = _INSTALL_PS1.read_text(encoding = "utf-8")
i = text.find("ROCm PyTorch install failed")
assert i != -1, "ROCm->CPU fallback block not found in install.ps1"
- window = text[i : i + 600]
+ # Slice up to the fallback's own install command, not a fixed byte count: the commentary
+ # grows and a byte window quietly stops covering the line it checks.
+ _end = text.find("--default-index", i)
+ assert _end != -1, "ROCm->CPU fallback install command not found in install.ps1"
+ window = text[i:_end]
assert (
"Invoke-InstallCommandRetry" in window
), "the ROCm->CPU fallback torch install must use Invoke-InstallCommandRetry"
diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.py
index fd1a5564724..db4f3202151 100644
--- a/tests/studio/install/test_rocm_support.py
+++ b/tests/studio/install/test_rocm_support.py
@@ -5548,12 +5548,35 @@ class TestRocmTorchPkgSpecs:
assert "2.11" in torch_spec
def test_default_caps_below_211(self):
- """Default spec (rocm7.1 and earlier) should cap below 2.11."""
+ """Default spec (rocm7.0 and earlier) should cap below 2.11."""
specs = stack_mod._ROCM_TORCH_PKG_SPECS.get("_default")
assert specs is not None
torch_spec = specs[0]
assert "<2.11" in torch_spec
+ def test_rocm71_repair_matches_install_sh_default_range(self):
+ """rocm7.1 serves a paired 2.11 trio, so the repair path must not cap at <2.11.
+
+ install.sh leaves a rocm7.1 leaf on its default trio (torch>=2.4,<2.12.0 /
+ torchvision>=0.19,<0.27.0 / torchaudio>=2.4,<2.12.0), which resolves
+ torch 2.11.0+rocm7.1 on that index. Falling back to _default here would
+ force-reinstall 2.10.0+rocm7.1 over it on the next `studio update`.
+ """
+ specs = stack_mod._ROCM_TORCH_PKG_SPECS.get("rocm7.1")
+ assert specs is not None, "rocm7.1 must have its own repair spec"
+ assert specs == (
+ "torch>=2.4,<2.12.0",
+ "torchvision>=0.19,<0.27.0",
+ "torchaudio>=2.4,<2.12.0",
+ )
+ # Not the rocm7.2 spec: no 2.11 floor applies to rocm7.1.
+ assert specs != stack_mod._ROCM_TORCH_PKG_SPECS["rocm7.2"]
+
+ def test_rocm71_is_not_a_known_211_floor_version(self):
+ """The widened rocm7.1 range must NOT promote it to a floored 2.11 line."""
+ assert (7, 1) not in stack_mod._ROCM_KNOWN_TORCH211_VERSIONS
+ assert (7, 2) in stack_mod._ROCM_KNOWN_TORCH211_VERSIONS
+
def test_specs_have_torch_vision_audio(self):
"""Each entry should be a 3-tuple: torch, torchvision, torchaudio."""
for tag, specs in stack_mod._ROCM_TORCH_PKG_SPECS.items():
diff --git a/tests/studio/install/test_windows_torch_flavor_invariant.py b/tests/studio/install/test_windows_torch_flavor_invariant.py
index 04dd9f6190b..910f0fa6255 100644
--- a/tests/studio/install/test_windows_torch_flavor_invariant.py
+++ b/tests/studio/install/test_windows_torch_flavor_invariant.py
@@ -159,9 +159,15 @@ class TestInstallPs1Parity:
same wheels, and a support log from either must read the same."""
def test_the_repair_trio_matches_install_ps1(self):
- match = re.search(r'else\s*\{\s*@\((\s*"torch[^)]*?)\)\s*\}', _INSTALL_SRC, re.S)
- assert match is not None, "install.ps1's flavor-repair spec array moved"
- ps_specs = tuple(re.findall(r'"([^"]+)"', match.group(1)))
+ # install.ps1 builds the non-XPU trio as three scalars; a kept pin substitutes one by one.
+ match = re.search(
+ r'\$_fixTorchSpec\s*=\s*("[^"]+")\s*;\s*'
+ r'\$_fixVisionSpec\s*=\s*("[^"]+")\s*;\s*'
+ r'\$_fixAudioSpec\s*=\s*("[^"]+")',
+ _INSTALL_SRC,
+ )
+ assert match is not None, "install.ps1's flavor-repair spec scalars moved"
+ ps_specs = tuple(re.findall(r'"([^"]+)"', "".join(match.groups())))
py_specs = tuple(
re.findall(
r'"([^"]+)"',
diff --git a/tests/studio/test_frontend_dist_cache.py b/tests/studio/test_frontend_dist_cache.py
index a0a3d66352d..e58baa6dc36 100644
--- a/tests/studio/test_frontend_dist_cache.py
+++ b/tests/studio/test_frontend_dist_cache.py
@@ -192,7 +192,9 @@ def _staleness_inputs_ps1() -> set[str]:
"""
text = SETUP_PS1.read_text(encoding = "utf-8")
block = re.search(
- r'\$DistDir = Join-Path \$FrontendDir "dist"(.*?)# Provision Node when the frontend build',
+ # Terminator matched on the stable "# Provision Node" prefix, not the whole sentence:
+ # pinning the full prose would make this reader return None and fail every check below.
+ r'\$DistDir = Join-Path \$FrontendDir "dist"(.*?)\n# Provision Node ',
text,
re.S,
)
diff --git a/tests/studio/test_pre_turing_cap.ps1 b/tests/studio/test_pre_turing_cap.ps1
index dcef77e4591..c9a4596dfad 100644
--- a/tests/studio/test_pre_turing_cap.ps1
+++ b/tests/studio/test_pre_turing_cap.ps1
@@ -102,7 +102,7 @@ foreach ($file in @("install.ps1", "studio/setup.ps1")) {
# --- the cap only rewrites the families it can replace ---------------------
$script:FakeSmiStdout = "7.0"
Check "cap cu130 on a V100 -> cu126" ((Get-CudaFamilyCappedForPreTuring 'cu130' "nvidia-smi") -eq 'cu126')
- Check "cap cu128 on a V100 -> cu128 (floor 70)" ((Get-CudaFamilyCappedForPreTuring 'cu128' "nvidia-smi") -eq 'cu128')
+ Check "cap cu128 on a V100 -> cu126 (floor 75)" ((Get-CudaFamilyCappedForPreTuring 'cu128' "nvidia-smi") -eq 'cu126')
Check "cap cu126 is a no-op" ((Get-CudaFamilyCappedForPreTuring 'cu126' "nvidia-smi") -eq 'cu126')
Check "cap cu124 is a no-op" ((Get-CudaFamilyCappedForPreTuring 'cu124' "nvidia-smi") -eq 'cu124')
Check "cap cpu is a no-op" ((Get-CudaFamilyCappedForPreTuring 'cpu' "nvidia-smi") -eq 'cpu')
diff --git a/tests/studio/test_previous_torch_pin.ps1 b/tests/studio/test_previous_torch_pin.ps1
new file mode 100644
index 00000000000..257af888898
--- /dev/null
+++ b/tests/studio/test_previous_torch_pin.ps1
@@ -0,0 +1,118 @@
+#!/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 install.ps1's torch release preservation helpers (ConvertTo-TorchNumericRelease,
+# Test-TorchReleaseInWindow, Get-PreviousTorchPin), the Windows port of _previous_torch_pin (PR 7250).
+# Run: pwsh -NoProfile -File tests/studio/test_previous_torch_pin.ps1
+
+$ErrorActionPreference = "Stop"
+$installPath = [System.IO.Path]::Combine($PSScriptRoot, "..", "..", "install.ps1")
+$installPath = (Resolve-Path $installPath).Path
+
+# Parse install.ps1 (also a syntax gate) and extract the helpers.
+$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-TorchNumericRelease", "Test-TorchReleaseInWindow", "Get-PreviousTorchPin")) {
+ $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 install.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++ }
+}
+
+$savedUpgrade = $env:UNSLOTH_TORCH_UPGRADE
+try {
+ Remove-Item Env:UNSLOTH_TORCH_UPGRADE -ErrorAction SilentlyContinue
+ $win = "torch>=2.4,<2.12.0"
+
+ foreach ($v in @("2.10.0", "2.10.0+cpu", "2.10.0+cu126", "2.10.1+cu130", "2.9.1+rocm7.2.1", "2.9.0+xpu", "2.10")) {
+ $r = ConvertTo-TorchNumericRelease $v
+ Check "release accepts $v" ($null -ne $r)
+ }
+ $r = ConvertTo-TorchNumericRelease "2.10.1+cu128"
+ Check "release strips only the +local tag" ($r.PublicBase -eq "2.10.1" -and $r.Minor -eq 10)
+
+ foreach ($v in @("", " ", "2.11.0.dev20260701+cu130", "2.9.0a0+gitabc123", "2.11.0rc1+cu130",
+ ".2.10", "2.10.", "2..10", "2.x.0", "not-a-version",
+ "Traceback (most recent call last):", "99999999999999999999.1")) {
+ Check "release rejects '$v'" ($null -eq (ConvertTo-TorchNumericRelease $v))
+ }
+
+ $cases = @(
+ @{ v = "2.4.0"; c = "torch>=2.4,<2.12.0"; ok = $true; n = "at the floor" }
+ @{ v = "2.3.1"; c = "torch>=2.4,<2.12.0"; ok = $false; n = "below the floor" }
+ @{ v = "2.11.0"; c = "torch>=2.4,<2.12.0"; ok = $true; n = "just below the ceiling" }
+ @{ v = "2.12.0"; c = "torch>=2.4,<2.12.0"; ok = $false; n = "at the ceiling" }
+ @{ v = "2.13.0"; c = "torch>=2.4,<2.12.0"; ok = $false; n = "above the ceiling" }
+ @{ v = "2.10.0"; c = "torch>=2.11.0,<2.12.0"; ok = $false; n = "2.11 floor rejects 2.10" }
+ @{ v = "2.11.0"; c = "torch>=2.11.0,<2.12.0"; ok = $true; n = "2.11 floor accepts 2.11" }
+ @{ v = "2.11.0"; c = "torch>=2.4,<2.13.0"; ok = $true; n = "future 2.13 ceiling keeps 2.11" }
+ @{ v = "2.12.1"; c = "torch>=2.4,<2.13.0"; ok = $true; n = "future 2.13 ceiling keeps 2.12" }
+ )
+ foreach ($t in $cases) {
+ $rel = ConvertTo-TorchNumericRelease $t.v
+ Check ("window: " + $t.n) ((Test-TorchReleaseInWindow -Release $rel -Constraint $t.c) -eq $t.ok)
+ }
+ # Malformed constraints fail closed.
+ $rel = ConvertTo-TorchNumericRelease "2.10.0"
+ Check "window: malformed constraint fails closed" (-not (Test-TorchReleaseInWindow -Release $rel -Constraint "torch"))
+ Check "window: exact-pin constraint fails closed" (-not (Test-TorchReleaseInWindow -Release $rel -Constraint "torch==2.10.0"))
+
+ $pin = Get-PreviousTorchPin -TorchVersion "2.10.0+cu128" -Constraint $win
+ Check "pin keeps 2.10.0 (exact release, sh parity)" ($pin.TorchSpec -eq "torch==2.10.0")
+ Check "pin pairs torchvision to the kept minor" ($pin.VisionSpec -eq "torchvision==0.25.*")
+ Check "pin pairs torchaudio to the kept minor" ($pin.AudioSpec -eq "torchaudio==2.10.*")
+ $pin = Get-PreviousTorchPin -TorchVersion "2.9.1+rocm7.2.1" -Constraint $win
+ Check "pin keeps 2.9.1 with 0.24.*/2.9.* companions" (
+ $pin.TorchSpec -eq "torch==2.9.1" -and $pin.VisionSpec -eq "torchvision==0.24.*" -and $pin.AudioSpec -eq "torchaudio==2.9.*")
+ $pin = Get-PreviousTorchPin -TorchVersion "2.11.0+cpu" -Constraint $win
+ Check "pin keeps 2.11.0 under a future-widened window" ($pin.TorchSpec -eq "torch==2.11.0")
+
+ Check "no pin without a previous version" ($null -eq (Get-PreviousTorchPin -TorchVersion "" -Constraint $win))
+ Check "no pin for a below-floor release" ($null -eq (Get-PreviousTorchPin -TorchVersion "2.3.1+cpu" -Constraint $win))
+ Check "raised ROCm floor rejects keeping 2.10" ($null -eq (Get-PreviousTorchPin -TorchVersion "2.10.0+rocm7.1" -Constraint "torch>=2.11.0,<2.12.0"))
+ Check "no pin for a nightly build" ($null -eq (Get-PreviousTorchPin -TorchVersion "2.11.0.dev20260701+cu130" -Constraint $win))
+ Check "no pin for an unsupported 2.12 under a <2.12 window" ($null -eq (Get-PreviousTorchPin -TorchVersion "2.12.0+cu130" -Constraint $win))
+
+ $env:UNSLOTH_TORCH_UPGRADE = "1"
+ Check "UNSLOTH_TORCH_UPGRADE=1 disables the pin" ($null -eq (Get-PreviousTorchPin -TorchVersion "2.10.0+cpu" -Constraint $win))
+ $env:UNSLOTH_TORCH_UPGRADE = "0"
+ Check "UNSLOTH_TORCH_UPGRADE=0 keeps the pin" ($null -ne (Get-PreviousTorchPin -TorchVersion "2.10.0+cpu" -Constraint $win))
+} finally {
+ if ($null -ne $savedUpgrade) { $env:UNSLOTH_TORCH_UPGRADE = $savedUpgrade }
+ else { Remove-Item Env:UNSLOTH_TORCH_UPGRADE -ErrorAction SilentlyContinue }
+}
+
+$src = Get-Content $installPath -Raw
+Check "probe runs before the rollback move" (
+ $src.IndexOf('$script:PrevTorchVer') -ge 0 -and
+ $src.IndexOf('$script:PrevTorchVer') -lt $src.IndexOf('Start-StudioVenvRollback -ExistingDir'))
+Check "pin decision cites the UNSLOTH_TORCH_UPGRADE escape hatch" ($src -match 'UNSLOTH_TORCH_UPGRADE=1 to get the newest')
+Check "kept-release fallback clears the pin" ($src -match '\$script:PrevTorchPin\s*=\s*\$null')
+Check "kept release exported for setup.ps1" ($src -match 'UNSLOTH_KEPT_TORCH')
+# The probe must read dist metadata: a broken DLL would fail "import torch" and drop the pin.
+Check "probe reads dist metadata, not import torch" (
+ $src -match 'importlib\.metadata as m; print\(m\.version' -and
+ $src -notmatch '-c "import torch; print\(torch\.__version__\)"')
+# Under irm | iex the script scope is the caller's session, so a second run must not inherit it.
+Check "preservation state reset before the venv branch" (
+ $src.IndexOf('$script:PrevTorchVer = ""') -ge 0 -and
+ $src.IndexOf('$script:PrevTorchVer = ""') -lt $src.IndexOf('if (Test-Path -LiteralPath $VenvPython) {'))
+Check "preservation pin reset before the venv branch" (
+ $src.IndexOf('$script:PrevTorchPin = $null') -lt $src.IndexOf('if (Test-Path -LiteralPath $VenvPython) {'))
+# A terminating exception must not leave the handoff set in a surviving session.
+Check "outer finally clears the kept-torch handoff" (
+ $src -match '(?s)try \{\s*Install-UnslothStudio @args\s*\} finally \{.*?Remove-Item Env:UNSLOTH_KEPT_TORCH')
+
+Write-Host ""
+if ($failures -gt 0) { Write-Host "$failures check(s) failed" -ForegroundColor Red; exit 1 }
+Write-Host "All checks passed"
+exit 0
diff --git a/tests/studio/test_torch_overrides_merge.ps1 b/tests/studio/test_torch_overrides_merge.ps1
new file mode 100644
index 00000000000..ebb9e67f126
--- /dev/null
+++ b/tests/studio/test_torch_overrides_merge.ps1
@@ -0,0 +1,139 @@
+#!/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 install.ps1's New-UnslothTorchOverridesFile, the Windows twin of install.sh's
+# _build_unsloth_torch_overrides. It folds a caller's UV_OVERRIDE file into the frozen torch-trio
+# pins without corrupting it: non-ASCII lines must survive, and the merged file must stay beside
+# the caller's override, as uv resolves `-r nested.txt` relative to THAT file's own directory.
+# Run: pwsh -NoProfile -File tests/studio/test_torch_overrides_merge.ps1
+
+$ErrorActionPreference = "Stop"
+$installPath = [System.IO.Path]::Combine($PSScriptRoot, "..", "..", "install.ps1")
+$installPath = (Resolve-Path $installPath).Path
+
+# Parse install.ps1 (also a syntax gate) and extract the helper.
+$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" }
+
+$fn = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
+ $n.Name -eq "New-UnslothTorchOverridesFile"
+}, $true)
+if ($fn.Count -ne 1) { throw "expected exactly one New-UnslothTorchOverridesFile in install.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++ }
+}
+
+# The helper reads $SkipTorch from its enclosing scope.
+$SkipTorch = $false
+
+# Stand-in interpreter: the helper only runs `& $PythonExe -c` and reads `name==version` lines.
+$work = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-ovtest-" + [guid]::NewGuid().ToString("N"))
+New-Item -ItemType Directory -Path $work -Force | Out-Null
+$fakePy = Join-Path $work "fakepython"
+Set-Content -LiteralPath $fakePy -Value @(
+ "#!/usr/bin/env bash"
+ "printf 'torch==2.11.0+cu130\ntorchvision==0.26.0+cu130\ntorchaudio==2.11.0+cu130\n'"
+) -Encoding ascii
+if ($IsLinux -or $IsMacOS) { & chmod +x $fakePy }
+
+$acute = [char]0x00E9 # e-acute, the cheapest non-ASCII requirement character
+$savedOverride = $env:UV_OVERRIDE
+$made = @()
+
+try {
+ $callerDir = Join-Path $work "callerdir"
+ New-Item -ItemType Directory -Path $callerDir -Force | Out-Null
+ $nested = Join-Path $callerDir "nested.txt"
+ Set-Content -LiteralPath $nested -Value "idna==3.6" -Encoding ascii
+ $callerOv = Join-Path $callerDir "over.txt"
+ [System.IO.File]::WriteAllText(
+ $callerOv,
+ "-r nested.txt`ncaf${acute}pkg==1.0`ntorch==1.0`ntorchvision==0.1`nplainpkg==2.0`n",
+ (New-Object System.Text.UTF8Encoding($false)))
+
+ $env:UV_OVERRIDE = $callerOv
+ $merged = New-UnslothTorchOverridesFile -PythonExe $fakePy
+ $made += $merged
+ Check "returns a merged overrides file" ($null -ne $merged -and (Test-Path -LiteralPath $merged))
+
+ $mergedText = [System.IO.File]::ReadAllText($merged)
+
+ Check "non-ASCII caller requirement survives the merge (no '?' substitution)" `
+ ($mergedText -match "caf${acute}pkg==1\.0" -and $mergedText -notmatch 'caf\?pkg')
+ Check "merged file sits where the caller's relative includes still resolve" `
+ (Test-Path -LiteralPath (Join-Path (Split-Path -Parent $merged) "nested.txt"))
+
+ # Guards on the behaviour that already worked, so the fix cannot regress it.
+ Check "frozen torch trio is pinned first" ($mergedText -match '^torch==2\.11\.0\+cu130')
+ Check "caller's torch-trio lines are dropped" `
+ ($mergedText -notmatch '(?m)^torch==1\.0$' -and $mergedText -notmatch '(?m)^torchvision==0\.1$')
+ Check "caller's relative include line is carried over" ($mergedText -match '(?m)^-r nested\.txt$')
+ Check "caller's ordinary requirements are carried over" ($mergedText -match '(?m)^plainpkg==2\.0$')
+ Check "merged file is newline terminated" ($mergedText.EndsWith("`n"))
+
+ $bytes = [System.IO.File]::ReadAllBytes($merged)
+ Check "merged file has no UTF-8 BOM" `
+ (-not ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF))
+
+ Remove-Item Env:UV_OVERRIDE -ErrorAction SilentlyContinue
+ $bare = New-UnslothTorchOverridesFile -PythonExe $fakePy
+ $made += $bare
+ Check "works with no caller override file" ($null -ne $bare -and (Test-Path -LiteralPath $bare))
+ if ($bare) {
+ $bareText = [System.IO.File]::ReadAllText($bare)
+ Check "bare merge carries the whole trio" `
+ ($bareText -match 'torch==2\.11\.0' -and $bareText -match 'torchvision==0\.26\.0' -and
+ $bareText -match 'torchaudio==2\.11\.0')
+ }
+
+ $SkipTorch = $true
+ Check "returns null under --no-torch" ($null -eq (New-UnslothTorchOverridesFile -PythonExe $fakePy))
+ $SkipTorch = $false
+
+ # ── the merged copy is tracked and locked down ────────────────────────────────
+ # The caller's non-torch lines land in this copy, one of which can be an authenticated URL.
+ # Three Windows-only hazards, so each is asserted behaviourally where it can be and
+ # structurally from the AST, which runs everywhere.
+
+ $env:UV_OVERRIDE = $ovPath
+ $script:TorchOverridesFile = $null
+ $merged = New-UnslothTorchOverridesFile -PythonExe $fakePy
+ $made += $merged
+ Check "the merged path is tracked for the outer cleanup" ($script:TorchOverridesFile -eq $merged)
+
+ $src = $fn[0].Extent.Text
+ $iTrack = $src.IndexOf('$script:TorchOverridesFile = $f')
+ $iWrite = $src.IndexOf('[System.IO.File]::WriteAllText(')
+ Check "the path is tracked BEFORE the write that can throw" (($iTrack -ge 0) -and ($iTrack -lt $iWrite))
+ Check "a failed write removes the file it created" ($src -match 'catch \{\s*\r?\n\s*Remove-Item -LiteralPath \$f')
+
+ # Get-Content decodes a BOM-less file with the ANSI code page on PS 5.1, which is where the
+ # mojibake came from. pwsh on Linux defaults to UTF-8, so the round trip cannot fail here
+ # even unfixed; the assertion holding the fix in place is the one on the reader.
+ Check "the caller's override is read as UTF-8, not by the shell's default codepage" (
+ ($src -match '\[System\.IO\.File\]::ReadAllLines\(') -and ($src -notmatch 'Get-Content -LiteralPath \$ovFile')
+ )
+ Check "the inherited ACL is replaced rather than kept" (
+ ($src -match 'SetAccessRuleProtection\(\$true, \$false\)') -and ($src -match 'Set-Acl -LiteralPath \$f')
+ )
+}
+finally {
+ if ($null -eq $savedOverride) { Remove-Item Env:UV_OVERRIDE -ErrorAction SilentlyContinue }
+ else { $env:UV_OVERRIDE = $savedOverride }
+ foreach ($m in $made) { if ($m) { Remove-Item -LiteralPath $m -Force -ErrorAction SilentlyContinue } }
+ Remove-Item -LiteralPath $work -Recurse -Force -ErrorAction SilentlyContinue
+}
+
+Write-Host ""
+if ($failures -gt 0) {
+ Write-Host "FAILED ($failures)" -ForegroundColor Red
+ exit 1
+}
+Write-Host "All New-UnslothTorchOverridesFile checks passed" -ForegroundColor Green
+exit 0
diff --git a/tests/studio/test_unsloth_torch_override.ps1 b/tests/studio/test_unsloth_torch_override.ps1
new file mode 100644
index 00000000000..e7ae30d5730
--- /dev/null
+++ b/tests/studio/test_unsloth_torch_override.ps1
@@ -0,0 +1,127 @@
+#!/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
+# Windows twin of tests/sh/test_unsloth_torch_override.sh: install.ps1's torch-trio --overrides
+# guard (New-UnslothTorchOverridesFile) on the Step-2 unsloth installs. The generated file folds in
+# the caller's UV_OVERRIDE lines, which can carry authenticated URLs, so it must never outlive the
+# run: install.sh removes its twin from the traps, install.ps1 from the outer finally.
+# Run: pwsh -NoProfile -File tests/studio/test_unsloth_torch_override.ps1
+
+$ErrorActionPreference = "Stop"
+$installPath = [System.IO.Path]::Combine($PSScriptRoot, "..", "..", "install.ps1")
+$installPath = (Resolve-Path $installPath).Path
+$installText = Get-Content -Raw $installPath
+
+# Parse install.ps1 (also serves as a syntax gate).
+$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" }
+
+$failures = 0
+function Check($name, $cond) {
+ if ($cond) { Write-Host " PASS $name" }
+ else { Write-Host " FAIL $name" -ForegroundColor Red; $script:failures++ }
+}
+
+# Text of every Invoke-InstallCommandRetry statement carrying $label; a with-deps path has two.
+function Get-InstallBlocks([string]$label) {
+ $calls = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.CommandAst] -and
+ $n.Extent.Text -like "*-Label `"$label`" *" -and
+ $n.GetCommandName() -eq "Invoke-InstallCommandRetry"
+ }, $true)
+ if ($calls.Count -eq 0) { throw "no Invoke-InstallCommandRetry found for '$label'" }
+ return @($calls | ForEach-Object { $_.Extent.Text })
+}
+
+Write-Host "New-UnslothTorchOverridesFile"
+$fnAst = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
+ $n.Name -eq "New-UnslothTorchOverridesFile"
+}, $true)
+Check "helper defined exactly once" ($fnAst.Count -eq 1)
+$fnText = $fnAst[0].Extent.Text
+Check "helper returns null under --no-torch" ($fnText -match 'if \(\$SkipTorch\) \{ return \$null \}')
+Check "helper folds in caller UV_OVERRIDE files" ($fnText -match '\$env:UV_OVERRIDE')
+
+Write-Host "with-deps unsloth installs pass --overrides"
+foreach ($label in @("install unsloth (local)", "install unsloth")) {
+ $blocks = Get-InstallBlocks $label
+ $guarded = @($blocks | Where-Object { $_ -match '--overrides \$script:TorchOverridesFile' })
+ Check "'$label' has one overrides-guarded call and one plain fallback" (
+ $blocks.Count -eq 2 -and $guarded.Count -eq 1)
+}
+
+Write-Host "the --no-deps no-torch installs carry no overrides"
+foreach ($label in @("install unsloth (no-torch)", "install unsloth (migrated no-torch)")) {
+ $blocks = Get-InstallBlocks $label
+ Check "'$label' has no --overrides" (@($blocks | Where-Object { $_ -match '--overrides' }).Count -eq 0)
+}
+
+Write-Host "the generated temp file never outlives the run"
+$removals = [regex]::Matches($installText, 'Remove-Item -LiteralPath \$script:TorchOverridesFile -Force')
+# One in-flow removal per with-deps install, plus the outer-finally sweep.
+Check "in-flow removal after each with-deps install, plus a final sweep" ($removals.Count -eq 3)
+$outer = @($ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.TryStatementAst] -and
+ $n.Body.Extent.Text -match 'Install-UnslothStudio @args'
+}, $true))
+Check "outer try/finally around Install-UnslothStudio found" ($outer.Count -eq 1)
+$finallyText = $outer[0].Finally.Extent.Text
+Check "outer finally removes the overrides temp file" ($finallyText -match 'Remove-Item -LiteralPath \$script:TorchOverridesFile -Force')
+Check "outer finally still clears UNSLOTH_KEPT_TORCH" ($finallyText -match 'Remove-Item Env:UNSLOTH_KEPT_TORCH')
+# install.sh empties _UNSLOTH_TORCH_OVERRIDES before arming its traps so an inherited value is
+# never rm'd; under `irm | iex` the same reset must precede the outer try.
+Check "overrides path reset to null before the outer try" (
+ $installText -match '(?m)^\$script:TorchOverridesFile = \$null\r?\ntry \{\r?\n\s*Install-UnslothStudio @args')
+
+Write-Host "outer finally actually deletes the file after a terminating error"
+# Behavioural: run the real finally body with a live temp file holding a credential line.
+$leakFile = [System.IO.Path]::GetTempFileName()
+Set-Content -LiteralPath $leakFile -Encoding ascii -Value @(
+ "torch==2.11.0+cu128",
+ "private-pkg @ https://svc:[email protected]/private-1.0-py3-none-any.whl")
+$script:TorchOverridesFile = $leakFile
+$env:UNSLOTH_KEPT_TORCH = "2.11.0"
+# Strip the `finally { ... }` wrapper and run the statements: Invoke-Expression uses this scope.
+$finallyBody = ($finallyText.Trim() -replace '(?s)^\{', '') -replace '(?s)\}$', ''
+try {
+ try { throw "simulated terminating error mid-install" }
+ finally { Invoke-Expression $finallyBody }
+} catch { }
+Check "temp overrides file removed" (-not (Test-Path -LiteralPath $leakFile))
+Check "kept-torch handoff still cleared" ($null -eq $env:UNSLOTH_KEPT_TORCH)
+Check "tracked path reset so a rerun cannot re-remove it" ($null -eq $script:TorchOverridesFile)
+Remove-Item -LiteralPath $leakFile -Force -ErrorAction SilentlyContinue
+
+Write-Host "the inherited-override filter drops the torch trio in any casing"
+# PowerShell's -notmatch is case-insensitive, so a caller override `Torch<2.11` is dropped.
+$filterPattern = $null
+if ($fnText -match '\$_ -notmatch ''([^'']+)''') { $filterPattern = $Matches[1] }
+Check "filter pattern extracted from the helper" ($null -ne $filterPattern)
+$inherited = @(
+ "# comment survives",
+ "Torch<2.11",
+ "TORCHVISION>=0.19",
+ "TorchAudio==2.1",
+ "torch<2.11.0",
+ "torchvision==0.25.0",
+ "torchaudio!=2.11.0",
+ "torchmetrics==1.0",
+ "transformers>=4.57.6",
+ "anyio<4.14.0"
+)
+$merged = @("torch==2.11.0+cu128") + @($inherited | Where-Object { $_ -notmatch $filterPattern })
+$trio = @($merged | Where-Object { $_ -match '^(torch|torchvision|torchaudio)([\s<>=!~;@[]|$)' })
+Check "exactly one trio requirement survives (the generated pin)" ($trio.Count -eq 1)
+Check "the survivor is the generated exact pin" ($trio[0] -eq "torch==2.11.0+cu128")
+foreach ($keep in @("torchmetrics==1.0", "transformers>=4.57.6", "anyio<4.14.0", "# comment survives")) {
+ Check "unrelated inherited override preserved: $keep" ($merged -contains $keep)
+}
+
+Write-Host ""
+if ($failures -gt 0) {
+ Write-Host "FAILED: $failures check(s)" -ForegroundColor Red
+ exit 1
+}
+Write-Host "All checks passed."
diff --git a/tests/studio/test_xpu_arm64_torchaudio.ps1 b/tests/studio/test_xpu_arm64_torchaudio.ps1
index 01a8dbc15b5..bab22aac5c2 100644
--- a/tests/studio/test_xpu_arm64_torchaudio.ps1
+++ b/tests/studio/test_xpu_arm64_torchaudio.ps1
@@ -61,23 +61,27 @@ foreach ($p in @("", "linux-x86_64", "macosx-14.0-arm64", "win32")) {
Check "an uppercase arm64 is still arm64" ((Get-XpuTorchSpecs -Platform "WIN-ARM64").Count -eq 2)
Check "the probe lowercases its answer" ((Get-FunctionAst "Get-VenvPlatformTag").Extent.Text -match 'ToLowerInvariant')
-# --- both call sites go through it -----------------------------------------------------------
-# Text, not AST, for the call count: asserting the COUNT catches a copy that quietly
-# reintroduces its own literal trio.
+# Text, not AST, for the call count: the COUNT catches a copy that reintroduces its own literal
+# trio. Three sites: fresh XPU install, flavor repair, release-preservation probe.
$src = Get-Content -Raw -LiteralPath $installPs1
-Check "builder is used at 2 sites" (([regex]::Matches($src, 'Get-XpuTorchSpecs -Platform')).Count -eq 2)
+Check "builder is used at 3 sites" (([regex]::Matches($src, 'Get-XpuTorchSpecs -Platform')).Count -eq 3)
# The literal trio must exist in exactly ONE place now (the builder itself), or the drift is back.
Check "one literal torchaudio 2.6 pin" (([regex]::Matches($src, '"torchaudio>=2\.6,<2\.11\.0"')).Count -eq 1)
-# The repair site specifically: find the assignment that feeds the reinstall command.
-$fixAssign = $ast.FindAll({ param($n)
+# A kept-release pin substitutes one spec at a time and must restore the same way.
+$origAssign = $ast.FindAll({ param($n)
$n -is [System.Management.Automation.Language.AssignmentStatementAst] -and
- $n.Left.Extent.Text -eq '$_fixSpecs'
+ $n.Left.Extent.Text -eq '$_origFixSpecs' -and
+ $n.Right.Extent.Text -match 'Get-XpuTorchSpecs'
}, $true)
-Check "the repair builds one _fixSpecs" ($fixAssign.Count -eq 1)
-Check "the repair calls the builder" ($fixAssign.Count -eq 1 -and $fixAssign[0].Right.Extent.Text -match 'Get-XpuTorchSpecs')
-# The non-XPU arm must be untouched -- this PR must not move CUDA/ROCm hosts at all.
-Check "the repair keeps the 2.4 CUDA floor" ($fixAssign.Count -eq 1 -and $fixAssign[0].Right.Extent.Text -match 'torch>=2\.4,<2\.11\.0')
+Check "the repair calls the builder" ($origAssign.Count -eq 1)
+# The non-XPU arm keeps the generic 2.4 floor: only the ceiling moved to the 2.11 line.
+$defaultAssign = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.AssignmentStatementAst] -and
+ $n.Left.Extent.Text -eq '$_fixTorchSpec' -and
+ $n.Right.Extent.Text -match '^"torch>='
+}, $true)
+Check "the repair keeps the 2.4 CUDA floor" ($defaultAssign.Count -eq 1 -and $defaultAssign[0].Right.Extent.Text -eq '"torch>=2.4,<2.12.0"')
if ($failures -gt 0) { Write-Host "FAILED: $failures" -ForegroundColor Red; exit 1 }
Write-Host "All XPU arm64 torchaudio checks passed."
diff --git a/tests/test_windows_amd_gpu_scan_fallback.py b/tests/test_windows_amd_gpu_scan_fallback.py
index 76b80233d76..c3a08f3fd95 100644
--- a/tests/test_windows_amd_gpu_scan_fallback.py
+++ b/tests/test_windows_amd_gpu_scan_fallback.py
@@ -108,9 +108,14 @@ def _amd_scan_block(src: str) -> str:
def _arch_resolution_block(src: str) -> str:
- """Everything from the arch-resolution header up to the ROCm version capture that follows."""
- start = src.index(" # ── Arch resolution:")
- end = src.index(" # Capture ROCm version early", start)
+ """Everything from the arch-resolution guard up to the hipconfig probe that follows.
+
+ Anchored on CODE at both ends for the same reason as _installer_scan_block: the two
+ comments this used to key on are exactly the kind a comment pass rewrites, and the
+ failure it produces is a ValueError rather than an assertion that says anything."""
+ marker = src.index("$script:ROCmUnsupportedGfxArch = $null")
+ start = src.index(" if (-not $script:ROCmGfxArch) {", marker)
+ end = src.index(" if ($HasROCm -or $HipSdkInstalled) {", start)
return src[start:end]
@@ -444,10 +449,12 @@ def _installer_scan_block() -> str:
The report-only peer scan added for #8529 is a SEPARATE block, deliberately outside
this one: it feeds no label and no arch."""
src = INSTALL_PS1.read_text(encoding = "utf-8")
- start = src.index(
- " if (-not $HasROCm) {\n try {\n # ConfigManagerErrorCode"
- )
- end = src.index(" # Capture ROCm version for wheel selection", start)
+ # Anchored on CODE at both ends. The end anchor used to be a comment and a comment
+ # pass deleted it, which turned four tests into ValueError instead of a failure
+ # that said anything. The next statement after the block is the hipconfig probe.
+ body = src.index("$amdAdapters = @(Get-CimInstance Win32_VideoController")
+ start = src.rindex(" if (-not $HasROCm) {", 0, body)
+ end = src.index(" if ($HasROCm -or $HipSdkInstalled) {", start)
return src[start:end]