-
Notifications
You must be signed in to change notification settings - Fork 609
add BaseModel; store type in serialization #3335
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
17 commits
Select commit
Hold shift + click to select a range
b374abc
basemodel; store type
njzjz 325ff74
use DeepEval
njzjz 99dbc7b
fix DPModel.__new__
njzjz 7fbaf71
fallback to DPModel
njzjz c2fb0be
Update deepmd/dpmodel/model/base_model.py
njzjz 7af6ce3
Apply suggestions from code review
njzjz fd455b7
Merge branch 'devel' into basemodel
njzjz 1fe4def
fix indent
njzjz 74c4116
combine deserialize
njzjz ffc7dfc
fix typo
njzjz 054fba6
use self as type hint
njzjz 2f9d56f
Revert "use self as type hint"
njzjz 4a17c1f
add serialize
njzjz 9681276
Apply suggestions from code review
njzjz 67b83ed
add make_base_model
njzjz fbd1300
fix error message
njzjz d3ef0a4
move get_model_def_script to make_base_atomic_model
njzjz 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
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,158 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import inspect | ||
| from abc import ( | ||
| ABC, | ||
| abstractmethod, | ||
| ) | ||
| from typing import ( | ||
| Any, | ||
| List, | ||
| Type, | ||
| ) | ||
|
|
||
| from deepmd.utils.plugin import ( | ||
| make_plugin_registry, | ||
| ) | ||
|
|
||
|
|
||
| def make_base_model() -> Type[object]: | ||
| class BaseBaseModel(ABC, make_plugin_registry("model")): | ||
| """Base class for final exported model that will be directly used for inference. | ||
|
|
||
| The class defines some abstractmethods that will be directly called by the | ||
| inference interface. If the final model class inherits some of those methods | ||
| from other classes, `BaseModel` should be inherited as the last class to ensure | ||
| the correct method resolution order. | ||
|
|
||
| This class is backend-indepedent. | ||
|
|
||
| See Also | ||
| -------- | ||
| deepmd.dpmodel.model.base_model.BaseModel | ||
| BaseModel class for DPModel backend. | ||
| """ | ||
|
|
||
| def __new__(cls, *args, **kwargs): | ||
| if inspect.isabstract(cls): | ||
| cls = cls.get_class_by_type(kwargs.get("type", "standard")) | ||
| return super().__new__(cls) | ||
|
|
||
| @abstractmethod | ||
| def __call__(self, *args: Any, **kwds: Any) -> Any: | ||
| """Inference method. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| *args : Any | ||
| The input data for inference. | ||
| **kwds : Any | ||
| The input data for inference. | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| The output of the inference. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def get_type_map(self) -> List[str]: | ||
| """Get the type map.""" | ||
|
|
||
| @abstractmethod | ||
| def get_rcut(self): | ||
| """Get the cut-off radius.""" | ||
|
|
||
| @abstractmethod | ||
| def get_dim_fparam(self): | ||
| """Get the number (dimension) of frame parameters of this atomic model.""" | ||
|
|
||
| @abstractmethod | ||
| def get_dim_aparam(self): | ||
| """Get the number (dimension) of atomic parameters of this atomic model.""" | ||
|
|
||
| @abstractmethod | ||
| 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. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| 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). | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def model_output_type(self) -> str: | ||
| """Get the output type for the model.""" | ||
|
|
||
| @abstractmethod | ||
| def serialize(self) -> dict: | ||
| """Serialize the model. | ||
|
|
||
| Returns | ||
| ------- | ||
| dict | ||
| The serialized data | ||
| """ | ||
| pass | ||
|
|
||
| @classmethod | ||
| def deserialize(cls, data: dict) -> "BaseBaseModel": | ||
| """Deserialize the model. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data : dict | ||
| The serialized data | ||
|
|
||
| Returns | ||
| ------- | ||
| BaseModel | ||
| The deserialized model | ||
| """ | ||
| if inspect.isabstract(cls): | ||
| return cls.get_class_by_type(data["type"]).deserialize(data) | ||
| raise NotImplementedError("Not implemented in class %s" % cls.__name__) | ||
|
|
||
| model_def_script: str | ||
|
|
||
| @abstractmethod | ||
| def get_model_def_script(self) -> str: | ||
| """Get the model definition script.""" | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def get_nnei(self) -> int: | ||
| """Returns the total number of selected neighboring atoms in the cut-off radius.""" | ||
| # for C++ interface | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def get_nsel(self) -> int: | ||
| """Returns the total number of selected neighboring atoms in the cut-off radius.""" | ||
| pass | ||
|
|
||
| return BaseBaseModel | ||
|
|
||
|
|
||
| class BaseModel(make_base_model()): | ||
| """Base class for final exported model that will be directly used for inference. | ||
|
|
||
| The class defines some abstractmethods that will be directly called by the | ||
| inference interface. If the final model class inherbits some of those methods | ||
| from other classes, `BaseModel` should be inherited as the last class to ensure | ||
| the correct method resolution order. | ||
|
|
||
| This class is for the DPModel backend. | ||
|
|
||
| See Also | ||
| -------- | ||
| deepmd.dpmodel.model.base_model.BaseBaseModel | ||
| Backend-independent BaseModel class. | ||
| """ | ||
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
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.