• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ElementwiseBaseLayer.hpp"
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 
ElementwiseBaseLayer(unsigned int numInputSlots,unsigned int numOutputSlots,LayerType type,const char * name)16 ElementwiseBaseLayer::ElementwiseBaseLayer(unsigned int numInputSlots, unsigned int numOutputSlots,
17                                            LayerType type, const char* name)
18     : Layer(numInputSlots, numOutputSlots, type, name)
19 {
20 }
21 
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const22 std::vector<TensorShape> ElementwiseBaseLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
23 {
24     ARMNN_ASSERT(inputShapes.size() == 2);
25     TensorShape input0 = inputShapes[0];
26     TensorShape input1 = inputShapes[1];
27 
28     if (m_ShapeInferenceMethod == ShapeInferenceMethod::ValidateOnly)
29     {
30         ARMNN_ASSERT(input0.GetNumDimensions() == input1.GetNumDimensions());
31     }
32     else if (m_ShapeInferenceMethod == ShapeInferenceMethod::InferAndValidate &&
33              inputShapes[0].GetNumDimensions() < inputShapes[1].GetNumDimensions())
34     {
35         input1 = inputShapes[0];
36         input0 = inputShapes[1];
37     }
38 
39     unsigned int numDims = input0.GetNumDimensions();
40     unsigned int shiftedDims = input0.GetNumDimensions() - input1.GetNumDimensions();
41 
42     // Get the max of the inputs.
43     std::vector<unsigned int> dims(numDims);
44     for (unsigned int i = shiftedDims; i < numDims; i++)
45     {
46         unsigned int dim0 = input0[i];
47         unsigned int dim1 = input1[i - shiftedDims];
48 
49 #if !NDEBUG
50         // Validate inputs are broadcast compatible.
51         ARMNN_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1,
52                          "Dimensions should either match or one should be of size 1.");
53 #endif
54 
55         dims[i] = std::max(dim0, dim1);
56     }
57 
58     // Fill in the rest of the shifted dimensions.
59     for (unsigned int i = 0; i < shiftedDims; i++)
60     {
61         dims[i] = input0[i];
62     }
63 
64     return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
65 }
66 
ValidateTensorShapesFromInputs()67 void ElementwiseBaseLayer::ValidateTensorShapesFromInputs()
68 {
69     VerifyLayerConnections(2, CHECK_LOCATION());
70 
71     const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
72 
73     VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
74 
75     auto inferredShapes = InferOutputShapes({
76         GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
77         GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()
78     });
79 
80     ARMNN_ASSERT(inferredShapes.size() == 1);
81 
82     ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, GetLayerTypeAsCString(GetType()));
83 }
84 
85 } // namespace armnn
86