Autoregressive generation loop
Generate text by repeatedly computing logits for the last position, picking or sampling a token, appending it and running the model again. Explain why output tokens are produced strictly one at a time.
Builds on: The decoder-only transformer, end to end
What the animation says
Only the last row is new
One forward pass reads every token at once, and gives one row of predictions per position. Here the prompt is four tokens: the, cat, sat, on. So we get four rows. Each row predicts the token right after its position. Row one predicts the second token. But we already have it: cat. Tap the one row that predicts something we don't have yet.
Why not the whole reply at once?
So one pass gives us one genuinely new token. Why not read the token after that from the same pass? To predict position six, the model needs token five as input. But token five doesn't exist yet. It appears only once we choose it from row four. So a reply can't come out in one pass. Each token waits for the one before it. To get the token after next, what has to happen first?
Pass one, with real numbers
Pick, append, run again. Let's do it with a vocabulary of four: the, cat, sat, and end of text. The prompt is: the, cat. Two tokens in, so the logits are two rows of four. Keep only the last row. Exponentiate it: one, one, six, two. They sum to ten. Divide by ten: point one, point one, point six, point two. That's the softmax. Argmax picks sat. Sampling would pick it sixty percent of the time. Append it. Now pass two runs. What shape are its logits?
Stop, and count the cost
Pass two runs on all three tokens, so the logits are three by four. Again, only the last row matters. Its argmax is end of text, so we stop. Two passes, two new tokens. Every generated token costs exactly one pass. Each pass reads one more token than the last. The final token is never an input. It's what the last pass produces. When sampling, the chance of this reply is a product: point six, times the chance of end of text after 'the cat sat'.
The loop in numpy
Here's the loop in numpy. The model is a toy where row t only sees ids up to t. The loop is the real thing. Inside the loop: run the model on every id so far, and keep the last row. Softmax it, sample one id, and append it. The prompt has two ids and the loop runs four times. That's four passes, and the last pass reads five ids. This naive loop redoes the whole sequence every step. Tap the line that does it.
Only the last row predicts something new
The generation loop, precisely
Two steps with a 4-token vocabulary
The loop in numpy
import numpy as np
rng = np.random.default_rng(0)
V, d = 5, 8
W_E, W_U = rng.normal(size=(V, d)), rng.normal(size=(d, V))
def model(ids): # [T] ids -> [T, V] logits
return np.cumsum(W_E[ids], axis=0) @ W_U # row t sees ids[:t+1]
ids = [1, 3] # prompt
for _ in range(4):
z = model(np.array(ids))[-1] # logits of the last position only
p = np.exp(z - z.max()); p /= p.sum()
ids.append(int(rng.choice(V, p=p))) # or int(z.argmax())
print(ids)
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