Commit 0acd0e5
fix: make global DOS inference work on the dpmodel and JAX backends (deepmodeling#5722)
## Problem
Fixes deepmodeling#5674. Three related defects in global DOS inference, uncovered in
sequence:
1. `DeepDOS.eval` unconditionally read the atomic `dos` output and
summed it, even for `atomic=False`. The dpmodel and JAX backends only
return the atomic output when `atomic=True`; on the global-DOS-only path
(e.g. `dp test` without atomic DOS labels) `results["dos"]` raised
`KeyError`. TF and PyTorch masked this because they always include the
atomic output.
2. Fixing the `KeyError` exposed that dpmodel and JAX DOS inference was
broken more deeply: both `DeepEval.get_numb_dos` implementations
hard-returned `0`, so the DOS reshape target was `(nframes, 0)` and
inference failed on every path, not just the missing-key case.
3. On TF, the global DOS did not equal the sum of the atomic DOS for
multi-frame inputs — even though, by construction of the model, it must.
`deepmd/tf/model/dos.py` reduced the atomic DOS with
`reshape([natoms[0], -1])` + `reduce_sum(axis=0)`, which sums across the
wrong axis and mixes atoms from different frames together. Single-frame
inputs happened to give the right answer, so no test caught it.
## Fix
Backend-agnostic (`deep_dos.py`): prefer the atomic `dos` output and sum
it whenever the backend returns it (this is the exact global DOS on
TF/PT, whose reduced output is not necessarily the plain sum), and fall
back to the reduced `dos_redu` only when the atomic output is absent
(dpmodel/JAX at `atomic=False`). Reading `dos` unconditionally is what
raised the original `KeyError`.
dpmodel: add `get_numb_dos` to the dpmodel `DOSModel` (mirroring the
PyTorch model), add a default `get_numb_dos` returning 0 on the shared
base model so non-DOS models can still be serialized, and delegate
`dpmodel/infer/deep_eval.py:get_numb_dos` to the model.
JAX: the evaluator wraps a deserialized `HLO` object with no live model,
so `numb_dos` is now persisted into the StableHLO export constants and
exposed via `HLO.get_numb_dos`; the `dos` output is registered in the
HLO `OUTPUT_DEFS` table; and `jax/infer/deep_eval.py:get_numb_dos`
delegates to the model. With these, JAX DOS inference works end to end.
TF: reduce the atomic DOS per frame — `reshape([-1, natoms[0],
numb_dos])` + `reduce_sum(axis=1)`, mirroring the energy model — so the
global DOS equals the atomic sum for multi-frame inputs.
## Test
- `source/tests/common/dpmodel/test_deep_dos.py`: builds a dpmodel DOS
model and evaluates it — `atomic=False` returns the global DOS
(`KeyError` on master), and the global DOS equals the sum of the atomic
DOS (guarding the `dos_redu == sum(dos)` invariant relied on by all
backends).
- `source/tests/jax/test_deep_dos.py`: exports a DOS model to `.hlo`,
checks `numb_dos` survives the round trip, and evaluates the global DOS.
- `source/tests/tf/test_model_dos.py`: adds
`test_multiframe_global_equals_atomic_sum`, which builds a two-frame DOS
graph and asserts the global DOS equals the per-frame atomic sum — this
fails on the old axis-0 reduction and passes after the per-frame fix.
The existing single-frame assertions were updated to the corrected
output shapes.
dpmodel and JAX DOS inference previously had no test, and the TF path
had only single-frame coverage; DOS was effectively exercised only where
the bugs were masked.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Corrected DOS inference so global DOS is reported properly instead of
using a fixed default.
* Fixed multi-frame DOS aggregation to keep results separated by frame
and sum across atoms correctly.
* Improved consistency when using the model in different backends and
after export, so DOS output counts are preserved.
* **New Features**
* Added support for exposing DOS output counts in model inference and
export workflows.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>1 parent b255998 commit 0acd0e5
12 files changed
Lines changed: 1972 additions & 190 deletions
File tree
- deepmd
- dpmodel
- infer
- model
- infer
- jax
- infer
- model
- utils
- tf/model
- source/tests
- common/dpmodel
- infer
- jax
- tf
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
| 278 | + | |
278 | 279 | | |
279 | 280 | | |
| 281 | + | |
280 | 282 | | |
281 | 283 | | |
| 284 | + | |
282 | 285 | | |
283 | 286 | | |
| 287 | + | |
284 | 288 | | |
285 | 289 | | |
| 290 | + | |
286 | 291 | | |
287 | 292 | | |
| 293 | + | |
288 | 294 | | |
289 | 295 | | |
290 | 296 | | |
| 297 | + | |
291 | 298 | | |
292 | 299 | | |
293 | 300 | | |
| |||
296 | 303 | | |
297 | 304 | | |
298 | 305 | | |
| 306 | + | |
299 | 307 | | |
300 | 308 | | |
301 | 309 | | |
| |||
1000 | 1008 | | |
1001 | 1009 | | |
1002 | 1010 | | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
1003 | 1015 | | |
1004 | 1016 | | |
1005 | 1017 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
| 127 | + | |
132 | 128 | | |
| 129 | + | |
133 | 130 | | |
134 | 131 | | |
135 | 132 | | |
136 | 133 | | |
137 | | - | |
138 | | - | |
| 134 | + | |
139 | 135 | | |
140 | 136 | | |
141 | 137 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
180 | | - | |
| 180 | + | |
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
33 | 40 | | |
34 | 41 | | |
35 | 42 | | |
| |||
61 | 68 | | |
62 | 69 | | |
63 | 70 | | |
| 71 | + | |
64 | 72 | | |
65 | 73 | | |
66 | 74 | | |
| |||
84 | 92 | | |
85 | 93 | | |
86 | 94 | | |
| 95 | + | |
87 | 96 | | |
88 | 97 | | |
89 | 98 | | |
| |||
212 | 221 | | |
213 | 222 | | |
214 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
215 | 228 | | |
216 | 229 | | |
217 | 230 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
310 | 310 | | |
311 | 311 | | |
312 | 312 | | |
| 313 | + | |
313 | 314 | | |
314 | 315 | | |
315 | 316 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
217 | | - | |
218 | | - | |
219 | | - | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
220 | 224 | | |
221 | | - | |
| 225 | + | |
222 | 226 | | |
223 | 227 | | |
224 | 228 | | |
| |||
| 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 | + | |
0 commit comments