blob: 099f2f1be331776d31dd9389a8f390c2961efa6e [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{
45template <typename T, bool has_pads>
46inline void linearize_volume(const uint8_t *const in_ptr,
47 T *out_ptr,
48 bool has_bias,
49 int top_left_x,
50 int top_left_y,
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010051 int kernel_width,
52 int kernel_height,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 int kernel_depth,
54 int input_w,
55 int input_h,
56 int input_stride_x,
57 int input_stride_y,
58 int input_stride_z,
59 int fixed_point_position)
60{
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010061 const int kernel_size2 = kernel_width * kernel_height;
62 const int x_e = top_left_x + kernel_width;
63 const int y_e = top_left_y + kernel_height;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064
65 // Linearize volume
66 int d = 0;
67 // This for loop linearize a volume with 3 slices. This allows:
68 // 1) to reduce the iterations of the outer for loop "d"
69 // 2) to have an optimized im2col for the first convolution layer where usually we have 3 IFMs
70 for(; d <= (kernel_depth - 3); d += 3)
71 {
72 for(int y = top_left_y; y < y_e; ++y)
73 {
74 if((y < 0 || y >= input_h) && has_pads)
75 {
76 // All the values will be zeros
77 for(int x = top_left_x; x < x_e; ++x, ++out_ptr)
78 {
79 *(out_ptr + 0 * kernel_size2) = 0;
80 *(out_ptr + 1 * kernel_size2) = 0;
81 *(out_ptr + 2 * kernel_size2) = 0;
82 }
83 }
84 else
85 {
86 for(int x = top_left_x; x < x_e; ++x, ++out_ptr)
87 {
88 if((x < 0 || x >= input_w) && has_pads)
89 {
90 *(out_ptr + 0 * kernel_size2) = 0;
91 *(out_ptr + 1 * kernel_size2) = 0;
92 *(out_ptr + 2 * kernel_size2) = 0;
93 }
94 else
95 {
96 *(out_ptr + 0 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 0) * input_stride_z + y * input_stride_y + x * input_stride_x)));
97 *(out_ptr + 1 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 1) * input_stride_z + y * input_stride_y + x * input_stride_x)));
98 *(out_ptr + 2 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 2) * input_stride_z + y * input_stride_y + x * input_stride_x)));
99 }
100 }
101 }
102 }
103 out_ptr += 2 * kernel_size2;
104 }
105
106 // Left over
107 for(; d < kernel_depth; d++)
108 {
109 for(int y = top_left_y; y < y_e; ++y)
110 {
111 if((y < 0 || y >= input_h) && has_pads)
112 {
113 // All the values will be zeros
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100114 memset(out_ptr, 0, kernel_width * sizeof(T));
115 out_ptr += kernel_width;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 }
117 else
118 {
119 for(int x = top_left_x; x < x_e; ++x, ++out_ptr)
120 {
121 if((x < 0 || x >= input_w) && has_pads)
122 {
123 *out_ptr = 0;
124 }
125 else
126 {
127 *out_ptr = *(reinterpret_cast<const T *>(in_ptr + (d * input_stride_z + y * input_stride_y + x * input_stride_x)));
128 }
129 }
130 }
131 }
132 }
133
134 // Append 1 if the convolution layer has biases
135 if(has_bias)
136 {
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100137 if(std::is_same<T, qint8_t>::value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 {
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100139 *out_ptr = sqcvt_qs8_f32(1.0f, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100141 else if(std::is_same<T, qint16_t>::value)
142 {
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100143 *out_ptr = sqcvt_qs16_f32(1.0f, fixed_point_position);
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100144 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 else
146 {
147 *out_ptr = static_cast<T>(1);
148 }
149 }
150}
151} // namespace
152
153template <typename T, bool has_pads>
154void NEIm2ColKernel::run_generic(const Window &window)
155{
156 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
158
159 const int kernel_depth = _input->info()->dimension(2);
160 const int input_w = _input->info()->dimension(0);
161 const int input_h = _input->info()->dimension(1);
162 const int input_stride_x = _input->info()->strides_in_bytes().x();
163 const int input_stride_y = _input->info()->strides_in_bytes().y();
164 const int input_stride_z = _input->info()->strides_in_bytes().z();
165
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100166 int pad_left = 0;
167 int pad_top = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 int stride_x = 0;
169 int stride_y = 0;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100170 pad_left = _conv_info.pad_left();
171 pad_top = _conv_info.pad_top();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 std::tie(stride_x, stride_y) = _conv_info.stride();
173
174 // Setup input window
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100175 const int start_x = -pad_left;
176 const int start_y = -pad_top;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177
178 Window window_in(window);
179 // The first three dimensions of the input are increased by the inner loops
180 window_in.set(Window::DimX, Window::Dimension(0, 0, 0));
181 window_in.set(Window::DimY, Window::Dimension(0, 0, 0));
182 window_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
183
184 // Setup output window
185 Window window_out(window);
186 window_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _output->info()->strides_in_bytes().y() / _output->info()->element_size()));
187 window_out.set(Window::DimY, Window::Dimension(window.y().start() * _convolved_dims.first, window.y().end() * _convolved_dims.first, _convolved_dims.first));
188 window_out.set(Window::DimZ, Window::Dimension(0, 1, 1));
189
190 // Create iterators
191 Iterator in(_input, window_in);
192 Iterator out(_output, window_out);
193
194 execute_window_loop(window, [&](const Coordinates & id)
195 {
196 const int top_left_x = id.x() * stride_x + start_x;
197 const int top_left_y = id.y() * stride_y + start_y;
198
199 // Get pointers
200 const uint8_t *const input_ptr = in.ptr();
201 auto output_ptr = reinterpret_cast<T *>(out.ptr());
202
203 // Linearize volume
204 linearize_volume<T, has_pads>(input_ptr,
205 output_ptr,
206 _has_bias,
207 top_left_x,
208 top_left_y,
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100209 static_cast<int>(_kernel_width),
210 static_cast<int>(_kernel_height),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 kernel_depth,
212 input_w,
213 input_h,
214 input_stride_x,
215 input_stride_y,
216 input_stride_z,
217 _input->info()->fixed_point_position());
218 },
219 in, out);
220}
221
222template <typename T>
223void NEIm2ColKernel::run_reduced(const Window &window)
224{
225 const size_t in_width = _input->info()->dimension(0);
226 const size_t in_height = _input->info()->dimension(1);
227 const size_t out_step_x = in_width * _input->info()->element_size();
228 const size_t out_step_y = out_step_x * in_height;
229 const size_t out_width = _output->info()->dimension(0);
230
231 Window in_window(window);
232 in_window.set(Window::DimX, Window::Dimension(0, 1, 1));
233
234 Window out_window;
SiCong Li86b53332017-08-23 11:02:43 +0100235 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100236 out_window.set(Window::DimX, Window::Dimension(out_window.x().start(), out_window.x().end(), in_width));
237
238 Window in_slice = in_window.first_slice_window_3D();
239 Window out_slice = out_window.first_slice_window_1D();
240
241 do
242 {
243 Iterator in(_input, in_slice);
244 Iterator out(_output, out_slice);
245
246 uint8_t *out_ptr = out.ptr();
247
248 execute_window_loop(in_slice, [&](const Coordinates & id)
249 {
250 memcpy(out_ptr + id.y() * out_step_x + id.z() * out_step_y, in.ptr(), out_step_x);
251 },
252 in);
253
254 // Add bias
255 if(_has_bias)
256 {
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100257 if(std::is_same<T, qint8_t>::value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258 {
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100259 *(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 +0100260 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100261 else if(std::is_same<T, qint16_t>::value)
262 {
Georgios Pinitas21efeb42017-07-04 12:47:17 +0100263 *(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 +0100264 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100265 else
266 {
267 *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = static_cast<T>(1);
268 }
269 }
270 }
271 while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
272}
273
274NEIm2ColKernel::NEIm2ColKernel()
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100275 : _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 +0100276{
277}
278
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100279void 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 +0100280{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100281 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32, DataType::QS8, DataType::QS16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100283 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100284
285 _input = input;
286 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100287 _conv_info = conv_info;
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100288 _kernel_width = kernel_dims.width;
289 _kernel_height = kernel_dims.height,
290 _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
291 _kernel_width, _kernel_height,
292 _conv_info);
293 _has_bias = has_bias;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100294
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100295 unsigned int stride_x = 0;
296 unsigned int stride_y = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297 std::tie(stride_x, stride_y) = conv_info.stride();
298
299 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)
300 && (std::equal(input->info()->tensor_shape().cbegin() + 3,
301 input->info()->tensor_shape().cend(),
302 output->info()->tensor_shape().cbegin() + 1))
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100303 && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100304
305 Window window = calculate_max_window(*input->info(), Steps());
306
307 if(run_img2col_reduced)
308 {
309 switch(_input->info()->data_type())
310 {
311 case DataType::F32:
312 _func = &NEIm2ColKernel::run_reduced<float>;
313 break;
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100314#ifdef ARM_COMPUTE_AARCH64_V8_2
Pablo Tello659abc02017-06-22 16:00:16 +0100315 case DataType::F16:
316 _func = &NEIm2ColKernel::run_reduced<float16_t>;
317 break;
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100318#endif /* ARM_COMPUTE_AARCH64_V8_2 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319 case DataType::QS8:
320 _func = &NEIm2ColKernel::run_reduced<qint8_t>;
321 break;
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100322 case DataType::QS16:
323 _func = &NEIm2ColKernel::run_reduced<qint16_t>;
324 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100325 default:
326 ARM_COMPUTE_ERROR("Data type not supported");
327 break;
328 }
329 }
330 else
331 {
332 switch(_input->info()->data_type())
333 {
334 case DataType::F32:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100335 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<float, false> : &NEIm2ColKernel::run_generic<float, true>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336 break;
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100337#ifdef ARM_COMPUTE_AARCH64_V8_2
Pablo Tello659abc02017-06-22 16:00:16 +0100338 case DataType::F16:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100339 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<float16_t, false> : &NEIm2ColKernel::run_generic<float16_t, true>;
Pablo Tello659abc02017-06-22 16:00:16 +0100340 break;
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100341#endif /* ARM_COMPUTE_AARCH64_V8_2 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100342 case DataType::QS8:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100343 _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<qint8_t, false> : &NEIm2ColKernel::run_generic<qint8_t, true>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344 break;
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100345 case DataType::QS16:
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100346 _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 +0100347 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100348 default:
349 ARM_COMPUTE_ERROR("Data type not supported");
350 break;
351 }
352 window.set(Window::DimX, Window::Dimension(0, _convolved_dims.first, 1));
353 window.set(Window::DimY, Window::Dimension(0, _convolved_dims.second, 1));
354 window.set(Window::DimZ, Window::Dimension(0, 1, 1));
355 }
356
357 // The NEIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped
358 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
359
360 IKernel::configure(window);
361}
362
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100363void NEIm2ColKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100364{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100365 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100366 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
367 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
368
369 (this->*_func)(window);
370}