Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +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/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/PixelValue.h" |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 32 | #include "support/ToolchainSupport.h" |
| 33 | |
| 34 | using namespace arm_compute; |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 35 | using namespace arm_compute::misc; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 36 | using namespace arm_compute::misc::shape_calculator; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 37 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 38 | NEDepthwiseConvolutionLayer3x3::NEDepthwiseConvolutionLayer3x3() |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 39 | : _dwc_kernel(), _output_stage_kernel(), _border_handler(), _permute_input(), _permute_weights(), _permute_output(), _accumulator(), _input_nhwc(), _weights_hwio(), _output_nhwc(), _has_bias(false), |
| 40 | _is_quantized(false), _is_optimized(false), _are_weights_reshaped(false) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 44 | void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 45 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32); |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
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 | PixelValue zero_value(0.f); |
| 50 | |
| 51 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 52 | _has_bias = biases != nullptr; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 53 | _is_optimized = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input->info()->tensor_shape(), |
| 54 | conv_info, |
| 55 | input->info()->data_type()); |
| 56 | _are_weights_reshaped = false; |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 57 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 58 | if(_is_optimized) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 59 | { |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 60 | // Configure the function to transform the input tensor from NCHW -> NHWC |
| 61 | _permute_input.configure(input, &_input_nhwc, PermutationVector(2U, 0U, 1U)); |
| 62 | |
| 63 | // Configure the function to transform the weights tensor from IHW -> HWI |
| 64 | _permute_weights.configure(weights, &_weights_hwio, PermutationVector(2U, 0U, 1U)); |
| 65 | |
| 66 | // Configure optimized depthwise |
| 67 | _dwc_kernel.configure(&_input_nhwc, &_weights_hwio, &_output_nhwc, conv_info, DataLayout::NHWC); |
| 68 | |
| 69 | // Configure the function to transform the convoluted output to ACL's native ordering format NCHW |
| 70 | _permute_output.configure(&_output_nhwc, output, PermutationVector(1U, 2U, 0U)); |
| 71 | |
| 72 | // Allocate tensors |
| 73 | _input_nhwc.allocator()->allocate(); |
| 74 | _weights_hwio.allocator()->allocate(); |
| 75 | _output_nhwc.allocator()->allocate(); |
| 76 | |
| 77 | // Create convolver (deferred) |
| 78 | _dwc_kernel.generate_convolver(); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 79 | } |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 80 | else |
| 81 | { |
| 82 | // Allocate the intermediate accumulator tensor in case of fixed point input |
| 83 | if(_is_quantized) |
| 84 | { |
| 85 | _accumulator.allocator()->init(TensorInfo(output->info()->tensor_shape(), 1, DataType::S32)); |
| 86 | _accumulator.info()->set_quantization_info(input->info()->quantization_info()); |
| 87 | zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset)); |
| 88 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 89 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 90 | // Configure depthwise convolution kernel |
| 91 | _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 92 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 93 | // Configure border handler |
| 94 | _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value); |
| 95 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 96 | |
| 97 | // Configure biases accumulation |
| 98 | if(_has_bias || _is_quantized) |
| 99 | { |
| 100 | if(_is_quantized) |
| 101 | { |
| 102 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale; |
| 103 | int output_multiplier, output_shift; |
| 104 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 105 | _output_stage_kernel.configure(&_accumulator, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset); |
| 106 | _accumulator.allocator()->allocate(); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | _output_stage_kernel.configure(output, biases); |
| 111 | } |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 115 | void NEDepthwiseConvolutionLayer3x3::run() |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 116 | { |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 117 | // Permute weights in HWIO format if the optimized kernel will be executedd |
| 118 | if(!_are_weights_reshaped && _is_optimized) |
| 119 | { |
| 120 | _are_weights_reshaped = true; |
| 121 | _permute_weights.run(); |
| 122 | } |
| 123 | |
| 124 | // Handle input |
| 125 | if(_is_optimized) |
| 126 | { |
| 127 | // Permute input to NHWC format execution |
| 128 | _permute_input.run(); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | // Fill border in NCHW format execution |
| 133 | NEScheduler::get().schedule(&_border_handler, Window::DimX); |
| 134 | } |
| 135 | |
| 136 | // Execute depthwise convolution |
| 137 | NEScheduler::get().schedule(&_dwc_kernel, Window::DimX); |
| 138 | |
| 139 | // Permute output to ACL's native NCHW format in case of NHWC execution |
| 140 | if(_is_optimized) |
| 141 | { |
| 142 | _permute_output.run(); |
| 143 | } |
| 144 | |
| 145 | // Add biases |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 146 | if(_has_bias || _is_quantized) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 147 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 148 | NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 149 | } |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 152 | NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer() |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 153 | : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _input_reshaped(), |
| 154 | _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_quantized(false) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 155 | { |
| 156 | } |
| 157 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 158 | void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 159 | { |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 160 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 161 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 162 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != weights->info()->dimension(2)); |
| 163 | |
| 164 | const size_t weights_w = weights->info()->dimension(0); |
| 165 | const size_t weights_h = weights->info()->dimension(1); |
| 166 | const size_t weights_z = weights->info()->dimension(2); |
| 167 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 168 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 169 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 170 | // Should bias be appended ? |
| 171 | bool append_bias = (biases != nullptr) && !_is_quantized; |
| 172 | |
| 173 | // Calculate output shape |
| 174 | TensorShape dwc_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info); |
| 175 | |
| 176 | // Output width and height |
| 177 | const unsigned int conv_w = dwc_output_shape.x(); |
| 178 | const unsigned int conv_h = dwc_output_shape.y(); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 179 | |
| 180 | // Set up intermediate tensors |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 181 | const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 182 | const size_t conv_size = conv_w * conv_h; |
| 183 | |
| 184 | // Im2Col configuration |
| 185 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 186 | shape_im2col.set(0, patch_size); |
| 187 | shape_im2col.set(1, conv_size); |
| 188 | shape_im2col.set(2, weights_z); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 189 | _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
| 190 | _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 191 | |
| 192 | // Weights reshape configuration |
| 193 | const TensorShape shape_weights_reshape(patch_size, weights_z); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 194 | _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape)); |
| 195 | _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 196 | |
| 197 | // GEMV configuration |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 198 | DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type(); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 199 | TensorShape shape_v2mm_out = input->info()->tensor_shape(); |
| 200 | shape_v2mm_out.set(0, conv_size * weights_z); |
| 201 | shape_v2mm_out.set(1, 1); |
| 202 | shape_v2mm_out.set(2, 1); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 203 | _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out)); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 204 | _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 205 | _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(dwc_output_shape)); |
| 206 | _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h); |
| 207 | |
| 208 | // Output staged configuration |
| 209 | if(_is_quantized) |
| 210 | { |
| 211 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale; |
| 212 | int output_multiplier, output_shift; |
| 213 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 214 | _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset); |
| 215 | _output_reshaped.allocator()->allocate(); |
| 216 | } |
| 217 | |
| 218 | // Fill borders on inputs |
Anthony Barbier | fb8dda2 | 2018-01-30 09:27:05 +0000 | [diff] [blame] | 219 | PixelValue zero_in(static_cast<int32_t>(0)); |
| 220 | PixelValue zero_w(static_cast<int32_t>(0)); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 221 | if(_is_quantized) |
| 222 | { |
| 223 | zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset)); |
| 224 | zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset)); |
| 225 | } |
| 226 | BorderSize border_size = _v2mm_kernel.border_size(); |
| 227 | _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in); |
| 228 | |
| 229 | border_size.bottom = 0; |
| 230 | _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 231 | |
| 232 | // Allocate intermediate tensors |
| 233 | _input_reshaped.allocator()->allocate(); |
| 234 | _weights_reshaped.allocator()->allocate(); |
| 235 | _v2mm_output.allocator()->allocate(); |
| 236 | } |
| 237 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 238 | void NEDepthwiseConvolutionLayer::run() |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 239 | { |
| 240 | NEScheduler::get().schedule(&_im2col_kernel, Window::DimX); |
| 241 | NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 242 | NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX); |
| 243 | NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 244 | NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX); |
| 245 | NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 246 | if(_is_quantized) |
| 247 | { |
| 248 | NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX); |
| 249 | } |
Anthony Barbier | fb8dda2 | 2018-01-30 09:27:05 +0000 | [diff] [blame] | 250 | } |