ArmNN
 22.05
RefGatherNdWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include "Gather.hpp"
9 #include "Profiling.hpp"
10 #include "RefWorkloadUtils.hpp"
12 
13 namespace armnn
14 {
15 
17 {
19 }
20 
22 {
23  Execute(workingMemDescriptor.m_Inputs, workingMemDescriptor.m_Outputs);
24 }
25 
26 void RefGatherNdWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const
27 {
28  ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefGatherNdWorkload_Execute");
29 
30  const TensorInfo& inputInfo0 = GetTensorInfo(inputs[0]);
31  const TensorInfo& inputInfo1 = GetTensorInfo(inputs[1]);
32  const TensorInfo& outputInfo = GetTensorInfo(outputs[0]);
33 
34  std::unique_ptr<Decoder<float>> params_decoderPtr = MakeDecoder<float>(inputInfo0, inputs[0]->Map());
35 
36  const int32_t* indicesDataPtr = reinterpret_cast<int32_t*>(inputs[1]->Map());
37  std::vector<int32_t> indices(indicesDataPtr, indicesDataPtr + inputInfo1.GetNumElements());
38 
39  std::unique_ptr<Encoder<float>> output_encoderPtr = MakeEncoder<float>(outputInfo, outputs[0]->Map());
40 
41  std::map<std::string, unsigned int> keyIndices = CalculateGatherNdKeyIndices(inputInfo0, inputInfo1);
42 
43  /// Calculate flattened indices: flattenedIndices = indices * flattenedCoefficients
44  // Calculate the flattened coefficients to use in the multiplication
45  // to calculate the flattened indices needed by gather
46  TensorShape paramsShape = inputInfo0.GetShape();
47  std::vector<unsigned int> flattenedCoeff(keyIndices["ND"], 1);
48  for (unsigned int i = 1; i < keyIndices["ND"]; ++i)
49  {
50  flattenedCoeff[i-1] = paramsShape[i];
51  }
52  for (unsigned int i = keyIndices["ND"]-1; i > 0; --i)
53  {
54  flattenedCoeff[i-1] *= flattenedCoeff[i];
55  }
56 
57  // Prepare the vector to store the output of the matrix multiplication,
58  // which will represent the flattened indices needed by gather
59  armnn::TensorInfo flattenedIndices_Info = inputInfo1;
60  flattenedIndices_Info.SetShape({ keyIndices["W"] });
61  std::vector<int32_t> flattenedIndices(flattenedIndices_Info.GetNumElements(), 0);
62 
63  // Multiplication to calculate the flattened indices, which are the indices needed by gather.
64  for (unsigned int i = 0; i < keyIndices["W"]; ++i)
65  {
66  for (unsigned int j = 0; j < keyIndices["ND"]; ++j)
67  {
68  flattenedIndices[i] += indices[i * keyIndices["ND"] + j] * static_cast<int32_t>(flattenedCoeff[j]);
69  }
70  }
71 
72  /// Call Gather with adequate shapes
73  // Reshape params into {K, C}
74  armnn::TensorInfo params_K_C_Info = inputInfo0;
75  params_K_C_Info.SetShape({ keyIndices["K"], keyIndices["C"] });
76 
77  // Reshape indices into {N, W}
78  armnn::TensorInfo indices_N_W_Info = inputInfo1;
79  indices_N_W_Info.SetShape({ keyIndices["N"], keyIndices["W"] });
80 
81  // Reshape output to have the shape given by gather {N, W, C}
82  // (the original outputInfo has the shape given by gatherNd)
83  armnn::TensorInfo outputGather_Info = outputInfo;
84  outputGather_Info.SetShape({ keyIndices["N"], keyIndices["W"], keyIndices["C"] });
85 
86  // output_gather = gather(params_K_C, indices_N_W)
87  Gather(params_K_C_Info, indices_N_W_Info, outputGather_Info,
88  *params_decoderPtr, flattenedIndices.data(), *output_encoderPtr, 0);
89 }
90 
91 } //namespace armnn
void ExecuteAsync(WorkingMemDescriptor &workingMemDescriptor) override
CPU Execution: Reference C++ kernels.
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) ...
Copyright (c) 2021 ARM Limited and Contributors.
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:193
void Execute() const override
std::vector< ITensorHandle * > m_Outputs
std::vector< ITensorHandle * > m_Inputs
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
unsigned int GetNumElements() const
Definition: Tensor.hpp:196