README.md 8.1 KB
Newer Older
H
Helin Wang 已提交
1
# Design Doc: Distributed Training
H
Helin Wang 已提交
2 3 4

## Objective

5
We want Paddle to support training on the general-purpose cluster. The cluster runs Paddle, the web server (e.g., Nginx), the log collector (e.g., fluentd), the distributed queue service (e.g., Kafka), the log joiner and other data processors written using Storm, Spark, and Hadoop MapReduce on the same cluster. As illustrated in the following graph:
H
Helin Wang 已提交
6

H
Helin Wang 已提交
7
<img src="src/arch.png"/>
H
Helin Wang 已提交
8 9 10

This poses new challenges for Paddle,

H
Helin Wang 已提交
11
- Paddle need to be fault tolerant.
H
Helin Wang 已提交
12
- Input training data can be online data from real time logs or batch data from distributed file system.
H
Helin Wang 已提交
13
- User needs a simple way to train model on Paddle cloud. Complexities such as job scheduling should be hidden from user.
H
Helin Wang 已提交
14 15 16

## Training Job

H
Helin Wang 已提交
17
A training job will be created once user asks Paddle cloud to train a model. The training job is made up of different processes that collaboratively consume data and produce a trained model. There are three kinds of processes:
H
Helin Wang 已提交
18 19 20 21 22

- Master process
- Trainer process
- Parameter server process

H
Helin Wang 已提交
23
One training job will only have one master process, typically multiple trainer processes and parameter server processes. Their relation is illustrated in the following graph:
H
Helin Wang 已提交
24

H
Helin Wang 已提交
25
<img src="src/paddle-model-sharding.png"/>
H
Helin Wang 已提交
26 27 28

### Master Process

29
The master process will:
H
Helin Wang 已提交
30

31 32 33 34 35 36 37 38
- Shard dataset into [tasks](#task) and dispatch tasks to trainers.
- Keep track of training progress on the dataset with [task queue](#task-queue). A training job will iterate on the dataset for a full pass until it goes into next pass.

Now we will explain the concepts mentioned above:

#### Task 

A task is a piece of sharded data to be trained. The total number of tasks will be much bigger than the total number of trainers. The number of data instances inside a task will be much bigger than the mini-batch size.
H
Helin Wang 已提交
39 40 41

#### Task Queue

42
Master process has three task queues to track training progress. As illustrated in the graph below, Job A and Job B both have one master process. Each master process has three task queues.
H
Helin Wang 已提交
43

H
Helin Wang 已提交
44
<img src="src/paddle-task-queues.png"/>
H
Helin Wang 已提交
45

H
Helin Wang 已提交
46
- The todo queue holds tasks to be dispatched. When a job starts, the master process fills in the todo queue with all tasks.
47
- The pending queue holds tasks that are currently training by trainers.
H
Helin Wang 已提交
48
- the done queue holds tasks that are already trained.
H
Helin Wang 已提交
49

50
The life cycle of a single task is illustrated below:
H
Helin Wang 已提交
51

H
Helin Wang 已提交
52
<img src="src/paddle-task-states.png"/>
H
Helin Wang 已提交
53 54

1. When a new pass of training starts, all tasks will be placed in the todo queue.
H
Helin Wang 已提交
55 56
1. The master process will dispatch few tasks to each trainer at a time, puts them in the pending queue and waits for completion.
1. The trainer will work on it's tasks and tell the master process once a task is completed. The master process will dispatch a new task to that trainer.
57 58
1. If a task timeout. the master process will move it back to the todo queue. The timeout count will increase by one. If the timeout count is above an threashold, the task is likely to cause a trainer to crash, so it will be discarded.
1. The master process will move completed task to the done queue. When the todo queue is empty, the master process will start a new pass by moving all tasks in the done queue to todo queue and resetting the timeout counter of all tasks to zero.
H
Helin Wang 已提交
59 60 61

### Trainer Process

62
The trainer process will:
H
Helin Wang 已提交
63

64 65
- Receive the tasks from the master.
- Work on the tasks: alculate and upload gradient to the parameter servers, and update local model by downloading new parameters from the parameter servers.
H
Helin Wang 已提交
66

67
### Parameter Server Process
H
Helin Wang 已提交
68

69
Parameter server processes hold the parameters collabratively. The parameters are sharded on different parameter servers.
H
Helin Wang 已提交
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84
The parameter server will:

- Receive gradient from the trainers, update its parameters, and give the trainers the latest parameters.
- Periodically save its parameters to distributed file system by overriding the previous save.

### Optimization Algorithms

The communication pattern between the trainers and the parameter servers depends on the category of optimization algorithm:

- Synchronous Stochastic Gradient Decent (sync-SGD)

	Parameter server will wait for all trainer finish n-th mini-batch calculation and send their gradients before broadcasting new parameters to every trainer. Every trainer will wait for the new parameters before starting n+1-th mini-batch.
  
- Asynchronous Stochastic Gradient Decent (async-SGD)
H
Helin Wang 已提交
85

86 87 88 89 90
	There will no synchronization between different trainers, and parameter server updates its parameter as soon as it receives new gradient:

	- Each trainer uploads its accumulated gradient every **n** mini-batches.
	- Every **m** mini-batches, the trainer downloads new parameters from parameter server.
	- **n** and **m** do not have to be equal.
H
Helin Wang 已提交
91 92 93

## Fault Tolerant

H
Helin Wang 已提交
94
The training job will pause if the master process is dead, or any of the parameter server process is dead. They will be started by [Kubernetes](https://kubernetes.io/) and recover in few minutes. Please refer to [fault recovery](#fault-recovery).
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

The training job will continue to make progress if there is at least one training process running. The strategy depends on the type of optimization algorithm:

- sync-SGD

	TODO

- async-SGD

	Since async-SGD does not require synchronization between mini-batches, the system will by definition make process if at least one trainer is running.

## Fault Recovery

PaddlePaddle uses [etcd](https://github.com/coreos/etcd) to keep track of the states of processes. Because etcd is a distributed reliable key-value store, the restarted process can recover its states from etcd. The model parameter are periodically saved into distributed file system, so a restarted parameter server can recover its parameters from the saved file.

H
Helin Wang 已提交
110
Now we will introduce how each process recovers from failure, the graph below provides an illustration:
111

H
Helin Wang 已提交
112
<img src="src/paddle-etcd.png"/>
113 114 115

### Master Process

H
Helin Wang 已提交
116
When the master is started by the Kubernetes, it executes the following steps at startup:
117 118 119 120 121 122 123 124

1. Grabs a unique *master* lock in etcd, which prevents concurrent master instantiations.
1. Recovers the task queues from etcd if they already exists, otherwise the master will create them.
1. Watches the trainer prefix keys `/trainer/` on etcd to find the live trainers.
1. Starts dispatching the tasks to the trainers.

The master process will kill itself if its etcd lease expires.

H
Helin Wang 已提交
125
When the master process is dead for any reason, Kubernetes will restart it. It will be online again with all states recovered from etcd in few minutes.
126 127 128

### Trainer Process

H
Helin Wang 已提交
129
When the trainer is started by the Kubernetes, it executes the following steps at startup:
130 131 132 133 134 135 136 137 138

1. Watches the available parameter server prefix keys `/ps/` on etcd and waits until count of parameter servers reaches the desired count.
1. Generates an unique ID, and sets key `/trainer/<unique ID>` with its contact address as value. The key will be deleted when the lease expires, so the master will be aware of the trainer being online and offline.
1. Waits for tasks from the master to start training.

If trainer's etcd lease expires, it will try set key `/trainer/<unique ID>` again so that the master process can discover the trainer again.

### Parameter Server Process

H
Helin Wang 已提交
139 140 141 142 143 144 145 146 147 148 149 150
When the parameter server is started by Kubernetes, it executes the following steps at startup:

1. Read desired total number of parameter servers from etcd `/ps_desired`
1. Search though etcd keys `/ps/<index>` (`/ps/0`, `/ps/1`, ...) to find the first non-existant key whose index is smaller than the total number of parameter servers. Set the key using a transaction to avoid concurrent writes. The parameter server's index is inferred from the key name.

	The desired number of parameter servers is 3:
	
	<img src="src/paddle-ps-0.png"/>
	
	The third parameter server joined:
	
	<img src="src/paddle-ps-1.png"/>
151

H
Helin Wang 已提交
152
1. The parameter server can load parameters if there are already saved parameters in the save path (inferred from its index).
153 154
1. Now the parameter server is ready for the trainers' requests.

H
Helin Wang 已提交
155
If the parameter server's etcd lease expires, the parameter server will kill itself.
156 157 158 159 160 161 162 163 164 165 166 167 168 169


## Dynamic Scaling

### Trainer Scaling

TODO

### Parameter Server Scaling

Not planned for v1.

## Training Dataset Format

H
Helin Wang 已提交
170 171 172 173 174
TODO

## User Interface

TODO