← AI Engineering Latent Shelf · Ch.7

Book Study Guide · Chip Huyen · Chapter 7

Finetuning

Finetuning is for form, and RAG is for facts. Prompting and RAG adapt a model through its inputs; finetuning adapts it by changing its weights — the biggest lever, and the biggest commitment.

— the chapter's compass

Adapting a model by adjusting its weights. This chapter covers what finetuning is (transfer learning), when to finetune and when not to (and how it compares to RAG), the memory bottleneck that makes it expensive, and the memory-efficient techniques that answer it — PEFT/LoRA and model merging.

transfer learningfinetune vs RAGmemory & quantizationPEFT / LoRA15 quiz questions
01Overview

Finetuning as transfer learning

To finetune, you start with a base model that has some but not all of the capabilities you need, and train it further to do your task well. It's a form of transfer learning: knowledge gained on a data-rich task (for LLMs, text completion) is transferred to a specialized, data-poor task (legal Q&A, text-to-SQL).

Key concept · Sample efficiency and unlocking capabilities

Transfer learning improves sample efficiency: a good base model learns a task from far fewer examples (training from scratch for legal Q&A might need millions of examples; finetuning a base model, a few hundred). Ideally most of what's needed is already in the base model, and finetuning just refines behavior — OpenAI's InstructGPT framed finetuning as unlocking capabilities the model already has but that are hard to reach through prompting alone. Finetuning is an extension of pre-training; earlier chapters already met two kinds — supervised finetuning (SFT, on (input, output) pairs) and preference finetuning (RLHF/DPO). Self-supervised finetuning on cheap task-related data is continued pre-training.

02When (not) to finetune

Finetuning vs. RAG, and where finetuning sits in the workflow

Finetuning is powerful but rarely the first thing to try. Knowing when it helps — and when a cheaper method wins — saves enormous effort.

Key concept · Reasons not to finetune

Much of what finetuning buys, prompting and context can buy more cheaply. Two big cautions: finetuning for one task can degrade performance on others (bad if your app takes diverse prompts — consider separate models, then merge them), and it carries a high up-front and ongoing cost — you need training data, ML training knowledge (learning rate, overfitting, evaluation), serving infrastructure, and a plan to keep up as new base models arrive faster than you can improve a finetuned one. Also beware the "you must finetune for domain-specific tasks" argument: as general models get stronger, they often beat domain-specific models (GPT-4 outscored BloombergGPT on financial benchmarks).

Key concept · Finetuning is for form, RAG is for facts

Once prompting is maxed out, choose your next step by your model's failure mode. Information-based failures (outputs factually wrong or outdated, or the model lacks private data) → RAG, which supplies knowledge and reduces hallucination. Behavior-based failures (right facts but wrong form — irrelevant, malformatted, not following a syntax like JSON or a domain-specific language) → finetuning, which teaches form and style. Studies found RAG often beats finetuning for up-to-date-knowledge tasks — and the two aren't exclusive: RAG on top of a finetuned model can help further.

Key concept · The adaptation workflow

There's no universal recipe, but a sensible order (with an evaluation pipeline in place from the start): (1) get the model working with prompting and version your prompts; (2) add examples (1–50); (3) if it fails on missing information, add RAG, starting with simple term-based retrieval; (4) then, by failure mode, either add more advanced RAG (information failures) or finetune (behavior failures); (5) combine RAG and finetuning for a further boost. Finetuning sits late because it's the most expensive step to build and maintain.

03Memory bottlenecks

Why finetuning is memory-hungry

Finetuning's number-one challenge is memory: at foundation-model scale, naive finetuning often needs more memory than a single GPU has. Understanding what drives the footprint explains why the efficient techniques work.

Key concept · Backpropagation, trainable vs. frozen parameters

Finetuning needs far more memory than inference because of how models train. Each step has a forward pass (compute output) and a backward pass (backpropagation — update weights from the error). A trainable parameter can be updated; a frozen parameter is kept fixed. During inference no parameters update (light); during finetuning, each trainable parameter also needs memory for its gradients and optimizer states (e.g. Adam keeps two extra values per parameter) plus activations. So the footprint is driven by three things: the number of parameters, the number of trainable parameters, and the numerical representation — and fewer trainable parameters → far less memory, which is the whole motivation for PEFT.

Key concept · Numerical precision and quantization

Each value's bit width sets its memory. FP32 = 4 bytes/parameter, so a 13B model's weights alone take ~52 GB; halve the width and it's ~26 GB. Quantization converts a model from more bits to fewer — a simple, effective way to shrink the footprint. Two rules: inference tolerates very low precision (16, 8, even 4 bits), while training is more sensitive, so it's usually done in mixed precision (some ops in 32-bit, some in 16- or 8-bit). Quantization is what lets a big base model be finetuned on modest hardware (e.g. QLoRA).

04PEFT and LoRA

Finetuning by touching only a few parameters

PEFT (parameter-efficient finetuning) has become the dominant approach: instead of updating all of a model's weights (full finetuning), update only a tiny fraction, slashing the memory needed for gradients and optimizer states while keeping most of the quality.

Key concept · LoRA — low-rank adaptation

The most popular PEFT method is LoRA. It freezes the original weights and learns a small update to them, factored into two low-rank matrices (a big weight update ΔW is approximated as A×B, where A and B are far smaller). Only A and B are trained — often well under 1% of the model's parameters — so gradients/optimizer states shrink dramatically. LoRA is prized for being memory-efficient, modular (train many small adapters for different tasks and swap them over one shared base model), and adding no inference latency once the learned update is merged back into the weights. QLoRA combines a quantized (e.g. 4-bit) frozen base with LoRA adapters, letting large models be finetuned on a single GPU.

Key concept · Adapters vs. soft prompts; PEFT vs. full finetuning

PEFT methods split into two families. Adapter-based (LoRA and kin) insert small trainable modules into the network. Soft-prompt-based (prefix tuning, prompt tuning, P-tuning) prepend trainable "virtual token" vectors to the input and train only those — no natural-language prompt, just learned embeddings. Versus full finetuning, PEFT uses a fraction of the memory and data, is much cheaper to store and serve (one base + tiny adapters), and usually reaches comparable quality — full finetuning still wins when you need to reshape the model deeply and have the resources.

05Model merging & tactics

Combining models, and running a finetuning project

If you finetuned separate models for different tasks, you can fold them into one with model merging — no additional training data required — which is useful for multi-task serving, on-device deployment, and federated setups.

Key concept · Ways to merge models

Three broad approaches. Summing / weight averaging: combine weights of models that share an architecture — simple linear or weighted averaging, SLERP (spherical interpolation), and task arithmetic (represent each finetuned skill as a "task vector" = finetuned − base, then add or even subtract skills; TIES and DARE reduce interference between vectors). Layer stacking: assemble a new model from layers of existing ones (used for mixture-of-experts "upcycling" and "frankenmerges"). Merging can produce a single model that's good at several tasks without paying to serve several models.

Key concept · Finetuning tactics

Running a finetuning project: pick a framework/API and a base model (start from the strongest that fits your budget and licensing; you can distill from a larger model into a smaller one). Tune the core hyperparameters — learning rate (the most important), batch size, and number of epochs — and watch for over/underfitting with your evaluation pipeline. A practical order some teams use is finetune-first vs. distillation-first depending on data and compute; whatever the path, benchmark every step against the same evaluation pipeline so you know each change actually helped.

★Test yourself

Chapter 7 quiz

Fifteen questions on finetuning, memory, PEFT/LoRA, and model merging. Answer first, then expand Show answer.

Part A · Multiple choice

Q1

Finetuning adapts a model by:

  • A Adjusting its weights
  • B Editing the prompt only
  • C Adding retrieval
  • D Increasing temperature
Show answer

A. Prompting and RAG work through inputs; finetuning changes the weights.

Q2

Finetuning is a form of:

  • A Transfer learning
  • B Reinforcement learning only
  • C Clustering
  • D Quantization
Show answer

A. Knowledge from a data-rich task transfers to a data-poor one, improving sample efficiency.

Q3

The chapter's rule of thumb is:

  • A Finetuning is for form; RAG is for facts
  • B Finetuning is for facts; RAG is for form
  • C Always finetune first
  • D Never use RAG
Show answer

A. Behavior/format failures → finetune; missing/outdated info → RAG.

Q4

Why does finetuning need much more memory than inference?

  • A Backpropagation stores gradients and optimizer states for trainable parameters
  • B It uses more tokens
  • C It runs the model twice as fast
  • D It has no reason to
Show answer

A. Inference updates no parameters; finetuning does.

Q5

Storing a 13B-parameter model's weights in FP32 (4 bytes each) takes about:

  • A ~52 GB
  • B ~13 GB
  • C ~4 GB
  • D ~1 TB
Show answer

A. Halving the bit width (to 2 bytes) roughly halves it to ~26 GB.

Q6

Quantization reduces memory by:

  • A Converting values to a representation with fewer bits
  • B Deleting parameters
  • C Adding GPUs
  • D Increasing precision
Show answer

A. Inference tolerates low precision; training usually needs mixed precision.

Q7

LoRA reduces memory by:

  • A Freezing the base weights and training small low-rank update matrices
  • B Training all parameters faster
  • C Removing layers
  • D Using a bigger optimizer
Show answer

A. Often <1% of parameters are trained, so gradients/optimizer states shrink.

Q8

QLoRA combines LoRA with:

  • A A quantized (e.g. 4-bit) frozen base model
  • B Full 32-bit training
  • C RAG
  • D Prompt caching
Show answer

A. Letting large models be finetuned on a single GPU.

Q9

Model merging is useful for:

  • A Combining separately finetuned models into one (multi-task, on-device)
  • B Deleting a model
  • C Collecting training data
  • D Increasing context length
Show answer

A. Via weight averaging/SLERP, task arithmetic, or layer stacking — no extra training data needed.

Part B · True or false

Q10

Finetuning for one task can degrade performance on other tasks.

Show answer

True. A reason to consider separate models (then merge) when prompts are diverse.

Q11

You should always finetune before trying prompting or RAG.

Show answer

False. Finetuning has high up-front and ongoing cost; start with prompting, then RAG, then finetune.

Q12

PEFT reduces the number of trainable parameters to cut memory use.

Show answer

True. Fewer trainable parameters → far less memory for gradients and optimizer states.

Q13

Once merged into the weights, LoRA adds significant inference latency.

Show answer

False. After merging the learned update back, there's no extra inference latency.

Part C · Short answer

Q14

Name the three main contributors to a model's finetuning memory footprint.

Show answer

The number of parameters, the number of trainable parameters, and the numerical representation (bit width).

Q15

In task arithmetic, what is a "task vector"?

Show answer

The difference between a finetuned model's weights and the base model's (finetuned − base); task vectors can be added or subtracted to combine or remove skills.