-
Notifications
You must be signed in to change notification settings - Fork 542
add universal Python inference interface DeepPot #3164
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from .deep_pot import ( | ||
DeepPot, | ||
) | ||
|
||
__all__ = ["DeepPot"] |
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,33 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from enum import ( | ||
Enum, | ||
) | ||
|
||
|
||
class DPBackend(Enum): | ||
"""DeePMD-kit backend.""" | ||
|
||
TensorFlow = 1 | ||
PyTorch = 2 | ||
Paddle = 3 | ||
Unknown = 4 | ||
|
||
|
||
def detect_backend(filename: str) -> DPBackend: | ||
"""Detect the backend of the given model file. | ||
|
||
Parameters | ||
---------- | ||
filename : str | ||
The model file name | ||
""" | ||
if filename.endswith(".pb"): | ||
return DPBackend.TensorFlow | ||
elif filename.endswith(".pth") or filename.endswith(".pt"): | ||
return DPBackend.PyTorch | ||
elif filename.endswith(".pdmodel"): | ||
return DPBackend.Paddle | ||
return DPBackend.Unknown | ||
|
||
|
||
__all__ = ["DPBackend", "detect_backend"] |
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,88 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from abc import ( | ||
ABC, | ||
abstractmethod, | ||
) | ||
from typing import ( | ||
List, | ||
Optional, | ||
Tuple, | ||
Union, | ||
) | ||
|
||
import numpy as np | ||
|
||
from deepmd_utils.utils import ( | ||
AutoBatchSize, | ||
) | ||
|
||
from .backend import ( | ||
Backend, | ||
detect_backend, | ||
) | ||
|
||
|
||
class DeepPot(ABC): | ||
"""Potential energy model. | ||
|
||
Parameters | ||
---------- | ||
model_file : Path | ||
The name of the frozen model file. | ||
auto_batch_size : bool or int or AutoBatchSize, default: True | ||
If True, automatic batch size will be used. If int, it will be used | ||
as the initial batch size. | ||
neighbor_list : ase.neighborlist.NewPrimitiveNeighborList, optional | ||
The ASE neighbor list class to produce the neighbor list. If None, the | ||
neighbor list will be built natively in the model. | ||
""" | ||
|
||
@abstractmethod | ||
def __init__( | ||
self, | ||
model_file, | ||
*args, | ||
auto_batch_size: Union[bool, int, AutoBatchSize] = True, | ||
neighbor_list=None, | ||
**kwargs, | ||
) -> None: | ||
... | ||
|
||
|
||
def __new__(cls, model_file: str, *args, **kwargs): | ||
if cls is DeepPot: | ||
backend = detect_backend(model_file) | ||
if backend == Backend.TensorFlow: | ||
from deepmd.infer.deep_pot import DeepPot as DeepPotTF | ||
|
||
|
||
return super().__new__(DeepPotTF) | ||
elif backend == Backend.PyTorch: | ||
from deepmd_pt.infer.deep_eval import DeepPot as DeepPotPT | ||
|
||
return super().__new__(DeepPotPT) | ||
else: | ||
raise NotImplementedError("Unsupported backend: " + str(backend)) | ||
return super().__new__(cls) | ||
|
||
@abstractmethod | ||
def eval( | ||
self, | ||
coords: np.ndarray, | ||
cells: np.ndarray, | ||
atom_types: List[int], | ||
atomic: bool = False, | ||
fparam: Optional[np.ndarray] = None, | ||
aparam: Optional[np.ndarray] = None, | ||
efield: Optional[np.ndarray] = None, | ||
mixed_type: bool = False, | ||
) -> Tuple[np.ndarray, ...]: | ||
"""Evaluate the model.""" | ||
# This method has been used by: | ||
# documentation python.md | ||
# dp model_devi: +fparam, +aparam, +mixed_type | ||
# dp test: +atomic, +fparam, +aparam, +efield, +mixed_type | ||
# finetune: +mixed_type | ||
# dpdata | ||
# ase | ||
|
||
|
||
__all__ = ["DeepPot"] |
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.
Check notice
Code scanning / CodeQL
Cyclic import