2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
5 #include "Pooling2dLayer.hpp"
7 #include "LayerCloneBase.hpp"
9 #include <armnn/TypesUtils.hpp>
10 #include <backends/WorkloadData.hpp>
11 #include <backends/WorkloadFactory.hpp>
16 Pooling2dLayer::Pooling2dLayer(const Pooling2dDescriptor& param, const char* name)
17 : LayerWithParameters(1, 1, LayerType::Pooling2d, param, name)
21 std::unique_ptr<IWorkload> Pooling2dLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const
23 Pooling2dQueueDescriptor descriptor;
24 return factory.CreatePooling2d(descriptor, PrepInfoAndDesc(descriptor, graph));
27 Pooling2dLayer* Pooling2dLayer::Clone(Graph& graph) const
29 return CloneBase<Pooling2dLayer>(graph, m_Param, GetName());
32 void Pooling2dLayer::ValidateTensorShapesFromInputs()
34 ConditionalThrow<LayerValidationException>(GetInputSlot(0).GetConnection() != nullptr,
35 "Pooling2dLayer: InputSlot must be connected to an OutputSlot");
36 ConditionalThrow<LayerValidationException>(GetInputSlot(0).GetConnection()->IsTensorInfoSet(),
37 "Pooling2dLayer: TensorInfo must be set on connected InputSlot.");
39 IOutputSlot* input = GetInputSlot(0).GetConnection();
40 const TensorShape& inputShape = input->GetTensorInfo().GetShape();
42 // If we support multiple batch dimensions in the future, then this assert will need to change.
43 BOOST_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Pooling2dLayer will always have 4D input.");
46 unsigned int inWidth = inputShape[3];
47 unsigned int inHeight = inputShape[2];
48 unsigned int inChannels = inputShape[1];
49 unsigned int inBatchSize = inputShape[0];
51 bool isGlobalPooling = (m_Param.m_StrideX==0 && m_Param.m_StrideY==0);
52 unsigned int outWidth = 1;
53 unsigned int outHeight = 1;
56 BOOST_ASSERT_MSG(m_Param.m_StrideX!=0 && m_Param.m_StrideY!=0,
57 "Stride can only be zero when performing global pooling");
59 auto CalcSize = [](auto inSize, auto lowPad, auto highPad, auto poolSize, auto stride, auto padMethod,
60 auto outputShapeRounding)
62 unsigned int readSize = inSize + lowPad + highPad - poolSize;
63 float div = static_cast<float>(readSize) / static_cast<float>(stride);
65 unsigned int size = 0;
66 switch (outputShapeRounding)
68 case OutputShapeRounding::Ceiling:
69 size = static_cast<unsigned int>(ceil(div)) + 1;
71 case OutputShapeRounding ::Floor:
72 size = static_cast<unsigned int>(floor(div)) + 1;
75 BOOST_ASSERT_MSG(false, "Unsupported Output Shape Rounding");
78 // Make sure that border operations will start from inside the input and not the padded area
79 // This is what both Caffe and CL does...
80 if ((size - 1)*stride >= inSize + lowPad)
88 outWidth = CalcSize(inWidth, m_Param.m_PadLeft, m_Param.m_PadRight, m_Param.m_PoolWidth, m_Param.m_StrideX,
89 m_Param.m_PaddingMethod, m_Param.m_OutputShapeRounding);
90 outHeight= CalcSize(inHeight, m_Param.m_PadTop, m_Param.m_PadBottom, m_Param.m_PoolHeight, m_Param.m_StrideY,
91 m_Param.m_PaddingMethod, m_Param.m_OutputShapeRounding);
95 unsigned int outChannels = inChannels;
96 unsigned int outBatchSize = inBatchSize;
98 TensorShape shapeOut({outBatchSize, outChannels, outHeight, outWidth});
100 ConditionalThrowIfNotEqual<LayerValidationException>(
101 "Pooling2dLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
102 GetOutputSlot(0).GetTensorInfo().GetShape(),