Transformers from the ground up
The block, one matrix at a time
- Advanced
- 10 chapters
- 164 min read
What a transformer actually computes: token ids and embedding rows, byte-pair merges and tokenizer fertility, scaled dot-product attention by hand, multiple heads, the feed-forward block, residual streams and layer norm, positional information, the next-token objective, the key-value cache that makes generation affordable, and a capstone block whose causality you can test.
What You'll Learn
- Explain how text becomes token ids and embedding rows
- Read a merge table and measure tokenizer fertility
- Compute scaled dot-product attention and a causal mask by hand
- Split a model width into heads and concatenate them back
- Explain what the feed-forward block adds and where its parameters live
- Keep a residual stream stable with layer norm
- Compare absolute, relative and rotary position information
- Read a next-token cross-entropy loss and its perplexity
- Estimate a key-value cache and explain prefill versus decode
- Build and test a tiny causal attention block
Prerequisites
- Comfortable with Python functions, lists and dictionaries
- Basic linear algebra: vectors, matrices and dot products
- No ML framework is needed — every example is plain Python
Vocabulary
The terms the chapters assume, in plain words.
- attention
- A weighted blend of value rows, with weights decided by how well each query matches each key.
- attention map
- The table of scores showing how much each position attended to every other position.
- causal mask
- A rule that blocks each position from seeing later positions, so predictions can only use the past.
- dimension
- One axis of a list or grid of numbers; its length is how many values fit along that axis.
- distribution
- The pattern of how often each possible value appears, or how likely each one is.
- dot product
- Multiply matching numbers in two lists and add the results; a large total means the lists point the same way.
- embedding
- A learned list of numbers that stands for a token, with similar tokens placed near each other.
- GPU
- A processor built for many small calculations at once, which is why matrix work runs well on it.
- gradient
- A list of slopes, one per number, saying how much each nudge changes the result.
- hard pointer
- A choice that sends all the weight to one position, instead of spreading it across several.
- head
- One parallel slice of attention, with its own projections; a layer runs many so it can track different relations at once.
- key
- What a position advertises it can offer, compared against every query to decide who receives attention.
- key axis
- The direction in the key vectors whose width decides how far the scores are divided down.
- layer
- One stage of a model that reads the running vector, transforms it and passes it on.
- layer norm
- A per-position rescale that keeps the residual stream at a steady size before the next block.
- logits
- The raw scores a model produces before softmax turns them into probabilities.
- matrix
- A grid of numbers arranged in rows and columns, used to transform a list into another list.
- negative infinity
- A value smaller than every real number, used to mark a score that must never be chosen.
- one-hot
- A vector with a single one and zeros elsewhere, meaning all the weight sits on one choice.
- output projection
- The learned matrix applied after attention, which turns the mixed values back into the model's width.
- parameter
- A number inside a model that training adjusts, rather than an input it reads.
- positional encoding
- Extra numbers added to token embeddings so the model can tell which position each one came from.
- projection
- A learned matrix that turns a list of numbers into a different list, often shorter.
- query
- What a position is looking for, compared against every key to decide where to take information from.
- residual stream
- The running vector each block reads from and adds its output back into.
- retrieval layer
- A layer whose job is to pull specific stored information into the current position.
- saturating
- A score so large that the softmax gives almost all the weight to one position.
- scale
- The step that divides the scores by the square root of the key width, so they stay in a workable range.
- score matrix
- The grid holding every query's score against every key, one row per position.
- soft alignment
- Matching by degree rather than by a single choice, so several positions can contribute a little.
- softmax
- A rule that turns a list of scores into positive weights that sum to one.
- token
- A chunk of text — a word, part of a word, or a character — that the model treats as one unit.
- value
- The content a position passes on when attention selects it.
- variance
- The average squared distance of values from their average, so a spread-out set scores higher.
- vector
- An ordered list of numbers, where each position is one measured property.
- weighted sum
- Adding values together with different shares, where the shares come from the attention scores.
- weights
- The numbers a model multiplies inputs by before adding the results together.
Course Content
- 01Tokens and embeddingsNot started
A transformer never sees letters. It sees integer ids from a fixed vocabulary, mapped to vectors that move during training. Tokenisation is the first place a model can fail.
- 02Attention by handNot started
Attention is three projections and a weighted average. Every token asks a question, every token advertises what it knows, and a softmax decides who answers.
- 03Multi-head attentionNot started
One attention pattern per layer is not enough. Several heads, each with its own projections, look for different relations and are concatenated back into one vector.
- 04The feed-forward blockNot started
Between attention layers sits a two-layer network applied to each token separately. It holds most of the parameters and does much of the per-token computation.
- 05Residual streams and layer normNot started
Every sublayer writes into a shared residual stream. Layer norm keeps that stream at a workable scale, and the order of the two operations decides how deep a model can be trained.
- 06Positional informationNot started
Attention is permutation-invariant, so position has to be injected. Sinusoidal encodings, learned tables and rotary embeddings are three answers to the same problem.
- 07The training objectiveNot started
A language model is trained to predict the next token. One shifted copy of the sequence supplies every target, and cross-entropy turns logits into a loss.
- 08Tokenisation in depthNot started
A tokenizer is a learned merge table, not a rule about words. Read the merges, measure fertility, and see why a vocabulary change is a model change.
- 09KV cache and inferenceNot started
Generation is a loop of one-token forward passes. The cache keeps the keys and values that do not change, turning a cubic decode into a quadratic one and a memory problem.
- 10Capstone: a tiny attention blockNot started
One block, end to end: layer norm, causal multi-head attention, residual, layer norm, feed-forward, residual. Run it on three tokens and check that the first position cannot see the third.
