← All posts

Model development

Post 22 of 26

Prompt, retrieve or fine-tune: choosing how to adapt a model, and how LoRA works

Posts in this series · 26

The last three posts gave three ways to make a model do what an application needs: prompting, retrieval and tools, and, from the post-training track, fine-tuning. They are often presented as rungs of a ladder, but they fix different problems. This post shows how to diagnose which problem you have, and then explains LoRA, the method that made fine-tuning affordable.

Three problems, three tools

Look at your evaluation set’s failures and ask what is missing:

These combine. A support assistant may use retrieval for policy documents, a fine-tuned small model for the company’s response style, and a prompt for the current case.

Try it: choose a method. Which would you start with for each case?

  1. A legal assistant must answer questions about a client’s 400-page contract.
  2. A classifier must label support tickets into 30 categories; a large model with a long prompt of examples does it well but is too slow and expensive at 2 million tickets a day.
  3. A chatbot answers correctly but too casually for a bank.

Answer. (1) Retrieval: the knowledge is private, specific and long; retrieve relevant clauses and cite them. (2) Fine-tuning a small model on the large model’s labels (distillation, from the post-training data post): the behaviour exists, the cost is the problem. (3) Prompting first: tone is an instruction problem; fine-tune only if instructions and examples cannot make it consistent.

What fine-tuning needs

Fine-tuning replaces a long prompt with a change to the weights, which removes the prompt’s cost at every request. It needs:

LoRA: training a low-rank change

Low-Rank Adaptation (LoRA) freezes the pre-trained weights and learns only a small change to some of them (Hu et al., 2022, §4.1). For a weight matrix W0W_0 of shape d×kd \times k, the adapted layer computes

h=W0x+αrBAx,B∈Rd×r, A∈Rr×k, r≪min⁡(d,k).h = W_0 x + \frac{\alpha}{r} B A x, \qquad B \in \mathbb{R}^{d \times r},\ A \in \mathbb{R}^{r \times k},\ r \ll \min(d, k).

The saving is large. A 4,096×4,0964{,}096 \times 4{,}096 matrix has 16.8 million weights; a rank-8 adapter for it has 8×4,096+4,096×8=65,5368 \times 4{,}096 + 4{,}096 \times 8 = 65{,}536, 0.39%. Applying rank 8 to the query and value projections of all 32 layers of Llama 3 8B trains 3.4 million parameters, 0.04% of the model. Only those need gradients and optimizer state; the frozen 8B weights need just their BF16 copy, 16 GB.

Why should a low-rank change be enough? The paper’s hypothesis is that the change fine-tuning needs has low “intrinsic rank”, and its experiments support it for the tasks studied: on GPT-3 175B, LoRA matched or exceeded full fine-tuning on three benchmarks (Table 4), a rank as small as one sufficed when adapting the query and value matrices (§7.2, Table 6), and training memory fell from 1.2 TB to 350 GB (§4.2). The same paper notes that on GPT-2 Medium performance peaked at larger ranks, so the right rank is task- and model-dependent. A toy shows the mechanism: when the true change to a 16 × 16 layer has rank 2, a rank-2 LoRA trained on its outputs recovers it to a loss of 3×10−63 \times 10^{-6}.

No cost at inference. After training, αrBA\frac{\alpha}{r}BA can be added into W0W_0 once. The merged layer is an ordinary matrix of the original shape, so the model runs exactly as fast as before; on the toy, the merged layer’s outputs equal base plus adapter. Alternatively the adapters can stay separate: many small adapters (tens of megabytes each) can share one base model, swapped per customer or task.

QLoRA goes further by storing the frozen base model in 4 bits, about 4 GB for 8 billion parameters, while training LoRA adapters through it. Its authors fine-tuned a 65-billion-parameter model on a single 48 GB GPU while preserving 16-bit fine-tuning quality (Dettmers et al., 2023, abstract). Quantization is covered in the inference post.

LoRA’s limits follow from its design: a low-rank update may be too small for large changes in knowledge or capability, the choice of which matrices to adapt and the rank matter, and LoRA results are usually reported against full fine-tuning on specific tasks, not in general. The LoRA review looks at that evidence.

Everyday picture: instead of rewriting a textbook, you print a thin booklet of corrections that slots inside the cover. It is cheap to write and easy to swap, and you can bind it in permanently whenever you like.

What to remember

Sources

Further reading