forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'deepmodeling:devel' into devel
- Loading branch information
Showing
6 changed files
with
185 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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,76 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
import functools | ||
import operator | ||
from pathlib import ( | ||
Path, | ||
) | ||
from typing import ( | ||
Optional, | ||
Type, | ||
Union, | ||
) | ||
|
||
from deepmd.backend.backend import ( | ||
Backend, | ||
) | ||
|
||
|
||
def format_model_suffix( | ||
filename: str, | ||
feature: Optional[Backend.Feature] = None, | ||
preferred_backend: Optional[Union[str, Type["Backend"]]] = None, | ||
strict_prefer: Optional[bool] = None, | ||
) -> str: | ||
"""Check and format the suffixes of a filename. | ||
When preferred_backend is not given, this method checks the suffix of the filename | ||
is within the suffixes of the any backends (with the given feature) and doesn't do formating. | ||
When preferred_backend is given, strict_prefer must be given. | ||
If strict_prefer is True and the suffix is not within the suffixes of the preferred backend, | ||
or strict_prefer is False and the suffix is not within the suffixes of the any backend with the given feature, | ||
the filename will be formatted with the preferred suffix of the preferred backend. | ||
Parameters | ||
---------- | ||
filename : str | ||
The filename to be formatted. | ||
feature : Backend.Feature, optional | ||
The feature of the backend, by default None | ||
preferred_backend : str or type of Backend, optional | ||
The preferred backend, by default None | ||
strict_prefer : bool, optional | ||
Whether to strictly prefer the preferred backend, by default None | ||
Returns | ||
------- | ||
str | ||
The formatted filename with the correct suffix. | ||
Raises | ||
------ | ||
ValueError | ||
When preferred_backend is not given and the filename is not supported by any backend. | ||
""" | ||
if preferred_backend is not None and strict_prefer is None: | ||
raise ValueError("strict_prefer must be given when preferred_backend is given.") | ||
if isinstance(preferred_backend, str): | ||
preferred_backend = Backend.get_backend(preferred_backend) | ||
if preferred_backend is not None and strict_prefer: | ||
all_backends = [preferred_backend] | ||
elif feature is None: | ||
all_backends = list(Backend.get_backends().values()) | ||
else: | ||
all_backends = list(Backend.get_backends_by_feature(feature).values()) | ||
|
||
all_suffixes = set( | ||
functools.reduce( | ||
operator.iconcat, [backend.suffixes for backend in all_backends], [] | ||
) | ||
) | ||
pp = Path(filename) | ||
current_suffix = pp.suffix | ||
if current_suffix not in all_suffixes: | ||
if preferred_backend is not None: | ||
return str(pp) + preferred_backend.suffixes[0] | ||
raise ValueError(f"Unsupported model file format: {filename}") | ||
return filename |
This file contains 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,80 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
"""Common entrypoints.""" | ||
|
||
import argparse | ||
from pathlib import ( | ||
Path, | ||
) | ||
|
||
from deepmd.backend.backend import ( | ||
Backend, | ||
) | ||
from deepmd.backend.suffix import ( | ||
format_model_suffix, | ||
) | ||
from deepmd.entrypoints.doc import ( | ||
doc_train_input, | ||
) | ||
from deepmd.entrypoints.gui import ( | ||
start_dpgui, | ||
) | ||
from deepmd.entrypoints.neighbor_stat import ( | ||
neighbor_stat, | ||
) | ||
from deepmd.entrypoints.test import ( | ||
test, | ||
) | ||
from deepmd.infer.model_devi import ( | ||
make_model_devi, | ||
) | ||
from deepmd.loggers.loggers import ( | ||
set_log_handles, | ||
) | ||
|
||
|
||
def main(args: argparse.Namespace): | ||
"""DeePMD-Kit entry point. | ||
Parameters | ||
---------- | ||
args : List[str] or argparse.Namespace, optional | ||
list of command line arguments, used to avoid calling from the subprocess, | ||
as it is quite slow to import tensorflow; if Namespace is given, it will | ||
be used directly | ||
Raises | ||
------ | ||
RuntimeError | ||
if no command was input | ||
""" | ||
set_log_handles(args.log_level, Path(args.log_path) if args.log_path else None) | ||
|
||
dict_args = vars(args) | ||
|
||
if args.command == "test": | ||
dict_args["model"] = format_model_suffix( | ||
dict_args["model"], | ||
feature=Backend.Feature.DEEP_EVAL, | ||
preferred_backend=args.backend, | ||
strict_prefer=False, | ||
) | ||
test(**dict_args) | ||
elif args.command == "doc-train-input": | ||
doc_train_input(**dict_args) | ||
elif args.command == "model-devi": | ||
dict_args["models"] = [ | ||
format_model_suffix( | ||
mm, | ||
feature=Backend.Feature.DEEP_EVAL, | ||
preferred_backend=args.backend, | ||
strict_prefer=False, | ||
) | ||
for mm in dict_args["models"] | ||
] | ||
make_model_devi(**dict_args) | ||
elif args.command == "neighbor-stat": | ||
neighbor_stat(**dict_args) | ||
elif args.command == "gui": | ||
start_dpgui(**dict_args) | ||
else: | ||
raise ValueError(f"Unknown command: {args.command}") |
This file contains 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 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 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 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