xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_scale.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=4
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#CSR = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : dense, d1 : compressed) }>
35
36#trait_scale = {
37  indexing_maps = [
38    affine_map<(i,j) -> (i,j)>   // X (out)
39  ],
40  iterator_types = ["parallel", "parallel"],
41  doc = "X(i,j) = X(i,j) * 2"
42}
43
44//
45// Integration test that lowers a kernel annotated as sparse to actual sparse
46// code, initializes a matching sparse storage scheme from a dense tensor,
47// and runs the resulting code with the JIT compiler.
48//
49module {
50  //
51  // A kernel that scales a sparse matrix A by a factor of 2.0.
52  //
53  func.func @sparse_scale(%argx: tensor<8x8xf32, #CSR>) -> tensor<8x8xf32, #CSR> {
54    %c = arith.constant 2.0 : f32
55    %0 = linalg.generic #trait_scale
56      outs(%argx: tensor<8x8xf32, #CSR>) {
57        ^bb(%x: f32):
58          %1 = arith.mulf %x, %c : f32
59          linalg.yield %1 : f32
60    } -> tensor<8x8xf32, #CSR>
61    return %0 : tensor<8x8xf32, #CSR>
62  }
63
64  //
65  // Main driver that converts a dense tensor into a sparse tensor
66  // and then calls the sparse scaling kernel with the sparse tensor
67  // as input argument.
68  //
69  func.func @main() {
70    %c0 = arith.constant 0 : index
71    %f0 = arith.constant 0.0 : f32
72
73    // Initialize a dense tensor.
74    %0 = arith.constant dense<[
75       [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0],
76       [0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
77       [0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0],
78       [0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0],
79       [0.0, 1.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0],
80       [0.0, 1.0, 1.0, 0.0, 0.0, 6.0, 0.0, 0.0],
81       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 7.0, 1.0],
82       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 8.0]
83    ]> : tensor<8x8xf32>
84
85    // Convert dense tensor to sparse tensor and call sparse kernel.
86    %1 = sparse_tensor.convert %0 : tensor<8x8xf32> to tensor<8x8xf32, #CSR>
87    %2 = call @sparse_scale(%1)
88      : (tensor<8x8xf32, #CSR>) -> tensor<8x8xf32, #CSR>
89
90    // Print the resulting compacted values for verification.
91    //
92    // CHECK:      ---- Sparse Tensor ----
93    // CHECK-NEXT: nse = 16
94    // CHECK-NEXT: dim = ( 8, 8 )
95    // CHECK-NEXT: lvl = ( 8, 8 )
96    // CHECK-NEXT: pos[1] : ( 0, 3, 4, 5, 6, 8, 11, 14, 16 )
97    // CHECK-NEXT: crd[1] : ( 0, 2, 7, 1, 2, 3, 1, 4, 1, 2, 5, 2, 6, 7, 2, 7 )
98    // CHECK-NEXT: values : ( 2, 2, 2, 4, 6, 8, 2, 10, 2, 2, 12, 2, 14, 2, 2, 16 )
99    // CHECK-NEXT: ----
100    //
101    sparse_tensor.print %2 : tensor<8x8xf32, #CSR>
102
103    // Release the resources.
104    bufferization.dealloc_tensor %1 : tensor<8x8xf32, #CSR>
105
106    return
107  }
108}
109