Fine-Tuning LLMs: LoRA & QLoRA
Understand fine-tuning for engineers: LoRA and QLoRA, instruction tuning, RLHF and DPO, and the crucial decision of when to fine-tune versus when retrieval or prompting is the better tool.
On this page
Fine-tuning is powerful, frequently reached for too early, and often the wrong tool. This tutorial explains the techniques an engineer should understand — LoRA, QLoRA, alignment methods — and, most importantly, the decision of when to fine-tune versus when retrieval or prompting solves the problem better and cheaper.
Key Takeaways
- Facts → retrieval. Behaviour → fine-tuning. Most needs are knowledge needs.
- LoRA trains small adapters instead of all weights — cheap, swappable.
- QLoRA adds quantization so you can fine-tune large models on modest hardware.
- Most application teams never need to fine-tune — try prompting and RAG first.
The decision that matters most
Before any technique, the real question: should you fine-tune at all? The answer follows one rule.
Full fine-tuning vs parameter-efficient
Full fine-tuning updates every weight in the model. It is expensive — you need the compute and memory to train a model with billions of parameters — and produces a full-size model per task.
Parameter-efficient fine-tuning (PEFT) updates only a small number of new parameters, leaving the base model frozen. LoRA is the dominant PEFT method. For almost everyone, PEFT is the practical choice.
LoRA
LoRA (Low-Rank Adaptation) inserts small trainable matrices alongside the model's existing weights and trains only those. The base model is untouched. The result is a small adapter file — often a few megabytes against a multi-gigabyte model — that you load on top of the base at inference.
The benefits:
- Cheap — far less compute and memory than full fine-tuning.
- Swappable — different adapters for different tasks, on one base model.
- Portable — the adapter is small and easy to store and version.
QLoRA
QLoRA adds quantization: the base model's weights are stored at lower precision (typically 4-bit) to slash memory use, while the LoRA adapters train at higher precision. This is what makes it possible to fine-tune a large model on a single consumer or modest cloud GPU, where full fine-tuning would need a cluster.
The trade-off is a small quality cost from running the base at reduced precision — usually acceptable, and a good deal for the memory saving. See model distillation & quantization for quantization in depth.
Instruction tuning and alignment
The models you use as assistants have been through more than base training:
- Instruction tuning — training on examples of following instructions, turning a raw next-token predictor into something that responds to requests.
- RLHF (Reinforcement Learning from Human Feedback) — training a reward model from human preference comparisons, then optimising the model against it. This is much of what makes a model helpful and harmless.
- DPO (Direct Preference Optimization) — a simpler alternative to RLHF that optimises directly on preference pairs, achieving similar alignment with less machinery.
These are typically done by model providers, not application teams. You benefit from them; you rarely perform them.
When fine-tuning genuinely helps
Legitimate cases, once prompting has been tried and found wanting:
- A consistent output format or style that prompting cannot reliably enforce at your volume.
- A narrow, high-volume classification where a fine-tuned small model beats prompting a large one on both cost and accuracy.
- A specialised domain language where the base model's register is persistently wrong.
- Latency or cost — a fine-tuned small model can replace a prompted large one for a fixed task.
A practical path
- Prompt engineering first. Most problems dissolve here. Free, instant, no maintenance.
- RAG for anything knowledge-related. Updatable, citable, no training.
- Few-shot examples in the prompt for format consistency.
- Fine-tune only when the above genuinely fall short, and the case is behaviour, not facts.
Reaching step four should be the exception, backed by evidence that steps one to three were insufficient — not the default first move it is often treated as.
Next
Frequently Asked Questions
When should I fine-tune a model instead of using RAG?
What is LoRA?
What is the difference between LoRA and QLoRA?
What are RLHF and DPO?
Related tutorials
- Transformer Architecture ExplainedThe transformer architecture explained for engineers, not researchers: self-attention, multi-head attention, positional encoding and why it explains context limits, token cost and hallucination.
- Vector Databases Deep DiveHow vector databases work under the hood: the HNSW index, approximate nearest-neighbour search, cosine vs Euclidean distance, product quantization and metadata filtering — for Java developers.
- Foundation Models Deep DiveUnderstand the foundation models you build on: GPT, Claude, Gemini, Llama and Mistral families, how they differ, and a practical framework for choosing a model for your Java application.
- Embedding Models & Semantic SearchHow embedding models power semantic search: bi-encoders vs cross-encoders, re-ranking, hybrid search combining keywords and vectors, and choosing embeddings for retrieval quality.