Lesson 29.3 · 3 of 9 · 5 min read · Reviewed 24 Nov 2026
LoRA and parameter-efficient tuning
By the end, you can
- Explain LoRA and parameter-efficient tuning.
- Know why they're the default for most teams.
The idea
frozen base weights W (billions of numbers, unchanged)
+
small adapter A x B (low-rank matrices, a tiny fraction of W's size)
=
behaviour of the tuned model
Instead of changing the huge weight matrices, LoRALoRA: Low-rank adaptation: fine-tuning a small set of added weights instead of the whole model. learns a small low-rank update for some of them. Training touches far fewer numbers, so it needs much less GPU memory, and the result is an adapter file measured in megabytes rather than gigabytes.
Why it’s the default
| Full fine-tuning | LoRA | |
|---|---|---|
| GPU memory | Very high | Much lower |
| Training time | Long | Shorter |
| Output size | A full model copy | A small adapter |
| Many tasks, one base | Many full copies | Many adapters, swap at load time |
| Quality | Highest ceiling | Close to full tuning for most tasks |
QLoRA
QLoRA keeps the frozen base in 4-bit form (30.2) while training LoRA adapters in higher precision. This made tuning large models possible on a single GPU.
Settings you’ll see
- Rank (r): adapter size. Higher rank means more capacity, more memory.
- Target modules: which layers get adapters (often the attentionAttention: The mechanism that lets each token weigh every other token in the context when building its representation. projections).
- Learning rate and epochs: too high or too many and the model overfits or forgets general skillsSkill: A folder of instructions, scripts and resources that an agent loads when relevant to a task..
Pick settings using the validation set, and confirm with the test set (29.5).
Key points
- Full fine-tuning updates every weight: costly in memory, time and storage.
- LoRA freezes the base model and trains small add-on matrices (adapters) that adjust its behaviour.
- Adapters are small files; you can keep many for one base model and swap them per task.
- QLoRA combines LoRA with a quantized base model, so large models can be tuned on modest GPUs.
Count LoRA parameters for a model
Compute how many weights a LoRA adapter trains and how big its file is, compared with full fine-tuning.
You will have: A table of trainable parameters, share of the model and adapter size for several ranks and target modules.
Steps
- Each adapted matrix of size d x d gets two small matrices: d x r and r x d, so 2 x d x r weights.
- Multiply by the number of adapted matrices per layer and the number of layers.
- Print trainable weights, share of the base model and adapter size in 16-bit for ranks 8, 16 and 64.
- Repeat with two and with four attention projections targeted.
- Compare with the size of a full 16-bit copy of the model.
Done when
- Rank 16 on two projections of an 8B model (4,096 wide, 32 layers) gives about 8.4 million weights.
- The share of the model stays well under 1% for all ranks shown.
- The adapter file is measured in megabytes; the full copy in gigabytes.
Open in the Playground No API key needed.
Starter code (Python)
PARAMS = 8e9
WIDTH = 4096
LAYERS = 32
def lora_weights(rank, matrices_per_layer):
# TODO: 2 x WIDTH x rank per adapted matrix, times matrices per layer, times LAYERS
return 0
for mats in (2, 4):
for r in (8, 16, 64):
w = lora_weights(r, mats)
print(f"targets={mats} r={r:<3} trainable={w:,}")
# TODO: add share of the model and adapter size in MB (2 bytes per weight)
Show a solution
PARAMS = 8e9
WIDTH = 4096
LAYERS = 32
def lora_weights(rank, matrices_per_layer):
return 2 * WIDTH * rank * matrices_per_layer * LAYERS
for mats in (2, 4):
for r in (8, 16, 64):
w = lora_weights(r, mats)
print(f"targets={mats} r={r:<3} trainable={w:>11,} share={w / PARAMS:.3%} adapter={w * 2 / 1e6:6.1f} MB")
print(f"full 16-bit copy: {PARAMS * 2 / 1e9:.0f} GB")