blob: e81a6912a1b44fd0c648040f598af75707d4767f [file] [log] [blame]
Derek Lambertic2fe5fb2019-05-08 10:23:08 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Matteo Martincighe5b8eb92019-11-28 15:45:42 +00006#include <armnn/backends/OptimizationViews.hpp>
Derek Lambertic2fe5fb2019-05-08 10:23:08 +01007
8namespace armnn
9{
10
11bool OptimizationViews::Validate(const armnn::SubgraphView& originalSubgraph) const
12{
13 //This needs to verify that:
14 // 1) the sum of m_SuccesfulOptimizations & m_FailedOptimizations & m_UntouchedSubgraphs contains subgraphviews
15 // which cover the entire space of the originalSubgraph.
16 // 2) Each SubstitutionPair contains matching inputs and outputs
17 bool valid = true;
18
19 // Create a copy of the layer list from the original subgraph and sort it
Francis Murtagh56ccf682021-12-13 18:48:12 +000020 SubgraphView::IConnectableLayers originalLayers = originalSubgraph.GetIConnectableLayers();
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010021 originalLayers.sort();
22
23 // Create a new list based on the sum of all the subgraphs and sort it
Francis Murtagh56ccf682021-12-13 18:48:12 +000024 SubgraphView::IConnectableLayers countedLayers;
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010025 for (auto& failed : m_FailedOptimizations)
26 {
Francis Murtagh56ccf682021-12-13 18:48:12 +000027 countedLayers.insert(countedLayers.end(),
28 failed.GetIConnectableLayers().begin(),
29 failed.GetIConnectableLayers().end());
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010030 }
31 for (auto& untouched : m_UntouchedSubgraphs)
32 {
Francis Murtagh56ccf682021-12-13 18:48:12 +000033 countedLayers.insert(countedLayers.end(),
34 untouched.GetIConnectableLayers().begin(),
35 untouched.GetIConnectableLayers().end());
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010036 }
37 for (auto& successful : m_SuccesfulOptimizations)
38 {
39 countedLayers.insert(countedLayers.end(),
Francis Murtagh56ccf682021-12-13 18:48:12 +000040 successful.m_SubstitutableSubgraph.GetIConnectableLayers().begin(),
41 successful.m_SubstitutableSubgraph.GetIConnectableLayers().end());
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010042 }
43 countedLayers.sort();
44
45 // Compare the two lists to make sure they match
46 valid &= originalLayers.size() == countedLayers.size();
47
48 auto oIt = originalLayers.begin();
49 auto cIt = countedLayers.begin();
50 for (size_t i=0; i < originalLayers.size() && valid; ++i, ++oIt, ++cIt)
51 {
52 valid &= (*oIt == *cIt);
53 }
54
55 // Compare the substitution subgraphs to ensure they are compatible
56 if (valid)
57 {
58 for (auto& substitution : m_SuccesfulOptimizations)
59 {
60 bool validSubstitution = true;
61 const SubgraphView& replacement = substitution.m_ReplacementSubgraph;
62 const SubgraphView& old = substitution.m_SubstitutableSubgraph;
Francis Murtagh56ccf682021-12-13 18:48:12 +000063 validSubstitution &= replacement.GetIInputSlots().size() == old.GetIInputSlots().size();
64 validSubstitution &= replacement.GetIOutputSlots().size() == old.GetIOutputSlots().size();
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010065 valid &= validSubstitution;
66 }
67 }
68 return valid;
69}
70} //namespace armnn