Attention and the transformer

Attention lets each word in a sentence look at the other words and decide which ones matter. A transformer is a model built around this idea.

In an AI chatbot you type "The animal didn't cross the street because it was too tired" and ask what "it" refers to. It answers the animal. You change tired to wide and ask again. This time it answers the street.

The words you typedOne sentence
The language modelProcesses the sentence
Its answerThe animal or the street
Think first. One word changed and the answer changed. What must the chatbot have done with the other words in the sentence to produce a different answer?
Ask students what they looked at in the sentence to decide. In both cases they looked at the last word, and the widget shows the model doing the same.

Each bar is a weight. Click a highlighted word, or switch the last word between tired and wide, and watch where it points. The weights are set by hand to show the idea.

More detail

What the word needs and what the others contain

The weight is a similarity score between a query and a key. In a real model, both are positions in the space described in Vectors and similarity, and the match is a distance calculated fresh for each sentence. The short descriptions in quotes are our own words for what those positions encode. The model holds positions and holds no sentences about meaning.

Every token is compared with every other token, so the number of comparisons is the square of the length. Move the slider and watch the comparisons grow.

Comparisons
—
Compared with 1k
—

Older recurrent models compressed everything read so far into one fixed-size hidden state, and early words faded. A transformer keeps every word, so word 1 and word 5,000 are one step apart. All the comparisons happen at once, so training can be spread across many chips.

You can now say what attention does and why it replaced the single hidden state.

More detail

The older approach

Recurrent networks showed the single hidden state that was rewritten after each word, where animal had faded by the time the sentence needed it. Attention keeps every word and lets each one look at all the others directly.

Where the name comes from

The T in GPT stands for transformer, and GPT stands for Generative Pre-trained Transformer. The 2017 paper that introduced the design was titled Attention Is All You Need.

Doubling the prompt

Doubling the input roughly quadruples the comparisons, so the cost of long prompts rises faster than the number of tokens.