Top-p Sampling: Why the Same Setting Keeps 2 Tokens One Time and 8 the Next
A language model writes text one token at a time (a token is a word or a piece of a word). At each step it gives every possible next token a probability, then picks one at random, weighted by those probabilities. That weighted random pick is called sampling. Top-k sampling limits the pick to a fixed number of the highest-probability tokens. That's predictable, but it ignores whether the model is sure or spread across many options. Top-p sampling, also called nucleus sampling, keeps the smallest group of top tokens whose probabilities add up to at least p , where p is a number you choose, such as 0.90 for 90%. ("Nucleus" just means the core group of likely tokens.) When one or two tokens dominate, that group is tiny. When the probabilities are spread out, the group widens. This post shows it on a small made-up vocabulary of 12 candidate answers, with code you can rerun. The Rule Sort the probabilities from highest to lowest and add them up in or...