Linear algebra for LLMsPart 8: Gradients through matrices

The chain rule as matrix products

6 min

The Jacobian

y=f(x), x∈Rn, y∈Rm:Jij=∂yi∂xj\displaystyle y = f(x),\ x \in \mathbb{R}^n,\ y \in \mathbb{R}^m: \quad J_{ij} = \frac{\partial y_i}{\partial x_j}

For a function from vectors to vectors, the derivative is a matrix: row ii holds the slopes of output ii with respect to every input. Shape m×nm \times n, outputs by inputs.

Write

Jacobian Jij=∂yi/∂xjJ_{ij} = \partial y_i / \partial x_j, shape (outputs × inputs).

A linear map is its own Jacobian

y=Wx  ⇒  J=W\displaystyle y = Wx \;\Rightarrow\; J = W

yi=∑jWijxjy_i = \sum_j W_{ij} x_j, so ∂yi/∂xj=Wij\partial y_i / \partial x_j = W_{ij}. The best linear approximation of a linear map is the map itself.

Composing functions multiplies Jacobians

z=g(f(x))  ⇒  Jz,x=Jz,y Jy,x\displaystyle z = g(f(x)) \;\Rightarrow\; J_{z,x} = J_{z,y}\, J_{y,x}

The chain rule for vectors is a matrix product, in the same right-to-left order as composing maps (Part 3). A network is a long composition, so its derivative is a long product of Jacobians.

Write

Chain rule: Jg∘f=Jg JfJ_{g \circ f} = J_g\, J_f.

Elementwise functions have diagonal Jacobians

y=σ(x) entrywise  ⇒  J=diag⁡(σ′(x))\displaystyle y = \sigma(x) \text{ entrywise} \;\Rightarrow\; J = \operatorname{diag}(\sigma'(x))

Output ii depends only on input ii, so the Jacobian is diagonal, and multiplying by it is just ⊙ σ′(x)\odot\, \sigma'(x) (Part 2). Code never builds it.

Backprop: start from the loss

∂L∂x=∂L∂z Jz,y Jy,x\displaystyle \frac{\partial L}{\partial x} = \frac{\partial L}{\partial z}\, J_{z,y}\, J_{y,x}
(1×m)(m×k)(k×n)\displaystyle (1 \times m)(m \times k)(k \times n)

The loss is a scalar, so ∂L/∂z\partial L / \partial z is a single row. Multiplying from the left keeps a row vector at every step: a vector–Jacobian product, cheap. Multiplying the Jacobians together first would build big matrices for nothing. That’s why gradients flow backward, from the loss to the input.

Write

Backprop = vector–Jacobian products, from the loss backward: δx=δyJy,x\delta_x = \delta_y J_{y,x}.

✏️ Your turn

One backward step

y=Wx,W=[1234],∂L∂y=[1,−1]\displaystyle y = Wx, \quad W = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad \frac{\partial L}{\partial y} = [1, -1]

Write ∂L/∂x\partial L / \partial x.

Work it out on paper, then go to the next card to check.

Answer

∂L∂x=[1,−1][1234]=[−2,−2]\displaystyle \frac{\partial L}{\partial x} = [1, -1]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} = [-2, -2]

The upstream row times the Jacobian WW. In column layout the same thing is written W⊤∂L∂yW^\top \frac{\partial L}{\partial y}.

In PyTorch

torch.autograd.functional.jacobian(f, x)     # the full J, for small checks
torch.autograd.grad(L, x)                    # what backward computes: a VJP

In a transformer

Autograd stores what each operation needs for its vector–Jacobian product during the forward pass (the activations), then runs the products in reverse. That stored data is why training uses far more memory than inference.

📌 Recap

The chain rule in one card

  • Jacobian: Jij=∂yi/∂xjJ_{ij} = \partial y_i / \partial x_j; for y=Wxy = Wx, J=WJ = W.
  • Composition multiplies Jacobians; elementwise functions give diagonals (⊙\odot).
  • Backprop multiplies a row vector by Jacobians, from the loss backward.

Write

The three lines above.