blob: 72821c890d0b645106e4e48fe19dc22cd92abcdd [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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEIm2ColKernel.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
45using namespace arm_compute;
Giorgio Arena368e6352018-08-20 15:06:07 +010046using namespace misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48namespace
49{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000050Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +010051 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Georgios Pinitasd912fd82017-11-27 21:00:13 +000052{
Anthony Barbiereaefd002018-07-20 17:49:35 +010053 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Georgios Pinitasdff29352020-12-31 15:35:17 +000054 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +000055 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 +000056 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(input->data_type()) && has_bias);
Alex Gilday7da29b62018-03-23 14:16:00 +000057 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
Giorgio Arena0f170392018-07-18 16:13:12 +010058 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 +000059
Georgios Pinitasdff29352020-12-31 15:35:17 +000060 // Since there's no implicit padding added, check the total input spatial dimensions (with conv paddings) are big enough for the kernel dimensions
61 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
62 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
63 const unsigned total_width = input->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right();
64 const unsigned total_height = input->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom();
65 ARM_COMPUTE_RETURN_ERROR_ON((total_width < kernel_dims.width) || (total_height < kernel_dims.height));
66
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010067 if(output->total_size() > 0)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000068 {
Giorgio Arena368e6352018-08-20 15:06:07 +010069 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 +010070 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output, output);
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000072 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010073 }
Giorgio Arena156fcf32018-03-09 15:30:43 +000074
Georgios Pinitas631c41a2017-12-06 11:53:03 +000075 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000076}
77
Giorgio Arena368e6352018-08-20 15:06:07 +010078std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
79 bool has_bias, const Size2D &dilation)
80{
Georgios Pinitasdff29352020-12-31 15:35:17 +000081 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
82
83 // Output tensor auto initialization if not yet initialized
84 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, false)));
85
86 const DataLayout data_layout = input->data_layout();
87 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
88 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
89 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arena368e6352018-08-20 15:06:07 +010090
91 std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(input->dimension(width_idx), input->dimension(height_idx),
92 kernel_dims.width, kernel_dims.height,
93 conv_info, dilation);
94
Giorgio Arena368e6352018-08-20 15:06:07 +010095 Window win = calculate_max_window(*input, Steps());
96 win.set(width_idx, Window::Dimension(0, convolved_dims.first, 1));
97 win.set(height_idx, Window::Dimension(0, convolved_dims.second, 1));
98 win.set(channel_idx, Window::Dimension(0, 1, 1));
99
100 // The NEIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped
101 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
102
103 return std::make_pair(Status{}, win);
104}
105
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106template <typename T, bool has_pads>
Giorgio Arenafb629082018-08-20 18:03:27 +0100107inline void linearize_volume_nchw(const uint8_t *const in_ptr,
108 T *out_ptr,
109 bool has_bias,
110 int top_left_x,
111 int top_left_y,
112 int kernel_width,
113 int kernel_height,
114 int kernel_depth,
115 int input_w,
116 int input_h,
117 int input_stride_x,
118 int input_stride_y,
119 int input_stride_z,
120 int pad_value,
121 int dilation_x,
122 int dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123{
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100124 const int kernel_size2 = kernel_width * kernel_height;
Alex Gilday7da29b62018-03-23 14:16:00 +0000125 const int x_e = top_left_x + kernel_width * dilation_x;
126 const int y_e = top_left_y + kernel_height * dilation_y;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127
128 // Linearize volume
129 int d = 0;
130 // This for loop linearize a volume with 3 slices. This allows:
131 // 1) to reduce the iterations of the outer for loop "d"
132 // 2) to have an optimized im2col for the first convolution layer where usually we have 3 IFMs
133 for(; d <= (kernel_depth - 3); d += 3)
134 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000135 for(int y = top_left_y; y < y_e; y += dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 {
137 if((y < 0 || y >= input_h) && has_pads)
138 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000139 // All the values will be the offset (will be zeros when not quantized)
Alex Gilday7da29b62018-03-23 14:16:00 +0000140 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000142 *(out_ptr + 0 * kernel_size2) = pad_value;
143 *(out_ptr + 1 * kernel_size2) = pad_value;
144 *(out_ptr + 2 * kernel_size2) = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 }
146 }
147 else
148 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000149 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 {
151 if((x < 0 || x >= input_w) && has_pads)
152 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000153 *(out_ptr + 0 * kernel_size2) = pad_value;
154 *(out_ptr + 1 * kernel_size2) = pad_value;
155 *(out_ptr + 2 * kernel_size2) = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 }
157 else
158 {
159 *(out_ptr + 0 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 0) * input_stride_z + y * input_stride_y + x * input_stride_x)));
160 *(out_ptr + 1 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 1) * input_stride_z + y * input_stride_y + x * input_stride_x)));
161 *(out_ptr + 2 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 2) * input_stride_z + y * input_stride_y + x * input_stride_x)));
162 }
163 }
164 }
165 }
166 out_ptr += 2 * kernel_size2;
167 }
168
169 // Left over
170 for(; d < kernel_depth; d++)
171 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000172 for(int y = top_left_y; y < y_e; y += dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 {
174 if((y < 0 || y >= input_h) && has_pads)
175 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000176 // All the values will be the offset (will be zeros when not quantized)
Georgios Pinitas8a14b2c2020-09-04 20:20:56 +0100177 memset(static_cast<void *>(out_ptr), pad_value, kernel_width * sizeof(T));
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100178 out_ptr += kernel_width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 }
180 else
181 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000182 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183 {
184 if((x < 0 || x >= input_w) && has_pads)
185 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000186 *out_ptr = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
188 else
189 {
190 *out_ptr = *(reinterpret_cast<const T *>(in_ptr + (d * input_stride_z + y * input_stride_y + x * input_stride_x)));
191 }
192 }
193 }
194 }
195 }
196
197 // Append 1 if the convolution layer has biases
198 if(has_bias)
199 {
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100200 *out_ptr = static_cast<T>(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 }
202}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100203
204template <typename T, bool has_pads>
Giorgio Arenafb629082018-08-20 18:03:27 +0100205inline void linearize_volume_nhwc(const uint8_t *const in_ptr,
206 T *out_ptr,
207 bool has_bias,
208 int start_x,
209 int start_y,
210 int kernel_width,
211 int kernel_height,
212 int input_w,
213 int input_h,
214 int input_c,
215 int input_stride_y,
216 int input_stride_z,
217 int pad_value,
218 int dilation_x,
219 int dilation_y)
220{
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100221 const int end_x = start_x + kernel_width * dilation_x;
222 const int end_y = start_y + kernel_height * dilation_y;
223 const int pad_quant = kernel_width * input_c;
224 const int element_size = static_cast<int>(sizeof(T));
225 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 +0100226 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100227 for(int y = start_y; y < end_y; y += dilation_y)
Giorgio Arenafb629082018-08-20 18:03:27 +0100228 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100229 //optimized for no dilation and no boundary pixels
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100230 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 +0100231 out_ptr += input_c * kernel_width;
Giorgio Arenafb629082018-08-20 18:03:27 +0100232 }
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100233 }
234 else
235 {
236 for(int y = start_y; y < end_y; y += dilation_y)
Giorgio Arenafb629082018-08-20 18:03:27 +0100237 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100238 if(y < 0 || y >= input_h)
Giorgio Arenafb629082018-08-20 18:03:27 +0100239 {
Georgios Pinitas8a14b2c2020-09-04 20:20:56 +0100240 memset(static_cast<void *>(out_ptr), pad_value, pad_quant * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100241 out_ptr += pad_quant;
242 }
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100243 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 +0100244 {
245 for(int x = start_x; x < end_x; x += dilation_x)
Giorgio Arenafb629082018-08-20 18:03:27 +0100246 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100247 if(x < 0 || x >= input_w)
248 {
Georgios Pinitas8a14b2c2020-09-04 20:20:56 +0100249 memset(static_cast<void *>(out_ptr), pad_value, input_c * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100250 out_ptr += input_c;
251 }
252 else
253 {
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100254 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 +0100255 out_ptr += input_c;
256 }
Giorgio Arenafb629082018-08-20 18:03:27 +0100257 }
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100258 }
259 else
260 {
261 //optimized for no dilation and no boundary pixels
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100262 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 +0100263 out_ptr += input_c * kernel_width;
Giorgio Arenafb629082018-08-20 18:03:27 +0100264 }
265 }
266 }
Giorgio Arenafb629082018-08-20 18:03:27 +0100267 // Append 1 if the convolution layer has biases
268 if(has_bias)
269 {
270 *out_ptr = static_cast<T>(1);
271 }
272}
273} // namespace
274
275template <typename T, bool has_pads, bool is_nchw>
Giorgio Arena368e6352018-08-20 15:06:07 +0100276void NEIm2ColKernel::run_im2col(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100277{
278 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
279 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
280
Georgios Pinitas329b4d62020-01-15 17:48:20 +0000281 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
282 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
283 const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000284
Giorgio Arena156fcf32018-03-09 15:30:43 +0000285 const int input_w = _input->info()->dimension(width_idx);
286 const int input_h = _input->info()->dimension(height_idx);
Giorgio Arenafb629082018-08-20 18:03:27 +0100287 const int input_c = _input->info()->dimension(channel_idx);
288 const int input_stride_x = _input->info()->strides_in_bytes().x();
289 const int input_stride_y = _input->info()->strides_in_bytes().y();
290 const int input_stride_z = _input->info()->strides_in_bytes().z();
291 const int pad_left = _conv_info.pad_left();
292 const int pad_top = _conv_info.pad_top();
293 const int stride_x = _conv_info.stride().first;
294 const int stride_y = _conv_info.stride().second;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100295 const int pad_value = is_data_type_quantized(_input->info()->data_type()) ? _input->info()->quantization_info().uniform().offset : 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100296
Giorgio Arenaf485a102018-04-20 16:06:21 +0100297 Window window_in_out(window);
298 // The first three dimensions of the input and output are increased by the inner loops
299 window_in_out.set(Window::DimX, Window::Dimension(0, 0, 0));
300 window_in_out.set(Window::DimY, Window::Dimension(0, 0, 0));
301 window_in_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302
303 // Create iterators
Giorgio Arenaf485a102018-04-20 16:06:21 +0100304 Iterator in(_input, window_in_out);
305 Iterator out(_output, window_in_out);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306
307 execute_window_loop(window, [&](const Coordinates & id)
308 {
Giorgio Arenafb629082018-08-20 18:03:27 +0100309 const int start_w = id[width_idx] * stride_x - pad_left;
310 const int start_h = id[height_idx] * stride_y - pad_top;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311
312 // Get pointers
313 const uint8_t *const input_ptr = in.ptr();
Giorgio Arenaf485a102018-04-20 16:06:21 +0100314 auto output_ptr = reinterpret_cast<T *>(out.ptr() + (id[width_idx] + id[height_idx] * _convolved_dims.first) * _output->info()->strides_in_bytes().y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315
316 // Linearize volume
Giorgio Arenafb629082018-08-20 18:03:27 +0100317 if(is_nchw)
318 {
319 linearize_volume_nchw<T, has_pads>(input_ptr,
320 output_ptr,
321 _has_bias,
322 start_w,
323 start_h,
324 _kernel_width,
325 _kernel_height,
326 input_c,
327 input_w,
328 input_h,
329 input_stride_x,
330 input_stride_y,
331 input_stride_z,
332 pad_value,
333 _dilation.x(),
334 _dilation.y());
335 }
336 else
337 {
338 linearize_volume_nhwc<T, has_pads>(input_ptr,
339 output_ptr,
340 _has_bias,
341 start_w,
342 start_h,
343 _kernel_width,
344 _kernel_height,
345 input_w,
346 input_h,
347 input_c,
348 input_stride_y,
349 input_stride_z,
350 pad_value,
351 _dilation.x(),
352 _dilation.y());
353 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100354 },
355 in, out);
356}
357
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100358NEIm2ColKernel::NEIm2ColKernel()
Georgios Pinitas329b4d62020-01-15 17:48:20 +0000359 : _func(), _input(nullptr), _output(nullptr), _convolved_dims(), _conv_info(), _kernel_width(0), _kernel_height(0), _has_bias(false), _dilation(1U, 1U), _data_layout(DataLayout::UNKNOWN)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100360{
361}
362
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000363void NEIm2ColKernel::configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +0100364 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000366 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena368e6352018-08-20 15:06:07 +0100367 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), kernel_dims, conv_info, has_bias, dilation, num_groups));
Giorgio Arena0f170392018-07-18 16:13:12 +0100368 ARM_COMPUTE_UNUSED(num_groups);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369
Georgios Pinitas329b4d62020-01-15 17:48:20 +0000370 _data_layout = input->info()->data_layout();
371 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
372 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000373
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100374 _input = input;
375 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100376 _conv_info = conv_info;
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100377 _kernel_width = kernel_dims.width;
Alex Gilday7da29b62018-03-23 14:16:00 +0000378 _kernel_height = kernel_dims.height;
379 _dilation = dilation;
Giorgio Arena156fcf32018-03-09 15:30:43 +0000380 _convolved_dims = scaled_dimensions(input->info()->dimension(width_idx), input->info()->dimension(height_idx),
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100381 _kernel_width, _kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000382 _conv_info, _dilation);
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100383 _has_bias = has_bias;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100384
Georgios Pinitas329b4d62020-01-15 17:48:20 +0000385 if(_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100386 {
Giorgio Arenafb629082018-08-20 18:03:27 +0100387 switch(_input->info()->data_type())
388 {
389 case DataType::F32:
390 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float, false, true> : &NEIm2ColKernel::run_im2col<float, true, true>;
391 break;
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000392#if defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16)
393 case DataType::BFLOAT16:
394 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<bfloat16, false, true> : &NEIm2ColKernel::run_im2col<bfloat16, true, true>;
395 break;
396#endif /* defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16) */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000397#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Giorgio Arenafb629082018-08-20 18:03:27 +0100398 case DataType::F16:
399 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float16_t, false, true> : &NEIm2ColKernel::run_im2col<float16_t, true, true>;
400 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000401#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000402 case DataType::QASYMM8_SIGNED:
Giorgio Arenafb629082018-08-20 18:03:27 +0100403 case DataType::QASYMM8:
404 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<qasymm8_t, false, true> : &NEIm2ColKernel::run_im2col<qasymm8_t, true, true>;
405 break;
406 default:
407 ARM_COMPUTE_ERROR("Data type not supported");
408 break;
409 }
410 }
411 else
412 {
413 switch(_input->info()->data_type())
414 {
415 case DataType::F32:
416 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float, false, false> : &NEIm2ColKernel::run_im2col<float, true, false>;
417 break;
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000418#if defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16)
419 case DataType::BFLOAT16:
420 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<bfloat16, false, false> : &NEIm2ColKernel::run_im2col<bfloat16, true, false>;
421 break;
422#endif /* defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16) */
Giorgio Arenafb629082018-08-20 18:03:27 +0100423#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
424 case DataType::F16:
425 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float16_t, false, false> : &NEIm2ColKernel::run_im2col<float16_t, true, false>;
426 break;
427#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
428 case DataType::QASYMM8:
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000429 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<uint8_t, false, false> : &NEIm2ColKernel::run_im2col<qasymm8_t, true, false>;
430 break;
431 case DataType::QASYMM8_SIGNED:
432 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<int8_t, false, false> : &NEIm2ColKernel::run_im2col<qasymm8_t, true, false>;
Giorgio Arenafb629082018-08-20 18:03:27 +0100433 break;
434 default:
435 ARM_COMPUTE_ERROR("Data type not supported");
436 break;
437 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100438 }
439
Giorgio Arena368e6352018-08-20 15:06:07 +0100440 // Configure kernel window
441 auto win_config = validate_and_configure_window(input->info(), output->info(), kernel_dims, conv_info, has_bias, dilation);
442 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
443 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100444}
445
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000446Status NEIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +0100447 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000448{
Giorgio Arena368e6352018-08-20 15:06:07 +0100449 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, dilation, num_groups));
450 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), kernel_dims, conv_info, has_bias, dilation).first);
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000451 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000452}
453
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100454void NEIm2ColKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100455{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100456 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100457 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
458 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
459
460 (this->*_func)(window);
461}