xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_out_reduction.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 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#SparseMatrix = #sparse_tensor.encoding<{
35  map = (d0, d1) -> (d0 : compressed, d1 : compressed)
36}>
37
38#SparseTensor = #sparse_tensor.encoding<{
39  map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed)
40}>
41
42#redsum = {
43  indexing_maps = [
44    affine_map<(i,j,k) -> (i,j,k)>, // A
45    affine_map<(i,j,k) -> (i,j,k)>, // B
46    affine_map<(i,j,k) -> (i,j)>    // X (out)
47  ],
48  iterator_types = ["parallel", "parallel", "reduction"],
49  doc = "X(i,j) = SUM_k A(i,j,k) * B(i,j,k)"
50}
51
52module {
53  func.func @redsum(%arga: tensor<?x?x?xi32, #SparseTensor>,
54               %argb: tensor<?x?x?xi32, #SparseTensor>)
55                   -> tensor<?x?xi32, #SparseMatrix> {
56    %c0 = arith.constant 0 : index
57    %c1 = arith.constant 1 : index
58    %d0 = tensor.dim %arga, %c0 : tensor<?x?x?xi32, #SparseTensor>
59    %d1 = tensor.dim %arga, %c1 : tensor<?x?x?xi32, #SparseTensor>
60    %xinit = tensor.empty(%d0, %d1): tensor<?x?xi32, #SparseMatrix>
61    %0 = linalg.generic #redsum
62      ins(%arga, %argb: tensor<?x?x?xi32, #SparseTensor>,
63                        tensor<?x?x?xi32, #SparseTensor>)
64      outs(%xinit: tensor<?x?xi32, #SparseMatrix>) {
65        ^bb(%a: i32, %b: i32, %x: i32):
66          %0 = arith.muli %a, %b : i32
67          %1 = arith.addi %x, %0 : i32
68          linalg.yield %1 : i32
69    } -> tensor<?x?xi32, #SparseMatrix>
70    return %0 : tensor<?x?xi32, #SparseMatrix>
71  }
72
73  // Driver method to call and verify tensor kernel.
74  func.func @main() {
75    // Setup very sparse 3-d tensors.
76    %t1 = arith.constant sparse<
77       [ [1,1,3], [2,0,0], [2,2,1], [2,2,2], [2,2,3] ], [ 1, 2, 3, 4, 5 ]
78    > : tensor<3x3x4xi32>
79    %t2 = arith.constant sparse<
80       [ [1,0,0], [1,1,3], [2,2,1], [2,2,3] ], [ 6, 7, 8, 9 ]
81    > : tensor<3x3x4xi32>
82    %st1 = sparse_tensor.convert %t1
83      : tensor<3x3x4xi32> to tensor<?x?x?xi32, #SparseTensor>
84    %st2 = sparse_tensor.convert %t2
85      : tensor<3x3x4xi32> to tensor<?x?x?xi32, #SparseTensor>
86
87    // Call kernel.
88    %0 = call @redsum(%st1, %st2)
89      : (tensor<?x?x?xi32, #SparseTensor>,
90         tensor<?x?x?xi32, #SparseTensor>) -> tensor<?x?xi32, #SparseMatrix>
91
92    //
93    // Verify results. Only two entries stored in result. Correct structure.
94    //
95    // CHECK:      ---- Sparse Tensor ----
96    // CHECK-NEXT: nse = 2
97    // CHECK-NEXT: dim = ( 3, 3 )
98    // CHECK-NEXT: lvl = ( 3, 3 )
99    // CHECK-NEXT: pos[0] : ( 0, 2 )
100    // CHECK-NEXT: crd[0] : ( 1, 2 )
101    // CHECK-NEXT: pos[1] : ( 0, 1, 2 )
102    // CHECK-NEXT: crd[1] : ( 1, 2 )
103    // CHECK-NEXT: values : ( 7, 69 )
104    // CHECK-NEXT: ----
105    //
106    sparse_tensor.print %0 : tensor<?x?xi32, #SparseMatrix>
107
108    // Release the resources.
109    bufferization.dealloc_tensor %st1 : tensor<?x?x?xi32, #SparseTensor>
110    bufferization.dealloc_tensor %st2 : tensor<?x?x?xi32, #SparseTensor>
111    bufferization.dealloc_tensor %0 : tensor<?x?xi32, #SparseMatrix>
112
113    return
114  }
115}
116