mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
onnxruntime-gpu reports CUDAExecutionProvider as "available" just
because the library was compiled with CUDA support, even on machines
with no GPU. This made gpu_available() return True incorrectly,
causing upscale.py to try torch.device("cuda") and fall back to
Lanczos instead of running Real-ESRGAN on CPU.
torch.cuda.is_available() actually probes the hardware. Use it as
the single source of truth for GPU detection.
Verified: CUDA image on Apple Silicon (no GPU) now correctly reports
gpu: false and all AI tools run on CPU without crashes.
31 lines
932 B
Python
31 lines
932 B
Python
"""Runtime GPU/CUDA detection utility."""
|
|
import functools
|
|
import os
|
|
|
|
|
|
@functools.lru_cache(maxsize=1)
|
|
def gpu_available():
|
|
"""Return True if a usable CUDA GPU is present at runtime."""
|
|
# Allow explicit disable via env var (set to "false" or "0")
|
|
override = os.environ.get("STIRLING_GPU")
|
|
if override is not None and override.lower() in ("0", "false", "no"):
|
|
return False
|
|
|
|
# Use torch.cuda as the source of truth. It actually probes
|
|
# the hardware. onnxruntime's get_available_providers() only
|
|
# reports compiled-in backends, not whether a GPU exists.
|
|
try:
|
|
import torch
|
|
return torch.cuda.is_available()
|
|
except ImportError:
|
|
pass
|
|
|
|
return False
|
|
|
|
|
|
def onnx_providers():
|
|
"""Return ONNX Runtime execution providers in priority order."""
|
|
if gpu_available():
|
|
return ["CUDAExecutionProvider", "CPUExecutionProvider"]
|
|
return ["CPUExecutionProvider"]
|