From 5fc0fd6661f9647092deb052d052973a237bd52d Mon Sep 17 00:00:00 2001 From: Matthew Sloyan Date: Mon, 3 May 2021 12:22:03 +0100 Subject: MLCE-418 Reduce layer does not support multiple axes * Added backend specific optimization to chain new reduces layers for each axis to simulate behaviour of a layer with multiple axes. * Added function to calculate reduced output shape. * Added unit tests. * Includes rework to fix IVGCVSW-5987. Signed-off-by: Matthew Sloyan Change-Id: I154b3698b5e6756b05b2a0b5a3f0896184efce72 --- src/backends/cl/ClBackend.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/backends/cl/ClBackend.cpp') diff --git a/src/backends/cl/ClBackend.cpp b/src/backends/cl/ClBackend.cpp index 35770d9219..a9ab237325 100644 --- a/src/backends/cl/ClBackend.cpp +++ b/src/backends/cl/ClBackend.cpp @@ -30,6 +30,7 @@ #include "workloads/ClDivisionWorkload.hpp" #include "workloads/ClFullyConnectedWorkload.hpp" #include "workloads/ClMultiplicationWorkload.hpp" +#include "workloads/ClReduceWorkload.hpp" #include "workloads/ClSubtractionWorkload.hpp" #include @@ -220,6 +221,7 @@ OptimizationViews ClBackend::OptimizeSubgraphView(const SubgraphView& subgraph, --it; Layer& base = **it; + // Fuse activation into previous layer if supported by backend if ((base.GetType() == LayerType::DepthwiseConvolution2d || base.GetType() == LayerType::Convolution2d || base.GetType() == LayerType::BatchNormalization || base.GetType() == LayerType::FullyConnected || base.GetType() == LayerType::Addition || base.GetType() == LayerType::Multiplication @@ -451,6 +453,25 @@ OptimizationViews ClBackend::OptimizeSubgraphView(const SubgraphView& subgraph, } } } + + // Separate reduce layer with multiple axes into multiple reduce layers with 1 axis. + if (base.GetType() == LayerType::Reduce) + { + ReduceLayer* baseLayer = PolymorphicDowncast(&base); + ReduceDescriptor reduceDescriptor = baseLayer->GetParameters(); + + if (!reduceDescriptor.m_vAxis.empty() && reduceDescriptor.m_vAxis.size() > 1) + { + // Add new layers to the graph and connect them. + std::vector layers = ChainReduceLayers(optimizationViews, + baseLayer, + reduceDescriptor); + + // Replace existing baselayer with new subgraph. + ReplaceLayers(optimizationViews, baseLayer, layers); + untouched.erase(baseLayer->GetGuid()); + } + } } if (optimizationViews.GetSubstitutions().empty()) -- cgit v1.2.1