blob: 8eb235b36001eef87739aa86ae3378c2ca2bbcf1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/FixedPoint.h"
28#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
35#include <arm_neon.h>
36#include <cstddef>
37#include <cstdint>
38#include <cstring>
39#include <tuple>
40
41using namespace arm_compute;
42
43namespace
44{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000045Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
Georgios Pinitasd912fd82017-11-27 21:00:13 +000046{
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
50 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias);
51 ARM_COMPUTE_UNUSED(kernel_dims);
52 ARM_COMPUTE_UNUSED(conv_info);
53
Georgios Pinitas631c41a2017-12-06 11:53:03 +000054 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +000055}
56
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057template <typename T, bool has_pads>
58inline void linearize_volume(const uint8_t *const in_ptr,
59 T *out_ptr,
60 bool has_bias,
61 int top_left_x,
62 int top_left_y,
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010063 int kernel_width,
64 int kernel_height,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 int kernel_depth,
66 int input_w,
67 int input_h,
68 int input_stride_x,
69 int input_stride_y,
70 int input_stride_z,
71 int fixed_point_position)
72{
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010073 const int kernel_size2 = kernel_width * kernel_height;
74 const int x_e = top_left_x + kernel_width;
75 const int y_e = top_left_y + kernel_height;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
77 // Linearize volume
78 int d = 0;
79 // This for loop linearize a volume with 3 slices. This allows:
80 // 1) to reduce the iterations of the outer for loop "d"
81 // 2) to have an optimized im2col for the first convolution layer where usually we have 3 IFMs
82 for(; d <= (kernel_depth - 3); d += 3)
83 {
84 for(int y = top_left_y; y < y_e; ++y)
85 {
86 if((y < 0 || y >= input_h) && has_pads)
87 {
88 // All the values will be zeros
89 for(int x = top_left_x; x < x_e; ++x, ++out_ptr)
90 {
91 *(out_ptr + 0 * kernel_size2) = 0;
92 *(out_ptr + 1 * kernel_size2) = 0;
93 *(out_ptr + 2 * kernel_size2) = 0;
94 }
95 }
96 else
97 {
98 for(int x = top_left_x; x < x_e; ++x, ++out_ptr)
99 {
100 if((x < 0 || x >= input_w) && has_pads)
101 {
102 *(out_ptr + 0 * kernel_size2) = 0;
103 *(out_ptr + 1 * kernel_size2) = 0;
104 *(out_ptr + 2 * kernel_size2) = 0;
105 }
106 else
107 {
108 *(out_ptr + 0 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 0) * input_stride_z + y * input_stride_y + x * input_stride_x)));
109 *(out_ptr + 1 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 1) * input_stride_z + y * input_stride_y + x * input_stride_x)));
110 *(out_ptr + 2 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 2) * input_stride_z + y * input_stride_y + x * input_stride_x)));
111 }
112 }
113 }
114 }
115 out_ptr += 2 * kernel_size2;
116 }
117
118 // Left over
119 for(; d < kernel_depth; d++)
120 {
121 for(int y = top_left_y; y < y_e; ++y)
122 {
123 if((y < 0 || y >= input_h) && has_pads)
124 {
125 // All the values will be zeros
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100126 memset(out_ptr, 0, kernel_width * sizeof(T));
127 out_ptr += kernel_width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 }
129 else
130 {
131 for(int x = top_left_x; x < x_e; ++x, ++out_ptr)
132 {
133 if((x < 0 || x >= input_w) && has_pads)
134 {
135 *out_ptr = 0;
136 }
137 else
138 {
139 *out_ptr = *(reinterpret_cast<const T *>(in_ptr + (d * input_stride_z + y * input_stride_y + x * input_stride_x)));
140 }
141 }
142 }
143 }
144 }
145
146 // Append 1 if the convolution layer has biases
147 if(has_bias)
148 {
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100149 if(std::is_same<T, qint8_t>::value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 {
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100151 *out_ptr = sqcvt_qs8_f32(1.0f, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100153 else if(std::is_same<T, qint16_t>::value)
154 {
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100155 *out_ptr = sqcvt_qs16_f32(1.0f, fixed_point_position);
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100156 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 else
158 {
159 *out_ptr = static_cast<T>(1);
160 }
161 }
162}
163} // namespace
164
165template <typename T, bool has_pads>
166void NEIm2ColKernel::run_generic(const Window &window)
167{
168 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
169 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
170
171 const int kernel_depth = _input->info()->dimension(2);
172 const int input_w = _input->info()->dimension(0);
173 const int input_h = _input->info()->dimension(1);
174 const int input_stride_x = _input->info()->strides_in_bytes().x();
175 const int input_stride_y = _input->info()->strides_in_bytes().y();
176 const int input_stride_z = _input->info()->strides_in_bytes().z();
177
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100178 int pad_left = 0;
179 int pad_top = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 int stride_x = 0;
181 int stride_y = 0;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100182 pad_left = _conv_info.pad_left();
183 pad_top = _conv_info.pad_top();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184 std::tie(stride_x, stride_y) = _conv_info.stride();
185
186 // Setup input window
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100187 const int start_x = -pad_left;
188 const int start_y = -pad_top;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189
190 Window window_in(window);
191 // The first three dimensions of the input are increased by the inner loops
192 window_in.set(Window::DimX, Window::Dimension(0, 0, 0));
193 window_in.set(Window::DimY, Window::Dimension(0, 0, 0));
194 window_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
195
196 // Setup output window
197 Window window_out(window);
198 window_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _output->info()->strides_in_bytes().y() / _output->info()->element_size()));
199 window_out.set(Window::DimY, Window::Dimension(window.y().start() * _convolved_dims.first, window.y().end() * _convolved_dims.first, _convolved_dims.first));
200 window_out.set(Window::DimZ, Window::Dimension(0, 1, 1));
201
202 // Create iterators
203 Iterator in(_input, window_in);
204 Iterator out(_output, window_out);
205
206 execute_window_loop(window, [&](const Coordinates & id)
207 {
208 const int top_left_x = id.x() * stride_x + start_x;
209 const int top_left_y = id.y() * stride_y + start_y;
210
211 // Get pointers
212 const uint8_t *const input_ptr = in.ptr();
213 auto output_ptr = reinterpret_cast<T *>(out.ptr());
214
215 // Linearize volume
216 linearize_volume<T, has_pads>(input_ptr,
217 output_ptr,
218 _has_bias,
219 top_left_x,
220 top_left_y,
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100221 static_cast<int>(_kernel_width),
222 static_cast<int>(_kernel_height),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 kernel_depth,
224 input_w,
225 input_h,
226 input_stride_x,
227 input_stride_y,
228 input_stride_z,
229 _input->info()->fixed_point_position());
230 },
231 in, out);
232}
233
234template <typename T>
235void NEIm2ColKernel::run_reduced(const Window &window)
236{
237 const size_t in_width = _input->info()->dimension(0);
238 const size_t in_height = _input->info()->dimension(1);
239 const size_t out_step_x = in_width * _input->info()->element_size();
240 const size_t out_step_y = out_step_x * in_height;
241 const size_t out_width = _output->info()->dimension(0);
242
243 Window in_window(window);
244 in_window.set(Window::DimX, Window::Dimension(0, 1, 1));
245
246 Window out_window;
SiCong Li86b53332017-08-23 11:02:43 +0100247 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248 out_window.set(Window::DimX, Window::Dimension(out_window.x().start(), out_window.x().end(), in_width));
249
250 Window in_slice = in_window.first_slice_window_3D();
251 Window out_slice = out_window.first_slice_window_1D();
252
253 do
254 {
255 Iterator in(_input, in_slice);
256 Iterator out(_output, out_slice);
257
258 uint8_t *out_ptr = out.ptr();
259
260 execute_window_loop(in_slice, [&](const Coordinates & id)
261 {
262 memcpy(out_ptr + id.y() * out_step_x + id.z() * out_step_y, in.ptr(), out_step_x);
263 },
264 in);
265
266 // Add bias
267 if(_has_bias)
268 {
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100269 if(std::is_same<T, qint8_t>::value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270 {
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100271 *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = sqcvt_qs8_f32(1.0f, _input->info()->fixed_point_position());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100273 else if(std::is_same<T, qint16_t>::value)
274 {
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100275 *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = sqcvt_qs16_f32(1.0f, _input->info()->fixed_point_position());
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100276 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100277 else
278 {
279 *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = static_cast<T>(1);
280 }
281 }
282 }
283 while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
284}
285
286NEIm2ColKernel::NEIm2ColKernel()
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100287 : _func(), _input(nullptr), _output(nullptr), _convolved_dims(), _conv_info(), _kernel_width(0), _kernel_height(0), _has_bias(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288{
289}
290
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100291void NEIm2ColKernel::configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292{
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000293 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
294
295 // Perform validation step
296 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), kernel_dims, conv_info, has_bias));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297
298 _input = input;
299 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100300 _conv_info = conv_info;
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100301 _kernel_width = kernel_dims.width;
302 _kernel_height = kernel_dims.height,
303 _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
304 _kernel_width, _kernel_height,
305 _conv_info);
306 _has_bias = has_bias;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100308 unsigned int stride_x = 0;
309 unsigned int stride_y = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100310 std::tie(stride_x, stride_y) = conv_info.stride();
311
312 bool run_img2col_reduced = (output->info()->dimension(0) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))) && (TensorShape::num_max_dimensions >= 4)
313 && (std::equal(input->info()->tensor_shape().cbegin() + 3,
314 input->info()->tensor_shape().cend(),
315 output->info()->tensor_shape().cbegin() + 1))
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100316 && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100317
318 Window window = calculate_max_window(*input->info(), Steps());
319
320 if(run_img2col_reduced)
321 {
322 switch(_input->info()->data_type())
323 {
324 case DataType::F32:
325 _func = &NEIm2ColKernel::run_reduced<float>;
326 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000327#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello659abc02017-06-22 16:00:16 +0100328 case DataType::F16:
329 _func = &NEIm2ColKernel::run_reduced<float16_t>;
330 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000331#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332 case DataType::QS8:
333 _func = &NEIm2ColKernel::run_reduced<qint8_t>;
334 break;
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100335 case DataType::QS16:
336 _func = &NEIm2ColKernel::run_reduced<qint16_t>;
337 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338 default:
339 ARM_COMPUTE_ERROR("Data type not supported");
340 break;
341 }
342 }
343 else
344 {
345 switch(_input->info()->data_type())
346 {
347 case DataType::F32:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100348 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<float, false> : &NEIm2ColKernel::run_generic<float, true>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100349 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000350#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello659abc02017-06-22 16:00:16 +0100351 case DataType::F16:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100352 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<float16_t, false> : &NEIm2ColKernel::run_generic<float16_t, true>;
Pablo Tello659abc02017-06-22 16:00:16 +0100353 break;
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000354#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100355 case DataType::QS8:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100356 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<qint8_t, false> : &NEIm2ColKernel::run_generic<qint8_t, true>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100357 break;
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100358 case DataType::QS16:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100359 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<qint16_t, false> : &NEIm2ColKernel::run_generic<qint16_t, true>;
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100360 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100361 default:
362 ARM_COMPUTE_ERROR("Data type not supported");
363 break;
364 }
365 window.set(Window::DimX, Window::Dimension(0, _convolved_dims.first, 1));
366 window.set(Window::DimY, Window::Dimension(0, _convolved_dims.second, 1));
367 window.set(Window::DimZ, Window::Dimension(0, 1, 1));
368 }
369
370 // The NEIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped
371 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
372
373 IKernel::configure(window);
374}
375
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000376Status NEIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias)
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000377{
378 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000379 return Status{};
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000380}
381
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100382void NEIm2ColKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100383{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100384 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100385 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
386 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
387
388 (this->*_func)(window);
389}