1//-------------------------------------------------------------------------------------------------- 2// WHEN CREATING A NEW TEST, PLEASE JUST COPY & PASTE WITHOUT EDITS. 3// 4// Set-up that's shared across all tests in this directory. In principle, this 5// config could be moved to lit.local.cfg. However, there are downstream users that 6// do not use these LIT config files. Hence why this is kept inline. 7// 8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true 9// DEFINE: %{sparsifier_opts_sve} = enable-arm-sve=true %{sparsifier_opts} 10// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}" 11// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}" 12// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils 13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils 14// DEFINE: %{run_opts} = -e main -entry-point-result=void 15// DEFINE: %{run} = mlir-runner %{run_opts} %{run_libs} 16// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs_sve} 17// 18// DEFINE: %{env} = 19//-------------------------------------------------------------------------------------------------- 20 21// RUN: %{compile} | %{run} | FileCheck %s 22// 23// Do the same run, but now with direct IR generation. 24// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true 25// RUN: %{compile} | %{run} | FileCheck %s 26// 27// Do the same run, but now with direct IR generation and vectorization. 28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true vl=2 reassociate-fp-reductions=true enable-index-optimizations=true 29// RUN: %{compile} | %{run} | FileCheck %s 30// 31// Do the same run, but now with direct IR generation and VLA vectorization. 32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %} 33 34#SM = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : compressed, d1 : compressed) }> 35 36#trait_sampled_dense_dense = { 37 indexing_maps = [ 38 affine_map<(i,j,k) -> (i,j)>, // S 39 affine_map<(i,j,k) -> (i,k)>, // A 40 affine_map<(i,j,k) -> (k,j)>, // B 41 affine_map<(i,j,k) -> (i,j)> // X (out) 42 ], 43 iterator_types = ["parallel", "parallel", "reduction"], 44 doc = "X(i,j) += S(i,j) SUM_k A(i,k) B(k,j)" 45} 46 47#trait_matmul = { 48 indexing_maps = [ 49 affine_map<(d0, d1, d2) -> (d1, d0)>, 50 affine_map<(d0, d1, d2) -> (d0, d2)>, 51 affine_map<(d0, d1, d2) -> (d1, d2)> 52 ], 53 iterator_types = ["reduction", "parallel", "parallel"] 54} 55 56#trait_scale = { 57 indexing_maps = [ 58 affine_map<(d0, d1) -> (d0, d1)>, 59 affine_map<(d0, d1) -> (d0, d1)>, 60 affine_map<(d0, d1) -> (d0, d1)> 61 ], 62 iterator_types = ["parallel", "parallel"] 63} 64 65// 66// Integration test for sampled dense dense matmul fusion. 67// 68module { 69 // 70 // A kernel that computes a direct sampled matrix matrix multiplication 71 // (with dense result). 72 // 73 func.func @sampled_dd(%args: tensor<8x8xf64, #SM>, 74 %arga: tensor<8x8xf64>, 75 %argb: tensor<8x8xf64>) -> tensor<8x8xf64> { 76 %1 = arith.constant dense<0.0> : tensor<8x8xf64> 77 %2 = linalg.generic #trait_sampled_dense_dense 78 ins(%args, %arga, %argb: tensor<8x8xf64, #SM>, 79 tensor<8x8xf64>, tensor<8x8xf64>) 80 outs(%1: tensor<8x8xf64>) { 81 ^bb(%s: f64, %a: f64, %b: f64, %x: f64): 82 %p = arith.mulf %a, %b : f64 83 %q = arith.mulf %s, %p : f64 84 %r = arith.addf %x, %q : f64 85 linalg.yield %r : f64 86 } -> tensor<8x8xf64> 87 return %2 : tensor<8x8xf64> 88 } 89 90 // 91 // A kernel that computes an unfused sampled matrix matrix multiplication 92 // (with dense result). 93 // 94 func.func @sampled_dd_unfused(%args: tensor<8x8xf64, #SM>, 95 %arga: tensor<8x8xf64>, 96 %argb: tensor<8x8xf64>) -> tensor<8x8xf64> { 97 // Perform dense-dense matrix matrix multiplication. 98 %1 = arith.constant dense<0.0> : tensor<8x8xf64> 99 %2 = linalg.generic #trait_matmul 100 ins(%arga, %argb : tensor<8x8xf64>, tensor<8x8xf64>) 101 outs(%1 : tensor<8x8xf64>) { 102 ^bb0(%a: f64, %b: f64, %x: f64): 103 %p = arith.mulf %a, %b : f64 104 %q = arith.addf %x, %p : f64 105 linalg.yield %q : f64 106 } -> tensor<8x8xf64> 107 // Sample the result with elements-wise multiplication with sparse matrix. 108 %3 = linalg.generic #trait_scale 109 ins(%2, %args : tensor<8x8xf64>, tensor<8x8xf64, #SM>) 110 outs(%1 : tensor<8x8xf64>) { 111 ^bb0(%t: f64, %s: f64, %x: f64): 112 %r = arith.mulf %t, %s : f64 113 linalg.yield %r : f64 114 } -> tensor<8x8xf64> 115 bufferization.dealloc_tensor %2 : tensor<8x8xf64> 116 return %3 : tensor<8x8xf64> 117 } 118 119 // 120 // A kernel that computes a direct sampled matrix matrix multiplication 121 // (with sparse result). 122 // 123 func.func @sparse_sampled_dd(%args: tensor<8x8xf64, #SM>, 124 %arga: tensor<8x8xf64>, 125 %argb: tensor<8x8xf64>) -> tensor<8x8xf64, #SM> { 126 %1 = tensor.empty() : tensor<8x8xf64, #SM> 127 %2 = linalg.generic #trait_sampled_dense_dense 128 ins(%args, %arga, %argb: tensor<8x8xf64, #SM>, 129 tensor<8x8xf64>, tensor<8x8xf64>) 130 outs(%1: tensor<8x8xf64, #SM>) { 131 ^bb(%s: f64, %a: f64, %b: f64, %x: f64): 132 %p = arith.mulf %a, %b : f64 133 %q = arith.mulf %s, %p : f64 134 %r = arith.addf %x, %q : f64 135 linalg.yield %r : f64 136 } -> tensor<8x8xf64, #SM> 137 return %2 : tensor<8x8xf64, #SM> 138 } 139 140 // 141 // A kernel that computes an unfused sampled matrix matrix multiplication 142 // (with sparse result). 143 // 144 func.func @sparse_sampled_dd_unfused( 145 %args: tensor<8x8xf64, #SM>, 146 %arga: tensor<8x8xf64>, 147 %argb: tensor<8x8xf64>) -> tensor<8x8xf64, #SM> { 148 // Perform dense-dense matrix matrix multiplication. 149 %1 = arith.constant dense<0.0> : tensor<8x8xf64> 150 %2 = linalg.generic #trait_matmul 151 ins(%arga, %argb : tensor<8x8xf64>, tensor<8x8xf64>) 152 outs(%1 : tensor<8x8xf64>) { 153 ^bb0(%a: f64, %b: f64, %x: f64): 154 %p = arith.mulf %a, %b : f64 155 %q = arith.addf %x, %p : f64 156 linalg.yield %q : f64 157 } -> tensor<8x8xf64> 158 // Sample the result with elements-wise multiplication with sparse matrix. 159 %3 = tensor.empty() : tensor<8x8xf64, #SM> 160 %4 = linalg.generic #trait_scale 161 ins(%2, %args : tensor<8x8xf64>, tensor<8x8xf64, #SM>) 162 outs(%3 : tensor<8x8xf64, #SM>) { 163 ^bb0(%t: f64, %s: f64, %x: f64): 164 %r = arith.mulf %t, %s : f64 165 linalg.yield %r : f64 166 } -> tensor<8x8xf64, #SM> 167 return %4 : tensor<8x8xf64, #SM> 168 } 169 170 // 171 // Main driver. 172 // 173 func.func @main() { 174 %d0 = arith.constant 0.0 : f64 175 %c0 = arith.constant 0 : index 176 177 %t = arith.constant sparse<[[0, 0], [7,7]], [1.0, 2.0]> 178 : tensor<8x8xf64> 179 %s = sparse_tensor.convert %t 180 : tensor<8x8xf64> to tensor<8x8xf64, #SM> 181 182 %a = arith.constant dense<3.0> : tensor<8x8xf64> 183 %b = arith.constant dense<4.0> : tensor<8x8xf64> 184 185 // Call the kernels. 186 %0 = call @sampled_dd(%s, %a, %b) 187 : (tensor<8x8xf64, #SM>, 188 tensor<8x8xf64>, tensor<8x8xf64>) -> tensor<8x8xf64> 189 %1 = call @sampled_dd_unfused(%s, %a, %b) 190 : (tensor<8x8xf64, #SM>, 191 tensor<8x8xf64>, tensor<8x8xf64>) -> tensor<8x8xf64> 192 %2 = call @sparse_sampled_dd(%s, %a, %b) 193 : (tensor<8x8xf64, #SM>, 194 tensor<8x8xf64>, tensor<8x8xf64>) -> tensor<8x8xf64, #SM> 195 %3 = call @sparse_sampled_dd_unfused(%s, %a, %b) 196 : (tensor<8x8xf64, #SM>, 197 tensor<8x8xf64>, tensor<8x8xf64>) -> tensor<8x8xf64, #SM> 198 199 // Verify the outputs. 200 // 201 // CHECK: ( ( 96, 0, 0, 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0 ), 202 // CHECK-SAME: ( 0, 0, 0, 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0 ), 203 // CHECK-SAME: ( 0, 0, 0, 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0 ), 204 // CHECK-SAME: ( 0, 0, 0, 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 192 ) ) 205 // 206 // CHECK: ( ( 96, 0, 0, 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0 ), 207 // CHECK-SAME: ( 0, 0, 0, 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0 ), 208 // CHECK-SAME: ( 0, 0, 0, 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0 ), 209 // CHECK-SAME: ( 0, 0, 0, 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 192 ) ) 210 // 211 // CHECK: ---- Sparse Tensor ---- 212 // CHECK-NEXT: nse = 2 213 // CHECK-NEXT: dim = ( 8, 8 ) 214 // CHECK-NEXT: lvl = ( 8, 8 ) 215 // CHECK-NEXT: pos[0] : ( 0, 2 ) 216 // CHECK-NEXT: crd[0] : ( 0, 7 ) 217 // CHECK-NEXT: pos[1] : ( 0, 1, 2 ) 218 // CHECK-NEXT: crd[1] : ( 0, 7 ) 219 // CHECK-NEXT: values : ( 96, 192 ) 220 // CHECK-NEXT: ---- 221 // 222 // CHECK: ---- Sparse Tensor ---- 223 // CHECK-NEXT: nse = 2 224 // CHECK-NEXT: dim = ( 8, 8 ) 225 // CHECK-NEXT: lvl = ( 8, 8 ) 226 // CHECK-NEXT: pos[0] : ( 0, 2 ) 227 // CHECK-NEXT: crd[0] : ( 0, 7 ) 228 // CHECK-NEXT: pos[1] : ( 0, 1, 2 ) 229 // CHECK-NEXT: crd[1] : ( 0, 7 ) 230 // CHECK-NEXT: values : ( 96, 192 ) 231 // CHECK-NEXT: ---- 232 // 233 %v0 = vector.transfer_read %0[%c0, %c0], %d0 234 : tensor<8x8xf64>, vector<8x8xf64> 235 %v1 = vector.transfer_read %1[%c0, %c0], %d0 236 : tensor<8x8xf64>, vector<8x8xf64> 237 vector.print %v0 : vector<8x8xf64> 238 vector.print %v1 : vector<8x8xf64> 239 sparse_tensor.print %2 : tensor<8x8xf64, #SM> 240 sparse_tensor.print %3 : tensor<8x8xf64, #SM> 241 242 // Release the resources. 243 bufferization.dealloc_tensor %s : tensor<8x8xf64, #SM> 244 bufferization.dealloc_tensor %0 : tensor<8x8xf64> 245 bufferization.dealloc_tensor %1 : tensor<8x8xf64> 246 bufferization.dealloc_tensor %2 : tensor<8x8xf64, #SM> 247 bufferization.dealloc_tensor %3 : tensor<8x8xf64, #SM> 248 249 return 250 } 251} 252