ArmNN
 25.11
Loading...
Searching...
No Matches
FoldPadIntoLayer2d.hpp
Go to the documentation of this file.
1//
2// Copyright © 2021-2025 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "Optimization.hpp"
9
11
14
15namespace armnn
16{
17namespace optimizations
18{
19namespace pad_fold
20{
21inline float GetZeroElement(const TensorInfo& tensorInfo)
22{
23 return static_cast<float>(tensorInfo.IsQuantized() ? tensorInfo.GetQuantizationOffset() : 0);
24}
25
26inline float GetLowestElement(const TensorInfo& tensorInfo)
27{
28 constexpr float negativeInfinity = -std::numeric_limits<float>::infinity();
29 const float scale = tensorInfo.GetQuantizationScale();
30 const int32_t offset = tensorInfo.GetQuantizationOffset();
31
32 switch (tensorInfo.GetDataType())
33 {
35 return armnnUtils::SelectiveQuantize<armnn::Half>(negativeInfinity, scale, offset);
37 return armnnUtils::SelectiveQuantize<float>(negativeInfinity, scale, offset);
39 return armnnUtils::SelectiveQuantize<uint8_t>(negativeInfinity, scale, offset);
41 return armnnUtils::SelectiveQuantize<int16_t>(negativeInfinity, scale, offset);
43 // Fall-through
45 return armnnUtils::SelectiveQuantize<int8_t>(negativeInfinity, scale, offset);
47 return armnnUtils::SelectiveQuantize<armnn::BFloat16>(negativeInfinity, scale, offset);
48 default:
49 {
50 ARMNN_ASSERT_MSG(false, "Unsupported DataType");
51 return NAN;
52 }
53 }
54}
55
56inline bool IsNeutralElement(const Convolution2dDescriptor&, const TensorInfo& tensorInfo, const float tensorValue)
57{
58 return tensorValue == GetZeroElement(tensorInfo);
59}
60
62 const TensorInfo& tensorInfo,
63 const float tensorValue)
64{
65 return tensorValue == GetZeroElement(tensorInfo);
66}
67
68inline bool IsNeutralElement(
69 const Pooling2dDescriptor& descriptor, const TensorInfo& tensorInfo, const float tensorValue)
70{
71 return (descriptor.m_PoolType == PoolingAlgorithm::Max)
72 ? tensorValue <= GetLowestElement(tensorInfo)
73 : tensorValue == GetZeroElement(tensorInfo);
74}
75
76inline bool IsPooling2dPadded(const Pooling2dDescriptor& poolDescriptor)
77{
78 const auto poolingPadValues = std::make_tuple(poolDescriptor.m_PadLeft, poolDescriptor.m_PadRight,
79 poolDescriptor.m_PadTop, poolDescriptor.m_PadBottom);
80 if (poolingPadValues != std::make_tuple(0U, 0U, 0U, 0U))
81 {
82 return true;
83 }
84 return false;
85}
86
87template <typename Descriptor>
89 const PadDescriptor& padDescriptor, Descriptor& layerDescriptor, const TensorInfo& tensorInfo)
90{
91 armnnUtils::DataLayoutIndexed layout = armnnUtils::DataLayoutIndexed(layerDescriptor.m_DataLayout);
92 constexpr unsigned int batchIndex = 0;
93
94 constexpr auto noPad = std::make_pair(0U, 0U);
95
96 if ((!IsNeutralElement(layerDescriptor, tensorInfo, padDescriptor.m_PadValue)) ||
97 (padDescriptor.m_PadList[batchIndex] != noPad) || (padDescriptor.m_PadList[layout.GetChannelsIndex()] != noPad))
98 {
99 return false;
100 }
101
102 const auto& padList = padDescriptor.m_PadList;
103
104 // In Convolution2dDescriptor/Pooling2dDescriptor, padLeft and padRight are defined as paddings
105 // on width dimension whereas padTop and padBottom - paddings on height dimension, so updating
106 // these according to data layout
107 layerDescriptor.m_PadLeft += padList[layout.GetWidthIndex()].first;
108 layerDescriptor.m_PadRight += padList[layout.GetWidthIndex()].second;
109 layerDescriptor.m_PadTop += padList[layout.GetHeightIndex()].first;
110 layerDescriptor.m_PadBottom += padList[layout.GetHeightIndex()].second;
111
112 return true;
113}
114
115inline bool TryFoldPadIntoLayer2d(const PadDescriptor& padDescriptor,
116 Pooling2dDescriptor& poolDescriptor,
117 const TensorInfo& tensorInfo,
118 bool isBackendOptimization = false)
119{
120 // Cannot fold Average or L2 pooling if padding exists and the padding method is Exclude.
121 if (poolDescriptor.m_PoolType != PoolingAlgorithm::Max &&
122 IsPooling2dPadded(poolDescriptor) &&
123 poolDescriptor.m_PaddingMethod == PaddingMethod::Exclude)
124 {
125 return false;
126 }
127
128 // Cannot fold Average pooling if data type is quantized and layout is NHWC in Neon backend.
129 // Therefore, this specific case will become a backend specific optimization.
130 if (!isBackendOptimization &&
131 tensorInfo.IsQuantized() &&
132 poolDescriptor.m_PoolType == PoolingAlgorithm::Average &&
133 poolDescriptor.m_DataLayout == DataLayout::NHWC)
134 {
135 return false;
136 }
137
139
140 return TryFoldPadIntoLayer2d<Pooling2dDescriptor>(padDescriptor, poolDescriptor, tensorInfo);
141}
142
143template <typename Layer2dT>
144Layer2dT* FoldPadIntoLayer2dImpl(Graph& graph, InputSlot& connection)
145{
147 Layer2dT& layer2d = *PolymorphicDowncast<Layer2dT*>(&connection.GetOwningLayer());
148
149 const PadDescriptor& padDescriptor = padLayer.GetParameters();
150 auto newLayer2dDescriptor = layer2d.GetParameters();
151
152 if (!TryFoldPadIntoLayer2d(padDescriptor, newLayer2dDescriptor, padLayer.GetOutputSlot().GetTensorInfo()))
153 {
154 return nullptr;
155 }
156
157 // Workaround an issue in the compute library. The conv2d algorithm that the
158 // compute library is choosing is not handling the 1x1 filter case when
159 // the padding size >= filter size
160 if (layer2d.GetType() == armnn::LayerType::Convolution2d)
161 {
162 // Get filter width and height
163 armnnUtils::DataLayoutIndexed dataLayoutIndex(newLayer2dDescriptor.m_DataLayout);
164 const TensorShape& filterShape = layer2d.GetInputSlot(1).GetTensorInfo().GetShape();
165 unsigned int filterWidth = filterShape[dataLayoutIndex.GetWidthIndex()];
166 unsigned int filterHeight = filterShape[dataLayoutIndex.GetHeightIndex()];
167 // Calculate total padding and check conditions
168 auto horizontalPadding = newLayer2dDescriptor.m_PadLeft + newLayer2dDescriptor.m_PadRight;
169 auto verticalPadding = newLayer2dDescriptor.m_PadTop + newLayer2dDescriptor.m_PadBottom;
170 if ((filterWidth == 1) && (horizontalPadding >= filterWidth))
171 {
172 return nullptr;
173 }
174 else if ((filterHeight == 1) && (verticalPadding >= filterHeight))
175 {
176 return nullptr;
177 }
178 }
179
180 // Save original parent output slot of the pad layer
181 OutputSlot& parentSlot = *padLayer.GetInputSlot(0).GetConnectedOutputSlot();
182
183 // Insert new layer2d layer between the pad layer and its parent layer.
184 const std::string name = std::string("folded-") + padLayer.GetName() + "-into-" + layer2d.GetName();
185 auto& newLayer2d = *graph.InsertNewLayer<Layer2dT>(padLayer.GetInputSlot(0), newLayer2dDescriptor, name.c_str());
186
187 newLayer2d.GetOutputSlot().MoveAllConnections(parentSlot);
188 // Start at 1 to connect only weights and bias
189 for (unsigned int i = 1; i < layer2d.GetNumInputSlots(); ++i)
190 {
191 if (layer2d.GetInputSlot(i).GetConnectedOutputSlot() != nullptr)
192 {
193 Layer& tgtLayer = layer2d.GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer();
194 // Remove old connection and connect to new layer2d
195 tgtLayer.GetOutputSlot(0).Disconnect(layer2d.GetInputSlot(i));
196 tgtLayer.GetOutputSlot(0).Connect(newLayer2d.GetInputSlot(i));
197 }
198 }
199
200 // Moves connections in old layer2d layer output to new layer.
201 // Old layer2d layer will be removed as it's left unconnected.
202 // Pad layer will be removed if left unconnected.
203 layer2d.GetOutputSlot().MoveAllConnections(newLayer2d.GetOutputSlot());
204
205 // Copy the backend
206 newLayer2d.SetBackendId(layer2d.GetBackendId());
207
208 return &newLayer2d;
209}
210
212{
213public:
214 void Run(Graph& graph, InputSlot& connection) const
215 {
216 const auto newConv2dLayer = FoldPadIntoLayer2dImpl<Convolution2dLayer>(graph, connection);
217
218 if (newConv2dLayer != nullptr)
219 {
220 const auto conv2dLayer = PolymorphicDowncast<Convolution2dLayer*>(&connection.GetOwningLayer());
221 ARMNN_ASSERT_MSG(newConv2dLayer->GetInputSlot(1).GetConnection() != nullptr,
222 "FoldPadIntoConvolution2d: New convolution layer is missing connection to weights layer");
223
224 if (conv2dLayer->GetParameters().m_BiasEnabled)
225 {
226 ARMNN_ASSERT_MSG(newConv2dLayer->GetInputSlot(2).GetConnection() != nullptr,
227 "FoldPadIntoConvolution2d: New convolution layer is missing "
228 "connection to bias layer.");
229 }
230 }
231 }
232
233protected:
236};
237
239{
240public:
241 void Run(Graph& graph, InputSlot& connection) const
242 {
243 const auto newConv2dLayer = FoldPadIntoLayer2dImpl<DepthwiseConvolution2dLayer>(graph, connection);
244
245 if (newConv2dLayer != nullptr)
246 {
247 const auto conv2dLayer = PolymorphicDowncast<DepthwiseConvolution2dLayer*>(&connection.GetOwningLayer());
248 ARMNN_ASSERT_MSG(newConv2dLayer->GetInputSlot(1).GetConnection() != nullptr,
249 "FoldPadIntoDepthwiseConvolution2d: New convolution layer is missing "
250 "connection to weights layer");
251
252 if (conv2dLayer->GetParameters().m_BiasEnabled)
253 {
254 ARMNN_ASSERT_MSG(newConv2dLayer->GetInputSlot(2).GetConnection() != nullptr,
255 "FoldPadIntoConvolution2d: New convolution layer is missing "
256 "connection to bias layer.");
257 }
258 }
259 }
260protected:
263};
264
266{
267public:
268 void Run(Graph& graph, InputSlot& connection) const
269 {
271 }
272
273protected:
276};
277} // namespace pad_fold
278
287
288} // namespace optimizations
289} // namespace armnn
290
291
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition Assert.hpp:15
This layer represents a depthwise convolution 2d operation.
LayerT * InsertNewLayer(InputSlot &insertBefore, Args &&... args)
Inserts a new layer between the output slot currently connected to insertBefore and insertBefore itse...
Definition Graph.hpp:481
Layer & GetOwningLayer() const
Definition Layer.hpp:53
const OutputSlot * GetConnectedOutputSlot() const
Definition Layer.hpp:56
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition Layer.hpp:337
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition Layer.hpp:339
const char * GetName() const override
Returns the name of the layer.
Definition Layer.hpp:332
const Parameters & GetParameters() const override
If the layer has a descriptor return it.
Layer & GetOwningLayer() const
Definition Layer.hpp:132
void Disconnect(InputSlot &slot)
Definition Layer.cpp:131
const TensorInfo & GetTensorInfo() const override
Definition Layer.cpp:100
int Connect(InputSlot &destination)
Definition Layer.cpp:123
This layer represents a pad operation.
Definition PadLayer.hpp:15
float GetQuantizationScale() const
Definition Tensor.cpp:461
int32_t GetQuantizationOffset() const
Definition Tensor.cpp:482
bool IsQuantized() const
Definition Tensor.cpp:508
DataType GetDataType() const
Definition Tensor.hpp:200
void Run(Graph &graph, InputSlot &connection) const
void Run(Graph &graph, InputSlot &connection) const
Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout.
unsigned int GetHeightIndex() const
unsigned int GetChannelsIndex() const
float GetLowestElement(const TensorInfo &tensorInfo)
bool TryFoldPadIntoLayer2d(const PadDescriptor &padDescriptor, Descriptor &layerDescriptor, const TensorInfo &tensorInfo)
Layer2dT * FoldPadIntoLayer2dImpl(Graph &graph, InputSlot &connection)
bool IsPooling2dPadded(const Pooling2dDescriptor &poolDescriptor)
float GetZeroElement(const TensorInfo &tensorInfo)
bool IsNeutralElement(const Convolution2dDescriptor &, const TensorInfo &tensorInfo, const float tensorValue)
OptimizeForExclusiveConnection< PadLayer, DepthwiseConvolution2dLayer, pad_fold::FoldPadIntoDepthwiseConvolution2dImpl > FoldPadIntoDepthwiseConvolution2d
OptimizeForExclusiveConnection< PadLayer, Pooling2dLayer, pad_fold::FoldPadIntoPooling2dImpl > FoldPadIntoPooling2d
OptimizeForExclusiveConnection< PadLayer, Convolution2dLayer, pad_fold::FoldPadIntoConvolution2dImpl > FoldPadIntoConvolution2d
Copyright (c) 2021 ARM Limited and Contributors.
@ Exclude
The padding fields don't count and are ignored.
Definition Types.hpp:194
@ IgnoreValue
The padding fields count, but are ignored.
Definition Types.hpp:192
DestType PolymorphicDowncast(SourceType *value)
Polymorphic downcast for build in pointers only.
T SelectiveQuantize(float value, float scale, int32_t offset)
A Convolution2dDescriptor for the Convolution2dLayer.
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
A PadDescriptor for the PadLayer.
float m_PadValue
Optional value to use for padding, defaults to 0.
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding for input dimension.
A Pooling2dDescriptor for the Pooling2dLayer.
uint32_t m_PadRight
Padding right value in the width dimension.
PoolingAlgorithm m_PoolType
The pooling algorithm to use (Max. Average, L2).
uint32_t m_PadTop
Padding top value in the height dimension.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
PaddingMethod m_PaddingMethod
The padding method to be used. (Exclude, IgnoreValue).
uint32_t m_PadBottom
Padding bottom value in the height dimension.
uint32_t m_PadLeft
Padding left value in the width dimension.