Language modeling as next-token prediction
Write p(x₁…x_T) = ∏ p(x_t | x_{<t}), an exact factorization by the chain rule of probability. Explain why a model of next-token conditionals is a complete generative model of text.
Builds on: What an LLM computes, end to end, Generating text one token at a time
What the animation says
One question only
An LLM only ever answers one question. Given the tokens so far, how likely is each possible next token? Yet we use it to score whole sentences and to write whole replies. So here is the puzzle: does guessing one token at a time lose anything about whole texts?
Every text is a path
Nothing is lost. To see why, draw every possible text as a tree. From the root, one branch for each possible first token. From each of those, one branch for each second token. A path stops when it takes the end branch, so every text is exactly one path. The model puts a probability on every branch, and a path's probability is its branches multiplied. Your turn: finish the path for the text 'the cat sat'.
The chain rule is exact
Written as a formula, this is the chain rule. The probability of a whole text equals the product, over every position, of that token's probability given everything before it. It is just: probability of A and B equals probability of A, times B given A, applied again and again. It is exact for any distribution. No assumption that only recent tokens matter. With the end token last, even length is covered. Errors come only from imperfect guesses.
Scoring the cat sat
Let's score one full path: the cat sat, then end. The model gives 'the' a probability of point two. 'Cat' after 'the', point oh five. 'Sat' after 'the cat', point three. Multiply as we go: point two times point oh five is point oh one. Times point three, point oh oh three. The model also gives the end token, after 'the cat sat', a probability of point one. So what is the probability of the complete text?
Add logs instead
It's point oh oh oh three: every step counted, end included. For long texts, a product like this gets too small for a computer to store. So add natural logs instead: about minus one point six, minus three, minus one point two, and minus two point three. They add up to about minus eight point one. Raise e to that power, and you're back at point oh oh oh three. Same score, safe to compute.
A full distribution
Do the conditionals alone really make a full distribution? Here's a toy model with three tokens. This function gives next-token probabilities for any prefix, and they sum to one. This one is the chain rule: multiply the conditionals along a sequence. Score one sequence and you get about point oh four. Sum over all twenty-seven length-three sequences, and you get one. So a next-token predictor scores any text, and generates by drawing one token at a time. Its quality rests only on how good those guesses are.
Every text is a path through a tree
The chain rule: an exact factorization
Scoring "the cat sat⟨end⟩"
Conditionals define a full distribution
import numpy as np, itertools
V = 3
def cond(prefix): # toy p(next | prefix), sums to 1
s = np.arange(1, V + 1) + sum(prefix)
return s / s.sum()
def joint(seq): # chain rule: multiply conditionals
return np.prod([cond(seq[:t])[seq[t]] for t in range(len(seq))])
print(joint([2, 0, 1])) # 0.5 * 0.25 * 0.333 = 0.0417
print(sum(joint(s) for s in itertools.product(range(V), repeat=3))) # ~1.0
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses