LoRA: low-rank fine-tuning
Freeze W and learn the update ΔW = BA with rank r ≪ d. Compute the parameter and optimizer-memory savings, and explain why low-rank updates often suffice for fine-tuning.
Builds on: Rank and low-rank factorization, Supervised fine-tuning on demonstrations, GPU memory for training
What the animation says
The memory wall
Fine-tuning a seven-billion-parameter model the usual way trains every weight. With mixed precision and Adam, each weight costs about sixteen bytes. The weight, its gradient, a full-precision copy, and two Adam moments. Seven billion times sixteen: about a hundred twelve gigabytes, before activations. An eighty-gigabyte GPU can't hold it. Yet does each weight really need its own update? What does chat fine-tuning mostly change?
Freeze W, add a thin side path
Mostly format and persona: a small shift. So maybe a small change is enough. Freeze every pretrained weight. Beside a chosen layer, add a small trainable side path. Its change matrix is the product of two thin factors, so its rank is at most r. Within that layer, outputs can only shift along r directions. That's often enough. The model already computes the features; fine-tuning mostly re-weights and redirects them. Absorbing lots of new knowledge is a bigger, more spread-out change, and may need a larger r.
The LoRA layer, counted
Here's the layer: X times frozen W, plus X times B times A, scaled by alpha over r. A starts at zero, so the side path adds nothing: training begins exactly at the base model. Now count. A d by d layer has d squared weights. B and A together hold just two times d times r. With d of 4096 and r of 8, that's sixty-five thousand versus nearly seventeen million. One two-hundred-fifty-sixth. Your turn: tap every matrix that gets gradients.
The bill for a 7B model
Now a real seven-billion model: width 4096, thirty-two layers. Put rank-eight LoRA on the query and value projections in every layer. Sixty-five thousand per matrix, times two, times thirty-two: about four point two million. Six hundredths of a percent. Frozen weights stay in sixteen-bit, two bytes each: fourteen gigabytes, with no gradients or Adam moments. The adapter, at sixteen bytes per parameter, is about sixty-seven megabytes. But does LoRA also remove activation memory?
Forward pass and merge
No. Backprop still flows through every frozen layer, so activations remain. In code, this line computes X times B first, then times A. That keeps everything thin: the full d by d update is never built. Since A starts at zero, this check prints True: same output as the base model. After training, merge once: add the scaled B times A into W. Same shape, same inference cost. Or keep adapters separate, and swap them per task on one shared base.
Freeze W, learn a thin side path
The LoRA layer
Savings on a 7B model
LoRA forward pass and merge
import numpy as np
rng = np.random.default_rng(0)
d, r, s = 512, 8, 2.0 # s = alpha / r
W = rng.normal(0, 0.02, (d, d)) # frozen, no grad
B = rng.normal(0, 0.02, (d, r)) # trainable
A = np.zeros((r, d)) # trainable, starts at 0
X = rng.normal(size=(4, d)) # 4 tokens
Y = X @ W + s * (X @ B) @ A # never form B @ A
print(np.allclose(Y, X @ W)) # True: starts as base
print(W.size, B.size + A.size) # 262144 8192
W_merged = W + s * B @ A # after training: merge
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses