blob: 1745b8297ad506a1773eae64db5447feb0b1bf5c [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. 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
8#include "TypeUtils.hpp"
Matthew Benthamd8777392018-10-08 09:38:55 +01009#include "ClWorkloadUtils.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010010
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000011#include <aclCommon/ArmComputeUtils.hpp>
12#include <aclCommon/ArmComputeTensorUtils.hpp>
13#include <cl/ClTensorHandle.hpp>
14#include <backendsCommon/CpuTensorHandle.hpp>
Matteo Martincigh747ef822018-12-18 09:26:39 +000015#include <backendsCommon/WorkloadUtils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010016
Matthew Benthamd8777392018-10-08 09:38:55 +010017#include <arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h>
18
telsoa01c577f2c2018-08-31 09:22:23 +010019namespace armnn
20{
21
22using namespace armcomputetensorutils;
23
24arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo& input,
Matteo Martincigh747ef822018-12-18 09:26:39 +000025 const TensorInfo& output,
26 const DepthwiseConvolution2dDescriptor& descriptor,
27 const TensorInfo& weights,
28 const Optional<TensorInfo>& biases)
telsoa01c577f2c2018-08-31 09:22:23 +010029{
Matteo Martincigh747ef822018-12-18 09:26:39 +000030 const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
Nikhil Raja05c2102018-09-25 16:16:13 +010031 const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
Matteo Martincigh747ef822018-12-18 09:26:39 +000032
33 // ArmNN's weight format is [ M, I, H, W ]
34 const unsigned int aclDepthMultiplier = weights.GetShape()[0];
35
36 // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
37 // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
38 TensorInfo weightsPermuted = ConvertWeightTensorInfoFromArmnnToAcl(weights, descriptor.m_DataLayout);
39
40 // Convert the weights into the compute library format
41 const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weightsPermuted, descriptor.m_DataLayout);
telsoa01c577f2c2018-08-31 09:22:23 +010042
43 arm_compute::TensorInfo aclBiasesInfo;
44 arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr;
arovir01a6824102018-08-28 17:40:45 +010045
telsoa01c577f2c2018-08-31 09:22:23 +010046 if (descriptor.m_BiasEnabled)
47 {
David Beck5eec11d2018-10-04 15:43:17 +010048 BOOST_ASSERT(biases.has_value());
arovir01a6824102018-08-28 17:40:45 +010049
David Beck5eec11d2018-10-04 15:43:17 +010050 aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout);
telsoa01c577f2c2018-08-31 09:22:23 +010051 optionalAclBiasesInfo = &aclBiasesInfo;
52 }
53
54 const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor);
telsoa01c577f2c2018-08-31 09:22:23 +010055
56 return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo,
57 &aclWeightsInfo,
58 optionalAclBiasesInfo,
59 &aclOutputInfo,
60 aclPadStrideInfo,
61 aclDepthMultiplier);
62}
63
Matthew Benthamd8777392018-10-08 09:38:55 +010064ClDepthwiseConvolutionWorkload::ClDepthwiseConvolutionWorkload(
telsoa01c577f2c2018-08-31 09:22:23 +010065 const DepthwiseConvolution2dQueueDescriptor& descriptor,
66 const WorkloadInfo& info)
Matthew Benthamd8777392018-10-08 09:38:55 +010067 : BaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info)
telsoa01c577f2c2018-08-31 09:22:23 +010068{
Matteo Martincigh747ef822018-12-18 09:26:39 +000069 // Allocate a buffer for the swizzling of the weight tensor
70 std::unique_ptr<unsigned char[]> permuteBuffer(new unsigned char[m_Data.m_Weight->GetTensorInfo().GetNumBytes()]);
telsoa01c577f2c2018-08-31 09:22:23 +010071
Matteo Martincigh747ef822018-12-18 09:26:39 +000072 // Convert the weight format from ArmNN's [ M, I, H, W ] (does NOT depend on the data layout) to either
73 // [ 1, H, W, I * M ] (if NHWC) or [ 1, I * M, H, W ] (if NCHW), as required by the compute library
74 ConstTensor weightPermuted = ConvertWeightTensorFromArmnnToAcl(m_Data.m_Weight,
75 m_Data.m_Parameters.m_DataLayout,
76 permuteBuffer.get());
77
78 // Convert the weights into the compute library format
telsoa01c577f2c2018-08-31 09:22:23 +010079 m_KernelTensor = std::make_unique<arm_compute::CLTensor>();
Matteo Martincigh747ef822018-12-18 09:26:39 +000080 BuildArmComputeTensor(*m_KernelTensor, weightPermuted.GetInfo(), m_Data.m_Parameters.m_DataLayout);
telsoa01c577f2c2018-08-31 09:22:23 +010081
82 if (m_Data.m_Parameters.m_BiasEnabled)
83 {
84 m_BiasTensor = std::make_unique<arm_compute::CLTensor>();
Nikhil Rajcec6b652018-10-12 13:51:57 +010085 BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout);
telsoa01c577f2c2018-08-31 09:22:23 +010086 }
87
88 arm_compute::PadStrideInfo padStrideInfo(m_Data.m_Parameters.m_StrideX,
89 m_Data.m_Parameters.m_StrideY,
90 m_Data.m_Parameters.m_PadLeft,
91 m_Data.m_Parameters.m_PadRight,
92 m_Data.m_Parameters.m_PadTop,
93 m_Data.m_Parameters.m_PadBottom,
94 arm_compute::DimensionRoundingType::FLOOR);
95
Matthew Benthamd8777392018-10-08 09:38:55 +010096 std::string name = std::string("ClDepthwiseConvolutionWorkload");
telsoa01c577f2c2018-08-31 09:22:23 +010097 m_Data.ValidateInputsOutputs(name, 1, 1);
98
99 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
100 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
101
Nikhil Rajcec6b652018-10-12 13:51:57 +0100102 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
103 input.info()->set_data_layout(aclDataLayout);
104 output.info()->set_data_layout(aclDataLayout);
105
Matteo Martincigh747ef822018-12-18 09:26:39 +0000106 // ArmNN's weight format is [ M, I, H, W ]
107 auto& weightInfo = m_Data.m_Weight->GetTensorInfo();
108
109 // Get the depth multiplier
telsoa01c577f2c2018-08-31 09:22:23 +0100110 const unsigned int depthMultiplier = weightInfo.GetShape()[0];
111
Matteo Martincigh747ef822018-12-18 09:26:39 +0000112 // Check for optimisation opportunities.
113 bool use3x3Optimisation = (weightInfo.GetShape()[2] == 3) && (weightInfo.GetShape()[3] == 3);
telsoa01c577f2c2018-08-31 09:22:23 +0100114 if (use3x3Optimisation)
115 {
116 m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer3x3>();
117 static_cast<arm_compute::CLDepthwiseConvolutionLayer3x3*>(m_DepthwiseConvolutionLayer.get())->configure(
118 &input,
119 m_KernelTensor.get(),
120 m_BiasTensor.get(),
121 &output,
122 padStrideInfo,
123 depthMultiplier);
124 }
125 else
126 {
127 m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>();
128 static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure(
129 &input,
130 m_KernelTensor.get(),
131 m_BiasTensor.get(),
132 &output,
133 padStrideInfo,
134 depthMultiplier);
135 }
136
137 BOOST_ASSERT(m_DepthwiseConvolutionLayer);
Matthew Benthamd8777392018-10-08 09:38:55 +0100138
Matteo Martincigh747ef822018-12-18 09:26:39 +0000139 ScopedCpuTensorHandle weightsPermutedHandle(weightPermuted);
140 InitializeArmComputeClTensorData(*m_KernelTensor, &weightsPermutedHandle);
Matthew Benthamd8777392018-10-08 09:38:55 +0100141
142 if (m_BiasTensor)
143 {
144 InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias);
145 }
146
147 m_DepthwiseConvolutionLayer->prepare();
148 FreeUnusedTensors();
telsoa01c577f2c2018-08-31 09:22:23 +0100149}
150
Matthew Benthamd8777392018-10-08 09:38:55 +0100151void ClDepthwiseConvolutionWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100152{
153 FreeTensorIfUnused(m_KernelTensor);
154 FreeTensorIfUnused(m_BiasTensor);
155}
156
Matthew Benthamd8777392018-10-08 09:38:55 +0100157void ClDepthwiseConvolutionWorkload::Execute() const
158{
159 ARMNN_SCOPED_PROFILING_EVENT_CL("ClDepthwiseConvolutionWorkload_Execute");
160 BOOST_ASSERT(m_DepthwiseConvolutionLayer);
161
Aron Virginas-Tara8e06ed2018-10-19 16:46:15 +0100162 RunClFunction(*m_DepthwiseConvolutionLayer, CHECK_LOCATION());
Matthew Benthamd8777392018-10-08 09:38:55 +0100163}
telsoa01c577f2c2018-08-31 09:22:23 +0100164
165} // namespace armnn