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: add CPP inference with LAMMPS #4467

Open
wants to merge 56 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
bea3123
add LAMMPS CPP inference
HydrogenSulfate Dec 10, 2024
2e6cae4
update code
HydrogenSulfate Dec 10, 2024
a8145fa
update CPP inference test(deeppot_sea)
HydrogenSulfate Dec 27, 2024
0e6172d
refine code
HydrogenSulfate Dec 27, 2024
79f0b98
fix suggestions from code rabbit
HydrogenSulfate Dec 27, 2024
c2a9295
update
HydrogenSulfate Dec 31, 2024
5a048c4
update test shell
HydrogenSulfate Dec 31, 2024
8800bb0
change createNlistTensor to createNlistTensorPD to avoid mutliple def…
HydrogenSulfate Dec 31, 2024
269e96e
use CPU inference lib instead of GPU inference lib
HydrogenSulfate Dec 31, 2024
3f7e5af
resize output vector size before copy to CPU
HydrogenSulfate Dec 31, 2024
ff50600
fix test_deeppot_pd related code
HydrogenSulfate Jan 2, 2025
c24cee2
update CMAKE to avoid lib not found
HydrogenSulfate Jan 2, 2025
ee6547e
Merge branch 'devel' into add_cpp_infer
HydrogenSulfate Jan 2, 2025
2a1be37
update LD_LIBRARY_PATH
HydrogenSulfate Jan 3, 2025
ca60d08
move LD_LIBRARY_PATH to the begin position
HydrogenSulfate Jan 3, 2025
e26ca2a
update test_cc_local
HydrogenSulfate Jan 3, 2025
64ccf14
add paddle inference lib into 'LD_LIBRARY_PATH' in test_cc
HydrogenSulfate Jan 3, 2025
5470ce0
fix 29814 typo
HydrogenSulfate Jan 3, 2025
aaf3131
update mkl and onednn lib in test_cc
HydrogenSulfate Jan 3, 2025
a24a6e9
output more details in fsanitize
HydrogenSulfate Jan 5, 2025
7e6f5f4
test for line number
HydrogenSulfate Jan 6, 2025
7030ccc
add fsanitize-ignorelist for some mem leak can be ignored currently
HydrogenSulfate Jan 7, 2025
94793f3
Merge branch 'devel' into add_cpp_infer
HydrogenSulfate Jan 7, 2025
0e22018
fix fsanitize-ignorelist path
HydrogenSulfate Jan 7, 2025
4e7183f
use $ instead of 29814
HydrogenSulfate Jan 7, 2025
c3a2b27
Update .github/workflows/test_cc.yml
HydrogenSulfate Jan 7, 2025
0045f77
try format
njzjz Jan 7, 2025
d598c3e
use the clang compiler
njzjz Jan 7, 2025
5b663f1
enable more C++ test case
HydrogenSulfate Jan 7, 2025
cbbd11e
fix all serialization and inference code
HydrogenSulfate Jan 7, 2025
9f558e7
Merge branch 'add_cpp_infer' of https://github.com/HydrogenSulfate/de…
HydrogenSulfate Jan 8, 2025
a749056
restore gpu information printing code
HydrogenSulfate Jan 8, 2025
f48ba9b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 8, 2025
33219f1
solve mutiple definition of omp functions
HydrogenSulfate Jan 8, 2025
4801b2a
Merge branch 'add_cpp_infer' of https://github.com/HydrogenSulfate/de…
HydrogenSulfate Jan 8, 2025
f82aa99
fix dtype for inference model
HydrogenSulfate Jan 8, 2025
196f821
Merge branch 'devel' into add_cpp_infer
HydrogenSulfate Jan 8, 2025
76e9019
split pd C++ test from test_cc_local
HydrogenSulfate Jan 8, 2025
8e85649
split pd C++ test from test_cc_local
HydrogenSulfate Jan 8, 2025
2204844
export model in prim mode to bypass latent error in PNormGradKernel
HydrogenSulfate Jan 8, 2025
26f8010
Merge branch 'add_cpp_infer' of https://github.com/HydrogenSulfate/de…
HydrogenSulfate Jan 8, 2025
60dabca
add libpaddle_inference.so as module to igorelist.txt
HydrogenSulfate Jan 8, 2025
17b39c1
use LSAN_OPTIONS instead of fsanitize-ignorelist
HydrogenSulfate Jan 9, 2025
1679314
remove LD_LIBRARY_PATH in cov test env
HydrogenSulfate Jan 9, 2025
2e9bfa7
Merge branch 'devel' into add_cpp_infer
HydrogenSulfate Jan 9, 2025
2944d58
fix
HydrogenSulfate Jan 9, 2025
8e5a9f7
update source/lmp/plugin/CMakeLists.txt
HydrogenSulfate Jan 9, 2025
8940bd9
append original LD_LIBRARY_PATH to
HydrogenSulfate Jan 9, 2025
4bc5db3
copy paddle so instead of set LD_LIBRARY_PATH
HydrogenSulfate Jan 9, 2025
b761e83
revert Clang to GCC
njzjz Jan 9, 2025
38904c3
fix rtol to 1e-6 and atol to 1e-9
HydrogenSulfate Jan 9, 2025
58711c7
Merge branch 'add_cpp_infer' of https://github.com/HydrogenSulfate/de…
HydrogenSulfate Jan 9, 2025
d265094
update rtol and atol for python unitest
HydrogenSulfate Jan 10, 2025
05759cc
skip test_pair_deepmd_mpi
HydrogenSulfate Jan 10, 2025
50122a7
Update build_cxx.sh
HydrogenSulfate Jan 10, 2025
2a2fd1b
Fix PATH export location in test workflow
njzjz Jan 10, 2025
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
- id: trailing-whitespace
exclude: "^.+\\.pbtxt$"
- id: end-of-file-fixer
exclude: "^.+\\.pbtxt$"
exclude: "^.+\\.pbtxt$|deeppot_sea\\.json$"
- id: check-yaml
- id: check-json
- id: check-added-large-files
Expand Down Expand Up @@ -63,7 +63,7 @@ repos:
rev: v19.1.4
hooks:
- id: clang-format
exclude: ^(source/3rdparty|source/lib/src/gpu/cudart/.+\.inc|.+\.ipynb$)
exclude: ^(source/3rdparty|source/lib/src/gpu/cudart/.+\.inc|.+\.ipynb$|.+\.json$)
# markdown, yaml, CSS, javascript
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
Expand Down
18 changes: 15 additions & 3 deletions deepmd/pd/utils/serialization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import json

import paddle

Expand Down Expand Up @@ -38,7 +37,7 @@ def deserialize_to_file(model_file: str, data: dict) -> None:
raise ValueError("Paddle backend only supports converting .json file")
model = BaseModel.deserialize(data["model"])
# JIT will happy in this way...
model.model_def_script = json.dumps(data["model_def_script"])
# model.model_def_script = json.dumps(data["model_def_script"])
if "min_nbor_dist" in data.get("@variables", {}):
model.min_nbor_dist = float(data["@variables"]["min_nbor_dist"])
# model = paddle.jit.to_static(model)
Expand All @@ -49,7 +48,20 @@ def deserialize_to_file(model_file: str, data: dict) -> None:
"FLAGS_enable_pir_api": 1,
}
)
from paddle.static import (
InputSpec,
)

jit_model = paddle.jit.to_static(
model.forward_lower,
full_graph=True,
input_spec=[
InputSpec([-1, -1, 3], dtype="float64", name="coord"),
InputSpec([-1, -1], dtype="int32", name="atype"),
InputSpec([-1, -1, -1], dtype="int32", name="nlist"),
],
)
paddle.jit.save(
model,
jit_model,
model_file.split(".json")[0],
)
57 changes: 57 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if(ENABLE_TENSORFLOW)
# JAX requires TF C interface, contained in TF C++ library
set(ENABLE_JAX ON)
endif()
option(ENABLE_PADDLE "Enable Paddle interface" OFF)
option(BUILD_TESTING "Build test and enable coverage" OFF)
set(DEEPMD_C_ROOT
""
Expand All @@ -26,6 +27,58 @@ if(NOT DEEPMD_C_ROOT)
set_if_higher(CMAKE_CXX_STANDARD 14)
endif()

if(ENABLE_PADDLE)
if(NOT DEFINED PADDLE_INFERENCE_DIR)
# message( FATAL_ERROR "Make sure PADDLE_INFERENCE_DIR is set when
# ENABLE_PADDLE=ON")
message(STATUS "PADDLE_INFERENCE_DIR is not defined. Downloading...")
set(DOWNLOAD_URL
"https://paddle-qa.bj.bcebos.com/paddle-pipeline/GITHUB_Docker_Compile_Test_Cuda118_cudnn860_Trt8531_D1/ce51e82e84fc97e0a55a162037f1554746159cad/paddle_inference.tgz"
)
set(TGZ_FILE "${CMAKE_BINARY_DIR}/paddle_inference.tgz")
set(EXTRACTED_DIR "${CMAKE_BINARY_DIR}/paddle_inference_install_dir")
file(DOWNLOAD ${DOWNLOAD_URL} ${TGZ_FILE})
message(STATUS "Downloading finished, extracting...")
execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xzvf ${TGZ_FILE}
OUTPUT_QUIET)
file(REMOVE ${TGZ_FILE})
set(PADDLE_INFERENCE_DIR
${EXTRACTED_DIR}
CACHE PATH
"Path to 'paddle_inference_install_dir' or 'paddle_inference'")
else()
message(
STATUS "PADDLE_INFERENCE_DIR is already defined: ${PADDLE_INFERENCE_DIR}")
endif()

message(STATUS "Final PADDLE_INFERENCE_DIR is set to ${PADDLE_INFERENCE_DIR}")

set(PADDLE_INFERENCE_DIR
${PADDLE_INFERENCE_DIR}
CACHE PATH "Path to 'paddle_inference_install_dir' or 'paddle_inference'")

# used in api_cc
set(PADDLE_LIBRARIES
"${PADDLE_INFERENCE_DIR}/paddle/lib/libpaddle_inference.so"
CACHE PATH "Path to libpaddle_inference.so")

include_directories("${PADDLE_INFERENCE_DIR}/")
set(PADDLE_LIB_THIRD_PARTY_PATH
"${PADDLE_INFERENCE_DIR}/third_party/install/")

include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}protobuf/include")
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}glog/include")
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}gflags/include")
include_directories("${PADDLE_LIB_THIRD_PARTY_PATH}xxhash/include")

link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}protobuf/lib")
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}glog/lib")
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}gflags/lib")
link_directories("${PADDLE_LIB_THIRD_PARTY_PATH}xxhash/lib")
link_directories("${PADDLE_INFERENCE_DIR}/paddle/lib")
# if (USE_ROCM_TOOLKIT) add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1) endif()
endif(ENABLE_PADDLE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve robustness of Paddle library download and configuration.

Several improvements could make the Paddle integration more robust:

  1. The download URL is hardcoded and might become outdated
  2. No verification of downloaded file integrity
  3. No fallback mechanism if download fails
  4. Third-party library paths are hardcoded

Consider implementing these improvements:

 if(ENABLE_PADDLE)
   if(NOT DEFINED PADDLE_INFERENCE_DIR)
-    message(STATUS "PADDLE_INFERENCE_DIR is not defined. Downloading...")
-    set(DOWNLOAD_URL
-        "https://paddle-qa.bj.bcebos.com/paddle-pipeline/GITHUB_Docker_Compile_Test_Cuda118_cudnn860_Trt8531_D1/ce51e82e84fc97e0a55a162037f1554746159cad/paddle_inference.tgz"
-    )
+    set(PADDLE_DOWNLOAD_URL "https://paddle-qa.bj.bcebos.com/paddle-pipeline/GITHUB_Docker_Compile_Test_Cuda118_cudnn860_Trt8531_D1/ce51e82e84fc97e0a55a162037f1554746159cad/paddle_inference.tgz" 
+        CACHE STRING "URL to download Paddle inference library")
+    set(PADDLE_DOWNLOAD_HASH "expected_hash_here" 
+        CACHE STRING "Expected hash of downloaded Paddle inference library")
+    
+    message(STATUS "PADDLE_INFERENCE_DIR is not defined. Downloading from ${PADDLE_DOWNLOAD_URL}...")
     set(TGZ_FILE "${CMAKE_BINARY_DIR}/paddle_inference.tgz")
     set(EXTRACTED_DIR "${CMAKE_BINARY_DIR}/paddle_inference_install_dir")
-    file(DOWNLOAD ${DOWNLOAD_URL} ${TGZ_FILE})
+    
+    file(DOWNLOAD ${PADDLE_DOWNLOAD_URL} ${TGZ_FILE}
+         EXPECTED_HASH SHA256=${PADDLE_DOWNLOAD_HASH}
+         STATUS DOWNLOAD_STATUS
+         SHOW_PROGRESS)
+         
+    list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
+    if(NOT STATUS_CODE EQUAL 0)
+      message(FATAL_ERROR "Failed to download Paddle inference library. Please set PADDLE_INFERENCE_DIR manually.")
+    endif()

Also, consider making third-party library paths configurable:

-  set(PADDLE_LIB_THIRD_PARTY_PATH
-      "${PADDLE_INFERENCE_DIR}/third_party/install/")
+  set(PADDLE_THIRD_PARTY_PATH "${PADDLE_INFERENCE_DIR}/third_party/install/"
+      CACHE PATH "Path to Paddle third-party libraries")

Committable suggestion skipped: line range outside the PR's diff.


if(BUILD_TESTING)
enable_testing()
add_subdirectory(${CMAKE_SOURCE_DIR}/cmake/coverage_config coverage_config)
Expand Down Expand Up @@ -279,9 +332,13 @@ if(NOT DEEPMD_C_ROOT)
if(ENABLE_JAX)
message(STATUS "- JAX")
endif()
if(ENABLE_PADDLE)
message(STATUS "- Paddle")
endif()
if(NOT ENABLE_TENSORFLOW
AND NOT ENABLE_PYTORCH
AND NOT ENABLE_JAX
AND NOT ENABLE_PADDLE
AND NOT BUILD_PY_IF)
message(FATAL_ERROR "No backend is enabled.")
endif()
Expand Down
8 changes: 8 additions & 0 deletions source/api_cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ if(ENABLE_JAX)
target_link_libraries(${libname} PRIVATE TensorFlow::tensorflow_c)
target_compile_definitions(${libname} PRIVATE BUILD_JAX)
endif()
if(ENABLE_PADDLE AND NOT BUILD_PY_IF)
target_link_libraries(${libname} PUBLIC "${PADDLE_LIBRARIES}")
target_compile_definitions(${libname} PUBLIC BUILD_PADDLE)
if(DP_VARIANT STREQUAL "rocm")
target_link_libraries(${libname}
PUBLIC "${hip_LIB_INSTALL_DIR}/libgalaxyhip.so")
endif()
endif()

target_include_directories(
${libname}
Expand Down
Loading
Loading