diff options
author | Nina Drozd <nina.drozd@arm.com> | 2019-04-18 14:48:51 +0100 |
---|---|---|
committer | Nina Drozd <nina.drozd@arm.com> | 2019-04-19 16:49:43 +0100 |
commit | 861985ff2964720a0165e109c3fc568cb245bbe9 (patch) | |
tree | f5829f273076dec3fb1d67b071fe1baf0bb73fbe /src/armnn/optimizations | |
parent | 17660e68c91d48bfb3fc3c9540a1834f33e9e561 (diff) | |
download | armnn-861985ff2964720a0165e109c3fc568cb245bbe9.tar.gz |
IVGCVSW-2925: Combine Pad with Convolution2d in the Optimizer
* Added new optimization for folding pad layer into convolution2d layer following it
* Added new test in OptimizerTests.cpp
* Added new optimization into All optimizations
* Added call to new optimization in Optimize in Network.cpp
* Updated CMakeLists.txt
Signed-off-by: Nina Drozd <nina.drozd@arm.com>
Change-Id: I682e07c71bbd42c49c02dda30a848a9ab2b16e7e
Diffstat (limited to 'src/armnn/optimizations')
-rw-r--r-- | src/armnn/optimizations/All.hpp | 1 | ||||
-rw-r--r-- | src/armnn/optimizations/FoldPadIntoConvolution2d.hpp | 82 |
2 files changed, 83 insertions, 0 deletions
diff --git a/src/armnn/optimizations/All.hpp b/src/armnn/optimizations/All.hpp index 0a6684ee3b..68965fd23c 100644 --- a/src/armnn/optimizations/All.hpp +++ b/src/armnn/optimizations/All.hpp @@ -13,3 +13,4 @@ #include "OptimizeInverseConversions.hpp" #include "ConvertFp32NetworkToFp16.hpp" #include "AddDebug.hpp" +#include "FoldPadIntoConvolution2d.hpp" diff --git a/src/armnn/optimizations/FoldPadIntoConvolution2d.hpp b/src/armnn/optimizations/FoldPadIntoConvolution2d.hpp new file mode 100644 index 0000000000..f789ffae9a --- /dev/null +++ b/src/armnn/optimizations/FoldPadIntoConvolution2d.hpp @@ -0,0 +1,82 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "Optimization.hpp" + +namespace armnn +{ +namespace optimizations +{ + +class FoldPadIntoConvolution2dImpl +{ +public: + + void Run(Graph& graph, InputSlot& connection) const + { + Layer& base = connection.GetConnectedOutputSlot()->GetOwningLayer(); + Layer& child = connection.GetOwningLayer(); + + BOOST_ASSERT(base.GetType() == LayerType::Pad); + BOOST_ASSERT(child.GetType() == LayerType::Convolution2d); + + PadLayer* padLayer = boost::polymorphic_downcast<PadLayer*>(&base); + Convolution2dLayer* convolution2dLayer = boost::polymorphic_downcast<Convolution2dLayer*>(&child); + + OutputSlot* parentOut = base.GetInputSlot(0).GetConnectedOutputSlot(); + const TensorInfo& outInfo = child.GetOutputHandler().GetTensorInfo(); + + const std::string name = std::string("folded-") + base.GetName() + std::string("-into-") + child.GetName(); + Convolution2dDescriptor descriptor = convolution2dLayer->GetParameters(); + + auto padList = padLayer->GetParameters().m_PadList; + + armnn::DataLayout dataLayout = descriptor.m_DataLayout; + + // In Convolution2dDescriptor, padLeft and padRight are defined as paddings on width dimension + // whereas padTop and padBottom - paddings on height dimension, so setting these according to data layout + if(dataLayout == armnn::DataLayout::NHWC) + { + descriptor.m_PadLeft = padList[2].first; + descriptor.m_PadRight = padList[2].second; + descriptor.m_PadTop = padList[1].first; + descriptor.m_PadBottom = padList[1].second; + } + else + { + descriptor.m_PadLeft = padList[3].first; + descriptor.m_PadRight = padList[3].second; + descriptor.m_PadTop = padList[2].first; + descriptor.m_PadBottom = padList[2].second; + } + + auto& newConv2dLayer = *graph.InsertNewLayer<Convolution2dLayer>(base.GetInputSlot(0), + descriptor, + name.c_str()); + newConv2dLayer.GetOutputHandler().SetTensorInfo(outInfo); + + // Reconnects with original parent. + newConv2dLayer.GetOutputSlot().MoveAllConnections(*parentOut); + // Parent is now the new convolution2d layer. + parentOut = &newConv2dLayer.GetOutputSlot(); + + // Moves connections in child output to parent layer. + // Child layer will be removed as it's left unconnected. + // Base layer will be removed if left unconnected. + child.GetOutputSlot().MoveAllConnections(*parentOut); + } +protected: + FoldPadIntoConvolution2dImpl() = default; + ~FoldPadIntoConvolution2dImpl() = default; +}; + +using FoldPadIntoConvolution2d = OptimizeForConnection<PadLayer, Convolution2dLayer, FoldPadIntoConvolution2dImpl>; + +} // namespace optimizations +} // namespace armnn + + |