liveBook · Manning

Distributed training

As explained in Deep Learning with Python, Third Edition

A training approach that spreads computation and/or data across multiple devices to speed up model training.

A single GPU limits how quickly a growing model can train—and sometimes whether it fits at all. Distributed training uses multiple devices in two main ways: split the data across copies of a model, or split the model itself across devices. section 18.2.1

Two GPUs, one batch

With data parallelism, each GPU holds a complete model replica. Split a batch of 128 into two batches of 64; each replica processes one half. During training, the results from the devices must then be merged. This is the usual choice when the model fits on one GPU. section 18.2.1 section 18.2.2

Choose the split that matches the constraint:

  • Data parallelism splits samples across model replicas. Use a sufficiently large global batch so each GPU has enough work. section 18.2.1 section 18.2.2
  • Model parallelism splits one model across devices. Reserve it chiefly for models too large for one device, rather than as a speed trick for ordinary models. section 18.2.1
  • Combine them when needed: split a model across four devices, then replicate that arrangement twice to use eight devices. section 18.2.1
keras.distribution.set_distribution(keras.distribution.DataParallel())

For Keras with the JAX backend, set data parallelism before building the model. Device names can also be supplied explicitly when only selected GPUs should participate. section 18.2.2

device_mesh = keras.distribution.DeviceMesh(
    shape=(2, 4),                                                     #1
    axis_names=["data", "model"],                                     #2
)

For a combined setup, a device mesh names separate data and model axes; this example assigns two positions to data and four to the model. section 18.2.2

Model-parallel configuration also needs a layout map specifying how model variables use the mesh, followed by a ModelParallel distribution setting. Scaling is not free: merging updates adds overhead. Approximate speedups given for two, four, and eight GPUs are 2×, 3.8×, and 7.3×, assuming batches large enough to keep them busy. A rented multi-GPU VM can avoid much of the hardware setup. section 18.2.2 section 18.2.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through replicas, batch splitting, scaling limits, and JAX mesh configuration; leaves cloud VM setup instructions aside.
Examples
Uses a batch of 128 on two GPUs, an eight-device combined layout, and Keras distribution listings.
What it assumes
Assumes basic familiarity with models, batches, and GPUs while introducing replicas and device meshes through examples.
What you can do after
Apply it via Keras and JAX by setting DataParallel or configuring a ModelParallel device mesh and layout map.
How it connects
Connects data parallelism and model parallelism to batch size, gradients, device meshes, and variable layouts.

Also explained in

Compare how 6 books explain distributed training →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.