telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [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 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 6 | #include "NeonConvolution2dWorkload.hpp" |
| 7 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 8 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 9 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame^] | 10 | #include <neon/workloads/NeonWorkloadUtils.hpp> |
| 11 | |
| 12 | #include <arm_compute/runtime/NEON/functions/NEConvolutionLayer.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | |
David Beck | 711fa31 | 2018-09-24 10:46:38 +0100 | [diff] [blame] | 14 | #include <armnn/Types.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 15 | #include <Half.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 17 | namespace armnn |
| 18 | { |
| 19 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 20 | using namespace armcomputetensorutils; |
| 21 | |
| 22 | arm_compute::Status NeonConvolution2dWorkloadValidate(const TensorInfo& input, |
| 23 | const TensorInfo& output, |
| 24 | const Convolution2dDescriptor& descriptor, |
| 25 | const TensorInfo& weights, |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 26 | const Optional<TensorInfo>& biases) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 27 | { |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 28 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 29 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
| 30 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout); |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 31 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 32 | arm_compute::TensorInfo aclBiasesInfo; |
| 33 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 34 | |
| 35 | if (descriptor.m_BiasEnabled) |
| 36 | { |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 37 | BOOST_ASSERT(biases.has_value()); |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 38 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 39 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 40 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 41 | } |
| 42 | |
| 43 | arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor); |
| 44 | |
| 45 | return arm_compute::NEConvolutionLayer::validate(&aclInputInfo, |
| 46 | &aclWeightsInfo, |
| 47 | optionalAclBiasesInfo, |
| 48 | &aclOutputInfo, |
| 49 | layerInfo); |
| 50 | } |
| 51 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 52 | NeonConvolution2dWorkload::NeonConvolution2dWorkload( |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 53 | const Convolution2dQueueDescriptor& descriptor, const WorkloadInfo& info, |
| 54 | std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 55 | : BaseWorkload<Convolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | { |
| 57 | using arm_compute::NEDirectConvolutionLayer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 58 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 59 | m_Data.ValidateInputsOutputs("NeonConvolution2dWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 61 | // todo: check tensor shapes match. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | |
| 63 | arm_compute::ITensor& input = boost::polymorphic_downcast<INeonTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 64 | arm_compute::ITensor& output = boost::polymorphic_downcast<INeonTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 65 | |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 66 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 67 | input.info()->set_data_layout(aclDataLayout); |
| 68 | output.info()->set_data_layout(aclDataLayout); |
| 69 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 70 | m_KernelTensor = std::make_unique<arm_compute::Tensor>(); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 71 | BuildArmComputeTensor(*m_KernelTensor, m_Data.m_Weight->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 72 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 74 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 75 | m_BiasTensor = std::make_unique<arm_compute::Tensor>(); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 76 | BuildArmComputeTensor(*m_BiasTensor, m_Data.m_Bias->GetTensorInfo(), m_Data.m_Parameters.m_DataLayout); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | arm_compute::PadStrideInfo padStrideInfo(m_Data.m_Parameters.m_StrideX, |
| 80 | m_Data.m_Parameters.m_StrideY, |
| 81 | m_Data.m_Parameters.m_PadLeft, |
| 82 | m_Data.m_Parameters.m_PadRight, |
| 83 | m_Data.m_Parameters.m_PadTop, |
| 84 | m_Data.m_Parameters.m_PadBottom, |
| 85 | arm_compute::DimensionRoundingType::FLOOR); |
| 86 | |
narpra01 | fca75c3 | 2018-11-16 12:38:41 +0000 | [diff] [blame] | 87 | auto convolutionLayer = std::make_unique<arm_compute::NEConvolutionLayer>(memoryManager); |
| 88 | convolutionLayer->configure(&input, |
| 89 | m_KernelTensor.get(), |
| 90 | m_BiasTensor.get(), |
| 91 | &output, |
| 92 | padStrideInfo); |
| 93 | m_ConvolutionLayer.reset(convolutionLayer.release()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 94 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 95 | BOOST_ASSERT(m_ConvolutionLayer); |
| 96 | |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 97 | InitializeArmComputeTensorData(*m_KernelTensor, m_Data.m_Weight); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 98 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 99 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 100 | { |
| 101 | InitializeArmComputeTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 102 | } |
| 103 | |
| 104 | m_ConvolutionLayer->prepare(); |
| 105 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 108 | void NeonConvolution2dWorkload::Execute() const |
| 109 | { |
| 110 | ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonConvolution2dWorkload_Execute"); |
| 111 | m_ConvolutionLayer->run(); |
| 112 | } |
| 113 | |
| 114 | void NeonConvolution2dWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 115 | { |
| 116 | FreeTensorIfUnused(m_KernelTensor); |
| 117 | FreeTensorIfUnused(m_BiasTensor); |
| 118 | } |
| 119 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 120 | } //namespace armnn |