liveBook · Manning

Model parallelism

As explained in Deep Learning with Python, Third Edition

A distributed-training method where different parts of one model are placed on different devices to fit or execute a larger model.

A model can grow too large to fit on one GPU. Model parallelism addresses that limit by placing different parts of one model on different devices. The devices work together on the same batch. This differs from data parallelism, which puts a complete model replica on each device and gives each replica different data. Use model parallelism primarily to train a larger model, not as the default way to speed up a model that already fits on one device. section 18.2.1 section 18.4

Split a large dense layer

Consider a Keras model with a 16,000-feature input, a Dense layer with 64,000 units, and a second Dense layer with 8,000 units. One way to distribute the first layer is to split its kernel and bias into two halves along the output-unit dimension. Run the matrix multiplication for one half on gpu:0 and the other on gpu:1. Each device computes a different part of that layer’s output from the same input. This illustrates the central choice: divide the model’s work, rather than divide the batch between full copies of the model. section 18.2.1

A practical Keras setup with JAX describes the device arrangement first. A DeviceMesh with shape (2, 4) and axes named data and model uses four devices to split model computation, then makes two copies of that split model for different sub-batches. The two methods can therefore work together: model parallelism handles model size, while data parallelism handles separate data. section 18.2.1 section 18.2.2

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

The (2, 4) mesh names the two device axes used for data parallelism and model parallelism. section 18.2.2

To specify the split in Keras:

  • Create a LayoutMap for the device mesh. Map each Dense kernel to (None, "model") and each bias to ("model",), using the variables’ paths as keys. section 18.2.2
  • Pass the layout map to ModelParallel, set batch_dim_name to "data", and register the distribution strategy. Inspect a variable’s sharding to check the resulting mesh and partition specification. section 18.2.2

Rule of thumb: start with data parallelism when a full model fits on each device; reach for model parallelism when it does not. Architectures with naturally parallel branches are especially well suited to splitting model computation. section 18.2.1 section 18.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through a dense-layer split and a Keras device-mesh configuration, including variable layouts and sharding inspection.
Examples
Uses a large two-layer Keras model, a two-GPU kernel split, and an eight-GPU mesh with data and model axes.
What it assumes
Assumes familiarity with Keras models and GPU training while introducing device meshes and layout maps from their basic roles.
What you can do after
Apply it via Keras ModelParallel and LayoutMap to partition model variables across a device mesh.
How it connects
Connects model parallelism to data parallelism, device meshes, variable sharding, and distributed training.

Also explained in

Compare how 5 books explain model parallelism →

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.