Language Models from Scratch


Language Models from Scratch

Anna
So, this course is all about building language models from scratch. The motivation is pretty striking: researchers are becoming disconnected from the underlying technology. Eight years ago, we implemented and trained our own models. Now, many people just prompt a proprietary model and call it a day.

Michael
Right, and it's not necessarily bad — abstractions let us do more. But the problem is that these abstractions are leaky. You don't really understand what's happening inside, and that limits fundamental research. The course aims to fix that by teaching you to build from the ground up.

Anna
Exactly. Of course, frontier models like GPT-4 are out of reach — they're rumored to be 1.8 trillion parameters and cost $100 million to train. So we'll build small models. But we have to be precise about what we learn: the mechanics, the mindset of scaling and efficiency, and intuitions about data and modeling decisions.

Michael
And the bitter lesson isn't that scale is all that matters — it's that algorithms at scale matter. Efficiency is crucial, especially at large scale where you can't afford waste. Between 2012 and 2019, algorithmic efficiency improved by 44x for image models. That's faster than Moore's law.

Anna
So the right mindset is: given a compute and data budget, what's the best model you can build? That question applies at any scale. Historically, language models go back to Shannon's work on entropy. In the 2000s, Google trained huge n-gram models on two trillion tokens, but those lacked the emergent behaviors we see today.

Michael
Right. The deep learning revolution brought neural language models, sequence-to-sequence models, Adam optimizer, attention mechanisms, and the transformer in 2017. Then OpenAI pushed scaling laws and produced GPT-2 and GPT-3. Now we have both closed models and open-weight models from Meta, DeepSeek, AI2, and others.

Anna
The course is five units, and it's intense. One student said the first assignment was as much work as all five assignments in another class plus the final project. But if you have that obsessive need to understand things all the way down, it's worth it. There's a cluster provided by Together AI with H100s.

Michael
Let's break down the five units. The first is Basics. You implement a tokenizer — specifically BPE — a transformer architecture, and a training loop with optimizer and loss. No cheating with pre-built transformer modules; you write it from scratch using PyTorch primitives.

Anna
Then the Systems unit dives into kernels, parallelism, and inference. You'll learn about GPU architecture, write Triton kernels, and implement data parallelism and a simplified version of model parallelism. Benchmarking and profiling are critical — you need to know where the bottlenecks are.

Michael
The third unit is Scaling Laws. The goal is to run small-scale experiments and predict hyperparameters at larger scales. For example, if you have a compute budget, should you train a larger model on fewer tokens or a smaller model on more tokens? The chinchilla optimal rule says to train a model on about 20 times its parameter count in tokens.

Anna
Unit four is Data. You get a raw Common Crawl dump and learn to process it: filtering, deduplication, training classifiers to get high-quality text. The leaderboard asks you to minimize perplexity given a token budget. It's eye-opening to see how much of the web is just trash.

Michael
Finally, Alignment. After pre-training, you have a base model that completes text. To make it useful, you do supervised fine-tuning on instruction-response pairs, then use preference data or verifiers with algorithms like DPO or GRPO. This unit implements SFT, DPO, and GRPO.

Anna
Everything ties back to efficiency. In this compute-constrained regime, we have lots of data but limited compute. So we filter data aggressively, use tokenization for efficiency, and often train for only one epoch. Scaling laws help us use less compute to find the right architecture size.

Michael
Let's dig into tokenization. The simplest approach is character-based: each Unicode code point is a token. But that gives poor compression — about 1.5 bytes per token — and a huge vocabulary with rare characters wasting space. Byte-based gives a fixed vocabulary of 256 but very long sequences, which is terrible for attention.

Anna
Word-based tokenization is adaptive: you split text into words using regex. But new words get an unknown token, which breaks things. BPE, or byte pair encoding, solves this. It was introduced in 1994 for data compression and later adapted for NLP. GPT-2 popularized it in language modeling.

Michael
The idea is simple: start with bytes, then iteratively merge the most frequent adjacent pair. Each merge creates a new token, expanding the vocabulary. Common sequences become single tokens, rare ones stay as multiple tokens. The algorithm is trained on your corpus and then used to encode any text.

Anna
For example, in the lecture they walked through 'cat and hat'. Bytes like 'c', 'a', 't', space, etc. The first merge might combine 't' and 'space' if that pair appears often. After several merges, 'cat' might become a single token. The key is to store merges in order and replay them when encoding.

Michael
BPE is heuristic but effective. It adapts to the statistics of your data, giving good compression without the issues of word-based or byte-based approaches. In the assignment, you'll implement BPE from scratch and optimize it for speed. It's trickier than it sounds — especially handling special tokens and pre-tokenization.

Anna
It's clear that this course is built around the philosophy that to understand something, you have to build it. From tokenization to parallelism to alignment, you touch every piece. Even if frontier models are out of reach, the mechanics and mindset you learn here are directly applicable.

Michael
Right. And the instructors are candid about what you won't learn: you won't leave ready to lead a billion-dollar model build, but you'll have a solid foundation. The course even has an executable lecture format — a Python program that delivers the lecture content — which is a neat way to blend code and explanation.

Anna
So whether you're following along online or taking it for credit, the materials are all available: lecture videos, assignments, and the cluster. It's a rare opportunity to go end-to-end on language model development. I'm excited to dive into the details in the upcoming sessions.

Ask anything
We've read this whole study set, so questions about it get the best answers.