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 <aclCommon/ArmComputeTensorUtils.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 9 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 10 | #include <backendsCommon/CpuTensorHandle.hpp> |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 11 | #include <neon/workloads/NeonWorkloadUtils.hpp> |
| 12 | |
| 13 | #include <arm_compute/runtime/NEON/functions/NEConvolutionLayer.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | |
David Beck | 711fa31 | 2018-09-24 10:46:38 +0100 | [diff] [blame] | 15 | #include <armnn/Types.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 16 | #include <Half.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
| 20 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 21 | using namespace armcomputetensorutils; |
| 22 | |
| 23 | arm_compute::Status NeonConvolution2dWorkloadValidate(const TensorInfo& input, |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 24 | const TensorInfo& output, |
| 25 | const Convolution2dDescriptor& descriptor, |
| 26 | const TensorInfo& weights, |
| 27 | const Optional<TensorInfo>& biases, |
| 28 | bool isFastMathEnabled) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 29 | { |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 30 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 31 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
| 32 | const arm_compute::TensorInfo aclWeightsInfo = BuildArmComputeTensorInfo(weights, descriptor.m_DataLayout); |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 33 | |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 34 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(descriptor.m_DilationX, |
| 35 | descriptor.m_DilationY); |
| 36 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 37 | arm_compute::TensorInfo aclBiasesInfo; |
| 38 | arm_compute::TensorInfo *optionalAclBiasesInfo = nullptr; |
| 39 | |
| 40 | if (descriptor.m_BiasEnabled) |
| 41 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 42 | ARMNN_ASSERT(biases.has_value()); |
arovir01 | a682410 | 2018-08-28 17:40:45 +0100 | [diff] [blame] | 43 | |
David Beck | 5eec11d | 2018-10-04 15:43:17 +0100 | [diff] [blame] | 44 | aclBiasesInfo = BuildArmComputeTensorInfo(biases.value(), descriptor.m_DataLayout); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 45 | optionalAclBiasesInfo = &aclBiasesInfo; |
| 46 | } |
| 47 | |
| 48 | arm_compute::PadStrideInfo layerInfo = BuildArmComputePadStrideInfo(descriptor); |
| 49 | |
| 50 | return arm_compute::NEConvolutionLayer::validate(&aclInputInfo, |
| 51 | &aclWeightsInfo, |
| 52 | optionalAclBiasesInfo, |
| 53 | &aclOutputInfo, |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 54 | layerInfo, |
| 55 | arm_compute::WeightsInfo(), |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 56 | aclDilationInfo, |
| 57 | arm_compute::ActivationLayerInfo(), |
| 58 | isFastMathEnabled); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 61 | NeonConvolution2dWorkload::NeonConvolution2dWorkload( |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame^] | 62 | const Convolution2dQueueDescriptor& descriptor, |
| 63 | const WorkloadInfo& info, |
| 64 | std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager, |
| 65 | const bool isFastMathEnabled) |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 66 | : BaseWorkload<Convolution2dQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 67 | { |
| 68 | using arm_compute::NEDirectConvolutionLayer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 69 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 70 | m_Data.ValidateInputsOutputs("NeonConvolution2dWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 71 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | // todo: check tensor shapes match. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 74 | arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 75 | arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 76 | |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 77 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 78 | input.info()->set_data_layout(aclDataLayout); |
| 79 | output.info()->set_data_layout(aclDataLayout); |
| 80 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | m_KernelTensor = std::make_unique<arm_compute::Tensor>(); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 82 | 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] | 83 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 84 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 85 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 86 | m_BiasTensor = std::make_unique<arm_compute::Tensor>(); |
Francis Murtagh | d59116e | 2018-10-04 16:03:07 +0100 | [diff] [blame] | 87 | 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] | 88 | } |
| 89 | |
Aron Virginas-Tar | 6f3785d | 2019-07-22 15:30:22 +0100 | [diff] [blame] | 90 | arm_compute::PadStrideInfo padStrideInfo = BuildArmComputePadStrideInfo(m_Data.m_Parameters); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 91 | |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 92 | const arm_compute::Size2D aclDilationInfo = BuildArmComputeSize2D(m_Data.m_Parameters.m_DilationX, |
| 93 | m_Data.m_Parameters.m_DilationY); |
| 94 | |
narpra01 | fca75c3 | 2018-11-16 12:38:41 +0000 | [diff] [blame] | 95 | auto convolutionLayer = std::make_unique<arm_compute::NEConvolutionLayer>(memoryManager); |
| 96 | convolutionLayer->configure(&input, |
| 97 | m_KernelTensor.get(), |
| 98 | m_BiasTensor.get(), |
| 99 | &output, |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 100 | padStrideInfo, |
| 101 | arm_compute::WeightsInfo(), |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame^] | 102 | aclDilationInfo, |
| 103 | arm_compute::ActivationLayerInfo(), |
| 104 | isFastMathEnabled); |
| 105 | |
| 106 | m_ConvolutionMethod = |
| 107 | convolutionLayer->get_convolution_method(input.info(), |
| 108 | m_KernelTensor->info(), |
| 109 | output.info(), |
| 110 | padStrideInfo, |
| 111 | arm_compute::WeightsInfo(), |
| 112 | aclDilationInfo, |
| 113 | arm_compute::ActivationLayerInfo(), |
| 114 | isFastMathEnabled); |
Jan Eilers | 59c6670 | 2019-07-11 10:22:36 +0100 | [diff] [blame] | 115 | |
narpra01 | fca75c3 | 2018-11-16 12:38:41 +0000 | [diff] [blame] | 116 | m_ConvolutionLayer.reset(convolutionLayer.release()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 117 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 118 | ARMNN_ASSERT(m_ConvolutionLayer); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 119 | |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 120 | InitializeArmComputeTensorData(*m_KernelTensor, m_Data.m_Weight); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 121 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 122 | if (m_Data.m_Parameters.m_BiasEnabled) |
| 123 | { |
| 124 | InitializeArmComputeTensorData(*m_BiasTensor, m_Data.m_Bias); |
| 125 | } |
| 126 | |
| 127 | m_ConvolutionLayer->prepare(); |
| 128 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 131 | void NeonConvolution2dWorkload::Execute() const |
| 132 | { |
| 133 | ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonConvolution2dWorkload_Execute"); |
| 134 | m_ConvolutionLayer->run(); |
| 135 | } |
| 136 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame^] | 137 | arm_compute::ConvolutionMethod NeonConvolution2dWorkload::GetConvolutionMethod() const |
| 138 | { |
| 139 | return m_ConvolutionMethod; |
| 140 | } |
| 141 | |
Nattapat Chaimanowong | 974b65f | 2018-10-15 15:07:34 +0100 | [diff] [blame] | 142 | void NeonConvolution2dWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | { |
| 144 | FreeTensorIfUnused(m_KernelTensor); |
| 145 | FreeTensorIfUnused(m_BiasTensor); |
| 146 | } |
| 147 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 148 | } //namespace armnn |