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 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#SparseVector = #sparse_tensor.encoding<{ 35 map = (d0) -> (d0 : compressed) 36}> 37 38#SparseMatrix = #sparse_tensor.encoding<{ 39 map = (d0, d1) -> (d0 : compressed, d1 : compressed) 40}> 41 42#Sparse3dTensor = #sparse_tensor.encoding<{ 43 map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed) 44}> 45 46#Sparse4dTensor = #sparse_tensor.encoding<{ 47 map = (d0, d1, d2, d3) -> (d0 : compressed, d1 : compressed, d2 : compressed, d3 : compressed) 48}> 49 50// 51// Test with various forms of the two most elementary reshape 52// operations: collapse. 53// 54module { 55 56 func.func @collapse_dense(%arg0: tensor<3x4xf64>) -> tensor<12xf64> { 57 %0 = tensor.collapse_shape %arg0 [[0, 1]] : tensor<3x4xf64> into tensor<12xf64> 58 return %0 : tensor<12xf64> 59 } 60 61 func.func @collapse_from_sparse(%arg0: tensor<3x4xf64, #SparseMatrix>) -> tensor<12xf64> { 62 %0 = tensor.collapse_shape %arg0 [[0, 1]] : tensor<3x4xf64, #SparseMatrix> into tensor<12xf64> 63 return %0 : tensor<12xf64> 64 } 65 66 func.func @collapse_to_sparse(%arg0: tensor<3x4xf64>) -> tensor<12xf64, #SparseVector> { 67 %0 = tensor.collapse_shape %arg0 [[0, 1]] : tensor<3x4xf64> into tensor<12xf64, #SparseVector> 68 return %0 : tensor<12xf64, #SparseVector> 69 } 70 71 func.func @collapse_sparse2sparse(%arg0: tensor<3x4xf64, #SparseMatrix>) -> tensor<12xf64, #SparseVector> { 72 %0 = tensor.collapse_shape %arg0 [[0, 1]] : tensor<3x4xf64, #SparseMatrix> into tensor<12xf64, #SparseVector> 73 return %0 : tensor<12xf64, #SparseVector> 74 } 75 76 func.func @collapse_dense_6x10(%arg0: tensor<2x3x5x2xf64>) -> tensor<6x10xf64> { 77 %0 = tensor.collapse_shape %arg0 [[0, 1], [2, 3]] : tensor<2x3x5x2xf64> into tensor<6x10xf64> 78 return %0 : tensor<6x10xf64> 79 } 80 81 func.func @collapse_from_sparse_6x10(%arg0: tensor<2x3x5x2xf64, #Sparse4dTensor>) -> tensor<6x10xf64> { 82 %0 = tensor.collapse_shape %arg0 [[0, 1], [2, 3]] : tensor<2x3x5x2xf64, #Sparse4dTensor> into tensor<6x10xf64> 83 return %0 : tensor<6x10xf64> 84 } 85 86 func.func @collapse_to_sparse_6x10(%arg0: tensor<2x3x5x2xf64>) -> tensor<6x10xf64, #SparseMatrix> { 87 %0 = tensor.collapse_shape %arg0 [[0, 1], [2, 3]] : tensor<2x3x5x2xf64> into tensor<6x10xf64, #SparseMatrix> 88 return %0 : tensor<6x10xf64, #SparseMatrix> 89 } 90 91 func.func @collapse_sparse2sparse_6x10(%arg0: tensor<2x3x5x2xf64, #Sparse4dTensor>) -> tensor<6x10xf64, #SparseMatrix> { 92 %0 = tensor.collapse_shape %arg0 [[0, 1], [2, 3]] : tensor<2x3x5x2xf64, #Sparse4dTensor> into tensor<6x10xf64, #SparseMatrix> 93 return %0 : tensor<6x10xf64, #SparseMatrix> 94 } 95 96 func.func @collapse_dense_dyn(%arg0: tensor<?x?x?x?xf64>) -> tensor<?x?xf64> { 97 %0 = tensor.collapse_shape %arg0 [[0, 1], [2, 3]] : tensor<?x?x?x?xf64> into tensor<?x?xf64> 98 return %0 : tensor<?x?xf64> 99 } 100 101 func.func @collapse_from_sparse_dyn(%arg0: tensor<?x?x?x?xf64, #Sparse4dTensor>) -> tensor<?x?xf64> { 102 %0 = tensor.collapse_shape %arg0 [[0, 1], [2, 3]] : tensor<?x?x?x?xf64, #Sparse4dTensor> into tensor<?x?xf64> 103 return %0 : tensor<?x?xf64> 104 } 105 106 func.func @collapse_to_sparse_dyn(%arg0: tensor<?x?x?x?xf64>) -> tensor<?x?xf64, #SparseMatrix> { 107 %0 = tensor.collapse_shape %arg0 [[0, 1], [2, 3]] : tensor<?x?x?x?xf64> into tensor<?x?xf64, #SparseMatrix> 108 return %0 : tensor<?x?xf64, #SparseMatrix> 109 } 110 111 func.func @collapse_sparse2sparse_dyn(%arg0: tensor<?x?x?x?xf64, #Sparse4dTensor>) -> tensor<?x?xf64, #SparseMatrix> { 112 %0 = tensor.collapse_shape %arg0 [[0, 1], [2, 3]] : tensor<?x?x?x?xf64, #Sparse4dTensor> into tensor<?x?xf64, #SparseMatrix> 113 return %0 : tensor<?x?xf64, #SparseMatrix> 114 } 115 116 // 117 // Main driver. 118 // 119 func.func @main() { 120 %c0 = arith.constant 0 : index 121 %df = arith.constant -1.0 : f64 122 123 // Setup test vectors and matrices.. 124 %m = arith.constant dense <[ [ 1.1, 0.0, 1.3, 0.0 ], 125 [ 2.1, 0.0, 2.3, 0.0 ], 126 [ 3.1, 0.0, 3.3, 0.0 ]]> : tensor<3x4xf64> 127 %n = arith.constant dense <[ 128 [ [[ 1.0, 0.0], [ 3.0, 0.0], [ 5.0, 0.0], [ 7.0, 0.0], [ 9.0, 0.0]], 129 [[ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0]], 130 [[21.0, 0.0], [23.0, 0.0], [25.0, 0.0], [27.0, 0.0], [29.0, 0.0]] ], 131 [ [[ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0]], 132 [[41.0, 0.0], [43.0, 0.0], [45.0, 0.0], [47.0, 0.0], [49.0, 0.0]], 133 [[ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0], [ 0.0, 0.0]] ] ]> : tensor<2x3x5x2xf64> 134 %sm = sparse_tensor.convert %m : tensor<3x4xf64> to tensor<3x4xf64, #SparseMatrix> 135 %sn = sparse_tensor.convert %n : tensor<2x3x5x2xf64> to tensor<2x3x5x2xf64, #Sparse4dTensor> 136 137 %dm = tensor.cast %m : tensor<3x4xf64> to tensor<?x?xf64> 138 139 %dn = tensor.cast %n : tensor<2x3x5x2xf64> to tensor<?x?x?x?xf64> 140 %sdn = sparse_tensor.convert %dn : tensor<?x?x?x?xf64> to tensor<?x?x?x?xf64, #Sparse4dTensor> 141 142 // Call the kernels. 143 %collapse0 = call @collapse_dense(%m) : (tensor<3x4xf64>) -> tensor<12xf64> 144 %collapse1 = call @collapse_from_sparse(%sm) : (tensor<3x4xf64, #SparseMatrix>) -> tensor<12xf64> 145 %collapse2 = call @collapse_to_sparse(%m) : (tensor<3x4xf64>) -> tensor<12xf64, #SparseVector> 146 %collapse3 = call @collapse_sparse2sparse(%sm) : (tensor<3x4xf64, #SparseMatrix>) -> tensor<12xf64, #SparseVector> 147 %collapse4 = call @collapse_dense_6x10(%n) : (tensor<2x3x5x2xf64>) -> tensor<6x10xf64> 148 %collapse5 = call @collapse_from_sparse_6x10(%sn) : (tensor<2x3x5x2xf64, #Sparse4dTensor>) -> tensor<6x10xf64> 149 %collapse6 = call @collapse_to_sparse_6x10(%n) : (tensor<2x3x5x2xf64>) -> tensor<6x10xf64, #SparseMatrix> 150 %collapse7 = call @collapse_sparse2sparse_6x10(%sn) : (tensor<2x3x5x2xf64, #Sparse4dTensor>) -> tensor<6x10xf64, #SparseMatrix> 151 %collapse8 = call @collapse_dense_dyn(%dn) : (tensor<?x?x?x?xf64>) -> tensor<?x?xf64> 152 %collapse9 = call @collapse_from_sparse_dyn(%sdn) : (tensor<?x?x?x?xf64, #Sparse4dTensor>) -> tensor<?x?xf64> 153 %collapse10 = call @collapse_to_sparse_dyn(%dn) : (tensor<?x?x?x?xf64>) -> tensor<?x?xf64, #SparseMatrix> 154 %collapse11 = call @collapse_sparse2sparse_dyn(%sdn) : (tensor<?x?x?x?xf64, #Sparse4dTensor>) -> tensor<?x?xf64, #SparseMatrix> 155 156 // 157 // Verify results of collapse 158 // 159 // CHECK: ( 1.1, 0, 1.3, 0, 2.1, 0, 2.3, 0, 3.1, 0, 3.3, 0 ) 160 // CHECK-NEXT: ( 1.1, 0, 1.3, 0, 2.1, 0, 2.3, 0, 3.1, 0, 3.3, 0 ) 161 // 162 // CHECK: ---- Sparse Tensor ---- 163 // CHECK-NEXT: nse = 6 164 // CHECK-NEXT: dim = ( 12 ) 165 // CHECK-NEXT: lvl = ( 12 ) 166 // CHECK-NEXT: pos[0] : ( 0, 6 ) 167 // CHECK-NEXT: crd[0] : ( 0, 2, 4, 6, 8, 10 ) 168 // CHECK-NEXT: values : ( 1.1, 1.3, 2.1, 2.3, 3.1, 3.3 ) 169 // CHECK-NEXT: ---- 170 // 171 // CHECK: ---- Sparse Tensor ---- 172 // CHECK-NEXT: nse = 6 173 // CHECK-NEXT: dim = ( 12 ) 174 // CHECK-NEXT: lvl = ( 12 ) 175 // CHECK-NEXT: pos[0] : ( 0, 6 ) 176 // CHECK-NEXT: crd[0] : ( 0, 2, 4, 6, 8, 10 ) 177 // CHECK-NEXT: values : ( 1.1, 1.3, 2.1, 2.3, 3.1, 3.3 ) 178 // CHECK-NEXT: ---- 179 // 180 // CHECK: ( ( 1, 0, 3, 0, 5, 0, 7, 0, 9, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), ( 21, 0, 23, 0, 25, 0, 27, 0, 29, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), ( 41, 0, 43, 0, 45, 0, 47, 0, 49, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ) 181 // CHECK-NEXT: ( ( 1, 0, 3, 0, 5, 0, 7, 0, 9, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), ( 21, 0, 23, 0, 25, 0, 27, 0, 29, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), ( 41, 0, 43, 0, 45, 0, 47, 0, 49, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ) 182 // 183 // CHECK: ---- Sparse Tensor ---- 184 // CHECK-NEXT: nse = 15 185 // CHECK-NEXT: dim = ( 6, 10 ) 186 // CHECK-NEXT: lvl = ( 6, 10 ) 187 // CHECK-NEXT: pos[0] : ( 0, 3 ) 188 // CHECK-NEXT: crd[0] : ( 0, 2, 4 ) 189 // CHECK-NEXT: pos[1] : ( 0, 5, 10, 15 ) 190 // CHECK-NEXT: crd[1] : ( 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8 ) 191 // CHECK-NEXT: values : ( 1, 3, 5, 7, 9, 21, 23, 25, 27, 29, 41, 43, 45, 47, 49 ) 192 // CHECK-NEXT: ---- 193 // 194 // CHECK: ---- Sparse Tensor ---- 195 // CHECK-NEXT: nse = 15 196 // CHECK-NEXT: dim = ( 6, 10 ) 197 // CHECK-NEXT: lvl = ( 6, 10 ) 198 // CHECK-NEXT: pos[0] : ( 0, 3 ) 199 // CHECK-NEXT: crd[0] : ( 0, 2, 4 ) 200 // CHECK-NEXT: pos[1] : ( 0, 5, 10, 15 ) 201 // CHECK-NEXT: crd[1] : ( 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8 ) 202 // CHECK-NEXT: values : ( 1, 3, 5, 7, 9, 21, 23, 25, 27, 29, 41, 43, 45, 47, 49 ) 203 // CHECK-NEXT: ---- 204 // 205 // CHECK: ( ( 1, 0, 3, 0, 5, 0, 7, 0, 9, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), ( 21, 0, 23, 0, 25, 0, 27, 0, 29, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), ( 41, 0, 43, 0, 45, 0, 47, 0, 49, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ) 206 // CHECK-NEXT: ( ( 1, 0, 3, 0, 5, 0, 7, 0, 9, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), ( 21, 0, 23, 0, 25, 0, 27, 0, 29, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ), ( 41, 0, 43, 0, 45, 0, 47, 0, 49, 0 ), ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ) 207 // 208 // CHECK: ---- Sparse Tensor ---- 209 // CHECK-NEXT: nse = 15 210 // CHECK-NEXT: dim = ( 6, 10 ) 211 // CHECK-NEXT: lvl = ( 6, 10 ) 212 // CHECK-NEXT: pos[0] : ( 0, 3 ) 213 // CHECK-NEXT: crd[0] : ( 0, 2, 4 ) 214 // CHECK-NEXT: pos[1] : ( 0, 5, 10, 15 ) 215 // CHECK-NEXT: crd[1] : ( 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8 ) 216 // CHECK-NEXT: values : ( 1, 3, 5, 7, 9, 21, 23, 25, 27, 29, 41, 43, 45, 47, 49 ) 217 // CHECK-NEXT: ---- 218 // 219 // CHECK: ---- Sparse Tensor ---- 220 // CHECK-NEXT: nse = 15 221 // CHECK-NEXT: dim = ( 6, 10 ) 222 // CHECK-NEXT: lvl = ( 6, 10 ) 223 // CHECK-NEXT: pos[0] : ( 0, 3 ) 224 // CHECK-NEXT: crd[0] : ( 0, 2, 4 ) 225 // CHECK-NEXT: pos[1] : ( 0, 5, 10, 15 ) 226 // CHECK-NEXT: crd[1] : ( 0, 2, 4, 6, 8, 0, 2, 4, 6, 8, 0, 2, 4, 6, 8 ) 227 // CHECK-NEXT: values : ( 1, 3, 5, 7, 9, 21, 23, 25, 27, 29, 41, 43, 45, 47, 49 ) 228 // CHECK-NEXT: ---- 229 // 230 %v0 = vector.transfer_read %collapse0[%c0], %df: tensor<12xf64>, vector<12xf64> 231 vector.print %v0 : vector<12xf64> 232 %v1 = vector.transfer_read %collapse1[%c0], %df: tensor<12xf64>, vector<12xf64> 233 vector.print %v1 : vector<12xf64> 234 sparse_tensor.print %collapse2 : tensor<12xf64, #SparseVector> 235 sparse_tensor.print %collapse3 : tensor<12xf64, #SparseVector> 236 237 %v4 = vector.transfer_read %collapse4[%c0, %c0], %df: tensor<6x10xf64>, vector<6x10xf64> 238 vector.print %v4 : vector<6x10xf64> 239 %v5 = vector.transfer_read %collapse5[%c0, %c0], %df: tensor<6x10xf64>, vector<6x10xf64> 240 vector.print %v5 : vector<6x10xf64> 241 sparse_tensor.print %collapse6 : tensor<6x10xf64, #SparseMatrix> 242 sparse_tensor.print %collapse7 : tensor<6x10xf64, #SparseMatrix> 243 244 %v8 = vector.transfer_read %collapse8[%c0, %c0], %df: tensor<?x?xf64>, vector<6x10xf64> 245 vector.print %v8 : vector<6x10xf64> 246 %v9 = vector.transfer_read %collapse9[%c0, %c0], %df: tensor<?x?xf64>, vector<6x10xf64> 247 vector.print %v9 : vector<6x10xf64> 248 sparse_tensor.print %collapse10 : tensor<?x?xf64, #SparseMatrix> 249 sparse_tensor.print %collapse11 : tensor<?x?xf64, #SparseMatrix> 250 251 // Release sparse resources. 252 bufferization.dealloc_tensor %sm : tensor<3x4xf64, #SparseMatrix> 253 bufferization.dealloc_tensor %sn : tensor<2x3x5x2xf64, #Sparse4dTensor> 254 bufferization.dealloc_tensor %sdn : tensor<?x?x?x?xf64, #Sparse4dTensor> 255 bufferization.dealloc_tensor %collapse2 : tensor<12xf64, #SparseVector> 256 bufferization.dealloc_tensor %collapse3 : tensor<12xf64, #SparseVector> 257 bufferization.dealloc_tensor %collapse6 : tensor<6x10xf64, #SparseMatrix> 258 bufferization.dealloc_tensor %collapse7 : tensor<6x10xf64, #SparseMatrix> 259 bufferization.dealloc_tensor %collapse10 : tensor<?x?xf64, #SparseMatrix> 260 bufferization.dealloc_tensor %collapse11 : tensor<?x?xf64, #SparseMatrix> 261 262 // Release dense resources. 263 bufferization.dealloc_tensor %collapse1 : tensor<12xf64> 264 bufferization.dealloc_tensor %collapse5 : tensor<6x10xf64> 265 bufferization.dealloc_tensor %collapse9: tensor<?x?xf64> 266 267 return 268 } 269} 270