Level 5 — Attention and the Transformer
About 7 h · + optional Python ~0.9 h
If an Implement Lab in this level stalls, review P3 — loops and list comprehensions or P7 — tensor axes and shapes.
Before L5.7: If multi-axis tensor shapes are unfamiliar, use optional P7 — Tensor Shapes for Transformers and its CPU notebook to trace the real self-attention code.
Level 4 turned text into token representations. Level 5 asks what happens when one token needs information from other tokens in the sequence.
Start with one sentence
Consider:
the cat chased the toy because it rolled
When the model is processing it, some earlier words are more useful than others. toy may be especially relevant. Treating every earlier position exactly the same would throw away that difference.
Attention is a way for a model to build a new representation by mixing information from other allowed positions with different weights.
A simple picture is:
possible context pieces
↓
assign larger or smaller weights
↓
combine their information
↓
new representation for this position
The weights are not permanently fixed. They depend on the representations being compared, so different positions can gather different information.
That is the main idea. The rest of this Level explains how those weights are calculated, how attention becomes a Transformer block, and how the same core machinery is arranged into encoder-only, decoder-only, and encoder-decoder families.
A few words you will meet
| Term | Plain meaning in this Level |
|---|---|
| attention score | a number used to compare how strongly one position should consider another |
| attention weight | the normalized amount of influence assigned to a position; a row of weights usually sums to 1 |
| query | the representation asking “what information is useful for me?” |
| key | the representation used to decide how well another position matches that request |
| value | the information that is actually mixed after the weights are chosen |
| causal mask | a rule that blocks a position from using future tokens when future information is not allowed |
| attention head | one attention calculation with its own learned projections; multiple heads let the model form several mixtures in parallel |
| residual connection | adding a block's input back to its transformed output so the original stream is preserved while new information is added |
| Transformer block | a repeated unit that combines attention with additional per-position processing and normalization |
| encoder-only Transformer | a family that contextualizes a provided input and returns representations rather than autoregressively generating a target sequence |
| decoder-only Transformer | a family whose target self-attention is causal so generation can extend a visible prefix one token at a time |
| encoder-decoder Transformer | a family with a source encoder and a target decoder that can attend to the encoded source |
You will not need all of these at once. The lessons introduce them in order.
Learning goal
Build attention and a Transformer block from pieces you can inspect, distinguish the three major Transformer families, and assemble a small decoder-style Transformer for the next-token path used in Level 6.
By the end of this Level, you should be able to:
- explain why attention is useful when different positions need different context;
- explain the distinct query, key, and value roles;
- compute a small dot-product attention row by hand;
- explain why very large comparison scores are scaled before softmax;
- show that blocked future positions receive zero attention probability;
- split and recombine multiple attention heads without changing the overall sequence representation size;
- explain what residual connections, layer normalization, and the feed-forward part each contribute;
- trace one Transformer block from input to output;
- distinguish encoder-only, decoder-only, and encoder-decoder Transformers by job, context access, and output;
- explain masked-token prediction versus next-token prediction at a beginner level without treating either objective as the definition of a whole family;
- stack decoder-style blocks while preserving shape and the rule against future target access;
- assemble a mini decoder-style Transformer that produces one raw vocabulary score per possible next token.
When this Level uses the word invariant, it means a property that should stay true while other things change. For example, after attention weights are normalized, each valid row should still sum to 1. If an invariant breaks, it gives you a precise place to debug.
The learning path
L5.1 — Why Attention begins with the weighted-mixture idea using ordinary words and small numbers.
L5.2–L5.7 build the scoring and mixing process: words looking at words, query/key/value roles, dot products, scaling, attention weights, and self-attention.
L5.8 — Causal Masking adds the rule that a next-token model must not use future positions. After this lesson, complete the Level 5 mini checkpoint.
L5.9–L5.13 build multi-head attention and then add residual connections, normalization, feed-forward computation, and one complete reusable Transformer block.
L5.14 — Transformer Families compares encoder-only, decoder-only, and encoder-decoder architectures through the jobs they perform, the context they can use, and the objectives commonly paired with them.
L5.15–L5.16 intentionally return to the decoder-only branch, stack causal blocks, and assemble the mini Transformer that Level 6 trains for next-token prediction.
How to debug attention
A full Transformer can feel complicated because several small calculations are connected. Debug from the smallest visible boundary outward:
shapes
→ comparison scores
→ scaling
→ mask
→ normalized weights
→ weighted values
→ multiple heads
→ residual stream
For example, if a future token receives a non-zero weight in a causal model, stop there. There is no reason to inspect the final language-model loss until the mask behavior is correct.
Level Project
After L5.16 — Mini Transformer Integration Workshop, complete Build a Mini Transformer.
The project intentionally follows the decoder-only branch. It combines causal multi-head attention, the residual/normalization/feed-forward block, shape preservation, and one intentional failure/debug path. The goal is to make each important boundary explainable rather than treating the Transformer as one large formula.
Completion is stored locally on this device.