Papa Programmer
Lesson 22 of 36

Why train fewer parameters

9 min read

Optional

The cost of updating every weight

Full fine-tuning updates every weight, as Fitting training into memory showed. Each trainable weight needs a gradient and optimiser state next to it, so a 7 billion weight model can need over 100 GB of GPU memory before any activations are counted. A single consumer card does not have that, and renting a larger one costs more for every experiment.

Storage is the second cost. Every fully tuned model is a complete copy of the weights. If you serve five tasks, five copies of a 14 GB file take 70 GB, and each one has to be loaded, versioned and kept in step with its tokenizer.

Freeze the base, train an adapter

The idea is to freeze the base model, which means the optimiser never changes its weights, and add a small set of new weights that does change. The forward pass uses both. Only the new weights need gradients and optimiser state. The frozen weights are still loaded, still used in every forward pass, and still take memory, but they carry no extra training state.

The new weights are called an adapter. They live in their own small file, and you attach them to the matching base model at load time.

Count what you save

The program below counts the attention weights of a 7 billion parameter style model with 32 layers and a width of 4,096. It compares training all four attention projections in full with training a LoRA adapter of rank 8 on the same four. The next Lesson explains what rank means. For now it is the size of the adapter.

python
hidden, layers, projections, rank = 4096, 32, 4, 8
bytes_per_value = 2

full = layers * projections * hidden * hidden
adapter = layers * projections * 2 * hidden * rank

print(f"attention weights, full: {full:,} ({full * bytes_per_value / 1e9:.2f} GB at 16-bit)")
print(f"adapter, rank {rank}: {adapter:,} ({adapter * bytes_per_value / 1e6:.1f} MB)")
print(f"trainable share: {adapter / full:.2%}")

tasks, base_gb = 5, 14
adapter_gb = adapter * bytes_per_value / 1e9
print(f"{tasks} tasks as full copies: {tasks * base_gb} GB")
print(f"{tasks} tasks as one base plus adapters: {base_gb + tasks * adapter_gb:.2f} GB")

It prints:

text
attention weights, full: 2,147,483,648 (4.29 GB at 16-bit)
adapter, rank 8: 8,388,608 (16.8 MB)
trainable share: 0.39%
5 tasks as full copies: 70 GB
5 tasks as one base plus adapters: 14.08 GB

The adapter has about 8.4 million numbers, 0.39% of the 2.1 billion attention weights, and its file is about 17 MB. Five tasks that would take 70 GB as full copies fit in about 14 GB of base model plus five small adapter files. The saving in training memory follows from the same count: gradients and optimiser state are needed only for the adapter's weights, not for the whole model.

These numbers cover attention weights only, and a real model also has feed-forward layers and embeddings. The shape of the result holds anyway: the trainable part is a small fraction of the whole.

What else changes

  • Sharing. Many adapters can sit on one base model, so you can keep one adapter per task, customer or language.
  • Experiments. A small adapter is quick to save, copy and compare, which makes it cheap to try several settings.
  • Reset. Detach the adapter and you have the original behaviour back, which helps with Catastrophic forgetting because the base weights never moved.
  • Dependencies. An adapter only works with the base model it was trained on. Keep the base model identity, tokenizer and adapter settings together as one record.

What you give up

An adapter limits how the model can change. For many tasks that is enough, and it often matches full fine-tuning on narrow, well-defined jobs. For a large shift, such as teaching a model a new language or a very different domain, a small adapter can fall short.

Methods also differ in speed. The forward and backward passes still run through the full model, so training is not always much faster. The main savings are in memory and storage, and the speed you get depends on the setup.

So the rule is the usual one. Train an adapter first, evaluate it on held-out cases with the same measures as the base model, and move to more trainable weights only if the results show you need them. Evaluating a fine-tuned model covers that comparison.

The main methods

The rest of this module covers them in turn.

What it trainsWhy use it
LoRATwo small matrices beside chosen layersLoRA keeps the base frozen and trains the adapter
QLoRAA LoRA adapter on a 4-bit frozen baseFits large models on one GPU
Prompt, prefix and adapter layersLearned inputs or small modulesFewest trained numbers
Methods in this module

The Lessons How LoRA works and LoRA in code come first, then Quantisation and QLoRA and Other efficient methods. For the library that implements these methods, see the Hugging Face PEFT overview.

Freeze the baseThe optimiser never changes the base weights, so they need no training state.
One base, many adaptersEach task adds a small file instead of a full copy of the model.
Check the limitAn adapter limits change, so compare it with a baseline before you trust it.

Your reading position is saved

0%

0 of 36 complete

Contents

Learning tools