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