← All notes

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

Sources

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

Permanent ID: iil:note:akd6epn5

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.},
}

Markdown

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.

All artifact files (JSON index)

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.