Transformers
Paying attention…
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Transformers
Paying attention…
One idea — let every token decide which other tokens to read from — and a few pieces that make it trainable. Every page here runs on the same small GPT, so every number you see is the real one.
The seq2seq decoder had one vector to remember a whole sentence. Give it every encoder state and a way to choose, and the same toy translator goes from 44% to 100% of words right on long sentences.
c_t = Σᵢ αₜᵢ hᵢSequence-to-SequenceAlignmentDrag a query arrow among four keys and watch the dot products, the softmax and the blended value move with it. Attention is a soft dictionary lookup, and here it fits on one plane.
out = Σⱼ softmax(q · kⱼ) vⱼWhy AttentionSoft lookupRun a sentence through a small GPT trained for this site and follow one real head: the scores, the softmax and the blend that let "She" find the name it refers to.
softmax(QKᵀ / √d) VQuery, Key and ValueAttention headThe whole computation as four matrix steps with their shapes: QKᵀ, ÷√d, mask, softmax, ×V. Then raise the head size and watch softmax saturate when the √d is left out.
Attention(Q, K, V) = softmax(QKᵀ / √dₖ) VSelf-AttentionMatrix formWhy a GPT may only look back. Remove the triangle of −∞ and every earlier prediction changes when you edit a later word — the model would be reading the answer it is meant to predict.
Sᵢⱼ = −∞ for j > iScaled Dot-Product AttentionMaskingSplit 64 numbers into four heads of 16, let each attend on its own, join them and mix with W_O. The tiny GPT's real heads, side by side, and what the model loses when you switch one off.
MultiHead = concat(head₁, …, headₕ) W_OCausal MaskingParallel headsAttention ignores order until you add it. Sinusoids, the tiny GPT's learned positions, rotary embeddings, and what happens to its predictions when the positions are taken away.
PE(p, 2i) = sin(p / 10000^(2i/d))Multi-Head AttentionOrderAttention, add and norm, feed-forward, add and norm, with the shape [T, d] at every arrow. Watch the residual stream carry each token through the block and what attention and the FFN each add to it.
x + Attn(LN(x)), then x + FFN(LN(x))Positional EncodingResidual streamBERT reads both ways, GPT only looks back, T5 does both and joins them with cross-attention: the decoder's queries against the encoder's keys and values. Three mask patterns, one mechanism.
softmax(Q_dec K_encᵀ / √d) V_encThe Transformer BlockArchitecture familiesThe capstone: a full forward pass through a real 195,136-parameter GPT, from token ids to the next-word probabilities. Open any head or any feed-forward neuron and see what it does.
logits = LN(x_L) W_EᵀThe Transformer BlockFull forward passGPT, BERT, T5, Llama and Claude are all stacks of the same block. Its pieces — attention, masks, heads, positions, residuals — are few, and each one is small enough to watch working.
Every page runs the same 195,136-parameter GPT trained for this site. Take a piece out — the mask, the positions, a head, the √d — and the predictions change in ways you can measure.
Once a block makes sense, a language model is that block repeated, read out as next-word probabilities and sampled. The next section starts from exactly the model you take apart here.