Boss lesson · Unit 03
One token at a time
The generation loop, sampling and the KV cache
What you practise
- The autoregressive loop, greedy vs sampling, temperature, top-k and top-p worked on real numbers, stop tokens, the KV cache and why the first token is the slow one
Try a question from this lesson
Pick one
One run of the model yields probabilities for the next token only. How does a model write a 100-token answer?
Correct answer
It runs 100 times, feeding each chosen token back in
Why
Generation is a loop: predict a distribution, choose one token, append it to the input, run again. The model writes the way it was trained to predict, one token at a time, each conditioned on everything before it.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Temperature
Sampling draws the next token at random, in proportion to its probability. Temperature reshapes the distribution first: the logits are divided by T before the softmax.
T below 1 sharpens it: likely tokens become even more likely. T above 1 flattens it: unlikely tokens get a real chance. As T approaches 0, sampling turns into greedy decoding.
Concept
The KV cache
Run naively, the loop re-reads the whole sequence for every new token.
It does not have to. With a causal mask, the keys and values of earlier tokens never change when a token is added. So they are stored, and each step only computes the newest token: its query against the cached keys and values.
10 more exercises in the app
- 1× order the lines
- 3× multiple choice
- 4× type the answer
- 1× fill the blanks
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.