Foundations to Frontier
Tests
F. NLP and Large Language Models › F6 Fine-Tuning and Adaptation

Parameter-Efficient Fine-Tuning: Adapters, Prefix Tuning, LoRA, QLoRA and DoRA

Parameter-efficient fine-tuning (PEFT) adapts a pretrained model by training a small number of new parameters while the original weights stay frozen. Adapters insert small bottleneck layers; prefix and prompt tuning learn virtual tokens. LoRA adds a trainable low-rank update B·A to chosen weight matrices, which can be merged afterwards for zero inference cost. QLoRA keeps the frozen base in 4-bit precision so a 65B model can be fine-tuned on one GPU. DoRA separates each weight's magnitude and direction. PEFT cuts training memory, makes per-task checkpoints tiny, and tends to forget less. This page covers each method, the choice of rank and targets, and trade-offs. In the exercises you implement the LoRA forward pass, merging and parameter counts. You then adapt the pretrained one-block Transformer to a new task from 30 examples. Full fine-tuning (9472 parameters) reached loss 0.80. LoRA reached 1.80 at rank 1 (490 parameters), 0.82 at rank 2 (980) and 0.61 at rank 8 (3920). Forgetting of the original task grew with rank (1.6, 2.9, 5.2, against 5.7 for full fine-tuning). LoRA on a 4-bit quantised base (QLoRA) matched rank-2 LoRA (0.80).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Fine-tuning with a tiny add-on

Instead of changing all of a huge model, parameter-efficient methods bolt on a small add-on and train only that. LoRA, the most popular, adds a slim correction next to some weight matrices; it can have less than 1% of the model’s parameters, yet often works about as well as changing everything. QLoRA goes further by storing the big frozen model in 4-bit numbers to save memory. The demo shows how few bits a weight really needs.

Transfer and efficiency lab
4-bit uniform quantisation: 16 levels, step 0.1333, measured MSE 1.47e-3 vs Δ²/12 = 1.48e-3. Each extra bit cuts the error by 4×.
Why engineers care

PEFT makes fine-tuning feasible on a single workstation GPU and lets one base model serve many teams, each with a tiny adapter: one for maintenance, one for flight operations, one for procurement.

📝 Quick check (Level 0)
0/3 answered
  1. 1. PEFT trains…
  2. 2. LoRA adds…
  3. 3. QLoRA keeps the frozen base model in…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • LoRA: for a frozen weight W ∈ ℝ^(d_in×d_out), learn A ∈ ℝ^(d_in×r) and B ∈ ℝ^(r×d_out) (B starts at 0), and use W + (α/r)AB. Parameters r(d_in + d_out): 0.78% of a 4096 × 4096 matrix at r = 16. Typical targets are the attention projections and MLP matrices; ranks are 4–64.
  • Merging: after training, W’ = W + (α/r)AB, so inference has no extra cost. Unmerged adapters can be swapped per request.
  • QLoRA: base weights in 4-bit NF4 with double quantisation and paged optimizers. 65B parameters take 32.5 GB, so fine-tuning fits on one 48 GB GPU; adapters are trained in bf16.
  • Adapters (Houlsby): a bottleneck down-up projection with a residual inside each block (2db + b + d parameters; about 99k for d = 768, b = 64). They add a little inference latency.
  • Prefix and prompt tuning: trainable key/value prefixes per layer (L × length × 2d), or soft prompt embeddings at the input only; very few parameters, weaker on small models.
  • DoRA: write W = m · V/‖V‖ and train the magnitude m plus a LoRA update of the direction V; often closer to full fine-tuning at the same rank.
📝 Quick check (Level 1)
0/3 answered
  1. 1. LoRA parameters for a 4096 × 4096 matrix at r = 16:
  2. 2. At initialisation, a LoRA model equals…
  3. 3. A 65B model at 4 bits occupies about…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

PEFT in code

Exercise 1.

  • Implement the LoRA forward pass (identical to the base when B = 0).
  • Merge an adapter into the weight; the output is unchanged.
  • Count LoRA parameters.

Practice.

  • Hugging Face peft: LoraConfig(r, lora_alpha, target_modules, lora_dropout), get_peft_model, merge_and_unload; bitsandbytes 4-bit loading for QLoRA; use_dora=True for DoRA.
  • LoRA usually needs a higher learning rate than full fine-tuning (about 1e-4–2e-4 for LLMs, lower for tiny models).
  • Target all linear layers for best quality; raise r when the adaptation is large.
  • Keep adapters per task, and evaluate the merged model exactly as it will be served (quantised or not).
from peft import LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
base = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B",
          quantization_config=BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4"))   # QLoRA
model = get_peft_model(base, LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05,
                       target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]))
model.print_trainable_parameters()          # ~0.5% of parameters
🐍 Exercise 1: LoRA forward, merge and size
Implement the LoRA forward pass, adapter merging and the LoRA parameter count.
📝 Quick check (Level 2)
0/3 answered
  1. 1. PEFT function that folds adapters into the weights:
  2. 2. LoRA learning rates for LLMs are typically…
  3. 3. The flag for DoRA in peft:

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why low rank works. Aghajanyan and colleagues (2021) showed that fine-tuning has a low intrinsic dimension: a random low-dimensional subspace of updates achieves most of the gain. LoRA restricts each update ΔW to rank r. It works when the needed adaptation is close to low-rank, and loses capacity when it is not.

Rank as a dial. Small r means few parameters, implicit regularisation and limited change to the network, so less forgetting but possibly too little capacity. Large r approaches full fine-tuning in flexibility, and in forgetting. In exercise 2, rank 8 beat full fine-tuning on the new task: a small, constrained update overfitted 30 examples less than changing every weight.

Quantised bases. Freezing the base lets it be stored in 4 bits; gradients flow through the dequantised weights into the adapters. The adapter can even compensate for some quantisation error, which is why QLoRA matches 16-bit LoRA in many studies.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Fine-tuning has a low ___ dimension:
  2. 2. Larger LoRA rank gives…
  3. 3. In QLoRA, gradients flow into…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How much rank does an adapter need, and what does it cost in forgetting?

History.

  • N. Houlsby et al., “Parameter-efficient transfer learning for NLP” (ICML 2019), adapters.
  • X. L. Li and P. Liang, “Prefix-tuning” (ACL 2021).
  • E. J. Hu et al., “LoRA: low-rank adaptation of large language models” (ICLR 2022).
  • T. Dettmers et al., “QLoRA” (NeurIPS 2023).
  • S.-Y. Liu et al., “DoRA: weight-decomposed low-rank adaptation” (ICML 2024).

Exercise 2.

  • Set-up: the pretrained one-block Transformer from F6-1 (task A: copy 8 back), adapted to task B (copy 8 back plus 1) from 30 examples, 200 Adam steps.
  • Methods:
    • full fine-tuning at lr 0.001;
    • LoRA (lr 0.001, α = r) of rank 1, 2 or 8 on all seven weight matrices;
    • QLoRA: rank-2 LoRA on a 4-bit quantised base.
method trainable parameters loss on B loss on A (forgetting)
base model 0 4.629 0.545
full fine-tuning 9472 0.802 5.722
LoRA rank 1 490 1.801 1.598
LoRA rank 2 980 0.816 2.914
LoRA rank 8 3920 0.614 5.167
QLoRA rank 2 (4-bit base) 980 0.804 3.117
  • Rank is a capacity dial: rank 1 is too small for task B; rank 2 matches full fine-tuning with 10% of its parameters; rank 8 beats it.
  • Forgetting grows with rank: rank 1 changes the network least and keeps task A best. No method avoided forgetting entirely, because the output mapping itself had to change.
  • The 4-bit base costs nothing here: 4-bit quantisation changes the base model’s A loss by 0.001, and QLoRA matches LoRA.
  • Seeds: seeds 1 and 2 gave rank-8 B losses of 0.641/0.668 against full fine-tuning 0.768/0.880, and the same ordering of forgetting.
  • Tuning note: my first setting (lr 0.01, α = 2r) was far too aggressive. Higher ranks then did worse (rank 8: 3.78 on B), so the learning rate must shrink as adapter capacity grows.
🐍 Exercise 2 (research): how much rank does an adapter need, and what does it cost in forgetting?
Adapt a pretrained Transformer with full fine-tuning, LoRA of several ranks and QLoRA on a 4-bit base; compare new-task loss, forgetting and trainable parameters.

PEFT in AI, robotics and aerospace

  • Many specialised assistants: one frozen base with small adapters per department or aircraft type.
  • Constrained hardware: QLoRA fine-tuning on a single workstation; merged or swappable adapters at inference.
  • Design lesson: start with a small rank and a moderate learning rate, raise rank if the task needs it, and watch forgetting.

References

  1. N. Houlsby et al. (2019). “Parameter-efficient transfer learning for NLP.” Proc. ICML.
  2. E. J. Hu et al. (2022). “LoRA: low-rank adaptation of large language models.” Proc. ICLR.
  3. T. Dettmers, A. Pagnoni, A. Holtzman and L. Zettlemoyer (2023). “QLoRA: efficient finetuning of quantized LLMs.” NeurIPS.
  4. S.-Y. Liu et al. (2024). “DoRA: weight-decomposed low-rank adaptation.” Proc. ICML.
  5. A. Aghajanyan, L. Zettlemoyer and S. Gupta (2021). “Intrinsic dimensionality explains the effectiveness of language model fine-tuning.” Proc. ACL.