narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 1 | // |
Kevin May | 49f8d6a | 2023-06-01 16:42:05 +0100 | [diff] [blame] | 2 | // Copyright © 2017,2022-2023 Arm Ltd and Contributors. All rights reserved. |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "Gather.hpp" |
| 7 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 8 | #include <armnn/backends/WorkloadData.hpp> |
Kevin May | 49f8d6a | 2023-06-01 16:42:05 +0100 | [diff] [blame] | 9 | |
| 10 | #include <fmt/format.h> |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 11 | |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 12 | namespace armnn |
| 13 | { |
| 14 | |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 15 | void Gather(const TensorInfo& paramsInfo, |
| 16 | const TensorInfo& indicesInfo, |
| 17 | const TensorInfo& outputInfo, |
Ellen Norris-Thompson | 6858d3f | 2019-06-21 15:50:00 +0100 | [diff] [blame] | 18 | Decoder<float>& params, |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 19 | const int32_t* indices, |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 20 | Encoder<float>& output, |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 21 | const int32_t axis_int) |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 22 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 23 | IgnoreUnused(outputInfo); |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 24 | |
| 25 | const int paramsRank = static_cast<int>(paramsInfo.GetNumDimensions()); |
Kevin May | 49f8d6a | 2023-06-01 16:42:05 +0100 | [diff] [blame] | 26 | if((axis_int < -1 * paramsRank) || (paramsRank <= axis_int)) |
| 27 | { |
| 28 | throw InvalidArgumentException((fmt::format("Gather: Axis {} is not within [-{}, {}) range", |
| 29 | axis_int, paramsRank, paramsRank))); |
| 30 | } |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 31 | const unsigned int axis = (axis_int < 0) ? static_cast<unsigned int>(paramsRank + axis_int) |
| 32 | : static_cast<unsigned int>(axis_int); |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 33 | |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 34 | const TensorShape& paramsShape = paramsInfo.GetShape(); |
| 35 | |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 36 | // Product of all dimensions to the left side of the axis |
| 37 | unsigned int paramsOuterProduct = 1; |
| 38 | for (unsigned int i = 0; i < axis; ++i) |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 39 | { |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 40 | paramsOuterProduct *= paramsShape[i]; |
| 41 | } |
| 42 | // Product of all dimensions to the right side of the axis |
| 43 | unsigned int paramsInnerProduct = 1; |
| 44 | for (unsigned int k = 1 + axis; k < paramsInfo.GetNumDimensions(); ++k) |
| 45 | { |
| 46 | paramsInnerProduct *= paramsShape[k]; |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 49 | unsigned int offset = 0; |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 50 | unsigned int outIndex = 0; |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 51 | for (unsigned int i = 0; i < paramsOuterProduct; ++i) |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 52 | { |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 53 | for (unsigned int j = 0; j < indicesInfo.GetNumElements(); ++j) |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 54 | { |
Kevin May | 49f8d6a | 2023-06-01 16:42:05 +0100 | [diff] [blame] | 55 | unsigned int index = |
| 56 | (indices[j] < 0) ? static_cast<unsigned int>(static_cast<int>(paramsShape[axis]) + indices[j]) |
| 57 | : static_cast<unsigned int>(indices[j]); |
| 58 | |
| 59 | if (index >= paramsShape[axis]) |
| 60 | { |
| 61 | throw InvalidArgumentException((fmt::format("Gather: index >= paramsShape[axis]: {} >= {}", |
| 62 | index, paramsShape[axis] ))); |
| 63 | } |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 64 | |
| 65 | unsigned int startOffset = (paramsInnerProduct * index) + offset; |
| 66 | unsigned int endOffset = startOffset + paramsInnerProduct; |
| 67 | |
| 68 | for (unsigned int k = startOffset; k < endOffset; ++k) |
| 69 | { |
| 70 | params[k]; |
| 71 | float outputValue = params.Get(); |
| 72 | output[outIndex]; |
| 73 | output.Set(outputValue); |
| 74 | ++outIndex; |
| 75 | } |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 76 | } |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 77 | offset += paramsShape[axis] * paramsInnerProduct; |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kevin May | 49f8d6a | 2023-06-01 16:42:05 +0100 | [diff] [blame] | 80 | if (outIndex != outputInfo.GetNumElements()) |
| 81 | { |
| 82 | throw InvalidArgumentException((fmt::format("Gather: Invalid outIndex {} ", outIndex))); |
| 83 | } |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 86 | } //namespace armnn |