xref: /aosp_15_r20/external/armnn/src/armnn/layers/Pooling3dLayer.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1*89c4ff92SAndroid Build Coastguard Worker //
2*89c4ff92SAndroid Build Coastguard Worker // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3*89c4ff92SAndroid Build Coastguard Worker // SPDX-License-Identifier: MIT
4*89c4ff92SAndroid Build Coastguard Worker //
5*89c4ff92SAndroid Build Coastguard Worker 
6*89c4ff92SAndroid Build Coastguard Worker #include "Pooling3dLayer.hpp"
7*89c4ff92SAndroid Build Coastguard Worker 
8*89c4ff92SAndroid Build Coastguard Worker #include "LayerCloneBase.hpp"
9*89c4ff92SAndroid Build Coastguard Worker 
10*89c4ff92SAndroid Build Coastguard Worker #include <armnn/TypesUtils.hpp>
11*89c4ff92SAndroid Build Coastguard Worker 
12*89c4ff92SAndroid Build Coastguard Worker #include <armnnUtils/DataLayoutIndexed.hpp>
13*89c4ff92SAndroid Build Coastguard Worker 
14*89c4ff92SAndroid Build Coastguard Worker #include <armnn/backends/WorkloadData.hpp>
15*89c4ff92SAndroid Build Coastguard Worker #include <armnn/backends/WorkloadFactory.hpp>
16*89c4ff92SAndroid Build Coastguard Worker 
17*89c4ff92SAndroid Build Coastguard Worker using namespace armnnUtils;
18*89c4ff92SAndroid Build Coastguard Worker 
19*89c4ff92SAndroid Build Coastguard Worker namespace armnn
20*89c4ff92SAndroid Build Coastguard Worker {
21*89c4ff92SAndroid Build Coastguard Worker 
Pooling3dLayer(const Pooling3dDescriptor & param,const char * name)22*89c4ff92SAndroid Build Coastguard Worker Pooling3dLayer::Pooling3dLayer(const Pooling3dDescriptor& param, const char* name)
23*89c4ff92SAndroid Build Coastguard Worker     : LayerWithParameters(1, 1, LayerType::Pooling3d, param, name)
24*89c4ff92SAndroid Build Coastguard Worker {
25*89c4ff92SAndroid Build Coastguard Worker }
26*89c4ff92SAndroid Build Coastguard Worker 
CreateWorkload(const IWorkloadFactory & factory) const27*89c4ff92SAndroid Build Coastguard Worker std::unique_ptr<IWorkload> Pooling3dLayer::CreateWorkload(const IWorkloadFactory& factory) const
28*89c4ff92SAndroid Build Coastguard Worker {
29*89c4ff92SAndroid Build Coastguard Worker     Pooling3dQueueDescriptor descriptor;
30*89c4ff92SAndroid Build Coastguard Worker     SetAdditionalInfo(descriptor);
31*89c4ff92SAndroid Build Coastguard Worker 
32*89c4ff92SAndroid Build Coastguard Worker     return factory.CreateWorkload(LayerType::Pooling3d, descriptor, PrepInfoAndDesc(descriptor));
33*89c4ff92SAndroid Build Coastguard Worker }
34*89c4ff92SAndroid Build Coastguard Worker 
Clone(Graph & graph) const35*89c4ff92SAndroid Build Coastguard Worker Pooling3dLayer* Pooling3dLayer::Clone(Graph& graph) const
36*89c4ff92SAndroid Build Coastguard Worker {
37*89c4ff92SAndroid Build Coastguard Worker     return CloneBase<Pooling3dLayer>(graph, m_Param, GetName());
38*89c4ff92SAndroid Build Coastguard Worker }
39*89c4ff92SAndroid Build Coastguard Worker 
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const40*89c4ff92SAndroid Build Coastguard Worker std::vector<TensorShape> Pooling3dLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
41*89c4ff92SAndroid Build Coastguard Worker {
42*89c4ff92SAndroid Build Coastguard Worker     ARMNN_ASSERT(inputShapes.size() == 1);
43*89c4ff92SAndroid Build Coastguard Worker     const TensorShape& inputShape = inputShapes[0];
44*89c4ff92SAndroid Build Coastguard Worker     const DataLayoutIndexed dimensionIndices = m_Param.m_DataLayout;
45*89c4ff92SAndroid Build Coastguard Worker 
46*89c4ff92SAndroid Build Coastguard Worker     // If we support multiple batch dimensions in the future, then this assert will need to change.
47*89c4ff92SAndroid Build Coastguard Worker     ARMNN_ASSERT_MSG(inputShape.GetNumDimensions() == 5, "Pooling3dLayer will always have 5D input.");
48*89c4ff92SAndroid Build Coastguard Worker 
49*89c4ff92SAndroid Build Coastguard Worker     unsigned int inWidth = inputShape[dimensionIndices.GetWidthIndex()];
50*89c4ff92SAndroid Build Coastguard Worker     unsigned int inHeight = inputShape[dimensionIndices.GetHeightIndex()];
51*89c4ff92SAndroid Build Coastguard Worker     unsigned int inDepth = inputShape[dimensionIndices.GetDepthIndex()];
52*89c4ff92SAndroid Build Coastguard Worker     unsigned int inChannels = inputShape[dimensionIndices.GetChannelsIndex()];
53*89c4ff92SAndroid Build Coastguard Worker     unsigned int inBatchSize = inputShape[0];
54*89c4ff92SAndroid Build Coastguard Worker 
55*89c4ff92SAndroid Build Coastguard Worker     bool isGlobalPooling = (m_Param.m_StrideX==0 && m_Param.m_StrideY==0 && m_Param.m_StrideZ==0);
56*89c4ff92SAndroid Build Coastguard Worker     unsigned int outWidth = 1;
57*89c4ff92SAndroid Build Coastguard Worker     unsigned int outHeight = 1;
58*89c4ff92SAndroid Build Coastguard Worker     unsigned int outDepth = 1;
59*89c4ff92SAndroid Build Coastguard Worker     if (!isGlobalPooling)
60*89c4ff92SAndroid Build Coastguard Worker     {
61*89c4ff92SAndroid Build Coastguard Worker         ARMNN_ASSERT_MSG(m_Param.m_StrideX!=0 && m_Param.m_StrideY!=0 && m_Param.m_StrideZ!=0,
62*89c4ff92SAndroid Build Coastguard Worker                          "Stride can only be zero when performing global pooling");
63*89c4ff92SAndroid Build Coastguard Worker 
64*89c4ff92SAndroid Build Coastguard Worker         auto CalcSize = [](auto inSize, auto lowPad, auto highPad, auto poolSize, auto stride, auto outputShapeRounding)
65*89c4ff92SAndroid Build Coastguard Worker             {
66*89c4ff92SAndroid Build Coastguard Worker                 unsigned int readSize = inSize + lowPad + highPad - poolSize;
67*89c4ff92SAndroid Build Coastguard Worker                 float div = static_cast<float>(readSize) / static_cast<float>(stride);
68*89c4ff92SAndroid Build Coastguard Worker 
69*89c4ff92SAndroid Build Coastguard Worker                 unsigned int size = 0;
70*89c4ff92SAndroid Build Coastguard Worker                 switch (outputShapeRounding)
71*89c4ff92SAndroid Build Coastguard Worker                 {
72*89c4ff92SAndroid Build Coastguard Worker                     case OutputShapeRounding::Ceiling:
73*89c4ff92SAndroid Build Coastguard Worker                         size = static_cast<unsigned int>(ceil(div)) + 1;
74*89c4ff92SAndroid Build Coastguard Worker                         break;
75*89c4ff92SAndroid Build Coastguard Worker                     case OutputShapeRounding ::Floor:
76*89c4ff92SAndroid Build Coastguard Worker                         size = static_cast<unsigned int>(floor(div)) + 1;
77*89c4ff92SAndroid Build Coastguard Worker                         break;
78*89c4ff92SAndroid Build Coastguard Worker                     default:
79*89c4ff92SAndroid Build Coastguard Worker                         ARMNN_ASSERT_MSG(false, "Unsupported Output Shape Rounding");
80*89c4ff92SAndroid Build Coastguard Worker                 }
81*89c4ff92SAndroid Build Coastguard Worker 
82*89c4ff92SAndroid Build Coastguard Worker                 // Makes sure that border operations will start from inside the input and not the padded area.
83*89c4ff92SAndroid Build Coastguard Worker                 // This is what CL does...
84*89c4ff92SAndroid Build Coastguard Worker                 if ((size - 1)*stride >= inSize + lowPad)
85*89c4ff92SAndroid Build Coastguard Worker                 {
86*89c4ff92SAndroid Build Coastguard Worker                     --size;
87*89c4ff92SAndroid Build Coastguard Worker                 }
88*89c4ff92SAndroid Build Coastguard Worker 
89*89c4ff92SAndroid Build Coastguard Worker                 return size;
90*89c4ff92SAndroid Build Coastguard Worker             };
91*89c4ff92SAndroid Build Coastguard Worker 
92*89c4ff92SAndroid Build Coastguard Worker         outWidth = CalcSize(inWidth, m_Param.m_PadLeft, m_Param.m_PadRight, m_Param.m_PoolWidth, m_Param.m_StrideX,
93*89c4ff92SAndroid Build Coastguard Worker                             m_Param.m_OutputShapeRounding);
94*89c4ff92SAndroid Build Coastguard Worker         outHeight = CalcSize(inHeight, m_Param.m_PadTop, m_Param.m_PadBottom, m_Param.m_PoolHeight, m_Param.m_StrideY,
95*89c4ff92SAndroid Build Coastguard Worker                             m_Param.m_OutputShapeRounding);
96*89c4ff92SAndroid Build Coastguard Worker         outDepth = CalcSize(inDepth, m_Param.m_PadFront, m_Param.m_PadBack, m_Param.m_PoolDepth, m_Param.m_StrideZ,
97*89c4ff92SAndroid Build Coastguard Worker                             m_Param.m_OutputShapeRounding);
98*89c4ff92SAndroid Build Coastguard Worker     }
99*89c4ff92SAndroid Build Coastguard Worker     unsigned int outChannels = inChannels;
100*89c4ff92SAndroid Build Coastguard Worker     unsigned int outBatchSize = inBatchSize;
101*89c4ff92SAndroid Build Coastguard Worker 
102*89c4ff92SAndroid Build Coastguard Worker     TensorShape tensorShape = m_Param.m_DataLayout == armnn::DataLayout::NDHWC ?
103*89c4ff92SAndroid Build Coastguard Worker         TensorShape( { outBatchSize, outDepth, outHeight, outWidth, outChannels } ) :
104*89c4ff92SAndroid Build Coastguard Worker         TensorShape( { outBatchSize, outChannels, outDepth, outHeight, outWidth });
105*89c4ff92SAndroid Build Coastguard Worker 
106*89c4ff92SAndroid Build Coastguard Worker     return std::vector<TensorShape>({ tensorShape });
107*89c4ff92SAndroid Build Coastguard Worker }
108*89c4ff92SAndroid Build Coastguard Worker 
ValidateTensorShapesFromInputs()109*89c4ff92SAndroid Build Coastguard Worker void Pooling3dLayer::ValidateTensorShapesFromInputs()
110*89c4ff92SAndroid Build Coastguard Worker {
111*89c4ff92SAndroid Build Coastguard Worker     VerifyLayerConnections(1, CHECK_LOCATION());
112*89c4ff92SAndroid Build Coastguard Worker 
113*89c4ff92SAndroid Build Coastguard Worker     const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
114*89c4ff92SAndroid Build Coastguard Worker 
115*89c4ff92SAndroid Build Coastguard Worker     VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
116*89c4ff92SAndroid Build Coastguard Worker 
117*89c4ff92SAndroid Build Coastguard Worker     auto inferredShapes = InferOutputShapes({ GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape() });
118*89c4ff92SAndroid Build Coastguard Worker 
119*89c4ff92SAndroid Build Coastguard Worker     ARMNN_ASSERT(inferredShapes.size() == 1);
120*89c4ff92SAndroid Build Coastguard Worker 
121*89c4ff92SAndroid Build Coastguard Worker     ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "Pooling3dLayer");
122*89c4ff92SAndroid Build Coastguard Worker }
123*89c4ff92SAndroid Build Coastguard Worker 
ExecuteStrategy(IStrategy & strategy) const124*89c4ff92SAndroid Build Coastguard Worker void Pooling3dLayer::ExecuteStrategy(IStrategy& strategy) const
125*89c4ff92SAndroid Build Coastguard Worker {
126*89c4ff92SAndroid Build Coastguard Worker     strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
127*89c4ff92SAndroid Build Coastguard Worker }
128*89c4ff92SAndroid Build Coastguard Worker 
129*89c4ff92SAndroid Build Coastguard Worker } // namespace armnn
130