xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_expand.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#CSC = #sparse_tensor.encoding<{
35  map = (d0, d1) -> (d1 : dense, d0 : compressed)
36}>
37
38module {
39  //
40  // Column-wise storage forces the ijk loop to permute into jki
41  // so that access pattern expansion (workspace) needs to be
42  // done along dimension with size 8.
43  //
44  func.func @matmul(%A: tensor<8x2xf64, #CSC>,
45                    %B: tensor<2x4xf64, #CSC>) -> tensor<8x4xf64, #CSC> {
46    %C = tensor.empty() : tensor<8x4xf64, #CSC>
47    %D = linalg.matmul
48      ins(%A, %B: tensor<8x2xf64, #CSC>, tensor<2x4xf64, #CSC>)
49         outs(%C: tensor<8x4xf64, #CSC>) -> tensor<8x4xf64, #CSC>
50    return %D: tensor<8x4xf64, #CSC>
51  }
52
53  //
54  // Main driver.
55  //
56  func.func @main() {
57    %c0 = arith.constant 0 : index
58    %d1 = arith.constant -1.0 : f64
59
60    // Initialize various dense matrices for stress testing.
61    %da = arith.constant dense<[
62        [ 1.1, 2.1 ],
63        [ 1.2, 2.2 ],
64        [ 1.3, 2.3 ],
65        [ 1.4, 2.4 ],
66        [ 1.5, 2.5 ],
67        [ 1.6, 2.6 ],
68        [ 1.7, 2.7 ],
69        [ 1.8, 2.8 ]
70    ]> : tensor<8x2xf64>
71    %db = arith.constant dense<[
72        [ 10.1, 11.1, 12.1, 13.1 ],
73        [ 10.2, 11.2, 12.2, 13.2 ]
74    ]> : tensor<2x4xf64>
75
76    // Convert all these matrices to sparse format.
77    %x1 = sparse_tensor.convert %da : tensor<8x2xf64> to tensor<8x2xf64, #CSC>
78    %x2 = sparse_tensor.convert %db : tensor<2x4xf64> to tensor<2x4xf64, #CSC>
79
80    // Call kernels with dense.
81    %x3 = call @matmul(%x1, %x2)
82       : (tensor<8x2xf64, #CSC>,
83          tensor<2x4xf64, #CSC>) -> tensor<8x4xf64, #CSC>
84
85    //
86    // CHECK:      ---- Sparse Tensor ----
87    // CHECK-NEXT: nse = 32
88    // CHECK-NEXT: dim = ( 8, 4 )
89    // CHECK-NEXT: lvl = ( 4, 8 )
90    // CHECK-NEXT: pos[1] : ( 0, 8, 16, 24, 32 )
91    // CHECK-NEXT: crd[1] : ( 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0,
92    // CHECK-SAME:            1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 )
93    // CHECK-NEXT: values : ( 32.53, 34.56, 36.59, 38.62, 40.65, 42.68, 44.71, 46.74,
94    // CHECK-SAME:            35.73, 37.96, 40.19, 42.42, 44.65, 46.88, 49.11, 51.34,
95    // CHECK-SAME:            38.93, 41.36, 43.79, 46.22, 48.65, 51.08, 53.51, 55.94,
96    // CHECK-SAME:            42.13, 44.76, 47.39, 50.02, 52.65, 55.28, 57.91, 60.54 )
97    // CHECK-NEXT: ----
98    //
99    sparse_tensor.print %x3 : tensor<8x4xf64, #CSC>
100
101    // Release the resources.
102    bufferization.dealloc_tensor %x1 : tensor<8x2xf64, #CSC>
103    bufferization.dealloc_tensor %x2 : tensor<2x4xf64, #CSC>
104    bufferization.dealloc_tensor %x3 : tensor<8x4xf64, #CSC>
105
106    return
107  }
108}
109