Decoder-only language models
How an LLM turns text into the next token
A language model has one job: given some text, predict a small piece of what comes next. Everything an LLM produces, from a chat reply to working code, is that prediction repeated. This article follows the full lifecycle in order: the corpus and tokenizer that prepare the data, the transformer block that computes on it, the pre-training that sets the numbers, and the generation loop that turns a prompt into text one token at a time.
Scope. The diagrams show a modern decoder-only transformer, the family used by GPT-style language models. The 2017 transformer had an encoder and a decoder. Model widths, layer counts, token IDs, probabilities, and 3D positions here are illustrative; tensor shapes and operation order are stated where they matter.
Section 1 · Lifecycle
A model is built once, then used many times
An LLM has two separate lives. In the build loop, a data pipeline assembles a large collection of text called the corpus, a tokenizer turns that text into token sequences, and pre-training adjusts the model's parameters, often called weights: the billions of learned numbers that determine its behavior. The finished parameters are saved as a checkpoint, one large file the model can be reloaded from.
In the use loop, a prompt passes through the frozen tokenizer and checkpoint. The model produces one token, appends it to the sequence, and runs again, once per small piece of generated text. The parameters do not change while an answer is generated.
The sections below follow the build order: corpus, tokenizer, vectors and the transformer block, then pre-training, then the generation loop a prompt travels through. Training comes after the block because the update rule is easier to picture once you have seen what the parameters compute. Jay Alammar's Illustrated Transformer uses a similar progressive reveal for the original architecture.
Section 2 · Corpus
The model's evidence starts as a selected dataset
The build loop starts with text. Training text arrives as web crawls, books, code, and papers, and none of it is ready to use. Data pipelines extract the usable text, remove boilerplate and near-duplicates, apply quality and safety filters, choose language and domain mixtures, then split documents into training examples. Those decisions determine what patterns the model can observe and how often it observes them.
Engineers usually train the tokenizer, described next in Section 3, on a representative sample of the corpus, then use that frozen tokenizer to encode the much larger training set. The FineWeb paper is useful because it publishes concrete filtering and deduplication experiments. Proprietary model reports may disclose only part of the data recipe.
Accuracy check. Documents do not flow directly into a transformer during training. They become packed arrays of token IDs, commonly with boundaries or special tokens marking document transitions. Figure 2 compresses storage, shuffling, packing, and distributed loading into one stream.
Section 3 · Tokenization
Text becomes a sequence of vocabulary addresses
The corpus supplies raw strings. A neural network computes with numbers, so text has to become numbers first. A tokenizer segments a string into pieces found in a fixed vocabulary and returns the corresponding integer IDs. A common word may occupy one token. A rare name may split into several subwords or bytes. Spaces, punctuation, and code fragments can be tokens too.
s is the input string, T is its token count, and |V| is the vocabulary size. A token ID is an address. Its numeric value carries no distance or meaning.
Byte pair encoding and Unigram tokenization use different algorithms to choose the inventory. SentencePiece describes both in a language-independent system that can train from raw sentences. Production tokenizers often add byte fallback and special control tokens.
Keep the artifacts separate. Tokenizer construction chooses a discrete vocabulary. Pre-training later learns continuous embedding vectors and transformer weights. Once model training begins, changing the token-to-ID map would invalidate the embedding rows tied to those IDs.
Section 4 · Embeddings and position
Each token ID selects a learned vector
A token ID by itself is only a catalog number. The embedding table attaches content to it: one learned row per vocabulary entry. Looking up token ti returns that row, a vector of dmodel numbers, and a vector can be pictured as a point in space. Tokens that appear in similar contexts tend to drift toward nearby points during training. The looked-up vector is the initial hidden state, the model's working representation, at position i.
The bracket is a row lookup, not a semantic database query. Pre-training adjusts every used row through gradient descent.
The network also needs token order; "dog bites man" and "man bites dog" contain the same tokens. Many current decoders use rotary position embeddings, or RoPE. RoPE rotates query and key coordinates at each attention layer, so attention scores depend on relative offsets between positions. It is not an extra point appended to the token embedding.
R(i) rotates pairs of coordinates by position-dependent angles. The dot product q′i · k′j then carries information about i − j. See RoFormer.
Figure 4 projects a toy set of vectors into three dimensions. A real hidden width may contain thousands of coordinates. Nearby points can suggest similar usage, but this drawing is not a measurement from a trained model, and a contextual hidden state is not a static word meaning.
Section 5 · Transformer block
Attention mixes positions; the MLP transforms each position
A transformer is a stack of near-identical blocks, and each block makes two moves. Attention lets every position read from earlier positions, so the state for "bank" in "the river bank" can absorb "river". The MLP, a small feed-forward network applied to each position on its own, then reworks whatever attention gathered. The rest of this section states both moves precisely.
Let X hold the hidden states for a sequence. One attention head makes three linear projections. Queries represent what each position is looking for, keys represent what each position offers for matching, and values carry the information that will be mixed.
Row i of A contains the weights used to update token i. Mcausal[i,j] is −∞ when j > i, so softmax assigns future positions zero probability. The division by √dhead keeps dot products numerically controlled.
Multiple heads run in parallel, their results are combined, and an output projection returns to the model width. Grouped-query attention shares key and value heads to reduce memory traffic, but each query still obeys the causal mask.
A modern pre-normalized block commonly follows this residual pattern:
The residual path preserves the current state while each sublayer writes an update. A SwiGLU-style MLP expands, gates, then projects each position back to dmodel. Exact normalization, gating, and bias choices vary by model.
The original Attention Is All You Need paper introduced the encoder-decoder transformer with post-normalization. Modern decoder-only implementations often use RMSNorm, RoPE, gated MLPs, and grouped-query attention; OpenELM documents one compact example.
| Object | Typical shape | What varies |
|---|---|---|
| Token IDs | [T] | Sequence length T |
| Hidden states | [T, d_model] | One vector per position |
| Attention scores | [heads, T, T] | One causal matrix per query head |
| Output logits | [T, |V|] | One vocabulary score vector per position |
Section 6 · Pre-training
Every position supplies a next-token training target
With the block defined, the build loop can set its numbers. Pre-training is a guessing game graded at every position. The model reads corpus text and, at each position, must assign high probability to the token that comes next. The causal mask keeps it from peeking at later tokens, yet all positions in a training sequence can be graded in parallel; this setup is often called teacher forcing.
If the model assigns the observed token probability 0.80, that position contributes −log(0.80) ≈ 0.223. Probability 0.05 contributes about 2.996. Confident mistakes cost more.
Backpropagation computes the gradient of the batch loss: for every participating parameter, a measure of how a small change would raise or lower the loss. An optimizer such as AdamW maintains running statistics and uses that signal to apply a small update. Data-parallel and tensor-parallel systems distribute the work across many accelerators.
θ contains embeddings, attention projections, MLP weights, normalization scales, and the output projection. η is the learning rate. AdamW's moment estimates and weight decay are suppressed here.
The GPT-3 paper is a concrete decoder-only training reference. Chinchilla showed that, within its studied compute regime, better results came from balancing parameter count with more training tokens. That scaling result is an empirical recipe, not a law that fixes every model or dataset.
After base training. Instruction tuning and preference optimization adjust the checkpoint again using curated examples and preference signals. They shape behavior, but generation still uses the same autoregressive forward pass. See InstructGPT for an influential pipeline.
Section 7 · Generation
The last hidden state becomes one score per token
Sections 4 and 5 assembled the block, and Section 6 set its numbers. At request time, the prompt is tokenized and passed through the frozen transformer: the same forward pass used in training, but with no update afterward. A final normalization and linear output projection map the latest hidden state to one logit, an unnormalized score, for every vocabulary entry. Some models tie this output matrix to the input embedding table.
WU is the output, or unembedding, matrix; some architectures omit the bias b. Lower positive temperature τ concentrates mass on larger logits. Higher τ flattens the distribution. Greedy decoding chooses the largest logit; top-k and top-p sampling first restrict the candidate set.
The chosen token is appended and decoding repeats. During the first prompt pass, called prefill, the model computes keys and values for every prompt token. A KV cache keeps those tensors so later decode steps compute the new position while attending over cached keys and values. Systems such as PagedAttention focus on managing that cache efficiently.
What remains fixed. The checkpoint does not learn from the request. The KV cache is temporary sequence state, not durable memory. Randomness enters through the decoding rule; the forward pass for fixed token IDs and weights is deterministic up to numerical implementation details.
Section 8 · Complete pass
Put the data, weights, and token loop on one timeline
The upper route builds the artifacts: a corpus supports tokenizer construction and pre-training, then training saves a checkpoint. The lower route uses the fixed tokenizer and checkpoint to map a prompt to one selected token. That token extends the context for the next decode step.
References
Papers and visual references
Links appear beside the claims they support. This list collects the primary technical references and the visual work that informed the article's pacing.
- Attention Is All You Need
Vaswani et al., 2017. Scaled dot-product attention and the original encoder-decoder transformer. - SentencePiece
Kudo and Richardson, 2018. BPE and Unigram tokenization trained directly from raw sentences. - The FineWeb Datasets
Penedo et al., 2024. A documented web corpus with filtering and deduplication experiments. - RoFormer
Su et al., 2021. Rotary position embeddings applied to attention queries and keys. - Language Models are Few-Shot Learners
Brown et al., 2020. Decoder-only autoregressive pre-training at GPT-3 scale. - Training Compute-Optimal Large Language Models
Hoffmann et al., 2022. Empirical scaling of parameters and training tokens under a compute budget. - OpenELM
Mehta et al., 2024. A documented modern decoder using RMSNorm, RoPE, grouped-query attention, and SwiGLU. - InstructGPT
Ouyang et al., 2022. Supervised instruction tuning and preference-based post-training. - PagedAttention
Kwon et al., 2023. Efficient serving through paged KV-cache management. - The Illustrated Transformer
Jay Alammar's linear, progressive visual explanation of the original transformer. - Transformer Explainer
Cho et al., 2024. Interactive transitions between system-level and operation-level transformer views. - LLM Visualization
Brendan Bycroft's detailed 3D inspection of a toy GPT forward pass.