ArmNN
 25.11
Loading...
Searching...
No Matches
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
14using namespace armnnUtils;
15
16namespace armnn
17{
18
23
24std::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
58std::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
#define CHECK_LOCATION()
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
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,...
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of BatchToSpaceNdLayer.
BatchToSpaceNdLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the BatchToSpaceNd type.
BatchToSpaceNdLayer(const BatchToSpaceNdDescriptor &param, const char *name)
Constructor to create a BatchToSpaceNdLayer.
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
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.
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition Layer.cpp:614
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition Layer.cpp:410
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition Layer.cpp:526
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition Layer.hpp:339
LayerType * CloneBase(Graph &graph, Params &&... params) const
const char * GetName() const override
Returns the name of the layer.
Definition Layer.hpp:332
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition Layer.cpp:457
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition Layer.cpp:303
ShapeInferenceMethod m_ShapeInferenceMethod
Definition Layer.hpp:441
LayerWithParameters(unsigned int numInputSlots, unsigned int numOutputSlots, LayerType type, const BatchToSpaceNdDescriptor &param, const char *name)
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
const BatchToSpaceNdDescriptor & GetParameters() const override
const TensorInfo & GetTensorInfo() const override
Definition Layer.cpp:100
const TensorShape & GetShape() const
Definition Tensor.hpp:193
Copyright (c) 2021 ARM Limited and Contributors.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition Types.hpp:494
void BatchToSpaceNd(const TensorInfo &inputInfo, const TensorInfo &outputInfo, const BatchToSpaceNdDescriptor &params, Decoder< float > &inputData, Encoder< float > &outputData)
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.