ArmNN
 25.11
Loading...
Searching...
No Matches
RefGatherNdWorkload.cpp
Go to the documentation of this file.
1//
2// Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <fmt/format.h>
8
9#include "Gather.hpp"
10#include "Profiling.hpp"
11#include "RefWorkloadUtils.hpp"
13
14namespace armnn
15{
16
18{
19 Execute(m_Data.m_Inputs, m_Data.m_Outputs);
20}
21
22void RefGatherNdWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const
23{
24 ARMNN_SCOPED_PROFILING_EVENT_REF_NAME_GUID("RefGatherNdWorkload_Execute");
25
26 const TensorInfo& inputInfo0 = GetTensorInfo(inputs[0]);
27 const TensorInfo& inputInfo1 = GetTensorInfo(inputs[1]);
28 const TensorInfo& outputInfo = GetTensorInfo(outputs[0]);
29
30 std::unique_ptr<Decoder<float>> params_decoderPtr = MakeDecoder<float>(inputInfo0, inputs[0]->Map());
31
32 const int32_t* indicesDataPtr = reinterpret_cast<int32_t*>(inputs[1]->Map());
33 std::vector<int32_t> indices(indicesDataPtr, indicesDataPtr + inputInfo1.GetNumElements());
34 // Check for negative indices, it could not be checked in validate as we do not have access to the values there
35 for (unsigned int i = 0; i < inputInfo1.GetNumElements(); ++i)
36 {
37 if (indices[i] < 0)
38 {
39 throw InvalidArgumentException((fmt::format("GatherNd: indices[{}] < 0", i)));
40 }
41 }
42
43 std::unique_ptr<Encoder<float>> output_encoderPtr = MakeEncoder<float>(outputInfo, outputs[0]->Map());
44
45 std::map<std::string, unsigned int> keyIndices = CalculateGatherNdKeyIndices(inputInfo0, inputInfo1);
46
47 /// Calculate flattened indices: flattenedIndices = indices * flattenedCoefficients
48 // Calculate the flattened coefficients to use in the multiplication
49 // to calculate the flattened indices needed by gather
50 TensorShape paramsShape = inputInfo0.GetShape();
51 std::vector<unsigned int> flattenedCoeff(keyIndices["ND"], 1);
52 for (unsigned int i = 1; i < keyIndices["ND"]; ++i)
53 {
54 flattenedCoeff[i-1] = paramsShape[i];
55 }
56 for (unsigned int i = keyIndices["ND"]-1; i > 0; --i)
57 {
58 flattenedCoeff[i-1] *= flattenedCoeff[i];
59 }
60
61 // Prepare the vector to store the output of the matrix multiplication,
62 // which will represent the flattened indices needed by gather
63 armnn::TensorInfo flattenedIndices_Info = inputInfo1;
64 flattenedIndices_Info.SetShape({ keyIndices["W"] });
65 std::vector<int32_t> flattenedIndices(flattenedIndices_Info.GetNumElements(), 0);
66
67 // Multiplication to calculate the flattened indices, which are the indices needed by gather.
68 for (unsigned int i = 0; i < keyIndices["W"]; ++i)
69 {
70 for (unsigned int j = 0; j < keyIndices["ND"]; ++j)
71 {
72 flattenedIndices[i] += indices[i * keyIndices["ND"] + j] * static_cast<int32_t>(flattenedCoeff[j]);
73 }
74 }
75
76 /// Call Gather with adequate shapes
77 // Reshape params into {K, C}
78 armnn::TensorInfo params_K_C_Info = inputInfo0;
79 params_K_C_Info.SetShape({ keyIndices["K"], keyIndices["C"] });
80
81 // Reshape indices into {N, W}
82 armnn::TensorInfo indices_N_W_Info = inputInfo1;
83 indices_N_W_Info.SetShape({ keyIndices["N"], keyIndices["W"] });
84
85 // Reshape output to have the shape given by gather {N, W, C}
86 // (the original outputInfo has the shape given by gatherNd)
87 armnn::TensorInfo outputGather_Info = outputInfo;
88 outputGather_Info.SetShape({ keyIndices["N"], keyIndices["W"], keyIndices["C"] });
89
90 // output_gather = gather(params_K_C, indices_N_W)
91 Gather(params_K_C_Info, indices_N_W_Info, outputGather_Info,
92 *params_decoderPtr, flattenedIndices.data(), *output_encoderPtr, 0);
93}
94
95} //namespace armnn
#define ARMNN_SCOPED_PROFILING_EVENT_REF_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
const TensorShape & GetShape() const
Definition Tensor.hpp:193
unsigned int GetNumElements() const
Definition Tensor.hpp:198
void SetShape(const TensorShape &newShape)
Definition Tensor.hpp:195
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< Decoder< T > > MakeDecoder(const TensorInfo &info, const void *data=nullptr)
std::unique_ptr< Encoder< T > > MakeEncoder(const TensorInfo &info, void *data=nullptr)
std::map< std::string, unsigned int > CalculateGatherNdKeyIndices(TensorInfo inputInfo0, TensorInfo inputInfo1)
Calculates the key index values needed for GatherNd: N, ND, K, W, C (N is always 1)
armnn::TensorInfo GetTensorInfo(unsigned int numberOfBatches, unsigned int numberOfChannels, unsigned int height, unsigned int width, const armnn::DataLayout dataLayout, const armnn::DataType dataType)