Skip to content

Commit af53648

Browse files
committed
[Examples][CI] Enable testing of examples through lit
Adds lit as a test dependency via pyproject.toml. Adds FileCheck as an external dependency -- for CI we use Ubuntu's llvm-dev package.
1 parent fa58880 commit af53648

File tree

9 files changed

+53
-20
lines changed

9 files changed

+53
-20
lines changed

.github/workflows/examples.yml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ on:
77

88
jobs:
99
Examples:
10-
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
arch: [x86_64, AArch64]
13+
14+
runs-on: ${{ matrix.arch == 'AArch64' && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }}
1115
steps:
1216
- uses: actions/checkout@v5
1317

@@ -16,21 +20,11 @@ jobs:
1620

1721
- name: Install the project and its dependencies
1822
run: |
19-
uv sync
20-
uv sync --extra ingress-torch-cpu
21-
22-
- name: Run MLP From File
23-
run: |-
24-
uv run python/examples/ingress/torch/mlp_from_file.py
23+
sudo apt-get install llvm-dev # Obtain FileCheck, used in testing.
24+
uv sync --extra ingress-torch-cpu --group test
2525
26-
- name: Run MLP From Module
27-
run: |-
28-
uv run python/examples/ingress/torch/mlp_from_model.py
29-
30-
- name: Run Compile And Run
31-
run: |-
32-
uv run python/examples/mlir/compile_and_run.py
26+
- name: Run lit-enabled examples as tests
27+
run: |
28+
export FILECHECKPATH=FileCheck-18 # Ubuntu's llvm-dev appends a version number.
29+
uv run lit python/examples # Makes sure to substitute FileCheck for $FILECHECKPATH
3330
34-
- name: Run apply basic schedule to basic payload
35-
run: |-
36-
uv run python/examples/schedule/transform_a_payload_according_to_a_schedule.py

lit.cfg.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
3+
import lit.formats
4+
from lit.TestingConfig import TestingConfig
5+
6+
# Imagine that, all your variables defined and with types!
7+
assert isinstance(config := eval("config"), TestingConfig)
8+
9+
config.name = "Lighthouse test suite"
10+
config.test_format = lit.formats.ShTest(True)
11+
config.test_source_root = os.path.dirname(__file__)
12+
config.test_exec_root = os.path.dirname(__file__) + "/lit.out"
13+
14+
config.substitutions.append(("%PYTHON", "uv run"))
15+
if filecheck_path := os.environ.get("FILECHECKPATH"):
16+
config.substitutions.append(("FileCheck", filecheck_path))

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ dependencies = [
66
"mlir-python-bindings==20251118+99630eb1b"
77
]
88

9+
[dependency-groups]
10+
test = [
11+
"lit==18.1.8" # Tool to configure, discover and run tests
12+
]
13+
914
[project.optional-dependencies]
1015
# Additional "targets" which pull in optional dependencies -- use `uv sync --extra TARGET`
1116
ingress_torch_cpu = [
@@ -63,6 +68,7 @@ name = "torch_mlir"
6368
url = "https://github.com/llvm/torch-mlir-release/releases/expanded_assets/dev-wheels"
6469
explicit = true
6570

71+
# Derive package version from version in code
6672
[build-system]
6773
requires = ["setuptools>=68", "wheel"]
6874
build-backend = "setuptools.build_meta"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config.excludes = ["MLPModel"]

python/examples/ingress/torch/mlp_from_file.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# RUN: %PYTHON %s
2+
13
"""
24
Example demonstrating how to load a PyTorch model to MLIR using Lighthouse
35
without initializing the model class on the user's side.

python/examples/ingress/torch/mlp_from_model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# RUN: %PYTHON %s
2+
13
"""
24
Example demonstrating how to load an already initialized PyTorch model
35
to MLIR using Lighthouse.

python/examples/lit.local.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config.suffixes = {'.py'}

python/examples/mlir/compile_and_run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# RUN: %PYTHON %s
2+
13
import torch
24
import argparse
35

python/examples/schedule/transform_a_payload_according_to_a_schedule.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# RUN: %PYTHON %s | FileCheck %s
2+
13
# Simply demonstrates applying a schedule to a payload.
24
# To do so generates a basic payload and a basic schedule, purely as an example.
35

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

29+
# NB: Do the CHECKing on the transformed output:
30+
# CHECK-LABEL: result of applying schedule to payload
31+
# CHECK: func.func @fold_add_on_two_matmuls
32+
# CHECK-SAME: (%[[MATRIX_A:.*]]: {{.*}}, %[[MATRIX_B:.*]]: {{.*}}, %[[WEIGHTS:.*]]: {{.*}})
2733
@func.func(matrixType, matrixType, matrixType)
2834
def fold_add_on_two_matmuls(matrixA, matrixB, weights):
2935
empty = tensor.empty(matrixType.shape, matrixType.element_type)
3036
c0 = arith.constant(F32Type.get(), 0.0)
37+
# CHECK: %[[ZERO_INIT:.*]] = linalg.fill
3138
zero_init = linalg.fill(c0, outs=[empty])
39+
# CHECK: %[[A_X_WEIGHTS:.*]] = linalg.matmul ins(%[[MATRIX_A]], %[[WEIGHTS]]{{.*}}) outs(%[[ZERO_INIT]]
3240
A_x_weights = linalg.matmul(matrixA, weights, outs=[zero_init])
33-
empty2 = tensor.empty(matrixType.shape, matrixType.element_type)
34-
zero_init2 = linalg.fill(c0, outs=[empty2])
35-
B_x_weights = linalg.matmul(matrixB, weights, outs=[zero_init2])
41+
# CHECK: %[[RES:.*]] = linalg.matmul ins(%[[MATRIX_B]], %[[WEIGHTS]]{{.*}}) outs(%[[A_X_WEIGHTS]]
42+
B_x_weights = linalg.matmul(matrixB, weights, outs=[zero_init])
43+
# CHECK-NOT: linalg.add
3644
added = linalg.add(A_x_weights, B_x_weights, outs=[empty])
45+
# CHECK: return %[[RES]]
3746
return added
3847

3948
print(payload)

0 commit comments

Comments
 (0)