Level 5 · research · ~45 h
Implement LoRA and QLoRA from scratch
Implement Low-Rank Adaptation and its quantized variant from scratch: inject trainable rank-decomposed updates into a frozen transformer, add 4-bit NF4 quantization with double quantization, and evaluate on an instruction-tuning task.
Prerequisites
Problem statement
- 01Implement Low-Rank Adaptation and its quantized variant from scratch: inject trainable rank-decomposed updates into a frozen transformer, add 4-bit NF4 quantization with double quantization, and evaluate on an instruction-tuning task.
Real-world motivation
- 01Parameter-efficient fine-tuning makes adaptation feasible under tight memory budgets, but the quantization details in QLoRA matter for fidelity.
Learning objectives
- 01Implement LoRA layers with correct scaling
- 02Implement NF4 quantization and dequantization
- 03Compare full fine-tuning, LoRA and QLoRA
Technical architecture
- 01frozen base weights with B A low-rank deltas
- 02rank r and alpha scaling
- 03NF4 codebook with blockwise scales and double quantization
Mathematical foundations
- 01$W'=W+\frac{\alpha}{r}BA$, $B\in\mathbb{R}^{d\times r}$, $A\in\mathbb{R}^{r\times k}$
- 02NF4 quantization maps normalized values to 16 levels
Dataset and sources
- 01Alpaca instruction subset
- 02WikiText-103 for language-model perplexity
Step-by-step tasks
- 01Implement LoRALinear with merged inference mode
- 02Verify merged weights equal W + alpha/r BA exactly
- 03Implement NF4 quantization with 64-value blocks
- 04Add double quantization of block scales
- 05Fine-tune a 7B model on Alpaca under a fixed memory budget
- 06Compare with full fine-tuning on a small model where feasible
Implementation requirements
- 01Use PyTorch hooks or explicit module replacement
- 02Keep LoRA parameters in fp32
- 03Use paged optimizers if GPU memory permits
Testing requirements
- 01Check merged inference matches unmerged forward
- 02Verify quantization round-trip error bounds
- 03Confirm only LoRA parameters receive gradients
Evaluation metrics
- 01validation perplexity
- 02instruction rubric score
- 03peak VRAM
- 04trainable parameter fraction
Expected outputs
- 01LoRA and NF4 implementations
- 02memory comparison table
- 03adapter checkpoints
- 04evaluation report
Common failure modes
- 01wrong alpha/r scaling makes updates ineffective
- 02double-quantization error compounds
- 03all linear layers targeted including lm_head breaks output
Stretch goals
- 01add DoRA
- 02study rank and target-module selection
Research extensions
- 01derive effective rank of learned adapters
- 02study quantization error propagation through layers
GitHub submission requirements
- 01Implementation with tests
- 02Merged-weight equivalence proof
- 03Memory and quality comparison
- 04Adapter release with a reproducibility script
Completion rubric
- 01LoRA math correct
- 02merge equivalence verified
- 03NF4 implemented
- 04memory measured
- 05quality comparison fair
Submit when every rubric item is satisfied and your repository contains runnable code plus a reproduction command.