Building Large Language Models


Building Large Language Models

Hope
Welcome back to another episode! I'm Hope, and I'm here with Maria. Today we're diving into something that's been on everyone's mind: how do large language models—like ChatGPT, Claude, or Gemini—actually work? We're going to unpack the key components that go into building these models, from pre-training all the way to making them helpful assistants.

Maria
Exactly, Hope. There's a lot of buzz, but the real magic involves several ingredients: the architecture, the training loss, the data, how we evaluate them, and the systems that make them run. And surprisingly, a lot of the effort in industry goes into data and evaluation, not just fancy architectures.

Hope
Right! So let's start with the basics. At its core, a language model is a probability distribution over sequences of tokens. For example, it can tell you the likelihood of a sentence like 'the mouse ate the cheese' compared to 'the cheese ate the mouse.' The second one should have lower probability because mice don't eat mice—well, usually.

Maria
And that's the generative part—once you have that distribution, you can sample from it to generate new text. Most modern LLMs use an autoregressive approach: they predict the next token given all the previous tokens using the chain rule of probability. So you generate one token at a time.

Hope
That sequential generation is why generating long responses takes time. But during training, you just need to predict the correct next token and adjust the model weights to make that token more likely. The loss function is simple cross-entropy, which is equivalent to maximizing the log-likelihood of the training text.

Maria
Now, before we even get to training, there's this crucial step: tokenization. You can't just feed raw text into a neural network. You need to split it into tokens—subwords or characters. The most common method is Byte-Pair Encoding, where you start with characters and iteratively merge the most frequent pairs. This gives you a vocabulary that balances between efficiency and coverage.

Hope
And tokenization matters way more than people think. For example, numbers can get their own tokens, so the model doesn't always see them as we do. That's why researchers hope we'll eventually move away from tokenizers, maybe with architectures that handle longer sequences efficiently.

Maria
Let's talk about evaluation. One classic metric is perplexity—essentially the exponential of the average loss. It tells you how many tokens the model is 'hesitating' between. A perfect model has perplexity 1, and a random guess has perplexity equal to vocabulary size. In recent years, perplexity on standard benchmarks dropped from about 70 to under 10.

Hope
But perplexity depends on the tokenizer, so it's not perfect for comparing models. More common now are benchmarks like MMLU—a collection of multiple-choice questions across many subjects. You evaluate by comparing the likelihood of correct vs. incorrect answers. But even here, the evaluation setup can vary a lot, leading to inconsistent results.

Maria
Another big issue is data contamination. If the test set was in the training data, the model might just memorize answers. Researchers have clever ways to check for this, like seeing if the model is more likely to generate test examples in the original order than a random order.

Hope
Speaking of data—where does all this training data come from? People often say 'train on the entire internet,' but that's messy. We're talking about petabytes from web crawls like Common Crawl, with 250 billion pages. But raw web pages are full of HTML, boilerplate, and low-quality content. So there's a massive pipeline to clean it.

Maria
Right. Steps include: extract text from HTML, filter out harmful or duplicated content, deduplicate near-identical pages, use heuristics to remove low-quality documents, then train classifiers to identify high-quality sources—like pages linked from Wikipedia—and finally, reweight different domains (like upweighting code and books).

Hope
And at the very end of pre-training, models often go through a 'high-quality data' phase with a lower learning rate to overfit on curated data like Wikipedia. The scale is enormous: modern models like LLaMA 3 are trained on 15 trillion tokens—roughly 50 times the size of older academic datasets like The Pile.

Maria
All this data and compute brings us to scaling laws. What researchers found around 2020 is that if you increase model size, data size, and compute, the loss decreases predictably on a log-log scale. This means you can forecast how much improvement you'll get by scaling up—and critically, bigger models almost always perform better; overfitting is not an issue.

Hope
Scaling laws also help answer practical questions. Suppose you have a fixed compute budget. Should you train a smaller model on more data, or a larger model on less data? The Chinchilla paper showed that for optimal compute, you should use about 20 tokens per parameter. But if you care about inference cost, you might want a smaller model, so the ratio can go up to 150 tokens per parameter.

Maria
These laws are so powerful that they change how we do hyperparameter tuning. Instead of training huge models many times, you train small models at different scales, fit a scaling law, and extrapolate to the large model. That way, you can train the final model for the full budget instead of wasting compute on early experiments.

Hope
And the implications go even further: scaling laws suggest that small architectural tweaks—like activation functions—matter less than data quality and systems. So the recent focus on data and infrastructure makes sense. Now, after pre-training, we need to turn the raw language model into a helpful assistant. That's post-training.

Maria
Post-training starts with supervised fine-tuning (SFT). You take your pre-trained model and fine-tune it on a small set of human-written examples of instructions and desired responses. It's the same next-token prediction loss, but now on high-quality instruction-response pairs.

Hope
And surprisingly, you don't need much data for SFT. Studies like LIMA show that as few as 1,000 carefully curated examples can be enough. The idea is that the model already knows the knowledge from pre-training—it just needs to learn which 'user' to mimic.

Maria
But SFT has limits: it's behavioral cloning—the model can't surpass the average human ability to write answers, and it might even contribute to hallucination because the model is forced to generate plausible-sounding answers it doesn't truly know. That's where reinforcement learning from human feedback (RLHF) comes in.

Hope
RLHF works in stages. First, you train a reward model on human comparisons: given two responses to the same prompt, which one is better? Then, you fine-tune the language model using reinforcement learning to maximize the reward, while adding a penalty for diverging too far from the original model to prevent over-optimization.

Maria
The most famous algorithm is PPO, which is what ChatGPT originally used. But it's complex and tricky to implement. A simpler alternative is Direct Preference Optimization (DPO), which directly maximizes the probability of preferred responses and minimizes the dispreferred ones, without needing a separate reward model.

Hope
And DPO turns out to work just as well—or better—than PPO. Now, collecting human preferences has its own challenges: humans are slow, expensive, and inconsistent—they only agree with each other about 66% of the time on binary choices. So researchers have started using LLMs themselves as judges.

Maria
That's right. For example, AlpacaFarm used a strong model like GPT-4 to generate preferences, achieving higher agreement with human judgments at a fraction of the cost. But you have to be careful: LLMs have biases—they tend to favor longer, more verbose answers. So evaluation tools need to control for such factors.

Hope
And that brings us to evaluation of post-trained models. Because models after RLHF aren't proper probability distributions anymore—they're policies optimized for a specific reward—metrics like perplexity don't apply. Instead, you need human or LLM judges to compare outputs.

Maria
One popular benchmark is Chatbot Arena, where users chat with two anonymous models and pick the better one. It's very human-centric but expensive and slow. Faster alternatives use an LLM as a judge—like AlpacaEval—which correlates highly with human rankings and costs only a few dollars to run.

Hope
But again, you have to watch for biases. LLM judges also prefer longer outputs, and if you simply ask the model to be verbose, its rating can jump dramatically. Researchers now use regression techniques to control for length and other confounding factors.

Maria
Let's not forget systems. Training these massive models requires huge computational resources. For example, LLaMA 3 400B was trained on 16,000 H100 GPUs for about 70 days, costing tens of millions of dollars. And the carbon footprint? About 4,000 tons of CO2—the equivalent of 2,000 round trips from New York to London.

Hope
Efficiency tricks are crucial. Most training uses mixed precision: weights stored in 32-bit, but computations done in 16-bit. Operator fusion—combining multiple operations into a single kernel—can speed things up by 2x. And parallelization strategies like data, model, and pipeline parallelism are essential to keep thousands of GPUs busy.

Maria
We've covered a lot: from tokenization and data pipelines to scaling laws and alignment techniques. If you're interested in diving deeper, courses like CS324 on large language models or CS336 (building an LLM from scratch) are great next steps. Thanks for joining us!

Hope
And remember—the next time you chat with an LLM, there's an incredible amount of engineering and data work behind that simple text box. Stay curious, and we'll see you next time!

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