ArmNN
 24.08
ElementwiseBaseLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2018,2020-2021,2023-2024 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  if (inputShapes.size() != 2)
26  {
27  throw armnn::Exception("inputShapes' size is \"" + std::to_string(inputShapes.size()) +
28  "\" - should be \"2\".");
29  }
30 
31  TensorShape input0 = inputShapes[0];
32  TensorShape input1 = inputShapes[1];
33 
34  if (inputShapes[0].GetNumDimensions() < inputShapes[1].GetNumDimensions())
35  {
36  input1 = inputShapes[0];
37  input0 = inputShapes[1];
38  }
39 
40  unsigned int numDims = input0.GetNumDimensions();
41  unsigned int shiftedDims = input0.GetNumDimensions() - input1.GetNumDimensions();
42 
43  // Get the max of the inputs.
44  std::vector<unsigned int> dims(numDims);
45  for (unsigned int i = shiftedDims; i < numDims; i++)
46  {
47  unsigned int dim0 = input0[i];
48  unsigned int dim1 = input1[i - shiftedDims];
49 
50  // Validate inputs are broadcast compatible.
51  if (dim0 != dim1 && dim0 != 1 && dim1 != 1)
52  {
53  throw armnn::Exception("Dimensions should either match or one should be of size 1.");
54  }
55 
56  dims[i] = std::max(dim0, dim1);
57  }
58 
59  // Fill in the rest of the shifted dimensions.
60  for (unsigned int i = 0; i < shiftedDims; i++)
61  {
62  dims[i] = input0[i];
63  }
64 
65  return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
66 }
67 
69 {
71 
72  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
73 
75 
76  auto inferredShapes = InferOutputShapes({ GetInputSlot(0).GetTensorInfo().GetShape(),
78 
79  if (inferredShapes.size() != 1)
80  {
81  throw armnn::LayerValidationException("inferredShapes has "
82  + std::to_string(inferredShapes.size()) +
83  " elements - should only have 1.");
84  }
85 
86  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, GetLayerTypeAsCString(GetType()));
87 }
88 
90 {
91  strategy.ExecuteStrategy(this, BaseDescriptor(), {}, GetName());
92 }
93 
94 } // namespace armnn
armnn::GetLayerTypeAsCString
const char * GetLayerTypeAsCString(LayerType type)
Definition: InternalTypes.cpp:13
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:100
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:457
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::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:614
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::LayerValidationException
Definition: Exceptions.hpp:105
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:526
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
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:89
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:410
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:68
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