xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_vector_ops.mlir (revision eb206e9ea84eff0a0596fed2de8316d924f946d1)
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 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  VLA vectorization.
32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}
33
34#SparseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : compressed)}>
35#DenseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : dense)}>
36
37//
38// Traits for 1-d tensor (aka vector) operations.
39//
40#trait_scale = {
41  indexing_maps = [
42    affine_map<(i) -> (i)>,  // a (in)
43    affine_map<(i) -> (i)>   // x (out)
44  ],
45  iterator_types = ["parallel"],
46  doc = "x(i) = a(i) * 2.0"
47}
48#trait_scale_inpl = {
49  indexing_maps = [
50    affine_map<(i) -> (i)>   // x (out)
51  ],
52  iterator_types = ["parallel"],
53  doc = "x(i) *= 2.0"
54}
55#trait_op = {
56  indexing_maps = [
57    affine_map<(i) -> (i)>,  // a (in)
58    affine_map<(i) -> (i)>,  // b (in)
59    affine_map<(i) -> (i)>   // x (out)
60  ],
61  iterator_types = ["parallel"],
62  doc = "x(i) = a(i) OP b(i)"
63}
64#trait_dot = {
65  indexing_maps = [
66    affine_map<(i) -> (i)>,  // a (in)
67    affine_map<(i) -> (i)>,  // b (in)
68    affine_map<(i) -> ()>   // x (out)
69  ],
70  iterator_types = ["parallel"],
71  doc = "x(i) += a(i) * b(i)"
72}
73
74module {
75  // Scales a sparse vector into a new sparse vector.
76  func.func @vector_scale(%arga: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> {
77    %s = arith.constant 2.0 : f64
78    %c = arith.constant 0 : index
79    %d = tensor.dim %arga, %c : tensor<?xf64, #SparseVector>
80    %xv = tensor.empty(%d) : tensor<?xf64, #SparseVector>
81    %0 = linalg.generic #trait_scale
82       ins(%arga: tensor<?xf64, #SparseVector>)
83        outs(%xv: tensor<?xf64, #SparseVector>) {
84        ^bb(%a: f64, %x: f64):
85          %1 = arith.mulf %a, %s : f64
86          linalg.yield %1 : f64
87    } -> tensor<?xf64, #SparseVector>
88    return %0 : tensor<?xf64, #SparseVector>
89  }
90
91  // Scales a sparse vector in place.
92  func.func @vector_scale_inplace(%argx: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> {
93    %s = arith.constant 2.0 : f64
94    %0 = linalg.generic #trait_scale_inpl
95      outs(%argx: tensor<?xf64, #SparseVector>) {
96        ^bb(%x: f64):
97          %1 = arith.mulf %x, %s : f64
98          linalg.yield %1 : f64
99    } -> tensor<?xf64, #SparseVector>
100    return %0 : tensor<?xf64, #SparseVector>
101  }
102
103  // Adds two sparse vectors into a new sparse vector.
104  func.func @vector_add(%arga: tensor<?xf64, #SparseVector>,
105                   %argb: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> {
106    %c = arith.constant 0 : index
107    %d = tensor.dim %arga, %c : tensor<?xf64, #SparseVector>
108    %xv = tensor.empty(%d) : tensor<?xf64, #SparseVector>
109    %0 = linalg.generic #trait_op
110       ins(%arga, %argb: tensor<?xf64, #SparseVector>, tensor<?xf64, #SparseVector>)
111        outs(%xv: tensor<?xf64, #SparseVector>) {
112        ^bb(%a: f64, %b: f64, %x: f64):
113          %1 = arith.addf %a, %b : f64
114          linalg.yield %1 : f64
115    } -> tensor<?xf64, #SparseVector>
116    return %0 : tensor<?xf64, #SparseVector>
117  }
118
119  // Multiplies two sparse vectors into a new sparse vector.
120  func.func @vector_mul(%arga: tensor<?xf64, #SparseVector>,
121                   %argb: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> {
122    %c = arith.constant 0 : index
123    %d = tensor.dim %arga, %c : tensor<?xf64, #SparseVector>
124    %xv = tensor.empty(%d) : tensor<?xf64, #SparseVector>
125    %0 = linalg.generic #trait_op
126       ins(%arga, %argb: tensor<?xf64, #SparseVector>, tensor<?xf64, #SparseVector>)
127        outs(%xv: tensor<?xf64, #SparseVector>) {
128        ^bb(%a: f64, %b: f64, %x: f64):
129          %1 = arith.mulf %a, %b : f64
130          linalg.yield %1 : f64
131    } -> tensor<?xf64, #SparseVector>
132    return %0 : tensor<?xf64, #SparseVector>
133  }
134
135  // Multiplies two sparse vectors into a new "annotated" dense vector.
136  func.func @vector_mul_d(%arga: tensor<?xf64, #SparseVector>,
137                     %argb: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #DenseVector> {
138    %c = arith.constant 0 : index
139    %d = tensor.dim %arga, %c : tensor<?xf64, #SparseVector>
140    %xv = tensor.empty(%d) : tensor<?xf64, #DenseVector>
141    %0 = linalg.generic #trait_op
142       ins(%arga, %argb: tensor<?xf64, #SparseVector>, tensor<?xf64, #SparseVector>)
143        outs(%xv: tensor<?xf64, #DenseVector>) {
144        ^bb(%a: f64, %b: f64, %x: f64):
145          %1 = arith.mulf %a, %b : f64
146          linalg.yield %1 : f64
147    } -> tensor<?xf64, #DenseVector>
148    return %0 : tensor<?xf64, #DenseVector>
149  }
150
151  // Sum reduces dot product of two sparse vectors.
152  func.func @vector_dotprod(%arga: tensor<?xf64, #SparseVector>,
153                       %argb: tensor<?xf64, #SparseVector>,
154                       %argx: tensor<f64>) -> tensor<f64> {
155    %0 = linalg.generic #trait_dot
156       ins(%arga, %argb: tensor<?xf64, #SparseVector>, tensor<?xf64, #SparseVector>)
157        outs(%argx: tensor<f64>) {
158        ^bb(%a: f64, %b: f64, %x: f64):
159          %1 = arith.mulf %a, %b : f64
160          %2 = arith.addf %x, %1 : f64
161          linalg.yield %2 : f64
162    } -> tensor<f64>
163    return %0 : tensor<f64>
164  }
165
166  // Driver method to call and verify vector kernels.
167  func.func @main() {
168    %c0 = arith.constant 0 : index
169    %d1 = arith.constant 1.1 : f64
170
171    // Setup sparse vectors.
172    %v1 = arith.constant sparse<
173       [ [0], [3], [11], [17], [20], [21], [28], [29], [31] ],
174         [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]
175    > : tensor<32xf64>
176    %v2 = arith.constant sparse<
177       [ [1], [3], [4], [10], [16], [18], [21], [28], [29], [31] ],
178         [11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0 ]
179    > : tensor<32xf64>
180    %sv1 = sparse_tensor.convert %v1 : tensor<32xf64> to tensor<?xf64, #SparseVector>
181    // TODO: Use %sv1 when copying sparse tensors is supported.
182    %sv1_dup = sparse_tensor.convert %v1 : tensor<32xf64> to tensor<?xf64, #SparseVector>
183    %sv2 = sparse_tensor.convert %v2 : tensor<32xf64> to tensor<?xf64, #SparseVector>
184
185    // Setup memory for a single reduction scalar.
186    %x = tensor.from_elements %d1 : tensor<f64>
187
188    // Call sparse vector kernels.
189    %0 = call @vector_scale(%sv1)
190       : (tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector>
191    %1 = call @vector_scale_inplace(%sv1_dup)
192       : (tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector>
193    %2 = call @vector_add(%1, %sv2)
194       : (tensor<?xf64, #SparseVector>,
195          tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector>
196    %3 = call @vector_mul(%1, %sv2)
197       : (tensor<?xf64, #SparseVector>,
198          tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector>
199    %4 = call @vector_mul_d(%1, %sv2)
200       : (tensor<?xf64, #SparseVector>,
201          tensor<?xf64, #SparseVector>) -> tensor<?xf64, #DenseVector>
202    %5 = call @vector_dotprod(%1, %sv2, %x)
203       : (tensor<?xf64, #SparseVector>,
204          tensor<?xf64, #SparseVector>, tensor<f64>) -> tensor<f64>
205
206    //
207    // Verify the results.
208    //
209    // CHECK:      ---- Sparse Tensor ----
210    // CHECK-NEXT: nse = 9
211    // CHECK-NEXT: dim = ( 32 )
212    // CHECK-NEXT: lvl = ( 32 )
213    // CHECK-NEXT: pos[0] : ( 0, 9 )
214    // CHECK-NEXT: crd[0] : ( 0, 3, 11, 17, 20, 21, 28, 29, 31 )
215    // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, 6, 7, 8, 9 )
216    // CHECK-NEXT: ----
217    // CHECK:      ---- Sparse Tensor ----
218    // CHECK-NEXT: nse = 10
219    // CHECK-NEXT: dim = ( 32 )
220    // CHECK-NEXT: lvl = ( 32 )
221    // CHECK-NEXT: pos[0] : ( 0, 10 )
222    // CHECK-NEXT: crd[0] : ( 1, 3, 4, 10, 16, 18, 21, 28, 29, 31 )
223    // CHECK-NEXT: values : ( 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 )
224    // CHECK-NEXT: ----
225    // CHECK:      ---- Sparse Tensor ----
226    // CHECK-NEXT: nse = 9
227    // CHECK-NEXT: dim = ( 32 )
228    // CHECK-NEXT: lvl = ( 32 )
229    // CHECK-NEXT: pos[0] : ( 0, 9 )
230    // CHECK-NEXT: crd[0] : ( 0, 3, 11, 17, 20, 21, 28, 29, 31 )
231    // CHECK-NEXT: values : ( 2, 4, 6, 8, 10, 12, 14, 16, 18 )
232    // CHECK-NEXT: ----
233    // CHECK:      ---- Sparse Tensor ----
234    // CHECK-NEXT: nse = 9
235    // CHECK-NEXT: dim = ( 32 )
236    // CHECK-NEXT: lvl = ( 32 )
237    // CHECK-NEXT: pos[0] : ( 0, 9 )
238    // CHECK-NEXT: crd[0] : ( 0, 3, 11, 17, 20, 21, 28, 29, 31 )
239    // CHECK-NEXT: values : ( 2, 4, 6, 8, 10, 12, 14, 16, 18 )
240    // CHECK-NEXT: ----
241    // CHECK:      ---- Sparse Tensor ----
242    // CHECK-NEXT: nse = 14
243    // CHECK-NEXT: dim = ( 32 )
244    // CHECK-NEXT: lvl = ( 32 )
245    // CHECK-NEXT: pos[0] : ( 0, 14 )
246    // CHECK-NEXT: crd[0] : ( 0, 1, 3, 4, 10, 11, 16, 17, 18, 20, 21, 28, 29, 31 )
247    // CHECK-NEXT: values : ( 2, 11, 16, 13, 14, 6, 15, 8, 16, 10, 29, 32, 35, 38 )
248    // CHECK-NEXT: ----
249    // CHECK:      ---- Sparse Tensor ----
250    // CHECK-NEXT: nse = 5
251    // CHECK-NEXT: dim = ( 32 )
252    // CHECK-NEXT: lvl = ( 32 )
253    // CHECK-NEXT: pos[0] : ( 0, 5 )
254    // CHECK-NEXT: crd[0] : ( 3, 21, 28, 29, 31 )
255    // CHECK-NEXT: values : ( 48, 204, 252, 304, 360 )
256    // CHECK-NEXT: ----
257    // CHECK:      ---- Sparse Tensor ----
258    // CHECK-NEXT: nse = 32
259    // CHECK-NEXT: dim = ( 32 )
260    // CHECK-NEXT: lvl = ( 32 )
261    // CHECK-NEXT: values : ( 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 204, 0, 0, 0, 0, 0, 0, 252, 304, 0, 360 )
262    // CHECK-NEXT: ----
263    // CHECK-NEXT: 1169.1
264    //
265    sparse_tensor.print %sv1 : tensor<?xf64, #SparseVector>
266    sparse_tensor.print %sv2 : tensor<?xf64, #SparseVector>
267    sparse_tensor.print %0 : tensor<?xf64, #SparseVector>
268    sparse_tensor.print %1 : tensor<?xf64, #SparseVector>
269    sparse_tensor.print %2 : tensor<?xf64, #SparseVector>
270    sparse_tensor.print %3 : tensor<?xf64, #SparseVector>
271    sparse_tensor.print %4 : tensor<?xf64, #DenseVector>
272    %v5 = tensor.extract %5[] : tensor<f64>
273    vector.print %v5 : f64
274
275    // Release the resources.
276    bufferization.dealloc_tensor %sv1 : tensor<?xf64, #SparseVector>
277    bufferization.dealloc_tensor %sv1_dup : tensor<?xf64, #SparseVector>
278    bufferization.dealloc_tensor %sv2 : tensor<?xf64, #SparseVector>
279    bufferization.dealloc_tensor %0 : tensor<?xf64, #SparseVector>
280    // Note: No dealloc for %1 because it was inplace!
281    bufferization.dealloc_tensor %2 : tensor<?xf64, #SparseVector>
282    bufferization.dealloc_tensor %3 : tensor<?xf64, #SparseVector>
283    bufferization.dealloc_tensor %4 : tensor<?xf64, #DenseVector>
284    bufferization.dealloc_tensor %5 : tensor<f64>
285    return
286  }
287}
288