Top-k Sampling › How Top-k Sampling Works

Michael BrenndoerferUpdated July 29, 20258 min read
Language AI HandbookAll chapters
Part 28 of 67GPT Architecture
Chapter 203Top-k Sampling › How Top-k Sampling Works
Reading section 3 of 13 · How Top-k Sampling Works · 8 minSection 3 of 13 · 8 min

Book contents

Section 3 of 13 · 8 min read

Top-k Sampling › How Top-k Sampling WorksOne section · Sections in this chapter3 / 13 ↓
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

Now that we understand the problem, let us build up the solution piece by piece. The core insight behind top-k sampling is deceptively simple: if most of the probability mass concentrates in a handful of tokens, why not just sample from those and ignore the rest?

The elegance of this solution is that it makes almost no assumptions about the shape of the distribution. It does not try to model why probability falls off or where the "natural" cutoff should be. It simply imposes a hard count-based boundary and accepts the consequences. This makes top-k both easy to understand and easy to implement, which explains why it became so widely adopted despite its simplicity.

Think of top-k sampling as a casting director who says: "I will only consider actors in the top kk of my ranking. Everyone else is not being called back, regardless of how close they were to the boundary." The director does not re-evaluate the top actors based on who was excluded; they simply rescale their assessments of the remaining candidates. Someone ranked 3rd out of 50,000 is still ranked 3rd out of the top kk, and their relative standing compared to other top-kk candidates is unchanged.

This analogy also highlights an important subtlety: the excluded tokens receive zero probability, not some small residual probability. This is a hard cut, not a soft fade. The benefit is that we guarantee no sampling from the tail. The cost is that tokens just outside the boundary (ranked k+1k+1 and k+2k+2) receive zero probability even if they were only slightly less probable than the token at rank kk. Whether this boundary effect matters in practice depends on how steeply the probability drops off near rank kk.

The Intuition: Focus on What Matters

Think about how you complete sentences. Given "The capital of France is", you do not mentally consider every possible word in English. You immediately focus on a small set of plausible continuations: "Paris", maybe "a", "the", or "known". Your mental probability distribution is not uniform across all words, and neither is the model's. Top-k sampling formalizes this intuition by explicitly restricting attention to the most likely candidates.

The approach works in five steps:

  1. Compute probabilities: The model outputs a distribution over all vocabulary tokens
  2. Rank by likelihood: Sort tokens from most to least probable
  3. Keep the top k: Select only the kk highest-probability tokens
  4. Zero out the rest: Set all other token probabilities to exactly zero
  5. Renormalize and sample: Rescale the remaining probabilities so they sum to 1, then sample
Top-k Sampling

Top-k sampling restricts the sampling space to the kk most probable tokens. After zeroing out tokens outside the top-kk, the remaining probabilities are renormalized before sampling.

From Intuition to Mathematics

Let us formalize this process. At generation step ii, the model has seen context x<ix_{<i} (all previous tokens) and outputs a probability P(xi∣x<i)P(x_i | x_{<i}) for each possible next token xix_i. This distribution spans the entire vocabulary, but we only want to sample from the top performers.

First, we need to identify which tokens make the cut. We define VkV_k as the set containing the kk tokens with highest probability. Formally, Vk={v1,v2,…,vk}V_k = \{v_1, v_2, \ldots, v_k\} where P(v1∣x<i)≥P(v2∣x<i)≥…≥P(vk∣x<i)P(v_1 | x_{<i}) \geq P(v_2 | x_{<i}) \geq \ldots \geq P(v_k | x_{<i}) and every token outside VkV_k has probability no greater than the smallest element of VkV_k. If the vocabulary has 50,000 tokens and k=50k=50, then VkV_k contains exactly the 50 most likely tokens for this specific context.

Now we need to handle a subtle but important issue: after removing tokens from consideration, the probabilities of the remaining tokens no longer sum to 1. A probability distribution must sum to 1 for sampling to work correctly. If the top-kk tokens collectively hold 85% of the probability mass, the removed tokens held the other 15%, and our truncated distribution sums to only 0.85. The solution is renormalization: we divide each remaining probability by the total mass of the kept tokens, scaling everything up so the distribution is valid again.

To compute the renormalization constant, we sum the probabilities of all top-kk tokens:

Zk=∑x∈VkP(x∣x<i)Z_k = \sum_{x \in V_k} P(x | x_{<i})

where:

  • VkV_k: the set of kk tokens with highest probability
  • P(x∣x<i)P(x | x_{<i}): the model's original probability for token xx given context x<ix_{<i}
  • ZkZ_k: the total probability mass held by the top-kk tokens, which acts as a normalizing constant

This gives us the formal definition of top-k sampling. Given the original distribution and the normalization constant, the truncated distribution assigns probability:

Pk(xi∣x<i)={P(xi∣x<i)Zkif xi∈Vk0otherwiseP_k(x_i | x_{<i}) = \begin{cases} \frac{P(x_i | x_{<i})}{Z_k} & \text{if } x_i \in V_k \\ 0 & \text{otherwise} \end{cases}

where:

  • xix_i: a candidate token at position ii
  • x<ix_{<i}: the context (all tokens before position ii)
  • P(xi∣x<i)P(x_i | x_{<i}): the original probability the model assigns to token xix_i given the context
  • VkV_k: the set of kk tokens with highest probability under P(⋅∣x<i)P(\cdot | x_{<i})
  • Zk=∑x∈VkP(x∣x<i)Z_k = \sum_{x \in V_k} P(x | x_{<i}): the normalization constant, which equals the sum of probabilities of the top-kk tokens
  • Pk(xi∣x<i)P_k(x_i | x_{<i}): the renormalized probability used for sampling

Why does this formula make sense? Notice that for tokens inside VkV_k, dividing by ZkZ_k (which is less than 1) makes each probability larger than the original. This is correct: we are redistributing the probability that was held by the excluded tokens and giving it proportionally to the included tokens. For tokens outside VkV_k, the formula assigns zero. The resulting distribution sums to exactly 1 because ∑x∈VkPk(x∣x<i)=∑x∈VkP(x∣x<i)/Zk=Zk/Zk=1\sum_{x \in V_k} P_k(x | x_{<i}) = \sum_{x \in V_k} P(x | x_{<i}) / Z_k = Z_k / Z_k = 1.

Why Renormalization Preserves Relative Probabilities

A necessary property of this construction is that renormalization preserves the relative likelihood of tokens within the top-kk. This matters because the model's ranking captures important information about which tokens are more semantically appropriate than others, and we want to honor that information.

Consider two tokens: "Paris" with original probability 0.30 and "the" with probability 0.15. In the original distribution, "Paris" is exactly twice as likely as "the".

After truncation, suppose the top-kk tokens have cumulative probability Zk=0.90Z_k = 0.90. The renormalized probabilities become:

  • "Paris": 0.30/0.90=0.3330.30 / 0.90 = 0.333
  • "the": 0.15/0.90=0.1670.15 / 0.90 = 0.167

Notice that 0.333/0.167=20.333 / 0.167 = 2. The ratio is preserved! This happens because we divide both probabilities by the same constant ZkZ_k. Renormalization scales all probabilities equally, maintaining their relative ordering and ratios.

This property matters because it means top-k sampling respects the model's preferences among plausible tokens. We are not arbitrarily reweighting tokens; we are simply restricting which tokens can be sampled while honoring the model's ranking within that restricted set. If you trust the model's learned distribution over the top candidates, renormalized top-k sampling samples from that distribution faithfully.

The key insight is that top-k sampling changes the support of the distribution (which tokens have non-zero probability) without changing the shape of the distribution over the supported tokens. The relative probabilities among kept tokens are identical to what they would be under the original distribution, up to the constant scaling factor 1/Zk1/Z_k.

Visualizing the Truncation

The following visualization shows this process concretely. On the left, we see the original distribution over the top 20 tokens. On the right, we see the truncated distribution after keeping only k=10k=10 tokens and renormalizing.

Out[8]:
Visualization
Horizontal bar chart showing top 20 token probabilities. The top 10 bars are blue (kept) and the bottom 10 are gray (truncated).
Original distribution (top 20 tokens shown). Blue bars indicate tokens kept in top-10, gray bars are truncated.
Horizontal bar chart showing the 10 kept tokens with renormalized probabilities that sum to 1.0.
Top-10 truncated distribution after renormalization. Probabilities now sum to 1.