Skip to content

Commit 31afd03

Browse files
authored
Enable PyTorch backend for DeePMD-kit v3 (#5253)
* enable PyTorch backend for DeePMD-kit v3 * update CMakeLists.txt
1 parent 50a204f commit 31afd03

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

CMakeLists.txt

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -436,22 +436,6 @@ if(ENABLE_FLOAT_FFTW)
436436
endif()
437437

438438
if(ENABLE_DEEPKS)
439-
# Torch uses outdated components to detect CUDA arch, causing failure on
440-
# latest CUDA kits. Set CMake variable TORCH_CUDA_ARCH_LIST in the form of
441-
# "major.minor" if required.
442-
find_package(Torch REQUIRED)
443-
if(NOT Torch_VERSION VERSION_LESS "2.1.0")
444-
set_if_higher(CMAKE_CXX_STANDARD 17)
445-
elseif(NOT Torch_VERSION VERSION_LESS "1.5.0")
446-
set_if_higher(CMAKE_CXX_STANDARD 14)
447-
endif()
448-
include_directories(${TORCH_INCLUDE_DIRS})
449-
if(MKL_FOUND)
450-
list(PREPEND math_libs ${TORCH_LIBRARIES})
451-
else()
452-
list(APPEND math_libs ${TORCH_LIBRARIES})
453-
endif()
454-
add_compile_options(${TORCH_CXX_FLAGS})
455439
target_link_libraries(${ABACUS_BIN_NAME} deepks)
456440

457441
find_path(libnpy_SOURCE_DIR npy.hpp HINTS ${libnpy_INCLUDE_DIR})
@@ -470,6 +454,25 @@ if(ENABLE_DEEPKS)
470454
add_compile_definitions(__DEEPKS)
471455
endif()
472456

457+
# Torch uses outdated components to detect CUDA arch, causing failure on
458+
# latest CUDA kits. Set CMake variable TORCH_CUDA_ARCH_LIST in the form of
459+
# "major.minor" if required.
460+
if(ENABLE_DEEPKS OR DEFINED Torch_DIR)
461+
find_package(Torch REQUIRED)
462+
if(NOT Torch_VERSION VERSION_LESS "2.1.0")
463+
set_if_higher(CMAKE_CXX_STANDARD 17)
464+
elseif(NOT Torch_VERSION VERSION_LESS "1.5.0")
465+
set_if_higher(CMAKE_CXX_STANDARD 14)
466+
endif()
467+
include_directories(${TORCH_INCLUDE_DIRS})
468+
if(MKL_FOUND)
469+
list(PREPEND math_libs ${TORCH_LIBRARIES})
470+
else()
471+
list(APPEND math_libs ${TORCH_LIBRARIES})
472+
endif()
473+
add_compile_options(${TORCH_CXX_FLAGS})
474+
endif()
475+
473476
if (ENABLE_CNPY)
474477
find_path(cnpy_SOURCE_DIR
475478
cnpy.h
@@ -590,13 +593,14 @@ if(DEFINED DeePMD_DIR)
590593
add_compile_definitions(__DPMDC)
591594
else()
592595
target_link_libraries(${ABACUS_BIN_NAME} DeePMD::deepmd_cc)
593-
if(NOT DEFINED TensorFlow_DIR)
594-
set(TensorFlow_DIR ${DeePMD_DIR})
595-
endif()
596-
find_package(TensorFlow REQUIRED)
597-
if(TensorFlow_FOUND)
598-
target_link_libraries(${ABACUS_BIN_NAME} TensorFlow::tensorflow_cc)
599-
endif()
596+
endif()
597+
endif()
598+
599+
if(DEFINED TensorFlow_DIR)
600+
find_package(TensorFlow REQUIRED)
601+
include_directories(${TensorFlow_DIR}/include)
602+
if(TensorFlow_FOUND)
603+
target_link_libraries(${ABACUS_BIN_NAME} TensorFlow::tensorflow_cc)
600604
endif()
601605
endif()
602606

docs/advanced/install.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,22 @@ cmake -B build -DENABLE_DEEPKS=1 -DTorch_DIR=~/libtorch/share/cmake/Torch/ -Dlib
3838
If the Deep Potential model is employed in Molecule Dynamics calculations, the following prerequisites and steps are needed:
3939

4040
- [DeePMD-kit](https://github.com/deepmodeling/deepmd-kit)
41-
- [TensorFlow](https://www.tensorflow.org/)
41+
- [TensorFlow](https://www.tensorflow.org/) (optional)
42+
- [LibTorch](https://pytorch.org/) (optional)
4243

44+
In the simplest case, the `tensorflow_cc` and `torch` libraries are in the same directory as the `deepmd_c`/`deepmd_cc` libraries, then
4345
```bash
44-
cmake -B build -DDeePMD_DIR=~/deepmd-kit -DTensorFlow_DIR=~/tensorflow
46+
cmake -B build -DDeePMD_DIR=/dir_to_deepmd-kit
4547
```
48+
DeePMD-kit supports TensorFlow backend but its libraries are placed at another directory, then
4649

47-
> `deepmd_c`/`deepmd_cc` and `tensorflow_cc` libraries would be called according to `DeePMD_DIR` and `TensorFlow_DIR`, which is showed in detail in [this page](https://github.com/deepmodeling/deepmd-kit/blob/master/doc/inference/cxx.md). If `TensorFlow_DIR` is not defined, it will be the same as `DeePMD_DIR`. Note that `tensorflow_cc` is not required if `deepmd_c` is found.
50+
```bash
51+
cmake -B build -DDeePMD_DIR=/dir_to_deepmd-kit -DTensorFlow_DIR=/dir_to_tensorflow
52+
```
53+
Similarly, DeePMD-kit supports PyTorch backend but its libraries are placed at another directory, then
54+
```bash
55+
cmake -B build -DDeePMD_DIR=/dir_to_deepmd-kit -DTorch_DIR=/dir_to_pytorch
56+
```
4857

4958
## Build with LibRI and LibComm
5059

@@ -282,15 +291,21 @@ directly.
282291

283292
> Note: This part is only required if you want to load a trained DeeP Potential and run molecular dynamics with that. To train the DeeP Potential with DP-GEN, no extra prerequisite is needed and please refer to [this page](http://abacus.deepmodeling.com/en/latest/advanced/interface/dpgen.html) for ABACUS interface with DP-GEN.
284293
285-
To compile ABACUS with DeePMD-kit, you need to define `DeePMD_DIR` and `TensorFlow_DIR` in the file `Makefile.vars` or use
294+
To compile ABACUS with DeePMD-kit, you need to define `DeePMD_DIR` and `TensorFlow_DIR` (TensorFlow Backend, optional) and/or `LIBTORCH_DIR` (PyTorch Backend, optional) in the file `Makefile.vars`.
286295

296+
Or the `tensorflow_cc` and `torch` libraries are in the same directory as the `deepmd_c`/`deepmd_cc` libraries, then
287297
```makefile
288-
make DeePMD_DIR=~/deepmd-kit TensorFlow_DIR=~/tensorflow
298+
make DeePMD_DIR=/dir_to_deepmd-kit
289299
```
300+
DeePMD-kit supports TensorFlow backend but its libraries are placed at another directory, then
290301

291-
directly.
292-
293-
> `deepmd_c`/`deepmd_cc` and `tensorflow_cc` libraries would be called according to `DeePMD_DIR` and `TensorFlow_DIR`, which is showed in detail in [this page](https://github.com/deepmodeling/deepmd-kit/blob/master/doc/inference/cxx.md). If `TensorFlow_DIR` is not defined, it will be the same as `DeePMD_DIR`. Note that `tensorflow_cc` is not required if `deepmd_c` is found.
302+
```makefile
303+
make DeePMD_DIR=/dir_to_deepmd-kit TensorFlow_DIR=/dir_to_tensorflow
304+
```
305+
Similarly, DeePMD-kit supports PyTorch backend but its libraries are placed at another directory, then
306+
```makefile
307+
make DeePMD_DIR=/dir_to_deepmd-kit Torch_DIR=/dir_to_pytorch
308+
```
294309

295310
### Add LibRI Support
296311
To use new EXX, you need two libraries: [LibRI](https://github.com/abacusmodeling/LibRI) and [LibComm](https://github.com/abacusmodeling/LibComm) and need to define `LIBRI_DIR` and `LIBCOMM_DIR` in the file `Makefile.vars` or use

source/Makefile

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,17 @@ ifdef LIBRI_DIR
135135
HONG += -D__EXX -DEXX_H_COMM=2 -DEXX_DM=3 -DTEST_EXX_LCAO=0 -DTEST_EXX_RADIAL=1
136136
endif
137137

138+
ifdef LIBNPY_DIR
139+
CNPY_INCLUDE_DIR = -I${LIBNPY_DIR}/include
140+
HONG += -D__DEEPKS
141+
INCLUDES += $(CNPY_INCLUDE_DIR)
142+
endif
143+
138144
ifdef LIBTORCH_DIR
139-
ifdef LIBNPY_DIR
140-
LIBTORCH_INCLUDE_DIR = -I${LIBTORCH_DIR}/include -I${LIBTORCH_DIR}/include/torch/csrc/api/include
141-
LIBTORCH_LIB_DIR= ${LIBTORCH_DIR}/lib
142-
LIBTORCH_LIB = -L${LIBTORCH_LIB_DIR} -ltorch -lc10 -Wl,-rpath,${LIBTORCH_LIB_DIR} -Wl,--no-as-needed,"${LIBTORCH_LIB_DIR}/libtorch_cpu.so" -Wl,--as-needed ${LIBTORCH_LIB_DIR}/libc10.so -lpthread -Wl,--no-as-needed,"${LIBTORCH_LIB_DIR}/libtorch.so" -Wl,--as-needed
143-
CNPY_INCLUDE_DIR = -I${LIBNPY_DIR}/include
144-
HONG += -D__DEEPKS
145-
INCLUDES += $(LIBTORCH_INCLUDE_DIR) $(CNPY_INCLUDE_DIR)
146-
LIBS += $(LIBTORCH_LIB)
147-
endif
145+
LIBTORCH_INCLUDE_DIR = -I${LIBTORCH_DIR}/include -I${LIBTORCH_DIR}/include/torch/csrc/api/include
146+
INCLUDES += $(LIBTORCH_INCLUDE_DIR)
147+
LIBTORCH_LIB_DIR= ${LIBTORCH_DIR}/lib
148+
LIBS += -L${LIBTORCH_LIB_DIR} -ltorch -lc10 -Wl,-rpath,${LIBTORCH_LIB_DIR} -Wl,--no-as-needed,"${LIBTORCH_LIB_DIR}/libtorch_cpu.so" -Wl,--as-needed ${LIBTORCH_LIB_DIR}/libc10.so -lpthread -Wl,--no-as-needed,"${LIBTORCH_LIB_DIR}/libtorch.so" -Wl,--as-needed
148149
endif
149150

150151
ifdef DeePMD_DIR
@@ -162,20 +163,19 @@ ifdef DeePMD_DIR
162163
LIBS += -L${DeeP_LIB_DIR} -ldeepmd_c -Wl,-rpath=${DeeP_LIB_DIR}
163164
else
164165
LIBS += -L${DeeP_LIB_DIR} -ldeepmd_cc -Wl,-rpath=${DeeP_LIB_DIR}
165-
166-
#==========================
167-
# TensorFlow package
168-
#==========================
169-
ifndef TensorFlow_DIR
170-
TensorFlow_DIR = ${DeePMD_DIR}
171-
endif
172-
TensorFlow_LIB_DIR = ${TensorFlow_DIR}/lib
173-
LIBS += -L${TensorFlow_LIB_DIR} -ltensorflow_cc -Wl,-rpath=${TensorFlow_LIB_DIR}
174-
TensorFlow_INCLUDE_DIR = ${TensorFlow_DIR}/include
175-
INCLUDES += -I${TensorFlow_INCLUDE_DIR}
176166
endif
177167
endif
178168

169+
ifdef TensorFlow_DIR
170+
#==========================
171+
# TensorFlow package
172+
#==========================
173+
TensorFlow_LIB_DIR = ${TensorFlow_DIR}/lib
174+
LIBS += -L${TensorFlow_LIB_DIR} -ltensorflow_cc -Wl,-rpath=${TensorFlow_LIB_DIR}
175+
TensorFlow_INCLUDE_DIR = ${TensorFlow_DIR}/include
176+
INCLUDES += -I${TensorFlow_INCLUDE_DIR}
177+
endif
178+
179179
ifdef PEXSI_DIR
180180
OBJS_ABACUS += ${OBJS_HSOLVER_PEXSI}
181181
INCLUDES += -I${PEXSI_DIR}/include -I${PARMETIS_DIR}/include -I${DSUPERLU_DIR}/include

source/Makefile.vars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ CEREAL_DIR = /usr/local/include/cereal
5757
##------------------- OPTIONAL LIBS ---------------------------------
5858
## To use DEEPKS: set LIBTORCH_DIR and LIBNPY_DIR
5959
## To use LIBXC: set LIBXC_DIR which contains include and lib/libxc.a (>5.1.7)
60-
## To use DeePMD: set DeePMD_DIR and TensorFlow_DIR
60+
## To use DeePMD: set DeePMD_DIR LIBTORCH_DIR and TensorFlow_DIR
6161
## To use LibRI: set LIBRI_DIR and LIBCOMM_DIR
6262
## To use PEXSI: set PEXSI_DIR DSUPERLU_DIR and PARMETIS_DIR
6363
##---------------------------------------------------------------------

0 commit comments

Comments
 (0)