skip to content

What is a learned soft prompt, and how is it trained while the language model's weights stay frozen?

level: middleimportance: should knowfreq 36%

answer

  1. numbers in place of words
  2. no token ID behind them
  3. joined to the embedded input
  4. gradient passes through, updates land elsewhere
  5. virtual tokens times embedding width

basics

~20 s

A learned soft prompt is a small matrix of trainable vectors, called virtual tokens, joined to a frozen model's embedded input. Backpropagation on labelled examples updates only those vectors, so the result is numbers, not readable text.

solid answer

~50 s

A soft prompt is `k` trainable vectors, each as wide as the model's token embeddings. They sit where prompt tokens' embeddings would sit, but they have no token ID and no vocabulary entry, which is why they are called virtual tokens. Training embeds each labelled input, joins the vectors to it, runs the model as usual and computes the task loss. Backpropagation carries the gradient through the frozen layers, and the optimizer updates only the vectors. The artifact is `k x width` numbers tied to the model they were trained on, so one frozen model can serve many tasks. Lester et al. (2021) call this input-only form prompt tuning: 5 virtual tokens at width 4,096 cost 20,480 parameters against about 11 billion. What you give up is readability and portability, because the output is a matrix, not an instruction.

code

pseudocode · 10 lines
pseudocode
M = load_model(frozen = true)          # every weight fixed
P = new_matrix(rows = k, cols = M.embedding_width)   # the only trainable values

for each labelled batch (X, Y):
    inputs = concat(P, M.embed(X))     # k + n rows per example
    loss   = M.task_loss(inputs, Y)
    grads  = backprop(loss, wrt = P)   # flows through the frozen layers
    P      = optimizer_step(P, grads)  # no update reaches M

save(P, model_id = M.id)

go deeper

for a junior

Recall the plain definition: trainable vectors joined to the input, frozen model, no words involved. Being able to say what a virtual token is will carry most of a screening answer.

for a middle

Walk through one training step: embed, concatenate, loss, backpropagate, update only the vectors. Then work the parameter count out loud, tokens times embedding width, with stated sizes.

for a senior

Show you know the cost beyond parameters: the vectors need a labelled set, a model you can run, and they cannot be read in review. Quote any published gain with its model size and benchmark.

for a principal

Place it among per-task artifacts: what sharing one frozen model buys a platform, and what it costs when nobody can read the artifact and each model upgrade means training again.

## What a soft prompt is Prompting means adding extra input for a model to condition on while its weights stay fixed. With a **written prompt**, that input is a sequence of real tokens, and each token's vector comes from the model's frozen embedding table. A **learned soft prompt** drops that restriction: the vectors are free parameters of their own, fitted to labelled examples. Each vector is a **virtual token**. It occupies a position the model reads exactly as it reads a token's embedding, but it has no token ID, no vocabulary entry and no text behind it. Li and Liang (2021) use the phrase for their prefixes; Lester et al. (2021) call the input-only form **prompt tuning**, the simplest member of the family and the one described here. Do not confuse these learned vectors with a cached prompt prefix, which is ordinary text whose computation a server reuses. ## How training runs 1. Load a pretrained model and freeze every weight. 2. Create a matrix `P` with `k` rows, each as wide as the token embeddings. 3. Embed the input tokens `X`, giving `n` rows of the same width. 4. Concatenate them. `[P; X]` has `k + n` rows and flows through the model as normal. 5. Compute the task loss on the expected output. 6. Backpropagate, and apply the update to `P` alone. 7. Select the best checkpoint on a dev set and save `P`. Three points catch candidates out: - **Frozen does not mean out of the path.** The loss depends on `P` through every layer above it, so the gradient has to travel through the frozen layers. Their weights simply receive no update. - **It is task-level.** The same `P` is joined to every input of the task. It is not generated per request. - **Nobody writes it.** The signal comes from the whole labelled set, whereas a few-shot written prompt carries only the examples that fit in the input. ## What you end up with The cost is `k x width`, and the figures are easy to work: - Lester et al. (2021): 5 virtual tokens at width 4,096 is **20,480** parameters beside a frozen model of about 11 billion. - This team's illustrative case: 20 virtual tokens at width 2,048 is **40,960** parameters per task. A dozen tasks is 491,520, under 2 MB at 4 bytes per parameter. - Each request still spends `k` positions of the model's input on the vectors, and they join the attention computation like any other position. The serving benefit is that one frozen model stays shared. Lester et al. show mixed-task batches on one model, each row carrying its own task's matrix. ## How it sits beside its neighbours | | Written prompt | Learned soft prompt | Adapter | |---|---|---|---| | What is produced | Text | A matrix of vectors | Extra trainable layers or weights | | Fitted to data by | A person or a search | Backpropagation | Backpropagation | | Human-readable | Yes | No | No | | Model's function | Unchanged | Unchanged, new input only | Changed | Lester et al. draw the last row themselves for bottleneck adapters: an adapter modifies the function that acts on the input representation, while a soft prompt leaves the function fixed and adds new input representations. How a low-rank adapter is sized is a separate subject; here it is enough that it trains weights inside the model. ## What the papers report, and how far it goes - On the SuperGLUE benchmark, Lester et al. found prompt tuning more competitive as the model grew, matching multi-task model tuning at about 11 billion parameters. Smaller models lagged. - On that benchmark, they also put prompt tuning well ahead of few-shot written prompts on a far larger model of that date. - The same paper reports prompt tuning ahead of model tuning on most of its out-of-domain question-answering sets, with a gap of 12.5 F1 points on one, and behind on others. - Later work finds input-only tuning weak on mid-sized models and token-labelling tasks (the P-tuning v2 paper, Liu et al., 2021). These are 2021 results on the models and tasks of that date. Treat them as findings to re-measure on your own task, not as a law about soft prompts, since a model released later may behave differently.

  • If every layer is frozen, how does the gradient reach the vectors?
    Freezing means the optimizer does not update those weights; it does not take them out of the computation. The loss depends on the vectors through every layer that reads them, so backpropagation computes the gradient by passing through the frozen layers. The weights stay as they were, and only the vectors change.
  • Can a soft prompt be combined with written text in the same request?
    Yes. Liu et al. (2021) join continuous vectors with discrete prompt tokens in their original P-tuning work and report that the combination performed better in their experiments. The written part stays readable and editable. Editing it changes what the vectors were trained beside, though, so re-evaluate on held-out data after any change.

  • A soft prompt is a written prompt whose wording was optimized until it worked.
  • Freezing the model stops gradients from passing through its layers during training.
  • After training you can decode the vectors and read the prompt back as text.
  • A soft prompt is generated afresh for each input at inference.
  • Learned soft prompts match full fine-tuning at any model size.