Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 1 | // |
| 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 | |
| 18 | using namespace armnnUtils; |
| 19 | |
| 20 | namespace armnn |
| 21 | { |
| 22 | RefL2NormalizationWorkload::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 | |
Ferran Balaguer | e52211e | 2019-06-17 12:23:52 +0100 | [diff] [blame] | 64 | float maximum = reduction < m_Data.m_Parameters.m_Eps ? m_Data.m_Parameters.m_Eps : reduction; |
| 65 | |
| 66 | const float scale = 1.0f / sqrtf(maximum); |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 67 | |
| 68 | (*inputDecoder)[index]; |
| 69 | (*outputEncoder)[index]; |
| 70 | outputEncoder->Set(inputDecoder->Get() * scale); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | } //namespace armnn |