Skip to content

What LoRA Actually Does

Fine-tuning the whole of a model means adjusting every one of its parameters — the billions of internal numbers that training sets. That is expensive in memory, slow, and leaves you with a whole new copy of the model for every behaviour you teach. LoRA — short for Low-Rank Adaptation — is a way of getting most of the effect while adjusting almost none of the numbers.

Here is the idea, without the algebra first. A model's knowledge lives in large tables of numbers called weight matrices. Changing a behaviour means changing some of those tables. LoRA's observation, from the 2021 paper that introduced it, is that the change needed for a specific new behaviour is usually simple — much simpler than the table it modifies — and can be written as the product of two small, thin tables. So LoRA leaves the original table exactly as it is and learns the two thin ones instead.

Three consequences follow, and they are the reason this guide exists:

  • The base model is frozen. Its weights never move. Nothing it knew is overwritten; the risk of the model forgetting its general abilities — catastrophic forgetting, the standing hazard of full fine-tuning — is much reduced.
  • The adapter is tiny. The two thin tables are saved as an adapter: a small file — the lab's is a few hundred kilobytes — that sits beside the base model and is applied on top of it at load time. One base model, many adapters, each a different behaviour, swapped in seconds.
  • Training is cheap. Only the thin tables are adjusted, so the memory and time needed are a fraction of full training. A model that would need a datacentre graphics card to fine-tune whole can often be LoRA-trained on a gaming card — and a small model, like this guide's, on a plain CPU.

Rank and alpha: the two numbers that size an adapter

When you configure a LoRA adapter you meet two numbers everywhere, and they are worth understanding plainly.

Rank (written r) is how thick the thin tables are. Rank 8 means each change is expressed through a bottleneck 8 numbers wide. A higher rank can express subtler, more complex changes — and needs more examples to learn from, more memory, and more time, and can memorise a small dataset instead of learning from it. Rank is the adapter's capacity. Small jobs take small ranks: 4, 8, 16 are the common starting points, and the lab uses 8.

Alpha (written lora_alpha) is how loudly the adapter speaks. The learned change is scaled by alpha divided by rank before it is added to the frozen weights, so with the lab's alpha of 16 and rank of 8, the adapter's voice is doubled. The habit in the field is to set alpha to twice the rank and then mostly adjust the learning rate (chapter 5) rather than either of these.

A third setting, dropout, randomly ignores a fraction of the adapter during training to keep it from memorising. On a dataset this small the lab sets it to zero and relies on few passes and a low learning rate instead; chapter 6 says how it would know if that judgement was wrong.

Which layers get adapters

An adapter does not have to be attached to every table in the model. In practice LoRA is attached to the attention layers — the parts of the model that decide what to pay attention to — and often just to some of their tables. The lab's training script names one target, c_attn, which is what the base model's attention table is called. Different model families name their tables differently (q_proj, v_proj in many others), and the training library will refuse a name it cannot find rather than silently training nothing. Naming targets wrong is a classic silent failure in hand-rolled setups; chapter 13 has it.

What LoRA is not doing

The adapter has not read your data in the way a search index has. Nothing in the adapter can be looked up, quoted, or audited line by line; the examples have been averaged into the thin tables as tendencies. That is why the guide insists on testing with unseen questions (chapter 8): the only evidence that a tendency was learned, rather than a page memorised, is behaviour on material that was not in the book.