ArmNN
 23.08
BatchToSpaceNdLayer.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::BatchToSpaceNd, param, name)
21 {
22 }
23 
24 std::unique_ptr<IWorkload> BatchToSpaceNdLayer::CreateWorkload(const IWorkloadFactory& factory) const
25 {
27  SetAdditionalInfo(descriptor);
28 
29  return factory.CreateWorkload(LayerType::BatchToSpaceNd, descriptor, PrepInfoAndDesc(descriptor));
30 }
31 
33 {
34  auto layer = CloneBase<BatchToSpaceNdLayer>(graph, m_Param, GetName());
35  return std::move(layer);
36 }
37 
39 {
41 
42  const TensorShape &outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
43 
45 
46  auto inferredShapes = InferOutputShapes({GetInputSlot(0).GetTensorInfo().GetShape()});
47 
48  ARMNN_ASSERT(inferredShapes.size() == 1);
49 
50  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "BatchToSpaceNdLayer");
51 }
52 
53 std::vector<TensorShape> BatchToSpaceNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
54 {
55  const TensorShape& inputShape = inputShapes[0];
56  TensorShape outputShape(inputShape);
57 
58  unsigned int accumulatedBlockShape = std::accumulate(m_Param.m_BlockShape.begin(),
59  m_Param.m_BlockShape.end(),
60  1U,
61  std::multiplies<>());
62  outputShape[0] = (inputShape[0] / accumulatedBlockShape) < 1 ? 1 : (inputShape[0] / accumulatedBlockShape) ;
63 
64  // In a 4D tensor, there will be 2 spatialDimensions (H and W), and the for loop will run twice.
65  // In a 3D tensor, there will be 1 spatialDimensions, and the for loop will run once.
66  unsigned int firstSpatialDimension = m_Param.m_DataLayout == DataLayout::NCHW ? 2 : 1;
67  for (unsigned int i = 0; i < m_Param.m_BlockShape.size(); ++i)
68  {
69  unsigned int spatialDimension = firstSpatialDimension + i;
70  unsigned int cropSize = m_Param.m_Crops[i].first + m_Param.m_Crops[i].second;
71  unsigned int outputSize = inputShape[spatialDimension] * m_Param.m_BlockShape[i];
72  outputShape[spatialDimension] = outputSize - cropSize;
73  }
74 
75  return std::vector<TensorShape>({ outputShape });
76 }
77 
79 {
80  strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
81 }
82 
83 } // namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:92
WorkloadData.hpp
armnn::BatchToSpaceNdQueueDescriptor
Definition: WorkloadData.hpp:457
armnn::BatchToSpaceNdDescriptor::m_BlockShape
std::vector< unsigned int > m_BlockShape
Block shape values.
Definition: Descriptors.hpp:898
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::BatchToSpaceNd
void BatchToSpaceNd(const TensorInfo &inputInfo, const TensorInfo &outputInfo, const BatchToSpaceNdDescriptor &params, Decoder< float > &inputData, Encoder< float > &outputData)
Definition: BatchToSpaceNd.cpp:50
armnn::IStrategy
Definition: IStrategy.hpp:16
armnn::BatchToSpaceNdDescriptor::m_Crops
std::vector< std::pair< unsigned int, unsigned int > > m_Crops
The values to crop from the input dimension.
Definition: Descriptors.hpp:900
armnn::BatchToSpaceNdLayer
This layer represents a BatchToSpaceNd operation.
Definition: BatchToSpaceNdLayer.hpp:13
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< BatchToSpaceNdDescriptor >::GetParameters
const BatchToSpaceNdDescriptor & 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::BatchToSpaceNdLayer::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the BatchToSpaceNd type.
Definition: BatchToSpaceNdLayer.cpp:24
armnn::InputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition: Layer.cpp:592
armnn::BatchToSpaceNdLayer::BatchToSpaceNdLayer
BatchToSpaceNdLayer(const BatchToSpaceNdDescriptor &param, const char *name)
Constructor to create a BatchToSpaceNdLayer.
Definition: BatchToSpaceNdLayer.cpp:19
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::BatchToSpaceNdLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of BatchToSpaceNdLayer.
Definition: BatchToSpaceNdLayer.cpp:38
armnn::BatchToSpaceNdLayer::ExecuteStrategy
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
Definition: BatchToSpaceNdLayer.cpp:78
armnn::LayerWithParameters< BatchToSpaceNdDescriptor >::m_Param
BatchToSpaceNdDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
Definition: LayerWithParameters.hpp:52
armnn::LayerWithParameters< BatchToSpaceNdDescriptor >::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::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:287
armnn::BatchToSpaceNdDescriptor
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
Definition: Descriptors.hpp:875
armnn::BatchToSpaceNdLayer::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: BatchToSpaceNdLayer.cpp:53
armnn::LayerType::BatchToSpaceNd
@ BatchToSpaceNd
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
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::BatchToSpaceNdDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:902
armnn::BatchToSpaceNdLayer::Clone
BatchToSpaceNdLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: BatchToSpaceNdLayer.cpp:32
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:483
BatchToSpaceNdLayer.hpp
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