ArmNN
 24.02
ElementwiseBaseLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2018,2020-2021,2023 Arm Ltd and Contributors. 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 #include <armnn/utility/Assert.hpp>
12 
13 namespace armnn
14 {
15 
16 ElementwiseBaseLayer::ElementwiseBaseLayer(unsigned int numInputSlots,
17  unsigned int numOutputSlots,
18  LayerType type,
19  const char* name)
20  : Layer(numInputSlots, numOutputSlots, type, name)
21 {}
22 
23 std::vector<TensorShape> ElementwiseBaseLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
24 {
25  ARMNN_ASSERT(inputShapes.size() == 2);
26  TensorShape input0 = inputShapes[0];
27  TensorShape input1 = inputShapes[1];
28 
29  if (inputShapes[0].GetNumDimensions() < inputShapes[1].GetNumDimensions())
30  {
31  input1 = inputShapes[0];
32  input0 = inputShapes[1];
33  }
34 
35  unsigned int numDims = input0.GetNumDimensions();
36  unsigned int shiftedDims = input0.GetNumDimensions() - input1.GetNumDimensions();
37 
38  // Get the max of the inputs.
39  std::vector<unsigned int> dims(numDims);
40  for (unsigned int i = shiftedDims; i < numDims; i++)
41  {
42  unsigned int dim0 = input0[i];
43  unsigned int dim1 = input1[i - shiftedDims];
44 
45  // Validate inputs are broadcast compatible.
46  ARMNN_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1,
47  "Dimensions should either match or one should be of size 1.");
48 
49  dims[i] = std::max(dim0, dim1);
50  }
51 
52  // Fill in the rest of the shifted dimensions.
53  for (unsigned int i = 0; i < shiftedDims; i++)
54  {
55  dims[i] = input0[i];
56  }
57 
58  return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
59 }
60 
62 {
64 
65  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
66 
68 
69  auto inferredShapes = InferOutputShapes({ GetInputSlot(0).GetTensorInfo().GetShape(),
71 
72  ARMNN_ASSERT(inferredShapes.size() == 1);
73 
74  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, GetLayerTypeAsCString(GetType()));
75 }
76 
78 {
79  strategy.ExecuteStrategy(this, BaseDescriptor(), {}, GetName());
80 }
81 
82 } // namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::GetLayerTypeAsCString
const char * GetLayerTypeAsCString(LayerType type)
Definition: InternalTypes.cpp:13
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
TypesUtils.hpp
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::Layer::ValidateAndCopyShape
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:435
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
armnn::IStrategy
Definition: IStrategy.hpp:16
ARMNN_ASSERT_MSG
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::Layer::GetName
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:332
armnn::Layer
Definition: Layer.hpp:230
armnn::InputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition: Layer.cpp:592
Assert.hpp
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::TensorShape::GetNumDimensions
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:504
armnn::BaseDescriptor
Base class for all descriptors.
Definition: Descriptors.hpp:22
armnn::ElementwiseBaseLayer::ExecuteStrategy
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
Definition: ElementwiseBaseLayer.cpp:77
armnn::Layer::GetType
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:286
ElementwiseBaseLayer.hpp
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
Exceptions.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
InternalTypes.hpp
armnn::Layer::VerifyLayerConnections
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:391
armnn::ElementwiseBaseLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of the element wise operation.
Definition: ElementwiseBaseLayer.cpp:61
armnn::ElementwiseBaseLayer::ElementwiseBaseLayer
ElementwiseBaseLayer(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const char *name)
Definition: ElementwiseBaseLayer.cpp:16
armnn::Layer::m_ShapeInferenceMethod
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:441
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::IStrategy::ExecuteStrategy
virtual void ExecuteStrategy(const IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
armnn::ElementwiseBaseLayer::InferOutputShapes
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,...
Definition: ElementwiseBaseLayer.cpp:23