Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1 | // |
Francis Murtagh | 01f7269 | 2022-12-21 09:10:04 +0000 | [diff] [blame] | 2 | // Copyright © 2017, 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 6 | #include <CommonTestUtils.hpp> |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 7 | #include "MockBackendId.hpp" |
| 8 | |
| 9 | #include <Graph.hpp> |
| 10 | #include <Network.hpp> |
| 11 | |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 12 | #include <armnn/BackendRegistry.hpp> |
Cathal Corbett | 3464ba1 | 2022-03-04 11:36:39 +0000 | [diff] [blame] | 13 | #include <armnnTestUtils/MockBackend.hpp> |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 14 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 15 | #include <doctest/doctest.h> |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 16 | #include <unordered_map> |
| 17 | |
| 18 | using namespace armnn; |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | // The expected number of layers, input and output slots in a subgraph after a test |
| 24 | struct ExpectedSubgraphSize |
| 25 | { |
| 26 | size_t m_NumInputSlots = 0; |
| 27 | size_t m_NumOutputSlots = 0; |
| 28 | size_t m_NumLayers = 0; |
| 29 | }; |
| 30 | |
| 31 | // Keep the layers organized by layer name |
| 32 | using LayerNameToLayerMap = std::unordered_map<std::string, Layer*>; |
| 33 | |
| 34 | // Used to convert input and output slots from reference type (as stored in graphs) to |
| 35 | // pointer type (as stored in subgraphs) |
| 36 | template <typename SlotType> |
| 37 | SlotType* ConvertReferenceTypeToPointerType(const SlotType& input) |
| 38 | { |
| 39 | return const_cast<SlotType*>(&input); |
| 40 | } |
| 41 | |
| 42 | // Used to convert input and output slots from reference type (as stored in graphs) to |
| 43 | // pointer type (as stored in subgraphs), array version |
| 44 | template <typename SlotType> |
| 45 | std::vector<SlotType*> ConvertReferenceTypeToPointerType(const std::vector<SlotType>& input) |
| 46 | { |
| 47 | std::vector<SlotType*> output; |
| 48 | std::transform(input.begin(), |
| 49 | input.end(), |
| 50 | std::back_inserter(output), |
| 51 | [](const SlotType& inputItem) |
| 52 | { |
| 53 | return ConvertReferenceTypeToPointerType(inputItem); |
| 54 | }); |
| 55 | |
| 56 | return output; |
| 57 | } |
| 58 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 59 | // Convert from vector of Slots* (Input/Output) to vector of ISlots* (IInput/IOutput) |
| 60 | template <typename SlotType, typename ResultSlotType> |
| 61 | std::vector<ResultSlotType*> ConvertSlotsToISlots(const std::vector<SlotType*> input) |
| 62 | { |
| 63 | std::vector<ResultSlotType*> output; |
| 64 | for (auto slot : input) |
| 65 | { |
| 66 | output.push_back(PolymorphicDowncast<ResultSlotType*>(slot)); |
| 67 | } |
| 68 | return output; |
| 69 | } |
| 70 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 71 | // Convenience function to add an input layer to a graph |
| 72 | Layer* AddInputLayer(Graph& graph, |
| 73 | const std::string& layerName, |
| 74 | const TensorInfo& inputInfo, |
| 75 | LayerBindingId inputId = 0) |
| 76 | { |
| 77 | Layer* const inputLayer = graph.AddLayer<InputLayer>(inputId, layerName.c_str()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 78 | CHECK(inputLayer); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 79 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputInfo); |
| 80 | return inputLayer; |
| 81 | } |
| 82 | |
| 83 | // Convenience function to add an output layer to a graph |
| 84 | Layer* AddOutputLayer(Graph& graph, |
| 85 | const std::string& layerName) |
| 86 | { |
| 87 | Layer* const outputLayer = graph.AddLayer<OutputLayer>(0, layerName.c_str()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 88 | CHECK(outputLayer); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 89 | return outputLayer; |
| 90 | } |
| 91 | |
| 92 | // Convenience function to add a convolution layer to a graph |
| 93 | Convolution2dLayer* AddConvolutionLayer(Graph& graph, |
| 94 | LayerNameToLayerMap& layersInGraph, |
| 95 | const Convolution2dDescriptor& convolutionDescriptor, |
| 96 | const std::string& layerName, |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 97 | const TensorInfo& outputInfo) |
| 98 | { |
| 99 | Convolution2dLayer* const convLayer = graph.AddLayer<Convolution2dLayer>(convolutionDescriptor, layerName.c_str()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 100 | CHECK(convLayer); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 101 | convLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 102 | layersInGraph.insert(std::make_pair(convLayer->GetName(), convLayer)); |
| 103 | return convLayer; |
| 104 | } |
| 105 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 106 | // Convenience function to add a constant layer to a graph |
| 107 | ConstantLayer* AddConstantLayer(Graph& graph, |
| 108 | LayerNameToLayerMap& layersInGraph, |
| 109 | const std::string& layerName, |
| 110 | const ConstTensor& constTensor, |
| 111 | const TensorInfo& outputInfo) |
| 112 | { |
| 113 | ConstantLayer* const constantLayer = graph.AddLayer<ConstantLayer>(layerName.c_str()); |
| 114 | CHECK(constantLayer); |
| 115 | constantLayer->m_LayerOutput = std::make_shared<ScopedTensorHandle>(constTensor); |
| 116 | constantLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 117 | layersInGraph.insert(std::make_pair(constantLayer->GetName(), constantLayer)); |
| 118 | return constantLayer; |
| 119 | } |
| 120 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 121 | // Convenience function to add a pooling layer to a graph |
| 122 | Pooling2dLayer* AddPoolingLayer(Graph& graph, |
| 123 | LayerNameToLayerMap& layersInGraph, |
| 124 | const Pooling2dDescriptor& poolingDescriptor, |
| 125 | const std::string& layerName, |
| 126 | const TensorInfo& outputInfo) |
| 127 | { |
| 128 | Pooling2dLayer* const poolingLayer = graph.AddLayer<Pooling2dLayer>(poolingDescriptor, layerName.c_str()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 129 | CHECK(poolingLayer); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 130 | poolingLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 131 | layersInGraph.insert(std::make_pair(poolingLayer->GetName(), poolingLayer)); |
| 132 | return poolingLayer; |
| 133 | } |
| 134 | |
| 135 | // Convenience function to add an addition layer to a graph |
| 136 | AdditionLayer* AddAdditionaLayer(Graph& graph, |
| 137 | LayerNameToLayerMap& layersInGraph, |
| 138 | const std::string& layerName, |
| 139 | const TensorInfo& outputInfo) |
| 140 | { |
| 141 | AdditionLayer* const additionLayer = graph.AddLayer<AdditionLayer>(layerName.c_str()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 142 | CHECK(additionLayer); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 143 | additionLayer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 144 | layersInGraph.insert(std::make_pair(additionLayer->GetName(), additionLayer)); |
| 145 | return additionLayer; |
| 146 | } |
| 147 | |
| 148 | // Convenience function to check that the given substitution matches the specified expected values |
| 149 | void CheckSubstitution(const OptimizationViews::SubstitutionPair& substitution, |
| 150 | const ExpectedSubgraphSize& expectedSubstitutableSubgraphSize, |
| 151 | const ExpectedSubgraphSize& expectedReplacementSubgraphSize, |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 152 | const SubgraphView::IInputSlots& expectedSubstitutableInputSlots, |
| 153 | const SubgraphView::IOutputSlots& expectedSubstitutableOutputSlots, |
| 154 | const SubgraphView::IConnectableLayers& expectedSubstitutableLayers) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 155 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 156 | const SubgraphView& substitutableSubgraph = substitution.m_SubstitutableSubgraph; |
| 157 | const SubgraphView::IInputSlots& substitutableSubgraphInputSlots = substitutableSubgraph.GetIInputSlots(); |
| 158 | const SubgraphView::IOutputSlots& substitutableSubgraphOutputSlots = substitutableSubgraph.GetIOutputSlots(); |
| 159 | const SubgraphView::IConnectableLayers& substitutableSubgraphLayers = |
| 160 | substitutableSubgraph.GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 161 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 162 | const SubgraphView& replacementSubgraph = substitution.m_ReplacementSubgraph; |
| 163 | const SubgraphView::IInputSlots& replacementSubgraphInputSlots = replacementSubgraph.GetIInputSlots(); |
| 164 | const SubgraphView::IOutputSlots& replacementSubgraphOutputSlots = replacementSubgraph.GetIOutputSlots(); |
| 165 | const SubgraphView::IConnectableLayers& replacementSubgraphLayers = replacementSubgraph.GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 166 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 167 | CHECK(substitutableSubgraphInputSlots.size() == expectedSubstitutableSubgraphSize.m_NumInputSlots); |
| 168 | CHECK(substitutableSubgraphOutputSlots.size() == expectedSubstitutableSubgraphSize.m_NumOutputSlots); |
| 169 | CHECK(substitutableSubgraphLayers.size() == expectedSubstitutableSubgraphSize.m_NumLayers); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 170 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 171 | CHECK(AreEqual(substitutableSubgraphInputSlots, expectedSubstitutableInputSlots)); |
| 172 | CHECK(AreEqual(substitutableSubgraphOutputSlots, expectedSubstitutableOutputSlots)); |
| 173 | CHECK(AreEqual(substitutableSubgraphLayers, expectedSubstitutableLayers)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 174 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 175 | CHECK(replacementSubgraphInputSlots.size() == expectedReplacementSubgraphSize.m_NumInputSlots); |
| 176 | CHECK(replacementSubgraphOutputSlots.size() == expectedReplacementSubgraphSize.m_NumOutputSlots); |
| 177 | CHECK(replacementSubgraphLayers.size() == expectedReplacementSubgraphSize.m_NumLayers); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 178 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 179 | CHECK(!AreEqual(replacementSubgraphInputSlots, expectedSubstitutableInputSlots)); |
| 180 | CHECK(!AreEqual(replacementSubgraphOutputSlots, expectedSubstitutableOutputSlots)); |
| 181 | CHECK(!AreEqual(replacementSubgraphLayers, expectedSubstitutableLayers)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 182 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 183 | CHECK(std::all_of(replacementSubgraphLayers.begin(), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 184 | replacementSubgraphLayers.end(), |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 185 | [](const IConnectableLayer* layer) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 186 | { |
| 187 | return layer->GetType() == LayerType::PreCompiled; |
| 188 | })); |
| 189 | } |
| 190 | |
| 191 | // Convenience function to check that the given failed subgraph matches the specified expected values |
| 192 | void CheckFailedSubgraph(const SubgraphView& failedSubgraph, |
| 193 | const ExpectedSubgraphSize& expectedFailedSubgraphSize, |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 194 | const SubgraphView::IInputSlots& expectedFailedInputSlots, |
| 195 | const SubgraphView::IOutputSlots& expectedFailedOutputSlots, |
| 196 | const SubgraphView::IConnectableLayers& expectedFailedLayers) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 197 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 198 | const SubgraphView::IInputSlots& failedSubgraphInputSlots = failedSubgraph.GetIInputSlots(); |
| 199 | const SubgraphView::IOutputSlots& failedSubgraphOutputSlots = failedSubgraph.GetIOutputSlots(); |
| 200 | const SubgraphView::IConnectableLayers& failedSubgraphLayers = failedSubgraph.GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 201 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 202 | CHECK(failedSubgraphInputSlots.size() == expectedFailedSubgraphSize.m_NumInputSlots); |
| 203 | CHECK(failedSubgraphOutputSlots.size() == expectedFailedSubgraphSize.m_NumOutputSlots); |
| 204 | CHECK(failedSubgraphLayers.size() == expectedFailedSubgraphSize.m_NumLayers); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 205 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 206 | CHECK(AreEqual(failedSubgraphInputSlots, expectedFailedInputSlots)); |
| 207 | CHECK(AreEqual(failedSubgraphOutputSlots, expectedFailedOutputSlots)); |
| 208 | CHECK(AreEqual(failedSubgraphLayers, expectedFailedLayers)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Convenience function to check that the given untouched subgraph matches the specified expected values |
| 212 | void CheckUntouchedSubgraph(const SubgraphView& untouchedSubgraph, |
| 213 | const ExpectedSubgraphSize& expectedUntouchedSubgraphSize, |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 214 | const SubgraphView::IInputSlots& expectedUntouchedInputSlots, |
| 215 | const SubgraphView::IOutputSlots& expectedUntouchedOutputSlots, |
| 216 | const SubgraphView::IConnectableLayers& expectedUntouchedLayers) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 217 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 218 | const SubgraphView::IInputSlots& untouchedSubgraphInputSlots = untouchedSubgraph.GetIInputSlots(); |
| 219 | const SubgraphView::IOutputSlots& untouchedSubgraphOutputSlots = untouchedSubgraph.GetIOutputSlots(); |
| 220 | const SubgraphView::IConnectableLayers& untouchedSubgraphLayers = untouchedSubgraph.GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 221 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 222 | CHECK(untouchedSubgraphInputSlots.size() == expectedUntouchedSubgraphSize.m_NumInputSlots); |
| 223 | CHECK(untouchedSubgraphOutputSlots.size() == expectedUntouchedSubgraphSize.m_NumOutputSlots); |
| 224 | CHECK(untouchedSubgraphLayers.size() == expectedUntouchedSubgraphSize.m_NumLayers); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 225 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 226 | CHECK(AreEqual(untouchedSubgraphInputSlots, expectedUntouchedInputSlots)); |
| 227 | CHECK(AreEqual(untouchedSubgraphOutputSlots, expectedUntouchedOutputSlots)); |
| 228 | CHECK(AreEqual(untouchedSubgraphLayers, expectedUntouchedLayers)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Creates a subgraph containing only a single unsupported layer (only convolutions are unsupported by the mock backend) |
| 232 | SubgraphView::SubgraphViewPtr BuildFullyUnsupportedSubgraph1(Graph& graph, LayerNameToLayerMap& layersInGraph) |
| 233 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 234 | const TensorInfo inputInfo ({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
| 235 | const TensorInfo outputInfo({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 236 | |
| 237 | Pooling2dDescriptor poolingDescriptor; |
| 238 | poolingDescriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 239 | poolingDescriptor.m_PoolWidth = 2; |
| 240 | poolingDescriptor.m_PoolHeight = 2; |
| 241 | poolingDescriptor.m_StrideX = 2; |
| 242 | poolingDescriptor.m_StrideY = 2; |
| 243 | poolingDescriptor.m_PadLeft = 1; |
| 244 | poolingDescriptor.m_PadRight = 1; |
| 245 | poolingDescriptor.m_PadTop = 1; |
| 246 | poolingDescriptor.m_PadBottom = 1; |
| 247 | poolingDescriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 248 | poolingDescriptor.m_DataLayout = DataLayout::NHWC; |
| 249 | |
| 250 | // Construct the graph |
| 251 | Layer* const inputLayer = AddInputLayer(graph, "input layer", inputInfo); |
| 252 | Pooling2dLayer* const poolingLayer = AddPoolingLayer(graph, layersInGraph, poolingDescriptor, |
| 253 | "pooling layer", outputInfo); |
| 254 | Layer* const outputLayer = AddOutputLayer(graph, "output layer"); |
| 255 | |
| 256 | // Connect the network |
| 257 | inputLayer->GetOutputSlot(0).Connect(poolingLayer->GetInputSlot(0)); |
| 258 | poolingLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 259 | |
| 260 | // Create the subgraph view for the whole network |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 261 | return CreateSubgraphViewFrom(CreateInputsFrom(poolingLayer), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 262 | CreateOutputsFrom({poolingLayer}), |
| 263 | {poolingLayer}); |
| 264 | } |
| 265 | |
| 266 | // Creates a subgraph containing only unsupported layers (only convolutions are unsupported by the mock backend) |
| 267 | SubgraphView::SubgraphViewPtr BuildFullyUnsupportedSubgraph2(Graph& graph, LayerNameToLayerMap& layersInGraph) |
| 268 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 269 | const TensorInfo inputInfo ({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
| 270 | const TensorInfo outputInfo({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 271 | |
| 272 | Pooling2dDescriptor poolingDescriptor; |
| 273 | poolingDescriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 274 | poolingDescriptor.m_PoolWidth = 2; |
| 275 | poolingDescriptor.m_PoolHeight = 2; |
| 276 | poolingDescriptor.m_StrideX = 2; |
| 277 | poolingDescriptor.m_StrideY = 2; |
| 278 | poolingDescriptor.m_PadLeft = 1; |
| 279 | poolingDescriptor.m_PadRight = 1; |
| 280 | poolingDescriptor.m_PadTop = 1; |
| 281 | poolingDescriptor.m_PadBottom = 1; |
| 282 | poolingDescriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 283 | poolingDescriptor.m_DataLayout = DataLayout::NHWC; |
| 284 | |
| 285 | // Construct the graph |
| 286 | Layer* const inputLayer = AddInputLayer(graph, "input layer", inputInfo); |
| 287 | Pooling2dLayer* const pooling1Layer = AddPoolingLayer(graph, layersInGraph, poolingDescriptor, |
| 288 | "pooling1 layer", outputInfo); |
| 289 | Pooling2dLayer* const pooling2Layer = AddPoolingLayer(graph, layersInGraph, poolingDescriptor, |
| 290 | "pooling2 layer", outputInfo); |
| 291 | Pooling2dLayer* const pooling3Layer = AddPoolingLayer(graph, layersInGraph, poolingDescriptor, |
| 292 | "pooling3 layer", outputInfo); |
| 293 | Layer* const outputLayer = AddOutputLayer(graph, "output layer"); |
| 294 | |
| 295 | // Connect the network |
| 296 | inputLayer->GetOutputSlot(0).Connect(pooling1Layer->GetInputSlot(0)); |
| 297 | pooling1Layer->GetOutputSlot(0).Connect(pooling2Layer->GetInputSlot(0)); |
| 298 | pooling2Layer->GetOutputSlot(0).Connect(pooling3Layer->GetInputSlot(0)); |
| 299 | pooling3Layer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 300 | |
| 301 | // Create the subgraph view for the whole network |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 302 | return CreateSubgraphViewFrom(CreateInputsFrom(pooling1Layer), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 303 | CreateOutputsFrom({pooling3Layer}), |
| 304 | {pooling1Layer, |
| 305 | pooling2Layer, |
| 306 | pooling3Layer}); |
| 307 | } |
| 308 | |
| 309 | // Creates a simple subgraph with only one convolution layer, supported by the mock backend |
| 310 | SubgraphView::SubgraphViewPtr BuildFullyOptimizableSubgraph1(Graph& graph, LayerNameToLayerMap& layersInGraph) |
| 311 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 312 | const TensorInfo inputInfo ({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
| 313 | const TensorInfo outputInfo({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 314 | TensorInfo weightInfo({ 16, 1, 1, 16 }, DataType::QAsymmU8, 0.9f, 0); |
Keith Davis | 721e629 | 2022-05-17 10:06:53 +0100 | [diff] [blame] | 315 | TensorInfo biasInfo({ 1, 1, 1, 16 }, DataType::Signed32, 0.9f, 0); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 316 | |
| 317 | weightInfo.SetConstant(true); |
| 318 | biasInfo.SetConstant(true); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 319 | |
| 320 | Convolution2dDescriptor convolutionDescriptor; |
| 321 | convolutionDescriptor.m_StrideX = 1; |
| 322 | convolutionDescriptor.m_StrideY = 1; |
| 323 | convolutionDescriptor.m_BiasEnabled = true; |
| 324 | convolutionDescriptor.m_DataLayout = DataLayout::NHWC; |
| 325 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 326 | std::vector<float> weightsVector(64); |
| 327 | ConstTensor constWeightsTensor(weightInfo, weightsVector); |
| 328 | |
| 329 | std::vector<float> biasVector(16); |
| 330 | ConstTensor constBiasTensor(biasInfo, biasVector); |
| 331 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 332 | // Construct the graph |
| 333 | Layer* const inputLayer = AddInputLayer(graph, "input layer", inputInfo); |
| 334 | Convolution2dLayer* const convLayer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 335 | "conv layer", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 336 | |
| 337 | ConstantLayer* const weightsLayer = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 338 | AddConstantLayer(graph, layersInGraph, "Weights Layer", constWeightsTensor, weightInfo); |
| 339 | ConstantLayer* const biasLayer = AddConstantLayer(graph, layersInGraph, "Bias Layer", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 340 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 341 | Layer* const outputLayer = AddOutputLayer(graph, "output layer"); |
| 342 | |
| 343 | // Connect the network |
| 344 | inputLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 345 | weightsLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(1)); |
| 346 | biasLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(2)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 347 | convLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 348 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 349 | std::vector<unsigned int> ignoreSlots = {1, 2}; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 350 | // Create the subgraph view for the whole network |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 351 | return CreateSubgraphViewFrom(CreateInputsFrom(convLayer, ignoreSlots), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 352 | CreateOutputsFrom({convLayer}), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 353 | {convLayer, weightsLayer, biasLayer}); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // Creates a subgraph with five convolutions layers, all supported by the mock backend |
| 357 | SubgraphView::SubgraphViewPtr BuildFullyOptimizableSubgraph2(Graph& graph, LayerNameToLayerMap& layersInGraph) |
| 358 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 359 | const TensorInfo inputInfo ({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
| 360 | const TensorInfo outputInfo({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 361 | TensorInfo weightInfo({ 16, 1, 1, 16 }, DataType::QAsymmU8, 0.9f, 0); |
| 362 | TensorInfo biasInfo ({ 1, 1, 1, 16 }, DataType::Signed32, 0.9f, 0); |
| 363 | |
| 364 | weightInfo.SetConstant(true); |
| 365 | biasInfo.SetConstant(true); |
| 366 | |
| 367 | std::vector<float> weightsVector(64); |
| 368 | ConstTensor constWeightsTensor(weightInfo, weightsVector); |
| 369 | |
| 370 | std::vector<float> biasVector(16); |
| 371 | ConstTensor constBiasTensor(biasInfo, biasVector); |
| 372 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 373 | Convolution2dDescriptor convolutionDescriptor; |
| 374 | convolutionDescriptor.m_StrideX = 1; |
| 375 | convolutionDescriptor.m_StrideY = 1; |
| 376 | convolutionDescriptor.m_BiasEnabled = true; |
| 377 | convolutionDescriptor.m_DataLayout = DataLayout::NHWC; |
| 378 | |
| 379 | // Construct the graph |
| 380 | Layer* const inputLayer = AddInputLayer(graph, "input layer", inputInfo); |
| 381 | Convolution2dLayer* const conv1Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 382 | "conv1 layer", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 383 | ConstantLayer* const weightsLayer1 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 384 | AddConstantLayer(graph, layersInGraph, "Weights Layer 1", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 385 | ConstantLayer* const biasLayer1 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 386 | AddConstantLayer(graph, layersInGraph, "Bias Layer 1", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 387 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 388 | Convolution2dLayer* const conv2Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 389 | "conv2 layer", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 390 | ConstantLayer* const weightsLayer2 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 391 | AddConstantLayer(graph, layersInGraph, "Weights Layer 2", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 392 | ConstantLayer* const biasLayer2 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 393 | AddConstantLayer(graph, layersInGraph, "Bias Layer 2", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 394 | |
| 395 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 396 | Convolution2dLayer* const conv3Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 397 | "conv3 layer", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 398 | ConstantLayer* const weightsLayer3 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 399 | AddConstantLayer(graph, layersInGraph, "Weights Layer 3", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 400 | ConstantLayer* const biasLayer3 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 401 | AddConstantLayer(graph, layersInGraph, "Bias Layer 3", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 402 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 403 | Convolution2dLayer* const conv4Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 404 | "conv4 layer", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 405 | ConstantLayer* const weightsLayer4 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 406 | AddConstantLayer(graph, layersInGraph, "Weights Layer 4", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 407 | ConstantLayer* const biasLayer4 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 408 | AddConstantLayer(graph, layersInGraph, "Bias Layer 4", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 409 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 410 | Convolution2dLayer* const conv5Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 411 | "conv5 layer", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 412 | ConstantLayer* const weightsLayer5 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 413 | AddConstantLayer(graph, layersInGraph, "Weights Layer 5", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 414 | ConstantLayer* const biasLayer5 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 415 | AddConstantLayer(graph, layersInGraph, "Bias Layer 5", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 416 | |
| 417 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 418 | Layer* const outputLayer = AddOutputLayer(graph, "output layer"); |
| 419 | |
| 420 | // Connect the network |
| 421 | inputLayer->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 422 | weightsLayer1->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(1)); |
| 423 | biasLayer1->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(2)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 424 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 425 | conv1Layer->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(0)); |
| 426 | weightsLayer2->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(1)); |
| 427 | biasLayer2->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(2)); |
| 428 | |
| 429 | conv2Layer->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(0)); |
| 430 | weightsLayer3->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(1)); |
| 431 | biasLayer3->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(2)); |
| 432 | |
| 433 | conv3Layer->GetOutputSlot(0).Connect(conv4Layer->GetInputSlot(0)); |
| 434 | weightsLayer4->GetOutputSlot(0).Connect(conv4Layer->GetInputSlot(1)); |
| 435 | biasLayer4->GetOutputSlot(0).Connect(conv4Layer->GetInputSlot(2)); |
| 436 | |
| 437 | conv4Layer->GetOutputSlot(0).Connect(conv5Layer->GetInputSlot(0)); |
| 438 | weightsLayer5->GetOutputSlot(0).Connect(conv5Layer->GetInputSlot(1)); |
| 439 | biasLayer5->GetOutputSlot(0).Connect(conv5Layer->GetInputSlot(2)); |
| 440 | |
| 441 | conv5Layer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 442 | std::vector<unsigned int> ignoreSlots = {1, 2}; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 443 | // Create the subgraph view for the whole network |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 444 | return CreateSubgraphViewFrom(CreateInputsFrom(conv1Layer, ignoreSlots), |
| 445 | CreateOutputsFrom({ conv5Layer }), |
| 446 | { weightsLayer1, |
| 447 | biasLayer1, |
| 448 | conv1Layer, |
| 449 | weightsLayer2, |
| 450 | biasLayer2, |
| 451 | conv2Layer, |
| 452 | weightsLayer3, |
| 453 | biasLayer3, |
| 454 | conv3Layer, |
| 455 | weightsLayer4, |
| 456 | biasLayer4, |
| 457 | conv4Layer, |
| 458 | weightsLayer5, |
| 459 | biasLayer5, |
| 460 | conv5Layer }); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | // Creates a subgraph with both supported and unsupported layers |
| 464 | // (only convolutions are unsupported by the mock backend) |
| 465 | SubgraphView::SubgraphViewPtr BuildPartiallySupportedSubgraph(Graph& graph, LayerNameToLayerMap& layersInGraph) |
| 466 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 467 | const TensorInfo inputInfo ({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
| 468 | const TensorInfo outputInfo({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 469 | TensorInfo weightInfo({ 16, 1, 1, 16 }, DataType::QAsymmU8, 0.9f, 0); |
| 470 | TensorInfo biasInfo ({ 1, 1, 1, 16 }, DataType::Signed32, 0.9f, 0); |
| 471 | |
| 472 | weightInfo.SetConstant(true); |
| 473 | biasInfo.SetConstant(true); |
| 474 | |
| 475 | std::vector<float> weightsVector(64); |
| 476 | ConstTensor constWeightsTensor(weightInfo, weightsVector); |
| 477 | |
| 478 | std::vector<float> biasVector(16); |
| 479 | ConstTensor constBiasTensor(biasInfo, biasVector); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 480 | |
| 481 | Convolution2dDescriptor convolutionDescriptor; |
| 482 | convolutionDescriptor.m_StrideX = 1; |
| 483 | convolutionDescriptor.m_StrideY = 1; |
| 484 | convolutionDescriptor.m_BiasEnabled = true; |
| 485 | convolutionDescriptor.m_DataLayout = DataLayout::NHWC; |
| 486 | |
| 487 | Pooling2dDescriptor poolingDescriptor; |
| 488 | poolingDescriptor.m_PoolType = armnn::PoolingAlgorithm::Average; |
| 489 | poolingDescriptor.m_PoolWidth = 2; |
| 490 | poolingDescriptor.m_PoolHeight = 2; |
| 491 | poolingDescriptor.m_StrideX = 2; |
| 492 | poolingDescriptor.m_StrideY = 2; |
| 493 | poolingDescriptor.m_PadLeft = 1; |
| 494 | poolingDescriptor.m_PadRight = 1; |
| 495 | poolingDescriptor.m_PadTop = 1; |
| 496 | poolingDescriptor.m_PadBottom = 1; |
| 497 | poolingDescriptor.m_PaddingMethod = armnn::PaddingMethod::Exclude; |
| 498 | poolingDescriptor.m_DataLayout = DataLayout::NHWC; |
| 499 | |
| 500 | // Construct the graph |
| 501 | Layer* const inputLayer = AddInputLayer(graph, "input layer", inputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 502 | ConstantLayer* const weightsLayer1 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 503 | AddConstantLayer(graph, layersInGraph, "Weights Layer 1", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 504 | |
| 505 | ConstantLayer* const biasLayer1 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 506 | AddConstantLayer(graph, layersInGraph, "Bias Layer 1", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 507 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 508 | Convolution2dLayer* const conv1Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 509 | "conv1 layer", outputInfo); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 510 | Pooling2dLayer* const pooling1Layer = AddPoolingLayer(graph, layersInGraph, poolingDescriptor, |
| 511 | "pooling1 layer", outputInfo); |
| 512 | Pooling2dLayer* const pooling2Layer = AddPoolingLayer(graph, layersInGraph, poolingDescriptor, |
| 513 | "pooling2 layer", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 514 | |
| 515 | ConstantLayer* const weightsLayer2 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 516 | AddConstantLayer(graph, layersInGraph, "Weights Layer 2", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 517 | |
| 518 | ConstantLayer* const biasLayer2 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 519 | AddConstantLayer(graph, layersInGraph, "Bias Layer 2", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 520 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 521 | Convolution2dLayer* const conv2Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 522 | "conv2 layer", outputInfo); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 523 | Pooling2dLayer* const pooling3Layer = AddPoolingLayer(graph, layersInGraph, poolingDescriptor, |
| 524 | "pooling3 layer", outputInfo); |
| 525 | Layer* const outputLayer = AddOutputLayer(graph, "output layer"); |
| 526 | |
| 527 | // Connect the network |
| 528 | inputLayer->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 529 | weightsLayer1->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(1)); |
| 530 | biasLayer1->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(2)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 531 | conv1Layer->GetOutputSlot(0).Connect(pooling1Layer->GetInputSlot(0)); |
| 532 | pooling1Layer->GetOutputSlot(0).Connect(pooling2Layer->GetInputSlot(0)); |
| 533 | pooling2Layer->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 534 | weightsLayer2->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(1)); |
| 535 | biasLayer2->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(2)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 536 | conv2Layer->GetOutputSlot(0).Connect(pooling3Layer->GetInputSlot(0)); |
| 537 | pooling3Layer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 538 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 539 | std::vector<unsigned int> ignoreSlots = {1, 2}; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 540 | // Create the subgraph view for the whole network |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 541 | return CreateSubgraphViewFrom(CreateInputsFrom(conv1Layer, ignoreSlots), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 542 | CreateOutputsFrom({pooling3Layer}), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 543 | {weightsLayer1, |
| 544 | biasLayer1, |
| 545 | conv1Layer, |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 546 | pooling1Layer, |
| 547 | pooling2Layer, |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 548 | weightsLayer2, |
| 549 | biasLayer2, |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 550 | conv2Layer, |
| 551 | pooling3Layer}); |
| 552 | } |
| 553 | |
| 554 | // Creates a subgraph with only unoptimizable layers ("unoptimizable" is added to the layer's name) |
| 555 | SubgraphView::SubgraphViewPtr BuildFullyUnoptimizableSubgraph1(Graph& graph, LayerNameToLayerMap& layersInGraph) |
| 556 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 557 | const TensorInfo inputInfo ({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
| 558 | const TensorInfo outputInfo({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 559 | TensorInfo weightInfo({ 16, 1, 1, 16 }, DataType::QAsymmU8, 0.9f, 0); |
| 560 | TensorInfo biasInfo ({ 1, 1, 1, 16 }, DataType::Signed32, 0.9f, 0); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 561 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 562 | weightInfo.SetConstant(true); |
| 563 | biasInfo.SetConstant(true); |
| 564 | |
| 565 | std::vector<float> weightsVector(64); |
| 566 | ConstTensor constWeightsTensor(weightInfo, weightsVector); |
| 567 | |
| 568 | std::vector<float> biasVector(16); |
| 569 | ConstTensor constBiasTensor(biasInfo, biasVector); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 570 | Convolution2dDescriptor convolutionDescriptor; |
| 571 | convolutionDescriptor.m_StrideX = 1; |
| 572 | convolutionDescriptor.m_StrideY = 1; |
| 573 | convolutionDescriptor.m_BiasEnabled = true; |
| 574 | convolutionDescriptor.m_DataLayout = DataLayout::NHWC; |
| 575 | |
| 576 | // Construct the graph |
| 577 | Layer* const inputLayer = AddInputLayer(graph, "input layer", inputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 578 | |
| 579 | ConstantLayer* const weightsLayer = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 580 | AddConstantLayer(graph, layersInGraph, "Weights Layer unoptimizable", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 581 | |
| 582 | ConstantLayer* const biasLayer = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 583 | AddConstantLayer(graph, layersInGraph, "Bias Layer unoptimizable", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 584 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 585 | Convolution2dLayer* const convLayer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 586 | "conv layer unoptimizable", outputInfo); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 587 | Layer* const outputLayer = AddOutputLayer(graph, "output layer"); |
| 588 | |
| 589 | // Connect the network |
| 590 | inputLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 591 | weightsLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(1)); |
| 592 | biasLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(2)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 593 | convLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 594 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 595 | std::vector<unsigned int> ignoreSlots = {1, 2}; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 596 | // Create the subgraph view for the whole network |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 597 | return CreateSubgraphViewFrom(CreateInputsFrom(convLayer, ignoreSlots), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 598 | CreateOutputsFrom({convLayer}), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 599 | {convLayer, weightsLayer, biasLayer}); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // Creates a subgraph with some unoptimizable layers ("unoptimizable" is added to the layer's name) |
| 603 | SubgraphView::SubgraphViewPtr BuildPartiallyOptimizableSubgraph1(Graph& graph, LayerNameToLayerMap& layersInGraph) |
| 604 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 605 | const TensorInfo inputInfo ({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
| 606 | const TensorInfo outputInfo({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 607 | TensorInfo weightInfo({ 16, 1, 1, 16 }, DataType::QAsymmU8, 0.9f, 0); |
| 608 | TensorInfo biasInfo ({ 1, 1, 1, 16 }, DataType::Signed32, 0.9f, 0); |
| 609 | |
| 610 | weightInfo.SetConstant(true); |
| 611 | biasInfo.SetConstant(true); |
| 612 | |
| 613 | std::vector<float> weightsVector(64); |
| 614 | ConstTensor constWeightsTensor(weightInfo, weightsVector); |
| 615 | |
| 616 | std::vector<float> biasVector(16); |
| 617 | ConstTensor constBiasTensor(biasInfo, biasVector); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 618 | |
| 619 | Convolution2dDescriptor convolutionDescriptor; |
| 620 | convolutionDescriptor.m_StrideX = 1; |
| 621 | convolutionDescriptor.m_StrideY = 1; |
| 622 | convolutionDescriptor.m_BiasEnabled = true; |
| 623 | convolutionDescriptor.m_DataLayout = DataLayout::NHWC; |
| 624 | |
| 625 | // Construct the graph |
| 626 | Layer* const inputLayer = AddInputLayer(graph, "input layer", inputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 627 | |
| 628 | ConstantLayer* const weightsLayer1 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 629 | AddConstantLayer(graph, layersInGraph, "Weights Layer 1", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 630 | ConstantLayer* const biasLayer1 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 631 | AddConstantLayer(graph, layersInGraph, "Bias Layer 1", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 632 | ConstantLayer* const weightsLayer2 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 633 | AddConstantLayer(graph, layersInGraph, "Weights Layer 2 unoptimizable", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 634 | ConstantLayer* const biasLayer2 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 635 | AddConstantLayer(graph, layersInGraph, "Bias Layer 2 unoptimizable", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 636 | ConstantLayer* const weightsLayer3 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 637 | AddConstantLayer(graph, layersInGraph, "Weights Layer 3", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 638 | ConstantLayer* const biasLayer3 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 639 | AddConstantLayer(graph, layersInGraph, "Bias Layer 3", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 640 | ConstantLayer* const weightsLayer4 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 641 | AddConstantLayer(graph, layersInGraph, "Weights Layer 4 unoptimizable", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 642 | ConstantLayer* const biasLayer4 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 643 | AddConstantLayer(graph, layersInGraph, "Bias Layer 4 unoptimizable", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 644 | ConstantLayer* const weightsLayer5 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 645 | AddConstantLayer(graph, layersInGraph, "Weights Layer 5", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 646 | ConstantLayer* const biasLayer5 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 647 | AddConstantLayer(graph, layersInGraph, "Bias Layer 5", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 648 | |
| 649 | Convolution2dLayer* const conv1Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 650 | "conv1 layer", outputInfo); |
| 651 | Convolution2dLayer* const conv2Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
| 652 | "conv2 layer unoptimizable", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 653 | Convolution2dLayer* const conv3Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 654 | "conv3 layer", outputInfo); |
| 655 | Convolution2dLayer* const conv4Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
| 656 | "conv4 layer unoptimizable", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 657 | Convolution2dLayer* const conv5Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 658 | "conv5 layer", outputInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 659 | |
| 660 | Layer* const outputLayer = AddOutputLayer(graph, "output layer"); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 661 | |
| 662 | // Connect the network |
| 663 | inputLayer->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 664 | weightsLayer1->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(1)); |
| 665 | biasLayer1->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(2)); |
| 666 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 667 | conv1Layer->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 668 | weightsLayer2->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(1)); |
| 669 | biasLayer2->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(2)); |
| 670 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 671 | conv2Layer->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 672 | weightsLayer3->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(1)); |
| 673 | biasLayer3->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(2)); |
| 674 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 675 | conv3Layer->GetOutputSlot(0).Connect(conv4Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 676 | weightsLayer4->GetOutputSlot(0).Connect(conv4Layer->GetInputSlot(1)); |
| 677 | biasLayer4->GetOutputSlot(0).Connect(conv4Layer->GetInputSlot(2)); |
| 678 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 679 | conv4Layer->GetOutputSlot(0).Connect(conv5Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 680 | weightsLayer5->GetOutputSlot(0).Connect(conv5Layer->GetInputSlot(1)); |
| 681 | biasLayer5->GetOutputSlot(0).Connect(conv5Layer->GetInputSlot(2)); |
| 682 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 683 | conv5Layer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 684 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 685 | std::vector<unsigned int> ignoreSlots = {1, 2}; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 686 | // Create the subgraph view for the whole network |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 687 | return CreateSubgraphViewFrom(CreateInputsFrom(conv1Layer, ignoreSlots), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 688 | CreateOutputsFrom({conv5Layer}), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 689 | {weightsLayer1, |
| 690 | biasLayer1, |
| 691 | conv1Layer, |
| 692 | weightsLayer2, |
| 693 | biasLayer2, |
| 694 | conv2Layer, |
| 695 | weightsLayer3, |
| 696 | biasLayer3, |
| 697 | conv3Layer, |
| 698 | weightsLayer4, |
| 699 | biasLayer4, |
| 700 | conv4Layer, |
| 701 | weightsLayer5, |
| 702 | biasLayer5, |
| 703 | conv5Layer}); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | // Creates a subgraph with some input unoptimizable layers ("unoptimizable" is added to the layer's name), |
| 707 | // this is meant to test input slots coming from different layers |
| 708 | SubgraphView::SubgraphViewPtr BuildPartiallyOptimizableSubgraph2(Graph& graph, LayerNameToLayerMap& layersInGraph) |
| 709 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 710 | const TensorInfo inputInfo ({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
| 711 | const TensorInfo outputInfo({ 1, 16, 16, 16 }, DataType::QAsymmU8, 1.0f, 0); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 712 | TensorInfo weightInfo({ 16, 1, 1, 16 }, DataType::QAsymmU8, 0.9f, 0); |
| 713 | TensorInfo biasInfo ({ 1, 1, 1, 16 }, DataType::Signed32, 0.9f, 0); |
| 714 | |
| 715 | weightInfo.SetConstant(true); |
| 716 | biasInfo.SetConstant(true); |
| 717 | |
| 718 | std::vector<float> weightsVector(64); |
| 719 | ConstTensor constWeightsTensor(weightInfo, weightsVector); |
| 720 | |
| 721 | std::vector<float> biasVector(16); |
| 722 | ConstTensor constBiasTensor(biasInfo, biasVector); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 723 | |
| 724 | Convolution2dDescriptor convolutionDescriptor; |
| 725 | convolutionDescriptor.m_StrideX = 1; |
| 726 | convolutionDescriptor.m_StrideY = 1; |
| 727 | convolutionDescriptor.m_BiasEnabled = true; |
| 728 | convolutionDescriptor.m_DataLayout = DataLayout::NHWC; |
| 729 | |
| 730 | // Construct the graph |
| 731 | Layer* const input1Layer = AddInputLayer(graph, "input1 layer", inputInfo, 0); |
| 732 | Layer* const input2Layer = AddInputLayer(graph, "input2 layer", inputInfo, 1); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 733 | |
| 734 | ConstantLayer* const weightsLayer1 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 735 | AddConstantLayer(graph, layersInGraph, "Weights Layer 1", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 736 | ConstantLayer* const biasLayer1 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 737 | AddConstantLayer(graph, layersInGraph, "Bias Layer 1", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 738 | ConstantLayer* const weightsLayer2 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 739 | AddConstantLayer(graph, layersInGraph, "Weights Layer 2 unoptimizable", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 740 | ConstantLayer* const biasLayer2 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 741 | AddConstantLayer(graph, layersInGraph, "Bias Layer 2 unoptimizable", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 742 | ConstantLayer* const weightsLayer3 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 743 | AddConstantLayer(graph, layersInGraph, "Weights Layer 3", constWeightsTensor, weightInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 744 | ConstantLayer* const biasLayer3 = |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 745 | AddConstantLayer(graph, layersInGraph, "Bias Layer 3", constBiasTensor, biasInfo); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 746 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 747 | Convolution2dLayer* const conv1Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 748 | "conv1 layer", outputInfo); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 749 | Convolution2dLayer* const conv2Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 750 | "conv2 layer unoptimizable", outputInfo); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 751 | Convolution2dLayer* const conv3Layer = AddConvolutionLayer(graph, layersInGraph, convolutionDescriptor, |
Mike Kelly | ec67a0f | 2022-11-25 13:55:24 +0000 | [diff] [blame] | 752 | "conv3 layer", outputInfo); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 753 | AdditionLayer* const addLayer = AddAdditionaLayer(graph, layersInGraph, "add layer", outputInfo); |
| 754 | Layer* const outputLayer = AddOutputLayer(graph, "output layer"); |
| 755 | |
| 756 | // Connect the network |
| 757 | input1Layer->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 758 | weightsLayer1->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(1)); |
| 759 | biasLayer1->GetOutputSlot(0).Connect(conv1Layer->GetInputSlot(2)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 760 | conv1Layer->GetOutputSlot(0).Connect(addLayer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 761 | |
| 762 | input2Layer->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(0)); |
| 763 | weightsLayer2->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(1)); |
| 764 | biasLayer2->GetOutputSlot(0).Connect(conv2Layer->GetInputSlot(2)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 765 | conv2Layer->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(0)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 766 | weightsLayer3->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(1)); |
| 767 | biasLayer3->GetOutputSlot(0).Connect(conv3Layer->GetInputSlot(2)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 768 | conv3Layer->GetOutputSlot(0).Connect(addLayer->GetInputSlot(1)); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 769 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 770 | addLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 771 | |
| 772 | // Create the subgraph view for the whole network |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 773 | std::vector<unsigned int> ignoreSlots = {1, 2}; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 774 | return CreateSubgraphViewFrom(CreateInputsFrom({conv1Layer, |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 775 | conv2Layer}, ignoreSlots), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 776 | CreateOutputsFrom({addLayer}), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 777 | { weightsLayer1, |
| 778 | biasLayer1, |
| 779 | weightsLayer2, |
| 780 | biasLayer2, |
| 781 | weightsLayer3, |
| 782 | biasLayer3, |
| 783 | conv1Layer, |
| 784 | conv2Layer, |
| 785 | conv3Layer, |
| 786 | addLayer }); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // The input subgraph contains only a single unsupported layer (only convolutions are unsupported by the mock backend) |
| 790 | void FullyUnsupporteSubgraphTestImpl1() |
| 791 | { |
| 792 | Graph graph; |
| 793 | LayerNameToLayerMap layersInGraph; |
| 794 | |
| 795 | // Create an unsupported subgraph |
| 796 | SubgraphView::SubgraphViewPtr subgraphPtr = BuildFullyUnsupportedSubgraph1(graph, layersInGraph); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 797 | CHECK((subgraphPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 798 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 799 | const SubgraphView::IInputSlots& subgraphInputSlots = subgraphPtr->GetIInputSlots(); |
| 800 | const SubgraphView::IOutputSlots& subgraphOutputSlots = subgraphPtr->GetIOutputSlots(); |
| 801 | const SubgraphView::IConnectableLayers& subgraphLayers = subgraphPtr->GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 802 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 803 | CHECK(subgraphInputSlots.size() == 1); |
| 804 | CHECK(subgraphOutputSlots.size() == 1); |
| 805 | CHECK(subgraphLayers.size() == 1); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 806 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 807 | CHECK(Contains(layersInGraph, "pooling layer")); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 808 | |
| 809 | // Create a mock backend object |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 810 | MockBackendInitialiser initialiser; // Register the Mock Backend |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 811 | auto backendObjPtr = CreateBackendObject(MockBackendId()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 812 | CHECK((backendObjPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 813 | |
| 814 | // Optimize the subgraph |
| 815 | OptimizationViews optimizationViews; |
| 816 | |
| 817 | // Check that the optimization is carried out correctly, but no optimization is performed |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 818 | CHECK_NOTHROW(optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraphPtr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 819 | |
| 820 | // ======================================================================= |
| 821 | // The expected results are: |
| 822 | // - No substitutions |
| 823 | // - Exactly one failed subgraph, corresponding to the whole original one |
| 824 | // - No untouched subgraphs |
| 825 | // ======================================================================= |
| 826 | |
| 827 | // ----------------------- |
| 828 | // Check the substitutions |
| 829 | // ----------------------- |
| 830 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 831 | CHECK(optimizationViews.GetSubstitutions().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 832 | |
| 833 | // -------------------------- |
| 834 | // Check the failed subgraphs |
| 835 | // -------------------------- |
| 836 | |
| 837 | const OptimizationViews::Subgraphs& failedSubgraphs = optimizationViews.GetFailedSubgraphs(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 838 | CHECK(failedSubgraphs.size() == 1); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 839 | |
| 840 | CheckFailedSubgraph(failedSubgraphs.at(0), |
| 841 | { subgraphInputSlots.size(), subgraphOutputSlots.size(), subgraphLayers.size() }, |
| 842 | subgraphInputSlots, |
| 843 | subgraphOutputSlots, |
| 844 | subgraphLayers); |
| 845 | |
| 846 | // ----------------------------- |
| 847 | // Check the untouched subgraphs |
| 848 | // ----------------------------- |
| 849 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 850 | CHECK(optimizationViews.GetUntouchedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // The input subgraph contains only unsupported layers (only convolutions are unsupported by the mock backend) |
| 854 | void FullyUnsupporteSubgraphTestImpl2() |
| 855 | { |
| 856 | Graph graph; |
| 857 | LayerNameToLayerMap layersInGraph; |
| 858 | |
| 859 | // Create an unsupported subgraph |
| 860 | SubgraphView::SubgraphViewPtr subgraphPtr = BuildFullyUnsupportedSubgraph2(graph, layersInGraph); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 861 | CHECK((subgraphPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 862 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 863 | const SubgraphView::IInputSlots& subgraphInputSlots = subgraphPtr->GetIInputSlots(); |
| 864 | const SubgraphView::IOutputSlots& subgraphOutputSlots = subgraphPtr->GetIOutputSlots(); |
| 865 | const SubgraphView::IConnectableLayers& subgraphLayers = subgraphPtr->GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 866 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 867 | CHECK(subgraphInputSlots.size() == 1); |
| 868 | CHECK(subgraphOutputSlots.size() == 1); |
| 869 | CHECK(subgraphLayers.size() == 3); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 870 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 871 | CHECK(Contains(layersInGraph, "pooling1 layer")); |
| 872 | CHECK(Contains(layersInGraph, "pooling2 layer")); |
| 873 | CHECK(Contains(layersInGraph, "pooling3 layer")); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 874 | |
| 875 | // Create a mock backend object |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 876 | MockBackendInitialiser initialiser; // Register the Mock Backend |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 877 | auto backendObjPtr = CreateBackendObject(MockBackendId()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 878 | CHECK((backendObjPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 879 | |
| 880 | // Optimize the subgraph |
| 881 | OptimizationViews optimizationViews; |
| 882 | |
| 883 | // Check that the optimization is carried out correctly, but no optimization is performed |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 884 | CHECK_NOTHROW(optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraphPtr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 885 | |
| 886 | // ======================================================================= |
| 887 | // The expected results are: |
| 888 | // - No substitutions |
| 889 | // - Exactly one failed subgraph, corresponding to the whole original one |
| 890 | // - No untouched subgraphs |
| 891 | // ======================================================================= |
| 892 | |
| 893 | // ----------------------- |
| 894 | // Check the substitutions |
| 895 | // ----------------------- |
| 896 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 897 | CHECK(optimizationViews.GetSubstitutions().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 898 | |
| 899 | // -------------------------- |
| 900 | // Check the failed subgraphs |
| 901 | // -------------------------- |
| 902 | |
| 903 | const OptimizationViews::Subgraphs& failedSubgraphs = optimizationViews.GetFailedSubgraphs(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 904 | CHECK(failedSubgraphs.size() == 1); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 905 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 906 | std::list<IConnectableLayer*> expectedFailedLayers{ layersInGraph.at("pooling1 layer"), |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 907 | layersInGraph.at("pooling2 layer"), |
| 908 | layersInGraph.at("pooling3 layer") }; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 909 | |
| 910 | const SubgraphView& failedSubgraph = failedSubgraphs.at(0); |
| 911 | |
| 912 | CheckFailedSubgraph(failedSubgraph, |
| 913 | { subgraphInputSlots.size(), subgraphOutputSlots.size(), subgraphLayers.size() }, |
| 914 | subgraphInputSlots, |
| 915 | subgraphOutputSlots, |
| 916 | subgraphLayers); |
| 917 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 918 | const SubgraphView::IConnectableLayers& failedSubgraphLayers = failedSubgraph.GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 919 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 920 | CHECK_EQ(failedSubgraphLayers.front() + 0, expectedFailedLayers.front() + 0); |
| 921 | CHECK_EQ(failedSubgraphLayers.front() + 1, expectedFailedLayers.front() + 1); |
| 922 | CHECK_EQ(failedSubgraphLayers.front() + 2, expectedFailedLayers.front() + 2); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 923 | |
| 924 | // ----------------------------- |
| 925 | // Check the untouched subgraphs |
| 926 | // ----------------------------- |
| 927 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 928 | CHECK(optimizationViews.GetUntouchedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | // A simple case with only one layer (convolution) to optimize, supported by the mock backend |
| 932 | void FullyOptimizableSubgraphTestImpl1() |
| 933 | { |
| 934 | Graph graph; |
| 935 | LayerNameToLayerMap layersInGraph; |
| 936 | |
| 937 | // Create a fully optimizable subgraph |
Francis Murtagh | 01f7269 | 2022-12-21 09:10:04 +0000 | [diff] [blame] | 938 | SubgraphView::SubgraphViewPtr subgraphPtr = BuildFullyOptimizableSubgraph1(graph, layersInGraph); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 939 | CHECK((subgraphPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 940 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 941 | const SubgraphView::IInputSlots& subgraphInputSlots = subgraphPtr->GetIInputSlots(); |
| 942 | const SubgraphView::IOutputSlots& subgraphOutputSlots = subgraphPtr->GetIOutputSlots(); |
| 943 | const SubgraphView::IConnectableLayers& subgraphLayers = subgraphPtr->GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 944 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 945 | CHECK(subgraphInputSlots.size() == 1); |
| 946 | CHECK(subgraphOutputSlots.size() == 1); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 947 | CHECK(subgraphLayers.size() == 3); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 948 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 949 | CHECK(Contains(layersInGraph, "conv layer")); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 950 | CHECK(Contains(layersInGraph, "Weights Layer")); |
| 951 | CHECK(Contains(layersInGraph, "Bias Layer")); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 952 | |
| 953 | // Create a mock backend object |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 954 | MockBackendInitialiser initialiser; // Register the Mock Backend |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 955 | auto backendObjPtr = CreateBackendObject(MockBackendId()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 956 | CHECK((backendObjPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 957 | |
| 958 | // Optimize the subgraph |
| 959 | OptimizationViews optimizationViews; |
| 960 | |
| 961 | // Check that the optimization is carried out correctly |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 962 | CHECK_NOTHROW(optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraphPtr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 963 | |
| 964 | // =========================================================================================== |
| 965 | // The expected results are: |
| 966 | // - Exactly one substitution, mapping the whole input subgraph to a new replacement subgraph |
| 967 | // - No failed subgraphs |
| 968 | // - No untouched subgraphs |
| 969 | // =========================================================================================== |
| 970 | |
| 971 | // ----------------------- |
| 972 | // Check the substitutions |
| 973 | // ----------------------- |
| 974 | |
| 975 | const OptimizationViews::Substitutions& substitutions = optimizationViews.GetSubstitutions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 976 | CHECK(substitutions.size() == 1); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 977 | |
| 978 | CheckSubstitution(substitutions.at(0), |
| 979 | { subgraphInputSlots.size(), subgraphOutputSlots.size(), subgraphLayers.size() }, |
| 980 | { subgraphInputSlots.size(), subgraphOutputSlots.size(), 1 }, |
| 981 | subgraphInputSlots, |
| 982 | subgraphOutputSlots, |
| 983 | subgraphLayers); |
| 984 | |
| 985 | // -------------------------- |
| 986 | // Check the failed subgraphs |
| 987 | // -------------------------- |
| 988 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 989 | CHECK(optimizationViews.GetFailedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 990 | |
| 991 | // ----------------------------- |
| 992 | // Check the untouched subgraphs |
| 993 | // ----------------------------- |
| 994 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 995 | CHECK(optimizationViews.GetUntouchedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | // A case with five layers (all convolutions) to optimize, all supported by the mock backend |
| 999 | void FullyOptimizableSubgraphTestImpl2() |
| 1000 | { |
| 1001 | Graph graph; |
| 1002 | LayerNameToLayerMap layersInGraph; |
| 1003 | |
| 1004 | // Create a fully optimizable subgraph |
Francis Murtagh | 01f7269 | 2022-12-21 09:10:04 +0000 | [diff] [blame] | 1005 | SubgraphView::SubgraphViewPtr subgraphPtr = BuildFullyOptimizableSubgraph2(graph, layersInGraph); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1006 | CHECK((subgraphPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1007 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1008 | const SubgraphView::IInputSlots& subgraphInputSlots = subgraphPtr->GetIInputSlots(); |
| 1009 | const SubgraphView::IOutputSlots& subgraphOutputSlots = subgraphPtr->GetIOutputSlots(); |
| 1010 | const SubgraphView::IConnectableLayers& subgraphLayers = subgraphPtr->GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1011 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1012 | CHECK(subgraphInputSlots.size() == 1); |
| 1013 | CHECK(subgraphOutputSlots.size() == 1); |
| 1014 | CHECK(subgraphPtr->GetIConnectableLayers().size() == 15); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1015 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1016 | CHECK(Contains(layersInGraph, "conv1 layer")); |
| 1017 | CHECK(Contains(layersInGraph, "conv2 layer")); |
| 1018 | CHECK(Contains(layersInGraph, "conv3 layer")); |
| 1019 | CHECK(Contains(layersInGraph, "conv4 layer")); |
| 1020 | CHECK(Contains(layersInGraph, "conv5 layer")); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1021 | CHECK(Contains(layersInGraph, "Weights Layer 1")); |
| 1022 | CHECK(Contains(layersInGraph, "Weights Layer 2")); |
| 1023 | CHECK(Contains(layersInGraph, "Weights Layer 3")); |
| 1024 | CHECK(Contains(layersInGraph, "Weights Layer 4")); |
| 1025 | CHECK(Contains(layersInGraph, "Weights Layer 5")); |
| 1026 | CHECK(Contains(layersInGraph, "Bias Layer 1")); |
| 1027 | CHECK(Contains(layersInGraph, "Bias Layer 2")); |
| 1028 | CHECK(Contains(layersInGraph, "Bias Layer 3")); |
| 1029 | CHECK(Contains(layersInGraph, "Bias Layer 4")); |
| 1030 | CHECK(Contains(layersInGraph, "Bias Layer 5")); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1031 | |
| 1032 | // Create a mock backend object |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 1033 | MockBackendInitialiser initialiser; // Register the Mock Backend |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1034 | auto backendObjPtr = CreateBackendObject(MockBackendId()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1035 | CHECK((backendObjPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1036 | |
| 1037 | // Optimize the subgraph |
| 1038 | OptimizationViews optimizationViews; |
| 1039 | |
| 1040 | // Check that the optimization is carried out correctly |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1041 | CHECK_NOTHROW(optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraphPtr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1042 | |
| 1043 | // =========================================================================================== |
| 1044 | // The expected results are: |
| 1045 | // - Exactly one substitution, mapping the whole input subgraph to a new replacement subgraph |
| 1046 | // - No failed subgraphs |
| 1047 | // - No untouched subgraphs |
| 1048 | // =========================================================================================== |
| 1049 | |
| 1050 | // ----------------------- |
| 1051 | // Check the substitutions |
| 1052 | // ----------------------- |
| 1053 | |
| 1054 | const OptimizationViews::Substitutions& substitutions = optimizationViews.GetSubstitutions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1055 | CHECK(substitutions.size() == 1); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1056 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1057 | std::list<IConnectableLayer*> expectedSubstitutableLayers{ |
| 1058 | layersInGraph.at("Weights Layer 1"), |
| 1059 | layersInGraph.at("Weights Layer 2"), |
| 1060 | layersInGraph.at("Weights Layer 3"), |
| 1061 | layersInGraph.at("Weights Layer 4"), |
| 1062 | layersInGraph.at("Weights Layer 5"), |
| 1063 | layersInGraph.at("Bias Layer 1"), |
| 1064 | layersInGraph.at("Bias Layer 2"), |
| 1065 | layersInGraph.at("Bias Layer 3"), |
| 1066 | layersInGraph.at("Bias Layer 4"), |
| 1067 | layersInGraph.at("Bias Layer 5"), |
| 1068 | layersInGraph.at("conv1 layer"), |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1069 | layersInGraph.at("conv2 layer"), |
| 1070 | layersInGraph.at("conv3 layer"), |
| 1071 | layersInGraph.at("conv4 layer"), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1072 | layersInGraph.at("conv5 layer")}; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1073 | |
| 1074 | const OptimizationViews::SubstitutionPair& substitution = substitutions.at(0); |
| 1075 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1076 | CheckSubstitution( |
| 1077 | substitution, |
| 1078 | {subgraphInputSlots.size(), subgraphOutputSlots.size(), |
| 1079 | subgraphLayers.size()}, |
| 1080 | {subgraphInputSlots.size(), subgraphOutputSlots.size(), 1}, |
| 1081 | subgraphInputSlots, subgraphOutputSlots, expectedSubstitutableLayers); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1082 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1083 | const SubgraphView::IConnectableLayers& substitutableSubgraphLayers = |
| 1084 | substitution.m_SubstitutableSubgraph.GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1085 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1086 | CHECK_EQ(substitutableSubgraphLayers.front() + 0, expectedSubstitutableLayers.front() + 0); |
| 1087 | CHECK_EQ(substitutableSubgraphLayers.front() + 1, expectedSubstitutableLayers.front() + 1); |
| 1088 | CHECK_EQ(substitutableSubgraphLayers.front() + 2, expectedSubstitutableLayers.front() + 2); |
| 1089 | CHECK_EQ(substitutableSubgraphLayers.front() + 3, expectedSubstitutableLayers.front() + 3); |
| 1090 | CHECK_EQ(substitutableSubgraphLayers.front() + 4, expectedSubstitutableLayers.front() + 4); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1091 | |
| 1092 | // -------------------------- |
| 1093 | // Check the failed subgraphs |
| 1094 | // -------------------------- |
| 1095 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1096 | CHECK(optimizationViews.GetFailedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1097 | |
| 1098 | // ----------------------------- |
| 1099 | // Check the untouched subgraphs |
| 1100 | // ----------------------------- |
| 1101 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1102 | CHECK(optimizationViews.GetUntouchedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | // The input subgraph contaions both supported and unsupported layers |
| 1106 | // (but only convolutions are unsupported by the mock backend) |
| 1107 | void PartiallySupportedSubgraphTestImpl() |
| 1108 | { |
| 1109 | Graph graph; |
| 1110 | LayerNameToLayerMap layersInGraph; |
| 1111 | |
| 1112 | // Create a fully optimizable subgraph |
Francis Murtagh | 01f7269 | 2022-12-21 09:10:04 +0000 | [diff] [blame] | 1113 | SubgraphView::SubgraphViewPtr subgraphPtr = BuildPartiallySupportedSubgraph(graph, layersInGraph); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1114 | CHECK((subgraphPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1115 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1116 | const SubgraphView::IInputSlots& subgraphInputSlots = subgraphPtr->GetIInputSlots(); |
| 1117 | const SubgraphView::IOutputSlots& subgraphOutputSlots = subgraphPtr->GetIOutputSlots(); |
| 1118 | const SubgraphView::IConnectableLayers& subgraphLayers = subgraphPtr->GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1119 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1120 | CHECK(subgraphInputSlots.size() == 1); |
| 1121 | CHECK(subgraphOutputSlots.size() == 1); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1122 | CHECK(subgraphLayers.size() == 9); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1123 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1124 | CHECK(Contains(layersInGraph, "Weights Layer 1")); |
| 1125 | CHECK(Contains(layersInGraph, "Bias Layer 1")); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1126 | CHECK(Contains(layersInGraph, "conv1 layer")); |
| 1127 | CHECK(Contains(layersInGraph, "pooling1 layer")); |
| 1128 | CHECK(Contains(layersInGraph, "pooling2 layer")); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1129 | CHECK(Contains(layersInGraph, "Weights Layer 2")); |
| 1130 | CHECK(Contains(layersInGraph, "Bias Layer 2")); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1131 | CHECK(Contains(layersInGraph, "conv2 layer")); |
| 1132 | CHECK(Contains(layersInGraph, "pooling3 layer")); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1133 | |
| 1134 | // Create a mock backend object |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 1135 | MockBackendInitialiser initialiser; // Register the Mock Backend |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1136 | auto backendObjPtr = CreateBackendObject(MockBackendId()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1137 | CHECK((backendObjPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1138 | |
| 1139 | // Optimize the subgraph |
| 1140 | OptimizationViews optimizationViews; |
| 1141 | |
| 1142 | // Check that the optimization is carried out correctly |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1143 | CHECK_NOTHROW(optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraphPtr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1144 | |
| 1145 | // ======================================================================== |
| 1146 | // The expected results are: |
| 1147 | // - Exactly two substitution, corresponding to the supported layers |
| 1148 | // - Exactly two failed subgraphs, corresponding to the unsupported layers |
| 1149 | // - No untouched subgraphs |
| 1150 | // ======================================================================== |
| 1151 | |
| 1152 | // ----------------------- |
| 1153 | // Check the substitutions |
| 1154 | // ----------------------- |
| 1155 | |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1156 | OptimizationViews::Substitutions substitutions = optimizationViews.GetSubstitutions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1157 | CHECK(substitutions.size() == 2); |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1158 | // Sort into a consistent order |
| 1159 | std::sort(substitutions.begin(), substitutions.end(), [](auto s1, auto s2) { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1160 | return strcmp(s1.m_SubstitutableSubgraph.GetIConnectableLayers().front()->GetName(), |
| 1161 | s2.m_SubstitutableSubgraph.GetIConnectableLayers().front()->GetName()) < 0; |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1162 | }); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1163 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1164 | std::vector<ExpectedSubgraphSize> expectedSubstitutableSubgraphSizes{ { 1, 1, 3 }, |
| 1165 | { 1, 1, 3 } }; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1166 | std::vector<ExpectedSubgraphSize> expectedReplacementSubgraphSizes{ { 1, 1, 1 }, |
| 1167 | { 1, 1, 1 } }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1168 | std::vector<SubgraphView::IInputSlots> expectedSubstitutableInputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1169 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1170 | ConvertSlotsToISlots<InputSlot, IInputSlot>( |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1171 | {ConvertReferenceTypeToPointerType(layersInGraph.at("conv1 layer")->GetInputSlot(0))}), |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1172 | ConvertSlotsToISlots<InputSlot, IInputSlot>( |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1173 | {ConvertReferenceTypeToPointerType(layersInGraph.at("conv2 layer")->GetInputSlot(0))}) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1174 | }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1175 | |
| 1176 | std::vector<SubgraphView::IOutputSlots> expectedSubstitutableOutputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1177 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1178 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1179 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv1 layer")->GetOutputSlots())), |
| 1180 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1181 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv2 layer")->GetOutputSlots())) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1182 | }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1183 | std::vector<SubgraphView::IConnectableLayers> expectedSubstitutableLayers |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1184 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1185 | { layersInGraph.at("Weights Layer 1"), layersInGraph.at("Bias Layer 1"), layersInGraph.at("conv1 layer") }, |
| 1186 | { layersInGraph.at("Weights Layer 2"), layersInGraph.at("Bias Layer 2"), layersInGraph.at("conv2 layer") } |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1187 | }; |
| 1188 | |
| 1189 | for (size_t substitutionIndex = 0; substitutionIndex < substitutions.size(); substitutionIndex++) |
| 1190 | { |
| 1191 | CheckSubstitution(substitutions.at(substitutionIndex), |
| 1192 | expectedSubstitutableSubgraphSizes.at(substitutionIndex), |
| 1193 | expectedReplacementSubgraphSizes.at(substitutionIndex), |
| 1194 | expectedSubstitutableInputSlots.at(substitutionIndex), |
| 1195 | expectedSubstitutableOutputSlots.at(substitutionIndex), |
| 1196 | expectedSubstitutableLayers.at(substitutionIndex)); |
| 1197 | } |
| 1198 | |
| 1199 | // -------------------------- |
| 1200 | // Check the failed subgraphs |
| 1201 | // -------------------------- |
| 1202 | |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1203 | OptimizationViews::Subgraphs failedSubgraphs = optimizationViews.GetFailedSubgraphs(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1204 | CHECK(failedSubgraphs.size() == 2); |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1205 | // Sort into a consistent order |
| 1206 | std::sort(failedSubgraphs.begin(), failedSubgraphs.end(), [](auto s1, auto s2) { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1207 | return strcmp(s1.GetIConnectableLayers().front()->GetName(), |
| 1208 | s2.GetIConnectableLayers().front()->GetName()) < 0; |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1209 | }); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1210 | |
| 1211 | std::vector<ExpectedSubgraphSize> expectedFailedSubgraphSizes{ { 1, 1, 2 }, |
| 1212 | { 1, 1, 1 } }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1213 | std::vector<SubgraphView::IInputSlots> expectedFailedInputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1214 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1215 | ConvertSlotsToISlots<InputSlot, IInputSlot>( |
| 1216 | ConvertReferenceTypeToPointerType(layersInGraph.at("pooling1 layer")->GetInputSlots())), |
| 1217 | ConvertSlotsToISlots<InputSlot, IInputSlot>( |
| 1218 | ConvertReferenceTypeToPointerType(layersInGraph.at("pooling3 layer")->GetInputSlots())) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1219 | }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1220 | std::vector<SubgraphView::IOutputSlots> expectedFailedOutputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1221 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1222 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1223 | ConvertReferenceTypeToPointerType(layersInGraph.at("pooling2 layer")->GetOutputSlots())), |
| 1224 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1225 | ConvertReferenceTypeToPointerType(layersInGraph.at("pooling3 layer")->GetOutputSlots())) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1226 | }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1227 | std::vector<SubgraphView::IConnectableLayers> expectedFailedLayers |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1228 | { |
| 1229 | { layersInGraph.at("pooling1 layer"), |
| 1230 | layersInGraph.at("pooling2 layer") }, |
| 1231 | { layersInGraph.at("pooling3 layer") } |
| 1232 | }; |
| 1233 | |
| 1234 | for (size_t failedIndex = 0; failedIndex < failedSubgraphs.size(); failedIndex++) |
| 1235 | { |
| 1236 | CheckFailedSubgraph(failedSubgraphs.at(failedIndex), |
| 1237 | expectedFailedSubgraphSizes.at(failedIndex), |
| 1238 | expectedFailedInputSlots.at(failedIndex), |
| 1239 | expectedFailedOutputSlots.at(failedIndex), |
| 1240 | expectedFailedLayers.at(failedIndex)); |
| 1241 | } |
| 1242 | |
| 1243 | // ----------------------------- |
| 1244 | // Check the untouched subgraphs |
| 1245 | // ----------------------------- |
| 1246 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1247 | CHECK(optimizationViews.GetUntouchedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | // The input subgraph contains only unoptimizable layers ("unoptimizable" is added to the layer's name) |
| 1251 | void FullyUnoptimizableSubgraphTestImpl1() |
| 1252 | { |
| 1253 | Graph graph; |
| 1254 | LayerNameToLayerMap layersInGraph; |
| 1255 | |
| 1256 | // Create a fully optimizable subgraph |
Francis Murtagh | 01f7269 | 2022-12-21 09:10:04 +0000 | [diff] [blame] | 1257 | SubgraphView::SubgraphViewPtr subgraphPtr = BuildFullyUnoptimizableSubgraph1(graph, layersInGraph); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1258 | CHECK((subgraphPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1259 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1260 | const SubgraphView::IInputSlots& subgraphInputSlots = subgraphPtr->GetIInputSlots(); |
| 1261 | const SubgraphView::IOutputSlots& subgraphOutputSlots = subgraphPtr->GetIOutputSlots(); |
| 1262 | const SubgraphView::IConnectableLayers& subgraphLayers = subgraphPtr->GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1263 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1264 | CHECK(subgraphInputSlots.size() == 1); |
| 1265 | CHECK(subgraphOutputSlots.size() == 1); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1266 | CHECK(subgraphLayers.size() == 3); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1267 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1268 | CHECK(Contains(layersInGraph, "conv layer unoptimizable")); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1269 | |
| 1270 | // Create a mock backend object |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 1271 | MockBackendInitialiser initialiser; // Register the Mock Backend |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1272 | auto backendObjPtr = CreateBackendObject(MockBackendId()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1273 | CHECK((backendObjPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1274 | |
| 1275 | // Optimize the subgraph |
| 1276 | OptimizationViews optimizationViews; |
| 1277 | |
| 1278 | // Check that the optimization is carried out correctly |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1279 | CHECK_NOTHROW(optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraphPtr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1280 | |
| 1281 | // ============================================================================ |
| 1282 | // The expected results are: |
| 1283 | // - No substitutions |
| 1284 | // - No failed subgraphs |
| 1285 | // - Exactly one untouched subgraph, corresponding to the whole input subgraph |
| 1286 | // ============================================================================ |
| 1287 | |
| 1288 | // ----------------------- |
| 1289 | // Check the substitutions |
| 1290 | // ----------------------- |
| 1291 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1292 | CHECK(optimizationViews.GetSubstitutions().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1293 | |
| 1294 | // -------------------------- |
| 1295 | // Check the failed subgraphs |
| 1296 | // -------------------------- |
| 1297 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1298 | CHECK(optimizationViews.GetFailedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1299 | |
| 1300 | // ----------------------------- |
| 1301 | // Check the untouched subgraphs |
| 1302 | // ----------------------------- |
| 1303 | |
| 1304 | const OptimizationViews::Subgraphs& untouchedSubgraphs = optimizationViews.GetUntouchedSubgraphs(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1305 | CHECK(untouchedSubgraphs.size() == 1); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1306 | |
| 1307 | CheckUntouchedSubgraph(untouchedSubgraphs.at(0), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1308 | {subgraphInputSlots.size(), |
| 1309 | subgraphOutputSlots.size(), subgraphLayers.size()}, |
| 1310 | subgraphInputSlots, subgraphOutputSlots, |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1311 | subgraphLayers); |
| 1312 | } |
| 1313 | |
| 1314 | // The input subgraph contains some unoptimizable layers ("unoptimizable" is added to the layer's name) |
| 1315 | void PartiallyOptimizableSubgraphTestImpl1() |
| 1316 | { |
| 1317 | Graph graph; |
| 1318 | LayerNameToLayerMap layersInGraph; |
| 1319 | |
| 1320 | // Create a fully optimizable subgraph |
Francis Murtagh | 01f7269 | 2022-12-21 09:10:04 +0000 | [diff] [blame] | 1321 | SubgraphView::SubgraphViewPtr subgraphPtr = BuildPartiallyOptimizableSubgraph1(graph, layersInGraph); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1322 | CHECK((subgraphPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1323 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1324 | const SubgraphView::IInputSlots& subgraphInputSlots = subgraphPtr->GetIInputSlots(); |
| 1325 | const SubgraphView::IOutputSlots& subgraphOutputSlots = subgraphPtr->GetIOutputSlots(); |
| 1326 | const SubgraphView::IConnectableLayers& subgraphLayers = subgraphPtr->GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1327 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1328 | CHECK(subgraphInputSlots.size() == 1); |
| 1329 | CHECK(subgraphOutputSlots.size() == 1); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1330 | CHECK(subgraphLayers.size() == 15); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1331 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1332 | CHECK(Contains(layersInGraph, "conv1 layer")); |
| 1333 | CHECK(Contains(layersInGraph, "conv2 layer unoptimizable")); |
| 1334 | CHECK(Contains(layersInGraph, "conv3 layer")); |
| 1335 | CHECK(Contains(layersInGraph, "conv4 layer unoptimizable")); |
| 1336 | CHECK(Contains(layersInGraph, "conv5 layer")); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1337 | |
| 1338 | // Create a mock backend object |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 1339 | MockBackendInitialiser initialiser; // Register the Mock Backend |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1340 | auto backendObjPtr = CreateBackendObject(MockBackendId()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1341 | CHECK((backendObjPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1342 | |
| 1343 | // Optimize the subgraph |
| 1344 | OptimizationViews optimizationViews; |
| 1345 | |
| 1346 | // Check that the optimization is carried out correctly |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1347 | CHECK_NOTHROW(optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraphPtr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1348 | |
| 1349 | // =============================================================================== |
| 1350 | // The expected results are: |
| 1351 | // - Exactly three substitutions, corresponding to the optimizable layers |
| 1352 | // - No failed subgraphs |
| 1353 | // - Exactly two untouched subgraphs, corresponding to the non-optimizable layers |
| 1354 | // =============================================================================== |
| 1355 | |
| 1356 | // ----------------------- |
| 1357 | // Check the substitutions |
| 1358 | // ----------------------- |
| 1359 | |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1360 | OptimizationViews::Substitutions substitutions = optimizationViews.GetSubstitutions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1361 | CHECK(substitutions.size() == 3); |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1362 | // Sort into a consistent order |
| 1363 | std::sort(substitutions.begin(), substitutions.end(), |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1364 | [](auto s1, auto s2) |
| 1365 | { return strcmp(s1.m_SubstitutableSubgraph.GetIConnectableLayers().front()->GetName(), |
| 1366 | s2.m_SubstitutableSubgraph.GetIConnectableLayers().front()->GetName()) < 0; }); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1367 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1368 | std::vector<ExpectedSubgraphSize> expectedSubstitutableSubgraphSizes{ { 1, 1, 3 }, |
| 1369 | { 1, 1, 3 }, |
| 1370 | { 1, 1, 3 } }; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1371 | std::vector<ExpectedSubgraphSize> expectedReplacementSubgraphSizes{ { 1, 1, 1 }, |
| 1372 | { 1, 1, 1 }, |
| 1373 | { 1, 1, 1 } }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1374 | std::vector<SubgraphView::IInputSlots> expectedSubstitutableInputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1375 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1376 | ConvertSlotsToISlots<InputSlot, IInputSlot>( |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1377 | {ConvertReferenceTypeToPointerType(layersInGraph.at("conv1 layer")->GetInputSlot(0))}), |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1378 | ConvertSlotsToISlots<InputSlot, IInputSlot>( |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1379 | {ConvertReferenceTypeToPointerType(layersInGraph.at("conv3 layer")->GetInputSlot(0))}), |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1380 | ConvertSlotsToISlots<InputSlot, IInputSlot>( |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1381 | {ConvertReferenceTypeToPointerType(layersInGraph.at("conv5 layer")->GetInputSlot(0))}) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1382 | }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1383 | std::vector<SubgraphView::IOutputSlots> expectedSubstitutableOutputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1384 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1385 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1386 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv1 layer")->GetOutputSlots())), |
| 1387 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1388 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv3 layer")->GetOutputSlots())), |
| 1389 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1390 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv5 layer")->GetOutputSlots())) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1391 | }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1392 | std::vector<SubgraphView::IConnectableLayers> expectedSubstitutableLayers |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1393 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1394 | { layersInGraph.at("Weights Layer 1"), layersInGraph.at("Bias Layer 1"), layersInGraph.at("conv1 layer") }, |
| 1395 | { layersInGraph.at("Weights Layer 3"), layersInGraph.at("Bias Layer 3"), layersInGraph.at("conv3 layer") }, |
| 1396 | { layersInGraph.at("Weights Layer 5"), layersInGraph.at("Bias Layer 5"), layersInGraph.at("conv5 layer") } |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1397 | }; |
| 1398 | |
| 1399 | for (size_t substitutionIndex = 0; substitutionIndex < substitutions.size(); substitutionIndex++) |
| 1400 | { |
| 1401 | CheckSubstitution(substitutions.at(substitutionIndex), |
| 1402 | expectedSubstitutableSubgraphSizes.at(substitutionIndex), |
| 1403 | expectedReplacementSubgraphSizes.at(substitutionIndex), |
| 1404 | expectedSubstitutableInputSlots.at(substitutionIndex), |
| 1405 | expectedSubstitutableOutputSlots.at(substitutionIndex), |
| 1406 | expectedSubstitutableLayers.at(substitutionIndex)); |
| 1407 | } |
| 1408 | |
| 1409 | // -------------------------- |
| 1410 | // Check the failed subgraphs |
| 1411 | // -------------------------- |
| 1412 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1413 | CHECK(optimizationViews.GetFailedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1414 | |
| 1415 | // ----------------------------- |
| 1416 | // Check the untouched subgraphs |
| 1417 | // ----------------------------- |
| 1418 | |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1419 | OptimizationViews::Subgraphs untouchedSubgraphs = optimizationViews.GetUntouchedSubgraphs(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1420 | CHECK(untouchedSubgraphs.size() == 2); |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1421 | // Sort into a consistent order |
| 1422 | std::sort(untouchedSubgraphs.begin(), untouchedSubgraphs.end(), [](auto s1, auto s2) { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1423 | return strcmp(s1.GetIConnectableLayers().front()->GetName(), |
| 1424 | s2.GetIConnectableLayers().front()->GetName()) < 0; |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1425 | }); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1426 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1427 | std::vector<ExpectedSubgraphSize> expectedUntouchedSubgraphSizes{ { 1, 1, 3 }, |
| 1428 | { 1, 1, 3 } }; |
| 1429 | std::vector<SubgraphView::IInputSlots> expectedUntouchedInputSlots{ |
| 1430 | ConvertSlotsToISlots<InputSlot, |
| 1431 | IInputSlot>({ConvertReferenceTypeToPointerType( |
| 1432 | layersInGraph.at("conv2 layer unoptimizable")->GetInputSlot(0))}), |
| 1433 | ConvertSlotsToISlots<InputSlot, |
| 1434 | IInputSlot>({ConvertReferenceTypeToPointerType( |
| 1435 | layersInGraph.at("conv4 layer unoptimizable")->GetInputSlot(0))})}; |
| 1436 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1437 | std::vector<SubgraphView::IOutputSlots> expectedUntouchedOutputSlots |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1438 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1439 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1440 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv2 layer unoptimizable")->GetOutputSlots())), |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1441 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1442 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv4 layer unoptimizable")->GetOutputSlots())) |
| 1443 | }; |
| 1444 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1445 | std::vector<SubgraphView::IConnectableLayers> expectedUntouchedLayers |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1446 | { |
| 1447 | { layersInGraph.at("Weights Layer 2 unoptimizable"), |
| 1448 | layersInGraph.at("Bias Layer 2 unoptimizable"), |
| 1449 | layersInGraph.at("conv2 layer unoptimizable") }, |
| 1450 | { layersInGraph.at("Weights Layer 4 unoptimizable"), |
| 1451 | layersInGraph.at("Bias Layer 4 unoptimizable"), |
| 1452 | layersInGraph.at("conv4 layer unoptimizable") } |
| 1453 | }; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1454 | |
| 1455 | for (size_t untouchedIndex = 0; untouchedIndex < untouchedSubgraphs.size(); untouchedIndex++) |
| 1456 | { |
| 1457 | CheckUntouchedSubgraph(untouchedSubgraphs.at(untouchedIndex), |
| 1458 | expectedUntouchedSubgraphSizes.at(untouchedIndex), |
| 1459 | expectedUntouchedInputSlots.at(untouchedIndex), |
| 1460 | expectedUntouchedOutputSlots.at(untouchedIndex), |
| 1461 | expectedUntouchedLayers.at(untouchedIndex)); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | // The input subgraph contains some unoptimizable layers ("unoptimizable" is added to the layer's name), |
| 1466 | // this is meant to test input slots coming from different layers |
| 1467 | void PartiallyOptimizableSubgraphTestImpl2() |
| 1468 | { |
| 1469 | Graph graph; |
| 1470 | LayerNameToLayerMap layersInGraph; |
| 1471 | |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1472 | // Create a partially optimizable subgraph |
Francis Murtagh | 01f7269 | 2022-12-21 09:10:04 +0000 | [diff] [blame] | 1473 | SubgraphView::SubgraphViewPtr subgraphPtr = BuildPartiallyOptimizableSubgraph2(graph, layersInGraph); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1474 | CHECK((subgraphPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1475 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1476 | const SubgraphView::IInputSlots& subgraphInputSlots = subgraphPtr->GetIInputSlots(); |
| 1477 | const SubgraphView::IOutputSlots& subgraphOutputSlots = subgraphPtr->GetIOutputSlots(); |
| 1478 | const SubgraphView::IConnectableLayers& subgraphLayers = subgraphPtr->GetIConnectableLayers(); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1479 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1480 | CHECK(subgraphInputSlots.size() == 2); |
| 1481 | CHECK(subgraphOutputSlots.size() == 1); |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1482 | CHECK(subgraphLayers.size() == 10); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1483 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1484 | CHECK(Contains(layersInGraph, "conv1 layer")); |
| 1485 | CHECK(Contains(layersInGraph, "conv2 layer unoptimizable")); |
| 1486 | CHECK(Contains(layersInGraph, "conv3 layer")); |
| 1487 | CHECK(Contains(layersInGraph, "add layer")); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1488 | |
| 1489 | // Create a mock backend object |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 1490 | MockBackendInitialiser initialiser; // Register the Mock Backend |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1491 | auto backendObjPtr = CreateBackendObject(MockBackendId()); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1492 | CHECK((backendObjPtr != nullptr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1493 | |
| 1494 | // Optimize the subgraph |
| 1495 | OptimizationViews optimizationViews; |
| 1496 | |
| 1497 | // Check that the optimization is carried out correctly |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1498 | CHECK_NOTHROW(optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraphPtr)); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1499 | |
| 1500 | // ============================================================================== |
| 1501 | // The expected results are: |
| 1502 | // - Exactly one substitution, corresponding to the optimizable layers |
| 1503 | // - No failed subgraphs |
| 1504 | // - Exactly two untouched subgraphs, corresponding to the non-optimizable layer |
| 1505 | // ============================================================================== |
| 1506 | |
| 1507 | // ----------------------- |
| 1508 | // Check the substitutions |
| 1509 | // ----------------------- |
| 1510 | |
| 1511 | const OptimizationViews::Substitutions& substitutions = optimizationViews.GetSubstitutions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1512 | CHECK(substitutions.size() == 1); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1513 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1514 | ExpectedSubgraphSize expectedSubstitutableSubgraphSizes{ 2, 1, 7 }; |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1515 | ExpectedSubgraphSize expectedReplacementSubgraphSizes{ 2, 1, 1 }; |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1516 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1517 | SubgraphView::IInputSlots expectedSubstitutableInputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1518 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1519 | ConvertSlotsToISlots<InputSlot, IInputSlot>({ |
| 1520 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv1 layer")->GetInputSlots()[0])})[0], |
| 1521 | ConvertSlotsToISlots<InputSlot, IInputSlot>({ |
| 1522 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv3 layer")->GetInputSlots()[0])})[0] |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1523 | }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1524 | |
| 1525 | SubgraphView::IOutputSlots expectedSubstitutableOutputSlots |
| 1526 | { |
| 1527 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1528 | ConvertReferenceTypeToPointerType(layersInGraph.at("add layer")->GetOutputSlots())) |
| 1529 | }; |
| 1530 | |
| 1531 | SubgraphView::IConnectableLayers expectedSubstitutableLayers |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1532 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1533 | layersInGraph.at("Weights Layer 1"), |
| 1534 | layersInGraph.at("Weights Layer 3"), |
| 1535 | layersInGraph.at("Bias Layer 1"), |
| 1536 | layersInGraph.at("Bias Layer 3"), |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1537 | layersInGraph.at("conv1 layer"), |
| 1538 | layersInGraph.at("conv3 layer"), |
| 1539 | layersInGraph.at("add layer") |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1540 | }; |
| 1541 | |
Rob Hughes | 30db8ad | 2019-11-08 15:50:10 +0000 | [diff] [blame] | 1542 | CheckSubstitution(substitutions[0], |
| 1543 | expectedSubstitutableSubgraphSizes, |
| 1544 | expectedReplacementSubgraphSizes, |
| 1545 | expectedSubstitutableInputSlots, |
| 1546 | expectedSubstitutableOutputSlots, |
| 1547 | expectedSubstitutableLayers); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1548 | |
| 1549 | // -------------------------- |
| 1550 | // Check the failed subgraphs |
| 1551 | // -------------------------- |
| 1552 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1553 | CHECK(optimizationViews.GetFailedSubgraphs().empty()); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1554 | |
| 1555 | // ----------------------------- |
| 1556 | // Check the untouched subgraphs |
| 1557 | // ----------------------------- |
| 1558 | |
| 1559 | const OptimizationViews::Subgraphs& untouchedSubgraphs = optimizationViews.GetUntouchedSubgraphs(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1560 | CHECK(untouchedSubgraphs.size() == 1); |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1561 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1562 | std::vector<ExpectedSubgraphSize> expectedUntouchedSubgraphSizes{ { 1, 1, 3 } }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1563 | std::vector<SubgraphView::IInputSlots> expectedUntouchedInputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1564 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1565 | ConvertSlotsToISlots<InputSlot, |
| 1566 | IInputSlot>({ConvertReferenceTypeToPointerType( |
| 1567 | layersInGraph.at("conv2 layer unoptimizable")->GetInputSlot(0))})}; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1568 | std::vector<SubgraphView::IOutputSlots> expectedUntouchedOutputSlots |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1569 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1570 | ConvertSlotsToISlots<OutputSlot, IOutputSlot>( |
| 1571 | ConvertReferenceTypeToPointerType(layersInGraph.at("conv2 layer unoptimizable")->GetOutputSlots())) |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1572 | }; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1573 | std::vector<SubgraphView::IConnectableLayers> expectedUntouchedLayers |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1574 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 1575 | { layersInGraph.at("conv2 layer unoptimizable"), layersInGraph.at("Weights Layer 2 unoptimizable"), |
| 1576 | layersInGraph.at("Bias Layer 2 unoptimizable") } |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1577 | }; |
| 1578 | |
| 1579 | for (size_t untouchedIndex = 0; untouchedIndex < untouchedSubgraphs.size(); untouchedIndex++) |
| 1580 | { |
| 1581 | CheckUntouchedSubgraph(untouchedSubgraphs.at(untouchedIndex), |
| 1582 | expectedUntouchedSubgraphSizes.at(untouchedIndex), |
| 1583 | expectedUntouchedInputSlots.at(untouchedIndex), |
| 1584 | expectedUntouchedOutputSlots.at(untouchedIndex), |
| 1585 | expectedUntouchedLayers.at(untouchedIndex)); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | } // Anonymous namespace |
| 1590 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1591 | TEST_SUITE("OptimizeSubGraph") |
| 1592 | { |
| 1593 | TEST_CASE("FullyUnsupportedSubgraph1") { FullyUnsupporteSubgraphTestImpl1(); } |
| 1594 | TEST_CASE("FullyUnsupportedSubgraph2") { FullyUnsupporteSubgraphTestImpl2(); } |
| 1595 | TEST_CASE("FullyOptimizableSubgraph1") { FullyOptimizableSubgraphTestImpl1(); } |
| 1596 | TEST_CASE("FullyOptimizableSubgraph2") { FullyOptimizableSubgraphTestImpl2(); } |
| 1597 | TEST_CASE("PartiallySupportedSubgraph") { PartiallySupportedSubgraphTestImpl(); } |
| 1598 | TEST_CASE("FullyUnoptimizableSubgraph") { FullyUnoptimizableSubgraphTestImpl1(); } |
| 1599 | TEST_CASE("PartiallyOptimizableSubgraph1") { PartiallyOptimizableSubgraphTestImpl1(); } |
| 1600 | TEST_CASE("PartiallyOptimizableSubgraph2") { PartiallyOptimizableSubgraphTestImpl2(); } |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1601 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 1602 | } |