ArmNN
 20.08
StackLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 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  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  ARMNN_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  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
72 
74 
75  // Constructs and validates input shapes
76  std::vector<TensorShape> inputShapes;
77  for (unsigned int i = 0; i < GetNumInputSlots(); ++i)
78  {
80  if (inputShape != m_Param.m_InputShape)
81  {
82  throw LayerValidationException("StackLayer: TensorShape set on InputSlot[" +
83  std::to_string(i) +
84  "] does not match defined input shape");
85  }
86  inputShapes.push_back(inputShape);
87  }
88 
89  auto inferredShapes = InferOutputShapes(inputShapes);
90 
91  ARMNN_ASSERT(inferredShapes.size() == 1);
92 
93  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "StackLayer");
94 }
95 
96 void StackLayer::Accept(ILayerVisitor& visitor) const
97 {
98  visitor.VisitStackLayer(this, GetParameters(), GetName());
99 }
100 
101 } // 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:309
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
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
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:432
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
Definition: StackLayer.cpp:96
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 ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:392
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:344
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:312
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 ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
uint32_t m_NumInputs
Number of input tensors.
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:175
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:314
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:307
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:387