17 namespace optimizations
28 constexpr
float negativeInfinity = -std::numeric_limits<float>::infinity();
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);
45 return armnnUtils::SelectiveQuantize<int8_t>(negativeInfinity, scale, offset);
47 return armnnUtils::SelectiveQuantize<armnn::BFloat16>(negativeInfinity, scale, offset);
63 const float tensorValue)
78 const auto poolingPadValues = std::make_tuple(poolDescriptor.
m_PadLeft, poolDescriptor.
m_PadRight,
80 if (poolingPadValues != std::make_tuple(0U, 0U, 0U, 0U))
87 template <
typename Descriptor>
92 constexpr
unsigned int batchIndex = 0;
94 constexpr
auto noPad = std::make_pair(0U, 0U);
102 const auto& padList = padDescriptor.
m_PadList;
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;
118 bool isBackendOptimization =
false)
130 if (!isBackendOptimization &&
140 return TryFoldPadIntoLayer2d<Pooling2dDescriptor>(padDescriptor, poolDescriptor, tensorInfo);
143 template <
typename Layer2dT>
147 Layer2dT& layer2d = *PolymorphicDowncast<Layer2dT*>(&connection.
GetOwningLayer());
150 auto newLayer2dDescriptor = layer2d.GetParameters();
164 const TensorShape& filterShape = layer2d.GetInputSlot(1).GetTensorInfo().GetShape();
165 unsigned int filterWidth = filterShape[dataLayoutIndex.
GetWidthIndex()];
166 unsigned int filterHeight = filterShape[dataLayoutIndex.
GetHeightIndex()];
168 auto horizontalPadding = newLayer2dDescriptor.m_PadLeft + newLayer2dDescriptor.m_PadRight;
169 auto verticalPadding = newLayer2dDescriptor.m_PadTop + newLayer2dDescriptor.m_PadBottom;
170 if ((filterWidth == 1) && (horizontalPadding >= filterWidth))
174 else if ((filterHeight == 1) && (verticalPadding >= filterHeight))
184 const std::string name = std::string(
"folded-") + padLayer.
GetName() +
"-into-" + layer2d.GetName();
187 newLayer2d.GetOutputSlot().MoveAllConnections(parentSlot);
189 for (
unsigned int i = 1; i < layer2d.GetNumInputSlots(); ++i)
191 if (layer2d.GetInputSlot(i).GetConnectedOutputSlot() !=
nullptr)
203 layer2d.GetOutputSlot().MoveAllConnections(newLayer2d.GetOutputSlot());
213 const auto newConv2dLayer = FoldPadIntoLayer2dImpl<Convolution2dLayer>(graph, connection);
215 if (newConv2dLayer !=
nullptr)
217 const auto conv2dLayer = PolymorphicDowncast<Convolution2dLayer*>(&connection.
GetOwningLayer());
218 ARMNN_ASSERT_MSG(newConv2dLayer->GetInputSlot(1).GetConnection() !=
nullptr,
219 "FoldPadIntoConvolution2d: New convolution layer is missing connection to weights layer");
221 if (conv2dLayer->GetParameters().m_BiasEnabled)
223 ARMNN_ASSERT_MSG(newConv2dLayer->GetInputSlot(2).GetConnection() !=
nullptr,
224 "FoldPadIntoConvolution2d: New convolution layer is missing "
225 "connection to bias layer.");
240 const auto newConv2dLayer = FoldPadIntoLayer2dImpl<DepthwiseConvolution2dLayer>(graph, connection);
242 if (newConv2dLayer !=
nullptr)
244 const auto conv2dLayer = PolymorphicDowncast<DepthwiseConvolution2dLayer*>(&connection.
GetOwningLayer());
245 ARMNN_ASSERT_MSG(newConv2dLayer->GetInputSlot(1).GetConnection() !=
nullptr,
246 "FoldPadIntoDepthwiseConvolution2d: New convolution layer is missing "
247 "connection to weights layer");
249 if (conv2dLayer->GetParameters().m_BiasEnabled)
251 ARMNN_ASSERT_MSG(newConv2dLayer->GetInputSlot(2).GetConnection() !=
nullptr,
252 "FoldPadIntoConvolution2d: New convolution layer is missing "
253 "connection to bias layer.");
267 FoldPadIntoLayer2dImpl<Pooling2dLayer>(graph, connection);
#define ARMNN_ASSERT_MSG(COND, MSG)
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...
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
const char * GetName() const override
Returns the name of the layer.
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
const Parameters & GetParameters() const override
If the layer has a descriptor return it.
Layer & GetOwningLayer() const
void Disconnect(InputSlot &slot)
const TensorInfo & GetTensorInfo() const override
int Connect(InputSlot &destination)
This layer represents a pad operation.
float GetQuantizationScale() const
int32_t GetQuantizationOffset() const
DataType GetDataType() const
FoldPadIntoConvolution2dImpl()=default
void Run(Graph &graph, InputSlot &connection) const
~FoldPadIntoConvolution2dImpl()=default
void Run(Graph &graph, InputSlot &connection) const
FoldPadIntoDepthwiseConvolution2dImpl()=default
~FoldPadIntoDepthwiseConvolution2dImpl()=default
void Run(Graph &graph, InputSlot &connection) const
~FoldPadIntoPooling2dImpl()=default
FoldPadIntoPooling2dImpl()=default
Provides access to the appropriate indexes for Channels, Height and Width based on DataLayout.
unsigned int GetWidthIndex() const
unsigned int GetHeightIndex() const
unsigned int GetChannelsIndex() const
Layer2dT * FoldPadIntoLayer2dImpl(Graph &graph, InputSlot &connection)
float GetLowestElement(const TensorInfo &tensorInfo)
bool TryFoldPadIntoLayer2d(const PadDescriptor &padDescriptor, Descriptor &layerDescriptor, const TensorInfo &tensorInfo)
float GetZeroElement(const TensorInfo &tensorInfo)
bool IsPooling2dPadded(const Pooling2dDescriptor &poolDescriptor)
bool IsNeutralElement(const Convolution2dDescriptor &, const TensorInfo &tensorInfo, const float tensorValue)
Copyright (c) 2021 ARM Limited and Contributors.
@ Exclude
The padding fields don't count and are ignored.
@ IgnoreValue
The padding fields count, but are ignored.
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.