ArmNN
 24.02
StackLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "StackLayer.hpp"
6 #include "LayerCloneBase.hpp"
7 
8 #include <armnn/TypesUtils.hpp>
11 
12 #include <queue>
13 
14 namespace armnn
15 {
16 
17 StackLayer::StackLayer(const StackDescriptor& param, const char* name)
18  : LayerWithParameters(param.m_NumInputs, 1, LayerType::Stack, param, name)
19 {
20 }
21 
22 std::unique_ptr<IWorkload> StackLayer::CreateWorkload(const IWorkloadFactory& factory) const
23 {
24  StackQueueDescriptor descriptor;
25  SetAdditionalInfo(descriptor);
26 
27  return factory.CreateWorkload(LayerType::Stack, descriptor, PrepInfoAndDesc(descriptor));
28 }
29 
31 {
32  return CloneBase<StackLayer>(graph, m_Param, GetName());
33 }
34 
35 std::vector<TensorShape> StackLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
36 {
37  IgnoreUnused(inputShapes);
38 
39  const TensorShape& inputShape = m_Param.m_InputShape;
40  const unsigned int inputNumDimensions = inputShape.GetNumDimensions();
41  const unsigned int axis = m_Param.m_Axis;
42 
43  ARMNN_ASSERT(axis <= inputNumDimensions);
44 
45  std::vector<unsigned int> dimensionSizes(inputNumDimensions + 1, 0);
46  for (unsigned int i = 0; i < axis; ++i)
47  {
48  dimensionSizes[i] = inputShape[i];
49  }
50 
51  dimensionSizes[axis] = m_Param.m_NumInputs;
52 
53  for (unsigned int i = axis + 1; i < inputNumDimensions + 1; ++i)
54  {
55  dimensionSizes[i] = inputShape[i-1];
56  }
57 
58  TensorShape targetShape = TensorShape(inputNumDimensions + 1, dimensionSizes.data());
59 
60  return std::vector<TensorShape>({ targetShape });
61 }
62 
64 {
65  // Validates Stack layer.
66  ConditionalThrowIfNotEqual<LayerValidationException>(
67  "StackLayer: Num Input Slots must match Num Inputs.",
70 
72 
73  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
74 
76 
77  // Constructs and validates input shapes
78  std::vector<TensorShape> inputShapes;
79  for (unsigned int i = 0; i < GetNumInputSlots(); ++i)
80  {
81  TensorShape inputShape = GetInputSlot(i).GetTensorInfo().GetShape();
82  if (inputShape != m_Param.m_InputShape)
83  {
84  throw LayerValidationException("StackLayer: TensorShape set on InputSlot[" +
85  std::to_string(i) +
86  "] does not match defined input shape");
87  }
88  inputShapes.push_back(inputShape);
89  }
90 
91  auto inferredShapes = InferOutputShapes(inputShapes);
92 
93  ARMNN_ASSERT(inferredShapes.size() == 1);
94 
95  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "StackLayer");
96 }
97 
99 {
100  strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
101 }
102 
103 } // namespace armnn armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
WorkloadData.hpp
armnn::StackQueueDescriptor
Definition: WorkloadData.hpp:152
armnn::StackLayer::Clone
StackLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: StackLayer.cpp:30
TypesUtils.hpp
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::Layer::ValidateAndCopyShape
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:435
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
armnn::LayerType::Stack
@ Stack
armnn::StackDescriptor
A StackDescriptor for the StackLayer.
Definition: Descriptors.hpp:1251
armnn::StackLayer
This layer represents a stack operation.
Definition: StackLayer.hpp:13
armnn::Stack
void Stack(const StackQueueDescriptor &data, std::vector< std::unique_ptr< Decoder< float >>> &inputs, Encoder< float > &output, const TensorInfo &inputInfo, const TensorInfo &outputInfo)
Definition: Stack.cpp:12
armnn::IStrategy
Definition: IStrategy.hpp:16
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::LayerWithParameters< StackDescriptor >::GetParameters
const StackDescriptor & GetParameters() const override
Definition: LayerWithParameters.hpp:19
WorkloadFactory.hpp
armnn::LayerWithParameters
Definition: LayerWithParameters.hpp:14
armnn::Layer::GetName
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:332
armnn::InputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition: Layer.cpp:592
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::StackDescriptor::m_NumInputs
uint32_t m_NumInputs
Number of input tensors.
Definition: Descriptors.hpp:1275
armnn::LayerWithParameters< StackDescriptor >::m_Param
StackDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
Definition: LayerWithParameters.hpp:52
armnn::TensorShape::GetNumDimensions
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
armnn::LayerWithParameters< StackDescriptor >::PrepInfoAndDesc
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *Layer::CreateWorkload.
Definition: LayerWithParameters.hpp:44
armnn::StackDescriptor::m_Axis
uint32_t m_Axis
0-based axis along which to stack the input tensors.
Definition: Descriptors.hpp:1273
armnn::LayerValidationException
Definition: Exceptions.hpp:105
armnn::IWorkloadFactory
Definition: WorkloadFactory.hpp:22
armnn::StackLayer::StackLayer
StackLayer(const StackDescriptor &param, const char *name)
Constructor to create a StackLayer.
Definition: StackLayer.cpp:17
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:504
armnn::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:287
armnn::StackLayer::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the Stack type.
Definition: StackLayer.cpp:22
armnn::StackDescriptor::m_InputShape
TensorShape m_InputShape
Required shape of all input tensors.
Definition: Descriptors.hpp:1277
armnn::Layer::GetNumInputSlots
unsigned int GetNumInputSlots() const override
Returns the number of connectable input slots.
Definition: Layer.hpp:334
armnn::StackLayer::InferOutputShapes
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
By default returns inputShapes if the number of inputs are equal to number of outputs,...
Definition: StackLayer.cpp:35
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn::StackLayer::ExecuteStrategy
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
Definition: StackLayer.cpp:98
armnn::IgnoreUnused
void IgnoreUnused(Ts &&...)
Definition: IgnoreUnused.hpp:14
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
StackLayer.hpp
armnn::Layer::VerifyLayerConnections
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:391
armnn::StackLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of StackLayer.
Definition: StackLayer.cpp:63
armnn::Layer::m_ShapeInferenceMethod
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:441
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::Graph
Definition: Graph.hpp:30
armnn::IWorkloadFactory::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const =0
Backends should implement their own CreateWorkload function with a switch statement.
armnn::IStrategy::ExecuteStrategy
virtual void ExecuteStrategy(const IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
LayerCloneBase.hpp