Exercise ex-lora-math — the delta math behind LoRA, and its rank ceiling

A Java engineer who has only heard "LoRA freezes the model and trains a small adapter" pictures something architectural — a bolt-on module. LoRA & quantization showed the real picture: freezing W and training two thin matrices A (d_in × r) and B (r × d_out) whose product A @ B is a low-rank update ΔW, added back at inference with a scale factor alpha / r. This exercise builds that math directly: the forward pass, the merge that folds the adapter back into a plain matrix, the parameter count that makes it cheap — and the one fact every rank-r method has to live with, that a target update of true rank r+1 simply cannot be hit exactly, no matter how A and B are chosen.

~1 hruns in the browser 7 checksex-lora-math

What you're building

If you get stuck