TECHNOLOGY

Top-k and top-p construct different token candidate sets

In this article
THE QUICK TAKE

Top-k constrains candidate count, while top-p adapts candidate count to the shape of the distribution at each decoding step.

A language model may assign some probability to nearly every vocabulary token. Sampling filters restrict the candidates before a draw. Top-k and nucleus sampling, usually called top-p, answer different questions: how many candidates to keep, and how much probability mass to retain. Their difference matters most when distributions have different shapes.

A fixed count ignores the probability gap

Top-k sorts or otherwise identifies the highest-scoring candidates and keeps a fixed maximum count. Suppose a distribution strongly favors a punctuation token and gives many alternatives tiny probabilities. A fixed count can retain alternatives even when their combined support is small. On another step, many plausible words may have similar probability, yet the same count excludes some. This is a deliberate count constraint. It does not by itself guarantee a semantic diversity level or a particular probability mass across contexts.

A nucleus follows cumulative mass

Top-p orders candidates by probability and retains a prefix that reaches a chosen cumulative threshold under the documented implementation. A sharply concentrated distribution can produce a small nucleus; a flatter distribution can require a larger one. Consider probabilities of 0.6, 0.25 and 0.15 with a threshold of 0.8. The first candidate alone is insufficient, so the next is included. The boundary candidate can make retained mass exceed the threshold; the parameter is not an instruction to split one token’s probability.

Renormalization changes the actual draw

After excluding tokens, the remaining probabilities must be normalized for sampling. In the example, the excluded alternative can no longer be drawn on that step, and the kept candidates share the retained mass. This does not revise the model’s learned vocabulary or permanently ban a word from future contexts. The next generated token changes the input for the following step, where a new distribution and a new candidate set are computed. Local exclusion can nevertheless alter the entire subsequent trajectory.

Filter ordering belongs to the experiment

Temperature affects probabilities and therefore can affect a top-p candidate set. Combining top-k and top-p adds another implementation-dependent boundary: verify the documented processing order rather than assuming two isolated settings commute. Compare candidate identifiers and retained mass for the same saved scores before judging complete answers. Explicitly handle tied scores and minimum-candidate rules if exact reproduction matters. A filtering parameter describes a numerical selection procedure; an application still needs its own evaluation of factual correctness and useful coverage after generation.

Primary research and implementation references

Reproducible inference stacks: preserving a GPU pilot’s result; A GPU inference benchmark you can trust.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles