19.5 Parameter-efficient fine-tuning
This part of Choosing Training and Adaptation Methods develops the mechanisms below. Work the numerical examples or trace the code before checking the answer.
Concepts from other sections (13)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- LoRA: fine-tuning a frozen matrix through a trainable low-rank update initialized to zero. Read more: 19.7 The training step and validation
- QLoRA: LoRA on a base stored in 4-bit NF4 with double-quantized scales, about 0.516 bytes per parameter. Read more: 19.7 The training step and validation
- multi-LoRA serving: one base model serving many adapters in the same batch with a batched low-rank kernel. Read more: 20.8 From Training Run to Shared Model: Packaging, Publishing, and Use Cases
- Gradient check: a comparison of an analytic gradient against the numerical one that reports pass or fail and the element furthest past its allowance. Read more: 10.1 Partial derivatives, gradients, and gradcheck in Mathematics
- Chain rule: . Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP in Mathematics
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- BF16: a 16-bit float with float32’s sign and exponent and 7 mantissa bits; the top half of a rounded float32. Read more: 6.7 Safetensors for every dtype, atomic checkpoints, and the token stream
- activation checkpointing: discarding a segment’s activations in the forward pass and recomputing them during backward. Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- ZeRO / FSDP: sharding optimizer states, gradients, and (at stage 3) parameters across data-parallel ranks instead of replicating them. Read more: 19.7 The training step and validation
- Master copy: an fp32 copy of 16-bit weights kept so small updates are not rounded away. Read more: 11.1 Counting: parameters, FLOPs, KV bytes, and memory plans in Mathematics
- Backpropagation: reverse-mode automatic differentiation that computes all parameter gradients in one backward pass. Read more: Chapter 2: Deep Learning
- training step: one zero-grad, loss, finiteness check, backward, clip, and optimizer update on one batch. Read more: 6.6 Training loop and the autograd bigram
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
Learning objectives
- Explain the memory and computation saved by freezing weights.
Parameter-efficient fine-tuning
Section titled “Parameter-efficient fine-tuning”Every post-training method above can be run on a full copy of the weights, but the 16-bytes-per-parameter rule makes that expensive. Parameter-efficient fine-tuning freezes the base and trains a small number of new parameters instead. The key papers are LoRA, QLoRA, and DoRA.
LoRA freezes a weight matrix and learns a low-rank update:
is initialized Gaussian (or Kaiming) and is initialized to zero, so and training starts exactly at the base model. Each adapted matrix has trainable parameters instead of .
The hypothesis behind LoRA is that the fine-tuning update has low intrinsic rank. That makes it best for teaching format, style, or a narrow skill, and worst for injecting large amounts of new knowledge. The scaling keeps the update’s magnitude roughly constant as you change , so a learning rate transfers across ranks; rsLoRA uses instead (2312.03732) and does better at high rank.
Which modules to adapt matters more than the original paper suggested. Apply LoRA to all linear layers (q, k, v, o, gate, up, down), not just q and v. QLoRA showed this is needed to match full fine-tuning, and Thinking Machines’ LoRA Without Regret (free) found that LoRA on all layers matches full fine-tuning on small-to-medium datasets with about 10 times the full-FT learning rate, and is notably efficient for RL, where each episode carries few bits of information.
For zero-latency serving you can merge the adapter, , or keep adapters separate for multi-LoRA serving. On forgetting, LoRA Learns Less and Forgets Less (free) found lower target-domain gains than full fine-tuning on code and math continued pretraining, but better retention of base capabilities. lora_from_scratch.py implements LoRA on a frozen two-layer MLP with a merge check and a finite-difference gradient check.
What trains and what does not
Section titled “What trains and what does not”Frozen means “not updated”. It does not mean “skipped”. The backward pass still runs through the whole network, frozen layers included, because that is the only route for the error signal from the loss at the output to an adapter in an early block.
| Frozen | Adapter , | |
|---|---|---|
| The error signal passes through it | Yes | Yes |
| A gradient for its own values is computed | No | Yes |
| The optimizer updates it | No | Yes |
The base weights never improve. The effective weight does, because the layer behaves as . With at an adapted layer, the chain rule gives each matrix its direction:
The second formula contains , which starts at zero. On the first step every receives a zero gradient and only moves. starts to learn on the second step.
The loss is one scalar at the output, as in any training: cross-entropy for supervised fine-tuning, and reward-weighted log probabilities for RL. The adapter mechanics are the same in both.
Whether a layer trains depends on whether it has an adapter:
- A module without an adapter is not trained at all. LoRA is opt-in for each module. Untargeted projections, norms, embeddings, and the output head stay exactly as they are in the base model.
- A module with an adapter always trains. Every and receives a gradient on every step after the first.
Trained does not mean changed much. Where a layer matters for the task, grows into a real correction. Where it does not, the gradient is small, stays near zero, and the layer behaves almost like the base. AdaLoRA uses this unevenness: it scores the adapters and moves rank from the idle ones to the useful ones.
One pattern, one adapter for each layer
Section titled “One pattern, one adapter for each layer”No formal standard defines an adapter. The Hugging Face PEFT format is the de facto one: an adapter_config.json that holds the definition and an adapter_model.safetensors that holds only the and tensors. Downloads, loaders, and sharding covers the files and how a server loads them.
The config is a pattern, and the weight file is the definition for each layer. "target_modules": ["q_proj"] matches by name suffix, so it selects the q_proj in every block. A 32-block model gets 32 adapters from that one entry. Each has its own and , and nothing is shared between them:
layers.0.self_attn.q_proj.lora_A.weight shape (r, in_features)layers.0.self_attn.q_proj.lora_B.weight shape (out_features, r)layers.1.self_attn.q_proj.lora_A.weight...layers.31.self_attn.q_proj.lora_B.weightThe default is uniform: the same rank and the same on every match. The PEFT LoraConfig has finer controls:
| Field | Effect |
|---|---|
layers_to_transform | Adapt only the listed block indices |
target_modules as one string | Treat the string as a regular expression over the full module path |
rank_pattern | Override the rank for matching layers |
alpha_pattern | Override for matching layers |
exclude_modules | Remove matches from the selection |
An adapter fits only the exact base model it was trained on. The architecture and the base weights must both match, or the correction is meaningless.
QLoRA stores the base weights in 4 bits, keeps the adapters in BF16, and lets gradients flow through the dequantized base. Its 4-bit format, NF4, places 16 levels at the quantiles of , which is information-theoretically optimal for normally distributed weights. Blocks of 64 weights each carry an FP32 absmax scale, an overhead of 0.5 bits per parameter. Double quantization then quantizes those absmax constants to FP8 in blocks of 256, cutting the overhead to bits per parameter, for a total of about 4.127 bits, or 0.516 bytes, per parameter. Paged optimizers keep optimizer states in NVIDIA unified memory and page them to CPU during memory spikes from long sequences.
The compute dtype is still BF16, so QLoRA saves memory, not FLOPs, and runs slower than LoRA because of the dequantization. The paper’s headline result was a 65B fine-tune on one 48 GB GPU. The quantization details are in Quantization.
DoRA decomposes each weight into a magnitude vector and a column-normalized direction, . It trains directly and updates the direction with LoRA:
This brings learning dynamics closer to full fine-tuning at small rank, for a small compute overhead, and it merges just like LoRA.
Why LoRA fits on one GPU
Section titled “Why LoRA fits on one GPU”Now the payoff. The table below comes from memory_calc.py for Llama 3 shapes, rank 16 on all linear layers, sequence length 4096, micro-batch 1, with activation checkpointing:
| Model | Method | Trainable | States | + activations | Min H100-80GB |
|---|---|---|---|---|---|
| 8B | full FT (16 B/param) | 8.03B | 128.5 GB | 132 GB | 2 (FSDP), 8 in practice for throughput |
| 8B | LoRA (2 B/param base) | 41.9M (0.5%) | 16.7 GB | 20.5 GB | 1 (fits a 24 GB card) |
| 8B | QLoRA (0.516 B/param) | 41.9M | 4.8 GB | 8.6 GB | 1 |
| 70B | full FT | 70.6B | 1,129 GB | 1,138 GB | 18, i.e. 3 nodes |
| 70B | LoRA | 207M (0.3%) | 144.4 GB | 153 GB | 3 (shard the frozen base) |
| 70B | QLoRA | 207M | 39.7 GB | 48.3 GB | 1 |
The mechanism is simple. Frozen parameters need no gradient, no master copy, and no Adam moments, so 14 of their 16 bytes vanish, and QLoRA removes most of the remaining 2. Compute drops too: with no weight gradients for frozen matrices, the backward pass costs about instead of , so LoRA training costs about instead of . Training compute for a typical customer job is single-digit GPU-hours at most. The real cost is data and evaluation.
Serving many adapters
Section titled “Serving many adapters”Adapters are megabytes, so one base model can serve hundreds of fine-tunes. The server batches requests for different adapters together and applies each adapter’s with segmented gather matmul kernels (Punica, S-LoRA). vLLM (--enable-lora), SGLang, and LoRAX implement it, and it is the economic basis of per-customer fine-tunes at inference clouds: a LoRA deployment is billed like the base model.
The merged and unmerged paths are not numerically identical. computed in two parts does not round the same way as computed once. If RL rollouts come from one path and the trainer scores tokens on the other, that difference is one more source of trainer and inference engine drift. The serving side is covered in Serving and Load.
Further reading
Section titled “Further reading”All of these are free on arXiv.
| Topic | Paper | Read it for |
|---|---|---|
| The method | Hu et al., LoRA, 2021 | The low-rank update, the zero start, and which modules to adapt |
| Why low rank works | Aghajanyan et al., Intrinsic Dimensionality Explains the Effectiveness of Language Model Fine-Tuning, 2020 | The evidence that a fine-tune needs few free directions |
| Rank allocation | Zhang et al., AdaLoRA, 2023 | Moving rank from idle adapters to useful ones |
| Initialization | Meng et al., PiSSA, 2024 | Starting the adapter from the top singular directions of the weight |
| Learning rates | Hayou et al., LoRA+, 2024 | Separate learning rates for and |
| Fewer parameters | Kopiczko et al., VeRA, 2023 | Shared random matrices with small trained scaling vectors |
| Capacity and forgetting | Biderman et al., LoRA Learns Less and Forgets Less, 2024 | Measured gaps to full fine-tuning and retention of base ability |
| Multi-LoRA serving | Chen et al., Punica, 2023 | Batching requests for different adapters on one base model |
| Multi-LoRA serving cost | Sheng et al., S-LoRA, 2023 | Adapter memory management and the throughput cost of serving thousands of adapters |
| The engine underneath | Kwon et al., PagedAttention, 2023 | The cache and batching design that multi-adapter servers build on |
Summary
Section titled “Summary”Explain the memory and computation saved by freezing weights. State the assumptions before applying the result to a different workload.
Review questions
Section titled “Review questions”-
Does freezing the base remove its forward and backward compute?
Answer
The forward still runs and gradients must usually propagate through frozen layers to adapters. Only base weight-gradient computation and associated optimizer state disappear.
-
The base weights are frozen. How does the model’s behavior improve?
Answer
The layer computes with the effective weight . Backpropagation gives gradients for and , the optimizer moves them, and the product grows into a learned correction on top of an unchanged .
-
On the first training step, which adapter matrices change, and why?
Answer
Only . The gradient for is proportional to , and starts at zero, so every receives a zero gradient until has moved.
-
A config sets
"target_modules": ["q_proj", "v_proj"]on a 32-block model. How many adapters exist, and do they share weights?Answer
Sixty-four: one for each matching module in each block. Each adapter has its own and . The config is a pattern, and the keys in the weight file identify each layer’s tensors.
-
Are any layers left untrained in a LoRA fine-tune?
Answer
Yes. Every module without an adapter keeps its base values. Modules with an adapter all receive gradients, but those that matter little for the task stay close to a zero correction.
-
Why can a merged adapter and an unmerged adapter give different logits for the same input?
Answer
The two paths group the floating-point operations differently, so they round differently. The functions are equal in exact arithmetic and only close in finite precision.