Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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 | |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/utils/misc/InfoHelpers.h" |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 30 | #include "src/runtime/cpu/operators/CpuDepthwiseConv2d.h" |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 31 | |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 32 | using namespace arm_compute::misc; |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 33 | using namespace arm_compute::misc::shape_calculator; |
Michalis Spyrou | 7362f0d | 2017-10-18 17:58:22 +0100 | [diff] [blame] | 34 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 37 | NEDepthwiseConvolutionLayer::~NEDepthwiseConvolutionLayer() = default; |
| 38 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 39 | struct NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::Impl |
| 40 | { |
| 41 | ITensor *src{ nullptr }; // SRC_0 |
| 42 | ITensor *dst{ nullptr }; // DST_0 |
| 43 | const ITensor *weights |
| 44 | { |
| 45 | nullptr |
| 46 | }; // SRC_1 |
| 47 | const ITensor *biases |
| 48 | { |
| 49 | nullptr |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 50 | }; // SRC_2 |
| 51 | Tensor permuted_input{}; // INT_0 |
| 52 | Tensor permuted_weights{}; // INT_1 |
| 53 | Tensor permuted_output{}; // INT_2 |
| 54 | Tensor workspace{}; // INT_3 |
| 55 | Tensor packed_weights{}; // INT_4 |
| 56 | std::shared_ptr<cpu::CpuDepthwiseConv2d> op{ nullptr }; |
| 57 | bool is_prepared{ false }; |
| 58 | bool permute{ false }; |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 61 | NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::NEDepthwiseConvolutionLayerOptimizedInternal(std::shared_ptr<IMemoryManager> memory_manager) |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 62 | : _memory_group(memory_manager), _impl(std::make_unique<Impl>()) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 63 | { |
| 64 | } |
| 65 | |
Georgios Pinitas | 7d0adc6 | 2020-09-04 15:25:24 +0100 | [diff] [blame] | 66 | void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::configure(ITensor *input, |
| 67 | const ITensor *weights, |
| 68 | const ITensor *biases, |
| 69 | ITensor *output, const PadStrideInfo &conv_info, |
| 70 | unsigned int depth_multiplier, |
| 71 | const ActivationLayerInfo &act_info, |
| 72 | const Size2D &dilation) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 73 | { |
Georgios Pinitas | 7d0adc6 | 2020-09-04 15:25:24 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 75 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 76 | bool is_nhwc = input->info()->data_layout() == DataLayout::NCHW; |
| 77 | _impl->src = input; |
| 78 | _impl->weights = weights; |
| 79 | _impl->biases = biases; |
| 80 | _impl->dst = output; |
| 81 | _impl->permute = is_nhwc; |
| 82 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 83 | _impl->op = std::make_unique<cpu::CpuDepthwiseConv2d>(); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 84 | ConvolutionInfo info{ conv_info, depth_multiplier, act_info, dilation }; |
| 85 | _impl->op->configure(_impl->src->info(), _impl->weights->info(), _impl->biases == nullptr ? nullptr : _impl->biases->info(), |
| 86 | _impl->dst->info(), info); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 87 | |
Georgios Pinitas | 7d0adc6 | 2020-09-04 15:25:24 +0100 | [diff] [blame] | 88 | // Configure pipeline |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 89 | ActivationLayerInfo act_info_to_use = ActivationLayerInfo(); |
| 90 | const bool is_relu = arm_compute::utils::info_helpers::is_relu(act_info); |
| 91 | const bool is_relu6 = arm_compute::utils::info_helpers::is_relu6(act_info); |
| 92 | bool is_activationlayer_enabled = act_info.enabled() && !(is_relu || is_relu6); |
| 93 | |
| 94 | if(!is_activationlayer_enabled) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 95 | { |
| 96 | act_info_to_use = act_info; |
| 97 | } |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 98 | info = ConvolutionInfo{ conv_info, depth_multiplier, act_info_to_use, dilation }; |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 99 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 100 | auto dwc_optimized_func = std::make_unique<cpu::CpuDepthwiseConv2dAssemblyDispatch>(); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 101 | |
| 102 | if(is_nhwc) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 103 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 104 | auto permute_input = std::make_unique<cpu::CpuPermute>(); |
| 105 | auto permute_weights = std::make_unique<cpu::CpuPermute>(); |
| 106 | auto permute_output = std::make_unique<cpu::CpuPermute>(); |
| 107 | |
| 108 | _memory_group.manage(&_impl->permuted_input); |
| 109 | _memory_group.manage(&_impl->permuted_weights); |
| 110 | _memory_group.manage(&_impl->permuted_output); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 111 | |
| 112 | // Configure the function to transform the input tensor from NCHW -> NHWC |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 113 | permute_input->configure(input->info(), _impl->permuted_input.info(), PermutationVector(2U, 0U, 1U)); |
| 114 | _impl->permuted_input.info()->set_data_layout(DataLayout::NHWC); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 115 | |
| 116 | // Configure the function to transform the weights tensor from IHW -> HWI |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 117 | permute_weights->configure(weights->info(), _impl->permuted_weights.info(), PermutationVector(2U, 0U, 1U)); |
| 118 | _impl->permuted_weights.info()->set_data_layout(DataLayout::NHWC); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 119 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 120 | _impl->permuted_output.info()->set_data_layout(DataLayout::NHWC); |
| 121 | _impl->permuted_output.info()->set_quantization_info(output->info()->quantization_info()); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 122 | |
| 123 | // Configure optimized depthwise |
Michalis Spyrou | a7a7436 | 2021-04-23 10:32:48 +0100 | [diff] [blame] | 124 | dwc_optimized_func->configure(_impl->permuted_input.info(), _impl->permuted_weights.info(), biases == nullptr ? nullptr : biases->info(), _impl->permuted_output.info(), info); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 125 | |
| 126 | // Configure the function to transform the convoluted output to ACL's native ordering format NCHW |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 127 | _impl->permuted_output.info()->set_data_layout(DataLayout::NHWC); |
| 128 | permute_output->configure(_impl->permuted_output.info(), output->info(), PermutationVector(1U, 2U, 0U)); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 129 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 130 | _impl->permuted_input.allocator()->allocate(); |
| 131 | _impl->permuted_output.allocator()->allocate(); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 132 | } |
| 133 | else |
| 134 | { |
Michalis Spyrou | a7a7436 | 2021-04-23 10:32:48 +0100 | [diff] [blame] | 135 | dwc_optimized_func->configure(_impl->src->info(), _impl->weights->info(), biases == nullptr ? nullptr : biases->info(), _impl->dst->info(), info); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 136 | } |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 137 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 138 | // Allocate memory based on the internal memory requirements |
| 139 | experimental::MemoryRequirements mem_req = dwc_optimized_func->workspace(); |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame^] | 140 | _impl->workspace.allocator()->init(TensorInfo(TensorShape{ mem_req[0].size + mem_req[0].alignment }, 1, DataType::S8), mem_req[0].alignment); |
| 141 | _impl->packed_weights.allocator()->init(TensorInfo(TensorShape{ mem_req[1].size + mem_req[1].alignment }, 1, DataType::S8), mem_req[1].alignment); |
| 142 | _memory_group.manage(&_impl->workspace); |
| 143 | _memory_group.manage(&_impl->packed_weights); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 144 | _impl->workspace.allocator()->allocate(); |
| 145 | _impl->packed_weights.allocator()->allocate(); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | Status NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::validate(const ITensorInfo *input, |
| 149 | const ITensorInfo *weights, |
| 150 | const ITensorInfo *biases, |
| 151 | const ITensorInfo *output, |
| 152 | const PadStrideInfo &conv_info, |
| 153 | unsigned int depth_multiplier, |
| 154 | const ActivationLayerInfo &act_info, |
| 155 | const Size2D &dilation) |
| 156 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 157 | ConvolutionInfo info{ conv_info, depth_multiplier, act_info, dilation }; |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 158 | return cpu::CpuDepthwiseConv2d::validate(input, weights, biases, output, info); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 161 | void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::run() |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 162 | { |
| 163 | prepare(); |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 164 | MemoryGroupResourceScope scope_mg(_memory_group); |
| 165 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 166 | ITensorPack pack; |
| 167 | pack.add_tensor(TensorType::ACL_SRC_0, _impl->src); |
| 168 | pack.add_tensor(TensorType::ACL_SRC_1, _impl->weights); |
| 169 | pack.add_tensor(TensorType::ACL_SRC_2, _impl->biases); |
| 170 | pack.add_tensor(TensorType::ACL_INT_0, &_impl->permuted_input); |
| 171 | pack.add_tensor(TensorType::ACL_INT_1, &_impl->permuted_weights); |
| 172 | pack.add_tensor(TensorType::ACL_INT_2, &_impl->permuted_output); |
| 173 | pack.add_tensor(TensorType::ACL_INT_3, &_impl->workspace); |
| 174 | pack.add_tensor(TensorType::ACL_INT_4, &_impl->packed_weights); |
| 175 | pack.add_tensor(TensorType::ACL_DST_0, _impl->dst); |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 176 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 177 | _impl->op->run(pack); |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 180 | void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::prepare() |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 181 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 182 | if(!_impl->is_prepared) |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 183 | { |
| 184 | // Permute weights |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 185 | if(_impl->permute) |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 186 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 187 | _impl->permuted_weights.allocator()->allocate(); |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 190 | if(!_impl->permuted_weights.is_used()) |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 191 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 192 | _impl->permuted_weights.allocator()->free(); |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 195 | _impl->is_prepared = true; |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 199 | struct NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::Impl |
| 200 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 201 | Tensor permuted_input{}; |
| 202 | Tensor permuted_weights{}; |
| 203 | Tensor permuted_output{}; |
| 204 | bool is_prepared{ false }; |
| 205 | bool is_nchw{ false }; |
| 206 | bool is_activationlayer_enabled{ false }; |
| 207 | const ITensor *weights{ nullptr }; |
| 208 | const ITensor *biases{ nullptr }; |
| 209 | const ITensor *src{ nullptr }; |
| 210 | ITensor *dst{ nullptr }; |
| 211 | std::shared_ptr<cpu::CpuDepthwiseConv2d> op{ nullptr }; |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 212 | }; |
| 213 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 214 | NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::NEDepthwiseConvolutionLayerGeneric() |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 215 | : _impl(std::make_unique<Impl>()) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 216 | { |
| 217 | } |
| 218 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 219 | void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, |
| 220 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 221 | { |
Michele Di Giorgio | ff27192 | 2019-07-17 15:59:32 +0100 | [diff] [blame] | 222 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Michele Di Giorgio | ff27192 | 2019-07-17 15:59:32 +0100 | [diff] [blame] | 223 | ARM_COMPUTE_ERROR_THROW_ON(NEDepthwiseConvolutionLayer::validate(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(), |
| 224 | output->info(), conv_info, depth_multiplier, act_info, dilation)); |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 225 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 226 | const ConvolutionInfo info{ conv_info, depth_multiplier, act_info, dilation }; |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 227 | _impl->op = std::make_unique<cpu::CpuDepthwiseConv2d>(); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 228 | _impl->op->configure(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), info); |
| 229 | |
| 230 | _impl->src = input; |
| 231 | _impl->dst = output; |
| 232 | _impl->weights = weights; |
| 233 | _impl->biases = biases; |
| 234 | _impl->is_nchw = input->info()->data_layout() == DataLayout::NCHW; |
| 235 | _impl->is_prepared = !_impl->is_nchw; |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 236 | |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 237 | ITensor *input_to_use = input; |
| 238 | const ITensor *weights_to_use = weights; |
| 239 | ITensor *output_to_use = output; |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 240 | if(_impl->is_nchw) |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 241 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 242 | auto permute_input = std::make_unique<cpu::CpuPermute>(); |
| 243 | auto permute_weights = std::make_unique<cpu::CpuPermute>(); |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 244 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 245 | permute_input->configure(input->info(), _impl->permuted_input.info(), PermutationVector(2U, 0U, 1U)); |
| 246 | _impl->permuted_input.info()->set_data_layout(DataLayout::NHWC); |
| 247 | input_to_use = &_impl->permuted_input; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 248 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 249 | permute_weights->configure(weights->info(), _impl->permuted_weights.info(), PermutationVector(2U, 0U, 1U)); |
| 250 | _impl->permuted_weights.info()->set_data_layout(DataLayout::NHWC); |
| 251 | weights_to_use = &_impl->permuted_weights; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 252 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 253 | _impl->permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(TensorShape())); |
| 254 | output_to_use = &_impl->permuted_output; |
Giorgio Arena | 26b2216 | 2018-08-13 15:49:49 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 257 | auto depthwise_conv_kernel = std::make_unique<cpu::kernels::CpuDepthwiseConv2dNativeKernel>(); |
Michalis Spyrou | 3294449 | 2021-04-20 15:20:31 +0100 | [diff] [blame] | 258 | depthwise_conv_kernel->configure(input_to_use->info(), weights_to_use->info(), biases == nullptr ? nullptr : biases->info(), output_to_use->info(), info); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 259 | |
| 260 | if(_impl->is_nchw) |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 261 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 262 | auto permute_output = std::make_unique<cpu::CpuPermute>(); |
| 263 | permute_output->configure(_impl->permuted_output.info(), output->info(), PermutationVector(1U, 2U, 0U)); |
| 264 | _impl->permuted_output.info()->set_data_layout(DataLayout::NHWC); |
| 265 | |
| 266 | _impl->permuted_input.allocator()->allocate(); |
| 267 | _impl->permuted_weights.allocator()->allocate(); |
| 268 | _impl->permuted_output.allocator()->allocate(); |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 269 | } |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 272 | Status NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 273 | const PadStrideInfo &conv_info, |
| 274 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 275 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 276 | ConvolutionInfo info{ conv_info, depth_multiplier, act_info, dilation }; |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 277 | return cpu::CpuDepthwiseConv2d::validate(input, weights, biases, output, info); |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 280 | void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::run() |
Michalis Spyrou | b7b3153 | 2017-11-23 12:10:21 +0000 | [diff] [blame] | 281 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 282 | ITensorPack pack; |
| 283 | pack.add_tensor(TensorType::ACL_SRC_0, _impl->src); |
| 284 | pack.add_tensor(TensorType::ACL_SRC_1, _impl->weights); |
| 285 | pack.add_tensor(TensorType::ACL_SRC_2, _impl->biases); |
| 286 | pack.add_tensor(TensorType::ACL_INT_0, &_impl->permuted_input); |
| 287 | pack.add_tensor(TensorType::ACL_INT_1, &_impl->permuted_weights); |
| 288 | pack.add_tensor(TensorType::ACL_INT_2, &_impl->permuted_output); |
| 289 | pack.add_tensor(TensorType::ACL_DST_0, _impl->dst); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 290 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 291 | _impl->op->run(pack); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 292 | } |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 293 | |
| 294 | NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 295 | : _memory_group(std::move(memory_manager)), _impl(std::make_unique<Impl>()) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 296 | { |
| 297 | } |
| 298 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 299 | #ifndef DOXYGEN_SKIP_THIS |
| 300 | struct NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer::Impl |
| 301 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 302 | DepthwiseConvolutionFunction depth_conv_func{ DepthwiseConvolutionFunction::OPTIMIZED }; |
| 303 | NEDepthwiseConvolutionLayerOptimizedInternal func_optimized{ nullptr }; |
| 304 | NEDepthwiseConvolutionLayerGeneric func_generic{}; |
| 305 | std::shared_ptr<cpu::CpuDepthwiseConv2d> op{ nullptr }; |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 306 | }; |
| 307 | #endif // DOXYGEN_SKIP_THIS |
| 308 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 309 | void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, |
| 310 | const ActivationLayerInfo &act_info, const Size2D &dilation) |
| 311 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 312 | const ConvolutionInfo info{ conv_info, depth_multiplier, act_info, dilation }; |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 313 | _impl->op = std::make_shared<cpu::CpuDepthwiseConv2d>(); |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 314 | _impl->depth_conv_func = _impl->op->get_depthwiseconvolution_function(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), |
| 315 | info); |
| 316 | switch(_impl->depth_conv_func) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 317 | { |
| 318 | case DepthwiseConvolutionFunction::OPTIMIZED: |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 319 | _impl->func_optimized.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 320 | break; |
| 321 | case DepthwiseConvolutionFunction::GENERIC: |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 322 | _impl->func_generic.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 323 | break; |
| 324 | default: |
| 325 | ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction"); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 330 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) |
| 331 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 332 | ConvolutionInfo info{ conv_info, depth_multiplier, act_info, dilation }; |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 333 | return cpu::CpuDepthwiseConv2d::validate(input, weights, biases, output, info); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void NEDepthwiseConvolutionLayer::run() |
| 337 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 338 | switch(_impl->depth_conv_func) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 339 | { |
| 340 | case DepthwiseConvolutionFunction::OPTIMIZED: |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 341 | _impl->func_optimized.run(); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 342 | break; |
| 343 | case DepthwiseConvolutionFunction::GENERIC: |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 344 | _impl->func_generic.run(); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 345 | break; |
| 346 | default: |
| 347 | ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured"); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void NEDepthwiseConvolutionLayer::prepare() |
| 352 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 353 | switch(_impl->depth_conv_func) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 354 | { |
| 355 | case DepthwiseConvolutionFunction::OPTIMIZED: |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 356 | _impl->func_optimized.prepare(); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 357 | break; |
| 358 | case DepthwiseConvolutionFunction::GENERIC: |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 359 | _impl->func_generic.prepare(); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 360 | break; |
| 361 | default: |
| 362 | ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured"); |
| 363 | } |
| 364 | } |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 365 | } // namespace arm_compute |