Skip to main content

Python-first Development for AI Compilers

Project description

MLC Logo

MLC-Python

Python-first Development for AI Compilers.

MLC is a Python-first toolkit that streamlines the development of AI compilers, runtimes, and compound AI systems with its Pythonic dataclasses, structure-aware tooling, and Python-based text formats.

Beyond pure Python, MLC natively supports zero-copy interoperation with C++ plugins, and enables a smooth engineering practice transitioning from Python to hybrid or Python-free development.

:inbox_tray: Installation

pip install -U mlc-python

:key: Key Features

:building_construction: Define IRs with MLC Dataclasses

MLC provides Pythonic dataclasses:

import mlc.dataclasses as mlcd

@mlcd.py_class("demo.MyClass")
class MyClass(mlcd.PyClass):
  a: int
  b: str
  c: float | None

instance = MyClass(12, "test", c=None)

Type safety. MLC dataclass enforces strict type checking using Cython and C++.

>>> instance.c = 10; print(instance)
demo.MyClass(a=12, b='test', c=10.0)

>>> instance.c = "wrong type"
TypeError: must be real number, not str

>>> instance.non_exist = 1
AttributeError: 'MyClass' object has no attribute 'non_exist' and no __dict__ for setting new attributes

Serialization. MLC dataclasses are picklable and JSON-serializable.

>>> MyClass.from_json(instance.json())
demo.MyClass(a=12, b='test', c=None)

>>> import pickle; pickle.loads(pickle.dumps(instance))
demo.MyClass(a=12, b='test', c=None)

:snake: Design Python-based Text Formats for IRs

Printer. MLC looks up method __ir_print__ to convert IR nodes to Python AST:

[Example]. Copy the toy IR definition to REPL and then create a Func node below:

>>> a, b, c, d, e = Var("a"), Var("b"), Var("c"), Var("d"), Var("e")
>>> f = Func("f", [a, b, c],
  stmts=[
    Assign(lhs=d, rhs=Add(a, b)),  # d = a + b
    Assign(lhs=e, rhs=Add(d, c)),  # e = d + c
  ],
  ret=e)
  • Method mlc.printer.to_python converts an IR node to Python-based text;
>>> print(mlcp.to_python(f))  # Stringify to Python
def f(a, b, c):
  d = a + b
  e = d + c
  return e
  • Method mlc.printer.print_python further renders the text with proper syntax highlighting. [Screenshot]
>>> mlcp.print_python(f)  # Syntax highlighting

AST Parser. MLC has a concise set of APIs for implementing parser with Python's AST module, including:

  • Inspection API that obtains source code of a Python class or function and the variables they capture;
  • Variable management APIs that help with proper scoping;
  • AST fragment evaluation APIs;
  • Error rendering APIs.

[Example]. With MLC APIs, a parser can be implemented with 100 lines of code for the Python text format above defined by __ir_printer__.

:dart: Test IRs with MLC Structure-Aware Tooling

By annotating IR definitions with structure, MLC supports structural equality and structural hashing to detect structural equivalence between IRs:

Define a toy IR with `structure`.
import mlc.dataclasses as mlcd

@mlcd.py_class
class Expr(mlcd.PyClass):
  def __add__(self, other):
    return Add(a=self, b=other)

@mlcd.py_class(structure="nobind")
class Add(Expr):
  a: Expr
  b: Expr

@mlcd.py_class(structure="var")
class Var(Expr):
  name: str = mlcd.field(structure=None) # excludes `name` from defined structure

@mlcd.py_class(structure="bind")
class Let(Expr):
  rhs: Expr
  lhs: Var = mlcd.field(structure="bind") # `Let.lhs` is the def-site
  body: Expr

Structural equality. Member method eq_s compares the structural equality (alpha equivalence) of two IRs represented by MLC's structured dataclass.

>>> x, y, z = Var("x"), Var("y"), Var("z")
>>> L1 = Let(rhs=x + y, lhs=z, body=z)  # let z = x + y; z
>>> L2 = Let(rhs=y + z, lhs=x, body=x)  # let x = y + z; x
>>> L3 = Let(rhs=x + x, lhs=z, body=z)  # let z = x + x; z
>>> L1.eq_s(L2)
True
>>> L1.eq_s(L3, assert_mode=True)
ValueError: Structural equality check failed at {root}.rhs.b: Inconsistent binding. RHS has been bound to a different node while LHS is not bound

Structural hashing. The structure of MLC dataclasses can be hashed via hash_s, which guarantees if two dataclasses are alpha-equivalent, they will share the same structural hash:

>>> L1_hash, L2_hash, L3_hash = L1.hash_s(), L2.hash_s(), L3.hash_s()
>>> assert L1_hash == L2_hash
>>> assert L1_hash != L3_hash

:zap: Migrate Gradually to C++ with MLC Plugins

(🚧 Under construction)

MLC seamlessly supports zero-copy bidirectional interoperabilty with C++ plugins with no extra dependency. By gradually migrating classes and methods one at a time, a pure Python prototype can be transitioned to hybrid or Python-free development.

:fuelpump: Development

:gear: Editable Build

pip install --verbose --editable ".[dev]"
pre-commit install

:ferris_wheel: Create Wheels

This project uses cibuildwheel to build cross-platform wheels. See .github/workflows/wheels.ym for more details.

export CIBW_BUILD_VERBOSITY=3
export CIBW_BUILD="cp3*-manylinux_x86_64"
python -m pip install pipx
pipx run cibuildwheel==2.22.0 --output-dir wheelhouse

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mlc_python-0.3.8-cp39-abi3-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.9+Windows x86-64

mlc_python-0.3.8-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (43.4 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

mlc_python-0.3.8-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (43.7 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

mlc_python-0.3.8-cp39-abi3-macosx_11_0_arm64.whl (15.2 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

mlc_python-0.3.8-cp39-abi3-macosx_10_9_x86_64.whl (14.1 MB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

Details for the file mlc_python-0.3.8-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: mlc_python-0.3.8-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mlc_python-0.3.8-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 33496e1e8e512164289af80194d64e7caff42bb3e44571b8e07ed335bbdebd5f
MD5 d6fb223e2fb1c966395491cb3c8b44b6
BLAKE2b-256 47b6a32e789d217a8727c63144140e5a5058724ba1ee4d66f720cee2e856e5fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlc_python-0.3.8-cp39-abi3-win_amd64.whl:

Publisher: release.yml on mlc-ai/mlc-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlc_python-0.3.8-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mlc_python-0.3.8-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a02cb1e56461a03e022bdeb6b0c64616d66d1dc372c4ce792a4e79bf3e1d1436
MD5 1115f9e5eac24dd710a93db2416aae1d
BLAKE2b-256 4882dcbd280f37b9a98ea4ab2a7acf021085e9b790b889085d8540d26fa67030

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlc_python-0.3.8-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on mlc-ai/mlc-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlc_python-0.3.8-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for mlc_python-0.3.8-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2196ddfd0e77e84d10cbd93762417167d5110cd19d2dec429fbbf091a59fa52c
MD5 cb2a5b64c9071d43ba4b2b2ef3648244
BLAKE2b-256 a02501d0e3b2e05968de0c619bec26d958267c4df734bb84820e806d19807c3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlc_python-0.3.8-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on mlc-ai/mlc-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlc_python-0.3.8-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mlc_python-0.3.8-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2ff25c87392270f9325a83cb107ef04539365e4db362ca04f33f10f7e2cb5b99
MD5 b5e28d666f9e6c57e6d1da3660c1e6b2
BLAKE2b-256 1435ce91f0070d6434d396d2a83173642f76efc5b3e50aad288be9f251c5cd77

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlc_python-0.3.8-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on mlc-ai/mlc-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlc_python-0.3.8-cp39-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mlc_python-0.3.8-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 15894d0f1b53b2597f6303904b5b5ec53b5000b242d2f5792790de1d2db2fdd2
MD5 0ef1fe10d7724b3d16edfd29abdc4ab5
BLAKE2b-256 a507f515f2841d2eedbe7c962fe3955c906e5561b1cb26ede9cca9863d248b84

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlc_python-0.3.8-cp39-abi3-macosx_10_9_x86_64.whl:

Publisher: release.yml on mlc-ai/mlc-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page