xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_tanh.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// Current fails for SVE, see https://github.com/llvm/llvm-project/issues/60626
35// UNSUPPORTED: target=aarch64{{.*}}, mlir_arm_emulator
36
37#SparseVector = #sparse_tensor.encoding<{ map = (d0) -> (d0 : compressed) }>
38
39#trait_op = {
40  indexing_maps = [
41    affine_map<(i) -> (i)>   // X (out)
42  ],
43  iterator_types = ["parallel"],
44  doc = "X(i) = OP X(i)"
45}
46
47module {
48  // Performs zero-preserving math to sparse vector.
49  func.func @sparse_tanh(%vec: tensor<?xf64, #SparseVector>)
50                       -> tensor<?xf64, #SparseVector> {
51    %0 = linalg.generic #trait_op
52      outs(%vec: tensor<?xf64, #SparseVector>) {
53        ^bb(%x: f64):
54          %1 = math.tanh %x : f64
55          linalg.yield %1 : f64
56    } -> tensor<?xf64, #SparseVector>
57    return %0 : tensor<?xf64, #SparseVector>
58  }
59
60  // Driver method to call and verify vector kernels.
61  func.func @main() {
62    // Setup sparse vector.
63    %v1 = arith.constant sparse<
64       [ [0], [3], [11], [17], [20], [21], [28], [29], [31] ],
65         [ -1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0 ]
66    > : tensor<32xf64>
67    %sv1 = sparse_tensor.convert %v1
68         : tensor<32xf64> to tensor<?xf64, #SparseVector>
69
70    // Call sparse vector kernel.
71    %0 = call @sparse_tanh(%sv1) : (tensor<?xf64, #SparseVector>)
72                                 -> tensor<?xf64, #SparseVector>
73
74    //
75    // Verify the results (within some precision).
76    //
77    // CHECK:      ---- Sparse Tensor ----
78    // CHECK-NEXT: nse = 9
79    // CHECK-NEXT: dim = ( 32 )
80    // CHECK-NEXT: lvl = ( 32 )
81    // CHECK-NEXT: pos[0] : ( 0, 9 )
82    // CHECK-NEXT: crd[0] : ( 0, 3, 11, 17, 20, 21, 28, 29, 31 )
83    // CHECK-NEXT: values : ({{ -0.761[0-9]*, 0.761[0-9]*, 0.96[0-9]*, 0.99[0-9]*, 0.99[0-9]*, 0.99[0-9]*, 0.99[0-9]*, 0.99[0-9]*, 1}} )
84    // CHECK-NEXT: ----
85    //
86    sparse_tensor.print %0 : tensor<?xf64, #SparseVector>
87
88    // Release the resources.
89    bufferization.dealloc_tensor %sv1 : tensor<?xf64, #SparseVector>
90    return
91  }
92}
93