Topic 1 of 5 · 3 min read
Why tokens need context from others
Goal: Explain with an example why a token's meaning depends on earlier tokens and why recurrent networks struggled with long-range links.
A token's meaning depends on the tokens around it, so a language model needs a way to pull information from other positions into each position. Attention is that mechanism.
Check yourself · 1 of 3
In "The backup job slowed the server because it was copying two terabytes of files", what decides what "it" refers to?
Why it matters
Take the sentence: "The server crashed because it ran out of memory." What does "it" point to? The server, not the memory. Now change one word: "The server crashed because it was unplugged." Still the server. Change the other words: "The backup job slowed the server because it was copying two terabytes of files." Now "it" means the job. The word "it" has no fixed meaning. Its meaning comes from the words earlier in the text, and sometimes far earlier.
A model that predicts the next token has to resolve links like this thousands of times per answer: pronouns, variable names defined 200 lines up, a constraint stated in the first paragraph of your prompt.
The old way and its limit
Before transformers, the standard tool was the recurrent neural network (RNN). It reads one token at a time and keeps a fixed-size summary vector, its "memory". At each step it mixes the new token into that summary.
Two problems follow:
- The bottleneck. Everything the model knows about the past must fit in one vector of fixed size. A long document gets squeezed the same as a short one, so early details fade.
- No parallelism. Step 10 needs the result of step 9. Training cannot spread a sentence across many GPU cores at once, which made big models slow to train.
In 2014 Bahdanau and colleagues added attention to RNN translation: instead of using only the final summary, the decoder could look back at every input position and weigh them. The 2017 transformer paper then dropped the recurrence and kept only attention.
The idea in one picture
Instead of passing a single summary forward, every token gets direct access to all earlier tokens and decides how much to read from each. The link between "it" and "server" is one step, however many words sit between them.
Before and after
Weak mental model: "The model reads left to right and remembers things, like a person." Better: "At every position the model re-reads the whole visible context and takes a weighted mix of it. Nothing is remembered except what is in the context window."
That second picture explains a lot of practical behaviour. Text that is not in the context cannot be attended to. Text that is there but buried can still get little weight.
What goes wrong
- Assuming memory. Engineers expect a model to remember a chat from yesterday. Without the text in the window, there is nothing to attend to.
- Assuming equal reading. All tokens are visible, but weights are uneven. A key instruction in the middle of a very long prompt may get less weight than the same instruction at the end.
- Confusing attention with understanding. Attention moves information around. It does not check that the link it made is right, so a model can resolve "it" to the wrong noun and sound confident.
Examples
Same sentence, different referent
Compare two sentences.
- "The backup job slowed the server because it was copying two terabytes of files."
- "The backup job slowed the server because it had only 4 GB of memory."
In the first, "it" means the job. In the second, it means the server. The token "it" is identical, so the difference comes entirely from other tokens. A model has to read them to decide.
A constraint lost in a long thread
You write in a long chat: "Never touch the billing/ folder." Two hundred messages later the agent edits a file there. With attention, the first message is still visible, but it competes with everything else for weight. The fix is practical: restate hard constraints near the task, or keep them in a short system prompt.
Key points
- A token has no fixed meaning: words like "it" or a variable name are resolved from the tokens before them.
- Recurrent networks squeezed the whole past into one fixed-size vector, so long-range details faded.
- Recurrent networks also had to read one token at a time, which made training slow to parallelise.
- Attention gives every token a direct, one-step link to every earlier token in the context.
- A model only attends to text that is in its context window; it has no other memory of the conversation.
Common mistakes
- Believing the model remembers earlier sessions when the text is no longer in the context window.
- Assuming every visible token is read equally, so a rule placed anywhere in a long prompt will be followed.
- Treating attention as proof of understanding, when it only routes information and can link the wrong tokens.
Check yourself · 2 of 3
What was the main limit of recurrent networks on long inputs?
Check yourself · 3 of 3
Why can a chat model ignore a rule that you gave it yesterday in another chat?
Practice items, not counted towards your mastery.
Start here
- The Illustrated Transformer (Jay Alammar) (opens in a new tab)ArticleFree
Step-by-step pictures of queries, keys, values, multi-head attention and the encoder-decoder stack.
- Transformer Explainer (Georgia Tech Polo Club) (opens in a new tab)ToolFree
Runs a small GPT-2 in your browser so you can watch attention weights change as you edit the prompt.
- 3Blue1Brown: Attention in transformers, step-by-step (opens in a new tab)VideoFree
A visual video lesson, with a written version, on queries, keys, values, masking and multi-head attention.
More for this topic
- Neural Machine Translation by Jointly Learning to Align and Translate (Bahdanau et al., 2014) (opens in a new tab)PaperFree
The first attention mechanism, introduced to remove the fixed-size summary bottleneck.