Lesson 8.2core3 h 30 min⚡ live demo

Fine-tuning and PEFT

Teach a model new behavior with a fraction of the parameters

Fine-tuning is how a general model becomes your model — a consistent style, a domain vocabulary, a structured output format, or a small cheap model that imitates a big one. Full fine-tuning of billions of parameters is out of reach for most people, so parameter-efficient methods, above all LoRA, carry the field. The low-rank idea comes straight from 1.3 Eigen-things, SVD and low rank; the training mechanics from 4.5 Optimizers and learning-rate schedules; and the decision of whether to fine-tune at all depends on what retrieval (9.1 Why RAG, and the canonical pipeline) and prompting (8.4 Prompting as programming) can already do.

You'll be able to

  • Decide between prompting, retrieval and fine-tuning for a concrete requirement
  • Prepare an SFT dataset with the correct chat template and loss masking
  • Derive LoRA's parameter savings and implement it with Hugging Face PEFT
  • Explain QLoRA's 4-bit NormalFloat quantization and why adapters stay in higher precision
  • Detect and mitigate overfitting and catastrophic forgetting in a fine-tune

Key ideas

  1. Retrieve for facts, fine-tune for form: fine-tuning changes behavior, style and skills, not up-to-date knowledge.
  2. SFT success depends on the chat template, loss masking on assistant tokens, and a small set of high-quality examples.
  3. Full fine-tuning costs ~16 bytes per parameter with Adam in mixed precision, so a 7B model needs ~112 GB before activations.
Show 4 more key ideas
  1. LoRA learns ΔW = (α/r)·BA with B zero-initialized; r(d+k) parameters instead of dk — e.g. 0.39% for a 4096² matrix at r = 8.
  2. Merging W₀ + (α/r)BA restores the original architecture, so LoRA adds no inference latency.
  3. QLoRA stores frozen weights in 4-bit NormalFloat with double quantization and keeps adapters in higher precision — a 65B fine-tune fits on one 48 GB GPU.
  4. Always evaluate the fine-tune against a well-prompted baseline on a held-out set, and watch for catastrophic forgetting.

Suggested path through this lesson