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