ArmNN
 20.02
SpaceToBatchNdLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. 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 
33  return factory.CreateSpaceToBatchNd(descriptor, PrepInfoAndDesc(descriptor));
34 }
35 
37 {
38  IgnoreUnused(graph);
39  return CloneBase<SpaceToBatchNdLayer>(graph, m_Param, GetName());
40 }
41 
42 std::vector<TensorShape> SpaceToBatchNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
43 {
44  BOOST_ASSERT(inputShapes.size() == 1);
45 
46  TensorShape inputShape = inputShapes[0];
47  TensorShape outputShape(inputShape);
48 
49  outputShape[0] = inputShape[0] * std::accumulate(m_Param.m_BlockShape.begin(),
50  m_Param.m_BlockShape.end(),
51  1U,
52  std::multiplies<>());
53 
54  DataLayoutIndexed dimensionIndices = m_Param.m_DataLayout;
55  unsigned int heightIndex = dimensionIndices.GetHeightIndex();
56  unsigned int widthIndex = dimensionIndices.GetWidthIndex();
57 
58  std::pair<unsigned int, unsigned int> heightPad = m_Param.m_PadList[0];
59  std::pair<unsigned int, unsigned int> widthPad = m_Param.m_PadList[1];
60 
61  outputShape[heightIndex] =
62  (inputShape[heightIndex] + heightPad.first + heightPad.second) / m_Param.m_BlockShape[0];
63  outputShape[widthIndex] =
64  (inputShape[widthIndex] + widthPad.first + widthPad.second) / m_Param.m_BlockShape[1];
65 
66  return std::vector<TensorShape>({ outputShape });
67 }
68 
70 {
72 
73  std::vector<TensorShape> inferredShapes = InferOutputShapes({
75 
76  BOOST_ASSERT(inferredShapes.size() == 1);
77 
78  ConditionalThrowIfNotEqual<LayerValidationException>(
79  "SpaceToBatchNdLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
81  inferredShapes[0]);
82 }
83 
85 {
87 }
88 
89 } // 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:88
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of SpaceToBatchNdLayer.
Copyright (c) 2020 ARM Limited.
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.
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:338
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:310
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...
std::vector< unsigned int > m_BlockShape
Block shape value.
#define CHECK_LOCATION()
Definition: Exceptions.hpp:192
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.
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:312
virtual const TensorInfo & GetTensorInfo() const =0
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:305
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