What is a decoder-only Transformer?
A decoder-only Transformer is a stack of identical blocks, each with masked self-attention and a feed-forward layer, trained to predict the next token of a text from the tokens before it. The causal mask stops each position from seeing later tokens, so one pass over a training text gives a prediction and a loss at every position. At inference time the model generates text by predicting one token, appending it, and repeating.
This is the architecture behind GPT, Llama, Mistral, Gemma, Claude, and most other large language models. The same network reads the prompt and writes the answer, which makes it a single general interface for chat, coding, translation, summarization, and reasoning tasks. Its design choices, such as pre-norm, RMSNorm, RoPE, grouped-query attention, and SwiGLU, now appear in almost every open model.
Key ideas
- A decoder-only model is a stack of masked self-attention and feed-forward blocks.
- The causal mask lets each position see only itself and earlier tokens.
- Training minimizes next-token cross-entropy at every position at once.
- Generation is autoregressive: predict a token, append it, repeat.
- A KV cache stores past keys and values so each new token costs one short pass.
- Modern LLMs add pre-norm RMSNorm, RoPE, grouped-query attention, and SwiGLU to the original design.
Common questions
What is the difference between decoder-only and encoder-decoder models?
An encoder-decoder model such as T5 reads the input with a bidirectional encoder and generates with a separate decoder that attends to it through cross-attention. A decoder-only model puts the input and output in one sequence and uses a single causal stack for both. The encoder-decoder split can be more efficient for tasks with a long input and short output, like summarization, while decoder-only models are simpler to scale and to use for open-ended tasks.
Why are most large language models decoder-only?
The training objective needs no labels and gives a loss at every token, so any text can be used. One model handles any task phrased as text, and the KV cache makes generation efficient. GPT-3 also showed that large decoder-only models can learn new tasks from examples in the prompt, which made scaling this design the main path.
What is a context window?
The context window is the maximum number of tokens the model can attend to at once, prompt and output together. It is limited by the positional encoding range used in training and by memory, since the KV cache and attention cost grow with length. Modern models range from a few thousand to over a million tokens.
Can a decoder-only model produce embeddings or classify text?
Yes. Because of the causal mask, the last token's hidden state is the only one that has seen the whole input, so it is often used as a summary vector for classification or retrieval. Many current embedding models are fine-tuned decoder-only LLMs. For classification, the model can also be prompted to output a label word.
Related explainers
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- How Self-Attention Works — Query, key, value — the core of self-attention
- Language Modeling — Predict the next token — the objective that powers GPT, BERT, and modern NLP
- Positional Encoding — How transformers know word order
- Tokenization & BPE — How text becomes numbers — from characters to subword tokens via byte-pair encoding
- RLHF — Align language models with human preferences using reward models and policy optimization
References
- Brown et al. (2020), "Language Models are Few-Shot Learners", NeurIPS
- Touvron et al. (2023), "LLaMA: Open and Efficient Foundation Language Models"
- Xiong et al. (2020), "On Layer Normalization in the Transformer Architecture", ICML
- Hoffmann et al. (2022), "Training Compute-Optimal Large Language Models"
- Holtzman et al. (2020), "The Curious Case of Neural Text Degeneration", ICLR
Last updated