Skip to content

Commit 35ece3b

Browse files
committed
[mlir][sparse][pytaco] add PyTACO SpMM example
Also contains a few TODOs on future enhancements Reviewed By: bixia Differential Revision: https://reviews.llvm.org/D118418
1 parent 843c12d commit 35ece3b

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%%MatrixMarket matrix coordinate real general
2+
3 3 9
3+
1 1 1.0
4+
1 2 2.0
5+
1 3 4.0
6+
2 1 4.0
7+
2 2 5.0
8+
2 3 6.0
9+
3 1 7.0
10+
3 2 8.0
11+
3 3 9.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%%MatrixMarket matrix coordinate real general
2+
3 3 9
3+
1 1 10.0
4+
1 2 11.0
5+
1 3 12.0
6+
2 1 13.0
7+
2 2 14.0
8+
2 3 15.0
9+
3 1 16.0
10+
3 2 17.0
11+
3 3 18.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1.0 1.0 100.0
2+
1.0 2.0 107.0
3+
1.0 3.0 114.0
4+
2.0 1.0 201.0
5+
2.0 2.0 216.0
6+
2.0 3.0 231.0
7+
3.0 1.0 318.0
8+
3.0 2.0 342.0
9+
3.0 3.0 366.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# RUN: SUPPORTLIB=%mlir_runner_utils_dir/libmlir_c_runner_utils%shlibext %PYTHON %s | FileCheck %s
2+
3+
import filecmp
4+
import numpy as np
5+
import os
6+
import sys
7+
import tempfile
8+
9+
_SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
10+
sys.path.append(_SCRIPT_PATH)
11+
12+
from tools import mlir_pytaco_api as pt
13+
14+
# Define the CSR format.
15+
#
16+
# TODO: accept "csr = pt.format([pt.dense, pt.compressed], [0, 1])"
17+
#
18+
csr = pt.format([pt.dense, pt.compressed])
19+
20+
# Read matrices A and B from file, infer size of output matrix C.
21+
A = pt.read(os.path.join(_SCRIPT_PATH, "data/A.mtx"), csr)
22+
B = pt.read(os.path.join(_SCRIPT_PATH, "data/B.mtx"), csr)
23+
C = pt.tensor((A.shape[0], B.shape[1]), csr)
24+
25+
# Define the kernel.
26+
i, j, k = pt.get_index_vars(3)
27+
C[i, j] = A[i, k] * B[k, j]
28+
29+
# Force evaluation of the kernel by writing out C.
30+
#
31+
# TODO: use sparse_tensor.out for output, so that C.tns becomes
32+
# a file in extended FROSTT format
33+
#
34+
with tempfile.TemporaryDirectory() as test_dir:
35+
golden_file = os.path.join(_SCRIPT_PATH, "data/gold_C.tns")
36+
out_file = os.path.join(test_dir, "C.tns")
37+
pt.write(out_file, C)
38+
#
39+
# CHECK: Compare files True
40+
#
41+
print(f"Compare files {filecmp.cmp(golden_file, out_file)}")

0 commit comments

Comments
 (0)