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.
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.
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.
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:
- a query: "what am I looking for?"
- a key: "what do I have?"
- a value: the information that the word gives.
When a query matches a key, the score is high.
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.
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.
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.
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.
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
- Vectors and positions. Each word becomes a list of numbers that also knows its place.
- Attention. The words share information with a query, a key and a value.
- Feed-forward. Each word uses what the model learned, alone.
- Many blocks. The two parts repeat, and each block adds context.
- The next word. The last vector scores every word, and the model picks one.
Next level
Not published yet. It will cover:
- the maths of attention: query × key, softmax and values
- the shortcuts (residual connections) and the normalisation inside each block
- the output layer, and how the model picks a word
- the encoder and the decoder: the 2017 design and the chatbot design