Inside a Foundation Model
How a raw text predictor becomes an assistant, and why the same prompt can return a different answer, or a made-up one.
Warm-up · from Lesson 1
Recall these before reading on. Spacing your review across sessions is what moves an idea into long-term memory.
1 · What makes one model differ from another
You'll mostly use models, not build them. Still, Huyen traces most differences between models to three decisions: training data, architecture and size, and post-training. Ch2 intro
- Data. A model can't do what its data never showed it. With no Vietnamese in the training data, there's no English→Vietnamese translation. Quality can beat quantity: a 1.3B-parameter model trained on 7B tokens of high-quality code outperformed much larger models on coding benchmarks. Ch2 · Training Data
- Architecture. Nearly every model you'll use is a transformer. You can skip its internals for now.
- Size is measured in parameters: “Llama-13B” has 13 billion. Bigger usually means more capable within a model family, though newer small models often beat older big ones: Llama 3-8B beats Llama 2-70B on MMLU. Ch2 · Model Size
Napkin math you'll reuse Each parameter stored in 16 bits takes 2 bytes, so a 7B model needs at least 14 GB of GPU memory just to run inference. Ch2 · Model Size Chapter 7 builds on this arithmetic.
2 · From monster to assistant: the training pipeline
Why SFT is needed. A pre-trained model only completes text. Give it “How to make pizza” and “for a family of six?” is a perfectly valid completion. SFT trains it on high-quality demonstration data, (prompt, response) pairs, often written by highly educated labelers at up to 30 minutes per pair. Ch2 · Supervised Finetuning
Why preference finetuning is needed. Demonstrations teach the model how to converse but not which conversations to have. In RLHF, you first train a reward model that scores responses, then optimize the language model to earn high scores. The reward model learns from comparison data, (prompt, winning response, losing response), because people agree far more on “A beats B” than on a 1–10 score. DPO is a simpler alternative; Meta switched to it for Llama 3. Ch2 · Preference Finetuning, Reward model
Pre-training [is like] reading to acquire knowledge, while post-training is like learning how to use that knowledge.Huyen, Ch. 2, “Post-Training”
3 · Sampling: how the model picks each token
For every next token, the model outputs one logit (a raw score) per token in its vocabulary. Softmax turns the logits into probabilities. Then a token is sampled. Huyen calls sampling “perhaps one of the most underrated concepts in AI.” Ch2 · Sampling
| Knob | What it does | Typical values |
|---|---|---|
| Greedy | Always takes the most likely token. Fine for classification, boring for text. | = temperature 0 |
| Temperature | Divides the logits before softmax. Lower values sharpen the distribution (consistent); higher values flatten it (creative, less coherent). | Providers allow 0–2; 0.7 is common for creative work; 0 for consistency |
| Top-k | Keeps only the k most likely tokens, which also cuts the softmax computation. | 50–500 |
| Top-p (nucleus) | Keeps the smallest set of top tokens whose probabilities add up to p, so the pool size adapts to the context. | 0.9–0.95 |
| Stopping condition | A maximum token count or stop tokens. Saves latency and cost, but can cut off JSON mid-object. | — |
A worked example from the book: logits [1, 2] for tokens A and B. At temperature 1, B gets 73%. At temperature 0.5, B gets 88%. Ch2 · Temperature
Try it: do the challenges below with the knobs
Before each challenge, predict which knob you'll need, then check whether you were right.
Toy logits invented for this exercise; the context “My favorite color is” is from Fig. 2-14. Click “Sample 20 times” to see the randomness for yourself.
Test-time compute. Instead of a single response, generate several and pick the best (best-of-N), using a scorer, the highest overall probability, or the user's choice. It reliably helps, but two outputs cost about twice as much as one. Ch2 · Test Time Compute
4 · The probabilistic nature of AI
Anything with a non-zero probability can come out, “no matter how far-fetched or wrong.” Ch2 · The Probabilistic Nature of AI That causes two problems you'll fight for the rest of the book:
Inconsistency
The same input can give different outputs (Huyen got 3/5 and then 5/5 grading the same essay), and a slightly different input can give a very different output. Mitigations: cache answers; fix temperature, top-p, and top-k; fix the seed. Even then, identical outputs aren't guaranteed, because the hardware running the model can change results. Ch2 · Inconsistency
Hallucination
A response not grounded in facts. Two complementary hypotheses: Ch2 · Hallucination
- Self-delusion (DeepMind, 2021). The model can't tell the text it was given from the text it generated. After writing “Chip Huyen is an architect,” it treats that as fact and builds on it. Errors snowball.
- Knowledge mismatch (Leo Gao, John Schulman, OpenAI). In SFT the model imitates labelers' answers. When those answers use knowledge the model doesn't have, we're effectively teaching it to make things up.
Practical levers for now: ask for concise answers (fewer tokens means fewer chances to invent), tell it to say “I don't know,” and ground it with context (Chapters 5–6). Detecting hallucinations is covered in Chapter 4.
Check yourself
Read next: Huyen, AI Engineering, Chapter 2: “Post-Training,” “Sampling” (through “Structured Outputs”), and “The Probabilistic Nature of AI.” These are the parts this lesson previews. You can skim “Modeling” (the transformer internals and scaling laws) on a first pass; Huyen herself says to skip any concept you're confident about.
Optional, about 30 min: the first half of Andrej Karpathy's “Intro to Large Language Models” covers the same pre-training → finetuning pipeline from a model builder's view.