Skip to content

Chapter 14: Matrix Calculus and Autodiff

A gradient is a linear map, and a program is a composition of small differentiable steps. Automatic differentiation is the chain rule applied to a program rather than to a formula. Forward mode pushes a tangent through each step, a Jacobian-vector product; reverse mode records the computation and pulls a cotangent back through each step, a vector-Jacobian product (VJP). If you know the VJP of each primitive, you can differentiate any program built from them, and knowing it in closed form is what makes it fast and stable.

Concepts from other sections (23)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

This chapter builds that machinery from first principles, in the order you would build an autograd engine. It starts with dual numbers, which carry a derivative alongside each value and give exact derivatives with no step size; they become the oracle that every later rule is checked against. It then builds reverse mode one scalar at a time, where the graph and the backward walk are small enough to print. Because reverse mode costs about one extra forward pass per scalar output, it is how every neural network trains: one loss, millions of parameters. Scalars are too slow for real work, so the third section derives closed-form VJPs for matmul, softmax, log-softmax, cross-entropy, LayerNorm, and RMSNorm with matrix differentials and the trace trick, a few lines each. The last section turns to the costs of reverse mode: Hessian-vector products that give curvature without forming the Hessian, and activation checkpointing, which trades recomputation for memory on a n\sqrt{n}-type schedule. A problem set drills the derivations on paper.

Two habits run through the whole chapter. Gradients are checked, never trusted: central differences in float64 against the analytic VJP catch nearly every wrong rule. And every rule is worked by hand on small numbers before it is coded.

Learning objectives

  • Compute derivatives and Jacobian-vector products with dual numbers, and explain why they are exact to rounding.
  • Implement scalar reverse mode over a recorded graph and explain why adjoints accumulate and why the walk follows reverse topological order.
  • Derive closed-form VJPs with matrix differentials and the trace trick, including the correction terms of normalization layers and the sum over broadcast axes.
  • Compute Hessian-vector products from two gradient calls, and plan an optimal activation-checkpointing schedule under a memory budget.
  • Choose between forward and reverse mode by counting passes, and estimate the FLOPs and memory of a backward pass.

You should be comfortable with the chain rule from Calculus 1, gradients and Jacobians from Calculus 3, matrix products and shapes from Linear Algebra, and topological sorting from Discrete Math 2, which orders the backward walk. The stable softmax and log-sum-exp from Numerical Methods and Floating Point appear inside the VJPs. A good companion read is Parr and Howard’s The Matrix Calculus You Need for Deep Learning, deriving every identity by hand, followed by sections 2 and 3 of the Baydin survey. If you have never built micrograd, doing so once from memory is excellent preparation.

  1. Dual numbers and forward-mode autodiff. Dual numbers a+bεa + b\varepsilon with ε2=0\varepsilon^2 = 0 turn Taylor’s theorem into an identity, so one evaluation returns a value and its exact derivative; the section builds a numpy-compatible Dual and its JVP.
  2. Scalar reverse mode: the Value graph. A scalar Value records each operation and its local rule, and backward walks the graph once in reverse topological order to give the gradient with respect to every input.
  3. Matrix differentials, the trace trick, and closed-form VJPs. The differential method derives the VJPs of matmul, softmax, log-softmax, fused cross-entropy, LayerNorm, and RMSNorm, and unbroadcast sums gradients over broadcast axes.
  4. Hessian-vector products and the recompute-versus-memory schedule. Two gradient calls give HvHv to O(ϵ2)O(\epsilon^2); a counting argument gives the least peak memory of activation checkpointing and the optimal schedule under a budget.
  5. Matrix calculus problem set: differentials, trace trick, VJPs, SDPA Jacobian. Pen-and-paper problems with worked answers on shapes, differentials, the trace trick, VJPs, forward versus reverse cost, and the attention backward.
  • Automatic differentiation / autodiff: computing exact derivatives of a program by applying the chain rule to each primitive operation it executes, in forward mode (Jacobian-vector products) or reverse mode (vector-Jacobian products); it is how neural networks get their gradients, with no step size and no symbolic formula.

The gradients built here feed the optimizers of Optimization. In AI Engineering, the Foundations chapter turns these rules into a tensor autograd engine, registering each VJP as an op. In AI Engineering, Deep Learning treats backpropagation in the textbook setting.