Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions deepmd/pt/model/model/sezm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,11 @@ def _select_neighbor_builder(nf: int, device: torch.device) -> NeighborList:
if is_vesin_torch_available():
return VesinNeighborList()
raise RuntimeError(
"SeZM neighbor-list construction requires either 'nvalchemiops' or "
"'vesin', but neither is importable."
"SeZM neighbor-list construction requires a neighbor-list backend, but "
"neither 'vesin' nor 'nvalchemiops' is importable. Install one of:\n"
" pip install 'vesin[torch]' # portable CPU/CUDA cell list\n"
" pip install nvalchemi-toolkit-ops # batched CUDA kernels\n"
"'vesin' also ships with the PyTorch extra: pip install 'deepmd-kit[torch]'."
)


Expand Down
49 changes: 49 additions & 0 deletions source/tests/pt/test_neighbor_backend_missing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
"""A missing neighbor-list backend must say what to install.

The DPA-4 / SeZM descriptor needs ``vesin`` or ``nvalchemiops`` at runtime.
Both ship with the ``torch`` extra, so a normal install has one, but an
install that skips that extra reaches a model build and then fails with a
message naming the *imports* rather than the distributions that provide
them. These tests pin that the message names installable packages.
"""

import unittest

import torch

from deepmd.pt.model.model import (
sezm_model,
)


class TestNeighborBackendMissing(unittest.TestCase):
def _message_without_backends(self) -> str:
"""The error raised when neither backend is importable."""
nv = sezm_model.is_nv_available
vesin = sezm_model.is_vesin_torch_available
sezm_model.is_nv_available = lambda: False
sezm_model.is_vesin_torch_available = lambda: False
try:
with self.assertRaises(RuntimeError) as caught:
sezm_model._select_neighbor_builder(1, torch.device("cpu"))
return str(caught.exception)
finally:
sezm_model.is_nv_available = nv
sezm_model.is_vesin_torch_available = vesin

def test_error_names_installable_packages(self) -> None:
"""The message must name pip-installable distributions, not imports."""
message = self._message_without_backends()
self.assertIn("pip install", message)
# the distribution names, which differ from the import names
self.assertIn("vesin[torch]", message)
self.assertIn("nvalchemi-toolkit-ops", message)

def test_error_points_at_the_extra(self) -> None:
"""A user who wants the whole descriptor should be told about it."""
self.assertIn("deepmd-kit[torch]", self._message_without_backends())


if __name__ == "__main__":
unittest.main()
Loading