Models
Teaching a model one more thing
Fine-tuning, and why LoRA trains 0.06% of the weights
The lesson before this one made a model from random weights. That takes a great deal of text and compute, and it is done once, by whoever builds the model. Fine-tuning is the small sequel that everyone else does. You take the finished weights and train a little more on your own examples, so the model answers the way your job needs. The loop is the same one: guess, measure, work backwards, nudge. What changes is how much of the model you let it nudge. This lesson does it twice. First the plain way, which moves every weight and leaves you a whole new model. Then with LoRA, which leaves the model alone and trains a small attachment beside it.
- Starts from
- a finished model, 32.8 billion weights
- Trains on
- a few thousand of your own examples
- Full fine-tuning
- moves every weight · a 65.6 GB copy
- LoRA
- trains 19.9 million · a 40 MB file
Read the whole lesson as text
1. A model that does not know your job
A support team wants tickets answered in their own format: a short diagnosis, then numbered steps, then the article to link. The base model can do this if every request carries the instructions and a few worked examples. That works, and it has a price. Those instructions are read again on every request, by every user, forever. The other way is to show the model 5,000 real tickets with good answers and let training move the behaviour into the weights. After that a bare ticket is enough. Fine-tuning is the right tool for how a model should behave. It is a poor tool for facts that change, which belong in the prompt.
- The training set is 5,000 tickets, each with the answer the team would have written.
2. The plain way: move every weight
Full fine-tuning is pre-training continued. Load the finished weights, run the examples through, measure the loss, work backwards and let the optimizer move all 32.8 billion weights a little, with a small step size so the model does not forget what it knew. It works well. Look at what it costs. Training needs room for every weight, its slope and the optimizer's two averages, 16 bytes a weight, which is 525 GB. And what comes out is a complete model. Do this for 100 customers and you are storing and serving 100 models of 65.6 GB each, and each one wants a GPU to itself.
- Backpropagation produces a slope for every one of the 32.8 billion weights.
- The optimizer nudges every weight. The result is a new model, the same size as the old one.
3. LoRA: leave the weights, add a detour
LoRA starts from an observation about fine-tuning. The change it makes to a big matrix of weights is simple in structure. It can be written, nearly, as the product of two much thinner matrices. So do not touch the big matrix at all. Freeze it, and put the two thin ones beside it as a detour. Take the matrix that makes the queries in one attention layer. It turns 5,120 numbers into 8,192, which is 41.9 million weights. The detour first squeezes the 5,120 numbers down to 16, then spreads those 16 out to 8,192. That is 212,992 weights, half a percent of the matrix it sits beside. The layer's output is the frozen path plus the detour.
- Each adapted matrix gets two thin matrices beside it. Its output is its own result plus theirs.
4. It starts out changing nothing
One of the two thin matrices starts as small random numbers and the other starts as all zeros. Anything times zero is zero, so on the first pass the detour adds nothing and the model behaves exactly like the base model. Training begins from what the model already knows. Then the usual loop runs. The examples go through, the loss is measured, and the slopes are worked backwards through the whole model. But the optimizer is handed only the thin matrices. The 32.8 billion frozen weights get no slope kept, no average and no update. As the steps go by, the detour learns the difference between the base model and the model the team wants.
- On the first batch the model is still the base model. Its loss on the tickets is 2.31.
- The optimizer updates the thin matrices in all 64 attention layers and nothing else.
5. Count what was trained
Add it up for the whole model. Each attention layer gets a detour on its query matrix and one on its value matrix. The value matrix is smaller, 5,120 numbers into 1,024, so its detour is 98,304 weights. Together that is 311,296 a layer, and there are 64 layers: 19.9 million trained weights. That is 0.06 percent of the model. Saved at 16 bits it is a 40 MB file. This file is the whole result of the fine-tuning. It is called an adapter. It is useless by itself and it only fits the exact base model it was trained beside.
- Training ends. What gets saved is only the thin matrices: 40 MB.
6. One base model, many adapters
Here is why this matters to whoever runs the GPUs. There are two ways to serve an adapter. One is to merge it: add the product of the thin matrices into the big one and you have a normal model again, with no extra work per token and one GPU's memory per customer. The other is to keep it apart. Load the base model into GPU memory once, load many adapters beside it at 40 MB each, and let each request say which one it wants. In vLLM an adapter is registered under a name, and a request asks for it in the model field like any other model. A hundred customers then cost 4 GB of adapters on top of one base model, where full fine-tuning cost a hundred GPUs.
- A request names the support adapter as its model. The pass uses the base weights plus that adapter's detours.
Summary
Fine-tuning is the training loop run a little longer on your own examples. Done in full it moves every weight, needs the memory of pre-training and leaves a complete model for each use. LoRA freezes the model and trains two thin matrices beside each attention matrix. They start as no change at all, they hold 0.06 percent of the weights, and they save to a 40 MB file. One GPU then holds one base model and many adapters, and a request picks its adapter by name. The base model has to be the exact one the adapter was trained beside.
Next lesson: A model bigger than one GPU