ArmNN
 24.08
BatchToSpaceNdLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2018-2024 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  if (inferredShapes.size() != 1)
49  {
50  throw armnn::LayerValidationException("inferredShapes has "
51  + std::to_string(inferredShapes.size()) +
52  " elements - should only have 1.");
53  }
54 
55  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "BatchToSpaceNdLayer");
56 }
57 
58 std::vector<TensorShape> BatchToSpaceNdLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
59 {
60  const TensorShape& inputShape = inputShapes[0];
61  TensorShape outputShape(inputShape);
62 
63  unsigned int accumulatedBlockShape = std::accumulate(m_Param.m_BlockShape.begin(),
64  m_Param.m_BlockShape.end(),
65  1U,
66  std::multiplies<>());
67  outputShape[0] = (inputShape[0] / accumulatedBlockShape) < 1 ? 1 : (inputShape[0] / accumulatedBlockShape) ;
68 
69  // In a 4D tensor, there will be 2 spatialDimensions (H and W), and the for loop will run twice.
70  // In a 3D tensor, there will be 1 spatialDimensions, and the for loop will run once.
71  unsigned int firstSpatialDimension = m_Param.m_DataLayout == DataLayout::NCHW ? 2 : 1;
72  for (unsigned int i = 0; i < m_Param.m_BlockShape.size(); ++i)
73  {
74  unsigned int spatialDimension = firstSpatialDimension + i;
75  unsigned int cropSize = m_Param.m_Crops[i].first + m_Param.m_Crops[i].second;
76  unsigned int outputSize = inputShape[spatialDimension] * m_Param.m_BlockShape[i];
77  outputShape[spatialDimension] = outputSize - cropSize;
78  }
79 
80  return std::vector<TensorShape>({ outputShape });
81 }
82 
84 {
85  strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
86 }
87 
88 } // namespace armnn
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:100
WorkloadData.hpp
armnn::BatchToSpaceNdQueueDescriptor
Definition: WorkloadData.hpp:462
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:457
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:614
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:83
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::LayerValidationException
Definition: Exceptions.hpp:105
armnn::IWorkloadFactory
Definition: WorkloadFactory.hpp:22
armnnUtils
Definition: CompatibleTypes.hpp:10
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:526
armnn::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:303
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:58
armnn::LayerType::BatchToSpaceNd
@ BatchToSpaceNd
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
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:410
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:491
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