ENع
←LLM training
Lesson 19 · LLMs in practice

How LLMs are trained

14 min10 steps5 questionsAdvanced
About this lesson

A large language model is trained in stages. Pretraining turns raw text into its own labelled examples and teaches the model to predict the next token: predict, score the guess with cross-entropy loss, follow the gradient, update the weights, and repeat billions of times. Fine-tuning on demonstrations and then on human preferences turns the text predictor into a helpful assistant. Watch one training step happen in real numbers on a six-token sentence, then watch it repeat until the model learns that "the" is followed by cat or mat, 50-50.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 19 · Interactive
01 / 10

Start with lots of text

Pretraining starts with a huge pile of text: GPT-3 learned from 300 billion tokens. Ours is one sentence, "the cat sat on the mat": 6 tokens. To keep it simple, each word is one token here.

text    "the cat sat on the mat"
tokens  the | cat | sat | on | the | mat
vocab   cat mat on sat the   (5 different tokens)

GPT-3   300 billion tokens of training text
stage 1 of 3: pretraining
Loading figure…
or ← →
Read the lesson step by step
  1. Start with lots of text

    Pretraining starts with a huge pile of text: GPT-3 learned from 300 billion tokens. Ours is one sentence, "the cat sat on the mat": 6 tokens. To keep it simple, each word is one token here.

    text    "the cat sat on the mat"
    tokens  the | cat | sat | on | the | mat
    vocab   cat mat on sat the   (5 different tokens)
    
    GPT-3   300 billion tokens of training text
    stage 1 of 3: pretraining
  2. The text labels itself

    Every token is the right answer for the token before it. 6 tokens give 5 examples: the→cat, cat→sat, sat→on, on→the, the→mat. Nobody writes labels: the text supplies them. This is called self-supervised learning.

    pairs = list(zip(text, text[1:]))
      example 1   input "the"   right answer "cat"
      example 2   input "cat"   right answer "sat"
      example 3   input "sat"   right answer "on"
      example 4   input "on"   right answer "the"
      example 5   input "the"   right answer "mat"
    
    no human labels: the next token IS the label
  3. Predict the next token

    The model's weights hold a score (a logit) for every possible next token. Given "the", softmax turns its 5 scores into probabilities that add up to 1. Our untrained toy starts every score at 0, so each token gets 0.20: a blind guess.

    p = softmax(W[a])
    
    scores  W["the"] = [0, 0, 0, 0, 0]
    softmax p_j = e^(z_j) / sum of e^(z_k)
    p       cat 0.20  mat 0.20  on 0.20  sat 0.20  the 0.20
  4. Score the guess

    The right answer was "cat". Cross-entropy loss is minus the log of the probability given to the right token: −ln 0.20 = 1.61. A sure, correct guess (probability 1) would score 0. Lower is better.

    loss = -math.log(p[b])
    
    p["cat"] = 0.20
    loss     = -ln(0.20) = 1.61
    p = 1.00 -> loss 0;  p = 0.01 -> loss 4.61
  5. Which way to nudge

    The gradient says how the loss changes as each score moves: p − 1 for the right token, p for the rest. "the → cat" alone gives cat −0.8. Both "the" examples add up: cat (−0.8 + 0.2) = −0.6, mat −0.6, others +0.4.

    G[a][j] += p[j] - (j == b)
    
    the→cat cat −0.8  mat +0.2  on +0.2  sat +0.2  the +0.2
    the→mat cat +0.2  mat −0.8  on +0.2  sat +0.2  the +0.2
    sum G   cat −0.6  mat −0.6  on +0.4  sat +0.4  the +0.4
    negative = raise this score; positive = lower it
  6. Update the weights

    Gradient descent steps against the gradient: W −= lr × G, learning rate 1. The cat and mat scores rise to 0.6; the rest fall to −0.4. Now cat and mat get 0.32 each; the loss for "the → cat" drops to 1.13.

    W[a][j] -= lr * G[a][j]      lr = 1
    
    scores  W["the"] = [0.6, 0.6, -0.4, -0.4, -0.4]
    p       cat 0.32  mat 0.32  on 0.12  sat 0.12  the 0.12
    loss    1.61 -> 1.13
  7. Repeat, many times

    Repeat: predict, score, nudge, update. After 100 steps, "the" is followed by cat 0.497 and mat 0.497. The loss heads to ln 2 ≈ 0.69, not 0: in our text, cat and mat really are 50-50. The model learned probabilities.

    steps  p(cat)  p(mat)  loss "the→cat"
        1   0.322   0.322     1.133
        2   0.383   0.383     0.959
        3   0.414   0.414     0.881
        5   0.445   0.445     0.810
       10   0.471   0.471     0.753
       20   0.485   0.485     0.723
       50   0.494   0.494     0.705
      100   0.497   0.497     0.699
    mean loss over the text: 1.61 -> 0.28
  8. Same loop, giant scale

    Real LLMs run this same loop, but a transformer reads all the earlier tokens, not just one. GPT-3 has 175 billion weights. Chinchilla, 70 billion weights, read 1.4 trillion tokens: about 20 tokens per weight.

                  weights        training tokens
    ours          25             5 examples
    GPT-3 (2020)  175 billion    300 billion
    Chinchilla    70 billion     1.4 trillion
    
    same loss, same gradient descent; batches of
    millions of tokens, many GPUs, and Adam: gradient
    descent with its own step size for each weight
  9. Fine-tune on examples

    A pretrained model only continues text. Supervised fine-tuning trains it on prompts paired with answers people wrote, using the same next-token loss, usually counted only on the answer tokens. InstructGPT used about 13,000 such demonstrations.

    stage 2: supervised fine-tuning (SFT)
    prompt  Q: Capital of France?
    answer  A: Paris.
    
    loss on answer tokens only (the prompt is masked)
    same loop as before, far less data
  10. Learn from preferences

    People compare two answers and pick the better one. A reward model learns to score answers the way people ranked them; reinforcement learning (PPO, Proximal Policy Optimization) then nudges the LLM toward higher scores. Labellers preferred a 1.3-billion-weight InstructGPT to the 175-billion-weight GPT-3.

    stage 3: RLHF
    answers   "Paris."  >  "No idea."   (people pick)
    reward    model trained on those rankings
    PPO       raise the reward, with a penalty for
              drifting far from the fine-tuned model
    DPO       a newer shortcut: no separate reward model

Quiz Unlocks after the last step.