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 | |
| 33 | arm_compute::TensorInfo aclBiasesInfo; |
| 34 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 35 | |
| 36 | if (descriptor.m_BiasEnabled) |
| 37 | { |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 38 | BOOST_ASSERT(biases.has_value()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 39 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 40 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 41 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 42 | } |
| 43 | |
| 44 | arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor); |
| 45 | |
| 46 | return arm_compute::CLConvolutionLayer::validate(&aclInputInfo, |
| 47 | &aclWeightsInfo, |
| 48 | optionalAclBiasesInfo, |
| 49 | &aclOutputInfo, |
| 50 | layerInfo); |
| 51 | } |
| 52 | |
| 53 | ClConvolution2dWorkload::ClConvolution2dWorkload(const Convolution2dQueueDescriptor& descriptor, |
| 54 | const WorkloadInfo& info, std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) |
| 55 | : BaseWorkload<Convolution2dQueueDescriptor>(descriptor, info) |
| 56 | , m_ConvolutionLayer(memoryManager) |
| 57 | { |
| 58 | // todo: check tensor shapes match. |
| 59 | const TensorInfo& weightInfo = m_Data.m_Weight->GetTensorInfo(); |
| 60 | |
| 61 | m_KernelTensor = std::make_unique<arm_compute::CLTensor>(); |
| 62 | BuildArmComputeTensor(*m_KernelTensor, weightInfo, m_Data.m_Parameters.m_DataLayout); |
| 63 | |
| 64 | arm_compute::PadStrideInfo padStrideInfo(m_Data.m_Parameters.m_StrideX, |
| 65 | m_Data.m_Parameters.m_StrideY, |
| 66 | m_Data.m_Parameters.m_PadLeft, |
| 67 | m_Data.m_Parameters.m_PadRight, |
| 68 | m_Data.m_Parameters.m_PadTop, |
| 69 | m_Data.m_Parameters.m_PadBottom, |
| 70 | arm_compute::DimensionRoundingType::FLOOR); |
| 71 | |
| 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 | |
| 87 | m_ConvolutionLayer.configure(&input, |
| 88 | m_KernelTensor.get(), |
| 89 | m_BiasTensor.get(), |
| 90 | &output, |
| 91 | padStrideInfo); |
| 92 | |
| 93 | InitializeArmComputeClTensorData(*m_KernelTensor, m_Data.m_Weight); |
| 94 | |
| 95 | if (m_BiasTensor) |
| 96 | { |
| 97 | InitializeArmComputeClTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 98 | } |
| 99 | |
| 100 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 101 | // delete all the input tensors that will no longer be needed |
| 102 | m_ConvolutionLayer.prepare(); |
| 103 | FreeUnusedTensors(); |
| 104 | } |
| 105 | |
| 106 | void ClConvolution2dWorkload::Execute() const |
| 107 | { |
| 108 | ARMNN_SCOPED_PROFILING_EVENT_CL("ClConvolution2dWorkload_Execute"); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 109 | RunClFunction(m_ConvolutionLayer, CHECK_LOCATION()); |
Matthew Bentham | d806792 | 2018-10-03 17:18:04 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void ClConvolution2dWorkload::FreeUnusedTensors() |
| 113 | { |
| 114 | FreeTensorIfUnused(m_KernelTensor); |
| 115 | FreeTensorIfUnused(m_BiasTensor); |
| 116 | } |
| 117 | |
| 118 | } //namespace armnn |