How a transformer works

Almost every AI chatbot uses one design: the transformer. Here, we build one, piece by piece, with one sentence.

The cat ate the fish so it was ___

The model must find the next word. To do that, it must know what "it" means: the cat, or the fish? Keep this question in mind. Each step below helps to answer it.

The parts of a transformer, from the words at the bottom to the next word at the top full 7 · score the next word ×N 6 feed-forward 5 · each word alone attention 3–4 · words share information 2 · vectors and positions 1 · all the words at once
Figure 1. The whole path, read from the bottom up. One block is attention, then feed-forward. A model repeats the block many times. Tap a part to go to its step.

1All the words at once

Older language models read a sentence one word at a time. By the end of a long text, the first words fade. A transformer reads all the words at the same time.

Older model
Transformer
Figure 2. The older model needs 8 steps for 8 words. The transformer needs 1.

Reading all the words at once is fast on modern chips. So a transformer can learn from a huge amount of text.

2Words become vectors

A computer can't calculate with words. So each word becomes a vector: a list of numbers. Words with similar meanings get similar vectors.

Tap a word to see its numbers.
Figure 3. Each column is one vector. A brighter cell is a larger number. All the words go in at the same time, so how does the model know their order?

The answer: each vector also gets its position in the sentence. Now the order is part of the numbers.

3Attention: words share information

This is the first part of a transformer block. Each word looks at the other words and takes in the information that it needs. For that, each word gets three vectors:

When a query matches a key, the score is high.

Choose the word that asks:
Figure 4. The scores of one word. A word looks only at itself and the words before it, because in a chatbot the next words don't exist yet. These numbers are made up for learning. A real model learns its own scores in training.

Choose it. Its query matches the key of cat best. So after attention, the vector of "it" also holds information about the cat.

4Many heads, side by side

A block does attention many times at the same time. Each copy is a head, and each head learns to look for a different kind of link. Nobody tells a head what to look for. It learns this in training.

Figure 5. Three heads, three different links from "it". These are examples. Real heads are often harder to explain.

5Feed-forward: each word alone

This is the second part of the block. Each vector goes through a small network, the feed-forward network. Each vector goes through it alone, with no sharing.

Figure 6. Compare this with Figure 4. In attention, the words share information. In feed-forward, each word stays in its own lane.

So the two parts do different jobs. Attention shares information between the words. Feed-forward uses what the model learned in training. Together, they make one transformer block.

6Stack the blocks

A large model stacks dozens of blocks, one on top of another. Each block adds more context to every vector. Move the slider to add blocks.

What the vector of "it" holds
Probability that the next word is "full":
Figure 7. A simple picture. A real vector doesn't hold sentences, only numbers. But with more blocks, the numbers hold more of the context.

7Score the next word

At the top of the stack, the last vector gives a score to every word that the model knows. The scores become probabilities.

The cat ate the fish so it was ___
Figure 8. The probabilities of the next word. "full" wins, because the model knows that "it" means the cat.

Then the model adds the word to the text and does all the steps again for the next word. One word at a time.

Check yourself

    The whole picture

    1. Vectors and positions. Each word becomes a list of numbers that also knows its place.
    2. Attention. The words share information with a query, a key and a value.
    3. Feed-forward. Each word uses what the model learned, alone.
    4. Many blocks. The two parts repeat, and each block adds context.
    5. The next word. The last vector scores every word, and the model picks one.

    Next level

    Not published yet. It will cover: