Part 6 of 15 in LLM fine-tuning techniques
P-Tuning replaces hand-written prompt words with trainable continuous embeddings, produced by a small LSTM or MLP "prompt encoder" and mixed in with ordinary prompt tokens. Researchers at Tsinghua University and MIT introduced it in March 2021 in "GPT Understands, Too" and followed it in October 2021 with P-Tuning v2, which puts trainable prompts at every layer and reported fine-tuning-level results with 0.1% to 3% of parameters tuned (arXiv 2103.10385; arXiv 2110.07602).
P-Tuning sits between the other two prompt-based methods in this series: prefix tuning (trainable vectors at every layer) and prompt tuning (trainable vectors at the input only).
What is P-Tuning?
P-Tuning is a method that adds trainable continuous prompt embeddings to a language model's input, concatenated with a discrete text prompt, and learns them by backpropagation. Its authors proposed it because hand-written prompts were unstable: on the LAMA knowledge-probing benchmark, changing a single word in a prompt cut accuracy by about 20 points (Liu et al., Sec. 1 and 2.1).
The paper's Table 1 shows the problem on LAMA relation P17 (country) with a frozen BERT-base:
| Manual prompt | P@1 without P-Tuning | P@1 with P-Tuning |
|---|---|---|
| [X] is located in [Y]. (original) | 31.3 | 57.8 |
| [X] is located in which country or state? [Y]. | 19.8 | 57.8 |
| [X] is located in which country? [Y]. | 31.4 | 58.1 |
| [X] is located in which country? In [Y]. | 51.1 | 58.1 |
Adding "In" moved the manual prompt from 31.4 to 51.1. With P-Tuning, all four prompts landed between 57.8 and 58.1.
How does P-Tuning work?
P-Tuning builds a prompt template from both kinds of tokens, for example [P0] [P1] "The capital of" x "is" [MASK], where each [Pi] is a trainable embedding. A prompt encoder, an LSTM or MLP, maps those embeddings to the vectors fed into the model, and the task loss updates them by backpropagation. The language model can be frozen or tuned (Liu et al., Sec. 2.2–2.3).

Step by step:
- Write a template. Start from a discrete prompt such as "The capital of [INPUT] is [LABEL]." (Sec. 2.2).
- Insert continuous tokens. Add trainable [Pi] tokens at chosen positions. On LAMA the authors used 3 continuous tokens before the subject, 3 after the original prompt and, for BERT-style models, 3 after the object (Sec. 3.1.1).
- Encode them. A function f maps each [Pi] to a vector hi. The paper uses a lightweight network for f, testing an LSTM, an MLP and the identity (Sec. 2.3).
- Feed the mixed sequence. The model receives h0…hi together with the normal word embeddings e(x) (Sec. 2.2).
- Optimize. The task loss updates the prompt embeddings and encoder. On LAMA the model was frozen; on SuperGLUE the model and prompts were optimized jointly (Sec. 3).
What changed in P-Tuning v2?
P-Tuning v2 moves the trainable prompts from the input layer into every layer, as prefix tokens. The authors call it "an optimized and adapted implementation" of deep prompt tuning, the approach of Li and Liang's prefix tuning, applied to language understanding. It raises the tuned share from 0.01% to 0.1%–3% (Liu et al. 2022, Sec. 1 and 3.2).
Other v2 choices, all from Sec. 3.3 of the v2 paper:
- Reparameterization is optional. An MLP helped on some datasets (RTE, CoNLL04) and gave "minimal or even negative effects" on others (BoolQ, CoNLL12).
- Prompt length depends on the task. Simple classification preferred fewer than 20 tokens; hard sequence labeling preferred about 100.
- A plain classification head. v2 drops the verbalizer (the LM head predicting label words) and uses a randomly initialized linear head on the tokens, as in BERT.
- Multi-task training is optional and can give a better starting point.
Worked example: how many parameters?
For P-Tuning at the input layer, the stored prompt is number of virtual tokens × embedding size. RoBERTa-large has a hidden size of 1,024 and 24 layers (model config).
| Setup on RoBERTa-large | Calculation | Prompt parameters | Share of ~355M |
|---|---|---|---|
| 20 input-layer tokens (P-Tuning style) | 20 × 1,024 | 20,480 | about 0.006% |
| 20 tokens at every layer, as key and value (deep prompts) | 20 × 24 × 2 × 1,024 | 983,040 | about 0.28% |
The deep-prompt row uses the shape of Hugging Face PEFT's prefix encoder, which outputs 2*layers*hidden values per virtual token (PEFT prefix tuning docs); it falls inside v2's reported 0.1%–3% range. The P-Tuning prompt encoder adds parameters during training. PEFT lists its MLP or LSTM head as present when inference_mode=False (PEFT P-tuning docs).
When was P-Tuning introduced, and by whom?
P-Tuning was introduced in "GPT Understands, Too" by Xiao Liu, Yanan Zheng, Zhengxiao Du, Ming Ding, Yujie Qian, Zhilin Yang and Jie Tang (Tsinghua University and MIT), posted to arXiv on March 18, 2021, and revised on Oct. 25, 2023 (arXiv 2103.10385). P-Tuning v2, by Xiao Liu, Kaixuan Ji, Yicheng Fu, Weng Lam Tam, Zhengxiao Du, Zhilin Yang and Jie Tang, followed on Oct. 14, 2021, and appeared at ACL 2022 (arXiv 2110.07602; ACL Anthology).
The two versions of the first paper read differently. The 2021 abstract said P-Tuning lets GPTs match or beat similar-sized BERTs on language-understanding tasks and that the best GPT recovered 64% (P@1) of world knowledge on LAMA (arXiv v1). The 2023 revision frames the contribution as stabilizing and improving prompts for both frozen and tuned models. The numbers on this page come from the current (2023) version unless stated.
Code: THUDM/P-tuning and THUDM/P-tuning-v2.
What results did the P-Tuning papers report?
On LAMA with frozen models, P-Tuning raised the best precision@1 from 43.3% to 50.6% on LAMA-34k and from 45.2% to 64.2% on LAMA-29k. With tuned models, it beat the PET method on SuperGLUE in full-data and few-shot settings. P-Tuning v2 matched fine-tuning from 330M to 10B parameters, including on sequence labeling (Liu et al., Sec. 3; v2, Sec. 4).
Knowledge probing, frozen models (P-Tuning paper, Table 3, LAMA-29k, P@1):
| Model | Manual prompt | P-Tuning |
|---|---|---|
| BERT-base (109M) | 31.7 | 52.3 |
| RoBERTa-large (355M) | 22.1 | 53.5 |
| GPT2-xl (1.5B) | 22.8 | 54.4 |
| MegatronLM (11B) | 23.1 | 64.2 |
SuperGLUE, tuned models (Table 4, dev set, 7-task average): BERT-large went from 72.5 with standard classification fine-tuning and 73.1 with PET to 74.6 with P-Tuning; GPT2-medium went from 63.1 and 74.9 to 75.6.
Few-shot SuperGLUE on ALBERT (Table 5, average of 4 data splits): P-Tuning 71.81, PET 70.74, Lester et al.'s prompt tuning 58.56. The paper also tested prompt encoders: LSTM and MLP both worked, while optimizing the raw embeddings ("EMB") was unstable and much worse on some tasks such as WiC and CB (Sec. 3.3.3).
Stability (Sec. 3.4, Table 6): across manual prompts on LAMA P17, the standard deviation fell from 10.1 to 0.46 with P-Tuning.
P-Tuning v2, all backbones frozen (v2 paper, Tables 2–3):
| Task, BERT-large (335M) | Fine-tuning | P-Tuning / Lester et al. | P-Tuning v2 |
|---|---|---|---|
| RTE (accuracy) | 70.4 | 53.5 | 78.3 |
| BoolQ (accuracy) | 77.7 | 67.2 | 75.8 |
| SQuAD 1.1 dev (EM / F1) | 84.2 / 91.1 | 1.0 / 8.5 | 77.8 / 86.0 |
At 10B parameters (GLM), the gap closed for all methods: BoolQ was 88.7 for fine-tuning and 88.8 for both prompt methods (Table 2). The v2 authors say they found prompts added to deeper layers matter more: on RTE, prompts in layers 17–24 alone came close to prompts in all layers (Sec. 4.3, Figure 3).
How is P-Tuning different from prefix tuning and prompt tuning?
P-Tuning (v1) learns prompt embeddings at the input, generated by an LSTM or MLP encoder and interleaved with real words. Prompt tuning prepends embeddings at the input with no encoder and a frozen model. Prefix tuning and P-Tuning v2 put prompts at every layer (v2 paper, Table 1; Lester et al., Sec. 4).
| P-Tuning (v1) | P-Tuning v2 | Prefix tuning | Prompt tuning | |
|---|---|---|---|---|
| Prompt location | Input, anywhere in the template | Every layer | Every layer | Input, prepended |
| Encoder / reparameterization | LSTM or MLP | Optional | MLP | None |
| Base model | Frozen (LAMA) or tuned (SuperGLUE) | Frozen | Frozen | Frozen |
| Output | Verbalizer via LM head | Linear classification head | Generated text | Generated text (T5) |
| Original tasks | Knowledge probing, NLU | NLU incl. sequence labeling | Generation | NLU as text-to-text |
Lester et al. note two practical differences from prompt tuning: to reach strong SuperGLUE results, P-Tuning "has to be used in conjunction with model tuning", and it adds "anchor" tokens to the input, while prompt tuning leaves inputs untouched (Sec. 4, footnote 10).
Weight-based methods such as LoRA and adapters work differently again: they change or add weights rather than inputs.
Which libraries support P-Tuning?
Hugging Face PEFT supports P-Tuning through PromptEncoderConfig. The PEFT docs (v0.22.0, read Dec. 4, 2026) describe the method as trainable prompt embeddings optimized by a prompt encoder, with tokens that can go anywhere in the input (PEFT P-tuning docs). PEFT has no class named after P-Tuning v2; the v2 authors' code is at THUDM/P-tuning-v2.
PEFT's PromptEncoderConfig accepts encoder_reparameterization_type (default MLP, or LSTM), encoder_hidden_size, encoder_num_layers (default 2) and encoder_dropout (default 0.0), per the same reference page.
How do you use P-Tuning in code?
This sets up P-Tuning with an LSTM prompt encoder on RoBERTa-large for a two-class task, using PEFT. Class and argument names are from the PEFT v0.22.0 reference (PromptEncoderConfig, PeftModel).
from transformers import AutoModelForSequenceClassification
from peft import PromptEncoderConfig, get_peft_model
model = AutoModelForSequenceClassification.from_pretrained(
"FacebookAI/roberta-large", num_labels=2
)
config = PromptEncoderConfig(
task_type="SEQ_CLS",
num_virtual_tokens=20,
encoder_reparameterization_type="LSTM", # the paper tests LSTM and MLP (Sec. 2.3); PEFT's default is MLP
encoder_hidden_size=128,
)
model = get_peft_model(model, config)
model.print_trainable_parameters()
# Train with a standard transformers Trainer or your own loop.
PEFT concatenates the virtual-token embeddings in front of the input embeddings (PEFT v0.22.0 source, peft_model.py). Its docs say P-Tuning tokens "can be inserted anywhere" in principle, and the paper's templates interleave them with words, so reproducing a specific template from the paper means using the authors' code.
Common pitfalls
- Skipping the encoder. Optimizing raw prompt embeddings ("EMB") "is unstable and can substantially under-perform" the LSTM or MLP encoder on some tasks (Liu et al., Sec. 3.3.3).
- Assuming more prompt tokens is better. In few-shot tests, "it is not that a larger number of prompt tokens would always be better"; the authors suggest picking the count by model selection (Sec. 3.3.3).
- Expecting frozen-model v1 results on SuperGLUE. v1's SuperGLUE numbers come from jointly tuning the model and prompts (Sec. 3). With frozen models below 10B parameters, P-Tuning and prompt tuning scored well below fine-tuning in the v2 paper (Table 2).
- Using input-only prompts for sequence labeling. On SQuAD 1.1, input-layer prompt methods scored 1.0 EM against 84.2 for fine-tuning on BERT-large; deep prompts (v2) scored 77.8 (v2, Table 3).
- Defaulting the reparameterization. The v2 authors found the MLP helps on some datasets and hurts on others (Sec. 3.3), so it is worth testing both.
FAQ
What is the difference between P-Tuning and P-Tuning v2?
P-Tuning adds trainable prompt embeddings at the input layer through an LSTM or MLP encoder. P-Tuning v2 adds trainable prompts at every layer, makes the encoder optional and uses a linear classification head, tuning 0.1%–3% of parameters (v2, Sec. 3).
Does P-Tuning freeze the language model?
It can do either. In the paper, models were frozen for LAMA knowledge probing and tuned jointly with the prompts on SuperGLUE (Liu et al., Sec. 3). P-Tuning v2 keeps the backbone frozen.
Is P-Tuning v2 as good as fine-tuning?
In its paper, P-Tuning v2 was comparable to fine-tuning across models from about 330M to 10B parameters on SuperGLUE, named-entity recognition, extractive QA and semantic role labeling, and beat it on RTE (78.3 vs 70.4 on BERT-large) (v2, Tables 2–3).
Why does P-Tuning use an LSTM?
The authors use a prompt encoder to model dependencies between prompt embeddings. In their ablation, LSTM and MLP encoders both worked, and direct embedding optimization was unstable (Sec. 2.3, 3.3.3).
Does "GPT Understands, Too" mean GPT beats BERT?
The 2021 version's abstract said P-Tuning lets GPTs reach comparable or better results than similar-sized BERTs on NLU tasks. The 2023 revision drops that framing and presents P-Tuning as a way to stabilize and improve prompts for any model (arXiv v1; current version).
Which Hugging Face class implements P-Tuning?
PromptEncoderConfig in PEFT, used with get_peft_model (PEFT docs).
Related techniques
- Prefix tuning: trainable prefixes at every layer, the deep-prompt design P-Tuning v2 builds on
- Prompt tuning (soft prompts): trainable input embeddings on a frozen model, no encoder
- All 15 LLM fine-tuning techniques
Sources
- Xiao Liu, Yanan Zheng, Zhengxiao Du, Ming Ding, Yujie Qian, Zhilin Yang, Jie Tang. "GPT Understands, Too." arXiv:2103.10385, Mar. 18, 2021 (v2 Oct. 25, 2023). arXiv, v1. Read Dec. 4, 2026.
- Hugging Face PEFT v0.22.0, P-tuning reference, Prefix tuning reference and PeftModel reference. Read Dec. 4, 2026.
- RoBERTa-large config.json. Read Dec. 4, 2026.
- Xiao Liu, Kaixuan Ji, Yicheng Fu, Weng Lam Tam, Zhengxiao Du, Zhilin Yang, Jie Tang. "P-Tuning v2: Prompt Tuning Can Be Comparable to Fine-tuning Universally Across Scales and Tasks." arXiv:2110.07602, Oct. 14, 2021 (v3 Mar. 20, 2022); ACL 2022. arXiv, ACL Anthology. Read Dec. 4, 2026.
- Brian Lester, Rami Al-Rfou, Noah Constant. "The Power of Scale for Parameter-Efficient Prompt Tuning." arXiv:2104.08691, 2021. arXiv. Read Dec. 4, 2026.
- Code: THUDM/P-tuning, THUDM/P-tuning-v2. Opened Dec. 4, 2026.
Series: ← Prev: Prefix Tuning · All 15 techniques · Next: Prompt Tuning →
- #llm-fine-tuning
- #p-tuning
- #p-tuning-v2
- #peft
- #soft-prompts



