The decoder-only transformer, end to end
Assemble token ids → embeddings (+ positions) → L blocks → final norm → unembedding → logits, with shapes at each step. Explain why one forward pass yields T next-token distributions.
Builds on: The pre-norm transformer block, Unembedding: from hidden state to logits, Causal masking, Injecting order: absolute position embeddings
What the animation says
What comes out?
You hand the model five tokens: the, cat, sat, on, the. You know the parts already: embeddings, position vectors, a stack of blocks, and the unembedding. Today we connect them, and track the shape of the data at every step. One forward pass ends in next-token probabilities. So how many next-token distributions come out for these five tokens?
Rows travel side by side
Five. Each token becomes its own row, and that row keeps its identity all the way up. Blocks read the rows and add to them. They never merge or drop a row. Only attention mixes rows, and the causal mask lets a row see just its own position and earlier ones. So at the top, row two summarizes 'the cat', and it guesses token three. Your turn: drag the bracket to cover every token row three can see.
Shapes on the way in
Exactly, one to three. Now shapes: two sequences, five tokens each. The ids are two by five. Each id picks a row of the embedding table, sixty-four wide: two by five by sixty-four. Add the five position vectors. Broadcasting copies them to both sequences. Then four blocks, each adding attention and a feed-forward layer. The shape never changes. Finally, a final norm, then times the unembedding, sixty-four by one thousand. What comes out?
Logits: one distribution per row
Two by five by one thousand. The multiply only touches the last axis, so every row keeps its place. Why the final norm? Pre-norm blocks never normalize the main path, so its scale drifts. This norm fixes it first. The result is the logits: for each row, one score per vocabulary token. Softmax turns scores into probabilities. Tap the axis it runs over.
The whole pass in code
Ten distributions: two sequences times five positions. Here's the whole pass as toy code. This line embeds the ids and adds positions: two by five by sixty-four. The loop runs four toy blocks, each adding to the rows. The shape stays put. The last line norms and multiplies by the embedding table transposed, called weight tying. Out come logits, two by five by one thousand. One pass, and every position gets a prediction built only from its own prefix.
T rows travel side by side
The full pipeline with shapes
Tracing shapes: B=2, T=5
A toy end-to-end forward pass
import numpy as np
B,T,V,d,L = 2,5,1000,64,4
rng = np.random.default_rng(0)
W_E = rng.normal(0,.02,(V,d)); P = rng.normal(0,.02,(16,d))
norm = lambda r: r/np.sqrt((r**2).mean(-1,keepdims=True)+1e-6)
M = np.triu(np.full((T,T),-np.inf),1)
def attn(x): s=x@x.transpose(0,2,1)/np.sqrt(d)+M; a=np.exp(s-s.max(-1,keepdims=True)); return (a/a.sum(-1,keepdims=True))@x
ids = rng.integers(0,V,(B,T))
r = W_E[ids] + P[:T] # [B,T,d]
for _ in range(L): r = r+attn(norm(r)); r = r+np.maximum(norm(r),0)
z = norm(r) @ W_E.T # [B,T,V]
print(z.shape) # (2, 5, 1000)
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