ArmNN
 25.11
Loading...
Searching...
No Matches
FullyConnected.cpp
Go to the documentation of this file.
1//
2// Copyright © 2017, 2024 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "FullyConnected.hpp"
7
9
10namespace armnn
11{
12
13void FullyConnected(const TensorShape& rInputShape,
14 Decoder<float>& rInputDecoder,
15 const TensorShape& rOutputShape,
16 Encoder<float>& rOutputEncoder,
17 const TensorShape& rWeightsShape,
18 Decoder<float>& rWeightDecoder,
19 Decoder<float>* pBiasDecoder,
20 const bool biasEnabled,
21 const unsigned int K,
22 const bool transposeWeights)
23{
24 // Perform FullyConnected implementation
25 unsigned int outputSize = rOutputShape[1];
26
27 const std::vector<float> decodedInputs = rInputDecoder.DecodeTensor(rInputShape);
28 const std::vector<float> decodedWeights = rWeightDecoder.DecodeTensor(rWeightsShape);
29
30 const TensorShape biasShape{outputSize};
31
32 const std::vector<float> decodedBiases = biasEnabled ? pBiasDecoder->DecodeTensor(biasShape) : std::vector<float>();
33
34
35 for (unsigned int n = 0; n < rInputShape[0]; n++)
36 {
37 for (unsigned int channelOutput = 0; channelOutput < outputSize; channelOutput++)
38 {
39 float outval = 0.f;
40
41 for (unsigned int channelInput = 0; channelInput < K; channelInput++)
42 {
43 float weight;
44 if (transposeWeights)
45 {
46 weight = decodedWeights[channelOutput * K + channelInput];
47 }
48 else
49 {
50 weight = decodedWeights[channelInput * outputSize + channelOutput];
51 }
52
53 outval += weight * decodedInputs[n * K + channelInput];
54 }
55
56 if (biasEnabled)
57 {
58 outval += decodedBiases[channelOutput];
59 }
60
61 rOutputEncoder[n * outputSize + channelOutput];
62 rOutputEncoder.Set(outval);
63 }
64 }
65}
66
67} //namespace armnn
virtual std::vector< float > DecodeTensor(const TensorShape &tensorShape, bool isDepthwise=false)=0
virtual void Set(IType right)=0
Copyright (c) 2021 ARM Limited and Contributors.