Run it first
The interactive playground below traces forward evaluation and reverse-mode gradient propagation through a 2-2-1 network. Step forward to compute preactivations and outputs; step backward to inspect how adjoint errors propagate and accumulate into weight sensitivities.
Enable JavaScript to use this local playground. The explanation and derivations below work without it.
Forward-mode vs. reverse-mode computational complexity
Consider differentiating a general mapping whose Jacobian matrix has dimension :
- Forward-Mode Automatic Differentiation computes directional derivatives (Jacobian-Vector Products, JVPs). Evaluating the full Jacobian requires seeding the forward pass with n standard basis vectors: forward sweeps.
- Reverse-Mode Automatic Differentiation propagates adjoint sensitivities backwards (Vector-Jacobian Products, VJPs). Seeding the backward pass at the output requires m backward sweeps: sweeps.
In deep learning, the objective function produces a single scalar loss: . The parameter space contains weights (millions or billions). Forward-mode autodiff would require P forward passes per training step—an impossible computational cost. Reverse-mode backpropagation computes the exact analytical gradient for all P parameters simultaneously in a single reverse sweep () at the cost of approximately two forward passes.
The multivariate chain rule: summing over fan-out paths
When an intermediate variable x fans out and influences multiple downstream operations , the total derivative of the scalar loss L with respect to x is the sum of partial derivatives across all outgoing dependency paths:
Forward pass: Backward pass (Adjoint accumulation):
┌──▶ [ y1 ] ──▶ ┌──◀ [ ∂L/∂y1 ] ◀──
[ x ]│ [ x ]│
└──▶ [ y2 ] ──▶ └──◀ [ ∂L/∂y2 ] ◀──
∂L/∂x = (∂L/∂y1 · ∂y1/∂x) + (∂L/∂y2 · ∂y2/∂x)
In implementation, this multivariate rule requires that backward passes initialize parameter gradient buffers with zero and accumulate with addition (+=) as incoming signals arrive. Overwriting gradients instead of adding them drops downstream dependency branches.
Analytical trace of the 2-2-1 network
The playground above executes a complete 2-2-1 network with input , target y, and parameters :
1. Forward equations
2. Backward equations
Each weight gradient is the outer product of the local output sensitivity and the incoming input activation x. Computing these derivatives requires storing all forward activations in memory until the reverse pass reaches their corresponding layer.
Memory vs. compute: activation checkpointing
In standard backpropagation, peak memory consumption scales linearly with model depth: for L layers, batch size B, and activation width d. For frontier LLMs, caching intermediate activations across 80+ layers with 32K context windows rapidly exhausts GPU VRAM before model weights even load.
Activation checkpointing (Griewank, 1992; Chen et al., 2016)
Activation checkpointing discards intermediate activations during the forward pass, retaining only the inputs to coarse block boundaries (e.g. every layers):
Forward Pass: [ x0 ] ──▶ ( Block 1 ) ──▶ [ x1 (saved) ] ──▶ ( Block 2 ) ──▶ [ x2 (saved) ] ──▶ Loss Backward Pass (Block 2): 1. Recompute intermediate activations inside Block 2 from saved x1 2. Backpropagate gradients through Block 2 back to x1 3. Discard Block 2 activations and repeat for Block 1 from saved x0
By re-evaluating the forward pass of one block at a time during the backward pass, activation memory drops from to , at the cost of running approximately one extra forward pass (+33% arithmetic compute).
Sources and further reading
- Linnainmaa (1976), Taylor expansion of the accumulated rounding error (reverse accumulation algorithm)
- Werbos (1974), Beyond Regression: New Tools for Prediction and Analysis in the Behavioral Sciences (Harvard PhD thesis)
- Rumelhart, Hinton, & Williams (1986), Learning representations by back-propagating errors (Nature)
- Chen et al. (2016), Training Deep Nets with Sublinear Memory Cost (Activation Checkpointing)
- Griewank (1992), Achieving logarithmic growth of temporal and spatial complexity in reverse automatic differentiation