blob: ce5699ef0b33c141a6f2e4476afb137396727139 [file] [log] [blame]
Ferran Balaguerd73d14f2019-06-10 10:29:54 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "RefL2NormalizationWorkload.hpp"
7
8#include "RefWorkloadUtils.hpp"
9#include "Decoders.hpp"
10#include "Encoders.hpp"
11#include "DataLayoutIndexed.hpp"
12
13
14#include "Profiling.hpp"
15
16#include <cmath>
17
18using namespace armnnUtils;
19
20namespace armnn
21{
22RefL2NormalizationWorkload::RefL2NormalizationWorkload(
23 const L2NormalizationQueueDescriptor& descriptor,
24 const WorkloadInfo& info)
25 : BaseWorkload<L2NormalizationQueueDescriptor>(descriptor, info) {}
26
27 void RefL2NormalizationWorkload::Execute() const
28 {
29 ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefL2NormalizationWorkload_Execute");
30
31 const TensorInfo& inputInfo = GetTensorInfo(m_Data.m_Inputs[0]);
32 const TensorInfo& outputInfo = GetTensorInfo(m_Data.m_Outputs[0]);
33
34 auto inputDecoder = MakeDecoder<float>(inputInfo, m_Data.m_Inputs[0]->Map());
35 auto outputEncoder = MakeEncoder<float>(outputInfo, m_Data.m_Outputs[0]->Map());
36
37 DataLayoutIndexed dataLayout(m_Data.m_Parameters.m_DataLayout);
38
39 const unsigned int batches = inputInfo.GetShape()[0];
40 const unsigned int channels = inputInfo.GetShape()[dataLayout.GetChannelsIndex()];
41 const unsigned int height = inputInfo.GetShape()[dataLayout.GetHeightIndex()];
42 const unsigned int width = inputInfo.GetShape()[dataLayout.GetWidthIndex()];
43
44 for (unsigned int n = 0; n < batches; ++n)
45 {
46 for (unsigned int c = 0; c < channels; ++c)
47 {
48 for (unsigned int h = 0; h < height; ++h)
49 {
50 for (unsigned int w = 0; w < width; ++w)
51 {
52 float reduction = 0.0;
53 for (unsigned int d = 0; d < channels; ++d)
54 {
55 unsigned int inputIndex = dataLayout.GetIndex(inputInfo.GetShape(), n, d, h, w);
56
57 (*inputDecoder)[inputIndex];
58 const float value = inputDecoder->Get();
59 reduction += value * value;
60 }
61
62 unsigned int index = dataLayout.GetIndex(inputInfo.GetShape(), n, c, h, w);
63
64 const float scale = 1.0f / sqrtf(reduction);
65
66 (*inputDecoder)[index];
67 (*outputEncoder)[index];
68 outputEncoder->Set(inputDecoder->Get() * scale);
69 }
70 }
71 }
72 }
73 }
74
75} //namespace armnn