From 033626d1ee9256032309bbc6685c3a69a919cd64 Mon Sep 17 00:00:00 2001 From: Les Bell Date: Mon, 3 Sep 2018 16:24:52 +0100 Subject: IVGCVSW-1783 refactor add/mul/div layers Change-Id: Ic043030455b3cf8ad7f92fd0a75175c1827a95f4 --- src/armnn/layers/AdditionLayer.cpp | 53 ++----------------------- src/armnn/layers/AdditionLayer.hpp | 10 ++--- src/armnn/layers/ArithmeticBaseLayer.cpp | 68 ++++++++++++++++++++++++++++++++ src/armnn/layers/ArithmeticBaseLayer.hpp | 26 ++++++++++++ src/armnn/layers/DivisionLayer.cpp | 50 +---------------------- src/armnn/layers/DivisionLayer.hpp | 7 +--- src/armnn/layers/MultiplicationLayer.cpp | 52 +----------------------- src/armnn/layers/MultiplicationLayer.hpp | 9 ++--- 8 files changed, 109 insertions(+), 166 deletions(-) create mode 100644 src/armnn/layers/ArithmeticBaseLayer.cpp create mode 100644 src/armnn/layers/ArithmeticBaseLayer.hpp (limited to 'src') diff --git a/src/armnn/layers/AdditionLayer.cpp b/src/armnn/layers/AdditionLayer.cpp index ab73a918db..c1413102b8 100644 --- a/src/armnn/layers/AdditionLayer.cpp +++ b/src/armnn/layers/AdditionLayer.cpp @@ -3,6 +3,7 @@ // See LICENSE file in the project root for full license information. // #include "AdditionLayer.hpp" + #include "LayerCloneBase.hpp" #include @@ -13,11 +14,12 @@ namespace armnn { AdditionLayer::AdditionLayer(const char* name) - : Layer(2, 1, LayerType::Addition, name) + : ArithmeticBaseLayer(2, 1, LayerType::Addition, name) { } -std::unique_ptr AdditionLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const +std::unique_ptr AdditionLayer::CreateWorkload(const Graph& graph, + const IWorkloadFactory& factory) const { AdditionQueueDescriptor descriptor; return factory.CreateAddition(descriptor, PrepInfoAndDesc(descriptor, graph)); @@ -28,51 +30,4 @@ AdditionLayer* AdditionLayer::Clone(Graph& graph) const return CloneBase(graph, GetName()); } -std::vector AdditionLayer::InferOutputShapes(const std::vector& inputShapes) const -{ - BOOST_ASSERT(inputShapes.size() == 2); - auto& input0 = inputShapes[0]; - auto& input1 = inputShapes[1]; - - // Get the max of the inputs. - 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]; - - // Validates inputs are broadcast compatible. -#if !NDEBUG - if (dim0 != dim1) - { - BOOST_ASSERT_MSG(dim0 == 1 || dim1 == 1, "Dimensions should either match or one should be of size 1."); - } -#endif - - dims[i] = std::max(dim0, dim1); - } - - return std::vector({ TensorShape(numDims, dims.data()) }); -} - -void AdditionLayer::ValidateTensorShapesFromInputs() -{ - VerifyLayerConnections(2, CHECK_LOCATION()); - - auto inferredShapes = InferOutputShapes({ - GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(), - GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape() - }); - - BOOST_ASSERT(inferredShapes.size() == 1); - - ConditionalThrowIfNotEqual( - "AdditionLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.", - GetOutputSlot(0).GetTensorInfo().GetShape(), - inferredShapes[0]); -} - } // namespace armnn diff --git a/src/armnn/layers/AdditionLayer.hpp b/src/armnn/layers/AdditionLayer.hpp index 37f0b5c259..d34536d114 100644 --- a/src/armnn/layers/AdditionLayer.hpp +++ b/src/armnn/layers/AdditionLayer.hpp @@ -4,23 +4,19 @@ // #pragma once -#include +#include "ArithmeticBaseLayer.hpp" namespace armnn { -class AdditionLayer : public Layer +class AdditionLayer : public ArithmeticBaseLayer { public: - virtual std::unique_ptr CreateWorkload(const Graph& graph, + virtual std::unique_ptr CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const override; AdditionLayer* Clone(Graph& graph) const override; - void ValidateTensorShapesFromInputs() override; - - std::vector InferOutputShapes(const std::vector& inputShapes) const override; - protected: AdditionLayer(const char* name); ~AdditionLayer() = default; diff --git a/src/armnn/layers/ArithmeticBaseLayer.cpp b/src/armnn/layers/ArithmeticBaseLayer.cpp new file mode 100644 index 0000000000..63937fba6c --- /dev/null +++ b/src/armnn/layers/ArithmeticBaseLayer.cpp @@ -0,0 +1,68 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// See LICENSE file in the project root for full license information. +// +#include "ArithmeticBaseLayer.hpp" + +#include "InternalTypes.hpp" +#include "armnn/Exceptions.hpp" +#include + +#include + +namespace armnn +{ + +ArithmeticBaseLayer::ArithmeticBaseLayer(unsigned int numInputSlots, unsigned int numOutputSlots, + LayerType type, const char* name) + : Layer(numInputSlots, numOutputSlots, type, name) +{ +} + +std::vector ArithmeticBaseLayer::InferOutputShapes(const std::vector& inputShapes) const +{ + BOOST_ASSERT(inputShapes.size() == 2); + auto& input0 = inputShapes[0]; + auto& input1 = inputShapes[1]; + + // Get the max of the inputs. + 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]; + +#if !NDEBUG + // Validate inputs are broadcast compatible. + BOOST_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1, + "Dimensions should either match or one should be of size 1."); +#endif + + dims[i] = std::max(dim0, dim1); + } + + return std::vector({ TensorShape(numDims, dims.data()) }); +} + +void ArithmeticBaseLayer::ValidateTensorShapesFromInputs() +{ + VerifyLayerConnections(2, CHECK_LOCATION()); + + auto inferredShapes = InferOutputShapes({ + GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(), + GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape() + }); + + BOOST_ASSERT(inferredShapes.size() == 1); + + std::string msg = GetLayerTypeAsCString(GetType()); + msg += "Layer: TensorShape set on OutputSlot[0] does not match the inferred shape."; + ConditionalThrowIfNotEqual(msg, + GetOutputSlot(0).GetTensorInfo().GetShape(), + inferredShapes[0]); +} + +} // namespace armnn diff --git a/src/armnn/layers/ArithmeticBaseLayer.hpp b/src/armnn/layers/ArithmeticBaseLayer.hpp new file mode 100644 index 0000000000..eee612add4 --- /dev/null +++ b/src/armnn/layers/ArithmeticBaseLayer.hpp @@ -0,0 +1,26 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// See LICENSE file in the project root for full license information. +// +#pragma once + +#include + +namespace armnn +{ + +/// NOTE: this is an abstract class, it does not implement: +/// std::unique_ptr Layer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const = 0; +/// Layer* Clone(Graph& graph) const = 0; +class ArithmeticBaseLayer : public Layer +{ +public: + void ValidateTensorShapesFromInputs() override; + std::vector InferOutputShapes(const std::vector& inputShapes) const override; + +protected: + ArithmeticBaseLayer(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const char* name); + ~ArithmeticBaseLayer() = default; +}; + +} // namespace diff --git a/src/armnn/layers/DivisionLayer.cpp b/src/armnn/layers/DivisionLayer.cpp index bf09e14229..c0f958929e 100644 --- a/src/armnn/layers/DivisionLayer.cpp +++ b/src/armnn/layers/DivisionLayer.cpp @@ -14,7 +14,7 @@ namespace armnn { DivisionLayer::DivisionLayer(const char* name) - : Layer(2, 1, LayerType::Division, name) + : ArithmeticBaseLayer(2, 1, LayerType::Division, name) { } @@ -22,7 +22,6 @@ std::unique_ptr DivisionLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const { DivisionQueueDescriptor descriptor; - return factory.CreateDivision(descriptor, PrepInfoAndDesc(descriptor, graph)); } @@ -31,51 +30,4 @@ DivisionLayer* DivisionLayer::Clone(Graph& graph) const return CloneBase(graph, GetName()); } -std::vector DivisionLayer::InferOutputShapes(const std::vector& inputShapes) const -{ - BOOST_ASSERT(inputShapes.size() == 2); - auto& input0 = inputShapes[0]; - auto& input1 = inputShapes[1]; - - // Get the max of the inputs. - 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]; - - // Validates inputs are broadcast compatible. -#if !NDEBUG - if (dim0 != dim1) - { - BOOST_ASSERT_MSG(dim0 == 1 || dim1 == 1, "Dimensions should either match or one should be of size 1."); - } -#endif - - dims[i] = std::max(dim0, dim1); - } - - return std::vector({ TensorShape(numDims, dims.data()) }); -} - -void DivisionLayer::ValidateTensorShapesFromInputs() -{ - VerifyLayerConnections(2, CHECK_LOCATION()); - - auto inferredShapes = InferOutputShapes({ - GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(), - GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape() - }); - - BOOST_ASSERT(inferredShapes.size() == 1); - - ConditionalThrowIfNotEqual( - "DivisionLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.", - GetOutputSlot(0).GetTensorInfo().GetShape(), - inferredShapes[0]); -} - } // namespace armnn diff --git a/src/armnn/layers/DivisionLayer.hpp b/src/armnn/layers/DivisionLayer.hpp index 1bd69c4446..c6a642e318 100644 --- a/src/armnn/layers/DivisionLayer.hpp +++ b/src/armnn/layers/DivisionLayer.hpp @@ -4,12 +4,12 @@ // #pragma once -#include +#include "ArithmeticBaseLayer.hpp" namespace armnn { -class DivisionLayer : public Layer +class DivisionLayer : public ArithmeticBaseLayer { public: virtual std::unique_ptr CreateWorkload(const Graph& graph, @@ -17,9 +17,6 @@ public: DivisionLayer* Clone(Graph& graph) const override; - void ValidateTensorShapesFromInputs() override; - std::vector InferOutputShapes(const std::vector& inputShapes) const override; - protected: DivisionLayer(const char* name); ~DivisionLayer() = default; diff --git a/src/armnn/layers/MultiplicationLayer.cpp b/src/armnn/layers/MultiplicationLayer.cpp index ed7683da5f..6970b3f7f4 100644 --- a/src/armnn/layers/MultiplicationLayer.cpp +++ b/src/armnn/layers/MultiplicationLayer.cpp @@ -14,15 +14,14 @@ namespace armnn { MultiplicationLayer::MultiplicationLayer(const char* name) - : Layer(2, 1, LayerType::Multiplication, name) + : ArithmeticBaseLayer(2, 1, LayerType::Multiplication, name) { } -std::unique_ptr MultiplicationLayer::CreateWorkload(const Graph& graph, +std::unique_ptr MultiplicationLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const { MultiplicationQueueDescriptor descriptor; - return factory.CreateMultiplication(descriptor, PrepInfoAndDesc(descriptor, graph)); } @@ -31,51 +30,4 @@ MultiplicationLayer* MultiplicationLayer::Clone(Graph& graph) const return CloneBase(graph, GetName()); } -std::vector MultiplicationLayer::InferOutputShapes(const std::vector& inputShapes) const -{ - BOOST_ASSERT(inputShapes.size() == 2); - auto& input0 = inputShapes[0]; - auto& input1 = inputShapes[1]; - - // Get the max of the inputs. - 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]; - - // Validates inputs are broadcast compatible. -#if !NDEBUG - if (dim0 != dim1) - { - BOOST_ASSERT_MSG(dim0 == 1 || dim1 == 1, "Dimensions should either match or one should be of size 1."); - } -#endif - - dims[i] = std::max(dim0, dim1); - } - - return std::vector({ TensorShape(numDims, dims.data()) }); -} - -void MultiplicationLayer::ValidateTensorShapesFromInputs() -{ - VerifyLayerConnections(2, CHECK_LOCATION()); - - auto inferredShapes = InferOutputShapes({ - GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(), - GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape() - }); - - BOOST_ASSERT(inferredShapes.size() == 1); - - ConditionalThrowIfNotEqual( - "MultiplicationLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.", - GetOutputSlot(0).GetTensorInfo().GetShape(), - inferredShapes[0]); -} - } // namespace armnn diff --git a/src/armnn/layers/MultiplicationLayer.hpp b/src/armnn/layers/MultiplicationLayer.hpp index bbfd1ee694..264d12feeb 100644 --- a/src/armnn/layers/MultiplicationLayer.hpp +++ b/src/armnn/layers/MultiplicationLayer.hpp @@ -4,22 +4,19 @@ // #pragma once -#include +#include "ArithmeticBaseLayer.hpp" namespace armnn { -class MultiplicationLayer : public Layer +class MultiplicationLayer : public ArithmeticBaseLayer { public: - virtual std::unique_ptr CreateWorkload(const Graph& graph, + virtual std::unique_ptr CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const override; MultiplicationLayer* Clone(Graph& graph) const override; - void ValidateTensorShapesFromInputs() override; - std::vector InferOutputShapes(const std::vector& inputShapes) const override; - protected: MultiplicationLayer(const char* name); ~MultiplicationLayer() = default; -- cgit v1.2.1