ViewWiki

The world, a little closer

Large language models and attention.

A chatbot writes its answer a word or part of a word at a time, each a guess at what comes next. How does it decide which earlier words matter?

Explore the scene Interactive article

01 / The experiment

Look back. Weigh. Guess.

Pick a sentence and a token, follow the arcs back to the words it attends to, then step to the wheel and turn the temperature to see how the next token is chosen.

Still of the Large language models and attention 3D scene, which explores: How does a language model decide which earlier words matter for the next one?
Interactive scene

Loading the interactive version…

The explanation below is available while the scene loads.

One attention head of one layer, on three short English sentences of eight tokens each. The tokens after the attending one are hidden from it, as they are in the model.The attention weights and next-token probabilities are illustrative, made by hand to resemble what large models do; no real model computed them, and the token numbers and vectors are made up. The temperature arithmetic, the shares adding to 100% and the blend of vectors are computed exactly.

02 / Three ways to guess

How far back can it look?

Each diagram shows which words of “The keys to the cabinet in the hall” the method uses to guess the next word.

One word back
Thekeystothecabinetinthehallnext: “is”

Last word only

Guess from the word just before, as early phone keyboards did. Fast, but blind to anything further back.

Looks at
The last word
Reaches “keys”, six tokens back
No
Likely next word
is
Work for each new token
The same for any length
A fixed window
Thekeystothecabinetinthehallnext: “is”

The last few words

Count which word most often follows the last three in a huge pile of text. Better, but "keys" is still out of reach.

Looks at
The last three words
Reaches “keys”, six tokens back
No
Likely next word
is
Work for each new token
The same for any length
Attention
Thekeystothecabinetinthehallnext: “are”

Look back at everything

Compare the last token with every earlier one and take most from the best matches, however far back.

Looks at
Every earlier token, weighted
Reaches “keys”, six tokens back
Yes, with 62% of the attention
Likely next word
are
Work for each new token
Grows with the length
The big idea

Attention lets every token take what it needs from any earlier token, weighted by how well the two match. Stacked in many layers and heads, that is what lets a large language model keep track of what a sentence is about.

weights = softmax(q·k / √d)

A machine that guesses the next token

A large language model, the engine inside a chatbot, does one thing over and over: given some text, it gives a probability for every possible next piece of text. One piece is picked, added to the end, and the whole text goes back in to choose the piece after that. An answer of a hundred words takes well over a hundred of these steps.

The pieces are tokens. A tokenizer cuts text into a fixed vocabulary of tokens, usually some tens of thousands to a couple of hundred thousand of them. Common words are one token each, usually including the space in front of them; rarer words are split into pieces. A made-up word like zorblat splits into zor and blat. Each token in the vocabulary has a number, and the model never sees letters, only those numbers.

Each token number is then looked up in a table that gives it a long list of numbers, its vector: a few hundred to over ten thousand numbers in a large model. The Scene shows eight, as the column of lit cells behind each tile: blue for a positive number, amber for a negative one. The same token always starts with the same vector, which is why both copies of the in the first sentence have identical columns. Training has arranged these vectors so that tokens used in similar ways have similar vectors.

Try it: step Follow the attention to Tokens and read the tiles. The faint ␣ mark on a tile is the space the token carries, and the number under the word is its place in the vocabulary.

Back to the experiment

Attention: looking back at the words that matter

To guess what follows The keys to the cabinet in the hall, the model needs to know that the sentence is about keys. The nearest nouns, cabinet and hall, are the wrong ones: the next word is far more likely to be are than is. A model that only looked at the last word or two would get this wrong.

Attention is how a token gathers what it needs from the tokens before it. Every token makes three vectors from its own: a query (what am I looking for?), a key (what do I offer?) and a value (what I will pass on). The attending token compares its query with the key of every earlier token, and the closer the match, the higher the score. The scores are turned into shares that add up to 100% (the softmax), and the token takes that share of each earlier token’s value and adds the blend to its own vector.

In the Scene, the raised tile is the attending token. Each arc runs back to an earlier token and is as thick as the share of attention that token gets; the brass strip under the tiles prints the shares. The column of the attending token changes colour as the blend arrives: it now carries information from the words it looked at.

Try it: with Sentence on The keys… and Attending token slid up to 8, the last token, see hall send most of its attention past cabinet to keys. Then drag Attending token down one step at a time and watch the arcs regroup.

Back to the experiment

The smoked glass covers the tokens after the attending one. A language model that writes left to right may only look back: during training it must not peek at the very word it is learning to predict. This is called the causal mask.

One head, many jobs

The Scene shows one attention head. A real model runs many heads side by side in each layer, each with its own way of making queries, keys and values, so each learns to look for something different. The three sentences show three kinds of looking that researchers have found in trained models:

  • Agreement. In The keys to the cabinet in the hall, a head that tracks the subject lets the model choose are, which agrees with keys.
  • Recall. In The Eiffel Tower is in the city of, the last token gathers Eiffel and Tower. Attention only brings the clue together; the answer, Paris, comes from what the rest of the network learned about the world in training.
  • Copying. In My pet zorblat sleeps. The zor, no model has ever learned the word zorblat. The head finds the earlier zor and looks at the token that came after it, so the model can copy blat. Heads that do this are called induction heads, and they are one reason models can use names and terms they meet for the first time in a prompt.

The other positions matter too. During training the model guesses the next token at every position at once, and is scored on how much probability it gave the token that really came next. Early in a sentence it cannot know much: after The the real next word, keys, gets under 0.1%.

Turning probabilities into a choice

After the attention and the rest of the network have done their work, the model ends with a score for every token in its vocabulary. The softmax turns those scores into probabilities. In the Scene they are the slices of the wheel: a large slice for a likely token, and a grey hatched slice for the long tail of every other token, a thousand slivers too thin to draw one by one.

The model does not have to take the likeliest token. A sampler spins the wheel. The temperature reshapes it before the spin: every score is divided by the temperature before the softmax. Below 1 the big slices grow and the small ones shrink, so the model plays safe and repeats itself more. Above 1 the slices even out, and because the tail holds so many tokens, it soon takes over and the text turns to nonsense. Many chat systems sample at a temperature between about 0.5 and 1, often also cutting off the tail first.

Try it: step Follow the attention to Next token and slide Temperature down to 0.2, then up to 1.6. The pointer stays at the same place on the wheel, but the slices move under it, and the pick shown on the glowing tile changes.

Back to the experiment

The chart beside the Scene lists the probabilities, marks the real next word with a star and says how much probability it got. That number is what training improves. The average surprise at the real next token, measured in bits, is the model’s cross-entropy, the same quantity as in information and entropy.

From one head to a large language model

A transformer, the design introduced by researchers at Google in 2017 and used by almost every large language model since, stacks this step many times. Each layer has many attention heads followed by a small network that works on each token separately, and each layer adds its result to the token’s vector, so the vector gathers more and more context on its way up. GPT-3, from 2020, had 96 layers of 96 heads and 175 billion adjustable numbers, its parameters. Those numbers start random and are tuned by gradient descent, as in neural networks, over trillions of tokens of text.

The result is a model that is very good at continuing text in a plausible way. That is not the same as being right. A model that has learned what a confident answer sounds like can produce one that is fluent and false, which is called a hallucination. Chat assistants are further trained on examples of helpful conversations and on people’s ratings of their answers, but underneath they still pick one token at a time.

Attention has a cost. Every token compares itself with every token before it, so doubling the length of the text roughly quadruples that work. This is why a model has a context window, a limit on how much text it can look back over at once, and why making it longer is hard.

What this model leaves out

The weights, probabilities, token numbers and vectors in the Scene are made by hand to look like what a large model does; they were not computed by one. A real model has many heads in each of dozens of layers, and what any single head does is often less tidy than these examples. Real vectors have hundreds to thousands of numbers, not eight, and each head compares and blends transformed versions of them rather than the raw columns shown. The Scene leaves out the position of each token, which real models add to its vector so that word order counts. The long tail of the wheel is drawn as a thousand equally unlikely tokens, while in a real vocabulary of tens of thousands their chances vary widely, and real samplers usually cut the tail off before spinning. The sentences are in English; Hebrew text is usually cut into more, shorter tokens.

Reuse with credit to ViewWiki and a link to this page.

04 / Learn this in a course

Practise it, step by step.

This lesson teaches the topic with checked questions.

  1. Attention in a language model Computer science · Machine learning