Skip to content
cd /learn/tiny-language-model

Step 2: How the model uses context

Inside a tiny language model · Step 2 of 4

How each character looks back at the characters before it, shown with real attention data from my Shakespeare model.

The big picture

Step 1 turned every character into a row of 384 numbers. This step is where the model reads those rows together. Each character looks back at the characters before it and pulls in what it needs from them. That looking back is called attention, and it's how the model uses context: the text that came before.

Swipe sideways to see the rest of this figure.

My model is built on Andrej Karpathy's nanoGPT, open source under the MIT license, and trained on his Tiny Shakespeare text from char-rnn. Every number and heatmap on this page comes from my own training run of that code.

Words to know

Context

The characters that came before the one being read. My model can see up to 256 of them.

Position row

A second learned row of 384 numbers for each slot (1st, 2nd … 256th), added so the model knows the order.

Attention

Each character scoring the characters before it and blending in information from them.

Attention share

How much of a character's attention goes to one earlier character. A character's shares always add up to 100%.

Head

One independent copy of attention. Each layer has 6, and each can look for something different.

Layer

One round of attention plus a small per-character calculation. My model has 6 rounds, one after another.

The problem: one row per character

In Step 1, each character got exactly one row. So the 'e' in "Citize", the 'e' in "proce", and the 'e' in "procee" all start from the identical 384 numbers. Yet the right next character is different every time: n, e, then d. A model that only saw the 'e' could never get all three. It has to look at what came before.

The last line shows the model getting it wrong. After "Be" at the start of a line, it guessed a space (28%) and gave the real answer, f, only 5.8%. Context helps, but it doesn't make the model perfect.

The chart shows how sure the trained model is that k comes next, depending on how much of "…hear me spea" it's allowed to see. With only the 'a', it says k is 3.3% likely (its top guess is n, at 21.5%). Seeing "pea" jumps to 71.4%, and "spea" to 99.9%. More context, better guess. Attention is the machinery that makes that possible.

Knowing the order: position rows

Attention on its own has no sense of order. Without help, "dog bites man" and "man bites dog" would look like the same bag of characters. So before attention, the model adds a second row to each character: a position row for the slot it sits in. The character row says what the character is; the position row says where it is.

Swipe sideways to see the rest of this figure.

Adding means number by number: the 1st number of one row plus the 1st number of the other, and so on for all 384. There are 256 position rows, one for each slot in the window. Like the character rows, they start random and get nudged during training.

Blue means two position rows point the same way, amber means opposite. The bright diagonal is each row compared with itself.

The line chart reads across by how many slots apart two positions are (1 = neighbors, 200 = far apart), and up by how alike their rows are, using all 384 numbers. At step 0, every pair scores about zero: the slots are unrelated. After training, neighbors score 0.628, slots 20 apart still score 0.236, and slots 100 or more apart drift negative. Nobody told the model that slot 5 is next to slot 6; it learned that nearby slots should look alike.

Attention: ask, score, blend

Here's attention for one character in one head, with real shares. Every character does three things:

  1. Ask. From its own row, it makes a question, roughly "what am I looking for?" Every earlier character also makes a label, roughly "here's what I've got."
  2. Score. It compares its question with every label. A good match gets a high score. The scores are turned into shares that add up to 100%.
  3. Blend. Each earlier character also offers a message, a set of numbers. The character takes in a mix of those messages in proportion to the shares, and adds that mix onto its own row.

In the picture, the 'k' at the end of "speak" gives 68% of its attention to the 's' that starts the word, 12% to the space before it, and 9% to 'p'. So after this head, the 'k' row carries information about where its word began.

Two things that are easy to get wrong. First, nobody writes these questions or labels by hand. How a row turns into a question, a label, or a message is set by learned numbers, nudged during training like everything else. Second, a share isn't a decision or an understanding. It's arithmetic: a weighted average where the weights came out of the scoring.

Attention only looks backward. A character can attend to itself and anything before it, never to what comes after. During training the whole passage is sitting there, so looking ahead would be cheating: the answer would be right next to the question. When the model writes new text, the future doesn't exist yet anyway. In the heatmaps below, that rule shows as an empty, hatched triangle.

Real attention: untrained vs trained

How to read a heatmap: each row is one character doing the looking (top to bottom through "To be, or not to be:"). Each column is a character it could look at. A bright cell means a big share. Every row adds up to 100%.

Normal training doesn't keep attention shares. So at steps 0, 2,500, and 5,000, I had the model read three fixed lines and recorded every head's shares as it went.

At step 0, the model splits its attention almost evenly over everything so far. That's why the top rows are brighter: the 2nd character has only 2 choices, so each gets about 50%, while the 20th character has 20, so each gets about 5%. Across all 36 heads, no share was more than 9 points away from an even split. After training, this head puts 99.8% of its attention on the character just before. It has become a "what was the previous character?" detector.

The chart measures focus: for each character, the biggest single share it gives, averaged over the First Citizen line ("First Citizen: Before we proceed any further, hear me speak.") and over a layer's 6 heads. At step 0, focus sits near 5%, an even spread. Training sharpened every layer, Layer 1 most of all (77.4%). Most of that sharpening was already there by step 2,500, the halfway point.

Why 6 heads

One head can only produce one set of shares per character. But a character might need several things at once: the letter just before it, where its word started, and what the line is about. So each layer runs 6 heads side by side, each with its own questions, labels, and messages. Nobody assigns them jobs. Each head drifts toward whatever helped the guesses during training.

Six heatmaps for the heads of Layer 1: heads 2 and 3 look one character back, heads 5 and 6 look two back, heads 1 and 4 split between one and two back.

A 2-by-3 grid of small triangular heatmaps on "To be, or not to be:" at step 5,000. Heads 2 and 3 show a bright line just left of the diagonal; heads 5 and 6 show it two cells left; heads 1 and 4 show both lines, fainter.

Head 1

Head 2

Head 3

Head 4

Head 5

Head 6

Layer 1 split the work. Heads 2 and 3 look at the previous character. Heads 5 and 6 look two characters back. Heads 1 and 4 split between the two. Together, after one round, each character's row knows roughly what the last couple of characters were, which is what spelling a word needs.

Deeper in, heads look farther back. Layer 4, Head 5 tends to look back to where the current word began: on the First Citizen line, 48% of its attention lands on the space before the current word or that word's first letter, against 8.5% for an even split. The 'k' example earlier came from this head.

Layer 6, Head 5 puts a big share on the very first character of the text: 72% on "To be, or not to be:". Researchers see the same habit in large models and call it an attention sink. The common explanation is that shares must always add up to 100%, so a head with nothing useful to find parks its attention on a harmless spot. That explanation comes from research on other models; my data shows the habit but can't prove the reason.

Share for each of 6 layers and 6 heads. Average share each head puts on the character just before. An even split over everything so far would give 6%. All 36 heads, measured on the First Citizen line; the brightest cell means 100%.
LayerHead 1Head 2Head 3Head 4Head 5Head 6
Layer 14595994002
Layer 25631675
Layer 31235161245
Layer 41213651323
Layer 517589127
Layer 6556745
Average share each head puts on the character just before. An even split over everything so far would give 6%.All 36 heads, measured on the First Citizen line; the brightest cell means 100%.All 36 heads, measured on the First Citizen line. At step 0 every head started out the same.

Every one of the 36 heads, scored on five measures, at step 0 and step 5,000.

HeadWhat it doesMeasured
Layer 1, Head 3Previous character99% 1 back
Layer 1, Head 2Previous character95% 1 back
Layer 1, Head 5Two characters back95% 2 back
Layer 4, Head 5Start of the current word48% (even: 8.5%)
Layer 2, Head 6First character27% (even: 4.4%)
Layer 2, Head 2Spread over everything93% spread

Heads get blurrier in later layers. That doesn't mean they're idle; their patterns just aren't simple position rules I can name from one line of text. These descriptions come from two short lines, so treat them as tendencies, not fixed rules.

This is where 384 from Step 1 pays off. Each head works with 64 numbers per character, and 6 heads × 64 = 384, so the results line up side by side into a full row again. More heads means more different things to look for at once, but each head gets fewer numbers to work with.

Six layers, stacked

One layer is attention followed by a small per-character processing step. In that step, each character's row goes through a calculation on its own: its 384 numbers are expanded to 1,536, passed through a simple filter, and squeezed back to 384. No looking at neighbors happens there. A useful way to think of it: attention gathers information, and the processing step works out what to do with it. Each layer adds its results onto the row instead of replacing it, so earlier findings carry forward. My model does this 6 times.

This tracks the 10 'e's in the First Citizen line, comparing their rows to each other (1 means identical). They start identical, and the position rows already make them a bit different (0.758). After the first layer of attention they drop to 0.316, and after all 6 they're down to 0.175. The rows stopped meaning just "e" and started meaning "this e, in this spot, after these characters." This is how the fuzzy kitten problem from Step 1 gets solved in larger models: the surrounding words reshape each row.

Swipe sideways to see the rest of this figure.

Attention gets the spotlight, but two-thirds of the model's 10.65 million learned numbers are in the per-character processing steps. The 98,304 position numbers come on top of that count, which nanoGPT leaves out when it reports the total.

What this step can and can't do

Can

  • Use up to 256 earlier characters, about 50 words, for every guess.
  • Spot patterns such as the previous letters, where a word started, and the start of the text.
  • Give the same character different rows depending on what came before.

Can't

  • See anything more than 256 characters back, or anything ahead.
  • Learn on its own: the shares come from numbers set during training.
  • Know anything outside Shakespeare, so security fuzzing still means nothing to it.
  • Write text: it only produces a share for each of the 65 characters.

Next: Step 3, how training sets the numbers.