All courses

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.

▶ Watch this lesson Start the whole course

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.

How do dozens of layers avoid losing information?

An LLM applies dozens of blocks in sequence, one after another, to every token's vector before predicting the next token. If each block *overwrote* its input, early details such as which token this was or what came before would have to survive dozens of rewrites, and any block could destroy them. So what does one block actually do to the vector it receives? And how does the final vector become a prediction?

A running vector that blocks add into

Each token position keeps one **running vector**, the **residual stream**, which acts like a shared notepad. Every block makes two edits: 1. **Attention step:** reads from this and earlier positions' vectors and mixes information across positions. 2. **Feed-forward step:** a small MLP that processes each position's vector on its own. Neither step erases the notepad. Each reads a **normalized copy** of the vector and *adds* its result in. Whatever no step changes passes straight through.

One block, and the final readout

Block $\ell$ takes in $r^{(\ell-1)}\in\mathbb{R}^{T\times d}$, which holds one row of $d$ numbers for each of the $T$ token positions: $$a = r^{(\ell-1)} + \text{Attn}(\text{Norm}(r^{(\ell-1)}))$$ $$r^{(\ell)} = a + \text{FF}(\text{Norm}(a))$$ - Attn (attention step) mixes rows; row $t$ uses only rows $\le t$. - FF (feed-forward step) acts on each row separately. - Norm rescales each row to a standard size. After the last block $L$: $z=\text{Norm}(r^{(L)})\,W_U$. Here $W_U\in\mathbb{R}^{d\times V}$ is a learned matrix and $V$ is the vocabulary size (the number of distinct tokens). Row $z_t\in\mathbb{R}^V$ holds one score per vocabulary token for position $t+1$.

One token through one block, d = 3

Start with $r=[1,0,2]$. **Norm copy:** RMS $=\sqrt{(1+0+4)/3}\approx1.29$, so the copy is $\approx[0.77,0,1.55]$. **Attention** reads that copy and earlier positions, then returns $[0.5,-1,0]$. Now $r=[1.5,-1,2]$. **FF** reads a normalized copy of the new $r$ and returns $[0,0.5,-0.5]$. Now $r=[1.5,-0.5,1.5]$. Only the copies were rescaled. The stream itself just accumulated additions.

A toy stack of blocks in numpy

This is a toy version. The attention step here is a fixed average over the current and earlier positions, and every block reuses the same matrices. In a real model each block has its own learned matrices. The point is the structure: normalize a copy, compute, add back. The shape stays $[T,d]$ after every block.
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

Each block reads normalized copies of the running per-token vector, the residual stream. Its attention step mixes across positions and its feed-forward step works on each position alone. Each step **adds** its result into the stream. After the last block, the stream is normalized and multiplied by the learned matrix $W_U$ to give one score per vocabulary token for the next token.

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.