Top P and Temperature

Temperature

The usual order in most implementations (OpenAI, Hugging Face, etc.) is:

  1. Apply temperature first -> this reshapes (sharpens or flattens) the probability distribution.
  1. Apply top-p (nucleus sampling) next -> from that temperature-adjusted distribution, collect the smallest set of tokens whose cumulative probability >= p, then sample from that set.

Top P

The model produces a probability distribution over all possible next tokens. Example (ASSUME top p is set to .8):

With top_p = 0.8, we build the smallest set of tokens whose cumulative probability >= 0.8:

A (0.40) + B (0.30) = 0.70 (still below 0.8).

Add C (0.15) -> total = 0.85 >= 0.8.

So the nucleus set is {A, B, C}, which made up .85

This means:

Renormalization

We rescale {A, B, C} to sum to 1:

Then sample randomly

Now the model draws one token at random from this set, weighted by these renormalized probabilities.

To do this the model generates a random number between 0 and 1