Lab 01 — Custom Autograd Engine
Phase: 01 — PyTorch & TF Framework Internals | Difficulty: ⭐⭐⭐☆☆ | Time: 2–3 hours
Read
../HITCHHIKERS-GUIDE.mdbefore starting. This lab builds intuition for how PyTorch computes gradients under the hood.
What you build
A minimal scalar-valued autograd engine that mirrors PyTorch's torch.Tensor autograd mechanism:
Valueclass wrapping scalars with+,*,tanh,relu,exp,powoperations- Reverse-mode automatic differentiation via
.backward() - Dynamic computation graph construction (define-by-run)
- Gradient accumulation across shared nodes (topological sort)
Key concepts
| Concept | What to understand |
|---|---|
| Computation graph | Each op creates a node; edges are _prev set |
| Chain rule | dL/dx = dL/dout * dout/dx applied backwards |
| STE | How gradients skip through non-differentiable ops |
detach() | Stops gradient flow — critical for quantization |
Files
| File | Purpose |
|---|---|
lab.py | Stub with TODO markers — implement this |
solution.py | Complete working implementation |
test_lab.py | pytest suite — run to verify your work |
requirements.txt | Dependencies |
Run
pip install -r requirements.txt
python lab.py # should print results after you fill in TODOs
pytest test_lab.py -v