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> |
| 15 | #include <backendsCommon/CpuTensorHandle.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, |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 27 | const Optional<TensorInfo>& biases) |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 28 | { |
| 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); |
| 32 | |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 33 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX, |
| 34 | descriptor.m_DilationY); |
| 35 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 36 | arm_compute::TensorInfo aclBiasesInfo; |
| 37 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 38 | |
| 39 | if (descriptor.m_BiasEnabled) |
| 40 | { |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 41 | BOOST_ASSERT(biases.has_value()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 42 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 43 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 44 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 45 | } |
| 46 | |
| 47 | arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor); |
| 48 | |
| 49 | return arm_compute::CLConvolutionLayer::validate(&aclInputInfo, |
| 50 | &aclWeightsInfo, |
| 51 | optionalAclBiasesInfo, |
| 52 | &aclOutputInfo, |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 53 | layerInfo, |
| 54 | arm_compute::WeightsInfo(), |
| 55 | aclDilationInfo); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor, |
| 59 | const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) |
| 60 | : BaseWorkload<Convolution2dQueueDescriptor>(descriptor, info) |
| 61 | , m_ConvolutionLayer(memoryManager) |
| 62 | { |
| 63 | // todo: check tensor shapes match. |
| 64 | const TensorInfo& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
| 65 | |
| 66 | m_KernelTensor = std::make_unique<arm_compute::CLTensor>(); |
| 67 | BuildArmComputeTensor(*m_KernelTensor, weightInfo, m_Data.m_Parameters.m_DataLayout); |
| 68 | |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 69 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX, |
| 70 | m_Data.m_Parameters.m_DilationY); |
| 71 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 72 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 73 | { |
| 74 | m_BiasTensor = std::make_unique<arm_compute::CLTensor>(); |
| 75 | BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout); |
| 76 | } |
| 77 | |
| 78 | m_Data.ValidateInputsOutputs("ClConvolution2dWorkload", 1, 1); |
| 79 | |
| 80 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 81 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 82 | |
| 83 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 84 | input.info()->set_data_layout(aclDataLayout); |
| 85 | output.info()->set_data_layout(aclDataLayout); |
| 86 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 87 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
| 88 | |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 89 | m_ConvolutionLayer.configure(&input, |
| 90 | m_KernelTensor.get(), |
| 91 | m_BiasTensor.get(), |
| 92 | &output, |
Jan Eilers | 4b961d3 | 2019-07-11 09:19:35 +0100 | [diff] [blame] | 93 | padStrideInfo, |
| 94 | arm_compute::WeightsInfo(), |
| 95 | aclDilationInfo); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 96 | |
| 97 | InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight); |
| 98 | |
| 99 | if (m_BiasTensor) |
| 100 | { |
| 101 | InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 102 | } |
| 103 | |
| 104 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 105 | // delete all the input tensors that will no longer be needed |
| 106 | m_ConvolutionLayer.prepare(); |
| 107 | FreeUnusedTensors(); |
| 108 | } |
| 109 | |
| 110 | void ClConvolution2dWorkload::Execute() const |
| 111 | { |
| 112 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClConvolution2dWorkload_Execute"); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 113 | RunClFunction(m_ConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void ClConvolution2dWorkload::FreeUnusedTensors() |
| 117 | { |
| 118 | FreeTensorIfUnused(m_KernelTensor); |
| 119 | FreeTensorIfUnused(m_BiasTensor); |
| 120 | } |
| 121 | |
| 122 | } //namespace armnn |