liveBook · Manning

Top-p filtering

As explained in Build a Reasoning Model (From Scratch)

A decoding method that restricts sampling to the smallest set of tokens whose cumulative probability reaches a chosen threshold p.

Top-p filtering, also called nucleus sampling, limits stochastic decoding to the smallest high-probability token set whose cumulative probability reaches a threshold p. It reduces accidental sampling of unrelated, low-confidence tokens while preserving more diversity than greedy decoding. section 4.5 section 4.7

Apply the filter after temperature scaling and softmax, but before sampling:

  • Sort token probabilities in descending order and compute their cumulative sum. section 4.5.1
  • Keep the prefix whose cumulative mass is below p; the common variant uses prefix mass before each token, thereby retaining the token that first crosses p. section 4.5.1 section 4.5.2
  • Zero out discarded probabilities, scatter retained values back to their original vocabulary positions, and renormalize them to sum to one. section 4.5 section 4.5.2
  • Sample from the filtered distribution with torch.multinomial; bypass filtering when top_p is unset or at least 1.0. section 4.5.2

A p = 0.8 cutoff

For a ten-token toy vocabulary, sorted probabilities accumulate as 0.4538, 0.7290, and 0.8119. The simple rule keeps two tokens; the common prefix rule also keeps the third token that crosses the threshold. After filtering, the retained values are renormalized, producing probabilities such as 0.5589, 0.1021, and 0.3390 in the original vocabulary order. With real next-token logits and temperature 0.35, applying top_p=0.8 removes rare outputs such as “Munich” and “Hamburg” while retaining likely city or formatting tokens. section 4.5.1 section 4.5.2

Figure 4.12 Overview of the top-p filtering process. The filter keeps only the highest-probability tokens by sorting them, applying a cumulative cutoff, selecting the top-p subset, and renormalizing the result.

Use the workflow figure to inspect sorting, cumulative cutoff, subset selection, and renormalization as one decoding pipeline. section 4.5

How Build a Reasoning Model (From Scratch) covers it

How deep it goes
Explains sorting, cumulative probabilities, cutoff variants, masking, scattering, renormalization, and generation integration in PyTorch.
Examples
Uses ten-token toy probabilities, p=0.8 plots and tensors, full-vocabulary logits, and city-token sampling outputs.
What it assumes
Assumes next-token logits, softmax probabilities, temperature scaling, and stochastic sampling, while deriving the filter from a toy distribution.
What you can do after
Apply it via PyTorch by inserting top_p_filter between probability conversion and torch.multinomial sampling.
How it connects
Connects top-p filtering to temperature scaling, softmax, multinomial sampling, and self-consistency sampling.

Also explained in

Compare how 12 books explain top-p filtering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Reasoning Model (From Scratch). The section links point to the parts of the book it draws on.