blob: c9a8c7dde581cc24ef41d2f18f43ea0babfae7f1 [file] [log] [blame]
Derek Lambertic2fe5fb2019-05-08 10:23:08 +01001//
Colm Donelana98e79a2022-12-06 21:32:29 +00002// Copyright © 2017,2022 Arm Ltd and Contributors. All rights reserved.
Derek Lambertic2fe5fb2019-05-08 10:23:08 +01003// 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 }
Mike Kelly4cc341c2023-07-07 15:43:06 +010043 for (auto& successful : m_DeletedSubgraphs)
44 {
45 countedLayers.insert(countedLayers.end(),
46 successful.GetIConnectableLayers().begin(),
47 successful.GetIConnectableLayers().end());
48 }
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010049 countedLayers.sort();
50
51 // Compare the two lists to make sure they match
52 valid &= originalLayers.size() == countedLayers.size();
53
54 auto oIt = originalLayers.begin();
55 auto cIt = countedLayers.begin();
56 for (size_t i=0; i < originalLayers.size() && valid; ++i, ++oIt, ++cIt)
57 {
58 valid &= (*oIt == *cIt);
59 }
60
61 // Compare the substitution subgraphs to ensure they are compatible
62 if (valid)
63 {
64 for (auto& substitution : m_SuccesfulOptimizations)
65 {
66 bool validSubstitution = true;
Mike Kellyb6de7a12023-07-18 12:03:41 +010067 const SubgraphView& replacement = substitution.m_ReplacementSubgraph;
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010068 const SubgraphView& old = substitution.m_SubstitutableSubgraph;
Francis Murtagh56ccf682021-12-13 18:48:12 +000069 validSubstitution &= replacement.GetIInputSlots().size() == old.GetIInputSlots().size();
70 validSubstitution &= replacement.GetIOutputSlots().size() == old.GetIOutputSlots().size();
Derek Lambertic2fe5fb2019-05-08 10:23:08 +010071 valid &= validSubstitution;
72 }
73 }
74 return valid;
75}
76} //namespace armnn