DataParallelCommunicator enables to train your neural network using multiple devices. It is normally used for gradients exchange in data parallel distributed training. Basically, there are two types of distributed trainings in Neural Network literature: Data Parallel and Model Parallel. Here we only focus on the former, Data Parallel Training. Data Parallel Distributed Training is based on the very simple equation used for the optimization of a neural network called (Mini-Batch) Stochastic Gradient Descent.
In the optimization process, the objective one tries to minimize is
$$ f(\mathbf{w}; X) = \frac{1}{B \times N} \sum_{i=1}^{B \times N} \ell(\mathbf{w}, \mathbf{x}_i), $$where $f$ is a neural network, $B \times N$ is the batch size, $\ell$ is a loss function for each data point $\mathbf{x} \in X$, and $\mathbf{w}$ is the trainable parameter of the neural network.
When taking the derivative of this objective, one gets,
$$ \nabla_{\mathbf{w}} f(\mathbf{w}; X) = \frac{1}{B \times N} \sum_{i=1}^{B \times N} \nabla_{\mathbf{w}} \ell (\mathbf{w}, \mathbf{x}_i). $$Since the derivative has linearity, one can change the objective to the sum of summations each of which is the sum of derivatives over $B$ data points.
$$ \nabla_{\mathbf{w}} f(\mathbf{w}; X) = \frac{1}{N} \left( \frac{1}{B} \sum_{i=1}^{B} \nabla_{\mathbf{w}} \ell (\mathbf{w}, \mathbf{x}_i) \ + \frac{1}{B} \sum_{i=B+1}^{B \times 2} \nabla_{\mathbf{w}} \ell (\mathbf{w}, \mathbf{x}_i) \ + \ldots \ + \frac{1}{B} \sum_{i=B \times (N-1) + 1}^{B \times N} \nabla_{\mathbf{w}} \ell (\mathbf{w}, \mathbf{x}_i) \right) $$In data parallel distributed training, the following steps are performed according to the above equation,
That is the underlying foundation of Data Parallel Distributed Training.
This tutorial shows the usage of Multi Process Data Parallel Communicator for data parallel distributed training with a very simple example.
This tutorial depends on IPython Cluster, thus when you want to run the following excerpts of the scripts on Jupyter Notebook, follow this to enable mpiexec/mpirun mode, then launch a corresponding Ipython Cluster on Ipython Clusters tab.
This code is only needed for this tutorial via Jupyter Notebook.
In [1]:
import ipyparallel as ipp
rc = ipp.Client(profile='mpi')
In [2]:
%%px
import os
import time
import nnabla as nn
import nnabla.communicators as C
from nnabla.ext_utils import get_extension_context
import nnabla.functions as F
from nnabla.initializer import (
calc_uniform_lim_glorot,
UniformInitializer)
import nnabla.parametric_functions as PF
import nnabla.solvers as S
import numpy as np
In [3]:
%%px
extension_module = "cudnn"
ctx = get_extension_context(extension_module)
comm = C.MultiProcessCommunicator(ctx)
comm.init()
n_devices = comm.size
mpi_rank = comm.rank
device_id = mpi_rank
ctx = get_extension_context(extension_module, device_id=device_id)
Check different ranks are assigned to different devices
In [4]:
%%px
print("n_devices={}".format(n_devices))
print("mpi_rank={}".format(mpi_rank))
In [5]:
%%px
# Data points setting
n_class = 2
b, c, h, w = 4, 1, 32, 32
# Data points
x_data = np.random.rand(b, c, h, w)
y_data = np.random.choice(n_class, b).reshape((b, 1))
x = nn.Variable(x_data.shape)
y = nn.Variable(y_data.shape)
x.d = x_data
y.d = y_data
# Network setting
C = 1
kernel = (3, 3)
pad = (1, 1)
stride = (1, 1)
In [6]:
%%px
rng = np.random.RandomState(0)
w_init = UniformInitializer(
calc_uniform_lim_glorot(C, C/2, kernel=(1, 1)),
rng=rng)
In [7]:
%%px
# Network
with nn.context_scope(ctx):
h = PF.convolution(x, C, kernel, pad, stride, w_init=w_init)
pred = PF.affine(h, n_class, w_init=w_init)
loss = F.mean(F.softmax_cross_entropy(pred, y))
Important notice here is that w_init
is passed to parametric functions
to let the network on each GPU start from the same values of trainable parameters in the
optimization process.
In [8]:
%%px
# Solver and add parameters
solver = S.Adam()
solver.set_parameters(nn.get_parameters())
Recall the basic usage of nnabla
API for training a neural network,
it is
In use of C.MultiProcessCommunicator
, these steps are performed in
different GPUs, and the only difference from these steps is comm.all_reduce()
.
Thus, in case of C.MultiProcessCommunicator
training steps are
as follows,
First, forward, zero_grad, and backward,
In [9]:
%%px
# Training steps
loss.forward()
solver.zero_grad()
loss.backward()
Check gradients of weights once,
In [10]:
%%px
for n, v in nn.get_parameters().items():
print(n, v.g)
You can see the different values on each device, then call all_reduce
,
In [11]:
%%px
comm.all_reduce([x.grad for x in nn.get_parameters().values()], division=True)
Commonly, all_reduce
only means the sum; however, comm.all_reduce
addresses
both cases: summation and summation division.
Again, check gradients of weights,
In [12]:
%%px
for n, v in nn.get_parameters().items():
print(n, v.g)
You can see the same values over the devices because of all_reduce
.
Update weights,
In [13]:
%%px
solver.update()
This concludes the usage of C.MultiProcessCommunicator
for
Data Parallel Distributed Training.
Now you should have an understanding of how to use C.MultiProcessCommunicator
,
go to the cifar10 example,
for more details.