blob: 5bb8b1c22acc8b793bba1831e32e14d448124ef3 [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 {
139 *out_ptr = scvt_qs8_f32(1.0f, fixed_point_position);
140 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100141 else if(std::is_same<T, qint16_t>::value)
142 {
143 *out_ptr = scvt_qs16_f32(1.0f, fixed_point_position);
144 }
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
166 int pad_x = 0;
167 int pad_y = 0;
168 int stride_x = 0;
169 int stride_y = 0;
170 std::tie(pad_x, pad_y) = _conv_info.pad();
171 std::tie(stride_x, stride_y) = _conv_info.stride();
172
173 // Setup input window
174 const int start_x = -pad_x;
175 const int start_y = -pad_y;
176
177 Window window_in(window);
178 // The first three dimensions of the input are increased by the inner loops
179 window_in.set(Window::DimX, Window::Dimension(0, 0, 0));
180 window_in.set(Window::DimY, Window::Dimension(0, 0, 0));
181 window_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
182
183 // Setup output window
184 Window window_out(window);
185 window_out.set(Window::DimX, Window::Dimension(0, _output->info()->dimension(0), _output->info()->strides_in_bytes().y() / _output->info()->element_size()));
186 window_out.set(Window::DimY, Window::Dimension(window.y().start() * _convolved_dims.first, window.y().end() * _convolved_dims.first, _convolved_dims.first));
187 window_out.set(Window::DimZ, Window::Dimension(0, 1, 1));
188
189 // Create iterators
190 Iterator in(_input, window_in);
191 Iterator out(_output, window_out);
192
193 execute_window_loop(window, [&](const Coordinates & id)
194 {
195 const int top_left_x = id.x() * stride_x + start_x;
196 const int top_left_y = id.y() * stride_y + start_y;
197
198 // Get pointers
199 const uint8_t *const input_ptr = in.ptr();
200 auto output_ptr = reinterpret_cast<T *>(out.ptr());
201
202 // Linearize volume
203 linearize_volume<T, has_pads>(input_ptr,
204 output_ptr,
205 _has_bias,
206 top_left_x,
207 top_left_y,
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100208 static_cast<int>(_kernel_width),
209 static_cast<int>(_kernel_height),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 kernel_depth,
211 input_w,
212 input_h,
213 input_stride_x,
214 input_stride_y,
215 input_stride_z,
216 _input->info()->fixed_point_position());
217 },
218 in, out);
219}
220
221template <typename T>
222void NEIm2ColKernel::run_reduced(const Window &window)
223{
224 const size_t in_width = _input->info()->dimension(0);
225 const size_t in_height = _input->info()->dimension(1);
226 const size_t out_step_x = in_width * _input->info()->element_size();
227 const size_t out_step_y = out_step_x * in_height;
228 const size_t out_width = _output->info()->dimension(0);
229
230 Window in_window(window);
231 in_window.set(Window::DimX, Window::Dimension(0, 1, 1));
232
233 Window out_window;
234 out_window.use_tensor_dimensions(_output->info());
235 out_window.set(Window::DimX, Window::Dimension(out_window.x().start(), out_window.x().end(), in_width));
236
237 Window in_slice = in_window.first_slice_window_3D();
238 Window out_slice = out_window.first_slice_window_1D();
239
240 do
241 {
242 Iterator in(_input, in_slice);
243 Iterator out(_output, out_slice);
244
245 uint8_t *out_ptr = out.ptr();
246
247 execute_window_loop(in_slice, [&](const Coordinates & id)
248 {
249 memcpy(out_ptr + id.y() * out_step_x + id.z() * out_step_y, in.ptr(), out_step_x);
250 },
251 in);
252
253 // Add bias
254 if(_has_bias)
255 {
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100256 if(std::is_same<T, qint8_t>::value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257 {
258 *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = scvt_qs8_f32(1.0f, _input->info()->fixed_point_position());
259 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100260 else if(std::is_same<T, qint16_t>::value)
261 {
262 *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = scvt_qs16_f32(1.0f, _input->info()->fixed_point_position());
263 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264 else
265 {
266 *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = static_cast<T>(1);
267 }
268 }
269 }
270 while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice));
271}
272
273NEIm2ColKernel::NEIm2ColKernel()
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100274 : _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 +0100275{
276}
277
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100278void 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 +0100279{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100280 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 +0100281 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100282 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100283
284 _input = input;
285 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286 _conv_info = conv_info;
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100287 _kernel_width = kernel_dims.width;
288 _kernel_height = kernel_dims.height,
289 _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
290 _kernel_width, _kernel_height,
291 _conv_info);
292 _has_bias = has_bias;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100293
294 unsigned int pad_x, pad_y, stride_x, stride_y = 0;
295 std::tie(pad_x, pad_y) = conv_info.pad();
296 std::tie(stride_x, stride_y) = conv_info.stride();
297
298 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)
299 && (std::equal(input->info()->tensor_shape().cbegin() + 3,
300 input->info()->tensor_shape().cend(),
301 output->info()->tensor_shape().cbegin() + 1))
302 && ((stride_x == 1) && (stride_y == 1) && (pad_x == 0) && (pad_y == 0));
303
304 Window window = calculate_max_window(*input->info(), Steps());
305
306 if(run_img2col_reduced)
307 {
308 switch(_input->info()->data_type())
309 {
310 case DataType::F32:
311 _func = &NEIm2ColKernel::run_reduced<float>;
312 break;
Pablo Tello659abc02017-06-22 16:00:16 +0100313#ifdef ARM_COMPUTE_ENABLE_FP16
314 case DataType::F16:
315 _func = &NEIm2ColKernel::run_reduced<float16_t>;
316 break;
Anthony Barbierac69aa12017-07-03 17:39:37 +0100317#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318 case DataType::QS8:
319 _func = &NEIm2ColKernel::run_reduced<qint8_t>;
320 break;
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100321 case DataType::QS16:
322 _func = &NEIm2ColKernel::run_reduced<qint16_t>;
323 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324 default:
325 ARM_COMPUTE_ERROR("Data type not supported");
326 break;
327 }
328 }
329 else
330 {
331 switch(_input->info()->data_type())
332 {
333 case DataType::F32:
334 _func = ((pad_x == 0) && (pad_y == 0)) ? &NEIm2ColKernel::run_generic<float, false> : &NEIm2ColKernel::run_generic<float, true>;
335 break;
Pablo Tello659abc02017-06-22 16:00:16 +0100336#ifdef ARM_COMPUTE_ENABLE_FP16
337 case DataType::F16:
338 _func = ((pad_x == 0) && (pad_y == 0)) ? &NEIm2ColKernel::run_generic<float16_t, false> : &NEIm2ColKernel::run_generic<float16_t, true>;
339 break;
Anthony Barbierac69aa12017-07-03 17:39:37 +0100340#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100341 case DataType::QS8:
342 _func = ((pad_x == 0) && (pad_y == 0)) ? &NEIm2ColKernel::run_generic<qint8_t, false> : &NEIm2ColKernel::run_generic<qint8_t, true>;
343 break;
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100344 case DataType::QS16:
345 _func = ((pad_x == 0) && (pad_y == 0)) ? &NEIm2ColKernel::run_generic<qint16_t, false> : &NEIm2ColKernel::run_generic<qint16_t, true>;
346 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100347 default:
348 ARM_COMPUTE_ERROR("Data type not supported");
349 break;
350 }
351 window.set(Window::DimX, Window::Dimension(0, _convolved_dims.first, 1));
352 window.set(Window::DimY, Window::Dimension(0, _convolved_dims.second, 1));
353 window.set(Window::DimZ, Window::Dimension(0, 1, 1));
354 }
355
356 // The NEIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped
357 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
358
359 IKernel::configure(window);
360}
361
362void NEIm2ColKernel::run(const Window &window)
363{
364 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
365 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
366
367 (this->*_func)(window);
368}