COMPILE.md 13.1 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
|              OS              |     Ubuntu16 and 18/CentOS 7      |
10 11
|             gcc              |          5.4.0(Cuda 10.1) and 8.2.0         |
|           gcc-c++            |          5.4.0(Cuda 10.1) and 8.2.0         |
B
barrierye 已提交
12
|            cmake             |          3.2.0 and later          |
13
|            Python            |          3.6.0 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
```
#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 已提交
62

W
wangjiawei04 已提交
63
```
B
barrierye 已提交
64 65 66 67

## Install Python dependencies

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

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

B
barriery 已提交
73
## GOPATH Setting
W
wangjiawei04 已提交
74
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 已提交
75

B
barriery 已提交
76 77 78 79 80 81 82 83
```shell
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
```

## Get go packages

```shell
M
fix ci  
MRXLT 已提交
84
go env -w GO111MODULE=on
M
MRXLT 已提交
85
go env -w GOPROXY=https://goproxy.cn,direct
M
MRXLT 已提交
86 87 88 89
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 已提交
90
```
M
bug fix  
MRXLT 已提交
91

B
barrierye 已提交
92

J
Jiawei Wang 已提交
93
## Compile Server
B
barrierye 已提交
94

J
Jiawei Wang 已提交
95
### Integrated CPU version paddle inference library
B
barrierye 已提交
96

D
Dong Daxiang 已提交
97
``` shell
B
barrierye 已提交
98
mkdir server-build-cpu && cd server-build-cpu
99 100 101
cmake -DPYTHON_INCLUDE_DIR=$PYTHON_INCLUDE_DIR \
    -DPYTHON_LIBRARIES=$PYTHON_LIBRARIES \
    -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
M
MRXLT 已提交
102
    -DSERVER=ON ..
D
Dong Daxiang 已提交
103 104 105
make -j10
```

J
Jiawei Wang 已提交
106
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 已提交
107

J
Jiawei Wang 已提交
108
### Integrated GPU version paddle inference library
109

W
wangjiawei04 已提交
110 111
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 已提交
112

W
wangjiawei04 已提交
113
| cmake environment variable | meaning | GPU environment considerations | whether Docker environment is needed |
T
TeslaZhao 已提交
114
|-----------------------|-------------------------------------|-------------------------------|--------------------|
T
TeslaZhao 已提交
115
| CUDA_TOOLKIT_ROOT_DIR | cuda installation path, usually /usr/local/cuda | Required for all environments | No (/usr/local/cuda) |
W
wangjiawei04 已提交
116 117 118
| 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 已提交
119

T
TeslaZhao 已提交
120
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 已提交
121 122

``` shell
H
HexToString 已提交
123
export CUDA_PATH='/usr/local/cuda'
124 125
export CUDNN_LIBRARY='/usr/local/cuda/lib64/'
export CUDA_CUDART_LIBRARY="/usr/local/cuda/lib64/"
W
wangjiawei04 已提交
126
export TENSORRT_LIBRARY_PATH="/usr/local/TensorRT-6.0.1.5/targets/x86_64-linux-gnu/"
127

W
wangjiawei04 已提交
128 129 130 131
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 已提交
132 133
    -DCUDA_TOOLKIT_ROOT_DIR=${CUDA_PATH} \
    -DCUDNN_LIBRARY=${CUDNN_LIBRARY} \
134
    -DCUDA_CUDART_LIBRARY=${CUDA_CUDART_LIBRARY} \
T
TeslaZhao 已提交
135
    -DTENSORRT_ROOT=${TENSORRT_LIBRARY_PATH} \
M
MRXLT 已提交
136
    -DSERVER=ON \
W
wangjiawei04 已提交
137
    -DWITH_GPU=ON ..
D
Dong Daxiang 已提交
138 139 140
make -j10
```

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

T
Thomas Young 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
### 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 已提交
161
**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 已提交
162

J
Jiawei Wang 已提交
163
## Compile Client
D
Dong Daxiang 已提交
164 165

``` shell
B
barrierye 已提交
166
mkdir client-build && cd client-build
167 168 169 170
cmake -DPYTHON_INCLUDE_DIR=$PYTHON_INCLUDE_DIR \
    -DPYTHON_LIBRARIES=$PYTHON_LIBRARIES \
    -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
    -DCLIENT=ON ..
D
Dong Daxiang 已提交
171 172
make -j10
```
D
Dong Daxiang 已提交
173

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

B
barrierye 已提交
176 177


J
Jiawei Wang 已提交
178
## Compile the App
B
barrierye 已提交
179 180

```bash
B
barrierye 已提交
181
mkdir app-build && cd app-build
182 183 184
cmake -DPYTHON_INCLUDE_DIR=$PYTHON_INCLUDE_DIR \
    -DPYTHON_LIBRARIES=$PYTHON_LIBRARIES \
    -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
M
MRXLT 已提交
185
    -DAPP=ON ..
B
barrierye 已提交
186 187 188
make
```

B
barrierye 已提交
189 190


J
Jiawei Wang 已提交
191 192
## Install wheel package

B
barrierye 已提交
193
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.
194
for example:cd server-build-cpu/python/dist && pip install -U xxxxx.whl
B
barrierye 已提交
195 196


J
Jiawei Wang 已提交
197
## Note
B
barrierye 已提交
198

J
Jiawei Wang 已提交
199
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 已提交
200
BUILD_DIR is the absolute path of server build CPU or server build GPU。
201
for example: cd server-build-cpu && export SERVING_BIN=${PWD}/core/general-server/serving
B
barrierye 已提交
202

203

B
barrierye 已提交
204

B
barrierye 已提交
205 206 207 208 209 210
## Verify

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



J
Jiawei Wang 已提交
211
## CMake Option Description
B
barrierye 已提交
212

J
Jiawei Wang 已提交
213
| Compile Options  |                    Description             | Default |
B
barrierye 已提交
214 215 216 217
| :--------------: | :----------------------------------------: | :--: |
|     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 已提交
218
|     WITH_OPENCV  |    Compile Paddle Serving with OPENCV      | OFF  |
M
MRXLT 已提交
219 220 221
|  CUDNN_LIBRARY   |    Define CuDNN library and header path    |      |
| CUDA_TOOLKIT_ROOT_DIR |       Define CUDA PATH                |      |
|   TENSORRT_ROOT  |           Define TensorRT PATH             |      |
B
barrierye 已提交
222 223 224 225 226
|      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 已提交
227
### WITH_GPU Option
B
barrierye 已提交
228

J
Jiawei Wang 已提交
229
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 已提交
230

J
Jiawei Wang 已提交
231
To compile the Paddle Serving GPU version on bare metal, you need to install these basic libraries:
B
barrierye 已提交
232 233 234

- CUDA
- CuDNN
M
MRXLT 已提交
235 236

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

J
Jiawei Wang 已提交
238 239 240 241
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 已提交
242 243


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

W
wangjiawei04 已提交
246 247 248 249 250
|          |  CUDA   |   CuDNN      | TensorRT |
| :----:   | :-----: | :----------: | :----:   |
| 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 已提交
251

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

J
Jiawei Wang 已提交
254
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 已提交
255

T
Thomas Young 已提交
256 257
## 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 已提交
258

T
Thomas Young 已提交
259
* 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 已提交
260 261 262 263 264 265 266 267

```
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 已提交
268
* 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 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300


```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 已提交
301
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 已提交
302 303 304



T
Thomas Young 已提交
305
The final file structure under the OpenCV installation path is as follows.
H
HexToString 已提交
306 307 308 309 310 311 312 313

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