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 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: expand 53// 54module { 55 56 func.func @expand_dense(%arg0: tensor<12xf64>) -> tensor<3x4xf64> { 57 %0 = tensor.expand_shape %arg0 [[0, 1]] output_shape [3, 4] : tensor<12xf64> into tensor<3x4xf64> 58 return %0 : tensor<3x4xf64> 59 } 60 61 func.func @expand_from_sparse(%arg0: tensor<12xf64, #SparseVector>) -> tensor<3x4xf64> { 62 %0 = tensor.expand_shape %arg0 [[0, 1]] output_shape [3, 4] : tensor<12xf64, #SparseVector> into tensor<3x4xf64> 63 return %0 : tensor<3x4xf64> 64 } 65 66 func.func @expand_to_sparse(%arg0: tensor<12xf64>) -> tensor<3x4xf64, #SparseMatrix> { 67 %0 = tensor.expand_shape %arg0 [[0, 1]] output_shape [3, 4] : tensor<12xf64> into tensor<3x4xf64, #SparseMatrix> 68 return %0 : tensor<3x4xf64, #SparseMatrix> 69 } 70 71 func.func @expand_sparse2sparse(%arg0: tensor<12xf64, #SparseVector>) -> tensor<3x4xf64, #SparseMatrix> { 72 %0 = tensor.expand_shape %arg0 [[0, 1]] output_shape [3, 4] : tensor<12xf64, #SparseVector> into tensor<3x4xf64, #SparseMatrix> 73 return %0 : tensor<3x4xf64, #SparseMatrix> 74 } 75 76 func.func @expand_dense_3x2x2(%arg0: tensor<3x4xf64>) -> tensor<3x2x2xf64> { 77 %0 = tensor.expand_shape %arg0 [[0], [1, 2]] output_shape [3, 2, 2] : tensor<3x4xf64> into tensor<3x2x2xf64> 78 return %0 : tensor<3x2x2xf64> 79 } 80 81 func.func @expand_from_sparse_3x2x2(%arg0: tensor<3x4xf64, #SparseMatrix>) -> tensor<3x2x2xf64> { 82 %0 = tensor.expand_shape %arg0 [[0], [1, 2]] output_shape [3, 2, 2] : tensor<3x4xf64, #SparseMatrix> into tensor<3x2x2xf64> 83 return %0 : tensor<3x2x2xf64> 84 } 85 86 func.func @expand_to_sparse_3x2x2(%arg0: tensor<3x4xf64>) -> tensor<3x2x2xf64, #Sparse3dTensor> { 87 %0 = tensor.expand_shape %arg0 [[0], [1, 2]] output_shape [3, 2, 2] : tensor<3x4xf64> into tensor<3x2x2xf64, #Sparse3dTensor> 88 return %0 : tensor<3x2x2xf64, #Sparse3dTensor> 89 } 90 91 func.func @expand_sparse2sparse_3x2x2(%arg0: tensor<3x4xf64, #SparseMatrix>) -> tensor<3x2x2xf64, #Sparse3dTensor> { 92 %0 = tensor.expand_shape %arg0 [[0], [1, 2]] output_shape [3, 2, 2] : tensor<3x4xf64, #SparseMatrix> into tensor<3x2x2xf64, #Sparse3dTensor> 93 return %0 : tensor<3x2x2xf64, #Sparse3dTensor> 94 } 95 96 func.func @expand_dense_dyn(%arg0: tensor<?x?xf64>) -> tensor<?x2x?xf64> { 97 %c0 = arith.constant 0 : index 98 %c1 = arith.constant 1 : index 99 %c2 = arith.constant 2 : index 100 %d0 = tensor.dim %arg0, %c0 : tensor<?x?xf64> 101 %d1 = tensor.dim %arg0, %c1 : tensor<?x?xf64> 102 %d2 = arith.divui %d1, %c2 : index 103 %0 = tensor.expand_shape %arg0 [[0], [1, 2]] output_shape [%d0, 2, %d2] : tensor<?x?xf64> into tensor<?x2x?xf64> 104 return %0 : tensor<?x2x?xf64> 105 } 106 107 func.func @expand_from_sparse_dyn(%arg0: tensor<?x?xf64, #SparseMatrix>) -> tensor<?x2x?xf64> { 108 %c0 = arith.constant 0 : index 109 %c1 = arith.constant 1 : index 110 %c2 = arith.constant 2 : index 111 %d0 = tensor.dim %arg0, %c0 : tensor<?x?xf64, #SparseMatrix> 112 %d1 = tensor.dim %arg0, %c1 : tensor<?x?xf64, #SparseMatrix> 113 %d2 = arith.divui %d1, %c2 : index 114 %0 = tensor.expand_shape %arg0 [[0], [1, 2]] output_shape [%d0, 2, %d2] : tensor<?x?xf64, #SparseMatrix> into tensor<?x2x?xf64> 115 return %0 : tensor<?x2x?xf64> 116 } 117 118 func.func @expand_to_sparse_dyn(%arg0: tensor<?x?xf64>) -> tensor<?x2x?xf64, #Sparse3dTensor> { 119 %c0 = arith.constant 0 : index 120 %c1 = arith.constant 1 : index 121 %c2 = arith.constant 2 : index 122 %d0 = tensor.dim %arg0, %c0 : tensor<?x?xf64> 123 %d1 = tensor.dim %arg0, %c1 : tensor<?x?xf64> 124 %d2 = arith.divui %d1, %c2 : index 125 %0 = tensor.expand_shape %arg0 [[0], [1, 2]] output_shape [%d0, 2, %d2] : tensor<?x?xf64> into tensor<?x2x?xf64, #Sparse3dTensor> 126 return %0 : tensor<?x2x?xf64, #Sparse3dTensor> 127 } 128 129 func.func @expand_sparse2sparse_dyn(%arg0: tensor<?x?xf64, #SparseMatrix>) -> tensor<?x2x?xf64, #Sparse3dTensor> { 130 %c0 = arith.constant 0 : index 131 %c1 = arith.constant 1 : index 132 %c2 = arith.constant 2 : index 133 %d0 = tensor.dim %arg0, %c0 : tensor<?x?xf64, #SparseMatrix> 134 %d1 = tensor.dim %arg0, %c1 : tensor<?x?xf64, #SparseMatrix> 135 %d2 = arith.divui %d1, %c2 : index 136 %0 = tensor.expand_shape %arg0 [[0], [1, 2]] output_shape [%d0, 2, %d2] : tensor<?x?xf64, #SparseMatrix> into tensor<?x2x?xf64, #Sparse3dTensor> 137 return %0 : tensor<?x2x?xf64, #Sparse3dTensor> 138 } 139 140 // 141 // Main driver. 142 // 143 func.func @main() { 144 %c0 = arith.constant 0 : index 145 %df = arith.constant -1.0 : f64 146 147 // Setup test vectors and matrices.. 148 %v = arith.constant dense <[ 1.0, 0.0, 3.0, 0.0, 5.0, 0.0, 149 7.0, 0.0, 9.0, 0.0, 11.0, 0.0]> : tensor<12xf64> 150 %m = arith.constant dense <[ [ 1.1, 1.2, 1.3, 1.4 ], 151 [ 2.1, 2.2, 2.3, 2.4 ], 152 [ 3.1, 3.2, 3.3, 3.4 ]]> : tensor<3x4xf64> 153 154 %sv = sparse_tensor.convert %v : tensor<12xf64> to tensor<12xf64, #SparseVector> 155 %sm = sparse_tensor.convert %m : tensor<3x4xf64> to tensor<3x4xf64, #SparseMatrix> 156 157 %dm = tensor.cast %m : tensor<3x4xf64> to tensor<?x?xf64> 158 %sdm = sparse_tensor.convert %dm : tensor<?x?xf64> to tensor<?x?xf64, #SparseMatrix> 159 160 // Call the kernels. 161 %expand0 = call @expand_dense(%v) : (tensor<12xf64>) -> tensor<3x4xf64> 162 %expand1 = call @expand_from_sparse(%sv) : (tensor<12xf64, #SparseVector>) -> tensor<3x4xf64> 163 %expand2 = call @expand_to_sparse(%v) : (tensor<12xf64>) -> tensor<3x4xf64, #SparseMatrix> 164 %expand3 = call @expand_sparse2sparse(%sv) : (tensor<12xf64, #SparseVector>) -> tensor<3x4xf64, #SparseMatrix> 165 %expand4 = call @expand_dense_3x2x2(%m) : (tensor<3x4xf64>) -> tensor<3x2x2xf64> 166 %expand5 = call @expand_from_sparse_3x2x2(%sm) : (tensor<3x4xf64, #SparseMatrix>) -> tensor<3x2x2xf64> 167 %expand6 = call @expand_to_sparse_3x2x2(%m) : (tensor<3x4xf64>) -> tensor<3x2x2xf64, #Sparse3dTensor> 168 %expand7 = call @expand_sparse2sparse_3x2x2(%sm) : (tensor<3x4xf64, #SparseMatrix>) -> tensor<3x2x2xf64, #Sparse3dTensor> 169 %expand8 = call @expand_dense_dyn(%dm) : (tensor<?x?xf64>) -> tensor<?x2x?xf64> 170 %expand9 = call @expand_from_sparse_dyn(%sdm) : (tensor<?x?xf64, #SparseMatrix>) -> tensor<?x2x?xf64> 171 %expand10 = call @expand_to_sparse_dyn(%dm) : (tensor<?x?xf64>) -> tensor<?x2x?xf64, #Sparse3dTensor> 172 %expand11 = call @expand_sparse2sparse_dyn(%sdm) : (tensor<?x?xf64, #SparseMatrix>) -> tensor<?x2x?xf64, #Sparse3dTensor> 173 174 // 175 // Verify results of expand with dense output. 176 // 177 // CHECK: ( ( 1, 0, 3, 0 ), ( 5, 0, 7, 0 ), ( 9, 0, 11, 0 ) ) 178 // CHECK-NEXT: ( ( 1, 0, 3, 0 ), ( 5, 0, 7, 0 ), ( 9, 0, 11, 0 ) ) 179 // CHECK-NEXT: ( ( ( 1.1, 1.2 ), ( 1.3, 1.4 ) ), ( ( 2.1, 2.2 ), ( 2.3, 2.4 ) ), ( ( 3.1, 3.2 ), ( 3.3, 3.4 ) ) ) 180 // CHECK-NEXT: ( ( ( 1.1, 1.2 ), ( 1.3, 1.4 ) ), ( ( 2.1, 2.2 ), ( 2.3, 2.4 ) ), ( ( 3.1, 3.2 ), ( 3.3, 3.4 ) ) ) 181 // CHECK-NEXT: ( ( ( 1.1, 1.2 ), ( 1.3, 1.4 ) ), ( ( 2.1, 2.2 ), ( 2.3, 2.4 ) ), ( ( 3.1, 3.2 ), ( 3.3, 3.4 ) ) ) 182 // CHECK-NEXT: ( ( ( 1.1, 1.2 ), ( 1.3, 1.4 ) ), ( ( 2.1, 2.2 ), ( 2.3, 2.4 ) ), ( ( 3.1, 3.2 ), ( 3.3, 3.4 ) ) ) 183 // 184 %m0 = vector.transfer_read %expand0[%c0, %c0], %df: tensor<3x4xf64>, vector<3x4xf64> 185 vector.print %m0 : vector<3x4xf64> 186 %m1 = vector.transfer_read %expand1[%c0, %c0], %df: tensor<3x4xf64>, vector<3x4xf64> 187 vector.print %m1 : vector<3x4xf64> 188 %m4 = vector.transfer_read %expand4[%c0, %c0, %c0], %df: tensor<3x2x2xf64>, vector<3x2x2xf64> 189 vector.print %m4 : vector<3x2x2xf64> 190 %m5 = vector.transfer_read %expand5[%c0, %c0, %c0], %df: tensor<3x2x2xf64>, vector<3x2x2xf64> 191 vector.print %m5 : vector<3x2x2xf64> 192 %m8 = vector.transfer_read %expand8[%c0, %c0, %c0], %df: tensor<?x2x?xf64>, vector<3x2x2xf64> 193 vector.print %m8 : vector<3x2x2xf64> 194 %m9 = vector.transfer_read %expand9[%c0, %c0, %c0], %df: tensor<?x2x?xf64>, vector<3x2x2xf64> 195 vector.print %m9 : vector<3x2x2xf64> 196 197 // 198 // Verify results of expand with sparse output. 199 // 200 // CHECK: ---- Sparse Tensor ---- 201 // CHECK-NEXT: nse = 6 202 // CHECK-NEXT: dim = ( 3, 4 ) 203 // CHECK-NEXT: lvl = ( 3, 4 ) 204 // CHECK-NEXT: pos[0] : ( 0, 3 ) 205 // CHECK-NEXT: crd[0] : ( 0, 1, 2 ) 206 // CHECK-NEXT: pos[1] : ( 0, 2, 4, 6 ) 207 // CHECK-NEXT: crd[1] : ( 0, 2, 0, 2, 0, 2 ) 208 // CHECK-NEXT: values : ( 1, 3, 5, 7, 9, 11 ) 209 // CHECK-NEXT: ---- 210 // 211 // CHECK: ---- Sparse Tensor ---- 212 // CHECK-NEXT: nse = 6 213 // CHECK-NEXT: dim = ( 3, 4 ) 214 // CHECK-NEXT: lvl = ( 3, 4 ) 215 // CHECK-NEXT: pos[0] : ( 0, 3 ) 216 // CHECK-NEXT: crd[0] : ( 0, 1, 2 ) 217 // CHECK-NEXT: pos[1] : ( 0, 2, 4, 6 ) 218 // CHECK-NEXT: crd[1] : ( 0, 2, 0, 2, 0, 2 ) 219 // CHECK-NEXT: values : ( 1, 3, 5, 7, 9, 11 ) 220 // CHECK-NEXT: ---- 221 // 222 // CHECK: ---- Sparse Tensor ---- 223 // CHECK-NEXT: nse = 12 224 // CHECK-NEXT: dim = ( 3, 2, 2 ) 225 // CHECK-NEXT: lvl = ( 3, 2, 2 ) 226 // CHECK-NEXT: pos[0] : ( 0, 3 ) 227 // CHECK-NEXT: crd[0] : ( 0, 1, 2 ) 228 // CHECK-NEXT: pos[1] : ( 0, 2, 4, 6 ) 229 // CHECK-NEXT: crd[1] : ( 0, 1, 0, 1, 0, 1 ) 230 // CHECK-NEXT: pos[2] : ( 0, 2, 4, 6, 8, 10, 12 ) 231 // CHECK-NEXT: crd[2] : ( 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ) 232 // CHECK-NEXT: values : ( 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3, 2.4, 3.1, 3.2, 3.3, 3.4 ) 233 // CHECK-NEXT: ---- 234 // 235 // CHECK: ---- Sparse Tensor ---- 236 // CHECK-NEXT: nse = 12 237 // CHECK-NEXT: dim = ( 3, 2, 2 ) 238 // CHECK-NEXT: lvl = ( 3, 2, 2 ) 239 // CHECK-NEXT: pos[0] : ( 0, 3 ) 240 // CHECK-NEXT: crd[0] : ( 0, 1, 2 ) 241 // CHECK-NEXT: pos[1] : ( 0, 2, 4, 6 ) 242 // CHECK-NEXT: crd[1] : ( 0, 1, 0, 1, 0, 1 ) 243 // CHECK-NEXT: pos[2] : ( 0, 2, 4, 6, 8, 10, 12 ) 244 // CHECK-NEXT: crd[2] : ( 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ) 245 // CHECK-NEXT: values : ( 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3, 2.4, 3.1, 3.2, 3.3, 3.4 ) 246 // CHECK-NEXT: ---- 247 // 248 // CHECK: ---- Sparse Tensor ---- 249 // CHECK-NEXT: nse = 12 250 // CHECK-NEXT: dim = ( 3, 2, 2 ) 251 // CHECK-NEXT: lvl = ( 3, 2, 2 ) 252 // CHECK-NEXT: pos[0] : ( 0, 3 ) 253 // CHECK-NEXT: crd[0] : ( 0, 1, 2 ) 254 // CHECK-NEXT: pos[1] : ( 0, 2, 4, 6 ) 255 // CHECK-NEXT: crd[1] : ( 0, 1, 0, 1, 0, 1 ) 256 // CHECK-NEXT: pos[2] : ( 0, 2, 4, 6, 8, 10, 12 ) 257 // CHECK-NEXT: crd[2] : ( 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ) 258 // CHECK-NEXT: values : ( 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3, 2.4, 3.1, 3.2, 3.3, 3.4 ) 259 // CHECK-NEXT: ---- 260 // 261 // CHECK: ---- Sparse Tensor ---- 262 // CHECK-NEXT: nse = 12 263 // CHECK-NEXT: dim = ( 3, 2, 2 ) 264 // CHECK-NEXT: lvl = ( 3, 2, 2 ) 265 // CHECK-NEXT: pos[0] : ( 0, 3 ) 266 // CHECK-NEXT: crd[0] : ( 0, 1, 2 ) 267 // CHECK-NEXT: pos[1] : ( 0, 2, 4, 6 ) 268 // CHECK-NEXT: crd[1] : ( 0, 1, 0, 1, 0, 1 ) 269 // CHECK-NEXT: pos[2] : ( 0, 2, 4, 6, 8, 10, 12 ) 270 // CHECK-NEXT: crd[2] : ( 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 ) 271 // CHECK-NEXT: values : ( 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3, 2.4, 3.1, 3.2, 3.3, 3.4 ) 272 // CHECK-NEXT: ---- 273 // 274 sparse_tensor.print %expand2 : tensor<3x4xf64, #SparseMatrix> 275 sparse_tensor.print %expand3 : tensor<3x4xf64, #SparseMatrix> 276 sparse_tensor.print %expand6 : tensor<3x2x2xf64, #Sparse3dTensor> 277 sparse_tensor.print %expand7 : tensor<3x2x2xf64, #Sparse3dTensor> 278 sparse_tensor.print %expand10 : tensor<?x2x?xf64, #Sparse3dTensor> 279 sparse_tensor.print %expand11 : tensor<?x2x?xf64, #Sparse3dTensor> 280 281 282 // Release sparse resources. 283 bufferization.dealloc_tensor %sv : tensor<12xf64, #SparseVector> 284 bufferization.dealloc_tensor %sm : tensor<3x4xf64, #SparseMatrix> 285 bufferization.dealloc_tensor %sdm : tensor<?x?xf64, #SparseMatrix> 286 bufferization.dealloc_tensor %expand2 : tensor<3x4xf64, #SparseMatrix> 287 bufferization.dealloc_tensor %expand3 : tensor<3x4xf64, #SparseMatrix> 288 bufferization.dealloc_tensor %expand6 : tensor<3x2x2xf64, #Sparse3dTensor> 289 bufferization.dealloc_tensor %expand7 : tensor<3x2x2xf64, #Sparse3dTensor> 290 bufferization.dealloc_tensor %expand10 : tensor<?x2x?xf64, #Sparse3dTensor> 291 bufferization.dealloc_tensor %expand11 : tensor<?x2x?xf64, #Sparse3dTensor> 292 293 // Release dense resources. 294 bufferization.dealloc_tensor %expand1 : tensor<3x4xf64> 295 bufferization.dealloc_tensor %expand5 : tensor<3x2x2xf64> 296 bufferization.dealloc_tensor %expand9 : tensor<?x2x?xf64> 297 298 return 299 } 300} 301