Skip to content

Commit 95ea3a0

Browse files
authored
[FIX] Resolve JIT GPU backend by explicit signals, not stray /opt/rocm (#652)
Fixes #651. The JIT extension backend selector decided CUDA-vs-HIP by probing for a ROCm home, whose fallback returns `/opt/rocm` whenever that directory merely exists. A CUDA/NVIDIA host carrying a stray `/opt/rocm` was therefore misclassified as HIP even with `nvcc` present and no `hipcc`. `_detect_gpu_backend()` now resolves the backend from explicit signals, in order: the `TVM_FFI_GPU_BACKEND` override, then PyTorch build signals (`torch.version.hip` / `torch.version.cuda`), then an available `hipcc` when no `nvcc` is present. Detection defaults to CUDA when otherwise inconclusive, and the existence of `/opt/rocm` alone never forces HIP. Toolkit-home, compiler, include-path, flag, and architecture discovery are unchanged and run only after the backend is resolved, so CUDA and HIP state never mix.
1 parent 458aaf7 commit 95ea3a0

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

python/tvm_ffi/cpp/extension.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,50 @@
4040
logger = logging.getLogger(__name__)
4141

4242

43+
def _find_compiler(name: str, *home_vars: str) -> str | None:
44+
"""Locate a compiler on ``PATH`` or under an env-specified toolkit home's ``bin``."""
45+
found = shutil.which(name)
46+
if found is not None:
47+
return found
48+
for var in home_vars:
49+
home = os.environ.get(var)
50+
if home:
51+
found = shutil.which(name, path=str(Path(home) / "bin"))
52+
if found is not None:
53+
return found
54+
return None
55+
56+
4357
@functools.lru_cache
4458
def _detect_gpu_backend() -> BACKEND_STR:
45-
"""Auto-detect whether to use CUDA or HIP (ROCm).
46-
47-
Returns 'hip' if ROCm/HIP is available, 'cuda' otherwise.
59+
"""Auto-detect whether to use CUDA or HIP (ROCm), defaulting to CUDA.
60+
61+
Resolution order: the ``TVM_FFI_GPU_BACKEND`` override, then PyTorch build
62+
signals (``torch.version.hip`` / ``torch.version.cuda``), then a discoverable
63+
``hipcc`` when no ``nvcc`` can be found. Compilers are resolved from ``PATH``
64+
and from the ``CUDA_HOME``/``CUDA_PATH`` and ``ROCM_HOME``/``ROCM_PATH``
65+
toolkit homes, so an env-specified CUDA install is honored even when ``nvcc``
66+
is off ``PATH``. The mere existence of ``/opt/rocm`` is not treated as
67+
evidence of HIP.
4868
"""
49-
# Check environment variable override first
5069
backend = os.environ.get("TVM_FFI_GPU_BACKEND", "").lower()
5170
if backend in ("cuda", "hip"):
5271
return backend # type: ignore[return-value]
5372
try:
54-
_find_rocm_home()
73+
import torch # noqa: PLC0415
74+
75+
version = getattr(torch, "version", None)
76+
if getattr(version, "hip", None):
77+
return "hip"
78+
if getattr(version, "cuda", None):
79+
return "cuda"
80+
except Exception:
81+
pass
82+
if _find_compiler("nvcc", "CUDA_HOME", "CUDA_PATH") is None and (
83+
_find_compiler("hipcc", "ROCM_HOME", "ROCM_PATH") is not None
84+
):
5585
return "hip"
56-
except RuntimeError:
57-
return "cuda"
86+
return "cuda"
5887

5988

6089
def _resolve_gpu_backend(backend: str | None) -> BACKEND_STR:

0 commit comments

Comments
 (0)