New Tool Radar

AI Concepts

How to Combine Top-P and Top-K Sampling So Both Rules Apply

Set top-p and top-k together so the token pool is bounded by probability mass and by count, and check the pool size on a test prompt.

Schematic with three steps: Set both values, not one; Check the pool on a flat distribution; Check the pool on a peaked distribution.

Top-p, or nucleus sampling, keeps only the smallest set of most probable tokens whose probabilities add up to top_p or higher, and top-k keeps only the k highest-probability tokens. Used together, both filters apply: a token must be inside the top-k count and inside the top-p probability mass to be sampled. The combination bounds the pool from two sides at once, which is useful when the distribution is sometimes flat, where top-p alone keeps too many tokens, and sometimes peaked, where top-k alone keeps too many.

What you need

  • A generation call where you control the sampling parameters
  • A test prompt that produces both flat and peaked distributions
  • A note of the top-p and top-k values you will try

Step 1: Set both values, not one

Set top_p below 1, for example 0.9, and top_k to a count such as 50, which is the default in the reference config. Setting only one leaves the other side unbounded.

Step 2: Check the pool on a flat distribution

On a prompt where many tokens are plausible, confirm the pool is bounded by top-k: the probability mass rule alone would keep hundreds of tokens, and the count rule cuts it down.

Step 3: Check the pool on a peaked distribution

On a prompt where one token dominates, confirm the pool is bounded by top-p: the count rule alone would keep 50 tokens, but the mass rule keeps only the few that matter.

Worked example: synthetic combined sampling check

The table below is a synthetic example. It shows the expected result of combining top-p and top-k; no text was generated.

Distribution top-p alone top-p plus top-k
Flat – many plausible tokens About 200 tokens kept 50 tokens kept
Peaked – one token dominates 3 tokens kept 3 tokens kept
Very flat Over 500 tokens kept 50 tokens kept
Decision Both set Pool always bounded

Expected result (UNTESTED). the combined setting keeps the pool at 50 tokens on flat distributions and at 3 tokens on peaked ones, so it is bounded from both sides. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • The default top_k in the reference config is 50 and the default top_p is 1.0, which means no filtering until you change them.
  • Sampling needs do_sample set to true; without it, greedy decoding ignores both parameters.
  • Top-p and top-k are filters on the same distribution, so setting both very tight can leave almost no tokens to sample from.
  • The right values depend on the task: factual tasks want a tight pool, creative tasks want a wider one.

Sources

See all