Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 2481d46 | 2019-02-19 18:47:46 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/InfoHelpers.h" |
| 35 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 36 | using namespace arm_compute::misc; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 37 | using namespace arm_compute::misc::shape_calculator; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 38 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
| 41 | NEDepthwiseConvolutionLayer3x3::NEDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager) |
| 42 | : _memory_group(memory_manager), _dwc_kernel(), _dwc_optimized_func(memory_manager), _output_stage_kernel(), _border_handler(), _permute_input(), _permute_weights(), _permute_output(), |
| 43 | _activationlayer_function(), _accumulator(), _permuted_input(), _permuted_weights(), _permuted_output(), _original_weights(nullptr), _has_bias(false), _is_quantized(false), _is_optimized(false), |
| 44 | _is_nchw(true), _permute(false), _is_activationlayer_enabled(false), _is_prepared(false) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 48 | void NEDepthwiseConvolutionLayer3x3::configure_generic(ITensor *input, |
| 49 | const ITensor *weights, |
| 50 | const ITensor *biases, |
| 51 | ITensor *output, |
| 52 | const PadStrideInfo &conv_info, |
| 53 | unsigned int depth_multiplier, |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 54 | const ActivationLayerInfo &act_info, |
| 55 | const Size2D &dilation) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 56 | { |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_UNUSED(act_info); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 58 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 59 | PixelValue zero_value(0.f); |
| 60 | |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 61 | // Initialize the intermediate accumulator tensor in case of quantized input |
| 62 | if(_is_quantized) |
| 63 | { |
| 64 | TensorShape accum_shape = output->info()->tensor_shape(); |
| 65 | DataLayout accum_layout = output->info()->data_layout(); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 66 | if(!_is_nchw) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 67 | { |
| 68 | permute(accum_shape, PermutationVector(1U, 2U, 0U)); |
| 69 | accum_layout = DataLayout::NCHW; |
| 70 | } |
| 71 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 72 | _memory_group.manage(&_accumulator); |
Georgios Pinitas | 2481d46 | 2019-02-19 18:47:46 +0000 | [diff] [blame] | 73 | _accumulator.allocator()->init(TensorInfo(accum_shape, 1, DataType::S32, output->info()->quantization_info())); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 74 | _accumulator.info()->set_data_layout(accum_layout); |
| 75 | zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().offset)); |
| 76 | } |
| 77 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 78 | if(!_is_nchw) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 79 | { |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 80 | _memory_group.manage(&_permuted_input); |
| 81 | _memory_group.manage(&_permuted_output); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 82 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 83 | // Configure the function to transform the input tensor from NHWC -> NCHW |
| 84 | _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U)); |
| 85 | _permuted_input.info()->set_data_layout(DataLayout::NCHW); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 86 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 87 | // Configure the function to transform the weights tensor from HWI -> IHW |
| 88 | _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U)); |
| 89 | _permuted_weights.info()->set_data_layout(DataLayout::NCHW); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 90 | |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 91 | // Configure depthwise |
| 92 | _dwc_kernel.configure(&_permuted_input, &_permuted_weights, (_is_quantized) ? &_accumulator : &_permuted_output, conv_info, depth_multiplier, dilation); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 93 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 94 | // Configure border handler |
| 95 | _border_handler.configure(&_permuted_input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value); |
| 96 | |
| 97 | // Allocate tensors |
| 98 | _permuted_input.allocator()->allocate(); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 99 | } |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 100 | else |
| 101 | { |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 102 | // Configure depthwise convolution kernel |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 103 | _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info, depth_multiplier, dilation); |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 104 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 105 | // Configure border handler |
| 106 | _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 107 | } |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 108 | |
| 109 | // Configure biases accumulation |
| 110 | if(_is_quantized) |
| 111 | { |
| 112 | const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info(); |
| 113 | |
| 114 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale; |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame^] | 115 | int output_multiplier; |
| 116 | int output_shift; |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 117 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 118 | _output_stage_kernel.configure(&_accumulator, biases, _is_nchw ? output : &_permuted_output, output_multiplier, output_shift, output_quant_info.offset); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 119 | _accumulator.allocator()->allocate(); |
| 120 | } |
| 121 | else if(_has_bias) |
| 122 | { |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 123 | _output_stage_kernel.configure(_is_nchw ? output : &_permuted_output, biases); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 126 | // Permute output |
| 127 | if(!_is_nchw) |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 128 | { |
| 129 | // Configure the function to transform the convoluted output to NHWC |
| 130 | _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U)); |
| 131 | _permuted_output.allocator()->allocate(); |
| 132 | } |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 133 | } |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 134 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 135 | void NEDepthwiseConvolutionLayer3x3::configure_optimized(const ITensor *input, |
| 136 | const ITensor *weights, |
| 137 | const ITensor *biases, |
| 138 | ITensor *output, |
| 139 | const PadStrideInfo &conv_info, |
| 140 | unsigned int depth_multiplier, |
| 141 | const ActivationLayerInfo &act_info) |
| 142 | { |
| 143 | ActivationLayerInfo act_info_to_use = ActivationLayerInfo(); |
| 144 | const bool is_relu = arm_compute::utils::info_helpers::is_relu(act_info); |
| 145 | const bool is_relu6 = arm_compute::utils::info_helpers::is_relu6(act_info); |
| 146 | _is_activationlayer_enabled = act_info.enabled() && !(is_relu || is_relu6); |
| 147 | if(!_is_activationlayer_enabled) |
| 148 | { |
| 149 | act_info_to_use = act_info; |
| 150 | } |
| 151 | |
| 152 | if(_is_nchw) |
| 153 | { |
| 154 | _memory_group.manage(&_permuted_input); |
| 155 | _memory_group.manage(&_permuted_output); |
| 156 | |
| 157 | // Configure the function to transform the input tensor from NCHW -> NHWC |
| 158 | _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U)); |
| 159 | _permuted_input.info()->set_data_layout(DataLayout::NHWC); |
| 160 | |
| 161 | // Configure the function to transform the weights tensor from IHW -> HWI |
| 162 | _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U)); |
| 163 | _permuted_weights.info()->set_data_layout(DataLayout::NHWC); |
| 164 | |
| 165 | // Configure optimized depthwise |
| 166 | _dwc_optimized_func.configure(&_permuted_input, &_permuted_weights, biases, &_permuted_output, conv_info, depth_multiplier, act_info_to_use); |
| 167 | |
| 168 | // Configure the function to transform the convoluted output to ACL's native ordering format NCHW |
| 169 | _permuted_output.info()->set_data_layout(DataLayout::NHWC); |
| 170 | _permute_output.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U)); |
| 171 | |
| 172 | // Allocate tensors |
| 173 | _permuted_input.allocator()->allocate(); |
| 174 | _permuted_output.allocator()->allocate(); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | _dwc_optimized_func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info_to_use); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void NEDepthwiseConvolutionLayer3x3::configure(ITensor *input, |
| 183 | const ITensor *weights, |
| 184 | const ITensor *biases, |
| 185 | ITensor *output, const PadStrideInfo &conv_info, |
| 186 | unsigned int depth_multiplier, |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 187 | const ActivationLayerInfo &act_info, |
| 188 | const Size2D &dilation) |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 189 | { |
| 190 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 191 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 192 | |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 193 | // idx_w and idx_h only used for validation |
| 194 | const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 195 | const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
| 196 | ARM_COMPUTE_UNUSED(idx_w); |
| 197 | ARM_COMPUTE_UNUSED(idx_h); |
| 198 | |
| 199 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_w) + (weights->info()->dimension(idx_w) - 1) * (dilation.x() - 1) > input->info()->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right()); |
| 200 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_h) + (weights->info()->dimension(idx_h) - 1) * (dilation.y() - 1) > input->info()->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom()); |
| 201 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 202 | _original_weights = weights; |
| 203 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 204 | _has_bias = biases != nullptr; |
| 205 | _is_optimized = NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input->info(), |
| 206 | weights->info(), |
| 207 | conv_info, |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 208 | depth_multiplier, dilation); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 209 | _is_nchw = input->info()->data_layout() == DataLayout::NCHW; |
| 210 | _permute = _is_optimized == _is_nchw; |
| 211 | _is_prepared = false; |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 212 | _is_activationlayer_enabled = act_info.enabled(); |
| 213 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 214 | // Configure appropriate pipeline |
| 215 | if(_is_optimized) |
| 216 | { |
| 217 | configure_optimized(input, weights, biases, output, conv_info, depth_multiplier, act_info); |
| 218 | } |
| 219 | else |
| 220 | { |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 221 | configure_generic(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // Configure activation |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 225 | if(_is_activationlayer_enabled) |
| 226 | { |
| 227 | _activationlayer_function.configure(output, nullptr, act_info); |
| 228 | } |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 231 | Status NEDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, |
| 232 | const ITensorInfo *weights, |
| 233 | const ITensorInfo *biases, |
| 234 | const ITensorInfo *output, |
| 235 | const PadStrideInfo &conv_info, |
| 236 | unsigned int depth_multiplier, |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 237 | const ActivationLayerInfo &act_info, |
| 238 | const Size2D &dilation) |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 239 | { |
| 240 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 241 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 242 | ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1); |
| 243 | const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 244 | const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 245 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) + (weights->dimension(idx_w) - 1) * (dilation.x() - 1) > input->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right()); |
| 246 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) + (weights->dimension(idx_h) - 1) * (dilation.y() - 1) > input->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom()); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 247 | |
Giorgio Arena | 66cbafb | 2018-08-23 14:51:00 +0100 | [diff] [blame] | 248 | if(biases != nullptr) |
| 249 | { |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 250 | const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL); |
Giorgio Arena | 66cbafb | 2018-08-23 14:51:00 +0100 | [diff] [blame] | 251 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 252 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx)); |
Giorgio Arena | 66cbafb | 2018-08-23 14:51:00 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 255 | if(!NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input, weights, conv_info, depth_multiplier, dilation)) |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 256 | { |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 257 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 258 | TensorInfo accumulator = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32)); |
| 259 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, is_quantized ? &accumulator : output, conv_info, depth_multiplier)); |
| 260 | |
| 261 | if(is_quantized) |
| 262 | { |
| 263 | ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&accumulator, biases, output)); |
| 264 | } |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionAssemblyDispatch::validate(input, weights, biases, output, conv_info, depth_multiplier)); |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 271 | //Validate Activation Layer |
| 272 | if(act_info.enabled()) |
| 273 | { |
| 274 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info)); |
| 275 | } |
| 276 | |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 277 | return Status{}; |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 280 | void NEDepthwiseConvolutionLayer3x3::run_generic() |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 281 | { |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 282 | // Fill border |
| 283 | NEScheduler::get().schedule(&_border_handler, Window::DimX); |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 284 | |
| 285 | // Execute depthwise convolution |
| 286 | NEScheduler::get().schedule(&_dwc_kernel, Window::DimX); |
| 287 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 288 | // Add biases |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 289 | if(_has_bias || _is_quantized) |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 290 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 291 | NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX); |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 292 | } |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 293 | |
| 294 | // Permute output |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 295 | if(!_is_nchw) |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 296 | { |
| 297 | _permute_output.run(); |
| 298 | } |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 299 | } |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 300 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 301 | void NEDepthwiseConvolutionLayer3x3::run_optimized() |
| 302 | { |
| 303 | // Run assembly function |
| 304 | _dwc_optimized_func.run(); |
| 305 | |
| 306 | // Permute output |
| 307 | if(_is_nchw) |
| 308 | { |
| 309 | _permute_output.run(); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void NEDepthwiseConvolutionLayer3x3::run() |
| 314 | { |
| 315 | prepare(); |
| 316 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 317 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 318 | |
| 319 | // Permute input |
| 320 | if(_permute) |
| 321 | { |
| 322 | _permute_input.run(); |
| 323 | } |
| 324 | |
| 325 | _is_optimized ? run_optimized() : run_generic(); |
| 326 | |
| 327 | // Run activation |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 328 | if(_is_activationlayer_enabled) |
| 329 | { |
| 330 | _activationlayer_function.run(); |
| 331 | } |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void NEDepthwiseConvolutionLayer3x3::prepare() |
| 335 | { |
| 336 | if(!_is_prepared) |
| 337 | { |
| 338 | // Permute weights |
| 339 | if(_permute) |
| 340 | { |
| 341 | _permuted_weights.allocator()->allocate(); |
| 342 | _permute_weights.run(); |
| 343 | _original_weights->mark_as_unused(); |
| 344 | } |
| 345 | |
| 346 | // Prepare optimized function |
| 347 | if(_is_optimized) |
| 348 | { |
| 349 | _dwc_optimized_func.prepare(); |
| 350 | if(!_permuted_weights.is_used()) |
| 351 | { |
| 352 | _permuted_weights.allocator()->free(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | _is_prepared = true; |
| 357 | } |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 360 | NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer() |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 361 | : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _permute_input(), |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 362 | _permute_weights(), _permute_output(), _activationlayer_function(), _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _permuted_input(), _permuted_weights(), |
| 363 | _permuted_output(), _is_prepared(false), _is_quantized(false), _is_nhwc(false), _is_activationlayer_enabled(false), _original_weights(nullptr) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 364 | { |
| 365 | } |
| 366 | |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 367 | void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 368 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 369 | { |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 370 | const unsigned int channel_idx = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL); |
| 371 | ARM_COMPUTE_UNUSED(channel_idx); |
Georgios Pinitas | 8cffcd6 | 2018-11-16 17:11:50 +0000 | [diff] [blame] | 372 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 373 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 374 | ARM_COMPUTE_ERROR_ON((input->info()->dimension(channel_idx) * depth_multiplier) != weights->info()->dimension(channel_idx)); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 375 | // idx_w and idx_h only used for validation |
| 376 | const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 377 | const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
| 378 | ARM_COMPUTE_UNUSED(idx_w); |
| 379 | ARM_COMPUTE_UNUSED(idx_h); |
| 380 | |
| 381 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_w) + (weights->info()->dimension(idx_w) - 1) * (dilation.x() - 1) > input->info()->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right()); |
| 382 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_h) + (weights->info()->dimension(idx_h) - 1) * (dilation.y() - 1) > input->info()->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom()); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 383 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 384 | _is_nhwc = input->info()->data_layout() == DataLayout::NHWC; |
| 385 | |
| 386 | ITensor *input_to_use = input; |
| 387 | const ITensor *weights_to_use = weights; |
| 388 | ITensor *output_to_use = output; |
| 389 | |
| 390 | if(_is_nhwc) |
| 391 | { |
| 392 | _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U)); |
| 393 | _permuted_input.info()->set_data_layout(DataLayout::NCHW); |
| 394 | input_to_use = &_permuted_input; |
| 395 | |
| 396 | _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U)); |
| 397 | _permuted_weights.info()->set_data_layout(DataLayout::NCHW); |
| 398 | weights_to_use = &_permuted_weights; |
| 399 | } |
| 400 | |
| 401 | const size_t weights_w = weights_to_use->info()->dimension(0); |
| 402 | const size_t weights_h = weights_to_use->info()->dimension(1); |
| 403 | const size_t weights_z = weights_to_use->info()->dimension(2); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 404 | |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 405 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 406 | _is_prepared = false; |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 407 | _original_weights = weights_to_use; |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 408 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 409 | // Should bias be appended ? |
| 410 | bool append_bias = (biases != nullptr) && !_is_quantized; |
| 411 | |
| 412 | // Calculate output shape |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 413 | TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier, dilation); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 414 | |
| 415 | // Output auto inizialitation if not yet initialized |
| 416 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 417 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 418 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 419 | if(_is_nhwc) |
| 420 | { |
| 421 | permute(output_shape, PermutationVector(1U, 2U, 0U)); |
| 422 | _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); |
| 423 | _permuted_output.info()->set_data_layout(DataLayout::NCHW); |
| 424 | output_to_use = &_permuted_output; |
| 425 | } |
| 426 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 427 | // Output width and height |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 428 | const unsigned int conv_w = output_shape.x(); |
| 429 | const unsigned int conv_h = output_shape.y(); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 430 | |
| 431 | // Set up intermediate tensors |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 432 | 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] | 433 | const size_t conv_size = conv_w * conv_h; |
| 434 | |
| 435 | // Im2Col configuration |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 436 | TensorShape shape_im2col = input_to_use->info()->tensor_shape(); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 437 | shape_im2col.set(0, patch_size); |
| 438 | shape_im2col.set(1, conv_size); |
| 439 | shape_im2col.set(2, weights_z); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 440 | _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW)); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 441 | _im2col_kernel.configure(input_to_use, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 442 | |
| 443 | // Weights reshape configuration |
| 444 | const TensorShape shape_weights_reshape(patch_size, weights_z); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 445 | _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW)); |
| 446 | _weights_reshape_kernel.configure(weights_to_use, &_weights_reshaped, append_bias ? biases : nullptr); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 447 | |
| 448 | // GEMV configuration |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 449 | DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type(); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 450 | TensorShape shape_v2mm_out = input_to_use->info()->tensor_shape(); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 451 | shape_v2mm_out.set(0, conv_size * weights_z); |
| 452 | shape_v2mm_out.set(1, 1); |
| 453 | shape_v2mm_out.set(2, 1); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 454 | _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out).set_data_layout(DataLayout::NCHW)); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 455 | _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 456 | _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 457 | _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output_to_use, conv_w, conv_h); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 458 | |
| 459 | // Output staged configuration |
| 460 | if(_is_quantized) |
| 461 | { |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 462 | const QuantizationInfo output_quant_info = output->info()->quantization_info(); |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 463 | |
| 464 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale; |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame^] | 465 | int output_multiplier; |
| 466 | int output_shift; |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 467 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 468 | _output_stage_kernel.configure(&_output_reshaped, biases, output_to_use, output_multiplier, output_shift, output_quant_info.offset); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 469 | _output_reshaped.allocator()->allocate(); |
| 470 | } |
| 471 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 472 | if(_is_nhwc) |
| 473 | { |
| 474 | _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U)); |
| 475 | |
| 476 | _permuted_input.allocator()->allocate(); |
| 477 | _permuted_weights.allocator()->allocate(); |
| 478 | _permuted_output.allocator()->allocate(); |
| 479 | } |
| 480 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 481 | // Fill borders on inputs |
Anthony Barbier | fb8dda2 | 2018-01-30 09:27:05 +0000 | [diff] [blame] | 482 | PixelValue zero_in(static_cast<int32_t>(0)); |
| 483 | PixelValue zero_w(static_cast<int32_t>(0)); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 484 | if(_is_quantized) |
| 485 | { |
| 486 | zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset)); |
| 487 | zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset)); |
| 488 | } |
| 489 | BorderSize border_size = _v2mm_kernel.border_size(); |
| 490 | _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in); |
| 491 | |
| 492 | border_size.bottom = 0; |
| 493 | _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] | 494 | |
| 495 | // Allocate intermediate tensors |
| 496 | _input_reshaped.allocator()->allocate(); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 497 | _v2mm_output.allocator()->allocate(); |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 498 | |
| 499 | //Configure Activation Layer |
| 500 | _is_activationlayer_enabled = act_info.enabled(); |
| 501 | |
| 502 | if(_is_activationlayer_enabled) |
| 503 | { |
| 504 | _activationlayer_function.configure(output, nullptr, act_info); |
| 505 | } |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Georgios Pinitas | 1049020 | 2018-08-17 17:16:06 +0100 | [diff] [blame] | 508 | Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 509 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 510 | { |
| 511 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 512 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 513 | ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1); |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 514 | |
| 515 | const unsigned int width_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 516 | const unsigned int height_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 517 | |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 518 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(width_idx) + (weights->dimension(width_idx) - 1) * (dilation.x() - 1) > input->dimension(width_idx) + conv_info.pad_left() + conv_info.pad_right()); |
| 519 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(height_idx) + (weights->dimension(height_idx) - 1) * (dilation.y() - 1) > input->dimension(height_idx) + conv_info.pad_top() + conv_info.pad_bottom()); |
Georgios Pinitas | 1049020 | 2018-08-17 17:16:06 +0100 | [diff] [blame] | 520 | // Clone output to use auto init |
| 521 | auto output_clone = output->clone(); |
| 522 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 523 | const ITensorInfo *input_to_use = input; |
| 524 | const ITensorInfo *weights_to_use = weights; |
Georgios Pinitas | 1049020 | 2018-08-17 17:16:06 +0100 | [diff] [blame] | 525 | const ITensorInfo *output_to_use = output_clone.get(); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 526 | |
| 527 | TensorShape permuted_input_shape = input->tensor_shape(); |
| 528 | TensorShape permuted_weights_shape = weights->tensor_shape(); |
| 529 | TensorInfo permuted_input; |
| 530 | TensorInfo permuted_weights; |
| 531 | |
| 532 | if(input->data_layout() == DataLayout::NHWC) |
| 533 | { |
| 534 | permute(permuted_input_shape, PermutationVector(1U, 2U, 0U)); |
| 535 | permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U)); |
| 536 | |
| 537 | permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW)); |
| 538 | permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW)); |
| 539 | |
| 540 | input_to_use = &permuted_input; |
| 541 | weights_to_use = &permuted_weights; |
| 542 | } |
| 543 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 544 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 545 | const bool append_bias = (biases != nullptr) && !is_quantized; |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 546 | TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 547 | const size_t weights_w = weights_to_use->dimension(0); |
| 548 | const size_t weights_h = weights_to_use->dimension(1); |
| 549 | const size_t weights_z = weights_to_use->dimension(2); |
Gian Marco Iodice | 23e2479 | 2018-09-07 15:32:14 +0100 | [diff] [blame] | 550 | const unsigned int conv_w = output_shape[width_idx]; |
| 551 | const unsigned int conv_h = output_shape[height_idx]; |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 552 | const size_t patch_size = weights_w * weights_h + (append_bias ? 1 : 0); |
| 553 | const size_t conv_size = conv_w * conv_h; |
| 554 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 555 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | 1049020 | 2018-08-17 17:16:06 +0100 | [diff] [blame] | 556 | auto_init_if_empty(*output_clone, input->clone()->set_tensor_shape(output_shape)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 557 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 558 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 559 | TensorInfo permuted_output; |
| 560 | if(input->data_layout() == DataLayout::NHWC) |
| 561 | { |
| 562 | permute(output_shape, PermutationVector(1U, 2U, 0U)); |
Georgios Pinitas | 1049020 | 2018-08-17 17:16:06 +0100 | [diff] [blame] | 563 | permuted_output = TensorInfo(output_clone->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape).set_data_layout(DataLayout::NCHW)); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 564 | output_to_use = &permuted_output; |
| 565 | } |
| 566 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 567 | // Im2Col configuration |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 568 | TensorShape shape_im2col = input_to_use->tensor_shape(); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 569 | shape_im2col.set(0, patch_size); |
| 570 | shape_im2col.set(1, conv_size); |
| 571 | shape_im2col.set(2, weights_z); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 572 | TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col).set_data_layout(DataLayout::NCHW)); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 573 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseIm2ColKernel::validate(input_to_use, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier, dilation)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 574 | |
| 575 | // Weights reshape configuration |
| 576 | const TensorShape shape_weights_reshape(patch_size, weights_z); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 577 | TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape).set_data_layout(DataLayout::NCHW)); |
| 578 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseWeightsReshapeKernel::validate(weights_to_use, &weights_reshaped, append_bias ? biases : nullptr)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 579 | |
| 580 | // GEMV configuration |
| 581 | DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type(); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 582 | TensorShape shape_v2mm_out = input_to_use->tensor_shape(); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 583 | shape_v2mm_out.set(0, conv_size * weights_z); |
| 584 | shape_v2mm_out.set(1, 1); |
| 585 | shape_v2mm_out.set(2, 1); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 586 | TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out).set_data_layout(DataLayout::NCHW)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 587 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output)); |
| 588 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 589 | TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_to_use->tensor_shape())); |
| 590 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output_to_use, conv_w, conv_h)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 591 | |
| 592 | if(is_quantized) |
| 593 | { |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 594 | ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output_to_use)); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 597 | // Validate Activation Layer |
| 598 | if(act_info.enabled()) |
| 599 | { |
| 600 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info)); |
| 601 | } |
| 602 | |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 603 | return Status{}; |
| 604 | } |
| 605 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 606 | void NEDepthwiseConvolutionLayer::run() |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 607 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 608 | prepare(); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 609 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 610 | if(_is_nhwc) |
| 611 | { |
| 612 | _permute_input.run(); |
| 613 | } |
| 614 | |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 615 | NEScheduler::get().schedule(&_im2col_kernel, Window::DimX); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 616 | NEScheduler::get().schedule(&_v2mm_input_fill_border, Window::DimX); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 617 | NEScheduler::get().schedule(&_v2mm_kernel, Window::DimX); |
| 618 | NEScheduler::get().schedule(&_vector_to_tensor_kernel, Window::DimX); |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 619 | if(_is_quantized) |
| 620 | { |
| 621 | NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX); |
| 622 | } |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 623 | |
| 624 | if(_is_nhwc) |
| 625 | { |
| 626 | _permute_output.run(); |
| 627 | } |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 628 | |
| 629 | if(_is_activationlayer_enabled) |
| 630 | { |
| 631 | _activationlayer_function.run(); |
| 632 | } |
Anthony Barbier | fb8dda2 | 2018-01-30 09:27:05 +0000 | [diff] [blame] | 633 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 634 | |
| 635 | void NEDepthwiseConvolutionLayer::prepare() |
| 636 | { |
| 637 | if(!_is_prepared) |
| 638 | { |
| 639 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 640 | |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 641 | if(_is_nhwc) |
| 642 | { |
| 643 | _permute_weights.run(); |
| 644 | } |
| 645 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 646 | // Run reshape and mark original weights as unused |
| 647 | _weights_reshaped.allocator()->allocate(); |
| 648 | NEScheduler::get().schedule(&_weights_reshape_kernel, Window::DimX); |
| 649 | NEScheduler::get().schedule(&_v2mm_weights_fill_border, Window::DimX); |
| 650 | _original_weights->mark_as_unused(); |
| 651 | |
| 652 | _is_prepared = true; |
| 653 | } |
| 654 | } |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 655 | } // namespace arm_compute |