1 //
2 // Copyright © 2017 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 <backendsCommon/CpuTensorHandle.hpp>
12 #include <backendsCommon/WorkloadData.hpp>
13 #include <backendsCommon/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.CreateMean(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 const TensorInfo& input = GetInputSlot(0).GetConnection()->GetTensorInfo();
53
54 ARMNN_ASSERT_MSG(input.GetNumDimensions() > 0 && input.GetNumDimensions() <= 4,
55 "MeanLayer: Mean supports up to 4D input.");
56
57 unsigned int rank = input.GetNumDimensions();
58 unsigned int outputRank = 0;
59
60 // Calculate output dimension
61 if (m_Param.m_KeepDims)
62 {
63 outputRank = rank;
64 }
65 else if (m_Param.m_Axis.empty())
66 {
67 outputRank = 1;
68 }
69 else if (m_Param.m_Axis.size() > input.GetNumDimensions())
70 {
71 throw LayerValidationException("MeanLayer: Dimensions to reduce can not be bigger than input dimensions");
72 }
73 else
74 {
75 outputRank = input.GetNumDimensions() - armnn::numeric_cast<unsigned int>(m_Param.m_Axis.size());
76 if (outputRank == 0)
77 {
78 outputRank = 1;
79 }
80 }
81
82 std::vector<unsigned int> dimSizes(outputRank, 1);
83 if (!m_Param.m_Axis.empty())
84 {
85 // Skip the dimension that has been reduced unless keepDims is true.
86 unsigned int outputIndex = 0;
87 for (unsigned int i = 0; i < input.GetNumDimensions(); ++i)
88 {
89 if (std::find(m_Param.m_Axis.begin(), m_Param.m_Axis.end(), i) == m_Param.m_Axis.end())
90 {
91 dimSizes[outputIndex] = armnn::numeric_cast<unsigned int>(input.GetShape()[i]);
92 ++outputIndex;
93 }
94 else if (m_Param.m_KeepDims)
95 {
96 dimSizes[outputIndex] = 1;
97 ++outputIndex;
98 }
99 }
100 }
101 const TensorShape& inferredShape = TensorShape(outputRank, dimSizes.data());
102
103 ValidateAndCopyShape(outputShape, inferredShape, m_ShapeInferenceMethod, "MeanLayer");
104 }
105
Accept(ILayerVisitor & visitor) const106 void MeanLayer::Accept(ILayerVisitor& visitor) const
107 {
108 visitor.VisitMeanLayer(this, GetParameters(), GetName());
109 }
110
111 } // namespace armnn
112