ArmNN
 25.11
Loading...
Searching...
No Matches
StackLayer.cpp
Go to the documentation of this file.
1//
2// Copyright © 2019-2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#include "StackLayer.hpp"
6#include "LayerCloneBase.hpp"
7
11
12#include <queue>
13
14namespace armnn
15{
16
17StackLayer::StackLayer(const StackDescriptor& param, const char* name)
18 : LayerWithParameters(param.m_NumInputs, 1, LayerType::Stack, param, name)
19{
20}
21
22std::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
35std::vector<TensorShape> StackLayer::InferOutputShapes(const std::vector<TensorShape>&) const
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 if (axis > inputNumDimensions)
42 {
43 throw armnn::Exception("axis must not be greater than input dimensions (\""
44 + std::to_string(axis) +
45 "\" vs \""
46 + std::to_string(inputNumDimensions) + "\").");
47 }
48
49 std::vector<unsigned int> dimensionSizes(inputNumDimensions + 1, 0);
50 for (unsigned int i = 0; i < axis; ++i)
51 {
52 dimensionSizes[i] = inputShape[i];
53 }
54
55 dimensionSizes[axis] = m_Param.m_NumInputs;
56
57 for (unsigned int i = axis + 1; i < inputNumDimensions + 1; ++i)
58 {
59 dimensionSizes[i] = inputShape[i-1];
60 }
61
62 TensorShape targetShape = TensorShape(inputNumDimensions + 1, dimensionSizes.data());
63
64 return std::vector<TensorShape>({ targetShape });
65}
66
68{
69 // Validates Stack layer.
71 "StackLayer: Num Input Slots must match Num Inputs.",
72 m_Param.m_NumInputs,
74
76
77 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
78
80
81 // Constructs and validates input shapes
82 std::vector<TensorShape> inputShapes;
83 for (unsigned int i = 0; i < GetNumInputSlots(); ++i)
84 {
86 if (inputShape != m_Param.m_InputShape)
87 {
88 throw LayerValidationException("StackLayer: TensorShape set on InputSlot[" +
89 std::to_string(i) +
90 "] does not match defined input shape");
91 }
92 inputShapes.push_back(inputShape);
93 }
94
95 auto inferredShapes = InferOutputShapes(inputShapes);
96
97 if (inferredShapes.size() != 1)
98 {
99 throw armnn::LayerValidationException("inferredShapes has "
100 + std::to_string(inferredShapes.size()) +
101 " elements - should only have 1.");
102 }
103
104 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "StackLayer");
105}
106
108{
109 strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
110}
111
112} // namespace armnn armnn
#define CHECK_LOCATION()
Base class for all ArmNN exceptions so that users can filter to just those.
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
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.
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition Layer.cpp:614
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition Layer.cpp:410
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition Layer.cpp:526
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition Layer.hpp:339
LayerType * CloneBase(Graph &graph, Params &&... params) const
unsigned int GetNumInputSlots() const override
Returns the number of connectable input slots.
Definition Layer.hpp:334
const char * GetName() const override
Returns the name of the layer.
Definition Layer.hpp:332
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition Layer.cpp:457
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition Layer.cpp:303
ShapeInferenceMethod m_ShapeInferenceMethod
Definition Layer.hpp:441
LayerWithParameters(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const StackDescriptor &param, const char *name)
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
const StackDescriptor & GetParameters() const override
const TensorInfo & GetTensorInfo() const override
Definition Layer.cpp:100
StackLayer(const StackDescriptor &param, const char *name)
Constructor to create a StackLayer.
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
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,...
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of StackLayer.
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the Stack type.
StackLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
const TensorShape & GetShape() const
Definition Tensor.hpp:193
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition Tensor.cpp:174
Copyright (c) 2021 ARM Limited and Contributors.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition Types.hpp:494
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
void ConditionalThrowIfNotEqual(const std::string &message, const ComparedType &leftHandSide, const ComparedType &rightHandSide)
ComparedType must support: operator==(const ComparedType&) operator<<(ostream&, const ComparedType&)
A StackDescriptor for the StackLayer.