On the path DNA-SocketThe residual stream is the running vector at each position that every block reads and adds to: where a model’s state can be read and where the study writes the gene. This step also fixes how the rest of the path counts blocks and layers.
On the path Weight-space programmingThe architecture of most of the networks in the experiments: blocks that read and add to one running vector per position, the residual stream.
On the path Attention paperThe pieces put together: the base and big models, their translation results, the paper’s ablations, and what came after.
On the path Inside a language modelHow attention and MLPs are stacked into blocks that read and add to one running vector per position, the residual stream.
The pieces put together
A Transformer is a stack of identical layers, each made of two kinds of sublayer: multi-head attention, in which every position reads the others, and a feed-forward layer, which works on each position on its own. Every sublayer sits on a residual connection with a layer normalisation. Below the stack, embeddings and a position encoding turn tokens into vectors; above it, a linear layer and a softmax turn vectors into probabilities.
The feed-forward sublayer is two linear layers with an activation between, applied to every position with the same weights; the middle is wider than the model, four times in the original:
x is one position’s vector, of width d; W1 of shape (d, 4d) and W2 of shape (4d, d), b1 and b2 their biases; max(0, ·) is the ReLU.
Three shapes
The original is an encoder-decoder: an encoder stack of self-attention reads the input; a decoder stack writes the output, with masked self-attention over what it has written and cross-attention to the encoder. Later models kept one half. Encoder-only models (BERT, 2018) read text to understand it. Decoder-only models (the GPT family, and most large language models since) drop the encoder and the cross-attention and write one token after another.
Writing: beam search
A decoder gives probabilities for the next token; to write a whole sentence it must choose. Taking the most probable token each time (greedy decoding) can lead into a dead end. Beam search keeps the k best partial sentences, scored by the sum of the logs of their tokens’ probabilities, extends each by every possible next token, and keeps the k best again.
On the path Attention paper
In the paper. Two sizes were trained (table 3):
| Model | Layers N | dmodel | dff | Heads | Dropout | Parameters |
|---|---|---|---|---|---|---|
| Base | 6 | 512 | 2048 | 8 | 0.1 | 65 million |
| Big | 6 | 1024 | 4096 | 16 | 0.3 | 213 million |
For translation it used beam search with 4 beams and a length penalty α = 0.6, writing at most 50 tokens more than the input, and averaged the weights of the last checkpoints (5 for the base model, 20 for the big one). On the WMT 2014 test sets the big model reached 28.4 BLEU English–German, more than 2 points above the best earlier result including ensembles, and 41.8 English–French, at a fraction of their training cost. The same architecture, with 4 layers and dmodel = 1024, also parsed English sentences into their grammatical structure, reaching 91.3 F1 trained on the Wall Street Journal treebank alone.
Test yourself
What does the feed-forward sublayer do that attention does not?
It works on each position on its own, transforming its vector with two linear layers and a ReLU; attention is the only place where positions exchange information.
What does a decoder-only model leave out of the original Transformer?
The encoder stack and the cross-attention; it keeps masked self-attention and the feed-forward layers and writes one token after another.
Why keep several beams rather than the single most probable token at each step?
A word that looks best now can lead to a worse sentence later; keeping several partial sentences lets a choice that starts slightly worse win in the end.
Where it leads
Kept in this browser only, never sent.
Paths through it
- DNA-Socketstep 5 of 22Follow this path
- Weight-space programmingstep 5 of 18Follow this path
- Attention paperstep 17 of 17Follow this path
- Inside a language modelstep 4 of 4Follow this path