Commit 034e613
feat(pt_expt): add dp finetune support (#5331)
## Summary
- Add `--finetune`, `--model-branch`, and `--use-pretrain-script`
support to `dp --pt-expt train`, mirroring the pt backend's finetune
flow (load pretrained checkpoint, change type map, selective weight
copy, output bias adjustment)
- Support finetuning from both `.pt` checkpoints and frozen `.pte`
models (embed `model_params` in `.pte` during freeze for
`--use-pretrain-script`)
- Fix a bug in dpmodel's `base_atomic_model.change_type_map` where
`out_bias`/`out_std` were not extended before remapping when the new
type map introduces unseen types, causing `IndexError` with negative
remap indices
## Usage examples
```bash
# Finetune from a .pt checkpoint
dp --pt-expt train input.json --finetune pretrained.pt
# Finetune from a frozen .pte model
dp --pt-expt train input.json --finetune pretrained.pte
# Copy descriptor/fitting config from pretrained model
dp --pt-expt train input.json --finetune pretrained.pt --use-pretrain-script
# Finetune from a multi-task pretrained model (select a branch)
dp --pt-expt train input.json --finetune pretrained.pt --model-branch Default
# Re-initialize fitting net randomly (only keep descriptor weights)
dp --pt-expt train input.json --finetune pretrained.pt --model-branch RANDOM
```
## Files changed
| File | Change |
|------|--------|
| `deepmd/pt_expt/utils/finetune.py` | **New** — `get_finetune_rules()`
for pt_expt, supports `.pt` and `.pte` |
| `deepmd/pt_expt/entrypoints/main.py` | Wire
`--finetune`/`--model-branch`/`--use-pretrain-script` through `train()`
→ `get_trainer()` → `Trainer`; pass `model_params` to `.pte` during
freeze |
| `deepmd/pt_expt/train/training.py` | Finetune weight loading in
`Trainer.__init__` (`.pt` and `.pte`); `model_change_out_bias()` |
| `deepmd/pt_expt/utils/serialization.py` | Embed/extract
`model_params.json` in `.pte` archive |
| `deepmd/dpmodel/atomic_model/base_atomic_model.py` | Fix
`change_type_map` to extend `out_bias`/`out_std` for new types
(array-api compatible) |
| `source/tests/pt_expt/test_finetune.py` | **New** — 9 tests covering
bias adjustment, type map change, CLI dispatch, `.pte` finetune,
`--use-pretrain-script`, `random_fitting`, inherited weight consistency
|
| `source/tests/consistent/model/test_ener.py` | Add
`test_change_type_map_new_type` verifying `out_bias`/`out_std` extension
across dp, pt, pt_expt |
## Test plan
- [x] `python -m pytest source/tests/pt_expt/test_finetune.py -v` (9
passed)
- [x] `python -m pytest source/tests/pt_expt/test_training.py -v` (11
passed, no regression)
- [x] `python -m pytest source/tests/consistent/model/test_ener.py -k
change_type_map -v` (3 passed)
- [x] `python -m pytest
source/tests/consistent/descriptor/test_se_e2_a.py -v` (351 passed, no
regression)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Fine-tuning workflow: supply pretrained checkpoints, select branch,
and toggle pretrain-script behavior
* Automatic expansion of atom type maps (new types get zero bias and
unit std) while preserving existing mappings
* Improved finetune resume: selective merging of pretrained
descriptor/fitting weights and bias-adjustment modes
* Export/import embeds/restores model metadata to/from artifacts
* **Tests**
* Unit and end-to-end tests for finetuning, bias adjustment, type-map
expansion, and frozen-artifact scenarios
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>1 parent 6122d97 commit 034e613
File tree
7 files changed
+1357
-20
lines changed- deepmd
- dpmodel/atomic_model
- pt_expt
- entrypoints
- train
- utils
- source/tests
- consistent/model
- pt_expt
7 files changed
+1357
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
209 | 223 | | |
210 | 224 | | |
211 | 225 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| |||
94 | 96 | | |
95 | 97 | | |
96 | 98 | | |
| 99 | + | |
| 100 | + | |
97 | 101 | | |
98 | 102 | | |
99 | 103 | | |
| |||
102 | 106 | | |
103 | 107 | | |
104 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
105 | 112 | | |
106 | 113 | | |
107 | 114 | | |
| |||
115 | 122 | | |
116 | 123 | | |
117 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
118 | 131 | | |
119 | 132 | | |
120 | 133 | | |
121 | 134 | | |
122 | 135 | | |
| 136 | + | |
| 137 | + | |
123 | 138 | | |
124 | 139 | | |
125 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
126 | 144 | | |
127 | 145 | | |
128 | 146 | | |
| |||
133 | 151 | | |
134 | 152 | | |
135 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
136 | 175 | | |
137 | 176 | | |
138 | 177 | | |
| |||
156 | 195 | | |
157 | 196 | | |
158 | 197 | | |
159 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
160 | 205 | | |
161 | 206 | | |
162 | 207 | | |
| |||
214 | 259 | | |
215 | 260 | | |
216 | 261 | | |
217 | | - | |
| 262 | + | |
218 | 263 | | |
219 | 264 | | |
220 | 265 | | |
| |||
250 | 295 | | |
251 | 296 | | |
252 | 297 | | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
253 | 301 | | |
254 | 302 | | |
255 | 303 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
| |||
380 | 383 | | |
381 | 384 | | |
382 | 385 | | |
| 386 | + | |
| 387 | + | |
383 | 388 | | |
384 | | - | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
385 | 396 | | |
386 | 397 | | |
387 | 398 | | |
| |||
429 | 440 | | |
430 | 441 | | |
431 | 442 | | |
432 | | - | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
433 | 449 | | |
434 | 450 | | |
435 | 451 | | |
| |||
472 | 488 | | |
473 | 489 | | |
474 | 490 | | |
475 | | - | |
476 | | - | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
481 | 578 | | |
482 | | - | |
483 | 579 | | |
484 | | - | |
| 580 | + | |
| 581 | + | |
485 | 582 | | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | 583 | | |
493 | 584 | | |
494 | 585 | | |
| |||
910 | 1001 | | |
911 | 1002 | | |
912 | 1003 | | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
0 commit comments