narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 1 | // |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 6 | #include "Reduce.hpp" |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 7 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 8 | #include <armnn/utility/NumericCast.hpp> |
| 9 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 10 | #include <armnn/backends/WorkloadData.hpp> |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 11 | |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 12 | #include <cstddef> |
| 13 | #include <functional> |
| 14 | #include <limits> |
| 15 | |
| 16 | namespace armnn |
| 17 | { |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 18 | |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 19 | bool NextIndex(const unsigned int numDims, const armnn::TensorShape& dims, std::vector<unsigned int>& current) |
| 20 | { |
| 21 | unsigned int carry = 1; |
| 22 | |
| 23 | for (unsigned int idx = numDims; idx-- > 0; ) |
| 24 | { |
| 25 | unsigned int current_val = current[idx] + carry; |
| 26 | if (dims[idx] == current_val) |
| 27 | { |
| 28 | current[idx] = 0; |
| 29 | } |
| 30 | else |
| 31 | { |
| 32 | current[idx] = current_val; |
| 33 | carry = 0; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | return (carry == 0); |
| 38 | } |
| 39 | |
James Conroy | 4d1ff58 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 40 | unsigned int ReducedOutputOffset(const unsigned int numDims, |
| 41 | const armnn::TensorShape& dims, |
| 42 | std::vector<unsigned int>& index, |
| 43 | const unsigned int numAxis, |
| 44 | const std::vector<unsigned int>& axis) |
| 45 | { |
| 46 | unsigned int offset = 0; |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 47 | for (unsigned int idx = 0; idx < numDims; ++idx) |
| 48 | { |
| 49 | bool isAxis = false; |
| 50 | if (!axis.empty()) |
| 51 | { |
| 52 | for (unsigned int axisIdx = 0; axisIdx < numAxis; ++axisIdx) |
| 53 | { |
| 54 | if (idx == axis[axisIdx]) |
| 55 | { |
| 56 | isAxis = true; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | if (!isAxis) |
| 62 | { |
James Conroy | 4d1ff58 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 63 | offset = offset * dims[idx] + index[idx]; |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | return offset; |
| 67 | } |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 68 | |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 69 | |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 70 | void Reduce(const TensorInfo& inputInfo, |
| 71 | const TensorInfo& outputInfo, |
| 72 | Decoder<float>& input, |
| 73 | Encoder<float>& output, |
| 74 | const std::vector<uint32_t> axis, |
| 75 | const ReduceOperation reduceOperation) |
| 76 | { |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 77 | armnn::TensorShape inputDims = inputInfo.GetShape(); |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 78 | unsigned int inputNumDims = inputInfo.GetNumDimensions(); |
| 79 | unsigned int numOutputs = outputInfo.GetNumElements(); |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 80 | |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 81 | // Initialise temp output |
| 82 | std::vector<float> tempOut(numOutputs); |
Teresa Charlin | 2226ca9 | 2021-02-11 23:05:40 +0000 | [diff] [blame] | 83 | switch(reduceOperation) |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 84 | { |
Teresa Charlin | 2226ca9 | 2021-02-11 23:05:40 +0000 | [diff] [blame] | 85 | case ReduceOperation::Mean: |
| 86 | case ReduceOperation::Sum: |
Rob Hughes | c013bc8 | 2021-07-14 09:31:31 +0100 | [diff] [blame] | 87 | std::fill(tempOut.begin(), tempOut.end(), 0.0f); |
Teresa Charlin | 2226ca9 | 2021-02-11 23:05:40 +0000 | [diff] [blame] | 88 | break; |
Teresa Charlin | 4e3e831 | 2021-08-05 12:34:37 +0100 | [diff] [blame] | 89 | case ReduceOperation::Prod: |
| 90 | std::fill(tempOut.begin(), tempOut.end(), 1.0f); |
| 91 | break; |
Teresa Charlin | 2226ca9 | 2021-02-11 23:05:40 +0000 | [diff] [blame] | 92 | case ReduceOperation::Max: |
| 93 | std::fill(tempOut.begin(), tempOut.end(), -1 * std::numeric_limits<float>::max()); |
| 94 | break; |
| 95 | case ReduceOperation::Min: |
| 96 | std::fill(tempOut.begin(), tempOut.end(), std::numeric_limits<float>::max()); |
| 97 | break; |
| 98 | default: |
| 99 | throw armnn::InvalidArgumentException("Unknown reduce method: " + |
| 100 | std::to_string(static_cast<int>(reduceOperation))); |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 103 | // Initialise temp index |
| 104 | std::vector<unsigned int> tempIndex(inputNumDims, 0); |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 105 | |
| 106 | std::vector<unsigned int> resolvedAxis = axis; |
| 107 | if (resolvedAxis.empty()) |
| 108 | { |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 109 | for (unsigned int idx = 0; idx < inputNumDims; ++idx) |
| 110 | { |
| 111 | resolvedAxis.push_back(idx); |
| 112 | } |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 113 | } |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 114 | auto numResolvedAxis = armnn::numeric_cast<unsigned int>(resolvedAxis.size()); |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 115 | |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 116 | // Iterates through input_data and operates over the reduced axis |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 117 | for (bool hasNext = true; hasNext; hasNext = NextIndex(inputNumDims, inputDims, tempIndex)) |
| 118 | { |
James Conroy | 4d1ff58 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 119 | unsigned int inputOffset = ReducedOutputOffset(inputNumDims, inputDims, tempIndex, 0, {}); |
| 120 | unsigned int outputOffset = ReducedOutputOffset(inputNumDims, inputDims, tempIndex, |
| 121 | numResolvedAxis, resolvedAxis); |
| 122 | input[inputOffset]; |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 123 | auto inputValue = input.Get(); |
Teresa Charlin | 4e3e831 | 2021-08-05 12:34:37 +0100 | [diff] [blame] | 124 | switch(reduceOperation) |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 125 | { |
Teresa Charlin | 4e3e831 | 2021-08-05 12:34:37 +0100 | [diff] [blame] | 126 | case ReduceOperation::Mean: |
| 127 | case ReduceOperation::Sum: |
| 128 | tempOut[outputOffset] += inputValue; |
| 129 | break; |
| 130 | case ReduceOperation::Prod: |
| 131 | tempOut[outputOffset] *= inputValue; |
| 132 | break; |
| 133 | case ReduceOperation::Max: |
| 134 | if (inputValue > tempOut[outputOffset]) |
| 135 | { |
| 136 | tempOut[outputOffset] = inputValue; |
| 137 | } |
| 138 | break; |
| 139 | case ReduceOperation::Min: |
| 140 | if (inputValue < tempOut[outputOffset]) |
| 141 | { |
| 142 | tempOut[outputOffset] = inputValue; |
| 143 | } |
| 144 | break; |
| 145 | default: |
| 146 | throw armnn::InvalidArgumentException("Unknown reduce method: " + |
| 147 | std::to_string(static_cast<int>(reduceOperation))); |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 148 | } |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 151 | // Takes average by num of elements added to get MEAN |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 152 | size_t numElementsInAxis = 1; |
| 153 | for (unsigned int idx = 0; idx < numResolvedAxis; ++idx) |
| 154 | { |
James Conroy | 4d1ff58 | 2019-06-10 17:06:39 +0100 | [diff] [blame] | 155 | unsigned int current = inputDims[resolvedAxis[idx]]; |
Matthew Sloyan | 24ac859 | 2020-09-23 16:57:23 +0100 | [diff] [blame] | 156 | ARMNN_ASSERT(armnn::numeric_cast<float>(current) < |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 157 | (std::numeric_limits<float>::max() / armnn::numeric_cast<float>(numElementsInAxis))); |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 158 | numElementsInAxis *= current; |
| 159 | } |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 160 | |
| 161 | for (unsigned int idx = 0; idx < numOutputs; ++idx) |
| 162 | { |
| 163 | output[idx]; |
| 164 | if (reduceOperation == ReduceOperation::Mean) |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 165 | { |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 166 | if (numElementsInAxis > 0) |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 167 | { |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 168 | output.Set(tempOut[idx] / armnn::numeric_cast<float>(numElementsInAxis)); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 169 | } |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
| 173 | output.Set(tempOut[idx]); |
narpra01 | 1e4c31d | 2018-09-28 11:07:51 +0100 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 177 | |
| 178 | } //namespace armnn |