ArmNN
 21.02
MeanLayer.cpp
Go to the documentation of this file.
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 
10 
14 
15 #include <cstring>
16 
17 namespace armnn
18 {
19 
20 MeanLayer::MeanLayer(const armnn::MeanDescriptor& param, const char* name)
21  : LayerWithParameters(1, 1, LayerType::Mean, param, name)
22 {}
23 
24 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;
29  SetAdditionalInfo(descriptor);
30 
31  return factory.CreateMean(descriptor, PrepInfoAndDesc(descriptor));
32 }
33 
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 
45 {
47 
48  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
49 
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 
106 void MeanLayer::Accept(ILayerVisitor& visitor) const
107 {
108  visitor.VisitMeanLayer(this, GetParameters(), GetName());
109 }
110 
111 } // namespace armnn
MeanDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of MeanLayer.
Definition: MeanLayer.cpp:44
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
MeanLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: MeanLayer.cpp:34
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the Mean type.
Definition: MeanLayer.cpp:24
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:432
Copyright (c) 2021 ARM Limited and Contributors.
MeanLayer(const MeanDescriptor &param, const char *name)
Constructor to create a MeanLayer.
Definition: MeanLayer.cpp:20
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
Definition: MeanLayer.cpp:106
virtual void VisitMeanLayer(const IConnectableLayer *layer, const MeanDescriptor &meanDescriptor, const char *name=nullptr)=0
Function a Mean layer should call back to when its Accept(ILayerVisitor&) function is invoked...
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:392
std::vector< unsigned int > m_Axis
Values for the dimensions to reduce.
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:348
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:316
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
bool m_KeepDims
Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept...
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:245
virtual std::unique_ptr< IWorkload > CreateMean(const MeanQueueDescriptor &descriptor, const WorkloadInfo &Info) const
A MeanDescriptor for the MeanLayer.
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:318
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:311
This layer represents a mean operation.
Definition: MeanLayer.hpp:14
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:191
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:408
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:419