About

What Is LoRA? Fine-tuning with Only a Few Trainable Parameters

Freeze the original weights and add two low-rank matrices

Short version: if you want to nudge a 7B model with your own data, you don't need to train all 6.7 billion weights. LoRA (Low-Rank Adaptation) freezes the original weights and trains two tiny matrices bolted on the side. After reading this you can count how many parameters LoRA actually trains, and pick Full Fine-tuning, LoRA or QLoRA for the GPU you have.

In one line

Leave the original weight W alone and write the change ΔW as B × A. If W is d×k, B is d×r and A is r×k. The rank r (the "width" of the matrices) is kept small, around 4–64. The forward pass is h = Wx + (α/r)·BAx, and only A and B get trained.

LoRA: freeze W, train a thin side pathxW (d × k) — frozen6.7B weights, not updatedA (r × k)starts randomB (d × r)starts at 0+hh = Wx + (α/r)·BAx. Trained: only A and B. On a 7B shape with q,v at r=8 that is 4,194,304 of 6,738,415,616 (0.062%).

I assumed both A and B start random. They don't. B starts at zero, so on the first training step the model outputs exactly what the original did. With peft 0.17.1 on a 64×64 Linear with r=4, the output matched the pre-LoRA output and B was all zeros. After 50 steps the original W still hadn't changed in a single entry.

How many parameters does it train? I counted

I built a model with the Llama 2 7B shape (hidden 4096, 32 layers, vocab 32,000) on the meta device and attached LoRA with peft. Total: 6,738,415,616.

r=8,  q,v only           4,194,304   0.062%
r=16, q,k,v,o           16,777,216   0.249%
r=16, attention + MLP   39,976,960   0.593%

This post used to say "0.1–1% of the original." Depending on where you attach it, it goes as low as 0.06%. The original paper reports 10,000× fewer trainable parameters and 3× less GPU memory on GPT-3 175B.

Why memory doesn't shrink as much

Trainable parameters are under 1%, but memory isn't. The frozen weights still have to sit on the GPU. A 7B model in bf16 is about 14GB on its own. Rough numbers (activations and batch left out):

Full FT (bf16 + Adam)   7B × 16 bytes  ≈ 112GB
LoRA (bf16, frozen)     7B × 2 bytes   ≈ 14GB + adapter
QLoRA (4-bit weights)   7B × 0.5 bytes ≈ 3.5GB + adapter

The 16 bytes is the usual back-of-envelope: weights 2, gradients 2, Adam state 8, fp32 master copy 4. I didn't measure these on a GPU. The QLoRA paper fine-tuned a 65B model on a single 48GB GPU.

What about a small model? I counted DistilBERT too

If 7B feels abstract, DistilBERT (a 66M model people use a lot for text classification, small enough for a laptop) makes it concrete. Same peft 0.17.1, r=8. Total including the classification head: 66,955,010.

r=8, q,v only              147,456   0.220%
r=8, q,k,v,o               294,912   0.440%
r=8, attention + FFN       663,552   0.991%

For classification the head on top (about 590k parameters) trains too, so with q,k,v,o what actually moves is 887,042 parameters, about 1.3%. Saving to disk makes the gap obvious: the full state_dict was 268MB, the adapter from save_pretrained() was 2.96MB. Each new use case adds a 3MB file instead of another copy of the model.

You can merge it when you're done

Add W + (α/r)·BA ahead of time and inference is just one Linear again. After merge_and_unload() in peft, the output matched the unmerged model. Or keep it unmerged and swap adapter files to use one base model for several jobs. Which is better depends on how you serve.

When to use what

Plenty of GPU and a very different domain: Full FT. Weights fit on the GPU in bf16: LoRA. They don't: QLoRA. But before any of that, check whether prompting or RAG gets you there. If you only need a tone or format, those are far cheaper. The bigger picture is in the fine-tuning post.

If you can't decide between tuning a model on your company data and going with RAG, feel free to ask negabaro. We can start from how far your data and GPUs will actually take you.

Key Concepts

1

Freeze the original weight matrix W (d×k)

2

Attach B (d×r) and A (r×k) beside W. A starts random, B starts at zero — so the first output equals the original model

3

Forward pass: h = Wx + (α/r)·BAx

4

Backprop: only A and B are updated (W stays put)

5

Trainable parameters: r×(d+k) per layer. On a 7B shape with q,v at r=8 that is 0.062% of the total

6

Inference: merge into W + (α/r)·BA for no extra compute, or keep it separate and swap adapters

Pros

  • ✓ Very few trainable parameters (0.06–0.6% on a 7B shape)
  • ✓ Less GPU memory (1/3 on GPT-3 175B in the original paper)
  • ✓ No extra inference cost once merged
  • ✓ The original model stays intact

Cons

  • ✗ Frozen weights still sit on the GPU, so memory does not shrink as much as trainable parameters
  • ✗ Can score a little below full fine-tuning
  • ✗ You have to choose r and which layers (q,v only or everything)

Use Cases

QLoRA — 4-bit quantization + LoRA (the paper fits 65B on one 48GB GPU) Swapping task-specific adapters on one base model Hugging Face PEFT library Stable Diffusion LoRA (image style learning)
← What Is DPO? Skipping RLHF's Reward Model and PPO What Is Prompt Engineering? Steering LLM Output Through Input →