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 | |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 32 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX, |
| 33 | descriptor.m_DilationY); |
| 34 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 35 | arm_compute::TensorInfo aclBiasesInfo; |
| 36 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 37 | |
| 38 | if (descriptor.m_BiasEnabled) |
| 39 | { |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 40 | BOOST_ASSERT(biases.has_value()); |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 41 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 42 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 43 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 44 | } |
| 45 | |
| 46 | arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor); |
| 47 | |
| 48 | return arm_compute::NEConvolutionLayer::validate(&aclInputInfo, |
| 49 | &aclWeightsInfo, |
| 50 | optionalAclBiasesInfo, |
| 51 | &aclOutputInfo, |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 52 | layerInfo, |
| 53 | arm_compute::WeightsInfo(), |
| 54 | aclDilationInfo); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 57 | NeonConvolution2dWorkload::NeonConvolution2dWorkload( |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | const Convolution2dQueueDescriptor& descriptor, const WorkloadInfo& info, |
| 59 | std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 60 | : BaseWorkload<Convolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | { |
| 62 | using arm_compute::NEDirectConvolutionLayer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 64 | m_Data.ValidateInputsOutputs("NeonConvolution2dWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 66 | // todo: check tensor shapes match. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 67 | |
Derek Lamberti | c81855f | 2019-06-13 17:34:19 +0100 | [diff] [blame] | 68 | arm_compute::ITensor& input = boost::polymorphic_downcast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 69 | arm_compute::ITensor& output = boost::polymorphic_downcast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 70 | |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 71 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 72 | input.info()->set_data_layout(aclDataLayout); |
| 73 | output.info()->set_data_layout(aclDataLayout); |
| 74 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 75 | m_KernelTensor = std::make_unique<arm_compute::Tensor>(); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 76 | 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] | 77 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 78 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 79 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | m_BiasTensor = std::make_unique<arm_compute::Tensor>(); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 81 | 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] | 82 | } |
| 83 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 84 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 85 | |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 86 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX, |
| 87 | m_Data.m_Parameters.m_DilationY); |
| 88 | |
narpra01 | fca75c3 | 2018-11-16 12:38:41 +0000 | [diff] [blame] | 89 | auto convolutionLayer = std::make_unique<arm_compute::NEConvolutionLayer>(memoryManager); |
| 90 | convolutionLayer->configure(&input, |
| 91 | m_KernelTensor.get(), |
| 92 | m_BiasTensor.get(), |
| 93 | &output, |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 94 | padStrideInfo, |
| 95 | arm_compute::WeightsInfo(), |
| 96 | aclDilationInfo); |
| 97 | |
narpra01 | fca75c3 | 2018-11-16 12:38:41 +0000 | [diff] [blame] | 98 | m_ConvolutionLayer.reset(convolutionLayer.release()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 99 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 100 | BOOST_ASSERT(m_ConvolutionLayer); |
| 101 | |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 102 | InitializeArmComputeTensorData(*m_KernelTensor, m_Data.m_Weight); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 103 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 104 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 105 | { |
| 106 | InitializeArmComputeTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 107 | } |
| 108 | |
| 109 | m_ConvolutionLayer->prepare(); |
| 110 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 113 | void NeonConvolution2dWorkload::Execute() const |
| 114 | { |
| 115 | ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonConvolution2dWorkload_Execute"); |
| 116 | m_ConvolutionLayer->run(); |
| 117 | } |
| 118 | |
| 119 | void NeonConvolution2dWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 120 | { |
| 121 | FreeTensorIfUnused(m_KernelTensor); |
| 122 | FreeTensorIfUnused(m_BiasTensor); |
| 123 | } |
| 124 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 125 | } //namespace armnn |