Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | #include "FullyConnected.hpp" |
| 7 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 8 | #include "RefWorkloadUtils.hpp" |
| 9 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | namespace armnn |
| 11 | { |
| 12 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 13 | void FullyConnected(const TensorShape& rInputShape, |
| 14 | Decoder<float>& rInputDecoder, |
| 15 | const TensorShape& rOutputShape, |
| 16 | Encoder<float>& rOutputEncoder, |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 17 | const TensorShape& rWeightsShape, |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 18 | Decoder<float>& rWeightDecoder, |
| 19 | Decoder<float>& rBiasDecoder, |
| 20 | const bool biasEnabled, |
| 21 | const unsigned int K, |
| 22 | const bool transposeWeights) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | { |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 24 | // Perform FullyConnected implementation |
| 25 | unsigned int outputSize = rOutputShape[1]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 26 | |
Finn Williams | ea8ce70 | 2020-09-29 19:54:00 +0100 | [diff] [blame] | 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 | const std::vector<float> decodedBiases = biasEnabled ? rBiasDecoder.DecodeTensor(biasShape) : std::vector<float>(); |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 32 | |
| 33 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 34 | for (unsigned int n = 0; n < rInputShape[0]; n++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | { |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 36 | for (unsigned int channelOutput = 0; channelOutput < outputSize; channelOutput++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | { |
| 38 | float outval = 0.f; |
| 39 | |
| 40 | for (unsigned int channelInput = 0; channelInput < K; channelInput++) |
| 41 | { |
| 42 | float weight; |
| 43 | if (transposeWeights) |
| 44 | { |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 45 | weight = decodedWeights[channelOutput * K + channelInput]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | } |
| 47 | else |
| 48 | { |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 49 | weight = decodedWeights[channelInput * outputSize + channelOutput]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 52 | outval += weight * decodedInputs[n * K + channelInput]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 55 | if (biasEnabled) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | { |
Finn Williams | b9dcfe6 | 2020-09-17 15:58:31 +0100 | [diff] [blame] | 57 | outval += decodedBiases[channelOutput]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 60 | rOutputEncoder[n * outputSize + channelOutput]; |
| 61 | rOutputEncoder.Set(outval); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } //namespace armnn |