Language models, retrieval, prompting, and language adaptation · Instruction, preference, and parameter-efficient adaptation · LNG069 · Training method
Prefix tuning
Adapts a frozen language model by optimizing continuous prefix activations that the model can attend to. It is useful when storing one full fine-tuned model per task is impractical.
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Adapts a frozen language model by optimizing continuous prefix activations that the model can attend to. It is useful when storing one full fine-tuned model per task is impractical.
Question / objective
Can learned attention prefixes adapt a frozen network without changing its original matrices?
Architecture / computational structure
Trainable prefix key/value representations, sometimes generated by a small network, are inserted into attention memory. Prefix parameters are learned; length, placement, and reparameterization are hyperparameters.
Assumptions
Short prefixes can limit adaptation capacity, while long prefixes consume attention/cache resources.
Formula / computational definition
Learn prefix parameters P while keeping theta fixed and minimize -log p_theta(y|x,P). Prefixes can provide trainable key/value activations across attention layers; this is more than prepending ordinary discrete text tokens.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Prefix length, layer placement, reparameterization network, initialization, learning rate, task objective, and optional trainable heads.
Fitting, fine-tuning, and adaptation
Optimize task cross-entropy or another justified task objective with the base frozen. Tune prefix length and learning rate on held-out examples, and specify whether heads are also trainable. Compare multiple seeds and test transfer/retention; moving prefixes across incompatible bases is not valid adaptation.
How to use and calibrate
Compare task quality, parameter count, memory, prompt sensitivity, and capability retention under equal training-data budgets. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.
Example application
Maintain several generation tasks through compact task-specific attention prefixes.
Interpretation / pitfalls
Watch out
Prefix representations are architecture-specific and may not transfer across base-model versions. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.
Python and R code examples
Full implementation route: PEFT documents prefix-tuning configurations for supported models. R calls the Python implementation via reticulate. The native example concatenates prefix keys/values to an attention memory to show the computational change; optimization and layer-wise prefix generation require an autodiff model.
Example scope: Prefix key/value memory concatenation for one attention layer; no optimization.
Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.
Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Prefix key/value memory concatenation for one attention layer; no optimization.
Shared setup for this chapter
import math
def softmax(xs):
m = max(xs)
es = [math.exp(x-m) for x in xs]
return [e/sum(es) for e in es]
def dot(a, b):
return sum(x*y for x, y in zip(a, b))
def matvec(matrix, vector):
return [dot(row, vector) for row in matrix]
def meanvec(rows):
return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]
def sigmoid(x):
return 1/(1+math.exp(-x))
def nll(probabilities):
return -sum(math.log(p) for p in probabilities)
prefix_keys = [[0.2,0.3],[0.4,-0.1]]; token_keys = [[1,0],[0,1]]
keys = prefix_keys+token_keys
query = [0.6,0.8]
print(softmax([dot(query,k)/math.sqrt(2) for k in keys]))
Component demo, not a trained model — Prefix key/value memory concatenation for one attention layer; no optimization.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
prefix_keys <- rbind(c(0.2,0.3),c(0.4,-0.1)); token_keys <- diag(2)
keys <- rbind(prefix_keys,token_keys); query <- c(0.6,0.8)
print(softmax(drop(keys %*% query)/sqrt(2)))
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG069.