Backpropagation
Backpropagation computes gradients through a composed computation graph using the chain rule. It is reverse-mode Automatic differentiation applied to a scalar training loss.
For a composition:
the chain rule is:
For model training:
More generally, each node in the graph receives an adjoint:
and sends it backward through local Jacobians:
This is usually implemented as vector-Jacobian products rather than explicit Jacobian matrices.
Reverse-Mode View
For an intermediate node:
the backward pass accumulates gradient contributions from all downstream users:
This accumulation is why reused tensors, shared parameters, and residual branches require careful gradient accounting.
For a parameter used in multiple places:
The optimizer updates the parameter after gradients from the chosen mini-batch and accumulation steps have been combined.
Loss Scaling
If the training loss is averaged over valid elements:
then masked or padded elements must be removed before the reduction. Otherwise the gradient scale depends on padding length:
instead of the intended valid-token count.
What Can Go Wrong
- A tensor is detached or converted outside the differentiable graph.
- A mask is applied after loss reduction instead of before it.
- A custom operation has a backward rule that does not match the forward computation.
- Gradient accumulation unintentionally scales the effective loss.
- Shared parameters receive multiple gradient contributions that are not expected.
- In-place operations overwrite values needed for backward.
- Mixed-precision scaling hides overflow or underflow if skipped silently.
Checks
- Are invalid or padded elements masked before loss reduction?
- Are gradients zeroed before the next step unless accumulation is intentional?
- Are frozen parameters excluded from gradient updates?
- Are gradient norms monitored when training is unstable?
- Does Gradient checking pass for custom differentiable code?
- Does the reported loss reduction match the intended example/token/object unit?
- Are gradient accumulation, distributed averaging, and batch-size scaling documented?