Nucleus sampling (top-p sampling) is a decoding method that adds up the next-word probabilities produced by a language model in descending order, keeps only the words up to the point where the total reaches , and randomly picks one of them in proportion to its probability. Its defining feature is that the number of words kept automatically grows or shrinks with the shape of the distribution. It was proposed by Holtzman et al. (2020) and is widely used for open-ended text generation such as stories and dialogue.
The previously common top-k sampling keeps the top words by probability. However, the right number of candidates varies greatly from situation to situation.
- When many words could follow, as after "Today is," it is better to keep more.
- When the answer is nearly determined, as after "Thank you very," it is better to narrow down to one or two.
Nucleus sampling handles both by cutting on total probability rather than on a count. The paper calls the set of top words that holds most of the probability mass the "nucleus."

