TL;DR
Data parallelism is a way to train one model faster by using several devices at once. Every device holds a full copy of the model and works on a different slice of the same mini-batch. The devices then average their gradients, so every copy applies the same update and the copies stay identical.
Builds on: Stochastic Gradient Descent · Leads to: All-Reduce, Model Parallelism
Where it's used
Image classifiers on ImageNet. Goyal and colleagues trained a ResNet-50 classifier on the ImageNet training set in about one hour using 256 GPUs, with a global batch of 8,192 images (2017). Each GPU processed 32 images per step and the gradients were averaged across all 256 GPUs. A single GPU would have taken many hours for the same job.
Large language models. Training a language model with billions of parameters uses data parallelism as the outer layer of the setup, usually with other techniques added. Each replica of the model processes its own shard of the text, and the replicas exchange gradients every step. Frameworks such as PyTorch expose this as DistributedDataParallel.
Horovod in industry. Uber's open-source Horovod library made synchronous data parallelism easy to use in TensorFlow and PyTorch (2017 onward). It is a common example of the pattern being packaged for teams that were not writing their own communication code.
How it works
Take two workers, a model with one number to learn, and a batch of four examples.
- Start with both workers holding the same value, . Worker A gets examples 1 and 2. Worker B gets examples 3 and 4. Each example has its own gradient, , , and .
- Each worker computes the average gradient over its own two examples. Worker A gets . Worker B gets . Check: each worker only sees half the batch, so each average is only a partial picture.
- The workers exchange these numbers and average them: . This is the gradient over the whole batch, because the shards are the same size. Check: , the same number.
- Both workers apply the same update with learning rate : . Both copies now hold .
- The next batch starts from identical copies, so the process repeats. The copies never drift apart, because they always apply the same averaged gradient.
The picture for a larger setup is the same:
flowchart LR B[Mini-batch of 32] --> S1[Shard 1: 8 examples] B --> S2[Shard 2: 8 examples] B --> S3[Shard 3: 8 examples] B --> S4[Shard 4: 8 examples] S1 --> G1[Gradient g1 on GPU 1] S2 --> G2[Gradient g2 on GPU 2] S3 --> G3[Gradient g3 on GPU 3] S4 --> G4[Gradient g4 on GPU 4] G1 --> A[Average all four gradients] G2 --> A G3 --> A G4 --> A A --> U[Every GPU applies the same update]
The math
Let there be workers, each holding a copy of the parameters . A global mini-batch of examples is split into equal shards of size . Worker computes its local gradient the average gradient of the loss over its own examples. The averaged gradient is , and every worker applies .
Step 2 in symbols
Each is a mean over examples. The worker has no access to the other shards, so it can only compute its own mean.
Step 3 in symbols
Because every shard has the same size , the average of the shard means equals the mean over the full batch: This is why the result matches a single device running on the whole batch. The equality depends on equal shard sizes. If shards differ in size, the average must be weighted by shard size.
Step 4 in symbols
With and , the update is . Every worker does this arithmetic on the same numbers, so the copies stay equal.
The cost of step 3
The averaging is an all-reduce: each worker must send and receive gradient numbers. For a model with parameters stored in bytes each, the gradient has bytes. A ring all-reduce over workers sends about bytes per worker per step. The step time is roughly , and the efficiency is when no communication is overlapped with compute. In practice, frameworks overlap much of with the backward pass.
Worked examples
Setup: , learning rate , four examples with gradients . Worker A has the first two, worker B the last two.
- Worker A averages its gradients: .
- Worker B averages its gradients: .
- Average the two worker results: .
- Check against the full batch: . They match, because the shards have equal size.
- Update: .
- Both workers now hold .
Interpretation: the workers split the work of computing one batch gradient and got the same answer as one worker would. Nothing about the model changed.
Setup: parameters, stored in 16-bit floats so bytes each. workers. Assume a link that moves 25 gigabytes per second per worker.
- Gradient size: bytes, which is 500 MB.
- Factor for ring all-reduce: .
- Data sent per worker per step: MB MB.
- Communication time: MB GB/s GB GB/s s, or 35 ms.
- Interpretation: if one forward and backward pass on the local shard takes longer than 35 ms, the communication can be hidden behind it. If the compute is shorter, communication dominates and adding more workers helps less.
Setup: a training run that works well with batch 256 and learning rate . The ImageNet training set has about 1,281,167 images. Each GPU processes 32 images per step.
- On 8 GPUs, the global batch is . This is the tuned configuration, with learning rate .
- Move to 64 GPUs with the same 32 images per GPU. The global batch is , which is 8 times larger.
- Linear scaling rule: multiply the learning rate by the same factor, . The rule is from Goyal and colleagues. It assumes the gradient noise is small enough that bigger batches give more reliable steps. Warm-up over the first few epochs is used to avoid instability at the start.
- Steps per epoch: , so 626 steps are needed to see every image once.
- Compare with the 8-GPU run: steps per epoch.
- Interpretation: the 64-GPU run needs about 8 times fewer steps per epoch. Whether it reaches the same accuracy depends on the batch staying below the Critical Batch Size and on the learning rate being adjusted correctly.
Try it: if the same model is trained on 128 GPUs with 32 images per GPU, what is the global batch and the scaled learning rate, starting from the 256 and tuning? Answer: the global batch is , which is 16 times 256. The linear scaling rule gives a learning rate of , which is usually too large and would need a longer warm-up or a different schedule. The lesson to take is that the rule is a starting point to test, not a guarantee.
Common mistakes and tradeoffs
It is tempting to think that doubling the GPUs halves the training time. In fact the gain is smaller, because each step now also pays for the all-reduce, and because a very large batch may stop reducing the number of steps needed (see Critical Batch Size). Measure the throughput and the steps to target accuracy, not only the GPU count.
It is tempting to think that each replica learns something different and that the copies must be merged later. In fact the replicas apply the same averaged gradient, so they stay identical at every step. Divergence would be a bug, usually from different random seeds, different data order that is not sharded properly, or non-deterministic operations.
It is tempting to think that summing the gradients is the same as averaging them. In fact summing multiplies the effective learning rate by when each worker's loss is already a mean over its shard. Frameworks average by default; check the reduction setting if the loss curve blows up when you add workers.
It is tempting to think that Batch Normalization behaves the same on every worker. In fact, each replica computes its mean and variance over its own shard, here 32 images, not the global 2,048. This changes the model's behaviour and often hurts accuracy with small per-GPU batches. Synchronised batch normalisation, which averages the statistics across workers, fixes this at the cost of extra communication.
It is tempting to think that data parallelism always works. In fact each device must hold the whole model, its gradients and its optimizer state in device memory. When the model does not fit in one device's memory, data parallelism alone fails, and Model Parallelism or memory-saving methods are needed.
When to use it: the model fits on one device, the training data is large, and you want more throughput with the same algorithm. When not to use it: the model or its optimizer state does not fit, or the network between devices is too slow for the gradient size. Alternatives include asynchronous updates (which give stale gradients and are harder to reason about) and sharded methods that split the optimizer state across devices.
History
By the early 2010s, neural networks had outgrown single machines. In 2012 a Google team described DistBelief, a system that trained large networks across many machines by combining model and data parallelism, with a central parameter server that held the weights and received updates asynchronously. Asynchronous updates were fast but meant that workers often used stale weights. Li and colleagues described a general parameter server design in 2014 that many later systems built on.
Synchronous data parallelism became the default for GPU training as the cost of the averaging step fell. The large-batch experiments of Goyal and colleagues in 2017 showed that a batch of 8,192 images could train ResNet-50 to full accuracy with the linear scaling rule and warm-up, which made large synchronous runs practical. Around the same time, ring-based all-reduce, which spreads the communication evenly across workers, was brought into deep learning frameworks, and Horovod packaged it for common frameworks. Those two steps made data parallelism the standard first move when a model needs more throughput.
Recap
- One model is copied onto devices, and each copy gets a different slice of the mini-batch.
- Each copy computes a gradient on its own slice. These gradients are averaged, so the result matches the gradient on the whole batch when the shards are equal in size.
- Every copy applies the same update, so the copies stay identical. The cost is the all-reduce, which grows with the number of parameters.
- Larger batches need a larger learning rate, usually with warm-up, and stop helping beyond the critical batch size.
- It needs the whole model on each device, so it is combined with Model Parallelism for models that are too large.
Read next: All-Reduce for how the averaging is done efficiently, Model Parallelism for splitting a model that does not fit, and Megatron-LM: Training Multi-Billion Parameter Language Models and ZeRO: Memory optimizations Toward Training Trillion Parameter Models, which combine these ideas for very large models.
Check yourself
QuestionRecall: in one sentence, what does every worker hold, and what is the only thing the workers exchange in a synchronous data-parallel step?Show answer
Every worker holds a full copy of the model parameters. The workers exchange their local gradients, which are averaged (all-reduce) so every copy applies the same update.
QuestionComputation: four workers each average their gradients over 8 examples. The four averages are 2, 4, 6 and 8. What is the averaged gradient, and what is the update with learning rate 0.05 starting from theta equal to 1?Show answer
The averaged gradient is , which is also the mean over all 32 examples because the shards are equal. The update is .
QuestionWhy: why does a synchronous data-parallel run with batch normalisation not give the same result as one device running the full batch?Show answer
Batch normalisation computes its mean and variance from the examples it sees. Each replica sees only its own shard, so its statistics come from a smaller batch than the global one. The averaged gradient is correct, but the forward pass differs, so the model's behaviour changes. Synchronised batch normalisation fixes this by averaging the statistics across workers.
QuestionApplied: a team has a 3-billion-parameter model that does not fit in the memory of one GPU, even with a batch of one. They propose adding 64 GPUs with data parallelism. What is wrong with this plan, and what would you suggest instead?Show answer
Data parallelism copies the whole model onto every device, so each GPU still needs memory for the full model, its gradients and optimizer state. Adding GPUs does not fix a per-device memory limit. Suggest splitting the model across devices with model parallelism, or using a method that shards the optimizer state and parameters across the GPUs, and then combining that with data parallelism to add throughput.
Sources
- Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour (Goyal et al., 2017)
- Measuring the Effects of Data Parallelism on Neural Network Training (Shallue et al., 2018)
- Horovod: fast and easy distributed deep learning in TensorFlow (Sergeev and Del Balso, 2018)
- Stochastic gradient descent (Wikipedia)