Search

Search papers and lessons

Adapter Tuning

Level 3 · 8 min read

Adapter tuning freezes a pretrained network and trains only small bottleneck modules inserted into each block, so each new task stores a few percent of the parameters.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

TL;DR

Adapter tuning adapts a large pretrained network to a new task by freezing every original weight and training only small modules, called adapters, that are inserted into each block. Each adapter squeezes its input down to a narrow vector, applies a nonlinearity, and expands it back, then adds the result to the input. Because only the adapters are trained and stored, each new task costs a few percent of the parameters of a full copy.

Builds on: Fine-Tuning, Transformer · Leads to: Low-Rank Adaptation (LoRA), QLoRA, IP-Adapter: Text Compatible Image Prompt Adapter for Diffusion Models

Where it's used

  • Serving many tasks from one model. A company that answers questions for dozens of customers can keep one frozen base model and store one small adapter per customer. Each adapter is a few megabytes, while a full copy of a base model with a hundred million or more parameters is hundreds of megabytes.
  • Early transfer to language tasks. In 2019 Houlsby and colleagues inserted adapters into BERT, a transformer pretrained on text, and tuned it on many classification benchmarks. They reported results close to full fine-tuning while training only a few percent of the parameters per task.
  • Conditioning image models. Image diffusion models such as Stable Diffusion were adapted with IP-Adapter, a small add-on that lets an image prompt steer generation. The base model stays frozen, in the same spirit as an adapter.

How it works

We follow one tiny adapter on a vector of size 4, with a bottleneck of size 1.

  1. Start with the frozen block's output. The block produces the vector h = (1, 2, 3, 4). Its weights are frozen and will not change.
  2. Squeeze down. Multiply h by a 1×41 \times 4 down-projection with row (0.5, 0, 0, 0). The result is 0.5. Check: 0.5 times 1 is 0.5, and the other terms are 0.
  3. Apply a nonlinearity. Pass 0.5 through a ReLU, which replaces negative numbers with 0 and keeps positive ones, so it leaves 0.5 unchanged. This is the only place the adapter bends its input.
  4. Expand back up. Multiply the bottleneck value by a 4×14 \times 1 up-projection with column (2, 0, 0, 0). The result is (1, 0, 0, 0). Check: 2 times 0.5 is 1.
  5. Add back the input. Add the adapter's output to the original vector: (1, 2, 3, 4) plus (1, 0, 0, 0) gives (2, 2, 3, 4). The adapter changes only the first entry here.
  6. Start from no change. If the up-projection is initialised to zeros, step 4 gives (0, 0, 0, 0) and the output is exactly h. Training then moves the adapter away from identity only as far as the task needs.
flowchart LR
  H[block output h] --> D[down-project to bottleneck]
  D --> A[nonlinearity]
  A --> U[up-project back to full width]
  H --> ADD((add))
  U --> ADD
  ADD --> N[next layer]

The math

Definition: Adapter
An adapter for an input h in Rd\mathbb{R}^d with bottleneck width mm is the map h↦h+Wup σ(Wdown h+bdown)+buph \mapsto h + W_{up}\,\sigma(W_{down}\,h + b_{down}) + b_{up}, where WdownW_{down} is m×dm \times d, WupW_{up} is d×md \times m, and σ\sigma is a nonlinearity such as ReLU. Only the adapter's weights are trained.

Step 1 in symbols

The frozen block produces h∈Rdh \in \mathbb{R}^d. The example above has d=4d = 4 and m=1m = 1, with Wdown=(0.5,0,0,0)W_{down} = (0.5, 0, 0, 0) and bdown=0b_{down} = 0.

Step 2 in symbols: the bottleneck

z=σ(Wdown h+bdown)z = \sigma(W_{down}\,h + b_{down}) is a vector of length mm. In the example Wdown h=0.5⋅1=0.5W_{down}\,h = 0.5 \cdot 1 = 0.5, and σ(0.5)=0.5\sigma(0.5) = 0.5.

Step 3 in symbols: expansion and residual sum

h′=h+Wup z+buph' = h + W_{up}\,z + b_{up}. In the example Wupz=(2,0,0,0)⊤⋅0.5=(1,0,0,0)W_{up} z = (2, 0, 0, 0)^{\top} \cdot 0.5 = (1, 0, 0, 0), so h′=(2,2,3,4)h' = (2, 2, 3, 4).

Step 4 in symbols: parameter count

An adapter has dm+mdm + m weights in WdownW_{down} and bdownb_{down}, and md+dmd + d in WupW_{up} and bupb_{up}, so 2dm+m+d2dm + m + d in total. With one adapter per block and LL blocks, the trainable count is L(2dm+m+d)L(2dm + m + d).

Worked examples

Example
Tiny: one adapter on a 4-vector

Use d=4d = 4, m=1m = 1, Wdown=(0.5,0,0,0)W_{down} = (0.5, 0, 0, 0) and Wup=(2,0,0,0)⊤W_{up} = (2, 0, 0, 0)^{\top}, with all biases zero. The input is h=(1,2,3,4)h = (1, 2, 3, 4).

  1. The bottleneck is σ(0.5⋅1)=0.5\sigma(0.5 \cdot 1) = 0.5.
  2. The expansion is 2⋅0.5=12 \cdot 0.5 = 1 in the first entry and 0 elsewhere.
  3. The output is h+(1,0,0,0)=(2,2,3,4)h + (1, 0, 0, 0) = (2, 2, 3, 4).
  4. The first entry changed from 1 to 2 and the rest did not change, because the adapter's weights only read and write the first coordinate.
Example
Standard: counting parameters for a 12-block transformer

Example 2 (standard). Take a transformer with d=768d = 768, bottleneck m=64m = 64, L=12L = 12 blocks, and two adapters per block, one after each sublayer. A full copy of a base model of this size has about 110 million parameters.

  1. One adapter has 2⋅768⋅64+64+768=98,304+832=99,1362 \cdot 768 \cdot 64 + 64 + 768 = 98{,}304 + 832 = 99{,}136 trainable parameters.
  2. There are 2⋅12=242 \cdot 12 = 24 adapters, so the total is 24⋅99,136=2,379,26424 \cdot 99{,}136 = 2{,}379{,}264.
  3. The fraction is 2,379,264/110,000,000≈0.0222{,}379{,}264 / 110{,}000{,}000 \approx 0.022, about 2 percent.
  4. Training 2.4 million numbers is much cheaper than training 110 million, and the saved task file is about 9.5 MB in 32-bit floats instead of about 440 MB.
Example
Realistic: serving 500 customers from one base model

Example 3 (realistic). A team runs one frozen base model for 500 customers. Each customer gets an adapter, so the team stores one base copy and 500 adapters. Using the Example 2 numbers, each adapter takes about 9.5 MB. The team stores 440+500⋅9.5≈5,190440 + 500 \cdot 9.5 \approx 5{,}190 MB, about 5.2 GB. Storing 500 full copies would take 500⋅440=220,000500 \cdot 440 = 220{,}000 MB, about 220 GB.

Try it: a model has d=1024d = 1024 and bottleneck m=16m = 16, with one adapter in each of L=24L = 24 blocks. How many trainable parameters are there? Answer: one adapter has 2⋅1024⋅16+16+1024=32,768+1,040=33,8082 \cdot 1024 \cdot 16 + 16 + 1024 = 32{,}768 + 1{,}040 = 33{,}808, and 24⋅33,808=811,39224 \cdot 33{,}808 = 811{,}392.

Common mistakes and tradeoffs

It is tempting to think that adapters are a separate model that runs beside the original. In fact the adapter sits inside the block, and its output is added to the frozen block's output, so the adapted model is one network with extra modules in its path.

It is tempting to think that the bottleneck width is a free choice that does not matter. In fact a narrow bottleneck stores fewer task-specific parameters and can limit how much the model can change, while a wide one approaches full fine-tuning in cost. The width is the main knob.

It is tempting to think that adapters always add latency at inference. In fact each adapter adds a small matrix product per block, which costs some time. Methods such as Low-Rank Adaptation (LoRA) can merge their update into the frozen weights after training, so the deployed model has no extra layers.

Use adapters when many tasks share one base model and storage or switching cost matters. Do not use them when a single task needs maximum accuracy and memory is not a concern, because full Fine-Tuning often does slightly better. Alternatives include Low-Rank Adaptation (LoRA), which learns a low-rank change to each existing weight matrix, and Transfer Learning with only the final layer trained.

History

Before 2019, adapting a large pretrained language model usually meant copying the whole model and training it on each task. This made many tasks expensive to store and serve. Houlsby and colleagues showed in 2019 that a small bottleneck module inserted into each transformer block, trained while the rest stayed frozen, could come close to full fine-tuning on many benchmarks. The idea caught on because it turned a per-task copy into a small file. Later methods, including LoRA in 2021, kept the same goal and changed the form of the small update.

Recap

  • A transformer block can be adapted by inserting a small bottleneck module whose output is added to the block's output.
  • The bottleneck squeezes the vector down, applies a nonlinearity, and expands it back, so only a few percent of the parameters are trained.
  • Starting the up-projection at zero makes the adapted model begin exactly as the frozen one.
  • Many tasks can share one base model, with one small adapter stored per task.
  • Read next: Low-Rank Adaptation (LoRA), which learns a low-rank update to existing weights, and QLoRA, which combines this with quantized base weights.

Check yourself

QuestionWhy are the adapter's weights initialised so that its output starts at zero, and what would go wrong with a random start?Show answer
If the up-projection starts at zero, the adapted block initially computes exactly what the pretrained block computes, so the starting point keeps the pretrained behaviour. A random start would add a random perturbation to every block before training begins, which can damage the pretrained features the adapter is supposed to build on.
QuestionAn adapter has d=512d = 512 and bottleneck m=8m = 8. How many trainable parameters does it have, counting both projections and both biases?Show answer
The count is 2dm+m+d=2⋅512⋅8+8+512=8,192+520=8,7122dm + m + d = 2 \cdot 512 \cdot 8 + 8 + 512 = 8{,}192 + 520 = 8{,}712.
QuestionWhy is the bottleneck needed? What happens to the parameter count if the adapter used m=dm = d instead?Show answer
The bottleneck keeps the number of trainable parameters small. With m=dm = d the adapter has about 2d22d^2 weights, which is the same order as a full dense layer, so most of the saving disappears.
QuestionA deployed service serves 100 tasks from one base model. Each task adapter is 9.5 MB and the base model is 440 MB. Compare the storage for adapters with full copies.Show answer
Adapters take 440+100⋅9.5=1,390440 + 100 \cdot 9.5 = 1{,}390 MB. Full copies take 100⋅440=44,000100 \cdot 440 = 44{,}000 MB. The adapter approach uses about 3 percent of the storage of full copies.

Sources