Top-p sampling
Top-p keeps the smallest set of most probable tokens whose probabilities add up to at least p.
Top-p sampling is also called nucleus sampling. Select highest-probability tokens while tracking their cumulative probability mass. Retain the smallest prefix that reaches or exceeds the threshold.
Rescale the retained probabilities. Nucleus sampling samples from the top-p portion of probability mass. Top-p lets the number of eligible tokens rise and fall dynamically.
Holtzman and colleagues introduced nucleus sampling to truncate the unreliable probability tail. The authors’ repository directs users to a Hugging Face implementation of nucleus sampling. vLLM uses one to consider all tokens.
Top-k sampling can fail for any one choice of k.
Build one nucleus from a sorted distribution.
- 1 · sortSelect highest-probability tokens while tracking their cumulative probability mass.
- 2 · accumulateSelect highest-probability tokens whose cumulative probability mass exceeds p.
- 3 · keepRetain the smallest prefix that reaches or exceeds the threshold.
- 4 · sampleRescale the retained probabilities.
The threshold p controls the cumulative probability of the top tokens considered.
| Who | What they ask | What it works with |
|---|---|---|
| Generation engineer | “How can the candidate count expand and contract dynamically?” | The top-p threshold |
| Evaluation team | “Which candidates were eligible at this decoding step?” | The cumulative probability nucleus |
| Runtime engineer | “Which setting disables top-p filtering?” | The serving engine's sampling parameters |
- Top-p adapts the number of eligible tokens to the distribution at each step.
- It removes the probability tail outside the selected nucleus.
- The nucleus always contains probability mass of at least p.
- Setting p too low can over-truncate the distribution and resemble greedy decoding.
- Nucleus sampling is a stochastic decoding method.
- A p value of one disables top-p filtering by considering all tokens.
Sources used
This explainer is written in original language. The links below support its factual claims.
- paperThe Curious Case of Neural Text Degeneration, Holtzman et al. · read 28 Sept 2026
- docsGeneration, Hugging Face · read 28 Sept 2026
- docsSamplingParams, vLLM · read 28 Sept 2026
- paperConformal Nucleus Sampling, Ravfogel, Goldberg and Goldberger · read 28 Sept 2026
- repoNucleus sampling and generations, Holtzman et al. · read 28 Sept 2026