telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "RefL2NormalizationFloat32Workload.hpp" |
| 7 | |
| 8 | #include "RefWorkloadUtils.hpp" |
| 9 | #include "TensorBufferArrayView.hpp" |
| 10 | |
| 11 | #include "Profiling.hpp" |
| 12 | |
| 13 | #include <cmath> |
| 14 | |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 15 | using namespace armnnUtils; |
| 16 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | void RefL2NormalizationFloat32Workload::Execute() const |
| 21 | { |
| 22 | ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefL2NormalizationFloat32Workload_Execute"); |
| 23 | |
| 24 | const TensorInfo& inputInfo = GetTensorInfo(m_Data.m_Inputs[0]); |
| 25 | const TensorInfo& outputInfo = GetTensorInfo(m_Data.m_Outputs[0]); |
| 26 | |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 27 | TensorBufferArrayView<const float> input(inputInfo.GetShape(), |
| 28 | GetInputTensorDataFloat(0, m_Data), |
| 29 | m_Data.m_Parameters.m_DataLayout); |
| 30 | TensorBufferArrayView<float> output(outputInfo.GetShape(), |
| 31 | GetOutputTensorDataFloat(0, m_Data), |
| 32 | m_Data.m_Parameters.m_DataLayout); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 33 | |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 34 | DataLayoutIndexed dataLayout(m_Data.m_Parameters.m_DataLayout); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 36 | const unsigned int batches = inputInfo.GetShape()[0]; |
| 37 | const unsigned int channels = inputInfo.GetShape()[dataLayout.GetChannelsIndex()]; |
| 38 | const unsigned int height = inputInfo.GetShape()[dataLayout.GetHeightIndex()]; |
| 39 | const unsigned int width = inputInfo.GetShape()[dataLayout.GetWidthIndex()]; |
| 40 | |
| 41 | for (unsigned int n = 0; n < batches; ++n) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 42 | { |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 43 | for (unsigned int c = 0; c < channels; ++c) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 44 | { |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 45 | for (unsigned int h = 0; h < height; ++h) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | { |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 47 | for (unsigned int w = 0; w < width; ++w) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 48 | { |
| 49 | float reduction = 0.0; |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 50 | for (unsigned int d = 0; d < channels; ++d) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 51 | { |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 52 | const float value = input.Get(n, d, h, w); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 53 | reduction += value * value; |
| 54 | } |
| 55 | |
| 56 | // Using std::max(reduction, epsilon) below would prevent against division by 0. |
| 57 | // However, at the time of writing: |
| 58 | // - This is not supported by the ACL functions used to implement L2Normalization in the CL |
| 59 | // backend. |
| 60 | // - The reference semantics for this operator do not include this parameter. |
| 61 | const float scale = 1.0f / sqrtf(reduction); |
Matteo Martincigh | b63973e | 2018-10-16 16:23:33 +0100 | [diff] [blame] | 62 | output.Get(n, c, h, w) = input.Get(n, c, h, w) * scale; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | } //namespace armnn |