xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_flatten.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// REDEFINE: %{env} = TENSOR0="%mlir_src_dir/test/Integration/data/test.tns"
22// RUN: %{compile} | env %{env} %{run} | FileCheck %s
23//
24// Do the same run, but now with direct IR generation.
25// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false
26// RUN: %{compile} | env %{env} %{run} | FileCheck %s
27//
28// Do the same run, but now with direct IR generation and vectorization.
29// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true
30// RUN: %{compile} | env %{env} %{run} | FileCheck %s
31//
32// Do the same run, but now with direct IR generation and VLA vectorization.
33// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | env %{env} %{run_sve} | FileCheck %s %}
34
35!Filename = !llvm.ptr
36
37#SparseTensor = #sparse_tensor.encoding<{
38  // Note that any dimToLvl permutation should give the same results
39  // since, even though it impacts the sparse storage scheme layout,
40  // it should not change the semantics.
41  map = (d0, d1, d2, d3,
42         d4, d5, d6, d7) -> (d7 : compressed, d6 : compressed,
43                             d1 : compressed, d2 : compressed,
44                             d0 : compressed, d3 : compressed,
45                             d4 : compressed, d5 : compressed)
46}>
47
48#trait_flatten = {
49  indexing_maps = [
50    affine_map<(i,j,k,l,m,n,o,p) -> (i,j,k,l,m,n,o,p)>, // A
51    affine_map<(i,j,k,l,m,n,o,p) -> (i,j)>              // X (out)
52  ],
53  iterator_types = [ "parallel",  "parallel",  "reduction", "reduction",
54                     "reduction", "reduction", "reduction", "reduction" ],
55  doc = "X(i,j) += A(i,j,k,l,m,n,o,p)"
56}
57
58//
59// Integration test that lowers a kernel annotated as sparse to
60// actual sparse code, initializes a matching sparse storage scheme
61// from file, and runs the resulting code with the JIT compiler.
62//
63module {
64  //
65  // A kernel that flattens a rank 8 tensor into a dense matrix.
66  //
67  func.func @kernel_flatten(%arga: tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>,
68                            %argx: tensor<7x3xf64>)
69                                -> tensor<7x3xf64> {
70    %0 = linalg.generic #trait_flatten
71      ins(%arga: tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>)
72      outs(%argx: tensor<7x3xf64>) {
73      ^bb(%a: f64, %x: f64):
74        %0 = arith.addf %x, %a : f64
75        linalg.yield %0 : f64
76    } -> tensor<7x3xf64>
77    return %0 : tensor<7x3xf64>
78  }
79
80  func.func private @getTensorFilename(index) -> (!Filename)
81  func.func private @printMemrefF64(%ptr : tensor<*xf64>)
82
83  //
84  // Main driver that reads tensor from file and calls the sparse kernel.
85  //
86  func.func @main() {
87    %d0 = arith.constant 0.0 : f64
88    %c0 = arith.constant 0 : index
89    %c1 = arith.constant 1 : index
90    %c3 = arith.constant 3 : index
91    %c7 = arith.constant 7 : index
92
93    // Setup matrix memory that is initialized to zero.
94    %x = arith.constant dense<0.000000e+00> : tensor<7x3xf64>
95
96    // Read the sparse tensor from file, construct sparse storage.
97    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
98    %a = sparse_tensor.new %fileName : !Filename to tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>
99
100    // Call the kernel.
101    %0 = call @kernel_flatten(%a, %x)
102      : (tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>, tensor<7x3xf64>) -> tensor<7x3xf64>
103
104    // Print the result for verification.
105    //
106    // CHECK:      {{\[}}[6.25,   0,   0],
107    // CHECK-NEXT: [4.224,   6.21,   0],
108    // CHECK-NEXT: [0,   0,   15.455],
109    // CHECK-NEXT: [0,   0,   0],
110    // CHECK-NEXT: [0,   0,   0],
111    // CHECK-NEXT: [0,   0,   0],
112    // CHECK-NEXT: [7,   0,   0]]
113    //
114    %1 = tensor.cast %0 : tensor<7x3xf64> to tensor<*xf64>
115    call @printMemrefF64(%1) : (tensor<*xf64>) -> ()
116
117    // Release the resources.
118    bufferization.dealloc_tensor %a : tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>
119    bufferization.dealloc_tensor %0 : tensor<7x3xf64>
120
121    return
122  }
123}
124