ArmNN
 20.02
MeanLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "MeanLayer.hpp"
7 #include "LayerCloneBase.hpp"
8 
12 
13 #include <cstring>
14 
15 namespace armnn
16 {
17 
18 MeanLayer::MeanLayer(const armnn::MeanDescriptor& param, const char* name)
19  : LayerWithParameters(1, 1, LayerType::Mean, param, name)
20 {}
21 
22 std::unique_ptr<IWorkload> MeanLayer::CreateWorkload(const armnn::IWorkloadFactory& factory) const
23 {
24  MeanQueueDescriptor descriptor;
25  descriptor.m_Parameters.m_Axis = m_Param.m_Axis;
27 
28  return factory.CreateMean(descriptor, PrepInfoAndDesc(descriptor));
29 }
30 
32 {
33  auto layer = CloneBase<MeanLayer>(graph, m_Param, GetName());
34 
35  layer->m_Param.m_Axis = m_Param.m_Axis;
36  layer->m_Param.m_KeepDims = m_Param.m_KeepDims;
37 
38  return std::move(layer);
39 }
40 
42 {
44 
45  const TensorInfo& input = GetInputSlot(0).GetConnection()->GetTensorInfo();
46 
47  BOOST_ASSERT_MSG(input.GetNumDimensions() > 0 && input.GetNumDimensions() <= 4,
48  "MeanLayer: Mean supports up to 4D input.");
49 
50  unsigned int rank = input.GetNumDimensions();
51  unsigned int outputRank = 0;
52 
53  // Calculate output dimension
54  if (m_Param.m_KeepDims)
55  {
56  outputRank = rank;
57  }
58  else if (m_Param.m_Axis.empty())
59  {
60  outputRank = 1;
61  }
62  else if (m_Param.m_Axis.size() >= input.GetNumDimensions())
63  {
64  throw LayerValidationException("MeanLayer: Dimensions to reduce can not be bigger than input dimensions");
65  }
66  else
67  {
68  outputRank = input.GetNumDimensions() - boost::numeric_cast<unsigned int>(m_Param.m_Axis.size());
69  if (outputRank == 0)
70  {
71  outputRank = 1;
72  }
73  }
74 
75  std::vector<unsigned int> dimSizes(outputRank, 1);
76  if (!m_Param.m_Axis.empty())
77  {
78  // Skip the dimension that has been reduced unless keepDims is true.
79  unsigned int outputIndex = 0;
80  for (unsigned int i = 0; i < input.GetNumDimensions(); ++i)
81  {
82  if (std::find(m_Param.m_Axis.begin(), m_Param.m_Axis.end(), i) == m_Param.m_Axis.end())
83  {
84  dimSizes[outputIndex] = boost::numeric_cast<unsigned int>(input.GetShape()[i]);
85  ++outputIndex;
86  }
87  else if (m_Param.m_KeepDims)
88  {
89  dimSizes[outputIndex] = 1;
90  ++outputIndex;
91  }
92  }
93  }
94  const TensorShape& inferredShape = TensorShape(outputRank, dimSizes.data());
95 
96  ConditionalThrowIfNotEqual<LayerValidationException>(
97  "MeanLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
99  inferredShape);
100 }
101 
102 void MeanLayer::Accept(ILayerVisitor& visitor) const
103 {
104  visitor.VisitMeanLayer(this, GetParameters(), GetName());
105 }
106 
107 } // 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:41
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
MeanLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: MeanLayer.cpp:31
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the Mean type.
Definition: MeanLayer.cpp:22
Copyright (c) 2020 ARM Limited.
MeanLayer(const MeanDescriptor &param, const char *name)
Constructor to create a MeanLayer.
Definition: MeanLayer.cpp:18
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
Definition: MeanLayer.cpp:102
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...
std::vector< unsigned int > m_Axis
Values for the dimensions to reduce.
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:338
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:310
bool m_KeepDims
Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept...
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
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.
void Mean(const armnn::TensorInfo &inputInfo, const armnn::TensorInfo &outputInfo, const std::vector< unsigned int > &axis, Decoder< float > &input, Encoder< float > &output)
Definition: Mean.cpp:71
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:312
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:305
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:92