xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_tensor_ops.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 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  VLA vectorization.
32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}
33
34#ST1 = #sparse_tensor.encoding<{map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed)}>
35#ST2 = #sparse_tensor.encoding<{map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : dense)}>
36
37//
38// Trait for 3-d tensor operation.
39//
40#trait_scale = {
41  indexing_maps = [
42    affine_map<(i,j,k) -> (i,j,k)>,  // A (in)
43    affine_map<(i,j,k) -> (i,j,k)>   // X (out)
44  ],
45  iterator_types = ["parallel", "parallel", "parallel"],
46  doc = "X(i,j,k) = A(i,j,k) * 2.0"
47}
48
49module {
50  // Scales a sparse tensor into a new sparse tensor.
51  func.func @tensor_scale(%arga: tensor<?x?x?xf64, #ST1>) -> tensor<?x?x?xf64, #ST2> {
52    %s = arith.constant 2.0 : f64
53    %c0 = arith.constant 0 : index
54    %c1 = arith.constant 1 : index
55    %c2 = arith.constant 2 : index
56    %d0 = tensor.dim %arga, %c0 : tensor<?x?x?xf64, #ST1>
57    %d1 = tensor.dim %arga, %c1 : tensor<?x?x?xf64, #ST1>
58    %d2 = tensor.dim %arga, %c2 : tensor<?x?x?xf64, #ST1>
59    %xm = tensor.empty(%d0, %d1, %d2) : tensor<?x?x?xf64, #ST2>
60    %0 = linalg.generic #trait_scale
61       ins(%arga: tensor<?x?x?xf64, #ST1>)
62        outs(%xm: tensor<?x?x?xf64, #ST2>) {
63        ^bb(%a: f64, %x: f64):
64          %1 = arith.mulf %a, %s : f64
65          linalg.yield %1 : f64
66    } -> tensor<?x?x?xf64, #ST2>
67    return %0 : tensor<?x?x?xf64, #ST2>
68  }
69
70  // Driver method to call and verify tensor kernel.
71  func.func @main() {
72    %c0 = arith.constant 0 : index
73    %d1 = arith.constant -1.0 : f64
74
75    // Setup sparse tensor.
76    %t = arith.constant dense<
77      [ [ [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
78          [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
79          [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
80          [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0 ] ],
81        [ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
82          [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
83          [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
84          [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ],
85        [ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
86          [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ],
87          [0.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0, 5.0 ],
88          [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ] ]> : tensor<3x4x8xf64>
89    %st = sparse_tensor.convert %t : tensor<3x4x8xf64> to tensor<?x?x?xf64, #ST1>
90
91    // Call sparse vector kernels.
92    %0 = call @tensor_scale(%st) : (tensor<?x?x?xf64, #ST1>) -> tensor<?x?x?xf64, #ST2>
93
94    //
95    // Sanity check on stored values.
96    //
97    // CHECK:      ---- Sparse Tensor ----
98    // CHECK-NEXT: nse = 5
99    // CHECK-NEXT: dim = ( 3, 4, 8 )
100    // CHECK-NEXT: lvl = ( 3, 4, 8 )
101    // CHECK-NEXT: pos[0] : ( 0, 2 )
102    // CHECK-NEXT: crd[0] : ( 0, 2 )
103    // CHECK-NEXT: pos[1] : ( 0, 2, 3 )
104    // CHECK-NEXT: crd[1] : ( 0, 3, 2 )
105    // CHECK-NEXT: pos[2] : ( 0, 1, 2, 5 )
106    // CHECK-NEXT: crd[2] : ( 0, 7, 1, 2, 7 )
107    // CHECK-NEXT: values : ( 1, 2, 3, 4, 5 )
108    // CHECK-NEXT: ----
109    // CHECK:      ---- Sparse Tensor ----
110    // CHECK-NEXT: nse = 24
111    // CHECK-NEXT: dim = ( 3, 4, 8 )
112    // CHECK-NEXT: lvl = ( 3, 4, 8 )
113    // CHECK-NEXT: pos[0] : ( 0, 2 )
114    // CHECK-NEXT: crd[0] : ( 0, 2 )
115    // CHECK-NEXT: pos[1] : ( 0, 2, 3 )
116    // CHECK-NEXT: crd[1] : ( 0, 3, 2 )
117    // CHECK-NEXT: values : ( 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 6, 8, 0, 0, 0, 0, 10 )
118    // CHECK-NEXT: ----
119    //
120    sparse_tensor.print %st : tensor<?x?x?xf64, #ST1>
121    sparse_tensor.print %0  : tensor<?x?x?xf64, #ST2>
122
123    // Release the resources.
124    bufferization.dealloc_tensor %st : tensor<?x?x?xf64, #ST1>
125    bufferization.dealloc_tensor %0  : tensor<?x?x?xf64, #ST2>
126    return
127  }
128}
129