aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/FullyConnected.cpp
blob: bf5814d2ad4bf7896d0f9ab7e4cbbea446a3eb59 (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
//

#include "FullyConnected.hpp"

#include <boost/assert.hpp>

namespace armnn
{

void FullyConnected(const float*      inputData,
                    float*            outputData,
                    const TensorInfo& inputTensorInfo,
                    const TensorInfo& outputTensorInfo,
                    const float*      weightData,
                    const float*      biasData,
                    bool              transposeWeights)
{
    unsigned int N = outputTensorInfo.GetShape()[1]; // Outputs Vector Size.

    BOOST_ASSERT(inputTensorInfo.GetNumDimensions() > 1); // Needs some data.

    unsigned int K = 1; // Total number of activations in the input.
    for (unsigned int i = 1; i < inputTensorInfo.GetNumDimensions(); i++)
    {
        K *= inputTensorInfo.GetShape()[i];
    }

    for (unsigned int n = 0; n < inputTensorInfo.GetShape()[0]; n++)
    {
        for (unsigned int channelOutput = 0; channelOutput < N; channelOutput++)
        {
            float outval = 0.f;

            for (unsigned int channelInput = 0; channelInput < K; channelInput++)
            {
                float weight;
                if (transposeWeights)
                {
                    weight = weightData[channelOutput * K + channelInput];
                }
                else
                {
                    weight = weightData[channelInput * N + channelOutput];
                }

                outval += weight * inputData[n * K + channelInput];
            }

            if (biasData)
            {
                outval += biasData[channelOutput];
            }

            outputData[n * N + channelOutput] = outval;
        }
    }
}

} //namespace armnn