Contrast forward-mode and reverse-mode automatic differentiation. For a function with n inputs and m outputs, when is each cheaper, and why does deep learning use reverse mode?
Answer
Model answer, written in a candidate's voice
In short
- Forward mode pushes tangents: one pass per input direction, computing Jacobian-vector products.
- Reverse mode pushes adjoints: one pass per output direction, computing vector-Jacobian products.
- Full Jacobian costs scale like n forward passes or m reverse passes, so forward wins when n is small and reverse wins when m is small.
- Deep learning has a scalar loss (m=1) with millions/billions of parameters (n huge), so reverse mode gives the whole gradient in one backward pass.
- Reverse mode stores activations/tape for the backward pass, so memory is the cost; checkpointing trades extra compute for lower memory.
- Concrete examples: dual numbers for forward mode; JAX jvp for JVP and vjp for VJP.
In full
Forward mode propagates tangents through the computation, so one pass gives a Jacobian-vector product for a chosen input direction; with dual numbers this is arithmetic on x + epsilon*dx, and in JAX it is jvp. Reverse mode propagates adjoints backward from outputs, so one pass gives a vector-Jacobian product for a chosen output direction, exposed in JAX as vjp. For a full Jacobian, forward mode needs about one pass per input direction (n passes), while reverse mode needs about one pass per output direction (m passes), up to constant factors. Deep learning uses reverse mode because the loss is scalar (m=1) while the parameter count n is enormous, so a single backward pass yields the gradient with respect to all parameters rather than requiring millions of forward-mode passes. The trade-off is memory: reverse mode must keep intermediate activations or a tape for the backward sweep, which is why activation checkpointing recomputes segments to reduce memory at the cost of extra compute. In short, forward mode is cheap when inputs are few, reverse mode is cheap when outputs are few, and scalar-loss training is exactly the many-inputs/one-output regime.
What a strong answer covers
- Forward mode pushes tangents (Jacobian–vector products): one pass per input direction.
- Reverse mode pushes adjoints (vector–Jacobian products): one pass per output.
- With a scalar loss and millions of parameters (n ≫ m), reverse mode wins.
- Reverse mode must store activations, so memory is the cost; activation checkpointing trades compute for memory.
- Mentions dual numbers and JAX
jvp/vjpas concrete examples.
Common mistakes
- Describing reverse mode as numerical differentiation.
- Ignoring the memory cost.