xref: /aosp_15_r20/external/armnn/src/armnn/layers/ResizeLayer.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ResizeLayer.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 using namespace armnnUtils;
17 
18 namespace armnn
19 {
20 
ResizeLayer(const ResizeDescriptor & param,const char * name)21 ResizeLayer::ResizeLayer(const ResizeDescriptor& param, const char* name)
22     : LayerWithParameters(1, 1, LayerType::Resize, param, name)
23 {
24 }
25 
CreateWorkload(const IWorkloadFactory & factory) const26 std::unique_ptr<IWorkload> ResizeLayer::CreateWorkload(const IWorkloadFactory& factory) const
27 {
28     ResizeQueueDescriptor descriptor;
29     SetAdditionalInfo(descriptor);
30 
31     return factory.CreateWorkload(LayerType::Resize, descriptor, PrepInfoAndDesc(descriptor));
32 }
33 
Clone(Graph & graph) const34 ResizeLayer* ResizeLayer::Clone(Graph& graph) const
35 {
36     return CloneBase<ResizeLayer>(graph, m_Param, GetName());
37 }
38 
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const39 std::vector<TensorShape> ResizeLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
40 {
41     ARMNN_ASSERT(inputShapes.size() == 1);
42 
43     const TensorShape& inputShape = inputShapes[0];
44     const DataLayoutIndexed dimensionIndices = m_Param.m_DataLayout;
45 
46     unsigned int outWidth = m_Param.m_TargetWidth;
47     unsigned int outHeight = m_Param.m_TargetHeight;
48     unsigned int outChannels = inputShape[dimensionIndices.GetChannelsIndex()];
49     unsigned int outBatch = inputShape[0];
50 
51     TensorShape tensorShape = m_Param.m_DataLayout == armnn::DataLayout::NHWC ?
52         TensorShape( { outBatch, outHeight, outWidth, outChannels } ) :
53         TensorShape( { outBatch, outChannels, outHeight, outWidth });
54 
55     if (m_Param.m_HalfPixelCenters && m_Param.m_AlignCorners)
56     {
57         throw LayerValidationException("ResizeLayer: AlignCorners cannot be true when HalfPixelCenters is true");
58     }
59 
60     return std::vector<TensorShape>({ tensorShape });
61 }
62 
ValidateTensorShapesFromInputs()63 void ResizeLayer::ValidateTensorShapesFromInputs()
64 {
65     VerifyLayerConnections(1, CHECK_LOCATION());
66 
67     const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
68 
69     VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
70 
71     auto inferredShapes = InferOutputShapes({ GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape() });
72 
73     ARMNN_ASSERT(inferredShapes.size() == 1);
74 
75     ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "ResizeLayer");
76 }
77 
ExecuteStrategy(IStrategy & strategy) const78 void ResizeLayer::ExecuteStrategy(IStrategy& strategy) const
79 {
80     strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
81 }
82 
83 } // namespace armnn
84