Understanding Transformers ยท Part 16 of 16
End-to-End Walkthrough and Cheat Sheet
One prompt, followed from text to next token โ and a one-page reference for everything in the series.
The full path: “The cat sat on the”
- Tokenize. The tokenizer splits the text into vocabulary tokens and returns integer IDs (Part 6).
- Embed. Each ID looks up a learned embedding vector; positional information is added or applied (Part 6).
- Enter block 1. Layer normalization prepares the vectors; causal self-attention lets each position use itself and earlier positions (Parts 8, 13).
- Mix information. Queries compare with Keys, softmax makes attention weights, and weighted Values update each token (Part 8).
- Transform locally. The feedforward network does nonlinear, position-wise computation (Part 11).
- Repeat. Residual connections carry the representation up through dozens of blocks, each building on the last (Parts 5, 12, 13).
- Produce logits. A final layer normalization is applied after the last block, and the last position's vector is projected to one score per vocabulary token (Part 10).
- Convert to probabilities. Softmax might give “mat” high probability, “floor” moderate, and unrelated tokens almost none (Part 10).
- Select and append. A decoding strategy picks a token; the model appends it and runs again, reusing the KV cache (Parts 14, 15).
The famous equation, unpacked
Attention(Q,K,V) = softmax(Q KT / √dk) V
Q: what each token is looking for. K: what each token can be matched on. QKT: every Query–Key compatibility score. √dk: a scale correction. softmax: normalized attention weights. V: the information being blended. It is matrix multiplication, dot-product similarity, scaling, normalization, and a weighted average — nothing more mystical than that.
Compact math reference
| Concept | Formula |
|---|---|
| Linear layer | y = Wx + b |
| Gradient update | w ← w − η · ∇L |
| Softmax | exp(zi) / Σj exp(zj) |
| Attention | softmax(QKT / √dk) V |
| Residual | x ← x + F(x) |
| Next-token loss | − log P(correct token) |
What you should now be able to explain
- Why next-token prediction can teach broad linguistic and factual competence.
- How vectors and matrices represent and transform information.
- How derivatives, backpropagation, and gradient descent train the weights.
- Why nonlinear activations and depth are necessary.
- How tokens become embeddings and pick up contextual meaning.
- How Queries, Keys, Values, softmax, and multi-head attention work.
- How residuals, normalization, and the FFN form a transformer block.
- How a prompt turns into logits and generates one token at a time.
Where to go next. With this foundation, the original “Attention Is All You Need” paper, an annotated transformer implementation, or a small PyTorch project that codes single-head attention by hand are all within reach. Later topics — RoPE, FlashAttention, mixture-of-experts, LoRA, quantization — are extensions of what's here.
Previous
← How GPT Trains and Generates
Next
References →