blob: 3b3ee3146a51e184e2a533cb3a05f3c36f3a2764 [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//
5#include "Network.hpp"
6#include "Graph.hpp"
7#include "Layer.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01008#include "DeviceSpec.hpp"
telsoa014fcda012018-03-09 14:13:49 +00009#include "Optimizer.hpp"
David Beckac42efd2018-09-26 17:41:13 +010010#include "optimizations/All.hpp"
telsoa014fcda012018-03-09 14:13:49 +000011
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000012#include <backendsCommon/CpuTensorHandle.hpp>
13#include <backendsCommon/WorkloadFactory.hpp>
David Beck263e3492018-11-09 14:46:40 +000014#include <backendsCommon/BackendRegistry.hpp>
15#include <backendsCommon/IBackendInternal.hpp>
David Beckac42efd2018-09-26 17:41:13 +010016
17#include <armnn/Exceptions.hpp>
telsoa014fcda012018-03-09 14:13:49 +000018#include <armnn/Utils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010019#include <armnn/TypesUtils.hpp>
telsoa014fcda012018-03-09 14:13:49 +000020
21#include <fcntl.h>
22#include <algorithm>
23#include <fstream>
24#include <memory>
telsoa01c577f2c2018-08-31 09:22:23 +010025#include <vector>
26#include <algorithm>
telsoa014fcda012018-03-09 14:13:49 +000027
28#include <boost/assert.hpp>
29#include <boost/format.hpp>
30#include <boost/log/trivial.hpp>
31#include <boost/numeric/conversion/converter_policies.hpp>
32#include <boost/cast.hpp>
33
34namespace armnn
35{
36
37armnn::INetwork* INetwork::CreateRaw()
38{
39 return new Network();
40}
41
42armnn::INetworkPtr INetwork::Create()
43{
44 return INetworkPtr(CreateRaw(), &INetwork::Destroy);
45}
46
47void INetwork::Destroy(INetwork* network)
48{
49 delete boost::polymorphic_downcast<Network*>(network);
50}
51
52Status Network::PrintGraph()
53{
54 m_Graph->Print();
55 return Status::Success;
56}
57
58void IOptimizedNetwork::Destroy(IOptimizedNetwork* network)
59{
60 delete boost::polymorphic_downcast<OptimizedNetwork*>(network);
61}
62
63Status OptimizedNetwork::PrintGraph()
64{
65 m_Graph->Print();
66 return Status::Success;
67}
68
surmeh01bceff2f2018-03-29 16:29:27 +010069Status OptimizedNetwork::SerializeToDot(std::ostream& stream) const
70{
71 return m_Graph->SerializeToDot(stream);
72}
73
jimfly016b0b53d2018-10-08 14:43:01 +010074bool CheckScaleSetOnQuantizedType(Layer* layer, Optional<std::vector<std::string>&> errMessages)
75{
76 bool noErrors = true;
77 unsigned int numOutputs = layer->GetNumOutputSlots();
78 for (unsigned int i = 0; i < numOutputs; i++) {
79 const OutputSlot &outputSlot = layer->GetOutputSlot(i);
80 const TensorInfo &info = outputSlot.GetTensorInfo();
81 if (DataType::QuantisedAsymm8 == info.GetDataType()) {
82 if (0.f == info.GetQuantizationScale()) {
83 noErrors = false;
84 std::stringstream ss;
85 ss << "ERROR: output " << i << " of layer " << GetLayerTypeAsCString(layer->GetType())
86 << " (" << layer->GetNameStr() << ") is of type"
87 << " Quantized 8 bit but its scale parameter has not been set";
88 BOOST_LOG_TRIVIAL(warning) << ss.str() ;
89 if (errMessages) {
90 errMessages.value().push_back(ss.str());
91 }
92 }
93 }
94 }
95 return noErrors;
96}
97
telsoa01c577f2c2018-08-31 09:22:23 +010098IOptimizedNetworkPtr Optimize(const INetwork& inNetwork,
David Beckf0b48452018-10-19 15:20:56 +010099 const std::vector<BackendId>& backendPreferences,
telsoa01c577f2c2018-08-31 09:22:23 +0100100 const IDeviceSpec& deviceSpec,
jimfly016b0b53d2018-10-08 14:43:01 +0100101 const OptimizerOptions& options,
102 Optional<std::vector<std::string>&> errMessages)
telsoa014fcda012018-03-09 14:13:49 +0000103{
telsoa01c577f2c2018-08-31 09:22:23 +0100104 if (backendPreferences.empty()) {
105 throw armnn::InvalidArgumentException("Invoked Optimize with no backends specified");
106 }
telsoa014fcda012018-03-09 14:13:49 +0000107 const Network& network = *boost::polymorphic_downcast<const Network*>(&inNetwork);
108 std::unique_ptr<Graph> graph = std::make_unique<Graph>(network.GetGraph());
109
telsoa01c577f2c2018-08-31 09:22:23 +0100110 auto optNet = IOptimizedNetworkPtr(new OptimizedNetwork(std::move(graph)), &IOptimizedNetwork::Destroy);
telsoa014fcda012018-03-09 14:13:49 +0000111
telsoa01c577f2c2018-08-31 09:22:23 +0100112 OptimizedNetwork* optNetObjPtr = boost::polymorphic_downcast<OptimizedNetwork*>(optNet.get());
113
114 // Perform optimisation passes
115 using namespace optimizations;
116 Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(SquashEqualPermuteSiblings(),
117 SquashEqualReshapeSiblings(),
118 OptimizeInversePermutes(),
119 MovePermuteUp(),
120 PermuteAsReshape(),
121 OptimizeConsecutiveReshapes()));
telsoa014fcda012018-03-09 14:13:49 +0000122
123 // Infer the tensor infos for all output slots. Throws an exception on failure.
telsoa01c577f2c2018-08-31 09:22:23 +0100124 optNetObjPtr->GetGraph().InferTensorInfos();
telsoa014fcda012018-03-09 14:13:49 +0000125
telsoa01c577f2c2018-08-31 09:22:23 +0100126 // if Fp32 to Fp16 optimization is set convert Fp32 network to Fp16
127 if (options.m_ReduceFp32ToFp16)
telsoa014fcda012018-03-09 14:13:49 +0000128 {
telsoa01c577f2c2018-08-31 09:22:23 +0100129 Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(Fp32NetworkToFp16Converter()));
telsoa014fcda012018-03-09 14:13:49 +0000130 }
131
telsoa01c577f2c2018-08-31 09:22:23 +0100132 // We know that DeviceSpec should be the only implementation of IDeviceSpec.
133 const DeviceSpec& spec = *boost::polymorphic_downcast<const DeviceSpec*>(&deviceSpec);
David Beck056be3c2018-10-22 13:16:00 +0100134 auto const& supportedBackends = spec.GetSupportedBackends();
telsoa014fcda012018-03-09 14:13:49 +0000135
telsoa01c577f2c2018-08-31 09:22:23 +0100136 // determine which of the preferred backends we have available for use
137 // and whether we have specified CpuRef as one of those backends.
138 bool cpuRefUsed = false;
David Beckf0b48452018-10-19 15:20:56 +0100139 std::vector<BackendId> availablePreferredBackends;
140 for (const auto& backend : backendPreferences)
telsoa01c577f2c2018-08-31 09:22:23 +0100141 {
142 // Check if the backend is in the available backend devices.
David Beck056be3c2018-10-22 13:16:00 +0100143 if (supportedBackends.count(backend) > 0)
telsoa01c577f2c2018-08-31 09:22:23 +0100144 {
145 availablePreferredBackends.push_back(backend);
David Beckf0b48452018-10-19 15:20:56 +0100146 if (backend == armnn::Compute::CpuRef) {
telsoa01c577f2c2018-08-31 09:22:23 +0100147 cpuRefUsed = true;
148 }
149 }
150 }
151 if (availablePreferredBackends.empty()) {
jimfly016b0b53d2018-10-08 14:43:01 +0100152 std::stringstream failureMsg;
153 failureMsg << "ERROR: None of the preferred backends " << backendPreferences
David Beck056be3c2018-10-22 13:16:00 +0100154 << " are supported. Current platform provides " << supportedBackends;
jimfly016b0b53d2018-10-08 14:43:01 +0100155 BOOST_LOG_TRIVIAL(warning) << failureMsg.str();
156 if (errMessages) {
157 errMessages.value().push_back(failureMsg.str());
158 }
159 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
telsoa01c577f2c2018-08-31 09:22:23 +0100160 }
161
162 auto ReturnWithError = [&](Layer* layer)
163 {
jimfly016b0b53d2018-10-08 14:43:01 +0100164 std::stringstream failureMsg;
165 failureMsg << "ERROR: Layer of type " << GetLayerTypeAsCString(layer->GetType())
166 << " is not supported on any preferred backend " << backendPreferences;
167 BOOST_LOG_TRIVIAL(warning) << failureMsg.str();
168 if (errMessages) {
169 errMessages.value().push_back(failureMsg.str());
170 }
telsoa01c577f2c2018-08-31 09:22:23 +0100171 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
172 };
173
David Beck263e3492018-11-09 14:46:40 +0000174 // The backends that we choose to run layers on
175 std::unordered_set<BackendId> chosenBackends;
176
telsoa01c577f2c2018-08-31 09:22:23 +0100177 // Assign a compute device for all nodes
jimfly016b0b53d2018-10-08 14:43:01 +0100178 bool bErrorFound = false;
telsoa01c577f2c2018-08-31 09:22:23 +0100179 for (auto&& layer : optNetObjPtr->GetGraph())
180 {
181 DataType dataType = layer->GetDataType();
182 std::string reasonIfUnsupported;
183 bool found = false;
jimfly016b0b53d2018-10-08 14:43:01 +0100184 if (!CheckScaleSetOnQuantizedType(layer, errMessages))
185 {
186 // don't bomb immediately, find all the quantized outputs
187 // which haven't had a scale set and report them all back.
188 bErrorFound = true;
189 }
David Beckf0b48452018-10-19 15:20:56 +0100190 for (const auto& backend : availablePreferredBackends)
telsoa01c577f2c2018-08-31 09:22:23 +0100191 {
192 // need to set the compute device on the layer
193 // before we can check if it is supported
David Beck33f0ae02018-10-18 15:13:56 +0100194 layer->SetBackendId(backend);
telsoa01c577f2c2018-08-31 09:22:23 +0100195 if (!IWorkloadFactory::IsLayerSupported(*layer, dataType, reasonIfUnsupported))
196 {
197 if (dataType == DataType::Float16)
198 {
199 if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported)
200 && layer->GetType() != LayerType::ConvertFp32ToFp16
201 && layer->GetType() != LayerType::ConvertFp16ToFp32)
202 {
203 // Insert FP16 -> FP32 conversion layer before current layer
204 std::vector<ConvertFp16ToFp32Layer*> convertFp16ToFp32Layers =
205 InsertConvertFp16ToFp32LayersBefore(optNetObjPtr->GetGraph(), *layer);
206
207 // Insert FP32 -> FP16 conversion layer after current layer
208 std::vector<ConvertFp32ToFp16Layer*> convertFp32ToFp16Layers =
209 InsertConvertFp32ToFp16LayersAfter(optNetObjPtr->GetGraph(), *layer);
210
211 // Assign a supported backend to the newly introduced conversion layers
David Beckf0b48452018-10-19 15:20:56 +0100212 auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend)
telsoa01c577f2c2018-08-31 09:22:23 +0100213 {
214 bool supportedBackendFound = false;
215 std::string reasonIfUnsupported;
216
217 // Try preferred backend first
David Beck33f0ae02018-10-18 15:13:56 +0100218 layer->SetBackendId(preferredBackend);
David Beck29c75de2018-10-23 13:35:58 +0100219 if (IWorkloadFactory::IsLayerSupported(*layer,
220 EmptyOptional(),
221 reasonIfUnsupported))
telsoa01c577f2c2018-08-31 09:22:23 +0100222 {
223 supportedBackendFound = true;
224 }
225 else
226 {
David Beckf0b48452018-10-19 15:20:56 +0100227 for (const auto& backend : availablePreferredBackends)
telsoa01c577f2c2018-08-31 09:22:23 +0100228 {
229 // Skip preferred backend (we already determined that it is not supported)
230 if (backend == preferredBackend)
231 {
232 continue;
233 }
234
David Beck33f0ae02018-10-18 15:13:56 +0100235 layer->SetBackendId(backend);
David Beck29c75de2018-10-23 13:35:58 +0100236 if (IWorkloadFactory::IsLayerSupported(*layer,
237 EmptyOptional(),
238 reasonIfUnsupported))
telsoa01c577f2c2018-08-31 09:22:23 +0100239 {
240 supportedBackendFound = true;
241 break;
242 }
243 }
244 }
245
246 return supportedBackendFound;
247 };
248
249 for (ConvertFp16ToFp32Layer* convertLayer : convertFp16ToFp32Layers)
250 {
251 if (!AssignFirstSupportedBackend(convertLayer, backend))
252 {
253 return ReturnWithError(convertLayer);
254 }
255 }
256
257 for (ConvertFp32ToFp16Layer* convertLayer : convertFp32ToFp16Layers)
258 {
259 if (!AssignFirstSupportedBackend(convertLayer, backend))
260 {
261 return ReturnWithError(convertLayer);
262 }
263 }
264
265 found = true;
266 break;
267 }
268 }
jimfly016b0b53d2018-10-08 14:43:01 +0100269 std::stringstream warningMsg;
270 warningMsg << "WARNING: Layer of type " << GetLayerTypeAsCString(layer->GetType())
David Beck33f0ae02018-10-18 15:13:56 +0100271 << " is not supported on requested backend " << layer->GetBackendId().Get()
jimfly016b0b53d2018-10-08 14:43:01 +0100272 << " for data type " << GetDataTypeName(dataType)
273 << " (reason: " << reasonIfUnsupported
274 << "), falling back to the next backend.";
275 BOOST_LOG_TRIVIAL(warning) << warningMsg.str();
276 if (errMessages) {
277 errMessages.value().push_back(warningMsg.str());
278 }
telsoa01c577f2c2018-08-31 09:22:23 +0100279 }
280 else
281 {
282 found = true;
David Beck263e3492018-11-09 14:46:40 +0000283 chosenBackends.insert(backend);
telsoa01c577f2c2018-08-31 09:22:23 +0100284 break;
285 }
286 }
287
288 // If the layer is unsupported by any devices, log and return a null network.
289 if (!found) {
290 // NOTE: if the layer is not an operation queue type AND we have not got CpuRef as a
291 // fallback we should set the compute device on the layer to CpuRef (these are not
292 // available as accelerated operations, or are only available under certain
293 // conditions, currently they comprise MemCopy, Constant, Permute)
294 armnn::LayerType layerType = layer->GetType();
295 if (!cpuRefUsed && (layerType == armnn::LayerType::MemCopy ||
296 layerType == armnn::LayerType::Constant ||
297 layerType == armnn::LayerType::Permute))
298 {
David Beck33f0ae02018-10-18 15:13:56 +0100299 layer->SetBackendId(armnn::Compute::CpuRef);
David Beck263e3492018-11-09 14:46:40 +0000300 chosenBackends.insert(armnn::Compute::CpuRef);
telsoa01c577f2c2018-08-31 09:22:23 +0100301 }
302 else
303 {
304 return ReturnWithError(layer);
305 }
306 }
307 }
jimfly016b0b53d2018-10-08 14:43:01 +0100308 if (bErrorFound)
309 {
310 return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy);
311 }
telsoa01c577f2c2018-08-31 09:22:23 +0100312
313 Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(OptimizeInverseConversionsFp16(),
314 OptimizeInverseConversionsFp32()));
315
316 optNetObjPtr->GetGraph().AddCopyLayers();
317
318 // Convert constants
319 Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(ConvertConstantsFloatToHalf()));
320 Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(ConvertConstantsHalfToFloat()));
321
David Beck263e3492018-11-09 14:46:40 +0000322 // Run backend specific optimizations
323 for (auto&& chosenBackend : chosenBackends)
324 {
325 auto factoryFun = BackendRegistryInstance().GetFactory(chosenBackend);
326 auto backendPtr = factoryFun();
327 BOOST_ASSERT(backendPtr.get() != nullptr);
328
329 auto backendSpecificOptimizations = backendPtr->GetOptimizations();
330 if (!backendSpecificOptimizations.empty())
331 {
332 Optimizer::Pass(optNetObjPtr->GetGraph(), backendSpecificOptimizations);
333 }
334 }
335
telsoa01c577f2c2018-08-31 09:22:23 +0100336 return optNet;
telsoa014fcda012018-03-09 14:13:49 +0000337}
338
jimfly016b0b53d2018-10-08 14:43:01 +0100339
telsoa014fcda012018-03-09 14:13:49 +0000340Network::Network()
341: m_Graph(std::make_unique<Graph>())
342{
343}
344
345Network::~Network()
346{
347}
348
349IConnectableLayer* Network::AddInputLayer(LayerBindingId id, const char* name)
350{
351 return m_Graph->AddLayer<InputLayer>(id, name);
352}
353
Éanna Ó Catháin4e1e1362018-11-12 11:36:34 +0000354IConnectableLayer* Network::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor,
355 const char* name)
356{
357 return m_Graph->AddLayer<BatchToSpaceNdLayer>(batchToSpaceNdDescriptor, name);
358}
359
telsoa014fcda012018-03-09 14:13:49 +0000360IConnectableLayer* Network::AddFullyConnectedLayerImpl(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +0100361 const ConstTensor& weights,
362 const ConstTensor* biases,
363 const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000364{
365 if (fullyConnectedDescriptor.m_BiasEnabled && (biases == nullptr))
366 {
367 throw InvalidArgumentException("AddFullyConnectedLayer: biases cannot be NULL");
368 }
369
370 const auto layer = m_Graph->AddLayer<FullyConnectedLayer>(fullyConnectedDescriptor, name);
371
372 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
373
374 if (fullyConnectedDescriptor.m_BiasEnabled)
375 {
376 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(*biases);
377 }
378
379 return layer;
380}
381
382IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +0100383 const ConstTensor& weights,
384 const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000385{
386 return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, nullptr, name);
387}
388
389IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +0100390 const ConstTensor& weights,
391 const ConstTensor& biases,
392 const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000393{
394 return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, &biases, name);
395}
396
397IConnectableLayer* Network::AddConvolution2dLayerImpl(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +0100398 const ConstTensor& weights,
399 const ConstTensor* biases,
400 const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000401{
402 if (convolution2dDescriptor.m_BiasEnabled && (biases == nullptr))
403 {
404 throw InvalidArgumentException("AddConvolution2dLayer: biases cannot be NULL");
405 }
406
407 const auto layer = m_Graph->AddLayer<Convolution2dLayer>(convolution2dDescriptor, name);
408
409 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
410
411 if (convolution2dDescriptor.m_BiasEnabled)
412 {
413 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(*biases);
414 }
415
416 return layer;
417}
418
419IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +0100420 const ConstTensor& weights,
421 const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000422{
423 return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, nullptr, name);
424}
425IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor,
telsoa01c577f2c2018-08-31 09:22:23 +0100426 const ConstTensor& weights,
427 const ConstTensor& biases,
428 const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000429{
430 return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, &biases, name);
431}
432
433IConnectableLayer* Network::AddDepthwiseConvolution2dLayerImpl(
434 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
435 const ConstTensor& weights,
436 const ConstTensor* biases,
437 const char* name)
438{
439 if (convolution2dDescriptor.m_BiasEnabled && (biases == nullptr))
440 {
441 throw InvalidArgumentException("AddDepthwiseConvolution2dLayer: biases cannot be NULL");
442 }
443
telsoa01c577f2c2018-08-31 09:22:23 +0100444 const auto layer = m_Graph->AddLayer<DepthwiseConvolution2dLayer>(convolution2dDescriptor,
445 name);
telsoa014fcda012018-03-09 14:13:49 +0000446
447 layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights);
448
449 if (convolution2dDescriptor.m_BiasEnabled)
450 {
451 layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(*biases);
452 }
453
454 return layer;
455}
456
457IConnectableLayer* Network::AddDepthwiseConvolution2dLayer(
458 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
459 const ConstTensor& weights,
460 const char* name)
461{
462 return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, nullptr, name);
463}
464IConnectableLayer* Network::AddDepthwiseConvolution2dLayer(
465 const DepthwiseConvolution2dDescriptor& convolution2dDescriptor,
466 const ConstTensor& weights,
467 const ConstTensor& biases,
468 const char* name)
469{
470 return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, &biases, name);
471}
472
473IConnectableLayer* Network::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor,
474 const char* name)
475{
476 return m_Graph->AddLayer<PermuteLayer>(permuteDescriptor, name);
477}
478
479IConnectableLayer* Network::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor,
480 const char* name)
481{
482 return m_Graph->AddLayer<Pooling2dLayer>(pooling2dDescriptor, name);
483}
484
485IConnectableLayer* Network::AddActivationLayer(const ActivationDescriptor& activationDescriptor,
486 const char* name)
487{
488 return m_Graph->AddLayer<ActivationLayer>(activationDescriptor, name);
489}
490
telsoa01c577f2c2018-08-31 09:22:23 +0100491IConnectableLayer* Network::AddNormalizationLayer(const NormalizationDescriptor&
492normalizationDescriptor,
telsoa014fcda012018-03-09 14:13:49 +0000493 const char* name)
494{
495 return m_Graph->AddLayer<NormalizationLayer>(normalizationDescriptor, name);
496}
497
498IConnectableLayer* Network::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor,
499 const char* name)
500{
501 return m_Graph->AddLayer<SoftmaxLayer>(softmaxDescriptor, name);
502}
503
504IConnectableLayer* Network::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor,
505 const char* name)
506{
507 return m_Graph->AddLayer<SplitterLayer>(splitterDescriptor, name);
508}
509
510IConnectableLayer* Network::AddMergerLayer(const OriginsDescriptor& mergerDescriptor,
511 const char* name)
512{
513 return m_Graph->AddLayer<MergerLayer>(mergerDescriptor, name);
514}
515
516IConnectableLayer* Network::AddAdditionLayer(const char* name)
517{
518 return m_Graph->AddLayer<AdditionLayer>(name);
519}
520
521IConnectableLayer* Network::AddMultiplicationLayer(const char* name)
522{
523 return m_Graph->AddLayer<MultiplicationLayer>(name);
524}
525
526IConnectableLayer* Network::AddOutputLayer(LayerBindingId id, const char* name)
527{
528 return m_Graph->AddLayer<OutputLayer>(id, name);
529}
530
531IConnectableLayer* Network::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc,
532 const ConstTensor& mean,
533 const ConstTensor& variance,
534 const ConstTensor& beta,
535 const ConstTensor& gamma,
536 const char* name)
537{
538 const auto layer = m_Graph->AddLayer<BatchNormalizationLayer>(desc, name);
539
540 layer->m_Mean = std::make_unique<ScopedCpuTensorHandle>(mean);
541 layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(variance);
542 layer->m_Beta = std::make_unique<ScopedCpuTensorHandle>(beta);
543 layer->m_Gamma = std::make_unique<ScopedCpuTensorHandle>(gamma);
544
545 return layer;
546}
547
telsoa01c577f2c2018-08-31 09:22:23 +0100548IConnectableLayer* Network::AddResizeBilinearLayer(const ResizeBilinearDescriptor&
549resizeDescriptor, const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000550{
551 return m_Graph->AddLayer<ResizeBilinearLayer>(resizeDescriptor,name);
552}
553
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100554IConnectableLayer* Network::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc,
555 const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000556{
Matteo Martincighbcd3c852018-09-28 14:14:12 +0100557 return m_Graph->AddLayer<L2NormalizationLayer>(desc, name);
telsoa014fcda012018-03-09 14:13:49 +0000558}
559
560IConnectableLayer* Network::AddConstantLayer(const ConstTensor& input, const char* name)
561{
telsoa01c577f2c2018-08-31 09:22:23 +0100562 auto layer = m_Graph->AddLayer<ConstantLayer>(name);
563
564 layer->m_LayerOutput = std::make_unique<ScopedCpuTensorHandle>(input);
565
566 return layer;
telsoa014fcda012018-03-09 14:13:49 +0000567}
568
telsoa01c577f2c2018-08-31 09:22:23 +0100569IConnectableLayer* Network::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor,
570 const char* name)
telsoa014fcda012018-03-09 14:13:49 +0000571{
572 return m_Graph->AddLayer<ReshapeLayer>(reshapeDescriptor, name);
573}
574
Nattapat Chaimanowong207ef9a2018-11-02 10:57:25 +0000575IConnectableLayer* Network::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor,
576 const char* name)
577{
578 return m_Graph->AddLayer<SpaceToBatchNdLayer>(spaceToBatchNdDescriptor, name);
579}
580
telsoa014fcda012018-03-09 14:13:49 +0000581IConnectableLayer* Network::AddFloorLayer(const char* name)
582{
583 return m_Graph->AddLayer<FloorLayer>(name);
584}
585
telsoa01c577f2c2018-08-31 09:22:23 +0100586IConnectableLayer* Network::AddLstmLayer(const LstmDescriptor& descriptor,
587 const LstmInputParams& params,
588 const char* name)
589{
590 const auto layer = m_Graph->AddLayer<LstmLayer>(descriptor, name);
591
592 //Lstm Basic Parameters
593 layer->m_BasicParameters.m_InputToForgetWeights =
594 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights));
595 layer->m_BasicParameters.m_InputToCellWeights =
596 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights));
597 layer->m_BasicParameters.m_InputToOutputWeights =
598 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights));
599 layer->m_BasicParameters.m_RecurrentToForgetWeights =
600 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights));
601 layer->m_BasicParameters.m_RecurrentToCellWeights =
602 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights));
603 layer->m_BasicParameters.m_RecurrentToOutputWeights =
604 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights));
605 layer->m_BasicParameters.m_ForgetGateBias =
606 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias));
607 layer->m_BasicParameters.m_CellBias =
608 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias));
609 layer->m_BasicParameters.m_OutputGateBias =
610 std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias));
611
612 //Lstm Cifg parameters
613 if(!descriptor.m_CifgEnabled)
614 {
615 if(params.m_InputToInputWeights == nullptr)
616 {
617 throw InvalidArgumentException("AddLstmLayer: Input To Input Weights cannot be NULL");
618 }
619 if(params.m_RecurrentToInputWeights == nullptr)
620 {
621 throw InvalidArgumentException(
622 "AddLstmLayer: Recurrent To Input Weights cannot be NULL");
623 }
624 if(params.m_InputGateBias == nullptr)
625 {
626 throw InvalidArgumentException("AddLstmLayer: Input Gate Bias cannot be NULL");
627 }
628 layer->m_CifgParameters.m_InputToInputWeights =
629 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights));
630 layer->m_CifgParameters.m_RecurrentToInputWeights =
631 std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights));
632 // In the VTS tests, cell-to-input weights may be null, even if the other CIFG params are not.
633 if(params.m_CellToInputWeights != nullptr)
634 {
635 layer->m_CifgParameters.m_CellToInputWeights =
636 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights));
637 }
638 layer->m_CifgParameters.m_InputGateBias =
639 std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias));
640 }
641
642 //Lstm projection parameters
643 if(descriptor.m_ProjectionEnabled)
644 {
645 if(params.m_ProjectionWeights == nullptr)
646 {
647 throw InvalidArgumentException("AddLstmLayer: Projection Weights cannot be NULL");
648 }
649 layer->m_ProjectionParameters.m_ProjectionWeights =
650 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights));
651 if(params.m_ProjectionBias != nullptr)
652 {
653 layer->m_ProjectionParameters.m_ProjectionBias =
654 std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias));
655 }
656 }
657
658 //Lstm Peephole params
659 if(descriptor.m_PeepholeEnabled)
660 {
661 if(params.m_CellToForgetWeights == nullptr)
662 {
663 throw InvalidArgumentException("AddLstmLayer: Cell To Forget Weights cannot be NULL");
664 }
665 if(params.m_CellToOutputWeights == nullptr)
666 {
667 throw InvalidArgumentException("AddLstmLayer: Cell To Output Weights cannot be NULL");
668 }
669 layer->m_PeepholeParameters.m_CellToForgetWeights =
670 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights));
671 layer->m_PeepholeParameters.m_CellToOutputWeights =
672 std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights));
673 }
674 return layer;
675}
676
Francis Murtaghe7a86a42018-08-29 12:42:10 +0100677IConnectableLayer* Network::AddDivisionLayer(const char* name)
678{
679 return m_Graph->AddLayer<DivisionLayer>(name);
680}
681
David Beck19526222018-09-12 16:00:08 +0100682IConnectableLayer* Network::AddSubtractionLayer(const char* name)
683{
684 return m_Graph->AddLayer<SubtractionLayer>(name);
685}
686
narpra0132b90462018-09-13 11:07:48 +0100687IConnectableLayer* Network::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name)
688{
689 return m_Graph->AddLayer<MeanLayer>(meanDescriptor,name);
690}
691
Mohamed Nour Abouelseoud5662c202018-09-24 13:30:09 +0100692IConnectableLayer* Network::AddPadLayer(const PadDescriptor& padDescriptor, const char* name)
693{
694 return m_Graph->AddLayer<PadLayer>(padDescriptor,name);
695}
696
telsoa014fcda012018-03-09 14:13:49 +0000697OptimizedNetwork::OptimizedNetwork(std::unique_ptr<Graph> graph)
698 : m_Graph(std::move(graph))
699{
700}
701
702OptimizedNetwork::~OptimizedNetwork()
703{
704}
705
706} // namespace armnn