Phase 7 · Transformers/
Lesson 7.6core2 h 30 min

Decoder-only: GPT and autoregressive generation

Causal LM objective, in-context learning, the generation loop and the Llama recipe

Every general-purpose LLM you will prompt, fine-tune or wrap in an agent is a decoder-only transformer trained to predict the next token. This lesson explains the objective, why it scaled so well, what in-context learning is, and how the autoregressive generation loop works — the loop that sampling (8.5 Decoding and sampling), the KV cache (7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization) and every agent framework (10.2 Tool use and function calling) build on. It also decodes the modern Llama recipe so you can read any model's config file.

You'll be able to

  • Write the causal language-modelling objective and explain why every token supervises training
  • Summarize what changed from GPT-1 to GPT-3 and define in-context learning
  • Implement the autoregressive generation loop and distinguish prefill from decode
  • Explain why decoder-only models won and list the components of the modern Llama-style recipe

Key ideas

  1. Causal LM objective: maximize Σ log p(x_t | x_<t); with the causal mask every token in a sequence is a training example.
  2. GPT-1 → GPT-2 → GPT-3 kept the architecture and scaled parameters (117M → 1.5B → 175B), data and compute; GPT-3 revealed few-shot in-context learning.
  3. Generation is a loop: forward pass → take the last position's logits → choose a token → append → repeat, until EOS or a length limit.
Show 4 more key ideas
  1. Prefill processes the prompt in parallel; decode produces one token per step and is where the KV cache matters.
  2. Decoder-only won through signal efficiency, a single text-in/text-out interface, native generation and predictable scaling.
  3. The Llama recipe: pre-norm RMSNorm, RoPE, SwiGLU, GQA, no biases, large vocabulary, training far past the compute-optimal token count.
  4. A base model continues text; SFT and preference optimization turn it into an assistant without changing the architecture.

Suggested path through this lesson