AI State of the Art
SOTA/Efficient Fine-tuning (PEFT)/24 min
Section 106

Prefix Tuning

Listen as a podcast →

The Flaw in Adapters That Prefix Tuning Sidesteps

In one sentence: adapters insert extra computation steps into the model; prefix tuning inserts nothing into the computation, it lengthens the input of every attention layer with learned vectors.

When you write an instruction for a model (a prompt) you are limited to words in the vocabulary. You fumble between "Summarize this text concisely", "Give me a short summary" and "Give me the gist". Each phrasing gives a different result, and finding the good one is guesswork.

Prefix tuning turns the question around: instead of hunting for the right words, learn directly the vectors those words would have produced. Vectors that match no word in the vocabulary, tuned by gradient descent, that is, by nudging their values step by step in whatever direction improves the result.

Prefix-Tuning: Optimizing Continuous Prompts for Generation (Li & Liang, 2021)
The Analogy

Hunting for the right phrasing of an instruction is trying every key on a finite keyring. Prefix tuning is cutting a key to measure. The result is not a readable sentence: it is a string of numbers, unreadable to us, that steers the model better than any sentence could.

How It Works: Vectors Injected at Every Layer

What sets prefix tuning apart from prompt tuning: instead of adding vectors only at the model's input, you add them inside every attention layer.

Concretely, in each layer, learned vectors are glued in front of the keys and the values:

K′=[PK  ;  K]V′=[PV  ;  V]K' = [P_K \; ; \; K] \qquad V' = [P_V \; ; \; V]

The brackets here just mean "placed end to end". KK and VV are the usual keys and values, PKP_K and PVP_V the learned vectors, and K′K', V′V' the lengthened lists attention will actually consult.

What Is Learned, What Is Computed, Do Not Miss This

The formula above puts two things of completely different natures side by side, and this is exactly where readers trip.

  • KK and VV are activations: recomputed on every pass, for every text, by pushing the text's words through the weight matrices WKW_K and WVW_V. They do not exist outside a text being processed.
  • PKP_K and PVP_V are learned parameters, frozen after training. They, and only they, are what gets saved and shipped. They are identical no matter what text is processed.

Put another way, prefix tuning learns ready-made activations and pretends to have computed them. That is why it needs no new weight matrix at all.

And the base model? Entirely frozen: neither WKW_K, nor WVW_V, nor anything else moves.

The real words of the text can then "consult" the prefix through attention and find information useful to the task, like a crib sheet planted at the start of each layer.

PKP_K and PVP_V hold ll vectors each, where ll is the prefix length: the number of fake words added.

Stabilization via Reparameterization

Optimizing these vectors directly turns out to be unstable: training oscillates and refuses to converge. The paper's remedy:

  1. Keep a smaller matrix PP of size l×dl \times d.
  2. Push it through a small multi-layer perceptron (MLP, two or three layers of neurons): P′=MLP(P)P' = \text{MLP}(P). It is P′P' that serves as the prefix.
  3. Once training is over, throw the MLP away and keep only P′P'.

Why this works: forcing every prefix vector to come out of one small shared network stops them from each wandering off on their own. The MLP constrains the search space. It is drawing with a template rather than freehand.

Comparison of two Transformer blocks. On the left, a regular block: multihead self-attention, fully connected layer, skip connection, LayerNorm, fully connected layers, skip connection, LayerNorm. On the right, the same block plus a Soft prompt box feeding fully connected layers whose output joins the block input in a Concat operation placed just before the multihead self-attention.
The block on the left is an ordinary Transformer layer. On the right, the same layer with the prefix: the learned vectors (Soft prompt) pass through a small network (the reparameterization described just above) then get glued in front of the block input, before attention. This wiring is repeated inside every layer of the model, which is what sets prefix tuning apart from a prompt written only at the input.Source : Sebastian Raschka, via Lightning AI

Number of Parameters

The count reads left to right:

l⏟fake words×d⏟model width×L⏟layers×2⏟K and V\underbrace{l}_{\text{fake words}} \times \underbrace{d}_{\text{model width}} \times \underbrace{L}_{\text{layers}} \times \underbrace{2}_{K \text{ and } V}

Where does the factor of 2 come from? From needing one prefix for the keys and one for the values. Queries get none: queries are asked by the words of the text, and a fake prefix word has nothing to ask, it is there to be consulted, not to consult.

Concrete numbers, on GPT-2 Medium (d=1024d = 1024, L=24L = 24 layers, prefix of l=10l = 10):

10×1024×24×2=491,520 parameters10 \times 1024 \times 24 \times 2 = 491{,}520 \text{ parameters}

Against the model's 345 million parameters, that is 491,520/345×106=0.14%491{,}520 / 345 \times 10^6 = 0.14\%. The paper rounds to 0.1%, and that is the figure it leads with.

Prefix Length: A Balancing Act

Too short, five fake words, and the prefix has no room to encode the task. Too long, five hundred, and it eats the available context while the model risks overfitting: memorizing the examples instead of extracting a rule. The common range runs from ten to two hundred.

Watch the real cost: parameters grow in proportion to ll. The same calculation with l=100l = 100 gives 100×1024×24×2=4.9100 \times 1024 \times 24 \times 2 = 4.9 million parameters, about 10 MB in 16 bits. That is no longer negligible at all.

Prompt Engineering vs Prefix Tuning

AspectWritten instructionPrefix tuning
What you pickwords from the vocabularyany string of numbers
How you find itby hand, by trialby gradient descent
Where it actsat the input onlyin every layer
Readabilityyou can reread your instructionunreadable
What you storea texta matrix of a few hundred thousand numbers

The decisive difference is the third row. A written instruction enters the model only through the front door, and its influence dilutes layer by layer. The prefix is replanted at every floor.

Empirical Results

The paper tests on two families of task:

  • Table-to-text (the E2E, WebNLG and DART datasets): turning a record of structured data into a readable sentence, "name: Starbucks, type: coffee shop" becomes "Starbucks serves coffee".
  • Summarization (the XSUM dataset): reducing news articles to a single sentence.

What the abstract claims, and this is checkable: by learning only 0.1% of the parameters, prefix tuning matches full fine-tuning when data is plentiful, beats it when data is scarce, and generalizes better to topics unseen during training.

On top, a results table on the E2E, WebNLG and DART datasets for GPT-2 Medium and GPT-2 Large, comparing Fine-tune, FT-top2, Adapter and Prefix at 0.1% of parameters, with BLEU, NIST, METEOR, ROUGE-L and CIDEr scores that stay very close from one row to the next. Below, four charts with a red and a blue curve showing ROUGE and BLEU scores against training set size, from 50 to 500 examples, the prefix tuning curve staying above the fine-tuning one.
On top, the two rows to compare are Fine-tune and Prefix (0.1%): the scores sit within a few tenths of each other, even though the second one trains a thousandth of the parameters. The columns hold automatic measures of how closely the generated text resembles a human-written reference: BLEU (Bilingual Evaluation Understudy) counts word sequences shared by the two, ROUGE does the same from the reference's point of view, and METEOR, NIST and CIDEr are variants of the idea. Below, the small-dataset effect: from 50 to 500 training examples, the prefix-tuning curve stays above the full fine-tuning one across the whole range.Source : Li and Liang paper, via Sebastian Raschka
A Real Advantage When Data Is Scarce

The abstract says it outright: when training examples are few, prefix tuning does better than full fine-tuning.

The explanation fits in an image. With millions of free parameters and two hundred examples, the model has room to memorize all two hundred, which teaches it nothing general. With only half a million parameters, it has no room to cheat: it is forced to find a rule. Few parameters, when data is scarce, is an advantage rather than a concession.

Ideal Use Case: Massive Multi-Task

Imagine a service that has to cover a thousand different tasks: legal summarization, technical translation, code generation. Under full fine-tuning you would need a thousand copies of the model. With prefix tuning, one model stays in memory and you swap the prefix per task.

The prefix's weight is computable: with l=10l = 10 on GPT-2 Medium, 491,520 numbers in 16 bits come to about 1 MB. A thousand tasks therefore fit in a gigabyte, against 1,000 x 700 MB (700 GB) for a thousand copies of the model.

Two-part diagram. On top, under the heading Fine-tuning, three large stacked pink rectangles labelled Transformer Translation, Transformer Summarization and Transformer Table-to-text, each one a complete copy of the model. Below, under the heading Prefix-tuning, a single large grey rectangle labelled Transformer Pretrained, preceded by three small stacked boxes labelled Prefix Translation, Prefix Summarization and Prefix Table-to-text. Both halves process the same example, a record name Starbucks type coffee shop turned into the sentence Starbucks serves coffee.
The contrast is visual. On top, one task means one entire copy of the model, in pink because everything is retrained. Below, a single grey model, never modified, with one small box per task placed in front of it. Switching from translation to summarization amounts to swapping that small box.Source : Li and Liang paper (figure 1), via Lilian Weng's Lil'Log

The Limits

It Eats the Available Context

The prefix occupies slots in the context window exactly as real words do. A hundred fake words on a 2,048-token window is 100/2,048=4.9%100 / 2{,}048 = 4.9\% of the budget gone before the document is even read. On long-document tasks, that hurts.

Its Influence Only Passes Through Attention

The prefix can act in one way only: by making itself consultable by the words of the text. It does not touch the small neural networks that follow attention, nor the weights themselves. For a deep adaptation (a new language, a far-off domain) that influence is too indirect.

Training Is Trickier Than with LoRA

Prefix tuning has a reputation for sensitivity to its settings: a learning rate slightly too high and training goes off the rails. That is precisely the reason for the MLP reparameterization described above.

LoRA is better behaved, because its correction starts from zero and stays confined to low rank. Budget time for tuning.

Scaling Up

On models of several billion parameters, prefix tuning generally sits below LoRA. It was designed in the era of GPT-2 and T5, models a hundred times smaller, and growth has not favoured it. We put no number on that gap: it varies by task and we found no reference measurement to cite.

Place in the Ecosystem

Prefix tuning matters above all for what it established: a model can be adapted without a single new weight, by learning only activations to glue in front of the real ones. It opened the way to prompt tuning, which pushes the same idea to its extreme by keeping only the model's input.

In practice it is rarely the first choice today, LoRA having taken over. It stays relevant for:

  • thousands of tasks served by one model in memory;
  • very constrained storage, provided the prefix stays short;
  • generation with a fixed output format.

Available in Hugging Face's PEFT library via PrefixTuningConfig. The full comparison is in PEFT: comparing the methods.

Check your understanding0 / 5

1. In the formula K' = [P_K ; K], what distinguishes P_K from K?

2. The parameter count has a factor of 2: one prefix for the keys, one for the values. Why no third one for the queries?

3. What separates prefix tuning from a carefully written instruction at the top of the prompt?

4. Why can prefix tuning beat full fine-tuning when training examples are scarce?

5. The page warns that training prefix tuning is trickier than training LoRA. Why?