COMPILE.md 13.7 KB
Newer Older
J
Jiawei Wang 已提交
1
# How to compile PaddleServing
D
Dong Daxiang 已提交
2

J
Jiawei Wang 已提交
3 4 5
([简体中文](./COMPILE_CN.md)|English)

## Compilation environment requirements
B
barrierye 已提交
6

B
barrierye 已提交
7 8
|            module            |              version              |
| :--------------------------: | :-------------------------------: |
W
wangjiawei04 已提交
9 10 11
|              OS              |     Ubuntu16 and 18/CentOS 7      |
|             gcc              | 4.8.5(Cuda 9.0 and 10.0) and 8.2(Others) |
|           gcc-c++            | 4.8.5(Cuda 9.0 and 10.0) and 8.2(Others) |
B
barrierye 已提交
12
|            cmake             |          3.2.0 and later          |
W
wangjiawei04 已提交
13
|            Python            |  2.7.2 and later / 3.5.1 and later |
B
barrierye 已提交
14 15 16 17 18 19 20
|              Go              |          1.9.2 and later          |
|             git              |         2.17.1 and later          |
|         glibc-static         |               2.17                |
|        openssl-devel         |              1.0.2k               |
|         bzip2-devel          |          1.0.6 and later          |
| python-devel / python3-devel | 2.7.5 and later / 3.6.8 and later |
|         sqlite-devel         |         3.7.17 and later          |
W
wangjiawei04 已提交
21
|           patchelf           |                0.9                |
B
barrierye 已提交
22 23 24
|           libXext            |               1.3.3               |
|            libSM             |               1.2.2               |
|          libXrender          |              0.9.10               |
D
Dong Daxiang 已提交
25

B
barrierye 已提交
26
It is recommended to use Docker for compilation. We have prepared the Paddle Serving compilation environment for you, see [this document](DOCKER_IMAGES.md).
B
barrierye 已提交
27

J
Jiawei Wang 已提交
28
## Get Code
D
Dong Daxiang 已提交
29 30 31

``` python
git clone https://github.com/PaddlePaddle/Serving
32
cd Serving && git submodule update --init --recursive
D
Dong Daxiang 已提交
33 34
```

W
wangjiawei04 已提交
35
## PYTHONROOT settings
D
Dong Daxiang 已提交
36

B
barrierye 已提交
37
```shell
W
wangjiawei04 已提交
38 39
# For example, the path of python is /usr/bin/python, you can set PYTHONROOT
export PYTHONROOT=/usr
D
Dong Daxiang 已提交
40 41
```

W
wangjiawei04 已提交
42
If you are using a Docker development image, please follow the following to determine the Python version to be compiled, and set the corresponding environment variables
B
barrierye 已提交
43

W
wangjiawei04 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
```
#Python 2.7
export PYTHONROOT=/usr/local/python2.7.15/
export PYTHON_INCLUDE_DIR=$PYTHONROOT/include/python2.7/
export PYTHON_LIBRARIES=$PYTHONROOT/lib/libpython2.7.so
export PYTHON_EXECUTABLE=$PYTHONROOT/bin/python2.7

#Python 3.5
export PYTHONROOT=/usr/local/python3.5.1
export PYTHON_INCLUDE_DIR=$PYTHONROOT/include/python3.5m
export PYTHON_LIBRARIES=$PYTHONROOT/lib/libpython3.5m.so
export PYTHON_EXECUTABLE=$PYTHONROOT/bin/python3.5

#Python3.6
export PYTHONROOT=/usr/local/
export PYTHON_INCLUDE_DIR=$PYTHONROOT/include/python3.6m
export PYTHON_LIBRARIES=$PYTHONROOT/lib/libpython3.6m.so
export PYTHON_EXECUTABLE=$PYTHONROOT/bin/python3.6

#Python3.7
export PYTHONROOT=/usr/local/
export PYTHON_INCLUDE_DIR=$PYTHONROOT/include/python3.7m
export PYTHON_LIBRARIES=$PYTHONROOT/lib/libpython3.7m.so
export PYTHON_EXECUTABLE=$PYTHONROOT/bin/python3.7

#Python3.8
export PYTHONROOT=/usr/local/
export PYTHON_INCLUDE_DIR=$PYTHONROOT/include/python3.8
export PYTHON_LIBRARIES=$PYTHONROOT/lib/libpython3.8.so
export PYTHON_EXECUTABLE=$PYTHONROOT/bin/python3.8
B
barrierye 已提交
74

W
wangjiawei04 已提交
75
```
B
barrierye 已提交
76 77 78 79

## Install Python dependencies

```shell
T
TeslaZhao 已提交
80
pip install -r python/requirements.txt -i https://mirror.baidu.com/pypi/simple
B
barrierye 已提交
81 82
```

W
wangjiawei04 已提交
83
If you use other Python version, please use the right `pip` accordingly.
B
barrierye 已提交
84

B
barriery 已提交
85
## GOPATH Setting
W
wangjiawei04 已提交
86
The default GOPATH is set to `$HOME/go`, you can also set it to other values. **If it is the Docker environment provided by Serving, you do not need to set up.**
B
barrierye 已提交
87

B
barriery 已提交
88 89 90 91 92 93 94 95
```shell
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
```

## Get go packages

```shell
M
fix ci  
MRXLT 已提交
96
go env -w GO111MODULE=on
M
MRXLT 已提交
97
go env -w GOPROXY=https://goproxy.cn,direct
M
MRXLT 已提交
98 99 100 101
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@v1.15.2
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@v1.15.2
go get -u github.com/golang/protobuf/protoc-gen-go@v1.4.3
go get -u google.golang.org/grpc@v1.33.0
B
barriery 已提交
102
```
M
bug fix  
MRXLT 已提交
103

B
barrierye 已提交
104

J
Jiawei Wang 已提交
105
## Compile Server
B
barrierye 已提交
106

J
Jiawei Wang 已提交
107
### Integrated CPU version paddle inference library
B
barrierye 已提交
108

D
Dong Daxiang 已提交
109
``` shell
B
barrierye 已提交
110
mkdir server-build-cpu && cd server-build-cpu
M
bug fix  
MRXLT 已提交
111
cmake -DPYTHON_INCLUDE_DIR=$PYTHONROOT/include/python2.7/ \
M
MRXLT 已提交
112 113 114
    -DPYTHON_LIBRARIES=$PYTHONROOT/lib/libpython2.7.so \
    -DPYTHON_EXECUTABLE=$PYTHONROOT/bin/python \
    -DSERVER=ON ..
D
Dong Daxiang 已提交
115 116 117
make -j10
```

J
Jiawei Wang 已提交
118
you can execute `make install` to put targets under directory `./output`, you need to add`-DCMAKE_INSTALL_PREFIX=./output`to specify output path to cmake command shown above.
B
barrierye 已提交
119

J
Jiawei Wang 已提交
120
### Integrated GPU version paddle inference library
121

W
wangjiawei04 已提交
122 123
Compared with CPU environment, GPU environment needs to refer to the following table,
**It should be noted that the following table is used as a reference for non-Docker compilation environment. The Docker compilation environment has been configured with relevant parameters and does not need to be specified in cmake process. **
M
MRXLT 已提交
124

W
wangjiawei04 已提交
125
| cmake environment variable | meaning | GPU environment considerations | whether Docker environment is needed |
T
TeslaZhao 已提交
126
|-----------------------|-------------------------------------|-------------------------------|--------------------|
T
TeslaZhao 已提交
127
| CUDA_TOOLKIT_ROOT_DIR | cuda installation path, usually /usr/local/cuda | Required for all environments | No (/usr/local/cuda) |
W
wangjiawei04 已提交
128 129 130
| CUDNN_LIBRARY | The directory where libcudnn.so.* is located, usually /usr/local/cuda/lib64/ | Required for all environments | No (/usr/local/cuda/lib64/) |
| CUDA_CUDART_LIBRARY | The directory where libcudart.so.* is located, usually /usr/local/cuda/lib64/ | Required for all environments | No (/usr/local/cuda/lib64/) |
| TENSORRT_ROOT | The upper level directory of the directory where libnvinfer.so.* is located, depends on the TensorRT installation directory | Cuda 9.0/10.0 does not need, other needs | No (/usr) |
M
MRXLT 已提交
131

T
TeslaZhao 已提交
132
If not in Docker environment, users can refer to the following execution methods. The specific path is subject to the current environment, and the code is only for reference.TENSORRT_LIBRARY_PATH is related to the TensorRT version and should be set according to the actual situation。For example, in the cuda10.1 environment, the TensorRT version is 6.0 (/usr/local/TensorRT-6.0.1.5/targets/x86_64-linux-gnu/),In the cuda10.2 environment, the TensorRT version is 7.1 (/usr/local/TensorRT-7.1.3.4/targets/x86_64-linux-gnu/).
W
wangjiawei04 已提交
133 134

``` shell
H
HexToString 已提交
135
export CUDA_PATH='/usr/local/cuda'
136 137
export CUDNN_LIBRARY='/usr/local/cuda/lib64/'
export CUDA_CUDART_LIBRARY="/usr/local/cuda/lib64/"
W
wangjiawei04 已提交
138
export TENSORRT_LIBRARY_PATH="/usr/local/TensorRT-6.0.1.5/targets/x86_64-linux-gnu/"
139

W
wangjiawei04 已提交
140 141 142 143
mkdir server-build-gpu && cd server-build-gpu
cmake -DPYTHON_INCLUDE_DIR=$PYTHON_INCLUDE_DIR \
    -DPYTHON_LIBRARIES=$PYTHON_LIBRARIES \
    -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
M
MRXLT 已提交
144 145
    -DCUDA_TOOLKIT_ROOT_DIR=${CUDA_PATH} \
    -DCUDNN_LIBRARY=${CUDNN_LIBRARY} \
146
    -DCUDA_CUDART_LIBRARY=${CUDA_CUDART_LIBRARY} \
T
TeslaZhao 已提交
147
    -DTENSORRT_ROOT=${TENSORRT_LIBRARY_PATH} \
M
MRXLT 已提交
148
    -DSERVER=ON \
W
wangjiawei04 已提交
149
    -DWITH_GPU=ON ..
D
Dong Daxiang 已提交
150 151 152
make -j10
```

W
wangjiawei04 已提交
153
Execute `make install` to put the target output in the `./output` directory.
B
barrierye 已提交
154

T
Thomas Young 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
### Compile C++ Server under the condition of WITH_OPENCV=ON
**Note:** Only when you need to redevelop the paddle serving C + + part, and the new code depends on the OpenCV library, you need to do so.

First of all , OpenCV library should be installed, if not, please refer to the `Compile and install OpenCV` section later in this article.

In the compile command, add `DOPENCV_DIR=${OPENCV_DIR}` and `DWITH_OPENCV=ON`,for example:
``` shell
OPENCV_DIR=your_opencv_dir #`your_opencv_dir` is the installation path of OpenCV library。
mkdir server-build-cpu && cd server-build-cpu
cmake -DPYTHON_INCLUDE_DIR=$PYTHON_INCLUDE_DIR/ \
    -DPYTHON_LIBRARIES=$PYTHON_LIBRARIES \
    -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
    -DOPENCV_DIR=${OPENCV_DIR} \
    -DWITH_OPENCV=ON
    -DSERVER=ON ..
make -j10
```

T
TeslaZhao 已提交
173
**Note:** After the compilation is successful, you need to set the `SERVING_BIN` path, see the following [Notes](https://github.com/PaddlePaddle/Serving/blob/develop/doc/COMPILE.md#Notes).
B
barrierye 已提交
174

J
Jiawei Wang 已提交
175
## Compile Client
D
Dong Daxiang 已提交
176 177

``` shell
B
barrierye 已提交
178
mkdir client-build && cd client-build
M
bug fix  
MRXLT 已提交
179 180 181 182
cmake -DPYTHON_INCLUDE_DIR=$PYTHONROOT/include/python2.7/ \
      -DPYTHON_LIBRARIES=$PYTHONROOT/lib/libpython2.7.so \
      -DPYTHON_EXECUTABLE=$PYTHONROOT/bin/python \
      -DCLIENT=ON ..
D
Dong Daxiang 已提交
183 184
make -j10
```
D
Dong Daxiang 已提交
185

J
Jiawei Wang 已提交
186
execute `make install` to put targets under directory `./output`
B
barrierye 已提交
187

B
barrierye 已提交
188 189


J
Jiawei Wang 已提交
190
## Compile the App
B
barrierye 已提交
191 192

```bash
B
barrierye 已提交
193
mkdir app-build && cd app-build
M
MRXLT 已提交
194 195 196 197
cmake -DPYTHON_INCLUDE_DIR=$PYTHONROOT/include/python2.7/ \
    -DPYTHON_LIBRARIES=$PYTHONROOT/lib/libpython2.7.so \
    -DPYTHON_EXECUTABLE=$PYTHONROOT/bin/python \
    -DAPP=ON ..
B
barrierye 已提交
198 199 200
make
```

B
barrierye 已提交
201 202


J
Jiawei Wang 已提交
203 204
## Install wheel package

B
barrierye 已提交
205
Regardless of the client, server or App part, after compiling, install the whl package in `python/dist/` in the temporary directory(`server-build-cpu`, `server-build-gpu`, `client-build`,`app-build`) of the compilation process.
206
for example:cd server-build-cpu/python/dist && pip install -U xxxxx.whl
B
barrierye 已提交
207 208


J
Jiawei Wang 已提交
209
## Note
B
barrierye 已提交
210

J
Jiawei Wang 已提交
211
When running the python server, it will check the `SERVING_BIN` environment variable. If you want to use your own compiled binary file, set the environment variable to the path of the corresponding binary file, usually`export SERVING_BIN=${BUILD_DIR}/core/general-server/serving`.
J
Jiawei Wang 已提交
212
BUILD_DIR is the absolute path of server build CPU or server build GPU。
213
for example: cd server-build-cpu && export SERVING_BIN=${PWD}/core/general-server/serving
B
barrierye 已提交
214

215

B
barrierye 已提交
216

B
barrierye 已提交
217 218 219 220 221 222
## Verify

Please use the example under `python/examples` to verify.



J
Jiawei Wang 已提交
223
## CMake Option Description
B
barrierye 已提交
224

J
Jiawei Wang 已提交
225
| Compile Options  |                    Description             | Default |
B
barrierye 已提交
226 227 228 229
| :--------------: | :----------------------------------------: | :--: |
|     WITH_AVX     | Compile Paddle Serving with AVX intrinsics | OFF  |
|     WITH_MKL     |  Compile Paddle Serving with MKL support   | OFF  |
|     WITH_GPU     |   Compile Paddle Serving with NVIDIA GPU   | OFF  |
H
HexToString 已提交
230
|     WITH_OPENCV  |    Compile Paddle Serving with OPENCV      | OFF  |
M
MRXLT 已提交
231 232 233
|  CUDNN_LIBRARY   |    Define CuDNN library and header path    |      |
| CUDA_TOOLKIT_ROOT_DIR |       Define CUDA PATH                |      |
|   TENSORRT_ROOT  |           Define TensorRT PATH             |      |
B
barrierye 已提交
234 235 236 237 238
|      CLIENT      |       Compile Paddle Serving Client        | OFF  |
|      SERVER      |       Compile Paddle Serving Server        | OFF  |
|       APP        |     Compile Paddle Serving App package     | OFF  |
|       PACK       |              Compile for whl               | OFF  |

J
Jiawei Wang 已提交
239
### WITH_GPU Option
B
barrierye 已提交
240

J
Jiawei Wang 已提交
241
Paddle Serving supports prediction on the GPU through the PaddlePaddle inference library. The WITH_GPU option is used to detect basic libraries such as CUDA/CUDNN on the system. If an appropriate version is detected, the GPU Kernel will be compiled when PaddlePaddle is compiled.
B
barrierye 已提交
242

J
Jiawei Wang 已提交
243
To compile the Paddle Serving GPU version on bare metal, you need to install these basic libraries:
B
barrierye 已提交
244 245 246

- CUDA
- CuDNN
M
MRXLT 已提交
247 248

To compile the TensorRT version, you need to install the TensorRT library.
B
barrierye 已提交
249

J
Jiawei Wang 已提交
250 251 252 253
Note here:

1. The basic library versions such as CUDA/CUDNN installed on the system where Serving is compiled, needs to be compatible with the actual GPU device. For example, the Tesla V100 card requires at least CUDA 9.0. If the version of the basic library such as CUDA used during compilation is too low, the generated GPU code is not compatible with the actual hardware device, which will cause the Serving process to fail to start or serious problems such as coredump.
2. Install the CUDA driver compatible with the actual GPU device on the system running Paddle Serving, and install the basic library compatible with the CUDA/CuDNN version used during compilation. If the version of CUDA/CuDNN installed on the system running Paddle Serving is lower than the version used at compile time, it may cause some cuda function call failures and other problems.
B
barrierye 已提交
254 255


J
Jiawei Wang 已提交
256
The following is the base library version matching relationship used by the PaddlePaddle release version for reference:
B
barrierye 已提交
257

W
wangjiawei04 已提交
258 259 260 261 262 263 264
|          |  CUDA   |   CuDNN      | TensorRT |
| :----:   | :-----: | :----------: | :----:   |
| post9    |  9.0    | CuDNN 7.6.4  |          |
| post10   |  10.0   | CuDNN 7.6.5  |          |
| post101  |  10.1   | CuDNN 7.6.5  | 6.0.1    |
| post102  |  10.2   | CuDNN 8.0.5  | 7.1.3    |
| post11   |  11.0   | CuDNN 8.0.4  | 7.1.3    |
B
barrierye 已提交
265

J
Jiawei Wang 已提交
266
### How to make the compiler detect the CuDNN library
B
barrierye 已提交
267

J
Jiawei Wang 已提交
268
Download the corresponding CUDNN version from NVIDIA developer official website and decompressing it, add `-DCUDNN_ROOT` to cmake command, to specify the path of CUDNN.
H
HexToString 已提交
269

T
Thomas Young 已提交
270 271
## Compile and install OpenCV
**Note:** You need to do this only if you need to import the opencv library into your C + + code.
H
HexToString 已提交
272

T
Thomas Young 已提交
273
* First of all, you need to download the source code compiled package in the Linux environment from the OpenCV official website. Taking OpenCV3.4.7 as an example, the download command is as follows.
H
HexToString 已提交
274 275 276 277 278 279 280 281

```
wget https://github.com/opencv/opencv/archive/3.4.7.tar.gz
tar -xf 3.4.7.tar.gz
```

Finally, you can see the folder of `opencv-3.4.7/` in the current directory.

T
Thomas Young 已提交
282
* Compile OpenCV, the OpenCV source path (`root_path`) and installation path (`install_path`) should be set by yourself. Enter the OpenCV source code path and compile it in the following way.
H
HexToString 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314


```shell
root_path=your_opencv_root_path
install_path=${root_path}/opencv3

rm -rf build
mkdir build
cd build

cmake .. \
    -DCMAKE_INSTALL_PREFIX=${install_path} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DWITH_IPP=OFF \
    -DBUILD_IPP_IW=OFF \
    -DWITH_LAPACK=OFF \
    -DWITH_EIGEN=OFF \
    -DCMAKE_INSTALL_LIBDIR=lib64 \
    -DWITH_ZLIB=ON \
    -DBUILD_ZLIB=ON \
    -DWITH_JPEG=ON \
    -DBUILD_JPEG=ON \
    -DWITH_PNG=ON \
    -DBUILD_PNG=ON \
    -DWITH_TIFF=ON \
    -DBUILD_TIFF=ON

make -j
make install
```

T
Thomas Young 已提交
315
Among them, `root_path` is the downloaded OpenCV source code path, and `install_path` is the installation path of OpenCV. After `make install` is completed, the OpenCV header file and library file will be generated in this folder for later source code compilation.
H
HexToString 已提交
316 317 318



T
Thomas Young 已提交
319
The final file structure under the OpenCV installation path is as follows.
H
HexToString 已提交
320 321 322 323 324 325 326 327

```
opencv3/
|-- bin
|-- include
|-- lib
|-- lib64
|-- share
T
Thomas Young 已提交
328
```