xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/concatenate_dim_0_permute.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#MAT_C_C = #sparse_tensor.encoding<{map = (d0, d1) -> (d0 : compressed, d1 : compressed)}>
35#MAT_D_C = #sparse_tensor.encoding<{map = (d0, d1) -> (d0 : dense, d1 : compressed)}>
36#MAT_C_D = #sparse_tensor.encoding<{map = (d0, d1) -> (d0 : compressed, d1 : dense)}>
37#MAT_D_D = #sparse_tensor.encoding<{
38  map = (d0, d1) -> (d1 : dense, d0 : dense)
39}>
40
41#MAT_C_C_P = #sparse_tensor.encoding<{
42  map = (d0, d1) -> (d1 : compressed, d0 : compressed)
43}>
44
45#MAT_C_D_P = #sparse_tensor.encoding<{
46  map = (d0, d1) -> (d1 : compressed, d0 : dense)
47}>
48
49#MAT_D_C_P = #sparse_tensor.encoding<{
50  map = (d0, d1) -> (d1 : dense, d0 : compressed)
51}>
52
53module {
54  func.func private @printMemrefF64(%ptr : tensor<*xf64>)
55  func.func private @printMemref1dF64(%ptr : memref<?xf64>) attributes { llvm.emit_c_interface }
56
57  //
58  // Tests with permutation.
59  //
60
61  // Concats all sparse matrices (with different encodings) to a sparse matrix.
62  func.func @concat_sparse_sparse_perm(%arg0: tensor<2x4xf64, #MAT_C_C_P>, %arg1: tensor<3x4xf64, #MAT_C_D>, %arg2: tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64, #MAT_C_C_P> {
63    %0 = sparse_tensor.concatenate %arg0, %arg1, %arg2 {dimension = 0 : index}
64         : tensor<2x4xf64, #MAT_C_C_P>, tensor<3x4xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C> to tensor<9x4xf64, #MAT_C_C_P>
65    return %0 : tensor<9x4xf64, #MAT_C_C_P>
66  }
67
68  // Concats all sparse matrices (with different encodings) to a dense matrix.
69  func.func @concat_sparse_dense_perm(%arg0: tensor<2x4xf64, #MAT_C_C_P>, %arg1: tensor<3x4xf64, #MAT_C_D_P>, %arg2: tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64> {
70    %0 = sparse_tensor.concatenate %arg0, %arg1, %arg2 {dimension = 0 : index}
71         : tensor<2x4xf64, #MAT_C_C_P>, tensor<3x4xf64, #MAT_C_D_P>, tensor<4x4xf64, #MAT_D_C> to tensor<9x4xf64>
72    return %0 : tensor<9x4xf64>
73  }
74
75  // Concats mix sparse and dense matrices to a sparse matrix.
76  func.func @concat_mix_sparse_perm(%arg0: tensor<2x4xf64>, %arg1: tensor<3x4xf64, #MAT_C_D_P>, %arg2: tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64, #MAT_C_C> {
77    %0 = sparse_tensor.concatenate %arg0, %arg1, %arg2 {dimension = 0 : index}
78         : tensor<2x4xf64>, tensor<3x4xf64, #MAT_C_D_P>, tensor<4x4xf64, #MAT_D_C> to tensor<9x4xf64, #MAT_C_C>
79    return %0 : tensor<9x4xf64, #MAT_C_C>
80  }
81
82  // Concats mix sparse and dense matrices to a dense matrix.
83  func.func @concat_mix_dense_perm(%arg0: tensor<2x4xf64>, %arg1: tensor<3x4xf64, #MAT_C_D>, %arg2: tensor<4x4xf64, #MAT_D_C_P>) -> tensor<9x4xf64> {
84    %0 = sparse_tensor.concatenate %arg0, %arg1, %arg2 {dimension = 0 : index}
85         : tensor<2x4xf64>, tensor<3x4xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C_P> to tensor<9x4xf64>
86    return %0 : tensor<9x4xf64>
87  }
88
89  func.func @dump_mat_9x4(%A: tensor<9x4xf64, #MAT_C_C>) {
90    %c = sparse_tensor.convert %A : tensor<9x4xf64, #MAT_C_C> to tensor<9x4xf64>
91    %cu = tensor.cast %c : tensor<9x4xf64> to tensor<*xf64>
92    call @printMemrefF64(%cu) : (tensor<*xf64>) -> ()
93
94    %n = sparse_tensor.number_of_entries %A : tensor<9x4xf64, #MAT_C_C>
95    vector.print %n : index
96
97    %1 = sparse_tensor.values %A : tensor<9x4xf64, #MAT_C_C> to memref<?xf64>
98    call @printMemref1dF64(%1) : (memref<?xf64>) -> ()
99
100    return
101  }
102
103  func.func @dump_mat_dense_9x4(%A: tensor<9x4xf64>) {
104    %u = tensor.cast %A : tensor<9x4xf64> to tensor<*xf64>
105    call @printMemrefF64(%u) : (tensor<*xf64>) -> ()
106
107    return
108  }
109
110  // Driver method to call and verify kernels.
111  func.func @main() {
112    %m42 = arith.constant dense<
113      [ [ 1.0, 0.0 ],
114        [ 3.1, 0.0 ],
115        [ 0.0, 2.0 ],
116        [ 0.0, 0.0 ] ]> : tensor<4x2xf64>
117    %m43 = arith.constant dense<
118      [ [ 1.0, 0.0, 1.0 ],
119        [ 1.0, 0.0, 0.5 ],
120        [ 0.0, 0.0, 1.0 ],
121        [ 5.0, 2.0, 0.0 ] ]> : tensor<4x3xf64>
122    %m24 = arith.constant dense<
123      [ [ 1.0, 0.0, 3.0, 0.0],
124        [ 0.0, 2.0, 0.0, 0.0] ]> : tensor<2x4xf64>
125    %m34 = arith.constant dense<
126      [ [ 1.0, 0.0, 1.0, 1.0],
127        [ 0.0, 0.5, 0.0, 0.0],
128        [ 1.0, 5.0, 2.0, 0.0] ]> : tensor<3x4xf64>
129    %m44 = arith.constant dense<
130      [ [ 0.0, 0.0, 1.5, 1.0],
131        [ 0.0, 3.5, 0.0, 0.0],
132        [ 1.0, 5.0, 2.0, 0.0],
133        [ 1.0, 0.5, 0.0, 0.0] ]> : tensor<4x4xf64>
134
135    %sm24cc = sparse_tensor.convert %m24 : tensor<2x4xf64> to tensor<2x4xf64, #MAT_C_C>
136    %sm34cd = sparse_tensor.convert %m34 : tensor<3x4xf64> to tensor<3x4xf64, #MAT_C_D>
137    %sm44dc = sparse_tensor.convert %m44 : tensor<4x4xf64> to tensor<4x4xf64, #MAT_D_C>
138
139    %sm24ccp = sparse_tensor.convert %m24 : tensor<2x4xf64> to tensor<2x4xf64, #MAT_C_C_P>
140    %sm34cdp = sparse_tensor.convert %m34 : tensor<3x4xf64> to tensor<3x4xf64, #MAT_C_D_P>
141    %sm44dcp = sparse_tensor.convert %m44 : tensor<4x4xf64> to tensor<4x4xf64, #MAT_D_C_P>
142
143    //
144    // CHECK:      ---- Sparse Tensor ----
145    // CHECK-NEXT: nse = 18
146    // CHECK-NEXT: dim = ( 9, 4 )
147    // CHECK-NEXT: lvl = ( 4, 9 )
148    // CHECK-NEXT: pos[0] : ( 0, 4 )
149    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )
150    // CHECK-NEXT: pos[1] : ( 0, 5, 11, 16, 18 )
151    // CHECK-NEXT: crd[1] : ( 0, 2, 4, 7, 8, 1, 3, 4, 6, 7, 8, 0, 2, 4, 5, 7, 2, 5 )
152    // CHECK-NEXT: values : ( 1, 1, 1, 1, 1, 2, 0.5, 5, 3.5, 5, 0.5, 3, 1, 2, 1.5, 2, 1, 1 )
153    // CHECK-NEXT: ----
154    //
155    %4 = call @concat_sparse_sparse_perm(%sm24ccp, %sm34cd, %sm44dc)
156               : (tensor<2x4xf64, #MAT_C_C_P>, tensor<3x4xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64, #MAT_C_C_P>
157    sparse_tensor.print %4 : tensor<9x4xf64, #MAT_C_C_P>
158
159    // CHECK:      {{\[}}[1,   0,   3,   0],
160    // CHECK-NEXT:  [0,   2,   0,   0],
161    // CHECK-NEXT:  [1,   0,   1,   1],
162    // CHECK-NEXT:  [0,   0.5,   0,   0],
163    // CHECK-NEXT:  [1,   5,   2,   0],
164    // CHECK-NEXT:  [0,   0,   1.5,   1],
165    // CHECK-NEXT:  [0,   3.5,   0,   0],
166    // CHECK-NEXT:  [1,   5,   2,   0],
167    // CHECK-NEXT:  [1,   0.5,   0,   0]]
168    %5 = call @concat_sparse_dense_perm(%sm24ccp, %sm34cdp, %sm44dc)
169               : (tensor<2x4xf64, #MAT_C_C_P>, tensor<3x4xf64, #MAT_C_D_P>, tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64>
170    call @dump_mat_dense_9x4(%5) : (tensor<9x4xf64>) -> ()
171
172    //
173    // CHECK:      ---- Sparse Tensor ----
174    // CHECK-NEXT: nse = 18
175    // CHECK-NEXT: dim = ( 9, 4 )
176    // CHECK-NEXT: lvl = ( 9, 4 )
177    // CHECK-NEXT: pos[0] : ( 0, 9 )
178    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8 )
179    // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 7, 10, 12, 13, 16, 18 )
180    // CHECK-NEXT: crd[1] : ( 0, 2, 1, 0, 2, 3, 1, 0, 1, 2, 2, 3, 1, 0, 1, 2, 0, 1 )
181    // CHECK-NEXT: values : ( 1, 3, 2, 1, 1, 1, 0.5, 1, 5, 2, 1.5, 1, 3.5, 1, 5, 2, 1, 0.5 )
182    // CHECK-NEXT: ----
183    //
184    %6 = call @concat_mix_sparse_perm(%m24, %sm34cdp, %sm44dc)
185               : (tensor<2x4xf64>, tensor<3x4xf64, #MAT_C_D_P>, tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64, #MAT_C_C>
186    sparse_tensor.print %6 : tensor<9x4xf64, #MAT_C_C>
187
188    // CHECK:      {{\[}}[1,   0,   3,   0],
189    // CHECK-NEXT:  [0,   2,   0,   0],
190    // CHECK-NEXT:  [1,   0,   1,   1],
191    // CHECK-NEXT:  [0,   0.5,   0,   0],
192    // CHECK-NEXT:  [1,   5,   2,   0],
193    // CHECK-NEXT:  [0,   0,   1.5,   1],
194    // CHECK-NEXT:  [0,   3.5,   0,   0],
195    // CHECK-NEXT:  [1,   5,   2,   0],
196    // CHECK-NEXT:  [1,   0.5,   0,   0]]
197    %7 = call @concat_mix_dense_perm(%m24, %sm34cd, %sm44dcp)
198               : (tensor<2x4xf64>, tensor<3x4xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C_P>) -> tensor<9x4xf64>
199    call @dump_mat_dense_9x4(%7) : (tensor<9x4xf64>) -> ()
200
201    // Release resources.
202    bufferization.dealloc_tensor %sm24cc  : tensor<2x4xf64, #MAT_C_C>
203    bufferization.dealloc_tensor %sm34cd  : tensor<3x4xf64, #MAT_C_D>
204    bufferization.dealloc_tensor %sm44dc  : tensor<4x4xf64, #MAT_D_C>
205    bufferization.dealloc_tensor %sm24ccp : tensor<2x4xf64, #MAT_C_C_P>
206    bufferization.dealloc_tensor %sm34cdp : tensor<3x4xf64, #MAT_C_D_P>
207    bufferization.dealloc_tensor %sm44dcp : tensor<4x4xf64, #MAT_D_C_P>
208    bufferization.dealloc_tensor %4  : tensor<9x4xf64, #MAT_C_C_P>
209    bufferization.dealloc_tensor %5  : tensor<9x4xf64>
210    bufferization.dealloc_tensor %6  : tensor<9x4xf64, #MAT_C_C>
211    bufferization.dealloc_tensor %7  : tensor<9x4xf64>
212    return
213  }
214}
215