2026-04-05 19:12:45 +08:00
|
|
|
"""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."""
|
2026-04-05 22:03:57 +08:00
|
|
|
# Allow explicit disable via env var (set to "false" or "0")
|
2026-04-14 20:55:42 +08:00
|
|
|
override = os.environ.get("ASHIM_GPU")
|
2026-04-05 22:03:57 +08:00
|
|
|
if override is not None and override.lower() in ("0", "false", "no"):
|
|
|
|
|
return False
|
2026-04-05 19:12:45 +08:00
|
|
|
|
2026-04-05 22:24:05 +08:00
|
|
|
# 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.
|
2026-04-05 19:12:45 +08:00
|
|
|
try:
|
|
|
|
|
import torch
|
2026-04-05 22:24:05 +08:00
|
|
|
return torch.cuda.is_available()
|
2026-04-05 19:12:45 +08:00
|
|
|
except ImportError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def onnx_providers():
|
|
|
|
|
"""Return ONNX Runtime execution providers in priority order."""
|
|
|
|
|
if gpu_available():
|
|
|
|
|
return ["CUDAExecutionProvider", "CPUExecutionProvider"]
|
|
|
|
|
return ["CPUExecutionProvider"]
|