Skip to content
DAH

Reverse-mode automatic differentiation

Computing exact gradients of any program in roughly the cost of running it once.

On this page14 sections
Why other work relies on it

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.

  1. 1Model has billions of knobs
  2. 2Nudging each knob is too slow
  3. 3Run once, record every step
  4. 4Walk backwards applying chain rule
  5. 5All gradients for ~one run's cost

The setting. A program computes a number yy (say, a training loss) from many inputs x1,…,xnx_1,\dots,x_n (the parameters of a model). To improve the model, gradient descent needs the gradient: the list of how much yy changes per small change in each xix_i. Writing the formula for it by hand is error-prone, and estimating it by nudging each input separately costs nn extra runs. For nn 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 vjv_j 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:

  1. Forward pass. Run the program normally and store the intermediate values (this stored record is often called the tape).
  2. Backward pass. Start with the output, whose "adjoint" is yˉ=1\bar y = 1. Walk the operations in reverse order. For each value viv_i compute vˉi=∑jvˉj ∂vj/∂vi\bar v_i = \sum_j \bar v_j\,\partial v_j/\partial v_i, which says how much the final output changes per unit change in viv_i. When you reach the inputs you have all of ∂y/∂xi\partial y/\partial x_i.

The formula in step 2 is the chain rule, summed over every operation jj that uses viv_i. 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 nn inputs, the whole gradient costs only a small constant multiple of one forward run, no matter how large nn 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 f:Rn→Rf: \mathbb{R}^n \to \mathbb{R} the full gradient costs a small constant multiple of evaluating ff, independent of nn. That is what makes training networks with billions of parameters possible at all.

How it works

  1. Forward pass: evaluate the program and store intermediate values.
  2. Backward pass: start from yˉ=1\bar{y} = 1 and propagate adjoints vˉi=∑jvˉj ∂vj/∂vi\bar{v}_i = \sum_j \bar{v}_j \, \partial v_j / \partial v_i.

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)
Words on this page
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)
New to this? Read first

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.

  1. Start with
  2. Then

Open problems & questions

No open problems or questions on this entry.

Progress timeline

    No progress recorded on this entry.