From 77bfb5e32faadb1383d48364a6f54adbff84ad80 Mon Sep 17 00:00:00 2001 From: Aron Virginas-Tar Date: Wed, 16 Oct 2019 17:45:38 +0100 Subject: IVGCVSW-3993 Add frontend and reference workload for ComparisonLayer * Added frontend for ComparisonLayer * Added RefComparisonWorkload * Deprecated and removed Equal and Greater layers and workloads * Updated tests to ensure backward compatibility Signed-off-by: Aron Virginas-Tar Change-Id: Id50c880be1b567c531efff919c0c366d0a71cbe9 --- src/armnn/layers/ComparisonLayer.cpp | 80 ++++++++++++++++++++++++++++++++++++ src/armnn/layers/ComparisonLayer.hpp | 50 ++++++++++++++++++++++ src/armnn/layers/EqualLayer.cpp | 39 ------------------ src/armnn/layers/EqualLayer.hpp | 38 ----------------- src/armnn/layers/GreaterLayer.cpp | 39 ------------------ src/armnn/layers/GreaterLayer.hpp | 39 ------------------ 6 files changed, 130 insertions(+), 155 deletions(-) create mode 100644 src/armnn/layers/ComparisonLayer.cpp create mode 100644 src/armnn/layers/ComparisonLayer.hpp delete mode 100644 src/armnn/layers/EqualLayer.cpp delete mode 100644 src/armnn/layers/EqualLayer.hpp delete mode 100644 src/armnn/layers/GreaterLayer.cpp delete mode 100644 src/armnn/layers/GreaterLayer.hpp (limited to 'src/armnn/layers') diff --git a/src/armnn/layers/ComparisonLayer.cpp b/src/armnn/layers/ComparisonLayer.cpp new file mode 100644 index 0000000000..75518e580e --- /dev/null +++ b/src/armnn/layers/ComparisonLayer.cpp @@ -0,0 +1,80 @@ +// +// Copyright © 2019 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "ComparisonLayer.hpp" + +#include "LayerCloneBase.hpp" + +#include +#include + +#include + +namespace armnn +{ + +ComparisonLayer::ComparisonLayer(const ComparisonDescriptor& param, const char* name) + : LayerWithParameters(2, 1, LayerType::Comparison, param, name) +{ +} + +std::unique_ptr ComparisonLayer::CreateWorkload(const Graph& graph, + const IWorkloadFactory& factory) const +{ + ComparisonQueueDescriptor descriptor; + return factory.CreateComparison(descriptor, PrepInfoAndDesc(descriptor, graph)); +} + +ComparisonLayer* ComparisonLayer::Clone(Graph& graph) const +{ + return CloneBase(graph, m_Param, GetName()); +} + +std::vector ComparisonLayer::InferOutputShapes(const std::vector& inputShapes) const +{ + BOOST_ASSERT(inputShapes.size() == 2); + const TensorShape& input0 = inputShapes[0]; + const TensorShape& input1 = inputShapes[1]; + + BOOST_ASSERT(input0.GetNumDimensions() == input1.GetNumDimensions()); + unsigned int numDims = input0.GetNumDimensions(); + + std::vector dims(numDims); + for (unsigned int i = 0; i < numDims; i++) + { + unsigned int dim0 = input0[i]; + unsigned int dim1 = input1[i]; + + BOOST_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1, + "Dimensions should either match or one should be of size 1."); + + dims[i] = std::max(dim0, dim1); + } + + return std::vector({ TensorShape(numDims, dims.data()) }); +} + +void ComparisonLayer::ValidateTensorShapesFromInputs() +{ + VerifyLayerConnections(2, CHECK_LOCATION()); + + std::vector inferredShapes = InferOutputShapes({ + GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(), + GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape() + }); + BOOST_ASSERT(inferredShapes.size() == 1); + + ConditionalThrowIfNotEqual( + "ComparisonLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.", + GetOutputSlot(0).GetTensorInfo().GetShape(), + inferredShapes[0]); +} + +void ComparisonLayer::Accept(ILayerVisitor& visitor) const +{ + visitor.VisitComparisonLayer(this, GetParameters(), GetName()); +} + +} // namespace armnn diff --git a/src/armnn/layers/ComparisonLayer.hpp b/src/armnn/layers/ComparisonLayer.hpp new file mode 100644 index 0000000000..bbc2b573bf --- /dev/null +++ b/src/armnn/layers/ComparisonLayer.hpp @@ -0,0 +1,50 @@ +// +// Copyright © 2019 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "LayerWithParameters.hpp" + +namespace armnn +{ + +/// This layer represents a comparison operation. +class ComparisonLayer : public LayerWithParameters +{ +public: + /// Makes a workload for the Comparison type + /// @param [in] graph The graph where this layer can be found + /// @param [in] factory The workload factory which will create the workload + /// @return A pointer to the created workload, or nullptr if not created + virtual std::unique_ptr CreateWorkload(const Graph& graph, + const IWorkloadFactory& factory) const override; + + /// Creates a dynamically-allocated copy of this layer + /// @param [in] graph The graph into which this layer is being cloned + ComparisonLayer* Clone(Graph& graph) 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. + /// @param [in] inputShapes The input shapes layer has. + /// @return A vector to the inferred output shape. + std::vector InferOutputShapes(const std::vector& inputShapes) const override; + + /// Check if the input tensor shape(s) will lead to a valid configuration + /// of @ref ComparisonLayer + void ValidateTensorShapesFromInputs() override; + + void Accept(ILayerVisitor& visitor) const override; + +protected: + /// Constructor to create a ComparisonLayer + /// @param [in] param ComparisonDescriptor to configure the ComparisonLayer + /// @param [in] name Optional name for the layer + ComparisonLayer(const ComparisonDescriptor& param, const char* name); + + /// Default destructor + ~ComparisonLayer() = default; +}; + +} // namespace armnn diff --git a/src/armnn/layers/EqualLayer.cpp b/src/armnn/layers/EqualLayer.cpp deleted file mode 100644 index 7d16668b06..0000000000 --- a/src/armnn/layers/EqualLayer.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "EqualLayer.hpp" - -#include "LayerCloneBase.hpp" - -#include -#include -#include - -namespace armnn -{ - -EqualLayer::EqualLayer(const char* name) - : ElementwiseBaseLayer(2, 1, LayerType::Equal, name) -{ -} - -std::unique_ptr EqualLayer::CreateWorkload(const Graph& graph, - const IWorkloadFactory& factory) const -{ - EqualQueueDescriptor descriptor; - return factory.CreateEqual(descriptor, PrepInfoAndDesc(descriptor, graph)); -} - -EqualLayer* EqualLayer::Clone(Graph& graph) const -{ - return CloneBase(graph, GetName()); -} - -void EqualLayer::Accept(ILayerVisitor& visitor) const -{ - visitor.VisitEqualLayer(this, GetName()); -} - -} // namespace armnn diff --git a/src/armnn/layers/EqualLayer.hpp b/src/armnn/layers/EqualLayer.hpp deleted file mode 100644 index b6a01eff2d..0000000000 --- a/src/armnn/layers/EqualLayer.hpp +++ /dev/null @@ -1,38 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - -#include "ElementwiseBaseLayer.hpp" - -namespace armnn -{ -/// This layer represents an equal operation. -class EqualLayer : public ElementwiseBaseLayer -{ -public: - /// Makes a workload for the Equal type. - /// @param [in] graph The graph where this layer can be found. - /// @param [in] factory The workload factory which will create the workload. - /// @return A pointer to the created workload, or nullptr if not created. - virtual std::unique_ptr CreateWorkload(const Graph& graph, - const IWorkloadFactory& factory) const override; - - /// Creates a dynamically-allocated copy of this layer. - /// @param [in] graph The graph into which this layer is being cloned. - EqualLayer* Clone(Graph& graph) const override; - - void Accept(ILayerVisitor& visitor) const override; - -protected: - /// Constructor to create a EqualLayer. - /// @param [in] name Optional name for the layer. - EqualLayer(const char* name); - - /// Default destructor - ~EqualLayer() = default; -}; - -} //namespace armnn diff --git a/src/armnn/layers/GreaterLayer.cpp b/src/armnn/layers/GreaterLayer.cpp deleted file mode 100644 index a9fe5e0d8c..0000000000 --- a/src/armnn/layers/GreaterLayer.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "GreaterLayer.hpp" - -#include "LayerCloneBase.hpp" - -#include -#include -#include - -namespace armnn -{ - -GreaterLayer::GreaterLayer(const char* name) - : ElementwiseBaseLayer(2, 1, LayerType::Greater, name) -{ -} - -std::unique_ptr GreaterLayer::CreateWorkload(const Graph& graph, - const IWorkloadFactory& factory) const -{ - GreaterQueueDescriptor descriptor; - return factory.CreateGreater(descriptor, PrepInfoAndDesc(descriptor, graph)); -} - -GreaterLayer* GreaterLayer::Clone(Graph& graph) const -{ - return CloneBase(graph, GetName()); -} - -void GreaterLayer::Accept(ILayerVisitor& visitor) const -{ - visitor.VisitGreaterLayer(this, GetName()); -} - -} // namespace armnn diff --git a/src/armnn/layers/GreaterLayer.hpp b/src/armnn/layers/GreaterLayer.hpp deleted file mode 100644 index bdee948d6e..0000000000 --- a/src/armnn/layers/GreaterLayer.hpp +++ /dev/null @@ -1,39 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - -#include "ElementwiseBaseLayer.hpp" - -namespace armnn -{ - -/// This layer represents a greater operation. -class GreaterLayer : public ElementwiseBaseLayer -{ -public: - /// Makes a workload for the Greater type. - /// @param [in] graph The graph where this layer can be found. - /// @param [in] factory The workload factory which will create the workload. - /// @return A pointer to the created workload, or nullptr if not created. - virtual std::unique_ptr CreateWorkload(const Graph& graph, - const IWorkloadFactory& factory) const override; - - /// Creates a dynamically-allocated copy of this layer. - /// @param [in] graph The graph into which this layer is being cloned. - GreaterLayer* Clone(Graph& graph) const override; - - void Accept(ILayerVisitor& visitor) const override; - -protected: - /// Constructor to create a GreaterLayer. - /// @param [in] name Optional name for the layer. - GreaterLayer(const char* name); - - /// Default destructor - ~GreaterLayer() = default; -}; - -} //namespace armnn -- cgit v1.2.1