Top-k and nucleus (top-p) sampling
Truncate the distribution to the k most likely tokens, or to the smallest set covering probability p, then renormalize. Explain how cutting the unreliable low-probability tail avoids derailing tokens.
Builds on: Temperature
What the animation says
The tail adds up
At every step, softmax gives all fifty thousand tokens some probability, never exactly zero. Say twenty sensible tokens hold ninety-five percent. The other fifty thousand share the last five. Each junk token is tiny. Together, they give every step a five percent chance of landing in the tail. And one junk token poisons the rest, because every later token is conditioned on it. So over a hundred tokens, what are the odds of at least one junk token?
Keep the head, cut the tail
Ninety-nine percent. A clean hundred-token run is under one percent likely. The head, the few tokens the model clearly favors, is where its estimates are trustworthy. The tail is a thin smear, there partly because softmax can't output zero. So keep the head, zero the rest, rescale the survivors to sum to one, then sample. Top-k keeps a fixed count. With k of two, A and B survive, summing to point seven. Divide by point seven: about point seven one and point two nine.
Top-p: cover the mass
Top-p, or nucleus sampling, doesn't fix a count. It fixes how much probability to cover. Sort tokens from most to least likely, and add them up as you go. Stop at the first token where the running sum reaches p. That's the kept set. Same five tokens. Running sums: point five, point seven, point eight five, point nine five, one. With p of point eight, tap the last token that top-p keeps.
Size adapts to confidence
C is the cutoff. Keep A, B and C; they sum to point eight five. Divide each by point eight five: about point five nine, point two four, point one eight. Now a confident step: one token at point nine two, a three percent runner-up, then crumbs. Top-p point eight stops at the first token. Only one survives, so it's greedy here. Top-k with k of two always keeps two. Does the three percent token get in?
Both rules in numpy
That three percent is the catch: a fixed k can't tell confident from unsure. Top-p adapts. In numpy, both rules fit in one function. This line sorts, most likely first. Top-k sets how many to keep. For top-p, this line finds where the running sum first reaches p. If both are given, the smaller kept set wins. Then zero the rest and divide by the sum. That's the same as setting cut logits to minus infinity and redoing softmax. Temperature usually comes first, then truncation, then sampling.
Keep the head, cut the tail, rescale
The truncation rule
Five tokens, two rules
Top-k and top-p in numpy
import numpy as np
def truncate(probs, k=None, top_p=None):
idx = np.argsort(probs)[::-1] # most likely first
keep = len(probs) if k is None else k
if top_p is not None:
cum = np.cumsum(probs[idx])
keep = min(keep, np.searchsorted(cum, top_p) + 1)
q = np.zeros_like(probs); q[idx[:keep]] = probs[idx[:keep]]
return q / q.sum()
probs = np.array([0.05, 0.50, 0.10, 0.20, 0.15])
print(truncate(probs, k=2)) # [0 .714 0 .286 0]
print(truncate(probs, top_p=0.8)) # [0 .588 0 .235 .176]
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses