telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 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 | #include <boost/assert.hpp> |
| 11 | |
| 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, |
| 19 | Decoder<float>& rWeightDecoder, |
| 20 | Decoder<float>& rBiasDecoder, |
| 21 | const bool biasEnabled, |
| 22 | const unsigned int K, |
| 23 | const bool transposeWeights) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | { |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 25 | // Perform FullyConnected implementation |
| 26 | unsigned int outputSize = rOutputShape[1]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 27 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 28 | for (unsigned int n = 0; n < rInputShape[0]; n++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | { |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 30 | for (unsigned int channelOutput = 0; channelOutput < outputSize; channelOutput++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 31 | { |
| 32 | float outval = 0.f; |
| 33 | |
| 34 | for (unsigned int channelInput = 0; channelInput < K; channelInput++) |
| 35 | { |
| 36 | float weight; |
| 37 | if (transposeWeights) |
| 38 | { |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 39 | rWeightDecoder[channelOutput * K + channelInput]; |
| 40 | weight = rWeightDecoder.Get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | } |
| 42 | else |
| 43 | { |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 44 | rWeightDecoder[channelInput * outputSize + channelOutput]; |
| 45 | weight = rWeightDecoder.Get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 48 | rInputDecoder[n * K + channelInput]; |
| 49 | outval += weight * rInputDecoder.Get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 52 | if (biasEnabled) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 53 | { |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 54 | rBiasDecoder[channelOutput]; |
| 55 | outval += rBiasDecoder.Get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Francis Murtagh | 43aec58 | 2019-05-27 12:14:10 +0100 | [diff] [blame] | 58 | rOutputEncoder[n * outputSize + channelOutput]; |
| 59 | rOutputEncoder.Set(outval); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | } //namespace armnn |