-
Notifications
You must be signed in to change notification settings - Fork 629
feat(pt_expt): add frozen model #5318
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f809729
feat(pt_expt): add frozen model support
b7306ca
refactor(pt_expt): refactor frozen model with dpmodel base and torch_…
4815fbd
fix(pt_expt): fix super() call in FrozenModel and remove redundant se…
9e11539
fix(pt_expt): fix super() call in FrozenModel and remove redundant se…
b1daa79
Merge remote-tracking branch 'origin/feat-pt-expt-frozen' into feat-p…
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from typing import ( | ||
| Any, | ||
| NoReturn, | ||
| ) | ||
|
|
||
| from deepmd.dpmodel.common import ( | ||
| NativeOP, | ||
| ) | ||
| from deepmd.dpmodel.output_def import ( | ||
| FittingOutputDef, | ||
| ) | ||
| from deepmd.utils.data_system import ( | ||
| DeepmdDataSystem, | ||
| ) | ||
|
|
||
| from .base_model import ( | ||
| BaseModel, | ||
| ) | ||
|
|
||
|
|
||
| class FrozenModel(NativeOP, BaseModel): | ||
| """Load model from a frozen model file, which cannot be trained. | ||
|
|
||
| The frozen model delegates all operations to the deserialized inner | ||
| model. ``serialize()`` returns the inner model's data, and | ||
| ``deserialize()`` dispatches to the appropriate model class via | ||
| ``BaseModel.deserialize``. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| model_file : str | ||
| The path to the frozen model file. | ||
| """ | ||
|
|
||
| def __init__(self, model_file: str, **kwargs: Any) -> None: | ||
| super().__init__() | ||
| self.model_file = model_file | ||
| from deepmd.backend.backend import ( | ||
| Backend, | ||
| ) | ||
|
|
||
| inp_backend: Backend = Backend.detect_backend_by_model(model_file)() | ||
| data = inp_backend.serialize_hook(model_file) | ||
| self.model = BaseModel.deserialize(data["model"]) | ||
|
|
||
| def call(self, *args: Any, **kwargs: Any) -> Any: | ||
| """Forward pass.""" | ||
| return self.model(*args, **kwargs) | ||
|
|
||
| def fitting_output_def(self) -> FittingOutputDef: | ||
| """Get the output def of developer implemented atomic models.""" | ||
| return self.model.fitting_output_def() | ||
|
|
||
| def get_rcut(self) -> float: | ||
| """Get the cut-off radius.""" | ||
| return self.model.get_rcut() | ||
|
|
||
| def get_type_map(self) -> list[str]: | ||
| """Get the type map.""" | ||
| return self.model.get_type_map() | ||
|
|
||
| def get_sel(self) -> list[int]: | ||
| """Returns the number of selected atoms for each type.""" | ||
| return self.model.get_sel() | ||
|
|
||
| def get_dim_fparam(self) -> int: | ||
| """Get the number (dimension) of frame parameters of this atomic model.""" | ||
| return self.model.get_dim_fparam() | ||
|
|
||
| def get_dim_aparam(self) -> int: | ||
| """Get the number (dimension) of atomic parameters of this atomic model.""" | ||
| return self.model.get_dim_aparam() | ||
|
|
||
| def get_sel_type(self) -> list[int]: | ||
| """Get the selected atom types of this model. | ||
|
|
||
| Only atoms with selected atom types have atomic contribution | ||
| to the result of the model. | ||
| If returning an empty list, all atom types are selected. | ||
| """ | ||
| return self.model.get_sel_type() | ||
|
|
||
| def is_aparam_nall(self) -> bool: | ||
| """Check whether the shape of atomic parameters is (nframes, nall, ndim). | ||
|
|
||
| If False, the shape is (nframes, nloc, ndim). | ||
| """ | ||
| return self.model.is_aparam_nall() | ||
|
|
||
| def mixed_types(self) -> bool: | ||
| """If true, the model | ||
| 1. assumes total number of atoms aligned across frames; | ||
| 2. uses a neighbor list that does not distinguish different atomic types. | ||
|
|
||
| If false, the model | ||
| 1. assumes total number of atoms of each atom type aligned across frames; | ||
| 2. uses a neighbor list that distinguishes different atomic types. | ||
| """ | ||
| return self.model.mixed_types() | ||
|
|
||
| def has_message_passing(self) -> bool: | ||
| """Returns whether the descriptor has message passing.""" | ||
| return self.model.has_message_passing() | ||
|
|
||
| def need_sorted_nlist_for_lower(self) -> bool: | ||
| """Returns whether the model needs sorted nlist when using `forward_lower`.""" | ||
| return self.model.need_sorted_nlist_for_lower() | ||
|
|
||
| def get_model_def_script(self) -> str: | ||
| """Get the model definition script.""" | ||
| return self.model.get_model_def_script() | ||
|
|
||
| def get_min_nbor_dist(self) -> float | None: | ||
| """Get the minimum neighbor distance.""" | ||
| return self.model.get_min_nbor_dist() | ||
|
|
||
| def get_nnei(self) -> int: | ||
| """Returns the total number of selected neighboring atoms in the cut-off radius.""" | ||
| return self.model.get_nnei() | ||
|
|
||
| def get_nsel(self) -> int: | ||
| """Returns the total number of selected neighboring atoms in the cut-off radius.""" | ||
| return self.model.get_nsel() | ||
|
|
||
| def model_output_type(self) -> str: | ||
| """Get the output type for the model.""" | ||
| return self.model.model_output_type() | ||
|
|
||
| def get_observed_type_list(self) -> list[str]: | ||
| """Get observed types (elements) of the model during data statistics.""" | ||
| return self.model.get_observed_type_list() | ||
|
|
||
| def serialize(self) -> dict: | ||
| """Serialize the model. | ||
|
|
||
| Returns the inner model's serialized data. | ||
| """ | ||
| return self.model.serialize() | ||
|
|
||
| @classmethod | ||
| def deserialize(cls, data: dict) -> NoReturn: | ||
| raise RuntimeError("Should not touch here.") | ||
|
|
||
| @classmethod | ||
| def update_sel( | ||
| cls, | ||
| train_data: DeepmdDataSystem, | ||
| type_map: list[str] | None, | ||
| local_jdata: dict, | ||
| ) -> tuple[dict, float | None]: | ||
| """Update the selection and perform neighbor statistics.""" | ||
| return local_jdata, None |
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,23 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from typing import ( | ||
| Any, | ||
| ) | ||
|
|
||
| from deepmd.dpmodel.model.frozen import FrozenModel as FrozenModelDP | ||
| from deepmd.pt_expt.common import ( | ||
| torch_module, | ||
| ) | ||
|
|
||
| from .model import ( | ||
| BaseModel, | ||
| ) | ||
|
|
||
|
|
||
| @BaseModel.register("frozen") | ||
| @torch_module | ||
| class FrozenModel(FrozenModelDP): | ||
| def __init__(self, model_file: str, **kwargs: Any) -> None: | ||
| super().__init__(model_file, **kwargs) | ||
| # Re-deserialize as a pt_expt model (parent creates a dpmodel model) | ||
| self.model = BaseModel.deserialize(self.model.serialize()) | ||
| self.model.eval() | ||
|
wanghan-iapcm marked this conversation as resolved.
|
||
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.