aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/BatchNormImpl.hpp
blob: fbcb2fdf5a55855af1f34b0b11703fa04bb75576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#pragma once

#include "RefWorkloadUtils.hpp"
#include "TensorBufferArrayView.hpp"

#include <armnn/Tensor.hpp>

#include <cmath>

namespace armnn
{

template<typename NormData>
static void BatchNormImpl(NormData     data,
                          const float* varIn,
                          const float* meanIn,
                          const float* gammaIn,
                          const float* betaIn,
                          float*       outputData,
                          const float* inputData)
{
    const TensorInfo& inputInfo = GetTensorInfo(data.m_Inputs[0]);
    const TensorInfo& outputInfo = GetTensorInfo(data.m_Outputs[0]);

    TensorBufferArrayView<const float> input(inputInfo.GetShape(),
                                             inputData,
                                             data.m_Parameters.m_DataLayout);
    TensorBufferArrayView<float> output(outputInfo.GetShape(),
                                        outputData,
                                        data.m_Parameters.m_DataLayout);

    DataLayoutIndexed dataLayout(data.m_Parameters.m_DataLayout);

    for (unsigned int c = 0; c < inputInfo.GetShape()[dataLayout.GetChannelsIndex()]; c++)
    {
        float var   = varIn[c];
        float mean  = meanIn[c];
        float gamma = gammaIn[c];
        float beta  = betaIn[c];

        float mult = gamma / sqrtf(var + data.m_Parameters.m_Eps);
        float add  = beta - mult * mean;

        for (unsigned int n = 0; n < inputInfo.GetShape()[0]; n++)
        {
            for (unsigned int h = 0; h < inputInfo.GetShape()[dataLayout.GetHeightIndex()]; h++)
            {
                for (unsigned int w = 0; w < inputInfo.GetShape()[dataLayout.GetWidthIndex()]; w++)
                {
                    output.Get(n, c, h, w) = mult * input.Get(n, c, h, w) + add;
                }
            }
        }
    }
}

} //namespace armnn