-
Notifications
You must be signed in to change notification settings - Fork 581
feat: add plugin mode for data modifier #4621
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
iProzd
merged 12 commits into
deepmodeling:devel
from
ChiahsinChu:devel-modifier-plugin
Mar 14, 2025
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
134b36a
add plugin for data modifier
ChiahsinChu b60f3a8
fix bug in argcheck
ChiahsinChu ce81c22
Merge branch 'devel' into devel-modifier-plugin
ChiahsinChu a42c7a4
Merge branch 'devel' into devel-modifier-plugin
ChiahsinChu 41606ae
remove old data modifier module
ChiahsinChu 8c86290
update data modifier in unit tests
ChiahsinChu f9c5d53
update data modifier in unit tests
ChiahsinChu a85908e
Merge branch 'devel' into devel-modifier-plugin
ChiahsinChu 148a7a8
Merge branch 'devel' into devel-modifier-plugin
ChiahsinChu 502b204
(tf) update DipoleChargeModifier.__new__
ChiahsinChu 3487689
Merge branch 'devel-modifier-plugin' of github.com:ChiahsinChu/deepmd…
ChiahsinChu af66d73
Merge branch 'devel' into devel-modifier-plugin
ChiahsinChu 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,8 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from .base_modifier import ( | ||
| make_base_modifier, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "make_base_modifier", | ||
| ] |
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,74 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import inspect | ||
| from abc import ( | ||
| ABC, | ||
| abstractmethod, | ||
| ) | ||
|
|
||
| from deepmd.utils.plugin import ( | ||
| PluginVariant, | ||
| make_plugin_registry, | ||
| ) | ||
|
|
||
|
|
||
| def make_base_modifier() -> type[object]: | ||
| class BaseModifier(ABC, PluginVariant, make_plugin_registry("modifier")): | ||
| """Base class for data modifier.""" | ||
|
|
||
| def __new__(cls, *args, **kwargs): | ||
| if cls is BaseModifier: | ||
| cls = cls.get_class_by_type(kwargs["type"]) | ||
| return super().__new__(cls) | ||
|
|
||
| @abstractmethod | ||
| def serialize(self) -> dict: | ||
| """Serialize the modifier. | ||
|
|
||
| Returns | ||
| ------- | ||
| dict | ||
| The serialized data | ||
| """ | ||
| pass | ||
|
|
||
| @classmethod | ||
| def deserialize(cls, data: dict) -> "BaseModifier": | ||
| """Deserialize the modifier. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data : dict | ||
| The serialized data | ||
|
|
||
| Returns | ||
| ------- | ||
| BaseModel | ||
| The deserialized modifier | ||
| """ | ||
| if inspect.isabstract(cls): | ||
| return cls.get_class_by_type(data["type"]).deserialize(data) | ||
| raise NotImplementedError(f"Not implemented in class {cls.__name__}") | ||
|
|
||
| @classmethod | ||
| def get_modifier(cls, modifier_params: dict) -> "BaseModifier": | ||
| """Get the modifier by the parameters. | ||
|
|
||
| By default, all the parameters are directly passed to the constructor. | ||
| If not, override this method. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| modifier_params : dict | ||
| The modifier parameters | ||
|
|
||
| Returns | ||
| ------- | ||
| BaseModifier | ||
| The modifier | ||
| """ | ||
| modifier_params = modifier_params.copy() | ||
| modifier_params.pop("type", None) | ||
| modifier = cls(**modifier_params) | ||
| return modifier | ||
|
|
||
| return BaseModifier | ||
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,12 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from .base_modifier import ( | ||
| BaseModifier, | ||
| ) | ||
| from .dipole_charge import ( | ||
| DipoleChargeModifier, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "BaseModifier", | ||
| "DipoleChargeModifier", | ||
| ] |
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,13 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from deepmd.dpmodel.modifier.base_modifier import ( | ||
| make_base_modifier, | ||
| ) | ||
| from deepmd.tf.infer import ( | ||
| DeepPot, | ||
| ) | ||
|
|
||
|
|
||
| class BaseModifier(DeepPot, make_base_modifier()): | ||
| def __init__(self, *args, **kwargs) -> None: | ||
| """Construct a basic model for different tasks.""" | ||
| DeepPot.__init__(self, *args, **kwargs) | ||
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
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.