Lab 01 — FX Graph Passes
Phase: 04 — torch.compile, FX Graph & Custom Backends | Difficulty: ⭐⭐⭐⭐☆ | Time: 3–4 hours
FX graph passes are how
torch.compiletransforms your model before lowering to a backend.
What you build
symbolic_tracea model to capture its FX graph- Write a graph pass that fuses
linear → reluinto a single node - Write a graph pass that eliminates redundant operations (dead code)
- Verify output numerical equivalence before and after transformation
Key concepts
| Concept | What to understand |
|---|---|
| FX IR | Three-address code representation of PyTorch operations |
| Node kinds | placeholder, get_attr, call_function, call_module, output |
| Graph rewriting | Insert/erase nodes; update users; maintain invariants |
| Operator fusion | Fused kernels reduce memory bandwidth (key for NPU perf) |
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