Unit 3 · Training

Lesson 11 · Available to read

Fine-Tuning and Instruction Tuning

Turning a document-completer into an assistant with supervised fine-tuning, instruction tuning and efficient methods like LoRA.

A base model knows a great deal but behaves like an autocomplete for documents (Lesson 9). Fine-tuning is how we turn it into something that answers questions, follows instructions and speaks in a consistent voice — and how organisations adapt a general model to their own tasks.

Supervised fine-tuning (SFT)

The idea is simple: keep training the pretrained model, but on a much smaller, carefully chosen dataset of demonstrations — examples of exactly the behaviour you want.

Each example is a conversation formatted with the model's chat template: special tokens mark the system message, the user's turns and the assistant's turns. The loss is the same next-token cross-entropy as in pretraining, with one change — it's usually computed only on the assistant's tokens. The prompt is context; the reply is what the model should learn to produce.

An SFT example in chat format: system and user turns are context; the loss is computed only on the assistant's reply.
An SFT example in chat format: system and user turns are context; the loss is computed only on the assistant's reply.

Compared with pretraining, SFT uses:

  • far less data — thousands to millions of examples instead of trillions of tokens;
  • a smaller learning rate and only a few passes over the data;
  • far less compute — often a tiny fraction of the pretraining cost.

Where the demonstrations come from

  • Human-written examples: annotators write ideal answers to diverse prompts. High quality, but slow and expensive.
  • Converted task collections: existing NLP datasets (translation, summarisation, question answering…) rewritten as instructions.
  • Synthetic data: answers generated by a stronger model, then filtered or verified — now very common, especially for code and maths where outputs can be checked automatically.

Quality beats quantity. A 2023 study (LIMA) fine-tuned a strong base model on just 1,000 carefully curated examples and got a surprisingly capable assistant. Its authors argued that almost all knowledge comes from pretraining, and SFT mainly teaches the format and style of good responses — sometimes called the superficial alignment hypothesis. That's an oversimplification, but it captures why a small amount of excellent data goes a long way.

Instruction tuning: generalising to new tasks

Early fine-tuning targeted one task at a time. Instruction tuning fine-tunes on a broad mix of tasks, each phrased as a natural-language instruction. Research around 2021–2022 (e.g. FLAN) found that this teaches models to follow instructions they have never seen — the model learns the meta-skill "read the instruction, do what it says" rather than a list of specific tasks. More diverse tasks and templates led to better generalisation.

Modern assistants are instruction-tuned on huge, varied mixtures: open questions, coding, writing, reasoning, multi-turn chats, refusals of harmful requests, tool-use transcripts and more.

Fine-tuning for your own use case

Beyond building general assistants, fine-tuning adapts a model to:

  • a domain (legal drafting, medical notes, internal jargon),
  • a format (always output valid JSON in a schema),
  • a style or persona (a brand voice), or
  • a narrow task done cheaply by a small model (classification, extraction).

Before fine-tuning, consider cheaper alternatives. Prompting (Lesson 14) is faster to iterate on. If the problem is missing or changing knowledge, retrieval (Lesson 15) is usually better than fine-tuning, because fine-tuned facts are hard to update and models may still hallucinate them. Fine-tuning shines when you need consistent behaviour or format at scale.

Risks

  • Catastrophic forgetting — training hard on a narrow dataset can erode general abilities. Mixing in general data and using small learning rates help.
  • Overfitting — with small datasets, the model may parrot training answers. Hold out a validation set (Lesson 4).
  • Safety regression — fine-tuning can weaken safety behaviours learned earlier, even with benign data; re-evaluate after every fine-tune.

Parameter-efficient fine-tuning: LoRA

Full fine-tuning updates every weight, which for a large model needs enormous GPU memory — the weights, their gradients and optimiser state — and produces a whole new copy of the model.

LoRA (Low-Rank Adaptation) freezes the original weights and learns a small update for selected matrices. Instead of changing a d × d matrix W directly, it learns two thin matrices, B (d × r) and A (r × d), with a small rank r:

W′ = W + B·A

LoRA keeps the big weight matrix W frozen and trains two thin matrices B and A whose product is a low-rank update.
LoRA keeps the big weight matrix W frozen and trains two thin matrices B and A whose product is a low-rank update.

For d = 4,096 and r = 8, the update has 2 × 4,096 × 8 = 65,536 trainable weights instead of 16.8 million — about 0.4%. The insight is that the change needed for adaptation usually lives in a low-dimensional subspace (the Math section's lessons on rank explain why low-rank matrices are so compact).

Benefits:

  • far less memory and compute for training;
  • tiny adapter files (megabytes) that can be swapped on top of one shared base model — one adapter per customer or task;
  • no extra inference cost once the update is merged into W.

QLoRA goes further by storing the frozen base model in 4-bit precision (Lesson 20) while training LoRA adapters, making it possible to fine-tune fairly large models on a single GPU.

What SFT can't do alone

SFT teaches by imitation: the model learns to produce responses like the demonstrations. But many qualities are easier to judge than to demonstrate — which of two answers is more helpful, more honest, less harmful. Learning from those judgements is the subject of Lesson 12.

The takeaway

Fine-tuning continues training a base model on curated (prompt, ideal response) examples; instruction tuning across many tasks teaches it to follow new instructions, and efficient methods like LoRA make this possible by training only a tiny low-rank update.

Practice

0 of 7 attempted

  1. 1Recall

    How does supervised fine-tuning differ from pretraining in data, objective and compute?

    Saved in this browser

  2. 2Explain in your own words

    Why is the loss usually masked so it only applies to the assistant's tokens?

    Saved in this browser

  3. 3Calculate

    A matrix W is 8,192 × 8,192. How many weights does it have? How many trainable weights would a LoRA update with rank r = 16 add? What percentage is that?

    Saved in this browser

  4. 4Multiple choice

    Your company's product catalogue changes weekly and you want an assistant that answers questions about current products. What is usually the best first approach?

  5. 5Explain in your own words

    What did instruction-tuning research discover about generalisation, and why is that important?

    Saved in this browser

  6. 6Recall

    Name three risks of fine-tuning and one mitigation for each.

    Saved in this browser

  7. 7Apply

    Write three high-quality SFT demonstration examples (user prompt + ideal assistant reply) for an assistant that helps people write polite emails. Then name one property all good demonstrations should share.

    Saved in this browser

See what unlocked