-
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
28
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,360
−24
Open
Changes from 3 commits
Commits
Show all changes
28 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
8bdb1f8
fix bug
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]] = [".pth", ".pt"] | ||
| """The suffixes of the backend.""" | ||
wanghan-iapcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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
| 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,12 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from .base_descriptor import ( | ||
| BaseDescriptor, | ||
| ) | ||
| from .se_e2_a import ( | ||
| DescrptSeA, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "BaseDescriptor", | ||
| "DescrptSeA", | ||
| ] |
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") |
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,102 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import importlib | ||
| from typing import ( | ||
| Any, | ||
| ) | ||
|
|
||
| from deepmd.dpmodel.descriptor.se_e2_a import DescrptSeAArrayAPI as DescrptSeADP | ||
| from deepmd.pt_expt.descriptor.base_descriptor import ( | ||
| BaseDescriptor, | ||
| ) | ||
| from deepmd.pt_expt.utils import ( | ||
| env, | ||
| ) | ||
| from deepmd.pt_expt.utils.exclude_mask import ( | ||
| PairExcludeMask, | ||
| ) | ||
| from deepmd.pt_expt.utils.network import ( | ||
| NetworkCollection, | ||
| ) | ||
|
|
||
| torch = importlib.import_module("torch") | ||
|
|
||
|
|
||
| @BaseDescriptor.register("se_e2_a_expt") | ||
| @BaseDescriptor.register("se_a_expt") | ||
| class DescrptSeA(DescrptSeADP, torch.nn.Module): | ||
| def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
| torch.nn.Module.__init__(self) | ||
| DescrptSeADP.__init__(self, *args, **kwargs) | ||
| self._convert_state() | ||
|
|
||
| def __setattr__(self, name: str, value: Any) -> None: | ||
Check noticeCode scanning / CodeQL Explicit returns mixed with implicit (fall through) returns Note
Mixing implicit and explicit returns may indicate an error, as implicit returns always return None.
|
||
| if name in {"davg", "dstd"} and "_buffers" in self.__dict__: | ||
| tensor = ( | ||
| None if value is None else torch.as_tensor(value, device=env.DEVICE) | ||
| ) | ||
| if name in self._buffers: | ||
| self._buffers[name] = tensor | ||
| return | ||
| return super().__setattr__(name, tensor) | ||
wanghan-iapcm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if name == "embeddings" and "_modules" in self.__dict__: | ||
| if value is not None and not isinstance(value, torch.nn.Module): | ||
| if hasattr(value, "serialize"): | ||
| value = NetworkCollection.deserialize(value.serialize()) | ||
| elif isinstance(value, dict): | ||
| value = NetworkCollection.deserialize(value) | ||
| return super().__setattr__(name, value) | ||
| if name == "emask" and "_modules" in self.__dict__: | ||
| if value is not None and not isinstance(value, torch.nn.Module): | ||
| value = PairExcludeMask( | ||
| self.ntypes, exclude_types=list(value.get_exclude_types()) | ||
| ) | ||
| return super().__setattr__(name, value) | ||
| return super().__setattr__(name, value) | ||
wanghan-iapcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def _convert_state(self) -> None: | ||
| if self.davg is not None: | ||
| davg = torch.as_tensor(self.davg, device=env.DEVICE) | ||
| if "davg" in self._buffers: | ||
| self._buffers["davg"] = davg | ||
| else: | ||
| if hasattr(self, "davg"): | ||
| delattr(self, "davg") | ||
| self.register_buffer("davg", davg) | ||
| if self.dstd is not None: | ||
| dstd = torch.as_tensor(self.dstd, device=env.DEVICE) | ||
| if "dstd" in self._buffers: | ||
| self._buffers["dstd"] = dstd | ||
| else: | ||
| if hasattr(self, "dstd"): | ||
| delattr(self, "dstd") | ||
| self.register_buffer("dstd", dstd) | ||
| if self.embeddings is not None: | ||
| self.embeddings = NetworkCollection.deserialize(self.embeddings.serialize()) | ||
| if self.emask is not None: | ||
| self.emask = PairExcludeMask( | ||
| self.ntypes, exclude_types=list(self.emask.get_exclude_types()) | ||
| ) | ||
|
|
||
| def forward( | ||
| self, | ||
| nlist: torch.Tensor, | ||
| extended_coord: torch.Tensor, | ||
| extended_atype: torch.Tensor, | ||
wanghan-iapcm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extended_atype_embd: torch.Tensor | None = None, | ||
| mapping: torch.Tensor | None = None, | ||
| type_embedding: torch.Tensor | None = None, | ||
| ) -> tuple[ | ||
| torch.Tensor, | ||
| torch.Tensor | None, | ||
| torch.Tensor | None, | ||
| torch.Tensor | None, | ||
| torch.Tensor | None, | ||
| ]: | ||
| del extended_atype_embd, type_embedding | ||
| descrpt, rot_mat, g2, h2, sw = self.call( | ||
| extended_coord, | ||
| extended_atype, | ||
| nlist, | ||
| mapping=mapping, | ||
| ) | ||
| return descrpt, rot_mat, g2, h2, sw | ||
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,11 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
| from .exclude_mask import ( | ||
| AtomExcludeMask, | ||
| PairExcludeMask, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "AtomExcludeMask", | ||
| "PairExcludeMask", | ||
| ] |
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,117 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import importlib | ||
| import logging | ||
| import multiprocessing | ||
| import os | ||
| import sys | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.common import ( | ||
| VALID_PRECISION, | ||
| ) | ||
| from deepmd.env import ( | ||
| GLOBAL_ENER_FLOAT_PRECISION, | ||
| GLOBAL_NP_FLOAT_PRECISION, | ||
| get_default_nthreads, | ||
| set_default_nthreads, | ||
| ) | ||
|
|
||
| log = logging.getLogger(__name__) | ||
| torch = importlib.import_module("torch") | ||
|
|
||
| if sys.platform != "win32": | ||
| try: | ||
| multiprocessing.set_start_method("fork", force=True) | ||
| log.debug("Successfully set multiprocessing start method to 'fork'.") | ||
| except (RuntimeError, ValueError) as err: | ||
| log.warning(f"Could not set multiprocessing start method: {err}") | ||
| else: | ||
| log.debug("Skipping fork start method on Windows (not supported).") | ||
|
|
||
| SAMPLER_RECORD = os.environ.get("SAMPLER_RECORD", False) | ||
| DP_DTYPE_PROMOTION_STRICT = os.environ.get("DP_DTYPE_PROMOTION_STRICT", "0") == "1" | ||
wanghan-iapcm marked this conversation as resolved.
Show resolved
Hide resolved
Check noticeCode scanning / CodeQL Unused global variable Note
The global variable 'DP_DTYPE_PROMOTION_STRICT' is not used.
|
||
| try: | ||
| # only linux | ||
| ncpus = len(os.sched_getaffinity(0)) | ||
| except AttributeError: | ||
| ncpus = os.cpu_count() | ||
| NUM_WORKERS = int(os.environ.get("NUM_WORKERS", min(4, ncpus))) | ||
| if multiprocessing.get_start_method() != "fork": | ||
| # spawn or forkserver does not support NUM_WORKERS > 0 for DataLoader | ||
| log.warning( | ||
| "NUM_WORKERS > 0 is not supported with spawn or forkserver start method. " | ||
| "Setting NUM_WORKERS to 0." | ||
| ) | ||
| NUM_WORKERS = 0 | ||
|
|
||
| # Make sure DDP uses correct device if applicable | ||
| LOCAL_RANK = os.environ.get("LOCAL_RANK") | ||
| LOCAL_RANK = int(0 if LOCAL_RANK is None else LOCAL_RANK) | ||
|
|
||
| if os.environ.get("DEVICE") == "cpu" or torch.cuda.is_available() is False: | ||
| DEVICE = torch.device("cpu") | ||
| else: | ||
| DEVICE = torch.device(f"cuda:{LOCAL_RANK}") | ||
|
|
||
| JIT = False | ||
| CACHE_PER_SYS = 5 # keep at most so many sets per sys in memory | ||
| ENERGY_BIAS_TRAINABLE = True | ||
| CUSTOM_OP_USE_JIT = False | ||
|
|
||
| PRECISION_DICT = { | ||
| "float16": torch.float16, | ||
| "float32": torch.float32, | ||
| "float64": torch.float64, | ||
| "half": torch.float16, | ||
| "single": torch.float32, | ||
| "double": torch.float64, | ||
| "int32": torch.int32, | ||
| "int64": torch.int64, | ||
| "bfloat16": torch.bfloat16, | ||
| "bool": torch.bool, | ||
| } | ||
| GLOBAL_PT_FLOAT_PRECISION = PRECISION_DICT[np.dtype(GLOBAL_NP_FLOAT_PRECISION).name] | ||
| GLOBAL_PT_ENER_FLOAT_PRECISION = PRECISION_DICT[ | ||
| np.dtype(GLOBAL_ENER_FLOAT_PRECISION).name | ||
| ] | ||
| PRECISION_DICT["default"] = GLOBAL_PT_FLOAT_PRECISION | ||
| assert VALID_PRECISION.issubset(PRECISION_DICT.keys()) | ||
| # cannot automatically generated | ||
| RESERVED_PRECISION_DICT = { | ||
| torch.float16: "float16", | ||
| torch.float32: "float32", | ||
| torch.float64: "float64", | ||
| torch.int32: "int32", | ||
| torch.int64: "int64", | ||
| torch.bfloat16: "bfloat16", | ||
| torch.bool: "bool", | ||
| } | ||
| assert set(PRECISION_DICT.values()) == set(RESERVED_PRECISION_DICT.keys()) | ||
| DEFAULT_PRECISION = "float64" | ||
|
|
||
| # throw warnings if threads not set | ||
| set_default_nthreads() | ||
| inter_nthreads, intra_nthreads = get_default_nthreads() | ||
njzjz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if inter_nthreads > 0: # the behavior of 0 is not documented | ||
| torch.set_num_interop_threads(inter_nthreads) | ||
| if intra_nthreads > 0: | ||
| torch.set_num_threads(intra_nthreads) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| __all__ = [ | ||
| "CACHE_PER_SYS", | ||
| "CUSTOM_OP_USE_JIT", | ||
| "DEFAULT_PRECISION", | ||
| "DEVICE", | ||
| "ENERGY_BIAS_TRAINABLE", | ||
| "GLOBAL_ENER_FLOAT_PRECISION", | ||
| "GLOBAL_NP_FLOAT_PRECISION", | ||
| "GLOBAL_PT_ENER_FLOAT_PRECISION", | ||
| "GLOBAL_PT_FLOAT_PRECISION", | ||
| "JIT", | ||
| "LOCAL_RANK", | ||
| "NUM_WORKERS", | ||
| "PRECISION_DICT", | ||
| "RESERVED_PRECISION_DICT", | ||
| "SAMPLER_RECORD", | ||
| ] | ||
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.