blob: e5d31289a42d369ac28bf5aa2020d0494bebfbcf [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardie6630e42018-01-18 15:50:39 +00002 * Copyright (c) 2017-2018 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);
62 }
Giorgio Arena156fcf32018-03-09 15:30:43 +000063
Georgios Pinitas631c41a2017-12-06 11:53:03 +000064 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000065}
66
Giorgio Arena368e6352018-08-20 15:06:07 +010067std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
68 bool has_bias, const Size2D &dilation)
69{
70 const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
71 const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
72 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
73
74 std::pair<unsigned int, unsigned int> convolved_dims = scaled_dimensions(input->dimension(width_idx), input->dimension(height_idx),
75 kernel_dims.width, kernel_dims.height,
76 conv_info, dilation);
77
78 // Output tensor auto initialization if not yet initialized
79 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation, false)));
80
81 Window win = calculate_max_window(*input, Steps());
82 win.set(width_idx, Window::Dimension(0, convolved_dims.first, 1));
83 win.set(height_idx, Window::Dimension(0, convolved_dims.second, 1));
84 win.set(channel_idx, Window::Dimension(0, 1, 1));
85
86 // The NEIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped
87 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
88
89 return std::make_pair(Status{}, win);
90}
91
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092template <typename T, bool has_pads>
93inline void linearize_volume(const uint8_t *const in_ptr,
94 T *out_ptr,
95 bool has_bias,
96 int top_left_x,
97 int top_left_y,
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010098 int kernel_width,
99 int kernel_height,
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 int kernel_depth,
101 int input_w,
102 int input_h,
103 int input_stride_x,
104 int input_stride_y,
105 int input_stride_z,
Alex Gilday7da29b62018-03-23 14:16:00 +0000106 int pad_value,
107 int dilation_x,
108 int dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109{
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100110 const int kernel_size2 = kernel_width * kernel_height;
Alex Gilday7da29b62018-03-23 14:16:00 +0000111 const int x_e = top_left_x + kernel_width * dilation_x;
112 const int y_e = top_left_y + kernel_height * dilation_y;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114 // Linearize volume
115 int d = 0;
116 // This for loop linearize a volume with 3 slices. This allows:
117 // 1) to reduce the iterations of the outer for loop "d"
118 // 2) to have an optimized im2col for the first convolution layer where usually we have 3 IFMs
119 for(; d <= (kernel_depth - 3); d += 3)
120 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000121 for(int y = top_left_y; y < y_e; y += dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 {
123 if((y < 0 || y >= input_h) && has_pads)
124 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000125 // All the values will be the offset (will be zeros when not quantized)
Alex Gilday7da29b62018-03-23 14:16:00 +0000126 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000128 *(out_ptr + 0 * kernel_size2) = pad_value;
129 *(out_ptr + 1 * kernel_size2) = pad_value;
130 *(out_ptr + 2 * kernel_size2) = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 }
132 }
133 else
134 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000135 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 {
137 if((x < 0 || x >= input_w) && has_pads)
138 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000139 *(out_ptr + 0 * kernel_size2) = pad_value;
140 *(out_ptr + 1 * kernel_size2) = pad_value;
141 *(out_ptr + 2 * kernel_size2) = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 }
143 else
144 {
145 *(out_ptr + 0 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 0) * input_stride_z + y * input_stride_y + x * input_stride_x)));
146 *(out_ptr + 1 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 1) * input_stride_z + y * input_stride_y + x * input_stride_x)));
147 *(out_ptr + 2 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 2) * input_stride_z + y * input_stride_y + x * input_stride_x)));
148 }
149 }
150 }
151 }
152 out_ptr += 2 * kernel_size2;
153 }
154
155 // Left over
156 for(; d < kernel_depth; d++)
157 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000158 for(int y = top_left_y; y < y_e; y += dilation_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 {
160 if((y < 0 || y >= input_h) && has_pads)
161 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000162 // All the values will be the offset (will be zeros when not quantized)
163 memset(out_ptr, pad_value, kernel_width * sizeof(T));
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100164 out_ptr += kernel_width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 }
166 else
167 {
Alex Gilday7da29b62018-03-23 14:16:00 +0000168 for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 {
170 if((x < 0 || x >= input_w) && has_pads)
171 {
Isabella Gottardie6630e42018-01-18 15:50:39 +0000172 *out_ptr = pad_value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 }
174 else
175 {
176 *out_ptr = *(reinterpret_cast<const T *>(in_ptr + (d * input_stride_z + y * input_stride_y + x * input_stride_x)));
177 }
178 }
179 }
180 }
181 }
182
183 // Append 1 if the convolution layer has biases
184 if(has_bias)
185 {
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100186 *out_ptr = static_cast<T>(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
188}
189} // namespace
190
191template <typename T, bool has_pads>
Giorgio Arena368e6352018-08-20 15:06:07 +0100192void NEIm2ColKernel::run_im2col(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193{
194 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
195 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
196
Giorgio Arena156fcf32018-03-09 15:30:43 +0000197 const DataLayout data_layout = _input->info()->data_layout();
198 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
199 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
200 const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
201
202 const int kernel_depth = _input->info()->dimension(channel_idx);
203 const int input_w = _input->info()->dimension(width_idx);
204 const int input_h = _input->info()->dimension(height_idx);
205 const int input_stride_x = _input->info()->strides_in_bytes()[width_idx];
206 const int input_stride_y = _input->info()->strides_in_bytes()[height_idx];
207 const int input_stride_z = _input->info()->strides_in_bytes()[channel_idx];
Isabella Gottardie6630e42018-01-18 15:50:39 +0000208 const int offset = is_data_type_quantized(_input->info()->data_type()) ? _input->info()->quantization_info().offset : 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100210 int pad_left = 0;
211 int pad_top = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212 int stride_x = 0;
213 int stride_y = 0;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100214 pad_left = _conv_info.pad_left();
215 pad_top = _conv_info.pad_top();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 std::tie(stride_x, stride_y) = _conv_info.stride();
217
218 // Setup input window
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100219 const int start_x = -pad_left;
220 const int start_y = -pad_top;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221
Giorgio Arenaf485a102018-04-20 16:06:21 +0100222 Window window_in_out(window);
223 // The first three dimensions of the input and output are increased by the inner loops
224 window_in_out.set(Window::DimX, Window::Dimension(0, 0, 0));
225 window_in_out.set(Window::DimY, Window::Dimension(0, 0, 0));
226 window_in_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227
228 // Create iterators
Giorgio Arenaf485a102018-04-20 16:06:21 +0100229 Iterator in(_input, window_in_out);
230 Iterator out(_output, window_in_out);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231
232 execute_window_loop(window, [&](const Coordinates & id)
233 {
Giorgio Arena156fcf32018-03-09 15:30:43 +0000234 const int top_left_x = id[width_idx] * stride_x + start_x;
235 const int top_left_y = id[height_idx] * stride_y + start_y;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236
237 // Get pointers
238 const uint8_t *const input_ptr = in.ptr();
Giorgio Arenaf485a102018-04-20 16:06:21 +0100239 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 +0100240
241 // Linearize volume
242 linearize_volume<T, has_pads>(input_ptr,
243 output_ptr,
244 _has_bias,
245 top_left_x,
246 top_left_y,
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100247 static_cast<int>(_kernel_width),
248 static_cast<int>(_kernel_height),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 kernel_depth,
250 input_w,
251 input_h,
252 input_stride_x,
253 input_stride_y,
254 input_stride_z,
Alex Gilday7da29b62018-03-23 14:16:00 +0000255 offset,
256 _dilation.x(),
257 _dilation.y());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258 },
259 in, out);
260}
261
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262NEIm2ColKernel::NEIm2ColKernel()
Alex Gilday7da29b62018-03-23 14:16:00 +0000263 : _func(), _input(nullptr), _output(nullptr), _convolved_dims(), _conv_info(), _kernel_width(0), _kernel_height(0), _has_bias(false), _dilation(1U, 1U)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264{
265}
266
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000267void NEIm2ColKernel::configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +0100268 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100269{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000270 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Giorgio Arena368e6352018-08-20 15:06:07 +0100271 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 +0100272 ARM_COMPUTE_UNUSED(num_groups);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273
Giorgio Arena156fcf32018-03-09 15:30:43 +0000274 const DataLayout data_layout = input->info()->data_layout();
275 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
276 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000277
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100278 _input = input;
279 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100280 _conv_info = conv_info;
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100281 _kernel_width = kernel_dims.width;
Alex Gilday7da29b62018-03-23 14:16:00 +0000282 _kernel_height = kernel_dims.height;
283 _dilation = dilation;
Giorgio Arena156fcf32018-03-09 15:30:43 +0000284 _convolved_dims = scaled_dimensions(input->info()->dimension(width_idx), input->info()->dimension(height_idx),
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100285 _kernel_width, _kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000286 _conv_info, _dilation);
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100287 _has_bias = has_bias;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288
Giorgio Arena368e6352018-08-20 15:06:07 +0100289 switch(_input->info()->data_type())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290 {
Giorgio Arena368e6352018-08-20 15:06:07 +0100291 case DataType::F32:
292 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float, false> : &NEIm2ColKernel::run_im2col<float, true>;
293 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000294#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Giorgio Arena368e6352018-08-20 15:06:07 +0100295 case DataType::F16:
296 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<float16_t, false> : &NEIm2ColKernel::run_im2col<float16_t, true>;
297 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000298#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Giorgio Arena368e6352018-08-20 15:06:07 +0100299 case DataType::QASYMM8:
300 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_im2col<qasymm8_t, false> : &NEIm2ColKernel::run_im2col<qasymm8_t, true>;
301 break;
302 default:
303 ARM_COMPUTE_ERROR("Data type not supported");
304 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100305 }
306
Giorgio Arena368e6352018-08-20 15:06:07 +0100307 // Configure kernel window
308 auto win_config = validate_and_configure_window(input->info(), output->info(), kernel_dims, conv_info, has_bias, dilation);
309 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
310 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311}
312
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000313Status NEIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info,
Giorgio Arena368e6352018-08-20 15:06:07 +0100314 bool has_bias, const Size2D &dilation, unsigned int num_groups)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000315{
Giorgio Arena368e6352018-08-20 15:06:07 +0100316 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, dilation, num_groups));
317 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 +0000318 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000319}
320
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100321void NEIm2ColKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100323 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
325 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
326
327 (this->*_func)(window);
328}