Linear algebra for LLMsPart 8: Gradients through matrices

A linear layer backwards

7 min

The setup

Y=XW,X ⁣:n×i,W ⁣:i×o,Y ⁣:n×o\displaystyle Y = XW, \quad X\!: n \times i, \quad W\!: i \times o, \quad Y\!: n \times o
G=∂L∂Y ⁣:n×o\displaystyle G = \frac{\partial L}{\partial Y}\!: n \times o

nn rows of input (tokens), ii features in, oo out. The backward pass hands this layer GG, the gradient of the loss with respect to its output. It must produce gradients for WW and for XX.

∂L/∂W\partial L/\partial W: only one product fits

∂L∂W ⁣:i×ofrom X (n×i) and G (n×o)\displaystyle \frac{\partial L}{\partial W}\!: i \times o \quad \text{from } X\ (n \times i) \text{ and } G\ (n \times o)
∂L∂W=X⊤G(i×n)(n×o)\displaystyle \frac{\partial L}{\partial W} = X^\top G \quad (i \times n)(n \times o)

The answer must be shaped like WW, and it can only be built from XX and GG. The only way to multiply them into i×oi \times o is X⊤GX^\top G, summing over the nn rows: every token contributes.

Write

∂L/∂W=X⊤G\partial L/\partial W = X^\top G.

∂L/∂X\partial L/\partial X: same trick

∂L∂X ⁣:n×ifrom G (n×o) and W (i×o)\displaystyle \frac{\partial L}{\partial X}\!: n \times i \quad \text{from } G\ (n \times o) \text{ and } W\ (i \times o)
∂L∂X=G W⊤(n×o)(o×i)\displaystyle \frac{\partial L}{\partial X} = G\, W^\top \quad (n \times o)(o \times i)

Shaped like XX, built from GG and WW: GW⊤GW^\top. It’s the vector–Jacobian product of the last deck, one row per token. Shape matching is the fastest way to get these right, and to catch a missing transpose.

Write

∂L/∂X=GW⊤\partial L/\partial X = GW^\top.

On numbers

X⊤G=X^\top G =GW⊤=\quad GW^\top =

Check one entry by hand: W21W_{21} multiplies Xn2X_{n2} into Yn1Y_{n1} for each row nn, so its gradient is X12G11+X22G21=2⋅1+1⋅0=2X_{12}G_{11} + X_{22}G_{21} = 2 \cdot 1 + 1 \cdot 0 = 2. That’s entry (2,1)(2, 1) of X⊤GX^\top G.

The bias

Y=XW+b  ⇒  ∂L∂b=∑nGn,:\displaystyle Y = XW + b \;\Rightarrow\; \frac{\partial L}{\partial b} = \textstyle\sum_n G_{n,:}

The bias was broadcast to every row (Part 4), so its gradient adds up the rows of GG: here [1,1,1][1, 1, 1]. Broadcasting forward means summing backward.

Write

∂L/∂b\partial L/\partial b = sum of GG’s rows. Broadcast forward ⇒ sum backward.

Backward costs twice the forward

forward: XW    (2nio)\displaystyle \text{forward: } XW \;\; (2nio)
backward: X⊤G  (2nio)+GW⊤  (2nio)\displaystyle \text{backward: } X^\top G \;(2nio) + GW^\top \;(2nio)

Each matrix product in the forward pass becomes two of the same size in the backward pass. So training costs about three forward passes: 2N+4N=6N2N + 4N = 6N FLOPs per token (next deck but one).

✏️ Your turn

Shapes for a real layer

X ⁣:(32,4096),W ⁣:(4096,14336)\displaystyle X\!: (32, 4096), \quad W\!: (4096, 14336)

Write the shapes of ∂L/∂W\partial L/\partial W and ∂L/∂X\partial L/\partial X.

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

Answer

∂L/∂W ⁣:(4096,14336)=(4096×32)(32×14336)\displaystyle \partial L/\partial W\!: (4096, 14336) = (4096 \times 32)(32 \times 14336)
∂L/∂X ⁣:(32,4096)=(32×14336)(14336×4096)\displaystyle \partial L/\partial X\!: (32, 4096) = (32 \times 14336)(14336 \times 4096)

In nn.Linear's layout

# y = x @ W.T with W stored (out, in); g = dL/dy
grad_W = g.T @ x          # (out, in), same as W
grad_x = g @ W            # (n, in)

The same rules, transposed to match how the weight is stored.

📌 Recap

A linear layer backwards in one card

  • Y=XWY = XW, G=∂L/∂YG = \partial L/\partial Y.
  • ∂L/∂W=X⊤G\partial L/\partial W = X^\top G; ∂L/∂X=GW⊤\partial L/\partial X = GW^\top: match the shapes.
  • Bias gradient: sum GG over rows.
  • Backward = two matmuls per forward matmul.

Write

The four lines above.