blob: e6c9cb5c20b66894ce6cf5a4a0da5fc13fe7212a [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Teresa Charlinee1497c2023-03-30 13:56:34 +01002// Copyright © 2017,2022-2023 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
Matthew Benthamd8777392018-10-08 09:38:55 +01006#include "ClDepthwiseConvolutionWorkload.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01007
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +01008#include <ResolveType.hpp>
Matthew Benthamd8777392018-10-08 09:38:55 +01009#include "ClWorkloadUtils.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010010
Mike Kelly07810fc2020-11-12 10:58:48 +000011#include <armnn/Exceptions.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000012#include <aclCommon/ArmComputeUtils.hpp>
13#include <aclCommon/ArmComputeTensorUtils.hpp>
14#include <cl/ClTensorHandle.hpp>
Colm Donelan0c479742021-12-10 12:43:54 +000015#include <armnn/backends/TensorHandle.hpp>
Matteo Martincigh747ef822018-12-18 09:26:39 +000016#include <backendsCommon/WorkloadUtils.hpp>
Colm Donelan0c479742021-12-10 12:43:54 +000017#include <armnn/backends/WorkloadData.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010018
Matthew Benthamd8777392018-10-08 09:38:55 +010019#include <arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h>
20
telsoa01c577f2c2018-08-31 09:22:23 +010021namespace armnn
22{
23
24using namespace armcomputetensorutils;
25
26arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo& input,
Matteo Martincigh747ef822018-12-18 09:26:39 +000027 const TensorInfo& output,
28 const DepthwiseConvolution2dDescriptor& descriptor,
29 const TensorInfo& weights,
Mike Kelly07810fc2020-11-12 10:58:48 +000030 const Optional<TensorInfo>& biases,
31 const ActivationDescriptor* activationDescriptor)
telsoa01c577f2c2018-08-31 09:22:23 +010032{
Matteo Martincigh747ef822018-12-18 09:26:39 +000033 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
Nikhil Raja05c2102018-09-25 16:16:13 +010034 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
Matteo Martincigh747ef822018-12-18 09:26:39 +000035
Cathal Corbett4b19d222022-05-11 20:12:17 +010036 // ArmNN format for weights for depthwise is [1, H, W, C] independently of the input/output layout
37 //
38 // ACL format for weights for depthwise is:
39 // - [1, H, W, C] for [N, H, W, C] input/output layout (matches with ArmNN)
40 // - [1, C, H, W] for [N, C, H, W] input/output layout
41 //
42 // Therefore ArmNN weights have to be permuted when input/output layout is [N, C, H, W] to pass them to ACL.
43 // The PermuteDepthwiseConv2dWeights backend optimization takes care of this, but it has not been performed yet,
44 // so we do the permute here for the TensorInfo weights.
Jan Eilers53ef7952021-06-02 12:01:25 +010045 unsigned int aclDepthMultiplier;
46 TensorInfo weightsPermuted;
47 std::tie(weightsPermuted, aclDepthMultiplier) = Convert1HWOTensorInfoToAcl(weights, input,descriptor.m_DataLayout);
Matteo Martincigh747ef822018-12-18 09:26:39 +000048
49 // Convert the weights into the compute library format
Cathal Corbett4452baf2022-05-13 09:55:59 +010050 arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
51 aclWeightsInfo.set_are_values_constant(weights.IsConstant());
telsoa01c577f2c2018-08-31 09:22:23 +010052
53 arm_compute::TensorInfo aclBiasesInfo;
Cathal Corbett4b19d222022-05-11 20:12:17 +010054 arm_compute::TensorInfo* optionalAclBiasesInfo = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +010055 if (descriptor.m_BiasEnabled)
56 {
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010057 ARMNN_ASSERT(biases.has_value());
TeresaARMacedd852023-05-11 15:16:39 +000058 // Same for bias as weights. We don't currently support non const.
59 if (!biases.value().IsConstant())
60 {
61 return arm_compute::Status{arm_compute::ErrorCode::RUNTIME_ERROR,
62 "ArmNN ClDepthwiseConv2dWorkload does not support non constant bias."};
63 }
David Beck5eec11d2018-10-04 15:43:17 +010064 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
Cathal Corbett4452baf2022-05-13 09:55:59 +010065 aclBiasesInfo.set_are_values_constant(biases.value().IsConstant());
telsoa01c577f2c2018-08-31 09:22:23 +010066 optionalAclBiasesInfo = &aclBiasesInfo;
67 }
68
69 const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
Pablo Tellof0bd6832019-04-26 17:58:13 +010070 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
71 descriptor.m_DilationX,
72 descriptor.m_DilationY);
telsoa01c577f2c2018-08-31 09:22:23 +010073
Mike Kelly07810fc2020-11-12 10:58:48 +000074 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
75 activationDescriptor);
76
telsoa01c577f2c2018-08-31 09:22:23 +010077 return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo,
78 &aclWeightsInfo,
79 optionalAclBiasesInfo,
80 &aclOutputInfo,
81 aclPadStrideInfo,
Pablo Tellof0bd6832019-04-26 17:58:13 +010082 aclDepthMultiplier,
Mike Kelly07810fc2020-11-12 10:58:48 +000083 activationInfo,
Pablo Tellof0bd6832019-04-26 17:58:13 +010084 aclDilationInfo);
85
telsoa01c577f2c2018-08-31 09:22:23 +010086}
87
Matthew Benthamd8777392018-10-08 09:38:55 +010088ClDepthwiseConvolutionWorkload::ClDepthwiseConvolutionWorkload(
telsoa01c577f2c2018-08-31 09:22:23 +010089 const DepthwiseConvolution2dQueueDescriptor& descriptor,
Sadik Armagane9444752020-12-02 11:28:58 +000090 const WorkloadInfo& info,
91 const arm_compute::CLCompileContext& clCompileContext)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000092 : ClBaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info)
telsoa01c577f2c2018-08-31 09:22:23 +010093{
Cathal Corbett4b19d222022-05-11 20:12:17 +010094 m_Data.ValidateInputsOutputs("ClDepthwiseConv2dWorkload", descriptor.m_Parameters.GetNumInputs(), 1);
Matteo Martincigh747ef822018-12-18 09:26:39 +000095
Cathal Corbett4b19d222022-05-11 20:12:17 +010096 arm_compute::ICLTensor& input = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
97 arm_compute::ICLTensor& output = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
98 arm_compute::ICLTensor& weights = PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
Teresa Charlinee1497c2023-03-30 13:56:34 +010099 weights.info()->set_are_values_constant(info.m_InputTensorInfos[1].IsConstant());
Cathal Corbett4b19d222022-05-11 20:12:17 +0100100 arm_compute::ITensorInfo* weightsInfo = weights.info();
101 arm_compute::ITensorInfo* inputInfo = input.info();
102 auto weightsShape = weightsInfo->tensor_shape();
103 auto inputShape = inputInfo->tensor_shape();
telsoa01c577f2c2018-08-31 09:22:23 +0100104
Cathal Corbett4b19d222022-05-11 20:12:17 +0100105 // The PermuteDepthwiseConv2dWeights backend optimization has been performed,
106 // converting weights to have the same data layout as input.
107 unsigned int depthMultiplier =
108 ComputeDepthwiseConv2dDepthMultiplier(m_Data.m_Parameters.m_DataLayout, weightsShape, inputShape);
109
110 arm_compute::ICLTensor* bias = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +0100111 if (m_Data.m_Parameters.m_BiasEnabled)
112 {
Cathal Corbett4b19d222022-05-11 20:12:17 +0100113 bias = &PolymorphicDowncast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
Teresa Charlinee1497c2023-03-30 13:56:34 +0100114 bias->info()->set_are_values_constant(info.m_InputTensorInfos[2].IsConstant());
TeresaARMacedd852023-05-11 15:16:39 +0000115 // We do not support dynamic bias
116 ARMNN_ASSERT(info.m_InputTensorInfos[2].IsConstant() == true);
telsoa01c577f2c2018-08-31 09:22:23 +0100117 }
118
Pablo Tellof0bd6832019-04-26 17:58:13 +0100119 const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(
120 m_Data.m_Parameters.m_DilationX,
121 m_Data.m_Parameters.m_DilationY);
122
Nikhil Rajcec6b652018-10-12 13:51:57 +0100123 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
124 input.info()->set_data_layout(aclDataLayout);
Cathal Corbett4b19d222022-05-11 20:12:17 +0100125 weights.info()->set_data_layout(aclDataLayout);
Nikhil Rajcec6b652018-10-12 13:51:57 +0100126 output.info()->set_data_layout(aclDataLayout);
127
Aron Virginas-Tar6f3785d2019-07-22 15:30:22 +0100128 arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters);
Pablo Tellof0bd6832019-04-26 17:58:13 +0100129
Mike Kelly07810fc2020-11-12 10:58:48 +0000130 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
131
Aron Virginas-Tarf4c502f2019-11-14 16:21:38 +0000132 m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>();
telsoa01c577f2c2018-08-31 09:22:23 +0100133
Kevin May9f6862d2021-10-22 15:42:28 +0100134 {
135 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "ClDepthwiseConvolutionWorkload_configure");
136 static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure(
137 clCompileContext,
138 &input,
Cathal Corbett4b19d222022-05-11 20:12:17 +0100139 &weights,
140 bias,
Kevin May9f6862d2021-10-22 15:42:28 +0100141 &output,
142 padStrideInfo,
143 depthMultiplier,
144 activationInfo,
145 aclDilationInfo);
146 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100147 ARMNN_ASSERT(m_DepthwiseConvolutionLayer);
Teresa Charlinee1497c2023-03-30 13:56:34 +0100148
149 // Add details for profiling output
150 WorkloadInfo detailsInfo;
151
152 detailsInfo.m_InputTensorInfos = info.m_InputTensorInfos;
153 detailsInfo.m_OutputTensorInfos = info.m_OutputTensorInfos;
154 detailsInfo.m_WeightsTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[1]);
155
156 if (descriptor.m_Parameters.m_BiasEnabled)
157 {
158 detailsInfo.m_BiasTensorInfo = armnn::Optional<armnn::TensorInfo>(info.m_InputTensorInfos[2]);
159 }
160
161 // Report Profiling Details
162 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClDepthwiseConvolutionWorkload_Construct",
163 descriptor.m_Parameters,
164 detailsInfo,
165 GetGuid());
telsoa01c577f2c2018-08-31 09:22:23 +0100166}
167
Matthew Benthamd8777392018-10-08 09:38:55 +0100168void ClDepthwiseConvolutionWorkload::Execute() const
169{
Cathal Corbett4b19d222022-05-11 20:12:17 +0100170 ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClDepthwiseConvolutionWorkload_Execute", GetGuid());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100171 ARMNN_ASSERT(m_DepthwiseConvolutionLayer);
Matthew Benthamd8777392018-10-08 09:38:55 +0100172
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100173 RunClFunction(*m_DepthwiseConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8777392018-10-08 09:38:55 +0100174}
telsoa01c577f2c2018-08-31 09:22:23 +0100175
176} // namespace armnn