Sampling chooses the next token stochastically from a probability distribution rather than always selecting the highest-probability token. It adds variability and creative alternatives to GPT text generation. section 5.3.2 section 4.7
A practical token-generation flow is:
- Convert model logits into probabilities with softmax, optionally applying temperature scaling to make the distribution more or less uniform. section 5.3.2
- Restrict choices with top-k sampling by replacing logits outside the k highest values with negative infinity. section 5.3.2
- Sample one token from the remaining distribution, append it to the context, and repeat the prediction cycle. section 4.7
Top-k sampling with k = 3
For a nine-token vocabulary, the three highest logits are 6.75, 6.28, and 4.51. Masking the other logits produces probabilities of 0.5775, 0.3610, and 0.0615 for the surviving tokens; sampling then selects among these three choices. section 5.3.2
Figure 5.15 Using top-k sampling with k = 3, we focus on the three tokens associated with the highest logits and mask out all other tokens with negative infinity (–inf) before applying the softmax function. This results in a probability distribution with a probability value 0 assigned to all non-top-k tokens. (The numbers in this figure are truncated to two digits after the decimal point to reduce visual clutter. The values in the “Softmax” row should add up to 1.0.)
Shows how top-k masking removes nonselected tokens before softmax, leaving a normalized distribution for stochastic selection. section 5.3.2
