xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_pooling_nhwc.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: %{sparsifier_opts} = enable-runtime-library=true
22// RUN: %{compile} | %{run} | FileCheck %s
23//
24// Do the same run, but now with direct IR generation.
25// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true
26// RUN: %{compile} | %{run} | FileCheck %s
27
28#CCCC = #sparse_tensor.encoding<{  map = (d0, d1, d2, d3) -> (d0 : compressed, d1 : compressed, d2 : compressed, d3 : compressed), posWidth = 32, crdWidth = 32 }>
29
30func.func @pooling_nhwc_sum_CCCC(%input: tensor<1x4x4x1xf32, #CCCC>, %filter: tensor<2x2xf32>) -> tensor<1x3x3x1xf32, #CCCC> {
31  %init = tensor.empty() : tensor<1x3x3x1xf32, #CCCC>
32  %0 = linalg.pooling_nhwc_sum {dilations = dense<1> : tensor<2xi64>,
33                                strides = dense<1> : tensor<2xi64>}
34     ins (%input, %filter: tensor<1x4x4x1xf32, #CCCC>, tensor<2x2xf32>)
35    outs (%init: tensor<1x3x3x1xf32, #CCCC>) -> tensor<1x3x3x1xf32, #CCCC>
36  return %0 : tensor<1x3x3x1xf32, #CCCC>
37}
38
39func.func @pooling_nhwc_sum(%input: tensor<1x4x4x1xf32>, %filter: tensor<2x2xf32>) -> tensor<1x3x3x1xf32> {
40  %init = arith.constant dense<[[ [[0.0], [0.0], [0.0]],
41                                  [[0.0], [0.0], [0.0]],
42                                  [[0.0], [0.0], [0.0]] ]]> : tensor<1x3x3x1xf32>
43  %0 = linalg.pooling_nhwc_sum {dilations = dense<1> : tensor<2xi64>,
44                                strides = dense<1> : tensor<2xi64>}
45     ins (%input, %filter: tensor<1x4x4x1xf32>, tensor<2x2xf32>)
46    outs (%init: tensor<1x3x3x1xf32>) -> tensor<1x3x3x1xf32>
47  return %0 : tensor<1x3x3x1xf32>
48}
49
50
51func.func @main() {
52  %c0 = arith.constant 0 : index
53  %zero = arith.constant 0.00000e+00 : f32
54
55  %filter = arith.constant dense<
56     [[  1.0,  0.0],
57      [  0.0,  1.0]]
58  > : tensor<2x2xf32>
59
60  %in_dense = arith.constant dense<
61     [[[[1.0],  [2.0],  [1.0],  [2.0]],
62       [[1.0],  [2.0],  [1.0],  [2.0]],
63       [[1.0],  [2.0],  [1.0],  [2.0]],
64       [[1.0],  [2.0],  [1.0],  [2.0]]]]
65  > : tensor<1x4x4x1xf32>
66
67  %in_CCCC = sparse_tensor.convert %in_dense : tensor<1x4x4x1xf32> to tensor<1x4x4x1xf32, #CCCC>
68
69  %dense_ret = call @pooling_nhwc_sum(%in_dense, %filter) : (tensor<1x4x4x1xf32>, tensor<2x2xf32>) -> tensor<1x3x3x1xf32>
70  %CCCC_ret = call @pooling_nhwc_sum_CCCC(%in_CCCC, %filter) : (tensor<1x4x4x1xf32, #CCCC>, tensor<2x2xf32>) -> tensor<1x3x3x1xf32, #CCCC>
71
72  // CHECK: ( ( ( ( 6 ), ( 6 ), ( 6 ) ), ( ( 6 ), ( 6 ), ( 6 ) ), ( ( 6 ), ( 6 ), ( 6 ) ) ) )
73  %dense_v = vector.transfer_read %dense_ret[%c0, %c0, %c0, %c0], %zero
74      : tensor<1x3x3x1xf32>, vector<1x3x3x1xf32>
75  vector.print %dense_v : vector<1x3x3x1xf32>
76
77  //
78  // Sparse pooling should have the same output.
79  //
80  // CHECK:      ---- Sparse Tensor ----
81  // CHECK-NEXT: nse = 9
82  // CHECK-NEXT: dim = ( 1, 3, 3, 1 )
83  // CHECK-NEXT: lvl = ( 1, 3, 3, 1 )
84  // CHECK-NEXT: pos[0] : ( 0, 1 )
85  // CHECK-NEXT: crd[0] : ( 0 )
86  // CHECK-NEXT: pos[1] : ( 0, 3 )
87  // CHECK-NEXT: crd[1] : ( 0, 1, 2 )
88  // CHECK-NEXT: pos[2] : ( 0, 3, 6, 9 )
89  // CHECK-NEXT: crd[2] : ( 0, 1, 2, 0, 1, 2, 0, 1, 2 )
90  // CHECK-NEXT: pos[3] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
91  // CHECK-NEXT: crd[3] : ( 0, 0, 0, 0, 0, 0, 0, 0, 0 )
92  // CHECK-NEXT: values : ( 6, 6, 6, 6, 6, 6, 6, 6, 6 )
93  // CHECK-NEXT: ----
94  //
95  sparse_tensor.print %CCCC_ret : tensor<1x3x3x1xf32, #CCCC>
96
97  // Releases resources.
98  bufferization.dealloc_tensor %in_CCCC : tensor<1x4x4x1xf32, #CCCC>
99  bufferization.dealloc_tensor %CCCC_ret : tensor<1x3x3x1xf32, #CCCC>
100  bufferization.dealloc_tensor %dense_ret : tensor<1x3x3x1xf32>
101  return
102}
103