Commit 2362236
feat(pt_expt): pluggable NeighborList strategy + O(N) vesin neighbor list for Python/ASE inference (deepmodeling#5491)
## Motivation
When deepmd consumes a neighbor list, `forward_common`/`call_common`
extends the local region into ~26 periodic-image buffer regions —
`extend_coord_with_ghosts` then a dense `build_neighbor_list` —
materializing ≈27×N ghost atoms and an O(N²) `[N, 27N]` distance matrix.
This is the Python/ASE front-end bottleneck at large N (DPA4 manuscript
§2.4).
## What this PR does
Makes neighbor-list construction **pluggable** via an optional
`NeighborList` strategy injected at `forward_common`/`call_common` (the
layer where the system is extended). The exported `forward_common_lower`
(the `.pt2`/AOTI/C++ entry) is left untouched, so there is **zero export
risk**.
- **dpmodel (torch-free core)** — `NeighborList` base +
`DefaultNeighborList` (the historical dense extend+build).
`neighbor_list=None` reproduces today's behavior **byte-identically**.
- **pt_expt** — `VesinNeighborList`, a device-aware
[`vesin.torch`](https://github.com/Luthaf/vesin) O(N) cell list: it runs
on the input tensor's device (CPU or CUDA) for torch, and is CPU-bridged
for numpy/dpmodel. It builds an `(i, j, S)` edge list, materializes only
the real-neighbor ghosts `coord[j] + S@box`, and emits the same extended
quartet `(extended_coord, extended_atype, nlist, mapping)`. Because the
representation is identical, force / global-virial / **atomic-virial**
all come out of the existing autograd + `communicate_extended_output`
routines unchanged.
- **inference** — `nlist_backend="auto" | "vesin" | "native"` on the
pt_expt `DeepEval` and the ASE `DP` calculator. `auto` uses vesin when
available/applicable and silently falls back to native otherwise;
`vesin` is strict (raises if unavailable, or for spin /
ASE-`neighbor_list` conflicts); `native` forces the dense builder.
- **pyproject** — depends on `vesin[torch]`.
## Verification
native vs vesin agree to fp round-off (energy `0.0`,
force/virial/atomic-virial ≤ ~1e-18, the only difference being
ghost-enumeration order):
- `source/tests/pt_expt/utils/test_neighbor_list.py` — builder
equivalence (numpy + torch namespaces, PBC/non-PBC, input-device
placement) and full **model** equivalence across 8 descriptor families
(se_e2_a, se_r, se_e3, dpa1, se_atten_v2, dpa2, dpa3, hybrid) for
dpmodel (energy/atomic-energy) and pt_expt
(energy/force/virial/atomic-virial), plus the `neighbor_list=None`
byte-identical fallback.
-
`source/tests/pt_expt/infer/test_deep_eval.py::TestDeepEvalNlistBackend`
— `nlist_backend` dispatch validation and vesin-vs-native equality
through the compiled `.pte`.
## Known limitations
- **Python `forward_common` path only.** This is the path where deepmd
builds the nlist (DeepPot / ASE). C/C++/LAMMPS enter at the exported
`forward_lower` with an externally-supplied list — accepting `(i,j,S)`
there is a planned follow-up.
- **Energy model validated;** spin is gated off vesin;
dipole/polar/dos/hessian/multi-task and fparam/aparam are not yet
covered by vesin equivalence tests (the seam is model-agnostic and the
dense default path is regression-tested).
- **Still materializes O(surface) ghost coords** (the minimal extended
array); a truly buffer-free / sparse-edge-list consumption is deferred.
In dense descriptors the env-mat per-edge tensors dominate memory
anyway.
- **No real-GPU CI** (the device test runs on CPU); ghosts are not
deduped (one per `S≠0` edge — correctness preserved via `mapping`
summation).
- Multi-frame vesin build and the neighbor-truncation (`sel`-exceeded)
path are not yet directly tested.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Pluggable neighbor-list backend for calculators and evaluators: "auto"
(default), "native", or "vesin"; preserves historical all‑pairs behavior
when None.
* Adds a Vesin-based O(N) neighbor-list option for faster neighbor
construction and an explicit Default (all‑pairs) builder.
* **Tests**
* Comprehensive test suites validating backend selection, error
handling, device/shape robustness, and numeric equivalence (native vs
vesin), including multi-frame cases.
* **Chores**
* Adds vesin[torch] runtime entry for the new backend.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>1 parent b77223e commit 2362236
14 files changed
Lines changed: 1381 additions & 34 deletions
File tree
- deepmd
- dpmodel
- model
- utils
- pt_expt
- infer
- model
- utils
- pt/infer
- source/tests
- pt_expt
- infer
- utils
- pt/model
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| 94 | + | |
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| 102 | + | |
101 | 103 | | |
102 | 104 | | |
103 | 105 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| |||
88 | 91 | | |
89 | 92 | | |
90 | 93 | | |
| 94 | + | |
91 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
92 | 111 | | |
93 | 112 | | |
94 | 113 | | |
| |||
97 | 116 | | |
98 | 117 | | |
99 | 118 | | |
| 119 | + | |
100 | 120 | | |
101 | 121 | | |
102 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
42 | | - | |
| 41 | + | |
| 42 | + | |
43 | 43 | | |
44 | | - | |
45 | 44 | | |
46 | 45 | | |
47 | 46 | | |
| |||
78 | 77 | | |
79 | 78 | | |
80 | 79 | | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
99 | 105 | | |
100 | 106 | | |
101 | 107 | | |
| |||
107 | 113 | | |
108 | 114 | | |
109 | 115 | | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
130 | 119 | | |
131 | 120 | | |
132 | 121 | | |
| |||
269 | 258 | | |
270 | 259 | | |
271 | 260 | | |
| 261 | + | |
272 | 262 | | |
273 | 263 | | |
274 | 264 | | |
| |||
290 | 280 | | |
291 | 281 | | |
292 | 282 | | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
293 | 288 | | |
294 | 289 | | |
295 | 290 | | |
| |||
316 | 311 | | |
317 | 312 | | |
318 | 313 | | |
| 314 | + | |
319 | 315 | | |
320 | 316 | | |
321 | 317 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
2 | 5 | | |
3 | 6 | | |
4 | 7 | | |
| |||
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
18 | 24 | | |
19 | 25 | | |
20 | 26 | | |
| |||
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
| 62 | + | |
56 | 63 | | |
57 | 64 | | |
58 | 65 | | |
| |||
62 | 69 | | |
63 | 70 | | |
64 | 71 | | |
| 72 | + | |
65 | 73 | | |
66 | 74 | | |
67 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
0 commit comments