Generating one token at a time
The last layer produces scores. A separate decoding procedure turns those scores into a choice and repeats the process to construct an answer.
Open this chapter with experiments and audio
What is the next-token distribution?
After reading a prompt, a causal language model has one logit for each vocabulary item at the final position. Logits are unrestricted scores. With scores 2, 1 and 0, the exponentials are approximately 7.39, 2.72 and 1. Divide each by their total to get probabilities 0.665, 0.245 and 0.090. Adding the same constant to all scores leaves their probabilities unchanged.
Greedy decoding picks the largest score. Sampling draws according to the distribution, so a token with probability 0.09 can occasionally win. Once drawn, that token becomes part of the next input. One early choice can lead to a different sentence even if the model weights stay fixed.
How sensitive are the choices to temperature?
Divide each logit by a positive temperature before applying softmax. A temperature below 1 enlarges score differences. A temperature above 1 shrinks them. Try both changes in the experiment and observe which candidates gain or lose probability. The adjustable scores represent a small example distribution.
Lowering the temperature makes high-scoring candidates more frequent. It does not change which answer the checkpoint has placed first, and cannot correct a factual mistake by itself. Top-k retains a fixed number of candidates. Top-p retains the shortest highest-probability prefix whose combined probability reaches a chosen threshold. The retained probabilities are normalized again before sampling.
How is generation different from training?
Rebuilding every earlier hidden state would repeat considerable work. A KV cache stores the keys and values computed for earlier positions in each relevant layer. Later positions can read them directly. The cache normally grows with the sequence and belongs to that request. Training parameters do not change during this process.
Estimate the size of this per-request state below. Hold the number of KV heads fixed and double the number of cached positions or parallel sequences.
With eight KV heads and one sequence of 2,048 tokens, this example uses 256 MiB. A second equally long sequence doubles the KV cache, while both requests continue to share one set of model parameters.
Repeat a draw and inspect the frequencies
from collections import Counter from math import exp from random import Random scores = [2, 1, 0] temperature = 1 weights = [exp((v-max(scores))/temperature) for v in scores] draws = Random(7).choices(["A", "B", "C"], weights=weights, k=10000) print(Counter(draws))
Expect roughly 6,650 A draws, 2,450 B draws and 900 C draws. Repeating this experiment samples repeatedly from one fixed distribution. A real continuation recalculates its distribution after every selected token. It can use additional intermediate tokens or candidate solutions to spend more inference computation on a task, but reliability still needs task-specific evaluation.
At top-p = 0.80, which candidates are retained for [0.665, 0.245, 0.090]?
The first two. The first alone is below 0.80 and their combined probability is 0.910. They are then normalized within the retained set.
Where a reasoning budget can go
An application can spend extra inference work by extending one sequence, generating several candidates, or asking a verifier to score them. These are different interventions. Extending a sequence gives later positions access to earlier intermediate results. Generating alternatives samples other paths. A verifier must recognize which path succeeds. Each adds cost even when the answer does not improve.
Suppose each independent attempt has a 40% chance of a correct result. With a perfect checker and three attempts, failure requires three misses. That has probability 0.6 × 0.6 × 0.6 = 0.216, giving 78.4% success. In the experiment, predict what happens when a checker discards half of the correct candidates. A success here means finding at least one correct accepted answer. The checker is assumed to reject every wrong answer.
What breaks if every attempt repeats the same misconception?
Perfectly correlated attempts repeat the first outcome. With a perfect checker the success rate stays at 40%, even after three attempts. An impressive best-of-many number can conceal the dependence on task difficulty, candidate diversity and the checker.
Inspect how temperature changes the distribution used for sampling →
Sources · Snell et al., 2024 · Cobbe et al., 2021