Skip to content

Commit 864ba3f

Browse files
authored
Merge pull request #15 from jim22k/use_built_bindings
Use mlir-python-bindings package instead of manually built bindings
2 parents 1006545 + 14bfb8d commit 864ba3f

10 files changed

+109
-284
lines changed

README.md

-64
Original file line numberDiff line numberDiff line change
@@ -49,67 +49,3 @@ array([0, 1], dtype=uint64)
4949
>>> values
5050
array([ 8. , 33.625])
5151
```
52-
53-
# Building MLIR
54-
55-
Until LLVM 16.0 is released, the required MLIR operations in the sparse_tensor dialect will only be
56-
available on the main repo branch, meaning there aren't any packages readily available to install
57-
from conda-forge.
58-
59-
To build locally, download the LLVM repo from GitHub.
60-
61-
```
62-
git clone https://github.com/llvm/llvm-project.git
63-
```
64-
65-
Next create a conda environment for building the project.
66-
67-
```
68-
conda create -n mlir_build_env -y
69-
conda activate mlir_build_env
70-
conda install python=3.10 numpy pyyaml cmake ninja pybind11 python-mlir-graphblas
71-
```
72-
73-
Define `PREFIX` as the location of your environment (active env location when running `conda info`).
74-
Then run cmake.
75-
76-
```
77-
export PREFIX=/location/to/your/conda/environment
78-
79-
cd llvm-project
80-
mkdir build
81-
cd build
82-
83-
cmake -G Ninja ../llvm \
84-
-DCMAKE_INSTALL_PREFIX=$PREFIX \
85-
-DLLVM_ENABLE_PROJECTS=mlir \
86-
-DLLVM_BUILD_EXAMPLES=ON \
87-
-DLLVM_INSTALL_UTILS=ON \
88-
-DLLVM_TARGETS_TO_BUILD="X86;AArch64;NVPTX;AMDGPU" \
89-
-DCMAKE_BUILD_TYPE=Release \
90-
-DLLVM_ENABLE_ASSERTIONS=ON \
91-
-DLLVM_BUILD_LLVM_DYLIB=ON \
92-
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
93-
-DPython3_EXECUTABLE=`which python`
94-
```
95-
96-
If building on a Mac, perform this additional step.
97-
98-
```
99-
cp $PREFIX/lib/libtinfo* lib/
100-
cp $PREFIX/lib/libz* lib/
101-
```
102-
103-
Then build the project
104-
105-
```
106-
cmake --build .
107-
cmake --install .
108-
```
109-
110-
Finally, set `LLVM_BUILD_DIR` to point to the current build directory.
111-
Now python-mlir-graphblas should be usable. Verify by running tests.
112-
113-
```
114-
LLVM_BUILD_DIR=. pytest --pyargs mlir_graphblas
115-
```

dev-environment.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# To use:
2+
# $ conda env create -f dev-environment.yml
3+
# $ conda activate mlir-graphblas-dev
4+
5+
name: mlir-graphblas-dev
6+
channels:
7+
- conda-forge
8+
- nodefaults # Only from conda-forge for faster solving
9+
dependencies:
10+
- python
11+
- mlir-python-bindings >=16.0
12+
- pytest

mlir_graphblas/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# TODO: remove this once mlir-python-bindings can be properly installed
2-
import os
3-
import sys
4-
sys.path.append(f"{os.environ['LLVM_BUILD_DIR']}/tools/mlir/python_packages/mlir_core")
1+
try:
2+
import mlir
3+
import mlir.ir
4+
except ImportError:
5+
raise ImportError("Missing mlir-python-bindings")
56

67
from .operations import *
78
from .operators import *

mlir_graphblas/compiler.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from mlir import ir
33
from mlir import passmanager
44

5-
from .utils import LIBMLIR_C_RUNNER_UTILS
5+
from .utils import MLIR_C_RUNNER_UTILS
66

77

88
engine_cache = {}
@@ -12,8 +12,7 @@ def compile(module: ir.Module, pipeline=None, *, opt_level=2, shared_libs=None):
1212
if pipeline is None:
1313
pipeline = 'builtin.module(sparse-compiler{reassociate-fp-reductions=1 enable-index-optimizations=1})'
1414
if shared_libs is None:
15-
shared_libs = [LIBMLIR_C_RUNNER_UTILS]
15+
shared_libs = [MLIR_C_RUNNER_UTILS]
1616
# print(module)
1717
passmanager.PassManager.parse(pipeline).run(module)
18-
return execution_engine.ExecutionEngine(
19-
module, opt_level=opt_level, shared_libs=shared_libs)
18+
return execution_engine.ExecutionEngine(module, opt_level=opt_level, shared_libs=shared_libs)

mlir_graphblas/implementations.py

-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,6 @@ def main(x, y):
358358

359359
def ewise_add(out_type: DType, op: BinaryOp, left: SparseTensorBase, right: SparseTensorBase):
360360
assert left.ndims == right.ndims
361-
assert left.dtype == right.dtype
362361

363362
if left._obj is None:
364363
if right.dtype == out_type:

mlir_graphblas/tests/test_operations.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,15 @@ def test_mxm(mm):
177177
# rowwise @ colwise
178178
z.clear()
179179
operations.mxm(z, Semiring.plus_times, x, ycol)
180-
matrix_compare(z, *expected)
180+
try:
181+
matrix_compare(z, *expected)
182+
except AssertionError:
183+
# Check for dense return, indicating lack of lex insert fix
184+
matrix_compare(z,
185+
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4],
186+
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
187+
[20.9, 0, 0, 0, 0, 16.5, 0, 0, 0, 0, 5.5, 0, 0, 0, 70.4, 0, 0, 0, 0, 0, 0, 0, 0, 13.2, 0])
188+
pytest.xfail("Waiting for lex insert fix")
181189
# colwise @ colwise
182190
z.clear()
183191
operations.mxm(z, Semiring.plus_times, xcol, ycol)

mlir_graphblas/tests/utils.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
def vector_compare(vec, i, v):
77
assert vec.ndims == 1
8+
assert len(i) == len(v), f"{len(i)} != {len(v)}"
89
idx, vals = vec.extract_tuples()
910
assert vals.dtype == vec.dtype.np_type
1011
np_assert_equal(idx, i)
@@ -13,12 +14,15 @@ def vector_compare(vec, i, v):
1314

1415
def matrix_compare(mat, r, c, v):
1516
assert mat.ndims == 2
17+
assert len(r) == len(c), f"{len(r)} != {len(c)}"
18+
assert len(r) == len(v), f"{len(r)} != {len(v)}"
1619
rows, cols, vals = mat.extract_tuples()
1720
assert vals.dtype == mat.dtype.np_type
21+
combo = np.core.records.fromarrays([r, c], names='r,c')
1822
if mat.is_rowwise():
19-
sort_order = np.argsort(r)
23+
sort_order = np.argsort(combo, order=['r', 'c'])
2024
else:
21-
sort_order = np.argsort(c)
25+
sort_order = np.argsort(combo, order=['c', 'r'])
2226
np_assert_equal(rows, np.array(r)[sort_order])
2327
np_assert_equal(cols, np.array(c)[sort_order])
2428
np_assert_allclose(vals, np.array(v)[sort_order])

mlir_graphblas/utils.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import ctypes
21
import numpy as np
2+
import ctypes
3+
from ctypes.util import find_library
34
from enum import Enum
45
from mlir import ir
56
from mlir.dialects.sparse_tensor import DimLevelType
@@ -8,13 +9,9 @@
89
GrbDimensionMismatch, GrbOutputNotEmpty, GrbIndexOutOfBounds, GrbEmptyObject
910
)
1011

11-
12+
MLIR_C_RUNNER_UTILS = find_library("mlir_c_runner_utils")
13+
c_lib = ctypes.CDLL(MLIR_C_RUNNER_UTILS)
1214
LLVMPTR = ctypes.POINTER(ctypes.c_char)
13-
# TODO: fix this once a proper package exists
14-
import os
15-
LIBMLIR_C_RUNNER_UTILS = f"{os.environ['LLVM_BUILD_DIR']}/lib/libmlir_c_runner_utils.dylib"
16-
17-
c_lib = ctypes.CDLL(LIBMLIR_C_RUNNER_UTILS)
1815

1916

2017
def get_sparse_output_pointer():

0 commit comments

Comments
 (0)