xref: /aosp_15_r20/external/armnn/src/armnn/layers/DepthToSpaceLayer.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "DepthToSpaceLayer.hpp"
7 #include "LayerCloneBase.hpp"
8 
9 #include <armnn/TypesUtils.hpp>
10 
11 #include <armnnUtils/DataLayoutIndexed.hpp>
12 
13 #include <armnn/backends/WorkloadData.hpp>
14 #include <armnn/backends/WorkloadFactory.hpp>
15 
16 #include <numeric>
17 
18 namespace armnn
19 {
20 
DepthToSpaceLayer(const DepthToSpaceDescriptor & param,const char * name)21 DepthToSpaceLayer::DepthToSpaceLayer(const DepthToSpaceDescriptor& param, const char* name)
22     : LayerWithParameters(1, 1, LayerType::DepthToSpace, param, name)
23 {}
24 
CreateWorkload(const IWorkloadFactory & factory) const25 std::unique_ptr<IWorkload> DepthToSpaceLayer::CreateWorkload(const IWorkloadFactory& factory) const
26 {
27     DepthToSpaceQueueDescriptor descriptor;
28     descriptor.m_Parameters.m_BlockSize  = m_Param.m_BlockSize;
29     descriptor.m_Parameters.m_DataLayout = m_Param.m_DataLayout;
30 
31     SetAdditionalInfo(descriptor);
32 
33     return factory.CreateWorkload(LayerType::DepthToSpace, descriptor, PrepInfoAndDesc(descriptor));
34 }
35 
Clone(Graph & graph) const36 DepthToSpaceLayer* DepthToSpaceLayer::Clone(Graph& graph) const
37 {
38     return CloneBase<DepthToSpaceLayer>(graph, m_Param, GetName());
39 }
40 
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const41 std::vector<TensorShape> DepthToSpaceLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
42 {
43     ARMNN_ASSERT(inputShapes.size() == 1);
44 
45     TensorShape inputShape = inputShapes[0];
46     TensorShape outputShape(inputShape);
47 
48     armnnUtils::DataLayoutIndexed dimensionIndices(m_Param.m_DataLayout);
49 
50     unsigned int hIndex = dimensionIndices.GetHeightIndex();
51     unsigned int wIndex = dimensionIndices.GetWidthIndex();
52     unsigned int cIndex = dimensionIndices.GetChannelsIndex();
53 
54     outputShape[hIndex] = inputShape[hIndex] * m_Param.m_BlockSize;
55     outputShape[wIndex] = inputShape[wIndex] * m_Param.m_BlockSize;
56 
57     outputShape[cIndex] = inputShape[cIndex] / (m_Param.m_BlockSize * m_Param.m_BlockSize);
58 
59     return std::vector<TensorShape>({ outputShape });
60 }
61 
ValidateTensorShapesFromInputs()62 void DepthToSpaceLayer::ValidateTensorShapesFromInputs()
63 {
64     VerifyLayerConnections(1, CHECK_LOCATION());
65 
66     const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
67 
68     VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
69 
70     std::vector<TensorShape> inferredShapes = InferOutputShapes({
71         GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape() });
72 
73     ARMNN_ASSERT(inferredShapes.size() == 1);
74 
75     ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "DepthToSpaceLayer");
76 }
77 
ExecuteStrategy(IStrategy & strategy) const78 void DepthToSpaceLayer::ExecuteStrategy(IStrategy& strategy) const
79 {
80     strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
81 }
82 
83 } // namespace armnn
84