2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "MeanLayer.hpp"
7 #include "LayerCloneBase.hpp"
9 #include <backendsCommon/CpuTensorHandle.hpp>
10 #include <backendsCommon/WorkloadData.hpp>
11 #include <backendsCommon/WorkloadFactory.hpp>
18 MeanLayer::MeanLayer(const armnn::MeanDescriptor& param, const char* name)
19 : LayerWithParameters(1, 1, LayerType::Mean, param, name)
22 std::unique_ptr<IWorkload> MeanLayer::CreateWorkload(const armnn::Graph& graph,
23 const armnn::IWorkloadFactory& factory) const
25 MeanQueueDescriptor descriptor;
26 descriptor.m_Parameters.m_Axis = m_Param.m_Axis;
27 descriptor.m_Parameters.m_KeepDims = m_Param.m_KeepDims;
29 return factory.CreateMean(descriptor, PrepInfoAndDesc(descriptor, graph));
32 MeanLayer* MeanLayer::Clone(Graph& graph) const
34 auto layer = CloneBase<MeanLayer>(graph, m_Param, GetName());
36 layer->m_Param.m_Axis = m_Param.m_Axis;
37 layer->m_Param.m_KeepDims = m_Param.m_KeepDims;
39 return std::move(layer);
42 void MeanLayer::ValidateTensorShapesFromInputs()
44 VerifyLayerConnections(1, CHECK_LOCATION());
46 const TensorInfo& input = GetInputSlot(0).GetConnection()->GetTensorInfo();
48 BOOST_ASSERT_MSG(input.GetNumDimensions() > 0 && input.GetNumDimensions() <= MaxNumOfTensorDimensions,
49 "MeanLayer: Mean supports up to 4D input.");
51 unsigned int rank = input.GetNumDimensions();
52 unsigned int outputRank = 0;
54 // Calculate output dimension
55 if (m_Param.m_KeepDims)
59 else if (m_Param.m_Axis.empty())
63 else if (m_Param.m_Axis.size() >= input.GetNumDimensions())
65 throw LayerValidationException("MeanLayer: Dimensions to reduce can not be bigger than input dimensions");
69 outputRank = input.GetNumDimensions() - boost::numeric_cast<unsigned int>(m_Param.m_Axis.size());
76 std::vector<unsigned int> dimSizes(outputRank, 1);
77 if (!m_Param.m_Axis.empty())
79 // Skip the dimension that has been reduced unless keepDims is true.
80 unsigned int outputIndex = 0;
81 for (unsigned int i = 0; i < input.GetNumDimensions(); ++i)
83 if (std::find(m_Param.m_Axis.begin(), m_Param.m_Axis.end(), i) == m_Param.m_Axis.end())
85 dimSizes[outputIndex] = boost::numeric_cast<unsigned int>(input.GetShape()[i]);
88 else if (m_Param.m_KeepDims)
90 dimSizes[outputIndex] = 1;
95 const TensorShape& inferredShape = TensorShape(outputRank, dimSizes.data());
97 ConditionalThrowIfNotEqual<LayerValidationException>(
98 "MeanLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
99 GetOutputSlot(0).GetTensorInfo().GetShape(),