diff options
author | Les Bell <les.bell@arm.com> | 2018-09-03 16:24:52 +0100 |
---|---|---|
committer | Matthew Bentham <matthew.bentham@arm.com> | 2018-09-17 17:21:25 +0100 |
commit | 033626d1ee9256032309bbc6685c3a69a919cd64 (patch) | |
tree | 42a1ab9c3b83c4da019b69e2189846663e0d19be /src/armnn/layers/ArithmeticBaseLayer.cpp | |
parent | 9e53a35b66b1ec7ceee7c712380a13596175b83b (diff) | |
download | armnn-033626d1ee9256032309bbc6685c3a69a919cd64.tar.gz |
IVGCVSW-1783 refactor add/mul/div layers
Change-Id: Ic043030455b3cf8ad7f92fd0a75175c1827a95f4
Diffstat (limited to 'src/armnn/layers/ArithmeticBaseLayer.cpp')
-rw-r--r-- | src/armnn/layers/ArithmeticBaseLayer.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
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 <armnn/TypesUtils.hpp> + +#include <boost/assert.hpp> + +namespace armnn +{ + +ArithmeticBaseLayer::ArithmeticBaseLayer(unsigned int numInputSlots, unsigned int numOutputSlots, + LayerType type, const char* name) + : Layer(numInputSlots, numOutputSlots, type, name) +{ +} + +std::vector<TensorShape> ArithmeticBaseLayer::InferOutputShapes(const std::vector<TensorShape>& 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<unsigned int> 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>({ 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<LayerValidationException>(msg, + GetOutputSlot(0).GetTensorInfo().GetShape(), + inferredShapes[0]); +} + +} // namespace armnn |