narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 1 | // |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 2 | // Copyright © 2017,2022 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> |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 9 | #include <armnn/utility/NumericCast.hpp> |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 10 | |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 11 | namespace armnn |
| 12 | { |
| 13 | |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 14 | void Gather(const TensorInfo& paramsInfo, |
| 15 | const TensorInfo& indicesInfo, |
| 16 | const TensorInfo& outputInfo, |
Ellen Norris-Thompson | 6858d3f | 2019-06-21 15:50:00 +0100 | [diff] [blame] | 17 | Decoder<float>& params, |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 18 | const int32_t* indices, |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 19 | Encoder<float>& output, |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 20 | const int32_t axis_int) |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 21 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 22 | IgnoreUnused(outputInfo); |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 23 | |
| 24 | const int paramsRank = static_cast<int>(paramsInfo.GetNumDimensions()); |
| 25 | ARMNN_ASSERT(-1 * paramsRank <= axis_int && axis_int < paramsRank); |
| 26 | const unsigned int axis = (axis_int < 0) ? static_cast<unsigned int>(paramsRank + axis_int) |
| 27 | : static_cast<unsigned int>(axis_int); |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 28 | |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 29 | const TensorShape& paramsShape = paramsInfo.GetShape(); |
| 30 | |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 31 | // Product of all dimensions to the left side of the axis |
| 32 | unsigned int paramsOuterProduct = 1; |
| 33 | for (unsigned int i = 0; i < axis; ++i) |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 34 | { |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 35 | paramsOuterProduct *= paramsShape[i]; |
| 36 | } |
| 37 | // Product of all dimensions to the right side of the axis |
| 38 | unsigned int paramsInnerProduct = 1; |
| 39 | for (unsigned int k = 1 + axis; k < paramsInfo.GetNumDimensions(); ++k) |
| 40 | { |
| 41 | paramsInnerProduct *= paramsShape[k]; |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 44 | unsigned int offset = 0; |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 45 | unsigned int outIndex = 0; |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 46 | for (unsigned int i = 0; i < paramsOuterProduct; ++i) |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 47 | { |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 48 | for (unsigned int j = 0; j < indicesInfo.GetNumElements(); ++j) |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 49 | { |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 50 | unsigned int index = armnn::numeric_cast<unsigned int>(indices[j]); |
| 51 | ARMNN_ASSERT(indices[j] >= 0 && index < paramsShape[axis]); |
| 52 | |
| 53 | unsigned int startOffset = (paramsInnerProduct * index) + offset; |
| 54 | unsigned int endOffset = startOffset + paramsInnerProduct; |
| 55 | |
| 56 | for (unsigned int k = startOffset; k < endOffset; ++k) |
| 57 | { |
| 58 | params[k]; |
| 59 | float outputValue = params.Get(); |
| 60 | output[outIndex]; |
| 61 | output.Set(outputValue); |
| 62 | ++outIndex; |
| 63 | } |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 64 | } |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 65 | offset += paramsShape[axis] * paramsInnerProduct; |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 68 | ARMNN_ASSERT(outIndex == outputInfo.GetNumElements()); |
narpra01 | 4951d84 | 2019-01-18 16:53:53 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Nikhil Raj | 369d8fc | 2022-11-24 13:12:36 +0000 | [diff] [blame] | 71 | } //namespace armnn |