LoRA β Don't Change All 66M, Just Plug In a 1M Adapter
Efficient alternative to fine-tuning β freeze the base model, train just two small matrices
The Problem with Fine-tuning
Fine-tuning adjusts all 66M numbers. Problems: 7B models need dozens of GB GPU memory, 5 models = 5Γ storage, long training.
LoRA's Idea
"Don't change all 66M. Just plug in a small adapter."
Fine-tuning: adjust all 66M β save 66M (260MB)
LoRA: freeze 66M + train adapter 1M β save 1M only (4MB)
Analogy: fine-tuning = modifying the phone itself. LoRA = putting on a case.
How It Works β Matrix Decomposition
Instead of changing W (768Γ768 = 589K numbers), LoRA adds two small matrices:
W(frozen) + A(768Γ8) Γ B(8Γ768) = adapter is only 12K numbers
Code
from peft import LoraConfig, get_peft_model
lora_config = LoraConfig(r=8, lora_alpha=16, target_modules=["q_lin", "v_lin"])
model = get_peft_model(model, lora_config)
model.print_trainable_parameters() # β 0.44% trainable
trainer.train() # same as fine-tuning
model.save_pretrained("./lora-adapter") # saves only adapter (few MB)
Fine-tuning vs LoRA
| Fine-tuning | LoRA | |
|---|---|---|
| Train | All 66M | 300K (0.44%) |
| Save size | 260 MB | Few MB |
| Quality | Baseline | Nearly identical |
Key Concepts
Freeze base model β lock all 66M numbers, set as non-trainable
Attach small matrices A(768Γ8), B(8Γ768) to each layer β adapter
Train only A and B β adjust only 0.44% of total
Inference: output = WΓinput + (AΓB)Γinput β original weights + adapter correction
Save adapter only (few MB) β swap adapters per use case