L1 Beginner · Module 1: The LLM forward pass, for inference engineers
1.4Autoregressive generation and sampling basics
An LLM produces one token per forward pass and feeds it back in to get the next. That loop is why answers stream word by word, why latency splits into TTFT and TPOT, and why decode, not prefill, dominates serving cost.
By the end you will be able to
- ✓Write the autoregressive generation loop and its stop conditions
- ✓Distinguish prefill from decode and connect them to TTFT and TPOT
- ✓Explain greedy decoding and temperature, and what each does to the distribution
- ✓Estimate end-to-end latency from TTFT, TPOT and output length
The model from 1.1–1.3 outputs one probability distribution over the next token. To produce a whole answer, you run it in a loop:
tokens = tokenize(prompt)while True: logits = model(tokens)[-1] # scores for the next token next_id = sample(logits) # pick one if next_id == EOS or len(out) == max_tokens: break tokens.append(next_id) # feed it back in out.append(next_id) stream_to_user(detokenize(next_id))This is autoregressive generation: each output becomes part of the next input. Token 50 cannot start until token 49 exists, so the steps for one request are strictly sequential. No amount of hardware can run them in parallel.
Two phases
The first pass through the loop is different from the rest:
- Prefill runs the whole prompt through the model in one forward pass. Hundreds or thousands of tokens are processed together, and the pass ends with the first output token. Its duration is most of the time to first token (TTFT).
- Decode is every pass after that. Each one processes a single new token per request. The time between output tokens is the time per output token (TPOT), also called inter-token latency.
Choosing the next token
sample(logits) turns scores into a single id. The two basic knobs:
Greedy decoding always takes the highest-scoring token, . It is deterministic and works well for short factual answers, but it can get stuck repeating itself.
Temperature divides the logits before softmax:
- sharpens the distribution toward the top token. is greedy.
- uses the model’s distribution as trained.
- flattens it, so less likely tokens get picked more often.
Here are the same logits for “The capital of France is ___” at three temperatures, from the lab below:
| Token | |||
|---|---|---|---|
| Paris | 0.955 | 0.706 | 0.509 |
| the | 0.026 | 0.117 | 0.153 |
| a | 0.014 | 0.086 | 0.125 |
| France | 0.003 | 0.039 | 0.074 |
Top-k, top-p, min-p and beam search refine this further; they are covered in Module 11.
When does it stop?
A request ends when the model emits an end-of-sequence token, when it hits max_tokens, or when the output matches a user-supplied stop
string. For a serving engine this means output length is unknown when a request arrives, so it cannot know in advance how long a request will
hold its memory. That uncertainty drives the scheduling problems in Module 10.
Lab: temperature in action 🟢
def sample(logits, temperature, rng): if temperature == 0: return int(np.argmax(logits)) # greedy p = softmax(logits / temperature) return int(rng.choice(len(p), p=p))python labs/m01/1.4/sampling.py20 samples at each temperature: T=0 1 distinct e.g. Paris, Paris, Paris, Paris, Paris, Paris T=0.5 1 distinct e.g. Paris, Paris, Paris, Paris, Paris, Paris T=1.0 6 distinct e.g. Paris, Paris, Paris, Paris, Paris, Paris T=1.5 5 distinct e.g. the, Paris, the, Paris, Paris, LyonSources
Check your understanding
0 / 4 answered1.Why can a single request's output tokens not be generated in parallel?
2.TTFT is 200 ms and TPOT is 20 ms. About how long does a 501-token answer take?
3.What does setting temperature below 1 do?
4.Why is output length a problem for a serving engine?