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" |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 7 | #include "RefWorkloadUtils.hpp" |
| 8 | #include "Decoders.hpp" |
| 9 | #include "Encoders.hpp" |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 10 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 11 | #include <Profiling.hpp> |
| 12 | |
| 13 | #include <armnnUtils/DataLayoutIndexed.hpp> |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 14 | #include <armnn/utility/NumericCast.hpp> |
Matthew Jackson | 82b15ed | 2019-07-25 16:14:30 +0100 | [diff] [blame] | 15 | |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 16 | #include <cmath> |
| 17 | |
| 18 | using namespace armnnUtils; |
| 19 | |
| 20 | namespace armnn |
| 21 | { |
| 22 | RefL2NormalizationWorkload::RefL2NormalizationWorkload( |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 23 | const L2NormalizationQueueDescriptor& descriptor, |
| 24 | const WorkloadInfo& info) |
| 25 | : BaseWorkload<L2NormalizationQueueDescriptor>(descriptor, info) {} |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 26 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 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 TensorShape& shape = inputInfo.GetShape(); |
| 40 | unsigned int paddedShapeArray[4]; |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 41 | const int idxShift = 4 - armnn::numeric_cast<int>(shape.GetNumDimensions()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 42 | |
| 43 | const unsigned int batches = (idxShift == 0) ? shape[0] : 1; |
| 44 | paddedShapeArray[0] = batches; |
| 45 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 46 | const int channelsIdx = armnn::numeric_cast<int>(dataLayout.GetChannelsIndex()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 47 | const unsigned int channels = (channelsIdx - idxShift >= 0) |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 48 | ? shape[armnn::numeric_cast<unsigned int>(channelsIdx - idxShift)] |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 49 | : 1; |
| 50 | paddedShapeArray[channelsIdx] = channels; |
| 51 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 52 | const int heightIdx = armnn::numeric_cast<int>(dataLayout.GetHeightIndex()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 53 | const unsigned int height = (heightIdx - idxShift >= 0) |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 54 | ? shape[armnn::numeric_cast<unsigned int>(heightIdx - idxShift)] |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 55 | : 1; |
| 56 | paddedShapeArray[heightIdx] = height; |
| 57 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 58 | const int widthIdx = armnn::numeric_cast<int>(dataLayout.GetWidthIndex()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 59 | const unsigned int width = (widthIdx - idxShift >= 0) |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 60 | ? shape[armnn::numeric_cast<unsigned int>(widthIdx - idxShift)] |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 61 | : 1; |
| 62 | paddedShapeArray[widthIdx] = width; |
| 63 | |
| 64 | const TensorShape& paddedShape = TensorShape(4, paddedShapeArray); |
| 65 | |
| 66 | for (unsigned int n = 0; n < batches; ++n) |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 67 | { |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 68 | for (unsigned int c = 0; c < channels; ++c) |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 69 | { |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 70 | for (unsigned int h = 0; h < height; ++h) |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 71 | { |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 72 | for (unsigned int w = 0; w < width; ++w) |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 73 | { |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 74 | float reduction = 0.0; |
| 75 | for (unsigned int d = 0; d < channels; ++d) |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 76 | { |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 77 | unsigned int inputIndex = dataLayout.GetIndex(paddedShape, n, d, h, w); |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 78 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 79 | (*inputDecoder)[inputIndex]; |
| 80 | const float value = inputDecoder->Get(); |
| 81 | reduction += value * value; |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 82 | } |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 83 | |
| 84 | unsigned int index = dataLayout.GetIndex(paddedShape, n, c, h, w); |
| 85 | |
| 86 | float maximum = reduction < m_Data.m_Parameters.m_Eps ? m_Data.m_Parameters.m_Eps : reduction; |
| 87 | |
| 88 | const float scale = 1.0f / sqrtf(maximum); |
| 89 | |
| 90 | (*inputDecoder)[index]; |
| 91 | (*outputEncoder)[index]; |
| 92 | outputEncoder->Set(inputDecoder->Get() * scale); |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 97 | } |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 98 | |
| 99 | } //namespace armnn |