Build a Tiny Autoregressive Generation Loop
You now know every station, so model.generate() has no remaining excuse to be a black box. This lesson rebuilds it: a working generation loop in a dozen lines, each line something the course so far already taught. Writing it yourself is the point, and so is the flaw you will build into it, on purpose, because noticing that flaw is how the course gets to prefill, decode, and KV memory.
The loop, written down
No Paris, exactly as the lesson on logits and sampling promised: a leads the real distribution, Paris runs second, and greedy takes the leader every time. The loop is behaving exactly as built. Keep this run in mind whenever a demo's output looks "wrong": the machine is faithfully following the distribution it computed, and the surprise is information about the model, not a bug in the loop.
Read it against the module. The forward pass is the trace from the forward-pass trace with the photographs turned off. logits[0, -1] is the one-row-of-six fact from logits and sampling as an index. argmax is greedy selection; swapping in temperature and a draw is three lines you could write from logits and sampling. The eos check and the budget are the two stop conditions from token generation and streaming, now from the inside. And the torch.cat is the whole autoregressive idea in one call: the output re-enters the input, once per token.
That word can now be defined in passing, because you just built it: autoregressive means each output is computed from all the outputs before it. A 500-token answer is 500 trips through this loop, which is the fact what inference is opened the course with, finally standing on a full mechanism.
Common misconception. There is no finished answer sitting anywhere, being revealed a word at a time. Each token is computed fresh, conditioned on everything before it, and the model has not committed to what it will say four tokens from now: when our loop chose a, "city of many" did not exist yet anywhere. Nothing is looked up either: the continuation was not retrieved from a table, it was recomputed from the weights, the same way, on every single request. Applications can bolt retrieval on around the model; the loop alone has none.
Inspect this. The production version of loop.py is real code you can read. Open src/transformers/generation/utils.py in the Transformers repository and find the while loop inside _sample, the method generate() dispatches to: forward pass, select token, append, check stop condition. Twenty lines of reading, and the library you have used since running your first LLM becomes this lesson with error handling.
The flaw you just built
Watch what the loop feeds the model. Step one ran the forward pass on 6 positions. Step two ran it on 7, the original prompt plus a, and recomputed every station for every one of them, including the six positions whose vectors, attention patterns, and logits the model had already computed one step earlier, and whose results cannot have changed, because nothing behind them changed. Step three redid it all again, one position longer.
Causal attention told you what each new token actually needs from the past: the keys and values of every earlier position. Our loop obtains them by rebuilding the entire past from scratch, every step. For four tokens on one prompt this is invisible; at real answer lengths it is the single most expensive habit in the program.
That waste has a name, a precise size, and a fix serving systems are organized around. All three are prefill, decode, and KV memory, whose opening lesson asks the question this loop leaves you holding: why does autoregressive generation recompute work?
The machine is no longer a black box: you have traced it, read its stations, and rebuilt its loop by hand. One lesson remains in this module, the visual lab, where the whole trip runs on one screen.