Reverse-mode automatic differentiation
Computing exact gradients of any program in roughly the cost of running it once.
On this page14 sections
Every gradient-based training loop in modern ML, plus large parts of scientific optimization and differentiable simulation.
A trick that shows how every knob in a huge program affects its result, for about one run's cost.
- 1Model has billions of knobs
- 2Nudging each knob is too slow
- 3Run once, record every step
- 4Walk backwards applying chain rule
- 5All gradients for ~one run's cost
The setting. A program computes a number (say, a training loss) from many inputs (the parameters of a model). To improve the model, gradient descent needs the gradient: the list of how much changes per small change in each . Writing the formula for it by hand is error-prone, and estimating it by nudging each input separately costs extra runs. For in the billions that is impossible.
Key idea: the chain rule, applied backwards. Any program is a chain of simple operations (add, multiply, exp, matrix product). Each intermediate result depends on earlier ones. The chain rule says the effect of an input on the output is a sum over all paths through this graph, with each path contributing a product of local slopes.
Reverse mode organises that sum cleverly:
- Forward pass. Run the program normally and store the intermediate values (this stored record is often called the tape).
- Backward pass. Start with the output, whose "adjoint" is . Walk the operations in reverse order. For each value compute , which says how much the final output changes per unit change in . When you reach the inputs you have all of .
The formula in step 2 is the chain rule, summed over every operation that uses . In words: the importance of a value is the sum of the importance of what it feeds, weighted by how strongly it feeds them.
Cost. For a function with one output and inputs, the whole gradient costs only a small constant multiple of one forward run, no matter how large is. Forward mode, which pushes derivatives forward instead, needs one pass per input, so it wins only when there are few inputs and many outputs.
The trade-off is memory. The backward pass needs the forward values, so they must be stored. Checkpointing stores only some of them and recomputes the rest, trading extra time for less memory.
Where it is hard. Reverse mode assumes smooth, step-by-step computation. Branches, loops with data-dependent length, sorting, rounding and other non-smooth operations need care, and choosing the best checkpointing schedule for an arbitrary graph is not fully solved. Higher-order derivatives (derivatives of derivatives) can blow up in cost if done naively.
Reverse-mode autodiff is not an approximation like finite differences. It gives exact derivatives of the program as written, up to ordinary rounding error.
Go deeper
The full technical text.
What it is
A program is a composition of primitive operations. Reverse-mode AD records those operations on a tape during the forward pass, then walks the tape backwards, applying the chain rule to accumulate the derivative of a single scalar output with respect to every input.
Why it matters
For a function the full gradient costs a small constant multiple of evaluating , independent of . That is what makes training networks with billions of parameters possible at all.
How it works
- Forward pass: evaluate the program and store intermediate values.
- Backward pass: start from and propagate adjoints .
The trade-off is memory: intermediates must be kept (or recomputed, via checkpointing).
Open problems
- Memory-optimal checkpointing schedules for arbitrary graphs
- Differentiating through discrete control flow and non-smooth operations
- Higher-order derivatives without blow-up
Words on this page (11)
- gradient
- The list of numbers saying how much an output changes when each input is nudged slightly; it points in the direction of fastest increase.
- derivative
- How fast one quantity changes when another changes by a tiny amount; the slope of a curve at a point.
- chain rule
- The rule for the derivative of a chain of steps: multiply the local slopes of each step along the way.
- gradient descent
- Improving a model by repeatedly taking a small step in the direction that lowers the loss, using the gradient to choose the direction.
- backpropagation
- The name for reverse-mode autodiff when it is used to compute gradients in neural networks.
- automatic differentiation
- A family of methods that compute exact derivatives of a computer program mechanically, without symbolic algebra or finite-difference guessing.
- loss function
- A single number scoring how badly a model is doing; training tries to make it small.
- neural network
- A model built from many simple adjustable units, whose settings are tuned on data.
- adjoint
- In reverse mode, the number stored for each intermediate value saying how much the final output changes per unit change in that value.
- checkpointing
- Saving only some intermediate values during the forward run and recomputing the others when needed, to trade time for memory.
- forward mode
- The other way to do automatic differentiation: push derivatives forward alongside the values, one input direction per pass.
New to this? Read first (3)
- 3Blue1Brown: What is backpropagation really doing?A visual walk through how backprop sends blame backwards through a network.
- Automatic differentiation (Wikipedia)Clear overview of forward versus reverse mode with a worked example.
- Automatic differentiation in machine learning: a survey (Baydin et al.)The standard survey; the first sections are readable for a newcomer.
Understanding map
map of the whole wiki →What to read before Reverse-mode automatic differentiation (left), and what builds on it: entries that depend on it and its open problems (right). Every box links to its page.
- Start with
- Reverse-mode automatic differentiationYou are here
- Then
- PyTorchLibrary
- Quantum Monte Carlo: DMC, AFQMC and neural-network wavefunctionsAlgorithm
- Machine-learned interatomic potentials and foundation potentialsAlgorithm
- Biomolecular structure prediction (AlphaFold-style)Algorithm
- The transformer architectureAlgorithm
- NCCL and collective communication: ring and tree all-reduceLibrary
- Mechanistic circuits and causal interventionsTheory
- Normalization layers (BatchNorm, LayerNorm, RMSNorm)Algorithm
Open problems & questions
No open problems or questions on this entry.
Progress timeline
No progress recorded on this entry.