xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_reshape.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#SparseVector = #sparse_tensor.encoding<{
35  map = (d0) -> (d0 : compressed)
36}>
37
38#SparseMatrix = #sparse_tensor.encoding<{
39  map = (d0, d1) -> (d0 : compressed, d1 : compressed)
40}>
41
42#Sparse3dTensor = #sparse_tensor.encoding<{
43  map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed)
44}>
45
46module {
47
48  func.func @reshape0(%arg0: tensor<3x4xf64, #SparseMatrix>) -> tensor<2x6xf64, #SparseMatrix> {
49    %shape = arith.constant dense <[ 2, 6 ]> : tensor<2xi32>
50    %0 = tensor.reshape %arg0(%shape) : (tensor<3x4xf64, #SparseMatrix>, tensor<2xi32>) -> tensor<2x6xf64, #SparseMatrix>
51    return %0 : tensor<2x6xf64, #SparseMatrix>
52  }
53
54  func.func @reshape1(%arg0: tensor<3x4xf64, #SparseMatrix>) -> tensor<12xf64, #SparseVector> {
55    %shape = arith.constant dense <[ 12 ]> : tensor<1xi32>
56    %0 = tensor.reshape %arg0(%shape) : (tensor<3x4xf64, #SparseMatrix>, tensor<1xi32>) -> tensor<12xf64, #SparseVector>
57    return %0 : tensor<12xf64, #SparseVector>
58  }
59
60  func.func @reshape2(%arg0: tensor<3x4xf64, #SparseMatrix>) -> tensor<2x3x2xf64, #Sparse3dTensor> {
61    %shape = arith.constant dense <[ 2, 3, 2 ]> : tensor<3xi32>
62    %0 = tensor.reshape %arg0(%shape) : (tensor<3x4xf64, #SparseMatrix>, tensor<3xi32>) -> tensor<2x3x2xf64, #Sparse3dTensor>
63    return %0 : tensor<2x3x2xf64, #Sparse3dTensor>
64  }
65
66
67  func.func @main() {
68    %m = arith.constant dense <[ [ 1.1,  0.0,  1.3,  0.0 ],
69                                 [ 2.1,  0.0,  2.3,  0.0 ],
70                                 [ 3.1,  0.0,  3.3,  0.0 ]]> : tensor<3x4xf64>
71    %sm = sparse_tensor.convert %m : tensor<3x4xf64> to tensor<3x4xf64, #SparseMatrix>
72
73    %reshaped0 = call @reshape0(%sm) : (tensor<3x4xf64, #SparseMatrix>) -> tensor<2x6xf64, #SparseMatrix>
74    %reshaped1 = call @reshape1(%sm) : (tensor<3x4xf64, #SparseMatrix>) -> tensor<12xf64, #SparseVector>
75    %reshaped2 = call @reshape2(%sm) : (tensor<3x4xf64, #SparseMatrix>) -> tensor<2x3x2xf64, #Sparse3dTensor>
76
77    %c0 = arith.constant 0 : index
78    %df = arith.constant -1.0 : f64
79
80    //
81    // CHECK:      ---- Sparse Tensor ----
82    // CHECK-NEXT: nse = 6
83    // CHECK-NEXT: dim = ( 2, 6 )
84    // CHECK-NEXT: lvl = ( 2, 6 )
85    // CHECK-NEXT: pos[0] : ( 0, 2 )
86    // CHECK-NEXT: crd[0] : ( 0, 1 )
87    // CHECK-NEXT: pos[1] : ( 0, 3, 6 )
88    // CHECK-NEXT: crd[1] : ( 0, 2, 4, 0, 2, 4 )
89    // CHECK-NEXT: values : ( 1.1, 1.3, 2.1, 2.3, 3.1, 3.3 )
90    // CHECK-NEXT: ----
91    // CHECK:      ---- Sparse Tensor ----
92    // CHECK-NEXT: nse = 6
93    // CHECK-NEXT: dim = ( 12 )
94    // CHECK-NEXT: lvl = ( 12 )
95    // CHECK-NEXT: pos[0] : ( 0, 6 )
96    // CHECK-NEXT: crd[0] : ( 0, 2, 4, 6, 8, 10 )
97    // CHECK-NEXT: values : ( 1.1, 1.3, 2.1, 2.3, 3.1, 3.3 )
98    // CHECK-NEXT: ----
99    // CHECK:      ---- Sparse Tensor ----
100    // CHECK-NEXT: nse = 6
101    // CHECK-NEXT: dim = ( 2, 3, 2 )
102    // CHECK-NEXT: lvl = ( 2, 3, 2 )
103    // CHECK-NEXT: pos[0] : ( 0, 2 )
104    // CHECK-NEXT: crd[0] : ( 0, 1 )
105    // CHECK-NEXT: pos[1] : ( 0, 3, 6 )
106    // CHECK-NEXT: crd[1] : ( 0, 1, 2, 0, 1, 2 )
107    // CHECK-NEXT: pos[2] : ( 0, 1, 2, 3, 4, 5, 6 )
108    // CHECK-NEXT: crd[2] : ( 0, 0, 0, 0, 0, 0 )
109    // CHECK-NEXT: values : ( 1.1, 1.3, 2.1, 2.3, 3.1, 3.3 )
110    // CHECK-NEXT: ----
111    //
112    sparse_tensor.print %reshaped0: tensor<2x6xf64, #SparseMatrix>
113    sparse_tensor.print %reshaped1: tensor<12xf64, #SparseVector>
114    sparse_tensor.print %reshaped2: tensor<2x3x2xf64, #Sparse3dTensor>
115
116    bufferization.dealloc_tensor %sm : tensor<3x4xf64, #SparseMatrix>
117    bufferization.dealloc_tensor %reshaped0 : tensor<2x6xf64, #SparseMatrix>
118    bufferization.dealloc_tensor %reshaped1 : tensor<12xf64, #SparseVector>
119    bufferization.dealloc_tensor %reshaped2 : tensor<2x3x2xf64, #Sparse3dTensor>
120
121    return
122  }
123
124}
125