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 | { |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 149 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 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 | |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 168 | if(is_data_type_quantized_asymmetric(input->data_type())) |
| 169 | { |
| 170 | ARM_COMPUTE_RETURN_ERROR_ON(output->data_type() != DataType::S32); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 175 | } |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return Status{}; |
| 179 | } |
| 180 | |
| 181 | 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, |
| 182 | IDepthwiseConvolution *convolver = nullptr) |
| 183 | { |
| 184 | Window win; |
| 185 | bool window_changed = false; |
| 186 | |
| 187 | if(is_optimized) |
| 188 | { |
| 189 | if(convolver != nullptr) |
| 190 | { |
| 191 | auto win_last = convolver->get_window(); |
| 192 | win.set(Window::DimX, Window::Dimension(0, win_last, 1)); |
| 193 | |
| 194 | // Auto-configure output |
| 195 | bool same_padding = conv_info.has_padding(); |
| 196 | TensorShape output_shape{ input->tensor_shape() }; |
| 197 | |
| 198 | output_shape.set(1, convolver->output_size(output_shape.y(), same_padding)); // Set width |
| 199 | output_shape.set(2, convolver->output_size(output_shape.z(), same_padding)); // Set height |
| 200 | |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 201 | const DataType output_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type(); |
| 202 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 203 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 204 | auto_init_if_empty(*output, input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_type(output_dt)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 205 | |
| 206 | // Configure window (optimised) |
| 207 | // Set padding in channels |
| 208 | const int num_channels = weights->dimension(0); |
| 209 | if((num_channels >= 128) && (num_channels % 16 == 0)) |
| 210 | { |
| 211 | input->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 212 | weights->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 213 | output->extend_padding(PaddingSize(0, 4, 0, 0)); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | // Get convolved dimensions |
| 220 | const TensorShape output_shape = compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier); |
| 221 | const DataType output_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type(); |
| 222 | |
| 223 | // Output auto inizialitation if not yet initialized |
| 224 | auto_init_if_empty(*output, input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_type(output_dt)); |
| 225 | |
| 226 | // Configure kernel window (generic) |
| 227 | const unsigned int conv_stride_x = conv_info.stride().first; |
| 228 | const unsigned int conv_stride_y = conv_info.stride().second; |
| 229 | const unsigned int conv_pad_top = conv_info.pad_top(); |
| 230 | const unsigned int conv_pad_left = conv_info.pad_left(); |
| 231 | |
| 232 | unsigned int num_elems_written_per_iteration = 16 >> conv_stride_x; |
| 233 | unsigned int num_elems_read_per_iteration = 0; |
| 234 | |
| 235 | switch(input->data_type()) |
| 236 | { |
| 237 | case DataType::QASYMM8: |
| 238 | num_elems_read_per_iteration = 16; |
| 239 | break; |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 240 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 241 | case DataType::F16: |
| 242 | num_elems_read_per_iteration = 24; |
| 243 | break; |
| 244 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 245 | case DataType::F32: |
| 246 | num_elems_read_per_iteration = 12; |
| 247 | break; |
| 248 | default: |
| 249 | ARM_COMPUTE_ERROR("Data type not supported."); |
| 250 | } |
| 251 | |
| 252 | // Configure kernel window |
| 253 | win = calculate_max_window(*output, Steps(num_elems_written_per_iteration)); |
| 254 | |
| 255 | AccessWindowRectangle input_access(input, -conv_pad_left, -conv_pad_top, num_elems_read_per_iteration, 3, conv_stride_x, conv_stride_y); |
| 256 | AccessWindowStatic weights_access(weights, 0, 0, 3, 3); |
| 257 | AccessWindowHorizontal output_access(output, 0, num_elems_written_per_iteration); |
| 258 | |
| 259 | window_changed = update_window_and_padding(win, input_access, weights_access, output_access); |
| 260 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 261 | } |
| 262 | |
| 263 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 264 | return std::make_pair(err, win); |
| 265 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 266 | } // namespace |
| 267 | |
| 268 | NEDepthwiseConvolutionLayer3x3Kernel::NEDepthwiseConvolutionLayer3x3Kernel() |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 269 | : _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] | 270 | { |
| 271 | } |
| 272 | |
| 273 | BorderSize NEDepthwiseConvolutionLayer3x3Kernel::border_size() const |
| 274 | { |
| 275 | return _border_size; |
| 276 | } |
| 277 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 278 | void NEDepthwiseConvolutionLayer3x3Kernel::configure(const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, |
| 279 | DataLayout data_layout) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 280 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 281 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 282 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 283 | _input = input; |
| 284 | _output = output; |
| 285 | _weights = weights; |
| 286 | _conv_info = conv_info; |
| 287 | _depth_multiplier = depth_multiplier; |
| 288 | _convolver = nullptr; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 289 | |
| 290 | _run_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(), |
| 291 | conv_info, |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 292 | input->info()->data_type(), depth_multiplier, |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 293 | data_layout); |
| 294 | |
| 295 | (_run_optimized) ? configure_optimized() : configure_generic(); |
| 296 | } |
| 297 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 298 | Status NEDepthwiseConvolutionLayer3x3Kernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier) |
| 299 | { |
| 300 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 301 | |
| 302 | bool is_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->tensor_shape(), conv_info, input->data_type(), depth_multiplier, input->data_layout()); |
| 303 | |
| 304 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, output, conv_info, depth_multiplier, is_optimized)); |
| 305 | 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); |
| 306 | return Status{}; |
| 307 | } |
| 308 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 309 | void NEDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, const ThreadInfo &info) |
| 310 | { |
| 311 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 312 | ARM_COMPUTE_UNUSED(info); |
| 313 | |
| 314 | (_run_optimized) ? run_optimized(window, info) : run_generic(window, info); |
| 315 | } |
| 316 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 317 | 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] | 318 | { |
| 319 | // Reshape input shape if in NHWC format |
| 320 | TensorShape in_shape{ input_shape }; |
| 321 | if(data_layout == DataLayout::NHWC) |
| 322 | { |
| 323 | in_shape.set(Window::DimX, input_shape.y()); |
| 324 | in_shape.set(Window::DimY, input_shape.z()); |
| 325 | in_shape.set(Window::DimZ, input_shape.x()); |
| 326 | } |
| 327 | |
| 328 | // Check supported data type |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 329 | bool supported_datatype = is_data_type_float(dt) || is_data_type_quantized(dt); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 330 | |
| 331 | // Check for supported strides |
| 332 | const auto &strides = conv_info.stride(); |
| 333 | bool supported_strides = (strides.first == strides.second) && ((strides.first == 1) || (strides.first == 2)); |
| 334 | |
| 335 | // Check for supported padding |
| 336 | const auto pad_top = conv_info.pad_top(); |
| 337 | const auto pad_right = conv_info.pad_right(); |
| 338 | const auto pad_bottom = conv_info.pad_bottom(); |
| 339 | const auto pad_left = conv_info.pad_left(); |
| 340 | PadStrideInfo same_pad = calculate_same_pad(in_shape, TensorShape(3U, 3U), conv_info); |
| 341 | 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()); |
| 342 | bool is_valid_padding = (pad_top == 0) && (pad_right == 0) && (pad_bottom == 0) && (pad_left == 0); |
| 343 | bool supported_padding = is_same_padding || is_valid_padding; |
| 344 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 345 | return supported_datatype && supported_strides && supported_padding && (depth_multiplier == 1); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void NEDepthwiseConvolutionLayer3x3Kernel::generate_convolver() |
| 349 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 350 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(_input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 351 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(_input, _weights); |
| 352 | ARM_COMPUTE_ERROR_ON(_weights->info()->dimension(1) != 3 || _weights->info()->dimension(2) != 3); |
| 353 | |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 354 | _convolver = create_convolver_object(_conv_info, _weights, _input, _output, true); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 355 | if(_convolver) |
| 356 | { |
| 357 | _convolver->set_offsets(-_input->info()->quantization_info().offset, -_weights->info()->quantization_info().offset); |
| 358 | } |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void NEDepthwiseConvolutionLayer3x3Kernel::configure_generic() |
| 362 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 363 | 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] | 364 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 365 | _num_elems_written_per_iteration = 16 >> _conv_info.stride().first; |
| 366 | _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] | 367 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 368 | auto win_config = validate_and_configure_window(_input->info(), _weights->info(), _output->info(), _conv_info, _depth_multiplier, false); |
| 369 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 370 | INEKernel::configure(win_config.second); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 373 | void NEDepthwiseConvolutionLayer3x3Kernel::configure_optimized() |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 374 | { |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 375 | 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] | 376 | |
| 377 | _border_size = BorderSize(0, 0); |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 378 | _convolver = create_convolver_object(_conv_info, _weights, _input, _output); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 379 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 380 | auto win_config = validate_and_configure_window(_input->info(), _weights->info(), _output->info(), _conv_info, _depth_multiplier, true, _convolver.get()); |
| 381 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 382 | INEKernel::configure(win_config.second); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void NEDepthwiseConvolutionLayer3x3Kernel::run_generic(const Window &window, const ThreadInfo &info) |
| 386 | { |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 387 | ARM_COMPUTE_UNUSED(info); |
| 388 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 389 | switch(_input->info()->data_type()) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 390 | { |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 391 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 392 | case DataType::F16: |
| 393 | convolve_3x3<float16_t, float16_t>(window, _num_elems_written_per_iteration, _input, _weights, _output, _conv_info, _depth_multiplier); |
| 394 | break; |
| 395 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 396 | case DataType::F32: |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 397 | 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] | 398 | break; |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 399 | case DataType::QASYMM8: |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 400 | 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] | 401 | break; |
| 402 | default: |
| 403 | ARM_COMPUTE_ERROR("Not implemented"); |
| 404 | } |
| 405 | } |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 406 | |
| 407 | void NEDepthwiseConvolutionLayer3x3Kernel::run_optimized(const Window &window, const ThreadInfo &info) |
| 408 | { |
| 409 | ARM_COMPUTE_UNUSED(info); |
| 410 | ARM_COMPUTE_ERROR_ON(!_convolver); |
| 411 | |
| 412 | const size_t start = window.x().start(); |
| 413 | const size_t end = window.x().end(); |
| 414 | _convolver->run(start, end); |
| 415 | } |
| 416 | |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 417 | std::unique_ptr<depthwise::IDepthwiseConvolution> NEDepthwiseConvolutionLayer3x3Kernel::create_convolver_object(PadStrideInfo conv_info, |
| 418 | const ITensor *w, |
| 419 | const ITensor *in, |
| 420 | ITensor *out, |
| 421 | bool setup_strides) |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 422 | { |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 423 | const DataType dt = in->info()->data_type(); |
Georgios Pinitas | be0ae93 | 2018-03-13 13:08:12 +0000 | [diff] [blame] | 424 | const TensorShape shape = in->info()->tensor_shape(); |
| 425 | const int in_rows = shape.z(); |
| 426 | const int in_cols = shape.y(); |
| 427 | const int n_batches = shape[3]; |
| 428 | const int n_channels = shape.x(); |
| 429 | const bool padding_same = conv_info.has_padding(); |
| 430 | const int weight_col_stride = (setup_strides) ? w->info()->strides_in_bytes().y() / w->info()->element_size() : 0; |
| 431 | const int weight_row_stride = (setup_strides) ? w->info()->strides_in_bytes().z() / w->info()->element_size() : 0; |
| 432 | const int input_col_stride = (setup_strides) ? in->info()->strides_in_bytes().y() / in->info()->element_size() : 0; |
| 433 | const int input_row_stride = (setup_strides) ? in->info()->strides_in_bytes().z() / in->info()->element_size() : 0; |
| 434 | const int input_batch_stride = (setup_strides) ? in->info()->strides_in_bytes()[3] / in->info()->element_size() : 0; |
| 435 | const int output_col_stride = (setup_strides) ? out->info()->strides_in_bytes().y() / out->info()->element_size() : 0; |
| 436 | const int output_row_stride = (setup_strides) ? out->info()->strides_in_bytes().z() / out->info()->element_size() : 0; |
| 437 | 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] | 438 | |
| 439 | const auto stride_x = conv_info.stride().first; |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 440 | switch(dt) |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 441 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 442 | case DataType::QASYMM8: |
| 443 | { |
| 444 | switch(stride_x) |
| 445 | { |
| 446 | case 1: |
| 447 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<4, 4, 3, 3, 1, 1, uint8_t, int32_t>>( |
| 448 | n_batches, in_rows, in_cols, n_channels, padding_same, |
| 449 | reinterpret_cast<const uint8_t *>(w->ptr_to_element(Coordinates())), |
| 450 | in->ptr_to_element(Coordinates()), |
| 451 | reinterpret_cast<int32_t *>(out->ptr_to_element(Coordinates())), weight_col_stride, |
| 452 | weight_row_stride, input_col_stride, input_row_stride, input_batch_stride, |
| 453 | output_col_stride, output_row_stride, output_batch_stride); |
| 454 | case 2: |
| 455 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<4, 4, 3, 3, 2, 2, uint8_t, int32_t>>( |
| 456 | n_batches, in_rows, in_cols, n_channels, padding_same, |
| 457 | reinterpret_cast<const uint8_t *>(w->ptr_to_element(Coordinates())), |
| 458 | in->ptr_to_element(Coordinates()), |
| 459 | reinterpret_cast<int32_t *>(out->ptr_to_element(Coordinates())), weight_col_stride, |
| 460 | weight_row_stride, input_col_stride, input_row_stride, input_batch_stride, |
| 461 | output_col_stride, output_row_stride, output_batch_stride); |
| 462 | default: |
| 463 | return nullptr; |
| 464 | } |
| 465 | break; |
| 466 | } |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 467 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 468 | case DataType::F16: |
| 469 | { |
| 470 | switch(stride_x) |
| 471 | { |
| 472 | case 1: |
| 473 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<4, 4, 3, 3, 1, 1, float16_t, float16_t>>( |
| 474 | n_batches, in_rows, in_cols, n_channels, padding_same, |
| 475 | reinterpret_cast<const float16_t *>(w->ptr_to_element(Coordinates())), |
| 476 | reinterpret_cast<float16_t *>(in->ptr_to_element(Coordinates())), |
| 477 | reinterpret_cast<float16_t *>(out->ptr_to_element(Coordinates())), weight_col_stride, |
| 478 | weight_row_stride, input_col_stride, input_row_stride, input_batch_stride, |
| 479 | output_col_stride, output_row_stride, output_batch_stride); |
| 480 | case 2: |
| 481 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<4, 4, 3, 3, 2, 2, float16_t, float16_t>>( |
| 482 | n_batches, in_rows, in_cols, n_channels, padding_same, |
| 483 | reinterpret_cast<const float16_t *>(w->ptr_to_element(Coordinates())), |
| 484 | reinterpret_cast<float16_t *>(in->ptr_to_element(Coordinates())), |
| 485 | reinterpret_cast<float16_t *>(out->ptr_to_element(Coordinates())), weight_col_stride, |
| 486 | weight_row_stride, input_col_stride, input_row_stride, input_batch_stride, |
| 487 | output_col_stride, output_row_stride, output_batch_stride); |
| 488 | default: |
| 489 | return nullptr; |
| 490 | } |
| 491 | break; |
| 492 | } |
| 493 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 494 | case DataType::F32: |
| 495 | { |
| 496 | switch(stride_x) |
| 497 | { |
| 498 | case 1: |
| 499 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<4, 4, 3, 3, 1, 1, float, float>>( |
| 500 | n_batches, in_rows, in_cols, n_channels, padding_same, |
| 501 | reinterpret_cast<const float *>(w->ptr_to_element(Coordinates())), |
| 502 | reinterpret_cast<float *>(in->ptr_to_element(Coordinates())), |
| 503 | reinterpret_cast<float *>(out->ptr_to_element(Coordinates())), weight_col_stride, |
| 504 | weight_row_stride, input_col_stride, input_row_stride, input_batch_stride, |
| 505 | output_col_stride, output_row_stride, output_batch_stride); |
| 506 | case 2: |
| 507 | return arm_compute::support::cpp14::make_unique<DepthwiseConvolution<3, 3, 3, 3, 2, 2, float, float>>( |
| 508 | n_batches, in_rows, in_cols, n_channels, padding_same, |
| 509 | reinterpret_cast<const float *>(w->ptr_to_element(Coordinates())), |
| 510 | reinterpret_cast<float *>(in->ptr_to_element(Coordinates())), |
| 511 | reinterpret_cast<float *>(out->ptr_to_element(Coordinates())), weight_col_stride, |
| 512 | weight_row_stride, input_col_stride, input_row_stride, input_batch_stride, |
| 513 | output_col_stride, output_row_stride, output_batch_stride); |
| 514 | default: |
| 515 | return nullptr; |
| 516 | } |
| 517 | break; |
| 518 | } |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 519 | default: |
| 520 | return nullptr; |
| 521 | } |
| 522 | } |