Transformers and attention: how models read text

Attention lets a language model weigh every earlier word when it reads the next one. How it works, what layers and heads add, and why long text costs more.

Popo
On this page

The T in GPT stands for transformer, a design Google researchers published in 2017 in a paper titled Attention Is All You Need. Nearly every well-known language model uses a version of it. Its central idea, attention, answers one question: when reading this word, which other words matter?

A word means nothing on its own

Google's announcement of the transformer used two pairs of sentences. In "I arrived at the bank after crossing the river", only "river" tells you which kind of bank. The other pair differs by a single word:

Last word “tired”: “it” is the animal

The animal didn't cross the street because it was too tired.

Last word “wide”: “it” is the street

The animal didn't cross the street because it was too wide.
Example from Google's 2017 post. Translating into French or German forces the choice, because “animal” and “street” have different genders there. At the time, the transformer got both right and Google Translate's system didn't.

You resolve "it" without noticing. A program has to connect a word to others that may be far away, and the connection depends on the meaning of the whole sentence.

What attention does

Earlier systems read in order, passing along a summary of what they'd read so far. By the end of a long sentence, the beginning had faded. Attention removes the chain: each token looks at the others directly.

  1. 1CompareThis token with every token before it
  2. 2ScoreHow relevant is each one here?
  3. 3WeighScores become shares that add up to 100%
  4. 4MixTake in information from each, by its share

Every token goes through this in every layer, all tokens at once.

In a model that generates text, a token can only look back at itself and the tokens before it, never ahead.

Each token is represented by a list of numbers, its embedding, which changes as it passes through the model. From it, the model computes three things, called the query, the key and the value. The query says what this token is looking for. The key says what a token has to offer. Where a query and a key match well, more of that token's value is mixed in.

How to produce queries, keys and values isn't programmed. It's learned in training, like everything else in the model.

Heads and layers

One comparison can't capture everything: grammar, what a pronoun refers to, the topic, the matching bracket. So attention is done several times in parallel, with different learned queries and keys. Each is called a head.

Then it's all repeated in layers. The first layers work with something close to the words themselves; later ones work with representations that have already absorbed their context.

ModelLayersAttention headsNumbers per token
Original (2017)6 + 68512
GPT-3 (2020)969612,288
Llama 3.1 70B (2024)80648,192
From the 2017 paper, the GPT-3 paper and Llama 3.1 70B's published configuration. The original was a translation model with 6 layers to read and 6 to write.

It's tempting to say one head handles pronouns and another handles grammar. The 2017 authors did find heads that appeared to specialize, but most don't have a role that can be described in a phrase, and a high attention score isn't proof that a word caused the output.

Why transformers took over

Because attention doesn't have to go through text in order, training can process all the tokens of a passage at the same time, which is what GPUs are good at. The original base model was trained in 12 hours on a single machine with eight GPUs, and the larger one took 3.5 days. The design scaled: the same idea with more layers, more numbers and far more text became today's models.

What it costs

Comparing every token with every earlier token means that twice as much text takes about four times as many comparisons. In practice models save the keys and values they've already computed, so they don't repeat the work for each new token, but those saved numbers take up memory. This is one reason that long conversations are expensive and context windows have a limit.

Order is a side issue attention doesn't solve: comparing tokens pairwise says nothing about which came first. Models add position information separately, otherwise "dog bites man" and "man bites dog" would look the same.