From 8492433df5eac916c4b7e0695bce63e222a564d4 Mon Sep 17 00:00:00 2001 From: Matteo Martincigh Date: Thu, 9 May 2019 12:46:16 +0100 Subject: IVGCVSW-3030 Refactor code and fix OptimizeSubgraphView * Refactored the Optimizer code for readibility, in view of upcoming changes * Rename one of the ISubgraphViewConverter interface methods to give it a more meaningful name * Improved the OptimizationViews class, added comments and useful funtion overloads * Fixed an error in the default implementationof the new OptimizeSubgraphView method in IBackendInternal Change-Id: I683a56d562aa093bac06f0e83fb13e144ed81485 Signed-off-by: Matteo Martincigh --- src/armnn/ISubgraphViewConverter.hpp | 2 +- src/armnn/Network.cpp | 17 +++++++++-------- src/backends/backendsCommon/IBackendInternal.hpp | 10 +++++----- src/backends/backendsCommon/OptimizationViews.hpp | 16 ++++++++++++---- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/armnn/ISubgraphViewConverter.hpp b/src/armnn/ISubgraphViewConverter.hpp index 1f4b4e5de1..4b2c10298d 100644 --- a/src/armnn/ISubgraphViewConverter.hpp +++ b/src/armnn/ISubgraphViewConverter.hpp @@ -20,7 +20,7 @@ class ISubgraphViewConverter public: virtual ~ISubgraphViewConverter() {} - virtual std::vector GetOutput() = 0; + virtual std::vector CompileNetwork() = 0; }; } // namespace armnn diff --git a/src/armnn/Network.cpp b/src/armnn/Network.cpp index 1eb40d5ba0..c3f29d4f3b 100644 --- a/src/armnn/Network.cpp +++ b/src/armnn/Network.cpp @@ -365,25 +365,26 @@ OptimizationResult ApplyBackendOptimizations(OptimizedNetwork* optNetObjPtr, for (auto& subgraph : subgraphs) { // Try to optimize the current sub-graph - OptimizationViews optViews = backendObjPtr->OptimizeSubgraphView(*subgraph); - BOOST_ASSERT(optViews.Validate(*subgraph)); + OptimizationViews optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraph); + BOOST_ASSERT(optimizationViews.Validate(*subgraph)); // Optimization attempted, check the resulting optimized sub-graph - for (auto& substitution : optViews.GetSubstitutions()) + for (auto& substitution : optimizationViews.GetSubstitutions()) { // Sub-graph optimized, substitute the sub-graph with the new optimized one in the main optimized graph - SubgraphView& optSubgraph = substitution.m_ReplacementSubgraph; - optGraph.SubstituteSubgraph(substitution.m_SubstitutableSubgraph, optSubgraph); + SubgraphView& replacementSubgraph = substitution.m_ReplacementSubgraph; + SubgraphView& substitutableSubgraph = substitution.m_SubstitutableSubgraph; + optGraph.SubstituteSubgraph(substitutableSubgraph, replacementSubgraph); // Assign the current backend to the optimized sub-graph - std::for_each(optSubgraph.begin(), optSubgraph.end(), [&selectedBackend](Layer* l) + std::for_each(replacementSubgraph.begin(), replacementSubgraph.end(), [&selectedBackend](Layer* l) { BOOST_ASSERT(l); l->SetBackendId(selectedBackend); }); } - if (!optViews.GetFailedSubgraphs().empty()) + if (!optimizationViews.GetFailedSubgraphs().empty()) { std::stringstream warningMsg; warningMsg << "Some sub-graph(s) failed to optimized on " << backendObjPtr->GetId() << " backend."; @@ -398,7 +399,7 @@ OptimizationResult ApplyBackendOptimizations(OptimizedNetwork* optNetObjPtr, } int count=0; - for (auto& failedSubgraph : optViews.GetFailedSubgraphs()) + for (auto& failedSubgraph : optimizationViews.GetFailedSubgraphs()) { // An error occurred: the optimization was attempted but not performed, try different backends std::stringstream subgraphMsg; diff --git a/src/backends/backendsCommon/IBackendInternal.hpp b/src/backends/backendsCommon/IBackendInternal.hpp index 5316f68009..3d94d6e9d9 100644 --- a/src/backends/backendsCommon/IBackendInternal.hpp +++ b/src/backends/backendsCommon/IBackendInternal.hpp @@ -59,7 +59,7 @@ public: // @deprecated Use "OptimizationViews OptimizeSubgraphView(const SubgraphView&);" instead. virtual SubgraphViewUniquePtr OptimizeSubgraphView(const SubgraphView& subgraph, bool& optimizationAttempted) const { - optimizationAttempted=false; + optimizationAttempted = false; return nullptr; } @@ -67,11 +67,11 @@ public: // Override this method with a custom optimization implementation. virtual OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph) const { - bool attempted=false; - SubgraphViewUniquePtr optSubgraph = OptimizeSubgraphView(subgraph, attempted); + bool optimizationAttempted = false; + SubgraphViewUniquePtr optSubgraph = OptimizeSubgraphView(subgraph, optimizationAttempted); OptimizationViews result; - if (!attempted) + if (!optimizationAttempted) { result.AddUntouchedSubgraph(SubgraphView(subgraph)); } @@ -79,7 +79,7 @@ public: { if (optSubgraph) { - result.AddSubstituion({*optSubgraph.get(), subgraph}); + result.AddSubstituion({subgraph, SubgraphView(*optSubgraph.get())}); } else { diff --git a/src/backends/backendsCommon/OptimizationViews.hpp b/src/backends/backendsCommon/OptimizationViews.hpp index 14296f0704..e96c11aaba 100644 --- a/src/backends/backendsCommon/OptimizationViews.hpp +++ b/src/backends/backendsCommon/OptimizationViews.hpp @@ -8,6 +8,7 @@ namespace armnn { + class OptimizationViews { public: @@ -44,18 +45,25 @@ public: m_UntouchedSubgraphs.emplace_back(subgraph); } - Substitutions GetSubstitutions() const { return m_SuccesfulOptimizations; } - Subgraphs GetFailedSubgraphs() const { return m_FailedOptimizations; } - Subgraphs GetUntouchedSubgraphs() const { return m_UntouchedSubgraphs; } + const Substitutions& GetSubstitutions() const { return m_SuccesfulOptimizations; } + const Subgraphs& GetFailedSubgraphs() const { return m_FailedOptimizations; } + const Subgraphs& GetUntouchedSubgraphs() const { return m_UntouchedSubgraphs; } + + Substitutions& GetSubstitutions() { return m_SuccesfulOptimizations; } + Subgraphs& GetFailedSubgraphs() { return m_FailedOptimizations; } + Subgraphs& GetUntouchedSubgraphs() { return m_UntouchedSubgraphs; } + bool Validate(const SubgraphView& originalSubgraph) const; - Graph& GetGraph() { return m_Graph; }; + Graph& GetGraph() { return m_Graph; } private: Substitutions m_SuccesfulOptimizations; ///< Proposed substitutions from successful optimizations Subgraphs m_FailedOptimizations; ///< Subgraphs from the original subgraph which cannot be supported Subgraphs m_UntouchedSubgraphs; ///< Subgraphs from the original subgraph which remain unmodified + /// Graph object used only as a container for any layer generated by the optimization process Graph m_Graph; }; + } //namespace armnn -- cgit v1.2.1