ArmNN
 21.02
SpaceToBatchNdLayer.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 
7 #include "LayerCloneBase.hpp"
8 
9 #include <armnn/TypesUtils.hpp>
10 
12 
15 
16 #include <numeric>
17 
18 using namespace armnnUtils;
19 
20 namespace armnn
21 {
22 
24  : LayerWithParameters(1, 1, LayerType::SpaceToBatchNd, param, name)
25 {}
26 
27 std::unique_ptr<IWorkload> SpaceToBatchNdLayer::CreateWorkload(const IWorkloadFactory& factory) const
28 {
32  SetAdditionalInfo(descriptor);
33 
34  return factory.CreateSpaceToBatchNd(descriptor, PrepInfoAndDesc(descriptor));
35 }
36 
38 {
39  IgnoreUnused(graph);
40  return CloneBase<SpaceToBatchNdLayer>(graph, m_Param, GetName());
41 }
42 
43 std::vector<TensorShape> SpaceToBatchNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
44 {
45  ARMNN_ASSERT(inputShapes.size() == 1);
46 
47  TensorShape inputShape = inputShapes[0];
48  TensorShape outputShape(inputShape);
49 
50  outputShape[0] = inputShape[0] * std::accumulate(m_Param.m_BlockShape.begin(),
51  m_Param.m_BlockShape.end(),
52  1U,
53  std::multiplies<>());
54 
55  DataLayoutIndexed dimensionIndices = m_Param.m_DataLayout;
56  unsigned int heightIndex = dimensionIndices.GetHeightIndex();
57  unsigned int widthIndex = dimensionIndices.GetWidthIndex();
58 
59  std::pair<unsigned int, unsigned int> heightPad = m_Param.m_PadList[0];
60  std::pair<unsigned int, unsigned int> widthPad = m_Param.m_PadList[1];
61 
62  outputShape[heightIndex] =
63  (inputShape[heightIndex] + heightPad.first + heightPad.second) / m_Param.m_BlockShape[0];
64  outputShape[widthIndex] =
65  (inputShape[widthIndex] + widthPad.first + widthPad.second) / m_Param.m_BlockShape[1];
66 
67  return std::vector<TensorShape>({ outputShape });
68 }
69 
71 {
73 
74  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
75 
77 
78  std::vector<TensorShape> inferredShapes = InferOutputShapes({
80 
81  ARMNN_ASSERT(inferredShapes.size() == 1);
82 
83  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "SpaceToBatchNdLayer");
84 }
85 
87 {
89 }
90 
91 } // namespace
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.
SpaceToBatchNdDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const SpaceToBatchNdDescriptor & GetParameters() const
unsigned int GetWidthIndex() const
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of SpaceToBatchNdLayer.
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:432
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
const IOutputSlot * GetConnection() const override
Definition: Layer.hpp:199
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding values for the input dimension: heightPad{top, bottom} widthPad{left, right}.
This layer represents a SpaceToBatchNd operation.
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:392
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the SpaceToBatchNd type.
unsigned int GetHeightIndex() const
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:348
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:316
virtual std::unique_ptr< IWorkload > CreateSpaceToBatchNd(const SpaceToBatchNdQueueDescriptor &descriptor, const WorkloadInfo &info) const
Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout...
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::vector< unsigned int > m_BlockShape
Block shape value.
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
void SpaceToBatchNd(const TensorInfo &inputInfo, const TensorInfo &outputInfo, const SpaceToBatchNdDescriptor &params, Decoder< float > &inputData, Encoder< float > &outputData)
SpaceToBatchNdLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:245
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
SpaceToBatchNdLayer(const SpaceToBatchNdDescriptor param, const char *name)
Constructor to create a SpaceToBatchNdLayer.
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:318
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:311
virtual void VisitSpaceToBatchNdLayer(const IConnectableLayer *layer, const SpaceToBatchNdDescriptor &spaceToBatchNdDescriptor, const char *name=nullptr)=0
Function a space to batch layer should call back to when its Accept(ILayerVisitor&) function is invok...
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:408
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:419