TinyLanguageBuilder / The Tiny Textbook

Chapter 3: Attention

An embedding looks at one token alone. But the meaning of a word depends on the words before it. Attention is the step where tokens look back at each other and pass information along. This chapter opens up the real attention layers of a model you train here.

1. Tokens need to talk to each other

In "Lily gave the dog a ball. She smiled", the word She is only useful if the model can find Lily earlier in the text. Looking up an embedding cannot do that, because each row of the table knows nothing about its neighbours. Attention is the mechanism that lets every token look at the tokens before it and blend in what it finds.

The key word is before. The model is trained to guess the next token, so a token may never look at the tokens after it, or it could just copy the answer. This rule is called a causal mask, and you will see it as the empty triangle in the picture below.

2. Query, key, value

Each token gets three short vectors, made from its embedding by three learned tables of numbers:

To decide where token i looks, take its query and compare it with the key of every earlier token (including itself) using a dot product. A big score means a good match. The scores are divided by the square root of the vector length to keep them moderate, then a softmax turns them into weights that are positive and add up to exactly 1. Finally the token takes a weighted average of the values. That average is what attention gives back.

Below, the real numbers from a real model. First train one; an untrained model attends almost evenly.

3. Get a model

Loading the engine…

Which model to look inside

4. Who looks at whom

Each row is a token that is looking; each column is a token it can look at. A row's weights add up to 1. Click a row to see its numbers below.

5. One token, step by step

Looks atScoreescore − topWeightBar
Total

Softmax in three moves: raise e to each score (so everything is positive and bigger scores pull ahead), add them up, and divide each by the total (so the weights sum to 1). The middle column measures every score from the largest one, which keeps the numbers small and leaves the weights exactly the same.

6. What is each head doing?

For your text above, the average share of attention each head gives to a few simple targets (skipping the first three tokens, which have almost nothing to look at):

HeadItselfPrevious tokenFirst tokenAll others

In the runs I looked at (1,500 to 3,000 steps), both layer-1 heads ended up giving roughly a quarter to a third of their attention to the previous token, against about a tenth for an even spread, while the layer-2 heads stayed more diffuse. Your run may differ a little, which is the point of measuring it yourself.

7. What to take from this, and what not to

Several heads, several habits. Each layer runs a few attention heads side by side, each with its own query, key and value tables, and then combines them. Training is free to let different heads specialise: one may mostly look one step back, another may spread out. Compare the rows of the table above before and after training.

Layers stack. The second layer looks at tokens that the first layer has already mixed, so it can combine information in ways one layer cannot.

A weight is not an explanation. A high weight shows where information can flow, not why the model answered as it did. And this model is tiny: it has 2 layers and 2 heads, trained for a minute on children's stories, so don't expect it to resolve "She" to "Lily". The mechanism is identical in far bigger models; only the scale differs.

Next chapter: how the model learns, meaning loss, gradients and the small steps that make all of this work.