|
| 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_opts} = -e main -entry-point-result=void |
| 14 | +// DEFINE: %{run} = mlir-cpu-runner %{run_opts} %{run_libs} |
| 15 | +// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs} |
| 16 | +// |
| 17 | +// DEFINE: %{env} = |
| 18 | +//-------------------------------------------------------------------------------------------------- |
| 19 | + |
| 20 | +// RUN: %{compile} | %{run} | FileCheck %s |
| 21 | +// |
| 22 | +// Do the same run, but now with direct IR generation. |
| 23 | +// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true |
| 24 | +// RUN: %{compile} | %{run} | FileCheck %s |
| 25 | +// |
| 26 | + |
| 27 | +#AllDense = #sparse_tensor.encoding<{ |
| 28 | + map = (i, j) -> ( |
| 29 | + i : dense, |
| 30 | + j : dense |
| 31 | + ) |
| 32 | +}> |
| 33 | + |
| 34 | +#AllDenseT = #sparse_tensor.encoding<{ |
| 35 | + map = (i, j) -> ( |
| 36 | + j : dense, |
| 37 | + i : dense |
| 38 | + ) |
| 39 | +}> |
| 40 | + |
| 41 | +#CSR = #sparse_tensor.encoding<{ |
| 42 | + map = (i, j) -> ( |
| 43 | + i : dense, |
| 44 | + j : compressed |
| 45 | + ) |
| 46 | +}> |
| 47 | + |
| 48 | +#DCSR = #sparse_tensor.encoding<{ |
| 49 | + map = (i, j) -> ( |
| 50 | + i : compressed, |
| 51 | + j : compressed |
| 52 | + ) |
| 53 | +}> |
| 54 | + |
| 55 | +#CSC = #sparse_tensor.encoding<{ |
| 56 | + map = (i, j) -> ( |
| 57 | + j : dense, |
| 58 | + i : compressed |
| 59 | + ) |
| 60 | +}> |
| 61 | + |
| 62 | +#DCSC = #sparse_tensor.encoding<{ |
| 63 | + map = (i, j) -> ( |
| 64 | + j : compressed, |
| 65 | + i : compressed |
| 66 | + ) |
| 67 | +}> |
| 68 | + |
| 69 | +#BSR = #sparse_tensor.encoding<{ |
| 70 | + map = (i, j) -> ( |
| 71 | + i floordiv 2 : compressed, |
| 72 | + j floordiv 4 : compressed, |
| 73 | + i mod 2 : dense, |
| 74 | + j mod 4 : dense |
| 75 | + ) |
| 76 | +}> |
| 77 | + |
| 78 | +#BSRC = #sparse_tensor.encoding<{ |
| 79 | + map = (i, j) -> ( |
| 80 | + i floordiv 2 : compressed, |
| 81 | + j floordiv 4 : compressed, |
| 82 | + j mod 4 : dense, |
| 83 | + i mod 2 : dense |
| 84 | + ) |
| 85 | +}> |
| 86 | + |
| 87 | +#BSC = #sparse_tensor.encoding<{ |
| 88 | + map = (i, j) -> ( |
| 89 | + j floordiv 4 : compressed, |
| 90 | + i floordiv 2 : compressed, |
| 91 | + i mod 2 : dense, |
| 92 | + j mod 4 : dense |
| 93 | + ) |
| 94 | +}> |
| 95 | + |
| 96 | +#BSCC = #sparse_tensor.encoding<{ |
| 97 | + map = (i, j) -> ( |
| 98 | + j floordiv 4 : compressed, |
| 99 | + i floordiv 2 : compressed, |
| 100 | + j mod 4 : dense, |
| 101 | + i mod 2 : dense |
| 102 | + ) |
| 103 | +}> |
| 104 | + |
| 105 | +module { |
| 106 | + |
| 107 | + // |
| 108 | + // Main driver that tests sparse tensor storage. |
| 109 | + // |
| 110 | + func.func @main() { |
| 111 | + %x = arith.constant dense <[ |
| 112 | + [ 1, 0, 2, 0, 0, 0, 0, 0 ], |
| 113 | + [ 0, 0, 0, 0, 0, 0, 0, 0 ], |
| 114 | + [ 0, 0, 0, 0, 0, 0, 0, 0 ], |
| 115 | + [ 0, 0, 3, 4, 0, 5, 0, 0 ] ]> : tensor<4x8xi32> |
| 116 | + |
| 117 | + %a = sparse_tensor.convert %x : tensor<4x8xi32> to tensor<4x8xi32, #CSR> |
| 118 | + %b = sparse_tensor.convert %x : tensor<4x8xi32> to tensor<4x8xi32, #DCSR> |
| 119 | + %c = sparse_tensor.convert %x : tensor<4x8xi32> to tensor<4x8xi32, #CSC> |
| 120 | + %d = sparse_tensor.convert %x : tensor<4x8xi32> to tensor<4x8xi32, #DCSC> |
| 121 | + %e = sparse_tensor.convert %x : tensor<4x8xi32> to tensor<4x8xi32, #BSR> |
| 122 | + %f = sparse_tensor.convert %x : tensor<4x8xi32> to tensor<4x8xi32, #BSRC> |
| 123 | + %g = sparse_tensor.convert %x : tensor<4x8xi32> to tensor<4x8xi32, #BSC> |
| 124 | + %h = sparse_tensor.convert %x : tensor<4x8xi32> to tensor<4x8xi32, #BSCC> |
| 125 | + |
| 126 | + // |
| 127 | + // CHECK: ---- Sparse Tensor ---- |
| 128 | + // CHECK-NEXT: nse = 5 |
| 129 | + // CHECK-NEXT: pos[1] : ( 0, 2, 2, 2, 5, |
| 130 | + // CHECK-NEXT: crd[1] : ( 0, 2, 2, 3, 5, |
| 131 | + // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, |
| 132 | + // CHECK-NEXT: ---- |
| 133 | + sparse_tensor.print %a : tensor<4x8xi32, #CSR> |
| 134 | + |
| 135 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 136 | + // CHECK-NEXT: nse = 5 |
| 137 | + // CHECK-NEXT: pos[0] : ( 0, 2, |
| 138 | + // CHECK-NEXT: crd[0] : ( 0, 3, |
| 139 | + // CHECK-NEXT: pos[1] : ( 0, 2, 5, |
| 140 | + // CHECK-NEXT: crd[1] : ( 0, 2, 2, 3, 5, |
| 141 | + // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, |
| 142 | + // CHECK-NEXT: ---- |
| 143 | + sparse_tensor.print %b : tensor<4x8xi32, #DCSR> |
| 144 | + |
| 145 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 146 | + // CHECK-NEXT: nse = 5 |
| 147 | + // CHECK-NEXT: pos[1] : ( 0, 1, 1, 3, 4, 4, 5, 5, 5, |
| 148 | + // CHECK-NEXT: crd[1] : ( 0, 0, 3, 3, 3, |
| 149 | + // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, |
| 150 | + // CHECK-NEXT: ---- |
| 151 | + sparse_tensor.print %c : tensor<4x8xi32, #CSC> |
| 152 | + |
| 153 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 154 | + // CHECK-NEXT: nse = 5 |
| 155 | + // CHECK-NEXT: pos[0] : ( 0, 4, |
| 156 | + // CHECK-NEXT: crd[0] : ( 0, 2, 3, 5, |
| 157 | + // CHECK-NEXT: pos[1] : ( 0, 1, 3, 4, 5, |
| 158 | + // CHECK-NEXT: crd[1] : ( 0, 0, 3, 3, 3, |
| 159 | + // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, |
| 160 | + // CHECK-NEXT: ---- |
| 161 | + sparse_tensor.print %d : tensor<4x8xi32, #DCSC> |
| 162 | + |
| 163 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 164 | + // CHECK-NEXT: nse = 24 |
| 165 | + // CHECK-NEXT: pos[0] : ( 0, 2, |
| 166 | + // CHECK-NEXT: crd[0] : ( 0, 1, |
| 167 | + // CHECK-NEXT: pos[1] : ( 0, 1, 3, |
| 168 | + // CHECK-NEXT: crd[1] : ( 0, 0, 1, |
| 169 | + // CHECK-NEXT: values : ( 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 5, 0, 0, |
| 170 | + // CHECK-NEXT: ---- |
| 171 | + sparse_tensor.print %e : tensor<4x8xi32, #BSR> |
| 172 | + |
| 173 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 174 | + // CHECK-NEXT: nse = 24 |
| 175 | + // CHECK-NEXT: pos[0] : ( 0, 2, |
| 176 | + // CHECK-NEXT: crd[0] : ( 0, 1, |
| 177 | + // CHECK-NEXT: pos[1] : ( 0, 1, 3, |
| 178 | + // CHECK-NEXT: crd[1] : ( 0, 0, 1, |
| 179 | + // CHECK-NEXT: values : ( 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 4, 0, 0, 0, 5, 0, 0, 0, 0, |
| 180 | + // CHECK-NEXT: ---- |
| 181 | + sparse_tensor.print %f : tensor<4x8xi32, #BSRC> |
| 182 | + |
| 183 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 184 | + // CHECK-NEXT: nse = 24 |
| 185 | + // CHECK-NEXT: pos[0] : ( 0, 2, |
| 186 | + // CHECK-NEXT: crd[0] : ( 0, 1, |
| 187 | + // CHECK-NEXT: pos[1] : ( 0, 2, 3, |
| 188 | + // CHECK-NEXT: crd[1] : ( 0, 1, 1, |
| 189 | + // CHECK-NEXT: values : ( 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 5, 0, 0, |
| 190 | + // CHECK-NEXT: ---- |
| 191 | + sparse_tensor.print %g : tensor<4x8xi32, #BSC> |
| 192 | + |
| 193 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 194 | + // CHECK-NEXT: nse = 24 |
| 195 | + // CHECK-NEXT: pos[0] : ( 0, 2, |
| 196 | + // CHECK-NEXT: crd[0] : ( 0, 1, |
| 197 | + // CHECK-NEXT: pos[1] : ( 0, 2, 3, |
| 198 | + // CHECK-NEXT: crd[1] : ( 0, 1, 1, |
| 199 | + // CHECK-NEXT: values : ( 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 4, 0, 0, 0, 5, 0, 0, 0, 0, |
| 200 | + // CHECK-NEXT: ---- |
| 201 | + sparse_tensor.print %h : tensor<4x8xi32, #BSCC> |
| 202 | + |
| 203 | + // Release the resources. |
| 204 | + bufferization.dealloc_tensor %a : tensor<4x8xi32, #CSR> |
| 205 | + bufferization.dealloc_tensor %b : tensor<4x8xi32, #DCSR> |
| 206 | + bufferization.dealloc_tensor %c : tensor<4x8xi32, #CSC> |
| 207 | + bufferization.dealloc_tensor %d : tensor<4x8xi32, #DCSC> |
| 208 | + bufferization.dealloc_tensor %e : tensor<4x8xi32, #BSR> |
| 209 | + bufferization.dealloc_tensor %f : tensor<4x8xi32, #BSRC> |
| 210 | + bufferization.dealloc_tensor %g : tensor<4x8xi32, #BSC> |
| 211 | + bufferization.dealloc_tensor %h : tensor<4x8xi32, #BSCC> |
| 212 | + |
| 213 | + return |
| 214 | + } |
| 215 | +} |
0 commit comments