telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 6 | #include "ClDepthwiseConvolutionWorkload.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 7 | |
| 8 | #include "TypeUtils.hpp" |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 9 | #include "ClWorkloadUtils.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 11 | #include <aclCommon/ArmComputeUtils.hpp> |
| 12 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 13 | #include <cl/ClTensorHandle.hpp> |
| 14 | #include <backendsCommon/CpuTensorHandle.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 16 | #include <arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h> |
| 17 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
| 20 | |
| 21 | using namespace armcomputetensorutils; |
| 22 | |
| 23 | arm_compute::Status ClDepthwiseConvolutionWorkloadValidate(const TensorInfo& input, |
| 24 | const TensorInfo& output, |
| 25 | const DepthwiseConvolution2dDescriptor& descriptor, |
| 26 | const TensorInfo& weights, |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 27 | const Optional<TensorInfo>& biases) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 28 | { |
Nikhil Raj | a05c210 | 2018-09-25 16:16:13 +0100 | [diff] [blame] | 29 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 30 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
| 31 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | |
| 33 | arm_compute::TensorInfo aclBiasesInfo; |
| 34 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 35 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | if (descriptor.m_BiasEnabled) |
| 37 | { |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 38 | BOOST_ASSERT(biases.has_value()); |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 39 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 40 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 41 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 42 | } |
| 43 | |
| 44 | const arm_compute::PadStrideInfo aclPadStrideInfo = BuildArmComputePadStrideInfo(descriptor); |
| 45 | const unsigned int aclDepthMultiplier = weights.GetShape()[0]; |
| 46 | |
| 47 | return arm_compute::CLDepthwiseConvolutionLayer::validate(&aclInputInfo, |
| 48 | &aclWeightsInfo, |
| 49 | optionalAclBiasesInfo, |
| 50 | &aclOutputInfo, |
| 51 | aclPadStrideInfo, |
| 52 | aclDepthMultiplier); |
| 53 | } |
| 54 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 55 | ClDepthwiseConvolutionWorkload::ClDepthwiseConvolutionWorkload( |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | const DepthwiseConvolution2dQueueDescriptor& descriptor, |
| 57 | const WorkloadInfo& info) |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 58 | : BaseWorkload<DepthwiseConvolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | { |
| 60 | auto& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
| 61 | |
| 62 | m_KernelTensor = std::make_unique<arm_compute::CLTensor>(); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 63 | BuildArmComputeTensor(*m_KernelTensor, weightInfo, m_Data.m_Parameters.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | |
| 65 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 66 | { |
| 67 | m_BiasTensor = std::make_unique<arm_compute::CLTensor>(); |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 68 | BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | arm_compute::PadStrideInfo padStrideInfo(m_Data.m_Parameters.m_StrideX, |
| 72 | m_Data.m_Parameters.m_StrideY, |
| 73 | m_Data.m_Parameters.m_PadLeft, |
| 74 | m_Data.m_Parameters.m_PadRight, |
| 75 | m_Data.m_Parameters.m_PadTop, |
| 76 | m_Data.m_Parameters.m_PadBottom, |
| 77 | arm_compute::DimensionRoundingType::FLOOR); |
| 78 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 79 | std::string name = std::string("ClDepthwiseConvolutionWorkload"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | m_Data.ValidateInputsOutputs(name, 1, 1); |
| 81 | |
| 82 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 83 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 84 | |
Nikhil Raj | cec6b65 | 2018-10-12 13:51:57 +0100 | [diff] [blame] | 85 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 86 | input.info()->set_data_layout(aclDataLayout); |
| 87 | output.info()->set_data_layout(aclDataLayout); |
| 88 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 89 | const unsigned int depthMultiplier = weightInfo.GetShape()[0]; |
| 90 | |
saoste01 | 9c6f3af | 2018-11-05 12:30:23 +0000 | [diff] [blame] | 91 | const unsigned int widthIndex = (m_Data.m_Parameters.m_DataLayout == DataLayout::NCHW) ? 3 : 2; |
| 92 | const unsigned int heightIndex = (m_Data.m_Parameters.m_DataLayout == DataLayout::NCHW) ? 2 : 1; |
| 93 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 94 | //Check for optimisation opportunities. |
saoste01 | 9c6f3af | 2018-11-05 12:30:23 +0000 | [diff] [blame] | 95 | bool use3x3Optimisation = (weightInfo.GetShape()[widthIndex] == 3) && (weightInfo.GetShape()[heightIndex] == 3); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 96 | if (use3x3Optimisation) |
| 97 | { |
| 98 | m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer3x3>(); |
| 99 | static_cast<arm_compute::CLDepthwiseConvolutionLayer3x3*>(m_DepthwiseConvolutionLayer.get())->configure( |
| 100 | &input, |
| 101 | m_KernelTensor.get(), |
| 102 | m_BiasTensor.get(), |
| 103 | &output, |
| 104 | padStrideInfo, |
| 105 | depthMultiplier); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | m_DepthwiseConvolutionLayer = std::make_unique<arm_compute::CLDepthwiseConvolutionLayer>(); |
| 110 | static_cast<arm_compute::CLDepthwiseConvolutionLayer*>(m_DepthwiseConvolutionLayer.get())->configure( |
| 111 | &input, |
| 112 | m_KernelTensor.get(), |
| 113 | m_BiasTensor.get(), |
| 114 | &output, |
| 115 | padStrideInfo, |
| 116 | depthMultiplier); |
| 117 | } |
| 118 | |
| 119 | BOOST_ASSERT(m_DepthwiseConvolutionLayer); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 120 | |
| 121 | InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight); |
| 122 | |
| 123 | if (m_BiasTensor) |
| 124 | { |
| 125 | InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 126 | } |
| 127 | |
| 128 | m_DepthwiseConvolutionLayer->prepare(); |
| 129 | FreeUnusedTensors(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 132 | void ClDepthwiseConvolutionWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | { |
| 134 | FreeTensorIfUnused(m_KernelTensor); |
| 135 | FreeTensorIfUnused(m_BiasTensor); |
| 136 | } |
| 137 | |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 138 | void ClDepthwiseConvolutionWorkload::Execute() const |
| 139 | { |
| 140 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClDepthwiseConvolutionWorkload_Execute"); |
| 141 | BOOST_ASSERT(m_DepthwiseConvolutionLayer); |
| 142 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 143 | RunClFunction(*m_DepthwiseConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d877739 | 2018-10-08 09:38:55 +0100 | [diff] [blame] | 144 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 145 | |
| 146 | } // namespace armnn |