blob: 1190eea57d3334b16501b498f3eb4053006b74c0 [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
6#include "OptimizationViews.hpp"
7
8
9namespace armnn
10{
11
12bool OptimizationViews::Validate(const armnn::SubgraphView& originalSubgraph) const
13{
14 //This needs to verify that:
15 // 1) the sum of m_SuccesfulOptimizations & m_FailedOptimizations & m_UntouchedSubgraphs contains subgraphviews
16 // which cover the entire space of the originalSubgraph.
17 // 2) Each SubstitutionPair contains matching inputs and outputs
18 bool valid = true;
19
20 // Create a copy of the layer list from the original subgraph and sort it
21 SubgraphView::Layers originalLayers = originalSubgraph.GetLayers();
22 originalLayers.sort();
23
24 // Create a new list based on the sum of all the subgraphs and sort it
25 SubgraphView::Layers countedLayers;
26 for (auto& failed : m_FailedOptimizations)
27 {
28 countedLayers.insert(countedLayers.end(), failed.GetLayers().begin(), failed.GetLayers().end());
29 }
30 for (auto& untouched : m_UntouchedSubgraphs)
31 {
32 countedLayers.insert(countedLayers.end(), untouched.GetLayers().begin(), untouched.GetLayers().end());
33 }
34 for (auto& successful : m_SuccesfulOptimizations)
35 {
36 countedLayers.insert(countedLayers.end(),
37 successful.m_SubstitutableSubgraph.GetLayers().begin(),
38 successful.m_SubstitutableSubgraph.GetLayers().end());
39 }
40 countedLayers.sort();
41
42 // Compare the two lists to make sure they match
43 valid &= originalLayers.size() == countedLayers.size();
44
45 auto oIt = originalLayers.begin();
46 auto cIt = countedLayers.begin();
47 for (size_t i=0; i < originalLayers.size() && valid; ++i, ++oIt, ++cIt)
48 {
49 valid &= (*oIt == *cIt);
50 }
51
52 // Compare the substitution subgraphs to ensure they are compatible
53 if (valid)
54 {
55 for (auto& substitution : m_SuccesfulOptimizations)
56 {
57 bool validSubstitution = true;
58 const SubgraphView& replacement = substitution.m_ReplacementSubgraph;
59 const SubgraphView& old = substitution.m_SubstitutableSubgraph;
60 validSubstitution &= replacement.GetInputSlots().size() == old.GetInputSlots().size();
61 validSubstitution &= replacement.GetOutputSlots().size() == old.GetOutputSlots().size();
62 valid &= validSubstitution;
63 }
64 }
65 return valid;
66}
67} //namespace armnn