ArmNN
 21.02
ReduceLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Samsung Electronics Co Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ReduceLayer.hpp"
7 #include "LayerCloneBase.hpp"
8 
9 #include <armnn/TypesUtils.hpp>
10 
13 
14 namespace armnn
15 {
16 
17 ReduceLayer::ReduceLayer(const ReduceDescriptor& param, const char* name)
18  : LayerWithParameters(1, 1, LayerType::Reduce, param, name)
19 {
20 }
21 
22 std::unique_ptr<IWorkload> ReduceLayer::CreateWorkload(const IWorkloadFactory& factory) const
23 {
24  ReduceQueueDescriptor descriptor;
25  return factory.CreateReduce(descriptor, PrepInfoAndDesc(descriptor));
26 }
27 
29 {
30  return CloneBase<ReduceLayer>(graph, m_Param, GetName());
31 }
32 
34 {
36 
37  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
38 
40 
41  const TensorInfo& input = GetInputSlot(0).GetConnection()->GetTensorInfo();
42 
43  ARMNN_ASSERT_MSG(input.GetNumDimensions() > 0 && input.GetNumDimensions() <= 4,
44  "ReduceLayer: Reduce supports up to 4D input.");
45 
46  unsigned int rank = input.GetNumDimensions();
47  unsigned int outputRank = 0;
48 
49  // Calculate output dimension
50  if (m_Param.m_KeepDims)
51  {
52  outputRank = rank;
53  }
54  else if (m_Param.m_vAxis.empty())
55  {
56  outputRank = 1;
57  }
58  else if (m_Param.m_vAxis.size() > input.GetNumDimensions())
59  {
60  throw LayerValidationException("ReduceLayer: Dimensions to reduce can not be bigger than input dimensions");
61  }
62  else
63  {
64  outputRank = input.GetNumDimensions() - armnn::numeric_cast<unsigned int>(m_Param.m_vAxis.size());
65  if (outputRank == 0)
66  {
67  outputRank = 1;
68  }
69  }
70 
71  std::vector<unsigned int> dimSizes(outputRank, 1);
72  if (!m_Param.m_vAxis.empty())
73  {
74  // Skip the dimension that has been reduced unless keepDims is true.
75  unsigned int outputIndex = 0;
76  for (unsigned int i = 0; i < input.GetNumDimensions(); ++i)
77  {
78  if (std::find(m_Param.m_vAxis.begin(), m_Param.m_vAxis.end(), i) == m_Param.m_vAxis.end())
79  {
80  dimSizes[outputIndex] = armnn::numeric_cast<unsigned int>(input.GetShape()[i]);
81  ++outputIndex;
82  }
83  else if (m_Param.m_KeepDims)
84  {
85  dimSizes[outputIndex] = 1;
86  ++outputIndex;
87  }
88  }
89  }
90  const TensorShape& inferredShape = TensorShape(outputRank, dimSizes.data());
91 
92  ValidateAndCopyShape(outputShape, inferredShape, m_ShapeInferenceMethod, "ReduceLayer");
93 }
94 
95 void ReduceLayer::Accept(ILayerVisitor& visitor) const
96 {
97  visitor.VisitReduceLayer(this, GetParameters(), GetName());
98 }
99 
100 } // namespace armnn
ReduceDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const ReduceDescriptor & GetParameters() const
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
Definition: ReduceLayer.cpp:95
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of ReduceLayer.
Definition: ReduceLayer.cpp:33
void Reduce(const TensorInfo &inputInfo, const TensorInfo &outputInfo, Decoder< float > &input, Encoder< float > &output, const std::vector< uint32_t > axis, const ReduceOperation reduceOperation)
Definition: Reduce.cpp:71
bool m_KeepDims
if true then output shape has no change.
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the Reduce type.
Definition: ReduceLayer.cpp:22
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:432
Copyright (c) 2021 ARM Limited and Contributors.
This layer represents a reduction operation.
Definition: ReduceLayer.hpp:13
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:392
ReduceLayer(const ReduceDescriptor &param, const char *name)
Constructor to create a ReduceLayer.
Definition: ReduceLayer.cpp:17
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
A ReduceDescriptor for the REDUCE operators.
virtual void VisitReduceLayer(const IConnectableLayer *layer, const ReduceDescriptor &reduceDescriptor, const char *name=nullptr)=0
Function that a reduce layer should call back to when its Accept(ILayerVisitor&) function is invoked...
virtual std::unique_ptr< IWorkload > CreateReduce(const ReduceQueueDescriptor &descriptor, const WorkloadInfo &info) const
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
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
ReduceLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: ReduceLayer.cpp:28
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