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.

Studio installers read the NVIDIA driver library when nvidia-smi cannot answer, and setup.ps1 keeps a working GPU prebuilt over a source build (#11007)

Daniel Han3a0dae2a1d9e
diff --git a/.github/workflows/cross-platform-parity-ci.yml b/.github/workflows/cross-platform-parity-ci.yml
index 01777020919..b7c086a59aa 100644
--- a/.github/workflows/cross-platform-parity-ci.yml
+++ b/.github/workflows/cross-platform-parity-ci.yml
@@ -96,6 +96,7 @@ on:
- 'tests/studio/test_torch_overrides_merge.ps1'
- 'tests/studio/test_amd_venv_repair_loop.ps1'
- 'tests/studio/test_amd_name_arch_r9700.ps1'
+ - 'tests/studio/test_nvidia_library_inventory.ps1'
- 'tests/studio/test_application_control_cli_fallback.ps1'
# test_application_control_cli_fallback.ps1 pins install.ps1's trampoline against
# process.rs's, so a change to either spelling has to run here.
@@ -174,6 +175,7 @@ on:
- 'tests/studio/test_torch_overrides_merge.ps1'
- 'tests/studio/test_amd_venv_repair_loop.ps1'
- 'tests/studio/test_amd_name_arch_r9700.ps1'
+ - 'tests/studio/test_nvidia_library_inventory.ps1'
- 'tests/studio/test_application_control_cli_fallback.ps1'
# test_application_control_cli_fallback.ps1 pins install.ps1's trampoline against
# process.rs's, so a change to either spelling has to run here.
@@ -296,6 +298,16 @@ jobs:
- name: AMD GPU-name arch table tests
shell: pwsh
run: pwsh -NoProfile -File tests/studio/test_amd_name_arch_r9700.ps1
+ # AST-extracted selectors over a stubbed nvidia-smi and inventory; the real-inventory
+ # checks skip on a runner without an NVIDIA driver library. The 5.1 leg compiles the
+ # P/Invoke helper with the .NET Framework compiler the CLI's setup.ps1 launch uses.
+ - name: NVIDIA driver-library inventory tests
+ shell: pwsh
+ run: pwsh -NoProfile -File tests/studio/test_nvidia_library_inventory.ps1
+ - name: NVIDIA driver-library inventory tests (Windows PowerShell 5.1)
+ if: runner.os == 'Windows'
+ shell: powershell
+ run: powershell -NoProfile -File tests\studio\test_nvidia_library_inventory.ps1
# AST-extracted helpers plus a real .cmd written to a temp dir, so no venv and no
# Windows needed. The 1260 classification is exercised against crafted exceptions:
# a hosted runner has no Application Control policy to be denied by. The enforced
diff --git a/install.ps1 b/install.ps1
index 11da8a26ec7..1972141560a 100644
--- a/install.ps1
+++ b/install.ps1
@@ -5404,6 +5404,145 @@ exit 0
return ($LASTEXITCODE -eq 0 -and $out -match '(?m)^GPU\s+\d+:')
}
+ # ── BEGIN SHARED WITH studio/setup.ps1 (Get-NvidiaLibraryInventory) ──
+ # nvml.dll sits in System32 with current drivers and under NVSMI with older ones; a bare
+ # name reaches only the former, so name the file, as studio/nvidia_probe.py does.
+ function Get-NvidiaNvmlLibraryPath {
+ $dirs = @()
+ if ($env:SystemRoot) { $dirs += (Join-Path $env:SystemRoot "System32") }
+ if ($env:ProgramFiles) { $dirs += (Join-Path $env:ProgramFiles "NVIDIA Corporation\NVSMI") }
+ foreach ($dir in $dirs) {
+ $candidate = Join-Path $dir "nvml.dll"
+ if (Test-Path -LiteralPath $candidate) { return $candidate }
+ }
+ return "nvml.dll"
+ }
+
+ # The driver's libraries as P/Invoke methods, emitted rather than compiled: the installer must
+ # not spawn csc.exe (New-StudioEmittedNativeType). $null when the type cannot be built. A
+ # missing library throws at the first call, not here.
+ function Get-NvidiaLibraryProbeType {
+ $name = "UnslothNvidiaProbeV2"
+ $existing = $name -as [type]
+ if ($existing) { return $existing }
+ # Dynamic Code Security can kill the process on an emitted load rather than throw: the
+ # same gate every other emitted type checks first, and no inventory when it says no.
+ if (-not (Test-StudioCanDefineNativeTypes)) { return $null }
+ $windows = ($env:OS -eq "Windows_NT")
+ $nvml = if ($windows) { Get-NvidiaNvmlLibraryPath } else { "libnvidia-ml.so.1" }
+ $cuda = if ($windows) { "nvcuda.dll" } else { "libcuda.so.1" }
+ $int = [int]; $uint = [uint32]; $refInt = [int].MakeByRefType()
+ $refUInt = [uint32].MakeByRefType(); $refPtr = [IntPtr].MakeByRefType()
+ try {
+ $null = New-StudioEmittedNativeType -TypeName $name -Imports @(
+ @{ Name = "nvmlInit_v2"; Library = $nvml; Return = $int; Args = @(); Ansi = $true },
+ @{ Name = "nvmlShutdown"; Library = $nvml; Return = $int; Args = @(); Ansi = $true },
+ @{ Name = "nvmlSystemGetCudaDriverVersion_v2"; Library = $nvml; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },
+ @{ Name = "nvmlDeviceGetCount_v2"; Library = $nvml; Return = $int; Args = @($refUInt); Out = @(1); Ansi = $true },
+ @{ Name = "nvmlDeviceGetHandleByIndex_v2"; Library = $nvml; Return = $int; Args = @($uint, $refPtr); Out = @(2); Ansi = $true },
+ @{ Name = "nvmlDeviceGetCudaComputeCapability"; Library = $nvml; Return = $int; Args = @([IntPtr], $refInt, $refInt); Out = @(2, 3); Ansi = $true },
+ @{ Name = "cuInit"; Library = $cuda; Return = $int; Args = @($uint); Ansi = $true },
+ @{ Name = "cuDriverGetVersion"; Library = $cuda; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },
+ @{ Name = "cuDeviceGetCount"; Library = $cuda; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },
+ @{ Name = "cuDeviceGet"; Library = $cuda; Return = $int; Args = @($refInt, $int); Out = @(1); Ansi = $true },
+ @{ Name = "cuDeviceGetAttribute"; Library = $cuda; Return = $int; Args = @($refInt, $int, $int); Out = @(1); Ansi = $true }
+ )
+ } catch { return $null }
+ return ($name -as [type])
+ }
+
+ # "source;cudaMajor;cudaMinor;cap,cap" from NVML, else the CUDA driver API; "" when neither
+ # answers. Versions are major*1000 + minor*10. Read in a runspace of its own under a deadline:
+ # a wedged driver can block inside the library, and the deadline leaves that runspace behind.
+ function Read-NvidiaLibraryRaw {
+ param([int]$TimeoutMs = 10000)
+ $type = Get-NvidiaLibraryProbeType
+ if (-not $type) { return "" }
+ $reader = {
+ param($T)
+ function Read-Nvml {
+ if ($T::nvmlInit_v2() -ne 0) { return "" }
+ try {
+ [uint32]$count = 0
+ if ($T::nvmlDeviceGetCount_v2([ref]$count) -ne 0 -or $count -eq 0) { return "" }
+ [int]$ver = 0
+ if ($T::nvmlSystemGetCudaDriverVersion_v2([ref]$ver) -ne 0 -or $ver -lt 1000) { return "" }
+ $caps = @()
+ for ([uint32]$i = 0; $i -lt $count; $i++) {
+ [IntPtr]$dev = [IntPtr]::Zero; [int]$major = 0; [int]$minor = 0
+ # One unreadable GPU voids the source: a partial list misleads the pre-Turing cap.
+ if ($T::nvmlDeviceGetHandleByIndex_v2($i, [ref]$dev) -ne 0) { return "" }
+ if ($T::nvmlDeviceGetCudaComputeCapability($dev, [ref]$major, [ref]$minor) -ne 0) { return "" }
+ $caps += "$major.$minor"
+ }
+ return "nvml;$([int][math]::Floor($ver / 1000));$([int][math]::Floor(($ver % 1000) / 10));$($caps -join ',')"
+ } finally { $null = $T::nvmlShutdown() }
+ }
+ function Read-Cuda {
+ # The driver API honours CUDA_VISIBLE_DEVICES; the inventory must be the physical one,
+ # so a hidden pre-Turing card still caps the family. cuInit reads the mask once.
+ $saved = $env:CUDA_VISIBLE_DEVICES
+ Remove-Item Env:CUDA_VISIBLE_DEVICES -ErrorAction SilentlyContinue
+ try { $init = $T::cuInit([uint32]0) } finally { if ($null -ne $saved) { $env:CUDA_VISIBLE_DEVICES = $saved } }
+ if ($init -ne 0) { return "" }
+ [int]$count = 0
+ if ($T::cuDeviceGetCount([ref]$count) -ne 0 -or $count -eq 0) { return "" }
+ [int]$ver = 0
+ if ($T::cuDriverGetVersion([ref]$ver) -ne 0 -or $ver -lt 1000) { return "" }
+ $caps = @()
+ for ($i = 0; $i -lt $count; $i++) {
+ [int]$dev = 0; [int]$major = 0; [int]$minor = 0
+ if ($T::cuDeviceGet([ref]$dev, $i) -ne 0) { return "" }
+ # CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR = 75, _MINOR = 76.
+ if ($T::cuDeviceGetAttribute([ref]$major, 75, $dev) -ne 0) { return "" }
+ if ($T::cuDeviceGetAttribute([ref]$minor, 76, $dev) -ne 0) { return "" }
+ $caps += "$major.$minor"
+ }
+ return "cuda;$([int][math]::Floor($ver / 1000));$([int][math]::Floor(($ver % 1000) / 10));$($caps -join ',')"
+ }
+ $r = ""
+ try { $r = Read-Nvml } catch { $r = "" }
+ if (-not $r) { try { $r = Read-Cuda } catch { $r = "" } }
+ return "$r"
+ }
+ $ps = $null; $handle = $null
+ try {
+ $ps = [powershell]::Create()
+ $null = $ps.AddScript($reader.ToString()).AddArgument($type)
+ $handle = $ps.BeginInvoke()
+ if (-not $handle.AsyncWaitHandle.WaitOne($TimeoutMs)) { return "" }
+ return "$(@($ps.EndInvoke($handle)) | Select-Object -Last 1)"
+ } catch { return "" }
+ finally { if ($ps -and $handle -and $handle.IsCompleted) { $ps.Dispose() } }
+ }
+
+ # NVIDIA inventory from the driver's own libraries (NVML, then the CUDA driver API), for a
+ # host whose nvidia-smi is absent, stale or hangs (#9255). Twin of studio/nvidia_probe.py.
+ # Cached. $null, or @{ Source; CudaMajor; CudaMinor; ComputeCaps ("8.9" strings); Count }.
+ function Get-NvidiaLibraryInventory {
+ param([int]$TimeoutSec = 10)
+ if ($script:NvidiaLibraryInventoryProbed) { return $script:NvidiaLibraryInventory }
+ $script:NvidiaLibraryInventoryProbed = $true
+ $script:NvidiaLibraryInventory = $null
+ if ("$($env:UNSLOTH_NVIDIA_LIBRARY_PROBE)".Trim() -eq "0") { return $null }
+ try { $raw = Read-NvidiaLibraryRaw -TimeoutMs ($TimeoutSec * 1000) } catch { return $null }
+ $parts = "$raw".Split(";")
+ if ($parts.Count -ne 4 -or -not $parts[3] -or [int]$parts[1] -lt 1) { return $null }
+ $caps = @($parts[3].Split(","))
+ if (@($caps | Where-Object { $_ -notmatch '^\d+\.\d+$' }).Count -gt 0) { return $null }
+ $script:NvidiaLibraryInventory = @{
+ Source = $parts[0]
+ CudaMajor = [int]$parts[1]
+ CudaMinor = [int]$parts[2]
+ ComputeCaps = $caps
+ Count = $caps.Count
+ }
+ return $script:NvidiaLibraryInventory
+ }
+ # ── END SHARED WITH studio/setup.ps1 (Get-NvidiaLibraryInventory) ──
+ # Under `irm | iex` the script scope is the caller's session: probe once per invocation.
+ $script:NvidiaLibraryInventoryProbed = $false
+ $script:NvidiaLibraryInventory = $null
# ── Detect GPU (robust: PATH + hardcoded fallback paths, mirrors setup.ps1) ──
$HasNvidiaSmi = $false
$NvidiaSmiExe = $null
@@ -5425,6 +5564,11 @@ exit 0
}
}
}
+ if (-not $HasNvidiaSmi -and (Get-NvidiaLibraryInventory)) {
+ # Same promotion as setup.ps1: the gates below read $HasNvidiaSmi as "NVIDIA GPU present".
+ $HasNvidiaSmi = $true
+ Write-StudioLine " NVIDIA GPU found through the driver library; nvidia-smi is unavailable" -ForegroundColor Gray
+ }
# ── AMD ROCm detection (Windows) — mirrors setup.ps1 ──
$HasROCm = $false
$HipSdkInstalled = $false # HIP SDK binary found (independent of device accessibility)
@@ -6054,10 +6198,13 @@ exit 0
# the wheel must support the host. Mirrors _nvidia_cu126_verdict in install.sh.
function Get-NvidiaCu126Verdict {
# Floor is per-release, not fixed: only 2.11 dropped sm_70 from cu128.
- param([string]$SmiExe, [int]$LegacyFloorSm = 75)
- if (-not $SmiExe) { return '' }
- $raw = Invoke-NvidiaSmiBounded $SmiExe @('--query-gpu=compute_cap', '--format=csv,noheader,nounits') -StdoutOnly
- if ($LASTEXITCODE -ne 0 -or -not $raw) { return '' }
+ param([string]$SmiExe, [int]$LegacyFloorSm = 75, [string[]]$ComputeCaps = @())
+ if ($ComputeCaps.Count -gt 0) {
+ $raw = $ComputeCaps -join "`n"
+ } elseif ($SmiExe) {
+ $raw = Invoke-NvidiaSmiBounded $SmiExe @('--query-gpu=compute_cap', '--format=csv,noheader,nounits') -StdoutOnly
+ if ($LASTEXITCODE -ne 0 -or -not $raw) { return '' }
+ } else { return '' }
$legacy = $false
$outsideCu126 = $false
$seen = $false
@@ -6076,11 +6223,11 @@ exit 0
}
function Get-CudaFamilyCappedForPreTuring {
- param([string]$Family, [string]$SmiExe)
+ param([string]$Family, [string]$SmiExe, [string[]]$ComputeCaps = @())
if ($Family -notin @('cu128', 'cu130')) { return $Family }
# 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) {
+ switch (Get-NvidiaCu126Verdict $SmiExe $legacyFloorSm $ComputeCaps) {
'cu126' {
substep "pre-Turing NVIDIA GPUs (sm_<75) are present -- selecting cu126, because PyTorch 2.11's $Family wheels start at sm_75" "Yellow"
return 'cu126'
@@ -6104,22 +6251,34 @@ exit 0
if (-not [string]::IsNullOrWhiteSpace($env:UNSLOTH_TORCH_INDEX_FAMILY)) {
return "$baseUrl/$($env:UNSLOTH_TORCH_INDEX_FAMILY.Trim().Trim('/'))"
}
- if (-not $NvidiaSmiExe) { return "$baseUrl/cpu" }
- try {
- $output = Invoke-NvidiaSmiBounded $NvidiaSmiExe
- if ($output -match 'CUDA(?: UMD)? Version:\s+(\d+)\.(\d+)') {
- $major = [int]$Matches[1]; $minor = [int]$Matches[2]
- if ($major -ge 13) { $family = "cu130" }
- elseif ($major -eq 12 -and $minor -ge 8) { $family = "cu128" }
- elseif ($major -eq 12 -and $minor -ge 6) { $family = "cu126" }
- elseif ($major -ge 12) { $family = "cu124" }
- elseif ($major -ge 11) { $family = "cu118" }
- else { return "$baseUrl/cpu" }
- return "$baseUrl/$(Get-CudaFamilyCappedForPreTuring $family $NvidiaSmiExe)"
+ $major = $null; $minor = $null; $caps = @()
+ if ($NvidiaSmiExe) {
+ try {
+ $output = Invoke-NvidiaSmiBounded $NvidiaSmiExe
+ if ($output -match 'CUDA(?: UMD)? Version:\s+(\d+)\.(\d+)') {
+ $major = [int]$Matches[1]; $minor = [int]$Matches[2]
+ }
+ } catch {}
+ }
+ if ($null -eq $major) {
+ # nvidia-smi absent, stale or hung: the driver library still names the version.
+ $inventory = Get-NvidiaLibraryInventory
+ if ($inventory) {
+ $major = $inventory.CudaMajor; $minor = $inventory.CudaMinor; $caps = $inventory.ComputeCaps
+ } elseif (-not $NvidiaSmiExe) {
+ return "$baseUrl/cpu"
+ } else {
+ substep "could not determine CUDA version from nvidia-smi, defaulting to cu126" "Yellow"
+ return "$baseUrl/cu126"
}
- } catch {}
- substep "could not determine CUDA version from nvidia-smi, defaulting to cu126" "Yellow"
- return "$baseUrl/cu126"
+ }
+ if ($major -ge 13) { $family = "cu130" }
+ elseif ($major -eq 12 -and $minor -ge 8) { $family = "cu128" }
+ elseif ($major -eq 12 -and $minor -ge 6) { $family = "cu126" }
+ elseif ($major -ge 12) { $family = "cu124" }
+ elseif ($major -ge 11) { $family = "cu118" }
+ else { return "$baseUrl/cpu" }
+ return "$baseUrl/$(Get-CudaFamilyCappedForPreTuring $family $NvidiaSmiExe $caps)"
}
function Remove-IndexUrlCredentials {
diff --git a/install.sh b/install.sh
index 6eb35ac5af3..ac53bbeb917 100755
--- a/install.sh
+++ b/install.sh
@@ -2428,8 +2428,101 @@ _cvd_hides_nvidia() {
[ -z "$_cvd_trim" ] || [ "$_cvd_trim" = "-1" ]
}
+# NVIDIA inventory from the driver's own libraries (NVML, then the CUDA driver API) for a host whose nvidia-smi is absent, stale or hangs (#9255): "<cuda major>.<minor> <cap>,<cap>" or exit 1. Inline rather than studio/nvidia_probe.py, which is not on disk yet when the torch index is chosen; the two read the same calls.
+# Memoised for the run: the presence check and the torch index must read the same answer,
+# and a wedged driver pays its deadline once.
+_NVIDIA_LIBRARY_INVENTORY_STATE=""
+_NVIDIA_LIBRARY_INVENTORY_VALUE=""
+_nvidia_library_inventory() {
+ [ "${UNSLOTH_NVIDIA_LIBRARY_PROBE:-1}" != "0" ] || return 1
+ case "${_NVIDIA_LIBRARY_INVENTORY_STATE:-}" in
+ found) printf '%s\n' "$_NVIDIA_LIBRARY_INVENTORY_VALUE"; return 0 ;;
+ none) return 1 ;;
+ esac
+ # The system python3, else the managed venv's once it exists. No interpreter yet is
+ # not an answer to remember: the venv arrives later in this run.
+ if command -v python3 >/dev/null 2>&1; then _nli_py=python3
+ elif [ -n "${VENV_DIR:-}" ] && [ -x "$VENV_DIR/bin/python" ]; then _nli_py="$VENV_DIR/bin/python"
+ else return 1
+ fi
+ _NVIDIA_LIBRARY_INVENTORY_STATE="none"
+ _NVIDIA_LIBRARY_INVENTORY_VALUE=$(_run_bounded "$_nli_py" -I - 2>/dev/null <<'PY'
+import ctypes, os, sys
+
+def load(*names):
+ for name in names:
+ try:
+ return ctypes.CDLL(name)
+ except OSError:
+ pass
+
+def sym(lib, name): # an older NVML exports the unversioned entry point only
+ return getattr(lib, name, None) or getattr(lib, name.replace("_v2", ""))
+
+def nvml():
+ lib = load("libnvidia-ml.so.1", "libnvidia-ml.so")
+ if lib is None or sym(lib, "nvmlInit_v2")() != 0:
+ return None
+ try:
+ count, version = ctypes.c_uint(), ctypes.c_int()
+ if sym(lib, "nvmlDeviceGetCount_v2")(ctypes.byref(count)) != 0 or not count.value:
+ return None
+ if sym(lib, "nvmlSystemGetCudaDriverVersion_v2")(ctypes.byref(version)) != 0 or version.value < 1000:
+ return None
+ caps = []
+ for i in range(count.value):
+ dev, major, minor = ctypes.c_void_p(), ctypes.c_int(), ctypes.c_int()
+ if sym(lib, "nvmlDeviceGetHandleByIndex_v2")(i, ctypes.byref(dev)) != 0 or \
+ lib.nvmlDeviceGetCudaComputeCapability(dev, ctypes.byref(major), ctypes.byref(minor)) != 0:
+ return None # one unreadable GPU voids the source
+ caps.append(f"{major.value}.{minor.value}")
+ return version.value, caps
+ finally:
+ lib.nvmlShutdown()
+
+def cuda():
+ # The driver API honours CUDA_VISIBLE_DEVICES; the inventory must be the physical one, so a
+ # hidden pre-Turing card still caps the family (studio/nvidia_probe.py does the same).
+ os.environ.pop("CUDA_VISIBLE_DEVICES", None)
+ lib = load("libcuda.so.1", "libcuda.so")
+ if lib is None or lib.cuInit(0) != 0:
+ return None
+ count, version = ctypes.c_int(), ctypes.c_int()
+ if lib.cuDeviceGetCount(ctypes.byref(count)) != 0 or not count.value:
+ return None
+ if lib.cuDriverGetVersion(ctypes.byref(version)) != 0 or version.value < 1000:
+ return None
+ caps = []
+ for i in range(count.value):
+ dev, major, minor = ctypes.c_int(), ctypes.c_int(), ctypes.c_int()
+ # CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR = 75, _MINOR = 76.
+ if lib.cuDeviceGet(ctypes.byref(dev), i) != 0 or \
+ lib.cuDeviceGetAttribute(ctypes.byref(major), 75, dev) != 0 or \
+ lib.cuDeviceGetAttribute(ctypes.byref(minor), 76, dev) != 0:
+ return None
+ caps.append(f"{major.value}.{minor.value}")
+ return version.value, caps
+
+found = None
+for reader in (nvml, cuda): # a reader that raises (a missing symbol) yields to the next
+ try:
+ found = reader()
+ except Exception:
+ found = None
+ if found and found[1]:
+ break
+if not found or not found[1]:
+ sys.exit(1)
+version, caps = found
+print(f"{version // 1000}.{version % 1000 // 10} {','.join(caps)}")
+PY
+) && [ -n "$_NVIDIA_LIBRARY_INVENTORY_VALUE" ] || return 1
+ _NVIDIA_LIBRARY_INVENTORY_STATE="found"
+ printf '%s\n' "$_NVIDIA_LIBRARY_INVENTORY_VALUE"
+}
+
# ── NVIDIA usable-GPU helper ──
-# nvidia-smi -L primary, /proc/driver/nvidia/gpus/ fallback; a hidden GPU is NOT usable.
+# nvidia-smi -L primary, /proc/driver/nvidia/gpus/ fallback, driver library last; a hidden GPU is NOT usable.
_has_usable_nvidia_gpu() {
if _cvd_hides_nvidia; then
return 1
@@ -2449,6 +2542,7 @@ _has_usable_nvidia_gpu() {
[ -n "$(ls -A /proc/driver/nvidia/gpus 2>/dev/null)" ]; then
return 0
fi
+ _nvidia_library_inventory >/dev/null 2>&1 && return 0
return 1
}
@@ -3759,8 +3853,13 @@ _probe_amd_gfx_arch() {
# Classify the physical NVIDIA inventory for a cu126 fallback: "cu126" when it covers every GPU, "uncovered" for an incompatible mix, empty when no fallback is needed or the inventory is unreadable. CUDA_VISIBLE_DEVICES is ignored because the wheel must support the host. Shared decision with install.ps1 / setup.ps1 / install_python_stack.py.
_nvidia_cu126_verdict() {
- [ -n "$1" ] || return 0
- _ncv_caps=$(_run_bounded "$1" --query-gpu=compute_cap --format=csv,noheader,nounits 2>/dev/null) || return 0
+ # $2: capabilities already read from the driver library, one per line, when nvidia-smi cannot.
+ if [ -n "${2:-}" ]; then
+ _ncv_caps=$2
+ else
+ [ -n "$1" ] || return 0
+ _ncv_caps=$(_run_bounded "$1" --query-gpu=compute_cap --format=csv,noheader,nounits 2>/dev/null) || return 0
+ fi
printf '%s\n' "$_ncv_caps" | awk '
{ gsub(/^[[:space:]]+|[[:space:]]+$/, "") } # match the .Trim()/.strip() siblings
/^[0-9]+\.[0-9]+$/ {
@@ -3789,7 +3888,7 @@ _cap_cuda_family_for_pre_turing() {
cu128|cu130) ;;
*) printf '%s\n' "$1"; return ;;
esac
- case "$(_nvidia_cu126_verdict "$2")" in
+ case "$(_nvidia_cu126_verdict "$2" "${3:-}")" in
cu126)
echo "[WARN] Pre-Turing NVIDIA GPUs (sm_<75) are present -- selecting cu126, because PyTorch 2.11's $1 wheels start at sm_75." >&2
printf '%s\n' "cu126"
@@ -4081,9 +4180,16 @@ get_torch_index_url() {
-e 's/.*CUDA UMD Version:[[:space:]]*\([0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' \
-e 's/.*CUDA Version:[[:space:]]*\([0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' \
| head -1)
+ _inventory_caps=""
if [ -z "$_cuda_ver" ]; then
- echo "[WARN] Could not determine CUDA version from nvidia-smi, defaulting to cu126" >&2
- echo "$_base/cu126"; return
+ # nvidia-smi absent, stale or hung: the driver library knows both; cu126 is the last resort.
+ if _inventory=$(_nvidia_library_inventory) && [ -n "$_inventory" ]; then
+ _cuda_ver=${_inventory%% *}
+ _inventory_caps=$(printf '%s' "${_inventory#* }" | tr ',' '\n')
+ else
+ echo "[WARN] Could not determine CUDA version from nvidia-smi, defaulting to cu126" >&2
+ echo "$_base/cu126"; return
+ fi
fi
_major=${_cuda_ver%%.*}
_minor=${_cuda_ver#*.}
@@ -4093,7 +4199,7 @@ get_torch_index_url() {
elif [ "$_major" -ge 12 ]; then _cuda_tag=cu124
elif [ "$_major" -ge 11 ]; then _cuda_tag=cu118
else echo "$_base/cpu"; return; fi
- echo "$_base/$(_cap_cuda_family_for_pre_turing "$_cuda_tag" "$_smi")"
+ echo "$_base/$(_cap_cuda_family_for_pre_turing "$_cuda_tag" "$_smi" "$_inventory_caps")"
}
# ── Torch flavor helpers (to repair a stale CPU / wrong-CUDA wheel) ──
@@ -4593,6 +4699,12 @@ if [ -n "$_te_trim" ]; then
fi
fi
+# The NVIDIA presence check runs here first: get_torch_index_url runs in a command substitution,
+# whose library inventory memo would not outlive it, and the later checks would probe again.
+# Not for a pinned index or no torch at all: the selection does not read the GPU then.
+if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ]; then
+ _has_usable_nvidia_gpu >/dev/null 2>&1 || true
+fi
TORCH_INDEX_URL=$(get_torch_index_url)
# Linux: ROCm runtime missing but a supported AMD gfx arch is inferable (Strix Halo in /proc/cpuinfo, lspci marketing name, UNSLOTH_ROCM_GFX_ARCH). Route to AMD's per-arch wheels like install.ps1 does on Windows (unslothai#7301). Gated on the runtime probes NOT naming a gfx: either no AMD GPU is detected at all, or the GPU is visible only through the env-independent KFD topology while rocminfo/amd-smi cannot read its arch (#7314; before the KFD detection fix these hosts reached this reroute via the false branch, so the empty-probe condition preserves that routing). A */cpu index chosen WITH a readable gfx and a readable but UNSUPPORTED ROCm version is a deliberate fallback and stays excluded, since the shared probe returns its gfx; an UNREADABLE version is only a detection miss, so it gets its own way in below (#8731). UNSLOTH_ROCM_GFX_ARCH stays authoritative either way.
diff --git a/studio/setup.ps1 b/studio/setup.ps1
index 39bd1e306e9..bf954f5fc45 100644
--- a/studio/setup.ps1
+++ b/studio/setup.ps1
@@ -159,6 +159,10 @@ if ($script:UnslothVerbose) {
$env:UNSLOTH_VERBOSE = '1'
}
$script:LlamaCppDegraded = $false
+$script:LlamaKeptGpuPrebuilt = $null
+$script:NvidiaSmiRejected = $false
+$script:NvidiaLibraryInventoryProbed = $false
+$script:NvidiaLibraryInventory = $null
# Set by the offline keep, read unconditionally by the sidecar and legacy-migration blocks:
# initialised for Set-StrictMode and for a dot-sourced rerun in the same session.
$script:OfflineFastPath = $false
@@ -565,6 +569,67 @@ function Exit-PathAccessDenied {
-UserSupplied:$UserSupplied -OwnershipUnverified:$OwnershipUnverified)
}
+# Why the prebuilt update failed, for the keep message. Twin of setup.sh _llama_update_fail_reason.
+function Get-LlamaUpdateFailReason {
+ param([string]$Output)
+ if ($Output -match '(?i)429|rate limit') { return "GitHub rate limit" }
+ if ($Output -match '(?i)timed out|timeout|connection|resol|network|unreachable|50[234]') { return "network error" }
+ return "download failed"
+}
+
+# The marker's backend as setup.sh _installed_prebuilt_backend reads it: backend, then the
+# llama_backend request, then the asset name of a marker from before the field existed.
+function Get-PrebuiltMarkerBackend {
+ param([string]$Marker)
+ if (-not (Test-PathQuiet $Marker "Leaf")) { return "" }
+ try { $payload = Get-Content -LiteralPath $Marker -Raw | ConvertFrom-Json } catch { return "" }
+ if ($null -eq $payload -or $payload -isnot [System.Management.Automation.PSCustomObject]) { return "" }
+ $field = {
+ param($name)
+ if ($payload.PSObject.Properties.Name -ccontains $name -and $payload.$name -is [string]) {
+ $value = $payload.$name.Trim().ToLowerInvariant()
+ if ($value -eq "hip") { "rocm" } else { $value }
+ } else { "" }
+ }
+ $answer = & $field "backend"
+ if (-not $answer -and (& $field "llama_backend") -in @("cuda", "rocm", "vulkan", "cpu")) { $answer = & $field "llama_backend" }
+ if (-not $answer) {
+ $asset = & $field "asset"
+ foreach ($value in @("cuda", "rocm", "vulkan", "cpu")) {
+ if ($asset -like "*-$value*" -or ($value -eq "rocm" -and $asset -like "*-hip*")) { $answer = $value; break }
+ }
+ }
+ return $answer
+}
+
+# The backend of an installed GPU prebuilt that still runs on a host that still has that GPU,
+# else "" (nothing to keep, GPU gone, or this run asked for something else). Twin of setup.sh.
+function Get-GpuPrebuiltToKeepOverSourceBuild {
+ param([string]$InstallDir)
+ if ($env:UNSLOTH_LLAMA_FORCE_COMPILE -eq "1" -or $LlamaPr -or $explicitLlamaSourceBackend) { return "" }
+ if ($env:UNSLOTH_LLAMA_RELEASE_TAG) { return "" }
+ # "master" is a branch, never a release: it asks for a source build, so nothing is kept.
+ if ("$($env:UNSLOTH_LLAMA_TAG)".Trim() -notin @("", "latest")) { return "" }
+ $backend = Get-PrebuiltMarkerBackend -Marker (Join-Path $InstallDir "UNSLOTH_PREBUILT_INFO.json")
+ if (-not $backend) { return "" }
+ $nvidia = $HasNvidiaSmi
+ $amd = $HasROCm -or [bool]$script:ROCmGfxArch
+ $present = switch ($backend) {
+ "cuda" { $nvidia }
+ "rocm" { $amd }
+ # Any Intel adapter runs the Vulkan bundle (windows_intel_gpu_in_registry), not only an XPU part.
+ "vulkan" { $nvidia -or $amd -or [bool]$script:IsIntelXpu -or (@(Get-IntelRegistryAdapterNames).Count -gt 0) }
+ default { $false }
+ }
+ if (-not $present) { return "" }
+ $prevEAP = $ErrorActionPreference
+ $ErrorActionPreference = "Continue"
+ try { $null = & python "$PSScriptRoot\install_llama_prebuilt.py" --check-installed $InstallDir 2>&1 } catch { return "" }
+ finally { $ErrorActionPreference = $prevEAP }
+ if ($LASTEXITCODE -ne 0) { return "" }
+ return $backend
+}
+
function Get-InstalledLlamaPrebuiltRelease {
param([string]$InstallDir)
@@ -686,25 +751,26 @@ function Write-CudaDriverToolkitMismatch {
}
function Get-CudaComputeCapability {
- # $NvidiaSmiExe is an absolute path that survives Refresh-Environment.
- $smiExe = if ($script:NvidiaSmiExe) { $script:NvidiaSmiExe } else {
+ # $NvidiaSmiExe is an absolute path that survives Refresh-Environment. Not rediscovered
+ # once detection rejected nvidia-smi: the driver library answered, and asking a wedged
+ # binary again costs a deadline per call.
+ $smiExe = if ($script:NvidiaSmiExe) { $script:NvidiaSmiExe } elseif ($script:NvidiaSmiRejected) { $null } else {
$cmd = Get-Command nvidia-smi -ErrorAction SilentlyContinue
if ($cmd) { $cmd.Source } else { $null }
}
- if (-not $smiExe) { return $null }
-
- try {
- $raw = Invoke-NvidiaSmiBounded $smiExe @('--query-gpu=compute_cap', '--format=csv,noheader')
- if ($LASTEXITCODE -ne 0 -or -not $raw) { return $null }
-
- $cap = ($raw -split "`n")[0].Trim()
- if ($cap -match '^(\d+)\.(\d+)$') {
- $major = $Matches[1]
- $minor = $Matches[2]
- return "$major$minor"
- }
- } catch { }
-
+ $cap = $null
+ if ($smiExe) {
+ try {
+ $raw = Invoke-NvidiaSmiBounded $smiExe @('--query-gpu=compute_cap', '--format=csv,noheader')
+ if ($LASTEXITCODE -eq 0 -and $raw) { $cap = ($raw -split "`n")[0].Trim() }
+ } catch { }
+ }
+ if (-not $cap) {
+ # Without it the source build turns CUDA off (#5854); the driver library has it.
+ $inventory = Get-NvidiaLibraryInventory
+ if ($inventory) { $cap = $inventory.ComputeCaps[0] }
+ }
+ if ($cap -match '^(\d+)\.(\d+)$') { return "$($Matches[1])$($Matches[2])" }
return $null
}
@@ -800,10 +866,13 @@ function Get-LlamaBuildJobs {
# the host. Mirrors _nvidia_cu126_verdict in install.sh.
function Get-NvidiaCu126Verdict {
# Floor is per-release, not fixed: only 2.11 dropped sm_70 from cu128.
- param([string]$SmiExe, [int]$LegacyFloorSm = 75)
- if (-not $SmiExe) { return '' }
- $raw = Invoke-NvidiaSmiBounded $SmiExe @('--query-gpu=compute_cap', '--format=csv,noheader,nounits') -StdoutOnly
- if ($LASTEXITCODE -ne 0 -or -not $raw) { return '' }
+ param([string]$SmiExe, [int]$LegacyFloorSm = 75, [string[]]$ComputeCaps = @())
+ if ($ComputeCaps.Count -gt 0) {
+ $raw = $ComputeCaps -join "`n"
+ } elseif ($SmiExe) {
+ $raw = Invoke-NvidiaSmiBounded $SmiExe @('--query-gpu=compute_cap', '--format=csv,noheader,nounits') -StdoutOnly
+ if ($LASTEXITCODE -ne 0 -or -not $raw) { return '' }
+ } else { return '' }
$legacy = $false
$outsideCu126 = $false
$seen = $false
@@ -822,11 +891,11 @@ function Get-NvidiaCu126Verdict {
}
function Get-CudaFamilyCappedForPreTuring {
- param([string]$Family, [string]$SmiExe)
+ param([string]$Family, [string]$SmiExe, [string[]]$ComputeCaps = @())
if ($Family -notin @('cu128', 'cu130')) { return $Family }
# 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
+ $verdict = Get-NvidiaCu126Verdict $SmiExe $legacyFloorSm $ComputeCaps
if (-not $verdict) { return $Family }
# This runs twice per setup; announce once without polluting pipeline output.
$announce = -not $script:PreTuringCapAnnounced
@@ -844,35 +913,183 @@ function Get-CudaFamilyCappedForPreTuring {
return $Family
}
+# ── BEGIN SHARED WITH install.ps1 (Get-NvidiaLibraryInventory) ──
+# nvml.dll sits in System32 with current drivers and under NVSMI with older ones; a bare
+# name reaches only the former, so name the file, as studio/nvidia_probe.py does.
+function Get-NvidiaNvmlLibraryPath {
+ $dirs = @()
+ if ($env:SystemRoot) { $dirs += (Join-Path $env:SystemRoot "System32") }
+ if ($env:ProgramFiles) { $dirs += (Join-Path $env:ProgramFiles "NVIDIA Corporation\NVSMI") }
+ foreach ($dir in $dirs) {
+ $candidate = Join-Path $dir "nvml.dll"
+ if (Test-Path -LiteralPath $candidate) { return $candidate }
+ }
+ return "nvml.dll"
+}
+
+# The driver's libraries as P/Invoke methods, emitted rather than compiled: the installer must
+# not spawn csc.exe (New-StudioEmittedNativeType). $null when the type cannot be built. A
+# missing library throws at the first call, not here.
+function Get-NvidiaLibraryProbeType {
+ $name = "UnslothNvidiaProbeV2"
+ $existing = $name -as [type]
+ if ($existing) { return $existing }
+ # Dynamic Code Security can kill the process on an emitted load rather than throw: the
+ # same gate every other emitted type checks first, and no inventory when it says no.
+ if (-not (Test-StudioCanDefineNativeTypes)) { return $null }
+ $windows = ($env:OS -eq "Windows_NT")
+ $nvml = if ($windows) { Get-NvidiaNvmlLibraryPath } else { "libnvidia-ml.so.1" }
+ $cuda = if ($windows) { "nvcuda.dll" } else { "libcuda.so.1" }
+ $int = [int]; $uint = [uint32]; $refInt = [int].MakeByRefType()
+ $refUInt = [uint32].MakeByRefType(); $refPtr = [IntPtr].MakeByRefType()
+ try {
+ $null = New-StudioEmittedNativeType -TypeName $name -Imports @(
+ @{ Name = "nvmlInit_v2"; Library = $nvml; Return = $int; Args = @(); Ansi = $true },
+ @{ Name = "nvmlShutdown"; Library = $nvml; Return = $int; Args = @(); Ansi = $true },
+ @{ Name = "nvmlSystemGetCudaDriverVersion_v2"; Library = $nvml; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },
+ @{ Name = "nvmlDeviceGetCount_v2"; Library = $nvml; Return = $int; Args = @($refUInt); Out = @(1); Ansi = $true },
+ @{ Name = "nvmlDeviceGetHandleByIndex_v2"; Library = $nvml; Return = $int; Args = @($uint, $refPtr); Out = @(2); Ansi = $true },
+ @{ Name = "nvmlDeviceGetCudaComputeCapability"; Library = $nvml; Return = $int; Args = @([IntPtr], $refInt, $refInt); Out = @(2, 3); Ansi = $true },
+ @{ Name = "cuInit"; Library = $cuda; Return = $int; Args = @($uint); Ansi = $true },
+ @{ Name = "cuDriverGetVersion"; Library = $cuda; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },
+ @{ Name = "cuDeviceGetCount"; Library = $cuda; Return = $int; Args = @($refInt); Out = @(1); Ansi = $true },
+ @{ Name = "cuDeviceGet"; Library = $cuda; Return = $int; Args = @($refInt, $int); Out = @(1); Ansi = $true },
+ @{ Name = "cuDeviceGetAttribute"; Library = $cuda; Return = $int; Args = @($refInt, $int, $int); Out = @(1); Ansi = $true }
+ )
+ } catch { return $null }
+ return ($name -as [type])
+}
+
+# "source;cudaMajor;cudaMinor;cap,cap" from NVML, else the CUDA driver API; "" when neither
+# answers. Versions are major*1000 + minor*10. Read in a runspace of its own under a deadline:
+# a wedged driver can block inside the library, and the deadline leaves that runspace behind.
+function Read-NvidiaLibraryRaw {
+ param([int]$TimeoutMs = 10000)
+ $type = Get-NvidiaLibraryProbeType
+ if (-not $type) { return "" }
+ $reader = {
+ param($T)
+ function Read-Nvml {
+ if ($T::nvmlInit_v2() -ne 0) { return "" }
+ try {
+ [uint32]$count = 0
+ if ($T::nvmlDeviceGetCount_v2([ref]$count) -ne 0 -or $count -eq 0) { return "" }
+ [int]$ver = 0
+ if ($T::nvmlSystemGetCudaDriverVersion_v2([ref]$ver) -ne 0 -or $ver -lt 1000) { return "" }
+ $caps = @()
+ for ([uint32]$i = 0; $i -lt $count; $i++) {
+ [IntPtr]$dev = [IntPtr]::Zero; [int]$major = 0; [int]$minor = 0
+ # One unreadable GPU voids the source: a partial list misleads the pre-Turing cap.
+ if ($T::nvmlDeviceGetHandleByIndex_v2($i, [ref]$dev) -ne 0) { return "" }
+ if ($T::nvmlDeviceGetCudaComputeCapability($dev, [ref]$major, [ref]$minor) -ne 0) { return "" }
+ $caps += "$major.$minor"
+ }
+ return "nvml;$([int][math]::Floor($ver / 1000));$([int][math]::Floor(($ver % 1000) / 10));$($caps -join ',')"
+ } finally { $null = $T::nvmlShutdown() }
+ }
+ function Read-Cuda {
+ # The driver API honours CUDA_VISIBLE_DEVICES; the inventory must be the physical one,
+ # so a hidden pre-Turing card still caps the family. cuInit reads the mask once.
+ $saved = $env:CUDA_VISIBLE_DEVICES
+ Remove-Item Env:CUDA_VISIBLE_DEVICES -ErrorAction SilentlyContinue
+ try { $init = $T::cuInit([uint32]0) } finally { if ($null -ne $saved) { $env:CUDA_VISIBLE_DEVICES = $saved } }
+ if ($init -ne 0) { return "" }
+ [int]$count = 0
+ if ($T::cuDeviceGetCount([ref]$count) -ne 0 -or $count -eq 0) { return "" }
+ [int]$ver = 0
+ if ($T::cuDriverGetVersion([ref]$ver) -ne 0 -or $ver -lt 1000) { return "" }
+ $caps = @()
+ for ($i = 0; $i -lt $count; $i++) {
+ [int]$dev = 0; [int]$major = 0; [int]$minor = 0
+ if ($T::cuDeviceGet([ref]$dev, $i) -ne 0) { return "" }
+ # CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR = 75, _MINOR = 76.
+ if ($T::cuDeviceGetAttribute([ref]$major, 75, $dev) -ne 0) { return "" }
+ if ($T::cuDeviceGetAttribute([ref]$minor, 76, $dev) -ne 0) { return "" }
+ $caps += "$major.$minor"
+ }
+ return "cuda;$([int][math]::Floor($ver / 1000));$([int][math]::Floor(($ver % 1000) / 10));$($caps -join ',')"
+ }
+ $r = ""
+ try { $r = Read-Nvml } catch { $r = "" }
+ if (-not $r) { try { $r = Read-Cuda } catch { $r = "" } }
+ return "$r"
+ }
+ $ps = $null; $handle = $null
+ try {
+ $ps = [powershell]::Create()
+ $null = $ps.AddScript($reader.ToString()).AddArgument($type)
+ $handle = $ps.BeginInvoke()
+ if (-not $handle.AsyncWaitHandle.WaitOne($TimeoutMs)) { return "" }
+ return "$(@($ps.EndInvoke($handle)) | Select-Object -Last 1)"
+ } catch { return "" }
+ finally { if ($ps -and $handle -and $handle.IsCompleted) { $ps.Dispose() } }
+}
+
+# NVIDIA inventory from the driver's own libraries (NVML, then the CUDA driver API), for a
+# host whose nvidia-smi is absent, stale or hangs (#9255). Twin of studio/nvidia_probe.py.
+# Cached. $null, or @{ Source; CudaMajor; CudaMinor; ComputeCaps ("8.9" strings); Count }.
+function Get-NvidiaLibraryInventory {
+ param([int]$TimeoutSec = 10)
+ if ($script:NvidiaLibraryInventoryProbed) { return $script:NvidiaLibraryInventory }
+ $script:NvidiaLibraryInventoryProbed = $true
+ $script:NvidiaLibraryInventory = $null
+ if ("$($env:UNSLOTH_NVIDIA_LIBRARY_PROBE)".Trim() -eq "0") { return $null }
+ try { $raw = Read-NvidiaLibraryRaw -TimeoutMs ($TimeoutSec * 1000) } catch { return $null }
+ $parts = "$raw".Split(";")
+ if ($parts.Count -ne 4 -or -not $parts[3] -or [int]$parts[1] -lt 1) { return $null }
+ $caps = @($parts[3].Split(","))
+ if (@($caps | Where-Object { $_ -notmatch '^\d+\.\d+$' }).Count -gt 0) { return $null }
+ $script:NvidiaLibraryInventory = @{
+ Source = $parts[0]
+ CudaMajor = [int]$parts[1]
+ CudaMinor = [int]$parts[2]
+ ComputeCaps = $caps
+ Count = $caps.Count
+ }
+ return $script:NvidiaLibraryInventory
+}
+# ── END SHARED WITH install.ps1 (Get-NvidiaLibraryInventory) ──
+
# Detect driver's max CUDA version from nvidia-smi and return the highest
# compatible PyTorch CUDA index tag (e.g. "cu128").
# PyTorch on Windows ships CPU-only by default from PyPI; CUDA wheels live at
# https://download.pytorch.org/whl/<tag>. The tag must not exceed the driver's
# capability: e.g. driver "CUDA Version: 12.9" → cu128 (not cu130).
function Get-PytorchCudaTag {
- $smiExe = if ($script:NvidiaSmiExe) { $script:NvidiaSmiExe } else {
+ # Not rediscovered once detection rejected nvidia-smi (see Get-CudaComputeCapability).
+ $smiExe = if ($script:NvidiaSmiExe) { $script:NvidiaSmiExe } elseif ($script:NvidiaSmiRejected) { $null } else {
$cmd = Get-Command nvidia-smi -ErrorAction SilentlyContinue
if ($cmd) { $cmd.Source } else { $null }
}
- if (-not $smiExe) { return "cu126" }
-
- try {
- $output = Invoke-NvidiaSmiBounded $smiExe
- if ($output -match 'CUDA(?: UMD)? Version:\s+(\d+)\.(\d+)') {
- $major = [int]$Matches[1]
- $minor = [int]$Matches[2]
- # PyTorch 2.10 offers: cu124, cu126, cu128, cu130
- if ($major -ge 13) { $family = "cu130" }
- elseif ($major -eq 12 -and $minor -ge 8) { $family = "cu128" }
- elseif ($major -eq 12 -and $minor -ge 6) { $family = "cu126" }
- elseif ($major -ge 12) { $family = "cu124" }
- elseif ($major -ge 11) { $family = "cu118" }
- else { return "cpu" }
- return (Get-CudaFamilyCappedForPreTuring $family $smiExe)
- }
- } catch { }
-
- return "cu126"
+ $major = $null
+ $minor = $null
+ $caps = @()
+ if ($smiExe) {
+ try {
+ $output = Invoke-NvidiaSmiBounded $smiExe
+ if ($output -match 'CUDA(?: UMD)? Version:\s+(\d+)\.(\d+)') {
+ $major = [int]$Matches[1]
+ $minor = [int]$Matches[2]
+ }
+ } catch { }
+ }
+ if ($null -eq $major) {
+ # nvidia-smi absent, stale or hung: the driver library knows the version. With neither,
+ # "" means unknown; guessing cu126 replaced a working cu130 venv every update (#9255).
+ $inventory = Get-NvidiaLibraryInventory
+ if (-not $inventory) { return "" }
+ $major = $inventory.CudaMajor
+ $minor = $inventory.CudaMinor
+ $caps = $inventory.ComputeCaps
+ }
+ # PyTorch 2.10 offers: cu124, cu126, cu128, cu130
+ if ($major -ge 13) { $family = "cu130" }
+ elseif ($major -eq 12 -and $minor -ge 8) { $family = "cu128" }
+ elseif ($major -eq 12 -and $minor -ge 6) { $family = "cu126" }
+ elseif ($major -ge 12) { $family = "cu124" }
+ elseif ($major -ge 11) { $family = "cu118" }
+ else { return "cpu" }
+ return (Get-CudaFamilyCappedForPreTuring $family $smiExe $caps)
}
function Trim-IndexPathSlashes {
@@ -2369,6 +2586,13 @@ if (-not $HasNvidiaSmi) {
}
}
}
+if (-not $HasNvidiaSmi -and (Get-NvidiaLibraryInventory)) {
+ # The driver lists a GPU nvidia-smi cannot: the gates below read this as "GPU present",
+ # and the consumers stop asking nvidia-smi, absent or rejected, for the rest of the run.
+ $HasNvidiaSmi = $true
+ $script:NvidiaSmiRejected = $true
+ Write-StudioLine " NVIDIA GPU found through the driver library; nvidia-smi is unavailable" -ForegroundColor Gray
+}
# amd-smi auto-elevates to read GPU memory, popping a DiskPart UAC prompt; RunAsInvoker stops it.
function Invoke-AmdSmiNoElevate {
param(
@@ -3192,7 +3416,9 @@ function Resolve-CudaToolkit {
$DriverMaxCuda = $null
try {
# test_resolve_cuda_toolkit.ps1 extracts this function alone, without Invoke-NvidiaSmiBounded.
- $smiOut = if (Get-Command Invoke-NvidiaSmiBounded -ErrorAction SilentlyContinue) {
+ $smiOut = if (-not $NvidiaSmiExe) {
+ ""
+ } elseif (Get-Command Invoke-NvidiaSmiBounded -ErrorAction SilentlyContinue) {
Invoke-NvidiaSmiBounded $NvidiaSmiExe
} else {
& $NvidiaSmiExe 2>&1 | Out-String
@@ -3202,6 +3428,14 @@ try {
substep "driver supports up to CUDA $DriverMaxCuda"
}
} catch {}
+if (-not $DriverMaxCuda -and (Get-Command Get-NvidiaLibraryInventory -ErrorAction SilentlyContinue)) {
+ # No nvidia-smi answer: the driver library names the same ceiling, so the toolkit filter holds.
+ $inventory = Get-NvidiaLibraryInventory
+ if ($inventory) {
+ $DriverMaxCuda = "$($inventory.CudaMajor).$($inventory.CudaMinor)"
+ substep "driver supports up to CUDA $DriverMaxCuda (driver library)"
+ }
+}
# Detect compute capability early so we can validate toolkit support
$CudaArch = Get-CudaComputeCapability
@@ -4312,6 +4546,11 @@ if ((Test-Path -LiteralPath $VenvDir -PathType Container) -and -not $NoTorchMode
}
} elseif ($HasNvidiaSmi) {
$expectedTorchTag = Get-PytorchCudaTag
+ if (-not $expectedTorchTag) {
+ # No driver version from anywhere: not evidence the venv is stale.
+ $_expectedKnown = $false
+ $expectedTorchTag = $installedTorchTag
+ }
} elseif ($script:IsIntelXpu) {
# Arc / Data Center host: the install below selects the xpu index. BEFORE the AMD
# arm -- both can be true on an unmapped-arch AMD box, where xpu is what gets
@@ -5296,6 +5535,11 @@ if ($PinnedTorchIndexUrl) {
$CuTag = if (Test-CudaFamilyLeaf $script:PreservedInstallerTorchTag) { $script:PreservedInstallerTorchTag } else { "cpu" }
} elseif ($HasNvidiaSmi) {
$CuTag = Get-PytorchCudaTag
+ if (-not $CuTag) {
+ # Unknown driver version: the installed family, else the widest wheel.
+ $CuTag = if (Test-CudaFamilyLeaf $installedTorchTag) { $installedTorchTag } else { "cu126" }
+ substep "could not read the CUDA driver version; installing torch $CuTag" "Yellow"
+ }
} elseif ($script:IsIntelXpu) {
# XPU (SYCL) wheels ship under the /xpu leaf, so $TorchInstallIndexUrl below resolves to
# <mirror>/xpu. A pin above still wins; the AMD reroute below needs $CuTag -eq "cpu".
@@ -6547,6 +6791,14 @@ if ($LocalLlamaCppLinked) {
substep "Check the error above, choose another backend, or retry" "Yellow"
Exit-SetupFailure "The selected llama.cpp backend could not be installed, so the installer will not substitute a different source backend."
} elseif ($prebuiltExit -eq 2) {
+ $keptBackend = Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $LlamaCppDir
+ if ($keptBackend) {
+ # The source fallback needs MSVC and nvcc and builds for the CPU without a
+ # compute capability, so it must not replace a working GPU prebuilt (#9255).
+ $script:LlamaKeptGpuPrebuilt = $keptBackend
+ step "llama.cpp" "update failed ($(Get-LlamaUpdateFailReason $prebuiltOutput)); keeping the installed $keptBackend prebuilt, the next update will retry" "Yellow"
+ Write-LlamaFailureLog -Output $prebuiltOutput
+ } else {
step "llama.cpp" "prebuilt install failed" "Yellow"
Write-LlamaFailureLog -Output $prebuiltOutput
if (Test-Path -LiteralPath $LlamaCppDir) {
@@ -6557,6 +6809,7 @@ if ($LocalLlamaCppLinked) {
# which this script cannot see -- exits 5 above instead.
substep "Prebuilt llama.cpp path unavailable or failed validation -- falling back to source build" "Yellow"
$NeedLlamaSourceBuild = $true
+ }
} else {
step "llama.cpp" "prebuilt helper failed unexpectedly" "Red"
Write-LlamaFailureLog -Output $prebuiltOutput
@@ -7347,6 +7600,9 @@ if ($script:StudioVtOk -and -not $env:NO_COLOR) {
}
Write-StudioLine " $Rule" -ForegroundColor DarkGray
}
+if ($script:LlamaKeptGpuPrebuilt) {
+ step "llama.cpp" "update failed; the installed $($script:LlamaKeptGpuPrebuilt) prebuilt was kept and the next update will retry" "Yellow"
+}
step "launch" "unsloth studio -p 8888"
substep "(add -H 0.0.0.0 for LAN / cloud access; exposes the raw port only, not a public URL)"
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)"
diff --git a/tests/sh/test_get_torch_index_url.sh b/tests/sh/test_get_torch_index_url.sh
index 151ef0b1220..a011cdf2083 100755
--- a/tests/sh/test_get_torch_index_url.sh
+++ b/tests/sh/test_get_torch_index_url.sh
@@ -53,6 +53,8 @@ _FAKE_ROCM_DIR=$(mktemp -d)
echo ""
sed -n '/^_trim_index_path_slashes()/,/^}/p' "$INSTALL_SH"
echo ""
+ sed -n '/^_nvidia_library_inventory()/,/^}/p' "$INSTALL_SH"
+ echo ""
sed -n '/^_nvidia_cu126_verdict()/,/^}/p' "$INSTALL_SH"
echo ""
sed -n '/^_cap_cuda_family_for_pre_turing()/,/^}/p' "$INSTALL_SH"
@@ -253,6 +255,72 @@ _result=$(run_func "$_dir")
assert_eq "unparseable -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
rm -rf "$_dir"
+# Helper: a python3 stand-in for the driver-library probe, printing "<cuda> <caps>".
+make_mock_probe() {
+ printf '#!/bin/sh\ncat >/dev/null\necho "%s"\n' "$2" > "$1/python3"
+ chmod +x "$1/python3"
+}
+
+# 8b) Unparseable banner, but the driver library names the version -> its family
+_dir=$(mktemp -d)
+cat > "$_dir/nvidia-smi" <<'MOCK'
+#!/bin/sh
+case "$1" in
+ -L) echo "GPU 0: NVIDIA GeForce RTX 5090 (UUID: GPU-fake-uuid)" ;;
+ *) echo "something completely unexpected" ;;
+esac
+MOCK
+chmod +x "$_dir/nvidia-smi"
+make_mock_probe "$_dir" "13.0 12.0"
+_result=$(run_func "$_dir")
+assert_eq "unparseable banner, library says 13.0 -> cu130" "https://download.pytorch.org/whl/cu130" "$_result"
+# 8c) The library's capabilities feed the pre-Turing cap
+make_mock_probe "$_dir" "12.8 6.1"
+_result=$(run_func "$_dir")
+assert_eq "library says 12.8 with sm_61 -> cu126" "https://download.pytorch.org/whl/cu126" "$_result"
+# 8d) The probe switched off -> the cu126 default again
+_result=$(PATH="$_dir:$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; UNSLOTH_NVIDIA_LIBRARY_PROBE=0; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null)
+assert_eq "probe off -> cu126 default" "https://download.pytorch.org/whl/cu126" "$_result"
+rm -rf "$_dir"
+
+# 8e) No nvidia-smi anywhere, the library lists a GPU -> its family, not cpu
+_dir=$(mktemp -d)
+make_mock_probe "$_dir" "12.9 8.9"
+_result=$(run_func "$_dir")
+assert_eq "no nvidia-smi, library says 12.9 -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
+rm -rf "$_dir"
+
+# 8f) The inventory is read once per run: the presence check's answer feeds the torch
+# index even when a second probe would fail, and the probe is not launched again.
+_dir=$(mktemp -d)
+printf '#!/bin/sh\ncat >/dev/null\nn=$(cat "%s/calls" 2>/dev/null || echo 0)\necho $((n + 1)) > "%s/calls"\n[ "$n" = 0 ] && echo "12.9 8.9"\n' "$_dir" "$_dir" > "$_dir/python3"
+chmod +x "$_dir/python3"
+_result=$(PATH="$_dir:$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; . '$_FUNC_FILE'; _has_usable_nvidia_gpu && get_torch_index_url" 2>/dev/null)
+assert_eq "memoised inventory -> cu128 from the first answer" "https://download.pytorch.org/whl/cu128" "$_result"
+assert_eq "memoised inventory -> one probe launch" "1" "$(cat "$_dir/calls")"
+rm -rf "$_dir"
+
+# 8g) No system python3: the managed venv's interpreter reads the library, and a run that
+# has no interpreter yet does not remember "no inventory" once the venv exists.
+_dir=$(mktemp -d)
+mkdir -p "$_dir/venv/bin"
+make_mock_probe "$_dir/venv/bin" "12.9 8.9"
+mv "$_dir/venv/bin/python3" "$_dir/venv/bin/python"
+_result=$(PATH="$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; VENV_DIR='$_dir/venv'; . '$_FUNC_FILE'; get_torch_index_url" 2>/dev/null)
+assert_eq "no python3, venv python reads 12.9 -> cu128" "https://download.pytorch.org/whl/cu128" "$_result"
+_result=$(PATH="$_TOOLS_DIR" bash -c "unset CUDA_VISIBLE_DEVICES; _ARCH=x86_64; VENV_DIR='$_dir/none'; . '$_FUNC_FILE'; _has_usable_nvidia_gpu; VENV_DIR='$_dir/venv'; get_torch_index_url" 2>/dev/null)
+assert_eq "no interpreter yet is not memoised -> cu128 once the venv exists" "https://download.pytorch.org/whl/cu128" "$_result"
+rm -rf "$_dir"
+
+# 8h) The memo lives in the parent shell: the presence check runs there before the
+# torch index is read in a command substitution, so the later checks do not probe again.
+_prime=$(grep -n '^ _has_usable_nvidia_gpu >/dev/null 2>&1 || true$' "$INSTALL_SH" | head -1 | cut -d: -f1)
+_guard=$(sed -n "$((_prime - 1))p" "$INSTALL_SH")
+_assign=$(grep -n -F 'TORCH_INDEX_URL=$(get_torch_index_url)' "$INSTALL_SH" | head -1 | cut -d: -f1)
+if [ -n "$_prime" ] && [ -n "$_assign" ] && [ "$_prime" -lt "$_assign" ]; then _result=ordered; else _result="prime=$_prime assign=$_assign"; fi
+assert_eq "presence check primes the inventory before the index substitution" "ordered" "$_result"
+assert_eq "the prime is skipped for a pinned index or no torch" 'if [ "$_torch_index_pinned" = false ] && [ "$SKIP_TORCH" = false ]; then' "$_guard"
+
# 9) ROCm 6.3 (no nvidia-smi) -> rocm6.3
_dir=$(make_mock_amd_smi "6.3")
_result=$(run_func "$_dir")
diff --git a/tests/studio/install/test_install_llama_prebuilt_logic.py b/tests/studio/install/test_install_llama_prebuilt_logic.py
index 0c7a1875f1c..9904d6b7bdd 100644
--- a/tests/studio/install/test_install_llama_prebuilt_logic.py
+++ b/tests/studio/install/test_install_llama_prebuilt_logic.py
@@ -4531,6 +4531,9 @@ function Write-LlamaFailureLog { param($Output) }
function Mark-StudioOwned { param($Path) }
function Get-InstalledLlamaPrebuiltRelease { param($InstallDir) return $null }
function Test-PathQuiet { param($p) return $false }
+# Nothing to keep, as in the bash mirror where the guard is absent: exit 2 falls back to a compile.
+function Get-GpuPrebuiltToKeepOverSourceBuild { param($InstallDir) return "" }
+function Get-LlamaUpdateFailReason { param($Output) return "network" }
function Exit-SetupFailure {
param($Message, $Code = 1)
Write-Output "setup_fail: $Code"
diff --git a/tests/studio/install/test_install_sh_nvidia_inventory_reader.py b/tests/studio/install/test_install_sh_nvidia_inventory_reader.py
new file mode 100644
index 00000000000..21492e4d7fb
--- /dev/null
+++ b/tests/studio/install/test_install_sh_nvidia_inventory_reader.py
@@ -0,0 +1,135 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+"""install.sh's inline driver-library reader (_nvidia_library_inventory) against stub
+libraries: an NVML that raises or lacks the versioned entry points must not stop the
+CUDA driver API from answering."""
+
+from __future__ import annotations
+
+import io
+import os
+import re
+import sys
+import types
+from contextlib import redirect_stdout
+from pathlib import Path
+
+INSTALL_SH = Path(__file__).resolve().parents[3] / "install.sh"
+
+
+def _reader_source() -> str:
+ text = INSTALL_SH.read_text(encoding = "utf-8")
+ m = re.search(r"_nvidia_library_inventory\(\) \{.*?<<'PY'\n(.*?)\nPY\n", text, re.S)
+ assert m, "the inline reader was not found"
+ return m.group(1)
+
+
+class _Ok:
+ """A C function stub: writes the values it was given to its byref arguments."""
+
+ def __init__(self, *values):
+ self.values = values
+
+ def __call__(self, *args):
+ refs = [a for a in args if hasattr(a, "_obj")]
+ for ref, value in zip(refs, self.values):
+ ref._obj.value = value
+ return 0
+
+
+def _lib(**symbols):
+ return types.SimpleNamespace(**symbols)
+
+
+def _cuda_lib(version = 13010, cap = (8, 9)):
+ return _lib(
+ cuInit = _Ok(),
+ cuDeviceGetCount = _Ok(1),
+ cuDriverGetVersion = _Ok(version),
+ cuDeviceGet = _Ok(0),
+ cuDeviceGetAttribute = _Attr(cap),
+ )
+
+
+class _Attr:
+ def __init__(self, cap):
+ self.cap = cap
+
+ def __call__(self, ref, attribute, dev):
+ ref._obj.value = self.cap[0] if attribute == 75 else self.cap[1]
+ return 0
+
+
+def _run(monkeypatch, libraries: dict[str, object]) -> tuple[int, str]:
+ """Run the reader with ctypes.CDLL answering from `libraries`; (exit code, stdout)."""
+ import ctypes as real_ctypes
+
+ def cdll(name):
+ if name in libraries:
+ return libraries[name]
+ raise OSError(f"{name}: cannot open shared object file")
+
+ stub = types.SimpleNamespace(
+ CDLL = cdll,
+ c_uint = real_ctypes.c_uint,
+ c_int = real_ctypes.c_int,
+ c_void_p = real_ctypes.c_void_p,
+ byref = real_ctypes.byref,
+ )
+ monkeypatch.setitem(sys.modules, "ctypes", stub)
+ out = io.StringIO()
+ code = 0
+ try:
+ with redirect_stdout(out):
+ exec(compile(_reader_source(), str(INSTALL_SH), "exec"), {"__name__": "__main__"})
+ except SystemExit as e:
+ code = int(e.code or 0)
+ return code, out.getvalue().strip()
+
+
+def test_the_cuda_driver_api_answers_when_nvml_lacks_the_versioned_symbols(monkeypatch):
+ # An older NVML: nvmlInit_v2 is missing, the unversioned nvmlInit exists but nothing else does.
+ nvml = _lib(nvmlInit = _Ok(), nvmlShutdown = _Ok())
+ code, out = _run(monkeypatch, {"libnvidia-ml.so.1": nvml, "libcuda.so.1": _cuda_lib()})
+ assert (code, out) == (0, "13.1 8.9")
+
+
+def test_a_reader_that_raises_yields_to_the_next(monkeypatch):
+ class _Broken:
+ def __getattr__(self, name):
+ raise AttributeError(name)
+
+ code, out = _run(monkeypatch, {"libnvidia-ml.so.1": _Broken(), "libcuda.so.1": _cuda_lib()})
+ assert (code, out) == (0, "13.1 8.9")
+
+
+def test_the_cuda_reader_sees_the_physical_cards(monkeypatch):
+ """The driver API honours CUDA_VISIBLE_DEVICES; the inventory must not."""
+ monkeypatch.setenv("CUDA_VISIBLE_DEVICES", "1")
+ lib = _cuda_lib()
+
+ def cu_init(flags):
+ assert "CUDA_VISIBLE_DEVICES" not in os.environ
+ return 0
+
+ lib.cuInit = cu_init
+ code, out = _run(monkeypatch, {"libcuda.so.1": lib})
+ assert (code, out) == (0, "13.1 8.9")
+
+
+def test_no_library_at_all_is_no_inventory(monkeypatch):
+ code, out = _run(monkeypatch, {})
+ assert (code, out) == (1, "")
+
+
+def test_nvml_answers_first_when_it_can(monkeypatch):
+ nvml = _lib(
+ nvmlInit_v2 = _Ok(),
+ nvmlShutdown = _Ok(),
+ nvmlDeviceGetCount_v2 = _Ok(2),
+ nvmlSystemGetCudaDriverVersion_v2 = _Ok(12080),
+ nvmlDeviceGetHandleByIndex_v2 = _Ok(1),
+ nvmlDeviceGetCudaComputeCapability = _Ok(6, 1),
+ )
+ code, out = _run(monkeypatch, {"libnvidia-ml.so.1": nvml, "libcuda.so.1": _cuda_lib()})
+ assert (code, out) == (0, "12.8 6.1,6.1")
diff --git a/tests/studio/test_installer_av_shapes.py b/tests/studio/test_installer_av_shapes.py
index ce7be10f09b..a2701301c7f 100644
--- a/tests/studio/test_installer_av_shapes.py
+++ b/tests/studio/test_installer_av_shapes.py
@@ -149,6 +149,20 @@ ALLOWED_PINVOKES = {
"QueryFullProcessImageNameW",
# Closing the handles CreateFileW and OpenProcess opened.
"CloseHandle",
+ # The NVIDIA driver's own inventory (Get-NvidiaLibraryInventory) for a host whose nvidia-smi is absent, stale
+ # or hangs: the CUDA driver version and one compute capability per GPU. No PowerShell or .NET equivalent
+ # exists; nvidia-smi is the thing being worked around, and reading the registry names no driver version.
+ "nvmlInit_v2",
+ "nvmlShutdown",
+ "nvmlSystemGetCudaDriverVersion_v2",
+ "nvmlDeviceGetCount_v2",
+ "nvmlDeviceGetHandleByIndex_v2",
+ "nvmlDeviceGetCudaComputeCapability",
+ "cuInit",
+ "cuDriverGetVersion",
+ "cuDeviceGetCount",
+ "cuDeviceGet",
+ "cuDeviceGetAttribute",
}
diff --git a/tests/studio/test_nvidia_library_inventory.ps1 b/tests/studio/test_nvidia_library_inventory.ps1
new file mode 100644
index 00000000000..c7b6789a8fb
--- /dev/null
+++ b/tests/studio/test_nvidia_library_inventory.ps1
@@ -0,0 +1,287 @@
+#!/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
+# Behavioural test for the NVIDIA driver-library inventory in install.ps1 and studio/setup.ps1:
+# the torch family and compute capability come from NVML / the CUDA driver API when nvidia-smi
+# cannot answer, an unknown driver version no longer means cu126, and a prebuilt update that
+# fails keeps a working GPU prebuilt instead of building for the CPU (#9255).
+# Run: pwsh -NoProfile -File tests/studio/test_nvidia_library_inventory.ps1
+
+$ErrorActionPreference = "Stop"
+$root = (Resolve-Path ([System.IO.Path]::Combine($PSScriptRoot, "..", ".."))).Path
+
+$failures = 0
+function Check($name, $cond) {
+ if ($cond) { Write-Host " PASS $name" }
+ else { Write-Host " FAIL $name" -ForegroundColor Red; $script:failures++ }
+}
+
+function Get-HelperSources($path, $names) {
+ $tokens = $null; $errors = $null
+ $ast = [System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$tokens, [ref]$errors)
+ if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "$path has parse errors" }
+ $out = @()
+ foreach ($name in $names) {
+ $fn = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name
+ }, $true)
+ if ($fn.Count -lt 1) { throw "expected $name in $path, found none" }
+ $out += $fn[0].Extent.Text
+ }
+ return $out
+}
+
+# Printer stubs, so the file does not depend on the ANSI helpers.
+function substep { param([string]$Message, [string]$Color = "DarkGray") $script:Substeps += $Message }
+function step { param([string]$Component, [string]$Message, [string]$Color = "") $script:Steps += $Message }
+function Write-StudioLine { param([string]$Line, [string]$ForegroundColor = "") }
+function Write-StudioStdoutMirror { param([string]$Line) }
+function Write-LlamaFailureLog { param([string]$Output, [int]$MaxLines = 120) }
+function Test-PathQuiet { param([string]$Path, [string]$PathType = "Any") Test-Path -LiteralPath $Path }
+function Test-CudaFamilyLeaf { param([string]$Leaf) return ($Leaf -match '^cu\d+$') }
+$script:FakeIntelAdapters = @()
+function Get-IntelRegistryAdapterNames { return $script:FakeIntelAdapters }
+
+# nvidia-smi stand-in: $script:FakeSmiStdout answers every probe, $script:FakeSmiRc is its exit code.
+function Invoke-NvidiaSmiBounded {
+ param([string]$Exe, [string[]]$SmiArgs = @(), [int]$TimeoutSec = 10, [switch]$StdoutOnly)
+ $global:LASTEXITCODE = $script:FakeSmiRc
+ return $script:FakeSmiStdout
+}
+
+$installPs1 = Join-Path $root "install.ps1"
+$setupPs1 = Join-Path $root "studio\setup.ps1"
+
+Write-Host ""
+Write-Host "=== shared inventory helper ==="
+$blockNames = @("Get-NvidiaNvmlLibraryPath", "Get-NvidiaLibraryProbeType", "Read-NvidiaLibraryRaw", "Get-NvidiaLibraryInventory")
+$installParts = @(Get-HelperSources $installPs1 $blockNames)
+$setupParts = @(Get-HelperSources $setupPs1 $blockNames)
+$installBlock = $installParts[3]
+$setupBlock = $setupParts[3]
+$setupPath = $setupParts[0]
+$readBlock = $setupParts[2]
+# install.ps1 nests its helpers one level deeper; compare the two copies without indentation.
+$strip = { param($text) ($text -split "`n" | ForEach-Object { $_.TrimStart() }) -join "`n" }
+for ($k = 0; $k -lt $blockNames.Count; $k++) {
+ Check "install.ps1 and setup.ps1 carry the same $($blockNames[$k])" ((& $strip $installParts[$k]) -eq (& $strip $setupParts[$k]))
+}
+# The installer must not spawn a C# compiler (windows-no-compiler-ci): the methods are emitted.
+Check "the inventory compiles nothing" ((($setupParts -join "`n") -notmatch 'Add-Type') -and ($setupParts[1] -match 'New-StudioEmittedNativeType'))
+Check "the emission is gated on the native-type capability" ($setupParts[1] -match 'if \(-not \(Test-StudioCanDefineNativeTypes\)\) \{ return \$null \}')
+Invoke-Expression $setupPath
+$pathRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-nvml-" + [guid]::NewGuid().ToString("N"))
+$sys32 = Join-Path (Join-Path $pathRoot "root") "System32"
+$nvsmi = Join-Path (Join-Path $pathRoot "pf") "NVIDIA Corporation\NVSMI"
+New-Item -ItemType Directory -Path $sys32, $nvsmi -Force | Out-Null
+$savedRoot = $env:SystemRoot; $savedPf = $env:ProgramFiles
+try {
+ $env:SystemRoot = Join-Path $pathRoot "root"; $env:ProgramFiles = Join-Path $pathRoot "pf"
+ Check "no nvml.dll on disk keeps the bare name" ((Get-NvidiaNvmlLibraryPath) -eq "nvml.dll")
+ Set-Content -Path (Join-Path $nvsmi "nvml.dll") -Value ""
+ Check "an NVSMI-only nvml.dll is named by its path" ((Get-NvidiaNvmlLibraryPath) -eq (Join-Path $nvsmi "nvml.dll"))
+ Set-Content -Path (Join-Path $sys32 "nvml.dll") -Value ""
+ Check "System32 wins when both exist" ((Get-NvidiaNvmlLibraryPath) -eq (Join-Path $sys32 "nvml.dll"))
+} finally {
+ $env:SystemRoot = $savedRoot; $env:ProgramFiles = $savedPf
+ Remove-Item -LiteralPath $pathRoot -Recurse -Force -ErrorAction SilentlyContinue
+}
+Check "a failed driver-version read is not an inventory" (
+ $readBlock -match 'nvmlSystemGetCudaDriverVersion_v2\(\[ref\]\$ver\) -ne 0' -and
+ $readBlock -match 'cuDriverGetVersion\(\[ref\]\$ver\) -ne 0')
+
+# The real libraries, in a child so the fake type below can own this session.
+$helperFile = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-inventory-" + [System.IO.Path]::GetRandomFileName() + ".ps1")
+$emitters = @(Get-HelperSources $setupPs1 @("New-StudioDynamicAssembly", "New-StudioEmittedNativeType"))
+# The capability gate is a Windows policy probe; the child stands in for it and answers yes.
+$gateStub = 'function Test-StudioCanDefineNativeTypes { return $true }'
+$helperBody = (@($gateStub) + $emitters + $setupParts) -join "`n"
+Set-Content -LiteralPath $helperFile -Value ($helperBody + "`n`$inv = Get-NvidiaLibraryInventory`nif (`$inv) { `$inv | ConvertTo-Json -Compress } else { 'null' }")
+$pwshExe = (Get-Process -Id $PID).Path
+$realJson = & $pwshExe -NoProfile -File $helperFile 2>&1 | Select-Object -Last 1
+$offJson = & $pwshExe -NoProfile -Command "`$env:UNSLOTH_NVIDIA_LIBRARY_PROBE = '0'; & '$helperFile'" 2>&1 | Select-Object -Last 1
+Remove-Item -LiteralPath $helperFile -ErrorAction SilentlyContinue
+Check "UNSLOTH_NVIDIA_LIBRARY_PROBE=0 turns the probe off" ("$offJson" -eq "null")
+if ("$realJson" -ne "null" -and "$realJson" -match '^\{') {
+ $real = "$realJson" | ConvertFrom-Json
+ Write-Host " host inventory: $($real.Source) CUDA $($real.CudaMajor).$($real.CudaMinor) caps $($real.ComputeCaps -join ',')"
+ Check "a real inventory names the driver family" ($real.CudaMajor -ge 11)
+ Check "a real inventory lists one capability per GPU" ($real.Count -eq @($real.ComputeCaps).Count -and $real.Count -ge 1)
+ Check "capabilities are major.minor strings" (@(@($real.ComputeCaps) | Where-Object { $_ -notmatch '^\d+\.\d+$' }).Count -eq 0)
+} else {
+ Write-Host " (no NVIDIA driver library on this host; the real-inventory checks are skipped)"
+}
+
+# The parser, over a stand-in for the library reader.
+Invoke-Expression $setupBlock
+$script:FakeRaw = ""
+function Read-NvidiaLibraryRaw { param([int]$TimeoutMs = 10000) return $script:FakeRaw }
+function Probe-Raw($raw) {
+ $script:NvidiaLibraryInventoryProbed = $false
+ $script:FakeRaw = $raw
+ return Get-NvidiaLibraryInventory
+}
+$parsed = Probe-Raw "nvml;13;1;8.9,12.0"
+Check "a probe line parses to the driver version and capabilities" (
+ $parsed.Source -eq "nvml" -and $parsed.CudaMajor -eq 13 -and $parsed.CudaMinor -eq 1 -and
+ $parsed.Count -eq 2 -and ($parsed.ComputeCaps -join ",") -eq "8.9,12.0")
+Check "the CUDA driver API line parses too" ((Probe-Raw "cuda;12;8;12.0").Source -eq "cuda")
+Check "an empty probe is no inventory" ($null -eq (Probe-Raw ""))
+Check "a zero driver version is no inventory" ($null -eq (Probe-Raw "nvml;0;0;8.9"))
+Check "no capabilities is no inventory" ($null -eq (Probe-Raw "nvml;13;0;"))
+Check "an unreadable capability voids the inventory" ($null -eq (Probe-Raw "nvml;13;0;N/A,8.9"))
+Check "one unreadable GPU voids the reader's source too" ($readBlock -notmatch '\bcontinue\b')
+Check "the CUDA driver API is read with the mask lifted" ($readBlock -match 'Remove-Item Env:CUDA_VISIBLE_DEVICES' -and $readBlock -match '\$env:CUDA_VISIBLE_DEVICES = \$saved')
+Check "setup.ps1 initialises the cache with its other script state" (
+ (Get-Content -LiteralPath $setupPs1 -Raw) -match '(?m)^\$script:NvidiaLibraryInventoryProbed = \$false')
+Check "the answer is cached" ($null -eq (Get-NvidiaLibraryInventory))
+
+Write-Host ""
+Write-Host "=== consumers of the inventory ==="
+$resolve = @(Get-HelperSources $setupPs1 @("Resolve-CudaToolkit"))[0]
+Check "Resolve-CudaToolkit takes the driver ceiling from the inventory" (
+ $resolve -match 'Get-NvidiaLibraryInventory' -and $resolve -match '-not \$NvidiaSmiExe')
+$installText = Get-Content -LiteralPath $installPs1 -Raw
+$sharedEnd = $installText.IndexOf("END SHARED WITH studio/setup.ps1 (Get-NvidiaLibraryInventory)")
+$detect = $installText.IndexOf("Detect GPU (robust", $sharedEnd)
+Check "install.ps1 resets the cache before each invocation's detection" (
+ $installText.Substring($sharedEnd, $detect - $sharedEnd) -match 'NvidiaLibraryInventoryProbed = \$false')
+
+# From here on the inventory is a stub the cases control.
+function Get-NvidiaLibraryInventory { param([int]$TimeoutSec = 10) return $script:FakeInventory }
+
+Write-Host ""
+Write-Host "=== studio/setup.ps1 ==="
+foreach ($src in (Get-HelperSources $setupPs1 @("Get-NvidiaCu126Verdict", "Get-CudaFamilyCappedForPreTuring",
+ "Get-PytorchCudaTag", "Get-CudaComputeCapability", "Get-LlamaUpdateFailReason",
+ "Get-PrebuiltMarkerBackend", "Get-GpuPrebuiltToKeepOverSourceBuild"))) {
+ Invoke-Expression $src
+}
+
+$script:NvidiaSmiRejected = $false
+$script:NvidiaSmiExe = "C:\fake\nvidia-smi.exe"
+$script:FakeSmiRc = 0
+$script:FakeSmiStdout = "| NVIDIA-SMI 580.00 Driver Version: 580.00 CUDA Version: 12.9 |"
+$script:FakeInventory = $null
+$script:PreTuringCapAnnounced = $false
+Check "a readable banner still picks the family from nvidia-smi" ((Get-PytorchCudaTag) -eq "cu128")
+
+$script:FakeSmiStdout = ""
+$script:FakeSmiRc = 1
+$script:FakeInventory = @{ Source = "nvml"; CudaMajor = 13; CudaMinor = 0; ComputeCaps = @("8.9"); Count = 1 }
+Check "a failing nvidia-smi reads the family from the inventory" ((Get-PytorchCudaTag) -eq "cu130")
+Check "the compute capability comes from the inventory too" ((Get-CudaComputeCapability) -eq "89")
+
+$script:FakeInventory = @{ Source = "nvml"; CudaMajor = 13; CudaMinor = 0; ComputeCaps = @("6.1"); Count = 1 }
+$script:PreTuringCapAnnounced = $false
+Check "the pre-Turing cap reads the inventory capabilities" ((Get-PytorchCudaTag) -eq "cu126")
+
+$script:NvidiaSmiExe = $null
+$script:FakeInventory = @{ Source = "cuda"; CudaMajor = 12; CudaMinor = 8; ComputeCaps = @("12.0"); Count = 1 }
+Check "no nvidia-smi at all still names the family" ((Get-PytorchCudaTag) -eq "cu128")
+
+$script:FakeInventory = $null
+Check "nothing answering means unknown, not cu126" ((Get-PytorchCudaTag) -eq "")
+Check "an unknown capability stays null" ($null -eq (Get-CudaComputeCapability))
+
+# Detection rejected nvidia-smi (absent or unusable) and the library answered: the consumers
+# do not rediscover it, so a wedged or stale banner never overrides the inventory.
+$script:NvidiaSmiRejected = $true
+$script:FakeSmiStdout = "Driver Version: 560.00 CUDA Version: 12.6"; $script:FakeSmiRc = 0
+$script:FakeInventory = @{ Source = "nvml"; CudaMajor = 13; CudaMinor = 1; ComputeCaps = @("8.9"); Count = 1 }
+Check "a rejected nvidia-smi is not asked again for the tag" ((Get-PytorchCudaTag) -eq "cu130")
+Check "a rejected nvidia-smi is not asked again for the capability" ((Get-CudaComputeCapability) -eq "89")
+$script:NvidiaSmiRejected = $false
+$setupText = Get-Content -LiteralPath $setupPs1 -Raw
+Check "setup.ps1 marks nvidia-smi rejected when the library stands in" ($setupText -match '(?m)^\s+\$script:NvidiaSmiRejected = \$true' -and $setupText -match '(?m)^\$script:NvidiaSmiRejected = \$false')
+
+Check "a 429 reads as a rate limit" ((Get-LlamaUpdateFailReason "HTTP Error 429: too many requests") -eq "GitHub rate limit")
+Check "a DNS failure reads as a network error" ((Get-LlamaUpdateFailReason "temporary failure in name resolution") -eq "network error")
+Check "anything else is a download failure" ((Get-LlamaUpdateFailReason "sha256 mismatch") -eq "download failed")
+
+# The keep guard: marker backend, GPU presence, this run's requests, and the installed tree running.
+$install = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-keep-" + [System.IO.Path]::GetRandomFileName())
+New-Item -ItemType Directory -Path $install -Force | Out-Null
+Set-Content -LiteralPath (Join-Path $install "UNSLOTH_PREBUILT_INFO.json") -Value '{"backend": "cuda", "release_tag": "v1"}'
+function python { $global:LASTEXITCODE = $script:FakePythonRc }
+$script:FakePythonRc = 0
+$HasNvidiaSmi = $true
+$HasROCm = $false
+$script:ROCmGfxArch = $null
+$script:IsIntelXpu = $false
+$LlamaPr = ""
+$explicitLlamaSourceBackend = $null
+foreach ($name in @("UNSLOTH_LLAMA_FORCE_COMPILE", "UNSLOTH_LLAMA_RELEASE_TAG", "UNSLOTH_LLAMA_TAG")) {
+ Remove-Item "Env:$name" -ErrorAction SilentlyContinue
+}
+Check "a running CUDA prebuilt on an NVIDIA host is kept" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "cuda")
+$env:UNSLOTH_LLAMA_TAG = "latest"
+Check "the default tag spelling still keeps" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "cuda")
+$env:UNSLOTH_LLAMA_TAG = "b7000"
+Check "a version pin asked for that version, so nothing is kept" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "")
+$env:UNSLOTH_LLAMA_TAG = "master"
+Check "master asks for a source build, so nothing is kept" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "")
+Remove-Item Env:UNSLOTH_LLAMA_TAG
+$env:UNSLOTH_LLAMA_FORCE_COMPILE = "1"
+Check "a forced compile is not kept over" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "")
+Remove-Item Env:UNSLOTH_LLAMA_FORCE_COMPILE
+$explicitLlamaSourceBackend = "cuda"
+Check "an explicit backend request is not kept over" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "")
+$explicitLlamaSourceBackend = $null
+$script:FakePythonRc = 1
+Check "an install that no longer runs is not kept" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "")
+$script:FakePythonRc = 0
+$HasNvidiaSmi = $false
+Check "a CUDA prebuilt with the GPU gone is not kept" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "")
+Set-Content -LiteralPath (Join-Path $install "UNSLOTH_PREBUILT_INFO.json") -Value '{"backend": "vulkan"}'
+$HasROCm = $true
+Check "a Vulkan prebuilt is kept for any GPU vendor" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "vulkan")
+$HasROCm = $false
+Check "a Vulkan prebuilt with no GPU at all is not kept" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "")
+$script:FakeIntelAdapters = @("Intel(R) UHD Graphics 770")
+Check "a Vulkan prebuilt is kept for an Intel adapter that is not an XPU part" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "vulkan")
+$script:FakeIntelAdapters = @()
+Set-Content -LiteralPath (Join-Path $install "UNSLOTH_PREBUILT_INFO.json") -Value '{"backend": "cpu"}'
+Check "a CPU prebuilt has nothing to keep" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "")
+# Markers from before the backend field, read as setup.sh reads them.
+$HasNvidiaSmi = $true
+Set-Content -LiteralPath (Join-Path $install "UNSLOTH_PREBUILT_INFO.json") -Value '{"llama_backend": "cuda"}'
+Check "a legacy marker naming the request is kept" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "cuda")
+Set-Content -LiteralPath (Join-Path $install "UNSLOTH_PREBUILT_INFO.json") -Value '{"asset": "app-b8508-mix-windows-x64-cuda13-newer.zip"}'
+Check "a legacy marker naming only the asset is kept" ((Get-GpuPrebuiltToKeepOverSourceBuild -InstallDir $install) -eq "cuda")
+Set-Content -LiteralPath (Join-Path $install "UNSLOTH_PREBUILT_INFO.json") -Value '{"backend": "HIP"}'
+Check "hip reads as rocm, case-insensitively" ((Get-PrebuiltMarkerBackend -Marker (Join-Path $install "UNSLOTH_PREBUILT_INFO.json")) -eq "rocm")
+Set-Content -LiteralPath (Join-Path $install "UNSLOTH_PREBUILT_INFO.json") -Value ''
+Check "an empty marker has no backend" ((Get-PrebuiltMarkerBackend -Marker (Join-Path $install "UNSLOTH_PREBUILT_INFO.json")) -eq "")
+Remove-Item -LiteralPath $install -Recurse -Force -ErrorAction SilentlyContinue
+
+Write-Host ""
+Write-Host "=== install.ps1 ==="
+foreach ($src in (Get-HelperSources $installPs1 @("Get-NvidiaCu126Verdict", "Get-CudaFamilyCappedForPreTuring",
+ "Trim-IndexPathSlashes", "Get-TorchIndexUrl"))) {
+ Invoke-Expression $src
+}
+foreach ($name in @("UNSLOTH_TORCH_INDEX_URL", "UNSLOTH_TORCH_INDEX_FAMILY", "UNSLOTH_PYTORCH_MIRROR")) {
+ Remove-Item "Env:$name" -ErrorAction SilentlyContinue
+}
+$NvidiaSmiExe = $null
+$script:FakeInventory = $null
+Check "no nvidia-smi and no inventory is the CPU index" ((Get-TorchIndexUrl) -eq "https://download.pytorch.org/whl/cpu")
+$script:FakeInventory = @{ Source = "nvml"; CudaMajor = 13; CudaMinor = 1; ComputeCaps = @("10.0"); Count = 1 }
+Check "no nvidia-smi but an inventory is the driver's family" ((Get-TorchIndexUrl) -eq "https://download.pytorch.org/whl/cu130")
+$NvidiaSmiExe = "C:\fake\nvidia-smi.exe"
+$script:FakeSmiRc = 0
+$script:FakeSmiStdout = "no banner here"
+Check "an unreadable banner falls back to the inventory" ((Get-TorchIndexUrl) -eq "https://download.pytorch.org/whl/cu130")
+$script:FakeInventory = @{ Source = "nvml"; CudaMajor = 12; CudaMinor = 8; ComputeCaps = @("7.0"); Count = 1 }
+$script:PreTuringCapAnnounced = $false
+Check "the cap applies to inventory capabilities" ((Get-TorchIndexUrl) -eq "https://download.pytorch.org/whl/cu126")
+$script:FakeInventory = $null
+$script:Substeps = @()
+Check "an unreadable banner with no inventory keeps the cu126 default" ((Get-TorchIndexUrl) -eq "https://download.pytorch.org/whl/cu126")
+$script:FakeSmiStdout = "| CUDA Version: 12.6 |"
+Check "a readable banner is still authoritative" ((Get-TorchIndexUrl) -eq "https://download.pytorch.org/whl/cu126")
+
+Write-Host ""
+if ($failures -gt 0) { Write-Host "$failures check(s) failed" -ForegroundColor Red; exit 1 }
+Write-Host "All checks passed"