Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 1 | // |
Colm Donelan | b4ef163 | 2024-02-01 15:00:43 +0000 | [diff] [blame] | 2 | // Copyright © 2019, 2024 Arm Ltd. All rights reserved. |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "LogSoftmax.hpp" |
| 7 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 8 | #include <armnnUtils/TensorUtils.hpp> |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 9 | #include <armnn/utility/NumericCast.hpp> |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 10 | |
| 11 | #include <cmath> |
| 12 | |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 13 | namespace |
| 14 | { |
| 15 | |
| 16 | inline bool ValidateAxis(int axis, unsigned int numDimensions) |
| 17 | { |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 18 | const int sNumDimensions = armnn::numeric_cast<int>(numDimensions); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 19 | return axis < sNumDimensions && axis >= -sNumDimensions; |
| 20 | } |
| 21 | |
| 22 | } // anonymous namespace |
| 23 | |
| 24 | namespace armnn |
| 25 | { |
| 26 | |
| 27 | void LogSoftmax(Decoder<float>& input, |
| 28 | Encoder<float>& output, |
| 29 | const TensorInfo& inputInfo, |
| 30 | const LogSoftmaxDescriptor& descriptor) |
| 31 | { |
| 32 | const unsigned int numDimensions = inputInfo.GetNumDimensions(); |
| 33 | |
Colm Donelan | b4ef163 | 2024-02-01 15:00:43 +0000 | [diff] [blame] | 34 | ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(ValidateAxis(descriptor.m_Axis, numDimensions), |
| 35 | "Axis index is not in range [-numDimensions, numDimensions)."); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 36 | |
| 37 | unsigned int uAxis = descriptor.m_Axis < 0 ? |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 38 | numDimensions - armnn::numeric_cast<unsigned int>(std::abs(descriptor.m_Axis)) : |
| 39 | armnn::numeric_cast<unsigned int>(descriptor.m_Axis); |
Aron Virginas-Tar | e662a94 | 2019-10-14 15:12:00 +0100 | [diff] [blame] | 40 | |
| 41 | const TensorShape& inputShape = inputInfo.GetShape(); |
| 42 | const unsigned int outerSize = armnnUtils::GetNumElementsBetween(inputShape, 0, uAxis); |
| 43 | const unsigned int axisSize = inputShape[uAxis]; |
| 44 | const unsigned int innerSize = armnnUtils::GetNumElementsBetween(inputShape, |
| 45 | uAxis + 1, |
| 46 | inputShape.GetNumDimensions()); |
| 47 | |
| 48 | for (unsigned int outer = 0; outer < outerSize; ++outer) |
| 49 | { |
| 50 | for (unsigned int inner = 0; inner < innerSize; ++inner) |
| 51 | { |
| 52 | // Find max |
| 53 | input[outer * axisSize * innerSize + inner]; |
| 54 | float maxValue = input.Get(); |
| 55 | for (unsigned int i = 1u; i < axisSize; ++i) |
| 56 | { |
| 57 | input[(outer * axisSize + i) * innerSize + inner]; |
| 58 | maxValue = std::max(maxValue, input.Get()); |
| 59 | } |
| 60 | |
| 61 | // Compute sum |
| 62 | float sum = 0.0f; |
| 63 | for (unsigned int i = 0u; i < axisSize; ++i) |
| 64 | { |
| 65 | input[(outer * axisSize + i) * innerSize + inner]; |
| 66 | sum += std::exp((input.Get() - maxValue) * descriptor.m_Beta); |
| 67 | } |
| 68 | |
| 69 | // Compute log sum |
| 70 | const float logSum = std::log(sum); |
| 71 | |
| 72 | // Compute result |
| 73 | for (unsigned int i = 0u; i < axisSize; ++i) |
| 74 | { |
| 75 | const unsigned int index = (outer * axisSize + i) * innerSize + inner; |
| 76 | |
| 77 | input [index]; |
| 78 | output[index]; |
| 79 | |
| 80 | output.Set((input.Get() - maxValue) * descriptor.m_Beta - logSum); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } // namespace armnn |