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