Transformers and Attention
In this article
- Sketch one forward pass of a decoder-only transformer, from token IDs through embeddings, position numbers, masked attention, feed-forward layers and the stack to logits and probabilities.
- Say which positions a token may read under the causal mask, why training needs the mask, and which vectors a changed token can reach.
- Explain why relevance scores are computed afresh for each text while the parameters stay fixed.
- Predict how many forward passes a reply takes and how a doubled prompt changes the attention work of each pass.
- Terms defined here: transformer, token embedding, positional encoding, attention, causal mask, transformer layer, logits, softmax, forward pass.
Overview
The question "In which city is the Eiffel Tower?" ends on ?. The model reads it as 10 token IDs and produces the scores for the token after ?, and those scores put ␣Paris at 0.92. But nothing about a question mark says Paris. The tower's name sits four positions earlier. So somewhere inside the model, information from ␣E, iff, el and ␣Tower has to travel to the last position before the next token is scored. Following that journey gives you a working picture of a modern large language model: what each position can see, why a changed word changes everything after it and nothing before it, and why a longer prompt costs far more than its length suggests.
Sign in to keep reading
The rest of this article opens with an account. An account also keeps your progress and quiz results on every device, and lets you follow news for up to five topics.
Still to read in this article
- From IDs to vectors
- Attention: mixing in earlier positions
- The causal mask
- A layer, then a stack
- From the last vector to scores, and what a pass costs
- Sources
- Check yourself, 5 questions