blob: bbcbb9f6f65a4ecaad05780c7eba80d91ecc525c [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#include "Runtime.hpp"
6
David Beck056be3c2018-10-22 13:16:00 +01007#include <armnn/Version.hpp>
Matteo Martincighc601aa62019-10-29 15:03:22 +00008#include <armnn/BackendRegistry.hpp>
Jan Eilers15fcc7e2021-07-14 13:50:15 +01009#include <armnn/BackendHelper.hpp>
Matthew Benthamf48afc62020-01-15 17:55:08 +000010#include <armnn/Logging.hpp>
alered01a7227ac2020-05-07 14:58:29 +010011#include <armnn/utility/Timer.hpp>
Matteo Martincighe54aa062019-08-05 14:12:11 +010012
Matteo Martincighe5b8eb92019-11-28 15:45:42 +000013#include <armnn/backends/IBackendContext.hpp>
Matteo Martincighe54aa062019-08-05 14:12:11 +010014#include <backendsCommon/DynamicBackendUtils.hpp>
Jan Eilersbb446e52020-04-02 13:56:54 +010015#include <armnn/utility/PolymorphicDowncast.hpp>
telsoa014fcda012018-03-09 14:13:49 +000016
Nikhil Raj77fe76b2021-06-09 14:55:32 +010017#include <common/include/LabelsAndEventClasses.hpp>
18
surmeh013537c2c2018-05-18 16:31:43 +010019#include <iostream>
20
Colm Donelan1aff3932020-02-05 17:48:59 +000021#include <backends/BackendProfiling.hpp>
telsoa014fcda012018-03-09 14:13:49 +000022
23using namespace armnn;
24using namespace std;
25
26namespace armnn
27{
Kevin Mayd92a6e42021-02-04 10:27:41 +000028IRuntime::IRuntime() : pRuntimeImpl( new RuntimeImpl(armnn::IRuntime::CreationOptions())) {}
29
30IRuntime::IRuntime(const IRuntime::CreationOptions& options) : pRuntimeImpl(new RuntimeImpl(options)) {}
31
32IRuntime::~IRuntime() = default;
telsoa014fcda012018-03-09 14:13:49 +000033
34IRuntime* IRuntime::CreateRaw(const CreationOptions& options)
35{
Kevin Mayd92a6e42021-02-04 10:27:41 +000036 return new IRuntime(options);
telsoa014fcda012018-03-09 14:13:49 +000037}
38
39IRuntimePtr IRuntime::Create(const CreationOptions& options)
40{
41 return IRuntimePtr(CreateRaw(options), &IRuntime::Destroy);
42}
43
44void IRuntime::Destroy(IRuntime* runtime)
45{
Kevin Mayd92a6e42021-02-04 10:27:41 +000046 delete runtime;
telsoa014fcda012018-03-09 14:13:49 +000047}
48
Kevin Mayd92a6e42021-02-04 10:27:41 +000049Status IRuntime::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network)
50{
51 return pRuntimeImpl->LoadNetwork(networkIdOut, std::move(network));
52}
53
54Status IRuntime::LoadNetwork(NetworkId& networkIdOut,
55 IOptimizedNetworkPtr network,
56 std::string& errorMessage)
57{
58 return pRuntimeImpl->LoadNetwork(networkIdOut, std::move(network), errorMessage);
59}
60
61Status IRuntime::LoadNetwork(NetworkId& networkIdOut,
62 IOptimizedNetworkPtr network,
63 std::string& errorMessage,
64 const INetworkProperties& networkProperties)
65{
66 return pRuntimeImpl->LoadNetwork(networkIdOut, std::move(network), errorMessage, networkProperties);
67}
68
69TensorInfo IRuntime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
70{
71 return pRuntimeImpl->GetInputTensorInfo(networkId, layerId);
72}
73
74TensorInfo IRuntime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
75{
76 return pRuntimeImpl->GetOutputTensorInfo(networkId, layerId);
77}
78
79Status IRuntime::EnqueueWorkload(NetworkId networkId,
80 const InputTensors& inputTensors,
81 const OutputTensors& outputTensors)
82{
83 return pRuntimeImpl->EnqueueWorkload(networkId, inputTensors, outputTensors);
84}
85
Mike Kelly55a8ffd2021-04-07 20:10:49 +010086Status IRuntime::Execute(IWorkingMemHandle& workingMemHandle,
87 const InputTensors& inputTensors,
88 const OutputTensors& outputTensors)
89{
90 return pRuntimeImpl->Execute(workingMemHandle, inputTensors, outputTensors);
91}
92
Kevin Mayd92a6e42021-02-04 10:27:41 +000093Status IRuntime::UnloadNetwork(NetworkId networkId)
94{
95 return pRuntimeImpl->UnloadNetwork(networkId);
96}
97
98const IDeviceSpec& IRuntime::GetDeviceSpec() const
99{
100 return pRuntimeImpl->GetDeviceSpec();
101}
102
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100103std::unique_ptr<IWorkingMemHandle> IRuntime::CreateWorkingMemHandle(NetworkId networkId)
104{
105 return pRuntimeImpl->CreateWorkingMemHandle(networkId);
106}
107
Kevin Mayd92a6e42021-02-04 10:27:41 +0000108const std::shared_ptr<IProfiler> IRuntime::GetProfiler(NetworkId networkId) const
109{
110 return pRuntimeImpl->GetProfiler(networkId);
111}
112
113void IRuntime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
114{
115 return pRuntimeImpl->RegisterDebugCallback(networkId, func);
116}
117
118int RuntimeImpl::GenerateNetworkId()
telsoa014fcda012018-03-09 14:13:49 +0000119{
120 return m_NetworkIdCounter++;
121}
122
Kevin Mayd92a6e42021-02-04 10:27:41 +0000123Status RuntimeImpl::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr inNetwork)
telsoa014fcda012018-03-09 14:13:49 +0000124{
telsoa01c577f2c2018-08-31 09:22:23 +0100125 std::string ignoredErrorMessage;
126 return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage);
127}
128
Kevin Mayd92a6e42021-02-04 10:27:41 +0000129Status RuntimeImpl::LoadNetwork(NetworkId& networkIdOut,
130 IOptimizedNetworkPtr inNetwork,
131 std::string& errorMessage)
David Monahan4f1e8e42019-09-04 09:22:10 +0100132{
Jan Eilersc1c872f2021-07-22 13:17:04 +0100133 INetworkProperties networkProperties(
134 false, MemorySource::Undefined, MemorySource::Undefined);
David Monahan4f1e8e42019-09-04 09:22:10 +0100135 return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties);
136}
137
Kevin Mayd92a6e42021-02-04 10:27:41 +0000138Status RuntimeImpl::LoadNetwork(NetworkId& networkIdOut,
139 IOptimizedNetworkPtr inNetwork,
140 std::string& errorMessage,
141 const INetworkProperties& networkProperties)
telsoa01c577f2c2018-08-31 09:22:23 +0100142{
telsoa014fcda012018-03-09 14:13:49 +0000143 IOptimizedNetwork* rawNetwork = inNetwork.release();
David Beck1b61be52018-11-08 09:19:14 +0000144
145 networkIdOut = GenerateNetworkId();
146
147 for (auto&& context : m_BackendContexts)
148 {
149 context.second->BeforeLoadNetwork(networkIdOut);
150 }
151
telsoa014fcda012018-03-09 14:13:49 +0000152 unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
Francis Murtagh3d2b4b22021-02-15 18:23:17 +0000153 std::unique_ptr<IOptimizedNetwork>(rawNetwork),
David Monahan4f1e8e42019-09-04 09:22:10 +0100154 errorMessage,
Sadik Armagan3184c902020-03-18 10:57:30 +0000155 networkProperties,
Finn Williamsf364d532021-06-09 17:07:33 +0100156 m_ProfilingService);
telsoa014fcda012018-03-09 14:13:49 +0000157
158 if (!loadedNetwork)
159 {
160 return Status::Failure;
161 }
162
telsoa01c577f2c2018-08-31 09:22:23 +0100163 {
164 std::lock_guard<std::mutex> lockGuard(m_Mutex);
165
166 // Stores the network
167 m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork);
168 }
telsoa014fcda012018-03-09 14:13:49 +0000169
David Beck1b61be52018-11-08 09:19:14 +0000170 for (auto&& context : m_BackendContexts)
171 {
172 context.second->AfterLoadNetwork(networkIdOut);
173 }
174
Sadik Armagan3184c902020-03-18 10:57:30 +0000175 if (m_ProfilingService.IsProfilingEnabled())
Keith Davise394bd92019-12-02 15:12:19 +0000176 {
Sadik Armagan3184c902020-03-18 10:57:30 +0000177 m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_LOADS);
Keith Davise394bd92019-12-02 15:12:19 +0000178 }
179
telsoa014fcda012018-03-09 14:13:49 +0000180 return Status::Success;
telsoa014fcda012018-03-09 14:13:49 +0000181}
182
Kevin Mayd92a6e42021-02-04 10:27:41 +0000183Status RuntimeImpl::UnloadNetwork(NetworkId networkId)
telsoa014fcda012018-03-09 14:13:49 +0000184{
David Beck1b61be52018-11-08 09:19:14 +0000185 bool unloadOk = true;
186 for (auto&& context : m_BackendContexts)
David Beck9efb57d2018-11-05 13:40:33 +0000187 {
David Beck1b61be52018-11-08 09:19:14 +0000188 unloadOk &= context.second->BeforeUnloadNetwork(networkId);
David Beck9efb57d2018-11-05 13:40:33 +0000189 }
David Beck1b61be52018-11-08 09:19:14 +0000190
191 if (!unloadOk)
192 {
Kevin Mayd92a6e42021-02-04 10:27:41 +0000193 ARMNN_LOG(warning) << "RuntimeImpl::UnloadNetwork(): failed to unload "
Derek Lamberti08446972019-11-26 16:38:31 +0000194 "network with ID:" << networkId << " because BeforeUnloadNetwork failed";
David Beck1b61be52018-11-08 09:19:14 +0000195 return Status::Failure;
196 }
David Beck9efb57d2018-11-05 13:40:33 +0000197
Jim Flynnf7713212020-07-14 09:50:59 +0100198 std::unique_ptr<profiling::TimelineUtilityMethods> timelineUtils =
199 profiling::TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService);
telsoa014fcda012018-03-09 14:13:49 +0000200 {
telsoa01c577f2c2018-08-31 09:22:23 +0100201 std::lock_guard<std::mutex> lockGuard(m_Mutex);
202
Jim Flynnf7713212020-07-14 09:50:59 +0100203 // If timeline recording is on mark the Network end of life
204 if (timelineUtils)
205 {
206 auto search = m_LoadedNetworks.find(networkId);
207 if (search != m_LoadedNetworks.end())
208 {
209 profiling::ProfilingGuid networkGuid = search->second->GetNetworkGuid();
210 timelineUtils->RecordEvent(networkGuid,
211 profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS);
212 }
213 }
telsoa01c577f2c2018-08-31 09:22:23 +0100214 if (m_LoadedNetworks.erase(networkId) == 0)
215 {
Kevin Mayd92a6e42021-02-04 10:27:41 +0000216 ARMNN_LOG(warning) << "WARNING: RuntimeImpl::UnloadNetwork(): " << networkId << " not found!";
telsoa01c577f2c2018-08-31 09:22:23 +0100217 return Status::Failure;
218 }
Sadik Armagan3184c902020-03-18 10:57:30 +0000219
220 if (m_ProfilingService.IsProfilingEnabled())
Keith Davise394bd92019-12-02 15:12:19 +0000221 {
Sadik Armagan3184c902020-03-18 10:57:30 +0000222 m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS);
Keith Davise394bd92019-12-02 15:12:19 +0000223 }
David Beck1b61be52018-11-08 09:19:14 +0000224 }
David Beck9efb57d2018-11-05 13:40:33 +0000225
David Beck1b61be52018-11-08 09:19:14 +0000226 for (auto&& context : m_BackendContexts)
227 {
228 context.second->AfterUnloadNetwork(networkId);
telsoa01c577f2c2018-08-31 09:22:23 +0100229 }
230
Kevin Mayd92a6e42021-02-04 10:27:41 +0000231 ARMNN_LOG(debug) << "RuntimeImpl::UnloadNetwork(): Unloaded network with ID: " << networkId;
telsoa014fcda012018-03-09 14:13:49 +0000232 return Status::Success;
233}
234
Kevin Mayd92a6e42021-02-04 10:27:41 +0000235const std::shared_ptr<IProfiler> RuntimeImpl::GetProfiler(NetworkId networkId) const
telsoa01c577f2c2018-08-31 09:22:23 +0100236{
237 auto it = m_LoadedNetworks.find(networkId);
238 if (it != m_LoadedNetworks.end())
239 {
240 auto& loadedNetwork = it->second;
241 return loadedNetwork->GetProfiler();
242 }
243
244 return nullptr;
245}
246
Kevin Mayd92a6e42021-02-04 10:27:41 +0000247void RuntimeImpl::ReportStructure() // armnn::profiling::IProfilingService& profilingService as param
Keith Davis33ed2212020-03-30 10:43:41 +0100248{
249 // No-op for the time being, but this may be useful in future to have the profilingService available
250 // if (profilingService.IsProfilingEnabled()){}
251
252 LoadedNetworks::iterator it = m_LoadedNetworks.begin();
253 while (it != m_LoadedNetworks.end())
254 {
255 auto& loadedNetwork = it->second;
256 loadedNetwork->SendNetworkStructure();
257 // Increment the Iterator to point to next entry
258 it++;
259 }
260}
261
Kevin Mayd92a6e42021-02-04 10:27:41 +0000262RuntimeImpl::RuntimeImpl(const IRuntime::CreationOptions& options)
Keith Davis33ed2212020-03-30 10:43:41 +0100263 : m_NetworkIdCounter(0),
264 m_ProfilingService(*this)
telsoa014fcda012018-03-09 14:13:49 +0000265{
alered01a7227ac2020-05-07 14:58:29 +0100266 const auto start_time = armnn::GetTimeNow();
Derek Lamberti08446972019-11-26 16:38:31 +0000267 ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n";
Keith Davis33ed2212020-03-30 10:43:41 +0100268 if ( options.m_ProfilingOptions.m_TimelineEnabled && !options.m_ProfilingOptions.m_EnableProfiling )
269 {
Jan Eilersc1c872f2021-07-22 13:17:04 +0100270 throw RuntimeException(
271 "It is not possible to enable timeline reporting without profiling being enabled");
Keith Davis33ed2212020-03-30 10:43:41 +0100272 }
273
Matteo Martincighe54aa062019-08-05 14:12:11 +0100274 // Load any available/compatible dynamic backend before the runtime
275 // goes through the backend registry
276 LoadDynamicBackends(options.m_DynamicBackendsPath);
277
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000278 BackendIdSet supportedBackends;
David Beck1b61be52018-11-08 09:19:14 +0000279 for (const auto& id : BackendRegistryInstance().GetBackendIds())
280 {
281 // Store backend contexts for the supported ones
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000282 try {
David Beck1b61be52018-11-08 09:19:14 +0000283 auto factoryFun = BackendRegistryInstance().GetFactory(id);
Colm Donelan380c1a02021-08-17 00:52:23 +0100284 ARMNN_ASSERT(factoryFun != nullptr);
David Beck1b61be52018-11-08 09:19:14 +0000285 auto backend = factoryFun();
Colm Donelan380c1a02021-08-17 00:52:23 +0100286 ARMNN_ASSERT(backend != nullptr);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100287 ARMNN_ASSERT(backend.get() != nullptr);
David Beck1b61be52018-11-08 09:19:14 +0000288
Jan Eilersc1c872f2021-07-22 13:17:04 +0100289 auto customAllocatorMapIterator = options.m_CustomAllocatorMap.find(id);
Francis Murtagh62573b62021-08-12 11:55:21 +0100290 if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end() &&
291 customAllocatorMapIterator->second == nullptr)
292 {
Colm Donelan380c1a02021-08-17 00:52:23 +0100293 // We need to manually clean up the dynamic backends before throwing an exception.
294 DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends());
295 m_DeviceSpec.ClearDynamicBackends();
Francis Murtagh62573b62021-08-12 11:55:21 +0100296 throw armnn::Exception("Allocator associated with id " + id.Get() + " is null");
297 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100298
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100299 // If the runtime is created in protected mode only add backends that support this mode
300 if (options.m_ProtectedMode)
301 {
302 // check if backend supports ProtectedMode
303 using BackendCapability = BackendOptions::BackendOption;
304 BackendCapability protectedContentCapability {"ProtectedContentAllocation", true};
305 if (!HasCapability(protectedContentCapability, id))
306 {
307 // Protected Content Allocation is not supported by the backend
308 // backend should not be registered
309 ARMNN_LOG(warning) << "Backend "
310 << id
311 << " is not registered as does not support protected content allocation \n";
312 continue;
313 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100314 // The user is responsible to provide a custom memory allocator which allows to allocate
315 // protected memory
316 if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end())
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100317 {
Jan Eilersc1c872f2021-07-22 13:17:04 +0100318 std::string err;
319 if (customAllocatorMapIterator->second->GetMemorySourceType()
320 == armnn::MemorySource::DmaBufProtected)
321 {
322 if (!backend->UseCustomMemoryAllocator(customAllocatorMapIterator->second, err))
323 {
324 ARMNN_LOG(error) << "The backend "
325 << id
326 << " reported an error when entering protected mode. Backend won't be"
327 << " used. ErrorMsg: " << err;
328 continue;
329 }
330 // No errors so register the Custom Allocator with the BackendRegistry
331 BackendRegistryInstance().RegisterAllocator(id, customAllocatorMapIterator->second);
332 }
333 else
334 {
335 ARMNN_LOG(error) << "The CustomAllocator provided with the runtime options doesn't support "
336 "protected memory. Protected mode can't be activated. The backend "
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100337 << id
Jan Eilersc1c872f2021-07-22 13:17:04 +0100338 << " is not going to be used. MemorySource must be MemorySource::DmaBufProtected";
339 continue;
340 }
341 }
342 else
343 {
344 ARMNN_LOG(error) << "Protected mode can't be activated for backend: "
345 << id
346 << " no custom allocator was provided to the runtime options.";
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100347 continue;
348 }
349 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100350 else
351 {
352 // If a custom memory allocator is provided make the backend use that instead of the default
353 if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end())
354 {
355 std::string err;
356 if (!backend->UseCustomMemoryAllocator(customAllocatorMapIterator->second, err))
357 {
358 ARMNN_LOG(error) << "The backend "
359 << id
360 << " reported an error when trying to use the provided custom allocator."
361 " Backend won't be used."
362 << " ErrorMsg: " << err;
363 continue;
364 }
365 // No errors so register the Custom Allocator with the BackendRegistry
366 BackendRegistryInstance().RegisterAllocator(id, customAllocatorMapIterator->second);
367 }
368 }
David Beck1b61be52018-11-08 09:19:14 +0000369 auto context = backend->CreateBackendContext(options);
370
371 // backends are allowed to return nullptrs if they
372 // don't wish to create a backend specific context
373 if (context)
374 {
375 m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
376 }
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000377 supportedBackends.emplace(id);
Colm Donelan1aff3932020-02-05 17:48:59 +0000378
379 unique_ptr<armnn::profiling::IBackendProfiling> profilingIface =
380 std::make_unique<armnn::profiling::BackendProfiling>(armnn::profiling::BackendProfiling(
Sadik Armagan3184c902020-03-18 10:57:30 +0000381 options, m_ProfilingService, id));
Colm Donelan1aff3932020-02-05 17:48:59 +0000382
383 // Backends may also provide a profiling context. Ask for it now.
384 auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface);
385 // Backends that don't support profiling will return a null profiling context.
386 if (profilingContext)
387 {
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100388 // Pass the context onto the profiling service.
389 m_ProfilingService.AddBackendProfilingContext(id, profilingContext);
Colm Donelan1aff3932020-02-05 17:48:59 +0000390 }
David Beck1b61be52018-11-08 09:19:14 +0000391 }
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000392 catch (const BackendUnavailableException&)
393 {
394 // Ignore backends which are unavailable
395 }
David Beck1b61be52018-11-08 09:19:14 +0000396 }
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100397
Finn Williams45a73622020-05-15 18:41:05 +0100398 BackendRegistryInstance().SetProfilingService(m_ProfilingService);
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100399 // pass configuration info to the profiling service
400 m_ProfilingService.ConfigureProfilingService(options.m_ProfilingOptions);
Jim Flynn6398a982020-05-27 17:05:21 +0100401 if (options.m_ProfilingOptions.m_EnableProfiling)
402 {
403 // try to wait for the profiling service to initialise
404 m_ProfilingService.WaitForProfilingServiceActivation(3000);
405 }
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100406
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000407 m_DeviceSpec.AddSupportedBackends(supportedBackends);
alered01a7227ac2020-05-07 14:58:29 +0100408
409 ARMNN_LOG(info) << "Initialization time: " << std::setprecision(2)
410 << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
surmeh01bceff2f2018-03-29 16:29:27 +0100411}
412
Kevin Mayd92a6e42021-02-04 10:27:41 +0000413RuntimeImpl::~RuntimeImpl()
surmeh01bceff2f2018-03-29 16:29:27 +0100414{
alered01a7227ac2020-05-07 14:58:29 +0100415 const auto start_time = armnn::GetTimeNow();
surmeh01bceff2f2018-03-29 16:29:27 +0100416 std::vector<int> networkIDs;
surmeh013537c2c2018-05-18 16:31:43 +0100417 try
418 {
419 // Coverity fix: The following code may throw an exception of type std::length_error.
420 std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
421 std::back_inserter(networkIDs),
422 [](const auto &pair) { return pair.first; });
423 }
424 catch (const std::exception& e)
425 {
426 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
427 // exception of type std::length_error.
428 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
429 std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
430 << "\nSome of the loaded networks may not be unloaded" << std::endl;
431 }
432 // We then proceed to unload all the networks which IDs have been appended to the list
433 // up to the point the exception was thrown (if any).
surmeh01bceff2f2018-03-29 16:29:27 +0100434
435 for (auto networkID : networkIDs)
436 {
surmeh013537c2c2018-05-18 16:31:43 +0100437 try
438 {
439 // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
440 // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
441 UnloadNetwork(networkID);
442 }
443 catch (const std::exception& e)
444 {
445 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
446 // exception of type std::length_error.
447 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
448 std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
449 << std::endl;
450 }
telsoa014fcda012018-03-09 14:13:49 +0000451 }
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000452
453 // Clear all dynamic backends.
454 DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends());
455 m_DeviceSpec.ClearDynamicBackends();
Colm Donelan1aff3932020-02-05 17:48:59 +0000456 m_BackendContexts.clear();
Finn Williams45a73622020-05-15 18:41:05 +0100457
458 BackendRegistryInstance().SetProfilingService(armnn::EmptyOptional());
alered01a7227ac2020-05-07 14:58:29 +0100459 ARMNN_LOG(info) << "Shutdown time: " << std::setprecision(2)
460 << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
telsoa014fcda012018-03-09 14:13:49 +0000461}
462
Kevin Mayd92a6e42021-02-04 10:27:41 +0000463LoadedNetwork* RuntimeImpl::GetLoadedNetworkPtr(NetworkId networkId) const
surmeh013537c2c2018-05-18 16:31:43 +0100464{
465 std::lock_guard<std::mutex> lockGuard(m_Mutex);
466 return m_LoadedNetworks.at(networkId).get();
467}
468
Kevin Mayd92a6e42021-02-04 10:27:41 +0000469TensorInfo RuntimeImpl::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
telsoa014fcda012018-03-09 14:13:49 +0000470{
surmeh013537c2c2018-05-18 16:31:43 +0100471 return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000472}
473
Kevin Mayd92a6e42021-02-04 10:27:41 +0000474TensorInfo RuntimeImpl::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
telsoa014fcda012018-03-09 14:13:49 +0000475{
surmeh013537c2c2018-05-18 16:31:43 +0100476 return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000477}
478
Derek Lamberti03614f62018-10-02 15:52:46 +0100479
Kevin Mayd92a6e42021-02-04 10:27:41 +0000480Status RuntimeImpl::EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100481 const InputTensors& inputTensors,
482 const OutputTensors& outputTensors)
telsoa014fcda012018-03-09 14:13:49 +0000483{
surmeh013537c2c2018-05-18 16:31:43 +0100484 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100485
486 if (!loadedNetwork)
487 {
488 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
489 return Status::Failure;
490 }
491 if (loadedNetwork->IsAsyncEnabled())
492 {
493 ARMNN_LOG(error) << "Network " << networkId << " is async enabled.\n";
494 return Status::Failure;
495 }
Narumol Prangnawarat5b4d0d52020-06-23 11:45:56 +0100496 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
497
498 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "EnqueueWorkload");
Derek Lamberti03614f62018-10-02 15:52:46 +0100499
500 static thread_local NetworkId lastId = networkId;
501 if (lastId != networkId)
502 {
503 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
504 {
505 network->FreeWorkingMemory();
506 });
507 }
508 lastId=networkId;
509
surmeh013537c2c2018-05-18 16:31:43 +0100510 return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000511}
512
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100513Status RuntimeImpl::Execute(IWorkingMemHandle& iWorkingMemHandle,
514 const InputTensors& inputTensors,
515 const OutputTensors& outputTensors)
516{
517 NetworkId networkId = iWorkingMemHandle.GetNetworkId();
518 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
519
520 if (!loadedNetwork)
521 {
522 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
523 return Status::Failure;
524 }
525 if (!loadedNetwork->IsAsyncEnabled())
526 {
Keith Davise813d672021-04-22 10:10:34 +0100527 ARMNN_LOG(error) << "Attempting execute " << networkId << " when it is not async enabled.\n";
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100528 return Status::Failure;
529 }
530 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
531
532 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Execute");
533
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100534 return loadedNetwork->Execute(inputTensors, outputTensors, iWorkingMemHandle);
535}
536
537/// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
538/// overlapped Execution by calling this function from different threads.
539std::unique_ptr<IWorkingMemHandle> RuntimeImpl::CreateWorkingMemHandle(NetworkId networkId)
540{
541 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
542
543 if (!loadedNetwork)
544 {
545 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
546 return nullptr;
547 }
548 if (!loadedNetwork->IsAsyncEnabled())
549 {
550 ARMNN_LOG(error) << "Network " << networkId << " is not async enabled.\n";
551 return nullptr;
552 }
553 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
554
555 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "CreateWorkingMemHandle");
556
557 static thread_local NetworkId lastId = networkId;
558 if (lastId != networkId)
559 {
560 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
561 {
562 network->FreeWorkingMemory();
563 });
564 }
565 lastId=networkId;
566
567 return loadedNetwork->CreateWorkingMemHandle(networkId);
568}
569
Kevin Mayd92a6e42021-02-04 10:27:41 +0000570void RuntimeImpl::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000571{
572 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
573 loadedNetwork->RegisterDebugCallback(func);
574}
575
Kevin Mayd92a6e42021-02-04 10:27:41 +0000576void RuntimeImpl::LoadDynamicBackends(const std::string& overrideBackendPath)
Matteo Martincighe54aa062019-08-05 14:12:11 +0100577{
578 // Get the paths where to load the dynamic backends from
579 std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath);
580
581 // Get the shared objects to try to load as dynamic backends
582 std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths);
583
584 // Create a list of dynamic backends
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100585 m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects);
586
587 // Register the dynamic backends in the backend registry
Matteo Martincigh89533902019-08-15 12:08:06 +0100588 BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends);
589
590 // Add the registered dynamic backend ids to the list of supported backends
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000591 m_DeviceSpec.AddSupportedBackends(registeredBackendIds, true);
telsoa014fcda012018-03-09 14:13:49 +0000592}
Matteo Martincighe54aa062019-08-05 14:12:11 +0100593
594} // namespace armnn