xref: /aosp_15_r20/external/armnn/src/armnn/layers/ElementwiseBinaryLayer.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ElementwiseBinaryLayer.hpp"
7 
8 #include "LayerCloneBase.hpp"
9 
10 namespace armnn
11 {
12 
ElementwiseBinaryLayer(const ElementwiseBinaryDescriptor & param,const char * name)13 ElementwiseBinaryLayer::ElementwiseBinaryLayer(const ElementwiseBinaryDescriptor& param, const char* name)
14     : LayerWithParameters(2, 1, LayerType::ElementwiseBinary, param, name)
15 {
16 }
17 
CreateWorkload(const IWorkloadFactory & factory) const18 std::unique_ptr<IWorkload> ElementwiseBinaryLayer::CreateWorkload(const IWorkloadFactory& factory) const
19 {
20     ElementwiseBinaryQueueDescriptor descriptor;
21     SetAdditionalInfo(descriptor);
22 
23     return factory.CreateWorkload(LayerType::ElementwiseBinary, descriptor, PrepInfoAndDesc(descriptor));
24 }
25 
Clone(Graph & graph) const26 ElementwiseBinaryLayer* ElementwiseBinaryLayer::Clone(Graph& graph) const
27 {
28     return CloneBase<ElementwiseBinaryLayer>(graph, m_Param, GetName());
29 }
30 
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const31 std::vector<TensorShape> ElementwiseBinaryLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
32 {
33     ARMNN_ASSERT(inputShapes.size() == 2);
34     TensorShape input0 = inputShapes[0];
35     TensorShape input1 = inputShapes[1];
36 
37     if (inputShapes[0].GetNumDimensions() < inputShapes[1].GetNumDimensions())
38     {
39         input1 = inputShapes[0];
40         input0 = inputShapes[1];
41     }
42 
43     unsigned int numDims     = input0.GetNumDimensions();
44     unsigned int shiftedDims = input0.GetNumDimensions() - input1.GetNumDimensions();
45 
46     // Get the max of the inputs.
47     std::vector<unsigned int> dims(numDims);
48     for (unsigned int i = shiftedDims; i < numDims; i++)
49     {
50         unsigned int dim0 = input0[i];
51         unsigned int dim1 = input1[i - shiftedDims];
52 
53         // Validate inputs are broadcast compatible.
54         ARMNN_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1,
55                          "Dimensions should either match or one should be of size 1.");
56 
57         dims[i] = std::max(dim0, dim1);
58     }
59 
60     // Fill in the rest of the shifted dimensions.
61     for (unsigned int i = 0; i < shiftedDims; i++)
62     {
63         dims[i] = input0[i];
64     }
65 
66     return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
67 }
68 
ValidateTensorShapesFromInputs()69 void ElementwiseBinaryLayer::ValidateTensorShapesFromInputs()
70 {
71     VerifyLayerConnections(2, CHECK_LOCATION());
72 
73     const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
74 
75     VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
76 
77     auto inferredShapes = InferOutputShapes({ GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
78                                               GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape() });
79 
80     ARMNN_ASSERT(inferredShapes.size() == 1);
81 
82     ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, GetLayerTypeAsCString(GetType()));
83 }
84 
ExecuteStrategy(IStrategy & strategy) const85 void ElementwiseBinaryLayer::ExecuteStrategy(IStrategy& strategy) const
86 {
87     strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
88 }
89 } // namespace armnn
90