PaddlePaddle v0.10.0 uses the "trainer-parameter server"
PaddlePaddle version 0.10.0 uses the "trainer-parameter server" architecture. We run multiple instances of trainers (where each trainer runs the same model) and parameter servers for distributed training. This architecture serves well, but has few limitations:
architecture. We run multiple replicated instances of trainers (runs
the same code written by the user) and parameter servers for
distributed training. This architecture served us well, but has some
limitations:
1. Need to write special code to handle tasks which should only be run
1. There is a need to write special code that handles tasks which should only be run on a single trainer. E.g., initializing the model, saving the model etc.
by a single trainer. E.g., initializing model and saving model.
2. Model parallelism is hard: need to write if-else branches conditioned
2. Model parallelism is hard: It would need all the if-else branches conditioned on the trainer ID to partition the model onto the trainers, and eventually manually writing out the inter-model-shard communication code to communicate between different trainers.
on the trainer ID to partition model onto each trainer, and manually
write the inter-model-shard communication code.
3. The user can not directly specify the parameter update rule: need
3. The user can not directly specify the parameter update rule: This would need to modify the parameter server code and compile a new binary. This makes things more complicated for researchers: A lot of extra effort is required to make this work. Besides, the training job submission program may not allow running arbitrary binaries.
to modify the parameter server C++ code and compile a new
binary. This adds complication for researchers: A lot of extra
effort is required. Besides, the training job submission program
may not allow running arbitrary binaries.
This design doc discusses PaddlePaddle's new distributed training
This design doc discusses PaddlePaddle's new distributed training architecture that addresses the above mentioned limitations.
architecture that addresses the above limitations.
## Analysis
## Analysis
We will assume the user writes the trainer program by Python, the same
The assumption is that the user writes the trainer program in either Python or C++.
analysis holds if the trainer program is written in C++.
### Limitation 1
### Limitation 1
If we look at the Python code that the user writes, there are two
There are two basic functionalities in the trainer program:
kinds of functionalities:
- The training logic such as load / save model and print log.
1. The training logic such as loading / saving the model and printing out the logs.
- The neural network definition such as the definition of the data
2. The neural network definition such as the definition of the data layer, the fully connected layer, the cost function and the
layer, the fully connected layer, the cost function and the
optimizer.
optimizer.
When we training with PaddlePaddle v0.10.0 distributedly, multiple
When we train using PaddlePaddle v0.10.0 in a distributed fashion, multiple instances of the same Python code are run on different nodes, hence both: the
replicated Python instances are running on different nodes: both the
training logic as well as the neural network computation logic, is replicated.
training logic and the neural network computation is replicated.
The tasks that should only run once all belong to the training logic,
The tasks that only need to be run once belong to the training logic. Hence if we only replicate the neural network computation part, and do **not**
if we only replicate the neural network computation, but do **not**
replicate the training logic, the limitation mentioned above can be avoided.
replicate the training logic, the limitation could be solved.
### Limitation 2
### Limitation 2
Model parallelism means running a single model on multiple nodes by
Model parallelism means that a single model is partitioned into different components and each node runs one of the component separately. This comes at the extra cost of managing the
partitioning the model onto different nodes and managing the
inter-model-shard communication between nodes.
inter-model-shard communications.
PaddlePaddle should be able to modify the nerual network computation
PaddlePaddle should ideally be able to modify the neural network computation and figure out the support for model parallelism automatically. However, the
definition to support model parallelism automatically. However, the
computation is only specified in Python code which sits outside of PaddlePaddle, hence PaddlePaddle can not support the feature in this setup.
computation is only specified in Python code, and PaddlePaddle can not
modify Python code.
Just like compiler uses a intermediate representation (IR) so that
Similar to how a compiler uses an intermediate representation (IR) so that the programmer does not need to manually optimize their code for most of the cases, we can have an intermediate representation in PaddlePaddle as well. The compiler optimizes the IR as follows:
programmer does not need to manually optimize their code in most of
the cases - the compiler will optimize the IR:
<img src="src/compiler.png"/>
<img src="src/compiler.png"/>
We can have our own IR too: PaddlePaddle can support model parallel by
PaddlePaddle can support model parallelism by converting the IR so that the user no longer needs to manually perform the computation and operations in the Python component:
converting the IR so the user no longer need to manually do it in
Python:
<img src="src/paddle-compile.png"/>
<img src="src/paddle-compile.png"/>
The IR for PaddlePaddle after refactor is called `Block`, it specifies
The IR for PaddlePaddle after refactoring is called a `Block`, it specifies the computation dependency graph and the variables used in the computation.
the computation dependency graph and the variables used in the
computation.
### Limitation 3
### Limitation 3
The user can not directly specify the parameter update rule for the
The user can not directly specify the parameter update rule for the parameter server in the Python module, since the parameter server does not use the same computation definition as the trainer. Instead, the update rule is baked inside the parameter server. The user can not specify the update rule explicitly.
parameter server because the parameter server does not use the same
computation definition as the trainer. Instead, the update rule is
baked in the parameter server. The user can not specify the update
rule in the same way of specifying the trainer computation.
This could be fixed by making the parameter server run the same
This could be fixed by making the parameter server run the same computation definition as the trainer (the user's Python module). For a detailed explanation, refer to this document -
computation definition as the trainer. For a detailed explanation,
please
see
[Design Doc: Operation Graph Based Parameter Server](./dist_train.md)
[Design Doc: Operation Graph Based Parameter Server](./dist_train.md)
## Distributed Training Architecture
## Distributed Training Architecture
The new distributed training architecture can address the above
The revamped distributed training architecture can address the above discussed limitations. Below is the illustration of how it does so:
limitations. Below is the illustration:
<img src="src/distributed_architecture.png"/>
<img src="src/distributed_architecture.png"/>
The architecture includes major components: *PaddlePaddle Python*,
The major components in the architecture are: *PaddlePaddle Python*, *PaddlePaddle converter* and *PaddlePaddle runtime*.
*PaddlePaddle converter* and *PaddlePaddle runtime*:
### PaddlePaddle Python
### PaddlePaddle Python
PaddlePaddle Python is the Python library that user's Python trainer
PaddlePaddle Python is the Python library that user's Python code invokes, to read the data. build the neural network topology, start training, etc.
invoke to build the neural network topology, start training, etc.
```Python
```Python
paddle.init()
paddle.init()
...
@@ -117,102 +81,60 @@ for i in range(1000):
...
@@ -117,102 +81,60 @@ for i in range(1000):
print cost_val
print cost_val
```
```
The code above is a typical Python trainer code, the neural network
The above code is what a typical Python trainer code is, the neural network topology is built using the helper functions such as `paddle.layer.fc`. Training is done by calling `session.eval` iteratively.
topology is built using helper functions such as
`paddle.layer.fc`. The training is done by calling `session.eval`
iteratively.
#### session.eval
#### session.eval
As shown in the graph, `session.eval` sends the IR and the evaluation
As shown in the graph, `session.eval` sends the IR and the evaluation inputs or targets to the PaddlePaddle cluster for evaluation.
inputs/targets to the PaddlePaddle cluster for evaluation. The
The targets can be any variable in the computation graph. When the target is say, the `optimizer` variable, the neural network will be optimized once. When the target is the `cost` variable, `session.eval` returns the cost value. Based on what the target is, an appropriate action is taken.
targets can be any variable in the computation graph. When the target
is the `optimizer` variable, the neural network will be optimized
once. When the target is the `cost` variable, `session.eval` returns
the cost value.
The Python `session` is a wrapper of the C++ `Session` class. For more
The Python `session` is a wrapper of the C++ `Session` class. For more information about `Session`, refer to this document - [Design Doc: Session](./session.md).
information about `Session`, please
see [Design Doc: Session](./session.md).
### PaddlePaddle Converter
### PaddlePaddle Converter
PaddlePaddle converter automatically converts the IR in the request
The PaddlePaddle converter automatically converts the IR in the request (IR and evaluation inputs/targets) from PaddlePaddle Python to partitioned IRs and dispatches the new IRs and evaluation inputs/targets to different PaddlePaddle runtimes. Below are the steps that are followed :
(IR and evaluation inputs/targets) from PaddlePaddle Python to new
partitioned IRs and dispatch the new IRs and evaluation inputs/targets
to different PaddlePaddle runtimes. Below are the steps:
1. Add `feed` OP that feeds the eval inputs, and `fetch` OP that
1. Add a `feed` OP that feeds the eval inputs, and a `fetch` OP that fetches the eval targets to the IR.
fetches the eval targets to the IR.
1. Extract a new computation (sub)graph with `feed` and `fetch` OP as
2. Extract a new computation (sub)graph with the `feed` and `fetch` OPs as the boundary. The runtime does not need to run the OP that is not dependent on the `fetch` OP.
the boundary. The runtime does not need to run the OP that is not
dependent by the `fetch` OP.
1. Optimizes the computation graph.
3. Optimize the computation graph.
1. Place the OPs in the graph onto different devices on different
4. Place the OPs in the graph onto different devices on different PaddlePaddle runtime according to a placement algorithm and the device constraints specified by the user.
PaddlePaddle runtime according to a placement algorithm and device
constraint specified by the user.
1. Partition the graph according to runtime boundaries and add `send` /
5. Partition the graph according to runtime boundaries and add `send` / `recv` OP pair on the runtime boundaries.
`recv` OP pair on the runtime boundaries.
1. Dispatch the partitioned graph to different PaddlePaddle runtimes.
6. Dispatch the partitioned graph to different PaddlePaddle runtimes.
7. PaddlePaddle runtimes with the `fetch` OP reports evaluation results back to the converter, the converter reports the evaluation results back to the PaddlePaddle Python.
1. PaddlePaddle runtimes with the `fetch` OP reports evaluation
results back to the converter, the convert reports the evaluation
results back to the PaddlePaddle Python.
The output IRs will be cached to optimize the conversion latency.
The output IRs will be cached to optimize the conversion latency.
#### Placement Algorithm
#### Placement Algorithm
Our first implementation will only support "trainer-parameter server"
Our first implementation will only support "trainer-parameter server" placement: the parameters, initializers, and optimizers are all placed on the PaddlePaddle runtimes with the parameter server role. Everything else will be placed on the PaddlePaddle runtimes with the trainer role. This has the same functionality as the "trainer-parameter server" architecture of PaddlePaddle v0.10.0, but is more generic and flexible.
placement: the parameters, initializers, and optimizers are placed on
the PaddlePaddle runtimes with the parameter server role. And
everything else will be placed on the PaddlePaddle runtimes with the
trainer role. This has the same functionality of our
"trainer-parameter server" architecture of PaddlePaddle v0.10.0, but
is more general and flexible.
In the future, we will implement the general placement algorithm,
In the future, a more general placement algorithm should be implemented, which makes placements according to the input IR, and a model of device computation time and device communication time. Model parallelism requires the generic placement algorithm.
which makes placements according to the input IR, and a model of
device computation time and device communication time. Model
parallelism requires the general placement algorithm.
### PaddlePaddle Runtime
### PaddlePaddle Runtime
The PaddlePaddle runtime owns multiple devices (e.g., CPUs, GPUs) and
The PaddlePaddle runtime owns multiple devices (e.g., CPUs, GPUs) and runs the IR. The runtime does not need to do OP placement since it is already done by the converter.
runs the IR. The runtime does not need to do OP placement since it's
already done by the converter.
### Local Training Architecture
### Local Training Architecture
The local training architecture will be the same as the distributed
The local training architecture will be the same as the distributed training architecture, the difference is that everything runs locally, and there is just one PaddlePaddle runtime:
training architecture, the differences are everything runs locally,
and there is just one PaddlePaddle runtime:
<img src="src/local_architecture.png"/>
<img src="src/local_architecture.png"/>
### Training Data
### Training Data
In PaddlePaddle v0.10.0, training data is typically read
In PaddlePaddle v0.10.0, training data is typically read with a [data reader](../reader/README.md) from Python. This approach is no longer efficient when training in a distributed fashion since the Python process no longer runs on the same node with the trainer processes. The Python reader will need to read from the distributed filesystem (assuming it has the required access) and send to the trainers, doubling the network traffic.
with [data reader](../reader/README.md) from Python. This approach is
no longer efficient when training distributedly since the Python
When doing distributed training, the user can still use Python data reader: the training data are sent with `session.eval`. However this should be used for debugging purpose only. The users are encouraged to use the read data OPs.
process no longer runs on the same node with the trainer processes,
the Python reader will need to read from the distributed filesystem
(assuming it has the access) and send to the trainers, doubling the
network traffic.
When doing distributed training, the user can still use Python data
reader: the training data are sent with `session.eval`. However should
be used for debugging purpose only. The users are encouraged to use
<spanid="design-doc-distributed-training-architecture"></span><h1>Design Doc: Distributed Training Architecture<aclass="headerlink"href="#design-doc-distributed-training-architecture"title="Permalink to this headline">¶</a></h1>
<spanid="design-doc-distributed-training-architecture"></span><h1>Design Doc: Distributed Training Architecture<aclass="headerlink"href="#design-doc-distributed-training-architecture"title="Permalink to this headline">¶</a></h1>
<divclass="section"id="abstract">
<divclass="section"id="abstract">
<spanid="abstract"></span><h2>Abstract<aclass="headerlink"href="#abstract"title="Permalink to this headline">¶</a></h2>
<spanid="abstract"></span><h2>Abstract<aclass="headerlink"href="#abstract"title="Permalink to this headline">¶</a></h2>
<p>PaddlePaddle v0.10.0 uses the “trainer-parameter server”
<p>PaddlePaddle version 0.10.0 uses the “trainer-parameter server” architecture. We run multiple instances of trainers (where each trainer runs the same model) and parameter servers for distributed training. This architecture serves well, but has few limitations:</p>
architecture. We run multiple replicated instances of trainers (runs
the same code written by the user) and parameter servers for
distributed training. This architecture served us well, but has some
limitations:</p>
<olclass="simple">
<olclass="simple">
<li>Need to write special code to handle tasks which should only be run
<li>There is a need to write special code that handles tasks which should only be run on a single trainer. E.g., initializing the model, saving the model etc.</li>
by a single trainer. E.g., initializing model and saving model.</li>
<li>Model parallelism is hard: It would need all the if-else branches conditioned on the trainer ID to partition the model onto the trainers, and eventually manually writing out the inter-model-shard communication code to communicate between different trainers.</li>
<li>Model parallelism is hard: need to write if-else branches conditioned
<li>The user can not directly specify the parameter update rule: This would need to modify the parameter server code and compile a new binary. This makes things more complicated for researchers: A lot of extra effort is required to make this work. Besides, the training job submission program may not allow running arbitrary binaries.</li>
on the trainer ID to partition model onto each trainer, and manually
write the inter-model-shard communication code.</li>
<li>The user can not directly specify the parameter update rule: need
to modify the parameter server C++ code and compile a new
binary. This adds complication for researchers: A lot of extra
effort is required. Besides, the training job submission program
may not allow running arbitrary binaries.</li>
</ol>
</ol>
<p>This design doc discusses PaddlePaddle’s new distributed training
<p>This design doc discusses PaddlePaddle’s new distributed training architecture that addresses the above mentioned limitations.</p>
architecture that addresses the above limitations.</p>
</div>
</div>
<divclass="section"id="analysis">
<divclass="section"id="analysis">
<spanid="analysis"></span><h2>Analysis<aclass="headerlink"href="#analysis"title="Permalink to this headline">¶</a></h2>
<spanid="analysis"></span><h2>Analysis<aclass="headerlink"href="#analysis"title="Permalink to this headline">¶</a></h2>
<p>We will assume the user writes the trainer program by Python, the same
<p>The assumption is that the user writes the trainer program in either Python or C++.</p>
analysis holds if the trainer program is written in C++.</p>
<divclass="section"id="limitation-1">
<divclass="section"id="limitation-1">
<spanid="limitation-1"></span><h3>Limitation 1<aclass="headerlink"href="#limitation-1"title="Permalink to this headline">¶</a></h3>
<spanid="limitation-1"></span><h3>Limitation 1<aclass="headerlink"href="#limitation-1"title="Permalink to this headline">¶</a></h3>
<p>If we look at the Python code that the user writes, there are two
<p>There are two basic functionalities in the trainer program:</p>
kinds of functionalities:</p>
<olclass="simple">
<ulclass="simple">
<li>The training logic such as loading / saving the model and printing out the logs.</li>
<li>The training logic such as load / save model and print log.</li>
<li>The neural network definition such as the definition of the data layer, the fully connected layer, the cost function and the
<li>The neural network definition such as the definition of the data
layer, the fully connected layer, the cost function and the
optimizer.</li>
optimizer.</li>
</ul>
</ol>
<p>When we training with PaddlePaddle v0.10.0 distributedly, multiple
<p>When we train using PaddlePaddle v0.10.0 in a distributed fashion, multiple instances of the same Python code are run on different nodes, hence both: the
replicated Python instances are running on different nodes: both the
training logic as well as the neural network computation logic, is replicated.</p>
training logic and the neural network computation is replicated.</p>
<p>The tasks that only need to be run once belong to the training logic. Hence if we only replicate the neural network computation part, and do <strong>not</strong>
<p>The tasks that should only run once all belong to the training logic,
replicate the training logic, the limitation mentioned above can be avoided.</p>
if we only replicate the neural network computation, but do <strong>not</strong>
replicate the training logic, the limitation could be solved.</p>
</div>
</div>
<divclass="section"id="limitation-2">
<divclass="section"id="limitation-2">
<spanid="limitation-2"></span><h3>Limitation 2<aclass="headerlink"href="#limitation-2"title="Permalink to this headline">¶</a></h3>
<spanid="limitation-2"></span><h3>Limitation 2<aclass="headerlink"href="#limitation-2"title="Permalink to this headline">¶</a></h3>
<p>Model parallelism means running a single model on multiple nodes by
<p>Model parallelism means that a single model is partitioned into different components and each node runs one of the component separately. This comes at the extra cost of managing the
partitioning the model onto different nodes and managing the
inter-model-shard communication between nodes.</p>
inter-model-shard communications.</p>
<p>PaddlePaddle should ideally be able to modify the neural network computation and figure out the support for model parallelism automatically. However, the
<p>PaddlePaddle should be able to modify the nerual network computation
computation is only specified in Python code which sits outside of PaddlePaddle, hence PaddlePaddle can not support the feature in this setup.</p>
definition to support model parallelism automatically. However, the
<p>Similar to how a compiler uses an intermediate representation (IR) so that the programmer does not need to manually optimize their code for most of the cases, we can have an intermediate representation in PaddlePaddle as well. The compiler optimizes the IR as follows:</p>
computation is only specified in Python code, and PaddlePaddle can not
modify Python code.</p>
<p>Just like compiler uses a intermediate representation (IR) so that
programmer does not need to manually optimize their code in most of
the cases - the compiler will optimize the IR:</p>
<p><imgsrc="src/compiler.png"/></p>
<p><imgsrc="src/compiler.png"/></p>
<p>We can have our own IR too: PaddlePaddle can support model parallel by
<p>PaddlePaddle can support model parallelism by converting the IR so that the user no longer needs to manually perform the computation and operations in the Python component:</p>
converting the IR so the user no longer need to manually do it in
Python:</p>
<p><imgsrc="src/paddle-compile.png"/></p>
<p><imgsrc="src/paddle-compile.png"/></p>
<p>The IR for PaddlePaddle after refactor is called <codeclass="docutils literal"><spanclass="pre">Block</span></code>, it specifies
<p>The IR for PaddlePaddle after refactoring is called a <codeclass="docutils literal"><spanclass="pre">Block</span></code>, it specifies the computation dependency graph and the variables used in the computation.</p>
the computation dependency graph and the variables used in the
computation.</p>
</div>
</div>
<divclass="section"id="limitation-3">
<divclass="section"id="limitation-3">
<spanid="limitation-3"></span><h3>Limitation 3<aclass="headerlink"href="#limitation-3"title="Permalink to this headline">¶</a></h3>
<spanid="limitation-3"></span><h3>Limitation 3<aclass="headerlink"href="#limitation-3"title="Permalink to this headline">¶</a></h3>
<p>The user can not directly specify the parameter update rule for the
<p>The user can not directly specify the parameter update rule for the parameter server in the Python module, since the parameter server does not use the same computation definition as the trainer. Instead, the update rule is baked inside the parameter server. The user can not specify the update rule explicitly.</p>
parameter server because the parameter server does not use the same
<p>This could be fixed by making the parameter server run the same computation definition as the trainer (the user’s Python module). For a detailed explanation, refer to this document -
computation definition as the trainer. Instead, the update rule is
baked in the parameter server. The user can not specify the update
rule in the same way of specifying the trainer computation.</p>
<p>This could be fixed by making the parameter server run the same
computation definition as the trainer. For a detailed explanation,
please
see
<aclass="reference external"href="design/refactor/dist_train.md">Design Doc: Operation Graph Based Parameter Server</a></p>
<aclass="reference external"href="design/refactor/dist_train.md">Design Doc: Operation Graph Based Parameter Server</a></p>
<spanid="distributed-training-architecture"></span><h2>Distributed Training Architecture<aclass="headerlink"href="#distributed-training-architecture"title="Permalink to this headline">¶</a></h2>
<spanid="distributed-training-architecture"></span><h2>Distributed Training Architecture<aclass="headerlink"href="#distributed-training-architecture"title="Permalink to this headline">¶</a></h2>
<p>The new distributed training architecture can address the above
<p>The revamped distributed training architecture can address the above discussed limitations. Below is the illustration of how it does so:</p>
<p>The architecture includes major components: <em>PaddlePaddle Python</em>,
<p>The major components in the architecture are: <em>PaddlePaddle Python</em>, <em>PaddlePaddle converter</em> and <em>PaddlePaddle runtime</em>.</p>
<em>PaddlePaddle converter</em> and <em>PaddlePaddle runtime</em>:</p>
<divclass="section"id="paddlepaddle-python">
<divclass="section"id="paddlepaddle-python">
<spanid="paddlepaddle-python"></span><h3>PaddlePaddle Python<aclass="headerlink"href="#paddlepaddle-python"title="Permalink to this headline">¶</a></h3>
<spanid="paddlepaddle-python"></span><h3>PaddlePaddle Python<aclass="headerlink"href="#paddlepaddle-python"title="Permalink to this headline">¶</a></h3>
<p>PaddlePaddle Python is the Python library that user’s Python trainer
<p>PaddlePaddle Python is the Python library that user’s Python code invokes, to read the data. build the neural network topology, start training, etc.</p>
invoke to build the neural network topology, start training, etc.</p>
<spanclass="nb">input</span><spanclass="o">=</span><spanclass="n">paddle</span><spanclass="o">.</span><spanclass="n">op</span><spanclass="o">.</span><spanclass="n">recordIO</span><spanclass="p">(</span><spanclass="s2">"/home/data/mnist.recordio"</span><spanclass="p">)</span><spanclass="c1"># file stored on the cluster</span>
<spanclass="nb">input</span><spanclass="o">=</span><spanclass="n">paddle</span><spanclass="o">.</span><spanclass="n">op</span><spanclass="o">.</span><spanclass="n">recordIO</span><spanclass="p">(</span><spanclass="s2">"/home/data/mnist.recordio"</span><spanclass="p">)</span><spanclass="c1"># file stored on the cluster</span>
<p>The code above is a typical Python trainer code, the neural network
<p>The above code is what a typical Python trainer code is, the neural network topology is built using the helper functions such as <codeclass="docutils literal"><spanclass="pre">paddle.layer.fc</span></code>. Training is done by calling <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> iteratively.</p>
topology is built using helper functions such as
<codeclass="docutils literal"><spanclass="pre">paddle.layer.fc</span></code>. The training is done by calling <codeclass="docutils literal"><spanclass="pre">session.eval</span></code>
iteratively.</p>
<divclass="section"id="session-eval">
<divclass="section"id="session-eval">
<spanid="session-eval"></span><h4>session.eval<aclass="headerlink"href="#session-eval"title="Permalink to this headline">¶</a></h4>
<spanid="session-eval"></span><h4>session.eval<aclass="headerlink"href="#session-eval"title="Permalink to this headline">¶</a></h4>
<p>As shown in the graph, <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> sends the IR and the evaluation
<p>As shown in the graph, <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> sends the IR and the evaluation inputs or targets to the PaddlePaddle cluster for evaluation.
inputs/targets to the PaddlePaddle cluster for evaluation. The
The targets can be any variable in the computation graph. When the target is say, the <codeclass="docutils literal"><spanclass="pre">optimizer</span></code> variable, the neural network will be optimized once. When the target is the <codeclass="docutils literal"><spanclass="pre">cost</span></code> variable, <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> returns the cost value. Based on what the target is, an appropriate action is taken.</p>
targets can be any variable in the computation graph. When the target
<p>The Python <codeclass="docutils literal"><spanclass="pre">session</span></code> is a wrapper of the C++ <codeclass="docutils literal"><spanclass="pre">Session</span></code> class. For more information about <codeclass="docutils literal"><spanclass="pre">Session</span></code>, refer to this document - <aclass="reference internal"href="session.html"><spanclass="doc">Design Doc: Session</span></a>.</p>
is the <codeclass="docutils literal"><spanclass="pre">optimizer</span></code> variable, the neural network will be optimized
once. When the target is the <codeclass="docutils literal"><spanclass="pre">cost</span></code> variable, <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> returns
the cost value.</p>
<p>The Python <codeclass="docutils literal"><spanclass="pre">session</span></code> is a wrapper of the C++ <codeclass="docutils literal"><spanclass="pre">Session</span></code> class. For more
information about <codeclass="docutils literal"><spanclass="pre">Session</span></code>, please
see <aclass="reference internal"href="session.html"><spanclass="doc">Design Doc: Session</span></a>.</p>
</div>
</div>
</div>
</div>
<divclass="section"id="paddlepaddle-converter">
<divclass="section"id="paddlepaddle-converter">
<spanid="paddlepaddle-converter"></span><h3>PaddlePaddle Converter<aclass="headerlink"href="#paddlepaddle-converter"title="Permalink to this headline">¶</a></h3>
<spanid="paddlepaddle-converter"></span><h3>PaddlePaddle Converter<aclass="headerlink"href="#paddlepaddle-converter"title="Permalink to this headline">¶</a></h3>
<p>PaddlePaddle converter automatically converts the IR in the request
<p>The PaddlePaddle converter automatically converts the IR in the request (IR and evaluation inputs/targets) from PaddlePaddle Python to partitioned IRs and dispatches the new IRs and evaluation inputs/targets to different PaddlePaddle runtimes. Below are the steps that are followed :</p>
(IR and evaluation inputs/targets) from PaddlePaddle Python to new
partitioned IRs and dispatch the new IRs and evaluation inputs/targets
to different PaddlePaddle runtimes. Below are the steps:</p>
<olclass="simple">
<olclass="simple">
<li>Add <codeclass="docutils literal"><spanclass="pre">feed</span></code> OP that feeds the eval inputs, and <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP that
<li>Add a <codeclass="docutils literal"><spanclass="pre">feed</span></code> OP that feeds the eval inputs, and a <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP that fetches the eval targets to the IR.</li>
fetches the eval targets to the IR.</li>
<li>Extract a new computation (sub)graph with the <codeclass="docutils literal"><spanclass="pre">feed</span></code> and <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OPs as the boundary. The runtime does not need to run the OP that is not dependent on the <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP.</li>
<li>Extract a new computation (sub)graph with <codeclass="docutils literal"><spanclass="pre">feed</span></code> and <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP as
<li>Optimize the computation graph.</li>
the boundary. The runtime does not need to run the OP that is not
<li>Place the OPs in the graph onto different devices on different PaddlePaddle runtime according to a placement algorithm and the device constraints specified by the user.</li>
dependent by the <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP.</li>
<li>Partition the graph according to runtime boundaries and add <codeclass="docutils literal"><spanclass="pre">send</span></code> / <codeclass="docutils literal"><spanclass="pre">recv</span></code> OP pair on the runtime boundaries.</li>
<li>Optimizes the computation graph.</li>
<li>Place the OPs in the graph onto different devices on different
PaddlePaddle runtime according to a placement algorithm and device
constraint specified by the user.</li>
<li>Partition the graph according to runtime boundaries and add <codeclass="docutils literal"><spanclass="pre">send</span></code> /
<codeclass="docutils literal"><spanclass="pre">recv</span></code> OP pair on the runtime boundaries.</li>
<li>Dispatch the partitioned graph to different PaddlePaddle runtimes.</li>
<li>Dispatch the partitioned graph to different PaddlePaddle runtimes.</li>
<li>PaddlePaddle runtimes with the <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP reports evaluation
<li>PaddlePaddle runtimes with the <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP reports evaluation results back to the converter, the converter reports the evaluation results back to the PaddlePaddle Python.</li>
results back to the converter, the convert reports the evaluation
results back to the PaddlePaddle Python.</li>
</ol>
</ol>
<p>The output IRs will be cached to optimize the conversion latency.</p>
<p>The output IRs will be cached to optimize the conversion latency.</p>
<divclass="section"id="placement-algorithm">
<divclass="section"id="placement-algorithm">
<spanid="placement-algorithm"></span><h4>Placement Algorithm<aclass="headerlink"href="#placement-algorithm"title="Permalink to this headline">¶</a></h4>
<spanid="placement-algorithm"></span><h4>Placement Algorithm<aclass="headerlink"href="#placement-algorithm"title="Permalink to this headline">¶</a></h4>
<p>Our first implementation will only support “trainer-parameter server”
<p>Our first implementation will only support “trainer-parameter server” placement: the parameters, initializers, and optimizers are all placed on the PaddlePaddle runtimes with the parameter server role. Everything else will be placed on the PaddlePaddle runtimes with the trainer role. This has the same functionality as the “trainer-parameter server” architecture of PaddlePaddle v0.10.0, but is more generic and flexible.</p>
placement: the parameters, initializers, and optimizers are placed on
<p>In the future, a more general placement algorithm should be implemented, which makes placements according to the input IR, and a model of device computation time and device communication time. Model parallelism requires the generic placement algorithm.</p>
the PaddlePaddle runtimes with the parameter server role. And
everything else will be placed on the PaddlePaddle runtimes with the
trainer role. This has the same functionality of our
“trainer-parameter server” architecture of PaddlePaddle v0.10.0, but
is more general and flexible.</p>
<p>In the future, we will implement the general placement algorithm,
which makes placements according to the input IR, and a model of
device computation time and device communication time. Model
parallelism requires the general placement algorithm.</p>
</div>
</div>
</div>
</div>
<divclass="section"id="paddlepaddle-runtime">
<divclass="section"id="paddlepaddle-runtime">
<spanid="paddlepaddle-runtime"></span><h3>PaddlePaddle Runtime<aclass="headerlink"href="#paddlepaddle-runtime"title="Permalink to this headline">¶</a></h3>
<spanid="paddlepaddle-runtime"></span><h3>PaddlePaddle Runtime<aclass="headerlink"href="#paddlepaddle-runtime"title="Permalink to this headline">¶</a></h3>
<p>The PaddlePaddle runtime owns multiple devices (e.g., CPUs, GPUs) and
<p>The PaddlePaddle runtime owns multiple devices (e.g., CPUs, GPUs) and runs the IR. The runtime does not need to do OP placement since it is already done by the converter.</p>
runs the IR. The runtime does not need to do OP placement since it’s
<spanid="local-training-architecture"></span><h3>Local Training Architecture<aclass="headerlink"href="#local-training-architecture"title="Permalink to this headline">¶</a></h3>
<spanid="local-training-architecture"></span><h3>Local Training Architecture<aclass="headerlink"href="#local-training-architecture"title="Permalink to this headline">¶</a></h3>
<p>The local training architecture will be the same as the distributed
<p>The local training architecture will be the same as the distributed training architecture, the difference is that everything runs locally, and there is just one PaddlePaddle runtime:</p>
training architecture, the differences are everything runs locally,
and there is just one PaddlePaddle runtime:</p>
<p><imgsrc="src/local_architecture.png"/></p>
<p><imgsrc="src/local_architecture.png"/></p>
</div>
</div>
<divclass="section"id="training-data">
<divclass="section"id="training-data">
<spanid="training-data"></span><h3>Training Data<aclass="headerlink"href="#training-data"title="Permalink to this headline">¶</a></h3>
<spanid="training-data"></span><h3>Training Data<aclass="headerlink"href="#training-data"title="Permalink to this headline">¶</a></h3>
<p>In PaddlePaddle v0.10.0, training data is typically read
<p>In PaddlePaddle v0.10.0, training data is typically read with a <aclass="reference internal"href="../reader/README.html"><spanclass="doc">data reader</span></a> from Python. This approach is no longer efficient when training in a distributed fashion since the Python process no longer runs on the same node with the trainer processes. The Python reader will need to read from the distributed filesystem (assuming it has the required access) and send to the trainers, doubling the network traffic.</p>
with <aclass="reference internal"href="../reader/README.html"><spanclass="doc">data reader</span></a> from Python. This approach is
<p>When doing distributed training, the user can still use Python data reader: the training data are sent with <codeclass="docutils literal"><spanclass="pre">session.eval</span></code>. However this should be used for debugging purpose only. The users are encouraged to use the read data OPs.</p>
no longer efficient when training distributedly since the Python
process no longer runs on the same node with the trainer processes,
the Python reader will need to read from the distributed filesystem
(assuming it has the access) and send to the trainers, doubling the
network traffic.</p>
<p>When doing distributed training, the user can still use Python data
reader: the training data are sent with <codeclass="docutils literal"><spanclass="pre">session.eval</span></code>. However should
be used for debugging purpose only. The users are encouraged to use
PaddlePaddle v0.10.0 uses the "trainer-parameter server"
PaddlePaddle version 0.10.0 uses the "trainer-parameter server" architecture. We run multiple instances of trainers (where each trainer runs the same model) and parameter servers for distributed training. This architecture serves well, but has few limitations:
architecture. We run multiple replicated instances of trainers (runs
the same code written by the user) and parameter servers for
distributed training. This architecture served us well, but has some
limitations:
1. Need to write special code to handle tasks which should only be run
1. There is a need to write special code that handles tasks which should only be run on a single trainer. E.g., initializing the model, saving the model etc.
by a single trainer. E.g., initializing model and saving model.
2. Model parallelism is hard: need to write if-else branches conditioned
2. Model parallelism is hard: It would need all the if-else branches conditioned on the trainer ID to partition the model onto the trainers, and eventually manually writing out the inter-model-shard communication code to communicate between different trainers.
on the trainer ID to partition model onto each trainer, and manually
write the inter-model-shard communication code.
3. The user can not directly specify the parameter update rule: need
3. The user can not directly specify the parameter update rule: This would need to modify the parameter server code and compile a new binary. This makes things more complicated for researchers: A lot of extra effort is required to make this work. Besides, the training job submission program may not allow running arbitrary binaries.
to modify the parameter server C++ code and compile a new
binary. This adds complication for researchers: A lot of extra
effort is required. Besides, the training job submission program
may not allow running arbitrary binaries.
This design doc discusses PaddlePaddle's new distributed training
This design doc discusses PaddlePaddle's new distributed training architecture that addresses the above mentioned limitations.
architecture that addresses the above limitations.
## Analysis
## Analysis
We will assume the user writes the trainer program by Python, the same
The assumption is that the user writes the trainer program in either Python or C++.
analysis holds if the trainer program is written in C++.
### Limitation 1
### Limitation 1
If we look at the Python code that the user writes, there are two
There are two basic functionalities in the trainer program:
kinds of functionalities:
- The training logic such as load / save model and print log.
1. The training logic such as loading / saving the model and printing out the logs.
- The neural network definition such as the definition of the data
2. The neural network definition such as the definition of the data layer, the fully connected layer, the cost function and the
layer, the fully connected layer, the cost function and the
optimizer.
optimizer.
When we training with PaddlePaddle v0.10.0 distributedly, multiple
When we train using PaddlePaddle v0.10.0 in a distributed fashion, multiple instances of the same Python code are run on different nodes, hence both: the
replicated Python instances are running on different nodes: both the
training logic as well as the neural network computation logic, is replicated.
training logic and the neural network computation is replicated.
The tasks that should only run once all belong to the training logic,
The tasks that only need to be run once belong to the training logic. Hence if we only replicate the neural network computation part, and do **not**
if we only replicate the neural network computation, but do **not**
replicate the training logic, the limitation mentioned above can be avoided.
replicate the training logic, the limitation could be solved.
### Limitation 2
### Limitation 2
Model parallelism means running a single model on multiple nodes by
Model parallelism means that a single model is partitioned into different components and each node runs one of the component separately. This comes at the extra cost of managing the
partitioning the model onto different nodes and managing the
inter-model-shard communication between nodes.
inter-model-shard communications.
PaddlePaddle should be able to modify the nerual network computation
PaddlePaddle should ideally be able to modify the neural network computation and figure out the support for model parallelism automatically. However, the
definition to support model parallelism automatically. However, the
computation is only specified in Python code which sits outside of PaddlePaddle, hence PaddlePaddle can not support the feature in this setup.
computation is only specified in Python code, and PaddlePaddle can not
modify Python code.
Just like compiler uses a intermediate representation (IR) so that
Similar to how a compiler uses an intermediate representation (IR) so that the programmer does not need to manually optimize their code for most of the cases, we can have an intermediate representation in PaddlePaddle as well. The compiler optimizes the IR as follows:
programmer does not need to manually optimize their code in most of
the cases - the compiler will optimize the IR:
<img src="src/compiler.png"/>
<img src="src/compiler.png"/>
We can have our own IR too: PaddlePaddle can support model parallel by
PaddlePaddle can support model parallelism by converting the IR so that the user no longer needs to manually perform the computation and operations in the Python component:
converting the IR so the user no longer need to manually do it in
Python:
<img src="src/paddle-compile.png"/>
<img src="src/paddle-compile.png"/>
The IR for PaddlePaddle after refactor is called `Block`, it specifies
The IR for PaddlePaddle after refactoring is called a `Block`, it specifies the computation dependency graph and the variables used in the computation.
the computation dependency graph and the variables used in the
computation.
### Limitation 3
### Limitation 3
The user can not directly specify the parameter update rule for the
The user can not directly specify the parameter update rule for the parameter server in the Python module, since the parameter server does not use the same computation definition as the trainer. Instead, the update rule is baked inside the parameter server. The user can not specify the update rule explicitly.
parameter server because the parameter server does not use the same
computation definition as the trainer. Instead, the update rule is
baked in the parameter server. The user can not specify the update
rule in the same way of specifying the trainer computation.
This could be fixed by making the parameter server run the same
This could be fixed by making the parameter server run the same computation definition as the trainer (the user's Python module). For a detailed explanation, refer to this document -
computation definition as the trainer. For a detailed explanation,
please
see
[Design Doc: Operation Graph Based Parameter Server](./dist_train.md)
[Design Doc: Operation Graph Based Parameter Server](./dist_train.md)
## Distributed Training Architecture
## Distributed Training Architecture
The new distributed training architecture can address the above
The revamped distributed training architecture can address the above discussed limitations. Below is the illustration of how it does so:
limitations. Below is the illustration:
<img src="src/distributed_architecture.png"/>
<img src="src/distributed_architecture.png"/>
The architecture includes major components: *PaddlePaddle Python*,
The major components in the architecture are: *PaddlePaddle Python*, *PaddlePaddle converter* and *PaddlePaddle runtime*.
*PaddlePaddle converter* and *PaddlePaddle runtime*:
### PaddlePaddle Python
### PaddlePaddle Python
PaddlePaddle Python is the Python library that user's Python trainer
PaddlePaddle Python is the Python library that user's Python code invokes, to read the data. build the neural network topology, start training, etc.
invoke to build the neural network topology, start training, etc.
```Python
```Python
paddle.init()
paddle.init()
...
@@ -117,102 +81,60 @@ for i in range(1000):
...
@@ -117,102 +81,60 @@ for i in range(1000):
print cost_val
print cost_val
```
```
The code above is a typical Python trainer code, the neural network
The above code is what a typical Python trainer code is, the neural network topology is built using the helper functions such as `paddle.layer.fc`. Training is done by calling `session.eval` iteratively.
topology is built using helper functions such as
`paddle.layer.fc`. The training is done by calling `session.eval`
iteratively.
#### session.eval
#### session.eval
As shown in the graph, `session.eval` sends the IR and the evaluation
As shown in the graph, `session.eval` sends the IR and the evaluation inputs or targets to the PaddlePaddle cluster for evaluation.
inputs/targets to the PaddlePaddle cluster for evaluation. The
The targets can be any variable in the computation graph. When the target is say, the `optimizer` variable, the neural network will be optimized once. When the target is the `cost` variable, `session.eval` returns the cost value. Based on what the target is, an appropriate action is taken.
targets can be any variable in the computation graph. When the target
is the `optimizer` variable, the neural network will be optimized
once. When the target is the `cost` variable, `session.eval` returns
the cost value.
The Python `session` is a wrapper of the C++ `Session` class. For more
The Python `session` is a wrapper of the C++ `Session` class. For more information about `Session`, refer to this document - [Design Doc: Session](./session.md).
information about `Session`, please
see [Design Doc: Session](./session.md).
### PaddlePaddle Converter
### PaddlePaddle Converter
PaddlePaddle converter automatically converts the IR in the request
The PaddlePaddle converter automatically converts the IR in the request (IR and evaluation inputs/targets) from PaddlePaddle Python to partitioned IRs and dispatches the new IRs and evaluation inputs/targets to different PaddlePaddle runtimes. Below are the steps that are followed :
(IR and evaluation inputs/targets) from PaddlePaddle Python to new
partitioned IRs and dispatch the new IRs and evaluation inputs/targets
to different PaddlePaddle runtimes. Below are the steps:
1. Add `feed` OP that feeds the eval inputs, and `fetch` OP that
1. Add a `feed` OP that feeds the eval inputs, and a `fetch` OP that fetches the eval targets to the IR.
fetches the eval targets to the IR.
1. Extract a new computation (sub)graph with `feed` and `fetch` OP as
2. Extract a new computation (sub)graph with the `feed` and `fetch` OPs as the boundary. The runtime does not need to run the OP that is not dependent on the `fetch` OP.
the boundary. The runtime does not need to run the OP that is not
dependent by the `fetch` OP.
1. Optimizes the computation graph.
3. Optimize the computation graph.
1. Place the OPs in the graph onto different devices on different
4. Place the OPs in the graph onto different devices on different PaddlePaddle runtime according to a placement algorithm and the device constraints specified by the user.
PaddlePaddle runtime according to a placement algorithm and device
constraint specified by the user.
1. Partition the graph according to runtime boundaries and add `send` /
5. Partition the graph according to runtime boundaries and add `send` / `recv` OP pair on the runtime boundaries.
`recv` OP pair on the runtime boundaries.
1. Dispatch the partitioned graph to different PaddlePaddle runtimes.
6. Dispatch the partitioned graph to different PaddlePaddle runtimes.
7. PaddlePaddle runtimes with the `fetch` OP reports evaluation results back to the converter, the converter reports the evaluation results back to the PaddlePaddle Python.
1. PaddlePaddle runtimes with the `fetch` OP reports evaluation
results back to the converter, the convert reports the evaluation
results back to the PaddlePaddle Python.
The output IRs will be cached to optimize the conversion latency.
The output IRs will be cached to optimize the conversion latency.
#### Placement Algorithm
#### Placement Algorithm
Our first implementation will only support "trainer-parameter server"
Our first implementation will only support "trainer-parameter server" placement: the parameters, initializers, and optimizers are all placed on the PaddlePaddle runtimes with the parameter server role. Everything else will be placed on the PaddlePaddle runtimes with the trainer role. This has the same functionality as the "trainer-parameter server" architecture of PaddlePaddle v0.10.0, but is more generic and flexible.
placement: the parameters, initializers, and optimizers are placed on
the PaddlePaddle runtimes with the parameter server role. And
everything else will be placed on the PaddlePaddle runtimes with the
trainer role. This has the same functionality of our
"trainer-parameter server" architecture of PaddlePaddle v0.10.0, but
is more general and flexible.
In the future, we will implement the general placement algorithm,
In the future, a more general placement algorithm should be implemented, which makes placements according to the input IR, and a model of device computation time and device communication time. Model parallelism requires the generic placement algorithm.
which makes placements according to the input IR, and a model of
device computation time and device communication time. Model
parallelism requires the general placement algorithm.
### PaddlePaddle Runtime
### PaddlePaddle Runtime
The PaddlePaddle runtime owns multiple devices (e.g., CPUs, GPUs) and
The PaddlePaddle runtime owns multiple devices (e.g., CPUs, GPUs) and runs the IR. The runtime does not need to do OP placement since it is already done by the converter.
runs the IR. The runtime does not need to do OP placement since it's
already done by the converter.
### Local Training Architecture
### Local Training Architecture
The local training architecture will be the same as the distributed
The local training architecture will be the same as the distributed training architecture, the difference is that everything runs locally, and there is just one PaddlePaddle runtime:
training architecture, the differences are everything runs locally,
and there is just one PaddlePaddle runtime:
<img src="src/local_architecture.png"/>
<img src="src/local_architecture.png"/>
### Training Data
### Training Data
In PaddlePaddle v0.10.0, training data is typically read
In PaddlePaddle v0.10.0, training data is typically read with a [data reader](../reader/README.md) from Python. This approach is no longer efficient when training in a distributed fashion since the Python process no longer runs on the same node with the trainer processes. The Python reader will need to read from the distributed filesystem (assuming it has the required access) and send to the trainers, doubling the network traffic.
with [data reader](../reader/README.md) from Python. This approach is
no longer efficient when training distributedly since the Python
When doing distributed training, the user can still use Python data reader: the training data are sent with `session.eval`. However this should be used for debugging purpose only. The users are encouraged to use the read data OPs.
process no longer runs on the same node with the trainer processes,
the Python reader will need to read from the distributed filesystem
(assuming it has the access) and send to the trainers, doubling the
network traffic.
When doing distributed training, the user can still use Python data
reader: the training data are sent with `session.eval`. However should
be used for debugging purpose only. The users are encouraged to use
<spanid="design-doc-distributed-training-architecture"></span><h1>Design Doc: Distributed Training Architecture<aclass="headerlink"href="#design-doc-distributed-training-architecture"title="永久链接至标题">¶</a></h1>
<spanid="design-doc-distributed-training-architecture"></span><h1>Design Doc: Distributed Training Architecture<aclass="headerlink"href="#design-doc-distributed-training-architecture"title="永久链接至标题">¶</a></h1>
<p>PaddlePaddle v0.10.0 uses the “trainer-parameter server”
<p>PaddlePaddle version 0.10.0 uses the “trainer-parameter server” architecture. We run multiple instances of trainers (where each trainer runs the same model) and parameter servers for distributed training. This architecture serves well, but has few limitations:</p>
architecture. We run multiple replicated instances of trainers (runs
the same code written by the user) and parameter servers for
distributed training. This architecture served us well, but has some
limitations:</p>
<olclass="simple">
<olclass="simple">
<li>Need to write special code to handle tasks which should only be run
<li>There is a need to write special code that handles tasks which should only be run on a single trainer. E.g., initializing the model, saving the model etc.</li>
by a single trainer. E.g., initializing model and saving model.</li>
<li>Model parallelism is hard: It would need all the if-else branches conditioned on the trainer ID to partition the model onto the trainers, and eventually manually writing out the inter-model-shard communication code to communicate between different trainers.</li>
<li>Model parallelism is hard: need to write if-else branches conditioned
<li>The user can not directly specify the parameter update rule: This would need to modify the parameter server code and compile a new binary. This makes things more complicated for researchers: A lot of extra effort is required to make this work. Besides, the training job submission program may not allow running arbitrary binaries.</li>
on the trainer ID to partition model onto each trainer, and manually
write the inter-model-shard communication code.</li>
<li>The user can not directly specify the parameter update rule: need
to modify the parameter server C++ code and compile a new
binary. This adds complication for researchers: A lot of extra
effort is required. Besides, the training job submission program
may not allow running arbitrary binaries.</li>
</ol>
</ol>
<p>This design doc discusses PaddlePaddle’s new distributed training
<p>This design doc discusses PaddlePaddle’s new distributed training architecture that addresses the above mentioned limitations.</p>
architecture that addresses the above limitations.</p>
<p>If we look at the Python code that the user writes, there are two
<p>There are two basic functionalities in the trainer program:</p>
kinds of functionalities:</p>
<olclass="simple">
<ulclass="simple">
<li>The training logic such as loading / saving the model and printing out the logs.</li>
<li>The training logic such as load / save model and print log.</li>
<li>The neural network definition such as the definition of the data layer, the fully connected layer, the cost function and the
<li>The neural network definition such as the definition of the data
layer, the fully connected layer, the cost function and the
optimizer.</li>
optimizer.</li>
</ul>
</ol>
<p>When we training with PaddlePaddle v0.10.0 distributedly, multiple
<p>When we train using PaddlePaddle v0.10.0 in a distributed fashion, multiple instances of the same Python code are run on different nodes, hence both: the
replicated Python instances are running on different nodes: both the
training logic as well as the neural network computation logic, is replicated.</p>
training logic and the neural network computation is replicated.</p>
<p>The tasks that only need to be run once belong to the training logic. Hence if we only replicate the neural network computation part, and do <strong>not</strong>
<p>The tasks that should only run once all belong to the training logic,
replicate the training logic, the limitation mentioned above can be avoided.</p>
if we only replicate the neural network computation, but do <strong>not</strong>
replicate the training logic, the limitation could be solved.</p>
<p>Model parallelism means running a single model on multiple nodes by
<p>Model parallelism means that a single model is partitioned into different components and each node runs one of the component separately. This comes at the extra cost of managing the
partitioning the model onto different nodes and managing the
inter-model-shard communication between nodes.</p>
inter-model-shard communications.</p>
<p>PaddlePaddle should ideally be able to modify the neural network computation and figure out the support for model parallelism automatically. However, the
<p>PaddlePaddle should be able to modify the nerual network computation
computation is only specified in Python code which sits outside of PaddlePaddle, hence PaddlePaddle can not support the feature in this setup.</p>
definition to support model parallelism automatically. However, the
<p>Similar to how a compiler uses an intermediate representation (IR) so that the programmer does not need to manually optimize their code for most of the cases, we can have an intermediate representation in PaddlePaddle as well. The compiler optimizes the IR as follows:</p>
computation is only specified in Python code, and PaddlePaddle can not
modify Python code.</p>
<p>Just like compiler uses a intermediate representation (IR) so that
programmer does not need to manually optimize their code in most of
the cases - the compiler will optimize the IR:</p>
<p><imgsrc="src/compiler.png"/></p>
<p><imgsrc="src/compiler.png"/></p>
<p>We can have our own IR too: PaddlePaddle can support model parallel by
<p>PaddlePaddle can support model parallelism by converting the IR so that the user no longer needs to manually perform the computation and operations in the Python component:</p>
converting the IR so the user no longer need to manually do it in
Python:</p>
<p><imgsrc="src/paddle-compile.png"/></p>
<p><imgsrc="src/paddle-compile.png"/></p>
<p>The IR for PaddlePaddle after refactor is called <codeclass="docutils literal"><spanclass="pre">Block</span></code>, it specifies
<p>The IR for PaddlePaddle after refactoring is called a <codeclass="docutils literal"><spanclass="pre">Block</span></code>, it specifies the computation dependency graph and the variables used in the computation.</p>
the computation dependency graph and the variables used in the
<p>The user can not directly specify the parameter update rule for the
<p>The user can not directly specify the parameter update rule for the parameter server in the Python module, since the parameter server does not use the same computation definition as the trainer. Instead, the update rule is baked inside the parameter server. The user can not specify the update rule explicitly.</p>
parameter server because the parameter server does not use the same
<p>This could be fixed by making the parameter server run the same computation definition as the trainer (the user’s Python module). For a detailed explanation, refer to this document -
computation definition as the trainer. Instead, the update rule is
baked in the parameter server. The user can not specify the update
rule in the same way of specifying the trainer computation.</p>
<p>This could be fixed by making the parameter server run the same
computation definition as the trainer. For a detailed explanation,
please
see
<aclass="reference external"href="design/refactor/dist_train.md">Design Doc: Operation Graph Based Parameter Server</a></p>
<aclass="reference external"href="design/refactor/dist_train.md">Design Doc: Operation Graph Based Parameter Server</a></p>
<spanid="distributed-training-architecture"></span><h2>Distributed Training Architecture<aclass="headerlink"href="#distributed-training-architecture"title="永久链接至标题">¶</a></h2>
<spanid="distributed-training-architecture"></span><h2>Distributed Training Architecture<aclass="headerlink"href="#distributed-training-architecture"title="永久链接至标题">¶</a></h2>
<p>The new distributed training architecture can address the above
<p>The revamped distributed training architecture can address the above discussed limitations. Below is the illustration of how it does so:</p>
<p>PaddlePaddle Python is the Python library that user’s Python trainer
<p>PaddlePaddle Python is the Python library that user’s Python code invokes, to read the data. build the neural network topology, start training, etc.</p>
invoke to build the neural network topology, start training, etc.</p>
<spanclass="nb">input</span><spanclass="o">=</span><spanclass="n">paddle</span><spanclass="o">.</span><spanclass="n">op</span><spanclass="o">.</span><spanclass="n">recordIO</span><spanclass="p">(</span><spanclass="s2">"/home/data/mnist.recordio"</span><spanclass="p">)</span><spanclass="c1"># file stored on the cluster</span>
<spanclass="nb">input</span><spanclass="o">=</span><spanclass="n">paddle</span><spanclass="o">.</span><spanclass="n">op</span><spanclass="o">.</span><spanclass="n">recordIO</span><spanclass="p">(</span><spanclass="s2">"/home/data/mnist.recordio"</span><spanclass="p">)</span><spanclass="c1"># file stored on the cluster</span>
<p>The code above is a typical Python trainer code, the neural network
<p>The above code is what a typical Python trainer code is, the neural network topology is built using the helper functions such as <codeclass="docutils literal"><spanclass="pre">paddle.layer.fc</span></code>. Training is done by calling <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> iteratively.</p>
topology is built using helper functions such as
<codeclass="docutils literal"><spanclass="pre">paddle.layer.fc</span></code>. The training is done by calling <codeclass="docutils literal"><spanclass="pre">session.eval</span></code>
<p>As shown in the graph, <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> sends the IR and the evaluation
<p>As shown in the graph, <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> sends the IR and the evaluation inputs or targets to the PaddlePaddle cluster for evaluation.
inputs/targets to the PaddlePaddle cluster for evaluation. The
The targets can be any variable in the computation graph. When the target is say, the <codeclass="docutils literal"><spanclass="pre">optimizer</span></code> variable, the neural network will be optimized once. When the target is the <codeclass="docutils literal"><spanclass="pre">cost</span></code> variable, <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> returns the cost value. Based on what the target is, an appropriate action is taken.</p>
targets can be any variable in the computation graph. When the target
<p>The Python <codeclass="docutils literal"><spanclass="pre">session</span></code> is a wrapper of the C++ <codeclass="docutils literal"><spanclass="pre">Session</span></code> class. For more information about <codeclass="docutils literal"><spanclass="pre">Session</span></code>, refer to this document - <aclass="reference internal"href="session.html"><spanclass="doc">Design Doc: Session</span></a>.</p>
is the <codeclass="docutils literal"><spanclass="pre">optimizer</span></code> variable, the neural network will be optimized
once. When the target is the <codeclass="docutils literal"><spanclass="pre">cost</span></code> variable, <codeclass="docutils literal"><spanclass="pre">session.eval</span></code> returns
the cost value.</p>
<p>The Python <codeclass="docutils literal"><spanclass="pre">session</span></code> is a wrapper of the C++ <codeclass="docutils literal"><spanclass="pre">Session</span></code> class. For more
information about <codeclass="docutils literal"><spanclass="pre">Session</span></code>, please
see <aclass="reference internal"href="session.html"><spanclass="doc">Design Doc: Session</span></a>.</p>
<p>PaddlePaddle converter automatically converts the IR in the request
<p>The PaddlePaddle converter automatically converts the IR in the request (IR and evaluation inputs/targets) from PaddlePaddle Python to partitioned IRs and dispatches the new IRs and evaluation inputs/targets to different PaddlePaddle runtimes. Below are the steps that are followed :</p>
(IR and evaluation inputs/targets) from PaddlePaddle Python to new
partitioned IRs and dispatch the new IRs and evaluation inputs/targets
to different PaddlePaddle runtimes. Below are the steps:</p>
<olclass="simple">
<olclass="simple">
<li>Add <codeclass="docutils literal"><spanclass="pre">feed</span></code> OP that feeds the eval inputs, and <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP that
<li>Add a <codeclass="docutils literal"><spanclass="pre">feed</span></code> OP that feeds the eval inputs, and a <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP that fetches the eval targets to the IR.</li>
fetches the eval targets to the IR.</li>
<li>Extract a new computation (sub)graph with the <codeclass="docutils literal"><spanclass="pre">feed</span></code> and <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OPs as the boundary. The runtime does not need to run the OP that is not dependent on the <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP.</li>
<li>Extract a new computation (sub)graph with <codeclass="docutils literal"><spanclass="pre">feed</span></code> and <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP as
<li>Optimize the computation graph.</li>
the boundary. The runtime does not need to run the OP that is not
<li>Place the OPs in the graph onto different devices on different PaddlePaddle runtime according to a placement algorithm and the device constraints specified by the user.</li>
dependent by the <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP.</li>
<li>Partition the graph according to runtime boundaries and add <codeclass="docutils literal"><spanclass="pre">send</span></code> / <codeclass="docutils literal"><spanclass="pre">recv</span></code> OP pair on the runtime boundaries.</li>
<li>Optimizes the computation graph.</li>
<li>Place the OPs in the graph onto different devices on different
PaddlePaddle runtime according to a placement algorithm and device
constraint specified by the user.</li>
<li>Partition the graph according to runtime boundaries and add <codeclass="docutils literal"><spanclass="pre">send</span></code> /
<codeclass="docutils literal"><spanclass="pre">recv</span></code> OP pair on the runtime boundaries.</li>
<li>Dispatch the partitioned graph to different PaddlePaddle runtimes.</li>
<li>Dispatch the partitioned graph to different PaddlePaddle runtimes.</li>
<li>PaddlePaddle runtimes with the <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP reports evaluation
<li>PaddlePaddle runtimes with the <codeclass="docutils literal"><spanclass="pre">fetch</span></code> OP reports evaluation results back to the converter, the converter reports the evaluation results back to the PaddlePaddle Python.</li>
results back to the converter, the convert reports the evaluation
results back to the PaddlePaddle Python.</li>
</ol>
</ol>
<p>The output IRs will be cached to optimize the conversion latency.</p>
<p>The output IRs will be cached to optimize the conversion latency.</p>
<p>Our first implementation will only support “trainer-parameter server”
<p>Our first implementation will only support “trainer-parameter server” placement: the parameters, initializers, and optimizers are all placed on the PaddlePaddle runtimes with the parameter server role. Everything else will be placed on the PaddlePaddle runtimes with the trainer role. This has the same functionality as the “trainer-parameter server” architecture of PaddlePaddle v0.10.0, but is more generic and flexible.</p>
placement: the parameters, initializers, and optimizers are placed on
<p>In the future, a more general placement algorithm should be implemented, which makes placements according to the input IR, and a model of device computation time and device communication time. Model parallelism requires the generic placement algorithm.</p>
the PaddlePaddle runtimes with the parameter server role. And
everything else will be placed on the PaddlePaddle runtimes with the
trainer role. This has the same functionality of our
“trainer-parameter server” architecture of PaddlePaddle v0.10.0, but
is more general and flexible.</p>
<p>In the future, we will implement the general placement algorithm,
which makes placements according to the input IR, and a model of
device computation time and device communication time. Model
parallelism requires the general placement algorithm.</p>
<p>The PaddlePaddle runtime owns multiple devices (e.g., CPUs, GPUs) and
<p>The PaddlePaddle runtime owns multiple devices (e.g., CPUs, GPUs) and runs the IR. The runtime does not need to do OP placement since it is already done by the converter.</p>
runs the IR. The runtime does not need to do OP placement since it’s
<spanid="local-training-architecture"></span><h3>Local Training Architecture<aclass="headerlink"href="#local-training-architecture"title="永久链接至标题">¶</a></h3>
<spanid="local-training-architecture"></span><h3>Local Training Architecture<aclass="headerlink"href="#local-training-architecture"title="永久链接至标题">¶</a></h3>
<p>The local training architecture will be the same as the distributed
<p>The local training architecture will be the same as the distributed training architecture, the difference is that everything runs locally, and there is just one PaddlePaddle runtime:</p>
training architecture, the differences are everything runs locally,
<p>In PaddlePaddle v0.10.0, training data is typically read
<p>In PaddlePaddle v0.10.0, training data is typically read with a <aclass="reference internal"href="../reader/README.html"><spanclass="doc">data reader</span></a> from Python. This approach is no longer efficient when training in a distributed fashion since the Python process no longer runs on the same node with the trainer processes. The Python reader will need to read from the distributed filesystem (assuming it has the required access) and send to the trainers, doubling the network traffic.</p>
with <aclass="reference internal"href="../reader/README.html"><spanclass="doc">data reader</span></a> from Python. This approach is
<p>When doing distributed training, the user can still use Python data reader: the training data are sent with <codeclass="docutils literal"><spanclass="pre">session.eval</span></code>. However this should be used for debugging purpose only. The users are encouraged to use the read data OPs.</p>
no longer efficient when training distributedly since the Python
process no longer runs on the same node with the trainer processes,
the Python reader will need to read from the distributed filesystem
(assuming it has the access) and send to the trainers, doubling the
network traffic.</p>
<p>When doing distributed training, the user can still use Python data
reader: the training data are sent with <codeclass="docutils literal"><spanclass="pre">session.eval</span></code>. However should
be used for debugging purpose only. The users are encouraged to use