aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/Gather.cpp
blob: 48039432a5ae93c2bba8cf31d03e981e08d95779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Copyright © 2017,2022 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "Gather.hpp"

#include <armnn/backends/WorkloadData.hpp>
#include <armnn/utility/NumericCast.hpp>

namespace armnn
{

void Gather(const TensorInfo& paramsInfo,
            const TensorInfo& indicesInfo,
            const TensorInfo& outputInfo,
            Decoder<float>& params,
            const int32_t* indices,
            Encoder<float>& output,
            const int32_t axis_int)
{
    IgnoreUnused(outputInfo);

    const int paramsRank = static_cast<int>(paramsInfo.GetNumDimensions());
    ARMNN_ASSERT(-1 * paramsRank <= axis_int && axis_int < paramsRank);
    const unsigned int axis = (axis_int < 0) ? static_cast<unsigned int>(paramsRank + axis_int)
                                             : static_cast<unsigned int>(axis_int);

    const TensorShape& paramsShape = paramsInfo.GetShape();

    // Product of all dimensions to the left side of the axis
    unsigned int paramsOuterProduct = 1;
    for (unsigned int i = 0; i < axis; ++i)
    {
        paramsOuterProduct *= paramsShape[i];
    }
    // Product of all dimensions to the right side of the axis
    unsigned int paramsInnerProduct = 1;
    for (unsigned int k = 1 + axis; k < paramsInfo.GetNumDimensions(); ++k)
    {
        paramsInnerProduct *= paramsShape[k];
    }

    unsigned int offset = 0;
    unsigned int outIndex = 0;
    for (unsigned int i = 0; i < paramsOuterProduct; ++i)
    {
        for (unsigned int j = 0; j < indicesInfo.GetNumElements(); ++j)
        {
            unsigned int index = armnn::numeric_cast<unsigned int>(indices[j]);
            ARMNN_ASSERT(indices[j] >= 0 && index < paramsShape[axis]);

            unsigned int startOffset = (paramsInnerProduct * index) + offset;
            unsigned int endOffset = startOffset + paramsInnerProduct;

            for (unsigned int k = startOffset; k < endOffset; ++k)
            {
                params[k];
                float outputValue = params.Get();
                output[outIndex];
                output.Set(outputValue);
                ++outIndex;
            }
        }
        offset += paramsShape[axis] * paramsInnerProduct;
    }

    ARMNN_ASSERT(outIndex == outputInfo.GetNumElements());
}

} //namespace armnn