Fine-Tuning & LoRA
Generative & Agentic AI · Customize & Ship · Advanced · 14 min
Teach a model new habits cheaply, and know when NOT to.
A huge frozen book with a thin sticky note on each page. LoRA only writes on the sticky notes: a tiny fraction of the paper, yet it changes how the whole book reads.
The idea in everyday words
A pre-trained model is a chef who graduated culinary school. Prompting = giving them a recipe card. RAG = handing them your cookbook. Fine-tuning = sending them to a 2-week course in YOUR restaurant's style. LoRA = instead of re-training the whole chef, you give them a small clip-on cheat sheet.
Ladder of options (try in order)
- Prompting – free, instant.
- RAG – add knowledge.
- Fine-tuning – change style, format, tone, or teach a narrow task with many examples.
- Pre-training from scratch – millions of dollars. Almost never.
LoRA in plain words
Full fine-tuning updates ALL billions of weights. LoRA freezes them and trains two tiny matrices (A and B) per layer. Trainable params per layer ≈ r × (d_in + d_out) where r (rank) is small like 8 or 16. QLoRA = LoRA on a 4-bit quantized model → fine-tune 7B on one consumer GPU.
Tools: Hugging Face PEFT, Unsloth, Axolotl. Cloud: Bedrock custom models, Vertex AI tuning, Azure OpenAI fine-tuning.
Go deeper (engineer level): LoRA mechanics and fine-tuning failure modes
What LoRA really computes. The layer output becomes W x + (α / r) · B A x. A (r × d_in) starts random and B (d_out × r) starts at zero, so at step 0 the model is exactly the base model and training only moves away from it as needed. α / r scales the update; if you change r, re-tune the learning rate. Common targets are the attention projections (q, k, v, o) and often the MLP layers too; adding MLP layers usually helps more than raising r.
Why it saves memory. Full fine-tuning with Adam in mixed precision keeps roughly 16 bytes per parameter (fp16 weights and gradients, fp32 master weights, two Adam moments): about 112 GB for a 7B model before activations. LoRA keeps the frozen base at 2 bytes per parameter (or about 0.5 with QLoRA's 4-bit NF4) and optimizer state only for the adapter. Activations still grow with batch × sequence length; gradient checkpointing trades compute for that memory.
Serving. Merge B A into W for zero extra latency, or keep adapters separate and serve many of them on one shared base (multi-LoRA serving), which is cheap for per-customer variants.
Failure modes:
- Overfitting a few hundred examples: training loss falls while held-out loss rises. Stop early.
- Catastrophic forgetting: the model gets good at your task and worse at general instructions. Keep a general eval set too.
- Chat template mismatch: training without the model's exact chat template, then calling it with one (or the reverse), quietly ruins quality.
- Eval contamination: near-duplicates of eval items in the training data make results look better than they are.
- Teaching facts: they are stored unreliably and go stale. Use RAG for knowledge.
Step by step
- Data:
Collect 500–5,000 good input→output examples (JSONL).
- Base model:
Pick e.g. Llama 8B.
- LoRA:
Freeze base, train small adapters (r=16).
- Evaluate:
Compare on held-out examples vs base model.
- Serve:
Merge adapter or load it on top at inference.
Code example
def lora_params(d_in, d_out, r):
return r * (d_in + d_out)
full = 4096 * 4096
lora = lora_params(4096, 4096, 8)
print("full layer params:", full)
print("LoRA params:", lora)
print("percent trained:", round(100 * lora / full, 3), "%")Interview tips
Explain RAG vs fine-tuning crisply: very common question.
Know LoRA/QLoRA and why they're memory-efficient.
Mention RLHF/DPO for aligning models to preferences.
Challenge: LoRA Parameter Counter
Given a list of layers as [d_in, d_out] pairs and a rank r, return the total LoRA trainable parameters: sum of r × (d_in + d_out).
Example: lora_total([[10, 10], [4, 6]], 2) → 2×20 + 2×10 = 60
Solve it in the browser with tests, hints and an AI coach.
Practice problems
- LoRA: Low-Rank Adaptation of Large Language Models (paper)
- Hugging Face PEFT documentation
- Amazon Bedrock: customize your model
- Amazon SageMaker JumpStart: fine-tune a model
More in Customize & Ship
- Making Models Smaller & Faster
- LLM Inference & Serving at Scale
- Multimodal & Image Generation
- Evaluation & Guardrails
- LLMOps: Shipping GenAI on the Cloud
Previous: Agent2Agent (A2A) Protocol · Next: Making Models Smaller & Faster