1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "GatherNdLayer.hpp"
7 #include "LayerCloneBase.hpp"
8
9 #include <armnn/TypesUtils.hpp>
10 #include <armnn/backends/WorkloadData.hpp>
11 #include <armnn/backends/WorkloadFactory.hpp>
12
13 namespace armnn
14 {
15
GatherNdLayer(const char * name)16 GatherNdLayer::GatherNdLayer(const char* name)
17 : Layer(2, 1, LayerType::GatherNd, name)
18 {
19 }
20
CreateWorkload(const armnn::IWorkloadFactory & factory) const21 std::unique_ptr<IWorkload> GatherNdLayer::CreateWorkload(const armnn::IWorkloadFactory& factory) const
22 {
23 GatherNdQueueDescriptor descriptor;
24 SetAdditionalInfo(descriptor);
25
26 return factory.CreateWorkload(LayerType::GatherNd, descriptor, PrepInfoAndDesc(descriptor));
27 }
28
Clone(Graph & graph) const29 GatherNdLayer* GatherNdLayer::Clone(Graph& graph) const
30 {
31 return CloneBase<GatherNdLayer>(graph, GetName());
32 }
33
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const34 std::vector<TensorShape> GatherNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
35 {
36 ARMNN_ASSERT(inputShapes.size() == 2);
37 const TensorShape& params = inputShapes[0];
38 const TensorShape& indices = inputShapes[1];
39
40 if (indices.GetDimensionality() == Dimensionality::Scalar && indices.GetNumDimensions() == 1)
41 {
42 return std::vector<TensorShape>({ TensorShape(Dimensionality::Scalar)});
43 }
44
45 const unsigned int paramsDim = params.GetNumDimensions();
46 const unsigned int indicesDim = indices.GetNumDimensions();
47
48 // last dimension of indices
49 unsigned int index_depth = indices[indicesDim - 1];
50 ARMNN_ASSERT(index_depth <= paramsDim);
51
52 // all but the last dimension of indices
53 std::vector<unsigned int> outer_shape;
54 outer_shape.reserve(indicesDim - 1);
55 for (unsigned int i = 0; i < indicesDim - 1; ++i)
56 {
57 outer_shape.emplace_back(indices[i]);
58 }
59
60 // elements after index_depth
61 std::vector<unsigned int> inner_shape;
62 inner_shape.reserve(paramsDim - index_depth);
63 for (unsigned int i = index_depth; i < paramsDim; ++i)
64 {
65 inner_shape.emplace_back(params[i]);
66 }
67
68 // concatenate outer_shape + inner_shape
69 std::vector<unsigned int> output_shape;
70 output_shape.reserve( outer_shape.size() + inner_shape.size() );
71 output_shape.insert( output_shape.end(), outer_shape.begin(), outer_shape.end() );
72 output_shape.insert( output_shape.end(), inner_shape.begin(), inner_shape.end() );
73
74 const auto outputDim = static_cast<unsigned int>(output_shape.size());
75 return std::vector<TensorShape>({ TensorShape({outputDim, output_shape.data()})});
76 }
77
ValidateTensorShapesFromInputs()78 void GatherNdLayer::ValidateTensorShapesFromInputs()
79 {
80 VerifyLayerConnections(2, CHECK_LOCATION());
81
82 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
83
84 VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
85
86 std::vector<TensorShape> inferredShapes = InferOutputShapes(
87 {GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
88 GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()});
89 ARMNN_ASSERT(inferredShapes.size() == 1);
90 ARMNN_ASSERT(inferredShapes[0].GetDimensionality() == Dimensionality::Specified ||
91 inferredShapes[0].GetDimensionality() == Dimensionality::Scalar);
92
93 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "GatherNdLayer");
94 }
95
96 } // namespace armnn
97