blob: 8e78846ee49276ca7df5e8ca8b4f5f560bf34797 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
Teresa Charlin588cbdf2022-01-19 15:55:37 +00002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
Matthew Benthamc48ac8c2018-12-12 16:15:59 +00006#include "NeonBatchNormalizationWorkload.hpp"
Matthew Benthamd80a7122019-01-08 17:52:37 +00007
8#include "NeonWorkloadUtils.hpp"
9
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000010#include <aclCommon/ArmComputeTensorUtils.hpp>
Mike Kelly07810fc2020-11-12 10:58:48 +000011#include <aclCommon/ArmComputeUtils.hpp>
12
Jan Eilersbb446e52020-04-02 13:56:54 +010013#include <armnn/utility/PolymorphicDowncast.hpp>
Mike Kelly07810fc2020-11-12 10:58:48 +000014
Colm Donelan0c479742021-12-10 12:43:54 +000015#include <armnn/backends/TensorHandle.hpp>
Matthew Benthamd80a7122019-01-08 17:52:37 +000016
17#include <arm_compute/runtime/NEON/functions/NEBatchNormalizationLayer.h>
telsoa014fcda012018-03-09 14:13:49 +000018
19namespace armnn
20{
21using namespace armcomputetensorutils;
22
telsoa01c577f2c2018-08-31 09:22:23 +010023
24arm_compute::Status NeonBatchNormalizationValidate(const TensorInfo& input,
25 const TensorInfo& output,
26 const TensorInfo& mean,
27 const TensorInfo& var,
28 const TensorInfo& beta,
29 const TensorInfo& gamma,
Mike Kelly07810fc2020-11-12 10:58:48 +000030 const BatchNormalizationDescriptor& descriptor,
31 const ActivationDescriptor* activationDescriptor)
telsoa01c577f2c2018-08-31 09:22:23 +010032{
Nikhil Rajd1340932018-10-18 14:27:50 +010033 const arm_compute::TensorInfo aclInputInfo =
Matthew Bentham8800c002018-11-19 13:19:28 +000034 armcomputetensorutils::BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
Nikhil Rajd1340932018-10-18 14:27:50 +010035 const arm_compute::TensorInfo aclOutputInfo =
Matthew Bentham8800c002018-11-19 13:19:28 +000036 armcomputetensorutils::BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
Nikhil Rajd1340932018-10-18 14:27:50 +010037 const arm_compute::TensorInfo aclMeanInfo =
Matthew Bentham8800c002018-11-19 13:19:28 +000038 armcomputetensorutils::BuildArmComputeTensorInfo(mean, descriptor.m_DataLayout);
Nikhil Rajd1340932018-10-18 14:27:50 +010039 const arm_compute::TensorInfo aclVarInfo =
Matthew Bentham8800c002018-11-19 13:19:28 +000040 armcomputetensorutils::BuildArmComputeTensorInfo(var, descriptor.m_DataLayout);
Nikhil Rajd1340932018-10-18 14:27:50 +010041 const arm_compute::TensorInfo aclBetaInfo =
Matthew Bentham8800c002018-11-19 13:19:28 +000042 armcomputetensorutils::BuildArmComputeTensorInfo(beta, descriptor.m_DataLayout);
Nikhil Rajd1340932018-10-18 14:27:50 +010043 const arm_compute::TensorInfo aclGammaInfo =
Matthew Bentham8800c002018-11-19 13:19:28 +000044 armcomputetensorutils::BuildArmComputeTensorInfo(gamma, descriptor.m_DataLayout);
telsoa01c577f2c2018-08-31 09:22:23 +010045
Mike Kelly07810fc2020-11-12 10:58:48 +000046 const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
47 activationDescriptor);
48
telsoa01c577f2c2018-08-31 09:22:23 +010049 return arm_compute::NEBatchNormalizationLayer::validate(&aclInputInfo,
50 &aclOutputInfo,
51 &aclMeanInfo,
52 &aclVarInfo,
53 &aclBetaInfo,
54 &aclGammaInfo,
Mike Kelly07810fc2020-11-12 10:58:48 +000055 descriptor.m_Eps,
56 activationInfo);
telsoa01c577f2c2018-08-31 09:22:23 +010057}
58
Matthew Benthamc48ac8c2018-12-12 16:15:59 +000059NeonBatchNormalizationWorkload::NeonBatchNormalizationWorkload(
telsoa014fcda012018-03-09 14:13:49 +000060 const BatchNormalizationQueueDescriptor& descriptor, const WorkloadInfo& info)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000061 : NeonBaseWorkload<BatchNormalizationQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000062{
Keith Davis2d0679f2021-08-05 11:35:00 +010063 // Report Profiling Details
64 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonBatchNormalizationWorkload_Construct",
65 descriptor.m_Parameters,
66 info,
67 this->GetGuid());
68
Matthew Benthamc48ac8c2018-12-12 16:15:59 +000069 m_Data.ValidateInputsOutputs("NeonBatchNormalizationWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000070
Jan Eilersbb446e52020-04-02 13:56:54 +010071 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
72 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa014fcda012018-03-09 14:13:49 +000073
Matthew Bentham8800c002018-11-19 13:19:28 +000074 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
Nikhil Rajd1340932018-10-18 14:27:50 +010075 input.info()->set_data_layout(aclDataLayout);
76 output.info()->set_data_layout(aclDataLayout);
77
telsoa01c577f2c2018-08-31 09:22:23 +010078 m_Mean = std::make_unique<arm_compute::Tensor>();
79 BuildArmComputeTensor(*m_Mean, m_Data.m_Mean->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000080
telsoa01c577f2c2018-08-31 09:22:23 +010081 m_Variance = std::make_unique<arm_compute::Tensor>();
82 BuildArmComputeTensor(*m_Variance, m_Data.m_Variance->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000083
telsoa01c577f2c2018-08-31 09:22:23 +010084 m_Gamma = std::make_unique<arm_compute::Tensor>();
85 BuildArmComputeTensor(*m_Gamma, m_Data.m_Gamma->GetTensorInfo());
86
87 m_Beta = std::make_unique<arm_compute::Tensor>();
88 BuildArmComputeTensor(*m_Beta, m_Data.m_Beta->GetTensorInfo());
89
Mike Kelly07810fc2020-11-12 10:58:48 +000090 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
91
Matthew Benthamd80a7122019-01-08 17:52:37 +000092 auto layer = std::make_unique<arm_compute::NEBatchNormalizationLayer>();
93 layer->configure(&input,
94 &output,
95 m_Mean.get(),
96 m_Variance.get(),
97 m_Beta.get(),
98 m_Gamma.get(),
Mike Kelly07810fc2020-11-12 10:58:48 +000099 m_Data.m_Parameters.m_Eps,
100 activationInfo);
Matthew Benthamd80a7122019-01-08 17:52:37 +0000101 m_Layer.reset(layer.release());
telsoa01c577f2c2018-08-31 09:22:23 +0100102
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +0100103 InitializeArmComputeTensorData(*m_Mean, m_Data.m_Mean);
104 InitializeArmComputeTensorData(*m_Variance, m_Data.m_Variance);
105 InitializeArmComputeTensorData(*m_Gamma, m_Data.m_Gamma);
106 InitializeArmComputeTensorData(*m_Beta, m_Data.m_Beta);
telsoa01c577f2c2018-08-31 09:22:23 +0100107
108 // Force Compute Library to perform the necessary copying and reshaping, after which
109 // delete all the input tensors that will no longer be needed
Matthew Benthamd80a7122019-01-08 17:52:37 +0000110 m_Layer->prepare();
telsoa01c577f2c2018-08-31 09:22:23 +0100111 FreeUnusedTensors();
telsoa014fcda012018-03-09 14:13:49 +0000112}
113
Matthew Benthamc48ac8c2018-12-12 16:15:59 +0000114void NeonBatchNormalizationWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000115{
Keith Davis2d0679f2021-08-05 11:35:00 +0100116 ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonBatchNormalizationWorkload_Execute", this->GetGuid());
Matthew Benthamd80a7122019-01-08 17:52:37 +0000117 m_Layer->run();
telsoa014fcda012018-03-09 14:13:49 +0000118}
119
Matthew Benthamc48ac8c2018-12-12 16:15:59 +0000120void NeonBatchNormalizationWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100121{
122 FreeTensorIfUnused(m_Mean);
123 FreeTensorIfUnused(m_Variance);
124 FreeTensorIfUnused(m_Gamma);
125 FreeTensorIfUnused(m_Beta);
126}
127
telsoa014fcda012018-03-09 14:13:49 +0000128} //namespace armnn