IVGCVSW-1951 Update NeonWorkloadUtils
[platform/upstream/armnn.git] / src / backends / neon / workloads / NeonBatchNormalizationFloatWorkload.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "NeonBatchNormalizationFloatWorkload.hpp"
7 #include <backends/CpuTensorHandle.hpp>
8 #include <backends/aclCommon/ArmComputeTensorUtils.hpp>
9 #include <armnn/ArmNN.hpp>
10
11 namespace armnn
12 {
13 using namespace armcomputetensorutils;
14
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 {
24     const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input);
25     const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output);
26     const arm_compute::TensorInfo aclMeanInfo = BuildArmComputeTensorInfo(mean);
27     const arm_compute::TensorInfo aclVarInfo = BuildArmComputeTensorInfo(var);
28     const arm_compute::TensorInfo aclBetaInfo = BuildArmComputeTensorInfo(beta);
29     const arm_compute::TensorInfo aclGammaInfo = BuildArmComputeTensorInfo(gamma);
30
31     return arm_compute::NEBatchNormalizationLayer::validate(&aclInputInfo,
32                                                             &aclOutputInfo,
33                                                             &aclMeanInfo,
34                                                             &aclVarInfo,
35                                                             &aclBetaInfo,
36                                                             &aclGammaInfo,
37                                                             descriptor.m_Eps);
38 }
39
40 NeonBatchNormalizationFloatWorkload::NeonBatchNormalizationFloatWorkload(
41     const BatchNormalizationQueueDescriptor& descriptor, const WorkloadInfo& info)
42     : FloatWorkload<BatchNormalizationQueueDescriptor>(descriptor, info)
43 {
44     m_Data.ValidateInputsOutputs("NeonBatchNormalizationFloatWorkload", 1, 1);
45
46     arm_compute::ITensor& input = boost::polymorphic_downcast<INeonTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
47     arm_compute::ITensor& output = boost::polymorphic_downcast<INeonTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
48
49     m_Mean = std::make_unique<arm_compute::Tensor>();
50     BuildArmComputeTensor(*m_Mean, m_Data.m_Mean->GetTensorInfo());
51
52     m_Variance = std::make_unique<arm_compute::Tensor>();
53     BuildArmComputeTensor(*m_Variance, m_Data.m_Variance->GetTensorInfo());
54
55     m_Gamma = std::make_unique<arm_compute::Tensor>();
56     BuildArmComputeTensor(*m_Gamma, m_Data.m_Gamma->GetTensorInfo());
57
58     m_Beta = std::make_unique<arm_compute::Tensor>();
59     BuildArmComputeTensor(*m_Beta, m_Data.m_Beta->GetTensorInfo());
60
61     m_Layer.configure(&input,
62                       &output,
63                       m_Mean.get(),
64                       m_Variance.get(),
65                       m_Beta.get(),
66                       m_Gamma.get(),
67                       m_Data.m_Parameters.m_Eps);
68
69     InitializeArmComputeTensorData(*m_Mean, m_Data.m_Mean);
70     InitializeArmComputeTensorData(*m_Variance, m_Data.m_Variance);
71     InitializeArmComputeTensorData(*m_Gamma, m_Data.m_Gamma);
72     InitializeArmComputeTensorData(*m_Beta, m_Data.m_Beta);
73
74     // Force Compute Library to perform the necessary copying and reshaping, after which
75     // delete all the input tensors that will no longer be needed
76     m_Layer.prepare();
77     FreeUnusedTensors();
78 }
79
80 void NeonBatchNormalizationFloatWorkload::Execute() const
81 {
82     ARMNN_SCOPED_PROFILING_EVENT_NEON("NeonBatchNormalizationFloatWorkload_Execute");
83     m_Layer.run();
84 }
85
86 void NeonBatchNormalizationFloatWorkload::FreeUnusedTensors()
87 {
88     FreeTensorIfUnused(m_Mean);
89     FreeTensorIfUnused(m_Variance);
90     FreeTensorIfUnused(m_Gamma);
91     FreeTensorIfUnused(m_Beta);
92 }
93
94 } //namespace armnn