Part 11›
✎

06. Capstone 5: Train a Small Language Model#

Level: Professional · Reading time: 25 min · Exercise: 200 h Prerequisites: How Large Language Models Are Trained, The Language Modeling Pipeline and Tokenization, Building Your Own: A Roadmap Through the Assignments

In one sentence: After this project you have built the parts of a language model with your own hands, a tokenizer, a Transformer, an optimizer and a training loop, trained them on a small corpus, and you can read the rest of part 10 as a map of work you have done.

Why this matters#

Part 10 explains how a language model is built. Its source course, Stanford CS336, teaches by building. The roadmap chapter of that part puts it plainly: watching the lectures gives the map, and the assignments are the territory.

This capstone is a roadmap project. It is long, it comes in stages, and each stage ends with a test you can run without a teacher. You may stop after the second stage. You then have a working language model of your own.

The goal#

Build a small language model from its first pieces: a tokenizer, a Transformer, an optimizer and a training loop. Then follow part 10 outward. Make the training fast. Predict how it scales. Build the data pipeline. Align the model with reinforcement learning from verifiable rewards. Each stage teaches one thing.

Three decisions before you start#

Whose tests tell you that you are right? There are two paths.

Path A tells you when you are right. Path B is closer to everyday work.

How much compute can you buy? See the table below. Most of the project needs none.

May an AI tool write the code? The CS336 handouts allow AI tools for conceptual questions and for documentation. They forbid them for implementing any part of an assignment, autocomplete included. Follow the same rule here. The learning is the point. Percy Liang's reason, from the first lecture of the course: coding agents can now solve all the assignments, and a student who feeds one an assignment learns nothing (Building Your Own).

The order, and what each stage teaches#

Stage You build It teaches Chapters of part 10 Compute
1. Tokenizer A byte-level BPE tokenizer How text becomes integers, and the trade-off in vocabulary size 01 CPU
2. Model and loop A Transformer, the loss, an optimizer, a training loop How the pieces fit into one program, and what each costs 02, 03 One GPU, for hours
3. Fast and predictable A profile, one optimization, a scaling-law fit Measure before optimizing. Predict before spending 05, 06, 07 One to several GPUs
4. Data HTML to text, filters, deduplication How far one data decision moves the loss 10 CPU, then a GPU
5. Alignment Supervised fine-tuning, then RLVR with a math grader Generate, score, update 11 One GPU

RLVR is reinforcement learning from verifiable rewards. A program, such as a test or an answer check, gives the reward.

Compute options#

Option Covers Cost
Your laptop's CPU The tokenizer, every correctness check, the data pipeline None
One rented GPU, by the hour Training a small model on TinyStories, a set of short children's stories: from minutes to a few hours By the hour
Several GPUs The distributed parts of stage 3 The largest bill
Free credit The CS336 course page lists a provider with a monthly allowance of free compute None

For prices, see the table in Building Your Own. It lists what one GPU type cost at several providers, with the date. Its worked example puts forty GPU hours between about $200 and $270. The hours are an estimate. Check current prices before you budget.

Milestones and acceptance criteria#

Stage 1: the tokenizer (1 to 2 weeks)#

Stage 2: the model, the optimizer and the loop (6 to 8 weeks)#

This stage is the half-way mark in effort.

Stage 3: fast and predictable (3 to 4 weeks)#

Stage 4: data (3 to 4 weeks)#

Stage 5: alignment (3 to 4 weeks)#

You are done when#

  1. Every box of stages 1 and 2 is checked. You have a language model of your own.
  2. For the full project, every box of stages 3 to 5 is checked too.
  3. A README states what you built, the GPU hours used, what they cost, and what you would do differently.

Effort, tools and cost#

Plan on sixteen to twenty-two weeks, about twenty, at ten hours a week for all five stages, as the roadmap chapter advises for self-study. The core, stages 1 and 2, takes about half of that. You need Python 3.10 or newer, the deep-learning library the assignments build on, PyTorch, and a GPU by the hour for training. Everything else runs on a laptop.

Three shortcuts are reasonable:

Stretch goals#

Pitfalls#

  1. Letting an agent write the code. Fix: use AI to explain, never to implement.
  2. Logic in the adapters, or edited tests. Fix: adapters stay one line long. Tests are never edited.
  3. Debugging on the GPU. Fix: get shapes and round trips right on the CPU first.
  4. Optimizing before profiling. Fix: measure, change one thing, measure again.
  5. Spreading the scaling budget thin. Fix: a few budgets, several sizes at each, and the prediction written before you look.
  6. Not looking at the data. Fix: read rejected and kept samples before you trust a filter.
  7. Stale weights during RL. The inference server and the trainer hold separate copies. Fix: confirm the update before each round, as the roadmap chapter warns.
  8. Buying GPU time before the CPU checks pass. Fix: the last box of stage 2 is a gate.
  9. A loss without its held-out set named. Fix: every reported loss names the data it was measured on.
  10. Comparing runs with different tokenizers or data. Fix: change one thing at a time.

What the practitioners say#

Try it#

The project is the exercise. Start with one hour:

  1. Clone the first assignment repository, or create an empty one.
  2. Run its test command, uv run pytest. Every unimplemented test fails with NotImplementedError.
  3. Make one test pass: encode a short sentence, decode it, and get the sentence back.

You are done with the first hour when one tokenizer test passes, and you have written a five-line plan that says which parts you will do on a CPU and which need a GPU.

Key takeaways#

Self-check#

  1. Why do stages 1 and 2 form a complete project on their own?
  2. What does the CS336 handouts' policy allow AI tools to be used for?
  3. Why is the last box of stage 2, CPU checks before GPU time, placed as a gate?
  4. What must be written down before a scaling-law run, and why?
  5. What are the two copies of the weights during RLVR, and what can go wrong?
  6. What is reward hacking?
Show answers 1. Together they deliver a trained language model of your own. Text becomes integers, a Transformer predicts the next token, an optimizer lowers the loss, and the loop keeps it running. The later stages improve and extend that model. 2. Conceptual questions and documentation, such as library signatures. Implementation is forbidden, autocomplete included. 3. GPU time costs money, and most early mistakes are correctness mistakes that a CPU test finds in seconds. The gate stops you from paying to discover them. 4. The predicted loss for the larger budget. A prediction written after the run is not a prediction. 5. The inference server holds one copy and the trainer holds another. If the server is not refreshed, the model generates with stale weights, and the round learns from the wrong behavior. 6. Passing the grader without doing the task.

Sources#

Next#

→ Checklists for Shipping AI Systems, the short form of everything this part asks for.