Skip to content

Commit b7effe5

Browse files
HydrogenSulfatenjzjzpre-commit-ci[bot]
authored
pd: add CPP inference with LAMMPS (#4467)
1. support LAMMPS inference with Paddle backend on cuda and dcu device 2. Fix `deepmd/pd/utils/serialization.py` and paddle inference files can be deserialized using `dp convert-backend deeppot_sea.yaml deeppot_sea.json` related PR: 1. PaddlePaddle/Paddle#70545 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit Based on the comprehensive changes, here are the updated release notes: - **New Features** - Added support for PaddlePaddle deep learning framework. - Introduced new Paddle-based deep potential model computations. - Expanded backend support for model inference with Paddle. - **Configuration** - New CMake option `ENABLE_PADDLE` to toggle Paddle support. - Added configuration parameters for Paddle version and inference directory. - **Testing** - Comprehensive test suites added for Paddle backend. - Enhanced LAMMPS integration tests with Paddle support. - **Documentation** - Updated version header and configuration files to reflect Paddle integration. - **Performance** - Added JIT compilation for Paddle model methods. - Optimized model serialization and deserialization. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: HydrogenSulfate <490868991@qq.com> Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu> Co-authored-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 65ca05a commit b7effe5

24 files changed

+2582
-13
lines changed

.devcontainer/build_cxx.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mkdir -p ${SCRIPT_PATH}/../buildcxx/
1111
cd ${SCRIPT_PATH}/../buildcxx/
1212
cmake -D ENABLE_TENSORFLOW=ON \
1313
-D ENABLE_PYTORCH=ON \
14+
-D ENABLE_PADDLE=ON \
1415
-D CMAKE_INSTALL_PREFIX=${SCRIPT_PATH}/../dp/ \
1516
-D LAMMPS_VERSION=stable_29Aug2024_update1 \
1617
-D CMAKE_BUILD_TYPE=Debug \

.github/workflows/suppr.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
leak:libpaddle_inference

.github/workflows/test_cc.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ jobs:
5252
LMP_CXX11_ABI_0: 1
5353
CMAKE_GENERATOR: Ninja
5454
CXXFLAGS: ${{ matrix.check_memleak && '-fsanitize=leak' || '' }}
55+
LSAN_OPTIONS: suppressions=${{ github.workspace }}/.github/workflows/suppr.txt
5556
# test lammps
56-
- run: pytest --cov=deepmd source/lmp/tests
57+
- run: |
58+
cp ${{ github.workspace }}/source/build_tests/paddle_inference_install_dir/paddle/lib/*.so ${{ github.workspace }}/dp_test/lib/
59+
cp ${{ github.workspace }}/source/build_tests/paddle_inference_install_dir/third_party/install/onednn/lib/* ${{ github.workspace }}/dp_test/lib/
60+
cp ${{ github.workspace }}/source/build_tests/paddle_inference_install_dir/third_party/install/mklml/lib/* ${{ github.workspace }}/dp_test/lib/
61+
pytest --cov=deepmd source/lmp/tests
5762
env:
5863
OMP_NUM_THREADS: 1
5964
TF_INTRA_OP_PARALLELISM_THREADS: 1
@@ -62,12 +67,16 @@ jobs:
6267
LD_LIBRARY_PATH: ${{ github.workspace }}/dp_test/lib:${{ github.workspace }}/libtorch/lib
6368
if: ${{ !matrix.check_memleak }}
6469
# test ipi
65-
- run: pytest --cov=deepmd source/ipi/tests
70+
- run: |
71+
export PATH=${{ github.workspace }}/dp_test/bin:$PATH
72+
cp ${{ github.workspace }}/source/build_tests/paddle_inference_install_dir/paddle/lib/*.so ${{ github.workspace }}/dp_test/lib/
73+
cp ${{ github.workspace }}/source/build_tests/paddle_inference_install_dir/third_party/install/onednn/lib/* ${{ github.workspace }}/dp_test/lib/
74+
cp ${{ github.workspace }}/source/build_tests/paddle_inference_install_dir/third_party/install/mklml/lib/* ${{ github.workspace }}/dp_test/lib/
75+
pytest --cov=deepmd source/ipi/tests
6676
env:
6777
OMP_NUM_THREADS: 1
6878
TF_INTRA_OP_PARALLELISM_THREADS: 1
6979
TF_INTER_OP_PARALLELISM_THREADS: 1
70-
PATH: ${{ github.workspace }}/dp_test/bin:$PATH
7180
LD_LIBRARY_PATH: ${{ github.workspace }}/dp_test/lib:${{ github.workspace }}/libtorch/lib
7281
if: ${{ !matrix.check_memleak }}
7382
- uses: codecov/codecov-action@v5

.github/workflows/test_cuda.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ jobs:
8585
- run: |
8686
export LD_LIBRARY_PATH=$CUDA_PATH/lib64:/usr/lib/x86_64-linux-gnu/:$GITHUB_WORKSPACE/dp_test/lib:$GITHUB_WORKSPACE/libtorch/lib:$LD_LIBRARY_PATH
8787
export PATH=$GITHUB_WORKSPACE/dp_test/bin:$PATH
88+
cp $GITHUB_WORKSPACE/source/build_tests/paddle_inference_install_dir/paddle/lib/* $GITHUB_WORKSPACE/dp_test/lib/
89+
cp $GITHUB_WORKSPACE/source/build_tests/paddle_inference_install_dir/third_party/install/onednn/lib/* $GITHUB_WORKSPACE/dp_test/lib/
90+
cp $GITHUB_WORKSPACE/source/build_tests/paddle_inference_install_dir/third_party/install/mklml/lib/* $GITHUB_WORKSPACE/dp_test/lib/
8891
python -m pytest -s source/lmp/tests || (cat log.lammps && exit 1)
8992
python -m pytest source/ipi/tests
9093
env:

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repos:
77
- id: trailing-whitespace
88
exclude: "^.+\\.pbtxt$"
99
- id: end-of-file-fixer
10-
exclude: "^.+\\.pbtxt$"
10+
exclude: "^.+\\.pbtxt$|deeppot_sea.*\\.json$"
1111
- id: check-yaml
1212
- id: check-json
1313
- id: check-added-large-files
@@ -63,7 +63,7 @@ repos:
6363
rev: v19.1.6
6464
hooks:
6565
- id: clang-format
66-
exclude: ^(source/3rdparty|source/lib/src/gpu/cudart/.+\.inc|.+\.ipynb$)
66+
exclude: ^(source/3rdparty|source/lib/src/gpu/cudart/.+\.inc|.+\.ipynb$|.+\.json$)
6767
# markdown, yaml, CSS, javascript
6868
- repo: https://github.com/pre-commit/mirrors-prettier
6969
rev: v4.0.0-alpha.8

deepmd/pd/utils/serialization.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
2-
import json
32

43
import paddle
54

@@ -34,21 +33,71 @@ def deserialize_to_file(model_file: str, data: dict) -> None:
3433
data : dict
3534
The dictionary to be deserialized.
3635
"""
36+
paddle.framework.core._set_prim_all_enabled(True)
3737
if not model_file.endswith(".json"):
3838
raise ValueError("Paddle backend only supports converting .json file")
39-
model = BaseModel.deserialize(data["model"])
39+
model: paddle.nn.Layer = BaseModel.deserialize(data["model"])
40+
model.eval()
4041
# JIT will happy in this way...
41-
model.model_def_script = json.dumps(data["model_def_script"])
4242
if "min_nbor_dist" in data.get("@variables", {}):
43-
model.min_nbor_dist = float(data["@variables"]["min_nbor_dist"])
44-
# model = paddle.jit.to_static(model)
43+
model.register_buffer(
44+
"buffer_min_nbor_dist",
45+
paddle.to_tensor(
46+
float(data["@variables"]["min_nbor_dist"]),
47+
),
48+
)
4549
paddle.set_flags(
4650
{
4751
"FLAGS_save_cf_stack_op": 1,
4852
"FLAGS_prim_enable_dynamic": 1,
4953
"FLAGS_enable_pir_api": 1,
5054
}
5155
)
56+
from paddle.static import (
57+
InputSpec,
58+
)
59+
60+
""" example output shape and dtype of forward
61+
atom_energy: fetch_name_0 (1, 6, 1) float64
62+
atom_virial: fetch_name_1 (1, 6, 1, 9) float64
63+
energy: fetch_name_2 (1, 1) float64
64+
force: fetch_name_3 (1, 6, 3) float64
65+
mask: fetch_name_4 (1, 6) int32
66+
virial: fetch_name_5 (1, 9) float64
67+
"""
68+
model.forward = paddle.jit.to_static(
69+
model.forward,
70+
full_graph=True,
71+
input_spec=[
72+
InputSpec([1, -1, 3], dtype="float64", name="coord"),
73+
InputSpec([1, -1], dtype="int64", name="atype"),
74+
InputSpec([1, 9], dtype="float64", name="box"),
75+
None,
76+
None,
77+
True,
78+
],
79+
)
80+
""" example output shape and dtype of forward_lower
81+
fetch_name_0: atom_energy [1, 192, 1] paddle.float64
82+
fetch_name_1: energy [1, 1] paddle.float64
83+
fetch_name_2: extended_force [1, 5184, 3] paddle.float64
84+
fetch_name_3: extended_virial [1, 5184, 1, 9] paddle.float64
85+
fetch_name_4: virial [1, 9] paddle.float64
86+
"""
87+
model.forward_lower = paddle.jit.to_static(
88+
model.forward_lower,
89+
full_graph=True,
90+
input_spec=[
91+
InputSpec([1, -1, 3], dtype="float64", name="coord"),
92+
InputSpec([1, -1], dtype="int32", name="atype"),
93+
InputSpec([1, -1, -1], dtype="int32", name="nlist"),
94+
None,
95+
None,
96+
None,
97+
True,
98+
None,
99+
],
100+
)
52101
paddle.jit.save(
53102
model,
54103
model_file.split(".json")[0],

source/CMakeLists.txt

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if(ENABLE_TENSORFLOW)
99
# JAX requires TF C interface, contained in TF C++ library
1010
set(ENABLE_JAX ON)
1111
endif()
12+
option(ENABLE_PADDLE "Enable Paddle interface" OFF)
1213
option(BUILD_TESTING "Build test and enable coverage" OFF)
1314
set(DEEPMD_C_ROOT
1415
""
@@ -26,6 +27,133 @@ if(NOT DEEPMD_C_ROOT)
2627
set_if_higher(CMAKE_CXX_STANDARD 14)
2728
endif()
2829

30+
if(ENABLE_PADDLE)
31+
if(NOT DEFINED PADDLE_INFERENCE_DIR)
32+
# message(FATAL_ERROR "Make sure PADDLE_INFERENCE_DIR is set when
33+
# ENABLE_PADDLE=ON")
34+
if(USE_CUDA_TOOLKIT)
35+
find_package(CUDAToolkit REQUIRED)
36+
string(REGEX MATCH "^[0-9]+" CUDA_MAJOR_VERSION "${CUDAToolkit_VERSION}")
37+
message(STATUS "Find CUDAToolkit_VERSION: ${CUDAToolkit_VERSION}")
38+
if(CUDA_MAJOR_VERSION VERSION_EQUAL "11")
39+
message(
40+
STATUS
41+
"PADDLE_INFERENCE_DIR is not defined, downloading CUDA11.8 infernece lib to: ${CMAKE_BINARY_DIR}/"
42+
)
43+
set(DOWNLOAD_URL
44+
"https://paddle-qa.bj.bcebos.com/paddle-pipeline/GITHUB_Docker_Compile_Test_Cuda118_cudnn860_Trt8531_D1/latest/paddle_inference.tgz"
45+
)
46+
elseif(CUDA_MAJOR_VERSION VERSION_EQUAL "12")
47+
message(
48+
STATUS
49+
"PADDLE_INFERENCE_DIR is not defined, downloading CUDA12.3 infernece lib to: ${CMAKE_BINARY_DIR}/"
50+
)
51+
set(DOWNLOAD_URL
52+
"https://paddle-qa.bj.bcebos.com/paddle-pipeline/GITHUB_Docker_Compile_Test_Cuda123_cudnn900_Trt8616_D1/latest/paddle_inference.tgz"
53+
)
54+
else()
55+
message(
56+
FATAL_ERROR
57+
"Paddle inference lib only support cuda 11 or 12, but your CUDA_MAJOR_VERSION is: ${CUDA_MAJOR_VERSION}"
58+
)
59+
endif()
60+
else()
61+
message(
62+
STATUS
63+
"PADDLE_INFERENCE_DIR is not defined, downloading CPU infernece lib to: ${CMAKE_BINARY_DIR}/"
64+
)
65+
set(DOWNLOAD_URL
66+
"https://paddle-qa.bj.bcebos.com/paddle-pipeline/GITHUB_Docker_Compile_Test_Cpu_Mkl_Avx_D1/latest/paddle_inference.tgz"
67+
)
68+
endif()
69+
set(TGZ_FILE "${CMAKE_BINARY_DIR}/paddle_inference.tgz")
70+
set(EXTRACTED_DIR "${CMAKE_BINARY_DIR}/paddle_inference_install_dir")
71+
file(DOWNLOAD ${DOWNLOAD_URL} ${TGZ_FILE})
72+
message(STATUS "Downloading finished, extracting...")
73+
execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xzvf ${TGZ_FILE}
74+
OUTPUT_QUIET)
75+
file(REMOVE ${TGZ_FILE})
76+
set(PADDLE_INFERENCE_DIR
77+
${EXTRACTED_DIR}
78+
CACHE PATH
79+
"Path to 'paddle_inference_install_dir' or 'paddle_inference'")
80+
else()
81+
message(
82+
STATUS "PADDLE_INFERENCE_DIR is already defined: ${PADDLE_INFERENCE_DIR}")
83+
endif()
84+
85+
link_directories(
86+
${PADDLE_INFERENCE_DIR}/paddle/lib
87+
${PADDLE_INFERENCE_DIR}/third_party/install/onednn/lib
88+
${PADDLE_INFERENCE_DIR}/third_party/install/mklml/lib)
89+
90+
# Check and append to existing CMAKE_BUILD_RPATH
91+
if(DEFINED CMAKE_BUILD_RPATH)
92+
list(APPEND CMAKE_BUILD_RPATH "${PADDLE_INFERENCE_DIR}/paddle/lib"
93+
"${PADDLE_INFERENCE_DIR}/third_party/install/onednn/lib"
94+
"${PADDLE_INFERENCE_DIR}/third_party/install/mklml/lib")
95+
else()
96+
set(CMAKE_BUILD_RPATH
97+
"${PADDLE_INFERENCE_DIR}/paddle/lib"
98+
"${PADDLE_INFERENCE_DIR}/third_party/install/onednn/lib"
99+
"${PADDLE_INFERENCE_DIR}/third_party/install/mklml/lib")
100+
endif()
101+
102+
# Check and append to existing CMAKE_INSTALL_RPATH
103+
if(DEFINED CMAKE_INSTALL_RPATH)
104+
list(APPEND CMAKE_INSTALL_RPATH "${PADDLE_INFERENCE_DIR}/paddle/lib"
105+
"${PADDLE_INFERENCE_DIR}/third_party/install/onednn/lib"
106+
"${PADDLE_INFERENCE_DIR}/third_party/install/mklml/lib")
107+
else()
108+
set(CMAKE_INSTALL_RPATH
109+
"${PADDLE_INFERENCE_DIR}/paddle/lib"
110+
"${PADDLE_INFERENCE_DIR}/third_party/install/onednn/lib"
111+
"${PADDLE_INFERENCE_DIR}/third_party/install/mklml/lib")
112+
endif()
113+
114+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
115+
message(STATUS "Final PADDLE_INFERENCE_DIR is set to ${PADDLE_INFERENCE_DIR}")
116+
117+
set(PADDLE_INFERENCE_DIR
118+
${PADDLE_INFERENCE_DIR}
119+
CACHE PATH "Path to 'paddle_inference_install_dir' or 'paddle_inference'")
120+
121+
# used in api_cc
122+
set(PADDLE_LIBRARIES
123+
"${PADDLE_INFERENCE_DIR}/paddle/lib/libpaddle_inference.so"
124+
CACHE PATH "Path to libpaddle_inference.so")
125+
126+
include_directories("${PADDLE_INFERENCE_DIR}/")
127+
set(PADDLE_LIB_THIRD_PARTY_PATH
128+
"${PADDLE_INFERENCE_DIR}/third_party/install/")
129+
130+
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}protobuf/include")
131+
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}glog/include")
132+
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}gflags/include")
133+
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}xxhash/include")
134+
list(APPEND BACKEND_INCLUDE_DIRS "${PADDLE_INFERENCE_DIR}/paddle/include")
135+
list(APPEND BACKEND_INCLUDE_DIRS
136+
"${PADDLE_LIB_THIRD_PARTY_PATH}protobuf/include")
137+
list(APPEND BACKEND_INCLUDE_DIRS "${PADDLE_LIB_THIRD_PARTY_PATH}glog/include")
138+
list(APPEND BACKEND_INCLUDE_DIRS
139+
"${PADDLE_LIB_THIRD_PARTY_PATH}gflags/include")
140+
list(APPEND BACKEND_INCLUDE_DIRS
141+
"${PADDLE_LIB_THIRD_PARTY_PATH}xxhash/include")
142+
143+
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}protobuf/lib")
144+
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}glog/lib")
145+
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}gflags/lib")
146+
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}xxhash/lib")
147+
link_directories("${PADDLE_INFERENCE_DIR}/paddle/lib")
148+
list(APPEND BACKEND_LIBRARY_PATH "${PADDLE_INFERENCE_DIR}/paddle/lib")
149+
list(APPEND BACKEND_LIBRARY_PATH
150+
"${PADDLE_INFERENCE_DIR}/third_party/install/onednn/lib")
151+
list(APPEND BACKEND_LIBRARY_PATH
152+
"${PADDLE_INFERENCE_DIR}/third_party/install/mklml/lib")
153+
154+
# if (USE_ROCM_TOOLKIT) add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1) endif()
155+
endif(ENABLE_PADDLE)
156+
29157
if(BUILD_TESTING)
30158
enable_testing()
31159
add_subdirectory(${CMAKE_SOURCE_DIR}/cmake/coverage_config coverage_config)
@@ -279,9 +407,13 @@ if(NOT DEEPMD_C_ROOT)
279407
if(ENABLE_JAX)
280408
message(STATUS "- JAX")
281409
endif()
410+
if(ENABLE_PADDLE)
411+
message(STATUS "- Paddle")
412+
endif()
282413
if(NOT ENABLE_TENSORFLOW
283414
AND NOT ENABLE_PYTORCH
284415
AND NOT ENABLE_JAX
416+
AND NOT ENABLE_PADDLE
285417
AND NOT BUILD_PY_IF)
286418
message(FATAL_ERROR "No backend is enabled.")
287419
endif()

source/api_cc/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ if(ENABLE_JAX)
2727
target_link_libraries(${libname} PRIVATE TensorFlow::tensorflow_c)
2828
target_compile_definitions(${libname} PRIVATE BUILD_JAX)
2929
endif()
30+
if(ENABLE_PADDLE AND NOT BUILD_PY_IF)
31+
target_link_libraries(${libname} PUBLIC "${PADDLE_LIBRARIES}")
32+
target_compile_definitions(${libname} PUBLIC BUILD_PADDLE)
33+
if(DP_VARIANT STREQUAL "rocm")
34+
target_link_libraries(${libname}
35+
PUBLIC "${hip_LIB_INSTALL_DIR}/libgalaxyhip.so")
36+
endif()
37+
endif()
3038

3139
target_include_directories(
3240
${libname}

0 commit comments

Comments
 (0)