blob: 3663727e488e0463365cce833f8414a1771eb0ca [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
Matteo Martincigh49124022019-01-11 13:25:59 +00005
telsoa014fcda012018-03-09 14:13:49 +00006#include "Network.hpp"
7#include "Graph.hpp"
8#include "Layer.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01009#include "DeviceSpec.hpp"
telsoa014fcda012018-03-09 14:13:49 +000010#include "Optimizer.hpp"
Derek Lambertiff05cc52019-04-26 13:05:17 +010011#include "SubgraphViewSelector.hpp"
Matteo Martincigh49124022019-01-11 13:25:59 +000012#include "BackendSettings.hpp"
David Beckac42efd2018-09-26 17:41:13 +010013#include "optimizations/All.hpp"
telsoa014fcda012018-03-09 14:13:49 +000014
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000015#include <backendsCommon/CpuTensorHandle.hpp>
16#include <backendsCommon/WorkloadFactory.hpp>
Matteo Martincighe5b8eb92019-11-28 15:45:42 +000017#include <armnn/backends/IBackendInternal.hpp>
Derek Lamberti84da38b2019-06-13 11:40:08 +010018#include <backendsCommon/TensorHandleFactoryRegistry.hpp>
David Beckac42efd2018-09-26 17:41:13 +010019
20#include <armnn/Exceptions.hpp>
telsoa014fcda012018-03-09 14:13:49 +000021#include <armnn/Utils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010022#include <armnn/TypesUtils.hpp>
Matteo Martincighc601aa62019-10-29 15:03:22 +000023#include <armnn/BackendRegistry.hpp>
Matthew Benthamf48afc62020-01-15 17:55:08 +000024#include <armnn/Logging.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000025#include <armnn/utility/IgnoreUnused.hpp>
telsoa014fcda012018-03-09 14:13:49 +000026
Jan Eilers99d9d4a2019-11-06 10:02:16 +000027#include <ProfilingService.hpp>
28
telsoa014fcda012018-03-09 14:13:49 +000029#include <fcntl.h>
30#include <algorithm>
31#include <fstream>
32#include <memory>
telsoa01c577f2c2018-08-31 09:22:23 +010033#include <vector>
34#include <algorithm>
telsoa014fcda012018-03-09 14:13:49 +000035
36#include <boost/assert.hpp>
37#include <boost/format.hpp>
telsoa014fcda012018-03-09 14:13:49 +000038#include <boost/numeric/conversion/converter_policies.hpp>
39#include <boost/cast.hpp>
40
41namespace armnn
42{
43
44armnn::INetwork* INetwork::CreateRaw()
45{
46 return new Network();
47}
48
49armnn::INetworkPtr INetwork::Create()
50{
51 return INetworkPtr(CreateRaw(), &INetwork::Destroy);
52}
53
54void INetwork::Destroy(INetwork* network)
55{
56 delete boost::polymorphic_downcast<Network*>(network);
57}
58
telsoa014fcda012018-03-09 14:13:49 +000059void IOptimizedNetwork::Destroy(IOptimizedNetwork* network)
60{
61 delete boost::polymorphic_downcast<OptimizedNetwork*>(network);
62}
63
64Status OptimizedNetwork::PrintGraph()
65{
66 m_Graph->Print();
67 return Status::Success;
68}
69
surmeh01bceff2f2018-03-29 16:29:27 +010070Status OptimizedNetwork::SerializeToDot(std::ostream& stream) const
71{
72 return m_Graph->SerializeToDot(stream);
73}
74
Matteo Martincigh49124022019-01-11 13:25:59 +000075void ReportError(const std::string& errorMessage,
76 Optional<std::vector<std::string>&> errorMessages)
77{
78 std::stringstream fullErrorMessage;
79 fullErrorMessage << "ERROR: " << errorMessage;
Derek Lamberti08446972019-11-26 16:38:31 +000080 ARMNN_LOG(warning) << fullErrorMessage.str();
Matteo Martincigh49124022019-01-11 13:25:59 +000081 if (errorMessages)
82 {
83 errorMessages.value().push_back(fullErrorMessage.str());
84 }
85}
86
87void ReportWarning(const std::string& warningMessage,
88 Optional<std::vector<std::string>&> warningMessages)
89{
90 std::stringstream fullWarningMessage;
91 fullWarningMessage << "WARNING: " << warningMessage;
Derek Lamberti08446972019-11-26 16:38:31 +000092 ARMNN_LOG(warning) << fullWarningMessage.str();
Matteo Martincigh49124022019-01-11 13:25:59 +000093 if (warningMessages)
94 {
95 warningMessages.value().push_back(fullWarningMessage.str());
96 }
97}
98
Derek Lamberti4a9e24b2020-01-03 16:53:38 +000099OptimizationResult ReturnWithError(OptimizationResult res,
100 const Layer* layer,
101 const BackendSettings& backendSettings,
102 Optional<std::vector<std::string>&> errMessages)
103{
104 std::stringstream failureMsg;
105 failureMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType())
106 << " is not supported on any preferred backend " << backendSettings.m_PreferredBackends;
107 ReportError(failureMsg.str(), errMessages);
108
109 res.m_Error = true;
110 return res;
111}
112
113
jimfly016b0b53d2018-10-08 14:43:01 +0100114bool CheckScaleSetOnQuantizedType(Layer* layer, Optional<std::vector<std::string>&> errMessages)
115{
116 bool noErrors = true;
117 unsigned int numOutputs = layer->GetNumOutputSlots();
118 for (unsigned int i = 0; i < numOutputs; i++) {
David Monahanb8554702019-04-25 16:03:38 +0100119 OutputSlot& outputSlot = layer->GetOutputSlot(i);
120 TensorInfo info = outputSlot.GetTensorInfo();
Derek Lambertif90c56d2020-01-10 17:14:08 +0000121 if (DataType::QAsymmU8 == info.GetDataType()) {
jimfly016b0b53d2018-10-08 14:43:01 +0100122 if (0.f == info.GetQuantizationScale()) {
123 noErrors = false;
124 std::stringstream ss;
Matteo Martincigh49124022019-01-11 13:25:59 +0000125 ss << "output " << i << " of layer " << GetLayerTypeAsCString(layer->GetType())
jimfly016b0b53d2018-10-08 14:43:01 +0100126 << " (" << layer->GetNameStr() << ") is of type"
127 << " Quantized 8 bit but its scale parameter has not been set";
Matteo Martincigh49124022019-01-11 13:25:59 +0000128 ReportError(ss.str(), errMessages);
jimfly016b0b53d2018-10-08 14:43:01 +0100129 }
David Monahanb8554702019-04-25 16:03:38 +0100130 // Softmax under QuantisedAsymm8 must always be scale (1.0f/256.0f) and offset 0
131 if ((info.GetQuantizationScale() != (1.0f / 256.0f) ||
132 info.GetQuantizationOffset() != 0) &&
133 layer->GetType() == armnn::LayerType::Softmax)
134 {
135 std::stringstream ss;
136 ss << "Quantization parameters for Softmax layer (Scale: " <<
137 info.GetQuantizationScale() << " and Offset: " << info.GetQuantizationOffset() <<
138 ") are incorrect and have been updated to Scale: 0.00390625 and Offset: 0";
Derek Lamberti08446972019-11-26 16:38:31 +0000139 ARMNN_LOG(warning) << ss.str();
David Monahanb8554702019-04-25 16:03:38 +0100140 info.SetQuantizationScale((1.0f /256.0f));
141 info.SetQuantizationOffset(0);
142 outputSlot.SetTensorInfo(info);
143 }
jimfly016b0b53d2018-10-08 14:43:01 +0100144 }
145 }
146 return noErrors;
147}
148
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000149OptimizationResult AttemptBackendAssignment(BackendSettings& backendSettings,
150 Graph& graph,
151 Layer* layer,
152 BackendId backend,
153 DataType dataTypeIn,
154 DataType dataTypeOut,
155 const std::vector<BackendId>& availablePreferredBackends,
156 std::string& reasonIfUnsupported,
157 Optional<std::vector<std::string>&> errMessages)
158{
159 OptimizationResult result;
160
161 // Helper lambda to compose meaningful error message before returning with error
162 auto ReturnError = [&](const Layer* layer)
163 {
164 return ReturnWithError(result, layer, backendSettings, errMessages);
165 };
166
167 // need to set the compute device on the layer
168 // before we can check if it is supported
169 layer->SetBackendId(backend);
170 if (!IWorkloadFactory::IsLayerSupported(*layer, EmptyOptional(), reasonIfUnsupported))
171 {
172 if (dataTypeIn == DataType::Float16 || dataTypeOut == DataType::Float16)
173 {
174 if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported)
175 && layer->GetType() != LayerType::ConvertFp32ToFp16
176 && layer->GetType() != LayerType::ConvertFp16ToFp32)
177 {
178 // Insert FP16 -> FP32 conversion layer before current layer
179 std::vector<ConvertFp16ToFp32Layer*> convertFp16ToFp32Layers;
180 if (dataTypeIn == DataType::Float16)
181 {
182 convertFp16ToFp32Layers =
183 InsertConvertFp16ToFp32LayersBefore(graph, *layer);
184 }
185
186 // Insert FP32 -> FP16 conversion layer after current layer
187 std::vector<ConvertFp32ToFp16Layer*> convertFp32ToFp16Layers;
188 if (dataTypeOut == DataType::Float16)
189 {
190 convertFp32ToFp16Layers =
191 InsertConvertFp32ToFp16LayersAfter(graph, *layer);
192 }
193
194 // Assign a supported backend to the newly introduced conversion layers
195 auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend)
196 {
197 bool supportedBackendFound = false;
198 std::string reasonIfUnsupported;
199
200 // Try preferred backend first
201 layer->SetBackendId(preferredBackend);
202 if (IWorkloadFactory::IsLayerSupported(*layer,
203 EmptyOptional(),
204 reasonIfUnsupported))
205 {
206 supportedBackendFound = true;
207 }
208 else
209 {
210 for (const auto& backend : availablePreferredBackends)
211 {
212 // Skip preferred backend (we already determined that it is not supported)
213 if (backend == preferredBackend)
214 {
215 continue;
216 }
217
218 layer->SetBackendId(backend);
219 if (IWorkloadFactory::IsLayerSupported(*layer,
220 EmptyOptional(),
221 reasonIfUnsupported))
222 {
223 supportedBackendFound = true;
224 break;
225 }
226 }
227 }
228
229 return supportedBackendFound;
230 };
231
232 for (ConvertFp16ToFp32Layer* convertLayer : convertFp16ToFp32Layers)
233 {
234 if (!AssignFirstSupportedBackend(convertLayer, backend))
235 {
236 return ReturnError(convertLayer);
237 }
238 }
239
240 for (ConvertFp32ToFp16Layer* convertLayer : convertFp32ToFp16Layers)
241 {
242 if (!AssignFirstSupportedBackend(convertLayer, backend))
243 {
244 return ReturnError(convertLayer);
245 }
246 }
247
248 return result;
249 }
250 }
251 std::stringstream warningMsg;
252 warningMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType())
253 << " is not supported on requested backend " << layer->GetBackendId().Get()
254 << " for input data type " << GetDataTypeName(dataTypeIn)
255 << " and output data type " << GetDataTypeName(dataTypeOut)
256 << " (reason: " << reasonIfUnsupported
257 << "), falling back to the next backend.";
258 ReportWarning(warningMsg.str(), errMessages);
259
260 return OptimizationResult(true, false);
261 }
262 else
263 {
264 return result;
265 }
266}
267
268
Matteo Martincigh49124022019-01-11 13:25:59 +0000269OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr,
270 BackendSettings& backendSettings,
271 Graph::Iterator& firstLayer,
272 Graph::Iterator& lastLayer,
273 Optional<std::vector<std::string>&> errMessages)
telsoa014fcda012018-03-09 14:13:49 +0000274{
Matteo Martincigh49124022019-01-11 13:25:59 +0000275 OptimizationResult result;
telsoa014fcda012018-03-09 14:13:49 +0000276
Matteo Martincigh49124022019-01-11 13:25:59 +0000277 // Helper lambda to compose meaningful error message before returning with error
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000278 auto ReturnError = [&](const Layer* layer)
279 {
280 return ReturnWithError(result, layer, backendSettings, errMessages);
281 };
Matteo Martincigh49124022019-01-11 13:25:59 +0000282
telsoa01c577f2c2018-08-31 09:22:23 +0100283
Matteo Martincigh49124022019-01-11 13:25:59 +0000284 auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends();
285 if (availablePreferredBackends.empty())
telsoa01c577f2c2018-08-31 09:22:23 +0100286 {
Matteo Martincigh49124022019-01-11 13:25:59 +0000287 std::stringstream failureMsg;
288 failureMsg << "No preferred backends are available";
289 ReportError(failureMsg.str(), errMessages);
290
291 result.m_Error = true;
292 return result;
293 }
294
295 for (auto it = firstLayer; it != lastLayer; ++it)
296 {
297 auto layer = *it;
Aron Virginas-Tar87972be2019-11-13 15:16:28 +0000298
299 DataType dataTypeIn = layer->GetNumInputSlots() == 0 ? DataType::Float32 :
300 layer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo().GetDataType();
301 DataType dataTypeOut = layer->GetNumOutputSlots() == 0 ? DataType::Float32 :
302 layer->GetOutputSlot(0).GetTensorInfo().GetDataType();
303
telsoa01c577f2c2018-08-31 09:22:23 +0100304 std::string reasonIfUnsupported;
305 bool found = false;
jimfly016b0b53d2018-10-08 14:43:01 +0100306 if (!CheckScaleSetOnQuantizedType(layer, errMessages))
307 {
308 // don't bomb immediately, find all the quantized outputs
309 // which haven't had a scale set and report them all back.
Matteo Martincigh49124022019-01-11 13:25:59 +0000310 result.m_Error = true;
jimfly016b0b53d2018-10-08 14:43:01 +0100311 }
Matteo Martincigh49124022019-01-11 13:25:59 +0000312
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000313 // First try assign layer to hint backend
314 if (layer->GetBackendHint().has_value() &&
315 backendSettings.IsBackendSupported(layer->GetBackendHint().value()) &&
316 AttemptBackendAssignment(backendSettings,
317 optNetObjPtr->GetGraph(),
318 layer,
319 layer->GetBackendHint().value(),
320 dataTypeIn,
321 dataTypeOut,
322 availablePreferredBackends,
323 reasonIfUnsupported,
324 errMessages).IsOk())
telsoa01c577f2c2018-08-31 09:22:23 +0100325 {
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000326 found = true;
327 backendSettings.m_SelectedBackends.insert(layer->GetBackendHint().value());
328 }
329 else
330 {
331 // Try assign layer to prefered list of backends
332 for (const auto& backend : availablePreferredBackends)
telsoa01c577f2c2018-08-31 09:22:23 +0100333 {
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000334 if (layer->GetBackendHint().has_value() &&
335 layer->GetBackendHint().value() == backend)
telsoa01c577f2c2018-08-31 09:22:23 +0100336 {
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000337 continue; //Don't re-test the backend hint
telsoa01c577f2c2018-08-31 09:22:23 +0100338 }
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000339
340 OptimizationResult res = AttemptBackendAssignment(backendSettings,
341 optNetObjPtr->GetGraph(),
342 layer,
343 backend,
344 dataTypeIn,
345 dataTypeOut,
346 availablePreferredBackends,
347 reasonIfUnsupported,
348 errMessages);
349
350 if (res.IsOk())
351 {
352 found = true;
353 backendSettings.m_SelectedBackends.insert(backend);
354 break;
355 }
356 else if (res.IsError())
357 {
358 return res; // Cannot continue.
359 // Note: we don't need to log the error as it would already
360 // be logged in AttemptBackendAssignment().
361 }
362 else
363 {
364 BOOST_ASSERT_MSG(res.IsWarningOnly(), "OptimizationResult in unexpected state.");
365 }
telsoa01c577f2c2018-08-31 09:22:23 +0100366 }
367 }
368
369 // If the layer is unsupported by any devices, log and return a null network.
Matteo Martincigh49124022019-01-11 13:25:59 +0000370 if (!found)
371 {
telsoa01c577f2c2018-08-31 09:22:23 +0100372 // NOTE: if the layer is not an operation queue type AND we have not got CpuRef as a
373 // fallback we should set the compute device on the layer to CpuRef (these are not
374 // available as accelerated operations, or are only available under certain
375 // conditions, currently they comprise MemCopy, Constant, Permute)
376 armnn::LayerType layerType = layer->GetType();
Matteo Martincigh49124022019-01-11 13:25:59 +0000377 if (!backendSettings.IsCpuRefUsed() && (layerType == armnn::LayerType::MemCopy ||
378 layerType == armnn::LayerType::Constant ||
379 layerType == armnn::LayerType::Permute))
telsoa01c577f2c2018-08-31 09:22:23 +0100380 {
Matteo Martincigh49124022019-01-11 13:25:59 +0000381 BackendId cpuBackendId(armnn::Compute::CpuRef);
382 layer->SetBackendId(cpuBackendId);
383 backendSettings.m_SelectedBackends.insert(cpuBackendId);
telsoa01c577f2c2018-08-31 09:22:23 +0100384 }
385 else
386 {
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000387 return ReturnError(layer);
telsoa01c577f2c2018-08-31 09:22:23 +0100388 }
389 }
390 }
Matteo Martincigh49124022019-01-11 13:25:59 +0000391
392 return result;
393}
394
Matteo Martincighadddddb2019-01-24 14:06:23 +0000395OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr,
396 BackendSettings& backendSettings,
Derek Lambertiff05cc52019-04-26 13:05:17 +0100397 SubgraphView& subgraph,
Matteo Martincighadddddb2019-01-24 14:06:23 +0000398 Optional<std::vector<std::string>&> errMessages)
Matteo Martincigh49124022019-01-11 13:25:59 +0000399{
Derek Lambertiff05cc52019-04-26 13:05:17 +0100400 Graph::Iterator firstLayer = subgraph.begin();
401 Graph::Iterator lastLayer = subgraph.end();
Matteo Martincighadddddb2019-01-24 14:06:23 +0000402 return AssignBackends(optNetObjPtr,
403 backendSettings,
404 firstLayer,
405 lastLayer,
406 errMessages);
407}
408
Derek Lamberti84da38b2019-06-13 11:40:08 +0100409BackendsMap CreateSupportedBackends(TensorHandleFactoryRegistry& handleFactoryRegistry,
410 BackendSettings& backendSettings)
411{
412 BackendsMap backends;
413 auto const& backendRegistry = BackendRegistryInstance();
414 for (auto&& selectedBackend : backendSettings.m_SupportedBackends)
415 {
416 auto backendFactory = backendRegistry.GetFactory(selectedBackend);
417 auto backendObjPtr = backendFactory();
418 BOOST_ASSERT(backendObjPtr);
419
420 backendObjPtr->RegisterTensorHandleFactories(handleFactoryRegistry);
421
422 backends[backendObjPtr->GetId()] = std::move(backendObjPtr);
423 }
424
425 return backends;
426}
427
Matteo Martincighadddddb2019-01-24 14:06:23 +0000428OptimizationResult ApplyBackendOptimizations(OptimizedNetwork* optNetObjPtr,
429 BackendSettings& backendSettings,
Derek Lamberti84da38b2019-06-13 11:40:08 +0100430 BackendsMap& backends,
Matteo Martincighadddddb2019-01-24 14:06:23 +0000431 Optional<std::vector<std::string>&> errMessages)
432{
433 BOOST_ASSERT(optNetObjPtr);
Matteo Martincigh49124022019-01-11 13:25:59 +0000434
435 OptimizationResult result;
436
Matteo Martincighadddddb2019-01-24 14:06:23 +0000437 // Get the optimized graph
438 Graph& optGraph = optNetObjPtr->GetGraph();
Matteo Martincigh49124022019-01-11 13:25:59 +0000439
Matteo Martincighadddddb2019-01-24 14:06:23 +0000440 // Run backend specific optimizations
Matteo Martincighadddddb2019-01-24 14:06:23 +0000441 for (auto&& selectedBackend : backendSettings.m_SelectedBackends)
Matteo Martincigh49124022019-01-11 13:25:59 +0000442 {
Derek Lamberti84da38b2019-06-13 11:40:08 +0100443 auto backendObjPtr = backends.find(selectedBackend)->second.get();
Matteo Martincighadddddb2019-01-24 14:06:23 +0000444 BOOST_ASSERT(backendObjPtr);
445
446 // Select sub-graphs based on backend
Derek Lambertiff05cc52019-04-26 13:05:17 +0100447 SubgraphViewSelector::Subgraphs subgraphs =
Rob Hughes65c32262019-07-23 15:33:39 +0100448 SubgraphViewSelector::SelectSubgraphs(optGraph,
Matteo Martincigh602af092019-05-01 10:31:27 +0100449 // Select layers assigned to the requested backend
450 [&backendObjPtr](const Layer& layer)
451 {
452 return layer.GetType() != LayerType::Input &&
453 layer.GetType() != LayerType::Output &&
454 layer.GetBackendId() == backendObjPtr->GetId();
455 });
Derek Lambertiff05cc52019-04-26 13:05:17 +0100456 if (subgraphs.empty())
Matteo Martincigh49124022019-01-11 13:25:59 +0000457 {
Matteo Martincighadddddb2019-01-24 14:06:23 +0000458 // No sub-graphs found, try with next selected backend
459 continue;
Matteo Martincigh49124022019-01-11 13:25:59 +0000460 }
Matteo Martincighadddddb2019-01-24 14:06:23 +0000461
462 // Try to optimize each sub-graph
Derek Lambertiff05cc52019-04-26 13:05:17 +0100463 for (auto& subgraph : subgraphs)
Matteo Martincigh49124022019-01-11 13:25:59 +0000464 {
Matteo Martincighadddddb2019-01-24 14:06:23 +0000465 // Try to optimize the current sub-graph
Matteo Martincigh84924332019-05-09 12:46:16 +0100466 OptimizationViews optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraph);
467 BOOST_ASSERT(optimizationViews.Validate(*subgraph));
Matteo Martincighadddddb2019-01-24 14:06:23 +0000468
469 // Optimization attempted, check the resulting optimized sub-graph
Matteo Martincigh84924332019-05-09 12:46:16 +0100470 for (auto& substitution : optimizationViews.GetSubstitutions())
Matteo Martincighadddddb2019-01-24 14:06:23 +0000471 {
472 // Sub-graph optimized, substitute the sub-graph with the new optimized one in the main optimized graph
Matteo Martincigh84924332019-05-09 12:46:16 +0100473 SubgraphView& replacementSubgraph = substitution.m_ReplacementSubgraph;
474 SubgraphView& substitutableSubgraph = substitution.m_SubstitutableSubgraph;
475 optGraph.SubstituteSubgraph(substitutableSubgraph, replacementSubgraph);
Matteo Martincighadddddb2019-01-24 14:06:23 +0000476
477 // Assign the current backend to the optimized sub-graph
Matteo Martincigh84924332019-05-09 12:46:16 +0100478 std::for_each(replacementSubgraph.begin(), replacementSubgraph.end(), [&selectedBackend](Layer* l)
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100479 {
480 BOOST_ASSERT(l);
481 l->SetBackendId(selectedBackend);
482 });
Matteo Martincighadddddb2019-01-24 14:06:23 +0000483 }
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100484
Matteo Martincigh84924332019-05-09 12:46:16 +0100485 if (!optimizationViews.GetFailedSubgraphs().empty())
Matteo Martincighadddddb2019-01-24 14:06:23 +0000486 {
Matteo Martincighadddddb2019-01-24 14:06:23 +0000487 std::stringstream warningMsg;
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100488 warningMsg << "Some sub-graph(s) failed to optimized on " << backendObjPtr->GetId() << " backend.";
Matteo Martincighadddddb2019-01-24 14:06:23 +0000489 ReportWarning(warningMsg.str(), errMessages);
490
491 // Failed to optimize the given sub-graph, re-assign the sub-graph layers to other available backends
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100492 BackendSettings settingsCopy(backendSettings);
Matteo Martincighadddddb2019-01-24 14:06:23 +0000493 if (!backendObjPtr->GetId().IsCpuRef())
494 {
495 // Add the current backend to the list of backends to ignore
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100496 settingsCopy.m_IgnoredBackends.insert(backendObjPtr->GetId());
Matteo Martincighadddddb2019-01-24 14:06:23 +0000497 }
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100498
499 int count=0;
Matteo Martincigh84924332019-05-09 12:46:16 +0100500 for (auto& failedSubgraph : optimizationViews.GetFailedSubgraphs())
Matteo Martincighadddddb2019-01-24 14:06:23 +0000501 {
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100502 // An error occurred: the optimization was attempted but not performed, try different backends
503 std::stringstream subgraphMsg;
504 subgraphMsg << "Re-assigning backends to " << failedSubgraph.GetLayers().size()
505 << " layers inside sub-graph " << count++;
Matteo Martincigh328d92b2019-07-04 17:52:55 +0100506 ReportWarning(subgraphMsg.str(), errMessages);
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100507
508 OptimizationResult reassignmentResult = AssignBackends(optNetObjPtr,
509 settingsCopy,
510 *subgraph,
511 errMessages);
512 if (reassignmentResult.m_Error)
513 {
514 // Failed to re-assign one of the remaining backends to each layer of the sub-graph
515 result.m_Error = true;
516 return result;
517 }
Matteo Martincighadddddb2019-01-24 14:06:23 +0000518 }
Matteo Martincigh49124022019-01-11 13:25:59 +0000519 }
520 }
521 }
522
523 return result;
524}
525
Derek Lamberti84da38b2019-06-13 11:40:08 +0100526bool RequiresCopy(ITensorHandleFactory::FactoryId src,
527 ITensorHandleFactory::FactoryId dst,
528 TensorHandleFactoryRegistry& registry)
529{
530 if (src != dst)
531 {
532 ITensorHandleFactory* srcFactory = registry.GetFactory(src);
533 ITensorHandleFactory* dstFactory = registry.GetFactory(dst);
534
Matteo Martincigha6539ed2019-08-27 13:43:32 +0100535 if (srcFactory && dstFactory &&
536 (srcFactory->GetExportFlags() & dstFactory->GetImportFlags()) != 0)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100537 {
538 return false;
539 }
540 return true;
541 }
542 return false;
543}
544
545// Find the handle factory for the input layer which results in fewest required copies.
546ITensorHandleFactory::FactoryId CalculateSlotOptionForInput(BackendsMap& backends,
547 OutputSlot& slot,
548 TensorHandleFactoryRegistry& registry)
549{
550 Layer& layer = slot.GetOwningLayer();
551 BOOST_ASSERT(layer.GetType() == LayerType::Input);
552
553 // Explicitly select the tensorhandle factory for InputLayer because the rules for it are slightly different. It
554 // doesn't matter which backend it is assigned to because they all use the same implementation, which
555 // requires Map/Unmap support. This means that, so long as the handle type supports map/unmap semantics, we can
556 // select a factory with maximum compatibility with the layers connected to the InputLayer.
557
558 // First ensure the from backends can support the TensorHandeAPI
559 auto frmBackend = backends.find(layer.GetBackendId());
560 if (frmBackend == backends.end() ||
561 !frmBackend->second->SupportsTensorAllocatorAPI())
562 {
563 return ITensorHandleFactory::LegacyFactoryId;
564 }
565
566 // Go through all connections to the output slot and determine the TensorHandleFactory which results in the
567 // fewest copies.
568 std::map<ITensorHandleFactory::FactoryId, int> factoryScores;
569 int topScore = 0;
570 ITensorHandleFactory::FactoryId topChoice = ITensorHandleFactory::LegacyFactoryId;
571
572 for (auto&& connection : slot.GetConnections())
573 {
574 const Layer& connectedLayer = connection->GetOwningLayer();
575
576 auto toBackend = backends.find(connectedLayer.GetBackendId());
577 BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer");
578
579 if (!toBackend->second.get()->SupportsTensorAllocatorAPI())
580 {
581 // The destination backend does not support the tensor allocator API, move to the next one
582 continue;
583 }
584
585 auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences();
586 for (auto&& dst : dstPrefs)
587 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100588 // Input layers use the mem copy workload or import, so the selected factory must
589 // support either the map/unmap API or Import API
Derek Lamberti84da38b2019-06-13 11:40:08 +0100590 ITensorHandleFactory* factory = registry.GetFactory(dst);
Derek Lambertif674aa02019-08-01 15:56:25 +0100591 if (!factory->SupportsMapUnmap() &&
592 !CheckFlag(factory->GetImportFlags(), MemorySource::Malloc)) // Just support cpu mem imports for now
Derek Lamberti84da38b2019-06-13 11:40:08 +0100593 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100594 // The current tensor handle factory does not support the map/unmap or import
595 // strategy, move to the next one
Derek Lamberti84da38b2019-06-13 11:40:08 +0100596 continue;
597 }
598
599 auto it = factoryScores.find(dst);
600 if (it == factoryScores.end())
601 {
602 // Add new score to the table
603 factoryScores[dst] = 0;
604 if (topChoice == ITensorHandleFactory::LegacyFactoryId)
605 {
606 topChoice = dst;
607 }
608 }
609 else
610 {
611 // Increase the score
612 factoryScores[dst]++;
613
614 // Track the best option
615 if (factoryScores[dst] > topScore)
616 {
617 topScore = factoryScores[dst];
618 topChoice = dst;
619 }
620 }
621 }
622 }
623
624 return topChoice;
625}
626
627// Find the handle factory for the output layer which results in fewest required copies.
628ITensorHandleFactory::FactoryId CalculateSlotOptionForOutput(BackendsMap& backends,
629 OutputSlot& slot,
630 TensorHandleFactoryRegistry& registry)
631{
Jan Eilers8eb25602020-03-09 12:13:48 +0000632 IgnoreUnused(backends, slot, registry);
Derek Lamberti94a88d22019-12-10 21:12:59 +0000633 return ITensorHandleFactory::DeferredFactoryId;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100634}
635
636// For all handle factories supported on the source backend, we wish to find the one which requires the fewest copies
637// when considering all connections.
638ITensorHandleFactory::FactoryId CalculateSlotOption(BackendsMap& backends,
639 OutputSlot& outputSlot,
640 TensorHandleFactoryRegistry& registry)
641{
642 // First ensure the from backends can support the TensorHandeAPI
643 Layer& layer = outputSlot.GetOwningLayer();
644 auto frmBackend = backends.find(layer.GetBackendId());
645 if (frmBackend == backends.end() ||
646 !frmBackend->second->SupportsTensorAllocatorAPI())
647 {
648 return ITensorHandleFactory::LegacyFactoryId;
649 }
650
651 // Connections to Output Layers requires support for map/unmap on the TensorHandle.
652 bool requiresMapUnmap = false;
653 for (auto&& connection : outputSlot.GetConnections())
654 {
655 const Layer& connectedLayer = connection->GetOwningLayer();
656 if (connectedLayer.GetType() == LayerType::Output)
657 {
658 requiresMapUnmap = true;
659 }
660 }
661
662 IBackendInternal* srcBackend = frmBackend->second.get();
663 auto srcPrefs = srcBackend->GetHandleFactoryPreferences();
664
665 // Initialize the scores
666 std::map<ITensorHandleFactory::FactoryId, int> factoryScores;
667 for (auto&& pref : srcPrefs)
668 {
669 if (requiresMapUnmap) // Only consider factories that support map/unmap if required
670 {
671 ITensorHandleFactory* factory = registry.GetFactory(pref);
672 if (!factory->SupportsMapUnmap())
673 {
674 // The current tensor handle factory does not support the map/unmap strategy, move to the next one
675 continue;
676 }
677 }
678
679 auto it = factoryScores.find(pref);
680 if (it == factoryScores.end())
681 {
682 // Add new score to the table
683 factoryScores[pref] = 0;
684 }
685 }
686
687 // Score each handle factory based on how many times it requires copies on the slot connections
688 for (auto&& connection : outputSlot.GetConnections())
689 {
690 const Layer& connectedLayer = connection->GetOwningLayer();
691
692 auto toBackend = backends.find(connectedLayer.GetBackendId());
693 BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer");
694
695 auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences();
696 for (auto&& src : srcPrefs)
697 {
698 if (factoryScores.find(src) == factoryScores.end()) // Don't consider excluded factories
699 {
700 continue;
701 }
702
703 for (auto&& dst : dstPrefs)
704 {
705 if (RequiresCopy(src, dst, registry))
706 {
707 // Copy avoided, increase the score
708 factoryScores[src]++;
709 break;
710 }
711 }
712 }
713 }
714
715 // Find the lowest score
716 int minScore = std::numeric_limits<int>::max();
717 for (auto it : factoryScores)
718 {
719 minScore = std::min(minScore, it.second);
720 }
721
722 // Collect factories matching the best(lowest) score
723 std::vector<ITensorHandleFactory::FactoryId> optimalFactories;
724 for (auto it : factoryScores)
725 {
726 if (it.second == minScore)
727 {
728 optimalFactories.push_back(it.first);
729 }
730 }
731
732 // For all compatible Factories matching the best score, find the preferred one for the current layer.
733 for (auto&& srcPref : srcPrefs)
734 {
735 for (auto&& comp : optimalFactories)
736 {
737 if (comp == srcPref)
738 {
739 return comp;
740 }
741 }
742 }
743
744 return ITensorHandleFactory::LegacyFactoryId;
745}
746
Derek Lambertif674aa02019-08-01 15:56:25 +0100747EdgeStrategy CalculateEdgeStrategy(BackendsMap& backends,
748 ITensorHandleFactory::FactoryId srcFactoryId,
749 const Layer& layer,
750 const Layer& connectedLayer,
751 TensorHandleFactoryRegistry& registry)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100752{
753 auto toBackend = backends.find(connectedLayer.GetBackendId());
754 BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer");
755
756 auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences();
757
758 // Legacy API check for backward compatibility
759 if (srcFactoryId == ITensorHandleFactory::LegacyFactoryId || dstPrefs.empty())
760 {
761 if (layer.GetBackendId() != connectedLayer.GetBackendId())
762 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100763 return EdgeStrategy::CopyToTarget;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100764 }
765 else
766 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100767 return EdgeStrategy::DirectCompatibility;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100768 }
769 }
770
771 // TensorHandleFactory API present, so perform more sophisticated strategies.
Derek Lambertif674aa02019-08-01 15:56:25 +0100772 // Dst Output layers don't require copy because they use import or map/unmap
Derek Lamberti84da38b2019-06-13 11:40:08 +0100773 if (connectedLayer.GetType() == LayerType::Output)
774 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100775 return EdgeStrategy::DirectCompatibility;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100776 }
777
778 // Search for direct match in prefs
779 for (auto&& pref : dstPrefs)
780 {
781 if (pref == srcFactoryId)
782 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100783 return EdgeStrategy::DirectCompatibility;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100784 }
785 }
786
787 // Search for export/import options
788 ITensorHandleFactory* srcFactory = registry.GetFactory(srcFactoryId);
Derek Lambertif674aa02019-08-01 15:56:25 +0100789 if (srcFactory->GetExportFlags() != 0)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100790 {
791 for (auto&& pref : dstPrefs)
792 {
793 ITensorHandleFactory* dstFactory = registry.GetFactory(pref);
James Conroyffab16f2019-11-07 14:37:09 +0000794
James Conroy47e863d2019-11-18 17:07:43 +0000795 // Handles cases when a destPref is not listed in TensorHandleFactoryRegistry
James Conroyffab16f2019-11-07 14:37:09 +0000796 if (!dstFactory) {
James Conroy47e863d2019-11-18 17:07:43 +0000797 continue;
James Conroyffab16f2019-11-07 14:37:09 +0000798 }
799
Derek Lambertif674aa02019-08-01 15:56:25 +0100800 if ((dstFactory->GetImportFlags() & srcFactory->GetExportFlags()) != 0)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100801 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100802 return EdgeStrategy::ExportToTarget;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100803 }
804 }
805 }
806
807 // Search for copy options via map/unmap
808 if (srcFactory->SupportsMapUnmap())
809 {
810 for (auto&& pref : dstPrefs)
811 {
812 ITensorHandleFactory* dstFactory = registry.GetFactory(pref);
James Conroy47e863d2019-11-18 17:07:43 +0000813 if (dstFactory && dstFactory->SupportsMapUnmap())
Derek Lamberti84da38b2019-06-13 11:40:08 +0100814 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100815 return EdgeStrategy::CopyToTarget;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100816 }
817 }
818 }
819
Derek Lambertif674aa02019-08-01 15:56:25 +0100820 return EdgeStrategy::Undefined;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100821}
822
823// Select the TensorHandleFactories and the corresponding memory strategy
824OptimizationResult SelectTensorHandleStrategy(Graph& optGraph,
825 BackendsMap& backends,
826 TensorHandleFactoryRegistry& registry,
827 Optional<std::vector<std::string>&> errMessages)
828{
829 OptimizationResult result;
830
831 optGraph.ForEachLayer([&backends, &registry, &result, &errMessages](Layer* layer)
832 {
833 BOOST_ASSERT(layer);
834
835 // Lets make sure the backend is in our list of supported backends. Something went wrong during backend
836 // assignment if this check fails
837 BOOST_ASSERT(backends.find(layer->GetBackendId()) != backends.end());
838
839 // Check each output separately
840 for (unsigned int slotIdx = 0; slotIdx < layer->GetNumOutputSlots(); slotIdx++)
841 {
842 OutputSlot& outputSlot = layer->GetOutputSlot(slotIdx);
843
844 ITensorHandleFactory::FactoryId slotOption = ITensorHandleFactory::LegacyFactoryId;
845
846 // Calculate the factory to use which results in the fewest copies being made.
847 switch(layer->GetType())
848 {
849 case LayerType::Input:
850 slotOption = CalculateSlotOptionForInput(backends, outputSlot, registry);
851 break;
852 case LayerType::Output:
853 slotOption = CalculateSlotOptionForOutput(backends, outputSlot, registry);
854 break;
855 default:
856 slotOption = CalculateSlotOption(backends, outputSlot, registry);
857 break;
858 }
859 outputSlot.SetTensorHandleFactory(slotOption);
860
Derek Lambertif674aa02019-08-01 15:56:25 +0100861 // Now determine the "best" edge strategy for each connection given the slotOption.
Derek Lamberti84da38b2019-06-13 11:40:08 +0100862 unsigned int connectionIdx = 0;
863 for (auto&& connection : outputSlot.GetConnections())
864 {
865 const Layer& connectedLayer = connection->GetOwningLayer();
866
Derek Lambertif674aa02019-08-01 15:56:25 +0100867 EdgeStrategy strategy = CalculateEdgeStrategy(backends, slotOption, *layer, connectedLayer, registry);
Derek Lamberti84da38b2019-06-13 11:40:08 +0100868
Derek Lambertif674aa02019-08-01 15:56:25 +0100869 if (strategy == EdgeStrategy::Undefined)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100870 {
871 result.m_Error = true;
872 if (errMessages)
873 {
874 errMessages.value().emplace_back("Could not find valid strategy required for compatibility"
875 " between backends.");
876 }
877 return;
878 }
879
Derek Lambertif674aa02019-08-01 15:56:25 +0100880 outputSlot.SetEdgeStrategy(connectionIdx, strategy);
Derek Lamberti84da38b2019-06-13 11:40:08 +0100881
882 connectionIdx++;
883 }
884 }
885 });
886
887 return result;
888}
889
Matteo Martincigh49124022019-01-11 13:25:59 +0000890IOptimizedNetworkPtr Optimize(const INetwork& inNetwork,
891 const std::vector<BackendId>& backendPreferences,
892 const IDeviceSpec& deviceSpec,
893 const OptimizerOptions& options,
Rob Hughes23214432019-11-05 11:27:36 +0000894 Optional<std::vector<std::string>&> messages)
Matteo Martincigh49124022019-01-11 13:25:59 +0000895{
896 if (backendPreferences.empty())
897 {
898 throw armnn::InvalidArgumentException("Invoked Optimize with no backends specified");
899 }
900
901 const Network& network = *boost::polymorphic_downcast<const Network*>(&inNetwork);
902 std::unique_ptr<Graph> graph = std::make_unique<Graph>(network.GetGraph());
903
904 auto optNet = IOptimizedNetworkPtr(new OptimizedNetwork(std::move(graph)), &IOptimizedNetwork::Destroy);
905
906 OptimizedNetwork* optNetObjPtr = boost::polymorphic_downcast<OptimizedNetwork*>(optNet.get());
907
Matteo Martincighadddddb2019-01-24 14:06:23 +0000908 // Get the optimized graph
909 Graph& optGraph = optNetObjPtr->GetGraph();
910
Matteo Martincigh49124022019-01-11 13:25:59 +0000911 // Perform optimisation passes
912 using namespace optimizations;
Matteo Martincighadddddb2019-01-24 14:06:23 +0000913 Optimizer::Pass(optGraph, MakeOptimizations(SquashEqualPermuteSiblings(),
Mike Kelly490b7be2020-03-03 12:39:09 +0000914 SquashEqualTransposeSiblings(),
Matteo Martincighadddddb2019-01-24 14:06:23 +0000915 SquashEqualReshapeSiblings(),
916 OptimizeInversePermutes(),
Mike Kelly490b7be2020-03-03 12:39:09 +0000917 OptimizeInverseTransposes(),
Matteo Martincighadddddb2019-01-24 14:06:23 +0000918 MovePermuteUp(),
Mike Kelly490b7be2020-03-03 12:39:09 +0000919 MoveTransposeUp(),
Matteo Martincighadddddb2019-01-24 14:06:23 +0000920 PermuteAsReshape(),
Mike Kelly490b7be2020-03-03 12:39:09 +0000921 TransposeAsReshape(),
Nina Drozd861985f2019-04-18 14:48:51 +0100922 OptimizeConsecutiveReshapes(),
Rob Hughes3a7d3a72019-09-24 16:59:56 +0100923 FoldPadIntoConvolution2d(),
Mike Kelly490b7be2020-03-03 12:39:09 +0000924 PermuteAndBatchToSpaceAsDepthToSpace(),
925 TransposeAndBatchToSpaceAsDepthToSpace()));
Matteo Martincigh49124022019-01-11 13:25:59 +0000926
Matteo Martincighadddddb2019-01-24 14:06:23 +0000927 // Infer the tensor infos for all output slots. Throws an exception on failure
928 optGraph.InferTensorInfos();
Matteo Martincigh49124022019-01-11 13:25:59 +0000929
930 // If Fp32 to Fp16 optimization is set convert Fp32 network to Fp16
931 if (options.m_ReduceFp32ToFp16)
932 {
Matteo Martincighadddddb2019-01-24 14:06:23 +0000933 Optimizer::Pass(optGraph, MakeOptimizations(Fp32NetworkToFp16Converter()));
Derek Lambertidd6804b2019-11-27 09:29:57 +0000934 Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf()));
Matteo Martincigh49124022019-01-11 13:25:59 +0000935 }
936
937 // Initialize backend settings
938 BackendSettings backendSettings(backendPreferences, deviceSpec);
939 if (backendSettings.GetAvailablePreferredBackends().empty())
940 {
941 std::stringstream failureMsg;
942 failureMsg << "None of the preferred backends " << backendPreferences
943 << " are supported. Current platform provides " << backendSettings.m_SupportedBackends;
Rob Hughes23214432019-11-05 11:27:36 +0000944 ReportError(failureMsg.str(), messages);
Matteo Martincigh49124022019-01-11 13:25:59 +0000945 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
946 }
947
Derek Lamberti84da38b2019-06-13 11:40:08 +0100948 // Create a map to temporarily hold initialized backend objects
949 TensorHandleFactoryRegistry tensorHandleFactoryRegistry;
950 BackendsMap backends = CreateSupportedBackends(tensorHandleFactoryRegistry, backendSettings);
951
Matteo Martincigh49124022019-01-11 13:25:59 +0000952 // Assign an available backend to each layer
Matteo Martincighadddddb2019-01-24 14:06:23 +0000953 Graph::Iterator firstLayer = optGraph.begin();
954 Graph::Iterator lastLayer = optGraph.end();
Derek Lamberti84da38b2019-06-13 11:40:08 +0100955 OptimizationResult assignBackendsResult = AssignBackends(optNetObjPtr,
956 backendSettings,
957 firstLayer,
958 lastLayer,
Rob Hughes23214432019-11-05 11:27:36 +0000959 messages);
Derek Lamberti84da38b2019-06-13 11:40:08 +0100960 if (assignBackendsResult.m_Error)
Matteo Martincigh49124022019-01-11 13:25:59 +0000961 {
962 // Failed to assign a backend to each layer
jimfly016b0b53d2018-10-08 14:43:01 +0100963 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
964 }
telsoa01c577f2c2018-08-31 09:22:23 +0100965
Matteo Martincighadddddb2019-01-24 14:06:23 +0000966 Optimizer::Pass(optGraph, MakeOptimizations(OptimizeInverseConversionsFp16(),
967 OptimizeInverseConversionsFp32()));
telsoa01c577f2c2018-08-31 09:22:23 +0100968
Matteo Martincighadddddb2019-01-24 14:06:23 +0000969 // Apply the backend-specific optimizations
970 OptimizationResult backendOptimizationResult = ApplyBackendOptimizations(optNetObjPtr,
971 backendSettings,
Derek Lamberti84da38b2019-06-13 11:40:08 +0100972 backends,
Rob Hughes23214432019-11-05 11:27:36 +0000973 messages);
Matteo Martincighadddddb2019-01-24 14:06:23 +0000974 if (backendOptimizationResult.m_Error)
Matteo Martincigh49124022019-01-11 13:25:59 +0000975 {
Matteo Martincighadddddb2019-01-24 14:06:23 +0000976 // Failed to apply the backend-specific optimizations
977 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
Matteo Martincigh49124022019-01-11 13:25:59 +0000978 }
979
Matteo Martincighadddddb2019-01-24 14:06:23 +0000980 // If the debug flag is set, then insert a DebugLayer after each layer
981 // Doing this after applying the backend optimizations as they might have changed some layers
982 if (options.m_Debug)
983 {
984 Optimizer::Pass(optGraph, MakeOptimizations(InsertDebugLayer()));
985 }
986
Derek Lamberti84da38b2019-06-13 11:40:08 +0100987 // Calculate the compatibility strategies for tensor handles
988 OptimizationResult strategyResult = SelectTensorHandleStrategy(optGraph,
989 backends,
990 tensorHandleFactoryRegistry,
Rob Hughes23214432019-11-05 11:27:36 +0000991 messages);
Derek Lamberti84da38b2019-06-13 11:40:08 +0100992 if (strategyResult.m_Error)
993 {
994 // Failed to apply the backend-specific optimizations
995 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
996 }
997
998 // Based on the tensor handle strategy determined above, insert copy layers where required.
Derek Lambertif674aa02019-08-01 15:56:25 +0100999 optGraph.AddCompatibilityLayers(backends, tensorHandleFactoryRegistry);
telsoa01c577f2c2018-08-31 09:22:23 +01001000
1001 // Convert constants
Matteo Martincighadddddb2019-01-24 14:06:23 +00001002 Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf()));
1003 Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsHalfToFloat()));
telsoa01c577f2c2018-08-31 09:22:23 +01001004
Derek Lamberti84da38b2019-06-13 11:40:08 +01001005 // Run backend specific optimizations (deprecated)
Matteo Martincigh49124022019-01-11 13:25:59 +00001006 for (auto&& chosenBackend : backendSettings.m_SelectedBackends)
David Beck263e3492018-11-09 14:46:40 +00001007 {
1008 auto factoryFun = BackendRegistryInstance().GetFactory(chosenBackend);
1009 auto backendPtr = factoryFun();
1010 BOOST_ASSERT(backendPtr.get() != nullptr);
1011
Matteo Martincighed735042019-05-22 09:42:43 +01001012 ARMNN_NO_DEPRECATE_WARN_BEGIN
David Beck263e3492018-11-09 14:46:40 +00001013 auto backendSpecificOptimizations = backendPtr->GetOptimizations();
Matteo Martincighed735042019-05-22 09:42:43 +01001014 ARMNN_NO_DEPRECATE_WARN_END
1015
David Beck263e3492018-11-09 14:46:40 +00001016 if (!backendSpecificOptimizations.empty())
1017 {
1018 Optimizer::Pass(optNetObjPtr->GetGraph(), backendSpecificOptimizations);
1019 }
1020 }
1021
telsoa01c577f2c2018-08-31 09:22:23 +01001022 return optNet;
telsoa014fcda012018-03-09 14:13:49 +00001023}
1024
1025Network::Network()
Jan Eilers99d9d4a2019-11-06 10:02:16 +00001026: m_Graph(std::make_unique<Graph>()),
1027 m_Guid(profiling::ProfilingService::Instance().NextGuid())
telsoa014fcda012018-03-09 14:13:49 +00001028{
1029}
1030
1031Network::~Network()
1032{
1033}
1034
Jan Eilers99d9d4a2019-11-06 10:02:16 +00001035Status Network::PrintGraph()
1036{
1037 m_Graph->Print();
1038 return Status::Success;
1039}
1040
telsoa014fcda012018-03-09 14:13:49 +00001041IConnectableLayer* Network::AddInputLayer(LayerBindingId id, const char* name)
1042{
1043 return m_Graph->AddLayer<InputLayer>(id, name);
1044}
1045
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +00001046IConnectableLayer* Network::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
1047 const char* name)
1048{
1049 return m_Graph->AddLayer<BatchToSpaceNdLayer>(batchToSpaceNdDescriptor, name);
1050}
1051
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01001052IConnectableLayer* Network::AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
1053 const char* name)
1054{
1055 return m_Graph->AddLayer<ComparisonLayer>(comparisonDescriptor, name);
1056}
1057
josh minor4a3c6102020-01-06 16:40:46 -06001058IConnectableLayer* Network::AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
1059 const char* name)
1060{
1061 return m_Graph->AddLayer<ElementwiseUnaryLayer>(elementwiseUnaryDescriptor, name);
1062}
1063
telsoa014fcda012018-03-09 14:13:49 +00001064IConnectableLayer* Network::AddFullyConnectedLayerImpl(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001065 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001066 const Optional<ConstTensor>& biases,
telsoa01c577f2c2018-08-31 09:22:23 +01001067 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001068{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001069 if (fullyConnectedDescriptor.m_BiasEnabled && !biases.has_value())
telsoa014fcda012018-03-09 14:13:49 +00001070 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001071 throw InvalidArgumentException("AddFullyConnectedLayer: biases cannot be empty");
telsoa014fcda012018-03-09 14:13:49 +00001072 }
1073
1074 const auto layer = m_Graph->AddLayer<FullyConnectedLayer>(fullyConnectedDescriptor, name);
1075
1076 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
1077
1078 if (fullyConnectedDescriptor.m_BiasEnabled)
1079 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001080 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value());
telsoa014fcda012018-03-09 14:13:49 +00001081 }
1082
1083 return layer;
1084}
1085
1086IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001087 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001088 const Optional<ConstTensor>& biases,
telsoa01c577f2c2018-08-31 09:22:23 +01001089 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001090{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001091 return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name);
telsoa014fcda012018-03-09 14:13:49 +00001092}
1093
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001094IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
1095 const ConstTensor& weights,
1096 const char* name)
1097{
Matteo Martincighfc598e12019-05-14 10:36:13 +01001098 Optional<ConstTensor> biases;
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001099 return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name);
1100}
1101
telsoa014fcda012018-03-09 14:13:49 +00001102IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001103 const ConstTensor& weights,
1104 const ConstTensor& biases,
1105 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001106{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001107 Optional<ConstTensor> optionalBiases(biases);
1108 return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, optionalBiases, name);
telsoa014fcda012018-03-09 14:13:49 +00001109}
1110
Jim Flynne242f2d2019-05-22 14:24:13 +01001111IConnectableLayer* Network::AddConcatLayer(const ConcatDescriptor& concatDescriptor,
Jim Flynn906f9462019-05-10 13:55:21 +01001112 const char* name)
1113{
Jim Flynne242f2d2019-05-22 14:24:13 +01001114 return m_Graph->AddLayer<ConcatLayer>(concatDescriptor, name);
Jim Flynn906f9462019-05-10 13:55:21 +01001115}
1116
telsoa014fcda012018-03-09 14:13:49 +00001117IConnectableLayer* Network::AddConvolution2dLayerImpl(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001118 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001119 const Optional<ConstTensor>& biases,
telsoa01c577f2c2018-08-31 09:22:23 +01001120 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001121{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001122 if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value())
telsoa014fcda012018-03-09 14:13:49 +00001123 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001124 throw InvalidArgumentException("AddConvolution2dLayer: biases cannot be empty");
telsoa014fcda012018-03-09 14:13:49 +00001125 }
1126
1127 const auto layer = m_Graph->AddLayer<Convolution2dLayer>(convolution2dDescriptor, name);
1128
1129 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
1130
1131 if (convolution2dDescriptor.m_BiasEnabled)
1132 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001133 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value());
telsoa014fcda012018-03-09 14:13:49 +00001134 }
1135
1136 return layer;
1137}
1138
1139IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001140 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001141 const Optional<ConstTensor>& biases,
telsoa01c577f2c2018-08-31 09:22:23 +01001142 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001143{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001144 return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name);
telsoa014fcda012018-03-09 14:13:49 +00001145}
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001146
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001147IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
1148 const ConstTensor& weights,
1149 const char* name)
1150{
Matteo Martincighfc598e12019-05-14 10:36:13 +01001151 Optional<ConstTensor> biases;
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001152 return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name);
1153}
1154
telsoa014fcda012018-03-09 14:13:49 +00001155IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001156 const ConstTensor& weights,
1157 const ConstTensor& biases,
1158 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001159{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001160 Optional<ConstTensor> optionalBiases(biases);
1161 return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name);
telsoa014fcda012018-03-09 14:13:49 +00001162}
1163
1164IConnectableLayer* Network::AddDepthwiseConvolution2dLayerImpl(
1165 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
1166 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001167 const Optional<ConstTensor>& biases,
telsoa014fcda012018-03-09 14:13:49 +00001168 const char* name)
1169{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001170 if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value())
telsoa014fcda012018-03-09 14:13:49 +00001171 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001172 throw InvalidArgumentException("AddDepthwiseConvolution2dLayer: biases cannot be empty");
telsoa014fcda012018-03-09 14:13:49 +00001173 }
1174
Matteo Martincigh3d6898c2019-01-15 16:11:44 +00001175 const auto layer = m_Graph->AddLayer<DepthwiseConvolution2dLayer>(convolution2dDescriptor, name);
telsoa014fcda012018-03-09 14:13:49 +00001176
1177 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
1178
1179 if (convolution2dDescriptor.m_BiasEnabled)
1180 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001181 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value());
telsoa014fcda012018-03-09 14:13:49 +00001182 }
1183
1184 return layer;
1185}
1186
Aron Virginas-Tardd6247f2019-09-19 14:31:17 +01001187IConnectableLayer* Network::AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
1188 const char* name)
1189{
1190 return m_Graph->AddLayer<DepthToSpaceLayer>(depthToSpaceDescriptor, name);
1191}
1192
telsoa014fcda012018-03-09 14:13:49 +00001193IConnectableLayer* Network::AddDepthwiseConvolution2dLayer(
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001194 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
1195 const ConstTensor& weights,
1196 const Optional<ConstTensor>& biases,
1197 const char* name)
1198{
1199 return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name);
1200}
1201
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001202IConnectableLayer* Network::AddDepthwiseConvolution2dLayer(
telsoa014fcda012018-03-09 14:13:49 +00001203 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
1204 const ConstTensor& weights,
1205 const char* name)
1206{
Matteo Martincighfc598e12019-05-14 10:36:13 +01001207 Optional<ConstTensor> biases;
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001208 return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name);
telsoa014fcda012018-03-09 14:13:49 +00001209}
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001210
telsoa014fcda012018-03-09 14:13:49 +00001211IConnectableLayer* Network::AddDepthwiseConvolution2dLayer(
1212 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
1213 const ConstTensor& weights,
1214 const ConstTensor& biases,
1215 const char* name)
1216{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001217 Optional<ConstTensor> optionalBiases(biases);
1218 return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name);
telsoa014fcda012018-03-09 14:13:49 +00001219}
1220
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +00001221IConnectableLayer* Network::AddDetectionPostProcessLayer(const armnn::DetectionPostProcessDescriptor& descriptor,
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +00001222 const ConstTensor& anchors, const char* name)
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +00001223{
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +00001224 const auto layer = m_Graph->AddLayer<DetectionPostProcessLayer>(descriptor, name);
1225
1226 layer->m_Anchors = std::make_unique<ScopedCpuTensorHandle>(anchors);
1227
1228 return layer;
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +00001229}
1230
telsoa014fcda012018-03-09 14:13:49 +00001231IConnectableLayer* Network::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
1232 const char* name)
1233{
1234 return m_Graph->AddLayer<PermuteLayer>(permuteDescriptor, name);
1235}
1236
1237IConnectableLayer* Network::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
1238 const char* name)
1239{
1240 return m_Graph->AddLayer<Pooling2dLayer>(pooling2dDescriptor, name);
1241}
1242
1243IConnectableLayer* Network::AddActivationLayer(const ActivationDescriptor& activationDescriptor,
1244 const char* name)
1245{
1246 return m_Graph->AddLayer<ActivationLayer>(activationDescriptor, name);
1247}
1248
Nikhil Rajee391d52019-09-05 17:50:44 +01001249IConnectableLayer* Network::AddArgMinMaxLayer(const ArgMinMaxDescriptor& argMinMaxDescriptor,
1250 const char* name)
1251{
1252 return m_Graph->AddLayer<ArgMinMaxLayer>(argMinMaxDescriptor, name);
1253}
1254
telsoa01c577f2c2018-08-31 09:22:23 +01001255IConnectableLayer* Network::AddNormalizationLayer(const NormalizationDescriptor&
1256normalizationDescriptor,
telsoa014fcda012018-03-09 14:13:49 +00001257 const char* name)
1258{
1259 return m_Graph->AddLayer<NormalizationLayer>(normalizationDescriptor, name);
1260}
1261
Aron Virginas-Tar636ab402019-09-16 14:27:45 +01001262IConnectableLayer* Network::AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name)
1263{
1264 return m_Graph->AddLayer<SliceLayer>(sliceDescriptor, name);
1265}
1266
telsoa014fcda012018-03-09 14:13:49 +00001267IConnectableLayer* Network::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
1268 const char* name)
1269{
1270 return m_Graph->AddLayer<SoftmaxLayer>(softmaxDescriptor, name);
1271}
1272
1273IConnectableLayer* Network::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
1274 const char* name)
1275{
1276 return m_Graph->AddLayer<SplitterLayer>(splitterDescriptor, name);
1277}
1278
Nattapat Chaimanowong5a4304a2018-11-28 10:44:37 +00001279IConnectableLayer* Network::AddMaximumLayer(const char* name)
1280{
1281 return m_Graph->AddLayer<MaximumLayer>(name);
1282}
1283
Éanna Ó Catháin20e58802018-12-04 10:29:06 +00001284IConnectableLayer* Network::AddMinimumLayer(const char* name)
1285{
1286 return m_Graph->AddLayer<MinimumLayer>(name);
1287}
1288
Jim Flynne242f2d2019-05-22 14:24:13 +01001289IConnectableLayer* Network::AddMergerLayer(const MergerDescriptor& mergerDescriptor,
Jim Flynn906f9462019-05-10 13:55:21 +01001290 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001291{
Jim Flynne242f2d2019-05-22 14:24:13 +01001292 return AddConcatLayer(mergerDescriptor, name);
telsoa014fcda012018-03-09 14:13:49 +00001293}
1294
Kevin May868eb142019-09-04 17:29:31 +01001295IConnectableLayer* Network::AddAbsLayer(const char * name)
1296{
josh minor4a3c6102020-01-06 16:40:46 -06001297 return AddElementwiseUnaryLayer(ElementwiseUnaryDescriptor(UnaryOperation::Abs), name);
Kevin May868eb142019-09-04 17:29:31 +01001298}
1299
telsoa014fcda012018-03-09 14:13:49 +00001300IConnectableLayer* Network::AddAdditionLayer(const char* name)
1301{
1302 return m_Graph->AddLayer<AdditionLayer>(name);
1303}
1304
1305IConnectableLayer* Network::AddMultiplicationLayer(const char* name)
1306{
1307 return m_Graph->AddLayer<MultiplicationLayer>(name);
1308}
1309
1310IConnectableLayer* Network::AddOutputLayer(LayerBindingId id, const char* name)
1311{
1312 return m_Graph->AddLayer<OutputLayer>(id, name);
1313}
1314
1315IConnectableLayer* Network::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
1316 const ConstTensor& mean,
1317 const ConstTensor& variance,
1318 const ConstTensor& beta,
1319 const ConstTensor& gamma,
1320 const char* name)
1321{
1322 const auto layer = m_Graph->AddLayer<BatchNormalizationLayer>(desc, name);
1323
1324 layer->m_Mean = std::make_unique<ScopedCpuTensorHandle>(mean);
1325 layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(variance);
1326 layer->m_Beta = std::make_unique<ScopedCpuTensorHandle>(beta);
1327 layer->m_Gamma = std::make_unique<ScopedCpuTensorHandle>(gamma);
1328
1329 return layer;
1330}
1331
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01001332IConnectableLayer* Network::AddResizeBilinearLayer(const ResizeBilinearDescriptor& descriptor,
1333 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001334{
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01001335 ResizeDescriptor resizeDescriptor;
1336 resizeDescriptor.m_Method = ResizeMethod::Bilinear;
1337 resizeDescriptor.m_DataLayout = descriptor.m_DataLayout;
1338 resizeDescriptor.m_TargetWidth = descriptor.m_TargetWidth;
1339 resizeDescriptor.m_TargetHeight = descriptor.m_TargetHeight;
1340
1341 return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name);
telsoa014fcda012018-03-09 14:13:49 +00001342}
1343
Teresa Charlina9075df2019-06-27 15:41:57 +01001344IConnectableLayer* Network::AddResizeLayer(const ResizeDescriptor&
1345resizeDescriptor, const char* name)
1346{
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01001347 return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name);
Teresa Charlina9075df2019-06-27 15:41:57 +01001348}
1349
Kevin Mayce5045a2019-10-02 14:07:47 +01001350IConnectableLayer* Network::AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
1351 const char* name)
1352{
1353 return m_Graph->AddLayer<InstanceNormalizationLayer>(desc, name);
1354}
1355
Matteo Martincighbcd3c852018-09-28 14:14:12 +01001356IConnectableLayer* Network::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
1357 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001358{
Matteo Martincighbcd3c852018-09-28 14:14:12 +01001359 return m_Graph->AddLayer<L2NormalizationLayer>(desc, name);
telsoa014fcda012018-03-09 14:13:49 +00001360}
1361
Aron Virginas-Tarf982dea2019-10-11 14:07:53 +01001362IConnectableLayer* Network::AddLogSoftmaxLayer(const LogSoftmaxDescriptor& desc,
1363 const char* name)
1364{
1365 return m_Graph->AddLayer<LogSoftmaxLayer>(desc, name);
1366}
1367
telsoa014fcda012018-03-09 14:13:49 +00001368IConnectableLayer* Network::AddConstantLayer(const ConstTensor& input, const char* name)
1369{
telsoa01c577f2c2018-08-31 09:22:23 +01001370 auto layer = m_Graph->AddLayer<ConstantLayer>(name);
1371
1372 layer->m_LayerOutput = std::make_unique<ScopedCpuTensorHandle>(input);
1373
1374 return layer;
telsoa014fcda012018-03-09 14:13:49 +00001375}
1376
telsoa01c577f2c2018-08-31 09:22:23 +01001377IConnectableLayer* Network::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
1378 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001379{
1380 return m_Graph->AddLayer<ReshapeLayer>(reshapeDescriptor, name);
1381}
1382
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +00001383IConnectableLayer* Network::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
1384 const char* name)
1385{
1386 return m_Graph->AddLayer<SpaceToBatchNdLayer>(spaceToBatchNdDescriptor, name);
1387}
1388
Aron Virginas-Tar972af152019-06-11 14:14:03 +01001389IConnectableLayer* Network::AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
1390 const char* name)
1391{
1392 return m_Graph->AddLayer<SpaceToDepthLayer>(spaceToDepthDescriptor, name);
1393}
1394
telsoa014fcda012018-03-09 14:13:49 +00001395IConnectableLayer* Network::AddFloorLayer(const char* name)
1396{
1397 return m_Graph->AddLayer<FloorLayer>(name);
1398}
1399
telsoa01c577f2c2018-08-31 09:22:23 +01001400IConnectableLayer* Network::AddLstmLayer(const LstmDescriptor& descriptor,
1401 const LstmInputParams& params,
1402 const char* name)
1403{
1404 const auto layer = m_Graph->AddLayer<LstmLayer>(descriptor, name);
1405
1406 //Lstm Basic Parameters
1407 layer->m_BasicParameters.m_InputToForgetWeights =
1408 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights));
1409 layer->m_BasicParameters.m_InputToCellWeights =
1410 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights));
1411 layer->m_BasicParameters.m_InputToOutputWeights =
1412 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights));
1413 layer->m_BasicParameters.m_RecurrentToForgetWeights =
1414 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights));
1415 layer->m_BasicParameters.m_RecurrentToCellWeights =
1416 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights));
1417 layer->m_BasicParameters.m_RecurrentToOutputWeights =
1418 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights));
1419 layer->m_BasicParameters.m_ForgetGateBias =
1420 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias));
1421 layer->m_BasicParameters.m_CellBias =
1422 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias));
1423 layer->m_BasicParameters.m_OutputGateBias =
1424 std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias));
1425
1426 //Lstm Cifg parameters
1427 if(!descriptor.m_CifgEnabled)
1428 {
1429 if(params.m_InputToInputWeights == nullptr)
1430 {
1431 throw InvalidArgumentException("AddLstmLayer: Input To Input Weights cannot be NULL");
1432 }
1433 if(params.m_RecurrentToInputWeights == nullptr)
1434 {
1435 throw InvalidArgumentException(
1436 "AddLstmLayer: Recurrent To Input Weights cannot be NULL");
1437 }
1438 if(params.m_InputGateBias == nullptr)
1439 {
1440 throw InvalidArgumentException("AddLstmLayer: Input Gate Bias cannot be NULL");
1441 }
1442 layer->m_CifgParameters.m_InputToInputWeights =
1443 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights));
1444 layer->m_CifgParameters.m_RecurrentToInputWeights =
1445 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights));
1446 // In the VTS tests, cell-to-input weights may be null, even if the other CIFG params are not.
1447 if(params.m_CellToInputWeights != nullptr)
1448 {
1449 layer->m_CifgParameters.m_CellToInputWeights =
1450 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights));
1451 }
1452 layer->m_CifgParameters.m_InputGateBias =
1453 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias));
1454 }
1455
1456 //Lstm projection parameters
1457 if(descriptor.m_ProjectionEnabled)
1458 {
1459 if(params.m_ProjectionWeights == nullptr)
1460 {
1461 throw InvalidArgumentException("AddLstmLayer: Projection Weights cannot be NULL");
1462 }
1463 layer->m_ProjectionParameters.m_ProjectionWeights =
1464 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights));
1465 if(params.m_ProjectionBias != nullptr)
1466 {
1467 layer->m_ProjectionParameters.m_ProjectionBias =
1468 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias));
1469 }
1470 }
1471
1472 //Lstm Peephole params
1473 if(descriptor.m_PeepholeEnabled)
1474 {
1475 if(params.m_CellToForgetWeights == nullptr)
1476 {
1477 throw InvalidArgumentException("AddLstmLayer: Cell To Forget Weights cannot be NULL");
1478 }
1479 if(params.m_CellToOutputWeights == nullptr)
1480 {
1481 throw InvalidArgumentException("AddLstmLayer: Cell To Output Weights cannot be NULL");
1482 }
1483 layer->m_PeepholeParameters.m_CellToForgetWeights =
1484 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights));
1485 layer->m_PeepholeParameters.m_CellToOutputWeights =
1486 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights));
1487 }
Jan Eilersf8c62972019-07-17 11:07:49 +01001488
1489 //Lstm Layer Normalization params
1490 if(descriptor.m_LayerNormEnabled)
1491 {
1492 if(!descriptor.m_CifgEnabled)
1493 {
1494 if(params.m_InputLayerNormWeights == nullptr)
1495 {
1496 throw InvalidArgumentException("AddLstmLayer: Input layer normalization weights cannot be NULL");
1497 }
1498 layer->m_LayerNormParameters.m_InputLayerNormWeights =
1499 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputLayerNormWeights));
1500 }
1501
1502 if(params.m_ForgetLayerNormWeights == nullptr)
1503 {
1504 throw InvalidArgumentException("AddLstmLayer: Forget layer normalization weights cannot be NULL");
1505 }
1506 if(params.m_CellLayerNormWeights == nullptr)
1507 {
1508 throw InvalidArgumentException("AddLstmLayer: Cell layer normalization weights cannot be NULL");
1509 }
1510 if(params.m_OutputLayerNormWeights == nullptr)
1511 {
1512 throw InvalidArgumentException("AddLstmLayer: Output layer normalization weights cannot be NULL");
1513 }
1514 layer->m_LayerNormParameters.m_ForgetLayerNormWeights =
1515 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetLayerNormWeights));
1516 layer->m_LayerNormParameters.m_CellLayerNormWeights =
1517 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellLayerNormWeights));
1518 layer->m_LayerNormParameters.m_OutputLayerNormWeights =
1519 std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputLayerNormWeights));
1520 }
telsoa01c577f2c2018-08-31 09:22:23 +01001521 return layer;
1522}
1523
Francis Murtaghe7a86a42018-08-29 12:42:10 +01001524IConnectableLayer* Network::AddDivisionLayer(const char* name)
1525{
1526 return m_Graph->AddLayer<DivisionLayer>(name);
1527}
1528
David Beck19526222018-09-12 16:00:08 +01001529IConnectableLayer* Network::AddSubtractionLayer(const char* name)
1530{
1531 return m_Graph->AddLayer<SubtractionLayer>(name);
1532}
1533
narpra0132b90462018-09-13 11:07:48 +01001534IConnectableLayer* Network::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name)
1535{
1536 return m_Graph->AddLayer<MeanLayer>(meanDescriptor,name);
1537}
1538
Mohamed Nour Abouelseoud5662c202018-09-24 13:30:09 +01001539IConnectableLayer* Network::AddPadLayer(const PadDescriptor& padDescriptor, const char* name)
1540{
1541 return m_Graph->AddLayer<PadLayer>(padDescriptor,name);
1542}
1543
Derek Lambertia9cca6a2019-03-25 15:41:58 +00001544IConnectableLayer *Network::AddQuantizeLayer(const char *name)
1545{
1546 return m_Graph->AddLayer<QuantizeLayer>(name);
1547}
1548
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +00001549IConnectableLayer* Network::AddDequantizeLayer(const char* name)
1550{
1551 return m_Graph->AddLayer<DequantizeLayer>(name);
1552}
1553
Conor Kennedy430b5d82018-11-14 15:28:28 +00001554IConnectableLayer* Network::AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
1555 const char* name)
1556{
1557 return m_Graph->AddLayer<StridedSliceLayer>(stridedSliceDescriptor, name);
1558}
1559
Matteo Martincigh59a950c2018-12-13 12:48:25 +00001560IConnectableLayer* Network::AddGreaterLayer(const char* name)
1561{
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01001562 return AddComparisonLayer(ComparisonDescriptor(ComparisonOperation::Greater), name);
Matteo Martincigh59a950c2018-12-13 12:48:25 +00001563}
1564
FrancisMurtagh20995952018-12-17 12:11:36 +00001565IConnectableLayer* Network::AddEqualLayer(const char* name)
1566{
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01001567 return AddComparisonLayer(ComparisonDescriptor(ComparisonOperation::Equal), name);
FrancisMurtagh20995952018-12-17 12:11:36 +00001568}
1569
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +00001570IConnectableLayer* Network::AddRsqrtLayer(const char * name)
1571{
josh minor4a3c6102020-01-06 16:40:46 -06001572 return AddElementwiseUnaryLayer(ElementwiseUnaryDescriptor(UnaryOperation::Rsqrt), name);
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +00001573}
1574
narpra01b89b05f2019-01-16 09:53:09 +00001575IConnectableLayer* Network::AddGatherLayer(const char* name)
1576{
1577 return m_Graph->AddLayer<GatherLayer>(name);
1578}
1579
Nattapat Chaimanowong1f886302019-04-05 13:37:19 +01001580IConnectableLayer* Network::AddMergeLayer(const char* name)
1581{
1582 return m_Graph->AddLayer<MergeLayer>(name);
1583}
1584
Sadik Armaganeff363d2019-04-05 15:25:46 +01001585IConnectableLayer* Network::AddSwitchLayer(const char* name)
1586{
1587 return m_Graph->AddLayer<SwitchLayer>(name);
1588}
1589
Matteo Martincigh0e406ee2019-06-12 15:42:18 +01001590IConnectableLayer* Network::AddPreluLayer(const char* name)
1591{
1592 return m_Graph->AddLayer<PreluLayer>(name);
1593}
1594
Aron Virginas-Tar639fb042019-06-20 14:28:19 +01001595IConnectableLayer* Network::AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
1596 const ConstTensor& weights,
1597 const Optional<ConstTensor>& biases,
1598 const char* name)
1599{
1600 if (descriptor.m_BiasEnabled && !biases.has_value())
1601 {
1602 throw InvalidArgumentException("AddTransposeConvolution2dLayer: Biases cannot be empty");
1603 }
1604
1605 const auto layer = m_Graph->AddLayer<TransposeConvolution2dLayer>(descriptor, name);
1606
1607 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
1608
1609 if (descriptor.m_BiasEnabled)
1610 {
1611 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value());
1612 }
1613
1614 return layer;
1615}
1616
Mike Kellyc9ea45a2020-02-28 18:11:58 +00001617IConnectableLayer* Network::AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
1618 const char* name)
1619{
1620 return m_Graph->AddLayer<TransposeLayer>(transposeDescriptor, name);
1621}
1622
Matthew Jackson2b8c1da2019-07-04 14:59:16 +01001623IConnectableLayer* Network::AddStackLayer(const StackDescriptor& stackDescriptor,
1624 const char* name)
1625{
1626 return m_Graph->AddLayer<StackLayer>(stackDescriptor, name);
1627}
1628
Derek Lamberti013c3902019-10-21 10:46:16 +01001629
1630IConnectableLayer* Network::AddStandInLayer(const StandInDescriptor& desc,
1631 const char* name)
1632{
1633 return m_Graph->AddLayer<StandInLayer>(desc, name);
1634}
1635
James Conroyee18dc82019-07-17 11:27:46 +01001636IConnectableLayer* Network::AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
1637 const char* name)
1638{
1639 const auto layer = m_Graph->AddLayer<QuantizedLstmLayer>(name);
1640
1641 // InputToX weights
1642 layer->m_QuantizedLstmParameters.m_InputToInputWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001643 std::make_unique<ScopedCpuTensorHandle>(params.GetInputToInputWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001644 layer->m_QuantizedLstmParameters.m_InputToForgetWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001645 std::make_unique<ScopedCpuTensorHandle>(params.GetInputToForgetWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001646 layer->m_QuantizedLstmParameters.m_InputToCellWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001647 std::make_unique<ScopedCpuTensorHandle>(params.GetInputToCellWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001648 layer->m_QuantizedLstmParameters.m_InputToOutputWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001649 std::make_unique<ScopedCpuTensorHandle>(params.GetInputToOutputWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001650
1651 // RecurrentToX weights
1652 layer->m_QuantizedLstmParameters.m_RecurrentToInputWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001653 std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToInputWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001654 layer->m_QuantizedLstmParameters.m_RecurrentToForgetWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001655 std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToForgetWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001656 layer->m_QuantizedLstmParameters.m_RecurrentToCellWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001657 std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToCellWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001658 layer->m_QuantizedLstmParameters.m_RecurrentToOutputWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001659 std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToOutputWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001660
1661 // Bias
1662 layer->m_QuantizedLstmParameters.m_InputGateBias =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001663 std::make_unique<ScopedCpuTensorHandle>(params.GetInputGateBias());
James Conroyee18dc82019-07-17 11:27:46 +01001664 layer->m_QuantizedLstmParameters.m_ForgetGateBias =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001665 std::make_unique<ScopedCpuTensorHandle>(params.GetForgetGateBias());
James Conroyee18dc82019-07-17 11:27:46 +01001666 layer->m_QuantizedLstmParameters.m_CellBias =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001667 std::make_unique<ScopedCpuTensorHandle>(params.GetCellBias());
James Conroyee18dc82019-07-17 11:27:46 +01001668 layer->m_QuantizedLstmParameters.m_OutputGateBias =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001669 std::make_unique<ScopedCpuTensorHandle>(params.GetOutputGateBias());
James Conroyee18dc82019-07-17 11:27:46 +01001670
1671 return layer;
1672}
1673
Mike Kelly8c1701a2019-02-11 17:01:27 +00001674void Network::Accept(ILayerVisitor& visitor) const
1675{
1676 for (auto layer : GetGraph())
1677 {
1678 layer->Accept(visitor);
1679 };
1680}
1681
telsoa014fcda012018-03-09 14:13:49 +00001682OptimizedNetwork::OptimizedNetwork(std::unique_ptr<Graph> graph)
Jan Eilers99d9d4a2019-11-06 10:02:16 +00001683 : m_Graph(std::move(graph)),
1684 m_Guid(profiling::ProfilingService::Instance().NextGuid())
telsoa014fcda012018-03-09 14:13:49 +00001685{
1686}
1687
1688OptimizedNetwork::~OptimizedNetwork()
1689{
1690}
1691
1692} // namespace armnn