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 | #pragma once |
| 7 | |
| 8 | #include "RefWorkloadUtils.hpp" |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 9 | #include "TensorBufferArrayView.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | |
| 11 | #include <armnn/Tensor.hpp> |
| 12 | |
| 13 | #include <cmath> |
| 14 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | |
| 18 | template<typename NormData> |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 19 | static void BatchNormImpl(NormData data, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | const float* varIn, |
| 21 | const float* meanIn, |
| 22 | const float* gammaIn, |
| 23 | const float* betaIn, |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 24 | float* outputData, |
| 25 | const float* inputData) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 26 | { |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 27 | const TensorInfo& inputInfo = GetTensorInfo(data.m_Inputs[0]); |
| 28 | const TensorInfo& outputInfo = GetTensorInfo(data.m_Outputs[0]); |
| 29 | |
| 30 | TensorBufferArrayView<const float> input(inputInfo.GetShape(), |
| 31 | inputData, |
| 32 | data.m_Parameters.m_DataLayout); |
| 33 | TensorBufferArrayView<float> output(outputInfo.GetShape(), |
| 34 | outputData, |
| 35 | data.m_Parameters.m_DataLayout); |
| 36 | |
| 37 | DataLayoutIndexed dataLayout(data.m_Parameters.m_DataLayout); |
| 38 | |
| 39 | for (unsigned int c = 0; c < inputInfo.GetShape()[dataLayout.GetChannelsIndex()]; c++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 40 | { |
| 41 | float var = varIn[c]; |
| 42 | float mean = meanIn[c]; |
| 43 | float gamma = gammaIn[c]; |
| 44 | float beta = betaIn[c]; |
| 45 | |
| 46 | float mult = gamma / sqrtf(var + data.m_Parameters.m_Eps); |
| 47 | float add = beta - mult * mean; |
| 48 | |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 49 | for (unsigned int n = 0; n < inputInfo.GetShape()[0]; n++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 50 | { |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 51 | for (unsigned int h = 0; h < inputInfo.GetShape()[dataLayout.GetHeightIndex()]; h++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 52 | { |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 53 | for (unsigned int w = 0; w < inputInfo.GetShape()[dataLayout.GetWidthIndex()]; w++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 54 | { |
Matteo Martincigh | 8eb675e | 2018-10-17 14:43:29 +0100 | [diff] [blame] | 55 | output.Get(n, c, h, w) = mult * input.Get(n, c, h, w) + add; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | } //namespace armnn |