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 "Softmax.hpp" |
| 7 | |
| 8 | #include <cmath> |
| 9 | #include <vector> |
| 10 | |
| 11 | namespace armnn |
| 12 | { |
| 13 | |
Francis Murtagh | 07f2121 | 2019-07-23 09:50:50 +0100 | [diff] [blame] | 14 | unsigned int GetNumElementsBetween(const TensorShape& shape, |
| 15 | unsigned int firstAxisInclusive, |
| 16 | unsigned int lastAxisExclusive) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 17 | { |
Francis Murtagh | 07f2121 | 2019-07-23 09:50:50 +0100 | [diff] [blame] | 18 | BOOST_ASSERT(0 <= firstAxisInclusive); |
| 19 | BOOST_ASSERT(firstAxisInclusive <= lastAxisExclusive); |
| 20 | BOOST_ASSERT(lastAxisExclusive <= shape.GetNumDimensions()); |
| 21 | unsigned int count = 1; |
| 22 | for (unsigned int i = firstAxisInclusive; i < lastAxisExclusive; i++) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | { |
Francis Murtagh | 07f2121 | 2019-07-23 09:50:50 +0100 | [diff] [blame] | 24 | count *= shape[i]; |
| 25 | } |
| 26 | return count; |
| 27 | } |
| 28 | |
| 29 | /// Computes the softmax function on some inputs, into outputs, with a shape given by tensorInfo. |
| 30 | void Softmax(Decoder<float>& in, Encoder<float>& out, const TensorInfo& inputTensorInfo, float beta, int axis) |
| 31 | { |
| 32 | BOOST_ASSERT_MSG(axis < static_cast<int>(inputTensorInfo.GetNumDimensions()), |
| 33 | "Required axis index greater than number of dimensions."); |
| 34 | BOOST_ASSERT_MSG(axis >= -static_cast<int>(inputTensorInfo.GetNumDimensions()), |
| 35 | "Required axis index lower than negative of the number of dimensions"); |
| 36 | |
| 37 | unsigned int uAxis = axis < 0 ? |
| 38 | inputTensorInfo.GetNumDimensions() - static_cast<unsigned int>(abs(axis)) |
| 39 | : static_cast<unsigned int>(axis); |
| 40 | |
| 41 | const TensorShape& inputShape = inputTensorInfo.GetShape(); |
| 42 | const unsigned int outerSize = GetNumElementsBetween(inputShape, 0, uAxis); |
| 43 | const unsigned int axisSize = inputShape[uAxis]; |
| 44 | const unsigned int innerSize = GetNumElementsBetween(inputShape, uAxis + 1, inputShape.GetNumDimensions()); |
| 45 | |
| 46 | for (unsigned int outer = 0; outer < outerSize; ++outer) |
| 47 | { |
| 48 | unsigned int inputBeginIdx = outer * axisSize * innerSize; |
| 49 | unsigned int inputEndIdx = inputBeginIdx + axisSize * innerSize; |
| 50 | unsigned int outputBeginIdx = outer * axisSize * innerSize; |
| 51 | |
| 52 | for (unsigned int inner = 0; inner < innerSize; ++inner, ++inputBeginIdx, ++inputEndIdx, ++outputBeginIdx) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 53 | { |
Francis Murtagh | 07f2121 | 2019-07-23 09:50:50 +0100 | [diff] [blame] | 54 | // Find max |
| 55 | float maxValue = std::numeric_limits<float>::lowest(); |
| 56 | for (unsigned int iter = inputBeginIdx; iter < inputEndIdx; iter += innerSize) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 57 | { |
Francis Murtagh | 07f2121 | 2019-07-23 09:50:50 +0100 | [diff] [blame] | 58 | in[iter]; |
| 59 | maxValue = std::max(maxValue, in.Get()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | |
Francis Murtagh | 07f2121 | 2019-07-23 09:50:50 +0100 | [diff] [blame] | 62 | // Compute sum |
| 63 | float sum = 0.0f; |
| 64 | for (unsigned int iter = inputBeginIdx; iter < inputEndIdx; iter += innerSize) |
| 65 | { |
| 66 | in[iter]; |
| 67 | sum += std::exp((in.Get() - maxValue) * beta); |
| 68 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 69 | |
Francis Murtagh | 07f2121 | 2019-07-23 09:50:50 +0100 | [diff] [blame] | 70 | // Compute result |
| 71 | unsigned int outputIter = outputBeginIdx; |
| 72 | out[outputIter]; |
| 73 | for (unsigned int iter = inputBeginIdx; iter < inputEndIdx; iter += innerSize, outputIter += innerSize) |
| 74 | { |
| 75 | out[outputIter]; |
| 76 | in[iter]; |
| 77 | out.Set(std::exp((in.Get() - maxValue) * beta) / sum); |
| 78 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } //namespace armnn |