blob: eee2c67ea9dc86310bb48b24f0a152c6d57bb82b [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
20 SubgraphView::Layers originalLayers = originalSubgraph.GetLayers();
21 originalLayers.sort();
22
23 // Create a new list based on the sum of all the subgraphs and sort it
24 SubgraphView::Layers countedLayers;
25 for (auto& failed : m_FailedOptimizations)
26 {
27 countedLayers.insert(countedLayers.end(), failed.GetLayers().begin(), failed.GetLayers().end());
28 }
29 for (auto& untouched : m_UntouchedSubgraphs)
30 {
31 countedLayers.insert(countedLayers.end(), untouched.GetLayers().begin(), untouched.GetLayers().end());
32 }
33 for (auto& successful : m_SuccesfulOptimizations)
34 {
35 countedLayers.insert(countedLayers.end(),
36 successful.m_SubstitutableSubgraph.GetLayers().begin(),
37 successful.m_SubstitutableSubgraph.GetLayers().end());
38 }
39 countedLayers.sort();
40
41 // Compare the two lists to make sure they match
42 valid &= originalLayers.size() == countedLayers.size();
43
44 auto oIt = originalLayers.begin();
45 auto cIt = countedLayers.begin();
46 for (size_t i=0; i < originalLayers.size() && valid; ++i, ++oIt, ++cIt)
47 {
48 valid &= (*oIt == *cIt);
49 }
50
51 // Compare the substitution subgraphs to ensure they are compatible
52 if (valid)
53 {
54 for (auto& substitution : m_SuccesfulOptimizations)
55 {
56 bool validSubstitution = true;
57 const SubgraphView& replacement = substitution.m_ReplacementSubgraph;
58 const SubgraphView& old = substitution.m_SubstitutableSubgraph;
59 validSubstitution &= replacement.GetInputSlots().size() == old.GetInputSlots().size();
60 validSubstitution &= replacement.GetOutputSlots().size() == old.GetOutputSlots().size();
61 valid &= validSubstitution;
62 }
63 }
64 return valid;
65}
66} //namespace armnn