xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_dot.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<{ map = (d0) -> (d0 : compressed) }>
35
36module {
37
38  //
39  // Sparse kernel.
40  //
41  func.func @sparse_dot(%a: tensor<1024xf32, #SparseVector>,
42                        %b: tensor<1024xf32, #SparseVector>,
43                        %x: tensor<f32>) -> tensor<f32> {
44    %dot = linalg.dot ins(%a, %b: tensor<1024xf32, #SparseVector>,
45                                  tensor<1024xf32, #SparseVector>)
46         outs(%x: tensor<f32>) -> tensor<f32>
47    return %dot : tensor<f32>
48  }
49
50  //
51  // Main driver.
52  //
53  func.func @main() {
54    // Setup two sparse vectors.
55    %d1 = arith.constant sparse<
56        [ [0], [1], [22], [23], [1022] ], [1.0, 2.0, 3.0, 4.0, 5.0]
57    > : tensor<1024xf32>
58    %d2 = arith.constant sparse<
59      [ [22], [1022], [1023] ], [6.0, 7.0, 8.0]
60    > : tensor<1024xf32>
61    %s1 = sparse_tensor.convert %d1 : tensor<1024xf32> to tensor<1024xf32, #SparseVector>
62    %s2 = sparse_tensor.convert %d2 : tensor<1024xf32> to tensor<1024xf32, #SparseVector>
63
64    //
65    // Verify the inputs.
66    //
67    // CHECK:      ---- Sparse Tensor ----
68    // CHECK-NEXT: nse = 5
69    // CHECK-NEXT: dim = ( 1024 )
70    // CHECK-NEXT: lvl = ( 1024 )
71    // CHECK-NEXT: pos[0] : ( 0, 5 )
72    // CHECK-NEXT: crd[0] : ( 0, 1, 22, 23, 1022 )
73    // CHECK-NEXT: values : ( 1, 2, 3, 4, 5 )
74    // CHECK-NEXT: ----
75    //
76    // CHECK:      ---- Sparse Tensor ----
77    // CHECK-NEXT: nse = 3
78    // CHECK-NEXT: dim = ( 1024 )
79    // CHECK-NEXT: lvl = ( 1024 )
80    // CHECK-NEXT: pos[0] : ( 0, 3 )
81    // CHECK-NEXT: crd[0] : ( 22, 1022, 1023 )
82    // CHECK-NEXT: values : ( 6, 7, 8 )
83    // CHECK-NEXT: ----
84    //
85    sparse_tensor.print %s1 : tensor<1024xf32, #SparseVector>
86    sparse_tensor.print %s2 : tensor<1024xf32, #SparseVector>
87
88    // Call the kernel and verify the output.
89    //
90    // CHECK: 53
91    //
92    %t = tensor.empty() : tensor<f32>
93    %z = arith.constant 0.0 : f32
94    %x = tensor.insert %z into %t[] : tensor<f32>
95    %0 = call @sparse_dot(%s1, %s2, %x) : (tensor<1024xf32, #SparseVector>,
96                                           tensor<1024xf32, #SparseVector>,
97                                           tensor<f32>) -> tensor<f32>
98    %1 = tensor.extract %0[] : tensor<f32>
99    vector.print %1 : f32
100
101    // Release the resources.
102    bufferization.dealloc_tensor %0 : tensor<f32>
103    bufferization.dealloc_tensor %s1 : tensor<1024xf32, #SparseVector>
104    bufferization.dealloc_tensor %s2 : tensor<1024xf32, #SparseVector>
105
106    return
107  }
108}
109