Keep probing for nvidia-smi after an unusable one on PATH

_has_usable_nvidia_gpu gated its Windows fixed-location fallback on the
PATH lookup missing, not on the GPU check failing. A stale or driverless
nvidia-smi exits non-zero listing nothing, so the search stopped there
and the function reported no NVIDIA GPU even with a working driver
binary under NVSMI or System32.

That answer routes a mixed AMD iGPU plus NVIDIA dGPU Windows host into
_ensure_rocm_torch() and replaces its CUDA stack with ROCm wheels.

install.ps1 and studio/setup.ps1 already do the right thing: both call
Test-NvidiaSmiHasGpu on the PATH result and fall through to the two fixed
paths when it fails, with the same reasoning recorded at install.ps1:1708
("a stale/driverless nvidia-smi can exit 0 while listing no GPU"). This
brings the Python helper to the same rule: collect the candidates, then
take the first that lists a GPU.

Reproduced with real stub executables through the real subprocess call,
before and after:

    PATH exe   fixed-location exe   before   after
    absent     working              True     True
    stale      working              False    True
    working    -                    True     True
    none       none                 False    False

Only the stale row changes. An AMD-only host with a leftover nvidia-smi
still gets False, so it is not denied the ROCm wheels.

tests/studio/install/test_nvidia_smi_candidate_probing.py pins all four
rows plus the CUDA_VISIBLE_DEVICES cases: 1 failed / 8 passed before,
9 passed after.
This commit is contained in:
Daniel Han 2026-07-27 23:17:58 +00:00
commit 10bc8aa538
2 changed files with 151 additions and 20 deletions

View file

@ -1175,9 +1175,32 @@ def _has_usable_nvidia_gpu() -> bool:
cvd = os.environ.get("CUDA_VISIBLE_DEVICES")
if cvd is not None and cvd.strip() in ("", "-1"):
return False
exe = shutil.which("nvidia-smi")
if not exe and IS_WINDOWS:
for _candidate in (
def _lists_a_gpu(exe: str) -> bool:
try:
result = subprocess.run(
[exe, "-L"],
stdout = subprocess.PIPE,
stderr = subprocess.DEVNULL,
text = True,
timeout = 10,
)
except Exception:
return False
return result.returncode == 0 and "GPU " in result.stdout
# Try every candidate until one lists a GPU, rather than committing to the
# first executable found. A stale or driverless nvidia-smi on PATH exits
# non-zero listing nothing; stopping there would report the host as
# NVIDIA-free and route it into _ensure_rocm_torch() even though a working
# driver binary sits at a fixed location. install.ps1 and setup.ps1 both
# gate their fallback on the GPU check failing, not on the PATH lookup
# missing, so mirror that.
candidates = []
_path_exe = shutil.which("nvidia-smi")
if _path_exe:
candidates.append(_path_exe)
if IS_WINDOWS:
candidates.extend((
os.path.join(
os.environ.get("ProgramFiles", r"C:\Program Files"),
"NVIDIA Corporation",
@ -1189,23 +1212,12 @@ def _has_usable_nvidia_gpu() -> bool:
"System32",
"nvidia-smi.exe",
),
):
if os.path.isfile(_candidate):
exe = _candidate
break
if exe:
try:
result = subprocess.run(
[exe, "-L"],
stdout = subprocess.PIPE,
stderr = subprocess.DEVNULL,
text = True,
timeout = 10,
)
if result.returncode == 0 and "GPU " in result.stdout:
return True
except Exception:
pass
))
for _candidate in candidates:
if _candidate != _path_exe and not os.path.isfile(_candidate):
continue
if _lists_a_gpu(_candidate):
return True
# Fallback: the NVIDIA driver exposes one subdirectory per GPU under
# /proc/driver/nvidia/gpus/ on Linux regardless of nvidia-smi state.
if sys.platform != "win32":