xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_conv_1d_nwc_wcf.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 enable-buffer-initialization=true
25// RUN: %{compile} | %{run} | FileCheck %s
26//
27// Do the same run, but now with direct IR generation and vectorization.
28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true vl=2 reassociate-fp-reductions=true enable-index-optimizations=true
29// RUN: %{compile} | %{run} | FileCheck %s
30//
31// Do the same run, but now with direct IR generation and VLA vectorization.
32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}
33
34#CCC = #sparse_tensor.encoding<{
35  map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed) }>
36
37#CDC = #sparse_tensor.encoding<{
38  map = (d0, d1, d2) -> (d0 : compressed, d1 : dense, d2 : compressed)
39
40  // FIXME: Still inadmissible might need investigation
41  // dimToLvl = affine_map<(i,j,k) -> (j,k,i)>
42}>
43
44// Creates and returns 3-D buffer of size (%s1, %s2, %s3) filled with the value %f
45func.func @alloc_3d_filled_f32(%s1 : index, %s2 : index, %s3 : index, %f : f32) -> tensor<?x?x?xf32> {
46  %buf = tensor.empty(%s1, %s2, %s3) : tensor<?x?x?xf32>
47  %ret = linalg.fill ins(%f : f32) outs(%buf : tensor<?x?x?xf32>) -> tensor<?x?x?xf32>
48  return %ret : tensor<?x?x?xf32>
49}
50
51func.func @conv_1d_nwc_wcf(%arg0: tensor<?x?x?xf32>, %arg1: tensor<?x?x?xf32>, %arg2: tensor<?x?x?xf32>) -> tensor<?x?x?xf32> {
52  %ret = linalg.conv_1d_nwc_wcf {dilations = dense<1> : tensor<1xi64>,
53                                   strides = dense<1> : tensor<1xi64>}
54     ins (%arg0, %arg1: tensor<?x?x?xf32>, tensor<?x?x?xf32>)
55    outs (%arg2: tensor<?x?x?xf32>) -> tensor<?x?x?xf32>
56  return %ret : tensor<?x?x?xf32>
57}
58
59func.func @conv_1d_nwc_wcf_CCC(%arg0: tensor<?x?x?xf32, #CCC>, %arg1: tensor<?x?x?xf32>) -> tensor<?x?x?xf32, #CCC> {
60  %c1 = arith.constant 1 : index
61  %c3 = arith.constant 3 : index
62  %c6 = arith.constant 6 : index
63  %s = tensor.empty(%c3, %c6, %c1) : tensor<?x?x?xf32, #CCC>
64  %ret = linalg.conv_1d_nwc_wcf {dilations = dense<1> : tensor<1xi64>,
65                                   strides = dense<1> : tensor<1xi64>}
66     ins (%arg0, %arg1: tensor<?x?x?xf32, #CCC>, tensor<?x?x?xf32>)
67    outs (%s: tensor<?x?x?xf32, #CCC>) -> tensor<?x?x?xf32, #CCC>
68  return %ret : tensor<?x?x?xf32, #CCC>
69}
70
71func.func @conv_1d_nwc_wcf_CDC(%arg0: tensor<?x?x?xf32, #CDC>, %arg1: tensor<?x?x?xf32>) -> tensor<?x?x?xf32, #CDC> {
72  %c1 = arith.constant 1 : index
73  %c3 = arith.constant 3 : index
74  %c6 = arith.constant 6 : index
75  %s = tensor.empty(%c3, %c6, %c1) : tensor<?x?x?xf32, #CDC>
76  %ret = linalg.conv_1d_nwc_wcf {dilations = dense<1> : tensor<1xi64>,
77                                   strides = dense<1> : tensor<1xi64>}
78     ins (%arg0, %arg1: tensor<?x?x?xf32, #CDC>, tensor<?x?x?xf32>)
79    outs (%s: tensor<?x?x?xf32, #CDC>) -> tensor<?x?x?xf32, #CDC>
80  return %ret : tensor<?x?x?xf32, #CDC>
81}
82
83func.func @main() {
84  %c0 = arith.constant 0 : index
85  %c1 = arith.constant 1 : index
86  %c3 = arith.constant 3 : index
87  %c6 = arith.constant 6 : index
88  %c8 = arith.constant 8 : index
89  %f10 = arith.constant 10.00000e+00 : f32
90  %val = arith.constant 2.00000e+00 : f32
91  %zero = arith.constant 0.00000e+00 : f32
92
93  %in1D_tmp = call @alloc_3d_filled_f32(%c3, %c8, %c1, %val) : (index, index, index, f32) -> (tensor<?x?x?xf32>)
94  %in1D_nwc = tensor.insert %f10 into %in1D_tmp[%c0, %c3, %c0] : tensor<?x?x?xf32>
95
96  %filter1D_nwc = call @alloc_3d_filled_f32(%c3, %c1, %c1, %val) : (index, index, index, f32) -> (tensor<?x?x?xf32>)
97  %out1D_nwc = call @alloc_3d_filled_f32(%c3, %c6, %c1, %zero) : (index, index, index, f32) -> (tensor<?x?x?xf32>)
98
99  %in1D_nwc_CCC = sparse_tensor.convert %in1D_nwc
100    : tensor<?x?x?xf32> to tensor<?x?x?xf32, #CCC>
101  %in1D_nwc_CDC = sparse_tensor.convert %in1D_nwc
102    : tensor<?x?x?xf32> to tensor<?x?x?xf32, #CDC>
103
104  %dense_ret = call @conv_1d_nwc_wcf(%in1D_nwc, %filter1D_nwc, %out1D_nwc) : (tensor<?x?x?xf32>, tensor<?x?x?xf32>, tensor<?x?x?xf32>) -> (tensor<?x?x?xf32>)
105  %CCC_ret = call @conv_1d_nwc_wcf_CCC(%in1D_nwc_CCC, %filter1D_nwc) : (tensor<?x?x?xf32, #CCC>, tensor<?x?x?xf32>) -> (tensor<?x?x?xf32, #CCC>)
106  %CDC_ret = call @conv_1d_nwc_wcf_CDC(%in1D_nwc_CDC, %filter1D_nwc) : (tensor<?x?x?xf32, #CDC>, tensor<?x?x?xf32>) -> (tensor<?x?x?xf32, #CDC>)
107
108  //      CHECK: ( ( ( 12 ), ( 28 ), ( 28 ), ( 28 ), ( 12 ), ( 12 ) ),
109  // CHECK-SAME:   ( ( 12 ), ( 12 ), ( 12 ), ( 12 ), ( 12 ), ( 12 ) ),
110  // CHECK-SAME:   ( ( 12 ), ( 12 ), ( 12 ), ( 12 ), ( 12 ), ( 12 ) ) )
111  %dense_v = vector.transfer_read %dense_ret[%c0, %c0, %c0], %zero
112      : tensor<?x?x?xf32>, vector<3x6x1xf32>
113  vector.print %dense_v : vector<3x6x1xf32>
114
115  //
116  // CHECK:      ---- Sparse Tensor ----
117  // CHECK-NEXT: nse = 18
118  // CHECK-NEXT: dim = ( 3, 6, 1 )
119  // CHECK-NEXT: lvl = ( 3, 6, 1 )
120  // CHECK-NEXT: pos[0] : ( 0, 3 )
121  // CHECK-NEXT: crd[0] : ( 0, 1, 2 )
122  // CHECK-NEXT: pos[1] : ( 0, 6, 12, 18 )
123  // CHECK-NEXT: crd[1] : ( 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5 )
124  // CHECK-NEXT: pos[2] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 )
125  // CHECK-NEXT: crd[2] : ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )
126  // CHECK-NEXT: values : ( 12, 28, 28, 28, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12 )
127  // CHECK-NEXT: ----
128  //
129  sparse_tensor.print %CCC_ret : tensor<?x?x?xf32, #CCC>
130
131  //
132  // CHECK:      ---- Sparse Tensor ----
133  // CHECK-NEXT: nse = 18
134  // CHECK-NEXT: dim = ( 3, 6, 1 )
135  // CHECK-NEXT: lvl = ( 3, 6, 1 )
136  // CHECK-NEXT: pos[0] : ( 0, 3 )
137  // CHECK-NEXT: crd[0] : ( 0, 1, 2 )
138  // CHECK-NEXT: pos[2] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 )
139  // CHECK-NEXT: crd[2] : ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )
140  // CHECK-NEXT: values : ( 12, 28, 28, 28, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12 )
141  // CHECK-NEXT: ----
142  //
143  sparse_tensor.print %CDC_ret : tensor<?x?x?xf32, #CDC>
144
145  // Free the resources
146  bufferization.dealloc_tensor %in1D_nwc : tensor<?x?x?xf32>
147  bufferization.dealloc_tensor %filter1D_nwc : tensor<?x?x?xf32>
148  bufferization.dealloc_tensor %out1D_nwc : tensor<?x?x?xf32>
149
150  bufferization.dealloc_tensor %in1D_nwc_CDC : tensor<?x?x?xf32, #CDC>
151  bufferization.dealloc_tensor %in1D_nwc_CCC : tensor<?x?x?xf32, #CCC>
152
153  bufferization.dealloc_tensor %CCC_ret : tensor<?x?x?xf32, #CCC>
154  bufferization.dealloc_tensor %CDC_ret : tensor<?x?x?xf32, #CDC>
155
156  return
157}
158