ArmNN
 20.02
FoldPadIntoConvolution2d.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "Optimization.hpp"
9 
10 namespace armnn
11 {
12 namespace optimizations
13 {
14 
16 {
17 public:
18 
19  void Run(Graph& graph, InputSlot& connection) const
20  {
21  Layer& base = connection.GetConnectedOutputSlot()->GetOwningLayer();
22  Layer& child = connection.GetOwningLayer();
23 
24  BOOST_ASSERT(base.GetType() == LayerType::Pad);
25  BOOST_ASSERT(child.GetType() == LayerType::Convolution2d);
26 
27  PadLayer* padLayer = boost::polymorphic_downcast<PadLayer*>(&base);
28  Convolution2dLayer* convolution2dLayer = boost::polymorphic_downcast<Convolution2dLayer*>(&child);
29 
30  OutputSlot* parentOut = base.GetInputSlot(0).GetConnectedOutputSlot();
31  const TensorInfo& outInfo = child.GetOutputHandler().GetTensorInfo();
32 
33  const std::string name = std::string("folded-") + base.GetName() + std::string("-into-") + child.GetName();
34  Convolution2dDescriptor descriptor = convolution2dLayer->GetParameters();
35 
36  auto padList = padLayer->GetParameters().m_PadList;
37 
38  armnn::DataLayout dataLayout = descriptor.m_DataLayout;
39 
40  // In Convolution2dDescriptor, padLeft and padRight are defined as paddings on width dimension
41  // whereas padTop and padBottom - paddings on height dimension, so setting these according to data layout
42  if(dataLayout == armnn::DataLayout::NHWC)
43  {
44  descriptor.m_PadLeft = padList[2].first;
45  descriptor.m_PadRight = padList[2].second;
46  descriptor.m_PadTop = padList[1].first;
47  descriptor.m_PadBottom = padList[1].second;
48  }
49  else
50  {
51  descriptor.m_PadLeft = padList[3].first;
52  descriptor.m_PadRight = padList[3].second;
53  descriptor.m_PadTop = padList[2].first;
54  descriptor.m_PadBottom = padList[2].second;
55  }
56 
57  auto& newConv2dLayer = *graph.InsertNewLayer<Convolution2dLayer>(base.GetInputSlot(0),
58  descriptor,
59  name.c_str());
60  newConv2dLayer.GetOutputHandler().SetTensorInfo(outInfo);
61 
62  // Copy weights and bias to the new convolution layer
63  BOOST_ASSERT_MSG(convolution2dLayer->m_Weight != nullptr,
64  "FoldPadIntoConvolution2d: Weights data should not be null.");
65  newConv2dLayer.m_Weight = std::move(convolution2dLayer->m_Weight);
66  if (descriptor.m_BiasEnabled)
67  {
68  BOOST_ASSERT_MSG(convolution2dLayer->m_Bias != nullptr,
69  "FoldPadIntoConvolution2d: Bias data should not be null if bias is enabled.");
70  newConv2dLayer.m_Bias = std::move(convolution2dLayer->m_Bias);
71  }
72 
73  // Reconnects with original parent.
74  newConv2dLayer.GetOutputSlot().MoveAllConnections(*parentOut);
75  // Parent is now the new convolution2d layer.
76  parentOut = &newConv2dLayer.GetOutputSlot();
77 
78  // Moves connections in child output to parent layer.
79  // Child layer will be removed as it's left unconnected.
80  // Base layer will be removed if left unconnected.
81  child.GetOutputSlot().MoveAllConnections(*parentOut);
82  }
83 protected:
84  FoldPadIntoConvolution2dImpl() = default;
86 };
87 
89 
90 } // namespace optimizations
91 } // namespace armnn
92 
93 
uint32_t m_PadBottom
Padding bottom value in the height dimension.
bool m_BiasEnabled
Enable/disable bias.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
DataLayout
Definition: Types.hpp:49
A Convolution2dDescriptor for the Convolution2dLayer.
Layer & GetOwningLayer() const
Definition: Layer.hpp:115
uint32_t m_PadRight
Padding right value in the width dimension.
Copyright (c) 2020 ARM Limited.
This layer represents a pad operation.
Definition: PadLayer.hpp:14
uint32_t m_PadTop
Padding top value in the height dimension.
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:310
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:55
Layer & GetOwningLayer() const
Definition: Layer.hpp:52
void SetTensorInfo(const TensorInfo &tensorInfo)
Sets the TensorInfo used by this output handler.
const OutputHandler & GetOutputHandler(unsigned int i=0) const
Definition: Layer.hpp:221
void Run(Graph &graph, InputSlot &connection) const
LayerType GetType() const
Definition: Layer.hpp:259
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:312
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:305
This layer represents a 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:409
void MoveAllConnections(OutputSlot &destination)
Moves all connections to another OutputSlot.
Definition: Layer.cpp:112
uint32_t m_PadLeft
Padding left value in the width dimension.
const TensorInfo & GetTensorInfo() const
Gets the matching TensorInfo for the output.