xref: /aosp_15_r20/external/armnn/src/armnn/layers/MeanLayer.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "MeanLayer.hpp"
7 #include "LayerCloneBase.hpp"
8 
9 #include <armnn/utility/NumericCast.hpp>
10 
11 #include <armnn/backends/TensorHandle.hpp>
12 #include <armnn/backends/WorkloadData.hpp>
13 #include <armnn/backends/WorkloadFactory.hpp>
14 
15 #include <cstring>
16 
17 namespace armnn
18 {
19 
MeanLayer(const armnn::MeanDescriptor & param,const char * name)20 MeanLayer::MeanLayer(const armnn::MeanDescriptor& param, const char* name)
21     : LayerWithParameters(1, 1, LayerType::Mean, param, name)
22 {}
23 
CreateWorkload(const armnn::IWorkloadFactory & factory) const24 std::unique_ptr<IWorkload> MeanLayer::CreateWorkload(const armnn::IWorkloadFactory& factory) const
25 {
26     MeanQueueDescriptor descriptor;
27     descriptor.m_Parameters.m_Axis = m_Param.m_Axis;
28     descriptor.m_Parameters.m_KeepDims = m_Param.m_KeepDims;
29     SetAdditionalInfo(descriptor);
30 
31     return factory.CreateWorkload(LayerType::Mean, descriptor, PrepInfoAndDesc(descriptor));
32 }
33 
Clone(Graph & graph) const34 MeanLayer* MeanLayer::Clone(Graph& graph) const
35 {
36     auto layer = CloneBase<MeanLayer>(graph, m_Param, GetName());
37 
38     layer->m_Param.m_Axis = m_Param.m_Axis;
39     layer->m_Param.m_KeepDims = m_Param.m_KeepDims;
40 
41     return std::move(layer);
42 }
43 
ValidateTensorShapesFromInputs()44 void MeanLayer::ValidateTensorShapesFromInputs()
45 {
46     VerifyLayerConnections(1, CHECK_LOCATION());
47 
48     const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
49 
50     VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
51 
52     std::vector<TensorShape> inferredShapes = InferOutputShapes(
53             { GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape() });
54 
55     ARMNN_ASSERT(inferredShapes.size() == 1);
56     ARMNN_ASSERT(inferredShapes[0].GetDimensionality() == Dimensionality::Specified);
57 
58     ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "MeanLayer");
59 }
60 
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const61 std::vector<TensorShape> MeanLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
62 {
63     ARMNN_ASSERT(inputShapes.size() == 1);
64     const TensorShape& input = inputShapes[0];
65 
66     ARMNN_ASSERT_MSG(input.GetNumDimensions() > 0 && input.GetNumDimensions() <= 4,
67                      "MeanLayer: Mean supports up to 4D input.");
68 
69     unsigned int rank = input.GetNumDimensions();
70     unsigned int outputRank = 0;
71 
72     // Calculate output dimension
73     if (m_Param.m_KeepDims)
74     {
75         outputRank = rank;
76     }
77     else if (m_Param.m_Axis.empty())
78     {
79         outputRank = 1;
80     }
81     else if (m_Param.m_Axis.size() > input.GetNumDimensions())
82     {
83         throw LayerValidationException("MeanLayer: Dimensions to reduce can not be bigger than input dimensions");
84     }
85     else
86     {
87         outputRank = input.GetNumDimensions() - armnn::numeric_cast<unsigned int>(m_Param.m_Axis.size());
88         if (outputRank == 0)
89         {
90             outputRank = 1;
91         }
92     }
93 
94     std::vector<unsigned int> dimSizes(outputRank, 1);
95     if (!m_Param.m_Axis.empty())
96     {
97         // Skip the dimension that has been reduced unless keepDims is true.
98         unsigned int outputIndex = 0;
99         for (unsigned int i = 0; i < input.GetNumDimensions(); ++i)
100         {
101             if (std::find(m_Param.m_Axis.begin(), m_Param.m_Axis.end(), i) == m_Param.m_Axis.end())
102             {
103                 dimSizes[outputIndex] = armnn::numeric_cast<unsigned int>(input[i]);
104                 ++outputIndex;
105             }
106             else if (m_Param.m_KeepDims)
107             {
108                 dimSizes[outputIndex] = 1;
109                 ++outputIndex;
110             }
111         }
112     }
113     return std::vector<TensorShape>({ TensorShape(outputRank, dimSizes.data()) });
114 }
115 
ExecuteStrategy(IStrategy & strategy) const116 void MeanLayer::ExecuteStrategy(IStrategy& strategy) const
117 {
118     strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
119 }
120 
121 } // namespace armnn
122