ArmNN
 22.05
ComparisonLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ComparisonLayer.hpp"
7 
8 #include "LayerCloneBase.hpp"
9 
12 
13 #include <algorithm>
14 
15 namespace armnn
16 {
17 
19  : LayerWithParameters(2, 1, LayerType::Comparison, param, name)
20 {
21 }
22 
23 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 
32 {
33  return CloneBase<ComparisonLayer>(graph, m_Param, GetName());
34 }
35 
36 std::vector<TensorShape> ComparisonLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
37 {
38  ARMNN_ASSERT(inputShapes.size() == 2);
39  const TensorShape& input0 = inputShapes[0];
40  const TensorShape& input1 = inputShapes[1];
41 
42  ARMNN_ASSERT(input0.GetNumDimensions() == input1.GetNumDimensions());
43  unsigned int numDims = input0.GetNumDimensions();
44 
45  std::vector<unsigned int> dims(numDims);
46  for (unsigned int i = 0; i < numDims; i++)
47  {
48  unsigned int dim0 = input0[i];
49  unsigned int dim1 = input1[i];
50 
51  ARMNN_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1,
52  "Dimensions should either match or one should be of size 1.");
53 
54  dims[i] = std::max(dim0, dim1);
55  }
56 
57  return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
58 }
59 
61 {
63 
64  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
65 
67 
68  std::vector<TensorShape> inferredShapes = InferOutputShapes({
71  });
72  ARMNN_ASSERT(inferredShapes.size() == 1);
73 
74  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "ComparisonLayer");
75 }
76 
78 void ComparisonLayer::Accept(ILayerVisitor& visitor) const
79 {
80  visitor.VisitComparisonLayer(this, GetParameters(), GetName());
81 }
83 
84 } // namespace armnn
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of ComparisonLayer.
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
By default returns inputShapes if the number of inputs are equal to number of outputs, otherwise infers the output shapes from given input shapes and layer properties.
ComparisonDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
#define ARMNN_NO_DEPRECATE_WARN_BEGIN
Definition: Deprecated.hpp:33
A ComparisonDescriptor for the ComparisonLayer.
Definition: Descriptors.hpp:89
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:491
Copyright (c) 2021 ARM Limited and Contributors.
const ComparisonDescriptor & GetParameters() const override
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:204
ComparisonLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:422
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:378
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:322
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
ARMNN_NO_DEPRECATE_WARN_BEGIN void Accept(ILayerVisitor &visitor) const override
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
ComparisonLayer(const ComparisonDescriptor &param, const char *name)
Constructor to create a ComparisonLayer.
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:274
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the Comparison type.
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:324
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:317
This layer represents a comparison operation.
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:421
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:467