ArmNN
 25.11
Loading...
Searching...
No Matches
RefL2NormalizationWorkload.cpp
Go to the documentation of this file.
1//
2// Copyright © 2019-2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
8#include "Decoders.hpp"
9#include "Encoders.hpp"
10
11#include <Profiling.hpp>
12
15
16#include <cmath>
17
18using namespace armnnUtils;
19
20namespace armnn
21{
26
28{
29 Execute(m_Data.m_Inputs, m_Data.m_Outputs);
30}
31
32void RefL2NormalizationWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const
33{
34 ARMNN_SCOPED_PROFILING_EVENT_REF_NAME_GUID("RefL2NormalizationWorkload_Execute");
35
36 const TensorInfo& inputInfo = GetTensorInfo(inputs[0]);
37 const TensorInfo& outputInfo = GetTensorInfo(outputs[0]);
38
39 auto inputDecoder = MakeDecoder<float>(inputInfo, inputs[0]->Map());
40 auto outputEncoder = MakeEncoder<float>(outputInfo, outputs[0]->Map());
41
43
44 const TensorShape& shape = inputInfo.GetShape();
45 unsigned int paddedShapeArray[4];
46 const int idxShift = 4 - armnn::numeric_cast<int>(shape.GetNumDimensions());
47
48 const unsigned int batches = (idxShift == 0) ? shape[0] : 1;
49 paddedShapeArray[0] = batches;
50
51 const int channelsIdx = armnn::numeric_cast<int>(dataLayout.GetChannelsIndex());
52 const unsigned int channels = (channelsIdx - idxShift >= 0)
53 ? shape[armnn::numeric_cast<unsigned int>(channelsIdx - idxShift)]
54 : 1;
55 paddedShapeArray[channelsIdx] = channels;
56
57 const int heightIdx = armnn::numeric_cast<int>(dataLayout.GetHeightIndex());
58 const unsigned int height = (heightIdx - idxShift >= 0)
59 ? shape[armnn::numeric_cast<unsigned int>(heightIdx - idxShift)]
60 : 1;
61 paddedShapeArray[heightIdx] = height;
62
63 const int widthIdx = armnn::numeric_cast<int>(dataLayout.GetWidthIndex());
64 const unsigned int width = (widthIdx - idxShift >= 0)
65 ? shape[armnn::numeric_cast<unsigned int>(widthIdx - idxShift)]
66 : 1;
67 paddedShapeArray[widthIdx] = width;
68
69 const TensorShape& paddedShape = TensorShape(4, paddedShapeArray);
70
71 for (unsigned int n = 0; n < batches; ++n)
72 {
73 for (unsigned int c = 0; c < channels; ++c)
74 {
75 for (unsigned int h = 0; h < height; ++h)
76 {
77 for (unsigned int w = 0; w < width; ++w)
78 {
79 float reduction = 0.0;
80 for (unsigned int d = 0; d < channels; ++d)
81 {
82 unsigned int inputIndex = dataLayout.GetIndex(paddedShape, n, d, h, w);
83
84 (*inputDecoder)[inputIndex];
85 const float value = inputDecoder->Get();
86 reduction += value * value;
87 }
88
89 unsigned int index = dataLayout.GetIndex(paddedShape, n, c, h, w);
90
91 float maximum = reduction < m_Data.m_Parameters.m_Eps ? m_Data.m_Parameters.m_Eps : reduction;
92
93 const float scale = 1.0f / sqrtf(maximum);
94
95 (*inputDecoder)[index];
96 (*outputEncoder)[index];
97 outputEncoder->Set(inputDecoder->Get() * scale);
98 }
99 }
100 }
101 }
102}
103
104} //namespace armnn
#define ARMNN_SCOPED_PROFILING_EVENT_REF_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
RefBaseWorkload(const L2NormalizationQueueDescriptor &descriptor, const WorkloadInfo &info)
RefL2NormalizationWorkload(const L2NormalizationQueueDescriptor &descriptor, const WorkloadInfo &info)
const TensorShape & GetShape() const
Definition Tensor.hpp:193
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition Tensor.cpp:174
Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout.
Copyright (c) 2021 ARM Limited and Contributors.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
std::unique_ptr< Decoder< T > > MakeDecoder(const TensorInfo &info, const void *data=nullptr)
std::unique_ptr< Encoder< T > > MakeEncoder(const TensorInfo &info, void *data=nullptr)
armnn::TensorInfo GetTensorInfo(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout, const armnn::DataType dataType)
float m_Eps
Used to avoid dividing by zero.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Contains information about TensorInfos of a layer.