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#ST = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed", "compressed"]}>
8
9//
10// Trait for 3-d tensor element wise multiplication.
11//
12#trait_mul = {
13  indexing_maps = [
14    affine_map<(i,j,k) -> (i,j,k)>,  // A (in)
15    affine_map<(i,j,k) -> (i,j,k)>,  // B (in)
16    affine_map<(i,j,k) -> (i,j,k)>   // X (out)
17  ],
18  iterator_types = ["parallel", "parallel", "parallel"],
19  doc = "X(i,j,k) = A(i,j,k) * B(i,j,k)"
20}
21
22module {
23  // Multiplies two 3-d sparse tensors element-wise into a new sparse tensor.
24  func.func @tensor_mul(%arga: tensor<?x?x?xf64, #ST>,
25                        %argb: tensor<?x?x?xf64, #ST>) -> tensor<?x?x?xf64, #ST> {
26    %c0 = arith.constant 0 : index
27    %c1 = arith.constant 1 : index
28    %c2 = arith.constant 2 : index
29    %d0 = tensor.dim %arga, %c0 : tensor<?x?x?xf64, #ST>
30    %d1 = tensor.dim %arga, %c1 : tensor<?x?x?xf64, #ST>
31    %d2 = tensor.dim %arga, %c2 : tensor<?x?x?xf64, #ST>
32    %xt = bufferization.alloc_tensor(%d0, %d1, %d2) : tensor<?x?x?xf64, #ST>
33    %0 = linalg.generic #trait_mul
34       ins(%arga, %argb: tensor<?x?x?xf64, #ST>, tensor<?x?x?xf64, #ST>)
35        outs(%xt: tensor<?x?x?xf64, #ST>) {
36        ^bb(%a: f64, %b: f64, %x: f64):
37          %1 = arith.mulf %a, %b : f64
38          linalg.yield %1 : f64
39    } -> tensor<?x?x?xf64, #ST>
40    return %0 : tensor<?x?x?xf64, #ST>
41  }
42
43  // Driver method to call and verify tensor multiplication kernel.
44  func.func @entry() {
45    %c0 = arith.constant 0 : index
46    %default_val = arith.constant -1.0 : f64
47
48    // Setup sparse tensor A
49    %ta = arith.constant dense<
50      [ [ [1.0, 0.0, 0.0, 0.0, 0.0 ],
51          [0.0, 0.0, 0.0, 0.0, 0.0 ],
52          [1.2, 0.0, 3.5, 0.0, 0.0 ] ],
53        [ [0.0, 0.0, 0.0, 0.0, 0.0 ],
54          [0.0, 0.0, 0.0, 0.0, 0.0 ],
55          [0.0, 0.0, 0.0, 0.0, 0.0 ] ],
56        [ [2.0, 0.0, 0.0, 0.0, 0.0 ],
57          [0.0, 0.0, 0.0, 0.0, 0.0 ],
58          [0.0, 0.0, 4.0, 0.0, 0.0 ]] ]> : tensor<3x3x5xf64>
59
60    // Setup sparse tensor B
61    %tb = arith.constant dense<
62      [ [ [0.0, 0.0, 0.0, 0.0, 4.0 ],
63          [0.0, 0.0, 0.0, 0.0, 0.0 ],
64          [2.0, 0.0, 1.0, 0.0, 0.0 ] ],
65        [ [0.0, 0.0, 0.0, 0.0, 9.0 ],
66          [0.0, 0.0, 0.0, 0.0, 0.0 ],
67          [0.0, 7.0, 0.0, 0.0, 0.0 ] ],
68        [ [1.0, 0.0, 0.0, 0.0, 0.0 ],
69          [0.0, 0.0, 0.0, 0.0, 0.0 ],
70          [0.0, 0.0, 2.0, 0.0, 0.0 ]] ]> : tensor<3x3x5xf64>
71
72    %sta = sparse_tensor.convert %ta : tensor<3x3x5xf64> to tensor<?x?x?xf64, #ST>
73    %stb = sparse_tensor.convert %tb : tensor<3x3x5xf64> to tensor<?x?x?xf64, #ST>
74
75
76    // Call sparse tensor multiplication kernel.
77    %0 = call @tensor_mul(%sta, %stb)
78      : (tensor<?x?x?xf64, #ST>, tensor<?x?x?xf64, #ST>) -> tensor<?x?x?xf64, #ST>
79
80    // Verify results
81    //
82    // CHECK:      ( 2.4, 3.5, 2, 8, -1, -1, -1, -1 )
83    // CHECK-NEXT: ( ( ( 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0 ), ( 2.4, 0, 3.5, 0, 0 ) ),
84    // CHECK-SAME: ( ( 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0 ) ),
85    // CHECK-SAME: ( ( 2, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0 ), ( 0, 0, 8, 0, 0 ) ) )
86    //
87    %m1 = sparse_tensor.values %0  : tensor<?x?x?xf64, #ST> to memref<?xf64>
88    %v1 = vector.transfer_read %m1[%c0], %default_val: memref<?xf64>, vector<8xf64>
89    vector.print %v1 : vector<8xf64>
90
91    // Print %0 in dense form.
92    %dt = sparse_tensor.convert %0 : tensor<?x?x?xf64, #ST> to tensor<?x?x?xf64>
93    %v2 = vector.transfer_read %dt[%c0, %c0, %c0], %default_val: tensor<?x?x?xf64>, vector<3x3x5xf64>
94    vector.print %v2 : vector<3x3x5xf64>
95
96    // Release the resources.
97    bufferization.dealloc_tensor %sta : tensor<?x?x?xf64, #ST>
98    bufferization.dealloc_tensor %stb : tensor<?x?x?xf64, #ST>
99    bufferization.dealloc_tensor %0  : tensor<?x?x?xf64, #ST>
100    return
101  }
102}
103