Commit dd8e492
refactor(pt_expt): use model API for inference, consistent file naming (#5354)
## Summary
### Problem
Two inconsistencies in `.pt2`/`.pte` files:
1. **Python reads a flat metadata dict instead of using the model API.**
Other backends (`.dp`/`.yaml`, `.pth`) deserialize the model and query
it directly for `get_rcut()`, `get_sel()`, `model_output_type()`, etc.
The `.pt2`/`.pte` backend was reading these from a metadata dict stored
at export time, duplicating model logic.
2. **Inconsistent file naming.** `model_def_script.json` stored C++
runtime metadata in `.pt2`/`.pte`, but training config in `.pth`.
Training config lived separately in `model_params.json`.
`output_keys.json` was a standalone file that logically belongs with
metadata.
### Solution
**Python inference**: `DeepEval` now deserializes `model.json` into a
dpmodel instance (`self._dpmodel`) at load time and delegates all API
calls to it. `_reconstruct_model_output_def()` is removed.
**File layout renamed** so that each filename means the same thing
across `.pth` and `.pt2`/`.pte`:
| File | Before | After |
|------|--------|-------|
| `model_def_script.json` | C++ metadata | **Training config** (matches
`.pth`) |
| `metadata.json` | *(did not exist)* | **C++ metadata** + output_keys |
| `model_params.json` | Training config | **Removed** |
| `output_keys.json` | Output key list | **Removed** (merged into
`metadata.json`) |
| `model.json` | Full serialized model | No change |
**C++ inference** (`DeepPotPTExpt.cc`): Updated to read
`extra/metadata.json` instead of `extra/model_def_script.json`, and
reads `output_keys` from the metadata dict instead of a separate
`output_keys.json`.
**Why `metadata.json` still exists**: C++ inference cannot deserialize
`model.json` to call model API methods. The alternative — compiling
methods like `get_rcut()`, `get_sel()` as additional AOTInductor entry
points — was benchmarked and rejected:
- **Compilation overhead**: ~12s per trivial constant-returning function
(C++ codegen + compile + link). With ~8 methods, that adds ~1.5 min to
freeze time.
- **String outputs**: `get_type_map()` returns strings. `torch.export`
only supports tensor I/O — encoding strings as int tensors adds
complexity for no benefit.
- **These are constants**: `rcut`, `sel`, `type_map` never change after
export. A flat JSON file is the simplest and fastest solution.
### Other changes
- `compress` and `change_bias` entrypoints now preserve training config
through `.pte`/`.pt2` round-trips
- `.gitignore` updated to exclude `.pte`/`.pt2` model files
- `_collect_metadata()` drops `model_output_type` and `sel_type` (not
used by C++; Python now gets them from the model)
## Test plan
- [x] `source/tests/pt_expt/infer/test_deep_eval.py` — 36/36 pass
(`.pte` + `.pt2`)
- New: `test_model_api_delegation`,
`test_get_model_def_script_with_params`
- Updated: `test_get_model_def_script`, `test_pt2_has_metadata`,
`test_dynamic_shapes`
- [x] `source/tests/pt_expt/model/` — 50/50 pass (frozen, compression,
serialization)
- [x] `source/tests/pt_expt/test_change_bias.py` — new tests for
`.pte`/`.pt2` model_def_script preservation
- [x] C++ tests — 3/3 suites pass (`.pt2` models regenerated with new
`metadata.json`)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Preserve and restore training configuration when freezing or modifying
frozen models; clearer messages when training config is absent.
* **Refactor**
* Consolidated metadata layout inside exported model archives for more
consistent loading across formats and runtimes.
* **Tests**
* Added and updated tests to validate config preservation, metadata
delegation, and archive contents.
* **Chores**
* Extended ignore patterns to skip additional model file extensions.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>1 parent bf3e483 commit dd8e492
File tree
9 files changed
+242
-111
lines changed- deepmd
- entrypoints
- pt_expt
- entrypoints
- infer
- utils
- source
- api_cc/src
- tests/pt_expt
- infer
9 files changed
+242
-111
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
67 | | - | |
| 70 | + | |
68 | 71 | | |
69 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
70 | 78 | | |
71 | 79 | | |
72 | 80 | | |
| |||
75 | 83 | | |
76 | 84 | | |
77 | 85 | | |
78 | | - | |
| 86 | + | |
79 | 87 | | |
80 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
81 | 94 | | |
82 | 95 | | |
83 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
259 | 259 | | |
260 | 260 | | |
261 | 261 | | |
262 | | - | |
| 262 | + | |
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
| |||
344 | 344 | | |
345 | 345 | | |
346 | 346 | | |
347 | | - | |
| 347 | + | |
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
| |||
440 | 440 | | |
441 | 441 | | |
442 | 442 | | |
443 | | - | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
444 | 446 | | |
445 | 447 | | |
446 | 448 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | 19 | | |
21 | 20 | | |
22 | 21 | | |
| |||
59 | 58 | | |
60 | 59 | | |
61 | 60 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | 61 | | |
85 | 62 | | |
86 | 63 | | |
| |||
124 | 101 | | |
125 | 102 | | |
126 | 103 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | 104 | | |
131 | 105 | | |
132 | 106 | | |
| |||
139 | 113 | | |
140 | 114 | | |
141 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
142 | 132 | | |
143 | 133 | | |
144 | | - | |
| 134 | + | |
145 | 135 | | |
146 | 136 | | |
147 | | - | |
148 | | - | |
149 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
150 | 140 | | |
151 | 141 | | |
152 | 142 | | |
| |||
159 | 149 | | |
160 | 150 | | |
161 | 151 | | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
169 | 160 | | |
170 | | - | |
171 | | - | |
| 161 | + | |
| 162 | + | |
172 | 163 | | |
173 | 164 | | |
174 | 165 | | |
| |||
189 | 180 | | |
190 | 181 | | |
191 | 182 | | |
192 | | - | |
| 183 | + | |
193 | 184 | | |
194 | 185 | | |
195 | 186 | | |
196 | | - | |
| 187 | + | |
197 | 188 | | |
198 | 189 | | |
199 | 190 | | |
200 | 191 | | |
201 | | - | |
| 192 | + | |
202 | 193 | | |
203 | 194 | | |
204 | 195 | | |
| |||
219 | 210 | | |
220 | 211 | | |
221 | 212 | | |
222 | | - | |
| 213 | + | |
223 | 214 | | |
224 | 215 | | |
225 | 216 | | |
| |||
364 | 355 | | |
365 | 356 | | |
366 | 357 | | |
367 | | - | |
368 | | - | |
| 358 | + | |
| 359 | + | |
369 | 360 | | |
370 | 361 | | |
371 | 362 | | |
| |||
476 | 467 | | |
477 | 468 | | |
478 | 469 | | |
479 | | - | |
480 | | - | |
| 470 | + | |
| 471 | + | |
481 | 472 | | |
482 | 473 | | |
483 | 474 | | |
| |||
703 | 694 | | |
704 | 695 | | |
705 | 696 | | |
706 | | - | |
707 | | - | |
| 697 | + | |
| 698 | + | |
708 | 699 | | |
709 | 700 | | |
710 | 701 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 36 | + | |
41 | 37 | | |
42 | 38 | | |
43 | 39 | | |
| |||
82 | 78 | | |
83 | 79 | | |
84 | 80 | | |
85 | | - | |
| 81 | + | |
86 | 82 | | |
87 | 83 | | |
88 | 84 | | |
| |||
0 commit comments