xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/concatenate_dim_1_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 (concatenate on dimension 1)
59  //
60
61  // Concats all sparse matrices (with different encodings) to a sparse matrix.
62  func.func @concat_sparse_sparse_perm_dim1(%arg0: tensor<4x2xf64, #MAT_C_C_P>, %arg1: tensor<4x3xf64, #MAT_C_D>, %arg2: tensor<4x4xf64, #MAT_D_C>) -> tensor<4x9xf64, #MAT_C_C_P> {
63    %0 = sparse_tensor.concatenate %arg0, %arg1, %arg2 {dimension = 1 : index}
64         : tensor<4x2xf64, #MAT_C_C_P>, tensor<4x3xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C> to tensor<4x9xf64, #MAT_C_C_P>
65    return %0 : tensor<4x9xf64, #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_dim1(%arg0: tensor<4x2xf64, #MAT_C_C_P>, %arg1: tensor<4x3xf64, #MAT_C_D_P>, %arg2: tensor<4x4xf64, #MAT_D_C>) -> tensor<4x9xf64> {
70    %0 = sparse_tensor.concatenate %arg0, %arg1, %arg2 {dimension = 1 : index}
71         : tensor<4x2xf64, #MAT_C_C_P>, tensor<4x3xf64, #MAT_C_D_P>, tensor<4x4xf64, #MAT_D_C> to tensor<4x9xf64>
72    return %0 : tensor<4x9xf64>
73  }
74
75  // Concats mix sparse and dense matrices to a sparse matrix.
76  func.func @concat_mix_sparse_perm_dim1(%arg0: tensor<4x2xf64>, %arg1: tensor<4x3xf64, #MAT_C_D_P>, %arg2: tensor<4x4xf64, #MAT_D_C>) -> tensor<4x9xf64, #MAT_C_C> {
77    %0 = sparse_tensor.concatenate %arg0, %arg1, %arg2 {dimension = 1 : index}
78         : tensor<4x2xf64>, tensor<4x3xf64, #MAT_C_D_P>, tensor<4x4xf64, #MAT_D_C> to tensor<4x9xf64, #MAT_C_C>
79    return %0 : tensor<4x9xf64, #MAT_C_C>
80  }
81
82  // Concats mix sparse and dense matrices to a dense matrix.
83  func.func @concat_mix_dense_perm_dim1(%arg0: tensor<4x2xf64>, %arg1: tensor<4x3xf64, #MAT_C_D>, %arg2: tensor<4x4xf64, #MAT_D_C_P>) -> tensor<4x9xf64> {
84    %0 = sparse_tensor.concatenate %arg0, %arg1, %arg2 {dimension = 1 : index}
85         : tensor<4x2xf64>, tensor<4x3xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C_P> to tensor<4x9xf64>
86    return %0 : tensor<4x9xf64>
87  }
88
89  func.func @dump_mat_dense_4x9(%A: tensor<4x9xf64>) {
90    %1 = tensor.cast %A : tensor<4x9xf64> to tensor<*xf64>
91    call @printMemrefF64(%1) : (tensor<*xf64>) -> ()
92
93    return
94  }
95
96  // Driver method to call and verify kernels.
97  func.func @main() {
98    %m42 = arith.constant dense<
99      [ [ 1.0, 0.0 ],
100        [ 3.1, 0.0 ],
101        [ 0.0, 2.0 ],
102        [ 0.0, 0.0 ] ]> : tensor<4x2xf64>
103    %m43 = arith.constant dense<
104      [ [ 1.0, 0.0, 1.0 ],
105        [ 1.0, 0.0, 0.5 ],
106        [ 0.0, 0.0, 1.0 ],
107        [ 5.0, 2.0, 0.0 ] ]> : tensor<4x3xf64>
108    %m24 = arith.constant dense<
109      [ [ 1.0, 0.0, 3.0, 0.0],
110        [ 0.0, 2.0, 0.0, 0.0] ]> : tensor<2x4xf64>
111    %m34 = arith.constant dense<
112      [ [ 1.0, 0.0, 1.0, 1.0],
113        [ 0.0, 0.5, 0.0, 0.0],
114        [ 1.0, 5.0, 2.0, 0.0] ]> : tensor<3x4xf64>
115    %m44 = arith.constant dense<
116      [ [ 0.0, 0.0, 1.5, 1.0],
117        [ 0.0, 3.5, 0.0, 0.0],
118        [ 1.0, 5.0, 2.0, 0.0],
119        [ 1.0, 0.5, 0.0, 0.0] ]> : tensor<4x4xf64>
120
121    %sm42cc = sparse_tensor.convert %m42 : tensor<4x2xf64> to tensor<4x2xf64, #MAT_C_C>
122    %sm43cd = sparse_tensor.convert %m43 : tensor<4x3xf64> to tensor<4x3xf64, #MAT_C_D>
123    %sm44dc = sparse_tensor.convert %m44 : tensor<4x4xf64> to tensor<4x4xf64, #MAT_D_C>
124
125    %sm42ccp = sparse_tensor.convert %m42 : tensor<4x2xf64> to tensor<4x2xf64, #MAT_C_C_P>
126    %sm43cdp = sparse_tensor.convert %m43 : tensor<4x3xf64> to tensor<4x3xf64, #MAT_C_D_P>
127    %sm44dcp = sparse_tensor.convert %m44 : tensor<4x4xf64> to tensor<4x4xf64, #MAT_D_C_P>
128
129    //
130    // CHECK:      ---- Sparse Tensor ----
131    // CHECK-NEXT: nse = 18
132    // CHECK-NEXT: dim = ( 4, 9 )
133    // CHECK-NEXT: lvl = ( 9, 4 )
134    // CHECK-NEXT: pos[0] : ( 0, 9 )
135    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8 )
136    // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 7, 10, 12, 15, 17, 18 )
137    // CHECK-NEXT: crd[1] : ( 0, 1, 2, 0, 1, 3, 3, 0, 1, 2, 2, 3, 1, 2, 3, 0, 2, 0 )
138    // CHECK-NEXT: values : ( 1, 3.1, 2, 1, 1, 5, 2, 1, 0.5, 1, 1, 1, 3.5, 5, 0.5, 1.5, 2, 1 )
139    // CHECK-NEXT: ----
140    //
141    %12 = call @concat_sparse_sparse_perm_dim1(%sm42ccp, %sm43cd, %sm44dc)
142               : (tensor<4x2xf64, #MAT_C_C_P>, tensor<4x3xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C>) -> tensor<4x9xf64, #MAT_C_C_P>
143    sparse_tensor.print %12 : tensor<4x9xf64, #MAT_C_C_P>
144
145    // CHECK:      {{\[}}[1,   0,   1,   0,   1,   0,   0,   1.5,   1],
146    // CHECK-NEXT:  [3.1,   0,   1,   0,   0.5,   0,   3.5,   0,   0],
147    // CHECK-NEXT:  [0,   2,   0,   0,   1,   1,   5,   2,   0],
148    // CHECK-NEXT:  [0,   0,   5,   2,   0,   1,   0.5,   0,   0]]
149    %13 = call @concat_sparse_dense_perm_dim1(%sm42ccp, %sm43cdp, %sm44dc)
150               : (tensor<4x2xf64, #MAT_C_C_P>, tensor<4x3xf64, #MAT_C_D_P>, tensor<4x4xf64, #MAT_D_C>) -> tensor<4x9xf64>
151    call @dump_mat_dense_4x9(%13) : (tensor<4x9xf64>) -> ()
152
153    //
154    // CHECK:      ---- Sparse Tensor ----
155    // CHECK-NEXT: nse = 18
156    // CHECK-NEXT: dim = ( 4, 9 )
157    // CHECK-NEXT: lvl = ( 4, 9 )
158    // CHECK-NEXT: pos[0] : ( 0, 4 )
159    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )
160    // CHECK-NEXT: pos[1] : ( 0, 5, 9, 14, 18 )
161    // CHECK-NEXT: crd[1] : ( 0, 2, 4, 7, 8, 0, 2, 4, 6, 1, 4, 5, 6, 7, 2, 3, 5, 6 )
162    // CHECK-NEXT: values : ( 1, 1, 1, 1.5, 1, 3.1, 1, 0.5, 3.5, 2, 1, 1, 5, 2, 5, 2, 1, 0.5 )
163    // CHECK-NEXT: ----
164    //
165    %14 = call @concat_mix_sparse_perm_dim1(%m42, %sm43cdp, %sm44dc)
166               : (tensor<4x2xf64>, tensor<4x3xf64, #MAT_C_D_P>, tensor<4x4xf64, #MAT_D_C>) -> tensor<4x9xf64, #MAT_C_C>
167    sparse_tensor.print %14 : tensor<4x9xf64, #MAT_C_C>
168
169    // CHECK:      {{\[}}[1,   0,   1,   0,   1,   0,   0,   1.5,   1],
170    // CHECK-NEXT:  [3.1,   0,   1,   0,   0.5,   0,   3.5,   0,   0],
171    // CHECK-NEXT:  [0,   2,   0,   0,   1,   1,   5,   2,   0],
172    // CHECK-NEXT:  [0,   0,   5,   2,   0,   1,   0.5,   0,   0]]
173    %15 = call @concat_mix_dense_perm_dim1(%m42, %sm43cd, %sm44dcp)
174               : (tensor<4x2xf64>, tensor<4x3xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C_P>) -> tensor<4x9xf64>
175    call @dump_mat_dense_4x9(%15) : (tensor<4x9xf64>) -> ()
176
177    // Release resources.
178    bufferization.dealloc_tensor %sm42cc  : tensor<4x2xf64, #MAT_C_C>
179    bufferization.dealloc_tensor %sm43cd  : tensor<4x3xf64, #MAT_C_D>
180    bufferization.dealloc_tensor %sm44dc  : tensor<4x4xf64, #MAT_D_C>
181    bufferization.dealloc_tensor %sm42ccp : tensor<4x2xf64, #MAT_C_C_P>
182    bufferization.dealloc_tensor %sm43cdp : tensor<4x3xf64, #MAT_C_D_P>
183    bufferization.dealloc_tensor %sm44dcp : tensor<4x4xf64, #MAT_D_C_P>
184    bufferization.dealloc_tensor %12 : tensor<4x9xf64, #MAT_C_C_P>
185    bufferization.dealloc_tensor %13 : tensor<4x9xf64>
186    bufferization.dealloc_tensor %14 : tensor<4x9xf64, #MAT_C_C>
187    bufferization.dealloc_tensor %15 : tensor<4x9xf64>
188    return
189  }
190}
191