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