The transformer block and the running vector
An LLM stacks dozens of identical-shaped blocks. Each block has an attention step, which mixes information across positions, and a feed-forward step (a small MLP), which processes each token's vector on its own. Neither step replaces the token's vector. Each adds its result into a running per-token vector, the residual stream, which carries everything forward to the next block. A normalization step keeps the numbers in a stable range. After the last block the running vector is turned into next-token scores.
What the animation says
Dozens of rewrites?
A language model runs every token's vector through dozens of blocks, one after another, before predicting the next token. Early on, that vector carries details like which token this is and what came before it. Now imagine each block threw its input away and wrote a brand-new vector. That early detail would have to survive every single rewrite. Any one block could wipe it out. So what should a block do with the vector it receives?
The running vector
Right: it adds. Each token position keeps one running vector, the residual stream, like a shared notepad. Each block makes two edits. The attention step reads this position and earlier ones, mixing information across positions. The feed-forward step, a small MLP, works on each position alone. Each step reads a normalized copy, rescaled to a standard size, and adds its result back in. Here are four positions. Position three's attention is about to read.
Numbers: the attention edit
Let's push real numbers through one block, with just three dimensions. The running vector starts as one, zero, two. Attention gets a normalized copy. The root mean square is about one point two nine, so the copy is about point seven seven, zero, one point five five. Attention reads that copy, plus earlier positions, and returns point five, minus one, zero. What is the running vector now?
Numbers: the feed-forward edit
It's one point five, minus one, two. The attention result was added to the stream, not to the copy. Now the feed-forward step reads a fresh normalized copy of this new vector: about point nine six, minus point six four, one point two nine. It returns zero, point five, minus point five. Add that in, and the stream becomes one point five, minus point five, one point five. Only the copies were rescaled. The stream itself just piled up additions.
The same shape in code
Here's that structure as a toy stack in numpy: four positions, eight numbers each, three blocks. In this toy, attention is a fixed average over this and earlier positions, and every block reuses the same weights. Real models learn separate weights per block. This line is the attention step: normalize a copy, mix across positions, add back. This line is the feed-forward step: normalize a copy, run a small network on each row alone, add back. After every block, the shape is still four by eight.
From stream to prediction
After the last block, the stream gets one final normalization. Then it's multiplied by a learned matrix, W U, which turns d numbers into V scores, one per vocabulary token. Row t's scores are for the token at position t plus one: the next token. So, block after block: read normalized copies, add edits into the running vector, then read it out as scores.
A running vector that blocks add into
One block, and the final readout
One token through one block, d = 3
A toy stack of blocks in numpy
import numpy as np
T, d, L = 4, 8, 3
rng = np.random.default_rng(0)
def norm(x): return x / np.sqrt((x**2).mean(-1, keepdims=True) + 1e-6)
def mix(x): # toy attention: average of this and earlier positions
w = np.tril(np.ones((T, T))); return (w / w.sum(1, keepdims=True)) @ x
W1, W2 = rng.normal(size=(d, 4*d))*0.1, rng.normal(size=(4*d, d))*0.1
r = rng.normal(size=(T, d)) # starting vector per token
for _ in range(L):
r = r + mix(norm(r)) # attention: across positions
r = r + np.maximum(norm(r) @ W1, 0) @ W2 # feed-forward: each row alone
print(r.shape) # (4, 8): unchanged by every block
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses