blob: 27c3d66b4fee06dba862d77cad74aaba1bde33e5 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasf5a80e12020-01-15 17:48:20 +00002 * Copyright (c) 2017-2020 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 */
24#include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h"
25
Anthony Barbiereaefd002018-07-20 17:49:35 +010026#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010030#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Validate.h"
34
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000035#include "arm_compute/core/utils/misc/ShapeCalculator.h"
36
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include <arm_neon.h>
38#include <cstddef>
39#include <cstdint>
40#include <cstring>
41#include <tuple>
42
43using namespace arm_compute;
Giorgio Arena368e6352018-08-20 15:06:07 +010044using namespace misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045
46namespace
47{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000048Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +010049 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Georgios Pinitasd912fd82017-11-27 21:00:13 +000050{
Anthony Barbiereaefd002018-07-20 17:49:35 +010051 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010052 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitasd912fd82017-11-27 21:00:13 +000053 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias);
Alex Gilday7da29b62018-03-23 14:16:00 +000054 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
Giorgio Arena0f170392018-07-18 16:13:12 +010055 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 +000056
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010057 if(output->total_size() > 0)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000058 {
Giorgio Arena368e6352018-08-20 15:06:07 +010059 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 +010060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output, output);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010063 }
Giorgio Arena156fcf32018-03-09 15:30:43 +000064
Georgios Pinitas631c41a2017-12-06 11:53:03 +000065 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000066}
67
Giorgio Arena368e6352018-08-20 15:06:07 +010068std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
69 bool has_bias, const Size2D &dilation)
70{
71 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
72 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
73 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
74
75 std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(input->dimension(width_idx), input->dimension(height_idx),
76 kernel_dims.width, kernel_dims.height,
77 conv_info, dilation);
78
79 // Output tensor auto initialization if not yet initialized
80 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, false)));
81
82 Window win = calculate_max_window(*input, Steps());
83 win.set(width_idx, Window::Dimension(0, convolved_dims.first, 1));
84 win.set(height_idx, Window::Dimension(0, convolved_dims.second, 1));
85 win.set(channel_idx, Window::Dimension(0, 1, 1));
86
87 // The NEIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped
88 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
89
90 return std::make_pair(Status{}, win);
91}
92
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093template <typename T, bool has_pads>
Giorgio Arenafb629082018-08-20 18:03:27 +010094inline void linearize_volume_nchw(const uint8_t *const in_ptr,
95 T *out_ptr,
96 bool has_bias,
97 int top_left_x,
98 int top_left_y,
99 int kernel_width,
100 int kernel_height,
101 int kernel_depth,
102 int input_w,
103 int input_h,
104 int input_stride_x,
105 int input_stride_y,
106 int input_stride_z,
107 int pad_value,
108 int dilation_x,
109 int dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110{
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100111 const int kernel_size2 = kernel_width * kernel_height;
Alex Gilday7da29b62018-03-23 14:16:00 +0000112 const int x_e = top_left_x + kernel_width * dilation_x;
113 const int y_e = top_left_y + kernel_height * dilation_y;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
115 // Linearize volume
116 int d = 0;
117 // This for loop linearize a volume with 3 slices. This allows:
118 // 1) to reduce the iterations of the outer for loop "d"
119 // 2) to have an optimized im2col for the first convolution layer where usually we have 3 IFMs
120 for(; d <= (kernel_depth - 3); d += 3)
121 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000122 for(int y = top_left_y; y < y_e; y += dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 {
124 if((y < 0 || y >= input_h) && has_pads)
125 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000126 // All the values will be the offset (will be zeros when not quantized)
Alex Gilday7da29b62018-03-23 14:16:00 +0000127 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 {
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 }
134 else
135 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000136 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 {
138 if((x < 0 || x >= input_w) && has_pads)
139 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000140 *(out_ptr + 0 * kernel_size2) = pad_value;
141 *(out_ptr + 1 * kernel_size2) = pad_value;
142 *(out_ptr + 2 * kernel_size2) = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 }
144 else
145 {
146 *(out_ptr + 0 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 0) * input_stride_z + y * input_stride_y + x * input_stride_x)));
147 *(out_ptr + 1 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 1) * input_stride_z + y * input_stride_y + x * input_stride_x)));
148 *(out_ptr + 2 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 2) * input_stride_z + y * input_stride_y + x * input_stride_x)));
149 }
150 }
151 }
152 }
153 out_ptr += 2 * kernel_size2;
154 }
155
156 // Left over
157 for(; d < kernel_depth; d++)
158 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000159 for(int y = top_left_y; y < y_e; y += dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 {
161 if((y < 0 || y >= input_h) && has_pads)
162 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000163 // All the values will be the offset (will be zeros when not quantized)
164 memset(out_ptr, pad_value, kernel_width * sizeof(T));
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100165 out_ptr += kernel_width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 }
167 else
168 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000169 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 {
171 if((x < 0 || x >= input_w) && has_pads)
172 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000173 *out_ptr = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 }
175 else
176 {
177 *out_ptr = *(reinterpret_cast<const T *>(in_ptr + (d * input_stride_z + y * input_stride_y + x * input_stride_x)));
178 }
179 }
180 }
181 }
182 }
183
184 // Append 1 if the convolution layer has biases
185 if(has_bias)
186 {
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100187 *out_ptr = static_cast<T>(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188 }
189}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190
191template <typename T, bool has_pads>
Giorgio Arenafb629082018-08-20 18:03:27 +0100192inline void linearize_volume_nhwc(const uint8_t *const in_ptr,
193 T *out_ptr,
194 bool has_bias,
195 int start_x,
196 int start_y,
197 int kernel_width,
198 int kernel_height,
199 int input_w,
200 int input_h,
201 int input_c,
202 int input_stride_y,
203 int input_stride_z,
204 int pad_value,
205 int dilation_x,
206 int dilation_y)
207{
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100208 const int end_x = start_x + kernel_width * dilation_x;
209 const int end_y = start_y + kernel_height * dilation_y;
210 const int pad_quant = kernel_width * input_c;
211 const int element_size = static_cast<int>(sizeof(T));
212 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 +0100213 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100214 for(int y = start_y; y < end_y; y += dilation_y)
Giorgio Arenafb629082018-08-20 18:03:27 +0100215 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100216 //optimized for no dilation and no boundary pixels
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100217 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 +0100218 out_ptr += input_c * kernel_width;
Giorgio Arenafb629082018-08-20 18:03:27 +0100219 }
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100220 }
221 else
222 {
223 for(int y = start_y; y < end_y; y += dilation_y)
Giorgio Arenafb629082018-08-20 18:03:27 +0100224 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100225 if(y < 0 || y >= input_h)
Giorgio Arenafb629082018-08-20 18:03:27 +0100226 {
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100227 memset(out_ptr, pad_value, pad_quant * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100228 out_ptr += pad_quant;
229 }
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100230 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 +0100231 {
232 for(int x = start_x; x < end_x; x += dilation_x)
Giorgio Arenafb629082018-08-20 18:03:27 +0100233 {
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100234 if(x < 0 || x >= input_w)
235 {
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100236 memset(out_ptr, pad_value, input_c * element_size);
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100237 out_ptr += input_c;
238 }
239 else
240 {
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100241 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 +0100242 out_ptr += input_c;
243 }
Giorgio Arenafb629082018-08-20 18:03:27 +0100244 }
Vidhya Sudhan Loganathan642680a2019-04-02 09:40:08 +0100245 }
246 else
247 {
248 //optimized for no dilation and no boundary pixels
Georgios Pinitas75bde5e2019-06-07 11:52:01 +0100249 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 +0100250 out_ptr += input_c * kernel_width;
Giorgio Arenafb629082018-08-20 18:03:27 +0100251 }
252 }
253 }
Giorgio Arenafb629082018-08-20 18:03:27 +0100254 // Append 1 if the convolution layer has biases
255 if(has_bias)
256 {
257 *out_ptr = static_cast<T>(1);
258 }
259}
260} // namespace
261
262template <typename T, bool has_pads, bool is_nchw>
Giorgio Arena368e6352018-08-20 15:06:07 +0100263void NEIm2ColKernel::run_im2col(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264{
265 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
266 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
267
Georgios Pinitasf5a80e12020-01-15 17:48:20 +0000268 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
269 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
270 const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000271
Giorgio Arena156fcf32018-03-09 15:30:43 +0000272 const int input_w = _input->info()->dimension(width_idx);
273 const int input_h = _input->info()->dimension(height_idx);
Giorgio Arenafb629082018-08-20 18:03:27 +0100274 const int input_c = _input->info()->dimension(channel_idx);
275 const int input_stride_x = _input->info()->strides_in_bytes().x();
276 const int input_stride_y = _input->info()->strides_in_bytes().y();
277 const int input_stride_z = _input->info()->strides_in_bytes().z();
278 const int pad_left = _conv_info.pad_left();
279 const int pad_top = _conv_info.pad_top();
280 const int stride_x = _conv_info.stride().first;
281 const int stride_y = _conv_info.stride().second;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100282 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 +0100283
Giorgio Arenaf485a102018-04-20 16:06:21 +0100284 Window window_in_out(window);
285 // The first three dimensions of the input and output are increased by the inner loops
286 window_in_out.set(Window::DimX, Window::Dimension(0, 0, 0));
287 window_in_out.set(Window::DimY, Window::Dimension(0, 0, 0));
288 window_in_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289
290 // Create iterators
Giorgio Arenaf485a102018-04-20 16:06:21 +0100291 Iterator in(_input, window_in_out);
292 Iterator out(_output, window_in_out);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100293
294 execute_window_loop(window, [&](const Coordinates & id)
295 {
Giorgio Arenafb629082018-08-20 18:03:27 +0100296 const int start_w = id[width_idx] * stride_x - pad_left;
297 const int start_h = id[height_idx] * stride_y - pad_top;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298
299 // Get pointers
300 const uint8_t *const input_ptr = in.ptr();
Giorgio Arenaf485a102018-04-20 16:06:21 +0100301 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 +0100302
303 // Linearize volume
Giorgio Arenafb629082018-08-20 18:03:27 +0100304 if(is_nchw)
305 {
306 linearize_volume_nchw<T, has_pads>(input_ptr,
307 output_ptr,
308 _has_bias,
309 start_w,
310 start_h,
311 _kernel_width,
312 _kernel_height,
313 input_c,
314 input_w,
315 input_h,
316 input_stride_x,
317 input_stride_y,
318 input_stride_z,
319 pad_value,
320 _dilation.x(),
321 _dilation.y());
322 }
323 else
324 {
325 linearize_volume_nhwc<T, has_pads>(input_ptr,
326 output_ptr,
327 _has_bias,
328 start_w,
329 start_h,
330 _kernel_width,
331 _kernel_height,
332 input_w,
333 input_h,
334 input_c,
335 input_stride_y,
336 input_stride_z,
337 pad_value,
338 _dilation.x(),
339 _dilation.y());
340 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100341 },
342 in, out);
343}
344
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100345NEIm2ColKernel::NEIm2ColKernel()
Georgios Pinitasf5a80e12020-01-15 17:48:20 +0000346 : _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 +0100347{
348}
349
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000350void NEIm2ColKernel::configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +0100351 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000353 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena368e6352018-08-20 15:06:07 +0100354 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 +0100355 ARM_COMPUTE_UNUSED(num_groups);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356
Georgios Pinitasf5a80e12020-01-15 17:48:20 +0000357 _data_layout = input->info()->data_layout();
358 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
359 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000360
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100361 _input = input;
362 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100363 _conv_info = conv_info;
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100364 _kernel_width = kernel_dims.width;
Alex Gilday7da29b62018-03-23 14:16:00 +0000365 _kernel_height = kernel_dims.height;
366 _dilation = dilation;
Giorgio Arena156fcf32018-03-09 15:30:43 +0000367 _convolved_dims = scaled_dimensions(input->info()->dimension(width_idx), input->info()->dimension(height_idx),
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100368 _kernel_width, _kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000369 _conv_info, _dilation);
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100370 _has_bias = has_bias;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100371
Georgios Pinitasf5a80e12020-01-15 17:48:20 +0000372 if(_data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373 {
Giorgio Arenafb629082018-08-20 18:03:27 +0100374 switch(_input->info()->data_type())
375 {
376 case DataType::F32:
377 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float, false, true> : &NEIm2ColKernel::run_im2col<float, true, true>;
378 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000379#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Giorgio Arenafb629082018-08-20 18:03:27 +0100380 case DataType::F16:
381 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float16_t, false, true> : &NEIm2ColKernel::run_im2col<float16_t, true, true>;
382 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000383#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Giorgio Arenafb629082018-08-20 18:03:27 +0100384 case DataType::QASYMM8:
385 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<qasymm8_t, false, true> : &NEIm2ColKernel::run_im2col<qasymm8_t, true, true>;
386 break;
387 default:
388 ARM_COMPUTE_ERROR("Data type not supported");
389 break;
390 }
391 }
392 else
393 {
394 switch(_input->info()->data_type())
395 {
396 case DataType::F32:
397 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float, false, false> : &NEIm2ColKernel::run_im2col<float, true, false>;
398 break;
399#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
400 case DataType::F16:
401 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float16_t, false, false> : &NEIm2ColKernel::run_im2col<float16_t, true, false>;
402 break;
403#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
404 case DataType::QASYMM8:
405 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<qasymm8_t, false, false> : &NEIm2ColKernel::run_im2col<qasymm8_t, true, false>;
406 break;
407 default:
408 ARM_COMPUTE_ERROR("Data type not supported");
409 break;
410 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411 }
412
Giorgio Arena368e6352018-08-20 15:06:07 +0100413 // Configure kernel window
414 auto win_config = validate_and_configure_window(input->info(), output->info(), kernel_dims, conv_info, has_bias, dilation);
415 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
416 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100417}
418
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000419Status NEIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +0100420 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000421{
Giorgio Arena368e6352018-08-20 15:06:07 +0100422 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, dilation, num_groups));
423 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 +0000424 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000425}
426
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100427void NEIm2ColKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100428{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100429 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100430 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
431 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
432
433 (this->*_func)(window);
434}