ArmNN
 25.11
Loading...
Searching...
No Matches
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"
10#include <armnn/TypesUtils.hpp>
12
13namespace armnn
14{
15
17 unsigned int numOutputSlots,
18 LayerType type,
19 const char* name)
20 : Layer(numInputSlots, numOutputSlots, type, name)
21{}
22
23std::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
87}
88
90{
91 strategy.ExecuteStrategy(this, BaseDescriptor(), {}, GetName());
92}
93
94} // namespace armnn
#define CHECK_LOCATION()
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
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,...
ElementwiseBaseLayer(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const char *name)
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of the element wise operation.
Base class for all ArmNN exceptions so that users can filter to just those.
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
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition Layer.cpp:614
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition Layer.cpp:410
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition Layer.cpp:526
Layer(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const char *name)
Definition Layer.cpp:260
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition Layer.hpp:339
const char * GetName() const override
Returns the name of the layer.
Definition Layer.hpp:332
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition Layer.hpp:286
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition Layer.cpp:457
ShapeInferenceMethod m_ShapeInferenceMethod
Definition Layer.hpp:441
const TensorInfo & GetTensorInfo() const override
Definition Layer.cpp:100
const TensorShape & GetShape() const
Definition Tensor.hpp:193
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition Tensor.cpp:174
Copyright (c) 2021 ARM Limited and Contributors.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition Types.hpp:494
const char * GetLayerTypeAsCString(LayerType type)
Base class for all descriptors.