Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 621965e | 2018-01-08 17:11:26 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayer3x3Kernel.h" |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 25 | #include "arm_compute/core/NEON/kernels/detail/NEDirectConvolutionDetail.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/AccessWindowStatic.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Coordinates.h" |
| 29 | #include "arm_compute/core/Error.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/ITensor.h" |
| 32 | #include "arm_compute/core/NEON/INEKernel.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/TensorShape.h" |
| 35 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 36 | #include "arm_compute/core/Utils.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/Validate.h" |
| 38 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 39 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 40 | #include "support/ToolchainSupport.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 41 | |
| 42 | using namespace arm_compute; |
| 43 | using namespace arm_compute::detail; |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 44 | using namespace arm_compute::misc::shape_calculator; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 45 | using namespace depthwise; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 46 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 47 | namespace |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 48 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 49 | template <typename T1, typename T2, unsigned int stridex> |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 50 | class convolver_3x3 |
| 51 | { |
| 52 | public: |
| 53 | static void convolve(const Window &window, unsigned int num_elems_written_per_iteration, |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 54 | const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 55 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 56 | const int input_offset = -input->info()->quantization_info().offset; |
| 57 | const int weights_offset = -weights->info()->quantization_info().offset; |
| 58 | |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 59 | const int input_stride_x = input->info()->strides_in_bytes().x(); |
| 60 | const int input_stride_y = input->info()->strides_in_bytes().y(); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 61 | const int input_stride_z = input->info()->strides_in_bytes().z(); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 62 | const int output_stride_y = output->info()->strides_in_bytes().y(); |
| 63 | const int kernel_stride_y = weights->info()->strides_in_bytes().y(); |
| 64 | const int kernel_stride_z = weights->info()->strides_in_bytes().z(); |
| 65 | const int output_w = output->info()->dimension(0); |
| 66 | const int output_h = output->info()->dimension(1); |
| 67 | const int delta_input = get_input_num_elems_processed<stridex>(num_elems_written_per_iteration); |
| 68 | const unsigned int conv_stride_y = std::get<1>(conv_info.stride()); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 69 | const unsigned int conv_pad_x = conv_info.pad_left(); |
| 70 | const unsigned int conv_pad_y = conv_info.pad_top(); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 71 | |
| 72 | // setup output window for the iterator |
| 73 | Window window_out = window; |
| 74 | window_out.set(Window::DimX, Window::Dimension(0, output->info()->dimension(Window::DimX), output->info()->dimension(Window::DimX))); |
| 75 | window_out.set(Window::DimY, Window::Dimension(0, output->info()->dimension(Window::DimY), output->info()->dimension(Window::DimY))); |
| 76 | |
| 77 | // setup input window for the iterator |
| 78 | Window window_in = window; |
| 79 | // we just want execute_window_loop to iterate over the dimensions > 2, so we set the first 2 dimensions to 0 |
| 80 | window_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 81 | window_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 82 | |
| 83 | Window window_k = calculate_max_window(*weights->info(), Steps(1u)); |
| 84 | |
| 85 | Iterator in(input, window_in); |
| 86 | Iterator out(output, window_out); |
| 87 | Iterator w(weights, window_k); |
| 88 | |
| 89 | const uint8_t *weights_ptr = w.ptr(); |
| 90 | |
| 91 | execute_window_loop(window_out, [&](const Coordinates & id) |
| 92 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 93 | int ih = 0; |
| 94 | int oh = 0; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 95 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 96 | const uint8_t *input_ptr = in.ptr() - conv_pad_x * input_stride_x - conv_pad_y * input_stride_y - (id.z() - id.z() / depth_multiplier) * input_stride_z; |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 97 | const uint8_t *ptr_weights_base = weights_ptr + id.z() * kernel_stride_z; |
| 98 | |
| 99 | const auto ptr_weights_r0 = reinterpret_cast<const T1 *>(ptr_weights_base); |
| 100 | const auto ptr_weights_r1 = reinterpret_cast<const T1 *>(ptr_weights_base + kernel_stride_y); |
| 101 | const auto ptr_weights_r2 = reinterpret_cast<const T1 *>(ptr_weights_base + kernel_stride_y * 2); |
| 102 | const auto vw_r0 = load_matrix_row(ptr_weights_r0, weights_offset); |
| 103 | const auto vw_r1 = load_matrix_row(ptr_weights_r1, weights_offset); |
| 104 | const auto vw_r2 = load_matrix_row(ptr_weights_r2, weights_offset); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 105 | |
| 106 | for(ih = 0, oh = 0; oh < output_h; ++oh, ih += conv_stride_y) |
| 107 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 108 | auto in_top = reinterpret_cast<const T1 *>(input_ptr + (ih + 0) * input_stride_y); |
| 109 | auto in_mid = reinterpret_cast<const T1 *>(input_ptr + (ih + 1) * input_stride_y); |
| 110 | auto in_low = reinterpret_cast<const T1 *>(input_ptr + (ih + 2) * input_stride_y); |
| 111 | auto p_out = reinterpret_cast<T2 *>(out.ptr() + oh * output_stride_y); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 112 | |
| 113 | for(int ow = 0; ow < output_w; ow += num_elems_written_per_iteration, |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 114 | in_top += delta_input, in_mid += delta_input, in_low += delta_input, |
| 115 | p_out += num_elems_written_per_iteration) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 116 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 117 | auto vres = convolve_3x3<stridex>(in_top, in_mid, in_low, vw_r0, vw_r1, vw_r2, input_offset); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 118 | store_results<stridex>(p_out, vres); |
| 119 | } |
| 120 | } |
| 121 | }, |
| 122 | in, out); |
| 123 | } |
| 124 | }; |
| 125 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 126 | template <typename T1, typename T2> |
| 127 | inline void convolve_3x3(const Window &window, unsigned int num_elems_written_per_iteration, |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 128 | const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 129 | { |
| 130 | const unsigned int conv_stride_x = std::get<0>(conv_info.stride()); |
| 131 | switch(conv_stride_x) |
| 132 | { |
| 133 | case 1: |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 134 | convolver_3x3<T1, T2, 1>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info, depth_multiplier); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 135 | break; |
| 136 | case 2: |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 137 | convolver_3x3<T1, T2, 2>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info, depth_multiplier); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 138 | break; |
| 139 | case 3: |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 140 | convolver_3x3<T1, T2, 3>::convolve(window, num_elems_written_per_iteration, input, weights, output, conv_info, depth_multiplier); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 141 | break; |
| 142 | default: |
| 143 | ARM_COMPUTE_ERROR("Not implemented"); |
| 144 | } |
| 145 | } |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 146 | |
| 147 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, bool is_optimized) |
| 148 | { |
| 149 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32); |
| 150 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 151 | |
Giorgio Arena | 66cbafb | 2018-08-23 14:51:00 +0100 | [diff] [blame] | 152 | const DataLayout data_layout = input->data_layout(); |
| 153 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 154 | const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 155 | |
| 156 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) != 3 || weights->dimension(height_idx) != 3); |
| 157 | |
| 158 | if(!is_optimized) |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 159 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 160 | ARM_COMPUTE_RETURN_ERROR_ON(conv_info.stride().first < 1 || conv_info.stride().first > 3); |
| 161 | } |
| 162 | |
| 163 | if(output->total_size() != 0) |
| 164 | { |
| 165 | const TensorShape output_shape = compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier); |
| 166 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 167 | |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 168 | //ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input->data_type()) && (output->data_type() != DataType::S32)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 169 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_float(input->data_type()) && (output->data_type() != DataType::F32)); |
| 170 | } |
| 171 | |
| 172 | return Status{}; |
| 173 | } |
| 174 | |
| 175 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, bool is_optimized, |
| 176 | IDepthwiseConvolution *convolver = nullptr) |
| 177 | { |
| 178 | Window win; |
| 179 | bool window_changed = false; |
| 180 | |
| 181 | if(is_optimized) |
| 182 | { |
| 183 | if(convolver != nullptr) |
| 184 | { |
| 185 | auto win_last = convolver->get_window(); |
| 186 | win.set(Window::DimX, Window::Dimension(0, win_last, 1)); |
| 187 | |
| 188 | // Auto-configure output |
| 189 | bool same_padding = conv_info.has_padding(); |
| 190 | TensorShape output_shape{ input->tensor_shape() }; |
| 191 | |
| 192 | output_shape.set(1, convolver->output_size(output_shape.y(), same_padding)); // Set width |
| 193 | output_shape.set(2, convolver->output_size(output_shape.z(), same_padding)); // Set height |
| 194 | |
| 195 | // Output auto inizialitation if not yet initialized |
| 196 | auto_init_if_empty(*output, input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); |
| 197 | |
| 198 | // Configure window (optimised) |
| 199 | // Set padding in channels |
| 200 | const int num_channels = weights->dimension(0); |
| 201 | if((num_channels >= 128) && (num_channels % 16 == 0)) |
| 202 | { |
| 203 | input->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 204 | weights->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 205 | output->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | // Get convolved dimensions |
| 212 | const TensorShape output_shape = compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier); |
| 213 | const DataType output_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type(); |
| 214 | |
| 215 | // Output auto inizialitation if not yet initialized |
| 216 | auto_init_if_empty(*output, input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_type(output_dt)); |
| 217 | |
| 218 | // Configure kernel window (generic) |
| 219 | const unsigned int conv_stride_x = conv_info.stride().first; |
| 220 | const unsigned int conv_stride_y = conv_info.stride().second; |
| 221 | const unsigned int conv_pad_top = conv_info.pad_top(); |
| 222 | const unsigned int conv_pad_left = conv_info.pad_left(); |
| 223 | |
| 224 | unsigned int num_elems_written_per_iteration = 16 >> conv_stride_x; |
| 225 | unsigned int num_elems_read_per_iteration = 0; |
| 226 | |
| 227 | switch(input->data_type()) |
| 228 | { |
| 229 | case DataType::QASYMM8: |
| 230 | num_elems_read_per_iteration = 16; |
| 231 | break; |
| 232 | case DataType::F32: |
| 233 | num_elems_read_per_iteration = 12; |
| 234 | break; |
| 235 | default: |
| 236 | ARM_COMPUTE_ERROR("Data type not supported."); |
| 237 | } |
| 238 | |
| 239 | // Configure kernel window |
| 240 | win = calculate_max_window(*output, Steps(num_elems_written_per_iteration)); |
| 241 | |
| 242 | AccessWindowRectangle input_access(input, -conv_pad_left, -conv_pad_top, num_elems_read_per_iteration, 3, conv_stride_x, conv_stride_y); |
| 243 | AccessWindowStatic weights_access(weights, 0, 0, 3, 3); |
| 244 | AccessWindowHorizontal output_access(output, 0, num_elems_written_per_iteration); |
| 245 | |
| 246 | window_changed = update_window_and_padding(win, input_access, weights_access, output_access); |
| 247 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 248 | } |
| 249 | |
| 250 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 251 | return std::make_pair(err, win); |
| 252 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 253 | } // namespace |
| 254 | |
| 255 | NEDepthwiseConvolutionLayer3x3Kernel::NEDepthwiseConvolutionLayer3x3Kernel() |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 256 | : _border_size(0), _input(), _output(), _weights(), _conv_info(), _convolver(nullptr), _num_elems_written_per_iteration(0), _run_optimized(false), _depth_multiplier(1) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 257 | { |
| 258 | } |
| 259 | |
| 260 | BorderSize NEDepthwiseConvolutionLayer3x3Kernel::border_size() const |
| 261 | { |
| 262 | return _border_size; |
| 263 | } |
| 264 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 265 | void NEDepthwiseConvolutionLayer3x3Kernel::configure(const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, |
| 266 | DataLayout data_layout) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 267 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 268 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 269 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 270 | _input = input; |
| 271 | _output = output; |
| 272 | _weights = weights; |
| 273 | _conv_info = conv_info; |
| 274 | _depth_multiplier = depth_multiplier; |
| 275 | _convolver = nullptr; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 276 | |
| 277 | _run_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(), |
| 278 | conv_info, |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 279 | input->info()->data_type(), depth_multiplier, |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 280 | data_layout); |
| 281 | |
| 282 | (_run_optimized) ? configure_optimized() : configure_generic(); |
| 283 | } |
| 284 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 285 | Status NEDepthwiseConvolutionLayer3x3Kernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier) |
| 286 | { |
| 287 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 288 | |
| 289 | bool is_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->tensor_shape(), conv_info, input->data_type(), depth_multiplier, input->data_layout()); |
| 290 | |
| 291 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, output, conv_info, depth_multiplier, is_optimized)); |
| 292 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(), output->clone().get(), conv_info, depth_multiplier, is_optimized).first); |
| 293 | return Status{}; |
| 294 | } |
| 295 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 296 | void NEDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, const ThreadInfo &info) |
| 297 | { |
| 298 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 299 | ARM_COMPUTE_UNUSED(info); |
| 300 | |
| 301 | (_run_optimized) ? run_optimized(window, info) : run_generic(window, info); |
| 302 | } |
| 303 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 304 | bool NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(TensorShape input_shape, PadStrideInfo conv_info, DataType dt, unsigned int depth_multiplier, DataLayout data_layout) |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 305 | { |
| 306 | // Reshape input shape if in NHWC format |
| 307 | TensorShape in_shape{ input_shape }; |
| 308 | if(data_layout == DataLayout::NHWC) |
| 309 | { |
| 310 | in_shape.set(Window::DimX, input_shape.y()); |
| 311 | in_shape.set(Window::DimY, input_shape.z()); |
| 312 | in_shape.set(Window::DimZ, input_shape.x()); |
| 313 | } |
| 314 | |
| 315 | // Check supported data type |
| 316 | bool supported_datatype = (dt == DataType::F32); |
| 317 | |
| 318 | // Check for supported strides |
| 319 | const auto &strides = conv_info.stride(); |
| 320 | bool supported_strides = (strides.first == strides.second) && ((strides.first == 1) || (strides.first == 2)); |
| 321 | |
| 322 | // Check for supported padding |
| 323 | const auto pad_top = conv_info.pad_top(); |
| 324 | const auto pad_right = conv_info.pad_right(); |
| 325 | const auto pad_bottom = conv_info.pad_bottom(); |
| 326 | const auto pad_left = conv_info.pad_left(); |
| 327 | PadStrideInfo same_pad = calculate_same_pad(in_shape, TensorShape(3U, 3U), conv_info); |
| 328 | bool is_same_padding = (pad_top == same_pad.pad_top()) && (pad_right == same_pad.pad_right()) && (pad_bottom == same_pad.pad_bottom()) && (pad_left == same_pad.pad_left()); |
| 329 | bool is_valid_padding = (pad_top == 0) && (pad_right == 0) && (pad_bottom == 0) && (pad_left == 0); |
| 330 | bool supported_padding = is_same_padding || is_valid_padding; |
| 331 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 332 | return supported_datatype && supported_strides && supported_padding && (depth_multiplier == 1); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void NEDepthwiseConvolutionLayer3x3Kernel::generate_convolver() |
| 336 | { |
| 337 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(_input, 1, DataType::F32); |
| 338 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(_input, _weights); |
| 339 | ARM_COMPUTE_ERROR_ON(_weights->info()->dimension(1) != 3 || _weights->info()->dimension(2) != 3); |
| 340 | |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 341 | _convolver = create_convolver_object(_conv_info, _weights, _input, _output, true); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void NEDepthwiseConvolutionLayer3x3Kernel::configure_generic() |
| 345 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 346 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _weights->info(), _output->info(), _conv_info, _depth_multiplier, _run_optimized)); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 347 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 348 | _num_elems_written_per_iteration = 16 >> _conv_info.stride().first; |
| 349 | _border_size = BorderSize(_conv_info.pad_top(), _conv_info.pad_right(), _conv_info.pad_bottom(), _conv_info.pad_left()); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 350 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 351 | auto win_config = validate_and_configure_window(_input->info(), _weights->info(), _output->info(), _conv_info, _depth_multiplier, false); |
| 352 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 353 | INEKernel::configure(win_config.second); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 356 | void NEDepthwiseConvolutionLayer3x3Kernel::configure_optimized() |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 357 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 358 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(_input->info(), _weights->info(), _output->info(), _conv_info, _depth_multiplier, _run_optimized)); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 359 | |
| 360 | _border_size = BorderSize(0, 0); |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 361 | _convolver = create_convolver_object(_conv_info, _weights, _input, _output); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 362 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 363 | auto win_config = validate_and_configure_window(_input->info(), _weights->info(), _output->info(), _conv_info, _depth_multiplier, true, _convolver.get()); |
| 364 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 365 | INEKernel::configure(win_config.second); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void NEDepthwiseConvolutionLayer3x3Kernel::run_generic(const Window &window, const ThreadInfo &info) |
| 369 | { |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 370 | ARM_COMPUTE_UNUSED(info); |
| 371 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 372 | switch(_input->info()->data_type()) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 373 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 374 | case DataType::F32: |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 375 | convolve_3x3<float, float>(window, _num_elems_written_per_iteration, _input, _weights, _output, _conv_info, _depth_multiplier); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 376 | break; |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 377 | case DataType::QASYMM8: |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 378 | convolve_3x3<uint8_t, int32_t>(window, _num_elems_written_per_iteration, _input, _weights, _output, _conv_info, _depth_multiplier); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 379 | break; |
| 380 | default: |
| 381 | ARM_COMPUTE_ERROR("Not implemented"); |
| 382 | } |
| 383 | } |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 384 | |
| 385 | void NEDepthwiseConvolutionLayer3x3Kernel::run_optimized(const Window &window, const ThreadInfo &info) |
| 386 | { |
| 387 | ARM_COMPUTE_UNUSED(info); |
| 388 | ARM_COMPUTE_ERROR_ON(!_convolver); |
| 389 | |
| 390 | const size_t start = window.x().start(); |
| 391 | const size_t end = window.x().end(); |
| 392 | _convolver->run(start, end); |
| 393 | } |
| 394 | |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 395 | std::unique_ptr<depthwise::IDepthwiseConvolution> NEDepthwiseConvolutionLayer3x3Kernel::create_convolver_object(PadStrideInfo conv_info, |
| 396 | const ITensor *w, |
| 397 | const ITensor *in, |
| 398 | ITensor *out, |
| 399 | bool setup_strides) |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 400 | { |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 401 | const TensorShape shape = in->info()->tensor_shape(); |
| 402 | const int in_rows = shape.z(); |
| 403 | const int in_cols = shape.y(); |
| 404 | const int n_batches = shape[3]; |
| 405 | const int n_channels = shape.x(); |
| 406 | const bool padding_same = conv_info.has_padding(); |
| 407 | const int weight_col_stride = (setup_strides) ? w->info()->strides_in_bytes().y() / w->info()->element_size() : 0; |
| 408 | const int weight_row_stride = (setup_strides) ? w->info()->strides_in_bytes().z() / w->info()->element_size() : 0; |
| 409 | const int input_col_stride = (setup_strides) ? in->info()->strides_in_bytes().y() / in->info()->element_size() : 0; |
| 410 | const int input_row_stride = (setup_strides) ? in->info()->strides_in_bytes().z() / in->info()->element_size() : 0; |
| 411 | const int input_batch_stride = (setup_strides) ? in->info()->strides_in_bytes()[3] / in->info()->element_size() : 0; |
| 412 | const int output_col_stride = (setup_strides) ? out->info()->strides_in_bytes().y() / out->info()->element_size() : 0; |
| 413 | const int output_row_stride = (setup_strides) ? out->info()->strides_in_bytes().z() / out->info()->element_size() : 0; |
| 414 | const int output_batch_stride = (setup_strides) ? out->info()->strides_in_bytes()[3] / out->info()->element_size() : 0; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 415 | |
| 416 | const auto stride_x = conv_info.stride().first; |
| 417 | switch(stride_x) |
| 418 | { |
| 419 | case 1: |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 420 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<4, 4, 3, 3, 1, 1, float, float>>( |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 421 | n_batches, |
| 422 | in_rows, |
| 423 | in_cols, |
| 424 | n_channels, |
| 425 | padding_same, |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 426 | reinterpret_cast<const float *>(w->ptr_to_element(Coordinates())), |
| 427 | reinterpret_cast<float *>(in->ptr_to_element(Coordinates())), |
| 428 | reinterpret_cast<float *>(out->ptr_to_element(Coordinates())), |
| 429 | weight_col_stride, weight_row_stride, |
| 430 | input_col_stride, input_row_stride, input_batch_stride, |
| 431 | output_col_stride, output_row_stride, output_batch_stride); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 432 | case 2: |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 433 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<3, 3, 3, 3, 2, 2, float, float>>( |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 434 | n_batches, |
| 435 | in_rows, |
| 436 | in_cols, |
| 437 | n_channels, |
| 438 | padding_same, |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 439 | reinterpret_cast<const float *>(w->ptr_to_element(Coordinates())), |
| 440 | reinterpret_cast<float *>(in->ptr_to_element(Coordinates())), |
| 441 | reinterpret_cast<float *>(out->ptr_to_element(Coordinates())), |
| 442 | weight_col_stride, weight_row_stride, |
| 443 | input_col_stride, input_row_stride, input_batch_stride, |
| 444 | output_col_stride, output_row_stride, output_batch_stride); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 445 | default: |
| 446 | return nullptr; |
| 447 | } |
| 448 | } |