You know a language model emits a probability distribution over the vocabulary. Next-token prediction is the concrete learning problem and the generation loop: given tokens so far, predict the following token—then append it and repeat.
This objective is why causal masking from Vol. 10 matters, why teacher forcing works in training, and why inference is autoregressive. Later lectures on sampling and search only change how you pick from the predicted distribution.
Learning Objectives
By the end of this lesson, students should be able to:
- Formulate next-token prediction as supervised classification over the vocabulary at each position.
- Explain teacher forcing: train on gold prefixes, not on the model’s own samples.
- Shift labels by one position (
input_idsvslabels) in a causal LM batch. - Implement a tiny PyTorch training step with cross-entropy on shifted logits.
- Describe the generate loop: predict → choose token → append → stop.
- Relate context length limits to the upcoming context window lecture.
Next-token prediction is the task of estimating P(xt | x1,…,xt-1) for each position t. During training, the model sees the true prefix and is scored against the true xt. During generation, the chosen token becomes part of the next prefix.
Training vs Generation
Full sequence in parallel (causal mask).
CE at every position vs gold next token.
One new token at a time.
EOS, max length, or stop string.
| Aspect | Training (teacher forcing) | Generation (inference) |
|---|---|---|
| Prefix source | Ground-truth tokens | Model’s own previous outputs |
| Parallelism | All positions in one forward (masked) | Mostly sequential (KV cache helps) |
| Objective | Minimize cross-entropy | Produce a coherent continuation |
| Error compounding | No (gold prefixes) | Yes—mistakes enter the context |
Label Shift
For input tokens [x1, x2, x3, x4], the model at positions 1–3 predicts x2, x3, x4. In code this is usually logits[..., :-1, :] vs labels[..., 1:], with ignore index on padding.
Why It Scales
- Unlimited self-supervised text.
- No hand-labeled classes needed.
- One head: vocab-sized classifier.
Why It’s Hard
- Huge vocabulary (|V| ~ 32k–256k).
- Long-range dependencies.
- Exposure bias at generation time.
Decoding Choices
- Greedy / beam search.
- Temperature sampling.
- Top-k / top-p truncation.
Code: Toy Causal LM Step
Strengths
- Simple, universal objective.
- Dense supervision (every position).
- Transfers to many downstream tasks.
Tradeoffs
- Teacher forcing ≠ free-running generation.
- Left-to-right bias; bidirectional tasks need other LMs.
- Compute grows with context length.
“At training time the model generates token-by-token like ChatGPT.” Training uses teacher forcing: the full gold sequence is fed under a causal mask, and losses at all positions are computed in one forward/backward. Autoregressive generation is an inference procedure. Confusing the two makes KV-cache and latency discussions impossible to follow.
Knowledge Check
- Short Answer: What does the model predict at position t during training? Answer: The distribution for token xt+1 (the next token) given x≤t.
- True/False: Teacher forcing feeds the model its own previous predictions during training. Answer: False—it feeds ground-truth tokens.
- Multiple Choice: Typical label alignment uses: (a) logits[:-1] vs labels[1:], (b) logits vs labels identical indices with no shift, (c) only the last token. Answer: (a) (with causal LM conventions).
- Short Answer: Name one stopping criterion for generation. Answer: EOS token, max new tokens, or a stop string (any one).
- True/False: Causal masking lets every position attend to future tokens in training. Answer: False.
- Multiple Choice: Exposure bias refers to: (a) dropout, (b) train-on-gold vs generate-on-model prefixes mismatch, (c) FP16 underflow. Answer: (b).
- Short Answer: Why is next-token prediction self-supervised? Answer: Targets are the next tokens already present in raw text—no external labels required.
- Short Answer: What loss is standard for this task? Answer: Cross-entropy (negative log-likelihood) over the vocabulary.
- Multiple Choice: Generation is primarily: (a) fully parallel over future tokens, (b) sequential token-by-token, (c) a single softmax over sentences. Answer: (b).
- True/False: Dense supervision means every non-padded position contributes a loss term. Answer: True.
Key Takeaways
- Next-token prediction is vocabulary-sized classification at each position under a causal mask.
- Training uses teacher forcing and parallel CE; generation appends tokens autoregressively.
- Label shift (
logits[:-1]vslabels[1:]) is the standard wiring. - Decoding strategies only change how we pick from P(next token | context).
- Next: Context Window—how much prefix the model can see.
Hands-on idea: On a whiteboard, write a 5-token sentence and have students fill the (input, target) pairs for each position.
Discussion prompt: How does exposure bias show up in long story generation? (Early wrong entity name poisons later pronouns.)
Recap: Next-token prediction is the LM’s train and generate objective—classify the next vocab id, then loop at inference. Continue with Context Window.