skip to content

A PyTorch DDP model whose auxiliary CTC head runs only on some steps raises a prior-iteration reduction error; what fixes it, at what cost?

level: middleimportance: should knowfreq 11%

answer

  1. a hook that never fires
  2. a bucket that never completes
  3. off by default
  4. a graph walk every iteration
  5. static only if the unused set never changes

basics

~20 s

On steps that skip the head, its parameters get no gradient, so their bucket never finishes reducing. Set find_unused_parameters=True and pay a graph traversal every iteration, or run the head every step with its loss weighted to zero.

solid answer

~50 s

DDP registers an autograd hook on every parameter that requires a gradient and expects all of them to fire in each backward. On steps where the intermediate-CTC head is skipped, its parameters never get a gradient, their bucket never completes, and when the next iteration starts DDP raises *Expected to have finished reduction in the prior iteration*. `find_unused_parameters` defaults to `False`. Setting it `True` makes DDP walk the autograd graph from the forward outputs after every forward and mark unreached parameters ready; the cost is that traversal every iteration, and every bucket is still all-reduced. The cheaper fix is structural: compute the head every step and weight its loss by zero on off-steps, so its parameters get zero gradients. `static_graph=True` does not fit, because the set of unused parameters changes from step to step. `TORCH_DISTRIBUTED_DEBUG=INFO` makes the error name the parameters that received no gradient.

go deeper

for a junior

Recall that DDP expects every trainable parameter to get a gradient each step, and that find_unused_parameters is off by default.

for a middle

Explain why an unused parameter stalls its bucket, what find_unused_parameters=True does after each forward, and what that traversal costs.

for a senior

Choose between find_unused_parameters, zero-weighted losses, static_graph and freezing from how the model's branches vary, and use the debug log to find the parameter.

for a principal

Set a team default for DDP flags so conditional heads are designed to keep every parameter in the graph rather than paying for a traversal everywhere.

## Why DDP needs every gradient When `DistributedDataParallel` wraps a model it registers one **autograd hook** per parameter that requires a gradient and assigns the parameters to **buckets**. During backward, each hook marks its parameter ready; when every parameter in a bucket is ready, DDP launches that bucket's all-reduce. At the end of backward DDP waits for every bucket. That design assumes every such parameter receives a gradient in every backward. A parameter that takes no part in computing the loss never fires its hook, so its bucket never completes. Here DDP does not hang silently: when the next iteration begins with the previous reduction unfinished, it raises > RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. In the conformer, the **intermediate-CTC head** — an auxiliary classifier on a middle encoder layer — runs only on every other step. On the steps that skip it, its weights are unused. ## Finding the culprit Setting `TORCH_DISTRIBUTED_DEBUG=INFO` (or `DETAIL`) makes DDP log the fully qualified names and indices of the parameters that did not receive a gradient when it crashes, such as `Parameters which did not receive grad for rank 0: ...`. On a large model that list is the quickest way to find the conditional branch. ## The fixes and their costs | Fix | What DDP does | Cost | Fits this model? | |---|---|---|---| | `find_unused_parameters=True` | walks the autograd graph from the forward outputs after each forward and marks unreached parameters ready | a graph traversal every iteration; all buckets still reduced | yes | | Run the head every step, loss × 0 on off-steps | nothing special: every hook fires, with zero gradients | the head's forward and backward compute on every step | yes | | `static_graph=True` | assumes the used and unused parameter sets never change | no per-iteration search for unused parameters | no — the set changes between steps | | Remove or freeze the head before wrapping | parameters without `requires_grad` are not registered | the head no longer trains | only if it is truly dead | Points worth knowing about each: - **`find_unused_parameters` defaults to `False`**, because the traversal has a cost on every iteration and most models use every parameter. Marking a parameter ready does not let DDP skip its bucket; it only stops DDP from waiting forever for a gradient that will not come. - **Zero-weighting the loss** keeps the head in the autograd graph, so its hooks fire with zero gradients. For a small head the extra compute is usually cheaper than a traversal on every step, and it keeps the default DDP configuration. - **`static_graph=True`** is for graphs whose used and unused sets are fixed for the whole run. It also enables cases the default cannot handle, such as activation checkpointing applied several times. DDP's logging data reports a `can_set_static_graph` value at the end of a run that tells you whether it would have been safe. - **Freezing** with `requires_grad_(False)` before wrapping takes the parameters out of DDP's bookkeeping entirely — right for a frozen feature extractor, wrong for a head you still want to train. ## The zero-weight pattern in code ```python out = model(feats, feat_lens, tokens) # head computed on every step aux_weight = 0.3 if step % 2 == 0 else 0.0 # same schedule as before loss = out.final_ctc_loss + aux_weight * out.inter_ctc_loss loss.backward() # every hook fires, some with zeros ``` The schedule is unchanged — the head still contributes on every other step — but the autograd graph is the same shape on every step, so DDP's default configuration works and no traversal runs. ## How to choose 1. If the conditional branch is small and cheap, make it unconditional and weight its loss by zero. 2. If the skipped part is expensive and its use varies per step, accept `find_unused_parameters=True` and measure the slowdown. 3. If the same parameters are unused on every step, set `static_graph=True` or remove them. 4. Never leave `find_unused_parameters=True` on "just in case": it costs time on models that do not need it. ## Mistakes to recognise - Assuming DDP skips the all-reduce for unused parameters once they are marked. - Setting `static_graph=True` on a model whose branches change per step. - Reading the error as a data or NCCL problem rather than a model-structure one.

  • What would change if the auxiliary head were never used at all, on any step?
    The unused set would then be the same on every iteration, so `static_graph=True` becomes valid and avoids the per-iteration traversal. Better still, remove the head or freeze it with `requires_grad_(False)` before wrapping; DDP only registers parameters that require gradients, so frozen ones never enter a bucket.
  • How can you tell after a run whether static_graph=True would have been safe for a DDP model?
    DDP's logging data — `model._get_ddp_logging_data()` at the end of a training run — includes a `can_set_static_graph` entry. When it is true, the used and unused parameter sets did not change during that run, and `static_graph=True` is usually safe for the same training loop.

  • DDP finds unused parameters by default
  • Marking a parameter unused lets DDP skip its bucket's all-reduce
  • static_graph=True handles parameters that are unused on some steps only
  • The prior-iteration reduction error means NCCL dropped a message
  • find_unused_parameters=True is free, so leave it on everywhere