from d2l import tensorflow as d2l
import tensorflow as tf
import kerasThe previous section did data-parallel training the hard way — manual all_reduce, manual replica management. In practice, every framework wraps it in a one-liner:
nn.DataParallel(net) (multi-GPU on one host) or nn.parallel.DistributedDataParallel (multi-host).gluon.Trainer(..., kvstore='device').tf.distribute.MirroredStrategy().Same numerical result; orders of magnitude less boilerplate; NCCL all-reduce under the hood.
We use a small ResNet for these experiments — the speedup from data parallelism only matters once the per-GPU compute is non-trivial:
def resnet18(num_classes, in_channels=1):
"""A slightly modified ResNet-18 model built with Keras."""
def resnet_block(num_channels, num_residuals, first_block=False):
blk = tf.keras.Sequential()
for i in range(num_residuals):
if i == 0 and not first_block:
blk.add(d2l.Residual(num_channels, use_1x1conv=True,
strides=2))
else:
blk.add(d2l.Residual(num_channels))
return blk
# Smaller conv, no max-pool (same as the PT version)
net = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, kernel_size=3, strides=1, padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation('relu'),
resnet_block(64, 2, first_block=True),
resnet_block(128, 2),
resnet_block(256, 2),
resnet_block(512, 2),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(num_classes),
])
return netWrap the model in the framework’s data-parallel container. Parameters are replicated to each GPU automatically:
Number of devices: 4
The wrapper also handles inference — splits the input minibatch across replicas, gathers outputs:
The loop looks like ordinary single-GPU training because the wrapper owns the distributed work:
The important lesson is the interface: after wrapping the model, most training code should not need to know how many GPUs are present.
test acc: 0.93, 105.1 sec total on ['/device:GPU:0']
Use this as the throughput baseline before the data-parallel wrapper adds replication and gradient averaging.
test acc: 0.91, 92.0 sec total on ['/device:GPU:0', '/device:GPU:1']
The training loop is unchanged; the wrapper splits the minibatch and synchronizes gradients under the hood.
DataParallel, MirroredStrategy) reduce data-parallel SGD to one line of setup.DistributedDataParallel / MultiWorkerMirroredStrategy — same idea, NCCL/Gloo across the network.