In PyTorch 2.14 DDP, how do gradient buckets overlap the all-reduce with backward, and when would you change bucket_cap_mb or gradient_as_bucket_view?
answer
- reverse order of registration
- a bucket goes when it is full
- 25 MiB unless told otherwise
- the first iteration is different
- gradients as views of the bucket
basics
~20 sDDP packs gradients into buckets, 25 MiB by default with a smaller first one, and launches each bucket's all-reduce as soon as its gradients are ready, while backward continues. Tune bucket_cap_mb by measuring overlap; gradient_as_bucket_view saves one gradient copy.
solid answer
~50 sAt construction DDP assigns parameters to buckets in roughly the reverse of `parameters()` order, because gradients become ready in that order during backward. An autograd hook marks each gradient ready; when a bucket is full of ready gradients, DDP launches its all-reduce asynchronously while backward computes earlier layers, and buckets are launched in index order so every rank issues the same collectives. In PyTorch 2.14, `bucket_cap_mb=None` means 25 MiB with a smaller first bucket; passing any value applies it to every bucket. After the first iteration DDP rebuilds its buckets once, from the order gradients actually arrived. Larger buckets mean fewer collectives but a later start and a longer exposed tail; smaller ones mean earlier overlap but more launches — so measure. `gradient_as_bucket_view=True` makes `param.grad` a view into the bucket, saving memory equal to the gradients' size and a copy per step.
go deeper
Recall that DDP groups gradients into buckets and starts reducing them while backward is still running, with a 25 MiB default size.
Explain reverse-order bucket assignment, the per-parameter hooks, launching buckets in index order, and the one-time bucket rebuild after the first iteration.
Tune bucket_cap_mb from measured overlap rather than folklore, and know what gradient_as_bucket_view saves, when it takes effect and what code it breaks.
Judge when DDP's communication is already well overlapped and further gains need a different parallel strategy or a faster interconnect, not more knob-turning.
## What a bucket is `DistributedDataParallel` does not all-reduce one gradient at a time, and it does not wait for the whole backward pass either. Its **Reducer** groups parameter gradients into **buckets** — flat buffers of a size limit — and runs one all-reduce per bucket. Fewer, larger messages amortise the fixed cost of each collective, and reducing a bucket while backward is still producing other gradients hides communication behind computation. That hiding is the **overlap** DDP is known for. ## How overlap happens 1. **Construction.** The Reducer maps parameters to buckets in roughly the **reverse order of `model.parameters()`**, because backward produces gradients from the last layer to the first. It registers one autograd hook per parameter. 2. **Backward.** Each hook fires when its gradient is ready and marks it. When every gradient in a bucket is marked, the Reducer launches that bucket's all-reduce **asynchronously** and backward keeps going. 3. **Ordering.** Buckets are launched in **bucket index order**, not in whatever order they happen to fill, because every rank must issue the same collectives in the same sequence; a mismatch can give wrong results or hang. 4. **End of backward.** The Reducer waits for all outstanding all-reduces and writes the averaged gradients into `param.grad`. The first iteration is special. With the default `find_unused_parameters=False` (or with `static_graph=True`), PyTorch 2.14 starts with all parameters in a single bucket, then, after the first iteration, **rebuilds the buckets once** from the order gradients actually became ready in backward. Benchmark from the second iteration onwards. ## The size knob: `bucket_cap_mb` | Setting in PyTorch 2.14 | Bucket limits | |---|---| | `bucket_cap_mb=None` (default) | 25 MiB, with a smaller first bucket (`dist._DEFAULT_FIRST_BUCKET_BYTES`) so the first all-reduce starts early | | `bucket_cap_mb=50` | 50 MiB for every bucket; the special small first bucket is turned off | | `bucket_cap_mb_list=[...]` | a separate cap per bucket (present in 2.14; not supported with the Python reducer) | For the conformer — about 120 million parameters, fp32 gradients — the gradients total about 480 MB, roughly 458 MiB, which at 25 MiB is about 19 buckets per backward. What moving the cap trades: - **Larger buckets**: fewer collectives, which helps when per-call latency dominates, as on a slower link between two nodes; but the first all-reduce starts later and the last bucket's all-reduce runs after backward has finished, fully exposed. - **Smaller buckets**: communication starts sooner and overlaps more, at the price of more collective launches. Never tune it blind. With `TORCH_DISTRIBUTED_DEBUG=DETAIL` and `TORCH_CPP_LOG_LEVEL=INFO`, DDP logs average backward compute time, backward communication time and **backward comm/comp overlap time** per rank. Change the cap, rerun, and keep the setting that raises overlap and lowers step time; a profiler trace shows the same thing on a timeline. A tuning pass that holds up in review: 1. Record step time and the logged overlap at the default from iteration two onwards, over a few hundred steps. 2. Try one larger and one smaller cap, for example 50 and 10, changing nothing else. 3. Keep the setting with the lowest step time, and recheck it whenever the node count, the interconnect or the model size changes, because the best size depends on all three. ## The memory knob: `gradient_as_bucket_view` By default each gradient lives in `param.grad` and is copied into its bucket for the all-reduce, so gradients occupy memory twice. With `gradient_as_bucket_view=True`, `param.grad` becomes a **view into the bucket**: - peak memory drops by roughly the total gradient size — about 458 MiB per rank for this model; - the copy between gradients and buckets disappears; - gradients become views only after the first iteration, so measure memory after it; - code that calls `detach_()` on gradients fails and must follow the pattern `Optimizer.zero_grad()` uses. A rough picture of what overlap is worth: suppose backward takes 120 ms on each rank and reducing all 458 MiB of gradients across the two nodes takes 90 ms. With one bucket, all 90 ms come after backward and are fully exposed. With about 19 buckets reduced while backward continues, most of the 90 ms hides behind compute and roughly the last bucket's share — around 5 ms — remains exposed, provided communication keeps up with compute. When communication is slower than compute, no bucket size can hide it all, and the remedy lies outside DDP's knobs. ## What does not change - Bucketing never shards anything: every rank still holds the full gradients. - A communication hook registered with `register_comm_hook` replaces the per-bucket all-reduce (for example, to compress it) but keeps the same bucket boundaries.
- Besides gradients, what does PyTorch 2.14 DDP send between ranks during training?Module buffers, such as BatchNorm running statistics: at the start of each forward DDP broadcasts them from rank 0 so every replica uses the same values. The flag is `forward_sync_buffers`, which defaults to on; `broadcast_buffers`, the older name, is deprecated since 2.13. Turning it off saves the broadcast but lets the replicas' buffers drift apart.
- Why might a step-time benchmark of a DDP job look worse on iteration one than afterwards, beyond warm-up?With the default settings, PyTorch 2.14 starts with every parameter in one bucket, so the single all-reduce can only start after backward ends. After the first iteration the Reducer rebuilds its buckets once from the observed gradient order, and overlap begins. Measure from the second iteration onwards.
Gradient buckets work like mail sacks at a sorting office: each sack leaves as soon as it is full, so trucks are on the road while sorting continues. Bigger sacks mean fewer trips but the first truck leaves later; tiny sacks mean constant trips.
- DDP waits for the whole backward pass before starting any all-reduce
- Buckets are reduced in whatever order they fill on each rank
- Bigger buckets always make DDP faster
- gradient_as_bucket_view shards gradients across ranks
- Passing bucket_cap_mb=25 is identical to leaving the default
Assumes PyTorch 2.14: the bucket_cap_mb=None default with a smaller first bucket, bucket_cap_mb_list and forward_sync_buffers are as in the v2.14.1 source of torch/nn/parallel/distributed.py.