# Define formats for storing the sparse matrix and dense vectors. csr = pt.format([dense, compressed]) dv = pt.format([dense]) # Load a sparse matrix stored in the matrix market format) and store it # as a CSR matrix. The matrix in this test is a reduced version of the data # downloaded from here: # https://www.cise.ufl.edu/research/sparse/MM/Boeing/pwtk.tar.gz # In order to run the program using the matrix above, you can download the # matrix and replace this path to the actual path to the file. A = pt.read(os.path.join(_SCRIPT_PATH, "data/pwtk.mtx"), csr) # These two lines have been modified from the original program to use static # data to support result comparison. x = pt.from_array(np.full((A.shape[1], ), 1, dtype=np.float64)) z = pt.from_array(np.full((A.shape[0], ), 2, dtype=np.float64)) # Declare the result to be a dense vector y = pt.tensor([A.shape[0]], dv) # Declare index vars i, j = pt.get_index_vars(2) # Define the SpMV computation y[i] = A[i, j] * x[j] + z[i] ########################################################################## # Perform the SpMV computation and write the result to file with tempfile.TemporaryDirectory() as test_dir:
import filecmp import numpy as np import os import sys import tempfile _SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) sys.path.append(_SCRIPT_PATH) from tools import mlir_pytaco_api as pt from tools import testing_utils as utils i, j, k = pt.get_index_vars(3) # Set up dense matrices. A = pt.from_array(np.full((8, 8), 2.0, dtype=np.float32)) B = pt.from_array(np.full((8, 8), 3.0, dtype=np.float32)) # Set up sparse matrices. S = pt.tensor([8, 8], pt.format([pt.compressed, pt.compressed])) X = pt.tensor([8, 8], pt.format([pt.compressed, pt.compressed])) Y = pt.tensor([8, 8], pt.compressed) # alternative syntax works too S.insert([0, 7], 42.0) # Define the SDDMM kernel. Since this performs the reduction as # sum(k, S[i, j] * A[i, k] * B[k, j]) # we only compute the intermediate dense matrix product that are actually # needed to compute the result, with proper asymptotic complexity. X[i, j] = S[i, j] * A[i, k] * B[k, j]
import filecmp import numpy as np import os import sys import tempfile _SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) sys.path.append(_SCRIPT_PATH) from tools import mlir_pytaco_api as pt from tools import testing_utils as utils i, j, k = pt.get_index_vars(3) # Set up dense matrices. A = pt.from_array(np.full((8, 8), 2.0)) B = pt.from_array(np.full((8, 8), 3.0)) # Set up sparse matrices. S = pt.tensor([8, 8], pt.format([pt.compressed, pt.compressed])) X = pt.tensor([8, 8], pt.format([pt.compressed, pt.compressed])) Y = pt.tensor([8, 8], pt.compressed) # alternative syntax works too S.insert([0, 7], 42.0) # Define the SDDMM kernel. Since this performs the reduction as # sum(k, S[i, j] * A[i, k] * B[k, j]) # we only compute the intermediate dense matrix product that are actually # needed to compute the result, with proper asymptotic complexity. X[i, j] = S[i, j] * A[i, k] * B[k, j]
dense = pt.dense # Define formats for storing the sparse tensor and dense matrices. csf = pt.format([compressed, compressed, compressed]) rm = pt.format([dense, dense]) # Load a sparse three-dimensional tensor from file (stored in the FROSTT # format) and store it as a compressed sparse fiber tensor. We use a small # tensor for the purpose of testing. To run the program using the data from # the real application, please download the data from: # http://frostt.io/tensors/nell-2/ B = pt.read(os.path.join(_SCRIPT_PATH, "data/nell-2.tns"), csf) # These two lines have been modified from the original program to use static # data to support result comparison. C = pt.from_array(np.full((B.shape[1], 25), 1, dtype=np.float64)) D = pt.from_array(np.full((B.shape[2], 25), 2, dtype=np.float64)) # Declare the result to be a dense matrix. A = pt.tensor([B.shape[0], 25], rm) # Declare index vars. i, j, k, l = pt.get_index_vars(4) # Define the MTTKRP computation. A[i, j] = B[i, k, l] * D[l, j] * C[k, j] ########################################################################## # Perform the MTTKRP computation and write the result to file. with tempfile.TemporaryDirectory() as test_dir:
# RUN: SUPPORTLIB=%mlir_runner_utils_dir/libmlir_c_runner_utils%shlibext %PYTHON %s | FileCheck %s import numpy as np import os import sys _SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) sys.path.append(_SCRIPT_PATH) from tools import mlir_pytaco_api as pt i, j = pt.get_index_vars(2) # Both tensors are true dense tensors. A = pt.from_array(np.full([2, 3], 1, dtype=np.float64)) B = pt.from_array(np.full([2, 3], 2, dtype=np.float64)) # Define the result tensor as a true dense tensor. The parameter is_dense=True # is an MLIR-PyTACO extension. C = pt.tensor([2, 3], dtype=pt.float64, is_dense=True) C[i, j] = A[i, j] + B[i, j] # CHECK: [3. 3. 3. 3. 3. 3.] print(C.to_array().reshape(6))