# 11.4. Multi-GPU Computation Implementation from Scratch¶

In this section, we will show how to use multiple GPU for computation. For example, we can train the same model using multiple GPUs. As you might expect, running the programs in this section requires at least two GPUs. In fact, installing multiple GPUs on a single machine is common because there are usually multiple PCIe slots on the motherboard. If the NVIDIA driver is properly installed, we can use the nvidia-smi command to view all GPUs on the current computer.

!nvidia-smi

Wed Oct  9 05:21:18 2019
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.67       Driver Version: 418.67       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla V100-SXM2...  Off  | 00000000:00:1B.0 Off |                    0 |
| N/A   58C    P0    58W / 300W |      0MiB / 16130MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla V100-SXM2...  Off  | 00000000:00:1C.0 Off |                    0 |
| N/A   52C    P0    58W / 300W |    318MiB / 16130MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  Tesla V100-SXM2...  Off  | 00000000:00:1D.0 Off |                    0 |
| N/A   52C    P0    60W / 300W |      0MiB / 16130MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   3  Tesla V100-SXM2...  Off  | 00000000:00:1E.0 Off |                    0 |
| N/A   51C    P0    43W / 300W |     11MiB / 16130MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    1    130119      C   /home/ubuntu/miniconda3/bin/python           307MiB |
+-----------------------------------------------------------------------------+


As we discussed in Section 11.3, most operations can use all the computational resources of all CPUs, or all computational resources of a single GPU. However, if we use multiple GPUs for model training, we still need to implement the corresponding algorithms. Of these, the most commonly used algorithm is called data parallelism.

## 11.4.1. Data Parallelism¶

In the deep learning field, Data Parallelism is currently the most widely used method for dividing model training tasks among multiple GPUs. Recall the process for training models using optimization algorithms described in Section 10.5. Now, we will demonstrate how data parallelism works using mini-batch stochastic gradient descent as an example.

Assume there are $$k$$ GPUs on a machine. Given the model to be trained, each GPU will maintain a complete set of model parameters independently. In any iteration of model training, given a random mini-batch, we divide the examples in the batch into $$k$$ portions and distribute one to each GPU. Then, each GPU will calculate the local gradient of the model parameters based on the mini-batch subset it was assigned and the model parameters it maintains. Next, we add together the local gradients on the $$k$$ GPUs to get the current mini-batch stochastic gradient. After that, each GPU uses this mini-batch stochastic gradient to update the complete set of model parameters that it maintains. Figure 10.1 depicts the mini-batch stochastic gradient calculation using data parallelism and two GPUs.

In order to implement data parallelism in a multi-GPU training scenario from scratch, we first import the required packages or modules.

%matplotlib inline
import d2l
from mxnet import autograd, np, npx, gluon
npx.set_np()


## 11.4.2. Define the Model¶

We use LeNet, introduced in Section 6.6, as the sample model for this section. Here, the model implementation only uses ndarrays.

# Initialize model parameters
scale = 0.01
W1 = np.random.normal(scale=scale, size=(20, 1, 3, 3))
b1 = np.zeros(20)
W2 = np.random.normal(scale=scale, size=(50, 20, 5, 5))
b2 = np.zeros(50)
W3 = np.random.normal(scale=scale, size=(800, 128))
b3 = np.zeros(128)
W4 = np.random.normal(scale=scale, size=(128, 10))
b4 = np.zeros(10)
params = [W1, b1, W2, b2, W3, b3, W4, b4]

# Define the model
def lenet(X, params):
h1_conv = npx.convolution(data=X, weight=params[0], bias=params[1],
kernel=(3, 3), num_filter=20)
h1_activation = npx.relu(h1_conv)
h1 = npx.pooling(data=h1_activation, pool_type='avg', kernel=(2, 2),
stride=(2, 2))
h2_conv = npx.convolution(data=h1, weight=params[2], bias=params[3],
kernel=(5, 5), num_filter=50)
h2_activation = npx.relu(h2_conv)
h2 = npx.pooling(data=h2_activation, pool_type='avg', kernel=(2, 2),
stride=(2, 2))
h2 = h2.reshape(h2.shape[0], -1)
h3_linear = np.dot(h2, params[4]) + params[5]
h3 = npx.relu(h3_linear)
y_hat = np.dot(h3, params[6]) + params[7]
return y_hat

# Cross-entropy loss function
loss = gluon.loss.SoftmaxCrossEntropyLoss()


## 11.4.3. Synchronize Data Among Multiple GPUs¶

We need to implement some auxiliary functions to synchronize data among the multiple GPUs. The following get_params function copies the model parameters to a specific GPU and initializes the gradient.

def get_params(params, ctx):
new_params = [p.copyto(ctx) for p in params]
for p in new_params:
return new_params


Try to copy the model parameter params to gpu(0).

new_params = get_params(params, d2l.try_gpu(0))
print('b1 weight:', new_params[1])

b1 weight: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] @gpu(0)
b1 grad: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] @gpu(0)


Here, the data is distributed among multiple GPUs. The following allreduce function adds up the data on each GPU and then broadcasts it to all the GPUs.

def allreduce(data):
for i in range(1, len(data)):
data[0][:] += data[i].copyto(data[0].context)
for i in range(1, len(data)):
data[0].copyto(data[i])


Perform a simple test of the allreduce function.

data = [np.ones((1, 2), ctx=d2l.try_gpu(i)) * (i + 1) for i in range(2)]
print('before allreduce:\n', data[0], '\n', data[1])
allreduce(data)
print('after allreduce:\n', data[0], '\n', data[1])

before allreduce:
[[1. 1.]] @gpu(0)
[[2. 2.]] @gpu(1)
after allreduce:
[[3. 3.]] @gpu(0)
[[3. 3.]] @gpu(1)


## 11.4.4. Split a Data Batch into Multiple GPUs¶

The utils module in Gluon provides a function to evenly split an array into multiple parts along the first dimension, and then copy the $$i^\mathrm{th}$$ part into the $$i^\mathrm{th}$$ device. It’s straightforward to implement, but we will use the pre-implemented version so later chapters can reuse the split_batch function we will define later.

Now, we try to divide the 4 data instances equally between 2 GPUs using the split_and_load function.

data = np.arange(24).reshape(4, 6)
ctx = [npx.gpu(0), npx.gpu(1)]
print('input: ', data)
print('output:', splitted)

input:  [[ 0.  1.  2.  3.  4.  5.]
[ 6.  7.  8.  9. 10. 11.]
[12. 13. 14. 15. 16. 17.]
[18. 19. 20. 21. 22. 23.]]
output: [array([[ 0.,  1.,  2.,  3.,  4.,  5.],
[ 6.,  7.,  8.,  9., 10., 11.]], ctx=gpu(0)), array([[12., 13., 14., 15., 16., 17.],
[18., 19., 20., 21., 22., 23.]], ctx=gpu(1))]


The split_batch function then splits both the features and labels.

# Save to the d2l package.
def split_batch(X, y, ctx_list):
"""Split X and y into multiple devices specified by ctx"""
assert X.shape[0] == y.shape[0]


## 11.4.5. Multi-GPU Training on a Single Mini-batch¶

Now we can implement multi-GPU training on a single mini-batch. Its implementation is primarily based on the data parallelism approach described in this section. We will use the auxiliary functions we just discussed, allreduce and split_and_load, to synchronize the data among multiple GPUs.

def train_batch(X, y, gpu_params, ctx_list, lr):
gpu_Xs, gpu_ys = split_batch(X, y, ctx_list)
with autograd.record():  # Loss is calculated separately on each GPU
ls = [loss(lenet(gpu_X, gpu_W), gpu_y)
for gpu_X, gpu_y, gpu_W in zip(gpu_Xs, gpu_ys, gpu_params)]
for l in ls:  # Back Propagation is performed separately on each GPU
l.backward()
# the GPUs
for i in range(len(gpu_params[0])):
# The model parameters are updated separately on each GPU
for param in gpu_params:
d2l.sgd(param, lr, X.shape[0])  # Here, we use a full-size batch


## 11.4.6. Training Functions¶

Now, we can define the training function. Here the training function is slightly different from the one used in the previous chapter. For example, here, we need to copy all the model parameters to multiple GPUs based on data parallelism and perform multi-GPU training on a single mini-batch for each iteration.

def train(num_gpus, batch_size, lr):
ctx_list = [d2l.try_gpu(i) for i in range(num_gpus)]
# Copy model parameters to num_gpus GPUs
gpu_params = [get_params(params, c) for c in ctx_list]
num_epochs, times, acces = 10, [], []
animator = d2l.Animator('epoch', 'test acc', xlim=[1, num_epochs])
timer = d2l.Timer()
for epoch in range(num_epochs):
timer.start()
for X, y in train_iter:
# Perform multi-GPU training for a single mini-batch
train_batch(X, y, gpu_params, ctx_list, lr)
npx.waitall()
timer.stop()
# Verify the model on GPU 0
lambda x: lenet(x, gpu_params[0]), test_iter, ctx[0]),))
print('test acc: %.2f, %.1f sec/epoch on %s' % (
animator.Y[0][-1], timer.avg(), ctx_list))


## 11.4.7. Multi-GPU Training Experiment¶

We will start by training with a single GPU. Assume the batch size is 256 and the learning rate is 0.2.

train(num_gpus=1, batch_size=256, lr=0.2)

test acc: 0.82, 2.1 sec/epoch on [gpu(0)]


By keeping the batch size and learning rate unchanged and changing the number of GPUs to 2, we can see that the improvement in test accuracy is roughly the same as in the results from the previous experiment. In terms of the optimization algorithms, they are identical.

Because of the extra communication overhead, and relative simple model we used here, there is no reduction in the training time. We will consider a more complex model in the next chapter.

train(num_gpus=2, batch_size=256, lr=0.2)

test acc: 0.84, 3.5 sec/epoch on [gpu(0), gpu(1)]


## 11.4.8. Summary¶

• We can use data parallelism to more fully utilize the computational resources of multiple GPUs to implement multi-GPU model training.
• With the same hyper-parameters, the training accuracy of the model is roughly equivalent when we change the number of GPUs.

## 11.4.9. Exercises¶

• In a multi-GPU training experiment, use 2 GPUs for training and double the batch_size to 512. How does the training time change? If we want a test accuracy comparable with the results of single-GPU training, how should the learning rate be adjusted?
• Change the model prediction part of the experiment to multi-GPU prediction.