ArmNN
 25.02
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
14 namespace armnn
15 {
16 
18 {
20 }
21 
22 void 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.
QueueDescriptor m_Data
Definition: Workload.hpp:74
void Execute() const override
unsigned int GetNumElements() const
Definition: Tensor.hpp:198
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:195
Copyright (c) 2021 ARM Limited and Contributors.
void Gather(const TensorInfo &paramsInfo, const TensorInfo &indicesInfo, const TensorInfo &outputInfo, Decoder< I > &params, const int32_t *indices, Encoder< O > &output, const int32_t axis_int)
Definition: Gather.cpp:15
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
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)
std::vector< ITensorHandle * > m_Inputs
std::vector< ITensorHandle * > m_Outputs