TL;DR
Model parallelism splits one neural network across several devices. Each device holds only part of the weights and does only part of the arithmetic, so a model that is too large for one device can still be trained or served. The two main ways to cut the model are inside a layer (tensor parallelism) and between layers (pipeline parallelism).
Builds on: Data Parallelism, GPU Memory Hierarchy, All-Reduce · Leads to: Megatron-LM, ZeRO, Activation Checkpointing
Where it's used
Training GPT-scale language models. The 175-billion-parameter GPT-3 Transformer model stores about 350 GB of weights in 16-bit numbers (175 billion times 2 bytes). That is several times the memory of an 80 GB GPU, so no single device can even hold the weights. Large runs split each layer across a group of GPUs inside one server, and then split the stack of layers across groups of servers.
The Megatron-LM training runs. The Megatron-LM team (NVIDIA, 2019) trained an 8.3-billion-parameter transformer on 512 GPUs. They split each layer across 8 GPUs that sit in the same server, because those GPUs share a fast link. The paper is the standard reference for tensor parallelism in transformers.
Serving large models. A 70-billion-parameter model in 16-bit precision needs about 140 GB for its weights, which is more than one 80 GB GPU can hold. Inference servers therefore spread the weights over at least two GPUs and pass activations between them for every token generated.
How it works
We will trace one tiny layer, a matrix multiplication, split across two devices. Device 1 and device 2 each hold half of the weight matrix.
Setup: the input is the row vector . The weight matrix is . Without splitting, the output is .
- Compute the whole layer on one device as a reference. . Check: the answer has one number per column of , so two numbers.
- Cut by columns. Device 1 gets the first column and device 2 gets the second column . Each device holds half the weights. Check: together they hold all four numbers of , and neither holds all of them.
- Both devices receive the full input , because each needs all of it. Device 1 computes . Device 2 computes . Check: each device did half the multiplications.
- Put the pieces side by side. The result is , the same as step 1. No addition across devices was needed here, because each output number came from exactly one device. Check: the gathered result matches the reference.
- Now the next layer. Suppose it multiplies by a matrix with one column, , so the output is . Cut by rows. Device 1 gets row and device 2 gets row . Device 1 already holds 's first entry, , and device 2 holds the second, . Each device multiplies only what it has: device 1 computes , and device 2 computes .
- The devices now add their partial results with an All-Reduce, which gives every device the sum . Check: the unsplit answer is .
The trick is in steps 4 to 6. The first layer's output was already split between the devices, and the second layer can use that split directly. Only one communication step (the all-reduce) is needed for the pair of layers. This is exactly the pattern Megatron-LM uses inside each Transformer block.
The other way to cut a model is by depth. Each device gets a run of consecutive layers, and the data flows from device to device like a factory line:
flowchart LR I[Input batch] --> S1[Stage 1: layers 1 to 4 on GPU 1] S1 --> S2[Stage 2: layers 5 to 8 on GPU 2] S2 --> S3[Stage 3: layers 9 to 12 on GPU 3] S3 --> S4[Stage 4: layers 13 to 16 on GPU 4] S4 --> O[Output]
Each stage waits for the one before it, so the devices sit idle while the pipeline fills and drains. The fix is to cut the batch into small micro-batches and keep several in flight, which the math section quantifies.
The math
For a layer , split the weight matrix into column blocks, , one block per device. Device computes , and the full output is the concatenation . For a following layer , split into row blocks, , so that . Each device computes one term, and the sum is an All-Reduce.
Step 1 in symbols
Here is the input row vector, is the weight matrix, and is the number of devices sharing one layer. Device stores only , which is of the columns of , so it stores of the weights of this layer.
Step 2 in symbols
With and , the column blocks are and . Then and . Concatenating gives , which equals computed on one device. No communication happens in this step.
Step 5 and 6 in symbols
Here is split into row blocks and . Each device computes : device 1 gets and device 2 gets . The all-reduce computes , which is .
Memory per device
If a model has parameters stored at bytes each, and it is split into tensor-parallel ways and pipeline stages, each device stores about
bytes of weights. The product is the number of devices that share one copy of the model. This counts weights only. Gradients, optimizer state and activations add more, so the true requirement is larger.
The pipeline bubble
With pipeline stages and equal micro-batches, the first stage starts on micro-batch 1 and the last stage sits idle until the first micro-batch reaches it. Counting the idle slots in an ideal schedule gives a fraction of time spent idle, the bubble,
The bubble shrinks as grows, because the fixed ramp-up of slots is spread over more useful work.
- 4 stages
- 8 stages
Worked examples
- Input , weights .
- Unsplit reference: the first output is , and the second is . So .
- Device 1 holds column . It computes .
- Device 2 holds column . It computes .
- Gather the two numbers in order. The result is , which matches step 2.
The split changed where the arithmetic ran, not the answer. Each device did two multiplications instead of four.
- Weights: parameters, stored at bytes each. Total: bytes, which is 350 GB.
- One 80 GB GPU cannot hold this, so the model must be split. Use tensor-parallel ways and stage.
- Per device: GB of weights.
- Compare with the 80 GB limit: GB remain for gradients, optimizer state, activations and buffers.
- Interpretation: 8-way tensor parallelism makes the weights fit, but the leftover room is limited. Real systems add more devices or techniques such as activation checkpointing to free memory for the rest.
The answer is only a lower bound on the memory needed, because it counts weights and nothing else.
- A model is split into 8 pipeline stages, so .
- With micro-batches, the bubble is . About 30 percent of the time is idle.
- With , the bubble is , about 10 percent.
- Quadrupling the micro-batches cut the idle time by about two thirds. Each micro-batch is smaller, so the work per step drops, but the schedule becomes much fuller.
- Interpretation: adding micro-batches helps, but it also means more memory for activations and more steps of overhead.
Try it: a pipeline has 4 stages and uses 12 micro-batches. What fraction of time is idle? Answer: , so 20 percent of the time is idle.
Common mistakes and tradeoffs
It is tempting to think that model parallelism always makes training faster. In fact it makes a model possible to run at all, and it adds communication, because the devices must exchange partial results at every layer. This is why tensor parallelism is usually kept inside one server, where GPUs share a fast link, and pipeline parallelism is used across servers, where traffic is less frequent.
It is tempting to think that splitting a model across devices divides its memory by in every respect. In fact only the weights and the matching optimizer state shrink cleanly. Activations are often the larger cost during training, and they are not divided the same way.
It is tempting to think that more pipeline stages are always better. In fact each extra stage adds to the bubble, unless you also raise the number of micro-batches, as the math shows.
Use model parallelism when the weights do not fit on one device, or when a single layer is too large to run on one device. Do not use it when Data Parallelism already fits: data parallelism is simpler and needs less communication inside each step. Alternatives include ZeRO-style sharding of optimizer state and gradients, which keeps the model whole on each device but spreads the memory-heavy state across them.
History
For most of the early deep learning era, a model could fit on one device, so the question did not arise. In 2012, AlexNet was split across two GTX 580 GPUs, each with 3 GB of memory, because the network did not fit on one card. The split was done by hand and was specific to that network.
Once language models with hundreds of millions and then billions of parameters appeared, the need became general. GPipe (2019) showed how to pipeline a network across devices with micro-batches. Megatron-LM (2019) showed how to split the matrices inside each transformer layer across GPUs in one server, and it trained an 8.3-billion-parameter model on 512 GPUs. ZeRO (2019) took a different route, sharding memory-heavy state across data-parallel workers. These methods caught on because the largest models kept growing faster than the memory of any single device.
Recap
- A model is split across devices either within a layer (tensor parallelism) or between layers (pipeline parallelism).
- Splitting a weight matrix by columns needs no communication for that layer. Splitting the next matrix by rows needs one All-Reduce to sum the partial results.
- Weights per device shrink in proportion to the number of devices that share a copy of the model.
- Pipeline stages sit idle during ramp-up. The bubble falls as micro-batches increase.
- Read next: Megatron-LM for the transformer version, ZeRO for sharding memory instead of the model, and Activation Checkpointing for trading compute for activation memory.
Check yourself
QuestionWhy does the column split of the first layer need no communication, while the row split of the second layer does?Show answer
In the column split, each device computes a different set of output numbers, and those numbers are simply placed side by side. No device needs another device's result to finish its own outputs. In the row split of the second layer, each device produces only a partial sum over the shared output dimension, so the partial sums must be added together, which is what the all-reduce does.
QuestionA model has 70 billion parameters stored in 2 bytes each, split 4 ways tensor-parallel and 2 pipeline stages. How many GB of weights does each device store?Show answer
Total bytes are , which is 140 GB. The number of devices sharing one copy is . Per device: GB.
QuestionA pipeline with 6 stages uses 6 micro-batches. What is the bubble fraction, and what would it be with 30 micro-batches?Show answer
With 6 micro-batches the bubble is . With 30 micro-batches it is . Raising the number of micro-batches from 6 to 30 cuts the idle fraction from about 45 percent to about 14 percent.
QuestionWhy is tensor parallelism usually kept inside one server rather than spread across many servers?Show answer
Tensor parallelism needs an all-reduce for every layer, in both the forward and backward pass, so the devices exchange data very often. Inside one server the GPUs share a fast link, and the cost of those exchanges stays small. Across servers the links are slower, and the same frequency of exchange would stall the computation.
QuestionA team wants to train a 13-billion-parameter model in 16-bit precision. Each GPU has 40 GB of memory. The team plans 2-way tensor parallelism and no pipeline. Will the weights fit on each device, and what is left over?Show answer
Total bytes are GB. Split 2 ways, each device stores GB of weights. That fits in 40 GB, leaving GB for gradients, optimizer state and activations. Whether that is enough depends on the optimizer and the batch size, so the weights alone do not settle the question.
Sources
- Shoeybi et al., Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism (2019): https://arxiv.org/abs/1909.08053
- Huang et al., GPipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism (2019): https://arxiv.org/abs/1811.06965
- Rajbhandari et al., ZeRO: Memory Optimizations Toward Training Trillion Parameter Models (2019): https://arxiv.org/abs/1910.02054