Distributed ML
Single GPU training is limited by memory and speed.
1. Communication Collectives
- Broadcast sends data from root device to all others, for model parameter synchronization.
- Reduce data across devices, storing result in root device. Used for gradient aggregation.
- Point-to-point: explicit comms between two devices.
- Gather: collect data from multiple devices to root device - for collecting partial predictions / logits for evaluation.
- Scatter: distribute data from root device to multiple devices.
- All-Gather: a gather (phase 1) followed by a broadcast (phase 2), used for synchronizing model parameters across devices.
- Reduce-Scatter: reduce (phase 1) data across devices, then divide (phase 2) into equal sized blocks across devices.
- All-to-All: each device sends different portions of data to every other device, used for shuffling data across devices.
- All-Reduce: reduce (phase 1) data across devices, then broadcast (phase 2) the result to all devices. Used for synchronizing model parameters across devices.
Data Parallelism splits data across devices. Here, model data fits into the whole GPU, btu training is too slow. So, keep a copy of the model on each device, and split the data across devices. Each device computes gradients for its own data, then gradients are aggregated across devices. This needs CC: All Reduce.
2. Model Parallelism
When a model can't fit on a GPU, split model across devices.
- Inter-layer Parallelism: split model into layer blocks. Each device computes a layer block. This needs all-to-all communication to pass activations between devices. This is bad for large models, as it requires a lot of communication between devices.
- Tensor Parallelism: Split layer weights into tensors - each device computes one tensor. For a transformer, we can parallelize both attention and linear blocks. All-Reduce is required to accumulate partial results (either in forward (
) or backward pass ( ) depending on direction of propagation). This must leave norm and dropout layers intact (overhead). Additionally, replicating across the tensor group invokes memory overhead. - Sequence Parallelism: normalization layers are independent along the sequence dimension, so this can be partitioned to reduce memory usage. This requires scatter and gather.
- SP+TP Parallelism leads to
and (conjugate). uses all-gather in forward pass, reduce-scatter in backward pass. uses reduce-scatter in forward pass, all-gather in backward pass. - Expert Parallelism: parallelize different experts across devices. This requires all-to-all communication to pass activations between devices.
- Pipeline Parallelism: each layer on a different device. Each device computes its layer and passes activations to the next device. This requires point-to-point communication between devices. This causes a pipeline bubble between devices. This is fixed by doing micro-batching - splitting a batch into smaller micro-batches, and sending them through the pipeline. This requires point-to-point communication between devices.
| Collectives | Used In |
|---|---|
| All-Reduce | DP, TP |
| All-Gather | TP+SP |
| Reduce-Scatter | TP+SP |
| Broadcast | DP |
| Point-to-Point | PP |
| All-to-All | EP |