Distributed Training and Memory Saving
Training large models across GPUs and saving memory: ZeRO, mixed precision, and more.
Data ParallelismData parallelism is the basic method of distributed training: replicas of the same model are placed on multiple GPUs, the minibatch is split among them, and the gradients are averaged with All-Reduce before every replica is updated in the same way.
FSDPFSDP (Fully Sharded Data Parallel) is a data-parallel method built into PyTorch that shards all of a model's weights, gradients, and optimizer states across GPUs and gathers only the weights of the unit needed right before computing it. For small models it is about as fast as DDP, and it can train large models that do not fit on a single GPU.
Gradient CheckpointingGradient checkpointing is a method that reduces training memory by saving only some of the activations computed in the forward pass and recomputing the rest during the backward pass. It brings the activation memory of an n-layer network down to O(√n), at the cost of roughly one extra forward pass of computation.
Mixed Precision TrainingMixed precision training is a training method that computes weights, activations, and gradients in 16 bits (FP16) and performs only the weight update in 32 bits (FP32). Combined with loss scaling, it roughly halves memory without losing accuracy and speeds up training on supported GPUs.
Pipeline Parallelism (GPipe)GPipe is a pipeline-parallel method that splits a model's sequence of layers into stages placed on different GPUs, and divides each mini-batch into small micro-batches that flow through the stages like an assembly line. It reduces GPU idle time (the bubble) and updates the gradients synchronously all at once, so large models can be trained with exactly the same training as on a single device.
Pipeline Parallelism (PipeDream)PipeDream is a pipeline-parallel method that splits a model's layers into stages placed on GPUs and keeps the pipeline flowing without stopping by having each GPU alternate one forward pass and one backward pass (1F1B). It stashes the weight version used in the forward pass and reuses it in the backward pass to obtain correct gradients, and it reached target accuracy up to 5.3x faster than data parallelism.
Tensor Parallelism (Megatron-LM)Tensor parallelism is a model-parallel method that splits the weight matrices of a single layer across multiple GPUs and divides the matrix multiplications among them. Megatron-LM parallelizes the Transformer's MLP and attention with only two All-Reduces in the forward pass and two in the backward pass per layer, and used this to train an 8.3B-parameter language model.
ZeROZeRO is a method that partitions the weights, gradients, and optimizer state, which each GPU redundantly holds in data parallelism, across GPUs, reducing memory per GPU by up to a factor of the number of GPUs. It keeps computation and communication volume nearly the same as data parallelism and makes it possible to train large models without rewriting them.
