Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "MockBackend.hpp" |
| 7 | #include "MockBackendId.hpp" |
| 8 | |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 9 | #include <armnn/BackendRegistry.hpp> |
| 10 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 11 | #include <armnn/backends/IBackendContext.hpp> |
| 12 | #include <armnn/backends/IMemoryManager.hpp> |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 13 | #include <backendsCommon/DefaultAllocator.hpp> |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 14 | |
| 15 | #include <Optimizer.hpp> |
| 16 | #include <SubgraphViewSelector.hpp> |
| 17 | |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | |
| 23 | bool IsLayerSupported(const armnn::Layer* layer) |
| 24 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 25 | ARMNN_ASSERT(layer != nullptr); |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 26 | |
| 27 | armnn::LayerType layerType = layer->GetType(); |
| 28 | switch (layerType) |
| 29 | { |
| 30 | case armnn::LayerType::Input: |
| 31 | case armnn::LayerType::Output: |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 32 | case armnn::LayerType::Addition: |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 33 | case armnn::LayerType::Convolution2d: |
| 34 | // Layer supported |
| 35 | return true; |
| 36 | default: |
| 37 | // Layer unsupported |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool IsLayerSupported(const armnn::Layer& layer) |
| 43 | { |
| 44 | return IsLayerSupported(&layer); |
| 45 | } |
| 46 | |
| 47 | bool IsLayerOptimizable(const armnn::Layer* layer) |
| 48 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 49 | ARMNN_ASSERT(layer != nullptr); |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 50 | |
| 51 | // A Layer is not optimizable if its name contains "unoptimizable" |
| 52 | const std::string layerName(layer->GetName()); |
| 53 | bool optimizable = layerName.find("unoptimizable") == std::string::npos; |
| 54 | |
| 55 | return optimizable; |
| 56 | } |
| 57 | |
| 58 | bool IsLayerOptimizable(const armnn::Layer& layer) |
| 59 | { |
| 60 | return IsLayerOptimizable(&layer); |
| 61 | } |
| 62 | |
| 63 | } // Anonymous namespace |
| 64 | |
| 65 | namespace armnn |
| 66 | { |
| 67 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 68 | MockBackendInitialiser::MockBackendInitialiser() |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 69 | { |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 70 | BackendRegistryInstance().Register(MockBackend::GetIdStatic(), |
| 71 | []() |
| 72 | { |
| 73 | return IBackendInternalUniquePtr(new MockBackend); |
| 74 | }); |
| 75 | } |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 76 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 77 | MockBackendInitialiser::~MockBackendInitialiser() |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 78 | { |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 79 | try |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 80 | { |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 81 | BackendRegistryInstance().Deregister(MockBackend::GetIdStatic()); |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 82 | } |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 83 | catch (...) |
| 84 | { |
| 85 | std::cerr << "could not deregister mock backend" << std::endl; |
| 86 | } |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | const BackendId& MockBackend::GetIdStatic() |
| 90 | { |
| 91 | static const BackendId s_Id{MockBackendId()}; |
| 92 | return s_Id; |
| 93 | } |
| 94 | |
| 95 | IBackendInternal::IWorkloadFactoryPtr MockBackend::CreateWorkloadFactory( |
Derek Lamberti | 901ea11 | 2019-12-10 22:07:09 +0000 | [diff] [blame] | 96 | const IBackendInternal::IMemoryManagerSharedPtr& /*memoryManager*/) const |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 97 | { |
| 98 | return IWorkloadFactoryPtr{}; |
| 99 | } |
| 100 | |
| 101 | IBackendInternal::IBackendContextPtr MockBackend::CreateBackendContext(const IRuntime::CreationOptions&) const |
| 102 | { |
| 103 | return IBackendContextPtr{}; |
| 104 | } |
| 105 | |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 106 | IBackendInternal::IBackendProfilingContextPtr MockBackend::CreateBackendProfilingContext( |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 107 | const IRuntime::CreationOptions& options, IBackendProfilingPtr& backendProfiling) |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 108 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 109 | IgnoreUnused(options); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 110 | std::shared_ptr<armnn::MockBackendProfilingContext> context = |
| 111 | std::make_shared<MockBackendProfilingContext>(backendProfiling); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 112 | MockBackendProfilingService::Instance().SetProfilingContextPtr(context); |
| 113 | return context; |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 116 | IBackendInternal::IMemoryManagerUniquePtr MockBackend::CreateMemoryManager() const |
| 117 | { |
| 118 | return IMemoryManagerUniquePtr{}; |
| 119 | } |
| 120 | |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 121 | IBackendInternal::ILayerSupportSharedPtr MockBackend::GetLayerSupport() const |
| 122 | { |
David Monahan | 7f2c35a | 2019-05-24 10:46:28 +0100 | [diff] [blame] | 123 | static ILayerSupportSharedPtr layerSupport{new MockLayerSupport}; |
| 124 | return layerSupport; |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | OptimizationViews MockBackend::OptimizeSubgraphView(const SubgraphView& subgraph) const |
| 128 | { |
| 129 | // Prepare the optimization views |
| 130 | OptimizationViews optimizationViews; |
| 131 | |
| 132 | // Get the layers of the input sub-graph |
| 133 | const SubgraphView::Layers& subgraphLayers = subgraph.GetLayers(); |
| 134 | |
| 135 | // Parse the layers |
| 136 | SubgraphView::Layers supportedLayers; |
| 137 | SubgraphView::Layers unsupportedLayers; |
| 138 | SubgraphView::Layers untouchedLayers; |
| 139 | std::for_each(subgraphLayers.begin(), |
| 140 | subgraphLayers.end(), |
| 141 | [&](Layer* layer) |
| 142 | { |
| 143 | bool supported = IsLayerSupported(layer); |
| 144 | if (supported) |
| 145 | { |
| 146 | // Layer supported, check if it's optimizable |
| 147 | bool optimizable = IsLayerOptimizable(layer); |
| 148 | if (optimizable) |
| 149 | { |
| 150 | // Layer fully supported |
| 151 | supportedLayers.push_back(layer); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | // Layer supported but not optimizable |
| 156 | untouchedLayers.push_back(layer); |
| 157 | } |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | // Layer unsupported |
| 162 | unsupportedLayers.push_back(layer); |
| 163 | } |
| 164 | }); |
| 165 | |
| 166 | // Check if there are supported layers |
| 167 | if (!supportedLayers.empty()) |
| 168 | { |
| 169 | // Select the layers that are neither inputs or outputs, but that are optimizable |
| 170 | auto supportedSubgraphSelector = [](const Layer& layer) |
| 171 | { |
| 172 | return layer.GetType() != LayerType::Input && |
| 173 | layer.GetType() != LayerType::Output && |
| 174 | IsLayerSupported(layer) && |
| 175 | IsLayerOptimizable(layer); |
| 176 | }; |
| 177 | |
| 178 | // Apply the subgraph selector to the supported layers to group them into sub-graphs were appropriate |
| 179 | SubgraphView mutableSubgraph(subgraph); |
| 180 | SubgraphViewSelector::Subgraphs supportedSubgraphs = |
| 181 | SubgraphViewSelector::SelectSubgraphs(mutableSubgraph, supportedSubgraphSelector); |
| 182 | |
| 183 | // Create a substitution pair for each supported sub-graph |
| 184 | std::for_each(supportedSubgraphs.begin(), |
| 185 | supportedSubgraphs.end(), |
| 186 | [&optimizationViews](const SubgraphView::SubgraphViewPtr& supportedSubgraph) |
| 187 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 188 | ARMNN_ASSERT(supportedSubgraph != nullptr); |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 189 | |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame^] | 190 | CompiledBlobPtr blobPtr; |
| 191 | BackendId backend = MockBackendId(); |
| 192 | |
| 193 | IConnectableLayer* preCompiledLayer = |
| 194 | optimizationViews.GetINetwork()->AddPrecompiledLayer( |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 195 | PreCompiledDescriptor(supportedSubgraph->GetNumInputSlots(), |
| 196 | supportedSubgraph->GetNumOutputSlots()), |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame^] | 197 | blobPtr, |
| 198 | backend, |
| 199 | nullptr); |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 200 | |
| 201 | SubgraphView substitutionSubgraph(*supportedSubgraph); |
| 202 | SubgraphView replacementSubgraph(preCompiledLayer); |
| 203 | |
| 204 | optimizationViews.AddSubstitution({ substitutionSubgraph, replacementSubgraph }); |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | // Check if there are unsupported layers |
| 209 | if (!unsupportedLayers.empty()) |
| 210 | { |
| 211 | // Select the layers that are neither inputs or outputs, and are not optimizable |
| 212 | auto unsupportedSubgraphSelector = [](const Layer& layer) |
| 213 | { |
| 214 | return layer.GetType() != LayerType::Input && |
| 215 | layer.GetType() != LayerType::Output && |
| 216 | !IsLayerSupported(layer); |
| 217 | }; |
| 218 | |
| 219 | // Apply the subgraph selector to the unsupported layers to group them into sub-graphs were appropriate |
| 220 | SubgraphView mutableSubgraph(subgraph); |
| 221 | SubgraphViewSelector::Subgraphs unsupportedSubgraphs = |
| 222 | SubgraphViewSelector::SelectSubgraphs(mutableSubgraph, unsupportedSubgraphSelector); |
| 223 | |
| 224 | // Add each unsupported sub-graph to the list of failed sub-graphs in the optimizization views |
| 225 | std::for_each(unsupportedSubgraphs.begin(), |
| 226 | unsupportedSubgraphs.end(), |
| 227 | [&optimizationViews](const SubgraphView::SubgraphViewPtr& unsupportedSubgraph) |
| 228 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 229 | ARMNN_ASSERT(unsupportedSubgraph != nullptr); |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 230 | |
| 231 | optimizationViews.AddFailedSubgraph(SubgraphView(*unsupportedSubgraph)); |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | // Check if there are untouched layers |
| 236 | if (!untouchedLayers.empty()) |
| 237 | { |
| 238 | // Select the layers that are neither inputs or outputs, that are supported but that and are not optimizable |
| 239 | auto untouchedSubgraphSelector = [](const Layer& layer) |
| 240 | { |
| 241 | return layer.GetType() != LayerType::Input && |
| 242 | layer.GetType() != LayerType::Output && |
| 243 | IsLayerSupported(layer) && |
| 244 | !IsLayerOptimizable(layer); |
| 245 | }; |
| 246 | |
| 247 | // Apply the subgraph selector to the untouched layers to group them into sub-graphs were appropriate |
| 248 | SubgraphView mutableSubgraph(subgraph); |
| 249 | SubgraphViewSelector::Subgraphs untouchedSubgraphs = |
| 250 | SubgraphViewSelector::SelectSubgraphs(mutableSubgraph, untouchedSubgraphSelector); |
| 251 | |
| 252 | // Add each untouched sub-graph to the list of untouched sub-graphs in the optimizization views |
| 253 | std::for_each(untouchedSubgraphs.begin(), |
| 254 | untouchedSubgraphs.end(), |
| 255 | [&optimizationViews](const SubgraphView::SubgraphViewPtr& untouchedSubgraph) |
| 256 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 257 | ARMNN_ASSERT(untouchedSubgraph != nullptr); |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 258 | |
| 259 | optimizationViews.AddUntouchedSubgraph(SubgraphView(*untouchedSubgraph)); |
| 260 | }); |
| 261 | } |
| 262 | |
| 263 | return optimizationViews; |
| 264 | } |
| 265 | |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 266 | std::unique_ptr<ICustomAllocator> MockBackend::GetDefaultAllocator() const |
| 267 | { |
| 268 | return std::make_unique<DefaultAllocator>(); |
| 269 | } |
| 270 | |
Matteo Martincigh | 88054f8 | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 271 | } // namespace armnn |