Gradient accumulation and all-reduce
Quick answer
Gradient accumulation combines contributions from multiple microbatches before an optimizer step. All-reduce is a collective that combines values across participating ranks and returns the reduced result to each rank. For mean sample loss, divide the total gradient sum by the total number of contributing samples. Examples use small synthetic inputs and Python or local CPU PyTorch. ByteMind execution is unverified.
Weight unequal worker gradients
counts=[2,6]; means=[1,5]print(sum(n*g for n,g in zip(counts,means))/sum(counts))print(sum(means)/len(means))Expected output (locally tested): 4.0 3.0. Averaging rank means incorrectly gives each rank equal weight.
How it works
If rank r has n_r examples and mean gradient g_r, the global gradient is sum(n_r*g_r)/sum(n_r). An unweighted mean of rank means is correct only for equal counts or special coincidental values. For R ranks with default DDP gradient averaging, scaling each local summed loss by R/N yields the intended global mean for N contributing samples, provided all ranks participate in the same step. Accumulate microbatch loss sums at unchanged parameters, normalize once, and perform one optimizer update. Clear old gradients before the accumulation cycle. With DDP, no_sync can avoid intermediate gradient communication; the forward must be inside the context, and the final backward must synchronize. Batch-dependent layers, stochastic masks and floating-point summation can break equivalence with a monolithic batch. All ranks must enter compatible collectives in the same order with matching tensor metadata. Distinguish unequal sample counts within a step from unequal numbers of steps: the latter needs an explicitly supported join or balanced schedule.
Accumulate native autograd
import torchw=torch.tensor(0.,dtype=torch.float64,requires_grad=True)for batch in [[1.,2.],[3.]]: y=torch.tensor(batch,dtype=torch.float64) (((w-y)**2).sum()/3).backward()print(round(w.grad.item(),8))Expected output (locally tested): -4.0. Two backward calls add gradients for the same parameter; denominator is total examples.
Common mistakes
Dividing every microbatch mean by the number of microbatches weights a short tail too heavily. Do not update parameters between microbatches when trying to reproduce one large-batch step. Do not confuse all-reduce SUM with a mean.
Practice and next steps
Compare the weighted and unweighted gradient for worker counts 2 and 6 and local means 1 and 5. Explain which represents the sample mean and why.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gradient accumulation and all-reduce — knowledge check
Apply gradient accumulation and all-reduce to small examples and debugging scenarios.
What denominator gives a global mean sample gradient?
When should the optimizer step in a single accumulation cycle?
What does all-reduce SUM return to participating ranks?
Why can a short final microbatch matter?
What must ranks agree on for collectives?