Fine-Tuning & Alignment (LoRA, QLoRA, DPO & RLHF)
Learn when to prompt, when to retrieve with RAG, and when you must fine-tune. Master parameter-efficient adaptation (LoRA & QLoRA) to adapt 70B models on commodity hardware, and understand how modern Direct Preference Optimization (DPO) aligns models with human intent without fragile reinforcement learning loops.
- Prompt vs. RAG vs. Fine-Tuning decision rules
- Low-Rank Adaptation (LoRA) weight decomposition
- 4-Bit NormalFloat QLoRA on consumer GPUs
- SFT, RLHF & Direct Preference Optimization (DPO)
1The Adaptation Spectrum: Prompting vs. RAG vs. Fine-Tuning
The single most common mistake in AI engineering is reaching for fine-tuning to solve a knowledge problem. LLMs acquire general reasoning during pre-training on trillions of tokens. Trying to teach an LLM recent facts via fine-tuning causes hallucinations and catastrophic forgetting.
Interactive Architectural Decision Matrix
Select your real-world system constraints to see the recommended adaptation pattern.
System & Business Constraints
RAG (Retrieval-Augmented Generation)
Inject relevant chunks dynamically into the context window with exact citations and zero training cost.
Consumes prompt tokens on every request, increasing TTFT (Time to First Token) and API token costs.
2The Computational Cost of Full Fine-Tuning
During full fine-tuning, every single parameter in the neural network is updated via backpropagation. For a modern 8-billion parameter model, storing the model weights in 16-bit precision takes only 16 GB of VRAM. Why can't we train it on a single 24 GB GPU?
2 Bytes / param
For an 8B model: 16 GB of pure weights.
2 Bytes / param
First-order derivatives computed during backward pass: +16 GB.
8–12 Bytes / param
Maintains 1st and 2nd momentum states in FP32: +64 to 96 GB!
Total VRAM for full fine-tuning an 8B model easily exceeds 96–120 GB, requiring an expensive distributed cluster of NVIDIA A100/H100 GPUs. Furthermore, training every weight risks catastrophic forgetting—the model loses its general knowledge and reasoning skills in exchange for the new niche data.
3Parameter-Efficient Fine-Tuning: LoRA & QLoRA
In 2021, Hu et al. from Microsoft introduced LoRA (Low-Rank Adaptation) based on a profound hypothesis: the weight updates ($Δ W$) during task adaptation have a very low intrinsic rank. Instead of updating the massive weight matrix $W$ directly, LoRA decomposes the update into two small low-rank matrices:
- Base weights (W) are completely frozen: No optimizer states or gradients are stored for them.
- Matrix A: Initialized with random Gaussian noise with dimensions $(r × d_in)$.
- Matrix B: Initialized with zeros with dimensions $(d_out × r)$, ensuring $Δ W = 0$ at the start of training.
- Rank (r): Typically chosen between 8 and 64. Reduces trainable parameters by over 99.8%!
LoRA & QLoRA Parameter & VRAM Calculator
See how low-rank decomposition shrinks billions of weights into lightweight megabyte adapter files.
Base weight W_base is frozen. Adapter matrix B (d × r) and A (r × k) learn task delta.
✓ Swappable at runtime in milliseconds
Risk: High catastrophic forgetting of general reasoning
QLoRA: Quantized LoRA (Dettmers et al., 2023)
QLoRA takes PEFT even further by quantizing the frozen base model weights to 4-bit NormalFloat (NF4)—an information-theoretically optimal quantile distribution for normally distributed neural network weights. With Double Quantization (quantizing the quantization constants) and Paged Optimizers (spilling memory spikes to CPU RAM), a full 70-billion parameter model can be fine-tuned on a single 48 GB GPU!
4Alignment: From Raw Model to Helpful Assistant (SFT, RLHF, DPO)
Pre-training teaches a model statistical correlation across the internet. Alignment teaches the model to be Helpful, Honest, and Harmless (the 3 Hs).
Alignment Lab: SFT vs. RLHF vs. Direct Preference Optimization (DPO)
Compare classic Reinforcement Learning from Human Feedback (PPO) with modern DPO loss.
Explain how to safely reset a database password in production.
To safely reset a database password: 1. Generate a high-entropy secret in your KMS. 2. Update credentials using zero-downtime dual-password rotation. 3. Verify application health before revoking the legacy credential.
Just run UPDATE users SET password = 'admin123' in psql and restart the backend server during lunch break.
Why DPO Has Replaced PPO in Modern Open-Source AI
In traditional RLHF with PPO, training is unstable because the policy model and reward model can enter adversarial feedback loops. If the reward model rewards longer answers, the LLM starts babbling useless paragraphs ("verbosity bias").
- Requires 3-4 models in GPU memory
- Hyperparameter sensitive & unstable
- Samples generated during training loops
- Only requires base model & frozen reference
- Simple binary cross-entropy loss formulation
- Offline dataset of (prompt, chosen, rejected) pairs
5Dataset Engineering: Less Is More for Alignment (LIMA)
The famous Meta AI paper LIMA: Less Is More for Alignment (Zhou et al., 2023) demonstrated that fine-tuning on just 1,000 meticulously curated, high-quality instruction pairs outperformed models fine-tuned on 50,000 uncurated pairs.
Standard ChatML Training Template
<|im_start|>system
You are a senior PostgreSQL database engineer. Respond only with valid SQL queries.<|im_end|>
<|im_start|>user
Find all users who registered in the last 30 days and have spent over $500.<|im_end|>
<|im_start|>assistant
SELECT u.id, u.email, SUM(o.amount) AS total_spent
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE u.created_at >= NOW() - INTERVAL '30 days'
GROUP BY u.id, u.email
HAVING SUM(o.amount) > 500;<|im_end|>Special delimiter tokens (<|im_start|>, <|im_end|>) prevent prompt injection attacks from breaking out of user roles during model execution.
6Instant Challenge
Architect the Medical Summarizer Pipeline
You are the Lead AI Architect at a hospital network. You must build an AI system that ingests confidential, real-time patient lab reports and outputs summaries formatted to strict Epic EHR HL7/FHIR JSON specifications. The system must run on on-premise servers (air-gapped), cite exact lab values, and guarantee schema compliance with minimal latency. Which architecture should you select?
1Choose the architecture that satisfies the hospital's requirements.
7Knowledge Checks
Why does LoRA freeze the base model weights and only train low-rank adapter matrices?
What is the primary technical breakthrough of Direct Preference Optimization (DPO) compared to PPO-based RLHF?
If your company requires an AI agent that knows about product stock levels that change every 5 minutes, what is the correct strategy?
What does the NF4 data type in QLoRA stand for, and why is it special?
8Recap
- RAG for facts, Fine-Tuning for form: Never fine-tune to inject frequently changing knowledge. Fine-tune to bake in specialized formatting, tone, or domain syntax.
- LoRA eliminates VRAM bottlenecks: Decomposing weight updates into rank matrices ($W + B × A$) cuts trainable parameters by over 99%.
- QLoRA democratizes 70B training: 4-bit NormalFloat quantization fits large models on single commodity GPUs.
- DPO simplifies alignment: Replaces fragile PPO reinforcement learning loops with direct binary cross-entropy optimization on preference pairs.
- Quality over quantity: LIMA proves that 1,000 clean, diverse instruction-response pairs beat 50,000 noisy samples.
Solve the challenge above to complete the chapter, or mark it complete now: