liveBook · Manning

Distributed training

As explained in Deep Learning with R, Third Edition

Distributed training is the execution of model training across multiple processors or machines that coordinate computation and parameter updates.

Distributed training uses multiple GPUs to train a model when one GPU limits training speed or cannot hold the model. The two main approaches are data parallelism, which splits the data, and model parallelism, which splits the model. section 18.2.1

keras$distribution$set_distribution(
  keras$distribution$DataParallel()
)

For data parallelism with Keras and JAX, set the distribution strategy before building the model. Keras then places a model replica on each GPU. section 18.2.2

A batch across two GPUs

With a batch of 128, two replicas each process 64 samples. During training, their gradients are averaged and the model copies are updated. This is the usual choice when the model fits on one GPU. section 18.2.1

Model parallelism instead places different parts of one model on different devices to process a batch together. It is chiefly useful when the model is too large for one device, not as the usual way to speed up a model that already fits. Both approaches can be combined: four devices can hold one split model while another four hold its replica. section 18.2.1

device_mesh <- keras$distribution$DeviceMesh(
  shape = shape(2, 4),  #1
  axis_names = c("data", "model")  #2
)

A two-by-four device mesh names separate data and model axes for that combined arrangement. Model parallelism also requires a layout map specifying how model variables are distributed. section 18.2.2

More GPUs do not guarantee proportional speedups: combining updates takes time, and each GPU needs enough samples to stay busy. A small global batch can leave the resulting per-GPU batches too small. section 18.2.2

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains data and model parallelism with batch-splitting examples and Keras configuration, but defers cloud VM setup.
Examples
Illustrates a 128-sample batch across two GPUs and a two-by-four device mesh.
What it assumes
Requires access to multiple GPUs; introduces the two distribution approaches before showing their Keras configuration.
What you can do after
Apply it via Keras and JAX to configure data parallelism and specify a combined data-and-model device layout.
How it connects
Connects data parallelism, model parallelism, batch size, gradients, device meshes, and variable sharding.

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 R, Third Edition. The section links point to the parts of the book it draws on.