In [Chapter 2 of the full book] we saw how to fine-tune and use a transformer transformer A neural architecture built on self-attention (no recurrence or convolution) that maps a sequence of token embeddings to a sequence of contextualized embeddings. defined in ch. 3 — open in glossary ; this chapter opens the hood and builds one from scratch in PyTorch. We start from the original 2017 architecture, assemble a working encoder piece by piece, peek at the decoder, and finish with a tour of the model zoo. A deep grasp of the internals isn’t strictly needed to apply transformers, but it is what lets you reason about their limits and adapt them to new domains.
Recap from Chapter 2 (assumed background). Text is first tokenized into sub-word units, each mapped to an integer input id; an embedding lookup table then turns each id into a dense vector. Everything below operates on those vectors.
The encoder–decoder architecture
The original Transformer was designed for sequence-to-sequence problems such as machine translation, and uses an encoder–decoder architecture encoder-decoder architecture The original transformer form: an encoder builds a representation of the input and a decoder generates an output sequence from it. Used for tasks like translation. defined in ch. 3 — open in glossary with two halves:
- The encoder encoder A stack of layers that turns input tokens into a sequence of contextualized embeddings (the hidden state / context). defined in ch. 3 — open in glossary converts an input sequence of tokens into a sequence of embedding vectors — the hidden state hidden state The sequence of embedding vectors the encoder produces for a sequence, also called the context. defined in ch. 3 — open in glossary or context.
- The decoder decoder A stack of layers that generates an output sequence one token at a time, autoregressively, using the encoder's output and its own past outputs. defined in ch. 3 — open in glossary uses that hidden state to iteratively generate an output sequence of tokens, one at a time.
Three things about the architecture are already visible:
- Positions are injected on purpose. Because attention is order-blind (see §3.4), the token embeddings token embedding The fixed vector a token id maps to via a lookup table (768-dimensional in BERT), before any context is mixed in. defined in ch. 3 — open in glossary are summed with positional embeddings positional embedding A position-dependent vector added to token embeddings so the otherwise order-blind attention mechanism can model token order. defined in ch. 3 — open in glossary so the model knows where each token sits.
- Both halves are stacks. The encoder is a stack of identical encoder layers (“blocks”), much like stacking convolutional layers in vision; the decoder has its own stack.
- The encoder feeds the decoder. The encoder’s output is handed to every decoder layer, which then predicts the most probable next token — and that prediction is fed back in to produce the next one, until an end-of-sequence (EOS) token appears.
Watch the decoder generate
Step through translating “Time flies like an arrow” into German. Notice the encoder runs once, while the decoder loops — each predicted token becomes part of the next step’s input.
1Encode the source
The encoder reads the whole English sentence at once and turns it into a set of hidden states (keys and values). The decoder has produced nothing yet; it will start from a special begin-of-sequence token.
Zooming into the encoder
The encoder is a stack of identical layers, each of which takes a sequence of embeddings and passes them through two sublayers: a multi-head self-attention layer and a position-wise feed-forward layer. Crucially, the output embeddings have the same shape as the inputs — a layer’s job is to update each embedding with context (the word “apple” drifts toward “company” when “keynote” or “phone” sits nearby). That shape-preserving property is what lets us stack the layers.
Click a sublayer to see what it does. Data flows bottom → top; the output has the same shape as the input.
Each sublayer is also wrapped in a skip connection and layer normalization — standard tricks for training deep networks that we add in §3.4. For now, the takeaway is the two-sublayer shape. To really understand the encoder we have to go deeper, starting with its beating heart: the self-attention layer.
Three flavors of transformer
Although the original design pairs an encoder with a decoder, each half was soon used on its own. Almost every model you’ll meet is one of three types.
| Encoder-only | Decoder-only | Encoder–decoder | |
|---|---|---|---|
| Attention | Bidirectional | Causal / autoregressive | Bi- + causal + cross |
| Context a token sees | left + right | left only | whole source + output so far |
| Example models | BERT, RoBERTa, DistilBERT | GPT, GPT-2/3, CTRL | T5, BART, M2M-100 |
| Best suited to | classification, NER, extractive QA | text generation, autocomplete | translation, summarization |
- Encoder-only encoder-only Models (the BERT family) that use bidirectional attention to build rich representations; best for understanding tasks like classification, NER, and QA. defined in ch. 3 — open in glossary models (BERT and friends) build a rich representation using bidirectional attention bidirectional attention Attention in which a token's representation depends on both its left and right context. defined in ch. 3 — open in glossary — great for classification, NER, and extractive QA (Ch. 7).
- Decoder-only decoder-only Models (the GPT family) that use causal attention to predict the next token; best for text generation. defined in ch. 3 — open in glossary models (the GPT family) auto-complete text using causal (autoregressive) attention causal attention Autoregressive attention in which a token attends only to itself and earlier tokens, never to future ones. defined in ch. 3 — open in glossary , where each token sees only what came before it.
- Encoder–decoder encoder-decoder A model family with both an encoder and a decoder (e.g. T5, BART); maps one sequence to another, as in translation and summarization. defined in ch. 3 — open in glossary models (T5, BART) map one sequence to another — translation, summarization.
Note — the boundaries are fuzzy
The split isn’t absolute. Decoder-only GPT models can be primed to do translation (conventionally a seq-to-seq task), and encoder-only BERT can be coaxed into summarization. The categories describe a model’s natural fit, not a hard limit.With the map in hand, let’s descend into the mechanism that makes all three work — self-attention.
Check yourself: the transformer architecture
1.What does the encoder produce from an input sequence of tokens?
2.Why must positional embeddings be added to the token embeddings?
3.During autoregressive decoding, what is fed back into the decoder at each step?
4.You need a model for extractive question answering and text classification. Which family fits best?
5.Predict: in a decoder-only (GPT-style) model, which tokens can position 3 attend to?