Skip to content

Commit b7fd91c

Browse files
committed
Upstream MLIR PyTACO implementation.
Add TACO tests to test/Integration/Dialect/SparseTensor/taco. Add the MLIR PyTACO implementation as tools under the directory. Reviewed By: aartbik, mehdi_amini Differential Revision: https://reviews.llvm.org/D117260
1 parent cab9616 commit b7fd91c

14 files changed

+2379
-0
lines changed

mlir/python/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
numpy
22
pybind11>=2.8.0
33
PyYAML
4+
dataclasses
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# MLIR-PyTACO: Implementing PyTACO with MLIR
2+
3+
TACO (http://tensor-compiler.org/) is a tensor algebra compiler. TACO defines
4+
PyTACO, a domain specific language in Python, for writing tensor algebra
5+
applications.
6+
7+
This directory contains the implementation of PyTACO using MLIR. In particular,
8+
we implement a Python layer that accepts the PyTACO language, generates MLIR
9+
linalg.generic OPs with sparse tensor annotation to represent the tensor
10+
computation, and invokes the MLIR sparse tensor code generator
11+
(https://mlir.llvm.org/docs/Dialects/SparseTensorOps/) as well as other MLIR
12+
compilation passes to generate an executable. Then, we invoke the MLIR execution
13+
engine to execute the program and pass the result back to the Python layer.
14+
15+
As can be seen from the tests in this directory, in order to port a PyTACO
16+
program to MLIR-PyTACO, we basically only need to replace this line that imports
17+
PyTACO:
18+
19+
```python
20+
import pytaco as pt
21+
```
22+
23+
with this line to import MLIR-PyTACO:
24+
25+
```python
26+
from tools import mlir_pytaco_api as pt
27+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
1 1 12
2+
1 2 12
3+
1 3 12
4+
1 4 12
5+
1 5 12
6+
1 6 12
7+
1 7 12
8+
1 8 12
9+
1 9 12
10+
1 10 12
11+
1 11 12
12+
1 12 12
13+
1 13 12
14+
1 14 12
15+
1 15 12
16+
1 16 12
17+
1 17 12
18+
1 18 12
19+
1 19 12
20+
1 20 12
21+
1 21 12
22+
1 22 12
23+
1 23 12
24+
1 24 12
25+
1 25 12
26+
2 1 6
27+
2 2 6
28+
2 3 6
29+
2 4 6
30+
2 5 6
31+
2 6 6
32+
2 7 6
33+
2 8 6
34+
2 9 6
35+
2 10 6
36+
2 11 6
37+
2 12 6
38+
2 13 6
39+
2 14 6
40+
2 15 6
41+
2 16 6
42+
2 17 6
43+
2 18 6
44+
2 19 6
45+
2 20 6
46+
2 21 6
47+
2 22 6
48+
2 23 6
49+
2 24 6
50+
2 25 6
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# See http://frostt.io/tensors/file-formats.html for FROSTT (.tns) format
2+
1 37102
3+
2 -20.4138
4+
3 804927
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1 1 1 1.0
2+
1 2 2 2.0
3+
1 3 4 3.0
4+
2 1 1 1.0
5+
2 4 3 2.0
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%%MatrixMarket matrix coordinate real symmetric
2+
%-------------------------------------------------------------------------------
3+
% To download a matrix for a real world application
4+
% https://math.nist.gov/MatrixMarket/
5+
%-------------------------------------------------------------------------------
6+
3 3 5
7+
1 1 37423.0879671
8+
2 1 -22.4050781162
9+
3 1 -300.654980157
10+
3 2 -.00869762944058
11+
3 3 805225.750212
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# RUN: SUPPORTLIB=%mlir_runner_utils_dir/libmlir_c_runner_utils%shlibext %PYTHON %s | FileCheck %s
2+
3+
import numpy as np
4+
import os
5+
import sys
6+
import tempfile
7+
8+
_SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
9+
sys.path.append(_SCRIPT_PATH)
10+
from tools import mlir_pytaco_api as pt
11+
12+
###### This PyTACO part is taken from the TACO open-source project. ######
13+
# See http://tensor-compiler.org/docs/data_analytics/index.html.
14+
15+
compressed = pt.compressed
16+
dense = pt.dense
17+
18+
# Define formats for storing the sparse tensor and dense matrices.
19+
csf = pt.format([compressed, compressed, compressed])
20+
rm = pt.format([dense, dense])
21+
22+
# Load a sparse three-dimensional tensor from file (stored in the FROSTT
23+
# format) and store it as a compressed sparse fiber tensor. We use a small
24+
# tensor for the purpose of testing. To run the program using the data from
25+
# the real application, please download the data from:
26+
# http://frostt.io/tensors/nell-2/
27+
B = pt.read(os.path.join(_SCRIPT_PATH, "data/nell-2.tns"), csf)
28+
29+
# These two lines have been modified from the original program to use static
30+
# data to support result comparison.
31+
C = pt.from_array(np.full((B.shape[1], 25), 1, dtype=np.float64))
32+
D = pt.from_array(np.full((B.shape[2], 25), 2, dtype=np.float64))
33+
34+
# Declare the result to be a dense matrix.
35+
A = pt.tensor([B.shape[0], 25], rm)
36+
37+
# Declare index vars.
38+
i, j, k, l = pt.get_index_vars(4)
39+
40+
# Define the MTTKRP computation.
41+
A[i, j] = B[i, k, l] * D[l, j] * C[k, j]
42+
43+
##########################################################################
44+
45+
# CHECK: Compare result True
46+
# Perform the MTTKRP computation and write the result to file.
47+
with tempfile.TemporaryDirectory() as test_dir:
48+
actual_file = os.path.join(test_dir, "A.tns")
49+
pt.write(actual_file, A)
50+
actual = np.loadtxt(actual_file, np.float64)
51+
expected = np.loadtxt(
52+
os.path.join(_SCRIPT_PATH, "data/gold_A.tns"), np.float64)
53+
print(f"Compare result {np.allclose(actual, expected, rtol=0.01)}")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# RUN: SUPPORTLIB=%mlir_runner_utils_dir/libmlir_c_runner_utils%shlibext %PYTHON %s | FileCheck %s
2+
3+
import numpy as np
4+
import os
5+
import sys
6+
import tempfile
7+
8+
_SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
9+
sys.path.append(_SCRIPT_PATH)
10+
from tools import mlir_pytaco_api as pt
11+
12+
###### This PyTACO part is taken from the TACO open-source project. ######
13+
# See http://tensor-compiler.org/docs/scientific_computing/index.html.
14+
15+
compressed = pt.compressed
16+
dense = pt.dense
17+
18+
# Define formats for storing the sparse matrix and dense vectors.
19+
csr = pt.format([dense, compressed])
20+
dv = pt.format([dense])
21+
22+
# Load a sparse matrix stored in the matrix market format) and store it
23+
# as a CSR matrix. The matrix in this test is a reduced version of the data
24+
# downloaded from here:
25+
# https://www.cise.ufl.edu/research/sparse/MM/Boeing/pwtk.tar.gz
26+
# In order to run the program using the matrix above, you can download the
27+
# matrix and replace this path to the actual path to the file.
28+
A = pt.read(os.path.join(_SCRIPT_PATH, "data/pwtk.mtx"), csr)
29+
30+
# These two lines have been modified from the original program to use static
31+
# data to support result comparison.
32+
x = pt.from_array(np.full((A.shape[1],), 1, dtype=np.float64))
33+
z = pt.from_array(np.full((A.shape[0],), 2, dtype=np.float64))
34+
35+
# Declare the result to be a dense vector
36+
y = pt.tensor([A.shape[0]], dv)
37+
38+
# Declare index vars
39+
i, j = pt.get_index_vars(2)
40+
41+
# Define the SpMV computation
42+
y[i] = A[i, j] * x[j] + z[i]
43+
44+
##########################################################################
45+
46+
# CHECK: Compare result True
47+
# Perform the SpMV computation and write the result to file
48+
with tempfile.TemporaryDirectory() as test_dir:
49+
actual_file = os.path.join(test_dir, "y.tns")
50+
pt.write(actual_file, y)
51+
actual = np.loadtxt(actual_file, np.float64)
52+
expected = np.loadtxt(
53+
os.path.join(_SCRIPT_PATH, "data/gold_y.tns"), np.float64)
54+
print(f"Compare result {np.allclose(actual, expected, rtol=0.01)}")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# RUN: SUPPORTLIB=%mlir_runner_utils_dir/libmlir_c_runner_utils%shlibext %PYTHON %s | FileCheck %s
2+
3+
import os
4+
import sys
5+
6+
_SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
7+
sys.path.append(_SCRIPT_PATH)
8+
from tools import mlir_pytaco_api as pt
9+
10+
compressed = pt.compressed
11+
dense = pt.dense
12+
13+
# Ensure that we can run an unmodified PyTACO program with a simple tensor
14+
# algebra expression using tensor index notation, and produce the expected
15+
# result.
16+
i, j = pt.get_index_vars(2)
17+
A = pt.tensor([2, 3])
18+
B = pt.tensor([2, 3])
19+
C = pt.tensor([2, 3])
20+
D = pt.tensor([2, 3], dense)
21+
A.insert([0, 1], 10)
22+
A.insert([1, 2], 40)
23+
B.insert([0, 0], 20)
24+
B.insert([1, 2], 30)
25+
C.insert([0, 1], 5)
26+
C.insert([1, 2], 7)
27+
D[i, j] = A[i, j] + B[i, j] - C[i, j]
28+
29+
# CHECK: [20. 5. 0. 0. 0. 63.]
30+
print(D.to_array().reshape(6))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Files in this directory are tools, not tests.
2+
config.unsupported = True

0 commit comments

Comments
 (0)