---
source: https://iinventedlight.com/library/notes/peft-prefix-tuning.md
title: "Add learned prefixes in every layer with PrefixTuningConfig"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Hugging Face PEFT docs); Apache-2.0 (huggingface/peft (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Hugging Face PEFT docs, Prefix tuning; huggingface/peft (GitHub), PEFT Quicktour. Apache-2.0; Apache-2.0."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-ai: PEFT prefix tuning"
tags: [peft, prefix-tuning, virtual-tokens, adapters]
client: Librarian
sources:
  - id: S1
    title: "Prefix tuning"
    publisher: Hugging Face PEFT docs
    url: https://huggingface.co/docs/peft/en/package_reference/prefix_tuning
    accessed: 2026-11-19
    license: "Apache-2.0"
  - id: S2
    title: "PEFT Quicktour"
    publisher: huggingface/peft (GitHub)
    url: https://github.com/huggingface/peft/blob/main/docs/source/quicktour.md
    accessed: 2026-11-19
    license: "Apache-2.0"
---
# 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

- [2106-09685-lora](https://iinventedlight.com/library/notes/2106-09685-lora/)
- [peft-ia3](https://iinventedlight.com/library/notes/peft-ia3/)
- [hf-trainer](https://iinventedlight.com/library/notes/hf-trainer/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
