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 | |
Matthew Bentham | c48ac8c | 2018-12-12 16:15:59 +0000 | [diff] [blame] | 6 | #include "NeonBatchNormalizationWorkload.hpp" |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 7 | |
| 8 | #include "NeonWorkloadUtils.hpp" |
| 9 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 10 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 11 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 12 | #include <backendsCommon/CpuTensorHandle.hpp> |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 13 | |
| 14 | #include <arm_compute/runtime/NEON/functions/NEBatchNormalizationLayer.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | |
| 16 | namespace armnn |
| 17 | { |
| 18 | using namespace armcomputetensorutils; |
| 19 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | |
| 21 | arm_compute::Status NeonBatchNormalizationValidate(const TensorInfo& input, |
| 22 | const TensorInfo& output, |
| 23 | const TensorInfo& mean, |
| 24 | const TensorInfo& var, |
| 25 | const TensorInfo& beta, |
| 26 | const TensorInfo& gamma, |
| 27 | const BatchNormalizationDescriptor& descriptor) |
| 28 | { |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 29 | const arm_compute::TensorInfo aclInputInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 30 | armcomputetensorutils::BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 31 | const arm_compute::TensorInfo aclOutputInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 32 | armcomputetensorutils::BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 33 | const arm_compute::TensorInfo aclMeanInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 34 | armcomputetensorutils::BuildArmComputeTensorInfo(mean, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 35 | const arm_compute::TensorInfo aclVarInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 36 | armcomputetensorutils::BuildArmComputeTensorInfo(var, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 37 | const arm_compute::TensorInfo aclBetaInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 38 | armcomputetensorutils::BuildArmComputeTensorInfo(beta, descriptor.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 39 | const arm_compute::TensorInfo aclGammaInfo = |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 40 | armcomputetensorutils::BuildArmComputeTensorInfo(gamma, descriptor.m_DataLayout); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 41 | |
| 42 | return arm_compute::NEBatchNormalizationLayer::validate(&aclInputInfo, |
| 43 | &aclOutputInfo, |
| 44 | &aclMeanInfo, |
| 45 | &aclVarInfo, |
| 46 | &aclBetaInfo, |
| 47 | &aclGammaInfo, |
| 48 | descriptor.m_Eps); |
| 49 | } |
| 50 | |
Matthew Bentham | c48ac8c | 2018-12-12 16:15:59 +0000 | [diff] [blame] | 51 | NeonBatchNormalizationWorkload::NeonBatchNormalizationWorkload( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 52 | const BatchNormalizationQueueDescriptor& descriptor, const WorkloadInfo& info) |
Matthew Bentham | c48ac8c | 2018-12-12 16:15:59 +0000 | [diff] [blame] | 53 | : BaseWorkload<BatchNormalizationQueueDescriptor>(descriptor, info) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 54 | { |
Matthew Bentham | c48ac8c | 2018-12-12 16:15:59 +0000 | [diff] [blame] | 55 | m_Data.ValidateInputsOutputs("NeonBatchNormalizationWorkload", 1, 1); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 57 | arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 58 | arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | |
Matthew Bentham | 8800c00 | 2018-11-19 13:19:28 +0000 | [diff] [blame] | 60 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
Nikhil Raj | d134093 | 2018-10-18 14:27:50 +0100 | [diff] [blame] | 61 | input.info()->set_data_layout(aclDataLayout); |
| 62 | output.info()->set_data_layout(aclDataLayout); |
| 63 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | m_Mean = std::make_unique<arm_compute::Tensor>(); |
| 65 | BuildArmComputeTensor(*m_Mean, m_Data.m_Mean->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 66 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 67 | m_Variance = std::make_unique<arm_compute::Tensor>(); |
| 68 | BuildArmComputeTensor(*m_Variance, m_Data.m_Variance->GetTensorInfo()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 69 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 70 | m_Gamma = std::make_unique<arm_compute::Tensor>(); |
| 71 | BuildArmComputeTensor(*m_Gamma, m_Data.m_Gamma->GetTensorInfo()); |
| 72 | |
| 73 | m_Beta = std::make_unique<arm_compute::Tensor>(); |
| 74 | BuildArmComputeTensor(*m_Beta, m_Data.m_Beta->GetTensorInfo()); |
| 75 | |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 76 | auto layer = std::make_unique<arm_compute::NEBatchNormalizationLayer>(); |
| 77 | layer->configure(&input, |
| 78 | &output, |
| 79 | m_Mean.get(), |
| 80 | m_Variance.get(), |
| 81 | m_Beta.get(), |
| 82 | m_Gamma.get(), |
| 83 | m_Data.m_Parameters.m_Eps); |
| 84 | m_Layer.reset(layer.release()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 85 | |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 86 | InitializeArmComputeTensorData(*m_Mean, m_Data.m_Mean); |
| 87 | InitializeArmComputeTensorData(*m_Variance, m_Data.m_Variance); |
| 88 | InitializeArmComputeTensorData(*m_Gamma, m_Data.m_Gamma); |
| 89 | InitializeArmComputeTensorData(*m_Beta, m_Data.m_Beta); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 90 | |
| 91 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 92 | // delete all the input tensors that will no longer be needed |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 93 | m_Layer->prepare(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 94 | FreeUnusedTensors(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Matthew Bentham | c48ac8c | 2018-12-12 16:15:59 +0000 | [diff] [blame] | 97 | void NeonBatchNormalizationWorkload::Execute() const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 98 | { |
Matthew Bentham | c48ac8c | 2018-12-12 16:15:59 +0000 | [diff] [blame] | 99 | ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonBatchNormalizationWorkload_Execute"); |
Matthew Bentham | d80a712 | 2019-01-08 17:52:37 +0000 | [diff] [blame] | 100 | m_Layer->run(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Matthew Bentham | c48ac8c | 2018-12-12 16:15:59 +0000 | [diff] [blame] | 103 | void NeonBatchNormalizationWorkload::FreeUnusedTensors() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 104 | { |
| 105 | FreeTensorIfUnused(m_Mean); |
| 106 | FreeTensorIfUnused(m_Variance); |
| 107 | FreeTensorIfUnused(m_Gamma); |
| 108 | FreeTensorIfUnused(m_Beta); |
| 109 | } |
| 110 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | } //namespace armnn |