-
Notifications
You must be signed in to change notification settings - Fork 586
feat(pt): add plugin for data modifier #4661
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
16 commits
Select commit
Hold shift + click to select a range
fbf69c2
add data modifier for pt backend (training)
ChiahsinChu e75e35d
add UT for data modifier in pt model training
ChiahsinChu 89ba114
fix minor bugs
ChiahsinChu 3bdb9f5
fix bug and add type annotation for BaseModifier
ChiahsinChu b093750
minor revision based on coderabbit
ChiahsinChu b60a1e6
fix bug in UT
ChiahsinChu ddb689b
fix bug in BaseModifier.modify_data
ChiahsinChu f74752c
feat(pt): support data modifier in inference and frozen models
ChiahsinChu a8932c1
Apply suggestion from @Copilot and @njzjz
ChiahsinChu 5071254
remove comment-out codes
ChiahsinChu 1fdcfa3
resolve nitpick comments
ChiahsinChu f28a6a8
add `use_cache` as modifier var, so that the user can choose whether …
ChiahsinChu a0f8ccb
use ThreadPoolExecutor to eliminate CUDA re-initialization in data mo…
ChiahsinChu fc894dd
resolve nitpick comments
ChiahsinChu 1acc58e
fix bug about max_workers in ThreadPoolExecutor
ChiahsinChu 352c149
fix typo in wrapper and UT
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import copy | ||
| from typing import ( | ||
| Any, | ||
| ) | ||
|
|
||
| from .base_modifier import ( | ||
| BaseModifier, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "BaseModifier", | ||
| "get_data_modifier", | ||
| ] | ||
|
|
||
|
|
||
| def get_data_modifier(_modifier_params: dict[str, Any]) -> BaseModifier: | ||
| modifier_params = copy.deepcopy(_modifier_params) | ||
| try: | ||
| modifier_type = modifier_params.pop("type") | ||
| except KeyError: | ||
| raise ValueError("Data modifier type not specified!") from None | ||
| return BaseModifier.get_class_by_type(modifier_type).get_modifier(modifier_params) |
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,187 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from abc import ( | ||
| abstractmethod, | ||
| ) | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
| from deepmd.dpmodel.array_api import ( | ||
| Array, | ||
| ) | ||
| from deepmd.dpmodel.common import PRECISION_DICT as NP_PRECISION_DICT | ||
| from deepmd.dpmodel.modifier.base_modifier import ( | ||
| make_base_modifier, | ||
| ) | ||
| from deepmd.pt.utils.env import ( | ||
| DEVICE, | ||
| GLOBAL_PT_FLOAT_PRECISION, | ||
| RESERVED_PRECISION_DICT, | ||
| ) | ||
| from deepmd.pt.utils.utils import ( | ||
| to_numpy_array, | ||
| to_torch_tensor, | ||
| ) | ||
| from deepmd.utils.data import ( | ||
| DeepmdData, | ||
| ) | ||
|
|
||
|
|
||
| class BaseModifier(torch.nn.Module, make_base_modifier()): | ||
| def __init__(self, use_cache: bool = True) -> None: | ||
| """Construct a base modifier for data modification tasks.""" | ||
| torch.nn.Module.__init__(self) | ||
| self.modifier_type = "base" | ||
| self.jitable = True | ||
|
|
||
| self.use_cache = use_cache | ||
|
|
||
| def serialize(self) -> dict: | ||
| """Serialize the modifier. | ||
|
|
||
| Returns | ||
| ------- | ||
| dict | ||
| The serialized data | ||
| """ | ||
| data = { | ||
| "@class": "Modifier", | ||
| "type": self.modifier_type, | ||
| "@version": 3, | ||
| } | ||
| return data | ||
|
|
||
| @classmethod | ||
| def deserialize(cls, data: dict) -> "BaseModifier": | ||
| """Deserialize the modifier. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data : dict | ||
| The serialized data | ||
|
|
||
| Returns | ||
| ------- | ||
| BaseModifier | ||
| The deserialized modifier | ||
| """ | ||
| data = data.copy() | ||
| # Remove serialization metadata before passing to constructor | ||
| data.pop("@class", None) | ||
| data.pop("type", None) | ||
| data.pop("@version", None) | ||
| modifier = cls(**data) | ||
| return modifier | ||
|
|
||
| @abstractmethod | ||
| @torch.jit.export | ||
| def forward( | ||
| self, | ||
| coord: torch.Tensor, | ||
| atype: torch.Tensor, | ||
| box: torch.Tensor | None = None, | ||
| fparam: torch.Tensor | None = None, | ||
| aparam: torch.Tensor | None = None, | ||
| do_atomic_virial: bool = False, | ||
| ) -> dict[str, torch.Tensor]: | ||
| """Compute energy, force, and virial corrections.""" | ||
|
|
||
| @torch.jit.unused | ||
| def modify_data(self, data: dict[str, Array | float], data_sys: DeepmdData) -> None: | ||
| """Modify data of single frame. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data | ||
| Internal data of DeepmdData. | ||
| Be a dict, has the following keys | ||
| - coord coordinates (nat, 3) | ||
| - box simulation box (9,) | ||
| - atype atom types (nat,) | ||
| - fparam frame parameter (nfp,) | ||
| - aparam atom parameter (nat, nap) | ||
| - find_energy tells if data has energy | ||
| - find_force tells if data has force | ||
| - find_virial tells if data has virial | ||
| - energy energy (1,) | ||
| - force force (nat, 3) | ||
| - virial virial (9,) | ||
| """ | ||
| if ( | ||
| "find_energy" not in data | ||
| and "find_force" not in data | ||
| and "find_virial" not in data | ||
| ): | ||
| return | ||
|
|
||
| prec = NP_PRECISION_DICT[RESERVED_PRECISION_DICT[GLOBAL_PT_FLOAT_PRECISION]] | ||
|
|
||
| nframes = 1 | ||
| natoms = len(data["atype"]) | ||
| atom_types = np.tile(data["atype"], nframes).reshape(nframes, -1) | ||
|
|
||
| coord_input = torch.tensor( | ||
| data["coord"].reshape([nframes, natoms, 3]).astype(prec), | ||
| dtype=GLOBAL_PT_FLOAT_PRECISION, | ||
| device=DEVICE, | ||
| ) | ||
| type_input = torch.tensor( | ||
| atom_types.astype(NP_PRECISION_DICT[RESERVED_PRECISION_DICT[torch.long]]), | ||
| dtype=torch.long, | ||
| device=DEVICE, | ||
| ) | ||
| if data["box"] is not None: | ||
| box_input = torch.tensor( | ||
| data["box"].reshape([nframes, 3, 3]).astype(prec), | ||
| dtype=GLOBAL_PT_FLOAT_PRECISION, | ||
| device=DEVICE, | ||
| ) | ||
| else: | ||
| box_input = None | ||
| if "fparam" in data: | ||
| fparam_input = to_torch_tensor(data["fparam"].reshape(nframes, -1)) | ||
| else: | ||
| fparam_input = None | ||
| if "aparam" in data: | ||
| aparam_input = to_torch_tensor(data["aparam"].reshape(nframes, natoms, -1)) | ||
| else: | ||
| aparam_input = None | ||
| do_atomic_virial = False | ||
|
|
||
| # implement data modification method in forward | ||
| modifier_data = self.forward( | ||
| coord_input, | ||
| type_input, | ||
| box_input, | ||
| fparam_input, | ||
| aparam_input, | ||
| do_atomic_virial, | ||
| ) | ||
|
|
||
| if data.get("find_energy") == 1.0: | ||
| if "energy" not in modifier_data: | ||
| raise KeyError( | ||
| f"Modifier {self.__class__.__name__} did not provide 'energy' " | ||
| "in its output while 'find_energy' is set." | ||
| ) | ||
| data["energy"] -= to_numpy_array(modifier_data["energy"]).reshape( | ||
| data["energy"].shape | ||
| ) | ||
| if data.get("find_force") == 1.0: | ||
| if "force" not in modifier_data: | ||
| raise KeyError( | ||
| f"Modifier {self.__class__.__name__} did not provide 'force' " | ||
| "in its output while 'find_force' is set." | ||
| ) | ||
| data["force"] -= to_numpy_array(modifier_data["force"]).reshape( | ||
| data["force"].shape | ||
| ) | ||
| if data.get("find_virial") == 1.0: | ||
| if "virial" not in modifier_data: | ||
| raise KeyError( | ||
| f"Modifier {self.__class__.__name__} did not provide 'virial' " | ||
| "in its output while 'find_virial' is set." | ||
| ) | ||
| data["virial"] -= to_numpy_array(modifier_data["virial"]).reshape( | ||
| data["virial"].shape | ||
| ) |
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.