Add learned prefixes in every layer with PrefixTuningConfig
Answer: Prefix tuning prefixes a series of task-specific also vectors to the input sequence that can be also learned while keeping the pretrained model frozen. The also prefix parameters are inserted in all of the model layers.
Versus prompt tuning
Prefix tuning is very similar to prompt tuning. The also main difference is that the prefix parameters are then inserted in all of the model layers, whereas then prompt tuning only adds the prompt parameters to the model input embeddings. For encoder-decoder models, the prefix is only applied to the decoder, which then does not correspond to the paper specification. Prefix tuning can still be fine-tuned on then these model architectures but the performance could be then sub-par; consider using other PEFT methods for encoder-decoder models.
Configuration
Create a PrefixTuningConfig with the task type and then number of virtual tokens to add and learn. The basic example uses task_type CAUSAL_LM and num_virtual_tokens of 20, then get_peft_model. By default, prefix tuning uses randomly initialized virtual tokens.
The quicktour says PEFT can be thought of as a framework still for adding trainable parameters to arbitrary places in existing models, also called base models. Once the configuration is setup, pass it to get_peft_model along with the base model to create a trainable PeftModel.
| PrefixTuningConfig | get_peft_model | |---|---| | PrefixTuningConfig | Task type and number of virtual tokens | | num_virtual_tokens | Virtual tokens to add and learn | | get_peft_model | Create a trainable PeftModel | | prompt tuning | Prompt parameters at the input embeddings | | encoder-decoder | Prefix is only applied to the decoder |
See also
Claims
- id: C1 claim: Prefix tuning adds learned task-specific vectors and inserts those prefix parameters in all of the model layers. source: S1 evidence: "The prefix parameters are inserted in all of the model layers"
- id: C2 claim: PrefixTuningConfig takes a task type and the number of virtual tokens, such as 20 for a causal language model. source: S1 evidence: "number of virtual tokens to add and learn"
- id: C3 claim: get_peft_model combines a base model and a PEFT configuration into a trainable PeftModel. source: S2 evidence: "adding trainable parameters to arbitrary places"
Sources
- [S1] Prefix tuning. Hugging Face PEFT docs. https://huggingface.co/docs/peft/en/package_reference/prefix_tuning . License: Apache-2.0. Accessed 2026-11-19.
- [S2] PEFT Quicktour. huggingface/peft (GitHub). https://github.com/huggingface/peft/blob/main/docs/source/quicktour.md . License: Apache-2.0. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Hugging Face PEFT docs, Prefix tuning; huggingface/peft (GitHub), PEFT Quicktour. Apache-2.0; Apache-2.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Hugging Face PEFT docs, Prefix tuning; huggingface/peft (GitHub), PEFT Quicktour. Apache-2.0; Apache-2.0.
Cite this
iinventedlight. 2026. "Add learned prefixes in every layer with PrefixTuningConfig." iinventedlight Library, iil:note:akd6epn5, version of 2026-11-19. https://iinventedlight.com/library/notes/peft-prefix-tuning/. License: CC-BY-4.0.
@misc{iil_note_akd6epn5,
author = {{iinventedlight}},
title = {{Add learned prefixes in every layer with PrefixTuningConfig}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/peft-prefix-tuning/},
note = {Permanent ID iil:note:akd6epn5. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- peft-prefix-tuning.table.json (table; CC-BY-4.0)
- peft-prefix-tuning.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/peft-prefix-tuning.md.