Skip to content

[FIX] Resolve JIT GPU backend by explicit signals, not stray /opt/rocm#652

Merged
tqchen merged 3 commits into
apache:mainfrom
tqchen:tvm-ffi-jit-cuda-detection
Jul 5, 2026
Merged

[FIX] Resolve JIT GPU backend by explicit signals, not stray /opt/rocm#652
tqchen merged 3 commits into
apache:mainfrom
tqchen:tvm-ffi-jit-cuda-detection

Conversation

@tqchen

@tqchen tqchen commented Jul 4, 2026

Copy link
Copy Markdown
Member

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.

tqchen added 2 commits July 4, 2026 23:37
The JIT extension backend selector decided HIP-vs-CUDA solely by probing for a
ROCm home, whose default guess succeeds whenever /opt/rocm merely exists. On a
CUDA/NVIDIA host that carries a stray /opt/rocm directory this misclassified the
backend as HIP even when nvcc was present and hipcc was not, breaking CUDA
source builds.

Replace that heuristic with an ordered, evidence-based resolver:

1. honor the explicit TVM_FFI_GPU_BACKEND override;
2. use PyTorch build signals (torch.version.hip / torch.version.cuda) when
   PyTorch is importable and conclusive;
3. fall back to an available nvcc (CUDA);
4. then an available hipcc (HIP);
5. otherwise raise a clear error naming the override and required compilers.

Backend selection no longer consults the ROCm home directory, so a stray
/opt/rocm can never force 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.
Inline the PyTorch build-signal check directly in _detect_gpu_backend and
drop the separate helper. When no override, Torch signal, or compiler
evidence resolves the backend, default to CUDA instead of raising, and
select HIP only from an available hipcc without nvcc. A stray /opt/rocm
still never forces HIP.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the GPU backend auto-detection logic in _detect_gpu_backend to resolve the backend using PyTorch build signals and compiler presence (nvcc and hipcc) rather than relying on the existence of /opt/rocm. The review feedback suggests improving the robustness of the detection by checking for nvcc and hipcc in directories specified by CUDA_HOME/CUDA_PATH and ROCM_HOME/ROCM_PATH when they are not in the system PATH, and safely accessing torch.version to avoid potential AttributeErrors.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/tvm_ffi/cpp/extension.py Outdated
Comment thread python/tvm_ffi/cpp/extension.py
Resolve nvcc/hipcc from PATH and from the CUDA_HOME/CUDA_PATH and
ROCM_HOME/ROCM_PATH toolkit homes, so a stray hipcc on PATH no longer wins
when an env-specified CUDA install is present but nvcc is off PATH. Guard the
torch.version lookup with getattr so a minimal or mocked torch cannot raise.
@tqchen tqchen merged commit 95ea3a0 into apache:main Jul 5, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] TVM-FFI incorrectly detects ROCm on CUDA systems if /opt/rocm exists, causing JIT compilation to use hipcc instead of nvcc

2 participants