Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 2 | // Copyright © 2019-2023 Arm Ltd and Contributors. All rights reserved. |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 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) |
Finn Williams | 73c547d | 2022-02-15 20:47:34 +0000 | [diff] [blame] | 25 | : RefBaseWorkload<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 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 29 | Execute(m_Data.m_Inputs, m_Data.m_Outputs); |
| 30 | } |
| 31 | |
Matthew Sloyan | 2d213a7 | 2022-06-30 17:13:04 +0100 | [diff] [blame] | 32 | void RefL2NormalizationWorkload::ExecuteAsync(ExecutionData& executionData) |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 33 | { |
Matthew Sloyan | 2d213a7 | 2022-06-30 17:13:04 +0100 | [diff] [blame] | 34 | WorkingMemDescriptor* workingMemDescriptor = static_cast<WorkingMemDescriptor*>(executionData.m_Data); |
| 35 | Execute(workingMemDescriptor->m_Inputs, workingMemDescriptor->m_Outputs); |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void RefL2NormalizationWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const |
| 39 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 40 | ARMNN_SCOPED_PROFILING_EVENT_REF_NAME_GUID("RefL2NormalizationWorkload_Execute"); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 41 | |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 42 | const TensorInfo& inputInfo = GetTensorInfo(inputs[0]); |
| 43 | const TensorInfo& outputInfo = GetTensorInfo(outputs[0]); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 44 | |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 45 | auto inputDecoder = MakeDecoder<float>(inputInfo, inputs[0]->Map()); |
| 46 | auto outputEncoder = MakeEncoder<float>(outputInfo, outputs[0]->Map()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 47 | |
| 48 | DataLayoutIndexed dataLayout(m_Data.m_Parameters.m_DataLayout); |
| 49 | |
| 50 | const TensorShape& shape = inputInfo.GetShape(); |
| 51 | unsigned int paddedShapeArray[4]; |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 52 | const int idxShift = 4 - armnn::numeric_cast<int>(shape.GetNumDimensions()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 53 | |
| 54 | const unsigned int batches = (idxShift == 0) ? shape[0] : 1; |
| 55 | paddedShapeArray[0] = batches; |
| 56 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 57 | const int channelsIdx = armnn::numeric_cast<int>(dataLayout.GetChannelsIndex()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 58 | const unsigned int channels = (channelsIdx - idxShift >= 0) |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 59 | ? shape[armnn::numeric_cast<unsigned int>(channelsIdx - idxShift)] |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 60 | : 1; |
| 61 | paddedShapeArray[channelsIdx] = channels; |
| 62 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 63 | const int heightIdx = armnn::numeric_cast<int>(dataLayout.GetHeightIndex()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 64 | const unsigned int height = (heightIdx - idxShift >= 0) |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 65 | ? shape[armnn::numeric_cast<unsigned int>(heightIdx - idxShift)] |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 66 | : 1; |
| 67 | paddedShapeArray[heightIdx] = height; |
| 68 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 69 | const int widthIdx = armnn::numeric_cast<int>(dataLayout.GetWidthIndex()); |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 70 | const unsigned int width = (widthIdx - idxShift >= 0) |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 71 | ? shape[armnn::numeric_cast<unsigned int>(widthIdx - idxShift)] |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 72 | : 1; |
| 73 | paddedShapeArray[widthIdx] = width; |
| 74 | |
| 75 | const TensorShape& paddedShape = TensorShape(4, paddedShapeArray); |
| 76 | |
| 77 | for (unsigned int n = 0; n < batches; ++n) |
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 | for (unsigned int c = 0; c < channels; ++c) |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 80 | { |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 81 | for (unsigned int h = 0; h < height; ++h) |
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 | for (unsigned int w = 0; w < width; ++w) |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 84 | { |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 85 | float reduction = 0.0; |
| 86 | for (unsigned int d = 0; d < channels; ++d) |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 87 | { |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 88 | unsigned int inputIndex = dataLayout.GetIndex(paddedShape, n, d, h, w); |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 89 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 90 | (*inputDecoder)[inputIndex]; |
| 91 | const float value = inputDecoder->Get(); |
| 92 | reduction += value * value; |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 93 | } |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 94 | |
| 95 | unsigned int index = dataLayout.GetIndex(paddedShape, n, c, h, w); |
| 96 | |
| 97 | float maximum = reduction < m_Data.m_Parameters.m_Eps ? m_Data.m_Parameters.m_Eps : reduction; |
| 98 | |
| 99 | const float scale = 1.0f / sqrtf(maximum); |
| 100 | |
| 101 | (*inputDecoder)[index]; |
| 102 | (*outputEncoder)[index]; |
| 103 | outputEncoder->Set(inputDecoder->Get() * scale); |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 108 | } |
Ferran Balaguer | d73d14f | 2019-06-10 10:29:54 +0100 | [diff] [blame] | 109 | |
| 110 | } //namespace armnn |