Model Mechanics Explainer 1

Decoder-only language models

How an LLM turns text into the next token

A language model reads tokens and predicts one token that could come next. It adds that token to the message and runs again. The full machine exists to make that repeated prediction useful.

Talk path: use the lifecycle map first, let the transformer videos explain the middle, then return to generation and the complete replay.
Technical 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 teaching examples; the article states tensor shapes and operation order where they matter.

All 8 sections

A model is built once, then used many times

In one sentence: training sets the model's numbers; a request reads those frozen numbers to produce one token at a time.

The build path collects text, creates a tokenizer, and adjusts billions of learned numbers called weights. Training saves the finished weights in a checkpoint.

The run path turns a prompt into tokens, reads the frozen checkpoint, chooses one next token, and repeats. A normal request does not change the checkpoint.

Figure 1 One machine builds artifacts; another runs them frozen Building the corpus
Try this: press Next to follow the build path, then the run path. Watch where the tokenizer and checkpoint appear in both.
Collect and filter a corpus Selected documents supply the evidence for tokenizer construction and pre-training.

The model's evidence starts as a selected dataset

In one sentence: the model can learn only from patterns that its training data contains.

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. They 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.

Figure 2 The pipeline filters documents, then packs them into token batches Collecting documents
Watch for: documents that leave the pipeline, the small tokenizer sample, and the token batches that continue to training.
sources
Schematic pipeline. Source proportions and rejection rates are illustrative. Corpus composition is a modeling decision, not background plumbing.

Text becomes a sequence of vocabulary addresses

In one sentence: the tokenizer cuts text into pieces and assigns each piece a number.

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. To feel this in your hands, train and operate a tokenizer in the playground.

Show tokenizer notation Tokenizer output
t₁, …, tT = Tokenize(s),   ti ∈ {0, …, |V| − 1}

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 (BPE) and Unigram tokenization use different algorithms to choose the vocabulary. 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.

Figure 3 A typesetter turns strings into addressed tiles Scanning a sentence
Try this: compare Sentence with Rare word or Emoji. Look for the example that needs the most token pieces.
Toy vocabulary. Boundaries and IDs are illustrative. A wall slot encodes only an ID; neighboring slots imply no similarity. Different strings can produce widely different sequence lengths.

Each token ID selects a learned vector

In one sentence: each token number selects a list of values the model can compare and change.

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 you can picture a vector 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.

Show the embedding lookup Embedding lookup
x(0)i = E[ti],   E ∈ ℝ|V| × dmodel

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.

Show the RoPE notation RoPE, simplified
q′i = R(i)qi,   k′j = R(j)kj

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. A contextual hidden state is not a static word meaning.

Figure 4 Rows become vectors; position rotates pairs of coordinates Drag to rotate
Try this: move from Lookup to Lattice to RoPE. Drag the lattice and watch the labels stay attached to the points.
Projection, not literal storage. The lattice is a three-dimensional shadow of a much higher-dimensional space, and the rotary view shows attention coordinates, not the embedding itself. The output layer later scores every vocabulary entry with matrix operations; it does not search this space for nearest points.

Attention mixes positions; the MLP transforms each position

In one sentence: attention pulls useful context from earlier tokens, then the MLP updates each token's working state.

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 looks for, keys represent what each position offers for matching, and values carry the information that attention will mix.

Show the attention math One causal attention head
Q = XWQ,   K = XWK,   V = XWV
A = softmax(QKT / √dhead + Mcausal)
Attention(X) = AV

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, the block combines their results, 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:

Show the block equations One decoder block, schematic
x ← x + Attention(RMSNorm(x))
x ← x + MLP(RMSNorm(x))

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.

A practical shape ledger
ObjectTypical shapeWhat 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
Figure 5 One decoder block, exploded for inspection Following the full block
Try this: start with Full block, then isolate Attention and MLP. The Residual view shows what each part adds to the running state.
Exploded tensor view. The planes make data dependencies visible. Implementations store these values in tensors and fuse many operations into accelerator kernels.

Every position supplies a next-token training target

In one sentence: the model guesses the next token, measures each error, and adjusts its weights.

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 training can grade all positions in a sequence in parallel. This setup is often called teacher forcing.

Show the training loss Autoregressive model and cross-entropy loss
pθ(t₁:T) = ∏i=1T pθ(ti | t<i)
L(θ) = −(1/N) Σi log pθ(ti | t<i)

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.

Show the optimizer step Optimizer step, schematic
θk+1 = AdamW(θk, ∇θL, η)

θ contains embeddings, attention projections, MLP weights, normalization scales, and the output projection. η is the learning rate. This schematic omits AdamW's moment estimates and weight decay.

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.

Figure 6 Score every position in parallel, then walk the error back Packing a token batch
Watch for: the target shifted by one token, many positions scored together, and the coral error signal moving backward.
forward pass
One optimizer step at toy scale. The targets come from the text itself: the target at each position is the next token. The model scores all positions in parallel under the causal mask; nothing here generates one token at a time. Production runs use many sequences and layers, accumulate gradients, synchronize workers, and save checkpoints throughout training.

The last hidden state becomes one score per token

In one sentence: the model scores possible next tokens, chooses one, adds it to the message, and runs again.

Sections 4 and 5 assembled the block, and Section 6 set its numbers. At request time, the tokenizer encodes the prompt, and the frozen transformer runs 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.

Show logits and temperature Logits and temperature-scaled probabilities
z = WUhT + b
pi = exp(zi/τ) / Σj exp(zj/τ)

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 loop appends the chosen token, 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 and attend 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.

Figure 7 Score the whole vocabulary, choose one tile, append it Scoring the vocabulary
Try this: generate one token, choose Top-k, then raise temperature. Compare how widely the candidate scores spread.
0.8
The skyline is visual shorthand. The model computes all logits with one matrix multiplication; the magnifier expands a few entries for inspection. The KV cache is temporary request state; the checkpoint stays frozen. Candidate values are illustrative.

Put the data, weights, and token loop on one timeline

In one sentence: the top lane builds reusable artifacts; the bottom lane reads them once per generated token.

The upper lane builds the artifacts: a corpus supports tokenizer construction and pre-training, then training saves a checkpoint. The lower lane uses the frozen tokenizer and checkpoint to map a prompt to one selected token. That token extends the context for the next decode step.

Figure 8 Corpus to checkpoint, prompt to next token Training text enters the pipeline
Try this: drag the scrubber from start to finish. Stop where the selected token loops back into the message.
Training begins with selected text The corpus supplies both the tokenizer sample and the encoded sequences used for next-token training.
1 / 12
cobalt: data coral: learning gold: output
Leave with this
  • Training changes the weights; a request reads them.
  • The transformer updates token states, then the output layer scores the next token.
  • Generation appends one chosen token and repeats the run path.

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.