Lab 01 — Transformer Architectural Variants
Phase: 02 — Model Architecture Mastery | Difficulty: ⭐⭐⭐⭐☆ | Time: 3–4 hours
Read
../HITCHHIKERS-GUIDE.mdbefore starting.
What you build
Implement and compare four key attention variants used in production LLMs:
- Multi-Head Attention (MHA) — original Transformer (Vaswani et al.)
- Multi-Query Attention (MQA) — single KV head, multiple Q heads (PaLM, Falcon)
- Grouped-Query Attention (GQA) — G KV groups (Llama 2/3, Mistral)
- RoPE positional embeddings — rotary position encoding used in Llama/GPT-NeoX
Key concepts
| Concept | What to understand |
|---|---|
| KV cache | MQA/GQA reduce memory for long context inference |
| Head grouping | G groups share KV, reducing VRAM at minimal accuracy cost |
| RoPE | Rotates Q and K before dot product — relative positions for free |
| Flash-attention pattern | Why attention needs tiling for memory efficiency |
Files
| File | Purpose |
|---|---|
lab.py | Stubs with TODO markers |
solution.py | Full working implementation + smoke test |
test_lab.py | pytest suite |
requirements.txt | Dependencies |
Run
pip install -r requirements.txt
python lab.py
pytest test_lab.py -v