Fine-tuning teaches a ready-made model your task: your tone of voice, your support tickets, your file format. Done the classic way, it adjusts every one of the model’s billions of numbers, which takes a rack of expensive GPUs. LoRA (low-rank adaptation) leaves the model untouched and trains a tiny add-on instead. LoRA learns the change to each weight matrix as the product of two thin matrices, often under 1% of the model’s size. Here you’ll count what that saves on real model sizes, then train a small one yourself and see when it’s enough.
Why fine-tuning every weight is expensive#
A language model is mostly big grids of numbers called weight matrices. A model the size of Llama 2 7B has about 6.7 billion numbers in them. Full fine-tuning nudges every single one, and while it trains, it also has to keep each number’s gradient (which way to move it) and the optimiser’s running averages. That’s roughly 12 bytes per number: around 81 GB for a 7B model, before counting the memory the text itself takes up as it passes through.
And you end up with a whole new copy of the model, 13.5 GB at 16 bits per number, for every task you tune it for. A support bot, a legal summariser and a code reviewer means three full copies.
The trick: learn a small change#
LoRA, introduced by Edward Hu and colleagues at Microsoft in 2021, is the best-known member of a family called parameter-efficient fine-tuning (PEFT): methods that train a small number of new or selected numbers instead of all of them. It freezes the original weights and learns only a change to add on top. The change to a matrix is the same size as the matrix, but LoRA builds it as two thin matrices multiplied together: B, tall and narrow, times A, short and wide. How narrow is set by the rank, r.
At rank 1, B is a single column and A a single row, and B × A is like a multiplication table: every row of the change is the same pattern, scaled up or down. Rank 2 adds two such patterns together, and so on. A low-rank change can still touch every weight, but it can only make a few kinds of adjustment.
Predict first
One weight matrix in a 7B model is 4,096 × 4,096: 16.8 million numbers. How many numbers does LoRA train for it at rank 8?
Adapting the query and value matrices of a 7B model at rank 8 trains 4,194,304 numbers: 0.06% of the model. The adapter file is 16.8 MB, against 13.5 GB for a full copy. Even adapting every layer only reaches 0.3%.
The memory drops too, because the frozen weights need no gradients and no optimiser state: about 13.5 GB instead of 81 for a 7B model. QLoRA goes further and stores the frozen model in 4 bits per number, about 3.5 GB. Its authors showed it could fine-tune a 65B model on a single 48 GB GPU, against more than 780 GB for ordinary 16-bit fine-tuning, while matching that fine-tuning’s results on their benchmarks. LoRA moves fine-tuning from a rack of data-centre GPUs to a single card.
But is such a small change enough?#
LoRA bets that the change a new task needs is simple, even when the model is not: that it has a low intrinsic rank, in the paper’s words. On GPT-3, with 175 billion numbers, the authors found ranks as low as 1 or 2 were competitive, and LoRA matched or beat full fine-tuning on all three tasks they tried, training between 4.7 and 38 million numbers instead of 175 billion.
Predict first
A task needs a change made of two patterns. You have only 30 noisy examples. Which learns the change better: full fine-tuning, adjusting all 576 numbers of the layer, or LoRA at rank 2, adjusting 96?
With 300 examples, full fine-tuning and LoRA at rank 2 or 8 all learn essentially the whole change, but LoRA rank 2 does it with 96 numbers instead of 576. Rank 1 stalls at about 75%, however many examples it gets: one pattern can’t make a two-pattern change. With only 30 examples, full fine-tuning fits its training examples most closely of all and yet learns just 84% of the real change, because some of what it fitted was noise. Rank 2 learns 99%.
The LoRA paper saw a version of this too: tuning GPT-3 on just 100 examples of a reasoning task called MNLI, LoRA scored 63.8% against full fine-tuning’s 60.2%. But the bet doesn’t always pay. The authors themselves warn that a task far from what the model already knows, such as a new language, may need a much higher rank or full fine-tuning.
Using it in practice#
- Rank: 8 or 16 is a common starting point. The QLoRA authors found that adding adapters to every layer mattered more than the rank.
- Alpha: the change B × A is scaled by alpha ÷ r before it’s added, a knob that works much like the learning rate. Many people set alpha to r or 2r and leave it.
- No slowdown when serving: after training you can add B × A into the original weights once, and the model runs exactly as fast as before.
- Swappable skills: or keep the adapters separate and swap them on one shared base model. The paper’s example: 100 tasks on GPT-3 take about 354 GB as adapters, against 35 TB as full copies.
In code#
With Hugging Face’s PEFT library (pip install transformers peft) on SmolLM2-135M, a small open model that runs on a laptop:
from transformers import AutoModelForCausalLM
from peft import LoraConfig, get_peft_model
model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-135M")
config = LoraConfig(r=8, lora_alpha=16, target_modules=["q_proj", "v_proj"], task_type="CAUSAL_LM")
model = get_peft_model(model, config)
model.print_trainable_parameters()
# trainable params: 460,800 || all params: 134,975,808 || trainable%: 0.3414
# ... train as usual, for example with transformers' Trainer ...
model.save_pretrained("my-adapter") # saves only the small A and B matrices (1.9 MB here)
merged = model.merge_and_unload() # folds B·A into the weights: no extra cost when answeringtarget_modules names the matrices to adapt: here the attention’s query and value projections, as in the original paper. Add "k_proj", "o_proj" and the feed-forward layers to adapt more, and load the model in 4 bits with the bitsandbytes library to make it QLoRA.
What to remember
- LoRA freezes the model and learns each weight change as B × A, two thin matrices, often well under 1% of the model.
- That cuts training memory and turns each fine-tune into a small file; QLoRA also stores the frozen model in 4 bits.
- It works when the change a task needs is simple: too low a rank can’t express it, and very different tasks may need more.