blob: 5da7cca83e5e5531d8ba1a6ebfdca3aa6c326b8b [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. 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
James Conroy1f58f032021-04-27 17:13:27 +010015#include <backendsCommon/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)
Matthew Benthamc48ac8c2018-12-12 16:15:59 +000061 : BaseWorkload<BatchNormalizationQueueDescriptor>(descriptor, info)
telsoa014fcda012018-03-09 14:13:49 +000062{
Matthew Benthamc48ac8c2018-12-12 16:15:59 +000063 m_Data.ValidateInputsOutputs("NeonBatchNormalizationWorkload", 1, 1);
telsoa014fcda012018-03-09 14:13:49 +000064
Jan Eilersbb446e52020-04-02 13:56:54 +010065 arm_compute::ITensor& input = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
66 arm_compute::ITensor& output = PolymorphicDowncast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
telsoa014fcda012018-03-09 14:13:49 +000067
Matthew Bentham8800c002018-11-19 13:19:28 +000068 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
Nikhil Rajd1340932018-10-18 14:27:50 +010069 input.info()->set_data_layout(aclDataLayout);
70 output.info()->set_data_layout(aclDataLayout);
71
telsoa01c577f2c2018-08-31 09:22:23 +010072 m_Mean = std::make_unique<arm_compute::Tensor>();
73 BuildArmComputeTensor(*m_Mean, m_Data.m_Mean->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000074
telsoa01c577f2c2018-08-31 09:22:23 +010075 m_Variance = std::make_unique<arm_compute::Tensor>();
76 BuildArmComputeTensor(*m_Variance, m_Data.m_Variance->GetTensorInfo());
telsoa014fcda012018-03-09 14:13:49 +000077
telsoa01c577f2c2018-08-31 09:22:23 +010078 m_Gamma = std::make_unique<arm_compute::Tensor>();
79 BuildArmComputeTensor(*m_Gamma, m_Data.m_Gamma->GetTensorInfo());
80
81 m_Beta = std::make_unique<arm_compute::Tensor>();
82 BuildArmComputeTensor(*m_Beta, m_Data.m_Beta->GetTensorInfo());
83
Mike Kelly07810fc2020-11-12 10:58:48 +000084 const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
85
Matthew Benthamd80a7122019-01-08 17:52:37 +000086 auto layer = std::make_unique<arm_compute::NEBatchNormalizationLayer>();
87 layer->configure(&input,
88 &output,
89 m_Mean.get(),
90 m_Variance.get(),
91 m_Beta.get(),
92 m_Gamma.get(),
Mike Kelly07810fc2020-11-12 10:58:48 +000093 m_Data.m_Parameters.m_Eps,
94 activationInfo);
Matthew Benthamd80a7122019-01-08 17:52:37 +000095 m_Layer.reset(layer.release());
telsoa01c577f2c2018-08-31 09:22:23 +010096
Nattapat Chaimanowong177d8d22018-10-16 13:21:27 +010097 InitializeArmComputeTensorData(*m_Mean, m_Data.m_Mean);
98 InitializeArmComputeTensorData(*m_Variance, m_Data.m_Variance);
99 InitializeArmComputeTensorData(*m_Gamma, m_Data.m_Gamma);
100 InitializeArmComputeTensorData(*m_Beta, m_Data.m_Beta);
telsoa01c577f2c2018-08-31 09:22:23 +0100101
102 // Force Compute Library to perform the necessary copying and reshaping, after which
103 // delete all the input tensors that will no longer be needed
Matthew Benthamd80a7122019-01-08 17:52:37 +0000104 m_Layer->prepare();
telsoa01c577f2c2018-08-31 09:22:23 +0100105 FreeUnusedTensors();
telsoa014fcda012018-03-09 14:13:49 +0000106}
107
Matthew Benthamc48ac8c2018-12-12 16:15:59 +0000108void NeonBatchNormalizationWorkload::Execute() const
telsoa014fcda012018-03-09 14:13:49 +0000109{
Matthew Benthamc48ac8c2018-12-12 16:15:59 +0000110 ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonBatchNormalizationWorkload_Execute");
Matthew Benthamd80a7122019-01-08 17:52:37 +0000111 m_Layer->run();
telsoa014fcda012018-03-09 14:13:49 +0000112}
113
Matthew Benthamc48ac8c2018-12-12 16:15:59 +0000114void NeonBatchNormalizationWorkload::FreeUnusedTensors()
telsoa01c577f2c2018-08-31 09:22:23 +0100115{
116 FreeTensorIfUnused(m_Mean);
117 FreeTensorIfUnused(m_Variance);
118 FreeTensorIfUnused(m_Gamma);
119 FreeTensorIfUnused(m_Beta);
120}
121
telsoa014fcda012018-03-09 14:13:49 +0000122} //namespace armnn