In this article, we'll explain how to config and run distributed training jobs with PaddlePaddle Fluid in a bare metal cluster.
## Preparations
### Get your cluster ready
Prepare your computer nodes in the cluster. Nodes in this cluster can be of any specification that runs PaddlePaddle, and with a unique IP address assigned to it. Make sure they can communicate with each other.
### Have PaddlePaddle installed
PaddlePaddle must be installed on all nodes. If you have GPU cards on your nodes, be sure to properly install drivers and CUDA libraries.
PaddlePaddle build and installation guide can be found from [here](http://www.paddlepaddle.org/docs/develop/documentation/en/getstarted/build_and_install/index_en.html).
### Update training script
#### Non-cluster training script
Let's take [Deep Learning 101](http://www.paddlepaddle.org/docs/develop/book/01.fit_a_line/index.html)'s first chapter: "fit a line" as an example.
This demo's non-cluster version with fluid API is as follows:
``` python
import paddle.v2 as paddle
import paddle.v2.fluid as fluid
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
exit(0) # if avg cost less than 10.0, we think our code is good.
exit(1)
```
We created a simple fully connected neural networks training program and handed it to the fluid executor to run for 100 passes.
Now let's try to convert it to a distributed version to run in a cluster.
#### Introducing parameter server
As you see from the non-cluster version of training script, there is only one role in it: the trainer, who does the computing as well as holding parameters. In cluster training, since multi-trainers are working on the same task, they need one centralized place to hold and distribute parameters. This centralized place is called the Parameter Server in PaddlePaddle.
![parameter server architect](src/trainer.png)
Parameter Server in fluid does not only hold parameters but is also assigned with a part of the program. Trainers communicate with parameter servers via send/receive OPs. For more tech detail, please refer to this [document](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/dist_refactor/distributed_architecture.md).
Now we need to create program for both trainers and parameter servers, the question is how?
#### Slice the program
Fluid provides a tool called "Distribute Transpiler" to automatically convert the non-cluster program into cluster program.
The idea behind this tool is to find optimize OPs and gradient parameters, slice the program into 2 pieces and connect them with send/receive OP.
Optimize OPs and gradient parameters can be found from the return values of optimizer's minimize function.
To put them together:
``` python
... #define the program, cost, and create sgd optimizer
optimize_ops, params_grads = sgd_optimizer.minimize(avg_cost) #get optimize OPs and gradient parameters
t = fluid.DistributeTranspiler() # create transpiler instance
# slice the program into 2 pieces with optimizer_ops and gradient parameters list, as well as pserver_endpoints, which is a comma separated list of [IP:PORT] and number of trainers
Please find the complete demo from [here](https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/fluid/tests/book_distribute/notest_dist_fit_a_line.py). In parameter server node run this in the command line:
<liclass="toctree-l2"><aclass="reference internal"href="../../../getstarted/build_and_install/index_en.html">Install and Build</a><ul>
<liclass="toctree-l3"><aclass="reference internal"href="../../../getstarted/build_and_install/pip_install_en.html">Install Using pip</a></li>
<liclass="toctree-l3"><aclass="reference internal"href="../../../getstarted/build_and_install/docker_install_en.html">Run in Docker Containers</a></li>
<liclass="toctree-l3"><aclass="reference internal"href="../../dev/build_en.html">Build using Docker</a></li>
<liclass="toctree-l3"><aclass="reference internal"href="../../../getstarted/build_and_install/build_from_source_en.html">Build from Sources</a></li>
<spanid="fluid-distributed-training"></span><h1>Fluid Distributed Training<aclass="headerlink"href="#fluid-distributed-training"title="Permalink to this headline">¶</a></h1>
<divclass="section"id="introduction">
<spanid="introduction"></span><h2>Introduction<aclass="headerlink"href="#introduction"title="Permalink to this headline">¶</a></h2>
<p>In this article, we’ll explain how to config and run distributed training jobs with PaddlePaddle Fluid in a bare metal cluster.</p>
</div>
<divclass="section"id="preparations">
<spanid="preparations"></span><h2>Preparations<aclass="headerlink"href="#preparations"title="Permalink to this headline">¶</a></h2>
<divclass="section"id="get-your-cluster-ready">
<spanid="get-your-cluster-ready"></span><h3>Get your cluster ready<aclass="headerlink"href="#get-your-cluster-ready"title="Permalink to this headline">¶</a></h3>
<p>Prepare your computer nodes in the cluster. Nodes in this cluster can be of any specification that runs PaddlePaddle, and with a unique IP address assigned to it. Make sure they can communicate with each other.</p>
<spanid="have-paddlepaddle-installed"></span><h3>Have PaddlePaddle installed<aclass="headerlink"href="#have-paddlepaddle-installed"title="Permalink to this headline">¶</a></h3>
<p>PaddlePaddle must be installed on all nodes. If you have GPU cards on your nodes, be sure to properly install drivers and CUDA libraries.</p>
<p>PaddlePaddle build and installation guide can be found from <aclass="reference external"href="http://www.paddlepaddle.org/docs/develop/documentation/en/getstarted/build_and_install/index_en.html">here</a>.</p>
</div>
<divclass="section"id="update-training-script">
<spanid="update-training-script"></span><h3>Update training script<aclass="headerlink"href="#update-training-script"title="Permalink to this headline">¶</a></h3>
<spanid="non-cluster-training-script"></span><h4>Non-cluster training script<aclass="headerlink"href="#non-cluster-training-script"title="Permalink to this headline">¶</a></h4>
<p>Let’s take <aclass="reference external"href="http://www.paddlepaddle.org/docs/develop/book/01.fit_a_line/index.html">Deep Learning 101</a>‘s first chapter: “fit a line” as an example.</p>
<p>This demo’s non-cluster version with fluid API is as follows:</p>
<spanclass="nb">exit</span><spanclass="p">(</span><spanclass="mi">0</span><spanclass="p">)</span><spanclass="c1"># if avg cost less than 10.0, we think our code is good.</span>
<spanid="introducing-parameter-server"></span><h4>Introducing parameter server<aclass="headerlink"href="#introducing-parameter-server"title="Permalink to this headline">¶</a></h4>
<p>As you see from the non-cluster version of training script, there is only one role in it: the trainer, who does the computing as well as holding parameters. In cluster training, since multi-trainers are working on the same task, they need one centralized place to hold and distribute parameters. This centralized place is called the Parameter Server in PaddlePaddle.</p>
<p><imgalt="parameter server architect"src="../../../_images/trainer.png"/></p>
<p>Parameter Server in fluid does not only hold parameters but is also assigned with a part of the program. Trainers communicate with parameter servers via send/receive OPs. For more tech detail, please refer to this <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/dist_refactor/distributed_architecture.md">document</a>.</p>
<p>Now we need to create program for both trainers and parameter servers, the question is how?</p>
</div>
<divclass="section"id="slice-the-program">
<spanid="slice-the-program"></span><h4>Slice the program<aclass="headerlink"href="#slice-the-program"title="Permalink to this headline">¶</a></h4>
<p>Fluid provides a tool called “Distribute Transpiler” to automatically convert the non-cluster program into cluster program.</p>
<p>The idea behind this tool is to find optimize OPs and gradient parameters, slice the program into 2 pieces and connect them with send/receive OP.</p>
<p>Optimize OPs and gradient parameters can be found from the return values of optimizer’s minimize function.</p>
<p>To put them together:</p>
<divclass="highlight-python"><divclass="highlight"><pre><span></span><spanclass="o">...</span><spanclass="c1">#define the program, cost, and create sgd optimizer</span>
<spanclass="n">optimize_ops</span><spanclass="p">,</span><spanclass="n">params_grads</span><spanclass="o">=</span><spanclass="n">sgd_optimizer</span><spanclass="o">.</span><spanclass="n">minimize</span><spanclass="p">(</span><spanclass="n">avg_cost</span><spanclass="p">)</span><spanclass="c1">#get optimize OPs and gradient parameters</span>
<spanclass="c1"># slice the program into 2 pieces with optimizer_ops and gradient parameters list, as well as pserver_endpoints, which is a comma separated list of [IP:PORT] and number of trainers</span>
<spanid="e2e-demo"></span><h3>E2E demo<aclass="headerlink"href="#e2e-demo"title="Permalink to this headline">¶</a></h3>
<p>Please find the complete demo from <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/fluid/tests/book_distribute/notest_dist_fit_a_line.py">here</a>. In parameter server node run this in the command line:</p>
<p><em>please note we assume that your parameter server runs at 192.168.1.2:6174</em></p>
<p>Wait until the prompt <codeclass="docutils literal"><spanclass="pre">Server</span><spanclass="pre">listening</span><spanclass="pre">on</span><spanclass="pre">192.168.1.2:6174</span></code></p>
<p><em>the reason you need to run this command twice in 2 nodes is: in the script we set the trainer count to be 2. You can change this setting on line 50</em></p>
<p>Now you have 2 trainers and 1 parameter server up and running.</p>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.