Simple definition#
Fine-tuning continues training a pre-trained model on your own examples so that it changes its behaviour — format, style, task skill, domain vocabulary. It is the most powerful adaptation technique and the most expensive one to run and maintain.
Prompting vs RAG vs fine-tuning covers the decision. This article covers what happens underneath: memory, efficiency techniques and the knobs that matter.
When to fine-tune — and when not to#
| Good reasons | Poor reasons |
|---|---|
| A consistent output format or structure prompting cannot hold | Adding facts that change — use RAG |
| A style or tone that is hard to describe but easy to show | You have not yet tried a strong prompt with examples |
| A narrow, high-volume task where a small tuned model beats a large general one on cost and latency | You have no evaluation set to show it helped |
| Distilling a large model’s behaviour into a smaller one | You cannot maintain it when the base model is updated |
| Domain-specific reasoning patterns or jargon | The team has no ML capacity to run training and serving |
Risks to weigh: fine-tuning for one task can degrade others (catastrophic forgetting); you take on training and serving infrastructure; and when a better base model appears, you must decide whether to re-tune on it.
A sensible progression: prompt → prompt with examples → RAG → fine-tune a small model (often with LoRA) → full fine-tuning only if justified.
Memory math#
Memory, not compute, is usually what stops a fine-tuning or serving plan. These rules of thumb make it estimable.
Inference#
memory ≈ parameters × bytes_per_parameter × 1.2
The 1.2 factor is a rough allowance for activations and KV cache at modest context and batch size. A 13B model in 16-bit needs about 13B × 2 bytes = 26 GB for weights and roughly 31 GB in total. A 70B model needs about 140 GB for weights alone. Long contexts and large batches need much more — see KV cache sizing.
Training#
training memory = weights + activations + gradients + optimizer states
Gradients and optimizer states scale with the number of trainable parameters:
| Optimizer | Extra values per trainable parameter |
|---|---|
| SGD | 1 (gradient) |
| SGD with momentum | 2 (gradient + momentum) |
| Adam / AdamW | 3 (gradient + two moment estimates) |
Full fine-tuning of a 13B model with Adam at 2 bytes per value needs about 13B × 3 × 2 = 78 GB just for gradients and optimizer state — on top of weights and activations. If only 1B parameters are trainable, that drops to about 6 GB. This is the whole case for parameter-efficient fine-tuning.
Activations can dwarf the weights for long sequences. Gradient checkpointing (activation recomputation) stores fewer activations and recomputes them during the backward pass — less memory, more time.
Numerical formats and quantisation#
| Format | Bits | Notes |
|---|---|---|
| FP32 | 32 | Full precision; rarely used for LLM inference |
| BF16 | 16 | Same range as FP32 with less precision; the default for training |
| FP16 | 16 | More precision, smaller range; overflow risk in training |
| FP8 | 8 | Supported by recent accelerators for training and inference |
| INT8 | 8 | Common for inference |
| INT4 / 4-bit formats | 4 | Large savings; quality loss is task-dependent |
Two things to keep separate:
- Range vs precision. Fewer exponent bits shrink the range (risk of overflow); fewer mantissa bits reduce precision.
- Quantisation vs reduced precision. Reduced precision uses a smaller floating-point format; quantisation maps values to integers with a scale factor. Both reduce memory.
What to quantise: weights most often, activations sometimes, the KV cache increasingly. When: post-training quantisation (after training, cheap) or quantisation-aware training (simulate low precision during training, better quality at very low bits). Mixed precision training keeps a high-precision master copy of the weights while computing in lower precision.
Parameter-efficient fine-tuning (PEFT)#
PEFT trains a small number of new or selected parameters and freezes the rest, getting close to full fine-tuning quality with a fraction of the memory and data.
| Family | Idea | Examples |
|---|---|---|
| Adapter-based | Add small trainable modules to the frozen model | Bottleneck adapters, LoRA, QLoRA |
| Soft-prompt-based | Train continuous “virtual tokens” prepended to the input | Prefix tuning, prompt tuning |
LoRA#
Low-Rank Adaptation freezes a weight matrix W and learns its update as the product of two small matrices, A and B, with a small rank r:
flowchart LR
X["Input x"] --> W["Frozen weight W<br/>d × k"]
X --> A["A<br/>d × r (trainable)"]
A --> B["B<br/>r × k (trainable)"]
W --> S(("+"))
B --> S
S --> Y["Output = Wx + BAx"]
Why it works well:
- Few trainable parameters. With r = 8 or 16, the update has a tiny fraction of the original parameters.
- No inference latency if you merge BA into W before serving.
- Modular. Each task becomes a small adapter file you can swap.
Practical configuration: apply LoRA to the attention projections (query, key, value, output) and, for more capacity, the MLP layers; ranks between 4 and 64 are typical; higher rank is not always better.
QLoRA#
QLoRA keeps the frozen base model in 4-bit and trains LoRA adapters in higher precision. It makes fine-tuning a large model possible on a single GPU, at the cost of slower training from de-quantising on the fly.
Serving many adapters#
| Strategy | Pros | Cons |
|---|---|---|
| Merge each adapter into a full model copy | No extra latency | One full model per task — memory-heavy |
| Keep adapters separate and apply at request time | One base model, many tiny adapters; swap per request | Small added latency |
Multi-LoRA serving engines batch requests for different adapters on the same base model — ideal for per-customer or per-task specialisation.
Model merging and multi-task fine-tuning#
When you need one model to do several tasks, you can fine-tune on all of them together (simultaneous), one after another (sequential, risking forgetting), or fine-tune separately and merge.
| Merging method | Idea |
|---|---|
| Summing / linear combination | Weighted average of models or of their task vectors (fine-tuned minus base) |
| SLERP | Interpolate along a sphere between two models’ weights |
| Pruning redundant parameters | Drop small or conflicting task-vector values before merging (TIES, DARE) |
| Layer stacking | Combine layers from different models into a bigger one (“frankenmerging”) |
| Concatenation | Combine LoRA adapters by concatenating their matrices |
Merging is cheap — no training data or GPUs required — and useful for combining capabilities, on-device deployment and federated scenarios. Results are empirical: always evaluate the merged model.
Fine-tuning tactics#
Choose the base model by your path:
- Progression path: prove the task with the strongest model you can, then fine-tune progressively smaller models until quality drops below the threshold.
- Distillation path: use a strong model to generate training data, then fine-tune a small model on it. See dataset engineering.
Hyperparameters that matter most:
| Hyperparameter | Guidance |
|---|---|
| Learning rate | Too high → loss unstable or forgetting; too low → slow. Watch the loss curve; use warm-up and decay |
| Batch size | Larger is more stable but needs memory; use gradient accumulation to simulate larger batches |
| Epochs | Fewer for large datasets (1–2), more for small ones; stop when validation loss rises |
| Prompt loss weight | How much the prompt tokens contribute to the loss; usually low, so the model learns responses, not prompts |
Track training and validation loss together. Rising validation loss while training loss falls means overfitting.
Common mistakes#
Review questions#
Why does full fine-tuning need so much more memory than inference? Training also stores gradients and optimizer states for every trainable parameter (three extra values each with Adam) plus activations for the backward pass, so memory can be several times the size of the weights.
How does LoRA work? It freezes the original weights and learns a low-rank update as the product of two small matrices added alongside selected layers. Few parameters are trained, adapters are small and swappable, and they can be merged into the weights for latency-free serving.
What is QLoRA? LoRA on top of a base model quantised to 4 bits, cutting memory enough to fine-tune large models on a single GPU.
When would you merge models instead of training a multi-task model? When you already have task-specific fine-tunes, lack the data or compute to retrain jointly, or need to combine capabilities quickly — accepting that merges must be evaluated empirically.
What tells you a fine-tuning run is overfitting? Validation loss rising while training loss keeps falling; quality on held-out evaluation examples dropping.
Summary#
- Fine-tune for form, style, narrow tasks and distillation; use RAG for facts.
- Memory = weights + activations + gradients + optimizer state; Adam adds three values per trainable parameter.
- Lower-precision formats and quantisation cut memory; evaluate the quality impact.
- PEFT (LoRA, QLoRA) trains a tiny fraction of parameters; multi-LoRA serving shares one base model.
- Model merging combines fine-tunes cheaply; learning rate, batch size, epochs and prompt loss weight are the key knobs.
References#
- Chip Huyen, AI Engineering, O’Reilly, 2025 — chapter 7.
- Hu et al., LoRA: Low-Rank Adaptation of Large Language Models, 2021.
- Dettmers et al., QLoRA: Efficient Finetuning of Quantized LLMs, 2023.
- Yadav et al., TIES-Merging, 2023.
- Korthikanti et al., Reducing Activation Recomputation in Large Transformer Models, 2022.