From 1addbf3e52fe1b25e8101de81da7f35e718d68b9 Mon Sep 17 00:00:00 2001 From: Rob Hughes Date: Fri, 19 Feb 2021 09:24:44 +0000 Subject: Make SubgraphViewSelector give deterministic results The subgraphs produced by SubgraphViewSelector were not deterministic as the order of the input slots, outputs slots and layers within each subgraph were determined by the pointer values of those objects, which are not guaranteed to be the same for each execution. This patch adds a post-processing sorting step based on the GUIDs of the layers and the slot indices so that the results will be the same for each execution. This makes debugging the optimised graph much easier as subsequent stages can also be deterministic. Change-Id: Ifbcb199733066f99e9f95808a8da22023e5944f1 Signed-off-by: Rob Hughes --- src/armnn/SubgraphViewSelector.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/armnn/SubgraphViewSelector.cpp b/src/armnn/SubgraphViewSelector.cpp index 96e75abf8b..21fbb7cd80 100644 --- a/src/armnn/SubgraphViewSelector.cpp +++ b/src/armnn/SubgraphViewSelector.cpp @@ -472,6 +472,39 @@ SubgraphViewSelector::SelectSubgraphs(SubgraphView& subgraph, const LayerSelecto infoPtr->CollectNonSelectedOutputSlots(layerInfos, outputs); layers.push_back(infoPtr->m_Layer); } + + // Sort lists into deterministic order, not relying on pointer values which may be different on each execution. + // This makes debugging the optimised graph much easier as subsequent stages can also be deterministic. + std::sort(inputs.begin(), inputs.end(), [](const InputSlot* a, const InputSlot* b) + { + const LayerGuid guidA = a->GetOwningLayer().GetGuid(); + const LayerGuid guidB = b->GetOwningLayer().GetGuid(); + if (guidA < guidB) + { + return true; + } + else if (guidA == guidB) + { + return (a->GetSlotIndex() < b->GetSlotIndex()); + } + return false; + }); + std::sort(outputs.begin(), outputs.end(), [](const OutputSlot* a, const OutputSlot* b) + { + const LayerGuid guidA = a->GetOwningLayer().GetGuid(); + const LayerGuid guidB = b->GetOwningLayer().GetGuid(); + if (guidA < guidB) + { + return true; + } + else if (guidA == guidB) + { + return (a->CalculateIndexOnOwner() < b->CalculateIndexOnOwner()); + } + return false; + }); + layers.sort([](const Layer* a, const Layer* b) { return a->GetGuid() < b->GetGuid(); }); + // Create a new sub-graph with the new lists of input/output slots and layer result.emplace_back(std::make_unique(std::move(inputs), std::move(outputs), -- cgit v1.2.1