Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ on:

jobs:
Examples:
runs-on: ubuntu-latest
strategy:
matrix:
arch: [x86_64, AArch64]

runs-on: ${{ matrix.arch == 'AArch64' && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }}
steps:
- uses: actions/checkout@v5

Expand All @@ -16,21 +20,11 @@ jobs:

- name: Install the project and its dependencies
run: |
uv sync
uv sync --extra ingress-torch-cpu

- name: Run MLP From File
run: |-
uv run python/examples/ingress/torch/mlp_from_file.py
sudo apt-get install -y llvm-dev # Obtain FileCheck, used in testing.
uv sync --extra ingress-torch-cpu --group test

- name: Run MLP From Module
run: |-
uv run python/examples/ingress/torch/mlp_from_model.py

- name: Run Compile And Run
run: |-
uv run python/examples/mlir/compile_and_run.py
- name: Run lit-enabled examples as tests
run: |
export FILECHECKPATH=FileCheck-18 # Ubuntu's llvm-dev appends a version number.
uv run lit python/examples # Makes sure to substitute FileCheck for $FILECHECKPATH

- name: Run apply basic schedule to basic payload
run: |-
uv run python/examples/schedule/transform_a_payload_according_to_a_schedule.py
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ pip install .[ingress_torch_cpu] \
--extra-index-url https://download.pytorch.org/whl \
--only-binary :all:
```

## Running tests

Running the tests is as simple as `lit .` in the root of the project.

We assume that the [`FileCheck`](https://llvm.org/docs/CommandGuide/FileCheck.html) and [`lit`](https://llvm.org/docs/CommandGuide/lit.html) executables are available on the `PATH`.
To obtain the [Python package for `lit`](https://pypi.org/project/lit/), simply run `uv sync --group test`.
16 changes: 16 additions & 0 deletions lit.cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

import lit.formats
from lit.TestingConfig import TestingConfig

# Imagine that, all your variables defined and with types!
assert isinstance(config := eval("config"), TestingConfig)

config.name = "Lighthouse test suite"
config.test_format = lit.formats.ShTest(True)
config.test_source_root = os.path.dirname(__file__)
config.test_exec_root = os.path.dirname(__file__) + "/lit.out"

config.substitutions.append(("%PYTHON", "uv run"))
if filecheck_path := os.environ.get("FILECHECKPATH"):
config.substitutions.append(("FileCheck", filecheck_path))
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ dependencies = [
"mlir-python-bindings==20251118+99630eb1b"
]

[dependency-groups]
test = [
"lit==18.1.8" # Tool to configure, discover and run tests
]

[project.optional-dependencies]
ingress_torch_mlir = [
"torch-mlir==20251122.639"
Expand Down Expand Up @@ -66,6 +71,7 @@ name = "torch_mlir"
url = "https://github.com/llvm/torch-mlir-release/releases/expanded_assets/dev-wheels"
explicit = true

# Derive package version from version in code
[build-system]
requires = ["setuptools>=68", "wheel"]
build-backend = "setuptools.build_meta"
Expand Down
2 changes: 2 additions & 0 deletions python/examples/ingress/torch/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.excludes = ["MLPModel"]

2 changes: 2 additions & 0 deletions python/examples/ingress/torch/mlp_from_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# RUN: %PYTHON %s

"""
Example demonstrating how to load a PyTorch model to MLIR using Lighthouse
without initializing the model class on the user's side.
Expand Down
2 changes: 2 additions & 0 deletions python/examples/ingress/torch/mlp_from_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# RUN: %PYTHON %s

"""
Example demonstrating how to load an already initialized PyTorch model
to MLIR using Lighthouse.
Expand Down
2 changes: 2 additions & 0 deletions python/examples/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.suffixes = {'.py'}

2 changes: 2 additions & 0 deletions python/examples/mlir/compile_and_run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# RUN: %PYTHON %s

import torch
import argparse

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# RUN: %PYTHON %s | FileCheck %s

# Simply demonstrates applying a schedule to a payload.
# To do so generates a basic payload and a basic schedule, purely as an example.

Expand All @@ -24,16 +26,23 @@ def example_payload() -> Module:
with InsertionPoint(payload.body):
matrixType = RankedTensorType.get([16, 16], F32Type.get())

# NB: Do the CHECKing on the transformed output:
# CHECK-LABEL: result of applying schedule to payload
# CHECK: func.func @fold_add_on_two_matmuls
# CHECK-SAME: (%[[MATRIX_A:.*]]: {{.*}}, %[[MATRIX_B:.*]]: {{.*}}, %[[WEIGHTS:.*]]: {{.*}})
@func.func(matrixType, matrixType, matrixType)
def fold_add_on_two_matmuls(matrixA, matrixB, weights):
empty = tensor.empty(matrixType.shape, matrixType.element_type)
c0 = arith.constant(F32Type.get(), 0.0)
# CHECK: %[[ZERO_INIT:.*]] = linalg.fill
zero_init = linalg.fill(c0, outs=[empty])
# CHECK: %[[A_X_WEIGHTS:.*]] = linalg.matmul ins(%[[MATRIX_A]], %[[WEIGHTS]]{{.*}}) outs(%[[ZERO_INIT]]
A_x_weights = linalg.matmul(matrixA, weights, outs=[zero_init])
empty2 = tensor.empty(matrixType.shape, matrixType.element_type)
zero_init2 = linalg.fill(c0, outs=[empty2])
B_x_weights = linalg.matmul(matrixB, weights, outs=[zero_init2])
# CHECK: %[[RES:.*]] = linalg.matmul ins(%[[MATRIX_B]], %[[WEIGHTS]]{{.*}}) outs(%[[A_X_WEIGHTS]]
B_x_weights = linalg.matmul(matrixB, weights, outs=[zero_init])
# CHECK-NOT: linalg.add
added = linalg.add(A_x_weights, B_x_weights, outs=[empty])
# CHECK: return %[[RES]]
return added

print(payload)
Expand Down