COMPILE.md 13.4 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

H
HexToString 已提交
120 121
### Compile C++ Server under the condition of WITH_OPENCV=ON
First of all , opencv library should be installed, if not, please refer to the `Compile and install opencv` section later in this article.
H
HexToString 已提交
122

H
HexToString 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135
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
```

J
Jiawei Wang 已提交
136
### Integrated GPU version paddle inference library
137

W
wangjiawei04 已提交
138 139
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 已提交
140

W
wangjiawei04 已提交
141
| cmake environment variable | meaning | GPU environment considerations | whether Docker environment is needed |
T
TeslaZhao 已提交
142
|-----------------------|-------------------------------------|-------------------------------|--------------------|
T
TeslaZhao 已提交
143
| CUDA_TOOLKIT_ROOT_DIR | cuda installation path, usually /usr/local/cuda | Required for all environments | No (/usr/local/cuda) |
W
wangjiawei04 已提交
144 145 146
| 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 已提交
147

T
TeslaZhao 已提交
148
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 已提交
149 150

``` shell
H
HexToString 已提交
151
export CUDA_PATH='/usr/local/cuda'
152 153
export CUDNN_LIBRARY='/usr/local/cuda/lib64/'
export CUDA_CUDART_LIBRARY="/usr/local/cuda/lib64/"
W
wangjiawei04 已提交
154
export TENSORRT_LIBRARY_PATH="/usr/local/TensorRT-6.0.1.5/targets/x86_64-linux-gnu/"
155

W
wangjiawei04 已提交
156 157 158 159
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 已提交
160 161
    -DCUDA_TOOLKIT_ROOT_DIR=${CUDA_PATH} \
    -DCUDNN_LIBRARY=${CUDNN_LIBRARY} \
162
    -DCUDA_CUDART_LIBRARY=${CUDA_CUDART_LIBRARY} \
T
TeslaZhao 已提交
163
    -DTENSORRT_ROOT=${TENSORRT_LIBRARY_PATH} \
M
MRXLT 已提交
164
    -DSERVER=ON \
W
wangjiawei04 已提交
165
    -DWITH_GPU=ON ..
D
Dong Daxiang 已提交
166 167 168
make -j10
```

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

W
wangjiawei04 已提交
171
**Note:** After the compilation is successful, you need to set the `SERVING_BIN` path, see the following [Notes](COMPILE.md#Notes) ).
B
barrierye 已提交
172

J
Jiawei Wang 已提交
173
## Compile Client
D
Dong Daxiang 已提交
174 175

``` shell
B
barrierye 已提交
176
mkdir client-build && cd client-build
M
bug fix  
MRXLT 已提交
177 178 179 180
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 已提交
181 182
make -j10
```
D
Dong Daxiang 已提交
183

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

B
barrierye 已提交
186 187


J
Jiawei Wang 已提交
188
## Compile the App
B
barrierye 已提交
189 190

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

B
barrierye 已提交
199 200


J
Jiawei Wang 已提交
201 202
## Install wheel package

B
barrierye 已提交
203
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.
204
for example:cd server-build-cpu/python/dist && pip install -U xxxxx.whl
B
barrierye 已提交
205 206


J
Jiawei Wang 已提交
207
## Note
B
barrierye 已提交
208

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

213

B
barrierye 已提交
214

B
barrierye 已提交
215 216 217 218 219 220
## Verify

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



J
Jiawei Wang 已提交
221
## CMake Option Description
B
barrierye 已提交
222

J
Jiawei Wang 已提交
223
| Compile Options  |                    Description             | Default |
B
barrierye 已提交
224 225 226 227
| :--------------: | :----------------------------------------: | :--: |
|     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 已提交
228
|     WITH_OPENCV  |    Compile Paddle Serving with OPENCV      | OFF  |
M
MRXLT 已提交
229 230 231
|  CUDNN_LIBRARY   |    Define CuDNN library and header path    |      |
| CUDA_TOOLKIT_ROOT_DIR |       Define CUDA PATH                |      |
|   TENSORRT_ROOT  |           Define TensorRT PATH             |      |
B
barrierye 已提交
232 233 234 235 236
|      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 已提交
237
### WITH_GPU Option
B
barrierye 已提交
238

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

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

- CUDA
- CuDNN
M
MRXLT 已提交
245 246

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

J
Jiawei Wang 已提交
248 249 250 251
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 已提交
252 253


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

W
wangjiawei04 已提交
256 257 258 259 260 261 262
|          |  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 已提交
263

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

J
Jiawei Wang 已提交
266
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 已提交
267

H
HexToString 已提交
268
## Compile and install opencv
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

* 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.

```
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.

* 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.


```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
```

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 OCR source code compilation.



The final file structure under the opencv installation path is as follows.

```
opencv3/
|-- bin
|-- include
|-- lib
|-- lib64
|-- share
```