-
Notifications
You must be signed in to change notification settings - Fork 589
feat: new backend pytorch exportable. #5194
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
Open
wanghan-iapcm
wants to merge
27
commits into
deepmodeling:master
Choose a base branch
from
wanghan-iapcm:feat-torch-exportable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,348
−24
Open
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ec2e031
implement pytorch-exportable for se_e2_a descriptor
b8a48ff
better type for xp.zeros
1cc001f
implement env, base_descriptor and exclude_mask, remove the dependenc…
f2fbe88
mv to_torch_tensor to common
e2afbe9
simplify __init__ of the NaiveLayer
4ba511a
fix bug
fb9598a
fix bug
fa03351
simplify init method of se_e2_a descriptor. fig bug in consistent UT
09b33f1
restructure the test folders. add test_common.
67f2e54
add test_exclusion_mask.py
f7d83dd
fix poitential import issue in test.
0c96bb6
correct __call__(). fix bug
9dca912
fix registration issue
17f0a5d
fix pt-expt file extension
8ce93ba
fix(pt): expansion of get_default_nthreads()
3091988
fix bug of intra-inter
85f0583
fix bug of default dp inter value
d33324d
fix cicd
4de9a56
feat: add support for se_r
f4dc0af
fix device of xp array
2384835
fix device of xp array
9646d71
revert extend_coord_with_ghosts
f270069
raise error for non-implemented methods
57433d3
restore import torch
eedcbaf
fix(pt,pt-expt): guard thread setters
d8b2cf4
make exclusion mask modules
aeef15a
fix(pt-expt): clear params on None
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,126 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from collections.abc import ( | ||
| Callable, | ||
| ) | ||
| from importlib.util import ( | ||
| find_spec, | ||
| ) | ||
| from typing import ( | ||
| TYPE_CHECKING, | ||
| ClassVar, | ||
| ) | ||
|
|
||
| from deepmd.backend.backend import ( | ||
| Backend, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from argparse import ( | ||
| Namespace, | ||
| ) | ||
|
|
||
| from deepmd.infer.deep_eval import ( | ||
| DeepEvalBackend, | ||
| ) | ||
| from deepmd.utils.neighbor_stat import ( | ||
| NeighborStat, | ||
| ) | ||
|
|
||
|
|
||
| @Backend.register("pt-expt") | ||
| @Backend.register("pytorch-exportable") | ||
| class PyTorchExportableBackend(Backend): | ||
| """PyTorch exportable backend.""" | ||
|
|
||
| name = "PyTorch Exportable" | ||
| """The formal name of the backend.""" | ||
| features: ClassVar[Backend.Feature] = ( | ||
| Backend.Feature.ENTRY_POINT | ||
| | Backend.Feature.DEEP_EVAL | ||
| | Backend.Feature.NEIGHBOR_STAT | ||
| | Backend.Feature.IO | ||
| ) | ||
| """The features of the backend.""" | ||
| suffixes: ClassVar[list[str]] = [".pte"] | ||
| """The suffixes of the backend.""" | ||
|
|
||
| def is_available(self) -> bool: | ||
| """Check if the backend is available. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| Whether the backend is available. | ||
| """ | ||
| return find_spec("torch") is not None | ||
|
|
||
| @property | ||
| def entry_point_hook(self) -> Callable[["Namespace"], None]: | ||
| """The entry point hook of the backend. | ||
|
|
||
| Returns | ||
| ------- | ||
| Callable[[Namespace], None] | ||
| The entry point hook of the backend. | ||
| """ | ||
| from deepmd.pt.entrypoints.main import main as deepmd_main | ||
|
|
||
| return deepmd_main | ||
|
|
||
| @property | ||
| def deep_eval(self) -> type["DeepEvalBackend"]: | ||
| """The Deep Eval backend of the backend. | ||
|
|
||
| Returns | ||
| ------- | ||
| type[DeepEvalBackend] | ||
| The Deep Eval backend of the backend. | ||
| """ | ||
| from deepmd.pt.infer.deep_eval import DeepEval as DeepEvalPT | ||
|
|
||
| return DeepEvalPT | ||
|
|
||
| @property | ||
| def neighbor_stat(self) -> type["NeighborStat"]: | ||
| """The neighbor statistics of the backend. | ||
|
|
||
| Returns | ||
| ------- | ||
| type[NeighborStat] | ||
| The neighbor statistics of the backend. | ||
| """ | ||
| from deepmd.pt.utils.neighbor_stat import ( | ||
| NeighborStat, | ||
| ) | ||
|
|
||
| return NeighborStat | ||
|
|
||
| @property | ||
| def serialize_hook(self) -> Callable[[str], dict]: | ||
| """The serialize hook to convert the model file to a dictionary. | ||
|
|
||
| Returns | ||
| ------- | ||
| Callable[[str], dict] | ||
| The serialize hook of the backend. | ||
| """ | ||
| from deepmd.pt.utils.serialization import ( | ||
| serialize_from_file, | ||
| ) | ||
|
|
||
| return serialize_from_file | ||
|
|
||
| @property | ||
| def deserialize_hook(self) -> Callable[[str, dict], None]: | ||
| """The deserialize hook to convert the dictionary to a model file. | ||
|
|
||
| Returns | ||
| ------- | ||
| Callable[[str, dict], None] | ||
| The deserialize hook of the backend. | ||
| """ | ||
| from deepmd.pt.utils.serialization import ( | ||
| deserialize_to_file, | ||
| ) | ||
|
|
||
| return deserialize_to_file | ||
wanghan-iapcm marked this conversation as resolved.
Outdated
Show resolved
Hide 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
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
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 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later |
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,35 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import importlib | ||
| from typing import ( | ||
| Any, | ||
| overload, | ||
| ) | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.pt_expt.utils import ( | ||
| env, | ||
| ) | ||
|
|
||
| torch = importlib.import_module("torch") | ||
wanghan-iapcm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @overload | ||
| def to_torch_array(array: np.ndarray) -> torch.Tensor: ... | ||
Check noticeCode scanning / CodeQL Statement has no effect Note
This statement has no effect.
|
||
|
|
||
|
|
||
| @overload | ||
| def to_torch_array(array: None) -> None: ... | ||
Check noticeCode scanning / CodeQL Statement has no effect Note
This statement has no effect.
|
||
|
|
||
|
|
||
| @overload | ||
| def to_torch_array(array: torch.Tensor) -> torch.Tensor: ... | ||
Check noticeCode scanning / CodeQL Statement has no effect Note
This statement has no effect.
|
||
|
|
||
|
|
||
| def to_torch_array(array: Any) -> torch.Tensor | None: | ||
| """Convert input to a torch tensor on the pt-expt device.""" | ||
| if array is None: | ||
| return None | ||
| if torch.is_tensor(array): | ||
| return array.to(device=env.DEVICE) | ||
| return torch.as_tensor(array, device=env.DEVICE) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from .base_descriptor import ( | ||
| BaseDescriptor, | ||
| ) | ||
| from .se_e2_a import ( | ||
| DescrptSeA, | ||
| ) | ||
| from .se_r import ( | ||
| DescrptSeR, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "BaseDescriptor", | ||
| "DescrptSeA", | ||
| "DescrptSeR", | ||
| ] |
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,10 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import importlib | ||
|
|
||
| from deepmd.dpmodel.descriptor import ( | ||
| make_base_descriptor, | ||
| ) | ||
|
|
||
| torch = importlib.import_module("torch") | ||
|
|
||
| BaseDescriptor = make_base_descriptor(torch.Tensor, "forward") |
Oops, something went wrong.
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.