1 //
2 // Copyright © 2019-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "ComparisonLayer.hpp"
7
8 #include "LayerCloneBase.hpp"
9
10 #include <armnn/backends/WorkloadData.hpp>
11 #include <armnn/backends/WorkloadFactory.hpp>
12
13 #include <algorithm>
14
15 namespace armnn
16 {
17
ComparisonLayer(const ComparisonDescriptor & param,const char * name)18 ComparisonLayer::ComparisonLayer(const ComparisonDescriptor& param, const char* name)
19 : LayerWithParameters(2, 1, LayerType::Comparison, param, name)
20 {
21 }
22
CreateWorkload(const IWorkloadFactory & factory) const23 std::unique_ptr<IWorkload> ComparisonLayer::CreateWorkload(const IWorkloadFactory& factory) const
24 {
25 ComparisonQueueDescriptor descriptor;
26 SetAdditionalInfo(descriptor);
27
28 return factory.CreateWorkload(LayerType::Comparison,descriptor, PrepInfoAndDesc(descriptor));
29 }
30
Clone(Graph & graph) const31 ComparisonLayer* ComparisonLayer::Clone(Graph& graph) const
32 {
33 return CloneBase<ComparisonLayer>(graph, m_Param, GetName());
34 }
35
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const36 std::vector<TensorShape> ComparisonLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
37 {
38 ARMNN_ASSERT(inputShapes.size() == 2);
39 TensorShape input0 = inputShapes[0];
40 TensorShape input1 = inputShapes[1];
41
42 if (inputShapes[0].GetNumDimensions() < inputShapes[1].GetNumDimensions())
43 {
44 input1 = inputShapes[0];
45 input0 = inputShapes[1];
46 }
47 unsigned int numDims = input0.GetNumDimensions();
48 unsigned int shiftedDims = input0.GetNumDimensions() - input1.GetNumDimensions();
49
50 // Get the max of the inputs.
51 std::vector<unsigned int> dims(numDims);
52 for (unsigned int i = shiftedDims; i < numDims; i++)
53 {
54 unsigned int dim0 = input0[i];
55 unsigned int dim1 = input1[i - shiftedDims];
56
57 // Validate inputs are broadcast compatible.
58 ARMNN_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1,
59 "Dimensions should either match or one should be of size 1.");
60
61 dims[i] = std::max(dim0, dim1);
62 }
63
64 // Fill in the rest of the shifted dimensions.
65 for (unsigned int i = 0; i < shiftedDims; i++)
66 {
67 dims[i] = input0[i];
68 }
69
70 return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
71 }
72
ValidateTensorShapesFromInputs()73 void ComparisonLayer::ValidateTensorShapesFromInputs()
74 {
75 VerifyLayerConnections(2, CHECK_LOCATION());
76
77 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
78
79 VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
80
81 std::vector<TensorShape> inferredShapes = InferOutputShapes({
82 GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
83 GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()
84 });
85 ARMNN_ASSERT(inferredShapes.size() == 1);
86
87 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "ComparisonLayer");
88 }
89
ExecuteStrategy(IStrategy & strategy) const90 void ComparisonLayer::ExecuteStrategy(IStrategy& strategy) const
91 {
92 strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
93 }
94
95 } // namespace armnn
96