ArmNN  NotReleased
ElementwiseBaseLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include "InternalTypes.hpp"
9 #include "armnn/Exceptions.hpp"
10 #include <armnn/TypesUtils.hpp>
11 
12 #include <boost/assert.hpp>
13 
14 namespace armnn
15 {
16 
17 ElementwiseBaseLayer::ElementwiseBaseLayer(unsigned int numInputSlots, unsigned int numOutputSlots,
18  LayerType type, const char* name)
19  : Layer(numInputSlots, numOutputSlots, type, name)
20 {
21 }
22 
23 std::vector<TensorShape> ElementwiseBaseLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
24 {
25  BOOST_ASSERT(inputShapes.size() == 2);
26  auto& input0 = inputShapes[0];
27  auto& input1 = inputShapes[1];
28 
29  // Get the max of the inputs.
30  BOOST_ASSERT(input0.GetNumDimensions() == input1.GetNumDimensions());
31  unsigned int numDims = input0.GetNumDimensions();
32  std::vector<unsigned int> dims(numDims);
33 
34  for (unsigned int i = 0; i < numDims; i++)
35  {
36  unsigned int dim0 = input0[i];
37  unsigned int dim1 = input1[i];
38 
39 #if !NDEBUG
40  // Validate inputs are broadcast compatible.
41  BOOST_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1,
42  "Dimensions should either match or one should be of size 1.");
43 #endif
44 
45  dims[i] = std::max(dim0, dim1);
46  }
47 
48  return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
49 }
50 
52 {
54 
55  auto inferredShapes = InferOutputShapes({
58  });
59 
60  BOOST_ASSERT(inferredShapes.size() == 1);
61 
62  std::string msg = GetLayerTypeAsCString(GetType());
63  msg += "Layer: TensorShape set on OutputSlot[0] does not match the inferred shape.";
64  ConditionalThrowIfNotEqual<LayerValidationException>(msg,
66  inferredShapes[0]);
67 }
68 
69 } // namespace armnn
LayerType GetType() const
Definition: Layer.hpp:259
virtual const TensorInfo & GetTensorInfo() const =0
char const * GetLayerTypeAsCString(LayerType type)
#define CHECK_LOCATION()
Definition: Exceptions.hpp:169
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:337
void ValidateTensorShapesFromInputs() override
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Definition: Layer.hpp:312
ElementwiseBaseLayer(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const char *name)
const InputSlot & GetInputSlot(unsigned int index) const override
Definition: Layer.hpp:310