ArmNN
 21.02
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.CreateComparison(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 {
79  visitor.VisitComparisonLayer(this, GetParameters(), GetName());
80 }
81 
82 } // 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 ComparisonDescriptor & GetParameters() const
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
A ComparisonDescriptor for the ComparisonLayer.
Definition: Descriptors.hpp:78
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:432
Copyright (c) 2021 ARM Limited and Contributors.
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
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:392
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:348
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:316
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
ComparisonLayer(const ComparisonDescriptor &param, const char *name)
Constructor to create a ComparisonLayer.
virtual std::unique_ptr< IWorkload > CreateComparison(const ComparisonQueueDescriptor &descriptor, const WorkloadInfo &Info) const
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:245
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
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:318
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:311
This layer represents a comparison operation.
virtual void VisitComparisonLayer(const IConnectableLayer *layer, const ComparisonDescriptor &comparisonDescriptor, const char *name=nullptr)=0
Function a Comparison layer should call back to when its Accept(ILayerVisitor&) function is invoked...
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:408
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:419