1// RUN: mlir-opt %s --sparse-compiler | \
2// RUN: mlir-cpu-runner \
3// RUN:  -e entry -entry-point-result=void  \
4// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
5// RUN: FileCheck %s
6
7#SparseVector = #sparse_tensor.encoding<{ dimLevelType = [ "compressed" ] }>
8
9#trait_op = {
10  indexing_maps = [
11    affine_map<(i) -> (i)>   // X (out)
12  ],
13  iterator_types = ["parallel"],
14  doc = "X(i) = OP X(i)"
15}
16
17module {
18  // Performs zero-preserving math to sparse vector.
19  func.func @sparse_tanh(%vec: tensor<?xf64, #SparseVector>
20                          {linalg.inplaceable = true})
21                       -> tensor<?xf64, #SparseVector> {
22    %0 = linalg.generic #trait_op
23      outs(%vec: tensor<?xf64, #SparseVector>) {
24        ^bb(%x: f64):
25          %1 = math.tanh %x : f64
26          linalg.yield %1 : f64
27    } -> tensor<?xf64, #SparseVector>
28    return %0 : tensor<?xf64, #SparseVector>
29  }
30
31  // Dumps a sparse vector of type f64.
32  func.func @dump_vec_f64(%arg0: tensor<?xf64, #SparseVector>) {
33    // Dump the values array to verify only sparse contents are stored.
34    %c0 = arith.constant 0 : index
35    %d0 = arith.constant -1.0 : f64
36    %0 = sparse_tensor.values %arg0
37      : tensor<?xf64, #SparseVector> to memref<?xf64>
38    %1 = vector.transfer_read %0[%c0], %d0: memref<?xf64>, vector<32xf64>
39    vector.print %1 : vector<32xf64>
40    // Dump the dense vector to verify structure is correct.
41    %dv = sparse_tensor.convert %arg0
42        : tensor<?xf64, #SparseVector> to tensor<?xf64>
43    %2 = bufferization.to_memref %dv : memref<?xf64>
44    %3 = vector.transfer_read %2[%c0], %d0: memref<?xf64>, vector<32xf64>
45    vector.print %3 : vector<32xf64>
46    memref.dealloc %2 : memref<?xf64>
47    return
48  }
49
50  // Driver method to call and verify vector kernels.
51  func.func @entry() {
52    // Setup sparse vector.
53    %v1 = arith.constant sparse<
54       [ [0], [3], [11], [17], [20], [21], [28], [29], [31] ],
55         [ -1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0 ]
56    > : tensor<32xf64>
57    %sv1 = sparse_tensor.convert %v1
58         : tensor<32xf64> to tensor<?xf64, #SparseVector>
59
60    // Call sparse vector kernel.
61    %0 = call @sparse_tanh(%sv1) : (tensor<?xf64, #SparseVector>)
62                                 -> tensor<?xf64, #SparseVector>
63
64    //
65    // Verify the results (within some precision).
66    //
67    // CHECK: {{( -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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 )}}
68    // CHECK-NEXT {{( -0.761[0-9]*, 0, 0, 0.761[0-9]*, 0, 0, 0, 0, 0, 0, 0, 0.96[0-9]*, 0, 0, 0, 0, 0, 0.99[0-9]*, 0, 0, 0.99[0-9]*, 0.99[0-9]*, 0, 0, 0, 0, 0, 0, 0.99[0-9]*, 0.99[0-9]*, 0, 1 )}}
69    //
70    call @dump_vec_f64(%sv1) : (tensor<?xf64, #SparseVector>) -> ()
71
72    // Release the resources.
73    sparse_tensor.release %sv1 : tensor<?xf64, #SparseVector>
74    return
75  }
76}
77