blob: a443721a45bd14c91344fc0334146a1f681a8c34 [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
Narumol Prangnawarat250d3922020-03-30 16:11:04 +0100149template <typename LayerT>
150LayerT* ConvertBf16ToFp32Weight(Layer* l)
151{
152 LayerT* layer = boost::polymorphic_downcast<LayerT*>(l);
153 if ((layer->GetType() == LayerType::Convolution2d || layer->GetType() == LayerType::FullyConnected)
154 && layer->m_Weight)
155 {
156 const TensorInfo& info = layer->m_Weight->GetTensorInfo();
157
158 if (info.GetDataType() == DataType::BFloat16)
159 {
160 std::vector<float> newValues(info.GetNumElements());
161
162 armnnUtils::FloatingPointConverter::ConvertBFloat16ToFloat32(
163 layer->m_Weight->template GetTensor<armnn::BFloat16>(), info.GetNumElements(), newValues.data());
164
165 TensorInfo newInfo(info.GetShape(), DataType::Float32);
166 ConstTensor newInput(newInfo, newValues);
167 layer->m_Weight.reset(new ScopedCpuTensorHandle(newInput));
168 }
169 }
170 return layer;
171}
172
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000173OptimizationResult AttemptBackendAssignment(BackendSettings& backendSettings,
174 Graph& graph,
175 Layer* layer,
176 BackendId backend,
177 DataType dataTypeIn,
178 DataType dataTypeOut,
179 const std::vector<BackendId>& availablePreferredBackends,
180 std::string& reasonIfUnsupported,
181 Optional<std::vector<std::string>&> errMessages)
182{
183 OptimizationResult result;
184
185 // Helper lambda to compose meaningful error message before returning with error
186 auto ReturnError = [&](const Layer* layer)
187 {
188 return ReturnWithError(result, layer, backendSettings, errMessages);
189 };
190
191 // need to set the compute device on the layer
192 // before we can check if it is supported
193 layer->SetBackendId(backend);
194 if (!IWorkloadFactory::IsLayerSupported(*layer, EmptyOptional(), reasonIfUnsupported))
195 {
196 if (dataTypeIn == DataType::Float16 || dataTypeOut == DataType::Float16)
197 {
198 if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported)
199 && layer->GetType() != LayerType::ConvertFp32ToFp16
200 && layer->GetType() != LayerType::ConvertFp16ToFp32)
201 {
202 // Insert FP16 -> FP32 conversion layer before current layer
203 std::vector<ConvertFp16ToFp32Layer*> convertFp16ToFp32Layers;
204 if (dataTypeIn == DataType::Float16)
205 {
206 convertFp16ToFp32Layers =
207 InsertConvertFp16ToFp32LayersBefore(graph, *layer);
208 }
209
210 // Insert FP32 -> FP16 conversion layer after current layer
211 std::vector<ConvertFp32ToFp16Layer*> convertFp32ToFp16Layers;
212 if (dataTypeOut == DataType::Float16)
213 {
214 convertFp32ToFp16Layers =
215 InsertConvertFp32ToFp16LayersAfter(graph, *layer);
216 }
217
218 // Assign a supported backend to the newly introduced conversion layers
219 auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend)
220 {
221 bool supportedBackendFound = false;
222 std::string reasonIfUnsupported;
223
224 // Try preferred backend first
225 layer->SetBackendId(preferredBackend);
226 if (IWorkloadFactory::IsLayerSupported(*layer,
227 EmptyOptional(),
228 reasonIfUnsupported))
229 {
230 supportedBackendFound = true;
231 }
232 else
233 {
234 for (const auto& backend : availablePreferredBackends)
235 {
236 // Skip preferred backend (we already determined that it is not supported)
237 if (backend == preferredBackend)
238 {
239 continue;
240 }
241
242 layer->SetBackendId(backend);
243 if (IWorkloadFactory::IsLayerSupported(*layer,
244 EmptyOptional(),
245 reasonIfUnsupported))
246 {
247 supportedBackendFound = true;
248 break;
249 }
250 }
251 }
252
253 return supportedBackendFound;
254 };
255
256 for (ConvertFp16ToFp32Layer* convertLayer : convertFp16ToFp32Layers)
257 {
258 if (!AssignFirstSupportedBackend(convertLayer, backend))
259 {
260 return ReturnError(convertLayer);
261 }
262 }
263
264 for (ConvertFp32ToFp16Layer* convertLayer : convertFp32ToFp16Layers)
265 {
266 if (!AssignFirstSupportedBackend(convertLayer, backend))
267 {
268 return ReturnError(convertLayer);
269 }
270 }
271
272 return result;
273 }
274 }
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000275 else if (dataTypeIn == DataType::BFloat16 || dataTypeOut == DataType::BFloat16)
276 {
277 if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported)
278 && layer->GetType() != LayerType::ConvertFp32ToBf16
279 && layer->GetType() != LayerType::ConvertBf16ToFp32)
280 {
281 // Insert BF16 -> FP32 conversion layer before current layer
282 std::vector<ConvertBf16ToFp32Layer*> convertBf16ToFp32Layers;
283 if (dataTypeIn == DataType::BFloat16)
284 {
285 convertBf16ToFp32Layers =
286 InsertConvertBf16ToFp32LayersBefore(graph, *layer);
Narumol Prangnawarat250d3922020-03-30 16:11:04 +0100287 if (layer->GetType() == LayerType::Convolution2d)
288 {
289 ConvertBf16ToFp32Weight<Convolution2dLayer>(layer);
290 }
291 else if (layer->GetType() == LayerType::FullyConnected)
292 {
293 ConvertBf16ToFp32Weight<FullyConnectedLayer>(layer);
294 }
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +0000295 }
296
297 // Insert FP32 -> BF16 conversion layer after current layer
298 std::vector<ConvertFp32ToBf16Layer*> convertFp32ToBf16Layers;
299 if (dataTypeOut == DataType::BFloat16)
300 {
301 convertFp32ToBf16Layers =
302 InsertConvertFp32ToBf16LayersAfter(graph, *layer);
303 }
304
305 // Assign a supported backend to the newly introduced conversion layers
306 auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend)
307 {
308 bool supportedBackendFound = false;
309 std::string reasonIfUnsupported;
310
311 // Try preferred backend first
312 layer->SetBackendId(preferredBackend);
313 if (IWorkloadFactory::IsLayerSupported(*layer,
314 EmptyOptional(),
315 reasonIfUnsupported))
316 {
317 supportedBackendFound = true;
318 }
319 else
320 {
321 for (const auto& backend : availablePreferredBackends)
322 {
323 // Skip preferred backend (we already determined that it is not supported)
324 if (backend == preferredBackend)
325 {
326 continue;
327 }
328
329 layer->SetBackendId(backend);
330 if (IWorkloadFactory::IsLayerSupported(*layer,
331 EmptyOptional(),
332 reasonIfUnsupported))
333 {
334 supportedBackendFound = true;
335 break;
336 }
337 }
338 }
339
340 return supportedBackendFound;
341 };
342
343 for (ConvertBf16ToFp32Layer* convertLayer : convertBf16ToFp32Layers)
344 {
345 if (!AssignFirstSupportedBackend(convertLayer, backend))
346 {
347 return ReturnError(convertLayer);
348 }
349 }
350
351 for (ConvertFp32ToBf16Layer* convertLayer : convertFp32ToBf16Layers)
352 {
353 if (!AssignFirstSupportedBackend(convertLayer, backend))
354 {
355 return ReturnError(convertLayer);
356 }
357 }
358
359 return result;
360 }
361 }
362
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000363 std::stringstream warningMsg;
364 warningMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType())
365 << " is not supported on requested backend " << layer->GetBackendId().Get()
366 << " for input data type " << GetDataTypeName(dataTypeIn)
367 << " and output data type " << GetDataTypeName(dataTypeOut)
368 << " (reason: " << reasonIfUnsupported
369 << "), falling back to the next backend.";
370 ReportWarning(warningMsg.str(), errMessages);
371
372 return OptimizationResult(true, false);
373 }
374 else
375 {
376 return result;
377 }
378}
379
380
Matteo Martincigh49124022019-01-11 13:25:59 +0000381OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr,
382 BackendSettings& backendSettings,
383 Graph::Iterator& firstLayer,
384 Graph::Iterator& lastLayer,
385 Optional<std::vector<std::string>&> errMessages)
telsoa014fcda012018-03-09 14:13:49 +0000386{
Matteo Martincigh49124022019-01-11 13:25:59 +0000387 OptimizationResult result;
telsoa014fcda012018-03-09 14:13:49 +0000388
Matteo Martincigh49124022019-01-11 13:25:59 +0000389 // Helper lambda to compose meaningful error message before returning with error
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000390 auto ReturnError = [&](const Layer* layer)
391 {
392 return ReturnWithError(result, layer, backendSettings, errMessages);
393 };
Matteo Martincigh49124022019-01-11 13:25:59 +0000394
telsoa01c577f2c2018-08-31 09:22:23 +0100395
Matteo Martincigh49124022019-01-11 13:25:59 +0000396 auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends();
397 if (availablePreferredBackends.empty())
telsoa01c577f2c2018-08-31 09:22:23 +0100398 {
Matteo Martincigh49124022019-01-11 13:25:59 +0000399 std::stringstream failureMsg;
400 failureMsg << "No preferred backends are available";
401 ReportError(failureMsg.str(), errMessages);
402
403 result.m_Error = true;
404 return result;
405 }
406
407 for (auto it = firstLayer; it != lastLayer; ++it)
408 {
409 auto layer = *it;
Aron Virginas-Tar87972be2019-11-13 15:16:28 +0000410
411 DataType dataTypeIn = layer->GetNumInputSlots() == 0 ? DataType::Float32 :
412 layer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo().GetDataType();
413 DataType dataTypeOut = layer->GetNumOutputSlots() == 0 ? DataType::Float32 :
414 layer->GetOutputSlot(0).GetTensorInfo().GetDataType();
415
telsoa01c577f2c2018-08-31 09:22:23 +0100416 std::string reasonIfUnsupported;
417 bool found = false;
jimfly016b0b53d2018-10-08 14:43:01 +0100418 if (!CheckScaleSetOnQuantizedType(layer, errMessages))
419 {
420 // don't bomb immediately, find all the quantized outputs
421 // which haven't had a scale set and report them all back.
Matteo Martincigh49124022019-01-11 13:25:59 +0000422 result.m_Error = true;
jimfly016b0b53d2018-10-08 14:43:01 +0100423 }
Matteo Martincigh49124022019-01-11 13:25:59 +0000424
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000425 // First try assign layer to hint backend
426 if (layer->GetBackendHint().has_value() &&
427 backendSettings.IsBackendSupported(layer->GetBackendHint().value()) &&
428 AttemptBackendAssignment(backendSettings,
429 optNetObjPtr->GetGraph(),
430 layer,
431 layer->GetBackendHint().value(),
432 dataTypeIn,
433 dataTypeOut,
434 availablePreferredBackends,
435 reasonIfUnsupported,
436 errMessages).IsOk())
telsoa01c577f2c2018-08-31 09:22:23 +0100437 {
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000438 found = true;
439 backendSettings.m_SelectedBackends.insert(layer->GetBackendHint().value());
440 }
441 else
442 {
443 // Try assign layer to prefered list of backends
444 for (const auto& backend : availablePreferredBackends)
telsoa01c577f2c2018-08-31 09:22:23 +0100445 {
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000446 if (layer->GetBackendHint().has_value() &&
447 layer->GetBackendHint().value() == backend)
telsoa01c577f2c2018-08-31 09:22:23 +0100448 {
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000449 continue; //Don't re-test the backend hint
telsoa01c577f2c2018-08-31 09:22:23 +0100450 }
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000451
452 OptimizationResult res = AttemptBackendAssignment(backendSettings,
453 optNetObjPtr->GetGraph(),
454 layer,
455 backend,
456 dataTypeIn,
457 dataTypeOut,
458 availablePreferredBackends,
459 reasonIfUnsupported,
460 errMessages);
461
462 if (res.IsOk())
463 {
464 found = true;
465 backendSettings.m_SelectedBackends.insert(backend);
466 break;
467 }
468 else if (res.IsError())
469 {
470 return res; // Cannot continue.
471 // Note: we don't need to log the error as it would already
472 // be logged in AttemptBackendAssignment().
473 }
474 else
475 {
476 BOOST_ASSERT_MSG(res.IsWarningOnly(), "OptimizationResult in unexpected state.");
477 }
telsoa01c577f2c2018-08-31 09:22:23 +0100478 }
479 }
480
481 // If the layer is unsupported by any devices, log and return a null network.
Matteo Martincigh49124022019-01-11 13:25:59 +0000482 if (!found)
483 {
telsoa01c577f2c2018-08-31 09:22:23 +0100484 // NOTE: if the layer is not an operation queue type AND we have not got CpuRef as a
485 // fallback we should set the compute device on the layer to CpuRef (these are not
486 // available as accelerated operations, or are only available under certain
487 // conditions, currently they comprise MemCopy, Constant, Permute)
488 armnn::LayerType layerType = layer->GetType();
Matteo Martincigh49124022019-01-11 13:25:59 +0000489 if (!backendSettings.IsCpuRefUsed() && (layerType == armnn::LayerType::MemCopy ||
490 layerType == armnn::LayerType::Constant ||
491 layerType == armnn::LayerType::Permute))
telsoa01c577f2c2018-08-31 09:22:23 +0100492 {
Matteo Martincigh49124022019-01-11 13:25:59 +0000493 BackendId cpuBackendId(armnn::Compute::CpuRef);
494 layer->SetBackendId(cpuBackendId);
495 backendSettings.m_SelectedBackends.insert(cpuBackendId);
telsoa01c577f2c2018-08-31 09:22:23 +0100496 }
497 else
498 {
Derek Lamberti4a9e24b2020-01-03 16:53:38 +0000499 return ReturnError(layer);
telsoa01c577f2c2018-08-31 09:22:23 +0100500 }
501 }
502 }
Matteo Martincigh49124022019-01-11 13:25:59 +0000503
504 return result;
505}
506
Matteo Martincighadddddb2019-01-24 14:06:23 +0000507OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr,
508 BackendSettings& backendSettings,
Derek Lambertiff05cc52019-04-26 13:05:17 +0100509 SubgraphView& subgraph,
Matteo Martincighadddddb2019-01-24 14:06:23 +0000510 Optional<std::vector<std::string>&> errMessages)
Matteo Martincigh49124022019-01-11 13:25:59 +0000511{
Derek Lambertiff05cc52019-04-26 13:05:17 +0100512 Graph::Iterator firstLayer = subgraph.begin();
513 Graph::Iterator lastLayer = subgraph.end();
Matteo Martincighadddddb2019-01-24 14:06:23 +0000514 return AssignBackends(optNetObjPtr,
515 backendSettings,
516 firstLayer,
517 lastLayer,
518 errMessages);
519}
520
Derek Lamberti84da38b2019-06-13 11:40:08 +0100521BackendsMap CreateSupportedBackends(TensorHandleFactoryRegistry& handleFactoryRegistry,
522 BackendSettings& backendSettings)
523{
524 BackendsMap backends;
525 auto const& backendRegistry = BackendRegistryInstance();
526 for (auto&& selectedBackend : backendSettings.m_SupportedBackends)
527 {
528 auto backendFactory = backendRegistry.GetFactory(selectedBackend);
529 auto backendObjPtr = backendFactory();
530 BOOST_ASSERT(backendObjPtr);
531
532 backendObjPtr->RegisterTensorHandleFactories(handleFactoryRegistry);
533
534 backends[backendObjPtr->GetId()] = std::move(backendObjPtr);
535 }
536
537 return backends;
538}
539
Matteo Martincighadddddb2019-01-24 14:06:23 +0000540OptimizationResult ApplyBackendOptimizations(OptimizedNetwork* optNetObjPtr,
541 BackendSettings& backendSettings,
Derek Lamberti84da38b2019-06-13 11:40:08 +0100542 BackendsMap& backends,
Matteo Martincighadddddb2019-01-24 14:06:23 +0000543 Optional<std::vector<std::string>&> errMessages)
544{
545 BOOST_ASSERT(optNetObjPtr);
Matteo Martincigh49124022019-01-11 13:25:59 +0000546
547 OptimizationResult result;
548
Matteo Martincighadddddb2019-01-24 14:06:23 +0000549 // Get the optimized graph
550 Graph& optGraph = optNetObjPtr->GetGraph();
Matteo Martincigh49124022019-01-11 13:25:59 +0000551
Matteo Martincighadddddb2019-01-24 14:06:23 +0000552 // Run backend specific optimizations
Matteo Martincighadddddb2019-01-24 14:06:23 +0000553 for (auto&& selectedBackend : backendSettings.m_SelectedBackends)
Matteo Martincigh49124022019-01-11 13:25:59 +0000554 {
Derek Lamberti84da38b2019-06-13 11:40:08 +0100555 auto backendObjPtr = backends.find(selectedBackend)->second.get();
Matteo Martincighadddddb2019-01-24 14:06:23 +0000556 BOOST_ASSERT(backendObjPtr);
557
558 // Select sub-graphs based on backend
Derek Lambertiff05cc52019-04-26 13:05:17 +0100559 SubgraphViewSelector::Subgraphs subgraphs =
Rob Hughes65c32262019-07-23 15:33:39 +0100560 SubgraphViewSelector::SelectSubgraphs(optGraph,
Matteo Martincigh602af092019-05-01 10:31:27 +0100561 // Select layers assigned to the requested backend
562 [&backendObjPtr](const Layer& layer)
563 {
564 return layer.GetType() != LayerType::Input &&
565 layer.GetType() != LayerType::Output &&
566 layer.GetBackendId() == backendObjPtr->GetId();
567 });
Derek Lambertiff05cc52019-04-26 13:05:17 +0100568 if (subgraphs.empty())
Matteo Martincigh49124022019-01-11 13:25:59 +0000569 {
Matteo Martincighadddddb2019-01-24 14:06:23 +0000570 // No sub-graphs found, try with next selected backend
571 continue;
Matteo Martincigh49124022019-01-11 13:25:59 +0000572 }
Matteo Martincighadddddb2019-01-24 14:06:23 +0000573
574 // Try to optimize each sub-graph
Derek Lambertiff05cc52019-04-26 13:05:17 +0100575 for (auto& subgraph : subgraphs)
Matteo Martincigh49124022019-01-11 13:25:59 +0000576 {
Matteo Martincighadddddb2019-01-24 14:06:23 +0000577 // Try to optimize the current sub-graph
Matteo Martincigh84924332019-05-09 12:46:16 +0100578 OptimizationViews optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraph);
579 BOOST_ASSERT(optimizationViews.Validate(*subgraph));
Matteo Martincighadddddb2019-01-24 14:06:23 +0000580
581 // Optimization attempted, check the resulting optimized sub-graph
Matteo Martincigh84924332019-05-09 12:46:16 +0100582 for (auto& substitution : optimizationViews.GetSubstitutions())
Matteo Martincighadddddb2019-01-24 14:06:23 +0000583 {
584 // Sub-graph optimized, substitute the sub-graph with the new optimized one in the main optimized graph
Matteo Martincigh84924332019-05-09 12:46:16 +0100585 SubgraphView& replacementSubgraph = substitution.m_ReplacementSubgraph;
586 SubgraphView& substitutableSubgraph = substitution.m_SubstitutableSubgraph;
587 optGraph.SubstituteSubgraph(substitutableSubgraph, replacementSubgraph);
Matteo Martincighadddddb2019-01-24 14:06:23 +0000588
589 // Assign the current backend to the optimized sub-graph
Matteo Martincigh84924332019-05-09 12:46:16 +0100590 std::for_each(replacementSubgraph.begin(), replacementSubgraph.end(), [&selectedBackend](Layer* l)
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100591 {
592 BOOST_ASSERT(l);
593 l->SetBackendId(selectedBackend);
594 });
Matteo Martincighadddddb2019-01-24 14:06:23 +0000595 }
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100596
Matteo Martincigh84924332019-05-09 12:46:16 +0100597 if (!optimizationViews.GetFailedSubgraphs().empty())
Matteo Martincighadddddb2019-01-24 14:06:23 +0000598 {
Matteo Martincighadddddb2019-01-24 14:06:23 +0000599 std::stringstream warningMsg;
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100600 warningMsg << "Some sub-graph(s) failed to optimized on " << backendObjPtr->GetId() << " backend.";
Matteo Martincighadddddb2019-01-24 14:06:23 +0000601 ReportWarning(warningMsg.str(), errMessages);
602
603 // Failed to optimize the given sub-graph, re-assign the sub-graph layers to other available backends
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100604 BackendSettings settingsCopy(backendSettings);
Matteo Martincighadddddb2019-01-24 14:06:23 +0000605 if (!backendObjPtr->GetId().IsCpuRef())
606 {
607 // Add the current backend to the list of backends to ignore
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100608 settingsCopy.m_IgnoredBackends.insert(backendObjPtr->GetId());
Matteo Martincighadddddb2019-01-24 14:06:23 +0000609 }
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100610
611 int count=0;
Matteo Martincigh84924332019-05-09 12:46:16 +0100612 for (auto& failedSubgraph : optimizationViews.GetFailedSubgraphs())
Matteo Martincighadddddb2019-01-24 14:06:23 +0000613 {
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100614 // An error occurred: the optimization was attempted but not performed, try different backends
615 std::stringstream subgraphMsg;
616 subgraphMsg << "Re-assigning backends to " << failedSubgraph.GetLayers().size()
617 << " layers inside sub-graph " << count++;
Matteo Martincigh328d92b2019-07-04 17:52:55 +0100618 ReportWarning(subgraphMsg.str(), errMessages);
Derek Lambertic2fe5fb2019-05-08 10:23:08 +0100619
620 OptimizationResult reassignmentResult = AssignBackends(optNetObjPtr,
621 settingsCopy,
622 *subgraph,
623 errMessages);
624 if (reassignmentResult.m_Error)
625 {
626 // Failed to re-assign one of the remaining backends to each layer of the sub-graph
627 result.m_Error = true;
628 return result;
629 }
Matteo Martincighadddddb2019-01-24 14:06:23 +0000630 }
Matteo Martincigh49124022019-01-11 13:25:59 +0000631 }
632 }
633 }
634
635 return result;
636}
637
Derek Lamberti84da38b2019-06-13 11:40:08 +0100638bool RequiresCopy(ITensorHandleFactory::FactoryId src,
639 ITensorHandleFactory::FactoryId dst,
640 TensorHandleFactoryRegistry& registry)
641{
642 if (src != dst)
643 {
644 ITensorHandleFactory* srcFactory = registry.GetFactory(src);
645 ITensorHandleFactory* dstFactory = registry.GetFactory(dst);
646
Matteo Martincigha6539ed2019-08-27 13:43:32 +0100647 if (srcFactory && dstFactory &&
648 (srcFactory->GetExportFlags() & dstFactory->GetImportFlags()) != 0)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100649 {
650 return false;
651 }
652 return true;
653 }
654 return false;
655}
656
657// Find the handle factory for the input layer which results in fewest required copies.
658ITensorHandleFactory::FactoryId CalculateSlotOptionForInput(BackendsMap& backends,
659 OutputSlot& slot,
660 TensorHandleFactoryRegistry& registry)
661{
662 Layer& layer = slot.GetOwningLayer();
663 BOOST_ASSERT(layer.GetType() == LayerType::Input);
664
665 // Explicitly select the tensorhandle factory for InputLayer because the rules for it are slightly different. It
666 // doesn't matter which backend it is assigned to because they all use the same implementation, which
667 // requires Map/Unmap support. This means that, so long as the handle type supports map/unmap semantics, we can
668 // select a factory with maximum compatibility with the layers connected to the InputLayer.
669
670 // First ensure the from backends can support the TensorHandeAPI
671 auto frmBackend = backends.find(layer.GetBackendId());
672 if (frmBackend == backends.end() ||
673 !frmBackend->second->SupportsTensorAllocatorAPI())
674 {
675 return ITensorHandleFactory::LegacyFactoryId;
676 }
677
678 // Go through all connections to the output slot and determine the TensorHandleFactory which results in the
679 // fewest copies.
680 std::map<ITensorHandleFactory::FactoryId, int> factoryScores;
681 int topScore = 0;
682 ITensorHandleFactory::FactoryId topChoice = ITensorHandleFactory::LegacyFactoryId;
683
684 for (auto&& connection : slot.GetConnections())
685 {
686 const Layer& connectedLayer = connection->GetOwningLayer();
687
688 auto toBackend = backends.find(connectedLayer.GetBackendId());
689 BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer");
690
691 if (!toBackend->second.get()->SupportsTensorAllocatorAPI())
692 {
693 // The destination backend does not support the tensor allocator API, move to the next one
694 continue;
695 }
696
697 auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences();
698 for (auto&& dst : dstPrefs)
699 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100700 // Input layers use the mem copy workload or import, so the selected factory must
701 // support either the map/unmap API or Import API
Derek Lamberti84da38b2019-06-13 11:40:08 +0100702 ITensorHandleFactory* factory = registry.GetFactory(dst);
Derek Lambertif674aa02019-08-01 15:56:25 +0100703 if (!factory->SupportsMapUnmap() &&
704 !CheckFlag(factory->GetImportFlags(), MemorySource::Malloc)) // Just support cpu mem imports for now
Derek Lamberti84da38b2019-06-13 11:40:08 +0100705 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100706 // The current tensor handle factory does not support the map/unmap or import
707 // strategy, move to the next one
Derek Lamberti84da38b2019-06-13 11:40:08 +0100708 continue;
709 }
710
711 auto it = factoryScores.find(dst);
712 if (it == factoryScores.end())
713 {
714 // Add new score to the table
715 factoryScores[dst] = 0;
716 if (topChoice == ITensorHandleFactory::LegacyFactoryId)
717 {
718 topChoice = dst;
719 }
720 }
721 else
722 {
723 // Increase the score
724 factoryScores[dst]++;
725
726 // Track the best option
727 if (factoryScores[dst] > topScore)
728 {
729 topScore = factoryScores[dst];
730 topChoice = dst;
731 }
732 }
733 }
734 }
735
736 return topChoice;
737}
738
739// Find the handle factory for the output layer which results in fewest required copies.
740ITensorHandleFactory::FactoryId CalculateSlotOptionForOutput(BackendsMap& backends,
741 OutputSlot& slot,
742 TensorHandleFactoryRegistry& registry)
743{
Jan Eilers8eb25602020-03-09 12:13:48 +0000744 IgnoreUnused(backends, slot, registry);
Derek Lamberti94a88d22019-12-10 21:12:59 +0000745 return ITensorHandleFactory::DeferredFactoryId;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100746}
747
748// For all handle factories supported on the source backend, we wish to find the one which requires the fewest copies
749// when considering all connections.
750ITensorHandleFactory::FactoryId CalculateSlotOption(BackendsMap& backends,
751 OutputSlot& outputSlot,
752 TensorHandleFactoryRegistry& registry)
753{
754 // First ensure the from backends can support the TensorHandeAPI
755 Layer& layer = outputSlot.GetOwningLayer();
756 auto frmBackend = backends.find(layer.GetBackendId());
757 if (frmBackend == backends.end() ||
758 !frmBackend->second->SupportsTensorAllocatorAPI())
759 {
760 return ITensorHandleFactory::LegacyFactoryId;
761 }
762
763 // Connections to Output Layers requires support for map/unmap on the TensorHandle.
764 bool requiresMapUnmap = false;
765 for (auto&& connection : outputSlot.GetConnections())
766 {
767 const Layer& connectedLayer = connection->GetOwningLayer();
768 if (connectedLayer.GetType() == LayerType::Output)
769 {
770 requiresMapUnmap = true;
771 }
772 }
773
774 IBackendInternal* srcBackend = frmBackend->second.get();
775 auto srcPrefs = srcBackend->GetHandleFactoryPreferences();
776
777 // Initialize the scores
778 std::map<ITensorHandleFactory::FactoryId, int> factoryScores;
779 for (auto&& pref : srcPrefs)
780 {
781 if (requiresMapUnmap) // Only consider factories that support map/unmap if required
782 {
783 ITensorHandleFactory* factory = registry.GetFactory(pref);
784 if (!factory->SupportsMapUnmap())
785 {
786 // The current tensor handle factory does not support the map/unmap strategy, move to the next one
787 continue;
788 }
789 }
790
791 auto it = factoryScores.find(pref);
792 if (it == factoryScores.end())
793 {
794 // Add new score to the table
795 factoryScores[pref] = 0;
796 }
797 }
798
799 // Score each handle factory based on how many times it requires copies on the slot connections
800 for (auto&& connection : outputSlot.GetConnections())
801 {
802 const Layer& connectedLayer = connection->GetOwningLayer();
803
804 auto toBackend = backends.find(connectedLayer.GetBackendId());
805 BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer");
806
807 auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences();
808 for (auto&& src : srcPrefs)
809 {
810 if (factoryScores.find(src) == factoryScores.end()) // Don't consider excluded factories
811 {
812 continue;
813 }
814
815 for (auto&& dst : dstPrefs)
816 {
817 if (RequiresCopy(src, dst, registry))
818 {
819 // Copy avoided, increase the score
820 factoryScores[src]++;
821 break;
822 }
823 }
824 }
825 }
826
827 // Find the lowest score
828 int minScore = std::numeric_limits<int>::max();
829 for (auto it : factoryScores)
830 {
831 minScore = std::min(minScore, it.second);
832 }
833
834 // Collect factories matching the best(lowest) score
835 std::vector<ITensorHandleFactory::FactoryId> optimalFactories;
836 for (auto it : factoryScores)
837 {
838 if (it.second == minScore)
839 {
840 optimalFactories.push_back(it.first);
841 }
842 }
843
844 // For all compatible Factories matching the best score, find the preferred one for the current layer.
845 for (auto&& srcPref : srcPrefs)
846 {
847 for (auto&& comp : optimalFactories)
848 {
849 if (comp == srcPref)
850 {
851 return comp;
852 }
853 }
854 }
855
856 return ITensorHandleFactory::LegacyFactoryId;
857}
858
Derek Lambertif674aa02019-08-01 15:56:25 +0100859EdgeStrategy CalculateEdgeStrategy(BackendsMap& backends,
860 ITensorHandleFactory::FactoryId srcFactoryId,
861 const Layer& layer,
862 const Layer& connectedLayer,
863 TensorHandleFactoryRegistry& registry)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100864{
865 auto toBackend = backends.find(connectedLayer.GetBackendId());
866 BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer");
867
868 auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences();
869
870 // Legacy API check for backward compatibility
871 if (srcFactoryId == ITensorHandleFactory::LegacyFactoryId || dstPrefs.empty())
872 {
873 if (layer.GetBackendId() != connectedLayer.GetBackendId())
874 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100875 return EdgeStrategy::CopyToTarget;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100876 }
877 else
878 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100879 return EdgeStrategy::DirectCompatibility;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100880 }
881 }
882
883 // TensorHandleFactory API present, so perform more sophisticated strategies.
Derek Lambertif674aa02019-08-01 15:56:25 +0100884 // Dst Output layers don't require copy because they use import or map/unmap
Derek Lamberti84da38b2019-06-13 11:40:08 +0100885 if (connectedLayer.GetType() == LayerType::Output)
886 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100887 return EdgeStrategy::DirectCompatibility;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100888 }
889
890 // Search for direct match in prefs
891 for (auto&& pref : dstPrefs)
892 {
893 if (pref == srcFactoryId)
894 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100895 return EdgeStrategy::DirectCompatibility;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100896 }
897 }
898
899 // Search for export/import options
900 ITensorHandleFactory* srcFactory = registry.GetFactory(srcFactoryId);
Derek Lambertif674aa02019-08-01 15:56:25 +0100901 if (srcFactory->GetExportFlags() != 0)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100902 {
903 for (auto&& pref : dstPrefs)
904 {
905 ITensorHandleFactory* dstFactory = registry.GetFactory(pref);
James Conroyffab16f2019-11-07 14:37:09 +0000906
James Conroy47e863d2019-11-18 17:07:43 +0000907 // Handles cases when a destPref is not listed in TensorHandleFactoryRegistry
James Conroyffab16f2019-11-07 14:37:09 +0000908 if (!dstFactory) {
James Conroy47e863d2019-11-18 17:07:43 +0000909 continue;
James Conroyffab16f2019-11-07 14:37:09 +0000910 }
911
Derek Lambertif674aa02019-08-01 15:56:25 +0100912 if ((dstFactory->GetImportFlags() & srcFactory->GetExportFlags()) != 0)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100913 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100914 return EdgeStrategy::ExportToTarget;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100915 }
916 }
917 }
918
919 // Search for copy options via map/unmap
920 if (srcFactory->SupportsMapUnmap())
921 {
922 for (auto&& pref : dstPrefs)
923 {
924 ITensorHandleFactory* dstFactory = registry.GetFactory(pref);
James Conroy47e863d2019-11-18 17:07:43 +0000925 if (dstFactory && dstFactory->SupportsMapUnmap())
Derek Lamberti84da38b2019-06-13 11:40:08 +0100926 {
Derek Lambertif674aa02019-08-01 15:56:25 +0100927 return EdgeStrategy::CopyToTarget;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100928 }
929 }
930 }
931
Derek Lambertif674aa02019-08-01 15:56:25 +0100932 return EdgeStrategy::Undefined;
Derek Lamberti84da38b2019-06-13 11:40:08 +0100933}
934
935// Select the TensorHandleFactories and the corresponding memory strategy
936OptimizationResult SelectTensorHandleStrategy(Graph& optGraph,
937 BackendsMap& backends,
938 TensorHandleFactoryRegistry& registry,
939 Optional<std::vector<std::string>&> errMessages)
940{
941 OptimizationResult result;
942
943 optGraph.ForEachLayer([&backends, &registry, &result, &errMessages](Layer* layer)
944 {
945 BOOST_ASSERT(layer);
946
947 // Lets make sure the backend is in our list of supported backends. Something went wrong during backend
948 // assignment if this check fails
949 BOOST_ASSERT(backends.find(layer->GetBackendId()) != backends.end());
950
951 // Check each output separately
952 for (unsigned int slotIdx = 0; slotIdx < layer->GetNumOutputSlots(); slotIdx++)
953 {
954 OutputSlot& outputSlot = layer->GetOutputSlot(slotIdx);
955
956 ITensorHandleFactory::FactoryId slotOption = ITensorHandleFactory::LegacyFactoryId;
957
958 // Calculate the factory to use which results in the fewest copies being made.
959 switch(layer->GetType())
960 {
961 case LayerType::Input:
962 slotOption = CalculateSlotOptionForInput(backends, outputSlot, registry);
963 break;
964 case LayerType::Output:
965 slotOption = CalculateSlotOptionForOutput(backends, outputSlot, registry);
966 break;
967 default:
968 slotOption = CalculateSlotOption(backends, outputSlot, registry);
969 break;
970 }
971 outputSlot.SetTensorHandleFactory(slotOption);
972
Derek Lambertif674aa02019-08-01 15:56:25 +0100973 // Now determine the "best" edge strategy for each connection given the slotOption.
Derek Lamberti84da38b2019-06-13 11:40:08 +0100974 unsigned int connectionIdx = 0;
975 for (auto&& connection : outputSlot.GetConnections())
976 {
977 const Layer& connectedLayer = connection->GetOwningLayer();
978
Derek Lambertif674aa02019-08-01 15:56:25 +0100979 EdgeStrategy strategy = CalculateEdgeStrategy(backends, slotOption, *layer, connectedLayer, registry);
Derek Lamberti84da38b2019-06-13 11:40:08 +0100980
Derek Lambertif674aa02019-08-01 15:56:25 +0100981 if (strategy == EdgeStrategy::Undefined)
Derek Lamberti84da38b2019-06-13 11:40:08 +0100982 {
983 result.m_Error = true;
984 if (errMessages)
985 {
986 errMessages.value().emplace_back("Could not find valid strategy required for compatibility"
987 " between backends.");
988 }
989 return;
990 }
991
Derek Lambertif674aa02019-08-01 15:56:25 +0100992 outputSlot.SetEdgeStrategy(connectionIdx, strategy);
Derek Lamberti84da38b2019-06-13 11:40:08 +0100993
994 connectionIdx++;
995 }
996 }
997 });
998
999 return result;
1000}
1001
Matteo Martincigh49124022019-01-11 13:25:59 +00001002IOptimizedNetworkPtr Optimize(const INetwork& inNetwork,
1003 const std::vector<BackendId>& backendPreferences,
1004 const IDeviceSpec& deviceSpec,
1005 const OptimizerOptions& options,
Rob Hughes23214432019-11-05 11:27:36 +00001006 Optional<std::vector<std::string>&> messages)
Matteo Martincigh49124022019-01-11 13:25:59 +00001007{
1008 if (backendPreferences.empty())
1009 {
1010 throw armnn::InvalidArgumentException("Invoked Optimize with no backends specified");
1011 }
1012
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +00001013 if (options.m_ReduceFp32ToFp16 && options.m_ReduceFp32ToBf16)
1014 {
1015 throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time.");
1016 }
1017
Matteo Martincigh49124022019-01-11 13:25:59 +00001018 const Network& network = *boost::polymorphic_downcast<const Network*>(&inNetwork);
1019 std::unique_ptr<Graph> graph = std::make_unique<Graph>(network.GetGraph());
1020
1021 auto optNet = IOptimizedNetworkPtr(new OptimizedNetwork(std::move(graph)), &IOptimizedNetwork::Destroy);
1022
1023 OptimizedNetwork* optNetObjPtr = boost::polymorphic_downcast<OptimizedNetwork*>(optNet.get());
1024
Matteo Martincighadddddb2019-01-24 14:06:23 +00001025 // Get the optimized graph
1026 Graph& optGraph = optNetObjPtr->GetGraph();
1027
Matteo Martincigh49124022019-01-11 13:25:59 +00001028 // Perform optimisation passes
1029 using namespace optimizations;
Matteo Martincighadddddb2019-01-24 14:06:23 +00001030 Optimizer::Pass(optGraph, MakeOptimizations(SquashEqualPermuteSiblings(),
Mike Kelly490b7be2020-03-03 12:39:09 +00001031 SquashEqualTransposeSiblings(),
Matteo Martincighadddddb2019-01-24 14:06:23 +00001032 SquashEqualReshapeSiblings(),
1033 OptimizeInversePermutes(),
Mike Kelly490b7be2020-03-03 12:39:09 +00001034 OptimizeInverseTransposes(),
Matteo Martincighadddddb2019-01-24 14:06:23 +00001035 MovePermuteUp(),
Mike Kelly490b7be2020-03-03 12:39:09 +00001036 MoveTransposeUp(),
Matteo Martincighadddddb2019-01-24 14:06:23 +00001037 PermuteAsReshape(),
Mike Kelly490b7be2020-03-03 12:39:09 +00001038 TransposeAsReshape(),
Nina Drozd861985f2019-04-18 14:48:51 +01001039 OptimizeConsecutiveReshapes(),
Rob Hughes3a7d3a72019-09-24 16:59:56 +01001040 FoldPadIntoConvolution2d(),
Mike Kelly490b7be2020-03-03 12:39:09 +00001041 PermuteAndBatchToSpaceAsDepthToSpace(),
1042 TransposeAndBatchToSpaceAsDepthToSpace()));
Matteo Martincigh49124022019-01-11 13:25:59 +00001043
Matteo Martincighadddddb2019-01-24 14:06:23 +00001044 // Infer the tensor infos for all output slots. Throws an exception on failure
1045 optGraph.InferTensorInfos();
Matteo Martincigh49124022019-01-11 13:25:59 +00001046
1047 // If Fp32 to Fp16 optimization is set convert Fp32 network to Fp16
1048 if (options.m_ReduceFp32ToFp16)
1049 {
Matteo Martincighadddddb2019-01-24 14:06:23 +00001050 Optimizer::Pass(optGraph, MakeOptimizations(Fp32NetworkToFp16Converter()));
Derek Lambertidd6804b2019-11-27 09:29:57 +00001051 Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf()));
Matteo Martincigh49124022019-01-11 13:25:59 +00001052 }
1053
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +00001054 // If Fp32 to Bf16 optimization is set convert Fp32 network to Bf16
Narumol Prangnawarat57ef0082020-03-26 09:20:43 +00001055 // Convert input of Convolution2d and FullyConnected from Fp32 to Bf16
1056 // Only Constant weight of Convolution2d and FullyConnected are converted from Fp32 to Bf16
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +00001057 if (options.m_ReduceFp32ToBf16)
1058 {
1059 Optimizer::Pass(optGraph, MakeOptimizations(Fp32NetworkToBf16Converter()));
Narumol Prangnawaratbc7ffb52020-03-20 15:01:01 +00001060 }
1061
Matteo Martincigh49124022019-01-11 13:25:59 +00001062 // Initialize backend settings
1063 BackendSettings backendSettings(backendPreferences, deviceSpec);
1064 if (backendSettings.GetAvailablePreferredBackends().empty())
1065 {
1066 std::stringstream failureMsg;
1067 failureMsg << "None of the preferred backends " << backendPreferences
1068 << " are supported. Current platform provides " << backendSettings.m_SupportedBackends;
Rob Hughes23214432019-11-05 11:27:36 +00001069 ReportError(failureMsg.str(), messages);
Matteo Martincigh49124022019-01-11 13:25:59 +00001070 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
1071 }
1072
Derek Lamberti84da38b2019-06-13 11:40:08 +01001073 // Create a map to temporarily hold initialized backend objects
1074 TensorHandleFactoryRegistry tensorHandleFactoryRegistry;
1075 BackendsMap backends = CreateSupportedBackends(tensorHandleFactoryRegistry, backendSettings);
1076
Matteo Martincigh49124022019-01-11 13:25:59 +00001077 // Assign an available backend to each layer
Matteo Martincighadddddb2019-01-24 14:06:23 +00001078 Graph::Iterator firstLayer = optGraph.begin();
1079 Graph::Iterator lastLayer = optGraph.end();
Derek Lamberti84da38b2019-06-13 11:40:08 +01001080 OptimizationResult assignBackendsResult = AssignBackends(optNetObjPtr,
1081 backendSettings,
1082 firstLayer,
1083 lastLayer,
Rob Hughes23214432019-11-05 11:27:36 +00001084 messages);
Derek Lamberti84da38b2019-06-13 11:40:08 +01001085 if (assignBackendsResult.m_Error)
Matteo Martincigh49124022019-01-11 13:25:59 +00001086 {
1087 // Failed to assign a backend to each layer
jimfly016b0b53d2018-10-08 14:43:01 +01001088 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
1089 }
telsoa01c577f2c2018-08-31 09:22:23 +01001090
Matteo Martincighadddddb2019-01-24 14:06:23 +00001091 Optimizer::Pass(optGraph, MakeOptimizations(OptimizeInverseConversionsFp16(),
1092 OptimizeInverseConversionsFp32()));
telsoa01c577f2c2018-08-31 09:22:23 +01001093
Matteo Martincighadddddb2019-01-24 14:06:23 +00001094 // Apply the backend-specific optimizations
1095 OptimizationResult backendOptimizationResult = ApplyBackendOptimizations(optNetObjPtr,
1096 backendSettings,
Derek Lamberti84da38b2019-06-13 11:40:08 +01001097 backends,
Rob Hughes23214432019-11-05 11:27:36 +00001098 messages);
Matteo Martincighadddddb2019-01-24 14:06:23 +00001099 if (backendOptimizationResult.m_Error)
Matteo Martincigh49124022019-01-11 13:25:59 +00001100 {
Matteo Martincighadddddb2019-01-24 14:06:23 +00001101 // Failed to apply the backend-specific optimizations
1102 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
Matteo Martincigh49124022019-01-11 13:25:59 +00001103 }
1104
Matteo Martincighadddddb2019-01-24 14:06:23 +00001105 // If the debug flag is set, then insert a DebugLayer after each layer
1106 // Doing this after applying the backend optimizations as they might have changed some layers
1107 if (options.m_Debug)
1108 {
1109 Optimizer::Pass(optGraph, MakeOptimizations(InsertDebugLayer()));
1110 }
1111
Derek Lamberti84da38b2019-06-13 11:40:08 +01001112 // Calculate the compatibility strategies for tensor handles
1113 OptimizationResult strategyResult = SelectTensorHandleStrategy(optGraph,
1114 backends,
1115 tensorHandleFactoryRegistry,
Rob Hughes23214432019-11-05 11:27:36 +00001116 messages);
Derek Lamberti84da38b2019-06-13 11:40:08 +01001117 if (strategyResult.m_Error)
1118 {
1119 // Failed to apply the backend-specific optimizations
1120 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
1121 }
1122
1123 // Based on the tensor handle strategy determined above, insert copy layers where required.
Derek Lambertif674aa02019-08-01 15:56:25 +01001124 optGraph.AddCompatibilityLayers(backends, tensorHandleFactoryRegistry);
telsoa01c577f2c2018-08-31 09:22:23 +01001125
1126 // Convert constants
Matteo Martincighadddddb2019-01-24 14:06:23 +00001127 Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf()));
1128 Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsHalfToFloat()));
telsoa01c577f2c2018-08-31 09:22:23 +01001129
Derek Lamberti84da38b2019-06-13 11:40:08 +01001130 // Run backend specific optimizations (deprecated)
Matteo Martincigh49124022019-01-11 13:25:59 +00001131 for (auto&& chosenBackend : backendSettings.m_SelectedBackends)
David Beck263e3492018-11-09 14:46:40 +00001132 {
1133 auto factoryFun = BackendRegistryInstance().GetFactory(chosenBackend);
1134 auto backendPtr = factoryFun();
1135 BOOST_ASSERT(backendPtr.get() != nullptr);
1136
Matteo Martincighed735042019-05-22 09:42:43 +01001137 ARMNN_NO_DEPRECATE_WARN_BEGIN
David Beck263e3492018-11-09 14:46:40 +00001138 auto backendSpecificOptimizations = backendPtr->GetOptimizations();
Matteo Martincighed735042019-05-22 09:42:43 +01001139 ARMNN_NO_DEPRECATE_WARN_END
1140
David Beck263e3492018-11-09 14:46:40 +00001141 if (!backendSpecificOptimizations.empty())
1142 {
1143 Optimizer::Pass(optNetObjPtr->GetGraph(), backendSpecificOptimizations);
1144 }
1145 }
1146
telsoa01c577f2c2018-08-31 09:22:23 +01001147 return optNet;
telsoa014fcda012018-03-09 14:13:49 +00001148}
1149
1150Network::Network()
Sadik Armagan3184c902020-03-18 10:57:30 +00001151: m_Graph(std::make_unique<Graph>())
telsoa014fcda012018-03-09 14:13:49 +00001152{
1153}
1154
1155Network::~Network()
1156{
1157}
1158
Jan Eilers99d9d4a2019-11-06 10:02:16 +00001159Status Network::PrintGraph()
1160{
1161 m_Graph->Print();
1162 return Status::Success;
1163}
1164
telsoa014fcda012018-03-09 14:13:49 +00001165IConnectableLayer* Network::AddInputLayer(LayerBindingId id, const char* name)
1166{
1167 return m_Graph->AddLayer<InputLayer>(id, name);
1168}
1169
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +00001170IConnectableLayer* Network::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
1171 const char* name)
1172{
1173 return m_Graph->AddLayer<BatchToSpaceNdLayer>(batchToSpaceNdDescriptor, name);
1174}
1175
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01001176IConnectableLayer* Network::AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor,
1177 const char* name)
1178{
1179 return m_Graph->AddLayer<ComparisonLayer>(comparisonDescriptor, name);
1180}
1181
josh minor4a3c6102020-01-06 16:40:46 -06001182IConnectableLayer* Network::AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor,
1183 const char* name)
1184{
1185 return m_Graph->AddLayer<ElementwiseUnaryLayer>(elementwiseUnaryDescriptor, name);
1186}
1187
telsoa014fcda012018-03-09 14:13:49 +00001188IConnectableLayer* Network::AddFullyConnectedLayerImpl(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001189 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001190 const Optional<ConstTensor>& biases,
telsoa01c577f2c2018-08-31 09:22:23 +01001191 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001192{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001193 if (fullyConnectedDescriptor.m_BiasEnabled && !biases.has_value())
telsoa014fcda012018-03-09 14:13:49 +00001194 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001195 throw InvalidArgumentException("AddFullyConnectedLayer: biases cannot be empty");
telsoa014fcda012018-03-09 14:13:49 +00001196 }
1197
1198 const auto layer = m_Graph->AddLayer<FullyConnectedLayer>(fullyConnectedDescriptor, name);
1199
1200 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
1201
1202 if (fullyConnectedDescriptor.m_BiasEnabled)
1203 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001204 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value());
telsoa014fcda012018-03-09 14:13:49 +00001205 }
1206
1207 return layer;
1208}
1209
1210IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001211 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001212 const Optional<ConstTensor>& biases,
telsoa01c577f2c2018-08-31 09:22:23 +01001213 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001214{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001215 return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name);
telsoa014fcda012018-03-09 14:13:49 +00001216}
1217
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001218IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
1219 const ConstTensor& weights,
1220 const char* name)
1221{
Matteo Martincighfc598e12019-05-14 10:36:13 +01001222 Optional<ConstTensor> biases;
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001223 return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name);
1224}
1225
telsoa014fcda012018-03-09 14:13:49 +00001226IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001227 const ConstTensor& weights,
1228 const ConstTensor& biases,
1229 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001230{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001231 Optional<ConstTensor> optionalBiases(biases);
1232 return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, optionalBiases, name);
telsoa014fcda012018-03-09 14:13:49 +00001233}
1234
Jim Flynne242f2d2019-05-22 14:24:13 +01001235IConnectableLayer* Network::AddConcatLayer(const ConcatDescriptor& concatDescriptor,
Jim Flynn906f9462019-05-10 13:55:21 +01001236 const char* name)
1237{
Jim Flynne242f2d2019-05-22 14:24:13 +01001238 return m_Graph->AddLayer<ConcatLayer>(concatDescriptor, name);
Jim Flynn906f9462019-05-10 13:55:21 +01001239}
1240
telsoa014fcda012018-03-09 14:13:49 +00001241IConnectableLayer* Network::AddConvolution2dLayerImpl(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001242 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001243 const Optional<ConstTensor>& biases,
telsoa01c577f2c2018-08-31 09:22:23 +01001244 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001245{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001246 if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value())
telsoa014fcda012018-03-09 14:13:49 +00001247 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001248 throw InvalidArgumentException("AddConvolution2dLayer: biases cannot be empty");
telsoa014fcda012018-03-09 14:13:49 +00001249 }
1250
1251 const auto layer = m_Graph->AddLayer<Convolution2dLayer>(convolution2dDescriptor, name);
1252
1253 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
1254
1255 if (convolution2dDescriptor.m_BiasEnabled)
1256 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001257 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value());
telsoa014fcda012018-03-09 14:13:49 +00001258 }
1259
1260 return layer;
1261}
1262
1263IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001264 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001265 const Optional<ConstTensor>& biases,
telsoa01c577f2c2018-08-31 09:22:23 +01001266 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001267{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001268 return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name);
telsoa014fcda012018-03-09 14:13:49 +00001269}
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001270
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001271IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
1272 const ConstTensor& weights,
1273 const char* name)
1274{
Matteo Martincighfc598e12019-05-14 10:36:13 +01001275 Optional<ConstTensor> biases;
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001276 return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name);
1277}
1278
telsoa014fcda012018-03-09 14:13:49 +00001279IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +01001280 const ConstTensor& weights,
1281 const ConstTensor& biases,
1282 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001283{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001284 Optional<ConstTensor> optionalBiases(biases);
1285 return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name);
telsoa014fcda012018-03-09 14:13:49 +00001286}
1287
1288IConnectableLayer* Network::AddDepthwiseConvolution2dLayerImpl(
1289 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
1290 const ConstTensor& weights,
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001291 const Optional<ConstTensor>& biases,
telsoa014fcda012018-03-09 14:13:49 +00001292 const char* name)
1293{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001294 if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value())
telsoa014fcda012018-03-09 14:13:49 +00001295 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001296 throw InvalidArgumentException("AddDepthwiseConvolution2dLayer: biases cannot be empty");
telsoa014fcda012018-03-09 14:13:49 +00001297 }
1298
Matteo Martincigh3d6898c2019-01-15 16:11:44 +00001299 const auto layer = m_Graph->AddLayer<DepthwiseConvolution2dLayer>(convolution2dDescriptor, name);
telsoa014fcda012018-03-09 14:13:49 +00001300
1301 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
1302
1303 if (convolution2dDescriptor.m_BiasEnabled)
1304 {
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001305 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value());
telsoa014fcda012018-03-09 14:13:49 +00001306 }
1307
1308 return layer;
1309}
1310
Aron Virginas-Tardd6247f2019-09-19 14:31:17 +01001311IConnectableLayer* Network::AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor,
1312 const char* name)
1313{
1314 return m_Graph->AddLayer<DepthToSpaceLayer>(depthToSpaceDescriptor, name);
1315}
1316
telsoa014fcda012018-03-09 14:13:49 +00001317IConnectableLayer* Network::AddDepthwiseConvolution2dLayer(
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001318 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
1319 const ConstTensor& weights,
1320 const Optional<ConstTensor>& biases,
1321 const char* name)
1322{
1323 return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name);
1324}
1325
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001326IConnectableLayer* Network::AddDepthwiseConvolution2dLayer(
telsoa014fcda012018-03-09 14:13:49 +00001327 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
1328 const ConstTensor& weights,
1329 const char* name)
1330{
Matteo Martincighfc598e12019-05-14 10:36:13 +01001331 Optional<ConstTensor> biases;
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001332 return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name);
telsoa014fcda012018-03-09 14:13:49 +00001333}
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001334
telsoa014fcda012018-03-09 14:13:49 +00001335IConnectableLayer* Network::AddDepthwiseConvolution2dLayer(
1336 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
1337 const ConstTensor& weights,
1338 const ConstTensor& biases,
1339 const char* name)
1340{
Aron Virginas-Tarad402702019-02-22 17:03:44 +00001341 Optional<ConstTensor> optionalBiases(biases);
1342 return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name);
telsoa014fcda012018-03-09 14:13:49 +00001343}
1344
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +00001345IConnectableLayer* Network::AddDetectionPostProcessLayer(const armnn::DetectionPostProcessDescriptor& descriptor,
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +00001346 const ConstTensor& anchors, const char* name)
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +00001347{
Narumol Prangnawarat6d302bf2019-02-04 11:46:26 +00001348 const auto layer = m_Graph->AddLayer<DetectionPostProcessLayer>(descriptor, name);
1349
1350 layer->m_Anchors = std::make_unique<ScopedCpuTensorHandle>(anchors);
1351
1352 return layer;
Narumol Prangnawarat94dd5d82019-01-23 18:06:26 +00001353}
1354
telsoa014fcda012018-03-09 14:13:49 +00001355IConnectableLayer* Network::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
1356 const char* name)
1357{
1358 return m_Graph->AddLayer<PermuteLayer>(permuteDescriptor, name);
1359}
1360
1361IConnectableLayer* Network::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
1362 const char* name)
1363{
1364 return m_Graph->AddLayer<Pooling2dLayer>(pooling2dDescriptor, name);
1365}
1366
1367IConnectableLayer* Network::AddActivationLayer(const ActivationDescriptor& activationDescriptor,
1368 const char* name)
1369{
1370 return m_Graph->AddLayer<ActivationLayer>(activationDescriptor, name);
1371}
1372
Nikhil Rajee391d52019-09-05 17:50:44 +01001373IConnectableLayer* Network::AddArgMinMaxLayer(const ArgMinMaxDescriptor& argMinMaxDescriptor,
1374 const char* name)
1375{
1376 return m_Graph->AddLayer<ArgMinMaxLayer>(argMinMaxDescriptor, name);
1377}
1378
telsoa01c577f2c2018-08-31 09:22:23 +01001379IConnectableLayer* Network::AddNormalizationLayer(const NormalizationDescriptor&
1380normalizationDescriptor,
telsoa014fcda012018-03-09 14:13:49 +00001381 const char* name)
1382{
1383 return m_Graph->AddLayer<NormalizationLayer>(normalizationDescriptor, name);
1384}
1385
Aron Virginas-Tar636ab402019-09-16 14:27:45 +01001386IConnectableLayer* Network::AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name)
1387{
1388 return m_Graph->AddLayer<SliceLayer>(sliceDescriptor, name);
1389}
1390
telsoa014fcda012018-03-09 14:13:49 +00001391IConnectableLayer* Network::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
1392 const char* name)
1393{
1394 return m_Graph->AddLayer<SoftmaxLayer>(softmaxDescriptor, name);
1395}
1396
1397IConnectableLayer* Network::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
1398 const char* name)
1399{
1400 return m_Graph->AddLayer<SplitterLayer>(splitterDescriptor, name);
1401}
1402
Nattapat Chaimanowong5a4304a2018-11-28 10:44:37 +00001403IConnectableLayer* Network::AddMaximumLayer(const char* name)
1404{
1405 return m_Graph->AddLayer<MaximumLayer>(name);
1406}
1407
Éanna Ó Catháin20e58802018-12-04 10:29:06 +00001408IConnectableLayer* Network::AddMinimumLayer(const char* name)
1409{
1410 return m_Graph->AddLayer<MinimumLayer>(name);
1411}
1412
Jim Flynne242f2d2019-05-22 14:24:13 +01001413IConnectableLayer* Network::AddMergerLayer(const MergerDescriptor& mergerDescriptor,
Jim Flynn906f9462019-05-10 13:55:21 +01001414 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001415{
Jim Flynne242f2d2019-05-22 14:24:13 +01001416 return AddConcatLayer(mergerDescriptor, name);
telsoa014fcda012018-03-09 14:13:49 +00001417}
1418
Kevin May868eb142019-09-04 17:29:31 +01001419IConnectableLayer* Network::AddAbsLayer(const char * name)
1420{
josh minor4a3c6102020-01-06 16:40:46 -06001421 return AddElementwiseUnaryLayer(ElementwiseUnaryDescriptor(UnaryOperation::Abs), name);
Kevin May868eb142019-09-04 17:29:31 +01001422}
1423
telsoa014fcda012018-03-09 14:13:49 +00001424IConnectableLayer* Network::AddAdditionLayer(const char* name)
1425{
1426 return m_Graph->AddLayer<AdditionLayer>(name);
1427}
1428
1429IConnectableLayer* Network::AddMultiplicationLayer(const char* name)
1430{
1431 return m_Graph->AddLayer<MultiplicationLayer>(name);
1432}
1433
1434IConnectableLayer* Network::AddOutputLayer(LayerBindingId id, const char* name)
1435{
1436 return m_Graph->AddLayer<OutputLayer>(id, name);
1437}
1438
1439IConnectableLayer* Network::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
1440 const ConstTensor& mean,
1441 const ConstTensor& variance,
1442 const ConstTensor& beta,
1443 const ConstTensor& gamma,
1444 const char* name)
1445{
1446 const auto layer = m_Graph->AddLayer<BatchNormalizationLayer>(desc, name);
1447
1448 layer->m_Mean = std::make_unique<ScopedCpuTensorHandle>(mean);
1449 layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(variance);
1450 layer->m_Beta = std::make_unique<ScopedCpuTensorHandle>(beta);
1451 layer->m_Gamma = std::make_unique<ScopedCpuTensorHandle>(gamma);
1452
1453 return layer;
1454}
1455
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01001456IConnectableLayer* Network::AddResizeBilinearLayer(const ResizeBilinearDescriptor& descriptor,
1457 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001458{
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01001459 ResizeDescriptor resizeDescriptor;
1460 resizeDescriptor.m_Method = ResizeMethod::Bilinear;
1461 resizeDescriptor.m_DataLayout = descriptor.m_DataLayout;
1462 resizeDescriptor.m_TargetWidth = descriptor.m_TargetWidth;
1463 resizeDescriptor.m_TargetHeight = descriptor.m_TargetHeight;
1464
1465 return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name);
telsoa014fcda012018-03-09 14:13:49 +00001466}
1467
Teresa Charlina9075df2019-06-27 15:41:57 +01001468IConnectableLayer* Network::AddResizeLayer(const ResizeDescriptor&
1469resizeDescriptor, const char* name)
1470{
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01001471 return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name);
Teresa Charlina9075df2019-06-27 15:41:57 +01001472}
1473
Kevin Mayce5045a2019-10-02 14:07:47 +01001474IConnectableLayer* Network::AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc,
1475 const char* name)
1476{
1477 return m_Graph->AddLayer<InstanceNormalizationLayer>(desc, name);
1478}
1479
Matteo Martincighbcd3c852018-09-28 14:14:12 +01001480IConnectableLayer* Network::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
1481 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001482{
Matteo Martincighbcd3c852018-09-28 14:14:12 +01001483 return m_Graph->AddLayer<L2NormalizationLayer>(desc, name);
telsoa014fcda012018-03-09 14:13:49 +00001484}
1485
Aron Virginas-Tarf982dea2019-10-11 14:07:53 +01001486IConnectableLayer* Network::AddLogSoftmaxLayer(const LogSoftmaxDescriptor& desc,
1487 const char* name)
1488{
1489 return m_Graph->AddLayer<LogSoftmaxLayer>(desc, name);
1490}
1491
telsoa014fcda012018-03-09 14:13:49 +00001492IConnectableLayer* Network::AddConstantLayer(const ConstTensor& input, const char* name)
1493{
telsoa01c577f2c2018-08-31 09:22:23 +01001494 auto layer = m_Graph->AddLayer<ConstantLayer>(name);
1495
1496 layer->m_LayerOutput = std::make_unique<ScopedCpuTensorHandle>(input);
1497
1498 return layer;
telsoa014fcda012018-03-09 14:13:49 +00001499}
1500
telsoa01c577f2c2018-08-31 09:22:23 +01001501IConnectableLayer* Network::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
1502 const char* name)
telsoa014fcda012018-03-09 14:13:49 +00001503{
1504 return m_Graph->AddLayer<ReshapeLayer>(reshapeDescriptor, name);
1505}
1506
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +00001507IConnectableLayer* Network::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
1508 const char* name)
1509{
1510 return m_Graph->AddLayer<SpaceToBatchNdLayer>(spaceToBatchNdDescriptor, name);
1511}
1512
Aron Virginas-Tar972af152019-06-11 14:14:03 +01001513IConnectableLayer* Network::AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor,
1514 const char* name)
1515{
1516 return m_Graph->AddLayer<SpaceToDepthLayer>(spaceToDepthDescriptor, name);
1517}
1518
telsoa014fcda012018-03-09 14:13:49 +00001519IConnectableLayer* Network::AddFloorLayer(const char* name)
1520{
1521 return m_Graph->AddLayer<FloorLayer>(name);
1522}
1523
telsoa01c577f2c2018-08-31 09:22:23 +01001524IConnectableLayer* Network::AddLstmLayer(const LstmDescriptor& descriptor,
1525 const LstmInputParams& params,
1526 const char* name)
1527{
1528 const auto layer = m_Graph->AddLayer<LstmLayer>(descriptor, name);
1529
1530 //Lstm Basic Parameters
1531 layer->m_BasicParameters.m_InputToForgetWeights =
1532 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights));
1533 layer->m_BasicParameters.m_InputToCellWeights =
1534 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights));
1535 layer->m_BasicParameters.m_InputToOutputWeights =
1536 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights));
1537 layer->m_BasicParameters.m_RecurrentToForgetWeights =
1538 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights));
1539 layer->m_BasicParameters.m_RecurrentToCellWeights =
1540 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights));
1541 layer->m_BasicParameters.m_RecurrentToOutputWeights =
1542 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights));
1543 layer->m_BasicParameters.m_ForgetGateBias =
1544 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias));
1545 layer->m_BasicParameters.m_CellBias =
1546 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias));
1547 layer->m_BasicParameters.m_OutputGateBias =
1548 std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias));
1549
1550 //Lstm Cifg parameters
1551 if(!descriptor.m_CifgEnabled)
1552 {
1553 if(params.m_InputToInputWeights == nullptr)
1554 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001555 throw InvalidArgumentException("AddLstmLayer: Input To Input Weights cannot be NULL "
1556 "when CIFG is disabled.");
telsoa01c577f2c2018-08-31 09:22:23 +01001557 }
1558 if(params.m_RecurrentToInputWeights == nullptr)
1559 {
1560 throw InvalidArgumentException(
Jan Eilerse2062cd2020-03-30 15:07:45 +01001561 "AddLstmLayer: Recurrent To Input Weights cannot be NULL "
1562 "when CIFG is disabled.");
telsoa01c577f2c2018-08-31 09:22:23 +01001563 }
1564 if(params.m_InputGateBias == nullptr)
1565 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001566 throw InvalidArgumentException("AddLstmLayer: Input Gate Bias cannot be NULL "
1567 "when CIFG is disabled.");
telsoa01c577f2c2018-08-31 09:22:23 +01001568 }
1569 layer->m_CifgParameters.m_InputToInputWeights =
1570 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights));
1571 layer->m_CifgParameters.m_RecurrentToInputWeights =
1572 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights));
telsoa01c577f2c2018-08-31 09:22:23 +01001573 layer->m_CifgParameters.m_InputGateBias =
1574 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias));
1575 }
1576
1577 //Lstm projection parameters
1578 if(descriptor.m_ProjectionEnabled)
1579 {
1580 if(params.m_ProjectionWeights == nullptr)
1581 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001582 throw InvalidArgumentException("AddLstmLayer: Projection Weights cannot be NULL "
1583 "when projection is enabled.");
telsoa01c577f2c2018-08-31 09:22:23 +01001584 }
1585 layer->m_ProjectionParameters.m_ProjectionWeights =
1586 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights));
1587 if(params.m_ProjectionBias != nullptr)
1588 {
1589 layer->m_ProjectionParameters.m_ProjectionBias =
1590 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias));
1591 }
1592 }
1593
1594 //Lstm Peephole params
1595 if(descriptor.m_PeepholeEnabled)
1596 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001597 if(!descriptor.m_CifgEnabled)
1598 {
1599 if(params.m_CellToInputWeights == nullptr)
1600 {
1601 throw InvalidArgumentException("AddLstmLayer: Cell To Input Weights cannot be NULL "
1602 "when Peephole is enabled and CIFG disabled.");
1603 }
1604
1605 layer->m_PeepholeParameters.m_CellToInputWeights =
1606 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights));
1607 }
1608
telsoa01c577f2c2018-08-31 09:22:23 +01001609 if(params.m_CellToForgetWeights == nullptr)
1610 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001611 throw InvalidArgumentException("AddLstmLayer: Cell To Forget Weights cannot be NULL "
1612 "when Peephole is enabled.");
telsoa01c577f2c2018-08-31 09:22:23 +01001613 }
1614 if(params.m_CellToOutputWeights == nullptr)
1615 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001616 throw InvalidArgumentException("AddLstmLayer: Cell To Output Weights cannot be NULL "
1617 "when Peephole is enabled.");
telsoa01c577f2c2018-08-31 09:22:23 +01001618 }
Jan Eilerse2062cd2020-03-30 15:07:45 +01001619
telsoa01c577f2c2018-08-31 09:22:23 +01001620 layer->m_PeepholeParameters.m_CellToForgetWeights =
1621 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights));
1622 layer->m_PeepholeParameters.m_CellToOutputWeights =
1623 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights));
1624 }
Jan Eilersf8c62972019-07-17 11:07:49 +01001625
1626 //Lstm Layer Normalization params
1627 if(descriptor.m_LayerNormEnabled)
1628 {
1629 if(!descriptor.m_CifgEnabled)
1630 {
1631 if(params.m_InputLayerNormWeights == nullptr)
1632 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001633 throw InvalidArgumentException("AddLstmLayer: Input layer normalization weights cannot be NULL "
1634 "when layer normalization is enabled and CIFG disabled.");
Jan Eilersf8c62972019-07-17 11:07:49 +01001635 }
1636 layer->m_LayerNormParameters.m_InputLayerNormWeights =
1637 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputLayerNormWeights));
1638 }
1639
1640 if(params.m_ForgetLayerNormWeights == nullptr)
1641 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001642 throw InvalidArgumentException("AddLstmLayer: Forget layer normalization weights cannot be NULL "
1643 "when layer normalization is enabled.");
Jan Eilersf8c62972019-07-17 11:07:49 +01001644 }
1645 if(params.m_CellLayerNormWeights == nullptr)
1646 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001647 throw InvalidArgumentException("AddLstmLayer: Cell layer normalization weights cannot be NULL "
1648 "when layer normalization is enabled.");
Jan Eilersf8c62972019-07-17 11:07:49 +01001649 }
1650 if(params.m_OutputLayerNormWeights == nullptr)
1651 {
Jan Eilerse2062cd2020-03-30 15:07:45 +01001652 throw InvalidArgumentException("AddLstmLayer: Output layer normalization weights cannot be NULL "
1653 "when layer normalization is enabled.");
Jan Eilersf8c62972019-07-17 11:07:49 +01001654 }
1655 layer->m_LayerNormParameters.m_ForgetLayerNormWeights =
1656 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetLayerNormWeights));
1657 layer->m_LayerNormParameters.m_CellLayerNormWeights =
1658 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellLayerNormWeights));
1659 layer->m_LayerNormParameters.m_OutputLayerNormWeights =
1660 std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputLayerNormWeights));
1661 }
telsoa01c577f2c2018-08-31 09:22:23 +01001662 return layer;
1663}
1664
Francis Murtaghe7a86a42018-08-29 12:42:10 +01001665IConnectableLayer* Network::AddDivisionLayer(const char* name)
1666{
1667 return m_Graph->AddLayer<DivisionLayer>(name);
1668}
1669
David Beck19526222018-09-12 16:00:08 +01001670IConnectableLayer* Network::AddSubtractionLayer(const char* name)
1671{
1672 return m_Graph->AddLayer<SubtractionLayer>(name);
1673}
1674
narpra0132b90462018-09-13 11:07:48 +01001675IConnectableLayer* Network::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name)
1676{
1677 return m_Graph->AddLayer<MeanLayer>(meanDescriptor,name);
1678}
1679
Mohamed Nour Abouelseoud5662c202018-09-24 13:30:09 +01001680IConnectableLayer* Network::AddPadLayer(const PadDescriptor& padDescriptor, const char* name)
1681{
1682 return m_Graph->AddLayer<PadLayer>(padDescriptor,name);
1683}
1684
Derek Lambertia9cca6a2019-03-25 15:41:58 +00001685IConnectableLayer *Network::AddQuantizeLayer(const char *name)
1686{
1687 return m_Graph->AddLayer<QuantizeLayer>(name);
1688}
1689
Nattapat Chaimanowonge4294fd2019-03-28 09:56:53 +00001690IConnectableLayer* Network::AddDequantizeLayer(const char* name)
1691{
1692 return m_Graph->AddLayer<DequantizeLayer>(name);
1693}
1694
Conor Kennedy430b5d82018-11-14 15:28:28 +00001695IConnectableLayer* Network::AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor,
1696 const char* name)
1697{
1698 return m_Graph->AddLayer<StridedSliceLayer>(stridedSliceDescriptor, name);
1699}
1700
Matteo Martincigh59a950c2018-12-13 12:48:25 +00001701IConnectableLayer* Network::AddGreaterLayer(const char* name)
1702{
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01001703 return AddComparisonLayer(ComparisonDescriptor(ComparisonOperation::Greater), name);
Matteo Martincigh59a950c2018-12-13 12:48:25 +00001704}
1705
FrancisMurtagh20995952018-12-17 12:11:36 +00001706IConnectableLayer* Network::AddEqualLayer(const char* name)
1707{
Aron Virginas-Tar77bfb5e2019-10-16 17:45:38 +01001708 return AddComparisonLayer(ComparisonDescriptor(ComparisonOperation::Equal), name);
FrancisMurtagh20995952018-12-17 12:11:36 +00001709}
1710
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +00001711IConnectableLayer* Network::AddRsqrtLayer(const char * name)
1712{
josh minor4a3c6102020-01-06 16:40:46 -06001713 return AddElementwiseUnaryLayer(ElementwiseUnaryDescriptor(UnaryOperation::Rsqrt), name);
Mohamed Nour Abouelseouda1d3c6a2018-12-27 12:39:16 +00001714}
1715
narpra01b89b05f2019-01-16 09:53:09 +00001716IConnectableLayer* Network::AddGatherLayer(const char* name)
1717{
1718 return m_Graph->AddLayer<GatherLayer>(name);
1719}
1720
Nattapat Chaimanowong1f886302019-04-05 13:37:19 +01001721IConnectableLayer* Network::AddMergeLayer(const char* name)
1722{
1723 return m_Graph->AddLayer<MergeLayer>(name);
1724}
1725
Sadik Armaganeff363d2019-04-05 15:25:46 +01001726IConnectableLayer* Network::AddSwitchLayer(const char* name)
1727{
1728 return m_Graph->AddLayer<SwitchLayer>(name);
1729}
1730
Matteo Martincigh0e406ee2019-06-12 15:42:18 +01001731IConnectableLayer* Network::AddPreluLayer(const char* name)
1732{
1733 return m_Graph->AddLayer<PreluLayer>(name);
1734}
1735
Aron Virginas-Tar639fb042019-06-20 14:28:19 +01001736IConnectableLayer* Network::AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor,
1737 const ConstTensor& weights,
1738 const Optional<ConstTensor>& biases,
1739 const char* name)
1740{
1741 if (descriptor.m_BiasEnabled && !biases.has_value())
1742 {
1743 throw InvalidArgumentException("AddTransposeConvolution2dLayer: Biases cannot be empty");
1744 }
1745
1746 const auto layer = m_Graph->AddLayer<TransposeConvolution2dLayer>(descriptor, name);
1747
1748 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
1749
1750 if (descriptor.m_BiasEnabled)
1751 {
1752 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value());
1753 }
1754
1755 return layer;
1756}
1757
Mike Kellyc9ea45a2020-02-28 18:11:58 +00001758IConnectableLayer* Network::AddTransposeLayer(const TransposeDescriptor& transposeDescriptor,
1759 const char* name)
1760{
1761 return m_Graph->AddLayer<TransposeLayer>(transposeDescriptor, name);
1762}
1763
Matthew Jackson2b8c1da2019-07-04 14:59:16 +01001764IConnectableLayer* Network::AddStackLayer(const StackDescriptor& stackDescriptor,
1765 const char* name)
1766{
1767 return m_Graph->AddLayer<StackLayer>(stackDescriptor, name);
1768}
1769
Derek Lamberti013c3902019-10-21 10:46:16 +01001770
1771IConnectableLayer* Network::AddStandInLayer(const StandInDescriptor& desc,
1772 const char* name)
1773{
1774 return m_Graph->AddLayer<StandInLayer>(desc, name);
1775}
1776
James Conroyee18dc82019-07-17 11:27:46 +01001777IConnectableLayer* Network::AddQuantizedLstmLayer(const QuantizedLstmInputParams& params,
1778 const char* name)
1779{
1780 const auto layer = m_Graph->AddLayer<QuantizedLstmLayer>(name);
1781
1782 // InputToX weights
1783 layer->m_QuantizedLstmParameters.m_InputToInputWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001784 std::make_unique<ScopedCpuTensorHandle>(params.GetInputToInputWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001785 layer->m_QuantizedLstmParameters.m_InputToForgetWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001786 std::make_unique<ScopedCpuTensorHandle>(params.GetInputToForgetWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001787 layer->m_QuantizedLstmParameters.m_InputToCellWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001788 std::make_unique<ScopedCpuTensorHandle>(params.GetInputToCellWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001789 layer->m_QuantizedLstmParameters.m_InputToOutputWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001790 std::make_unique<ScopedCpuTensorHandle>(params.GetInputToOutputWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001791
1792 // RecurrentToX weights
1793 layer->m_QuantizedLstmParameters.m_RecurrentToInputWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001794 std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToInputWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001795 layer->m_QuantizedLstmParameters.m_RecurrentToForgetWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001796 std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToForgetWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001797 layer->m_QuantizedLstmParameters.m_RecurrentToCellWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001798 std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToCellWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001799 layer->m_QuantizedLstmParameters.m_RecurrentToOutputWeights =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001800 std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToOutputWeights());
James Conroyee18dc82019-07-17 11:27:46 +01001801
1802 // Bias
1803 layer->m_QuantizedLstmParameters.m_InputGateBias =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001804 std::make_unique<ScopedCpuTensorHandle>(params.GetInputGateBias());
James Conroyee18dc82019-07-17 11:27:46 +01001805 layer->m_QuantizedLstmParameters.m_ForgetGateBias =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001806 std::make_unique<ScopedCpuTensorHandle>(params.GetForgetGateBias());
James Conroyee18dc82019-07-17 11:27:46 +01001807 layer->m_QuantizedLstmParameters.m_CellBias =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001808 std::make_unique<ScopedCpuTensorHandle>(params.GetCellBias());
James Conroyee18dc82019-07-17 11:27:46 +01001809 layer->m_QuantizedLstmParameters.m_OutputGateBias =
Francis Murtaghbb590b42019-08-14 09:51:36 +01001810 std::make_unique<ScopedCpuTensorHandle>(params.GetOutputGateBias());
James Conroyee18dc82019-07-17 11:27:46 +01001811
1812 return layer;
1813}
1814
James Conroy586a9aa2020-03-20 08:49:33 +00001815IConnectableLayer* Network::AddQLstmLayer(const QLstmDescriptor& descriptor,
1816 const LstmInputParams& params,
1817 const char* name)
1818{
1819 const auto layer = m_Graph->AddLayer<QLstmLayer>(descriptor, name);
1820
1821 // QLstm Basic Parameters
1822 layer->m_BasicParameters.m_InputToForgetWeights =
1823 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights));
1824 layer->m_BasicParameters.m_InputToCellWeights =
1825 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights));
1826 layer->m_BasicParameters.m_InputToOutputWeights =
1827 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights));
1828 layer->m_BasicParameters.m_RecurrentToForgetWeights =
1829 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights));
1830 layer->m_BasicParameters.m_RecurrentToCellWeights =
1831 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights));
1832 layer->m_BasicParameters.m_RecurrentToOutputWeights =
1833 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights));
1834 layer->m_BasicParameters.m_ForgetGateBias =
1835 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias));
1836 layer->m_BasicParameters.m_CellBias =
1837 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias));
1838 layer->m_BasicParameters.m_OutputGateBias =
1839 std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias));
1840
1841 // QLstm Cifg parameters
1842 if(!descriptor.m_CifgEnabled)
1843 {
1844 if(params.m_InputToInputWeights == nullptr)
1845 {
1846 throw InvalidArgumentException("AddQLstmLayer: Input To Input Weights cannot be NULL");
1847 }
1848
1849 if(params.m_RecurrentToInputWeights == nullptr)
1850 {
1851 throw InvalidArgumentException(
1852 "AddQLstmLayer: Recurrent To Input Weights cannot be NULL");
1853 }
1854
1855 if(params.m_InputGateBias == nullptr)
1856 {
1857 throw InvalidArgumentException("AddQLstmLayer: Input Gate Bias cannot be NULL");
1858 }
1859
1860 layer->m_CifgParameters.m_InputToInputWeights =
1861 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights));
1862 layer->m_CifgParameters.m_RecurrentToInputWeights =
1863 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights));
1864 layer->m_CifgParameters.m_InputGateBias =
1865 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias));
1866 }
1867
1868 // QLstm Projection parameters
1869 if(descriptor.m_ProjectionEnabled)
1870 {
1871 if(params.m_ProjectionWeights == nullptr)
1872 {
1873 throw InvalidArgumentException("AddQLstmLayer: Projection Weights cannot be NULL");
1874 }
1875
1876 if(params.m_ProjectionBias == nullptr)
1877 {
1878 throw InvalidArgumentException("AddQLstmLayer: Projection Biases cannot be NULL");
1879 }
1880
1881 layer->m_ProjectionParameters.m_ProjectionWeights =
1882 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights));
1883 layer->m_ProjectionParameters.m_ProjectionBias =
1884 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias));
1885 }
1886
1887 // QLstm Peephole params
1888 if(descriptor.m_PeepholeEnabled)
1889 {
1890 if(params.m_CellToForgetWeights == nullptr)
1891 {
1892 throw InvalidArgumentException("AddQLstmLayer: Cell To Forget Weights cannot be NULL");
1893 }
1894
1895 if(params.m_CellToOutputWeights == nullptr)
1896 {
1897 throw InvalidArgumentException("AddQLstmLayer: Cell To Output Weights cannot be NULL");
1898 }
1899
1900 if(!descriptor.m_CifgEnabled)
1901 {
1902 if(params.m_CellToInputWeights == nullptr)
1903 {
1904 throw InvalidArgumentException("AddQLstmLayer: Cell To Input Weights cannot be NULL");
1905 }
1906
1907 layer->m_PeepholeParameters.m_CellToInputWeights =
1908 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights));
1909 }
1910
1911 layer->m_PeepholeParameters.m_CellToForgetWeights =
1912 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights));
1913 layer->m_PeepholeParameters.m_CellToOutputWeights =
1914 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights));
1915 }
1916
1917 // QLstm Layer Normalization params
1918 if(descriptor.m_LayerNormEnabled)
1919 {
1920 if(params.m_ForgetLayerNormWeights == nullptr)
1921 {
1922 throw InvalidArgumentException("AddQLstmLayer: Forget layer normalization weights cannot be NULL");
1923 }
1924
1925 if(params.m_CellLayerNormWeights == nullptr)
1926 {
1927 throw InvalidArgumentException("AddQLstmLayer: Cell layer normalization weights cannot be NULL");
1928 }
1929
1930 if(params.m_OutputLayerNormWeights == nullptr)
1931 {
1932 throw InvalidArgumentException("AddQLstmLayer: Output layer normalization weights cannot be NULL");
1933 }
1934
1935 if(!descriptor.m_CifgEnabled)
1936 {
1937 if(params.m_InputLayerNormWeights == nullptr)
1938 {
1939 throw InvalidArgumentException("AddQLstmLayer: Input layer normalization weights cannot be NULL");
1940 }
1941
1942 layer->m_LayerNormParameters.m_InputLayerNormWeights =
1943 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputLayerNormWeights));
1944 }
1945
1946 layer->m_LayerNormParameters.m_ForgetLayerNormWeights =
1947 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetLayerNormWeights));
1948 layer->m_LayerNormParameters.m_CellLayerNormWeights =
1949 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellLayerNormWeights));
1950 layer->m_LayerNormParameters.m_OutputLayerNormWeights =
1951 std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputLayerNormWeights));
1952 }
1953 return layer;
1954}
1955
Mike Kelly8c1701a2019-02-11 17:01:27 +00001956void Network::Accept(ILayerVisitor& visitor) const
1957{
1958 for (auto layer : GetGraph())
1959 {
1960 layer->Accept(visitor);
1961 };
1962}
1963
telsoa014fcda012018-03-09 14:13:49 +00001964OptimizedNetwork::OptimizedNetwork(std::unique_ptr<Graph> graph)
Sadik Armagan3184c902020-03-18 10:57:30 +00001965 : m_Graph(std::move(graph)), m_Guid(profiling::ProfilingService::GetNextGuid())
telsoa014fcda012018-03-09 14:13:49 +00001966{
1967}
1968
1969OptimizedNetwork::~OptimizedNetwork()
1970{
1971}
1972
1973} // namespace armnn