Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClConvolution2dWorkload.hpp" |
| 7 | |
| 8 | #include "ClWorkloadUtils.hpp" |
| 9 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | #include <cl/ClLayerSupport.hpp> |
| 11 | #include <cl/ClTensorHandle.hpp> |
| 12 | #include <cl/ClLayerSupport.hpp> |
| 13 | #include <aclCommon/ArmComputeUtils.hpp> |
| 14 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 15 | #include <backendsCommon/TensorHandle.hpp> |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 16 | |
| 17 | #include <arm_compute/runtime/CL/functions/CLConvolutionLayer.h> |
| 18 | |
| 19 | namespace armnn |
| 20 | { |
| 21 | using namespace armcomputetensorutils; |
| 22 | |
| 23 | arm_compute::Status ClConvolution2dWorkloadValidate(const TensorInfo& input, |
| 24 | const TensorInfo& output, |
| 25 | const Convolution2dDescriptor& descriptor, |
| 26 | const TensorInfo& weights, |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 27 | const Optional<TensorInfo>& biases, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 28 | bool isFastMathEnabled, |
| 29 | const ActivationDescriptor* activationDescriptor) |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 30 | { |
| 31 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 32 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
| 33 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout); |
| 34 | |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 35 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX, |
| 36 | descriptor.m_DilationY); |
| 37 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 38 | arm_compute::TensorInfo aclBiasesInfo; |
| 39 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 40 | |
| 41 | if (descriptor.m_BiasEnabled) |
| 42 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 43 | ARMNN_ASSERT(biases.has_value()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 44 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 45 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 46 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 47 | } |
| 48 | |
| 49 | arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor); |
| 50 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 51 | const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo( |
| 52 | activationDescriptor); |
| 53 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 54 | return arm_compute::CLConvolutionLayer::validate(&aclInputInfo, |
| 55 | &aclWeightsInfo, |
| 56 | optionalAclBiasesInfo, |
| 57 | &aclOutputInfo, |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 58 | layerInfo, |
| 59 | arm_compute::WeightsInfo(), |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 60 | aclDilationInfo, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 61 | activationInfo, |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 62 | isFastMathEnabled); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 66 | const WorkloadInfo& info, |
| 67 | std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager, |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 68 | const arm_compute::CLCompileContext& clCompileContext, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 69 | const bool isFastMathEnabled) |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 70 | : BaseWorkload<Convolution2dQueueDescriptor>(descriptor, info) |
| 71 | , m_ConvolutionLayer(memoryManager) |
| 72 | { |
| 73 | // todo: check tensor shapes match. |
| 74 | const TensorInfo& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
| 75 | |
| 76 | m_KernelTensor = std::make_unique<arm_compute::CLTensor>(); |
| 77 | BuildArmComputeTensor(*m_KernelTensor, weightInfo, m_Data.m_Parameters.m_DataLayout); |
| 78 | |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 79 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX, |
| 80 | m_Data.m_Parameters.m_DilationY); |
| 81 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 82 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 83 | { |
| 84 | m_BiasTensor = std::make_unique<arm_compute::CLTensor>(); |
| 85 | BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout); |
| 86 | } |
| 87 | |
| 88 | m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", 1, 1); |
| 89 | |
| 90 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 91 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 92 | |
| 93 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 94 | input.info()->set_data_layout(aclDataLayout); |
| 95 | output.info()->set_data_layout(aclDataLayout); |
| 96 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 97 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
| 98 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 99 | const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor); |
| 100 | |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 101 | m_ConvolutionLayer.configure(clCompileContext, |
| 102 | &input, |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 103 | m_KernelTensor.get(), |
| 104 | m_BiasTensor.get(), |
| 105 | &output, |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 106 | padStrideInfo, |
| 107 | arm_compute::WeightsInfo(), |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 108 | aclDilationInfo, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 109 | activationInfo, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 110 | isFastMathEnabled); |
| 111 | |
| 112 | m_ConvolutionMethod = |
| 113 | m_ConvolutionLayer.get_convolution_method(input.info(), |
| 114 | m_KernelTensor->info(), |
| 115 | output.info(), |
| 116 | padStrideInfo, |
| 117 | arm_compute::WeightsInfo(), |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 118 | activationInfo, |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 119 | arm_compute::CLScheduler::get().target(), |
| 120 | aclDilationInfo, |
| 121 | isFastMathEnabled); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 122 | |
| 123 | InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight); |
| 124 | |
| 125 | if (m_BiasTensor) |
| 126 | { |
| 127 | InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 128 | } |
| 129 | |
| 130 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 131 | // delete all the input tensors that will no longer be needed |
| 132 | m_ConvolutionLayer.prepare(); |
| 133 | FreeUnusedTensors(); |
| 134 | } |
| 135 | |
| 136 | void ClConvolution2dWorkload::Execute() const |
| 137 | { |
| 138 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClConvolution2dWorkload_Execute"); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 139 | RunClFunction(m_ConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 142 | arm_compute::ConvolutionMethod ClConvolution2dWorkload::GetConvolutionMethod() const |
| 143 | { |
| 144 | return m_ConvolutionMethod; |
| 145 | } |
| 146 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 147 | void ClConvolution2dWorkload::FreeUnusedTensors() |
| 148 | { |
| 149 | FreeTensorIfUnused(m_KernelTensor); |
| 150 | FreeTensorIfUnused(m_BiasTensor); |
| 151 | } |
| 152 | |
| 153 | } //namespace armnn |