Build an LLM from scratch

Train a small language model on your own device, watch it learn to write short stories, then post-train it to follow a request.

6 labs · about 112 min

In what order?
Learn how it works, then build with it.
Do something first, then see why it works.

Start here · about 10 min

See what a language model does

Start Lab 01
  1. See what a language model doesNot started10 min

    A language model only ever guesses the next piece of text, and seeing it do that makes the rest easier to follow.

    Open Lab 01
    Tick what you did
  2. Follow the maths insideNot started15 min

    A language model guesses the next word with plain arithmetic on lists of numbers, and you can follow each calculation by hand.

    Open Lab 03
    Tick what you did
  3. Explore the design of a transformerNot started20 min

    A transformer is the kind of model inside most of today’s chatbots. Its design decides what the model can notice, so you will see which parts matter and why.

    Open Lab 02
    Tick what you did
  4. Pre-train your modelNot started45 min

    Pre-training is the first and biggest stage of training: the model reads thousands of short stories and learns language by guessing the next piece of text, many thousands of times. You load the stories, cut them into tokens (small numbered pieces of text), train, and then let your model write a story. The loss is a number for how wrong its guesses are: lower is better. To post-train it in the next two labs, train the small size.

    Open Lab 04
    Tick what you did
  5. Post-training 1: teach it a formatNot started10 min

    Post-training starts from the pre-trained model and keeps training it on chosen text. Mid-training mixes in a new kind of text, here simple sums, so you see what the model gains and what it forgets.

    Uses what you learned in Lab 04

    Open Lab 11
    Tick what you did
  6. Post-training 2: fine-tune on labelled examplesNot started12 min

    Supervised fine-tuning (SFT) trains on labelled data: requests, each paired with a good answer written for it. The model learns to follow a request instead of just continuing the text.

    Uses what you learned in Lab 11

    Open Lab 12
    Tick what you did