ArmNN
 22.05
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  descriptor.m_Parameters.m_vAxis = m_Param.m_vAxis;
28  SetAdditionalInfo(descriptor);
29 
30  return factory.CreateWorkload(LayerType::Reduce, descriptor, PrepInfoAndDesc(descriptor));
31 }
32 
34 {
35  auto layer = CloneBase<ReduceLayer>(graph, m_Param, GetName());
36  layer->m_Param.m_vAxis = m_Param.m_vAxis;
37  layer->m_Param.m_KeepDims = m_Param.m_KeepDims;
38  layer->m_Param.m_ReduceOperation = m_Param.m_ReduceOperation;
39 
40  return std::move(layer);
41 }
42 
44 {
46 
47  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
48 
50 
51  const TensorInfo& input = GetInputSlot(0).GetConnection()->GetTensorInfo();
52 
53  ARMNN_ASSERT_MSG(input.GetNumDimensions() > 0 && input.GetNumDimensions() <= 4,
54  "ReduceLayer: Reduce supports up to 4D input.");
55 
56  unsigned int rank = input.GetNumDimensions();
57  unsigned int outputRank = 0;
58 
59  // Calculate output dimension
60  if (m_Param.m_KeepDims)
61  {
62  outputRank = rank;
63  }
64  else if (m_Param.m_vAxis.empty())
65  {
66  outputRank = 1;
67  }
68  else if (m_Param.m_vAxis.size() > input.GetNumDimensions())
69  {
70  throw LayerValidationException("ReduceLayer: Dimensions to reduce can not be bigger than input dimensions");
71  }
72  else
73  {
74  outputRank = input.GetNumDimensions() - armnn::numeric_cast<unsigned int>(m_Param.m_vAxis.size());
75  if (outputRank == 0)
76  {
77  outputRank = 1;
78  }
79  }
80 
81  std::vector<unsigned int> dimSizes(outputRank, 1);
82  if (!m_Param.m_vAxis.empty())
83  {
84  // Skip the dimension that has been reduced unless keepDims is true.
85  unsigned int outputIndex = 0;
86  for (unsigned int i = 0; i < input.GetNumDimensions(); ++i)
87  {
88  if (std::find(m_Param.m_vAxis.begin(), m_Param.m_vAxis.end(), i) == m_Param.m_vAxis.end())
89  {
90  dimSizes[outputIndex] = armnn::numeric_cast<unsigned int>(input.GetShape()[i]);
91  ++outputIndex;
92  }
93  else if (m_Param.m_KeepDims)
94  {
95  dimSizes[outputIndex] = 1;
96  ++outputIndex;
97  }
98  }
99  }
100  const TensorShape& inferredShape = TensorShape(outputRank, dimSizes.data());
101 
102  ValidateAndCopyShape(outputShape, inferredShape, m_ShapeInferenceMethod, "ReduceLayer");
103 }
104 
106 void ReduceLayer::Accept(ILayerVisitor& visitor) const
107 {
108  visitor.VisitReduceLayer(this, GetParameters(), GetName());
109 }
111 
112 } // namespace armnn
ReduceDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
#define ARMNN_NO_DEPRECATE_WARN_BEGIN
Definition: Deprecated.hpp:33
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of ReduceLayer.
Definition: ReduceLayer.cpp:43
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:70
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:491
ReduceOperation m_ReduceOperation
Specifies the reduction operation to execute.
Copyright (c) 2021 ARM Limited and Contributors.
const ReduceDescriptor & GetParameters() const override
This layer represents a reduction operation.
Definition: ReduceLayer.hpp:13
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:204
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:422
ARMNN_NO_DEPRECATE_WARN_BEGIN void Accept(ILayerVisitor &visitor) const override
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:378
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:322
#define ARMNN_NO_DEPRECATE_WARN_END
Definition: Deprecated.hpp:34
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
A ReduceDescriptor for the REDUCE operators.
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:274
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:324
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:317
ReduceLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: ReduceLayer.cpp:33
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:195
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:421
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:467