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