xref: /aosp_15_r20/external/armnn/src/armnn/layers/LogicalBinaryLayer.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "LogicalBinaryLayer.hpp"
7 
8 #include "LayerCloneBase.hpp"
9 
10 #include <armnn/backends/WorkloadData.hpp>
11 #include <armnn/backends/WorkloadFactory.hpp>
12 
13 #include <algorithm>
14 
15 namespace armnn
16 {
17 
LogicalBinaryLayer(const LogicalBinaryDescriptor & param,const char * name)18 LogicalBinaryLayer::LogicalBinaryLayer(const LogicalBinaryDescriptor& param, const char* name)
19     : LayerWithParameters(2, 1, LayerType::LogicalBinary, param, name)
20 {
21 }
22 
CreateWorkload(const IWorkloadFactory & factory) const23 std::unique_ptr<IWorkload> LogicalBinaryLayer::CreateWorkload(const IWorkloadFactory& factory) const
24 {
25     LogicalBinaryQueueDescriptor descriptor;
26     return factory.CreateWorkload(LayerType::LogicalBinary, descriptor, PrepInfoAndDesc(descriptor));
27 }
28 
Clone(Graph & graph) const29 LogicalBinaryLayer* LogicalBinaryLayer::Clone(Graph& graph) const
30 {
31     return CloneBase<LogicalBinaryLayer>(graph, m_Param, GetName());
32 }
33 
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const34 std::vector<TensorShape> LogicalBinaryLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
35 {
36     ARMNN_ASSERT(inputShapes.size() == 2);
37     const TensorShape& input0 = inputShapes[0];
38     const TensorShape& input1 = inputShapes[1];
39 
40     ARMNN_ASSERT(input0.GetNumDimensions() == input1.GetNumDimensions());
41     unsigned int numDims = input0.GetNumDimensions();
42 
43     std::vector<unsigned int> dims(numDims);
44     for (unsigned int i = 0; i < numDims; i++)
45     {
46         unsigned int dim0 = input0[i];
47         unsigned int dim1 = input1[i];
48 
49         ARMNN_ASSERT_MSG(dim0 == dim1 || dim0 == 1 || dim1 == 1,
50                          "Dimensions should either match or one should be of size 1.");
51 
52         dims[i] = std::max(dim0, dim1);
53     }
54 
55     return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
56 }
57 
ValidateTensorShapesFromInputs()58 void LogicalBinaryLayer::ValidateTensorShapesFromInputs()
59 {
60     VerifyLayerConnections(2, CHECK_LOCATION());
61 
62     const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
63 
64     VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
65 
66     std::vector<TensorShape> inferredShapes = InferOutputShapes({
67         GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
68         GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()
69     });
70     ARMNN_ASSERT(inferredShapes.size() == 1);
71 
72     ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "LogicalBinaryLayer");
73 }
74 
ExecuteStrategy(IStrategy & strategy) const75 void LogicalBinaryLayer::ExecuteStrategy(IStrategy& strategy) const
76 {
77     strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
78 }
79 
80 } // namespace armnn
81