Commit e97967b
feat(pt_expt): add dp change-bias support (#5330)
## Summary
Add `dp --pt-expt change-bias` command to adjust the output bias (energy
shift per atom type) of pt_expt models without retraining. This brings
the pt_expt backend to parity with pt/tf/pd backends for this feature.
### Supported input/output formats
| Input | Output | Notes |
|-------|--------|-------|
| `.pt` checkpoint | `.pt` checkpoint | Modify bias before freezing |
| `.pte` frozen model | `.pte` frozen model | Round-trip: deserialize →
modify bias → re-export |
### Bias modes
- **Data-based** (`-s <data_dir>` or `-f <data_file>`): compute new bias
from data via linear regression (`change-by-statistic` or
`set-by-statistic`)
- **User-defined** (`-b 0.1 3.2 ...`): set bias values directly
### Implementation details
**`deepmd/pt_expt/entrypoints/main.py`** — `change_bias()` function +
CLI dispatch:
- `.pt` input: `torch.load` → extract `model_params` from `_extra_state`
→ `get_model()` → `ModelWrapper.load_state_dict()` → apply bias →
`torch.save()`
- `.pte` input: `serialize_from_file()` → `BaseModel.deserialize()` →
apply bias → `model.serialize()` → `deserialize_to_file()`
- Data loading uses pt_expt's own pipeline: `DeepmdDataSystem` +
`make_stat_input` (numpy-based, from `deepmd.utils.model_stat`)
- When `numb_batch=0` (default), uses all available batches via
`max(data.get_nbatches())`
**`deepmd/pt_expt/train/training.py`** — `model_change_out_bias()`
helper:
- Logs old/new bias values after calling the dpmodel-inherited
`change_out_bias()`
- Simpler than pt's version: no `DPModelCommon`/`compute_input_stats`
check needed since pt_expt models inherit dpmodel's implementation
directly
### Usage
```bash
# Change bias using data (checkpoint)
dp --pt-expt change-bias model.ckpt.pt -s /path/to/data -o updated.pt
# Change bias using data file list
dp --pt-expt change-bias model.ckpt.pt -f systems.txt -o updated.pt
# Set bias to specific values
dp --pt-expt change-bias model.ckpt.pt -b 0.1 3.2 -o updated.pt
# Change bias on frozen model
dp --pt-expt change-bias frozen.pte -s /path/to/data -o updated.pte
```
## Test plan
- [x] `python -m pytest source/tests/pt_expt/test_change_bias.py -v` — 4
end-to-end CLI tests:
- `test_change_bias_with_data` — bias changes when using `-s` flag
- `test_change_bias_with_data_sys_file` — bias changes when using `-f`
flag
- `test_change_bias_with_user_defined` — exact match with user-specified
values
- `test_change_bias_frozen_pte` — freeze → change-bias on `.pte` →
verify bias changed
- [x] `python -m pytest source/tests/consistent/model/test_ener.py -k
test_change_out_bias -v` — cross-backend consistency passes
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added a `change-bias` command to adjust a model's output bias by
supplying values or computing statistics from systems; supports
checkpoint and frozen model formats while preserving model metadata.
* Added a model bias-update helper to apply/statistically derive bias
adjustments consistently.
* **Tests**
* End-to-end tests for data-driven, file-list, user-specified, and
frozen-model workflows.
* Added tests ensuring fitting-statistics are computed when expected and
improved fixtures to clear leaked device contexts.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>1 parent 97e9c9e commit e97967b
4 files changed
Lines changed: 537 additions & 3 deletions
File tree
- deepmd/pt_expt
- entrypoints
- train
- source/tests/pt_expt
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
266 | 447 | | |
267 | 448 | | |
268 | 449 | | |
| |||
323 | 504 | | |
324 | 505 | | |
325 | 506 | | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
326 | 518 | | |
327 | 519 | | |
328 | 520 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1029 | 1029 | | |
1030 | 1030 | | |
1031 | 1031 | | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
1032 | 1040 | | |
1033 | 1041 | | |
1034 | 1042 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
37 | | - | |
| 35 | + | |
| 36 | + | |
38 | 37 | | |
39 | 38 | | |
40 | 39 | | |
| |||
46 | 45 | | |
47 | 46 | | |
48 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
49 | 66 | | |
50 | 67 | | |
51 | 68 | | |
| |||
0 commit comments