blob: 54075f2afa765e54782f552b24270309a31a2495 [file] [log] [blame]
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +01001/*
Viet-Hoa Doedafe7f2023-05-04 17:39:30 +01002 * Copyright (c) 2021-2023 Arm Limited.
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +01003 *
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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/CpuDepthwiseConv2d.h"
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010025
26#include "arm_compute/core/TensorInfo.h"
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010027#include "arm_compute/core/utils/misc/InfoHelpers.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/core/Validate.h"
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010030#include "arm_compute/runtime/NEON/NEScheduler.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
ramelg013ae3d882021-09-12 23:07:47 +010032#include "src/common/utils/Log.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010033#include "src/cpu/kernels/CpuDepthwiseConv2dNativeKernel.h"
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010034
35namespace arm_compute
36{
37namespace cpu
38{
39namespace
40{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041Status validate_arguments_optimized(const ITensorInfo *src,
42 const ITensorInfo *weights,
43 const ITensorInfo *biases,
44 const ITensorInfo *dst,
45 const ConvolutionInfo &info)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010046{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
49 DataType::F16, DataType::F32);
50 if (!is_data_type_quantized_per_channel(weights->data_type()))
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010051 {
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010052 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010053 }
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010054 ARM_COMPUTE_RETURN_ERROR_ON(src->data_layout() == DataLayout::UNKNOWN);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010055 ARM_COMPUTE_RETURN_ERROR_ON(info.dilation.x() < 1 || info.dilation.y() < 1);
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010056 const size_t idx_w = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH);
57 const size_t idx_h = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) + (weights->dimension(idx_w) - 1) * (info.dilation.x() - 1) >
59 src->dimension(idx_w) + info.pad_stride_info.pad_left() +
60 info.pad_stride_info.pad_right());
61 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) + (weights->dimension(idx_h) - 1) * (info.dilation.y() - 1) >
62 src->dimension(idx_h) + info.pad_stride_info.pad_top() +
63 info.pad_stride_info.pad_bottom());
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010064
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 if (biases != nullptr)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010066 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 const unsigned int channel_idx =
68 get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::CHANNEL);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010069 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
70 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
71 }
72
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010073 ARM_COMPUTE_RETURN_ON_ERROR(CpuDepthwiseConv2dAssemblyDispatch::validate(src, weights, biases, dst, info));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010074
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000075 // Validate Activation Layer
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 if (info.act_info.enabled() && !CpuDepthwiseConv2dAssemblyDispatch::is_activation_supported(info.act_info))
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010077 {
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010078 ARM_COMPUTE_RETURN_ON_ERROR(CpuActivation::validate(dst, nullptr, info.act_info));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010079 }
80 return Status{};
81}
82} // namespace
83
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010084void CpuDepthwiseConv2d::CpuDepthwiseConv2dOptimizedInternal::configure(ITensorInfo *src,
85 const ITensorInfo *weights,
86 const ITensorInfo *biases,
87 ITensorInfo *dst,
88 const ConvolutionInfo &info)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010089{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010090 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010091 // Perform validation step
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 ARM_COMPUTE_ERROR_THROW_ON(
93 CpuDepthwiseConv2dOptimizedInternal::validate(src, weights, (biases == nullptr) ? nullptr : biases, dst, info));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010094
Ramy Elgammala8db6122023-05-08 03:33:43 +010095 _is_quantized = is_data_type_quantized_asymmetric(src->data_type());
96 _has_bias = biases != nullptr;
97 _is_nchw = src->data_layout() == DataLayout::NCHW;
98 _permute = _is_nchw;
99 _is_prepared = false;
Milos Puzovica7077e92022-10-28 16:49:15 +0100100 _are_weights_const = weights->are_values_constant();
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100101
102 // Configure pipeline
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 _is_activationlayer_enabled =
104 info.act_info.enabled() && !CpuDepthwiseConv2dAssemblyDispatch::is_activation_supported(info.act_info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100105
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100106 _dwc_optimized_func = std::make_unique<CpuDepthwiseConv2dAssemblyDispatch>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if (_is_nchw)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100108 {
109 _permute_input = std::make_unique<cpu::CpuPermute>();
110 _permute_weights = std::make_unique<cpu::CpuPermute>();
111 _permute_output = std::make_unique<cpu::CpuPermute>();
112
113 auto input_perm = std::make_unique<TensorInfo>();
114 auto weights_perm = std::make_unique<TensorInfo>();
115 auto output_perm = std::make_unique<TensorInfo>();
116
117 // Configure the function to transform the input tensor from NCHW -> NHWC
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100118 _permute_input->configure(src, input_perm.get(), PermutationVector(2U, 0U, 1U));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100119 input_perm->set_data_layout(DataLayout::NHWC);
120
121 // Configure the function to transform the weights tensor from IHW -> HWI
122 _permute_weights->configure(weights, weights_perm.get(), PermutationVector(2U, 0U, 1U));
123 weights_perm->set_data_layout(DataLayout::NHWC);
124
125 output_perm->set_data_layout(DataLayout::NHWC);
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100126 output_perm->set_quantization_info(dst->quantization_info());
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100127
128 // Configure optimized depthwise
129 _dwc_optimized_func->configure(input_perm.get(), weights_perm.get(), biases, output_perm.get(), info);
130
131 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
132 output_perm->set_data_layout(DataLayout::NHWC);
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100133 _permute_output->configure(output_perm.get(), dst, PermutationVector(1U, 2U, 0U));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100134 }
135 else
136 {
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100137 _dwc_optimized_func->configure(src, weights, biases, dst, info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100138 }
139
140 // Configure activation
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 if (_is_activationlayer_enabled)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100142 {
143 _activationlayer_function = std::make_unique<cpu::CpuActivation>();
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100144 _activationlayer_function->configure(dst, nullptr, info.act_info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100145 }
146}
147
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100148Status CpuDepthwiseConv2d::CpuDepthwiseConv2dOptimizedInternal::validate(const ITensorInfo *src,
149 const ITensorInfo *weights,
150 const ITensorInfo *biases,
151 const ITensorInfo *dst,
152 const ConvolutionInfo &info)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100153{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100154 return validate_arguments_optimized(src, weights, biases, dst, info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100155}
156
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100157void CpuDepthwiseConv2d::CpuDepthwiseConv2dOptimizedInternal::run(ITensorPack &tensors)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100158{
159 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
160 prepare(tensors);
161
162 auto bias = tensors.get_const_tensor(TensorType::ACL_SRC_2);
163 auto dst = tensors.get_tensor(TensorType::ACL_DST_0);
164 auto workspace = tensors.get_tensor(TensorType::ACL_INT_3);
165 auto packed_weights = tensors.get_tensor(TensorType::ACL_INT_4);
166
167 // Permute input
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168 if (_permute)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100169 {
170 ITensorPack pack;
Michalis Spyroua7a74362021-04-23 10:32:48 +0100171 auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100172 auto src_perm = tensors.get_tensor(TensorType::ACL_INT_0);
173 pack.add_tensor(TensorType::ACL_SRC, src);
174 pack.add_tensor(TensorType::ACL_DST, src_perm);
175 _permute_input->run(pack);
176 }
177
178 // Run assembly function
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100179 if (_is_nchw)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100180 {
181 auto src_perm = tensors.get_tensor(TensorType::ACL_INT_0);
182 auto weights_perm = tensors.get_tensor(TensorType::ACL_INT_1);
183 auto dst_perm = tensors.get_tensor(TensorType::ACL_INT_2);
184
185 ITensorPack pack;
186 pack.add_tensor(TensorType::ACL_SRC_0, src_perm);
187 pack.add_tensor(TensorType::ACL_SRC_1, weights_perm);
188 pack.add_tensor(TensorType::ACL_SRC_2, bias);
189 pack.add_tensor(TensorType::ACL_INT_0, workspace);
190 pack.add_tensor(TensorType::ACL_INT_1, packed_weights);
191 pack.add_tensor(TensorType::ACL_DST, dst_perm);
192 _dwc_optimized_func->run(pack);
193 }
194 else
195 {
196 auto src = tensors.get_tensor(TensorType::ACL_SRC_0);
197 auto weights = tensors.get_tensor(TensorType::ACL_SRC_1);
198 auto dst = tensors.get_tensor(TensorType::ACL_DST);
199
200 ITensorPack pack;
201 pack.add_tensor(TensorType::ACL_SRC_0, src);
202 pack.add_tensor(TensorType::ACL_SRC_1, weights);
203 pack.add_tensor(TensorType::ACL_SRC_2, bias);
204 pack.add_tensor(TensorType::ACL_INT_0, workspace);
205 pack.add_tensor(TensorType::ACL_INT_1, packed_weights);
206 pack.add_tensor(TensorType::ACL_DST, dst);
207 _dwc_optimized_func->run(pack);
208 }
209
210 // Permute output
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100211 if (_is_nchw)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100212 {
213 ITensorPack pack;
214 auto dst_perm = tensors.get_tensor(TensorType::ACL_INT_2);
215 pack.add_tensor(TensorType::ACL_SRC, dst_perm);
216 pack.add_tensor(TensorType::ACL_DST, dst);
217 _permute_output->run(pack);
218 }
219
220 // Run activation
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100221 if (_is_activationlayer_enabled)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100222 {
223 ITensorPack pack;
224 pack.add_tensor(TensorType::ACL_SRC, dst);
225 pack.add_tensor(TensorType::ACL_DST, dst);
226 _activationlayer_function->run(pack);
227 }
228}
229
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100230void CpuDepthwiseConv2d::CpuDepthwiseConv2dOptimizedInternal::prepare(ITensorPack &tensors)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100231{
Milos Puzovica7077e92022-10-28 16:49:15 +0100232 // if weights are not constant then we need to repack so that weights
233 // can be updated in-place
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100234 if (!_are_weights_const)
Milos Puzovica7077e92022-10-28 16:49:15 +0100235 {
236 auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
237 auto bias = tensors.get_const_tensor(TensorType::ACL_SRC_2);
238 auto packed_weights = tensors.get_tensor(TensorType::ACL_INT_4);
239
240 ITensorPack pack_opt;
241 pack_opt.add_tensor(TensorType::ACL_SRC_1, weights);
242 pack_opt.add_tensor(TensorType::ACL_SRC_2, bias);
243 pack_opt.add_tensor(TensorType::ACL_INT_1, packed_weights);
244
245 // Prepare optimized function
246 _dwc_optimized_func->prepare(pack_opt);
247
248 return;
249 }
250
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100251 if (!_is_prepared)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100252 {
253 auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
254 auto bias = tensors.get_const_tensor(TensorType::ACL_SRC_2);
255 auto packed_weights = tensors.get_tensor(TensorType::ACL_INT_4);
256
257 // Permute weights
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100258 if (_permute)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100259 {
260 auto permuted_weights = tensors.get_tensor(TensorType::ACL_INT_1);
261
262 ITensorPack pack;
263 pack.add_tensor(TensorType::ACL_SRC, weights);
264 pack.add_tensor(TensorType::ACL_DST, permuted_weights);
265 _permute_weights->run(pack);
266
Michalis Spyroua7a74362021-04-23 10:32:48 +0100267 weights->mark_as_unused();
268
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100269 ITensorPack pack_opt;
270 pack_opt.add_const_tensor(TensorType::ACL_SRC_1, permuted_weights);
271 pack_opt.add_tensor(TensorType::ACL_SRC_2, bias);
272 pack_opt.add_tensor(TensorType::ACL_INT_1, packed_weights);
273
274 // Prepare optimized function
275 _dwc_optimized_func->prepare(pack_opt);
276 }
277 else
278 {
279 ITensorPack pack_opt;
280 pack_opt.add_tensor(TensorType::ACL_SRC_1, weights);
281 pack_opt.add_tensor(TensorType::ACL_SRC_2, bias);
282 pack_opt.add_tensor(TensorType::ACL_INT_1, packed_weights);
283
284 // Prepare optimized function
285 _dwc_optimized_func->prepare(pack_opt);
286 }
287
288 _is_prepared = true;
289 }
290}
291
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292void CpuDepthwiseConv2d::CpuDepthwiseConv2dGeneric::configure(ITensorInfo *src,
293 const ITensorInfo *weights,
294 const ITensorInfo *biases,
295 ITensorInfo *dst,
296 const ConvolutionInfo &info)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100297{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100298 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100299 ARM_COMPUTE_ERROR_THROW_ON(
300 CpuDepthwiseConv2d::validate(src, weights, (biases == nullptr) ? nullptr : biases, dst, info));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100301
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100302 _is_nchw = src->data_layout() == DataLayout::NCHW;
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100303 _is_prepared = !_is_nchw;
304
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100305 ITensorInfo *input_to_use = src;
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100306 const ITensorInfo *weights_to_use = weights;
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100307 ITensorInfo *output_to_use = dst;
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100308
309 auto input_perm = std::make_unique<TensorInfo>();
310 auto weights_perm = std::make_unique<TensorInfo>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100311 auto output_perm = std::make_unique<TensorInfo>(
312 dst->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(TensorShape()));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100313
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100314 if (_is_nchw)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100315 {
316 _permute_input = std::make_unique<cpu::CpuPermute>();
317 _permute_weights = std::make_unique<cpu::CpuPermute>();
318
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100319 _permute_input->configure(src, input_perm.get(), PermutationVector(2U, 0U, 1U));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100320 input_perm->set_data_layout(DataLayout::NHWC);
321 input_to_use = input_perm.get();
322
323 _permute_weights->configure(weights, weights_perm.get(), PermutationVector(2U, 0U, 1U));
324 weights_perm->set_data_layout(DataLayout::NHWC);
325 weights_to_use = weights_perm.get();
326
327 output_to_use = output_perm.get();
328 }
329
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100330 _depthwise_conv_kernel = std::make_unique<cpu::kernels::CpuDepthwiseConv2dNativeKernel>();
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100331 _depthwise_conv_kernel->configure(input_to_use, weights_to_use, biases, output_to_use, info);
332
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100333 if (_is_nchw)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100334 {
335 _permute_output = std::make_unique<cpu::CpuPermute>();
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100336 _permute_output->configure(output_perm.get(), dst, PermutationVector(1U, 2U, 0U));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100337 output_perm->set_data_layout(DataLayout::NHWC);
338 }
339
340 //Configure Activation Layer
341 _is_activationlayer_enabled = info.act_info.enabled();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100342 if (_is_activationlayer_enabled)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100343 {
344 _activationlayer_function = std::make_unique<cpu::CpuActivation>();
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100345 _activationlayer_function->configure(dst, nullptr, info.act_info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100346 }
347}
348
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100349Status CpuDepthwiseConv2d::CpuDepthwiseConv2dGeneric::validate(const ITensorInfo *src,
350 const ITensorInfo *weights,
351 const ITensorInfo *biases,
352 const ITensorInfo *dst,
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100353 const ConvolutionInfo &info)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100354{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100355 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100356 if (src->data_layout() == DataLayout::NCHW)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100357 {
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100358 TensorShape permuted_input_shape = src->tensor_shape();
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100359 TensorShape permuted_weights_shape = weights->tensor_shape();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100360 TensorShape permuted_output_shape =
361 misc::shape_calculator::compute_depthwise_convolution_shape(*src, *weights, info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100362 permute(permuted_input_shape, PermutationVector(2U, 0U, 1U));
363 permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U));
364 permute(permuted_output_shape, PermutationVector(2U, 0U, 1U));
365
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100366 const TensorInfo permuted_input = TensorInfo(src->clone()
367 ->set_is_resizable(true)
368 .reset_padding()
369 .set_tensor_shape(permuted_input_shape)
370 .set_data_layout(DataLayout::NHWC));
371 const TensorInfo permuted_weights = TensorInfo(weights->clone()
372 ->set_is_resizable(true)
373 .reset_padding()
374 .set_tensor_shape(permuted_weights_shape)
375 .set_data_layout(DataLayout::NHWC));
376 const TensorInfo permuted_output = TensorInfo(dst->clone()
377 ->set_is_resizable(true)
378 .reset_padding()
379 .set_tensor_shape(permuted_output_shape)
380 .set_data_layout(DataLayout::NCHW));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100381
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100382 ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(src, &permuted_input, PermutationVector(2U, 0U, 1U)));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100383 ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U)));
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100384 ARM_COMPUTE_RETURN_ON_ERROR(CpuPermute::validate(&permuted_output, dst, PermutationVector(1U, 2U, 0U)));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100385
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100386 ARM_COMPUTE_RETURN_ON_ERROR(cpu::kernels::CpuDepthwiseConv2dNativeKernel::validate(
387 &permuted_input, &permuted_weights, biases, &permuted_output, info));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100388 }
389 else
390 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100391 ARM_COMPUTE_RETURN_ON_ERROR(
392 cpu::kernels::CpuDepthwiseConv2dNativeKernel::validate(src, weights, biases, dst, info));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100393 }
394
395 // Validate Activation Layer
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100396 if (info.act_info.enabled() && !CpuDepthwiseConv2dAssemblyDispatch::is_activation_supported(info.act_info))
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100397 {
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100398 ARM_COMPUTE_RETURN_ON_ERROR(CpuActivation::validate(dst, nullptr, info.act_info));
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100399 }
400
401 return Status{};
402}
403
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100404void CpuDepthwiseConv2d::CpuDepthwiseConv2dGeneric::run(ITensorPack &tensors)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100405{
406 auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
407 auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
408 auto biases = tensors.get_const_tensor(TensorType::ACL_SRC_2);
409 auto dst = tensors.get_tensor(TensorType::ACL_DST_0);
410
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100411 if (_is_nchw)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100412 {
413 prepare(tensors);
414 auto src_perm = tensors.get_tensor(TensorType::ACL_INT_0);
415 auto weights_perm = tensors.get_tensor(TensorType::ACL_INT_1);
416 auto dst_perm = tensors.get_tensor(TensorType::ACL_INT_2);
417
418 ITensorPack pack;
419 pack.add_tensor(TensorType::ACL_SRC, src);
420 pack.add_tensor(TensorType::ACL_DST, src_perm);
421 _permute_input->run(pack);
422
423 ITensorPack pack_depth;
424 pack_depth.add_const_tensor(TensorType::ACL_SRC_0, src_perm);
425 pack_depth.add_const_tensor(TensorType::ACL_SRC_1, weights_perm);
426 pack_depth.add_tensor(TensorType::ACL_SRC_2, biases);
427 pack_depth.add_tensor(TensorType::ACL_DST, dst_perm);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100428 NEScheduler::get().schedule_op(_depthwise_conv_kernel.get(), Window::DimY, _depthwise_conv_kernel->window(),
429 pack_depth);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100430 }
431 else
432 {
433 ITensorPack pack_depth;
434 pack_depth.add_tensor(TensorType::ACL_SRC_0, src);
435 pack_depth.add_tensor(TensorType::ACL_SRC_1, weights);
436 pack_depth.add_tensor(TensorType::ACL_SRC_2, biases);
437 pack_depth.add_tensor(TensorType::ACL_DST, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100438 NEScheduler::get().schedule_op(_depthwise_conv_kernel.get(), Window::DimY, _depthwise_conv_kernel->window(),
439 pack_depth);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100440 }
441
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100442 if (_is_nchw)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100443 {
444 ITensorPack pack;
445 auto dst_perm = tensors.get_tensor(TensorType::ACL_INT_2);
446 pack.add_tensor(TensorType::ACL_SRC, dst_perm);
447 pack.add_tensor(TensorType::ACL_DST, dst);
448 _permute_output->run(pack);
449 }
450
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100451 if (_is_activationlayer_enabled)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100452 {
453 ITensorPack pack;
454 pack.add_tensor(TensorType::ACL_SRC, dst);
455 pack.add_tensor(TensorType::ACL_DST, dst);
456 _activationlayer_function->run(pack);
457 }
458}
459
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100460void CpuDepthwiseConv2d::CpuDepthwiseConv2dGeneric::prepare(ITensorPack &tensors)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100461{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100462 if (!_is_prepared)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100463 {
464 auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
465 auto weights_perm = tensors.get_tensor(TensorType::ACL_INT_1);
466
467 ARM_COMPUTE_ERROR_ON(!weights->is_used());
468
469 ITensorPack pack;
470 pack.add_tensor(TensorType::ACL_SRC, weights);
471 pack.add_tensor(TensorType::ACL_DST, weights_perm);
472
473 _permute_weights->run(pack);
474 weights->mark_as_unused();
475 _is_prepared = true;
476 }
477}
478
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100479void CpuDepthwiseConv2d::configure(ITensorInfo *src,
480 const ITensorInfo *weights,
481 const ITensorInfo *biases,
482 ITensorInfo *dst,
483 const ConvolutionInfo &info)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100484{
ramelg013ae3d882021-09-12 23:07:47 +0100485 ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, info);
486
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100487 _depth_conv_func =
488 get_depthwiseconvolution_function(src, weights, (biases != nullptr) ? biases : nullptr, dst, info);
489 switch (_depth_conv_func)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100490 {
491 case DepthwiseConvolutionFunction::OPTIMIZED:
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100492 _func_optimized.configure(src, weights, biases, dst, info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100493 break;
494 case DepthwiseConvolutionFunction::GENERIC:
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100495 _func_generic.configure(src, weights, biases, dst, info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100496 break;
497 default:
498 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
499 }
500}
501
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100502Status CpuDepthwiseConv2d::validate(const ITensorInfo *src,
503 const ITensorInfo *weights,
504 const ITensorInfo *biases,
505 const ITensorInfo *dst,
506 const ConvolutionInfo &info)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100507{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100508 DepthwiseConvolutionFunction depth_conv_func = get_depthwiseconvolution_function(src, weights, biases, dst, info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100509 switch (depth_conv_func)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100510 {
511 case DepthwiseConvolutionFunction::OPTIMIZED:
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100512 return CpuDepthwiseConv2dOptimizedInternal::validate(src, weights, biases, dst, info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100513 break;
514 case DepthwiseConvolutionFunction::GENERIC:
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100515 return CpuDepthwiseConv2dGeneric::validate(src, weights, biases, dst, info);
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100516 break;
517 default:
518 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
519 }
520}
521
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100522DepthwiseConvolutionFunction CpuDepthwiseConv2d::get_depthwiseconvolution_function(const ITensorInfo *src,
523 const ITensorInfo *weights,
524 const ITensorInfo *biases,
525 const ITensorInfo *dst,
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100526 const ConvolutionInfo &info)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100527{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100528 if (bool(CpuDepthwiseConv2dOptimizedInternal::validate(src, weights, biases, dst, info)))
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100529 {
530 return DepthwiseConvolutionFunction::OPTIMIZED;
531 }
532 else
533 {
534 return DepthwiseConvolutionFunction::GENERIC;
535 }
536}
537
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100538void CpuDepthwiseConv2d::run(ITensorPack &tensors)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100539{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100540 switch (_depth_conv_func)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100541 {
542 case DepthwiseConvolutionFunction::OPTIMIZED:
543 _func_optimized.run(tensors);
544 break;
545 case DepthwiseConvolutionFunction::GENERIC:
546 _func_generic.run(tensors);
547 break;
548 default:
549 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
550 }
551}
552
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100553void CpuDepthwiseConv2d::prepare(ITensorPack &tensors)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100554{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100555 switch (_depth_conv_func)
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100556 {
557 case DepthwiseConvolutionFunction::OPTIMIZED:
558 _func_optimized.prepare(tensors);
559 break;
560 case DepthwiseConvolutionFunction::GENERIC:
561 _func_generic.prepare(tensors);
562 break;
563 default:
564 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
565 }
566}
567} // namespace cpu
568} // namespace arm_compute