Finetuning
Changing the model itself: when it's worth it, why it eats memory, and how LoRA makes it affordable.
Warm-up · spaced review
1 · What finetuning is
Prompting, RAG and agents all adapt a model through its input. Finetuning adapts it by training it further, so its weights change. It's a form of transfer learning: what the model learned from abundant data (predicting text) transfers to a task with little data. Training a legal Q&A model from scratch might need millions of examples, while finetuning a good base model might need a few hundred. Ch7 · Finetuning Overview
You've met the kinds of finetuning already, and the Lesson 3 question still sorts them. Who writes the answer? Nobody: continued pre-training on raw domain text, such as legal documents. A human writes it: supervised finetuning on (instruction, response) pairs. A human only picks the better of two: preference finetuning. Huyen notes that in practice finetuning is used most often to make a model follow a specific style or format. Ch7 · Finetuning Overview
2 · Facts or form?
Finetuning costs more than prompting in data, hardware and ML skill, and the cost doesn't end at launch. You have to serve the finetuned model, and when a better base model comes out (often), you have to decide whether to redo the work. Finetuning for one task can also degrade others. Huyen's warning from practitioners is that “prompting doesn't work” usually means the prompt experiments were minimal and unsystematic. Ch7 · Reasons Not to Finetune
Specialist models don't automatically win either. BloombergGPT (50B parameters, an estimated $1.3–2.6M in compute) launched in March 2023. In the same month GPT-4 came out, and with zero-shot prompting it beat BloombergGPT on financial benchmarks. Ch7 · Finetuning Domain-Specific Tasks
So once you've done prompting well, the choice between RAG and finetuning depends on how the model fails. Ch7 · Finetuning and RAG
Finetuning is for form, and RAG is for facts.Huyen, Ch. 7
| Information failure → RAG | Behavior failure → finetune | |
|---|---|---|
| Symptom | Wrong, missing or outdated facts | Facts right, but irrelevant, badly formatted or off-style |
| Example | Private company data; last month's events | A rare syntax (a niche DSL or SQL dialect); specs lacking the detail your team needs |
| Evidence | Ovadia et al. (2024): RAG beat finetuning on current events, and base model + RAG often beat finetuned + RAG | Grammarly's finetuned Flan-T5 beat a text-editing GPT-3 variant 60× its size, with 82K examples |
Huyen's suggested order: (0) build your eval pipeline first, then (1) prompt systematically, (2) add examples (1 to 50), (3) add simple retrieval such as BM25 if facts are missing, (4) go further based on the failure: advanced RAG for information failures, finetuning for behavior ones, (5) or both. If the model has both kinds of failure, start with RAG, because it's easier. Ch7 · Finetuning and RAG
Practice: what's the right next step?
3 · Why finetuning eats memory
The rule: memory = (number of values) × (bytes per value). A model stored in 16 bits takes 2 bytes per parameter. Serving needs the weights plus about 20% for activations and the KV cache. Training additionally keeps three numbers for every parameter you train: its gradient and Adam's two optimizer states. Ch7 · Memory Math
Think of packing for a trip. Every parameter is an item, and the number format sets how big each item is. Training means that every item you want to change brings three more items with it. There are two ways to fit in the suitcase: make each item smaller, or change fewer items.
Inference ≈ params × bytes × 1.2
13B × 2 bytes = 26 GB weights → ≈ 31 GB
Full finetune ≈ weights + trainable params × 3 × 2 bytes (+ activations)
7B: 14 GB weights + 7B × 6 bytes = 42 GB → 56 GB
Consumer GPUs have 12–24 GB.
Lever 1: fewer bytes per value (quantization)
Formats: FP32 = 4 bytes, FP16 and BF16 = 2, INT8 = 1, INT4 = ½. Converting to fewer bits is called quantization, and it's cheap and highly effective. Serving in 16, 8 or even 4 bits is now standard, usually by quantizing a finished model (post-training quantization). Training is more sensitive, so it's done in higher or mixed precision. One trap: BF16 and FP16 are both 16 bits but split them differently. Llama 2 shipped in BF16, and teams that loaded it as FP16 got noticeably worse output. Ch7 · Numerical Representations; Quantization
Lever 2: fewer trainable parameters (PEFT)
Parameter-efficient finetuning (PEFT) freezes almost all the weights and trains a tiny number of new ones. The first adapters (Houlsby et al., 2019) came within 0.4% of full finetuning while training 3% of the parameters, but they added layers, and so added latency. PEFT is also sample-efficient: a few thousand examples can be enough, where full finetuning wants far more. Ch7 · Parameter-Efficient Finetuning
4 · LoRA, the default
LoRA (low-rank adaptation) is by far the most-used PEFT method. For a weight matrix W, it keeps W frozen and learns two thin matrices, A (n × r) and B (r × m), whose product has the same shape as W. Only A and B train. Afterwards, A × B can be added into W, so the finetuned model is no slower. The rank r is the knob: 4 to 64 is usually enough, and larger values often don't help. Ch7 · LoRA
Because the adapters are small and separate, one base model can serve many of them (multi-LoRA serving). For 100 customers with one 4096 × 4096 matrix each, storing 100 merged matrices takes 1.68B parameters, while one shared W plus 100 adapters takes 23.3M. Switching customers means loading only an adapter. Apple serves several adapters on one 3B on-device model this way. The catch: LoRA usually doesn't reach full-finetuning quality. Ch7 · Serving LoRA adapters
QLoRA combines both levers: the frozen weights are stored in 4 bits (the NF4 format) and LoRA adapters are trained on top. That let Dettmers et al. finetune a 65B model on a single 48 GB GPU. Ch7 · Quantized LoRA
5 · Try it: will it fit?
This calculator uses Huyen's formulas. Before each challenge, predict which knob you'll need to turn, then try it.
What did you just see? (open after the challenges)
- Serving 13B needs about 31 GB in 16 bits. INT8 (about 16 GB) or 4-bit fits. Quantizing for inference is routine.
- Full finetuning 7B needs 56 GB, three-quarters of it gradients and optimizer states. LoRA cuts the trainable parameters, and 16-bit weights plus a small adapter come to about 14 GB.
- 65B is 130 GB of weights in 16 bits, so LoRA alone isn't enough. You also need 4-bit weights: about 33 GB plus the adapter. That's QLoRA.
6 · If you do finetune
The training itself is the easy part. Frameworks and APIs handle it, and the hard parts are deciding to finetune, getting data (Lesson 9) and maintaining the result. Ch7 · Finetuning Tactics
| Decision | Huyen's default |
|---|---|
| Method | Start with LoRA; try full finetuning later. With only a few hundred examples, full finetuning may not beat LoRA. |
| Where | A finetuning API is quickest but limits the models and knobs. Frameworks (PEFT, Axolotl, unsloth, LLaMA-Factory) give control, on your own GPUs. |
| Learning rate | The step size. A jumpy loss curve means it's too big; slow and flat means it's too small. |
| Epochs | If training loss falls while validation loss rises, the model is overfitting: train fewer epochs. |
| Many tasks | Training tasks one after another risks catastrophic forgetting. Train them together, or separately and then merge the models. |
Check yourself
Read next: Huyen, AI Engineering, Chapter 7. Read “Finetuning Overview,” “When to Finetune” (all of it, this is the part you'll use most), the “Key Takeaways” box and “Memory Math,” “Parameter-Efficient Finetuning,” “LoRA” and “Finetuning Tactics.” She says herself this is the hardest chapter to read: skim the floating-point formats, the LoRA configuration tables and most of “Model Merging.”
If you want one more view: the book's companion repo, chiphuyen/aie-book, has a chapter summary and Huyen's curated reading list for Ch. 7.
Next: Lesson 9, Dataset Engineering (Ch. 8)