Skip to content

5.11 Specialization: LoRA and distillation

ready≈ 14 hoursNeeds: 4.6, 1.2
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (LoRA rank explorer)
  4. 4 Do
  5. 5 Check

Low-rank updates. Fine-tuning changes a pretrained model by a small amount, and the change seems to have low “intrinsic dimension”: a few hundred directions in parameter space are enough for many tasks (Aghajanyan et al. 2020). LoRA writes each adapted weight as W + (α/r) B A with A of shape r × din and B of shape dout × r, freezes W, and trains only A and B (Hu et al. 2021, section 4). B starts at zero, so training starts from the pretrained model exactly. The best rank-r approximation of any matrix is its truncated SVD (1.2).

What it saves. The forward pass still runs the full model, so LoRA saves little compute. It saves memory: frozen weights need no gradients and no Adam moments, which for GPT-3 cut training memory from 1.2 TB to 350 GB, and each task’s checkpoint from 350 GB to 35 MB (Hu et al., section 4.2). After training, B A is added into W, so inference costs nothing extra. Keeping the frozen weights in 4 bits (5.7) shrinks memory further (Dettmers et al. 2023, QLoRA).

Distillation. A large teacher’s output distribution says more than the label: an image of a cat is far more likely to be called a dog than a truck. Hinton et al. train a small student to match the teacher’s softmax at a temperature T > 1, which raises those small probabilities, plus the usual loss on labels. The soft term is multiplied by T² so that its gradients keep their size as T changes (Hinton, Vinyals, Dean 2015, section 2). Small language models are often trained this way from larger ones, on the teacher’s next-token distributions (Gemma Team 2024, Gemma 2).

LoRA's gradients, and why distillation multiplies by T²step 1 of 6
  1. W′=W+s BA,s=α/r,G=∂L/∂W′W' = W + s\,BA, \quad s = \alpha / r, \quad G = \partial L / \partial W'

LoRA rank explorer

Low-rank updates and parameter count

Static view on small screens. Open this page on a laptop to interact.

update ΔW (32×32)
rank-4 approximation B A
what it misses
singular values of ΔW, largest first; the bars left of the line are kept
relative error ‖ΔW − BA‖ / ‖ΔW‖ = 24.0% (the smallest possible at rank 4, by Eckart–Young) numbers trained for this 32×32 layer: 256 instead of 1024 (25%)
model: adapt:
Llama 2 7B at rank 4: 2.1M trainable numbers, 0.031% of 6.74B training memory, roughly: full fine-tuning 107.8 GB, LoRA 13.5 GB (frozen weights at 2 bytes each; activations not counted)
90%cat5%dog3%fox1%wolf0.0%car0.0%truck0.0%ship0.2%frog
teacher's soft targets for one image of a cat; entropy 0.44 nats (uniform: 2.08)

A fine-tuning update is often close to low rank, so a rank-r product B A captures most of it with r(din + dout) numbers. Slide the decay towards 1 to see an update with no low-rank structure, where small r misses most of it. In distillation, raising T reveals what the teacher's probabilities on wrong answers say: a cat looks more like a dog or a fox than a truck. A one-hot label carries none of that.

Three heatmaps of a 32 × 32 weight update ΔW: the update itself, its best rank-r approximation B A, and the residual. Sliders set the rank r and how fast the update’s singular values fall; a bar chart shows the singular values with the cutoff after the first r. The readout gives the relative error and the parameter saving for this layer. Buttons pick a model (GPT-2 small, Llama 2 7B or GPT-3 175B) and whether adapters go on W_q and W_v only or on every attention and MLP matrix, with the trainable-parameter count and rough training memory. A last slider sets a distillation temperature, with bar charts of a teacher’s probabilities over eight classes for one cat image. Keyboard: Tab to the sliders and buttons.

Shapes here follow PyTorch’s nn.Linear (weight stored as (outputs, inputs), y = x Wᵀ; see the note in 4.6), so B is (outputs, r) and A is (r, inputs).

LoRA and distillation: edit exercises/stubs/m5_11_lora.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 5.11
Hints · lora_grads 0/3 revealed
Hints · add_lora 0/3 revealed
Hints · distillation_loss 0/3 revealed

Then specialise your model. Open notebooks/5.11-lora-distill.ipynb on Kaggle with a GPU and paste in your 4.6 and 5.11 files. It pretrains your GPT on stories, adapts it to Python source code with every weight and with your adapters at ranks 1, 4 and 16, compares trainable parameters, loss on unseen code and forgetting of the stories, merges the adapters, and trains a smaller student with and without your distillation loss. LoRA usually learns the new domain less well than full fine-tuning and forgets less of the old one (Biderman et al. 2024).

1. Why does LoRA initialise B to zero (and A randomly)?
2. LoRA mainly reduces…
3. What does merging B A into W cost at inference?
4. What do a teacher's soft targets give a student that labels do not?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.