PEFT and LoRA: Fine-Tuning Without Retraining Everything
How low-rank adapters work, choosing rank and target modules, training with the peft library and serving many adapters on one model.

On this page
Full fine-tuning updates every one of a model's billions of weights. That needs data-centre GPUs, produces a complete copy of the model for every task, and is overkill for teaching a model one new behaviour. Parameter-efficient fine-tuning (PEFT) freezes the original model and trains a small number of new parameters instead. LoRA is the PEFT method that won: simple, cheap, fast to serve, and close to full fine-tuning in quality on most narrow tasks.
The PEFT family
All PEFT methods share one idea: keep the pretrained weights frozen and learn a small, task-specific addition. They differ in where that addition goes.
| Method | What it trains | Trainable share | Notes |
|---|---|---|---|
| Adapters | Small bottleneck layers inserted between existing layers | ~1–5% | Adds a little inference latency |
| Prompt / prefix tuning | Learned "virtual tokens" prepended to the input | <0.1% | Very cheap; weaker on hard tasks |
| (IA)³ | Learned scaling vectors on activations | <0.1% | Tiny; good for few-shot settings |
| LoRA | Low-rank update matrices beside existing weights | ~0.1–1% | No added latency once merged; the default choice |
How LoRA works
A layer in a transformer multiplies its input by a large weight matrix W, say 4096 × 4096, about 16.8 million numbers. Fine-tuning would learn a change ΔW of the same size. The insight behind LoRA is that the change needed for one task has low "intrinsic rank": it can be approximated by the product of two thin matrices.
So instead of learning ΔW directly, LoRA learns B (4096 × r) and A (r × 4096), where the rank r is small, often 8 to 64. Their product BA has the full 4096 × 4096 shape, but it's built from only 2 × 4096 × r trainable numbers. At r = 16 that's about 131 thousand instead of 16.8 million, roughly 0.8% of the layer.
The forward pass becomes:
h = W·x + (α / r) · B·A·xB starts at zero, so at step zero the model behaves exactly like the original. The scaling factor α / r controls how strongly the update is applied. Explore how rank and target modules change the size of the update:
A few things stand out when you play with it:
- Even an aggressive configuration on a 7B model trains well under 1% of the parameters.
- The adapter file is tens of megabytes, not gigabytes. You can store hundreds of task-specific adapters for one base model.
- Targeting the MLP projections (
gate_proj,up_proj,down_proj) adds many more parameters than attention alone, because those matrices are wider.
Choosing rank, alpha and target modules
There's no universal setting, but these defaults are a solid starting point:
- Rank r = 16. Go lower (4–8) for simple formatting tasks, higher (32–64) when the task differs a lot from what the base model already does. Past a point, more rank mostly adds cost.
- Alpha = 2 × r (so α / r = 2) is a common convention. If you change r, change alpha with it to keep the effective learning rate stable.
- Target all linear layers in attention and MLP blocks. Early LoRA work targeted only the query and value projections; targeting everything is now widely found to work better for little extra cost.
- Learning rate around 1e-4 to 2e-4, higher than full fine-tuning, because far fewer parameters are moving.
- Dropout of 0.05 on the LoRA layers helps on small datasets.
LoRA in code
The Hugging Face peft library wraps a model with LoRA layers in a few lines:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model
base_id = "meta-llama/Llama-3.1-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(base_id)
model = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype=torch.bfloat16, device_map="auto")
lora = LoraConfig(
r=16,
lora_alpha=32,
lora_dropout=0.05,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
task_type="CAUSAL_LM",
)
model = get_peft_model(model, lora)
model.print_trainable_parameters()
# trainable params: ~42M || all params: ~8.07B || trainable%: ~0.52From here, training is an ordinary supervised fine-tuning loop over your chat-formatted dataset. Libraries such as TRL provide a ready-made trainer that accepts the LoRA config directly. When training finishes, save only the adapter:
model.save_pretrained("adapters/claims-extractor-v3") # writes a small adapter, not the full modelServing: merge or hot-swap?
You have two options at inference time.
Merge the adapter into the base weights (W' = W + (α/r)·BA). The result is a normal model with zero extra latency, which suits a single dedicated task:
from peft import PeftModel
base = AutoModelForCausalLM.from_pretrained(base_id, torch_dtype=torch.bfloat16)
merged = PeftModel.from_pretrained(base, "adapters/claims-extractor-v3").merge_and_unload()
merged.save_pretrained("models/claims-extractor-v3-merged")Hot-swap adapters on one shared base model. Modern inference servers can keep a single copy of the base in GPU memory and apply a different LoRA adapter per request. One GPU then serves many customers or tasks, each with its own fine-tune. This is often the most cost-effective way to run many specialised models.
Common pitfalls
- Overfitting small datasets. With a few hundred examples, two or three epochs are usually enough. Watch the eval loss, not just the training loss.
- Template mismatch. Train and serve with exactly the same chat template and system prompt. A different template at inference time quietly undoes much of the fine-tune.
- Rank as a magic dial. If r = 64 isn't learning the task, the problem is usually the data, not the rank.
- Forgetting regression tests. LoRA changes less than full fine-tuning, but it can still degrade behaviours you didn't train on. Keep a general-skills check in your eval suite.