Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 6 | #include <armnn/backends/OptimizationViews.hpp> |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 7 | |
| 8 | namespace armnn |
| 9 | { |
| 10 | |
| 11 | bool 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 Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 20 | SubgraphView::IConnectableLayers originalLayers = originalSubgraph.GetIConnectableLayers(); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 21 | originalLayers.sort(); |
| 22 | |
| 23 | // Create a new list based on the sum of all the subgraphs and sort it |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 24 | SubgraphView::IConnectableLayers countedLayers; |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 25 | for (auto& failed : m_FailedOptimizations) |
| 26 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 27 | countedLayers.insert(countedLayers.end(), |
| 28 | failed.GetIConnectableLayers().begin(), |
| 29 | failed.GetIConnectableLayers().end()); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 30 | } |
| 31 | for (auto& untouched : m_UntouchedSubgraphs) |
| 32 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 33 | countedLayers.insert(countedLayers.end(), |
| 34 | untouched.GetIConnectableLayers().begin(), |
| 35 | untouched.GetIConnectableLayers().end()); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 36 | } |
| 37 | for (auto& successful : m_SuccesfulOptimizations) |
| 38 | { |
| 39 | countedLayers.insert(countedLayers.end(), |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 40 | successful.m_SubstitutableSubgraph.GetIConnectableLayers().begin(), |
| 41 | successful.m_SubstitutableSubgraph.GetIConnectableLayers().end()); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 42 | } |
| 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 Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 63 | validSubstitution &= replacement.GetIInputSlots().size() == old.GetIInputSlots().size(); |
| 64 | validSubstitution &= replacement.GetIOutputSlots().size() == old.GetIOutputSlots().size(); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 65 | valid &= validSubstitution; |
| 66 | } |
| 67 | } |
| 68 | return valid; |
| 69 | } |
| 70 | } //namespace armnn |