From 3b2ed551a236dacdf95cd01b960091953f49f234 Mon Sep 17 00:00:00 2001 From: HydrogenSulfate <490868991@qq.com> Date: Mon, 27 Nov 2023 03:48:59 +0000 Subject: [PATCH] fix for missing code --- README.md | 316 ++++++++++++++++++++++++++++-------- source/api_c/src/c_api.cc | 14 -- source/api_cc/src/common.cc | 205 +++++++++++++++++++++-- 3 files changed, 438 insertions(+), 97 deletions(-) diff --git a/README.md b/README.md index 4cb4fbb24a..c277947f3a 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,180 @@ -------------------------------------------------------------------------------- +## DeePMD-kit(PaddlePaddle backend) + +> [!IMPORTANT] +> 本项目为 DeePMD-kit 的 PaddlePaddle 版本,主要修改了部分代码,使其可以运行在 PaddlePaddle 上。运行功能包括在 water_se_e2_a 案例的单卡 GPU 训练、单卡 GPU 评估、导出静态图模型、接入 LAMMPS(GPU) 推理 5 部分的功能。 + +## 1. 环境安装 + +1. 安装 tensorflow 2.12 + + ``` sh + # Current stable release for CPU and GPU(CPU和GPU使用同一个命令,不再以安装tensorflow-gpu的形式安装GPU版本) + pip install tensorflow==2.12 -i https://pypi.tuna.tsinghua.edu.cn/simple + ``` + +2. 安装 paddlepaddle-develop + + 参考 [Paddle 官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux-pip.html),安装对应机器环境的 GPU 版 paddlepaddle-develop + +3. 安装 deepmd-kit + + ``` sh + git clone git@github.com:HydrogenSulfate/deepmd-kit.git -b add_ddle_backend + cd deepmd-kit + # 以 editable 的方式安装,方便调试 + pip install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple + ``` + +## 2. 运行具体功能 + +### 2.1 安装 python 自定义算子 + +**在运行训练、评估、导出静态图模型这 3 个功能之前,需要先安装 python 端的自定义算子库 `paddle_deepmd_lib`**,LAMMPS 推理功能则不需要安装。 + +``` sh +cd ./source/lib/paddle_src +python setup_ins.py install +``` + +### 2.1 训练 + +> [!NOTE] +> 暂时只支持 water_se_e2_a 案例的训练 + +``` sh +# 进入案例目录 +cd examples/water/se_e2_a +# 运行训练 +dp train ./input.json +``` + +### 2.2 评估 + +``` sh +# 进入案例目录 +cd examples/water/se_e2_a +# 设置好权重文件路径 +WEIGHT_PATH="path/to/your_model.pdparams" +# 运行评估 +dp test -m ${WEIGHT_PATH} -s ../data/data_3/ -n 30 +``` + +### 2.3 导出静态图模型 + +``` sh +# 进入案例目录 +cd examples/water/se_e2_a +# 设置权重文件路径 +WEIGHT_PATH="path/to/your_model.pdparams" +# 设置导出的静态图模型路径前缀(不需要加.pdmodel或.pdiparams后缀) +DUMP_PATH="path/to/your_dump" +# 导出静态图模型 +dp freeze -i ${WEIGHT_PATH} -o ${DUMP_PATH} +``` + +### 2.4 在 LAMMPS(GPU) 中推理 + +1. 修改 `examples/water/lmp/in.lammps` 文件,将 `pair_style deepmd` 后面的路径改为 **2.3 导出静态图模型** 这一章节内设置好的 DUMP_PATH 的值 + + ``` suggestion + pair_style deepmd "path/to/your_dump" + ``` + +2. 编译 Paddle,得到未裁剪算子的 Paddle 推理库(LAMMPS 推理涉及到 `xxx_grad` 反向算子,因而需要使用未裁剪的 Paddle 推理库) + + ``` sh + git clone https://github.com/PaddlePaddle/Paddle.git -b develop + cd Paddle + mkdir build + cd build + # 推荐使用 Anaconda 安装 python3.9 环境,并在该环境下执行编译命令 + cmake .. -DPY_VERSION=3.9 -DWITH_GPU=ON -WITH_DISTRIBUTE=ON -DWITH_TESTING=ON -DCMAKE_BUILD_TYPE=Release + make -j$(nproc) + pip install python/dist/paddlepaddle_gpu-0.0.0-cp39-cp39-linux_x86_64.whl + ``` + +3. 安装 LAMMPS 并运行推理 + + ``` sh + # 下载并解压 lammps 源码 + wget https://github.com/lammps/lammps/archive/stable_2Aug2023_update1.tar.gz + tar xf stable_2Aug2023_update1.tar.gz + # LAMMPS_DIR 设置为 LAMMPS 的安装目录 + export LAMMPS_DIR="/path/to/lammps-stable_2Aug2023_update1" + + # 设置推理时的 GPU 卡号 + export CUDA_VISIBLE_DEVICES=0 + # PADDLE_DIR 设置为第一步clone下的 Paddle 目录 + export PADDLE_DIR="/path/to/Paddle" + # DEEPMD_DIR 设置为本项目的根目录 + export DEEPMD_DIR="/path/to/deepmd-kit" + # PADDLE_INFERENCE_DIR 设置为第二步编译得到的 Paddle 推理库目录 + export PADDLE_INFERENCE_DIR="/path/to/paddle_inference_install_dir" + # TENSORFLOW_DIR 设置为 tensorflow 的安装目录,可用 pip show tensorflow 确定 + export TENSORFLOW_DIR="/path/to/tensorflow" + + export LD_LIBRARY_PATH=${PADDLE_DIR}/paddle/fluid/pybind/:$LD_LIBRARY_PATH + export LD_LIBRARY_PATH=${DEEPMD_DIR}/deepmd/op:$LD_LIBRARY_PATH + export LD_LIBRARY_PATH=${PADDLE_INFERENCE_DIR}/paddle/lib:$LD_LIBRARY_PATH + export LD_LIBRARY_PATH=${PADDLE_INFERENCE_DIR}/third_party/install/mkldnn/lib:$LD_LIBRARY_PATH + export LD_LIBRARY_PATH=${PADDLE_INFERENCE_DIR}/third_party/install/mklml/lib:$LD_LIBRARY_PATH + export LD_LIBRARY_PATH=${DEEPMD_DIR}/source/build:$LD_LIBRARY_PATH + export LIBRARY_PATH=${DEEPMD_DIR}/deepmd/op:$LIBRARY_PATH + + cd ${DEEPMD_DIR}/source + # rm -rf build # 若改动CMakeLists.txt,则需要打开该注释 + mkdir build + cd build + + # DEEPMD_INSTALL_DIR 设置为 deepmd-lammps 的目标安装目录,可自行设置任意路径 + export DEEPMD_INSTALL_DIR="path/to/deepmd_root" + + # 开始编译 + cmake -DCMAKE_INSTALL_PREFIX=${DEEPMD_INSTALL_DIR} -DPADDLE_ROOT=${PADDLE_INFERENCE_DIR} \ + -DUSE_CUDA_TOOLKIT=TRUE \ + -DTENSORFLOW_ROOT=${TENSORFLOW_DIR} \ + -DPADDLE_LIB=${PADDLE_INFERENCE_DIR} \ + -DFLOAT_PREC=low .. + make -j4 && make install + make lammps + + cd ${LAMMPS_DIR}/src/ + \cp -r ${DEEPMD_DIR}/source/build/USER-DEEPMD . + make yes-kspace + make yes-extra-fix + make yes-user-deepmd + make serial -j + export PATH=${LAMMPS_DIR}/src:$PATH + + cd ${DEEPMD_DIR}/examples/water/lmp + + lmp_serial -in in.lammps + ``` + +4. 直接运行推理 + + ``` sh + # 设置推理时的 GPU 卡号 + export CUDA_VISIBLE_DEVICES=0 + # LAMMPS_DIR 设置为 LAMMPS 的安装目录 + export LAMMPS_DIR="/path/to/lammps-stable_2Aug2023_update1" + + cd ${LAMMPS_DIR}/src/ + export PATH=${LAMMPS_DIR}/src:$PATH + + cd ${DEEPMD_DIR}/examples/water/lmp + + lmp_serial -in in.lammps + ``` + +--- + DeePMD-kit Manual ======== + [![GitHub release](https://img.shields.io/github/release/deepmodeling/deepmd-kit.svg?maxAge=86400)](https://github.com/deepmodeling/deepmd-kit/releases) [![doi:10.1016/j.cpc.2018.03.016](https://img.shields.io/badge/DOI-10.1016%2Fj.cpc.2018.03.016-blue)](https://doi.org/10.1016/j.cpc.2020.107206) [![Citations](https://citations.njzjz.win/10.1016/j.cpc.2018.03.016)](https://badge.dimensions.ai/details/doi/10.1016/j.cpc.2018.03.016) @@ -14,46 +186,53 @@ [![Documentation Status](https://readthedocs.org/projects/deepmd/badge/)](https://deepmd.readthedocs.io/) # Table of contents + - [About DeePMD-kit](#about-deepmd-kit) - - [Highlights in v2.0](#highlights-in-deepmd-kit-v2.0) - - [Highlighted features](#highlighted-features) - - [License and credits](#license-and-credits) - - [Deep Potential in a nutshell](#deep-potential-in-a-nutshell) + - [Highlights in v2.0](#highlights-in-deepmd-kit-v2.0) + - [Highlighted features](#highlighted-features) + - [License and credits](#license-and-credits) + - [Deep Potential in a nutshell](#deep-potential-in-a-nutshell) - [Download and install](#download-and-install) - [Use DeePMD-kit](#use-deepmd-kit) - [Code structure](#code-structure) - [Troubleshooting](#troubleshooting) # About DeePMD-kit + DeePMD-kit is a package written in Python/C++, designed to minimize the effort required to build deep learning-based model of interatomic potential energy and force field and to perform molecular dynamics (MD). This brings new hopes to addressing the accuracy-versus-efficiency dilemma in molecular simulations. Applications of DeePMD-kit span from finite molecules to extended systems and from metallic systems to chemically bonded systems. For more information, check the [documentation](https://deepmd.readthedocs.io/). # Highlights in DeePMD-kit v2.0 + * [Model compression](doc/freeze/compress.md). Accelerate the efficiency of model inference 4-15 times. -* [New descriptors](doc/model/overall.md). Including [`se_e2_r`](doc/model/train-se-e2-r.md) and [`se_e3`](doc/model/train-se-e3.md). -* [Hybridization of descriptors](doc/model/train-hybrid.md). Hybrid descriptor constructed from the concatenation of several descriptors. -* [Atom type embedding](doc/model/train-se-e2-a-tebd.md). Enable atom-type embedding to decline training complexity and refine performance. -* Training and inference of the dipole (vector) and polarizability (matrix). -* Split of training and validation dataset. -* Optimized training on GPUs. +- [New descriptors](doc/model/overall.md). Including [`se_e2_r`](doc/model/train-se-e2-r.md) and [`se_e3`](doc/model/train-se-e3.md). +- [Hybridization of descriptors](doc/model/train-hybrid.md). Hybrid descriptor constructed from the concatenation of several descriptors. +- [Atom type embedding](doc/model/train-se-e2-a-tebd.md). Enable atom-type embedding to decline training complexity and refine performance. +- Training and inference of the dipole (vector) and polarizability (matrix). +- Split of training and validation dataset. +- Optimized training on GPUs. ## Highlighted features + * **interfaced with TensorFlow**, one of the most popular deep learning frameworks, making the training process highly automatic and efficient, in addition, Tensorboard can be used to visualize training procedures. -* **interfaced with high-performance classical MD and quantum (path-integral) MD packages**, i.e., LAMMPS and i-PI, respectively. -* **implements the Deep Potential series models**, which have been successfully applied to finite and extended systems including organic molecules, metals, semiconductors, insulators, etc. -* **implements MPI and GPU supports**, making it highly efficient for high-performance parallel and distributed computing. -* **highly modularized**, easy to adapt to different descriptors for deep learning-based potential energy models. +- **interfaced with high-performance classical MD and quantum (path-integral) MD packages**, i.e., LAMMPS and i-PI, respectively. +- **implements the Deep Potential series models**, which have been successfully applied to finite and extended systems including organic molecules, metals, semiconductors, insulators, etc. +- **implements MPI and GPU supports**, making it highly efficient for high-performance parallel and distributed computing. +- **highly modularized**, easy to adapt to different descriptors for deep learning-based potential energy models. ## License and credits + The project DeePMD-kit is licensed under [GNU LGPLv3.0](./LICENSE). If you use this code in any future publications, please cite the following publications for general purpose: + - Han Wang, Linfeng Zhang, Jiequn Han, and Weinan E. "DeePMD-kit: A deep learning package for many-body potential energy representation and molecular dynamics." Computer Physics Communications 228 (2018): 178-184. - Jinzhe Zeng, Duo Zhang, Denghui Lu, Pinghui Mo, Zeyu Li, Yixiao Chen, Marián Rynik, Li'ang Huang, Ziyao Li, Shaochen Shi, Yingze Wang, Haotian Ye, Ping Tuo, Jiabin Yang, Ye Ding, Yifan Li, Davide Tisi, Qiyu Zeng, Han Bao, Yu Xia, Jiameng Huang, Koki Muraoka, Yibo Wang, Junhan Chang, Fengbo Yuan, Sigbjørn Løland Bore, Chun Cai, Yinnian Lin, Bo Wang, Jiayan Xu, Jia-Xin Zhu, Chenxing Luo, Yuzhi Zhang, Rhys E. A. Goodall, Wenshuo Liang, Anurag Kumar Singh, Sikai Yao, Jingchao Zhang, Renata Wentzcovitch, Jiequn Han, Jie Liu, Weile Jia, Darrin M. York, Weinan E, Roberto Car, Linfeng Zhang, Han Wang. "DeePMD-kit v2: A software package for Deep Potential models." [arXiv:2304.09409](https://doi.org/10.48550/arXiv.2304.09409). In addition, please follow [the bib file](CITATIONS.bib) to cite the methods you used. ## Deep Potential in a nutshell + The goal of Deep Potential is to employ deep learning techniques and realize an inter-atomic potential energy model that is general, accurate, computationally efficient and scalable. The key component is to respect the extensive and symmetry-invariant properties of a potential energy model by assigning a local reference frame and a local environment to each atom. Each environment contains a finite number of atoms, whose local coordinates are arranged in a symmetry-preserving way. These local coordinates are then transformed, through a sub-network, to so-called *atomic energy*. Summing up all the atomic energies gives the potential energy of the system. The initial proof of concept is in the [Deep Potential][1] paper, which employed an approach that was devised to train the neural network model with the potential energy only. With typical *ab initio* molecular dynamics (AIMD) datasets this is insufficient to reproduce the trajectories. The Deep Potential Molecular Dynamics ([DeePMD][2]) model overcomes this limitation. In addition, the learning process in DeePMD improves significantly over the Deep Potential method thanks to the introduction of a flexible family of loss functions. The NN potential constructed in this way reproduces accurately the AIMD trajectories, both classical and quantum (path integral), in extended and finite systems, at a cost that scales linearly with system size and is always several orders of magnitude lower than that of equivalent AIMD simulations. @@ -72,7 +251,6 @@ DeePMD-kit offers multiple installation methods. It is recommended to use easy m One may manually install DeePMD-kit by following the instructions on [installing the Python interface](doc/install/install-from-source.md#install-the-python-interface) and [installing the C++ interface](doc/install/install-from-source.md#install-the-c-interface). The C++ interface is necessary when using DeePMD-kit with LAMMPS, i-PI or GROMACS. - # Use DeePMD-kit A quick start on using DeePMD-kit can be found [here](doc/getting-started/quick_start.ipynb). @@ -82,74 +260,73 @@ A full [document](doc/train/train-input-auto.rst) on options in the training inp # Advanced - [Installation](doc/install/index.md) - - [Easy install](doc/install/easy-install.md) - - [Install from source code](doc/install/install-from-source.md) - - [Install from pre-compiled C library](doc/install/install-from-c-library.md) - - [Install LAMMPS](doc/install/install-lammps.md) - - [Install i-PI](doc/install/install-ipi.md) - - [Install GROMACS](doc/install/install-gromacs.md) - - [Building conda packages](doc/install/build-conda.md) - - [Install Node.js interface](doc/install/install-nodejs.md) + - [Easy install](doc/install/easy-install.md) + - [Install from source code](doc/install/install-from-source.md) + - [Install from pre-compiled C library](doc/install/install-from-c-library.md) + - [Install LAMMPS](doc/install/install-lammps.md) + - [Install i-PI](doc/install/install-ipi.md) + - [Install GROMACS](doc/install/install-gromacs.md) + - [Building conda packages](doc/install/build-conda.md) + - [Install Node.js interface](doc/install/install-nodejs.md) - [Data](doc/data/index.md) - - [System](doc/data/system.md) - - [Formats of a system](doc/data/data-conv.md) - - [Prepare data with dpdata](doc/data/dpdata.md) + - [System](doc/data/system.md) + - [Formats of a system](doc/data/data-conv.md) + - [Prepare data with dpdata](doc/data/dpdata.md) - [Model](doc/model/index.md) - - [Overall](doc/model/overall.md) - - [Descriptor `"se_e2_a"`](doc/model/train-se-e2-a.md) - - [Descriptor `"se_e2_r"`](doc/model/train-se-e2-r.md) - - [Descriptor `"se_e3"`](doc/model/train-se-e3.md) - - [Descriptor `"se_atten"`](doc/model/train-se-atten.md) - - [Descriptor `"hybrid"`](doc/model/train-hybrid.md) - - [Descriptor `sel`](doc/model/sel.md) - - [Fit energy](doc/model/train-energy.md) - - [Fit spin energy](doc/model/train-energy-spin.md) - - [Fit `tensor` like `Dipole` and `Polarizability`](doc/model/train-fitting-tensor.md) + - [Overall](doc/model/overall.md) + - [Descriptor `"se_e2_a"`](doc/model/train-se-e2-a.md) + - [Descriptor `"se_e2_r"`](doc/model/train-se-e2-r.md) + - [Descriptor `"se_e3"`](doc/model/train-se-e3.md) + - [Descriptor `"se_atten"`](doc/model/train-se-atten.md) + - [Descriptor `"hybrid"`](doc/model/train-hybrid.md) + - [Descriptor `sel`](doc/model/sel.md) + - [Fit energy](doc/model/train-energy.md) + - [Fit spin energy](doc/model/train-energy-spin.md) + - [Fit `tensor` like `Dipole` and `Polarizability`](doc/model/train-fitting-tensor.md) - [Fit electronic density of states (DOS)](doc/model/train-fitting-dos.md) - - [Train a Deep Potential model using `type embedding` approach](doc/model/train-se-e2-a-tebd.md) - - [Deep potential long-range](doc/model/dplr.md) - - [Deep Potential - Range Correction (DPRc)](doc/model/dprc.md) + - [Train a Deep Potential model using `type embedding` approach](doc/model/train-se-e2-a-tebd.md) + - [Deep potential long-range](doc/model/dplr.md) + - [Deep Potential - Range Correction (DPRc)](doc/model/dprc.md) - [Training](doc/train/index.md) - - [Training a model](doc/train/training.md) - - [Advanced options](doc/train/training-advanced.md) - - [Parallel training](doc/train/parallel-training.md) - - [Multi-task training](doc/train/multi-task-training.md) - - [TensorBoard Usage](doc/train/tensorboard.md) - - [Known limitations of using GPUs](doc/train/gpu-limitations.md) - - [Training Parameters](doc/train-input-auto.rst) + - [Training a model](doc/train/training.md) + - [Advanced options](doc/train/training-advanced.md) + - [Parallel training](doc/train/parallel-training.md) + - [Multi-task training](doc/train/multi-task-training.md) + - [TensorBoard Usage](doc/train/tensorboard.md) + - [Known limitations of using GPUs](doc/train/gpu-limitations.md) + - [Training Parameters](doc/train-input-auto.rst) - [Freeze and Compress](doc/freeze/index.rst) - - [Freeze a model](doc/freeze/freeze.md) - - [Compress a model](doc/freeze/compress.md) + - [Freeze a model](doc/freeze/freeze.md) + - [Compress a model](doc/freeze/compress.md) - [Test](doc/test/index.rst) - - [Test a model](doc/test/test.md) - - [Calculate Model Deviation](doc/test/model-deviation.md) + - [Test a model](doc/test/test.md) + - [Calculate Model Deviation](doc/test/model-deviation.md) - [Inference](doc/inference/index.rst) - - [Python interface](doc/inference/python.md) - - [C++ interface](doc/inference/cxx.md) - - [Node.js interface](doc/inference/nodejs.md) + - [Python interface](doc/inference/python.md) + - [C++ interface](doc/inference/cxx.md) + - [Node.js interface](doc/inference/nodejs.md) - [Integrate with third-party packages](doc/third-party/index.rst) - - [Use deep potential with ASE](doc/third-party/ase.md) - - [Run MD with LAMMPS](doc/third-party/lammps.md) - - [LAMMPS commands](doc/third-party/lammps-command.md) - - [Run path-integral MD with i-PI](doc/third-party/ipi.md) - - [Run MD with GROMACS](doc/third-party/gromacs.md) - - [Interfaces out of DeePMD-kit](doc/third-party/out-of-deepmd-kit.md) + - [Use deep potential with ASE](doc/third-party/ase.md) + - [Run MD with LAMMPS](doc/third-party/lammps.md) + - [LAMMPS commands](doc/third-party/lammps-command.md) + - [Run path-integral MD with i-PI](doc/third-party/ipi.md) + - [Run MD with GROMACS](doc/third-party/gromacs.md) + - [Interfaces out of DeePMD-kit](doc/third-party/out-of-deepmd-kit.md) - [Use NVNMD](doc/nvnmd/index.md) # Code structure The code is organized as follows: -* `data/raw`: tools manipulating the raw data files. -* `examples`: examples. -* `deepmd`: DeePMD-kit python modules. -* `source/api_cc`: source code of DeePMD-kit C++ API. -* `source/ipi`: source code of i-PI client. -* `source/lib`: source code of DeePMD-kit library. -* `source/lmp`: source code of Lammps module. -* `source/gmx`: source code of Gromacs plugin. -* `source/op`: TensorFlow op implementation. working with the library. - +- `data/raw`: tools manipulating the raw data files. +- `examples`: examples. +- `deepmd`: DeePMD-kit python modules. +- `source/api_cc`: source code of DeePMD-kit C++ API. +- `source/ipi`: source code of i-PI client. +- `source/lib`: source code of DeePMD-kit library. +- `source/lmp`: source code of Lammps module. +- `source/gmx`: source code of Gromacs plugin. +- `source/op`: TensorFlow op implementation. working with the library. # Troubleshooting @@ -167,7 +344,6 @@ The code is organized as follows: See [DeePMD-kit Contributing Guide](CONTRIBUTING.md) to become a contributor! 🤓 - [1]: https://arxiv.org/abs/1707.01478 [2]: https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.120.143001 [3]: https://arxiv.org/abs/1805.09003 diff --git a/source/api_c/src/c_api.cc b/source/api_c/src/c_api.cc index 8f890bc5b3..613af8c276 100644 --- a/source/api_c/src/c_api.cc +++ b/source/api_c/src/c_api.cc @@ -54,25 +54,11 @@ DP_DeepPot* DP_NewDeepPotWithParam2(const char* c_model, const char* c_file_content, const int size_file_content) { std::string model(c_model); - printf("==>> [DP_NewDeepPotWithParam2]\n"); std::string file_content(c_file_content, c_file_content + size_file_content); DP_NEW_OK(DP_DeepPot, deepmd::DeepPot dp(model, gpu_rank, file_content); DP_DeepPot* new_dp = new DP_DeepPot(dp); return new_dp;) } -// DP_DeepPot* DP_NewDeepPotWithParam3(const char* c_pdmodel_path, -// const char* c_pdiparams_path, -// const int gpu_rank, -// const char* c_file_content, -// const int size_file_content) { -// std::string pdmodel(c_pdmodel_path); -// std::string pdiparams(c_pdiparams_path); -// printf("==>> [DP_NewDeepPotWithParam3 Paddle ver]\n"); -// std::string file_content(c_file_content, c_file_content + size_file_content); -// DP_NEW_OK(DP_DeepPot, deepmd::DeepPot dp(pdmodel, pdiparams, gpu_rank, file_content); -// DP_DeepPot* new_dp = new DP_DeepPot(dp); return new_dp;) -// } - DP_DeepPotModelDevi::DP_DeepPotModelDevi() {} DP_DeepPotModelDevi::DP_DeepPotModelDevi(deepmd::DeepPotModelDevi& dp) : dp(dp) { diff --git a/source/api_cc/src/common.cc b/source/api_cc/src/common.cc index 38c63ac788..89e4a54a00 100644 --- a/source/api_cc/src/common.cc +++ b/source/api_cc/src/common.cc @@ -21,6 +21,7 @@ #endif #include "google/protobuf/io/zero_copy_stream_impl.h" #include "google/protobuf/text_format.h" +#include "paddle/include/paddle_inference_api.h" using namespace tensorflow; @@ -40,23 +41,24 @@ static std::vector split(const std::string& input_, bool deepmd::model_compatable(std::string& model_version) { std::vector words_mv = split(model_version, "."); std::vector words_gmv = split(global_model_version, "."); - // if (words_mv.size() != 2) { - // throw deepmd::deepmd_exception("invalid graph model version string " + - // model_version); - // } - // if (words_gmv.size() != 2) { - // throw deepmd::deepmd_exception("invalid supported model version string " + - // global_model_version); - // } - // int model_version_major = atoi(words_mv[0].c_str()); - // int model_version_minor = atoi(words_mv[1].c_str()); - // int MODEL_VERSION_MAJOR = atoi(words_gmv[0].c_str()); - // int MODEL_VERSION_MINOR = atoi(words_gmv[1].c_str()); + // if (words_mv.size() != 2) { + // throw deepmd::deepmd_exception("invalid graph model version string " + + // model_version); + // } + // if (words_gmv.size() != 2) { + // throw deepmd::deepmd_exception("invalid supported model version string + // " + + // global_model_version); + // } + // int model_version_major = atoi(words_mv[0].c_str()); + // int model_version_minor = atoi(words_mv[1].c_str()); + // int MODEL_VERSION_MAJOR = atoi(words_gmv[0].c_str()); + // int MODEL_VERSION_MINOR = atoi(words_gmv[1].c_str()); int model_version_major = 1; int model_version_minor = 1; int MODEL_VERSION_MAJOR = 1; int MODEL_VERSION_MINOR = 1; - printf(">>> debug\n"); + // printf(">>> debug\n"); return true; if (model_version_major != MODEL_VERSION_MAJOR || model_version_minor > MODEL_VERSION_MINOR) { @@ -498,6 +500,93 @@ int deepmd::session_input_tensors( return nloc; } +template +int deepmd::predictor_input_tensors( + const std::shared_ptr& predictor, + const std::vector& dcoord_, + const int& ntypes, + const std::vector& datype_, + const std::vector& dbox, + InputNlist& dlist, + const std::vector& fparam_, + const std::vector& aparam_, + const deepmd::AtomMap& atommap, + const int nghost, + const int ago, + const std::string scope) { + int nframes = dcoord_.size() / 3 / datype_.size(); + int nall = datype_.size(); + int nloc = nall - nghost; + assert(nall * 3 * nframes == dcoord_.size()); + assert(dbox.size() == nframes * 9); + + std::vector datype = atommap.get_type(); + std::vector type_count(ntypes, 0); + for (unsigned ii = 0; ii < datype.size(); ++ii) { + type_count[datype[ii]]++; + } + datype.insert(datype.end(), datype_.begin() + nloc, datype_.end()); + + std::vector dcoord(dcoord_); + atommap.forward(dcoord.begin(), dcoord_.begin(), 3, nframes, nall); + + // 准备输入Tensor句柄 + auto input_names = predictor->GetInputNames(); + auto coord_handle = predictor->GetInputHandle(input_names[0]); + auto atype_handle = predictor->GetInputHandle(input_names[1]); + auto natoms_handle = predictor->GetInputHandle(input_names[2]); + auto box_handle = predictor->GetInputHandle(input_names[3]); + auto mesh_handle = predictor->GetInputHandle(input_names[4]); + + // 设置输入 Tensor 的维度信息 + std::vector COORD_SHAPE = {nframes, nall * 3}; + std::vector ATYPE_SHAPE = {nframes, nall}; + std::vector BOX_SHAPE = {nframes, 9}; + std::vector MESH_SHAPE = {16}; + std::vector NATOMS_SHAPE = {2 + ntypes}; + + coord_handle->Reshape(COORD_SHAPE); + atype_handle->Reshape(ATYPE_SHAPE); + natoms_handle->Reshape(NATOMS_SHAPE); + box_handle->Reshape(BOX_SHAPE); + mesh_handle->Reshape(MESH_SHAPE); + + // 发送输入数据到Tensor句柄 + coord_handle->CopyFromCpu(dcoord.data()); + + std::vector datype_pad(nframes * nall, 0); + for (int ii = 0; ii < nframes; ++ii) { + for (int jj = 0; jj < nall; ++jj) { + datype_pad[ii * nall + jj] = datype[jj]; + } + } + atype_handle->CopyFromCpu(datype_pad.data()); + + std::vector mesh_pad(16, 0); + mesh_pad[0] = ago; + mesh_pad[1] = dlist.inum; + mesh_pad[2] = 0; + mesh_pad[3] = 0; + memcpy(&mesh_pad[4], &(dlist.ilist), sizeof(int*)); + memcpy(&mesh_pad[8], &(dlist.numneigh), sizeof(int*)); + memcpy(&mesh_pad[12], &(dlist.firstneigh), sizeof(int**)); + mesh_handle->CopyFromCpu(mesh_pad.data()); + + std::vector natoms_pad = {nloc, nall}; + for (int ii = 0; ii < ntypes; ++ii) { + natoms_pad.push_back(type_count[ii]); + } + natoms_handle->CopyFromCpu(natoms_pad.data()); + + box_handle->CopyFromCpu(dbox.data()); + + const int stride = sizeof(int*) / sizeof(int); + assert(stride * sizeof(int) == sizeof(int*)); + assert(stride <= 4); + + return nloc; +} + template int deepmd::session_input_tensors( std::vector>& input_tensors, @@ -772,6 +861,15 @@ VT deepmd::session_get_scalar(Session* session, return orc(0); } +template +VT deepmd::predictor_get_scalar(const std::shared_ptr& predictor, + const std::string name_) { + auto scalar_tensor = predictor->GetOutputHandle(name_); + VT *scalar_ptr = (VT *)malloc(1 * sizeof(VT)); + scalar_tensor->CopyToCpu(scalar_ptr); + return (*scalar_ptr); +} + template void deepmd::session_get_vector(std::vector& o_vec, Session* session, @@ -811,6 +909,13 @@ int deepmd::session_get_dtype(tensorflow::Session* session, return (int)output_rc.dtype(); } +paddle_infer::DataType deepmd::predictor_get_dtype( + const std::shared_ptr& predictor, + const std::string& name_) { + auto scalar_tensor = predictor->GetOutputHandle(name_); + return scalar_tensor->type(); +} + template void deepmd::select_map(std::vector& out, const std::vector& in, @@ -904,6 +1009,14 @@ template int deepmd::session_get_scalar(Session*, const std::string, const std::string); +template int deepmd::predictor_get_scalar( + const std::shared_ptr& predictor, + const std::string name_); + +template int64_t deepmd::predictor_get_scalar( + const std::shared_ptr& predictor, + const std::string name_); + template void deepmd::session_get_vector(std::vector&, Session*, const std::string, @@ -941,6 +1054,10 @@ template float deepmd::session_get_scalar(Session*, const std::string, const std::string); +template float deepmd::predictor_get_scalar( + const std::shared_ptr& predictor, + const std::string name_); + template void deepmd::session_get_vector(std::vector&, Session*, const std::string, @@ -978,6 +1095,10 @@ template double deepmd::session_get_scalar(Session*, const std::string, const std::string); +template double deepmd::predictor_get_scalar( + const std::shared_ptr& predictor, + const std::string name_); + template void deepmd::session_get_vector(std::vector&, Session*, const std::string, @@ -1113,6 +1234,7 @@ template int deepmd::session_input_tensors( const deepmd::AtomMap& atommap, const std::string scope); +/*下面是跟tensorflow session_input_tensors代码相关的模板声明*/ template int deepmd::session_input_tensors( std::vector>& input_tensors, const std::vector& dcoord_, @@ -1166,6 +1288,63 @@ template int deepmd::session_input_tensors( const int nghost, const int ago, const std::string scope); +/*tensorflow end*/ + +/*下面是跟paddle predictor_input_tensors代码相关的模板声明*/ +template int deepmd::predictor_input_tensors( + const std::shared_ptr& predictor, + const std::vector& dcoord_, + const int& ntypes, + const std::vector& datype_, + const std::vector& dbox, + InputNlist& dlist, + const std::vector& fparam_, + const std::vector& aparam_, + const deepmd::AtomMap& atommap, + const int nghost, + const int ago, + const std::string scope); +template int deepmd::predictor_input_tensors( + const std::shared_ptr& predictor, + const std::vector& dcoord_, + const int& ntypes, + const std::vector& datype_, + const std::vector& dbox, + InputNlist& dlist, + const std::vector& fparam_, + const std::vector& aparam_, + const deepmd::AtomMap& atommap, + const int nghost, + const int ago, + const std::string scope); + +template int deepmd::predictor_input_tensors( + const std::shared_ptr& predictor, + const std::vector& dcoord_, + const int& ntypes, + const std::vector& datype_, + const std::vector& dbox, + InputNlist& dlist, + const std::vector& fparam_, + const std::vector& aparam_, + const deepmd::AtomMap& atommap, + const int nghost, + const int ago, + const std::string scope); +template int deepmd::predictor_input_tensors( + const std::shared_ptr& predictor, + const std::vector& dcoord_, + const int& ntypes, + const std::vector& datype_, + const std::vector& dbox, + InputNlist& dlist, + const std::vector& fparam_, + const std::vector& aparam_, + const deepmd::AtomMap& atommap, + const int nghost, + const int ago, + const std::string scope); +/*paddle end*/ template int deepmd::session_input_tensors_mixed_type( std::vector>& input_tensors,