1// RUN: mlir-opt %s --sparse-compiler | \ 2// RUN: TENSOR0="%mlir_integration_test_dir/data/mttkrp_b.tns" \ 3// RUN: mlir-cpu-runner \ 4// RUN: -e entry -entry-point-result=void \ 5// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 6// RUN: FileCheck %s 7// 8// Do the same run, but now with SIMDization as well. This should not change the outcome. 9// 10// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=4" | \ 11// RUN: TENSOR0="%mlir_integration_test_dir/data/mttkrp_b.tns" \ 12// RUN: mlir-cpu-runner \ 13// RUN: -e entry -entry-point-result=void \ 14// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 15// RUN: FileCheck %s 16 17!Filename = !llvm.ptr<i8> 18 19#SparseTensor = #sparse_tensor.encoding<{ 20 dimLevelType = [ "compressed", "compressed", "compressed" ] 21}> 22 23#mttkrp = { 24 indexing_maps = [ 25 affine_map<(i,j,k,l) -> (i,k,l)>, // B 26 affine_map<(i,j,k,l) -> (k,j)>, // C 27 affine_map<(i,j,k,l) -> (l,j)>, // D 28 affine_map<(i,j,k,l) -> (i,j)> // A (out) 29 ], 30 iterator_types = ["parallel", "parallel", "reduction", "reduction"], 31 doc = "A(i,j) += B(i,k,l) * D(l,j) * C(k,j)" 32} 33 34// 35// Integration test that lowers a kernel annotated as sparse to 36// actual sparse code, initializes a matching sparse storage scheme 37// from file, and runs the resulting code with the JIT compiler. 38// 39module { 40 // 41 // Computes Matricized Tensor Times Khatri-Rao Product (MTTKRP) kernel. See 42 // http://tensor-compiler.org/docs/data_analytics/index.html. 43 // 44 func.func @kernel_mttkrp(%argb: tensor<?x?x?xf64, #SparseTensor>, 45 %argc: tensor<?x?xf64>, 46 %argd: tensor<?x?xf64>, 47 %arga: tensor<?x?xf64>) 48 -> tensor<?x?xf64> { 49 %0 = linalg.generic #mttkrp 50 ins(%argb, %argc, %argd: 51 tensor<?x?x?xf64, #SparseTensor>, tensor<?x?xf64>, tensor<?x?xf64>) 52 outs(%arga: tensor<?x?xf64>) { 53 ^bb(%b: f64, %c: f64, %d: f64, %a: f64): 54 %0 = arith.mulf %b, %c : f64 55 %1 = arith.mulf %d, %0 : f64 56 %2 = arith.addf %a, %1 : f64 57 linalg.yield %2 : f64 58 } -> tensor<?x?xf64> 59 return %0 : tensor<?x?xf64> 60 } 61 62 func.func private @getTensorFilename(index) -> (!Filename) 63 64 // 65 // Main driver that reads matrix from file and calls the sparse kernel. 66 // 67 func.func @entry() { 68 %f0 = arith.constant 0.0 : f64 69 %cst0 = arith.constant 0 : index 70 %cst1 = arith.constant 1 : index 71 %cst2 = arith.constant 2 : index 72 73 // Read the sparse input tensor B from a file. 74 %fileName = call @getTensorFilename(%cst0) : (index) -> (!Filename) 75 %b = sparse_tensor.new %fileName 76 : !Filename to tensor<?x?x?xf64, #SparseTensor> 77 78 // Get sizes from B, pick a fixed size for dim-2 of A. 79 %isz = tensor.dim %b, %cst0 : tensor<?x?x?xf64, #SparseTensor> 80 %jsz = arith.constant 5 : index 81 %ksz = tensor.dim %b, %cst1 : tensor<?x?x?xf64, #SparseTensor> 82 %lsz = tensor.dim %b, %cst2 : tensor<?x?x?xf64, #SparseTensor> 83 84 // Initialize dense input matrix C. 85 %c0 = bufferization.alloc_tensor(%ksz, %jsz) : tensor<?x?xf64> 86 %c = scf.for %k = %cst0 to %ksz step %cst1 iter_args(%c1 = %c0) -> tensor<?x?xf64> { 87 %c2 = scf.for %j = %cst0 to %jsz step %cst1 iter_args(%c3 = %c1) -> tensor<?x?xf64> { 88 %k0 = arith.muli %k, %jsz : index 89 %k1 = arith.addi %k0, %j : index 90 %k2 = arith.index_cast %k1 : index to i32 91 %kf = arith.sitofp %k2 : i32 to f64 92 %c4 = tensor.insert %kf into %c3[%k, %j] : tensor<?x?xf64> 93 scf.yield %c4 : tensor<?x?xf64> 94 } 95 scf.yield %c2 : tensor<?x?xf64> 96 } 97 98 // Initialize dense input matrix D. 99 %d0 = bufferization.alloc_tensor(%lsz, %jsz) : tensor<?x?xf64> 100 %d = scf.for %l = %cst0 to %lsz step %cst1 iter_args(%d1 = %d0) -> tensor<?x?xf64> { 101 %d2 = scf.for %j = %cst0 to %jsz step %cst1 iter_args(%d3 = %d1) -> tensor<?x?xf64> { 102 %k0 = arith.muli %l, %jsz : index 103 %k1 = arith.addi %k0, %j : index 104 %k2 = arith.index_cast %k1 : index to i32 105 %kf = arith.sitofp %k2 : i32 to f64 106 %d4 = tensor.insert %kf into %d3[%l, %j] : tensor<?x?xf64> 107 scf.yield %d4 : tensor<?x?xf64> 108 } 109 scf.yield %d2 : tensor<?x?xf64> 110 } 111 112 // Initialize dense output matrix A. 113 %a0 = bufferization.alloc_tensor(%isz, %jsz) : tensor<?x?xf64> 114 %a = scf.for %i = %cst0 to %isz step %cst1 iter_args(%a1 = %a0) -> tensor<?x?xf64> { 115 %a2 = scf.for %j = %cst0 to %jsz step %cst1 iter_args(%a3 = %a1) -> tensor<?x?xf64> { 116 %a4 = tensor.insert %f0 into %a3[%i, %j] : tensor<?x?xf64> 117 scf.yield %a4 : tensor<?x?xf64> 118 } 119 scf.yield %a2 : tensor<?x?xf64> 120 } 121 122 // Call kernel. 123 %0 = call @kernel_mttkrp(%b, %c, %d, %a) 124 : (tensor<?x?x?xf64, #SparseTensor>, 125 tensor<?x?xf64>, tensor<?x?xf64>, tensor<?x?xf64>) -> tensor<?x?xf64> 126 127 // Print the result for verification. 128 // 129 // CHECK: ( ( 16075, 21930, 28505, 35800, 43815 ), 130 // CHECK: ( 10000, 14225, 19180, 24865, 31280 ) ) 131 // 132 %v = vector.transfer_read %0[%cst0, %cst0], %f0 133 : tensor<?x?xf64>, vector<2x5xf64> 134 vector.print %v : vector<2x5xf64> 135 136 // Release the resources. 137 bufferization.dealloc_tensor %b : tensor<?x?x?xf64, #SparseTensor> 138 139 return 140 } 141} 142