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