Lab 02 — TVM Compilation

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

Apache TVM compiles ML models to optimized native code for CPU, GPU, and custom accelerators.

What you build

  • Import PyTorch model into TVM Relay IR
  • Apply TVM optimization passes (fusion, layout transform, quantization)
  • Compile to native code with tvm.build
  • Benchmark TVM vs PyTorch on target hardware
  • Analyze Relay IR for supported vs unsupported ops

Key concepts

ConceptWhat to understand
Relay IRFunctional IR; higher-level than LLVM IR
TE (Tensor Expressions)Schedule-based computation description
AutoTVM / MetaScheduleML-guided kernel auto-tuning
Compilation pipelinerelay.optimizete.create_prim_functvm.build

Files

FilePurpose
lab.pyStubs with TODO markers
solution.pyComplete working implementation
test_lab.pypytest suite (skipped if TVM not installed)
requirements.txtDependencies

Run

pip install -r requirements.txt  # TVM requires separate install: see tvm.apache.org
python lab.py
pytest test_lab.py -v