blob: 5e3385d4ab816bd7b1443d2329bdd2241ae26b4f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasdff29352020-12-31 15:35:17 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuIm2ColKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010029#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CPP/Validate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000037#include "arm_compute/core/utils/misc/ShapeCalculator.h"
38
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039#include <arm_neon.h>
40#include <cstddef>
41#include <cstdint>
42#include <cstring>
43#include <tuple>
44
Manuel Bottini90028992021-06-30 18:29:18 +010045namespace arm_compute
46{
Giorgio Arena368e6352018-08-20 15:06:07 +010047using namespace misc::shape_calculator;
Manuel Bottini90028992021-06-30 18:29:18 +010048namespace cpu
49{
50namespace kernels
51{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052namespace
53{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000054Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +010055 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Georgios Pinitasd912fd82017-11-27 21:00:13 +000056{
Anthony Barbiereaefd002018-07-20 17:49:35 +010057 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Georgios Pinitasdff29352020-12-31 15:35:17 +000058 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +000059 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::BFLOAT16, DataType::F16, DataType::F32);
Georgios Pinitas6e1791b2019-12-02 19:01:25 +000060 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(input->data_type()) && has_bias);
Alex Gilday7da29b62018-03-23 14:16:00 +000061 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
Sheri Zhangac6499a2021-02-10 15:32:38 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups > 1, "Number of groups greater than one are not supported on Neon");
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000063
Georgios Pinitasdff29352020-12-31 15:35:17 +000064 // Since there's no implicit padding added, check the total input spatial dimensions (with conv paddings) are big enough for the kernel dimensions
65 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
66 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
67 const unsigned total_width = input->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right();
68 const unsigned total_height = input->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom();
69 ARM_COMPUTE_RETURN_ERROR_ON((total_width < kernel_dims.width) || (total_height < kernel_dims.height));
70
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010071 if(output->total_size() > 0)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000072 {
Giorgio Arena368e6352018-08-20 15:06:07 +010073 TensorInfo expected_output = output->clone()->set_tensor_shape(compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, false));
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010074 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output, output);
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000076 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010077 }
Giorgio Arena156fcf32018-03-09 15:30:43 +000078
Georgios Pinitas631c41a2017-12-06 11:53:03 +000079 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000080}
81
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082template <typename T, bool has_pads>
Giorgio Arenafb629082018-08-20 18:03:27 +010083inline void linearize_volume_nchw(const uint8_t *const in_ptr,
84 T *out_ptr,
85 bool has_bias,
86 int top_left_x,
87 int top_left_y,
88 int kernel_width,
89 int kernel_height,
90 int kernel_depth,
91 int input_w,
92 int input_h,
93 int input_stride_x,
94 int input_stride_y,
95 int input_stride_z,
96 int pad_value,
97 int dilation_x,
98 int dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099{
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100100 const int kernel_size2 = kernel_width * kernel_height;
Alex Gilday7da29b62018-03-23 14:16:00 +0000101 const int x_e = top_left_x + kernel_width * dilation_x;
102 const int y_e = top_left_y + kernel_height * dilation_y;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
104 // Linearize volume
105 int d = 0;
106 // This for loop linearize a volume with 3 slices. This allows:
107 // 1) to reduce the iterations of the outer for loop "d"
108 // 2) to have an optimized im2col for the first convolution layer where usually we have 3 IFMs
109 for(; d <= (kernel_depth - 3); d += 3)
110 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000111 for(int y = top_left_y; y < y_e; y += dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 {
113 if((y < 0 || y >= input_h) && has_pads)
114 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000115 // All the values will be the offset (will be zeros when not quantized)
Alex Gilday7da29b62018-03-23 14:16:00 +0000116 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000118 *(out_ptr + 0 * kernel_size2) = pad_value;
119 *(out_ptr + 1 * kernel_size2) = pad_value;
120 *(out_ptr + 2 * kernel_size2) = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 }
122 }
123 else
124 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000125 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 {
127 if((x < 0 || x >= input_w) && has_pads)
128 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000129 *(out_ptr + 0 * kernel_size2) = pad_value;
130 *(out_ptr + 1 * kernel_size2) = pad_value;
131 *(out_ptr + 2 * kernel_size2) = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 }
133 else
134 {
135 *(out_ptr + 0 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 0) * input_stride_z + y * input_stride_y + x * input_stride_x)));
136 *(out_ptr + 1 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 1) * input_stride_z + y * input_stride_y + x * input_stride_x)));
137 *(out_ptr + 2 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 2) * input_stride_z + y * input_stride_y + x * input_stride_x)));
138 }
139 }
140 }
141 }
142 out_ptr += 2 * kernel_size2;
143 }
144
145 // Left over
146 for(; d < kernel_depth; d++)
147 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000148 for(int y = top_left_y; y < y_e; y += dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 {
150 if((y < 0 || y >= input_h) && has_pads)
151 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000152 // All the values will be the offset (will be zeros when not quantized)
Georgios Pinitas8a14b2c2020-09-04 20:20:56 +0100153 memset(static_cast<void *>(out_ptr), pad_value, kernel_width * sizeof(T));
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100154 out_ptr += kernel_width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 }
156 else
157 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000158 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 {
160 if((x < 0 || x >= input_w) && has_pads)
161 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000162 *out_ptr = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 }
164 else
165 {
166 *out_ptr = *(reinterpret_cast<const T *>(in_ptr + (d * input_stride_z + y * input_stride_y + x * input_stride_x)));
167 }
168 }
169 }
170 }
171 }
172
173 // Append 1 if the convolution layer has biases
174 if(has_bias)
175 {
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100176 *out_ptr = static_cast<T>(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 }
178}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
180template <typename T, bool has_pads>
Giorgio Arenafb629082018-08-20 18:03:27 +0100181inline void linearize_volume_nhwc(const uint8_t *const in_ptr,
182 T *out_ptr,
183 bool has_bias,
184 int start_x,
185 int start_y,
186 int kernel_width,
187 int kernel_height,
188 int input_w,
189 int input_h,
190 int input_c,
191 int input_stride_y,
192 int input_stride_z,
193 int pad_value,
194 int dilation_x,
195 int dilation_y)
196{
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100197 const int end_x = start_x + kernel_width * dilation_x;
198 const int end_y = start_y + kernel_height * dilation_y;
199 const int pad_quant = kernel_width * input_c;
200 const int element_size = static_cast<int>(sizeof(T));
201 if((start_y >= 0) && (end_y < input_h) && (start_x >= 0) && (end_x < input_w) && (dilation_x == 1) && (input_stride_y == input_c * element_size))
Giorgio Arenafb629082018-08-20 18:03:27 +0100202 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100203 for(int y = start_y; y < end_y; y += dilation_y)
Giorgio Arenafb629082018-08-20 18:03:27 +0100204 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100205 //optimized for no dilation and no boundary pixels
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100206 memcpy(out_ptr, reinterpret_cast<const T *>(in_ptr + (y * input_stride_z + start_x * input_stride_y)), input_c * kernel_width * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100207 out_ptr += input_c * kernel_width;
Giorgio Arenafb629082018-08-20 18:03:27 +0100208 }
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100209 }
210 else
211 {
212 for(int y = start_y; y < end_y; y += dilation_y)
Giorgio Arenafb629082018-08-20 18:03:27 +0100213 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100214 if(y < 0 || y >= input_h)
Giorgio Arenafb629082018-08-20 18:03:27 +0100215 {
Georgios Pinitas8a14b2c2020-09-04 20:20:56 +0100216 memset(static_cast<void *>(out_ptr), pad_value, pad_quant * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100217 out_ptr += pad_quant;
218 }
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100219 else if(dilation_x > 1 || start_x < 0 || end_x >= input_w || input_stride_y != input_c * element_size)
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100220 {
221 for(int x = start_x; x < end_x; x += dilation_x)
Giorgio Arenafb629082018-08-20 18:03:27 +0100222 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100223 if(x < 0 || x >= input_w)
224 {
Georgios Pinitas8a14b2c2020-09-04 20:20:56 +0100225 memset(static_cast<void *>(out_ptr), pad_value, input_c * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100226 out_ptr += input_c;
227 }
228 else
229 {
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100230 memcpy(out_ptr, reinterpret_cast<const T *>(in_ptr + (y * input_stride_z + x * input_stride_y)), input_c * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100231 out_ptr += input_c;
232 }
Giorgio Arenafb629082018-08-20 18:03:27 +0100233 }
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100234 }
235 else
236 {
237 //optimized for no dilation and no boundary pixels
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100238 memcpy(out_ptr, reinterpret_cast<const T *>(in_ptr + (y * input_stride_z + start_x * input_stride_y)), input_c * kernel_width * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100239 out_ptr += input_c * kernel_width;
Giorgio Arenafb629082018-08-20 18:03:27 +0100240 }
241 }
242 }
Giorgio Arenafb629082018-08-20 18:03:27 +0100243 // Append 1 if the convolution layer has biases
244 if(has_bias)
245 {
246 *out_ptr = static_cast<T>(1);
247 }
248}
249} // namespace
250
251template <typename T, bool has_pads, bool is_nchw>
Manuel Bottini90028992021-06-30 18:29:18 +0100252void CpuIm2ColKernel::run_im2col(const ITensor *src, ITensor *dst, const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253{
254 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini90028992021-06-30 18:29:18 +0100255 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256
Georgios Pinitas329b4d62020-01-15 17:48:20 +0000257 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
258 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
259 const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000260
Manuel Bottini90028992021-06-30 18:29:18 +0100261 const int input_w = src->info()->dimension(width_idx);
262 const int input_h = src->info()->dimension(height_idx);
263 const int input_c = src->info()->dimension(channel_idx);
264 const int input_stride_x = src->info()->strides_in_bytes().x();
265 const int input_stride_y = src->info()->strides_in_bytes().y();
266 const int input_stride_z = src->info()->strides_in_bytes().z();
Giorgio Arenafb629082018-08-20 18:03:27 +0100267 const int pad_left = _conv_info.pad_left();
268 const int pad_top = _conv_info.pad_top();
269 const int stride_x = _conv_info.stride().first;
270 const int stride_y = _conv_info.stride().second;
Manuel Bottini90028992021-06-30 18:29:18 +0100271 const int pad_value = is_data_type_quantized(src->info()->data_type()) ? src->info()->quantization_info().uniform().offset : 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272
Giorgio Arenaf485a102018-04-20 16:06:21 +0100273 Window window_in_out(window);
274 // The first three dimensions of the input and output are increased by the inner loops
275 window_in_out.set(Window::DimX, Window::Dimension(0, 0, 0));
276 window_in_out.set(Window::DimY, Window::Dimension(0, 0, 0));
277 window_in_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278
279 // Create iterators
Manuel Bottini90028992021-06-30 18:29:18 +0100280 Iterator in(src, window_in_out);
281 Iterator out(dst, window_in_out);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282
283 execute_window_loop(window, [&](const Coordinates & id)
284 {
Giorgio Arenafb629082018-08-20 18:03:27 +0100285 const int start_w = id[width_idx] * stride_x - pad_left;
286 const int start_h = id[height_idx] * stride_y - pad_top;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287
288 // Get pointers
289 const uint8_t *const input_ptr = in.ptr();
Manuel Bottini90028992021-06-30 18:29:18 +0100290 auto output_ptr = reinterpret_cast<T *>(out.ptr() + (id[width_idx] + id[height_idx] * _convolved_dims.first) * dst->info()->strides_in_bytes().y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100291
292 // Linearize volume
Giorgio Arenafb629082018-08-20 18:03:27 +0100293 if(is_nchw)
294 {
295 linearize_volume_nchw<T, has_pads>(input_ptr,
296 output_ptr,
297 _has_bias,
298 start_w,
299 start_h,
300 _kernel_width,
301 _kernel_height,
302 input_c,
303 input_w,
304 input_h,
305 input_stride_x,
306 input_stride_y,
307 input_stride_z,
308 pad_value,
309 _dilation.x(),
310 _dilation.y());
311 }
312 else
313 {
314 linearize_volume_nhwc<T, has_pads>(input_ptr,
315 output_ptr,
316 _has_bias,
317 start_w,
318 start_h,
319 _kernel_width,
320 _kernel_height,
321 input_w,
322 input_h,
323 input_c,
324 input_stride_y,
325 input_stride_z,
326 pad_value,
327 _dilation.x(),
328 _dilation.y());
329 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100330 },
331 in, out);
332}
333
Manuel Bottini29599d02021-07-06 15:01:35 +0100334void CpuIm2ColKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Manuel Bottini90028992021-06-30 18:29:18 +0100335 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336{
Manuel Bottini90028992021-06-30 18:29:18 +0100337 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
338 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
Giorgio Arena0f170392018-07-18 16:13:12 +0100339 ARM_COMPUTE_UNUSED(num_groups);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100340
Manuel Bottini90028992021-06-30 18:29:18 +0100341 _data_layout = src->data_layout();
342 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
343 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
344 const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000345
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100346 _conv_info = conv_info;
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100347 _kernel_width = kernel_dims.width;
Alex Gilday7da29b62018-03-23 14:16:00 +0000348 _kernel_height = kernel_dims.height;
349 _dilation = dilation;
Manuel Bottini90028992021-06-30 18:29:18 +0100350 _convolved_dims = scaled_dimensions(src->dimension(width_idx), dst->dimension(height_idx),
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100351 _kernel_width, _kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000352 _conv_info, _dilation);
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100353 _has_bias = has_bias;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100354
Georgios Pinitas329b4d62020-01-15 17:48:20 +0000355 if(_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356 {
Manuel Bottini90028992021-06-30 18:29:18 +0100357 switch(src->data_type())
Giorgio Arenafb629082018-08-20 18:03:27 +0100358 {
359 case DataType::F32:
Manuel Bottini90028992021-06-30 18:29:18 +0100360 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float, false, true> : &CpuIm2ColKernel::run_im2col<float, true, true>;
Giorgio Arenafb629082018-08-20 18:03:27 +0100361 break;
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000362#if defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16)
363 case DataType::BFLOAT16:
Manuel Bottini90028992021-06-30 18:29:18 +0100364 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<bfloat16, false, true> : &CpuIm2ColKernel::run_im2col<bfloat16, true, true>;
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000365 break;
366#endif /* defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16) */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000367#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Giorgio Arenafb629082018-08-20 18:03:27 +0100368 case DataType::F16:
Manuel Bottini90028992021-06-30 18:29:18 +0100369 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float16_t, false, true> : &CpuIm2ColKernel::run_im2col<float16_t, true, true>;
Giorgio Arenafb629082018-08-20 18:03:27 +0100370 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000371#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000372 case DataType::QASYMM8_SIGNED:
Giorgio Arenafb629082018-08-20 18:03:27 +0100373 case DataType::QASYMM8:
Manuel Bottini90028992021-06-30 18:29:18 +0100374 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<qasymm8_t, false, true> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, true>;
Giorgio Arenafb629082018-08-20 18:03:27 +0100375 break;
376 default:
377 ARM_COMPUTE_ERROR("Data type not supported");
378 break;
379 }
380 }
381 else
382 {
Manuel Bottini90028992021-06-30 18:29:18 +0100383 switch(src->data_type())
Giorgio Arenafb629082018-08-20 18:03:27 +0100384 {
385 case DataType::F32:
Manuel Bottini90028992021-06-30 18:29:18 +0100386 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float, false, false> : &CpuIm2ColKernel::run_im2col<float, true, false>;
Giorgio Arenafb629082018-08-20 18:03:27 +0100387 break;
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000388#if defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16)
389 case DataType::BFLOAT16:
Manuel Bottini90028992021-06-30 18:29:18 +0100390 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<bfloat16, false, false> : &CpuIm2ColKernel::run_im2col<bfloat16, true, false>;
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000391 break;
392#endif /* defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16) */
Giorgio Arenafb629082018-08-20 18:03:27 +0100393#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
394 case DataType::F16:
Manuel Bottini90028992021-06-30 18:29:18 +0100395 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<float16_t, false, false> : &CpuIm2ColKernel::run_im2col<float16_t, true, false>;
Giorgio Arenafb629082018-08-20 18:03:27 +0100396 break;
397#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
398 case DataType::QASYMM8:
Manuel Bottini90028992021-06-30 18:29:18 +0100399 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<uint8_t, false, false> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, false>;
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000400 break;
401 case DataType::QASYMM8_SIGNED:
Manuel Bottini90028992021-06-30 18:29:18 +0100402 _func = (!conv_info.has_padding()) ? &CpuIm2ColKernel::run_im2col<int8_t, false, false> : &CpuIm2ColKernel::run_im2col<qasymm8_t, true, false>;
Giorgio Arenafb629082018-08-20 18:03:27 +0100403 break;
404 default:
405 ARM_COMPUTE_ERROR("Data type not supported");
406 break;
407 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100408 }
409
Manuel Bottini90028992021-06-30 18:29:18 +0100410 // Output tensor auto initialization if not yet initialized
411 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_im2col_conv_shape(src, kernel_dims, conv_info, has_bias, dilation, false)));
412
413 std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(src->dimension(width_idx), src->dimension(height_idx),
414 kernel_dims.width, kernel_dims.height,
415 conv_info, dilation);
416
417 Window win = calculate_max_window(*src, Steps());
418 win.set(width_idx, Window::Dimension(0, convolved_dims.first, 1));
419 win.set(height_idx, Window::Dimension(0, convolved_dims.second, 1));
420 win.set(channel_idx, Window::Dimension(0, 1, 1));
Giorgio Arena368e6352018-08-20 15:06:07 +0100421 // Configure kernel window
Manuel Bottini90028992021-06-30 18:29:18 +0100422 ICpuKernel::configure(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100423}
424
Manuel Bottini90028992021-06-30 18:29:18 +0100425Status CpuIm2ColKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
426 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000427{
Manuel Bottini90028992021-06-30 18:29:18 +0100428 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, kernel_dims, conv_info, has_bias, dilation, num_groups));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000429 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000430}
431
Manuel Bottini90028992021-06-30 18:29:18 +0100432void CpuIm2ColKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100434 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100435 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini90028992021-06-30 18:29:18 +0100436 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100437
Manuel Bottini90028992021-06-30 18:29:18 +0100438 auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
439 auto dst = tensors.get_tensor(TensorType::ACL_DST);
440 (this->*_func)(src, dst, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100441}
Manuel Bottini90028992021-06-30 18:29:18 +0100442const char *CpuIm2ColKernel::name() const
443{
444 return "CpuIm2ColKernel";
445}
Dana Zlotnik4cdd6b82021-10-07 15:31:54 +0300446
447size_t CpuIm2ColKernel::get_mws(const CPUInfo &platform, size_t thread_count) const
448{
449 ARM_COMPUTE_UNUSED(platform, thread_count);
450
451 return ICPPKernel::small_network_mws;
452}
Manuel Bottini90028992021-06-30 18:29:18 +0100453} // namespace kernels
454} // namespace cpu
455} // namespace arm_compute