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