Quant Memo
Advanced

Reverse-Mode vs Forward-Mode Autodiff

Automatic differentiation can sweep through a computation graph in two directions, and which direction is cheaper depends entirely on whether you have more inputs or more outputs — which is why neural networks, with millions of inputs and one loss, always use reverse mode.

Prerequisites: Automatic Differentiation, Computational Graphs and Automatic Differentiation

Automatic differentiation can compute the exact derivative of any function built from elementary operations, by walking a computational graph and applying the chain rule mechanically. But there are two directions to walk that graph, and they are not interchangeable in cost — one of them is what makes training a network with millions of weights and a single loss number tractable at all, and the other one would make it impossible.

The analogy: tracing water through pipes, forward or backward

Picture a network of pipes where water flows from several source taps, through junctions that mix and split flows, down to several drains. If you want to know "how much does drain 3's flow change if I open tap 1 a little more," you can either trace forward from tap 1, following every downstream path it touches until you reach every drain (one trace answers "this tap's effect on every drain"), or trace backward from drain 3, following every upstream path back to every tap that feeds it (one trace answers "every tap's effect on this drain"). If there is one tap and many drains, forward tracing is efficient — one sweep gets everything. If there are many taps and one drain, backward tracing wins for the identical reason, reversed.

The two sweeps, precisely

Forward mode seeds one input's derivative to 11 and every other input's to 00, then propagates that "tangent" forward through the graph alongside the ordinary values, so every node carries both its value and its derivative with respect to that one seeded input. One forward sweep yields (every output)/(that one input)\partial(\text{every output})/\partial(\text{that one input}).

Reverse mode runs the forward pass once to get all values, then walks backward from a single output seeded with derivative 11, and at each node multiplies the incoming "adjoint" by that node's local derivative to push the sensitivity to its inputs. One backward sweep yields (that one output)/(every input)\partial(\text{that one output})/\partial(\text{every input}).

cost(forward)(number of inputs)×(cost of one sweep)\text{cost(forward)} \propto (\text{number of inputs}) \times (\text{cost of one sweep}) cost(reverse)(number of outputs)×(cost of one sweep)\text{cost(reverse)} \propto (\text{number of outputs}) \times (\text{cost of one sweep})

In plain English: forward mode's total cost scales with how many inputs you need derivatives for; reverse mode's scales with how many outputs you have. A neural network training loss has millions of inputs (the weights) and exactly one output (the scalar loss) — so reverse mode needs one sweep total, forward mode would need millions.

Worked example 1: one input, three outputs — forward wins

Let y1=x2y_1 = x^2, y2=sin(x)y_2 = \sin(x), y3=exy_3 = e^x, all functions of a single input x=1x=1. Forward mode: seed dx=1dx=1, propagate once — dy1=2xdx=2dy_1 = 2x\,dx = 2, dy2=cos(x)dx=0.540dy_2=\cos(x)\,dx=0.540, dy3=exdx=2.718dy_3=e^x\,dx=2.718 — all three derivatives from one sweep. Reverse mode would need three separate backward passes, one seeded from each output, to get the same three numbers. With one input and three outputs, forward mode is three times cheaper.

Worked example 2: three inputs, one output — reverse wins

Let L=x1x2+x2x3L = x_1 x_2 + x_2 x_3 at x1=2,x2=3,x3=4x_1=2, x_2=3, x_3=4. Reverse mode: one forward pass computes L=6+12=18L = 6+12=18; one backward pass, seeding LL's adjoint at 11, gives L/x1=x2=3\partial L/\partial x_1 = x_2 = 3, L/x2=x1+x3=6\partial L/\partial x_2 = x_1+x_3=6, L/x3=x2=3\partial L/\partial x_3=x_2=3 — all three gradients from a single backward sweep. Forward mode would need three separate forward sweeps, one seeded per input, to get the same three numbers. With three inputs and one output, reverse mode is three times cheaper — and for a network with a million weights, this ratio is a million to one.

forward mode: 1 input, 3 outputs reverse mode: 3 inputs, 1 output
Each sweep direction is cheapest when it matches the "few" side of the graph — one input against many outputs, or many inputs against one output.

Gradient descent
parameter →
position -0.623loss -0.141gradient -0.930converging

Every step shown in the explorer above is only possible because reverse-mode autodiff already collapsed a million-weight gradient into a single cheap backward sweep — the step itself is a separate question from how that gradient got computed.

Forward mode costs one sweep per input and is cheap when inputs are few; reverse mode costs one sweep per output and is cheap when outputs are few — neural network training has millions of inputs (weights) and one output (the scalar loss), which is exactly why reverse mode, not forward mode, is what every deep learning framework runs by default.

What this means in practice

Backpropagation is reverse-mode automatic differentiation applied to a graph whose final node is a scalar loss — nothing more exotic than that. Forward mode still has real uses: computing directional derivatives, Jacobian-vector products when the number of outputs genuinely exceeds the number of inputs, or certain second-order methods that combine both directions to get Hessian-vector products cheaply.

It's a common mistake to think reverse mode is simply "the better algorithm" in general. It is only cheaper when outputs are few relative to inputs — for a function with many outputs and few inputs, forward mode is strictly cheaper, and some libraries (like JAX) expose both jvp (forward) and vjp (reverse) explicitly because picking the wrong one for a given shape can cost orders of magnitude in compute.

Related concepts

Practice in interviews

Further reading

  • Baydin, Pearlmutter, Radul & Siskind, Automatic Differentiation in Machine Learning: A Survey (2018)
ShareTwitterLinkedIn