Backprop through a linear layer
dX = dY Wᵀ, dW = Xᵀ dY, db = column sums: derived by shape and by entry, checked on the tiny GPT's own projection.
What it covers9
Why it matters
Every linear layer in a GPT, the three attention projections, the output projection, the two halves of the feed-forward layer and the output head, is . Backprop through it is three formulas, and they hold for any batch size: , , = the column sums of . In the tiny GPT those layers hold 79 percent of the parameters, and in production models the proportion is similar.
This lesson derives the formulas two ways, by the shapes they must have and entry by entry from the chain rule, then checks them against autograd on a 3 × 2 example and on the tiny GPT's own query-key-value projection, a 32 → 96 layer with 3,168 parameters. On the way you meet the transpose trap of PyTorch's weight layout and see why a parameter shared across rows collects a sum.
What you'll be able to do
- Write the three gradients of Y = XW + b, dX = dY Wᵀ, dW = Xᵀ dY and db = column sums, and justify them by shape
- Derive dW entry by entry and explain the sum over the batch
- Handle PyTorch's (out, in) weight layout, where weight.grad = dYᵀ X
- Check hand-written gradients against autograd on the tiny GPT's own projection
Show the answer
Xᵀ dY: (d_in × n)(n × d_out) = d_in × d_out
Only Xᵀ dY multiplies and lands on d_in × d_out, the shape of W. dYᵀ X is its transpose, the shape PyTorch stores; X dY does not multiply at all.
The idea: three formulas, by shape
This rests on Matrix multiplication, the shape rule, and on Backpropagation, where the gradient first went backward through a layer.
Three formulas, by shape
A linear layer computes : holds one input row per example or token (), is , is added to every row, and is . Backprop hands you , written , the same shape as , and asks for , and .
The shapes alone pin down the candidates. must be and can only be built from () and : the one product that fits is . must be , built from and : , with the dimension summed away. must be a vector of numbers built from : its column sums.
Shape reasoning finds the formulas; it does not prove them. The next cell checks all three against autograd on a 3 × 2 input, a 2 × 4 weight and a hand-picked , and the section after derives them entry by entry.
Hand formulas against autograd
A 3 × 2 input, a 2 × 4 weight, a bias, and a chosen dY. The loss (Y · dY).sum() has gradient exactly dY at Y.
import torch # 3 rows, 2 inputs each X = torch.tensor([[1.0, 2.0], [0.0, 1.0], [3.0, -1.0]], requires_grad=True) # 2 in, 4 out W = torch.tensor([[1.0, 0.0, 2.0, -1.0], [0.5, 1.0, 0.0, 1.0]], requires_grad=True) b = torch.tensor([0.0, 1.0, -1.0, 0.5], requires_grad=True) Y = X @ W + b # the gradient arriving at Y dY = torch.tensor([[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0]]) (Y * dY).sum().backward() # a loss whose gradient with respect to Y is exactly dY print("Y:", Y) print("dX hand:", dY @ W.T); print("dX auto:", X.grad) print("dW hand:", X.T @ dY); print("dW auto:", W.grad) print("db hand:", dY.sum(0)); print("db auto:", b.grad)
Three pairs, three matches. Read dW's entries as sums down the rows of X and dY: dW[0][3] = 1·1 + 0·0 + 3·1 = 4 and dW[1][2] = 2·0 + 1·0 + (−1)·1 = −1. db is dY's column sums, [1, 1, 1, 2]: the bias of output 3 was used by rows 0 and 2, so it collects both gradients. dX's first column is 0, 0, 1: each row of dY dotted with W's first row [1, 0, 2, −1], and for the first two rows the hits cancel or vanish (1 − 1, then 0). The hand versions print with a grad_fn because W and X track gradients; the numbers are what matter.
import torch X = torch.zeros(5, 3); W = torch.zeros(3, 7); dY = torch.ones(5, 7) print((dY @ W.T).shape, (X.T @ dY).shape, dY.sum(0).shape)
Show the answer
torch.Size([5, 3]) torch.Size([3, 7]) torch.Size([7])
dX has X's shape (5 × 3), dW has W's shape (3 × 7), db has b's shape (7). A gradient always has the shape of its variable.
Each entry of dW adds up, over the batch, the input times the gradient it helped cause.
Write a sentence or two, then compare it with a good answer in the lesson.
In PyTorch: the formulas as code
One entry, by the chain rule
Write out one output: . Each weight appears in for every row , multiplied by . The chain rule sums over every place a variable is used, so
That sum over is the batch sum: a parameter shared by every row collects gradient from every row, the same rule as the += in the autograd engine's backward functions, where a value used twice adds up both gradients.
For the input, appears in for every output , multiplied by : . For the bias, appears in for every row with coefficient 1: .
The next cell shows the batch sum happening: each row's contribution to is an outer product of its input and its gradient, and the three contributions add up to .
The batch sum, row by row
Each row's outer product X[n]ᵀ dY[n], then their sum, next to Xᵀ dY.
import torch X = torch.tensor([[1.0, 2.0], [0.0, 1.0], [3.0, -1.0]]) dY = torch.tensor([[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0]]) # row n's outer product: (2 x 1) @ (1 x 4) rows = [X[n:n + 1].T @ dY[n:n + 1] for n in range(3)] for n, r in enumerate(rows): print("row", n, r) print("sum of rows:", sum(rows)) print("X.T @ dY: ", X.T @ dY)
Row 0 contributes its input [1, 2] times its gradient [1, 0, 0, 1]: a rank-one block that touches only columns 0 and 3. Row 1's gradient is a single 1 at column 1, so it adds its input [0, 1] there. Row 2 adds [3, −1] to columns 2 and 3. Stacked, the three blocks give [[1, 0, 3, 4], [2, 1, −1, 1]], which is Xᵀ dY exactly. Doubling the batch doubles the number of blocks, which is why training averages the loss over the batch: the mean keeps the size of the gradient, and so of the step, independent of the batch size.
The three formulas as a function
Write linear_backward(X, W, dY) returning the tuple (dX, dW, db) for Y = XW + b, with X of shape (n, d_in), W of shape (d_in, d_out) and dY of shape (n, d_out).
import torch def linear_backward(X, W, dY): return X, W, dY[0] X = torch.tensor([[1.0, 2.0], [0.0, 1.0], [3.0, -1.0]]) W = torch.tensor([[1.0, 0.0, 2.0, -1.0], [0.5, 1.0, 0.0, 1.0]]) dY = torch.tensor([[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0]]) dX, dW, db = linear_backward(X, W, dY) print(dW, db)
The transpose trap
PyTorch's Linear module stores its weight the other way around: shape (out, in), and computes x @ weight.T + bias. In that layout the gradient is the transpose of ours: weight.grad = , shape (out, in). The formula did not change; the bookkeeping did, and mixing the two layouts is the most common way to get a shape error in a hand-written backward pass.
The tiny GPT's query-key-value projection is such a layer: Linear(32, 96), weight (96, 32), bias (96,). On the six-token prompt "Our Kestrel tent is made from", its input is 6 × 32 and its output 6 × 96. The next cell runs the layer on the real activations from the model's first block, picks a random , and compares , the column sums and (not , for the same layout reason) with what autograd stores in weight.grad, bias.grad and the input's grad.
The tiny GPT's query-key-value projection
Linear(32, 96) on the real activations of the first block. Hand gradients in PyTorch's layout against autograd, and the parameter count.
import torch from tinygpt import load_pretrained model, tok = load_pretrained("tiny-gpt") ids = torch.tensor([tok.encode("Our Kestrel tent is made from")]) blk = model.blocks[0] with torch.no_grad(): h = model.tok(ids) + model.pos(torch.arange(ids.shape[1])) X = blk.ln1(h)[0].detach() # what the projection receives: 6 tokens x 32 numbers X.requires_grad_(True) Y = blk.qkv(X) # Linear(32, 96): weight (96, 32), bias (96,) print("X", tuple(X.shape), " weight", tuple(blk.qkv.weight.shape), " Y", tuple(Y.shape)) torch.manual_seed(0) dY = torch.randn(*Y.shape) (Y * dY).sum().backward() print("weight: max |dY^T X - autograd| =", (dY.T @ X.detach() - blk.qkv.weight.grad).abs().max().item()) print("bias: max |column sums - autograd| =", (dY.sum(0) - blk.qkv.bias.grad).abs().max().item()) print("input: max |dY W - autograd| =", (dY @ blk.qkv.weight.detach() - X.grad).abs().max().item()) print("parameters in this layer:", blk.qkv.weight.numel() + blk.qkv.bias.numel())
Zero difference on all three, on the model's own numbers. The weight's gradient is dYᵀ X because of the (out, in) layout: 96 × 6 times 6 × 32 gives the 96 × 32 of the weight. The input's gradient is dY W rather than dY Wᵀ for the same reason. The bias collects the column sums over the six tokens. This one layer holds 3,168 of the model's 40,195 parameters, and the same three formulas serve the other eight linear layers; the last cell of the lesson counts them.
import torch print(torch.ones(4, 2).sum(0).tolist())
Show the answer
[4.0, 4.0]
Four rows, two outputs: each output's bias collects a 1 from every row, so db = [4, 4]. One number per output, summed over the batch.
Show the answer
dYᵀ X, shape (out, in): the transpose of Xᵀ dY
The layer computes X Wᵀ, so the role of W is played by Wᵀ; the gradient with respect to the stored matrix is the transpose of Xᵀ dY.
The weight gradient in PyTorch's layout
Write torch_weight_grad(X, dY): for a PyTorch Linear layer whose weight is stored as (out, in), return weight.grad from the input X (n, in) and the upstream gradient dY (n, out). It is the transpose of Xᵀ dY, computed directly.
import torch def torch_weight_grad(X, dY): return X.T @ dY X = torch.tensor([[1.0, 2.0], [0.0, 1.0], [3.0, -1.0]]) dY = torch.ones(3, 4) print(torch_weight_grad(X, dY).shape)
Write a sentence or two, then compare it with a good answer in the lesson.
In real models: where this runs in a GPT
Count the layers this lesson covers. Each block of the tiny GPT has four linear layers: the query-key-value projection (32 → 96), the output projection (32 → 32), and the two halves of the feed-forward layer (32 → 128 and 128 → 32). The output head is a ninth (32 → 195). Everything else, embeddings and layer norms, is a few thousand parameters. The next cell lists the linear layers from the model itself and adds up: they hold 31,587 of the 40,195 parameters, 79 percent.
Production models keep the proportion: attention projections, feed-forward layers and the output head are matrix products, and their backward passes are the three formulas with the same shapes scaled up. What the formulas do not cover is the softmax inside attention and at the output, where the Jacobian is not a weight matrix. That is The softmax Jacobian and p − y.
Every linear layer in the tiny GPT
The model's parameters, filtered to the linear layers, with the share they hold.
from tinygpt import load_pretrained model, tok = load_pretrained("tiny-gpt") linear_names = ("qkv", "proj", "mlp", "head") total = linear = 0 for name, p in model.named_parameters(): total += p.numel() if any(part in name for part in linear_names): linear += p.numel() print(f"{name:22s} {str(tuple(p.shape)):11s} {p.numel():6d}") print("linear layers:", linear, " all parameters:", total, f" share: {100 * linear / total:.0f}%")
Nine layers, eighteen parameter tensors, 31,587 numbers: 79 percent of the model. The largest is the output head's weight, 195 × 32 = 6,240, then the two feed-forward weights of each block at 4,096 apiece. The remaining 21 percent is the token embedding (195 × 32), the position embedding (64 × 32) and five layer norms of 64 parameters each. Every one of the eighteen tensors listed here gets its gradient from dYᵀ X or the column sums.
Show the answer
Each layer reuses the gradient from the layer above, so all gradients cost about one extra pass
The gradient at a layer's output is computed once and used for both dW and dX; dX becomes the next layer's upstream gradient. One pass, every gradient.
Show the answer
W is used by every row, and the chain rule adds up the gradient from every use
A shared parameter collects a contribution from each place it is used; the n rows each contribute an outer product, and the sum is Xᵀ dY.
Where this shows up in an engineer's week
On the job: gradient shape errors are the most common bug in hand-written layers, and the transpose trap is the usual cause. Check
weight.grad.shape == weight.shapefirst, then compare against autograd on a tiny input before you trust a custom backward. Also check that the loss is a mean over the batch, so gradients do not grow with batch size.
Goes deeper in
- Jacobians: derivatives with shapes: the shapes argument behind these three formulas.
- The softmax Jacobian and p − y: the next node back from the loss, and its gradient p minus y.
- Backprop through attention: these formulas applied to q, k and v inside attention.
Write a sentence or two, then compare it with a good answer in the lesson.
You can now
- Write dX = dY Wᵀ, dW = Xᵀ dY and db = column sums, and justify each by its shape.
- Derive dW entry by entry and explain why a shared parameter sums over the batch.
- Translate to PyTorch's (out, in) layout, where weight.grad = dYᵀ X.
- Check a hand-written gradient against autograd on the tiny GPT's own projection.
Next: the softmax Jacobian, and why cross-entropy's gradient is p − y.
Practice what you just read
- 4 code examples you can edit and run
- a practice exercise that checks your code
- a 5-question quiz
- 10 review cards for spaced repetition
Try a quiz question
For Y = XW + b with X of shape (n, d_in) and W of shape (d_in, d_out), the gradient with respect to X is:
Before this lesson
Go deeper, if you like
- Backprop through attention Optional
Where this lesson leads.
575 lessons in thirteen phases. 176 are free without an account, and a free account opens the rest. See every lesson.
Phase 5 · about 36 hours of study
Transformers
Attention, GPT from scratch and pretraining
47 lessons 1 free
- Language modeling FreePredict the next token, and score it with cross-entropy.30 min
- Tokenization and BPEHow tokenizers are built from data.40 min
- A bigram modelThe simplest language model, built from counts.35 min
- Sampling strategiesTemperature, top-k and top-p: turning probabilities into text.35 min
- Beam search and constrained decodingWhy the likeliest word isn't the likeliest sentence, beam search with log probabilities, its taste for short and repetitive text, and forcing output into a grammar: what that guarantees and what it doesn't.40 min
- Neural language modelsWhy counting fails, and what embeddings fix.35 min
- Attention: the intuitionEach token decides which other tokens matter to it.30 min
- Queries, keys and valuesThe three projections behind attention.35 min
- Scaled dot-product attentionThe attention formula, line by line.35 min
- Causal maskingStop tokens from seeing the future.25 min
- Multi-head attentionSeveral attention patterns in parallel.40 min
- Positional encodingsTell the model where each token sits.30 min
- The transformer blockAttention, MLP, residuals and norms together.40 min
- The GPT architectureA stack of decoder blocks that predicts the next token.40 min
- Build a tiny GPTWrite and train a small GPT from scratch.45 min
- From your GPT to GPT-2 in Hugging FaceMatch your GPT's tensors to GPT-2's names and shapes, count its 124,439,808 parameters, load renamed weights into your own class, pad a batch and read a config.40 min
- Training a GPT wellWarmup and cosine decay, gradient clipping, a validation loss you can trust, saving and loading, and two training mistakes read off the loss curve.35 min
- Encoders and embedding modelsBERT-style models and where they fit.40 min
- Tokenization: text to integer IDsFollow "The cat sat" into the three integers a model receives.30 min
- Embeddings and positional wavesFrom three IDs to the 3 × 8 matrix that enters the first block.35 min
- Inside the attention matrixEvery entry of the 3 × 3 attention matrix, computed and steered.40 min
- The feed-forward layerExpand to four times d_model and back: shapes, parameters and what the layer stores.35 min
- The LM head and samplingFrom the last row to a probability for every token, and the next word.35 min
- Pretraining at scaleData, compute and what models learn from the internet.40 min
- Scaling lawsHow loss falls predictably with size, data and compute.40 min
- Inference and the KV cacheWhy generation is fast after the first token.40 min
- The block in today's open models OptionalRMSNorm, SwiGLU, rotary positions and grouped-query attention swapped into the block you built, with the parameter and cache counts before and after.35 min
- Long contextEfficient attention and million-token windows.35 min
- Mixture of expertsRoute each token to a few specialist sub-networks.35 min
- Capstone: diagnose your own GPTEntropy per position, a head's attention pattern and a head ablation on the tiny GPT, then the same three measurements on a GPT you train yourself.45 min
- Jacobians: derivatives with shapes OptionalThe derivative of a vector function is a matrix; the chain rule multiplies them, and backprop multiplies by them without building them.35 min
- Backprop through a linear layer OptionaldX = dY Wᵀ, dW = Xᵀ dY, db = column sums: derived by shape and by entry, checked on the tiny GPT's own projection.35 min
- The softmax Jacobian and p − y OptionalEvery output depends on every input: the full Jacobian, its cheap vector product, and why cross-entropy's gradient is p − y.35 min
- Backprop through attention OptionalFive formulas from the output back to queries, keys and values; why masked scores get zero gradient; what FlashAttention recomputes.40 min
- Multimodal transformersHow models see images and hear audio.35 min
- Object detection: boxes, overlap and suppressionEvery object and where it is: boxes in two formats, intersection over union, why one object gets many boxes, non-maximum suppression and its threshold, and grading with average precision.45 min
- Segmentation: a label for every pixelMasks, and how to grade them with IoU and Dice; why a pixel's label needs context and too much context blurs; the shrink-then-enlarge network with skip connections; and turning a mask into countable objects.45 min
- Build a vision transformerPatch embeddings, a class token, position embeddings and an unmasked block, trained in the page; then two failures that explain it: no positions means no layout, and few images means losing to a CNN a fraction of its size.45 min
- Image-text models in depth: retrieval, negatives and limitsHow to test a shared image-text space with recall at k, what the rest of the batch teaches, what hard and false negatives do, and why a similarity score can't see word order, counts or "no".45 min
- Self-supervised learning: labels from the data itselfPretrain an encoder with no labels by matching two views of each image, test it with a linear probe, and see what the augmentations remove, what collapse looks like, and how filling in masked parts differs.45 min
- Diffusion 1: turning data into noiseThe easy half of a diffusion model: one step of noise, why the data is shrunk as noise is added, the jump to any step with alpha bar, how to read a noise schedule, and the training examples a denoiser learns from.40 min
- Diffusion 2: learning to denoise, and samplingTrain a network to predict noise, then run it a hundred times from pure noise and watch a ring assemble: the sampling loop, why fresh noise goes back in, fewer steps and where they break, and a check that samples are new.45 min
- Diffusion 3: images, labels and guidanceFrom points to small images, a label the denoiser can be asked with, and classifier-free guidance: why a model that is told what to draw still often doesn't, how the guidance scale fixes it, and what it breaks when turned up too far.45 min
- Diffusion 4: latent diffusion and the whole pipelineWhy image generators diffuse an autoencoder's codes and not pixels: a latent diffusion model in miniature, what the autoencoder must be trained to do, the three parts of a text-to-image system, and where the cost of an image goes.40 min
- Sound as numbers: waveforms and spectrogramsA recording as an array of samples, why the sampling rate must be twice the highest pitch, spectrograms and the window trade-off, and the log-mel features a speech model is given.40 min
- Recognition and synthesis as a pipelineA whole speech recognizer for a made-up language: features, a guess per frame, collapsing repeats and blanks, a dictionary to repair mistakes, word error rate, and synthesis as the same stages run backward.40 min
- The voice agent loop and its latency budgetOne turn of a voice agent, timed: why every stage must stream, how to decide that someone has finished speaking, what to do when they interrupt, and why the slow turns matter more than the average.45 min
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write