Part 5 of 15 in LLM fine-tuning techniques
Prefix tuning adapts a frozen language model by training only a short sequence of continuous vectors, the "prefix", that every later token can attend to at every layer. Xiang Lisa Li and Percy Liang of Stanford introduced it in January 2021 and reported that, training about 0.1% of GPT-2's parameters, it matched full fine-tuning on table-to-text generation (arXiv 2101.00190).
This is the first of three pages on prompt-based parameter-efficient methods. The other two are P-Tuning and prompt tuning (soft prompts). The weight-based methods (LoRA, QLoRA, adapters, BitFit) change or add weights inside the network instead.
What is prefix tuning?
Prefix tuning is a parameter-efficient fine-tuning method for text generation. It keeps every language-model weight frozen and learns a small task-specific matrix of continuous vectors that is prepended to the input at every Transformer layer. The model attends to these vectors as if they were "virtual tokens", but they do not correspond to real words (Li & Liang, Abstract and Sec. 1).
The motivation in the paper is storage. Full fine-tuning produces a modified copy of the whole model for every task. With prefix tuning you store one copy of the model plus one small prefix per task; the paper puts the overhead at 250K parameters for its table-to-text setup and says it stores "1000x fewer parameters than fine-tuning" (Sec. 1).
How does prefix tuning work?
Prefix tuning prepends a trainable matrix P to the activations of every layer. For a decoder-only model the sequence becomes [PREFIX; x; y]; for an encoder-decoder model such as BART a prefix goes in front of both the encoder input and the decoder input. Only P is updated; the model's own parameters stay fixed (Li & Liang, Sec. 4.2).

Step by step, as the paper describes it:
- Pick a prefix length. The default in the paper is 10 positions (Sec. 5.3); the tuned values in Table 5 range from 5 (E2E, WebNLG) to 100 (XSUM summarization).
- Create the prefix matrix. P has one row per prefix position and one column per activation value at that position, across all layers (Sec. 4.2, Eq. 3). In GPT-2 each layer's activation is a key-value pair, and "the dimension of each key and value is 1024" (footnote 1).
- Compute activations. At a prefix position the activation is copied straight from P. At any other position the Transformer computes it normally, but because the prefix is always in the left context, every later activation still depends on P (Sec. 4.2).
- Train with the normal loss. The objective is the usual log-likelihood of the output y; only the prefix parameters receive gradients (Sec. 4.2).
- Reparameterize during training. Optimizing P directly "leads to unstable optimization and a slight drop in performance", so the authors build P from a smaller matrix P′ passed through an MLP: P[i,:] = MLP(P′[i,:]). P′ has k columns, with k = 512 for table-to-text and 800 for summarization (Sec. 4.3, footnote 4).
- Keep only the prefix. After training, the MLP is dropped and only P is saved (Sec. 4.3).
Worked example: how many parameters does a prefix have?
The size of the stored prefix is prefix length × number of layers × 2 (key and value) × key/value dimension. GPT-2 Medium has 24 layers and a hidden size of 1,024 (model config), and the paper's key and value dimension is 1,024 (footnote 1).
| Setting | Prefix length (Table 5) | Calculation | Prefix parameters | Paper's figure (footnote 9) |
|---|---|---|---|---|
| E2E | 5 | 5 × 24 × 2 × 1,024 | 245,760 | 250K |
| WebNLG | 5 | 5 × 24 × 2 × 1,024 | 245,760 | 250K |
| DART | 10 | 10 × 24 × 2 × 1,024 | 491,520 | 500K |
Against the 345M parameters the paper gives for GPT-2 (footnote 9), 245,760 is about 0.07%, which the paper rounds to "0.1%". The same shape appears in Hugging Face PEFT, whose prefix encoder outputs 2*layers*hidden values per virtual token (PEFT docs). The MLP used during training adds more parameters, but it is thrown away afterwards.
When was prefix tuning introduced, and by whom?
Prefix tuning was introduced in "Prefix-Tuning: Optimizing Continuous Prompts for Generation" by Xiang Lisa Li and Percy Liang of Stanford University. The paper was posted to arXiv on Jan. 1, 2021 (arXiv 2101.00190) and published at ACL-IJCNLP 2021, the 59th annual meeting of the Association for Computational Linguistics (ACL Anthology).
The authors' code is on GitHub at XiangLi1999/PrefixTuning.
What results did the prefix-tuning paper report?
The paper reports that with 0.1% of parameters, prefix tuning matched or beat full fine-tuning on three table-to-text datasets using GPT-2, came slightly below fine-tuning on XSUM summarization with BART, beat fine-tuning by 2.9 BLEU on average in low-data settings, and generalized better to unseen topics (Li & Liang, Sec. 6).
Table-to-text, GPT-2 Medium (Table 1, BLEU, higher is better):
| Method | Parameters trained | E2E | WebNLG (all) | WebNLG (unseen categories) | DART |
|---|---|---|---|---|---|
| Full fine-tuning | 100% | 68.2 | 46.5 | 27.7 | 46.2 |
| Fine-tune top 2 layers | n/a | 68.1 | 36.0 | 18.9 | 41.0 |
| Adapter | 3% | 68.9 | 54.9 | 48.3 | 45.2 |
| Adapter | 0.1% | 66.3 | 50.2 | 45.1 | 42.4 |
| Prefix tuning | 0.1% | 69.7 | 55.1 | 45.6 | 46.4 |
At the same 0.1% budget, prefix tuning beat adapters by 4.1 BLEU per dataset on average (Sec. 6.1). With GPT-2 Large, E2E BLEU was 70.3 for prefix tuning versus 68.5 for fine-tuning (Table 1).
Summarization, BART-Large on XSUM (Table 2, ROUGE-L): fine-tuning 37.25, prefix tuning with 2% of parameters 36.05, prefix tuning with 0.1% 35.05. The authors list three differences that could explain the gap: XSUM has 4x more examples, its inputs are 17x longer, and summarization needs reading comprehension (Sec. 6.2).
Low data (Sec. 6.3): on subsets of 50 to 500 examples, averaged over 10 models per size, prefix tuning beat fine-tuning by 2.9 BLEU on average, and the gap narrowed as data grew.
Extrapolation (Sec. 6.4, Table 3): trained on news and tested on sports articles, prefix tuning scored ROUGE-L 31.51 against 30.26 for fine-tuning. On WebNLG categories never seen in training, prefix tuning scored 45.6 BLEU against 27.7 (Table 1).
Ablations (Sec. 7): tuning only the input embeddings ("embedding-only", one virtual token) dropped E2E BLEU to 48.1 from 69.7, and placing the trainable activations between x and y ("infix") gave 67.9 (Table 4). Performance rose with prefix length up to 200 for summarization and 10 for table-to-text, then dipped slightly (Sec. 7.1).
How is prefix tuning different from P-Tuning, prompt tuning and adapters?
Prefix tuning puts trainable vectors at every layer and freezes the model. Prompt tuning puts them only at the input layer. P-Tuning also works at the input, mixes learned vectors with real words, and in its SuperGLUE experiments also tuned the model. Adapters insert new layers inside the network (Lester et al., Sec. 4; Li & Liang, Sec. 8.3).
| Prefix tuning | Prompt tuning | P-Tuning (v1) | Adapter tuning | |
|---|---|---|---|---|
| Where trainable parameters live | Prefix activations at every layer | Input embeddings only | Input embeddings, via an LSTM/MLP prompt encoder | New bottleneck layers between frozen layers |
| Base model | Frozen | Frozen | Frozen (LAMA) or tuned (SuperGLUE) | Frozen |
| Reparameterization in training | MLP | None needed (Lester et al., Sec. 4) | LSTM or MLP encoder | None |
| Original focus | Generation (GPT-2, BART) | Classification as text-to-text (T5) | Understanding (LAMA, SuperGLUE) | Understanding and generation |
| Paper's trained share | 0.1% (table-to-text) | Under 0.01% for models over 1B (Lester et al., Sec. 4) | Varies | About 3.6% in the work Li & Liang compare against (Sec. 2) |
Two differences the prefix-tuning paper calls out:
- Batching across users. Because the model is untouched, examples with different prefixes can share one batch; the paper says this is not possible with adapters, which sit between shared layers (Sec. 8.2).
- Fewer parameters than adapters. Prefix tuning gave "a further 30x reduction in task-specific parameters" compared with adapter work that tunes about 3.6% (Sec. 2). For more on that family, see adapter tuning.
Prefix tuning differs from LoRA in the same way: LoRA learns low-rank updates to weight matrices, while prefix tuning never changes a weight.
Which libraries support prefix tuning?
Hugging Face PEFT supports prefix tuning through PrefixTuningConfig and get_peft_model. The current docs (PEFT v0.22.0, read Dec. 4, 2026) say the prefix parameters are inserted in all model layers, and note that for encoder-decoder models PEFT applies the prefix only to the decoder, which differs from the paper (PEFT prefix tuning docs).
The authors' original implementation, built on Hugging Face Transformers (Sec. 5.3), is at XiangLi1999/PrefixTuning.
How do you use prefix tuning in code?
This sets up prefix tuning on GPT-2 Medium, the paper's table-to-text model, with PEFT. Every class and argument name below is from the PEFT v0.22.0 reference (PrefixTuningConfig, PeftModel).
from transformers import AutoModelForCausalLM
from peft import PrefixTuningConfig, get_peft_model
model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2-medium")
config = PrefixTuningConfig(
task_type="CAUSAL_LM",
num_virtual_tokens=10, # the paper's default prefix length (Sec. 5.3)
prefix_projection=True, # MLP reparameterization during training (Sec. 4.3)
encoder_hidden_size=512, # hidden size of that MLP
)
model = get_peft_model(model, config)
model.print_trainable_parameters()
# Train with a standard transformers Trainer or your own loop.
prefix_projection defaults to False in PEFT, in which case the prefix is trained directly with no MLP (PEFT docs). The paper's hyperparameters for this setup were AdamW, a linear schedule, learning rate 5e-5 (8e-5 for E2E), batch size 5 and 5 to 10 epochs (Sec. 5.3, Table 5).
Common pitfalls
- Training the prefix directly can be unstable. The paper found direct optimization "very sensitive to the learning rate and initialization" and used the MLP reparameterization (Sec. 4.3, footnote 3). In PEFT this is
prefix_projection=True. - Random initialization hurts with little data. Initializing the prefix with activations of real words "significantly outperforms random initialization, in low-data settings" (Sec. 7.4, Figure 5). PEFT offers
initialize_kv_prefix_from_text, but only whenprefix_projection=False(PEFT docs). - Longer is not always better. Past a threshold, longer prefixes gave lower training loss but slightly worse test scores, which the authors read as overfitting (Sec. 7.1, footnote 14).
- Encoder-decoder models in PEFT. PEFT applies the prefix to the decoder only and warns that performance "could be sub-par" for seq2seq models (PEFT docs).
- Harder generation tasks show a gap. On XSUM, the 0.1% prefix trailed fine-tuning by 2.2 ROUGE-L (Table 2).
FAQ
Is prefix tuning better than full fine-tuning?
In the original paper it matched or beat full fine-tuning on three table-to-text datasets with GPT-2 and beat it in low-data and unseen-topic tests, but trailed it on XSUM summarization (ROUGE-L 35.05 vs 37.25 at 0.1% of parameters) (Li & Liang, Tables 1–3).
How many parameters does prefix tuning train?
About 0.1% in the paper's table-to-text runs: 250K parameters for E2E and WebNLG and 500K for DART, against 345M in GPT-2 Medium. The summarization runs also tested a 2% setting (footnote 9, Table 2).
What is the difference between prefix tuning and prompt tuning?
Prefix tuning adds trainable vectors at every Transformer layer; prompt tuning adds them only to the input embeddings. Lester et al. describe prompt tuning as a simplification of prefix tuning that needs no reparameterization (Lester et al., Sec. 4).
Does prefix tuning change the model's weights?
No. The language model's parameters are fixed and only the prefix matrix is trained, so one stored model can serve many tasks (Sec. 4.2).
Why does prefix tuning use an MLP?
Directly optimizing the prefix led to unstable training and slightly lower scores, so the authors generate it from a smaller matrix through an MLP during training, then drop the MLP (Sec. 4.3).
Can prefix tuning serve several tasks in one batch?
Yes. Because the shared model is unchanged, the paper says requests with different prefixes can be batched together, which it says adapter tuning cannot do (Sec. 8.2).
Does Hugging Face PEFT support prefix tuning?
Yes, through PrefixTuningConfig and get_peft_model. For encoder-decoder models PEFT applies the prefix to the decoder only (PEFT docs).
Related techniques
- P-Tuning and P-Tuning v2: learned prompt embeddings through a prompt encoder; v2 adds prompts at every layer
- Prompt tuning (soft prompts): trainable embeddings at the input layer only
- All 15 LLM fine-tuning techniques
Sources
- Xiang Lisa Li, Percy Liang. "Prefix-Tuning: Optimizing Continuous Prompts for Generation." arXiv:2101.00190, Jan. 1, 2021; ACL-IJCNLP 2021. arXiv, ACL Anthology. Read Dec. 4, 2026.
- Hugging Face PEFT v0.22.0, Prefix tuning reference and PeftModel reference. Read Dec. 4, 2026.
- GPT-2 Medium config.json on Hugging Face. Read Dec. 4, 2026.
- Authors' code: XiangLi1999/PrefixTuning. Opened Dec. 4, 2026.
- Brian Lester, Rami Al-Rfou, Noah Constant. "The Power of Scale for Parameter-Efficient Prompt Tuning." arXiv:2104.08691, Apr. 18, 2021 (v2 Sep. 2, 2021). arXiv. Read Dec. 4, 2026.
Series: ← Prev: BitFit · All 15 techniques · Next: P-Tuning →
- #llm-fine-tuning
- #prefix-tuning
- #peft
- #soft-prompts
- #gpt-2



