0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Fine-tune a 7B model on one 24 GB GPU
Last episode, we learned that fully fine-tuning a seven-billion-parameter model needs over a hundred gigabytes. Dev has a single twenty-four-gigabyte card. Here's the trick. Freeze all seven billion original weights. Don't train them at all. Instead, beside each big weight matrix, add a pair of small new matrices, and train only those. The number of trainable parameters drops from seven billion to about eight million: around a tenth of one percent. Then shrink the frozen weights to four bits, and the whole job fits comfortably on one card.
These ideas, LoRA and QLoRA, made fine-tuning large models accessible to almost anyone. Today, we'll see exactly how they work.
1:01Where this fits in the course
Season Five, Module Nineteen, stop three: LoRA and QLoRA.
1:08Parameter-efficient fine-tuning (PEFT)
Full fine-tuning updates every weight, which is why it needs gradients and optimiser memory for all seven billion of them. Parameter-efficient fine-tuning takes a different approach: freeze the original model, and train only a small number of new or selected parameters. Frozen weights need no gradients and no optimiser states, which saves enormous amounts of memory. There are several such methods: small adapter layers inserted into the network, trainable "virtual tokens" added to the input, and the most popular of all, LoRA.
1:46How LoRA works: the low-rank bypass
LoRA, short for low-rank adaptation, works like this. Take one of the model's big weight matrices, say four thousand and ninety-six by four thousand and ninety-six, and freeze it. Next to it, add a bypass made of two slim matrices: one squeezes the input down to a tiny width, called the rank, say eight; the other expands it back out. The layer's output becomes the original output, plus whatever comes through the bypass. In other words, the effective weight becomes the original matrix plus the product of the two slim matrices, and only the slim matrices are trained.
One of them starts at zero, so at the beginning the bypass adds nothing, and the model behaves exactly as before. Now count. The original matrix has nearly seventeen million weights. The two slim matrices, at rank eight, have just sixty-five thousand, about four tenths of a percent. Add LoRA to the four attention matrices in every one of the model's thirty-two layers, and the total trainable parameters come to about eight point four million: around a tenth of a percent of the full model.
3:00Why a low-rank update works
Why can such a tiny update be enough? First, see what "low rank" means. Multiply a column of three numbers by a row of three numbers, and you get a full three-by-three grid, nine entries built from just six numbers. Every row is a multiple of the same pattern. That's a rank-one matrix. A rank-eight update is a combination of eight such patterns. Researchers have found that the changes needed to adapt a pre-trained model to a new task tend to lie in a small number of directions. The model already knows language; fine-tuning mostly nudges how it behaves.
It's like PCA from episode eighteen: most of the important variation lives in a few directions. Higher ranks give the update more capacity, at the cost of more parameters. In practice, small ranks often work well, but the best value depends on the task, so it's tuned with evaluation.
3:59LoRA memory and settings: rank, targets, learning rate
What does this do to memory? The frozen weights still take fourteen gigabytes in B-F sixteen. But gradients and optimiser states are only needed for the eight million LoRA parameters, about a hundred and thirty megabytes. Add activations, reduced with gradient checkpointing, and the job can fit on a twenty-four-gigabyte card, depending on text length and batch size. The main settings: which matrices get LoRA bypasses, often the attention projections and sometimes the feed-forward layers too; the rank and a scaling factor; and a learning rate, usually higher than you'd use for full fine-tuning.
On many tasks, LoRA gets close to full fine-tuning quality, at a tiny fraction of the cost. As always, verify on your own scorecard.
4:49LoRA adapters as cartridges, and merging
LoRA has a lovely practical bonus. The trained bypass, called an adapter, is tiny: often just tens of megabytes. So one shared base model can serve many adapters, like cartridges: one for Brightcart's support voice, one for ticket routing, one for replies in German. You store and switch small adapters instead of keeping several full copies of a fourteen-gigabyte model. And when you want maximum speed, you can merge an adapter into the weights, adding the product of the two slim matrices to the original matrix, to get an ordinary model with no extra computation at all.
5:30QLoRA: a 4-bit base with LoRA on top
QLoRA pushes this further. Freeze the base model and quantise it to four bits, from fourteen gigabytes down to three and a half, then train LoRA adapters on top in sixteen-bit precision. During training, the four-bit weights are temporarily expanded to do the maths, gradients flow through them to the adapters, and only the adapters change. The QLoRA work added a few clever refinements: a four-bit number format designed for the bell-shaped distribution of model weights, compressing the quantisation scales themselves, and tricks to avoid sudden memory spikes.
The result: the authors fine-tuned a sixty-five-billion-parameter model on a single forty-eight-gigabyte graphics card. For Brightcart's seven-billion-parameter model, a single consumer card is plenty. The trade-offs: a four-bit base can lose a little quality, and training is somewhat slower because of the constant conversions. Measure it.
6:37The limits of LoRA and PEFT
Parameter-efficient methods have limits. For very large changes, like teaching a model a new language, a small low-rank update may not be enough, and you might need higher ranks, more matrices, or full fine-tuning. They carry the same risks as any fine-tuning: forgetting and weakened safety, so the full evaluation, including safety, must be re-run. And no method can rescue poor training data, which is why the next module is all about data.
7:09Try it yourself: double the rank
At rank eight, one big four-thousand-by-four-thousand matrix gets about sixty-five and a half thousand trainable parameters. What happens at rank sixteen? Pause. Exactly double: about a hundred and thirty-one thousand, still under one percent of the original matrix. LoRA's parameter count grows in a straight line with the rank. A common approach is to start with a small rank and only increase it if the evaluation shows the adapter can't capture what you need.
7:48Try it yourself: adapt every layer
Our first LoRA setup only adapted the four attention matrices in each layer. What if we also adapt the three feed-forward matrices, which in a typical seven-billion-parameter design are four thousand by eleven thousand? Pause and estimate.
Each feed-forward adapter adds about a hundred and twenty-one thousand parameters. Per layer, that's about six hundred and twenty-five thousand, and across thirty-two layers, just under twenty million: still only about three tenths of a percent of the model. Adapting more matrices often improves quality, and it's still a tiny fraction of the full model.
8:33Pause & Predict: the model at step zero
Before any training, how does a LoRA model behave? Pause. Exactly like the base model. One of the two small matrices starts at zero, so their product is zero, and the original weights are unchanged. Training then moves away from the base model gradually, which makes it stable. The update is also multiplied by a scaling factor, the LoRA scale divided by the rank: with a scale of sixteen and rank eight, the update counts double. That setting interacts with the learning rate, so it's usually tuned together with it.
9:14Try it yourself: the optimiser's share of memory
With full fine-tuning, the master weights and Adam's running averages, twenty-eight plus fifty-six, took eighty-four gigabytes. With LoRA, those only exist for the eight point four million adapter parameters. At about sixteen bytes per trainable parameter, how much is that? Pause.
About a hundred and thirty-four megabytes. The frozen base still needs its weights, fourteen gigabytes in sixteen bits, or three and a half in four bits, and activations still take memory. But the enormous optimiser bill has almost vanished. That's where most of LoRA's memory saving comes from.
10:00Ten adapters, one base model
Here's where adapters really shine. Brightcart could train ten of them: support voice, catalogue tagging, invoice extraction, Spanish replies, and so on. Each is about seventeen megabytes, so all ten together take under two hundred megabytes. Ten fully fine-tuned copies of the model would take a hundred and forty gigabytes. One base model stays in memory, and the server attaches the right adapter for each request, like swapping cartridges.
10:31Merge an adapter, or keep it separate?
Once an adapter is trained, you have a choice. Merge it: add the small update into the original weights once, and you get an ordinary model with no extra computation per token, ideal for a single stable task. Or keep it separate: a little extra work per request, but adapters can be swapped, updated or rolled back independently, ideal for many tasks or frequent changes. Merging is for shipping one thing; keeping separate is for flexibility.
11:03Choosing LoRA rank and targets by experiment
How do you choose the rank and which matrices to adapt? By experiment. Brightcart runs a small grid: ranks four, eight, sixteen and thirty-two, adapting either attention alone or attention plus feed-forward, and scores each on the golden set and the voice judge. A common pattern is that gains flatten beyond a moderate rank, and that adapting more matrices helps more than raising the rank, but your own data decides. Choose the smallest setup that's within the noise of the best one.
11:36QLoRA on one 24 GB card, added up
So, can Dev fine-tune the seven-billion-parameter model on one twenty-four-gigabyte card? Add it up. The frozen base in four bits: three and a half gigabytes. The adapters and their optimiser states: about a tenth of a gigabyte. Then activations, which depend on batch size and sequence length, and can be reduced by recomputing them. It fits, with room to spare. The suitcase that burst at a hundred and twelve gigabytes now closes easily.
12:08LoRA beyond language: image generators
LoRA isn't only for language models. It's widely used with image generators too. Sana could train a small adapter on thirty approved Brightcart lifestyle photos, soft window light, oak tables, muted colours, so new generated scenes match the house style. The adapter is a small file that can be switched off, or swapped for a seasonal look. And the rule from episode forty-eight still applies: generate the surroundings, and keep the real products real.
12:40Myth vs Fact: does 0.1% mean 0.1% as much?
Myth: training a tenth of a percent of the parameters means the model can only learn a tenth of a percent as much. Fact: those few parameters steer the enormous knowledge already in the frozen model. For many behaviour-shaping tasks, a small adapter performs close to full fine-tuning.
12:59One-minute recap
Let's recap. Parameter-efficient fine-tuning freezes the base model and trains only a few new parameters. LoRA adds a low-rank bypass, the product of two slim matrices, beside each chosen weight matrix, starting at zero. At rank eight, that's sixty-five thousand trainable numbers per matrix instead of seventeen million, about eight million in total for a seven-billion-parameter model. Adapters are small, swappable and mergeable. And QLoRA trains adapters on top of a four-bit base, making fine-tuning possible on a single card.
One sentence to remember: freeze the giant, and train a thin bypass.
13:46Next: model merging
Mission complete. Brightcart can fine-tune its seven-billion-parameter model on a single card, producing a small "Brightcart voice" adapter, then re-run the full scorecard, including safety. Next, a surprising idea: what if you could combine separately fine-tuned models, or adapters, just by doing arithmetic on their weights? Model merging, and multi-task fine-tuning, the last stop in this module. See you on the map.