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