blob: 6fec1abe6f8dce2de245c01117a37e75070dc4a1 [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"
Ferran Balaguerd73d14f2019-06-10 10:29:54 +01007#include "RefWorkloadUtils.hpp"
8#include "Decoders.hpp"
9#include "Encoders.hpp"
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010010
Matteo Martincighe011d202019-11-28 11:35:47 +000011#include <Profiling.hpp>
12
13#include <armnnUtils/DataLayoutIndexed.hpp>
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010014
Matthew Jackson82b15ed2019-07-25 16:14:30 +010015#include <boost/numeric/conversion/cast.hpp>
16
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010017#include <cmath>
18
19using namespace armnnUtils;
20
21namespace armnn
22{
23RefL2NormalizationWorkload::RefL2NormalizationWorkload(
Matteo Martincighe011d202019-11-28 11:35:47 +000024 const L2NormalizationQueueDescriptor& descriptor,
25 const WorkloadInfo& info)
26 : BaseWorkload<L2NormalizationQueueDescriptor>(descriptor, info) {}
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010027
Matteo Martincighe011d202019-11-28 11:35:47 +000028void RefL2NormalizationWorkload::Execute() const
29{
30 ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefL2NormalizationWorkload_Execute");
31
32 const TensorInfo& inputInfo = GetTensorInfo(m_Data.m_Inputs[0]);
33 const TensorInfo& outputInfo = GetTensorInfo(m_Data.m_Outputs[0]);
34
35 auto inputDecoder = MakeDecoder<float>(inputInfo, m_Data.m_Inputs[0]->Map());
36 auto outputEncoder = MakeEncoder<float>(outputInfo, m_Data.m_Outputs[0]->Map());
37
38 DataLayoutIndexed dataLayout(m_Data.m_Parameters.m_DataLayout);
39
40 const TensorShape& shape = inputInfo.GetShape();
41 unsigned int paddedShapeArray[4];
42 const int idxShift = 4 - boost::numeric_cast<int>(shape.GetNumDimensions());
43
44 const unsigned int batches = (idxShift == 0) ? shape[0] : 1;
45 paddedShapeArray[0] = batches;
46
47 const int channelsIdx = boost::numeric_cast<int>(dataLayout.GetChannelsIndex());
48 const unsigned int channels = (channelsIdx - idxShift >= 0)
49 ? shape[boost::numeric_cast<unsigned int>(channelsIdx - idxShift)]
50 : 1;
51 paddedShapeArray[channelsIdx] = channels;
52
53 const int heightIdx = boost::numeric_cast<int>(dataLayout.GetHeightIndex());
54 const unsigned int height = (heightIdx - idxShift >= 0)
55 ? shape[boost::numeric_cast<unsigned int>(heightIdx - idxShift)]
56 : 1;
57 paddedShapeArray[heightIdx] = height;
58
59 const int widthIdx = boost::numeric_cast<int>(dataLayout.GetWidthIndex());
60 const unsigned int width = (widthIdx - idxShift >= 0)
61 ? shape[boost::numeric_cast<unsigned int>(widthIdx - idxShift)]
62 : 1;
63 paddedShapeArray[widthIdx] = width;
64
65 const TensorShape& paddedShape = TensorShape(4, paddedShapeArray);
66
67 for (unsigned int n = 0; n < batches; ++n)
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010068 {
Matteo Martincighe011d202019-11-28 11:35:47 +000069 for (unsigned int c = 0; c < channels; ++c)
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010070 {
Matteo Martincighe011d202019-11-28 11:35:47 +000071 for (unsigned int h = 0; h < height; ++h)
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010072 {
Matteo Martincighe011d202019-11-28 11:35:47 +000073 for (unsigned int w = 0; w < width; ++w)
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010074 {
Matteo Martincighe011d202019-11-28 11:35:47 +000075 float reduction = 0.0;
76 for (unsigned int d = 0; d < channels; ++d)
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010077 {
Matteo Martincighe011d202019-11-28 11:35:47 +000078 unsigned int inputIndex = dataLayout.GetIndex(paddedShape, n, d, h, w);
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010079
Matteo Martincighe011d202019-11-28 11:35:47 +000080 (*inputDecoder)[inputIndex];
81 const float value = inputDecoder->Get();
82 reduction += value * value;
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010083 }
Matteo Martincighe011d202019-11-28 11:35:47 +000084
85 unsigned int index = dataLayout.GetIndex(paddedShape, n, c, h, w);
86
87 float maximum = reduction < m_Data.m_Parameters.m_Eps ? m_Data.m_Parameters.m_Eps : reduction;
88
89 const float scale = 1.0f / sqrtf(maximum);
90
91 (*inputDecoder)[index];
92 (*outputEncoder)[index];
93 outputEncoder->Set(inputDecoder->Get() * scale);
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010094 }
95 }
96 }
97 }
Matteo Martincighe011d202019-11-28 11:35:47 +000098}
Ferran Balaguerd73d14f2019-06-10 10:29:54 +010099
100} //namespace armnn