xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_reduce_custom_prod.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// Product reductions - kept in a separate file as these are not supported by
32// the AArch64 SVE backend (so the set-up is a bit different to
33// sparse_reducitons.mlir)
34
35#SparseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : compressed)}>
36#CSR = #sparse_tensor.encoding<{map = (d0, d1) -> (d0 : dense, d1 : compressed)}>
37#CSC = #sparse_tensor.encoding<{
38  map = (d0, d1) -> (d1 : dense, d0 : compressed)
39}>
40
41//
42// Traits for tensor operations.
43//
44
45#trait_mat_reduce_rowwise = {
46  indexing_maps = [
47    affine_map<(i,j) -> (i,j)>,  // A (in)
48    affine_map<(i,j) -> (i)>   // X (out)
49  ],
50  iterator_types = ["parallel", "reduction"],
51  doc = "X(i) = PROD_j A(i,j)"
52}
53
54module {
55  func.func @redProdLex(%arga: tensor<?x?xf64, #CSR>) -> tensor<?xf64, #SparseVector> {
56    %c0 = arith.constant 0 : index
57    %cf1 = arith.constant 1.0 : f64
58    %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #CSR>
59    %xv = tensor.empty(%d0): tensor<?xf64, #SparseVector>
60    %0 = linalg.generic #trait_mat_reduce_rowwise
61      ins(%arga: tensor<?x?xf64, #CSR>)
62      outs(%xv: tensor<?xf64, #SparseVector>) {
63        ^bb(%a: f64, %b: f64):
64          %1 = sparse_tensor.reduce %a, %b, %cf1 : f64 {
65              ^bb0(%x: f64, %y: f64):
66                %2 = arith.mulf %x, %y : f64
67                sparse_tensor.yield %2 : f64
68            }
69          linalg.yield %1 : f64
70    } -> tensor<?xf64, #SparseVector>
71    return %0 : tensor<?xf64, #SparseVector>
72  }
73
74  func.func @redProdExpand(%arga: tensor<?x?xf64, #CSC>) -> tensor<?xf64, #SparseVector> {
75    %c0 = arith.constant 0 : index
76    %cf1 = arith.constant 1.0 : f64
77    %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #CSC>
78    %xv = tensor.empty(%d0): tensor<?xf64, #SparseVector>
79    %0 = linalg.generic #trait_mat_reduce_rowwise
80      ins(%arga: tensor<?x?xf64, #CSC>)
81      outs(%xv: tensor<?xf64, #SparseVector>) {
82        ^bb(%a: f64, %b: f64):
83          %1 = sparse_tensor.reduce %a, %b, %cf1 : f64 {
84              ^bb0(%x: f64, %y: f64):
85                %2 = arith.mulf %x, %y : f64
86                sparse_tensor.yield %2 : f64
87            }
88          linalg.yield %1 : f64
89    } -> tensor<?xf64, #SparseVector>
90    return %0 : tensor<?xf64, #SparseVector>
91  }
92
93
94  // Driver method to call and verify vector kernels.
95  func.func @main() {
96    %c0 = arith.constant 0 : index
97
98    // Setup sparse matrices.
99    %m1 = arith.constant sparse<
100       [ [0,0], [0,1], [1,0], [2,2], [2,3], [2,4], [3,0], [3,2], [3,3] ],
101         [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]
102    > : tensor<4x5xf64>
103    %m2 = arith.constant sparse<
104       [ [0,0], [1,3], [2,0], [2,3], [3,1], [4,1] ],
105         [6.0, 5.0, 4.0, 3.0, 2.0, 11.0 ]
106    > : tensor<5x4xf64>
107    %sm1 = sparse_tensor.convert %m1 : tensor<4x5xf64> to tensor<?x?xf64, #CSR>
108    %sm2r = sparse_tensor.convert %m2 : tensor<5x4xf64> to tensor<?x?xf64, #CSR>
109    %sm2c = sparse_tensor.convert %m2 : tensor<5x4xf64> to tensor<?x?xf64, #CSC>
110
111    // Call sparse matrix kernels.
112    %1 = call @redProdLex(%sm1) : (tensor<?x?xf64, #CSR>) -> tensor<?xf64, #SparseVector>
113    %2 = call @redProdExpand(%sm2c) : (tensor<?x?xf64, #CSC>) -> tensor<?xf64, #SparseVector>
114
115    //
116    // Verify the results.
117    //
118    // CHECK:      ---- Sparse Tensor ----
119    // CHECK-NEXT: nse = 9
120    // CHECK-NEXT: dim = ( 4, 5 )
121    // CHECK-NEXT: lvl = ( 4, 5 )
122    // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 )
123    // CHECK-NEXT: crd[1] : ( 0, 1, 0, 2, 3, 4, 0, 2, 3 )
124    // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, 6, 7, 8, 9 )
125    // CHECK-NEXT: ----
126    // CHECK:      ---- Sparse Tensor ----
127    // CHECK-NEXT: nse = 6
128    // CHECK-NEXT: dim = ( 5, 4 )
129    // CHECK-NEXT: lvl = ( 5, 4 )
130    // CHECK-NEXT: pos[1] : ( 0, 1, 2, 4, 5, 6 )
131    // CHECK-NEXT: crd[1] : ( 0, 3, 0, 3, 1, 1 )
132    // CHECK-NEXT: values : ( 6, 5, 4, 3, 2, 11 )
133    // CHECK-NEXT: ----
134    // CHECK:      ---- Sparse Tensor ----
135    // CHECK-NEXT: nse = 4
136    // CHECK-NEXT: dim = ( 4 )
137    // CHECK-NEXT: lvl = ( 4 )
138    // CHECK-NEXT: pos[0] : ( 0, 4 )
139    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )
140    // CHECK-NEXT: values : ( 2, 3, 120, 504 )
141    // CHECK-NEXT: ----
142    // CHECK:      ---- Sparse Tensor ----
143    // CHECK-NEXT: nse = 5
144    // CHECK-NEXT: dim = ( 5 )
145    // CHECK-NEXT: lvl = ( 5 )
146    // CHECK-NEXT: pos[0] : ( 0, 5 )
147    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3, 4 )
148    // CHECK-NEXT: values : ( 6, 5, 12, 2, 11 )
149    // CHECK-NEXT: ----
150    //
151    sparse_tensor.print %sm1 : tensor<?x?xf64, #CSR>
152    sparse_tensor.print %sm2r : tensor<?x?xf64, #CSR>
153    sparse_tensor.print %1 : tensor<?xf64, #SparseVector>
154    sparse_tensor.print %2 : tensor<?xf64, #SparseVector>
155
156    // Release the resources.
157    bufferization.dealloc_tensor %sm1 : tensor<?x?xf64, #CSR>
158    bufferization.dealloc_tensor %sm2r : tensor<?x?xf64, #CSR>
159    bufferization.dealloc_tensor %sm2c : tensor<?x?xf64, #CSC>
160    bufferization.dealloc_tensor %1 : tensor<?xf64, #SparseVector>
161    bufferization.dealloc_tensor %2 : tensor<?xf64, #SparseVector>
162    return
163  }
164}
165