ArmNN
 20.02
StackLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. 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  return factory.CreateStack(descriptor, PrepInfoAndDesc(descriptor));
26 }
27 
29 {
30  return CloneBase<StackLayer>(graph, m_Param, GetName());
31 }
32 
33 std::vector<TensorShape> StackLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
34 {
35  IgnoreUnused(inputShapes);
36 
37  const TensorShape& inputShape = m_Param.m_InputShape;
38  const unsigned int inputNumDimensions = inputShape.GetNumDimensions();
39  const unsigned int axis = m_Param.m_Axis;
40 
41  BOOST_ASSERT(axis <= inputNumDimensions);
42 
43  std::vector<unsigned int> dimensionSizes(inputNumDimensions + 1, 0);
44  for (unsigned int i = 0; i < axis; ++i)
45  {
46  dimensionSizes[i] = inputShape[i];
47  }
48 
49  dimensionSizes[axis] = m_Param.m_NumInputs;
50 
51  for (unsigned int i = axis + 1; i < inputNumDimensions + 1; ++i)
52  {
53  dimensionSizes[i] = inputShape[i-1];
54  }
55 
56  TensorShape targetShape = TensorShape(inputNumDimensions + 1, dimensionSizes.data());
57 
58  return std::vector<TensorShape>({ targetShape });
59 }
60 
62 {
63  // Validates Stack layer.
64  ConditionalThrowIfNotEqual<LayerValidationException>(
65  "StackLayer: Num Input Slots must match Num Inputs.",
68 
70 
71  // Constructs and validates input shapes
72  std::vector<TensorShape> inputShapes;
73  for (unsigned int i = 0; i < GetNumInputSlots(); ++i)
74  {
76  if (inputShape != m_Param.m_InputShape)
77  {
78  throw LayerValidationException("StackLayer: TensorShape set on InputSlot[" +
79  std::to_string(i) +
80  "] does not match defined input shape");
81  }
82  inputShapes.push_back(inputShape);
83  }
84 
85  auto inferredShapes = InferOutputShapes(inputShapes);
86 
87  BOOST_ASSERT(inferredShapes.size() == 1);
88 
89  ConditionalThrowIfNotEqual<LayerValidationException>(
90  "StackLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
92  inferredShapes[0]);
93 }
94 
95 void StackLayer::Accept(ILayerVisitor& visitor) const
96 {
97  visitor.VisitStackLayer(this, GetParameters(), GetName());
98 }
99 
100 } // namespace armnn armnn
uint32_t m_Axis
0-based axis along which to stack the input tensors.
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of StackLayer.
Definition: StackLayer.cpp:61
StackDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
unsigned int GetNumInputSlots() const override
Returns the number of connectable input slots.
Definition: Layer.hpp:307
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
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, otherwise infers the output shapes from given input shapes and layer properties.
Definition: StackLayer.cpp:33
TensorShape m_InputShape
Required shape of all input tensors.
virtual std::unique_ptr< IWorkload > CreateStack(const StackQueueDescriptor &descriptor, const WorkloadInfo &info) const
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
Definition: StackLayer.cpp:95
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
virtual void VisitStackLayer(const IConnectableLayer *layer, const StackDescriptor &stackDescriptor, const char *name=nullptr)=0
Function a stack layer should call back to when its Accept(ILayerVisitor&) function is invoked...
void Stack(const StackQueueDescriptor &data, std::vector< std::unique_ptr< Decoder< float >>> &inputs, Encoder< float > &output)
Definition: Stack.cpp:12
A StackDescriptor for the StackLayer.
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:338
StackLayer(const StackDescriptor &param, const char *name)
Constructor to create a StackLayer.
Definition: StackLayer.cpp:17
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:310
StackLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: StackLayer.cpp:28
This layer represents a stack operation.
Definition: StackLayer.hpp:13
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the Stack type.
Definition: StackLayer.cpp:22
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
uint32_t m_NumInputs
Number of input tensors.
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:43
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
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
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63