ArmNN
 25.02
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
7 #include "RefWorkloadUtils.hpp"
8 #include "Decoders.hpp"
9 #include "Encoders.hpp"
10 
11 #include <Profiling.hpp>
12 
15 
16 #include <cmath>
17 
18 using namespace armnnUtils;
19 
20 namespace armnn
21 {
23  const L2NormalizationQueueDescriptor& descriptor,
24  const WorkloadInfo& info)
26 
28 {
30 }
31 
32 void 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 
42  DataLayoutIndexed dataLayout(m_Data.m_Parameters.m_DataLayout);
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.
QueueDescriptor m_Data
Definition: Workload.hpp:74
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.
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
std::vector< ITensorHandle * > m_Inputs
std::vector< ITensorHandle * > m_Outputs
Contains information about TensorInfos of a layer.