{"id":51716580,"url":"https://github.com/appleweiping/mlc-exercises","last_synced_at":"2026-07-17T03:36:28.607Z","repository":{"id":369161692,"uuid":"1288650128","full_name":"appleweiping/mlc-exercises","owner":"appleweiping","description":"Solutions to the Machine Learning Compilation (MLC, mlc.ai) course exercises — TensorIR tensor programs, end-to-end model compilation, automatic program optimization, and framework integration with Apache TVM","archived":false,"fork":false,"pushed_at":"2026-07-03T21:11:34.000Z","size":83,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-07-03T23:13:18.763Z","etag":null,"topics":["compiler","csdiy","machine-learning-compilation","mlc","mlsys","tensor-ir","tvm"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/appleweiping.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-07-03T20:23:57.000Z","updated_at":"2026-07-03T21:11:38.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/appleweiping/mlc-exercises","commit_stats":null,"previous_names":["appleweiping/mlc-exercises"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/appleweiping/mlc-exercises","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appleweiping%2Fmlc-exercises","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appleweiping%2Fmlc-exercises/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appleweiping%2Fmlc-exercises/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appleweiping%2Fmlc-exercises/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/appleweiping","download_url":"https://codeload.github.com/appleweiping/mlc-exercises/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appleweiping%2Fmlc-exercises/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35566571,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-17T02:00:06.162Z","response_time":116,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compiler","csdiy","machine-learning-compilation","mlc","mlsys","tensor-ir","tvm"],"created_at":"2026-07-17T03:36:27.934Z","updated_at":"2026-07-17T03:36:28.593Z","avatar_url":"https://github.com/appleweiping.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MLC Exercises — Machine Learning Compilation with Apache TVM\n\n\u003e Solutions to the **Machine Learning Compilation** course exercises — TensorIR\n\u003e tensor programs, end-to-end model compilation in Relax, automatic program\n\u003e optimization (meta-schedule), and PyTorch framework integration — an\n\u003e independent, from-skeleton implementation of **MLC (mlc.ai, Tianqi Chen)**,\n\u003e part of a [csdiy.wiki](https://csdiy.wiki/) full-catalog build.\n\n![status](https://img.shields.io/badge/status-complete-brightgreen)\n![language](https://img.shields.io/badge/python-3.11-informational)\n![tvm](https://img.shields.io/badge/apache--tvm-0.25-orange)\n![license](https://img.shields.io/badge/license-MIT-blue)\n\n## Overview\n\n[Machine Learning Compilation](https://mlc.ai/) teaches how to take a model from\na training framework (PyTorch/TensorFlow/JAX) and progressively lower, transform\nand optimize it into fast deployable code, using **Apache TVM** (TensorIR +\nRelax). This repo implements the course's core exercises from scratch and\n**runs them for real on CPU**: every program is built, executed, and checked\nagainst a NumPy/PyTorch reference, and every claimed speedup is measured with\nTVM's own `time_evaluator`.\n\nThe official course exercises are notebooks\n([`mlc-ai/notebooks`](https://github.com/mlc-ai/notebooks)); the official\n`assignment1.ipynb` skeleton is kept as the base commit under `notebooks/`, and\neach exercise is implemented as a clean, tested Python module under `src/`.\n\n\u003e **Note on the TVM version.** The course was written for the `mlc-ai-nightly`\n\u003e *TVM Unity* wheels (Linux/macOS only). This repo targets the public\n\u003e `apache-tvm` **0.25** wheel — the post-Unity-refactor build that installs\n\u003e natively on Windows CPU — whose API was renamed (`tvm.tir.Schedule` →\n\u003e `tvm.s_tir.Schedule`, `tvm.script.tir` → `tvm.script.tirx` with `s_tir=True`,\n\u003e `T.block` → `T.sblock`, `tvm.nd` → `tvm.runtime`, etc.). `src/mlc_compat.py`\n\u003e documents the full mapping. All four exercises run end-to-end on this build.\n\n## Results (measured on CPU, 3 threads; apache-tvm 0.25, torch 2.12 CPU)\n\n| Exercise | What it does | Result (measured) |\n|---|---|---|\n| **1 · TensorIR** | matmul+ReLU written in TVMScript, then a `split/reorder/fuse/parallel/unroll/vectorize` schedule | correct to **2.3e-5** vs NumPy; hand-schedule **4.2×** faster (3.28 → 0.78 ms) |\n| **2 · End-to-end Relax** | Fashion-MNIST CNN built with `BlockBuilder`+`emit_te`; conv2d also via a registered external PyTorch kernel; conv2d TensorIR scheduled | outputs match PyTorch to **1.2e-7**; compiled Relax model reaches **84.5%** real Fashion-MNIST test accuracy (2000 imgs) |\n| **3 · Auto-optimization** | matmul (512²) tuned by an in-process random search and by real `meta_schedule.tune_tir` | random search **22.1×**, meta-schedule **34.5×** speedup (169 → 4.9 ms), numerically verified |\n| **4 · PyTorch integration** | trace `nn.Module` with `torch.fx`, import via `from_fx`, run a Relax optimization pipeline, compile \u0026 run | imported CNN matches PyTorch to **1.5e-8** |\n\n![speedups](results/speedup.png)\n\nSample predictions from the **compiled Relax model** on the real Fashion-MNIST\ntest set (green = correct):\n\n![predictions](results/fashion_mnist_predictions.png)\n\nFull numbers in [`results/summary.json`](results/summary.json); full log in\n[`results/run_all.log`](results/run_all.log).\n\n## Implemented exercises\n\n- [x] **Ex 1 — TensorIR tensor-program abstraction \u0026 transformations**\n  (`src/ex1_tensorir.py`): TVMScript \u0026 Tensor-Expression matmul+ReLU, a full loop\n  schedule, and a batched-matmul + bias + ReLU case study.\n- [x] **Ex 2 — End-to-end model execution in Relax** (`src/ex2_end_to_end.py`,\n  MLC *assignment 1* §2–4): build the Fashion-MNIST classifier via\n  `emit_te`/TOPI, integrate an external PyTorch conv2d kernel, and schedule the\n  conv2d TensorIR.\n- [x] **Ex 3 — Automatic program optimization** (`src/ex3_auto_optimize.py`):\n  stochastic schedule search with `sample_perfect_tile`, and real\n  `meta_schedule.tune_tir` evolutionary tuning.\n- [x] **Ex 4 — Integration with ML frameworks** (`src/ex4_pytorch_integration.py`):\n  `torch.fx` → `relax.frontend.torch.from_fx` → Relax optimization pipeline → VM.\n\n## Project structure\n\n```\nmlc-exercises/\n├── src/\n│   ├── mlc_compat.py            # course-era API  -\u003e  apache-tvm 0.25 helpers\n│   ├── ex1_tensorir.py          # TensorIR programs \u0026 schedules\n│   ├── ex2_end_to_end.py        # Relax end-to-end Fashion-MNIST model\n│   ├── ex3_auto_optimize.py     # random search + meta-schedule tuning\n│   └── ex4_pytorch_integration.py\n├── tests/                       # pytest numerical-correctness suite (16 tests)\n├── scripts/\n│   ├── get_data.py              # download assignment weights + Fashion-MNIST\n│   └── run_all.py               # reproduce every result -\u003e results/\n├── results/                     # measured evidence (json, log, figures)\n├── notebooks/assignment1_official.ipynb   # official skeleton (base commit)\n└── requirements.txt\n```\n\n## How to run\n\n```bash\n# Python 3.11 with the shared csdiy env, or any venv:\n#   D:\\Project\\_csdiy\\.venv-ml\\Scripts\\python.exe\npython -m pip install -r requirements.txt\n\n# 1. download the assignment's own weights + Fashion-MNIST (git-ignored)\npython scripts/get_data.py\n\n# 2. run the full test suite (numerical correctness of every exercise)\nOMP_NUM_THREADS=3 python -m pytest -m \"not slow\"      # 15 fast tests\nOMP_NUM_THREADS=3 python -m pytest -m slow            # + meta-schedule tuning\n\n# 3. reproduce every measured number + figures into results/\nOMP_NUM_THREADS=3 python scripts/run_all.py\n```\n\n## Verification\n\n- **Correctness:** `pytest` — 16 tests (15 fast + 1 meta-schedule). Every\n  TensorIR / Relax program is executed and `np.testing.assert_allclose`-checked\n  against a NumPy or PyTorch reference (see `tests/`).\n- **Schedules preserve semantics:** transformed modules are rebuilt and compared\n  to the originals; the tests also assert the intended primitives\n  (`T.parallel`, `T.vectorized`, `T.unroll`) actually appear in the lowered IR.\n- **Real inference run:** `scripts/run_all.py` runs the compiled Relax model over\n  the real Fashion-MNIST test set → **84.5%** accuracy (the assignment's\n  pretrained weights are ~83–84%), producing `results/fashion_mnist_predictions.png`.\n- **Real speedups:** timings use TVM's `time_evaluator` (warm, build excluded);\n  see `results/summary.json` and `results/speedup.png`.\n\n## Tech stack\n\nPython 3.11 · **Apache TVM 0.25** (TensorIR / `s_tir`, Relax, meta-schedule,\nTOPI, `relax.frontend.torch`) · PyTorch 2.12 (CPU) · torchvision · NumPy ·\nMatplotlib · pytest.\n\n## Key ideas / what I learned\n\n- **Tensor-program abstraction:** the same computation as a directly-written\n  TVMScript `prim_func`, a Tensor-Expression description, and a *schedulable*\n  block-based TensorIR — and how `split/reorder/fuse/parallel/vectorize/unroll`\n  reshape the loop nest while preserving semantics (and when they are *illegal*,\n  e.g. parallelising across a reduction's `init`).\n- **End-to-end graphs in Relax:** `BlockBuilder` + `emit_te` to assemble a\n  computational graph of `call_tir` ops, and mixing in an external vendor\n  (PyTorch) kernel via `call_dps_packed`.\n- **Automatic optimization:** letting a search + cost model discover a tiling\n  that beats a naive loop by 20–35×, instead of hand-tuning.\n- **Framework integration:** lowering a real PyTorch `nn.Module` into TVM via the\n  `torch.fx` frontend and running a Relax optimization pipeline before codegen.\n\n## Credits \u0026 license\n\nBased on the exercises of the **Machine Learning Compilation** course by\n**Tianqi Chen** and the MLC / Apache TVM community ([mlc.ai](https://mlc.ai/),\n[mlc-ai/notebooks](https://github.com/mlc-ai/notebooks)). This repository is an\nindependent educational reimplementation; all course materials, datasets, and\nspecifications belong to their original authors. Original code in this repo is\nreleased under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappleweiping%2Fmlc-exercises","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fappleweiping%2Fmlc-exercises","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappleweiping%2Fmlc-exercises/lists"}