Nucleus Sampling › The Top-p Formulation
Language AI HandbookAll chapters Nucleus Sampling › The Top-p FormulationOne section · Sections in this chapter3 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The insight behind nucleus sampling is simple: instead of asking "how many tokens should I consider?", we ask "how much probability mass should I capture?" This shift in perspective leads to an adaptive algorithm that naturally handles both high-confidence and uncertain predictions.
From Intuition to Definition
Think about what we really want when sampling the next token. We want to include all tokens that have a reasonable chance of being correct, and exclude all tokens that are essentially noise. But "reasonable" depends on context. After "The sky is", the token "blue" might have 40% probability, meaning we should consider other options. After "2 + 2 =", the token "4" might have 99% probability, meaning alternatives are probably mistakes.
The key insight is that probability itself tells us what's reasonable. If we capture 90% of the total probability mass, we've included essentially all the tokens the model considers plausible. Everything in the remaining 10% is, by definition, something the model thinks is unlikely. We're not guessing how many tokens to include; we're reading the distribution and respecting what it says.
Notice that this reframing changes what the hyperparameter means. With top-k, you are saying "always consider exactly options." With nucleus sampling, you are saying "always capture at least fraction of the probability mass." The second statement is much easier to reason about. Setting means "I want to include the options in the 90% probability core." That is a semantically meaningful statement that stays valid regardless of whether the model is confident or uncertain.
This leads us to define the nucleus as the smallest set of highest-probability tokens that together account for at least fraction of the total probability. Formally, given a probability distribution over vocabulary , the nucleus is the minimal set such that:
where:
- : the nucleus, the minimal set of highest-probability tokens we'll sample from
- : the probability assigned to token by the model
- : the cumulative probability threshold, typically set between 0.9 and 0.95
The nucleus is the minimal set of highest-probability tokens whose cumulative probability mass meets or exceeds the threshold . Tokens outside the nucleus are discarded, and the remaining probabilities are renormalized.
The Algorithm Step by Step
How do we find this minimal set? The algorithm is straightforward once you see the logic:
-
Sort tokens by probability in descending order. This puts the most likely tokens first.
-
Walk through the sorted list, accumulating probabilities. Keep adding tokens until your running sum reaches or exceeds .
-
Stop as soon as you cross the threshold. The tokens you've accumulated form the nucleus. Discard everything else.
-
Renormalize so that the remaining probabilities sum to 1, giving you a valid distribution to sample from.
Let's express this mathematically. After sorting, we have tokens ordered so that , where is the vocabulary size. We find the smallest such that:
where:
- : the token with the -th highest probability
- : the number of tokens in the nucleus (determined dynamically based on the distribution)
- : the total vocabulary size
The nucleus is then , containing exactly the top tokens needed to reach the probability threshold. The key property of this formulation is that emerges from the distribution itself. A peaked distribution yields small ; a flat distribution yields large .
In practice, this means the nucleus can be as small as 1 token (when the model is extremely confident) or as large as thousands of tokens (when the model is almost completely uncertain across its vocabulary). Top-k cannot span this range with a single setting; nucleus sampling spans it automatically.
Why Renormalization Matters
After truncating to the nucleus, the probabilities no longer sum to 1. If our nucleus contains 90% of the probability mass, the probabilities inside it sum to 0.90, not 1.0. To sample correctly, we need to renormalize.
The renormalized probability for each token in the nucleus is simply its original probability divided by the total probability mass in the nucleus:
where:
- : the renormalized probability used for sampling
- : the original probability of token
- : the sum of original probabilities over all tokens in the nucleus, which is the normalizing constant
This renormalization preserves the relative ordering of tokens. If "nice" was twice as likely as "good" before, it remains twice as likely after. We're simply scaling everything up so the probabilities form a valid distribution that sums to 1.
The renormalization step also has a subtle effect on quality: it redistributes the probability mass that was assigned to tail tokens back into the nucleus. This slightly increases the probability of every nucleus token, which is exactly what we want. When the model was "uncertain" about whether to pick from the tail, we're saying: no, allocate that residual probability to the plausible options instead.