Sub topic 10
Parameter-efficient tuning with LoRA
This chapter assumes you know what fine-tuning changes (the model's weights, trained on input and output pairs), what a context window is, and how to read Python. It covers when full fine-tuning is too heavy and how LoRA reduces the cost.
Why full fine-tuning is heavy
Full fine-tuning updates every weight in the model. For a model with billions of parameters, training must store the weights, the gradients, and the optimizer state at the same time. That needs far more GPU memory than inference does. Each tuned variant is also a full copy of the model, so ten tasks mean ten large checkpoints to store and deploy.
Parameter-efficient fine-tuning (PEFT) is a family of methods that train only a small number of new or selected parameters and leave the original weights frozen. LoRA (Low-Rank Adaptation) is the most widely used of them.
How LoRA works
A weight matrix W in a layer has shape d × k. Full tuning learns a change ΔW of that same size. LoRA assumes the useful change is low-rank, so it writes ΔW as the product of two thin matrices:
Ahas shaper × k.Bhas shaped × r.
The rank r is small, often 4 to 64. During training, W is frozen and only A and B learn. The layer's output becomes W·x + (alpha / r)·B·A·x, where alpha is a scaling factor. B starts at zero, so training begins from the unchanged base model.
The savings are easy to count. For a 4096 × 4096 matrix, W has about 16.8 million values. With r = 8, A and B together have 2 × 4096 × 8, which is 65,536 values. That is under 0.4% of the original.
d, k, r = 4096, 4096, 8
full = d * k
lora = r * (d + k)
print(f"full update: {full:,}")
print(f"LoRA update: {lora:,}")
print(f"fraction: {lora / full:.2%}")This prints 16,777,216 for the full update, 65,536 for LoRA, and a fraction of 0.39%.
What you get in practice
- Lower memory. Only the small matrices need gradients and optimizer state, so training fits on smaller GPUs.
- Small artifacts. An adapter is usually megabytes, not gigabytes. You can keep one base model and swap adapters per task or per customer.
- No added latency after merging. You can fold
B·AintoWfor serving. If you keep adapters separate, a small extra computation runs per layer. - Reversible. Removing the adapter restores the base model exactly.
A variant called QLoRA keeps the frozen base weights in 4-bit precision to cut memory further, while the adapters train in higher precision.
What LoRA does not change
LoRA is still fine-tuning. It changes behavior: style, format, tone, task skill. It is a poor way to teach the model large amounts of new facts, and it does not make knowledge fresh. Facts that change belong in retrieval, as earlier chapters showed. LoRA also needs the same curated training data and the same evaluation set as full fine-tuning.
Common errors
| Error | Effect | Fix |
|---|---|---|
| Rank too low for a hard task | Training loss plateaus, behavior barely changes | Raise `r` and compare against your baseline |
| Rank very high with little data | Overfitting, behaves like full tuning without the benefit | Lower `r` or add data |
| Adapting too few layers | Weak effect | Apply LoRA to more projection layers |
| Using an adapter with a different base model version | Degraded or broken output | Pin the base model version with each adapter |
| Skipping the evaluation set | No evidence the adapter beats the prompt | Score the adapter against the baseline from earlier chapters |
When to choose it
Pick LoRA over full fine-tuning when you have limited GPU memory, many task variants, or a need to swap behaviors on one base model. Pick neither if a prompt with a few examples already passes your evaluation.
Exercise
A team wants a support model that answers in a fixed house style and also knows this week's price list. They plan to train one LoRA adapter on the price list. What is wrong with that plan, and what would you do instead?
Show answer
LoRA is poor at storing facts and cannot stay current, because the price list changes weekly and every change would need retraining. Use LoRA (or a prompt with examples) for the house style, and use RAG to supply the current price list at query time.
Exercise
For a 2048 × 2048 weight matrix and rank r = 16, how many values does the LoRA update (A and B together) contain, and what fraction is that of the full matrix?
Show answer
LoRA has r × (d + k) = 16 × 4096 = 65,536 values. The full matrix has 2048 × 2048 = 4,194,304 values. The fraction is 65,536 / 4,194,304 = 1.56%.