Notes
The idea
Supervised fine-tuning is simple at heart. Take a model that is already trained, show it many inputs paired with the ideal output, and keep training so those outputs become more likely. It is the same next token training the model began with, just on your data. Fine-tuning is great at teaching behaviour: a format, a tone, a narrow task done consistently. It is poor at teaching new facts. For facts, use retrieval.
- start: a pretrained model
- show it: input → ideal output
- train: make the ideal output more likely, token by token
- good at teaching: format, style, a narrow task
- bad at teaching: fresh facts (use retrieval)
A training example
A training example is usually one line in a JSON lines file. It holds a short conversation: a system message, a user message, and the assistant reply you wish the model had written. Here the user says they were charged twice in March, and the ideal reply is a small JSON label, billing, high urgency. During training, the loss is usually counted only on the assistant tokens, so the model learns to answer, not to repeat the question.
{"messages": [
{"role": "system", "content":
"Classify the support ticket."},
{"role": "user", "content":
"Charged twice for March"},
{"role": "assistant", "content":
"{\"team\": \"billing\",
\"urgency\": \"high\"}"}
]}
loss on assistant tokens only How much data
How much data? Less than people expect. A few hundred good examples can lock in a format or a style. A few thousand handle a harder narrow task. Quality matters far more than count. Five hundred clean, consistent examples usually beat fifty thousand noisy ones, because the model copies the noise too. Match real production inputs, messy ones included, and keep labels so consistent that two careful people would agree.
- ~100s: a format or style
- ~1,000s: a harder narrow task
- consistent labels: two people would write the same answer
- covers the real input mix, including the weird cases
- looks like production input
- no duplicates of eval data
Train and watch loss
Before training, hold back ten to twenty percent of the examples as a validation set the model never trains on. Say two thousand examples, eighteen hundred for training and two hundred held out. With a batch of sixteen, one pass, called an epoch, is about one hundred and thirteen steps. Each step predicts the tokens, measures the loss, and nudges the weights. You watch two numbers: training loss on seen data, and validation loss on unseen data.
examples 2,000 split 1,800 train / 200 val batch size 16 steps / epoch 1,800 / 16 ≈ 113 epochs 3 → ~340 steps each step: predict tokens, measure cross-entropy loss, backprop, update train loss: fit to seen data val loss: fit to unseen data
Overfitting
Here is a typical run. After epoch one, both losses are around one point two. After epoch two, training loss is zero point eight five and validation zero point nine five. Then they split. Training loss keeps dropping to zero point three, but validation loss climbs to one point one. The model is memorising your examples instead of learning the pattern. That is overfitting. Keep the epoch two checkpoint, or train fewer epochs.
epoch train loss val loss 1 1.20 1.25 2 0.85 0.95 ← best 3 0.55 0.97 4 0.30 1.10 overfit train keeps falling: memorising val turns up: generalising worse fix: keep epoch 2 checkpoint, fewer epochs, lower LR, more data
Forgetting
The second failure is catastrophic forgetting. Train hard on one task and the model can lose skills it had before. It becomes brilliant at tickets but worse at summaries, maths, or politely refusing unsafe requests. To reduce it, use fewer epochs and a lower learning rate, mix some general examples into the training set, and prefer methods that leave the base weights frozen. Most important, test the old skills after tuning, not just the new one.
- after tuning on tickets only: tickets: great summaries, maths, safety refusals: noticeably worse
- reduce it:
- fewer epochs, lower LR
- mix in some general data
- LoRA (base stays frozen)
- always test old skills too
Full fine-tuning
The obvious approach is full fine-tuning: update every weight. It is expensive. With a standard optimiser called Adam and mixed precision, each parameter needs about sixteen bytes of GPU memory, for the weights, the gradients, a full precision copy, and the optimiser's running averages. A seven billion parameter model needs about one hundred and twelve gigabytes before activations. That means several large GPUs, plus a full fourteen gigabyte copy for every task.
per parameter, mixed precision + Adam: weights (16-bit) 2 bytes gradients (16-bit) 2 bytes master copy (32-bit) 4 bytes Adam moments (2 x 32) 8 bytes total ~16 bytes 7B x 16 bytes ≈ 112 GB + activations → several large GPUs output: a full 14 GB copy per task
The LoRA idea
LoRA, short for low rank adaptation, takes a smarter route. Freeze the original weight matrix completely. Next to it, add two thin matrices, A and B, whose product has the same shape as the original. Only those two are trained. The layer's output becomes the frozen output plus a scaled correction from B times A. B starts at zero, so on step one the model is exactly the base. The bet: most tasks need only a simple, low rank change.
W : d x d, frozen A : r x d, trained (random start) B : d x r, trained (starts at zero) r : the rank, small (4 to 64) output = W·x + (alpha / r)·B·A·x B = 0 at start → model = base the update B·A has rank ≤ r
Counting parameters
Let us count. One layer that is four thousand and ninety six by four thousand and ninety six holds about sixteen point eight million weights. A LoRA update with rank eight needs A, which is eight by four thousand and ninety six, and B, which is four thousand and ninety six by eight. That is two times four thousand and ninety six times eight, or sixty five thousand five hundred and thirty six weights. About zero point four percent of the layer.
full update: 4096 x 4096
= 16,777,216 weights
LoRA, rank 8:
A: 8 x 4096 = 32,768
B: 4096 x 8 = 32,768
total = 65,536
65,536 / 16,777,216 ≈ 0.39%
rank 16 → 131,072 (still < 1%) Why it is cheap
Apply that to the attention and feed forward layers of a seven billion parameter model and you train roughly twenty million parameters, about zero point three percent. There are no gradients or optimiser state for the frozen base, so memory falls sharply and training often fits on one GPU. The result is an adapter file of tens of megabytes, not a fourteen gigabyte copy. For most narrow tasks, quality lands close to full fine-tuning.
- applied to attention and MLP
- layers of a 7B model, rank 8: ~20M trainable parameters ~0.3% of the model adapter file: tens of MB
- optimiser state: adapter only often fits on one GPU
- quality: close to full tuning for most narrow tasks
QLoRA
QLoRA pushes this further. The frozen base is stored in four bits, using a format called N F four, so a seven billion parameter model shrinks from about fourteen gigabytes to about four. The adapters stay in sixteen bit precision. During training, each layer is expanded back to higher precision on the fly, so gradients can reach the adapters. You can tune a seven billion parameter model on one consumer GPU, at a small cost in quality and speed.
- base weights quantised to 4 bits (NF4 format)
- 7B: ~14 GB → ~4 GB
- adapters stay in 16-bit
- gradients flow through the de-quantised base to A, B
- a 7B model tunes on one consumer GPU
- small quality cost, slower step
Merge or swap
At serving time you have two choices. Merge: add the scaled B times A into the original weights once, and you get a normal model with no extra latency. Or swap: load the base model once and keep many adapters next to it, one per customer or task, choosing the right one per request. Modern serving engines can even batch requests that use different adapters together. Each extra task then costs megabytes of memory, not a whole new deployment.
- merge: W_new = W + (alpha / r)·B·A one model, zero extra latency
- swap: load the base once, keep dozens of adapters beside it pick one per request; mixed batches run together tiny cost per extra task
Settings that matter
A few settings do most of the work. Learning rate matters most. Too high and the loss jumps around or the model forgets; too low and nothing changes. LoRA usually runs around one to two times ten to the minus four, roughly ten times higher than full fine-tuning. Train for one to three epochs. Start with a rank of eight or sixteen and raise it only if the task is hard. Set alpha, the scale, to the rank or twice it.
- learning rate: the big one LoRA ~1e-4 to 2e-4 full tuning ~10x lower
- epochs: 1 to 3 (more if tiny)
- rank r: 8 or 16 is a fine start; raise for hard tasks
- alpha: often r or 2r
- targets: attention + MLP
- max length: fit your longest real examples
Prove it worked
Build an evaluation set the model never trained on, and run both the tuned model and the base model with your best prompt. Measure what you care about, such as valid JSON rate and correct team, plus a general skills set to catch forgetting. In this example the tuned model wins clearly on the task and barely moves on general skills. Compare against a strong prompt, or the win may be imaginary.
base+prompt tuned valid JSON 91% 99.5% correct team 84% 95% general skills set 78% 77% median latency 1.0x 0.6x held-out data only, no train overlap beat a good prompt, not a bad one check old skills did not drop
Common mistakes
The common mistakes are mostly about data. Trying to teach facts that should come from retrieval. Expecting forty examples to change everything. Labels that contradict each other. Evaluation rows that also appear in the training set, so the score is fake. Too many epochs. And a subtle one: training with one chat template but serving with another, so the model sees prompts shaped differently from what it learned.
- tuning to add facts
- 40 examples, high hopes
- labels that disagree
- eval rows also in training
- too many epochs
- different chat template at training and at inference
- no baseline to compare
- ignoring old skills
Numbers worth knowing
Numbers worth knowing. Hundreds to a few thousand good examples. Hold out ten to twenty percent for validation. One to three epochs. Full fine-tuning needs about sixteen bytes of GPU memory per parameter. LoRA trains roughly zero point one to one percent of the parameters, with ranks from four to sixty four, starting at eight or sixteen. A four bit base is about a quarter of its sixteen bit size.
good examples ~100s to ~1,000s validation split 10 - 20% epochs 1 - 3 full tuning memory ~16 bytes / param LoRA trainable ~0.1 - 1% of model LoRA rank 4 - 64 (8-16 start) adapter size MB to 100s of MB 4-bit base ~1/4 of 16-bit LoRA learning rate ~1e-4 to 2e-4
Say it like this
How to say it. Prompting plateaued at eighty four percent, so we fine-tuned. We built two thousand reviewed examples and held out ten percent. We trained a rank sixteen LoRA on a four bit base for two epochs and stopped when validation loss turned up. It scores ninety five percent against eighty four for the base with our best prompt, with no drop on general evals. One base model serves twelve adapters.
- "Prompting plateaued at 84%, so we fine-tuned. 2,000 reviewed examples, 10% held out. LoRA rank 16 on a 4-bit base, 2 epochs, stopped when val loss turned up. 95% vs 84% for base plus prompt, no drop on general evals. One base serves 12 adapters."