Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2 · Picking each token: the loop and sampling

In one sentence: the model gives a probability to every possible next token, and a separate step, the sampler, reshapes those probabilities with settings such as temperature and top-p and then draws one.

The problem

Ask the same question twice and you get two different answers. Turn “temperature” up and the text gets creative, then strange; set it to 0 and the answers repeat word for word. The model is the same in every case: what changes is how one token is chosen from its scores, at every step of lesson 1’s loop.

The idea: the model proposes, the sampler picks

Sometimes one token clearly leads ( city had 0.732 in lesson 1). Often none does. After Once upon a time, there was a, the top 10 are:

' young'       0.075
' man'         0.057
' little'      0.040
' group'       0.034
' farmer'      0.025
' very'        0.025
' king'        0.024
' boy'         0.024
' small'       0.021
' big'         0.018
sum of listed: 0.344  (10 candidates)

The ten best hold only 0.344 together. Think of a raffle where each token holds tickets in proportion to its probability: the sampler redistributes the tickets (or takes some away), then draws one.

From scores to probabilities: softmax

The model’s raw output is a score per token, a logit, any number, positive or negative. Softmax turns them into probabilities: raise e to each score and divide by the total, so every result is between 0 and 1 and they add up to 1. Bigger score, bigger share, and differences between scores become ratios between probabilities.

Temperature T divides every score before softmax. T below 1 stretches the differences, so the leaders take more of the tickets; T above 1 squeezes them, so the long tail gets more. T = 0 can’t divide, so it means greedy: take the top token, no draw at all.

The settings, measured

These are the same step’s probabilities after each setting, from llama.cpp’s server (it can report the probabilities after sampling when a request sets "post_sampling_probs": true). The temperature rows were measured with the cuts off (top-k 0, top-p 1, min-p 0):

Settingyoungmanlittlethe 10 shown add up towhat it does
none (temperature 1, cuts off)0.0750.0570.0400.344the model’s own probabilities
temperature 0.5, cuts off0.3260.1870.0910.859sharpens: the leaders gain
temperature 1.5, cuts off0.0170.0140.0110.098flattens: the tail gains
top-k 30.4370.3320.2311.000only the 3 best can be drawn
top-p 0.90.0830.0630.0440.382keep the fewest tokens that add up to 0.9
min-p 0.10.1650.1250.0870.759drop tokens under a tenth of the leader

After each cut, the tokens left are rescaled to add up to 1: top-k 3 keeps 0.075 + 0.057 + 0.040 = 0.172 and divides by it, so young gets 0.437.

Top-p 0.9 kept a long list here (young rose only to 0.083), because no few tokens stand out. Top-p adapts to the step: few tokens when the model is sure, many when it isn’t. Min-p does the same relative to the leader: with 0.1, the cut-off here is 0.1 × 0.075 = 0.0075.

A high temperature with the cuts off is where text goes wrong: the 151,926 tokens nobody wants get a real chance together, and one bad draw derails every step after it, since each token becomes part of the input for the next. The cuts are the guard: in Practical 0, temperature 1.5 with the default cuts still gave readable text.

What llama.cpp does by default

llama-completion --help and the server’s /props give this build’s defaults:

SettingDefaultOff with
--temp0.80--temp 0 (greedy)
--top-k400
--top-p0.951.0
--min-p0.050.0
--seed-1 (a random seed each run)a fixed number

They run as a chain, in this order: top_k, typ_p, top_p, min_p, xtc, temperature (after a few samplers that are off by default: the penalties, dry and top_n_sigma). So the cuts are made on the model’s own probabilities, and temperature reshapes what is left.

Seeds: why “random” can repeat

The draw uses random numbers from a generator started by a seed. Same seed, same settings, same prompt: the same draws, so the same text. Five runs of Once upon a time, 30 tokens each, gave three texts:

RunSettingsText begins
a, b--temp 0, there was a young man named John who lived in a small village. (both identical)
a, b--temp 0.8 --seed 1 in 16th century, the main player in the game was a mouse. (both identical)
c--temp 0.8 --seed 2, I went to a place where I could buy any item I wanted for a price.

A chat app usually picks a new seed per request, which is why the same question gets a different answer.

When the loop stops

The loop ends when the model picks its end token (<|im_end|>, lesson 1’s template), reported by an OpenAI-style API as "finish_reason":"stop", or when the length limit (max_tokens, -n) comes first: "finish_reason":"length", the text cut wherever it was. Asked for the capital of France “in one word”, the model answered Paris in 2 tokens (the word, then the end token); with max_tokens 1 the same Paris came back marked length: the end token never got its turn.

Try it on your Mac

Practical 0 · Your llama.cpp, token by token: install llama.cpp, count tokens with your own script, print each step’s candidates, and break the sampler on purpose.

Go deeper: the arithmetic, checked against the measurements

Softmax with temperature: pi = ezi/T / Σj ezj/T (softmax). Halving T doubles every score, which squares every ratio between two probabilities. Check it on the table: at temperature 1, young / man = 0.075 / 0.057, and squared that is 1.73; at temperature 0.5 the measured ratio is 0.326 / 0.187 = 1.74.

Top-p is nucleus sampling (Holtzman et al., The Curious Case of Neural Text Degeneration): sort, add up from the top until the sum reaches p, drop the rest. Here young’s rescaling tells you what was kept: 0.075 / 0.083 = 0.904, just over 0.9. Min-p (Nguyen et al., 2024) keeps tokens with p ≥ min_p × ptop; here the kept tokens added up to 0.075 / 0.165 = 0.455.

llama.cpp lists every sampler and its flag in the server README and the completion README; --samplers changes the order.

Go deeper: what production APIs do with these settings

The settings belong to each request, not the model: the same server answers a temperature: 0 request from a classifier and a temperature: 1 one from a writing tool side by side.

Greedy is repeatable on one machine and build, but not promised across them: other hardware or another build may round the arithmetic differently, and a near-tie between two tokens can then go the other way, changing all the text after it. This course checked repeatability on one machine and build only.

A length limit doesn’t make an answer shorter; it cuts it. To get short answers, ask for them in the prompt (the model then picks its end token sooner) and keep max_tokens as a safety net, checking finish_reason for "length".

Common misunderstandings

  • “Temperature 0 makes the model more accurate.” It makes it repeatable. In lesson 1, greedy picking stated that Paris is Europe’s largest city.
  • “Top-k 40 always leaves 40 choices.” It leaves at most 40, and top-p and min-p, run after it, can cut further.

Check yourself

1. You run with --temp 5 --top-k 1. Will the text be gibberish?

No. Top-k 1 leaves one token before temperature runs, so there is nothing to choose between: it’s greedy. You’ll check this in Practical 0.

2. With top-p 0.9, one step's leader has probability 0.95. How many tokens can be drawn?

One: the leader alone already reaches 0.9, so top-p keeps only it, and the step is greedy. A step with no clear leader, like Once upon a time, there was a, keeps a long list instead.

3. A test compares a model's output to a saved answer and fails randomly. What two settings make it repeatable?

Greedy (temperature 0), or a fixed seed with fixed settings. Both only hold on the same build and hardware.

Next: Practical 0 · Your llama.cpp, token by token, then lesson 3 · Prefill and decode, and the KV cache.