blob: bc82739f6e813b9d8bde98cc985b29e0837611ca [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
6#include "RefL2NormalizationFloat32Workload.hpp"
7
8#include "RefWorkloadUtils.hpp"
9#include "TensorBufferArrayView.hpp"
10
11#include "Profiling.hpp"
12
13#include <cmath>
14
Matteo Martincigh21350152018-11-28 16:22:22 +000015using namespace armnnUtils;
16
telsoa014fcda012018-03-09 14:13:49 +000017namespace armnn
18{
19
20void RefL2NormalizationFloat32Workload::Execute() const
21{
22 ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefL2NormalizationFloat32Workload_Execute");
23
24 const TensorInfo& inputInfo = GetTensorInfo(m_Data.m_Inputs[0]);
25 const TensorInfo& outputInfo = GetTensorInfo(m_Data.m_Outputs[0]);
26
Matteo Martincighb63973e2018-10-16 16:23:33 +010027 TensorBufferArrayView<const float> input(inputInfo.GetShape(),
28 GetInputTensorDataFloat(0, m_Data),
29 m_Data.m_Parameters.m_DataLayout);
30 TensorBufferArrayView<float> output(outputInfo.GetShape(),
31 GetOutputTensorDataFloat(0, m_Data),
32 m_Data.m_Parameters.m_DataLayout);
telsoa014fcda012018-03-09 14:13:49 +000033
Matteo Martincighb63973e2018-10-16 16:23:33 +010034 DataLayoutIndexed dataLayout(m_Data.m_Parameters.m_DataLayout);
telsoa014fcda012018-03-09 14:13:49 +000035
Matteo Martincighb63973e2018-10-16 16:23:33 +010036 const unsigned int batches = inputInfo.GetShape()[0];
37 const unsigned int channels = inputInfo.GetShape()[dataLayout.GetChannelsIndex()];
38 const unsigned int height = inputInfo.GetShape()[dataLayout.GetHeightIndex()];
39 const unsigned int width = inputInfo.GetShape()[dataLayout.GetWidthIndex()];
40
41 for (unsigned int n = 0; n < batches; ++n)
telsoa014fcda012018-03-09 14:13:49 +000042 {
Matteo Martincighb63973e2018-10-16 16:23:33 +010043 for (unsigned int c = 0; c < channels; ++c)
telsoa014fcda012018-03-09 14:13:49 +000044 {
Matteo Martincighb63973e2018-10-16 16:23:33 +010045 for (unsigned int h = 0; h < height; ++h)
telsoa014fcda012018-03-09 14:13:49 +000046 {
Matteo Martincighb63973e2018-10-16 16:23:33 +010047 for (unsigned int w = 0; w < width; ++w)
telsoa014fcda012018-03-09 14:13:49 +000048 {
49 float reduction = 0.0;
Matteo Martincighb63973e2018-10-16 16:23:33 +010050 for (unsigned int d = 0; d < channels; ++d)
telsoa014fcda012018-03-09 14:13:49 +000051 {
Matteo Martincighb63973e2018-10-16 16:23:33 +010052 const float value = input.Get(n, d, h, w);
telsoa014fcda012018-03-09 14:13:49 +000053 reduction += value * value;
54 }
55
56 // Using std::max(reduction, epsilon) below would prevent against division by 0.
57 // However, at the time of writing:
58 // - This is not supported by the ACL functions used to implement L2Normalization in the CL
59 // backend.
60 // - The reference semantics for this operator do not include this parameter.
61 const float scale = 1.0f / sqrtf(reduction);
Matteo Martincighb63973e2018-10-16 16:23:33 +010062 output.Get(n, c, h, w) = input.Get(n, c, h, w) * scale;
telsoa014fcda012018-03-09 14:13:49 +000063 }
64 }
65 }
66 }
67}
68
69} //namespace armnn