Lab 02 — QLoRA Adapter
Phase: 10 — GenAI on Edge | Difficulty: ⭐⭐⭐⭐⭐ | Time: 4–5 hours
QLoRA (Dettmers et al., 2023) fine-tunes 65B LLMs on a single 48GB GPU using 4-bit NF4 quantization + LoRA adapters.
What you build
LoRALinear— frozen base weight + trainable low-rank adapters A, B (B=0 init)quantize_nf4/dequantize_nf4— NF4 (Normal Float 4-bit) block quantization (block_size=64)QLoRALinear— NF4 quantized base + trainable LoRA adaptertrain_qlora— freeze base, train A/B/bias only with AdamWmerge_lora— W_merged = W_base_deq + B@A * (α/r) for deployment
Key concepts
| Concept | What to understand |
|---|---|
| LoRA | W = W_base + B@A * (α/r); rank r << d — few trainable params |
| NF4 | Quantization grid optimized for normally-distributed weights |
| Block quantization | Independent scale per 64 values — better accuracy than per-tensor |
| Double quant | Quantize the quantization constants themselves (saves ~0.4 bpw) |
| Memory savings | 7B model: FP16=14GB, INT8=7GB, NF4=3.5GB VRAM |
Files
| File | Purpose |
|---|---|
lab.py | Stubs with TODO markers |
solution.py | Complete working implementation |
test_lab.py | pytest suite |
requirements.txt | Dependencies |
Run
pip install -r requirements.txt
python lab.py
pytest test_lab.py -v