xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_sum_bf16.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// RUN: %{compile} | %{run} | FileCheck %s
21//
22// Do the same run, but now with direct IR generation.
23// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false
24// RUN: %{compile} | %{run} | FileCheck %s
25//
26// Do the same run, but now with vectorization.
27// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true
28// RUN: %{compile} | %{run} | FileCheck %s
29//
30// Do the same run, but now with  VLA vectorization.
31// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}
32
33// UNSUPPORTED: target=aarch64{{.*}}, mlir_arm_emulator
34
35!Filename = !llvm.ptr
36
37#SparseMatrix = #sparse_tensor.encoding<{
38  map = (d0, d1) -> (d0 : compressed, d1 : compressed)
39}>
40
41#trait_sum_reduce = {
42  indexing_maps = [
43    affine_map<(i,j) -> (i,j)>, // A
44    affine_map<(i,j) -> ()>     // x (out)
45  ],
46  iterator_types = ["reduction", "reduction"],
47  doc = "x += A(i,j)"
48}
49
50module {
51  //
52  // A kernel that sum-reduces a matrix to a single scalar.
53  //
54  func.func @kernel_sum_reduce(%arga: tensor<?x?xbf16, #SparseMatrix>,
55                               %argx: tensor<bf16>) -> tensor<bf16> {
56    %0 = linalg.generic #trait_sum_reduce
57      ins(%arga: tensor<?x?xbf16, #SparseMatrix>)
58      outs(%argx: tensor<bf16>) {
59      ^bb(%a: bf16, %x: bf16):
60        %0 = arith.addf %x, %a : bf16
61        linalg.yield %0 : bf16
62    } -> tensor<bf16>
63    return %0 : tensor<bf16>
64  }
65
66  func.func private @getTensorFilename(index) -> (!Filename)
67
68  //
69  // Main driver that reads matrix from file and calls the sparse kernel.
70  //
71  func.func @main() {
72    // Setup input sparse matrix from compressed constant.
73    %d = arith.constant dense <[
74       [ 1.1,  1.2,  0.0,  1.4 ],
75       [ 0.0,  0.0,  0.0,  0.0 ],
76       [ 3.1,  0.0,  3.3,  3.4 ]
77    ]> : tensor<3x4xbf16>
78    %a = sparse_tensor.convert %d : tensor<3x4xbf16> to tensor<?x?xbf16, #SparseMatrix>
79
80    %d0 = arith.constant 0.0 : bf16
81    // Setup memory for a single reduction scalar,
82    // initialized to zero.
83    %x = tensor.from_elements %d0 : tensor<bf16>
84
85    // Call the kernel.
86    %0 = call @kernel_sum_reduce(%a, %x)
87      : (tensor<?x?xbf16, #SparseMatrix>, tensor<bf16>) -> tensor<bf16>
88
89    // Print the result for verification.
90    //
91    // CHECK: 13.5
92    //
93    %v = tensor.extract %0[] : tensor<bf16>
94    %vf = arith.extf %v: bf16 to f32
95    vector.print %vf : f32
96
97    // Release the resources.
98    bufferization.dealloc_tensor %a : tensor<?x?xbf16, #SparseMatrix>
99    bufferization.dealloc_tensor %0 : tensor<bf16>
100
101    return
102  }
103}
104