Sub topic 2
Why full fine-tuning is expensive
This chapter assumes you can run a Hugging Face model for inference and know what a gradient and an optimizer are. It explains where the memory and storage go when you update every weight in a model. That cost is the reason LoRA exists.
What full fine-tuning updates
Full fine-tuning treats every parameter as trainable. A 7-billion-parameter model has 7 billion weights that receive gradients and get changed at each step. Training needs more than the weights themselves, so the memory bill is several times the size of the model.
Where the memory goes
During training with the Adam optimizer in mixed precision, each parameter needs four things:
- Weights in bf16: 2 bytes.
- Gradients in bf16: 2 bytes.
- A master copy of the weights in fp32: 4 bytes. Small updates would be lost to rounding in bf16, so the optimizer applies them to this copy.
- Adam state in fp32: 8 bytes. Adam keeps a running mean and a running variance of the gradients, 4 bytes each.
That adds up to about 16 bytes per parameter, which is 8 times the size of the bf16 weights alone.
You can check the numbers with a few lines of Python.
def gib(num_bytes: float) -> float:
return num_bytes / 2**30
params = 7e9 # a 7B-parameter model
bytes_per_param = {
"weights (bf16)": 2,
"gradients (bf16)": 2,
"fp32 master weights": 4,
"Adam state (fp32)": 8,
}
total = 0.0
for name, b in bytes_per_param.items():
size = gib(params * b)
total += size
print(f"{name:22s} {size:6.1f} GiB")
print(f"{'total (no activations)':22s} {total:6.1f} GiB")The total is about 104 GiB, and the weights are only 13 GiB of it. An 80 GB GPU cannot hold this training state for a 7B model, even before activations are counted.
Activations and other overhead
The figures above leave out activations. These are the intermediate outputs of each layer, which the backward pass needs to compute gradients. Their size grows with batch size, sequence length and number of layers. Gradient checkpointing reduces them by recomputing activations during the backward pass, at the price of extra compute. Memory fragmentation and framework buffers add a further margin.
Storage and deployment cost
Every fully fine-tuned model is a complete new copy of the weights. A 7B model in bf16 is about 14 GB. If you fine-tune it for five tasks or five customers, you store and ship about 70 GB, and you must load each copy separately to serve it. Training checkpoints are larger still, because they usually include optimizer state.
Other costs
- Hardware. You need either one very large GPU or several GPUs with sharded training, such as FSDP or DeepSpeed ZeRO. Sharding spreads the 16 bytes per parameter across devices but adds communication overhead and setup complexity.
- Forgetting. Updating every weight lets the model drift far from its original behavior, so it can lose general abilities while learning a narrow task.
- Iteration speed. Moving gigabytes of weights and optimizer state around makes each experiment slow and expensive.
Summary of the cost
| Item | Full fine-tuning of a 7B model |
|---|---|
| Trainable parameters | About 7 billion |
| Training state (excluding activations) | About 104 GiB |
| Saved artifact per task | About 14 GB |
| Typical hardware | Multi-GPU or a very large single GPU |
The next chapter shows how a low-rank update shrinks the trainable part from billions of parameters to millions.
Exercise
A 3B-parameter model is trained with Adam in mixed precision at 16 bytes per parameter. Roughly how many GB does the training state need, excluding activations? Which part of that would shrink if you trained only 1% of the parameters?
Show answer
3e9 × 16 bytes = 48 GB. The gradients, fp32 master weights and Adam state (14 of the 16 bytes per parameter) would shrink to about 1% of their size, since they exist only for trainable parameters. The 2-byte bf16 weights (6 GB) stay the same, so the total falls to roughly 6.7 GB plus activations.