Top-k Sampling
A sampling strategy where the selection of the next token is limited to the k most probable options. Everything below the k-th line is discarded.
How It Works
With top_k = 50, the model:
- Sorts all tokens by probability.
- Keeps the top 50.
- Randomly selects from them, considering weights.
Why It's Needed
Without limitation, the model can 'fall' into improbable nonsense — with hundreds of thousands of tokens in the dictionary, the tail of the distribution is enormous. Top-k trims this tail, preserving diversity.
Top-k vs Top-p
| Top-k | Top-p | |
|---|---|---|
| Rule | Exactly k tokens | Minimum pool until probability p |
| Drawback | May cut off necessary tokens in a 'flat' distribution | Adapts to the shape of the distribution |