aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/BatchNormImpl.hpp
blob: a7579c8373139d9a6b8e40f1461a779cd5121a77 (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#pragma once

#include "RefWorkloadUtils.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& inputInfo0 = GetTensorInfo(data.m_Inputs[0]);
    for (unsigned int c = 0; c < inputInfo0.GetShape()[1]; 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 < inputInfo0.GetShape()[0]; n++)
        {
            for (unsigned int j = 0; j < inputInfo0.GetShape()[2]; j++)
            {
                for (unsigned int i = 0; i < inputInfo0.GetShape()[3]; i++)
                {
                    unsigned int index = i +
                                         j*inputInfo0.GetShape()[3] +
                                         c*inputInfo0.GetShape()[3] * inputInfo0.GetShape()[2] +
                                         n*inputInfo0.GetShape()[3] * inputInfo0.GetShape()[2]
                                                                     * inputInfo0.GetShape()[1];

                    outputData[index] = mult * inputData[index] + add;
                }
            }
        }
    }
}

} //namespace armnn