README.md 8.0 KB
Newer Older
1
# Building PaddlePaddle
G
gongweibao 已提交
2

3
## Goals
G
gongweibao 已提交
4

Y
Yi Wang 已提交
5
We want the building procedure generates Docker images so that we can run PaddlePaddle applications on Kubernetes clusters.
G
gongweibao 已提交
6

Y
Yi Wang 已提交
7
We want to build .deb packages so that enterprise users can run PaddlePaddle applications without Docker.
G
gongweibao 已提交
8

Y
Yi Wang 已提交
9
We want to minimize the size of generated Docker images and .deb packages so to reduce the download time.
G
gongweibao 已提交
10

11
We want to encapsulate building tools and dependencies in a *development* Docker image so to ease the tools installation for developers.
12

Y
Yi Wang 已提交
13
Developers use various editors (emacs, vim, Eclipse, Jupyter Notebook), so the development Docker image contains only building tools, not editing tools, and developers are supposed to git clone source code into their development computers and map the code into the development container.
14

Y
Yi Wang 已提交
15
We want the procedure and tools also work with testing, continuous integration, and releasing.
16 17


18
## Docker Images
19

Y
Yi Wang 已提交
20
So we need two Docker images for each version of PaddlePaddle:
21 22 23

1. `paddle:<version>-dev`

Y
Yi Wang 已提交
24
   This a development image contains only the development tools and standardizes the building procedure.  Users include:
25 26 27 28 29

   - developers -- no longer need to install development tools on the host, and can build their current work on the host (development computer).
   - release engineers -- use this to build the official release from certain branch/tag on Github.com.
   - document writers / Website developers -- Our documents are in the source repo in the form of .md/.rst files and comments in source code.  We need tools to extract the information, typeset, and generate Web pages.

Y
Yi Wang 已提交
30
   Of course, developers can install building tools on their development computers.  But different versions of PaddlePaddle might require different set or version of building tools.  Also, it makes collaborative debugging easier if all developers use a unified development environment.
31 32

  The development image should include the following tools:
33 34 35 36 37 38 39 40

   - gcc/clang
   - nvcc
   - Python
   - sphinx
   - woboq
   - sshd

Y
Yi Wang 已提交
41
   Many developers work on a remote computer with GPU; they could ssh into the computer and  `docker exec` into the development container. However, running `sshd` in the container allows developers to ssh into the container directly.
42 43 44 45 46 47 48 49 50 51

1. `paddle:<version>`

   This is the production image, generated using the development image. This image might have multiple variants:

   - GPU/AVX   `paddle:<version>-gpu`
   - GPU/no-AVX  `paddle:<version>-gpu-noavx`
   - no-GPU/AVX  `paddle:<version>`
   - no-GPU/no-AVX  `paddle:<version>-noavx`

Y
Yi Wang 已提交
52
   We allow users to choose between GPU and no-GPU because the GPU version image is much larger than then the no-GPU version.
53

Y
Yi Wang 已提交
54
   We allow users the choice between AVX and no-AVX, because some cloud providers don't provide AVX-enabled VMs.
55 56 57 58 59 60 61 62


## Development Environment

Here we describe how to use above two images.  We start from considering our daily development environment.

Developers work on a computer, which is usually a laptop or desktop:

Y
Yi Wang 已提交
63
<img src="doc/paddle-development-environment.png" width=500 />
64 65 66

or, they might rely on a more sophisticated box (like with GPUs):

Y
Yi Wang 已提交
67
<img src="doc/paddle-development-environment-gpu.png" width=500 />
68

Y
Yi Wang 已提交
69
A principle here is that source code lies on the development computer (host) so that editors like Eclipse can parse the source code to support auto-completion.
70

71

72
## Usages
73

74
### Build the Development Docker Image
75

Y
Yi Wang 已提交
76
The following commands check out the source code to the host and build the development image `paddle:dev`:
77

78 79 80
```bash
git clone https://github.com/PaddlePaddle/Paddle paddle
cd paddle
Y
yi.wu 已提交
81
docker build -t paddle:dev .
82
```
83

Y
yi.wu 已提交
84
The `docker build` command assumes that `Dockerfile` is in the root source tree.  Note that in this design, this `Dockerfile` is this only one in our repo.
G
gongweibao 已提交
85

Y
yi.wu 已提交
86 87 88 89 90
Users can specify a Ubuntu mirror server for faster downloading:

```bash
docker build -t paddle:dev --build-arg UBUNTU_MIRROR=mirror://mirrors.ubuntu.com/mirrors.txt .
```
91

92
### Build PaddlePaddle from Source Code
G
gongweibao 已提交
93

94
Given the development image `paddle:dev`, the following command builds PaddlePaddle from the source tree on the development computer (host):
95

96
```bash
97
docker run --rm -v $PWD:/paddle -e "WITH_GPU=OFF" -e "WITH_AVX=ON" -e "WITH_TEST=OFF" -e "RUN_TEST=OFF" paddle:dev
98
```
G
gongweibao 已提交
99

Y
Yi Wang 已提交
100
This command mounts the source directory on the host into `/paddle` in the container, so the default entry point of `paddle:dev`, `build.sh`, could build the source code with possible local changes.  When it writes to `/paddle/build` in the container, it writes to `$PWD/build` on the host indeed.
101

102
`build.sh` builds the following:
103

104
- PaddlePaddle binaries,
Y
yi.wu 已提交
105
- `$PWD/build/paddle-<version>.deb` for production installation, and
106
- `$PWD/build/Dockerfile`, which builds the production Docker image.
107

Y
yi.wu 已提交
108 109 110
Users can specify the following Docker build arguments with either "ON" or "OFF" value:
- `WITH_GPU`: ***Required***. Generates NVIDIA CUDA GPU code and relies on CUDA libraries.
- `WITH_AVX`: ***Required***. Set to "OFF" prevents from generating AVX instructions. If you don't know what is AVX, you might want to set "ON".
Y
update  
yi.wu 已提交
111 112
- `WITH_TEST`: ***Optional, default OFF***. Build unit tests binaries. Once you've built the unit tests, you can run these test manually by the following command:
  ```bash
113
    docker run --rm -v $PWD:/paddle -e "WITH_GPU=OFF" -e "WITH_AVX=ON" paddle:dev sh -c "cd /paddle/build; make coverall"
Y
update  
yi.wu 已提交
114
  ```
Y
yi.wu 已提交
115
- `RUN_TEST`: ***Optional, default OFF***. Run unit tests after building. You can't run unit tests without building it.
116

117
### Build the Production Docker Image
118

119
The following command builds the production image:
120

121
```bash
Y
yi.wu 已提交
122
docker build -t paddle -f build/Dockerfile .
123
```
124

Y
yi.wu 已提交
125
This production image is minimal -- it includes binary `paddle`, the shared library `libpaddle.so`, and Python runtime.
126

127
### Run PaddlePaddle Applications
128

Y
Yi Wang 已提交
129
Again the development happens on the host.  Suppose that we have a simple application program in `a.py`, we can test and run it using the production image:
130

131
```bash
132
docker run --rm -it -v $PWD:/work paddle /work/a.py
133
```
134

135
But this works only if all dependencies of `a.py` are in the production image. If this is not the case, we need to build a new Docker image from the production image and with more dependencies installs.
136

Y
Yi Wang 已提交
137
### Build and Run PaddlePaddle Applications
G
gongweibao 已提交
138

139
We need a Dockerfile in https://github.com/paddlepaddle/book that builds Docker image `paddlepaddle/book:<version>`, basing on the PaddlePaddle production image:
140

141 142 143 144 145 146 147
```
FROM paddlepaddle/paddle:<version>
RUN pip install -U matplotlib jupyter ...
COPY . /book
EXPOSE 8080
CMD ["jupyter"]
```
G
gongweibao 已提交
148

149
The book image is an example of PaddlePaddle application image.  We can build it
150

151 152 153 154 155
```bash
git clone https://github.com/paddlepaddle/book
cd book
docker build -t book .
```
156

157
### Build and Run Distributed Applications
158

Y
Yi Wang 已提交
159
In our [API design doc](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/api.md#distributed-training), we proposed an API that starts a distributed training job on a cluster.  This API need to build a PaddlePaddle application into a Docker image as above and calls kubectl to run it on the cluster.  This API might need to generate a Dockerfile look like above and call `docker build`.
160

161
Of course, we can manually build an application image and launch the job using the kubectl tool:
G
gongweibao 已提交
162

163 164 165 166 167 168
```bash
docker build -f some/Dockerfile -t myapp .
docker tag myapp me/myapp
docker push
kubectl ...
```
Y
Yancey1989 已提交
169 170

### Reading source code with woboq codebrowser
171
For developers who are interested in the C++ source code, please use -e "WOBOQ=ON" to enable the building of C++ source code into HTML pages using [Woboq codebrowser](https://github.com/woboq/woboq_codebrowser).
Y
update  
Yancey1989 已提交
172

Y
update  
Yancey1989 已提交
173
- The following command builds PaddlePaddle, generates HTML pages from C++ source code, and writes HTML pages into `$HOME/woboq_out` on the host:
Y
Yancey1989 已提交
174 175

```bash
Y
update  
Yancey1989 已提交
176
docker run -v $PWD:/paddle -v $HOME/woboq_out:/woboq_out -e "WITH_GPU=OFF" -e "WITH_AVX=ON" -e "WITH_TEST=ON" -e "WOBOQ=ON" paddle:dev
Y
Yancey1989 已提交
177 178
```

Y
update  
Yancey1989 已提交
179
- You can open the generated HTML files in your Web browser. Or, if you want to run a Nginx container to serve them for a wider audience, you can run:
Y
Yancey1989 已提交
180 181

```
Y
update  
Yancey1989 已提交
182
docker run -v $HOME/woboq_out:/usr/share/nginx/html -d -p 8080:80 nginx
Y
Yancey1989 已提交
183
```