xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_sign.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 enable-buffer-initialization=true
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=4 enable-buffer-initialization=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#SparseVector = #sparse_tensor.encoding<{ map = (d0) -> (d0 : compressed) }>
35
36#trait_op = {
37  indexing_maps = [
38    affine_map<(i) -> (i)>, // a
39    affine_map<(i) -> (i)>  // x (out)
40  ],
41  iterator_types = ["parallel"],
42  doc = "x(i) = OP a(i)"
43}
44
45module {
46  // Performs sign operation (using semi-ring unary op)
47  // with semantics that
48  // > 0 : +1.0
49  // < 0 : -1.0
50  // +Inf: +1.0
51  // -Inf: -1.0
52  // +NaN: +NaN
53  // -NaN: -NaN
54  // +0.0: +0.0
55  // -0.0: -0.0
56  func.func @sparse_sign(%arg0: tensor<?xf64, #SparseVector>)
57                             -> tensor<?xf64, #SparseVector> {
58    %c0 = arith.constant 0 : index
59    %d = tensor.dim %arg0, %c0 : tensor<?xf64, #SparseVector>
60    %xin = tensor.empty(%d) : tensor<?xf64, #SparseVector>
61    %0 = linalg.generic #trait_op
62      ins(%arg0: tensor<?xf64, #SparseVector>)
63      outs(%xin: tensor<?xf64, #SparseVector>) {
64      ^bb0(%a: f64, %x: f64) :
65        %result = sparse_tensor.unary %a : f64 to f64
66          present={
67            ^bb1(%s: f64):
68              %z = arith.constant 0.0 : f64
69              %1 = arith.cmpf one, %s, %z : f64
70              %2 = arith.uitofp %1 : i1 to f64
71              %3 = math.copysign %2, %s : f64
72              %4 = arith.cmpf uno, %s, %s : f64
73              %5 = arith.select %4, %s, %3 : f64
74              sparse_tensor.yield %5 : f64
75          }
76          absent={}
77        linalg.yield %result : f64
78    } -> tensor<?xf64, #SparseVector>
79    return %0 : tensor<?xf64, #SparseVector>
80  }
81
82  // Driver method to call and verify sign kernel.
83  func.func @main() {
84    %c0 = arith.constant 0 : index
85    %du = arith.constant 0.0 : f64
86
87    %pnan = arith.constant 0x7FF0000001000000 : f64
88    %nnan = arith.constant 0xFFF0000001000000 : f64
89    %pinf = arith.constant 0x7FF0000000000000 : f64
90    %ninf = arith.constant 0xFFF0000000000000 : f64
91
92    // Setup sparse vector.
93    %v1 = arith.constant sparse<
94       [ [0], [3], [5], [11], [13], [17], [18], [20], [21], [28], [29], [31] ],
95         [ -1.5, 1.5, -10.2, 11.3, 1.0, -1.0,
96           0x7FF0000001000000, // +NaN
97           0xFFF0000001000000, // -NaN
98           0x7FF0000000000000, // +Inf
99           0xFFF0000000000000, // -Inf
100           -0.0,               // -Zero
101           0.0                 // +Zero
102        ]
103    > : tensor<32xf64>
104    %sv1 = sparse_tensor.convert %v1
105         : tensor<32xf64> to tensor<?xf64, #SparseVector>
106
107    // Call sign kernel.
108    %0 = call @sparse_sign(%sv1) : (tensor<?xf64, #SparseVector>)
109                                 -> tensor<?xf64, #SparseVector>
110
111    //
112    // Verify the results.
113    //
114    // CHECK:      ---- Sparse Tensor ----
115    // CHECK-NEXT: nse = 12
116    // CHECK-NEXT: dim = ( 32 )
117    // CHECK-NEXT: lvl = ( 32 )
118    // CHECK-NEXT: pos[0] : ( 0, 12 )
119    // CHECK-NEXT: crd[0] : ( 0, 3, 5, 11, 13, 17, 18, 20, 21, 28, 29, 31 )
120    // CHECK-NEXT: values : ( -1, 1, -1, 1, 1, -1, nan, -nan, 1, -1, -0, 0 )
121    // CHECK-NEXT: ----
122    //
123    sparse_tensor.print %0 : tensor<?xf64, #SparseVector>
124
125    // Release the resources.
126    bufferization.dealloc_tensor %sv1 : tensor<?xf64, #SparseVector>
127    bufferization.dealloc_tensor %0 : tensor<?xf64, #SparseVector>
128    return
129  }
130}
131
132
133