Zain Dana Harpernight school / L10

Attention. How a language model decides what matters.

A word in a sentence often needs another word from much earlier. Attention is how a transformer goes and gets it. Tallow shows you in his lamp room, and then you train one yourself.

The film

About 6:19, hosted by Tallow. It asks you two questions and waits for your answer before it shows you. The narrated cut runs 5:08, voiced by Tallow in a synthesized voice made from the author's own voice; the author approved these cuts for publishing. Captions are on by default. The live version under it draws the same film in your browser, waits for your answer at each question, and runs a little longer; the full words are also below.

Nothing loads until you press play.

The lab: Tallow's lamp room

Train the same kind of model the film uses: 2 layers, 2 heads each, 20,864 numbers, starting from random. It learns from sentences about the coast, such as "rerun found a kettle . pylon put a cone on the", where the last word can only be guessed by looking back. Pick a head to light the sentence with, change the test sentence, or turn the lamp off and train again.

The film's own runs of 2,000 steps filled 99% of the copy holes with attention on, 17% when each word could see only itself, and 64% with even light and no choosing. Your run is a new one, so its numbers will differ a little.

The model trains on your computer, in this page. Nothing is sent anywhere.

A circuit, switched off

A second, short film: a different small model, trained on a computer's main processor to continue random strings with one repeated stretch. One head in its first layer moves each character one place forward; a head in its second layer finds the earlier copy and reads what came next. Switch the heads off and the model loses the repeat. That is a causal test, which a picture of attention weights alone is not.

About 73 seconds, with captions. The narration is a synthesized voice made from the author's own voice, and the author listened to this cut before it was published. Every number on screen comes from the model's data file in raw-native.

Everything the film says

Attention

Attention. How a language model decides what matters.

I keep the lamp at Night School. Tonight the lamp is the lesson.

A hole at the end

Here is a sentence with a hole at the end.

Rerun found a kettle. Pylon put a cone on the...

What word goes in the hole? (the film pauses here)

You said kettle. And you did not read the sentence again. You glanced back, past the full stop, to one word near the start, and took it.

The old way: a whisper down the line

A language model like this one does one job. It reads the words so far and guesses the next one.

The first neural translation models squeezed the whole sentence into one fixed-length list of numbers, and worked from that.

Think of a message whispered down a line of people. By the end of a long line, the message has worn thin.

In 2014, Bahdanau, Cho and Bengio let a translation model look at every word of the sentence while it wrote each new word, and weigh how much each one counts. They called it an alignment model. Today we call the idea attention.

Question, label, cargo

Here is how the looking works. Every word does three things.

It asks a question. It holds up a label. And it carries some cargo.

The word at the end shines its question back along the sentence. Each earlier word answers with its label.

A good match gets a high score. The scores are turned into shares that add up to one hundred percent.

Then the word takes a blend of the cargo of every word so far, itself included, in those shares. That blend is what it carries forward.

Inside the lamp

Let's go inside the lamp.

Every word here is a list of 32 numbers. One dot is one number.

A match is a dot product: multiply the two lists number by number, and add it all up.

Each head works on a shorter list of its own, 16 numbers here, and the score is divided by the square root of that length. Without it, long lists give huge scores, the shares go all or nothing, and the model learns slowly.

This is a real model, trained on sentences from our coast. It has 20,864 numbers in total, and nothing in it was set by hand.

Which earlier word will get most of the light? (the film pauses here)

On this sentence, the word at the end gives more than 99 percent of its light to kettle.

One beam, several heads

One beam asks one kind of question. A transformer runs several at once, and calls each one a head.

The base transformer in the 2017 paper had eight heads in every attention layer. The larger one had sixteen.

Ours has 2 heads in each of 2 layers. Nobody told them what to look for.

After training, a head in the second layer puts 71 percent of its light on the word to be copied.

In small attention-only models, researchers at Anthropic found pairs of heads, in different layers, that work together to copy. The first writes into each word a note about the word before it. The second uses that note to find an earlier copy of the current word, and reads the word that followed it. They called them induction heads.

Turn the lamp off

Here is the test that matters. We trained the same model again with the lamp turned off, so each word could see only itself.

With attention, it filled the copy holes correctly 99 percent of the time.

With each word seeing only itself, 17 percent.

Then one more run. Every word got an even share of light over everything before it, with no choosing. That run reached 64 percent.

Knowing which words came before helps. Choosing where to look is what finishes the job.

What the light costs

There's a bill. Every word compares itself with every word before it.

In each head, a thousand words means about half a million comparisons. Double the text, and the attention work goes up about four times.

That's why people build special machinery to do this fast. We take it apart in the cannery, in a later lesson.

What the light does not tell you

One warning before you go.

The light shows where a head is reading from. It does not, on its own, tell you why the model answered what it did.

In 2019, researchers showed that in some text classifiers, very different attention patterns can give almost the same answer. Others replied that it depends on what you mean by explain, and proposed tests. Treat the light as a clue, and check it.

Your turn

The lamp room is open below this film. Train the model yourself, and watch the heads learn where to look.

My voice is synthesized, made from the author's voice.

Sources

Every claim in the film, with the paper it comes from and what that paper does not prove. The numbers about our model come from its weights file, which you can load and check: model.json and facts.json.

What this lesson leaves out

The model here is tiny: two layers on a made-up language of 46 words, so it can train in your browser in about a minute. Large models have dozens of layers and many heads, and their heads do jobs that are much harder to name. Where a head puts its light tells you where it reads from; on its own it does not tell you why the model answered as it did.

Next along the coast: the transformer block that attention sits in (L11), and attention rebuilt as one fast trip through the GPU (K23). Both are planned, not made yet.