Papa Programmer
Lesson 26 of 36

Other efficient methods

9 min read

Optional

One base model, five places to add weights

The methods below share the same recipe: freeze the base, add something small, train only that. They differ in where the small part enters the model. The PEFT library implements all of them behind one interface, so switching is mostly a change of configuration.

Prompt tuning. Learn a short sequence of new input vectors, called virtual tokens, and put them in front of the real input. They are not text and have no word equivalent. The base model reads them as if they were extra context.

Prefix tuning. Instead of changing only the input, learn extra key and value vectors for the attention in every layer. The model attends to them as a prefix. It reaches deeper than prompt tuning, so it has more freedom and more parameters.

Adapter layers. Insert a small trainable module, often a down-projection, a non-linearity and an up-projection, into each block. The original adapter method added these in sequence with the existing layers, which can add a little inference time.

LoRA. Add a low-rank update beside chosen weight matrices, as How LoRA works shows. It can be merged into the weights afterwards, so it need not add inference time.

DoRA. A LoRA variant. It splits each weight into a size and a direction, applies the low-rank update to the direction and trains the size as a separate vector. In published results it can improve on LoRA at the same rank, at a cost in training time and with extra setup rules.

Count the parameters

The program below counts trainable parameters for each method on a model with 32 layers and a width of 4,096, a size typical of a 7 billion parameter model. The settings are common starting values: 20 virtual tokens, a bottleneck of 64, and rank 8 on the query and value projections. The counts are approximate, since the real totals depend on the model and library.

python
layers, width = 32, 4096
total = 7_000_000_000

budgets = {
	"prompt tuning (20 tokens)": 20 * width,
	"prefix tuning (20 tokens)": 2 * layers * 20 * width,
	"adapter layers (bottleneck 64)": layers * 2 * width * 64,
	"LoRA (rank 8, q and v)": layers * 2 * 2 * width * 8,
	"DoRA (rank 8, q and v)": layers * 2 * (2 * width * 8 + width),
}

for name, count in budgets.items():
	print(f"{name:32} {count:>11,}  {count / total:.4%} of the model")

It prints:

text
prompt tuning (20 tokens)             81,920  0.0012% of the model
prefix tuning (20 tokens)          5,242,880  0.0749% of the model
adapter layers (bottleneck 64)    16,777,216  0.2397% of the model
LoRA (rank 8, q and v)             4,194,304  0.0599% of the model
DoRA (rank 8, q and v)             4,456,448  0.0637% of the model

Prompt tuning trains the fewest numbers by a wide margin, about 82 thousand. Prefix tuning trains about 5 million, because it adds vectors in every layer. LoRA at rank 8 on two projections trains about 4 million, and DoRA adds only a magnitude vector for each adapted matrix, so it is close to LoRA. The adapter layers train the most here, about 17 million, since each one holds a projection down to the bottleneck and another back up to the full width.

More parameters do not guarantee better quality. They do give the method more room to learn the task, and more room to memorise a small dataset, as Overfitting and underfitting explains.

Which to try

LoRA is the usual first choice. It has broad library support, merges for serving, and has a large body of recipes. The others have narrower roles.

  • Prompt and prefix tuning suit setups where you want a tiny file per task on a large frozen model, and where the task is simple. They tend to work best on larger models and can be less stable to train, so test on a small run.
  • Adapter layers suit cases where a library or model already ships with them.
  • DoRA is worth a trial when LoRA is close but not enough, and when your stack supports it. Check the merging rules for your setup.

Whichever you choose, the process is the same: pick the method your model and hardware support, train with a validation set, and compare quality, memory, training time and serving needs with a baseline. Hold one thing fixed in each comparison, such as data or steps, or the results do not say much. Evaluating a fine-tuned model covers the comparison.

Where the weights goTrainable sizeMerges into the model
Prompt tuningVirtual tokens at the inputSmallestNo
Prefix tuningKey and value vectors in every layerSmallNo
Adapter layersSmall modules inside each blockLargest hereNot usually
LoRALow-rank update beside weight matricesSmallYes, for supported models
DoRALow-rank direction plus a magnitude vectorSmallYes, with rules
Efficient methods side by side

For the current list of supported methods and their options, see the PEFT documentation.

Same recipe, different placeEvery method freezes the base and trains a small part that sits somewhere new.
Count, then compareTrainable size varies by orders of magnitude between methods.
Start with LoRAIt has the widest support, and it can merge into the model for serving.

Your reading position is saved

0%

0 of 36 complete

Contents

Learning tools