← Study Guide Mind Map Home

Math Foundations — Lecture 9

Pretraining objective, full FT cost, low-rank decomposition for LoRA, parameter counts, QLoRA quantisation.

Pretraining objective

A causal language model with parameters \(\theta\) is trained by minimising the negative log-likelihood of the next token across a corpus \(\mathcal{D}\):

\[\mathcal{L}_{\text{pre}}(\theta) = -\sum_{x \in \mathcal{D}}\sum_{t=1}^{|x|} \log p_\theta(x_t \mid x_{

For masked LMs (BERT) the inner sum runs over masked positions only. Either way the loss is computed on raw text — no labels needed, so pretraining can use trillions of tokens.

Full finetuning — memory cost

Let \(N\) be the total parameter count. Per-parameter memory during Adam training (mixed precision, naïve):

\[\text{Mem}_{\text{train}} \approx \underbrace{2N}_{\text{fp16 weights}} + \underbrace{4N}_{\text{fp32 master}} + \underbrace{4N + 4N}_{\text{Adam } m, v} + \underbrace{\text{activations}}_{\text{batch-dep.}}\]

That's ~16 bytes/parameter just for weights + optimiser. For \(N=7\times 10^9\): \(\sim 112\) GB — well beyond a single 24 GB GPU.

Quick Check: Roughly how much GPU memory does naïve full FT of a 7B model need just for weights and Adam state?
~100 GB (does not fit on one consumer GPU)
~7 GB
~700 MB

LoRA — low-rank decomposition

Take any frozen weight matrix \(W_0 \in \mathbb{R}^{d \times d}\) (assume square for simplicity). LoRA writes the finetuning update as a product of two narrow factors:

\[\Delta W = B\,A, \qquad B \in \mathbb{R}^{d \times r}, \quad A \in \mathbb{R}^{r \times d}, \quad r \ll d\]

The forward pass becomes

\[h = (W_0 + \Delta W)\,x = W_0\,x + B\,(A\,x)\]

Only \(A\) and \(B\) are trainable; \(W_0\) stays frozen. By construction \(\operatorname{rank}(\Delta W) \le r\), encoding the assumption that the useful finetuning update lies in a low-dimensional subspace.

Initialisation

Choose \(A \sim \mathcal{N}(0, \sigma^2)\) and \(B = 0\). Then at step 0:

\[\Delta W = B\,A = 0 \cdot A = 0 \implies h = W_0\,x\]

i.e. the model output is identical to the pretrained model — training departs smoothly from the foundation.

Optional scaling

\[h = W_0\,x + \frac{\alpha}{r}\,B\,A\,x\]

The factor \(\alpha/r\) (LoRA scaling) decouples learning-rate effects from the choice of rank \(r\).

Parameter count: full FT vs LoRA

One \(d \times d\) projection inside a Transformer layer.

Full FT\(d^2\) trainable parameters.
LoRA\(d r + r d = 2 d r\) trainable parameters.

Ratio of LoRA to full FT for one matrix:

\[\frac{2 d r}{d^2} = \frac{2 r}{d}\]

Concrete numbers for typical Transformer projection sizes:

\(d\)\(r\)Full FT (\(d^2\))LoRA (\(2dr\))Ratio
7688589{,}82412{,}288~48× smaller
4096816{,}777{,}21665{,}536~256× smaller
40961616{,}777{,}216131{,}072~128× smaller
81921667{,}108{,}864262{,}144~256× smaller

Across an entire 7B model, LoRA on attention projections typically yields <0.1% of \(N\) as trainable parameters — small enough that optimiser state and adapter checkpoints become trivial.

Quick Check: For \(d = 4096\) and rank \(r = 8\), how many trainable parameters does LoRA add to one weight matrix?
\(d^2 = 16{,}777{,}216\)
\(2 d r = 65{,}536\)
\(r^2 = 64\)

Inference: merging the delta

After training, define

\[W_{\text{merged}} = W_0 + B A \in \mathbb{R}^{d \times d}\]

Replace \(W_0\) with \(W_{\text{merged}}\) and discard \(A, B\). The model is now indistinguishable from a fully fine-tuned model at the level of one matrix — zero added FLOPs and zero added latency at inference. (Compare adapters/prefix tuning, which insert extra computation that cannot be folded away.)

QLoRA — quantising the base

QLoRA stores the frozen base weights in 4-bit precision. A real-valued weight \(w\) is approximated by

\[\hat{w} = s \cdot q, \qquad q \in \{c_0, c_1, \dots, c_{15}\}\]

where \(\{c_i\}\) are 16 fixed codebook values and \(s\) is a per-block scale. The NF4 codebook places the \(c_i\) at quantiles of a unit normal, matching the empirical weight distribution of pretrained Transformers.

Memory ratio versus fp16 storage of the same model:

\[\frac{\text{4-bit}}{\text{fp16}} = \frac{4}{16} = \frac{1}{4}\]

During training: dequantise on the fly inside each kernel, run the LoRA bypass \(B A\) in bf16, accumulate gradients only into \(A, B\). The base never updates, so its 4-bit quantisation never degrades. Combined with paged Adam moments, a 65B model is trainable on a single 48 GB GPU.

Quick Check: Storing the base model in 4-bit instead of fp16 saves what fraction of weight memory?
3/4 (memory drops to one quarter)
1/2
No saving — just faster compute

Summary: parameter footprint

Per Transformer projection matrix of size \(d \times d\):

Full FT\(d^2\) trainable, \(d^2\) base — base also updates.
LoRA\(2 d r\) trainable, \(d^2\) base frozen in fp16.
QLoRA\(2 d r\) trainable, \(d^2 / 4\) bytes of base storage (4-bit).
Adapters\(2 d r\) trainable + extra forward pass at inference.
Prompt tuning\(L \cdot d\) trainable for \(L\) virtual tokens — smallest, weakest unless model is huge.