ArmNN
 24.02
SpaceToBatchNdLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2018-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include "LayerCloneBase.hpp"
8 
11 
12 #include <numeric>
13 
14 using namespace armnnUtils;
15 
16 namespace armnn
17 {
18 
20  : LayerWithParameters(1, 1, LayerType::SpaceToBatchNd, param, name)
21 {}
22 
23 std::unique_ptr<IWorkload> SpaceToBatchNdLayer::CreateWorkload(const IWorkloadFactory& factory) const
24 {
28  SetAdditionalInfo(descriptor);
29 
30  return factory.CreateWorkload(LayerType::SpaceToBatchNd, descriptor, PrepInfoAndDesc(descriptor));
31 }
32 
34 {
35  IgnoreUnused(graph);
36  return CloneBase<SpaceToBatchNdLayer>(graph, m_Param, GetName());
37 }
38 
39 std::vector<TensorShape> SpaceToBatchNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
40 {
41  const TensorShape inputShape = inputShapes[0];
42  TensorShape outputShape(inputShape);
43 
44  outputShape[0] = inputShape[0] * std::accumulate(m_Param.m_BlockShape.begin(),
45  m_Param.m_BlockShape.end(),
46  1U,
47  std::multiplies<>());
48 
49  // In a 4D tensor, there will be 2 spatialDimensions (H and W), and the for loop will run twice.
50  // In a 3D tensor, there will be 1 spatialDimensions, and the for loop will run once.
51  unsigned int firstSpatialDimension = m_Param.m_DataLayout == DataLayout::NCHW ? 2 : 1;
52  for (unsigned int i = 0; i < m_Param.m_BlockShape.size(); ++i)
53  {
54  unsigned int spatialDimension = firstSpatialDimension + i;
55  outputShape[spatialDimension] =
56  (inputShape[spatialDimension] + m_Param.m_PadList[i].first + m_Param.m_PadList[i].second)
57  / m_Param.m_BlockShape[i];
58  }
59 
60  return std::vector<TensorShape>({ outputShape });
61 }
62 
64 {
66 
67  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
68 
70 
71  std::vector<TensorShape> inferredShapes = InferOutputShapes({
73 
74  ARMNN_ASSERT(inferredShapes.size() == 1);
75 
76  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "SpaceToBatchNdLayer");
77 }
78 
80 {
81  strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
82 }
83 
84 } // namespace
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
armnn::SpaceToBatchNdDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1071
WorkloadData.hpp
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::Layer::ValidateAndCopyShape
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:435
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
armnn::SpaceToBatchNdLayer::SpaceToBatchNdLayer
SpaceToBatchNdLayer(const SpaceToBatchNdDescriptor param, const char *name)
Constructor to create a SpaceToBatchNdLayer.
Definition: SpaceToBatchNdLayer.cpp:19
armnn::SpaceToBatchNdLayer::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the SpaceToBatchNd type.
Definition: SpaceToBatchNdLayer.cpp:23
armnn::SpaceToBatchNdLayer::InferOutputShapes
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,...
Definition: SpaceToBatchNdLayer.cpp:39
armnn::SpaceToBatchNdDescriptor::m_BlockShape
std::vector< unsigned int > m_BlockShape
Block shape value.
Definition: Descriptors.hpp:1066
armnn::IStrategy
Definition: IStrategy.hpp:16
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::LayerWithParameters< SpaceToBatchNdDescriptor >::GetParameters
const SpaceToBatchNdDescriptor & GetParameters() const override
Definition: LayerWithParameters.hpp:19
WorkloadFactory.hpp
armnn::LayerWithParameters
Definition: LayerWithParameters.hpp:14
armnn::Layer::GetName
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:332
armnn::InputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition: Layer.cpp:592
armnn::SpaceToBatchNdDescriptor::m_PadList
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding values for the input dimension: heightPad{top, bottom} widthPad{left,...
Definition: Descriptors.hpp:1069
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::LayerWithParameters< SpaceToBatchNdDescriptor >::m_Param
SpaceToBatchNdDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
Definition: LayerWithParameters.hpp:52
armnn::QueueDescriptorWithParameters::m_Parameters
LayerDescriptor m_Parameters
Definition: WorkloadData.hpp:66
armnn::LayerWithParameters< SpaceToBatchNdDescriptor >::PrepInfoAndDesc
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *Layer::CreateWorkload.
Definition: LayerWithParameters.hpp:44
armnn::IWorkloadFactory
Definition: WorkloadFactory.hpp:22
armnnUtils
Definition: CompatibleTypes.hpp:10
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:504
armnn::SpaceToBatchNdLayer
This layer represents a SpaceToBatchNd operation.
Definition: SpaceToBatchNdLayer.hpp:14
armnn::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:287
armnn::SpaceToBatchNdDescriptor
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
Definition: Descriptors.hpp:1043
armnn::SpaceToBatchNd
void SpaceToBatchNd(const TensorInfo &inputInfo, const TensorInfo &outputInfo, const SpaceToBatchNdDescriptor &params, Decoder< float > &inputData, Encoder< float > &outputData)
Definition: SpaceToBatchNd.cpp:48
armnn::SpaceToBatchNdLayer::ExecuteStrategy
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
Definition: SpaceToBatchNdLayer.cpp:79
armnn::SpaceToBatchNdLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of SpaceToBatchNdLayer.
Definition: SpaceToBatchNdLayer.cpp:63
armnn::SpaceToBatchNdLayer::Clone
SpaceToBatchNdLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: SpaceToBatchNdLayer.cpp:33
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn::LayerType::SpaceToBatchNd
@ SpaceToBatchNd
armnn::IgnoreUnused
void IgnoreUnused(Ts &&...)
Definition: IgnoreUnused.hpp:14
SpaceToBatchNdLayer.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::Layer::VerifyLayerConnections
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:391
armnn::SpaceToBatchNdQueueDescriptor
Definition: WorkloadData.hpp:385
armnn::Layer::m_ShapeInferenceMethod
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:441
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::Graph
Definition: Graph.hpp:30
armnn::IWorkloadFactory::CreateWorkload
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.
armnn::IStrategy::ExecuteStrategy
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
LayerCloneBase.hpp
armnn::DataLayout::NCHW
@ NCHW