The third family of pre-trained Transformers keeps only the decoder: a stack of Transformer layers with causal (left-to-right) self-attention, trained to predict the next token. This is just a language model, the oldest idea in NLP, made very large. Starting with OpenAI's GPT series, decoder-only models grew into today's large language models.
1Causal language modeling
A decoder-only model is trained on plain text with the language modeling objective: maximize the probability of each token given all the tokens before it,
$$\mathcal{L}=-\sum_{t=1}^{T}\log P(x_t\mid x_1,\dots,x_{t-1}).$$To make this work inside a Transformer, self-attention uses a causal mask: position $t$ may attend to positions $1,\dots,t$ but never to the future. Without the mask, the model could simply look at the word it is supposed to predict.
Two differences stand out:
- Context: the masked LM sees both sides of each target; the causal LM sees only the left side.
- Training signal: the masked LM learns from only the ~15% of positions that are masked; the causal LM gets a prediction problem at every position, so the same text gives several times more training signal.
2The GPT series
| GPT-1 (2018) | GPT-2 (2019) | GPT-3 (2020) | |
|---|---|---|---|
| Parameters | 117M | 1.5B | 175B |
| Layers / hidden size | 12 / 768 | 48 / 1600 | 96 / 12288 |
| Context length | 512 | 1024 | 2048 |
| Training data | BooksCorpus (≈7,000 books) | WebText (40GB, 8M web pages) | ≈300B tokens (filtered Common Crawl, WebText2, books, Wikipedia) |
| How it is used | fine-tune on each task | zero-shot prompting | few-shot in-context learning |
GPT-1: pre-train, then fine-tune
GPT-1 (“Improving Language Understanding by Generative Pre-Training”) used the same recipe that BERT made famous a few months later: pre-train, then fine-tune the whole model on each task. (BERT-base, 110M parameters, was deliberately sized to match GPT-1 for comparison.) To fine-tune, GPT-1 turns each input into a single token sequence using special start, delimiter, and extract tokens, and puts a linear classifier on the output at the final extract token, which has seen the whole input.
[Extract]. Since a left-to-right model cannot see both sentences “at once” from the first one, tasks without a natural order process both orders.GPT-2: tasks as text
GPT-2 (“Language Models are Unsupervised Multitask Learners”) made the model 10× larger and trained it on WebText, a large collection of web pages. Its main claim was that a big enough language model can do tasks without any fine-tuning, if the task is written as text that the model naturally continues. For example, appending TL;DR: to an article makes the model write a summary, because that pattern appears on the web. Results were far below fine-tuned models, but they showed that task ability can emerge from language modeling alone.
GPT-3: in-context learning
GPT-3 (“Language Models are Few-Shot Learners”) scaled up another 100×. Its key finding was in-context learning: put a task description and a few examples in the prompt, and the model performs the task on a new input, with no gradient updates. The paper distinguishes
- zero-shot: only a task description,
- one-shot: a description and one example,
- few-shot: a description and several examples (as many as fit in the context window).
Few-shot performance improved sharply with model size; small models barely benefit from the examples.
3Generating text
A decoder generates text by repeatedly predicting a distribution over the next token, choosing a token, appending it to the input, and predicting again. Greedy decoding always takes the most probable token; sampling draws a token at random according to the probabilities, which gives more varied text. Each choice changes everything that follows.
4Comparing the three families
Each family provides a good weight initialization, but for different uses:
- BERT (encoder): representations for classification, token labeling, and span extraction. It cannot generate.
- BART (encoder–decoder): representations for classification, and a pre-trained decoder for generation.
- T5 (encoder–decoder): classification and generation through one text-to-text format.
- GPT (decoder): GPT-1 was fine-tuned with a classifier head; from GPT-2 on, every task is framed as generating a continuation of a prompt.
| Encoder-only | Encoder–decoder | Decoder-only | |
|---|---|---|---|
| Examples | BERT, RoBERTa | BART, T5 | GPT, LLaMA |
| Objective | masked tokens | denoising | next token |
| Sees the input | bidirectionally | bidirectionally (encoder) | left to right |
| Generates text | no | yes | yes |
| Best for | understanding, embeddings | input → output tasks (translation, summarization) | general-purpose generation, prompting |
Why are today's LLMs decoder-only?
- Simplicity: one stack, one objective. Pre-training, fine-tuning, and use are all “predict the next token”.
- Training signal: every token of every document is a training example.
- Generality: any task, including multi-turn conversation, can be written as a prompt and a continuation, with no fixed split between input and output.
- Scaling: this simple recipe kept improving as models and data grew.
The cost is that a decoder reads the prompt only left to right. An encoder–decoder model reads its input bidirectionally, which can be more parameter-efficient for tasks with a clear input and output. At very large scale this difference matters less, and since GPT-3 most large language models (PaLM, LLaMA, GPT-4, and many others) have been decoder-only.
5Summary
- Decoder-only models are Transformer language models with causal masking, trained to predict the next token.
- Unlike masked LM, every position contributes to the loss.
- GPT-1: pre-train + fine-tune with special start/delimiter/extract tokens. GPT-2: tasks as text, zero-shot. GPT-3: 175B parameters, few-shot in-context learning without gradient updates.
- Generation repeats: predict a distribution, choose a token (greedy or sampling), append it.
- Encoder models are for understanding, encoder–decoder models for input-to-output generation, decoder-only models for general-purpose generation; today's LLMs are almost all decoder-only.
6Practice
Exercise 1: training signal
A training sequence has 512 tokens. How many prediction targets does it provide for (a) BERT's masked LM, (b) a causal LM?
Exercise 2: causal mask
For the input “the cat sat on”, which positions can the token “sat” attend to in GPT? In BERT?
Exercise 3: few-shot vs. fine-tuning
You give GPT-3 a prompt with 10 labeled sentiment examples followed by a new review. What happens to the model's parameters? How is this different from fine-tuning?
Quick quiz
7Further reading
- Radford et al. (2018). Improving Language Understanding by Generative Pre-Training. (GPT-1)
- Radford et al. (2019). Language Models are Unsupervised Multitask Learners. (GPT-2)
- Brown et al. (2020). Language Models are Few-Shot Learners. NeurIPS. (GPT-3)
- Zhao et al. (2023). A Survey of Large Language Models.
- Karpathy. Let's build GPT: from scratch, in code, spelled out. (Video: implementing a small decoder-only model.)
- Hugging Face. LLM Course: Transformer architectures. (Encoder, decoder, and encoder–decoder models side by side.)