Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 36 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include <arm_neon.h> |
| 38 | #include <cstddef> |
| 39 | #include <cstdint> |
| 40 | #include <cstring> |
| 41 | #include <tuple> |
| 42 | |
| 43 | using namespace arm_compute; |
| 44 | |
| 45 | namespace |
| 46 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 47 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 48 | bool has_bias, bool is_fully_connected, bool is_flatten, const Size2D &dilation) |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 49 | { |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::QASYMM8 && has_bias); |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 55 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 56 | TensorShape expected_output_shape; |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 57 | if(is_flatten) /* Called by FlattenLayer */ |
| 58 | { |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 59 | expected_output_shape = misc::shape_calculator::compute_im2col_flatten_shape(input); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 60 | } |
| 61 | else if(!is_fully_connected) /* Called by ConvolutionLayer */ |
| 62 | { |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 63 | expected_output_shape = misc::shape_calculator::compute_im2col_conv_shape(input, kernel_dims, conv_info, has_bias, dilation); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 64 | } |
| 65 | else /* Called by FullyConnectedLayer */ |
| 66 | { |
| 67 | const int num_batch_dimensions = std::max(0, static_cast<int>(output->tensor_shape().num_dimensions()) - 1); |
| 68 | const int num_input_dimensions = input->tensor_shape().num_dimensions() - num_batch_dimensions; |
| 69 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 70 | expected_output_shape = misc::shape_calculator::compute_im2col_fc_shape(input, num_input_dimensions); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 71 | } |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 72 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 73 | TensorInfo expected_output = output->clone()->set_tensor_shape(expected_output_shape); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(&expected_output, output); |
| 75 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 76 | return Status{}; |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | template <typename T, bool has_pads> |
| 80 | inline void linearize_volume(const uint8_t *const in_ptr, |
| 81 | T *out_ptr, |
| 82 | bool has_bias, |
| 83 | int top_left_x, |
| 84 | int top_left_y, |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 85 | int kernel_width, |
| 86 | int kernel_height, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | int kernel_depth, |
| 88 | int input_w, |
| 89 | int input_h, |
| 90 | int input_stride_x, |
| 91 | int input_stride_y, |
| 92 | int input_stride_z, |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 93 | int fixed_point_position, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 94 | int pad_value, |
| 95 | int dilation_x, |
| 96 | int dilation_y) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | { |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 98 | const int kernel_size2 = kernel_width * kernel_height; |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 99 | const int x_e = top_left_x + kernel_width * dilation_x; |
| 100 | const int y_e = top_left_y + kernel_height * dilation_y; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | |
| 102 | // Linearize volume |
| 103 | int d = 0; |
| 104 | // This for loop linearize a volume with 3 slices. This allows: |
| 105 | // 1) to reduce the iterations of the outer for loop "d" |
| 106 | // 2) to have an optimized im2col for the first convolution layer where usually we have 3 IFMs |
| 107 | for(; d <= (kernel_depth - 3); d += 3) |
| 108 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 109 | for(int y = top_left_y; y < y_e; y += dilation_y) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | { |
| 111 | if((y < 0 || y >= input_h) && has_pads) |
| 112 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 113 | // All the values will be the offset (will be zeros when not quantized) |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 114 | for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 116 | *(out_ptr + 0 * kernel_size2) = pad_value; |
| 117 | *(out_ptr + 1 * kernel_size2) = pad_value; |
| 118 | *(out_ptr + 2 * kernel_size2) = pad_value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | else |
| 122 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 123 | for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | { |
| 125 | if((x < 0 || x >= input_w) && has_pads) |
| 126 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 127 | *(out_ptr + 0 * kernel_size2) = pad_value; |
| 128 | *(out_ptr + 1 * kernel_size2) = pad_value; |
| 129 | *(out_ptr + 2 * kernel_size2) = pad_value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | { |
| 133 | *(out_ptr + 0 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 0) * input_stride_z + y * input_stride_y + x * input_stride_x))); |
| 134 | *(out_ptr + 1 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 1) * input_stride_z + y * input_stride_y + x * input_stride_x))); |
| 135 | *(out_ptr + 2 * kernel_size2) = *(reinterpret_cast<const T *>(in_ptr + ((d + 2) * input_stride_z + y * input_stride_y + x * input_stride_x))); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | out_ptr += 2 * kernel_size2; |
| 141 | } |
| 142 | |
| 143 | // Left over |
| 144 | for(; d < kernel_depth; d++) |
| 145 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 146 | for(int y = top_left_y; y < y_e; y += dilation_y) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | { |
| 148 | if((y < 0 || y >= input_h) && has_pads) |
| 149 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 150 | // All the values will be the offset (will be zeros when not quantized) |
| 151 | memset(out_ptr, pad_value, kernel_width * sizeof(T)); |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 152 | out_ptr += kernel_width; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | } |
| 154 | else |
| 155 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 156 | for(int x = top_left_x; x < x_e; x += dilation_x, ++out_ptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | { |
| 158 | if((x < 0 || x >= input_w) && has_pads) |
| 159 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 160 | *out_ptr = pad_value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | } |
| 162 | else |
| 163 | { |
| 164 | *out_ptr = *(reinterpret_cast<const T *>(in_ptr + (d * input_stride_z + y * input_stride_y + x * input_stride_x))); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Append 1 if the convolution layer has biases |
| 172 | if(has_bias) |
| 173 | { |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 174 | if(std::is_same<T, qint8_t>::value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 176 | *out_ptr = sqcvt_qs8_f32(1.0f, fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | } |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 178 | else if(std::is_same<T, qint16_t>::value) |
| 179 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 180 | *out_ptr = sqcvt_qs16_f32(1.0f, fixed_point_position); |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 181 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | else |
| 183 | { |
| 184 | *out_ptr = static_cast<T>(1); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } // namespace |
| 189 | |
| 190 | template <typename T, bool has_pads> |
| 191 | void NEIm2ColKernel::run_generic(const Window &window) |
| 192 | { |
| 193 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 194 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 195 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 196 | const DataLayout data_layout = _input->info()->data_layout(); |
| 197 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 198 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 199 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 200 | |
| 201 | const int kernel_depth = _input->info()->dimension(channel_idx); |
| 202 | const int input_w = _input->info()->dimension(width_idx); |
| 203 | const int input_h = _input->info()->dimension(height_idx); |
| 204 | const int input_stride_x = _input->info()->strides_in_bytes()[width_idx]; |
| 205 | const int input_stride_y = _input->info()->strides_in_bytes()[height_idx]; |
| 206 | const int input_stride_z = _input->info()->strides_in_bytes()[channel_idx]; |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 207 | const int offset = is_data_type_quantized(_input->info()->data_type()) ? _input->info()->quantization_info().offset : 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 208 | |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 209 | int pad_left = 0; |
| 210 | int pad_top = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | int stride_x = 0; |
| 212 | int stride_y = 0; |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 213 | pad_left = _conv_info.pad_left(); |
| 214 | pad_top = _conv_info.pad_top(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 215 | std::tie(stride_x, stride_y) = _conv_info.stride(); |
| 216 | |
| 217 | // Setup input window |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 218 | const int start_x = -pad_left; |
| 219 | const int start_y = -pad_top; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 220 | |
| 221 | Window window_in(window); |
| 222 | // The first three dimensions of the input are increased by the inner loops |
| 223 | window_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 224 | window_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 225 | window_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 226 | |
| 227 | // Setup output window |
| 228 | Window window_out(window); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 229 | window_out.set(width_idx, Window::Dimension(0, _output->info()->dimension(width_idx), _output->info()->strides_in_bytes()[width_idx + 1] / _output->info()->strides_in_bytes()[width_idx])); |
| 230 | window_out.set(height_idx, Window::Dimension(window[height_idx].start() * _convolved_dims.first, window[height_idx].end() * _convolved_dims.first, _convolved_dims.first)); |
| 231 | window_out.set(channel_idx, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 232 | |
| 233 | // Create iterators |
| 234 | Iterator in(_input, window_in); |
| 235 | Iterator out(_output, window_out); |
| 236 | |
| 237 | execute_window_loop(window, [&](const Coordinates & id) |
| 238 | { |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 239 | const int top_left_x = id[width_idx] * stride_x + start_x; |
| 240 | const int top_left_y = id[height_idx] * stride_y + start_y; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 241 | |
| 242 | // Get pointers |
| 243 | const uint8_t *const input_ptr = in.ptr(); |
| 244 | auto output_ptr = reinterpret_cast<T *>(out.ptr()); |
| 245 | |
| 246 | // Linearize volume |
| 247 | linearize_volume<T, has_pads>(input_ptr, |
| 248 | output_ptr, |
| 249 | _has_bias, |
| 250 | top_left_x, |
| 251 | top_left_y, |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 252 | static_cast<int>(_kernel_width), |
| 253 | static_cast<int>(_kernel_height), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 254 | kernel_depth, |
| 255 | input_w, |
| 256 | input_h, |
| 257 | input_stride_x, |
| 258 | input_stride_y, |
| 259 | input_stride_z, |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 260 | _input->info()->fixed_point_position(), |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 261 | offset, |
| 262 | _dilation.x(), |
| 263 | _dilation.y()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 264 | }, |
| 265 | in, out); |
| 266 | } |
| 267 | |
| 268 | template <typename T> |
| 269 | void NEIm2ColKernel::run_reduced(const Window &window) |
| 270 | { |
| 271 | const size_t in_width = _input->info()->dimension(0); |
| 272 | const size_t in_height = _input->info()->dimension(1); |
| 273 | const size_t out_step_x = in_width * _input->info()->element_size(); |
| 274 | const size_t out_step_y = out_step_x * in_height; |
| 275 | const size_t out_width = _output->info()->dimension(0); |
| 276 | |
| 277 | Window in_window(window); |
| 278 | in_window.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 279 | |
| 280 | Window out_window; |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 281 | out_window.use_tensor_dimensions(_output->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 282 | out_window.set(Window::DimX, Window::Dimension(out_window.x().start(), out_window.x().end(), in_width)); |
| 283 | |
| 284 | Window in_slice = in_window.first_slice_window_3D(); |
| 285 | Window out_slice = out_window.first_slice_window_1D(); |
| 286 | |
| 287 | do |
| 288 | { |
| 289 | Iterator in(_input, in_slice); |
| 290 | Iterator out(_output, out_slice); |
| 291 | |
| 292 | uint8_t *out_ptr = out.ptr(); |
| 293 | |
| 294 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 295 | { |
| 296 | memcpy(out_ptr + id.y() * out_step_x + id.z() * out_step_y, in.ptr(), out_step_x); |
| 297 | }, |
| 298 | in); |
| 299 | |
| 300 | // Add bias |
| 301 | if(_has_bias) |
| 302 | { |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 303 | if(std::is_same<T, qint8_t>::value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 304 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 305 | *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = sqcvt_qs8_f32(1.0f, _input->info()->fixed_point_position()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 306 | } |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 307 | else if(std::is_same<T, qint16_t>::value) |
| 308 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 309 | *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = sqcvt_qs16_f32(1.0f, _input->info()->fixed_point_position()); |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 310 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 311 | else |
| 312 | { |
| 313 | *(reinterpret_cast<T *>(out_ptr) + out_width - 1) = static_cast<T>(1); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_1D(out_slice)); |
| 318 | } |
| 319 | |
| 320 | NEIm2ColKernel::NEIm2ColKernel() |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 321 | : _func(), _input(nullptr), _output(nullptr), _convolved_dims(), _conv_info(), _kernel_width(0), _kernel_height(0), _has_bias(false), _dilation(1U, 1U) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 322 | { |
| 323 | } |
| 324 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 325 | void NEIm2ColKernel::configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 326 | bool has_bias, bool is_fully_connected, bool is_flatten, const Size2D &dilation) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 327 | { |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 328 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 329 | |
| 330 | // Perform validation step |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 331 | ARM_COMPUTE_UNUSED(is_fully_connected, is_flatten); |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 332 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), kernel_dims, conv_info, has_bias, is_fully_connected, is_flatten, dilation)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 334 | const DataLayout data_layout = input->info()->data_layout(); |
| 335 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 336 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 337 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 338 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 339 | _input = input; |
| 340 | _output = output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 341 | _conv_info = conv_info; |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 342 | _kernel_width = kernel_dims.width; |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 343 | _kernel_height = kernel_dims.height; |
| 344 | _dilation = dilation; |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 345 | _convolved_dims = scaled_dimensions(input->info()->dimension(width_idx), input->info()->dimension(height_idx), |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 346 | _kernel_width, _kernel_height, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 347 | _conv_info, _dilation); |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 348 | _has_bias = has_bias; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 349 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 350 | unsigned int stride_x = 0; |
| 351 | unsigned int stride_y = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 352 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 353 | |
| 354 | 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) |
| 355 | && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 356 | input->info()->tensor_shape().cend(), |
| 357 | output->info()->tensor_shape().cbegin() + 1)) |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 358 | && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding()) |
| 359 | && ((dilation.x() == 1) && (dilation.y() == 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 360 | |
| 361 | Window window = calculate_max_window(*input->info(), Steps()); |
| 362 | |
| 363 | if(run_img2col_reduced) |
| 364 | { |
| 365 | switch(_input->info()->data_type()) |
| 366 | { |
| 367 | case DataType::F32: |
| 368 | _func = &NEIm2ColKernel::run_reduced<float>; |
| 369 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 370 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 659abc0 | 2017-06-22 16:00:16 +0100 | [diff] [blame] | 371 | case DataType::F16: |
| 372 | _func = &NEIm2ColKernel::run_reduced<float16_t>; |
| 373 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 374 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 375 | case DataType::QS8: |
| 376 | _func = &NEIm2ColKernel::run_reduced<qint8_t>; |
| 377 | break; |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 378 | case DataType::QS16: |
| 379 | _func = &NEIm2ColKernel::run_reduced<qint16_t>; |
| 380 | break; |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 381 | case DataType::QASYMM8: |
| 382 | _func = &NEIm2ColKernel::run_reduced<qasymm8_t>; |
| 383 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 384 | default: |
| 385 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | switch(_input->info()->data_type()) |
| 392 | { |
| 393 | case DataType::F32: |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 394 | _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<float, false> : &NEIm2ColKernel::run_generic<float, true>; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 395 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 396 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 659abc0 | 2017-06-22 16:00:16 +0100 | [diff] [blame] | 397 | case DataType::F16: |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 398 | _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<float16_t, false> : &NEIm2ColKernel::run_generic<float16_t, true>; |
Pablo Tello | 659abc0 | 2017-06-22 16:00:16 +0100 | [diff] [blame] | 399 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 400 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 401 | case DataType::QS8: |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 402 | _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<qint8_t, false> : &NEIm2ColKernel::run_generic<qint8_t, true>; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 403 | break; |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 404 | case DataType::QS16: |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 405 | _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<qint16_t, false> : &NEIm2ColKernel::run_generic<qint16_t, true>; |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 406 | break; |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 407 | case DataType::QASYMM8: |
| 408 | _func = (!conv_info.has_padding()) ? &NEIm2ColKernel::run_generic<qasymm8_t, false> : &NEIm2ColKernel::run_generic<qasymm8_t, true>; |
| 409 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 410 | default: |
| 411 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 412 | break; |
| 413 | } |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame^] | 414 | window.set(width_idx, Window::Dimension(0, _convolved_dims.first, 1)); |
| 415 | window.set(height_idx, Window::Dimension(0, _convolved_dims.second, 1)); |
| 416 | window.set(channel_idx, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // The NEIm2ColKernel doesn't need padding so update_window_and_padding() can be skipped |
| 420 | output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 421 | |
| 422 | IKernel::configure(window); |
| 423 | } |
| 424 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 425 | Status NEIm2ColKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 426 | bool has_bias, bool is_fully_connected, bool is_flatten, const Size2D &dilation) |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 427 | { |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 428 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, kernel_dims, conv_info, has_bias, is_fully_connected, is_flatten, dilation)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 429 | return Status{}; |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 432 | void NEIm2ColKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 433 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 434 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 435 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 436 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 437 | |
| 438 | (this->*_func)(window); |
| 439 | } |