Commit f73de32
feat(dpmodel): NeighborGraph 3-body angle machinery (PR-E) (deepmodeling#5717)
## Summary
NeighborGraph PR-E: the optional 3-body **angle** extension of the
edge-graph neighbor-list contract (design discussion
wanghan-iapcm#4). An angle is a pair of edges sharing a
center (`dst(edge_a) == dst(edge_b)`), stored as `angle_index (2, A)`
into `[0, E)` + `angle_mask (A,)` — `edge_vec` stays the ONLY geometry
leaf, so force/virial assembly is untouched (proven by an invariance
test).
> **Stacked on deepmodeling#5715 (PR-D)** — reuses its `center_edge_pairs`. Please
merge deepmodeling#5715 first; this branch then rebases clean onto master. Only the
last 10 commits belong to this PR.
### What's added (all in `deepmd/dpmodel/utils/neighbor_graph/`)
- `pad_and_guard_angles` (graph.py) — angle-axis padder mirroring
`pad_and_guard_edges` (dynamic guard append / static `angle_capacity`
with overflow ValueError).
- `angles.py` (new):
- `build_angle_index(edge_index, edge_vec, edge_mask, n_total, a_rcut,
*, ordered=False, include_self=False, layout=None)` — unordered, no-self
pairs of edges sharing a center where BOTH edges are within `a_rcut`;
built on PR-D's `center_edge_pairs`; `pair_mask` folded into
`angle_mask` (never discarded).
- `attach_angles(graph, a_rcut, ...)` — post-hoc: edge graph in, graph
with angle fields out (`dataclasses.replace`); default builders keep
angles `None`.
- `angle_to_edge_sum` / `angle_to_node_sum` — segment-sum aggregation to
the query edge / shared center.
- `graph_angle_cos(angle_index, edge_vec, eps=1e-6)` — per-angle cos θ
mirroring dpa3 repflows `cosine_ij` eps placement exactly (`+eps` in
norm denominators, `*(1-eps)` on the product).
- `angle_padding_fraction(graph)` — mask-derived padding-waste report
for static capacities.
### Semantics / decisions
- **Unordered, no-self by default**: dpa3's dense angle tensor is the
redundant ordered `a_sel x a_sel` square including the `j==k` diagonal;
the graph set keeps one entry per unordered `{j,k}` pair and moves the
degenerate diagonal to the (a_rcut-filtered) edge channel. Dense parity
is therefore asserted against the OFF-DIAGONAL `cosine_ij[j,k]` (j != k)
at rtol/atol **1e-12** (same-math fp64), at non-binding `a_sel`. The
ordered+self full square stays available via flags.
- **`a_sel` = normalization-only** (carry-all within `a_rcut`),
consistent with the edge-`sel` decision.
- Second oracle: se_t dot-product convention cross-checked from
coordinates in the `sw == 1` regime (rtol 1e-12).
### Tests
`source/tests/common/dpmodel/test_angle_builder.py` (21) +
`test_graph_angle_cos_parity.py` (6): brute-force triplet oracle (all
flag combinations, multi-center, static layout, `node_capacity` branch),
dpa3 dense-parity + no-self-angle assertion, se_t coordinate oracle,
force/virial bit-exact invariance with/without angles, padding-fraction
(incl. `total==0`), torch-namespace smoke tests for every new function.
Full neighbor-graph suite: 54 passed.
### Known limitations
- **Machinery + angle-channel math only** — no dpa3 graph descriptor
here (dpa3 is message-passing; wiring = PR-G). se_t/se_t_tebd are not
migrated (`mixed_types=False`), used as oracle only.
- Angle enumeration is the compact **eager** form (`nonzero` in
`center_edge_pairs`) even when a static `layout` is passed —
`angle_capacity` fixes the output shape only; shape-static enumeration
for export is deferred to PR-G.
- Not bit-parity with dense dpa3 **by construction** (unordered/no-self
reformulation; recoverable in PR-G via symmetric angle→edge 2x +
edge-channel diagonal).
- Aggregation helpers follow the mask-then-reduce convention: callers
mask per-angle data by `angle_mask` before summing (padding angles point
at edge 0).
- numpy/torch validated; jax rides the array-API surface (no
jax-specific test here); `A ~ sum(deg^2)` capacity overhead mitigated by
`a_rcut < rcut` and reported by `angle_padding_fraction`.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added angle-graph utilities to build and attach 3-body angle
relationships on top of neighbor graphs.
* Introduced angle padding/guarding for fixed-capacity layouts.
* Added helpers to compute angle cosine values, reduce angle data back
to edges/nodes, and report angle padding coverage.
* Expanded publicly available exports for angle/edge-pair utilities and
additional segment reductions (max/softmax).
* **Tests**
* Added extensive unit tests covering index building, attachment,
masking/aggregation correctness, padding behavior, and cosine parity
across NumPy/Torch.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent 0acd0e5 commit f73de32
5 files changed
Lines changed: 1276 additions & 0 deletions
File tree
- deepmd/dpmodel/utils/neighbor_graph
- source/tests/common/dpmodel
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
12 | 20 | | |
13 | 21 | | |
14 | 22 | | |
| |||
30 | 38 | | |
31 | 39 | | |
32 | 40 | | |
| 41 | + | |
33 | 42 | | |
34 | 43 | | |
35 | 44 | | |
| |||
45 | 54 | | |
46 | 55 | | |
47 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
48 | 62 | | |
49 | 63 | | |
50 | 64 | | |
51 | 65 | | |
52 | 66 | | |
53 | 67 | | |
54 | 68 | | |
| 69 | + | |
55 | 70 | | |
56 | 71 | | |
| 72 | + | |
57 | 73 | | |
58 | 74 | | |
59 | 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 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
126 | 177 | | |
127 | 178 | | |
128 | 179 | | |
| |||
0 commit comments