Lab 01 — ONNX Export & Optimization

Phase: 05 — ML Compilers & IR | Difficulty: ⭐⭐⭐⭐☆ | Time: 3–4 hours

ONNX is the universal interchange format between training frameworks and inference runtimes (TensorRT, ONNX Runtime, CoreML, QNN).

What you build

  • Export PyTorch model to ONNX opset 17
  • Apply graph optimization passes (constant folding, op fusion)
  • Validate numerical equivalence PyTorch ↔ ONNX Runtime
  • Measure latency: PyTorch eager vs ORT vs optimized ORT

Key concepts

ConceptWhat to understand
ONNX opsetVersioned set of ops — opset 17 is required for newer transformer ops
torch.onnx.exportTraces model; use dynamo_export for torch.compile models
ORT optimizationsOrtOptimizationLevel.ORT_ENABLE_ALL applies graph-level rewrites
Dynamic shapesdynamic_axes allows variable batch/sequence length

Files

FilePurpose
lab.pyStubs with TODO markers
solution.pyComplete working implementation
test_lab.pypytest suite
requirements.txtDependencies

Run

pip install -r requirements.txt
python lab.py
pytest test_lab.py -v