-
Notifications
You must be signed in to change notification settings - Fork 582
Feat (pt): Expose Linear Ener Model #4194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wanghan-iapcm
merged 31 commits into
deepmodeling:devel
from
anyangml:feat/expose-linear-model
Oct 11, 2024
Merged
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
e906fac
fix: zbl mix type model
anyangml 91cf861
feat: add linear model
anyangml 7d3044c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 082ab74
fix: dftd3 example
anyangml 0104e18
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 21580e1
feat: add pt example
anyangml 08fcb55
fix: jit
anyangml 739670e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0522562
feat: add UTs
anyangml 8b1cb8c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f2753e7
fix: UTs
anyangml 63e7017
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0514eae
fix: UTs
anyangml f41df5b
fix: sel type UT
anyangml 000c1c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 189961c
fix: UT sel type dtype to long
anyangml 9715641
fix: revert dtype change
anyangml c8e86fe
fix: revert ut change
anyangml a935784
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 169203b
Merge branch 'devel' into feat/expose-linear-model
anyangml b664e55
fix: rename, fix UT device
anyangml 8f06bb5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] af16e65
change get_sel_type dtype to int64
anyangml 34e3c97
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 34843a6
feat: add test training
anyangml 1579a7e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 11350e2
fix: revert changes
anyangml 5b5e948
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 576c289
fix: update zbl example descriptor
anyangml 5e34b9c
Merge branch 'devel' into feat/expose-linear-model
anyangml d3b3342
feat: add linear example
anyangml File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from copy import ( | ||
| deepcopy, | ||
| ) | ||
| from typing import ( | ||
| Optional, | ||
| ) | ||
|
|
||
| import torch | ||
|
|
||
| from deepmd.pt.model.atomic_model import ( | ||
| LinearEnergyAtomicModel, | ||
| ) | ||
| from deepmd.pt.model.model.model import ( | ||
| BaseModel, | ||
| ) | ||
| from deepmd.utils.data_system import ( | ||
| DeepmdDataSystem, | ||
| ) | ||
|
|
||
| from .dp_model import ( | ||
| DPModelCommon, | ||
| ) | ||
| from .make_model import ( | ||
| make_model, | ||
| ) | ||
|
|
||
| DPLinearModel_ = make_model(LinearEnergyAtomicModel) | ||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @BaseModel.register("linear_ener") | ||
| class LinearEnergyModel(DPLinearModel_): | ||
| model_type = "ener" | ||
|
|
||
| def __init__( | ||
| self, | ||
| *args, | ||
| **kwargs, | ||
| ): | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def translated_output_def(self): | ||
| out_def_data = self.model_output_def().get_data() | ||
| output_def = { | ||
| "atom_energy": deepcopy(out_def_data["energy"]), | ||
| "energy": deepcopy(out_def_data["energy_redu"]), | ||
| } | ||
| if self.do_grad_r("energy"): | ||
| output_def["force"] = deepcopy(out_def_data["energy_derv_r"]) | ||
| output_def["force"].squeeze(-2) | ||
| if self.do_grad_c("energy"): | ||
| output_def["virial"] = deepcopy(out_def_data["energy_derv_c_redu"]) | ||
| output_def["virial"].squeeze(-2) | ||
| output_def["atom_virial"] = deepcopy(out_def_data["energy_derv_c"]) | ||
| output_def["atom_virial"].squeeze(-3) | ||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if "mask" in out_def_data: | ||
| output_def["mask"] = deepcopy(out_def_data["mask"]) | ||
| return output_def | ||
|
|
||
| def forward( | ||
| self, | ||
| coord, | ||
| atype, | ||
| box: Optional[torch.Tensor] = None, | ||
| fparam: Optional[torch.Tensor] = None, | ||
| aparam: Optional[torch.Tensor] = None, | ||
| do_atomic_virial: bool = False, | ||
| ) -> dict[str, torch.Tensor]: | ||
| model_ret = self.forward_common( | ||
| coord, | ||
| atype, | ||
| box, | ||
| fparam=fparam, | ||
| aparam=aparam, | ||
| do_atomic_virial=do_atomic_virial, | ||
| ) | ||
|
|
||
| model_predict = {} | ||
| model_predict["atom_energy"] = model_ret["energy"] | ||
| model_predict["energy"] = model_ret["energy_redu"] | ||
| if self.do_grad_r("energy"): | ||
| model_predict["force"] = model_ret["energy_derv_r"].squeeze(-2) | ||
| if self.do_grad_c("energy"): | ||
| model_predict["virial"] = model_ret["energy_derv_c_redu"].squeeze(-2) | ||
| if do_atomic_virial: | ||
| model_predict["atom_virial"] = model_ret["energy_derv_c"].squeeze(-3) | ||
| else: | ||
| model_predict["force"] = model_ret["dforce"] | ||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if "mask" in model_ret: | ||
| model_predict["mask"] = model_ret["mask"] | ||
| return model_predict | ||
|
|
||
| @torch.jit.export | ||
| def forward_lower( | ||
| self, | ||
| extended_coord, | ||
| extended_atype, | ||
| nlist, | ||
| mapping: Optional[torch.Tensor] = None, | ||
| fparam: Optional[torch.Tensor] = None, | ||
| aparam: Optional[torch.Tensor] = None, | ||
| do_atomic_virial: bool = False, | ||
| ): | ||
| model_ret = self.forward_common_lower( | ||
| extended_coord, | ||
| extended_atype, | ||
| nlist, | ||
| mapping=mapping, | ||
| fparam=fparam, | ||
| aparam=aparam, | ||
| do_atomic_virial=do_atomic_virial, | ||
| extra_nlist_sort=self.need_sorted_nlist_for_lower(), | ||
| ) | ||
|
|
||
| model_predict = {} | ||
| model_predict["atom_energy"] = model_ret["energy"] | ||
| model_predict["energy"] = model_ret["energy_redu"] | ||
| if self.do_grad_r("energy"): | ||
| model_predict["extended_force"] = model_ret["energy_derv_r"].squeeze(-2) | ||
| if self.do_grad_c("energy"): | ||
| model_predict["virial"] = model_ret["energy_derv_c_redu"].squeeze(-2) | ||
| if do_atomic_virial: | ||
| model_predict["extended_virial"] = model_ret["energy_derv_c"].squeeze( | ||
| -3 | ||
| ) | ||
| else: | ||
| assert model_ret["dforce"] is not None | ||
| model_predict["dforce"] = model_ret["dforce"] | ||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return model_predict | ||
anyangml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @classmethod | ||
| def update_sel( | ||
| cls, | ||
| train_data: DeepmdDataSystem, | ||
| type_map: Optional[list[str]], | ||
| local_jdata: dict, | ||
| ) -> tuple[dict, Optional[float]]: | ||
| """Update the selection and perform neighbor statistics. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| train_data : DeepmdDataSystem | ||
| data used to do neighbor statictics | ||
| type_map : list[str], optional | ||
| The name of each type of atoms | ||
| local_jdata : dict | ||
| The local data refer to the current class | ||
|
|
||
| Returns | ||
| ------- | ||
| dict | ||
| The updated local data | ||
| float | ||
| The minimum distance between two atoms | ||
| """ | ||
| local_jdata_cpy = local_jdata.copy() | ||
| type_map = local_jdata_cpy["type_map"] | ||
| min_nbor_dist = None | ||
| for idx, sub_model in enumerate(local_jdata_cpy["models"]): | ||
| if "tab_file" not in sub_model: | ||
| sub_model, temp_min = DPModelCommon.update_sel( | ||
| train_data, type_map, local_jdata["models"][idx] | ||
| ) | ||
| if min_nbor_dist is None or temp_min <= min_nbor_dist: | ||
| min_nbor_dist = temp_min | ||
| return local_jdata_cpy, min_nbor_dist | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.