1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "GatherLayer.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
GatherLayer(const GatherDescriptor & param,const char * name)16 GatherLayer::GatherLayer(const GatherDescriptor& param, const char* name)
17 : LayerWithParameters(2, 1, LayerType::Gather, param, name)
18 {
19 }
20
CreateWorkload(const armnn::IWorkloadFactory & factory) const21 std::unique_ptr<IWorkload> GatherLayer::CreateWorkload(const armnn::IWorkloadFactory& factory) const
22 {
23 GatherQueueDescriptor descriptor;
24 SetAdditionalInfo(descriptor);
25
26 return factory.CreateWorkload(LayerType::Gather, descriptor, PrepInfoAndDesc(descriptor));
27 }
28
Clone(Graph & graph) const29 GatherLayer* GatherLayer::Clone(Graph& graph) const
30 {
31 return CloneBase<GatherLayer>(graph, m_Param, GetName());
32 }
33
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const34 std::vector<TensorShape> GatherLayer::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 const unsigned int outputDim = paramsDim - 1 + indicesDim;
48
49 std::vector<unsigned int> dimSizes;
50
51 unsigned int axis = static_cast<unsigned int>(m_Param.m_Axis);
52 if (m_Param.m_Axis < 0)
53 {
54 int32_t axis_aux = static_cast<int32_t>(paramsDim) + m_Param.m_Axis;
55 axis = static_cast<unsigned int> (axis_aux);
56 }
57
58 for (unsigned int i = 0; i < axis; ++i)
59 {
60 dimSizes.push_back(params[i]);
61 }
62 for (unsigned int i = axis; i < indicesDim + axis; ++i)
63 {
64 dimSizes.push_back(indices[i - axis]);
65 }
66 for (unsigned int i = 1 + axis; i < paramsDim; ++i)
67 {
68 dimSizes.push_back(params[i]);
69 }
70
71 return std::vector<TensorShape>({ TensorShape({outputDim, dimSizes.data()})});
72 }
73
ValidateTensorShapesFromInputs()74 void GatherLayer::ValidateTensorShapesFromInputs()
75 {
76 VerifyLayerConnections(2, CHECK_LOCATION());
77
78 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
79
80 VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
81
82 std::vector<TensorShape> inferredShapes = InferOutputShapes(
83 {GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
84 GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()});
85 ARMNN_ASSERT(inferredShapes.size() == 1);
86 ARMNN_ASSERT(inferredShapes[0].GetDimensionality() == Dimensionality::Specified ||
87 inferredShapes[0].GetDimensionality() == Dimensionality::Scalar);
88
89 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "GatherLayer");
90 }
91
ExecuteStrategy(IStrategy & strategy) const92 void GatherLayer::ExecuteStrategy(IStrategy& strategy) const
93 {
94 strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
95 }
96
97 } // namespace armnn
98