[2020 VLDB] PyTorch Distributed: Experiences on Accelerating Data Parallel Training
One-line Summary
This paper presents the design, implementation, and evaluation of the PyTorch distributed data parallel module.
Paper Structure Outline
INTRODUCTION
BACKGROUND
PyTorch
Data Parallelism
AllReduce
SYSTEM DESIGN
API
Gradient Reduction
A Naive Solution
Gradient Bucketing
Overlap Computation with Communication
Gradient Accumulation
Collective Communication
IMPLEMENTATION
Python Front-end
Core Gradient Reduction
EVALUATION
Latency Breakdown
Bucket Size
Scalability
Round-Robin Process Group
DISCUSSION
Lessons Learned
Future Improvements
Gradient Order Prediction
Layer Dropping
Gradient Compression
RELATED WORK
CONCLUSION
Background & Motivation
There are three steps in training a DNN model:
Forward pass: Computes loss
Backward pass: Computes gradients
Optimizer step: Updates parameters
To train large models on large datasets, data parallelism is applied so that multiple workers work together to do the training. Each worker holds a replica of a model, trains the model (forward & backward pass) using a partition of the dataset, and averages the gradients/parameters among the workers.
System Design
API
There are two design goals when designing the API:
Non-instrusive: Converting local training scripts to distributed scripts should require minimal code modifications.
Interceptive: For as many optimizations as possible to work, the API needs to allow the implementation to intercept various signals and trigger appropriate algorithms correctly.
Gradient Reduction
Naive solution: DDP controls all training processes to (1) start from the same model state and (2) consume the same gradients in every iteration. (2) can be implemented by inserting a gradient synchronization phase after the local backward pass, or by adding a hook to trigger computation after every backward pass. There are two performance concerns:
Collective communication performs poorly on small tensors
By separating the gradient computation and synchronization, we lose the chance to overlap the two phases
Gradient bucketing: We can observe that collective communications are more effective on large tensors than on smaller tensors. As a result, we can use gradient reduction to bucket multiple gradients into one allreduce operation. However, DDP should not compact all gradients in one single allreduce, otherwise the communication cannot overlap with computation.
Overlap computation with communication: With bucketing, DDP only needs to wait until all contents in the same bucket is ready before launching communications. There are two things that requires attention, though. The first thing is that the reducing order must be the same across all processes, otherwise mismatches might occur. The other thing is that the backward pass could hang due to some gradients being skipped and never saying "I'm ready" to their corresponding buckets. See the graph below for two examples.
Gradient accumulation: Instead of doing allreduce for every iteration, do allreduce every n interations.
The paper covered some level of technical details for each of the above four subsections.
Collective Communication
DDP is built on top of communication libraries like NCCL, Gloo, and MPI. The APIs from all three libraries are wrapped into the same ProcessGroup API. In DDP, workers are expected to join a process group for commuication primitives to work on.
Implementation Details
Python Front-end
Configurable knobs
Model device affinity
Model buffers
Core Gradient Reduction
Parameter-to-bucket mapping
Autograd hook
Bucket allreduce
Globally unused parameters
Evaluation
Discussion
There is no single configuration that would work for all use cases, but there are some rules that can be summarized and can help us narrow down the range of the optimal configuration:
Communication backend: In most cases, NCCL is considerablly faster than Gloo.
Bucket size: The optimal bucket size lies in between the small extreme and the large extreme. The optimal bucket sizes are likely to increase with the size of the model in a sub-linear manner.
Resource allocation: In NCCL, it is recommended to keep the workers in a DDP group within the same machine, otherwise there will be significant slowdown due to the bandwidth across the machines being lower than that between same-machine GPUs.
Some future directions for optimizations:
Gradient order prediction: trace the backward order using autograd hooks and update parameter to bucket mapping accordingly
Layer dropping: drop layers randomly during the forward pass
Gradient compression: only communicates gradients with the necessary precision
New Vocabulary
Hooks in PyTorch: A hook can be registered on a Tensor or a nn.Module. A hook is basically a function that is executed when either forward or backward is called.
Links
Last updated