Skip to content
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

pd: support paddle backend and water/se_e2_a #4302

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
48f77f3
add core modules of paddle backend and water/se_e2_a example
HydrogenSulfate Nov 2, 2024
2082a59
add paddle code in consistent test
HydrogenSulfate Nov 2, 2024
2ae45b8
clean env and training
HydrogenSulfate Nov 2, 2024
7f03a04
add more test files
HydrogenSulfate Nov 2, 2024
4d1c44c
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 2, 2024
72c9b4e
fix pt->pd
HydrogenSulfate Nov 2, 2024
3b1c348
update test_python.yml
HydrogenSulfate Nov 2, 2024
a46dcb5
restore .pre-commit-config.yaml
HydrogenSulfate Nov 3, 2024
90f9ff9
remove redundant file
HydrogenSulfate Nov 3, 2024
0a6baa6
Skip bfloat16 for some cases
HydrogenSulfate Nov 3, 2024
4b77e55
enable prim by default in unitest
HydrogenSulfate Nov 3, 2024
6e139a2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 3, 2024
9437957
fix env code
HydrogenSulfate Nov 5, 2024
f1d762f
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 5, 2024
8534597
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 6, 2024
c22b45d
update test_ener.py
HydrogenSulfate Nov 6, 2024
39842ff
add missing pd_class
HydrogenSulfate Nov 6, 2024
07cd98e
use paddle Tensor instead of numpy array in pd/test_auto_batch_size.p…
HydrogenSulfate Nov 6, 2024
bb2d547
add training test and remove ase_calc.py
HydrogenSulfate Nov 7, 2024
5fb6d8e
add training test and remove ase_calc.py
HydrogenSulfate Nov 7, 2024
91066f8
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 7, 2024
90c9c03
upload missing json
HydrogenSulfate Nov 7, 2024
eb7384e
restore pt/test_auto_batch_size.py
HydrogenSulfate Nov 7, 2024
9faf54f
rerun CI for network problem
HydrogenSulfate Nov 7, 2024
4e3a121
add multitask unitest
HydrogenSulfate Nov 7, 2024
18333ab
add more unitest
HydrogenSulfate Nov 7, 2024
f9c6da8
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 7, 2024
3fd979d
remove redundant file and fix typo
HydrogenSulfate Nov 7, 2024
5922e84
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 7, 2024
f5a17a9
update unitest
HydrogenSulfate Nov 8, 2024
8bea1bf
delete record
HydrogenSulfate Nov 8, 2024
8a7875f
remove more unused code and files
HydrogenSulfate Nov 8, 2024
df9f887
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 8, 2024
67b81e1
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 8, 2024
b0bf733
Merge branch 'add_paddle_backend_core_and_water_se_e2_a' of https://g…
HydrogenSulfate Nov 8, 2024
71a3c0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 8, 2024
ede5047
remove redundant annotations
HydrogenSulfate Nov 8, 2024
d11bf4d
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 8, 2024
b7a8cec
add nvtx profiler code in training, which is more accurate and detailed
HydrogenSulfate Nov 8, 2024
7567cf8
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 8, 2024
416fec8
update code as devel and fix typo
HydrogenSulfate Nov 9, 2024
1c0161c
fix pth -> json
HydrogenSulfate Nov 9, 2024
02a6f84
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 9, 2024
3354e5c
update unitest and training
HydrogenSulfate Nov 9, 2024
0d3f8cf
install paddle when test_cuda
HydrogenSulfate Nov 9, 2024
18215ff
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 9, 2024
859b94d
fix unitest
HydrogenSulfate Nov 9, 2024
74ee1c2
add eta in logging message for convenient
HydrogenSulfate Nov 9, 2024
f176309
remove hybrid code and enable one unitest
HydrogenSulfate Nov 9, 2024
4935e7b
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 9, 2024
fac51d3
add pd/__init__.py
HydrogenSulfate Nov 11, 2024
d3ca1f0
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 11, 2024
db1cd76
fix enable_prim
HydrogenSulfate Nov 11, 2024
36512fd
remove unused layernorm
HydrogenSulfate Nov 11, 2024
5869a95
Update test_cuda.yml
HydrogenSulfate Nov 12, 2024
1f108bd
Update test_cuda.yml
HydrogenSulfate Nov 12, 2024
f9b09f6
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 14, 2024
6e6dedc
use custom format_training_message
HydrogenSulfate Nov 14, 2024
8cdef03
Merge branch 'add_paddle_backend_core_and_water_se_e2_a' of https://g…
HydrogenSulfate Nov 14, 2024
f0fb516
restore pre-commit
HydrogenSulfate Nov 14, 2024
f1fe02a
fix pre-commit ci fail
HydrogenSulfate Nov 14, 2024
27e6f2f
prune redundant files
HydrogenSulfate Nov 15, 2024
f272cd9
Merge branch 'devel' into add_paddle_backend_core_and_water_se_e2_a
HydrogenSulfate Nov 15, 2024
6c6ca34
fix unitest import
HydrogenSulfate Nov 15, 2024
7e05c35
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test_cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
- run: |
export PYTORCH_ROOT=$(python -c 'import torch;print(torch.__path__[0])')
export TENSORFLOW_ROOT=$(python -c 'import importlib,pathlib;print(pathlib.Path(importlib.util.find_spec("tensorflow").origin).parent)')
source/install/uv_with_retry.sh pip install --system --pre paddlepaddle-gpu -i https://www.paddlepaddle.org.cn/packages/nightly/cu123/
source/install/uv_with_retry.sh pip install --system -v -e .[gpu,test,lmp,cu12,torch,jax] mpi4py
env:
DP_VARIANT: cuda
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
export PYTORCH_ROOT=$(python -c 'import torch;print(torch.__path__[0])')
source/install/uv_with_retry.sh pip install --system -e .[test,jax] mpi4py
source/install/uv_with_retry.sh pip install --system horovod --no-build-isolation
source/install/uv_with_retry.sh pip install --system --pre "paddlepaddle" -i https://www.paddlepaddle.org.cn/packages/nightly/cpu/
env:
# Please note that uv has some issues with finding
# existing TensorFlow package. Currently, it uses
Expand Down
133 changes: 133 additions & 0 deletions backend/find_paddle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import importlib
import os
import site
from functools import (
lru_cache,
)
from importlib.machinery import (
FileFinder,
)
from importlib.util import (
find_spec,
)
from pathlib import (
Path,
)
from sysconfig import (
get_path,
)
from typing import (
Optional,
Union,
)


@lru_cache
def find_paddle() -> tuple[Optional[str], list[str]]:
"""Find PaddlePadle library.

Tries to find PaddlePadle in the order of:

1. Environment variable `PADDLE_ROOT` if set
2. The current Python environment.
3. user site packages directory if enabled
4. system site packages directory (purelib)

Considering the default PaddlePadle package still uses old CXX11 ABI, we
cannot install it automatically.

Returns
-------
str, optional
PaddlePadle library path if found.
list of str
Paddle requirement if not found. Empty if found.
"""
if os.environ.get("DP_ENABLE_PADDLE", "0") == "0":
return None, []
requires = []
pd_spec = None

if (pd_spec is None or not pd_spec) and os.environ.get("PADDLE_ROOT") is not None:
site_packages = Path(os.environ.get("PADDLE_ROOT")).parent.absolute()
pd_spec = FileFinder(str(site_packages)).find_spec("paddle")

# get paddle spec
# note: isolated build will not work for backend
if pd_spec is None or not pd_spec:
pd_spec = find_spec("paddle")

if not pd_spec and site.ENABLE_USER_SITE:
# first search TF from user site-packages before global site-packages
site_packages = site.getusersitepackages()
if site_packages:
pd_spec = FileFinder(site_packages).find_spec("paddle")

if not pd_spec:
# purelib gets site-packages path
site_packages = get_path("purelib")
if site_packages:
pd_spec = FileFinder(site_packages).find_spec("paddle")

# get install dir from spec
try:
pd_install_dir = pd_spec.submodule_search_locations[0] # type: ignore
# AttributeError if ft_spec is None
# TypeError if submodule_search_locations are None
# IndexError if submodule_search_locations is an empty list
except (AttributeError, TypeError, IndexError):
pd_install_dir = None
requires.extend(get_pd_requirement()["paddle"])
return pd_install_dir, requires


@lru_cache
def get_pd_requirement(pd_version: str = "") -> dict:
"""Get PaddlePadle requirement when Paddle is not installed.

If pd_version is not given and the environment variable `PADDLE_VERSION` is set, use it as the requirement.

Parameters
----------
pd_version : str, optional
Paddle version

Returns
-------
dict
PaddlePadle requirement.
"""
if pd_version is None:
return {"paddle": []}
if pd_version == "":
pd_version = os.environ.get("PADDLE_VERSION", "")

return {
"paddle": [
"paddlepaddle>=3.0.0b1" if pd_version != "" else "paddlepaddle>=3.0.0b1",
],
}


@lru_cache
def get_pd_version(pd_path: Optional[Union[str, Path]]) -> str:
"""Get Paddle version from a Paddle Python library path.

Parameters
----------
pd_path : str or Path
Paddle Python library path, e.g. "/python3.10/site-packages/paddle/"

Returns
-------
str
version
"""
if pd_path is None or pd_path == "":
return ""
version_file = Path(pd_path) / "version" / "__init__.py"
spec = importlib.util.spec_from_file_location("paddle.version", version_file)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module.full_version
124 changes: 124 additions & 0 deletions deepmd/backend/paddle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from importlib.util import (
find_spec,
)
from typing import (
TYPE_CHECKING,
Callable,
ClassVar,
)

from deepmd.backend.backend import (
Backend,
)

if TYPE_CHECKING:
from argparse import (

Check warning on line 16 in deepmd/backend/paddle.py

View check run for this annotation

Codecov / codecov/patch

deepmd/backend/paddle.py#L16

Added line #L16 was not covered by tests
Namespace,
)

from deepmd.infer.deep_eval import (

Check warning on line 20 in deepmd/backend/paddle.py

View check run for this annotation

Codecov / codecov/patch

deepmd/backend/paddle.py#L20

Added line #L20 was not covered by tests
DeepEvalBackend,
)
from deepmd.utils.neighbor_stat import (

Check warning on line 23 in deepmd/backend/paddle.py

View check run for this annotation

Codecov / codecov/patch

deepmd/backend/paddle.py#L23

Added line #L23 was not covered by tests
NeighborStat,
)


@Backend.register("pd")
@Backend.register("paddle")
class PaddleBackend(Backend):
"""Paddle backend."""

name = "Paddle"
"""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]] = [".json", ".pd"]
"""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("paddle") 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.pd.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.pd.infer.deep_eval import DeepEval as DeepEvalPD

return DeepEvalPD

@property
def neighbor_stat(self) -> type["NeighborStat"]:
"""The neighbor statistics of the backend.

Returns
-------
type[NeighborStat]
The neighbor statistics of the backend.
"""
from deepmd.pd.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.pd.utils.serialization import (

Check warning on line 105 in deepmd/backend/paddle.py

View check run for this annotation

Codecov / codecov/patch

deepmd/backend/paddle.py#L105

Added line #L105 was not covered by tests
serialize_from_file,
)

return serialize_from_file

Check warning on line 109 in deepmd/backend/paddle.py

View check run for this annotation

Codecov / codecov/patch

deepmd/backend/paddle.py#L109

Added line #L109 was not covered by tests

@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.pd.utils.serialization import (

Check warning on line 120 in deepmd/backend/paddle.py

View check run for this annotation

Codecov / codecov/patch

deepmd/backend/paddle.py#L120

Added line #L120 was not covered by tests
deserialize_to_file,
)

return deserialize_to_file

Check warning on line 124 in deepmd/backend/paddle.py

View check run for this annotation

Codecov / codecov/patch

deepmd/backend/paddle.py#L124

Added line #L124 was not covered by tests
2 changes: 1 addition & 1 deletion deepmd/dpmodel/model/make_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def format_nlist(

Returns
-------
formated_nlist
formatted_nlist
the formatted nlist.

"""
Expand Down
3 changes: 2 additions & 1 deletion deepmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ def main_parser() -> argparse.ArgumentParser:
formatter_class=RawTextArgumentDefaultsHelpFormatter,
epilog=textwrap.dedent(
"""\
Use --tf or --pt to choose the backend:
Use --tf, --pt or --pd to choose the backend:
dp --tf train input.json
dp --pt train input.json
dp --pd train input.json
"""
),
)
Expand Down
11 changes: 11 additions & 0 deletions deepmd/pd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: LGPL-3.0-or-later

# import customized OPs globally

from deepmd.utils.entry_point import (
load_entry_point,
)

load_entry_point("deepmd.pd")

__all__ = []
1 change: 1 addition & 0 deletions deepmd/pd/entrypoints/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
Loading