4.3.9Pretraining & Fine-Tuning LLMs

Adapter layers and prefix tuning

2,062 words9 min readdifficulty · medium1 backlinks

WHY does this exist?

WHAT is the trade we make? We give up the ability to freely reshape every weight, and bet that a low-capacity add-on is enough to specialise a model that already learned general features. Empirically, this bet usually wins.


Adapter Layers

HOW it is derived (from first principles)

We want a module that (1) can be inserted without breaking the pretrained forward pass, (2) has few parameters, (3) can express a useful transformation.

  1. Cheap capacity → factorize. A full learned map RdRd\mathbb{R}^d\to\mathbb{R}^d costs d2d^2 params. Force it through a bottleneck rr: down-project drd\to r, then up-project rdr\to d. Params drop to 2dr2dr. With rdr\ll d, that's tiny.
    • Why this step? 2drd22dr \ll d^2 when rdr\ll d. For d=1024,r=16d=1024, r=16: 2dr=32,7682dr=32{,}768 vs d2106d^2\approx 10^6.
  2. Add nonlinearity. Two stacked linears collapse to one linear map. Insert σ\sigma so the adapter can learn a nonlinear correction.
    • Why? Without σ\sigma, WupWdownW_{\text{up}}W_{\text{down}} is just a rank-rr linear map — less expressive.
  3. Wrap in a residual. Initialize Wup0W_{\text{up}}\approx 0 so at the start Adapter(h)h\text{Adapter}(h)\approx h.
    • Why? If the adapter starts as near-identity, the frozen model's behaviour is preserved on step 0, so training starts from a known-good point and won't destroy pretrained knowledge.

Prefix Tuning

HOW / WHY it works

  • The real token queries QQ now attend over extra learned key/value slots. Since softmax mixes over all keys, the prefix can inject task-specific information into every position.
  • Only Pk,PvP_k, P_v (size 2pd2 p d per layer) are trained; WQ,WK,WVW_Q, W_K, W_V stay frozen.
  • Why parametrize K,VK,V and not just prepend embeddings? Prepending raw embeddings ("prompt tuning") only affects layer 0; directly writing keys/values at every layer gives more control and trains more stably. (In practice a small MLP reparametrizes the prefix during training for stability.)


Adapters vs Prefix Tuning (80/20 core)

Adapters Prefix tuning
Where params go inside FFN blocks attention K/V
Trainable form Wdown,WupW_{\text{down}}, W_{\text{up}} Pk,PvP_k, P_v
Inference cost extra tiny FFN per layer longer effective sequence
Init trick Wup0W_{\text{up}}\to 0 (identity) reparam MLP for stability
Params 2Lkdr\approx 2Lk\,dr 2Lpd\approx 2Lpd

The 20% you must know: both freeze the backbone; adapters add a bottleneck FFN with residual init-to-identity; prefix tuning prepends learned key/value vectors; both train ~0.1–3% of weights and give one shared model + tiny per-task deltas.



Recall Feynman: explain to a 12-year-old

Imagine a super-smart robot that already read every book. You don't want to re-teach it everything just to do your homework. So you clip a tiny helper gadget onto it (adapter) or hand it a secret cheat-note it reads before every answer (prefix). The robot's big brain stays exactly the same — you only train the tiny gadget or the little note. That's cheap, fast, and you can keep 100 different gadgets for 100 different jobs, all clipping onto the same one robot.


Flashcards

What problem does PEFT (adapters / prefix tuning) solve?
Full fine-tuning needs a full model copy per task (O(W) params); PEFT freezes the backbone and trains only ~0.1–3% new params, one shared model + tiny deltas.
Write the adapter forward equation.
Adapter(h)=h+Wupσ(Wdownh)\text{Adapter}(h)=h+W_{\text{up}}\,\sigma(W_{\text{down}}h) with WdownRr×dW_{\text{down}}\in\mathbb{R}^{r\times d}, WupRd×rW_{\text{up}}\in\mathbb{R}^{d\times r}.
Why does an adapter use a bottleneck dimension rdr\ll d?
To cut params from d2d^2 (full map) to 2dr2dr; low capacity is cheap and acts as a regularizer.
Why does an adapter need a nonlinearity σ\sigma?
Two stacked linear maps collapse into one; σ\sigma lets the adapter learn a nonlinear correction (otherwise only a rank-rr linear map).
Why initialize Wup0W_{\text{up}}\approx 0 in an adapter?
So Adapter(h)h\text{Adapter}(h)\approx h at step 0 (near-identity), preserving pretrained behaviour and starting from a known-good point.
Approx trainable params for adapters across L layers?
2Lkdr\approx 2Lk\,dr (k adapters/layer), independent of vocab and sequence length.
In prefix tuning, what exactly is learned?
Per-layer key/value prefix matrices Pk,PvRp×dP_k,P_v\in\mathbb{R}^{p\times d}; queries stay real, K and V get the learned prefix prepended.
Prefix-tuning attention equation?
K=[Pk;K], V=[Pv;V]K'=[P_k;K],\ V'=[P_v;V], then softmax(QK/d)V\mathrm{softmax}(QK'^\top/\sqrt d)V'.
Prefix tuning vs prompt tuning?
Prompt tuning edits only layer-0 input embeddings; prefix tuning injects learned K/V at every layer — more expressive/stable.
Prefix param count formula?
2Lpd\approx 2Lpd (keys+values over L layers, prefix length p).
Steel-man: why is full fine-tuning NOT always better than PEFT?
On small data it overfits and forgets; PEFT's low capacity regularizes and often matches FT at ~100× lower storage.
Compute cost of a length-p prefix on a length-n sequence?
Attention becomes O((n+p)2)O((n+p)^2) — longer effective sequence per token.

Connections

  • LoRA and low-rank adaptation — another PEFT: trains low-rank weight deltas instead of inserted modules.
  • Prompt tuning and soft prompts — the layer-0-only special case of prefix tuning.
  • Transformer attention mechanism — prefix tuning edits its K/V.
  • Residual connections — the trick behind adapter init-to-identity.
  • Catastrophic forgetting — why freezing the backbone helps.
  • Full fine-tuning vs PEFT — the storage/compute trade-off.

Concept Map

costs O of W per task

motivates

freezes backbone

trains ~0.1-3%

method 1

method 2

inserted after sublayer

down then up project

adds nonlinearity

wrapped in

W_up near zero

prepends fake tokens

Full fine-tuning

Storage compute problem

Parameter-Efficient Fine-Tuning

Frozen weights

Tiny delta per task

Adapter layers

Prefix tuning

Bottleneck FFN

2dr params

Nonlinear correction

Residual skip connection

Near-identity at init

Trainable keys and values

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek bada LLM already language ko samajh chuka hai — usne internet ka bahut saara text padh liya. Ab agar tum har naye task ke liye poore model ko dobara train karo (full fine-tuning), to har task ke liye ek alag 7B-parameter copy banani padegi. Ye storage aur compute dono mein bahut mehenga hai. Iska smart solution hai PEFT: model ko freeze kar do, aur sirf thode se naye chhote parameters train karo jo model ko task ki taraf steer karein.

Do popular tarike hain. Adapter ek chhota bottleneck FFN hai jo har layer ke andar daala jaata hai: pehle dimension ko dd se rr (jahan rr bahut chhota hai) tak neeche laate hain, phir nonlinearity, phir wapas dd tak up-project, aur ek residual +h add karte hain. WupW_{up} ko lagbhag zero se init karte hain taaki shuru mein adapter almost identity ho — matlab pretrained model ka behaviour kharab na ho. Params sirf 2dr2dr — poore model ka ~1%.

Prefix tuning mein hum weights ko haath nahi lagate. Har attention layer mein hum kuch learnable "nakli tokens" ke key/value vectors (Pk,PvP_k, P_v) prepend kar dete hain. Real tokens ke queries in extra learned keys/values pe bhi attend karte hain, jisse har position pe task-specific information inject ho jaati hai. Ye discrete prompt words nahi hain — ye continuous vectors hain, har layer mein, isliye zyada powerful.

Core baat yaad rakho: "Freeze the giant, train the tiny." Ek hi bada model rakho, aur har task ke liye sirf ek chhota MB-size delta save karo. Isse 100 customers ko serve karna sasta ho jaata hai, aur choti dataset pe overfitting/forgetting bhi kam hoti hai kyunki capacity kam hai (natural regularizer).

Go deeper — visual, from zero

Test yourself — Pretraining & Fine-Tuning LLMs

Connections