Skip to content

Commit 157e3cc

Browse files
committed
alchemiops.py: Flag as unavailable on non-NVIDIA torch builds
* torch_sim/neighbors/alchemiops.py (_import_nvalchemiops_batch_neighbors): Check that we are really using NVIDIA's drivers when checking if nvalchemiops can be used. * tests/test_neighbors.py (test_alchemiops_import_guard_non_nvidia_builds): New test. On ROCm builds, nvalchemiops correctly imports but does not work during runtime, yielding File "<...>/lib/python3.12/site-packages/torch_sim/neighbors/__init__.py", line 86, in torchsim_nl return alchemiops_nl_n2( ^^^^^^^^^^^^^^^^^ File "<...>/lib/python3.12/site-packages/torch_sim/neighbors/alchemiops.py", line 62, in alchemiops_nl_n2 res = _batch_naive_neighbor_list( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<...>/lib/python3.12/site-packages/nvalchemiops/torch/neighbors/batch_naive.py", line 1513, in batch_naive_neighbor_list compute_naive_num_shifts(cell, cutoff, pbc) File "<...>/lib/python3.12/site-packages/nvalchemiops/torch/neighbors/neighbor_utils.py", line 326, in compute_naive_num_shifts wp_device = wp.device_from_torch(device) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<...>/lib/python3.12/site-packages/warp/_src/torch.py", line 39, in device_from_torch return warp._src.context.runtime.cuda_devices[torch_device.index] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^ IndexError: list index out of range
1 parent 17c14a0 commit 157e3cc

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

tests/test_neighbors.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,27 @@ def test_fallback_when_alchemiops_unavailable(monkeypatch: pytest.MonkeyPatch) -
528528
assert mapping2.shape[1] > 0
529529

530530

531+
def test_alchemiops_import_guard_non_nvidia_builds(
532+
monkeypatch: pytest.MonkeyPatch,
533+
) -> None:
534+
"""nvalchemiops is flagged unavailable on non-NVIDIA torch builds.
535+
536+
On ROCm builds ``torch.version.cuda`` is ``None`` even though a GPU is
537+
present, and on CPU-only builds ``torch.cuda.is_available()`` is ``False``.
538+
Both cases must short-circuit to ``None`` without attempting the import.
539+
"""
540+
from torch_sim.neighbors.alchemiops import _import_nvalchemiops_batch_neighbors
541+
542+
# Simulate a ROCm build: HIP is available, but there is no CUDA runtime.
543+
monkeypatch.setattr(torch.version, "cuda", None)
544+
assert _import_nvalchemiops_batch_neighbors() is None
545+
546+
# Simulate a CUDA build without an available GPU.
547+
monkeypatch.setattr(torch.version, "cuda", "12.4")
548+
monkeypatch.setattr(torch.cuda, "is_available", lambda: False)
549+
assert _import_nvalchemiops_batch_neighbors() is None
550+
551+
531552
@pytest.mark.skipif(not torch.cuda.is_available(), reason="GPU not available for testing")
532553
def test_torchsim_nl_gpu() -> None:
533554
"""Test that torchsim_nl works on GPU (CUDA/ROCm)."""

torch_sim/neighbors/alchemiops.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
def _import_nvalchemiops_batch_neighbors() -> tuple[object, object] | None:
1919
"""Return ``(batch_cell_list, batch_naive_neighbor_list)`` if importable."""
20+
# nvalchemiops is NVIDIA-CUDA-only (built on warp)
21+
# It does not work on non-NVIDIA builds. In particular, on ROCm
22+
# builds, nvalchemiops will fail during runtime, even though
23+
# Python bindings are imported without issues.
24+
if torch.version.cuda is None or not torch.cuda.is_available():
25+
return None
2026
try:
2127
from nvalchemiops.torch.neighbors import batch_cell_list as bcl
2228
from nvalchemiops.torch.neighbors import batch_naive_neighbor_list as bnl

0 commit comments

Comments
 (0)