xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_conversion_sparse2dense.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
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 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#Tensor1  = #sparse_tensor.encoding<{
35  map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed)
36}>
37
38#Tensor2  = #sparse_tensor.encoding<{
39  map = (d0, d1, d2) -> (d1 : compressed, d2 : compressed, d0 : compressed),
40}>
41
42#Tensor3  = #sparse_tensor.encoding<{
43  map = (d0, d1, d2) -> (d2 : compressed, d0 : compressed, d1 : compressed),
44}>
45
46#Tensor4  = #sparse_tensor.encoding<{
47  map = (d0, d1, d2) -> (d0 : dense, d1 : compressed, d2 : compressed)
48}>
49
50#Tensor5  = #sparse_tensor.encoding<{
51  map = (d0, d1, d2) -> (d1 : dense, d2 : compressed, d0 : compressed)
52}>
53
54#Tensor6  = #sparse_tensor.encoding<{
55  map = (d0, d1, d2) -> (d2 : dense, d0 : compressed, d1 : compressed)
56}>
57
58//
59// Integration test that tests conversions from sparse to dense tensors.
60//
61module {
62  //
63  // Utilities for output.
64  //
65  func.func @dump_234(%arg0: tensor<2x3x4xf64>) {
66    %c0 = arith.constant 0 : index
67    %d0 = arith.constant -1.0 : f64
68    %0 = vector.transfer_read %arg0[%c0, %c0, %c0], %d0: tensor<2x3x4xf64>, vector<2x3x4xf64>
69    vector.print %0 : vector<2x3x4xf64>
70    return
71  }
72  func.func @dump_p34(%arg0: tensor<?x3x4xf64>) {
73    %0 = tensor.cast %arg0 : tensor<?x3x4xf64> to tensor<2x3x4xf64>
74    call @dump_234(%0) : (tensor<2x3x4xf64>) -> ()
75    return
76  }
77  func.func @dump_2p4(%arg0: tensor<2x?x4xf64>) {
78    %0 = tensor.cast %arg0 : tensor<2x?x4xf64> to tensor<2x3x4xf64>
79    call @dump_234(%0) : (tensor<2x3x4xf64>) -> ()
80    return
81  }
82  func.func @dump_23p(%arg0: tensor<2x3x?xf64>) {
83    %0 = tensor.cast %arg0 : tensor<2x3x?xf64> to tensor<2x3x4xf64>
84    call @dump_234(%0) : (tensor<2x3x4xf64>) -> ()
85    return
86  }
87  func.func @dump_2pp(%arg0: tensor<2x?x?xf64>) {
88    %0 = tensor.cast %arg0 : tensor<2x?x?xf64> to tensor<2x3x4xf64>
89    call @dump_234(%0) : (tensor<2x3x4xf64>) -> ()
90    return
91  }
92  func.func @dump_p3p(%arg0: tensor<?x3x?xf64>) {
93    %0 = tensor.cast %arg0 : tensor<?x3x?xf64> to tensor<2x3x4xf64>
94    call @dump_234(%0) : (tensor<2x3x4xf64>) -> ()
95    return
96  }
97  func.func @dump_pp4(%arg0: tensor<?x?x4xf64>) {
98    %0 = tensor.cast %arg0 : tensor<?x?x4xf64> to tensor<2x3x4xf64>
99    call @dump_234(%0) : (tensor<2x3x4xf64>) -> ()
100    return
101  }
102  func.func @dump_ppp(%arg0: tensor<?x?x?xf64>) {
103    %0 = tensor.cast %arg0 : tensor<?x?x?xf64> to tensor<2x3x4xf64>
104    call @dump_234(%0) : (tensor<2x3x4xf64>) -> ()
105    return
106  }
107
108  //
109  // Main driver.
110  //
111  func.func @main() {
112    //
113    // Initialize a 3-dim dense tensor.
114    //
115    %src = arith.constant dense<[
116       [  [  1.0,  2.0,  3.0,  4.0 ],
117          [  5.0,  6.0,  7.0,  8.0 ],
118          [  9.0, 10.0, 11.0, 12.0 ] ],
119       [  [ 13.0, 14.0, 15.0, 16.0 ],
120          [ 17.0, 18.0, 19.0, 20.0 ],
121          [ 21.0, 22.0, 23.0, 24.0 ] ]
122    ]> : tensor<2x3x4xf64>
123
124    //
125    // Convert dense tensor directly to various sparse tensors.
126    //
127    %s2341 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x4xf64, #Tensor1>
128    %s2342 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x4xf64, #Tensor2>
129    %s2343 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x4xf64, #Tensor3>
130    %s2344 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x4xf64, #Tensor4>
131    %s2345 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x4xf64, #Tensor5>
132    %s2346 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x4xf64, #Tensor6>
133
134    // NOTE: We commented out most cases with the dynamic-sized output tensor because finishing
135    // all of them are currently taking too long, and they are not covering too many new code
136    // paths.
137    %sp344 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<?x3x4xf64, #Tensor4>
138    // %sp345 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<?x3x4xf64, #Tensor5>
139    // %sp346 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<?x3x4xf64, #Tensor6>
140    // %s2p44 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x?x4xf64, #Tensor4>
141    %s2p45 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x?x4xf64, #Tensor5>
142    // %s2p46 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x?x4xf64, #Tensor6>
143    // %s23p4 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x?xf64, #Tensor4>
144    // %s23p5 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x?xf64, #Tensor5>
145    %s23p6 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x?xf64, #Tensor6>
146    // %s2pp4 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x?x?xf64, #Tensor4>
147    // %s2pp5 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x?x?xf64, #Tensor5>
148    // %s2pp6 = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x?x?xf64, #Tensor6>
149
150    //
151    // Convert sparse tensor back to dense.
152    //
153    %d2341 = sparse_tensor.convert %s2341 : tensor<2x3x4xf64, #Tensor1> to tensor<2x3x4xf64>
154    %d2342 = sparse_tensor.convert %s2342 : tensor<2x3x4xf64, #Tensor2> to tensor<2x3x4xf64>
155    %d2343 = sparse_tensor.convert %s2343 : tensor<2x3x4xf64, #Tensor3> to tensor<2x3x4xf64>
156    %d2344 = sparse_tensor.convert %s2344 : tensor<2x3x4xf64, #Tensor4> to tensor<2x3x4xf64>
157    %d2345 = sparse_tensor.convert %s2345 : tensor<2x3x4xf64, #Tensor5> to tensor<2x3x4xf64>
158    %d2346 = sparse_tensor.convert %s2346 : tensor<2x3x4xf64, #Tensor6> to tensor<2x3x4xf64>
159
160    %dp344 = sparse_tensor.convert %sp344 : tensor<?x3x4xf64, #Tensor4> to tensor<?x3x4xf64>
161    // %dp345 = sparse_tensor.convert %sp345 : tensor<?x3x4xf64, #Tensor5> to tensor<?x3x4xf64>
162    // %dp346 = sparse_tensor.convert %sp346 : tensor<?x3x4xf64, #Tensor6> to tensor<?x3x4xf64>
163    // %d2p44 = sparse_tensor.convert %s2p44 : tensor<2x?x4xf64, #Tensor4> to tensor<2x?x4xf64>
164    %d2p45 = sparse_tensor.convert %s2p45 : tensor<2x?x4xf64, #Tensor5> to tensor<2x?x4xf64>
165    // %d2p46 = sparse_tensor.convert %s2p46 : tensor<2x?x4xf64, #Tensor6> to tensor<2x?x4xf64>
166    // %d23p4 = sparse_tensor.convert %s23p4 : tensor<2x3x?xf64, #Tensor4> to tensor<2x3x?xf64>
167    // %d23p5 = sparse_tensor.convert %s23p5 : tensor<2x3x?xf64, #Tensor5> to tensor<2x3x?xf64>
168    %d23p6 = sparse_tensor.convert %s23p6 : tensor<2x3x?xf64, #Tensor6> to tensor<2x3x?xf64>
169    // %d2pp4 = sparse_tensor.convert %s2pp4 : tensor<2x?x?xf64, #Tensor4> to tensor<2x?x?xf64>
170    // %d2pp5 = sparse_tensor.convert %s2pp5 : tensor<2x?x?xf64, #Tensor5> to tensor<2x?x?xf64>
171    // %d2pp6 = sparse_tensor.convert %s2pp6 : tensor<2x?x?xf64, #Tensor6> to tensor<2x?x?xf64>
172
173    %dp3p4 = sparse_tensor.convert %sp344 : tensor<?x3x4xf64, #Tensor4> to tensor<?x3x?xf64>
174    // %dp3p5 = sparse_tensor.convert %sp345 : tensor<?x3x4xf64, #Tensor5> to tensor<?x3x?xf64>
175    // %dp3p6 = sparse_tensor.convert %sp346 : tensor<?x3x4xf64, #Tensor6> to tensor<?x3x?xf64>
176    // %dpp44 = sparse_tensor.convert %s2p44 : tensor<2x?x4xf64, #Tensor4> to tensor<?x?x4xf64>
177    %dpp45 = sparse_tensor.convert %s2p45 : tensor<2x?x4xf64, #Tensor5> to tensor<?x?x4xf64>
178    // %dpp46 = sparse_tensor.convert %s2p46 : tensor<2x?x4xf64, #Tensor6> to tensor<?x?x4xf64>
179    // %dppp4 = sparse_tensor.convert %s2pp4 : tensor<2x?x?xf64, #Tensor4> to tensor<?x?x?xf64>
180    // %dppp5 = sparse_tensor.convert %s2pp5 : tensor<2x?x?xf64, #Tensor5> to tensor<?x?x?xf64>
181    // %dppp6 = sparse_tensor.convert %s2pp6 : tensor<2x?x?xf64, #Tensor6> to tensor<?x?x?xf64>
182
183    //
184    // Check round-trip equality.  And release dense tensors.
185    // CHECK-COUNT-12: ( ( ( 1, 2, 3, 4 ), ( 5, 6, 7, 8 ), ( 9, 10, 11, 12 ) ), ( ( 13, 14, 15, 16 ), ( 17, 18, 19, 20 ), ( 21, 22, 23, 24 ) ) )
186    //   was COUNT-28 before.
187    call @dump_234(%src) : (tensor<2x3x4xf64>) -> ()
188    call @dump_234(%d2341) : (tensor<2x3x4xf64>) -> ()
189    call @dump_234(%d2342) : (tensor<2x3x4xf64>) -> ()
190    call @dump_234(%d2343) : (tensor<2x3x4xf64>) -> ()
191    call @dump_234(%d2344) : (tensor<2x3x4xf64>) -> ()
192    call @dump_234(%d2345) : (tensor<2x3x4xf64>) -> ()
193    call @dump_234(%d2346) : (tensor<2x3x4xf64>) -> ()
194    call @dump_p34(%dp344) : (tensor<?x3x4xf64>) -> ()
195    // call @dump_p34(%dp345) : (tensor<?x3x4xf64>) -> ()
196    // call @dump_p34(%dp346) : (tensor<?x3x4xf64>) -> ()
197    // call @dump_2p4(%d2p44) : (tensor<2x?x4xf64>) -> ()
198    call @dump_2p4(%d2p45) : (tensor<2x?x4xf64>) -> ()
199    // call @dump_2p4(%d2p46) : (tensor<2x?x4xf64>) -> ()
200    // call @dump_23p(%d23p4) : (tensor<2x3x?xf64>) -> ()
201    // call @dump_23p(%d23p5) : (tensor<2x3x?xf64>) -> ()
202    call @dump_23p(%d23p6) : (tensor<2x3x?xf64>) -> ()
203    // call @dump_2pp(%d2pp4) : (tensor<2x?x?xf64>) -> ()
204    // call @dump_2pp(%d2pp5) : (tensor<2x?x?xf64>) -> ()
205    // call @dump_2pp(%d2pp6) : (tensor<2x?x?xf64>) -> ()
206    call @dump_p3p(%dp3p4) : (tensor<?x3x?xf64>) -> ()
207    // call @dump_p3p(%dp3p5) : (tensor<?x3x?xf64>) -> ()
208    // call @dump_p3p(%dp3p6) : (tensor<?x3x?xf64>) -> ()
209    // call @dump_pp4(%dpp44) : (tensor<?x?x4xf64>) -> ()
210    call @dump_pp4(%dpp45) : (tensor<?x?x4xf64>) -> ()
211    // call @dump_pp4(%dpp46) : (tensor<?x?x4xf64>) -> ()
212    // call @dump_ppp(%dppp4) : (tensor<?x?x?xf64>) -> ()
213    // call @dump_ppp(%dppp5) : (tensor<?x?x?xf64>) -> ()
214    // call @dump_ppp(%dppp6) : (tensor<?x?x?xf64>) -> ()
215
216    //
217    // Release sparse tensors.
218    //
219    bufferization.dealloc_tensor %s2341 : tensor<2x3x4xf64, #Tensor1>
220    bufferization.dealloc_tensor %s2342 : tensor<2x3x4xf64, #Tensor2>
221    bufferization.dealloc_tensor %s2343 : tensor<2x3x4xf64, #Tensor3>
222    bufferization.dealloc_tensor %s2344 : tensor<2x3x4xf64, #Tensor4>
223    bufferization.dealloc_tensor %s2345 : tensor<2x3x4xf64, #Tensor5>
224    bufferization.dealloc_tensor %s2346 : tensor<2x3x4xf64, #Tensor6>
225    bufferization.dealloc_tensor %sp344 : tensor<?x3x4xf64, #Tensor4>
226    // bufferization.dealloc_tensor %sp345 : tensor<?x3x4xf64, #Tensor5>
227    // bufferization.dealloc_tensor %sp346 : tensor<?x3x4xf64, #Tensor6>
228    // bufferization.dealloc_tensor %s2p44 : tensor<2x?x4xf64, #Tensor4>
229    bufferization.dealloc_tensor %s2p45 : tensor<2x?x4xf64, #Tensor5>
230    // bufferization.dealloc_tensor %s2p46 : tensor<2x?x4xf64, #Tensor6>
231    // bufferization.dealloc_tensor %s23p4 : tensor<2x3x?xf64, #Tensor4>
232    // bufferization.dealloc_tensor %s23p5 : tensor<2x3x?xf64, #Tensor5>
233    bufferization.dealloc_tensor %s23p6 : tensor<2x3x?xf64, #Tensor6>
234    // bufferization.dealloc_tensor %s2pp4 : tensor<2x?x?xf64, #Tensor4>
235    // bufferization.dealloc_tensor %s2pp5 : tensor<2x?x?xf64, #Tensor5>
236    // bufferization.dealloc_tensor %s2pp6 : tensor<2x?x?xf64, #Tensor6>
237
238    bufferization.dealloc_tensor %d2341 : tensor<2x3x4xf64>
239    bufferization.dealloc_tensor %d2342 : tensor<2x3x4xf64>
240    bufferization.dealloc_tensor %d2343 : tensor<2x3x4xf64>
241    bufferization.dealloc_tensor %d2344 : tensor<2x3x4xf64>
242    bufferization.dealloc_tensor %d2345 : tensor<2x3x4xf64>
243    bufferization.dealloc_tensor %d2346 : tensor<2x3x4xf64>
244    bufferization.dealloc_tensor %dp344 : tensor<?x3x4xf64>
245    bufferization.dealloc_tensor %d2p45 : tensor<2x?x4xf64>
246    bufferization.dealloc_tensor %d23p6 : tensor<2x3x?xf64>
247    bufferization.dealloc_tensor %dp3p4 : tensor<?x3x?xf64>
248    bufferization.dealloc_tensor %dpp45 : tensor<?x?x4xf64>
249
250    return
251  }
252}
253