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.

~15 min

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.
latency≈TTFT+(nout−1)×TPOT\text{latency} \approx \text{TTFT} + (n_{\text{out}} - 1) \times \text{TPOT}

Choosing the next token

sample(logits) turns scores into a single id. The two basic knobs:

Greedy decoding always takes the highest-scoring token, arg⁡max⁡izi\arg\max_i z_i. It is deterministic and works well for short factual answers, but it can get stuck repeating itself.

Temperature τ\tau divides the logits before softmax:

pi=ezi/τ∑jezj/τp_i = \frac{e^{z_i / \tau}}{\sum_j e^{z_j / \tau}}
  • τ<1\tau < 1 sharpens the distribution toward the top token. τ→0\tau \to 0 is greedy.
  • τ=1\tau = 1 uses the model’s distribution as trained.
  • τ>1\tau > 1 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τ=0.5\tau = 0.5τ=1.0\tau = 1.0τ=1.5\tau = 1.5
Paris0.9550.7060.509
the0.0260.1170.153
a0.0140.0860.125
France0.0030.0390.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 🟢

labs/m01/1.4/sampling.py
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))
Terminal window
python labs/m01/1.4/sampling.py
output (excerpt)
20 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, Lyon

Sources

Check your understanding

0 / 4 answered
  1. 1.Why can a single request's output tokens not be generated in parallel?

  2. 2.TTFT is 200 ms and TPOT is 20 ms. About how long does a 501-token answer take?

  3. 3.What does setting temperature below 1 do?

  4. 4.Why is output length a problem for a serving engine?