Tokenwerk · The LLM textbook

Chapter 38 · VII · Build your LLM · 5 minutes

Your own project: building further

Your model runs. Now you make it your project: your own data, a bigger model, honest evaluation and an extension you build yourself.

Done – and now?

You built a language model from scratch: tokenizer, data preparation, GPT with attention, training, generation, chat and modern components. You can explain every line of it. Very few of the people who work with ChatGPT every day can say that.

This last chapter is your springboard. It isn't about new theory anymore, but about turning your model into your project: your own data, a bigger model, honest evaluation, an extension of your own. Pick whatever excites you.

Project 1: your own data

The most exciting step first: train on text that interests you. A few ideas:

  • A book from Project Gutenberg: poems, legends, a novel. Just change URL and load_documents in prepare.py.
  • Your own texts: diary entries, song lyrics, recipes, club minutes: anything you have and are allowed to use.
  • A mix, for example stories and fairy tales, to see whether the model blends the styles.

For prepare.py to handle your own text, you only need to adapt load_documents: it should return a list of documents. Nothing more.

Rule of thumb: below 100,000 tokens the model mainly memorizes. From a million on it gets noticeably better. And: no personal data of other people (chapter 18).

Project 2: going bigger

Our model has just under a million parameters. With a graphics card, or for free in Google Colab, you can go much bigger. Change the Config in train.py, for example:

python
cfg = Config(vocab_size=tok.vocab_size, context=256, dim=384, heads=6, layers=6, modern=True)

That is about 11 million parameters. For that you also need more data (chapter 27: roughly 20 tokens per parameter, so about 200 million tokens) and more steps. A good way: the full TinyStories training set, about 2 GB of text.

When scaling up, stick to the rules from chapter 27: only change one thing at a time, measure tokens per second, test briefly first, then train long.

Project 3: honest evaluation

Is your model good? Put together the 20 test questions from chapter 26: for stories, for example, 20 beginnings that never appeared in training. Rate the answers with the scoring table from chapter 26. Compare:

  • classic against modern,
  • before SFT against after SFT,
  • small model against big model.

Write everything down in a table. A model that does better on your test questions is better, not one that happened to spit out a particularly nice sentence.

Project 4: build something in yourself

Pick exactly one extension and build it yourself:

  • Top-p sampling in generate.py (chapter 21). Easy, a good start.
  • GQA (chapter 23): fewer key/value heads than query heads. Medium.
  • A KV cache (chapter 23): keep keys and values while generating. Demanding. The golden rule: with and without the cache, exactly the same tokens must come out.
  • A mini MoE (chapter 28): two to four MLPs with a router. For advanced readers.

For every extension: first think about what should measurably improve, then build it, then check.

The reference project

The book also comes with a bigger reference project as a download: project zip. It does the same as your model, just with more equipment: resuming training after an interruption, gradient accumulation, top-p, GQA, a test collection and an evaluation script. Once you understand your own model, you can read the code there line by line and recognize almost everything.

Your final report

Whoever built something should also be able to describe it. Write one to two pages:

  • Goal: what should your model be able to do?
  • Data: where from, how much, what did you filter?
  • Model: settings and parameter count.
  • Training: steps, time, best validation loss.
  • Results: example texts and the points from your evaluation.
  • Limits: what doesn't work yet, and what would you try next?

That way someone else can rebuild your result. And while writing, you notice yourself what you have understood.

What you can do now

You can explain how a letter becomes a token and then a list of numbers. You can compute and program an attention row. You know why the model doesn't look into the future, how it learns from mistakes and why it sometimes talks nonsense. And you have trained your own model that writes and answers.

The big models are still an enormous feat of engineering after this, but no longer a black box. Congratulations!

Key points

  • Next steps: your own data, a bigger model, honest evaluation, an extension you build yourself.
  • Only change one thing at a time and measure. A report makes your result reproducible for others.
  • The reference project shows the same ideas with more equipment; your code is the key to it.