ArmNN
 24.02
NeonBatchNormalizationWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include "NeonWorkloadUtils.hpp"
9 
12 
14 
16 
17 #include <arm_compute/runtime/NEON/functions/NEBatchNormalizationLayer.h>
18 
19 namespace armnn
20 {
21 using namespace armcomputetensorutils;
22 
23 
25  const TensorInfo& output,
26  const TensorInfo& mean,
27  const TensorInfo& var,
28  const TensorInfo& beta,
29  const TensorInfo& gamma,
30  const BatchNormalizationDescriptor& descriptor,
31  const ActivationDescriptor* activationDescriptor)
32 {
33  const arm_compute::TensorInfo aclInputInfo =
34  armcomputetensorutils::BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
35  const arm_compute::TensorInfo aclOutputInfo =
36  armcomputetensorutils::BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
37  const arm_compute::TensorInfo aclMeanInfo =
38  armcomputetensorutils::BuildArmComputeTensorInfo(mean, descriptor.m_DataLayout);
39  const arm_compute::TensorInfo aclVarInfo =
40  armcomputetensorutils::BuildArmComputeTensorInfo(var, descriptor.m_DataLayout);
41  const arm_compute::TensorInfo aclBetaInfo =
42  armcomputetensorutils::BuildArmComputeTensorInfo(beta, descriptor.m_DataLayout);
43  const arm_compute::TensorInfo aclGammaInfo =
44  armcomputetensorutils::BuildArmComputeTensorInfo(gamma, descriptor.m_DataLayout);
45 
46  const arm_compute::ActivationLayerInfo activationInfo = ConvertActivationDescriptorToAclActivationLayerInfo(
47  activationDescriptor);
48 
49  return arm_compute::NEBatchNormalizationLayer::validate(&aclInputInfo,
50  &aclOutputInfo,
51  &aclMeanInfo,
52  &aclVarInfo,
53  &aclBetaInfo,
54  &aclGammaInfo,
55  descriptor.m_Eps,
56  activationInfo);
57 }
58 
60  const BatchNormalizationQueueDescriptor& descriptor, const WorkloadInfo& info)
62 {
63  // Report Profiling Details
64  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonBatchNormalizationWorkload_Construct",
65  descriptor.m_Parameters,
66  info,
67  this->GetGuid());
68 
69  m_Data.ValidateInputsOutputs("NeonBatchNormalizationWorkload", 1, 1);
70 
71  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();
73 
74  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
75  input.info()->set_data_layout(aclDataLayout);
76  output.info()->set_data_layout(aclDataLayout);
77 
78  m_Mean = std::make_unique<arm_compute::Tensor>();
79  BuildArmComputeTensor(*m_Mean, m_Data.m_Mean->GetTensorInfo());
80 
81  m_Variance = std::make_unique<arm_compute::Tensor>();
82  BuildArmComputeTensor(*m_Variance, m_Data.m_Variance->GetTensorInfo());
83 
84  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 
90  const arm_compute::ActivationLayerInfo activationInfo = ConvertAdditionalInfoToAclActivationLayerInfo(descriptor);
91 
92  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(),
100  activationInfo);
101  m_Layer.reset(layer.release());
102 
107 
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
110  m_Layer->prepare();
111  FreeUnusedTensors();
112 }
113 
115 {
116  ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID("NeonBatchNormalizationWorkload_Execute");
117  m_Layer->run();
118 }
119 
120 void NeonBatchNormalizationWorkload::FreeUnusedTensors()
121 {
122  FreeTensorIfUnused(m_Mean);
123  FreeTensorIfUnused(m_Variance);
124  FreeTensorIfUnused(m_Gamma);
125  FreeTensorIfUnused(m_Beta);
126 }
127 
128 } //namespace armnn
armnn::BatchNormalizationDescriptor
A BatchNormalizationDescriptor for the BatchNormalizationLayer.
Definition: Descriptors.hpp:828
armnn::BatchNormalizationQueueDescriptor
Definition: WorkloadData.hpp:311
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::BatchNormalizationDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:843
armnn::BatchNormalizationQueueDescriptor::m_Gamma
const ConstTensorHandle * m_Gamma
Definition: WorkloadData.hpp:324
armnn::DataLayout
DataLayout
Definition: Types.hpp:62
armnn::NeonBatchNormalizationWorkload::NeonBatchNormalizationWorkload
NeonBatchNormalizationWorkload(const BatchNormalizationQueueDescriptor &descriptor, const WorkloadInfo &info)
Definition: NeonBatchNormalizationWorkload.cpp:59
armnn::QueueDescriptor::ValidateInputsOutputs
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Definition: WorkloadData.cpp:446
armnn::ConvertAdditionalInfoToAclActivationLayerInfo
arm_compute::ActivationLayerInfo ConvertAdditionalInfoToAclActivationLayerInfo(const QueueDescriptor &queueDescriptor)
Definition: ArmComputeUtils.hpp:105
armnn::BatchNormalizationQueueDescriptor::m_Variance
const ConstTensorHandle * m_Variance
Definition: WorkloadData.hpp:322
armnn::TensorInfo
Definition: Tensor.hpp:152
NeonBatchNormalizationWorkload.hpp
armnn::BatchNormalizationQueueDescriptor::m_Mean
const ConstTensorHandle * m_Mean
Definition: WorkloadData.hpp:321
armnn::ConstTensorHandle::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: TensorHandle.hpp:40
armnn::NeonBatchNormalizationWorkload::Execute
virtual void Execute() const override
Definition: NeonBatchNormalizationWorkload.cpp:114
armnn::InitializeArmComputeTensorData
void InitializeArmComputeTensorData(arm_compute::Tensor &tensor, TensorInfo tensorInfo, const ITensorHandle *handle)
Definition: NeonWorkloadUtils.hpp:68
armnn::QueueDescriptorWithParameters::m_Parameters
LayerDescriptor m_Parameters
Definition: WorkloadData.hpp:66
armnn::WorkloadInfo
Contains information about TensorInfos of a layer.
Definition: WorkloadInfo.hpp:16
PolymorphicDowncast.hpp
armnn::BatchNormalizationQueueDescriptor::m_Beta
const ConstTensorHandle * m_Beta
Definition: WorkloadData.hpp:323
ArmComputeUtils.hpp
armnn::ConvertActivationDescriptorToAclActivationLayerInfo
arm_compute::ActivationLayerInfo ConvertActivationDescriptorToAclActivationLayerInfo(const ActivationDescriptor &actDesc)
Definition: ArmComputeUtils.hpp:87
armnn::BoostLogSeverityMapping::info
@ info
armnn::QueueDescriptor::m_Outputs
std::vector< ITensorHandle * > m_Outputs
Definition: WorkloadData.hpp:27
ARMNN_REPORT_PROFILING_WORKLOAD_DESC
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
TensorHandle.hpp
armnn::Status
Status
Definition: Types.hpp:42
armnn::BaseWorkload< BatchNormalizationQueueDescriptor >::m_Data
BatchNormalizationQueueDescriptor m_Data
Definition: Workload.hpp:89
armnn::NeonBatchNormalizationValidate
arm_compute::Status NeonBatchNormalizationValidate(const TensorInfo &input, const TensorInfo &output, const TensorInfo &mean, const TensorInfo &var, const TensorInfo &beta, const TensorInfo &gamma, const BatchNormalizationDescriptor &descriptor, const ActivationDescriptor *activationDescriptor)
Definition: NeonBatchNormalizationWorkload.cpp:24
NeonWorkloadUtils.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
ArmComputeTensorUtils.hpp
ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID
#define ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
Definition: NeonWorkloadUtils.hpp:32
armnn::BatchNormalizationDescriptor::m_Eps
float m_Eps
Value to add to the variance. Used to avoid dividing by zero.
Definition: Descriptors.hpp:841
armnn::NeonBaseWorkload
Definition: NeonBaseWorkload.hpp:13
armnn::QueueDescriptor::m_Inputs
std::vector< ITensorHandle * > m_Inputs
Definition: WorkloadData.hpp:26