Lab 02 — Custom C++ Operator
Phase: 01 — PyTorch & TF Framework Internals | Difficulty: ⭐⭐⭐⭐☆ | Time: 2–4 hours
Read
../HITCHHIKERS-GUIDE.mdbefore starting.
What you build
A custom PyTorch operator written in C++ that registers with the dispatcher:
- C++ extension using
pybind11andtorch/extension.h - Forward pass: fused linear + ReLU in a single C++ kernel
- Registration via
TORCH_LIBRARY/PYBIND11_MODULE - Python test verifying numeric equality with the PyTorch reference
Key concepts
| Concept | What to understand |
|---|---|
| ATen dispatcher | How torch.ops.mylib.op() routes to C++ |
at::Tensor | C++ tensor API — same data, different language |
setup.py + torch.utils.cpp_extension | Build system for extensions |
| Schema | The type signature registered with the dispatcher |
Files
| File | Purpose |
|---|---|
setup.py | Build configuration for C++ extension |
csrc/ | C++ source files |
solution.py | Python tests and usage |
requirements.txt | Python dependencies |
Run
pip install -r requirements.txt
python setup.py build_ext --inplace
python solution.py