blob: 9fe58287c38be587330142c2acf69790b6f01c05 [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";
David Beck1b61be52018-11-08 09:19:14 +0000268
Keith Davis33ed2212020-03-30 10:43:41 +0100269 if ( options.m_ProfilingOptions.m_TimelineEnabled && !options.m_ProfilingOptions.m_EnableProfiling )
270 {
Jan Eilersc1c872f2021-07-22 13:17:04 +0100271 throw RuntimeException(
272 "It is not possible to enable timeline reporting without profiling being enabled");
Keith Davis33ed2212020-03-30 10:43:41 +0100273 }
274
Matteo Martincighe54aa062019-08-05 14:12:11 +0100275 // Load any available/compatible dynamic backend before the runtime
276 // goes through the backend registry
277 LoadDynamicBackends(options.m_DynamicBackendsPath);
278
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000279 BackendIdSet supportedBackends;
David Beck1b61be52018-11-08 09:19:14 +0000280 for (const auto& id : BackendRegistryInstance().GetBackendIds())
281 {
282 // Store backend contexts for the supported ones
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000283 try {
David Beck1b61be52018-11-08 09:19:14 +0000284 auto factoryFun = BackendRegistryInstance().GetFactory(id);
285 auto backend = factoryFun();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100286 ARMNN_ASSERT(backend.get() != nullptr);
David Beck1b61be52018-11-08 09:19:14 +0000287
Jan Eilersc1c872f2021-07-22 13:17:04 +0100288 auto customAllocatorMapIterator = options.m_CustomAllocatorMap.find(id);
289
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100290 // If the runtime is created in protected mode only add backends that support this mode
291 if (options.m_ProtectedMode)
292 {
293 // check if backend supports ProtectedMode
294 using BackendCapability = BackendOptions::BackendOption;
295 BackendCapability protectedContentCapability {"ProtectedContentAllocation", true};
296 if (!HasCapability(protectedContentCapability, id))
297 {
298 // Protected Content Allocation is not supported by the backend
299 // backend should not be registered
300 ARMNN_LOG(warning) << "Backend "
301 << id
302 << " is not registered as does not support protected content allocation \n";
303 continue;
304 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100305 // The user is responsible to provide a custom memory allocator which allows to allocate
306 // protected memory
307 if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end())
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100308 {
Jan Eilersc1c872f2021-07-22 13:17:04 +0100309 std::string err;
310 if (customAllocatorMapIterator->second->GetMemorySourceType()
311 == armnn::MemorySource::DmaBufProtected)
312 {
313 if (!backend->UseCustomMemoryAllocator(customAllocatorMapIterator->second, err))
314 {
315 ARMNN_LOG(error) << "The backend "
316 << id
317 << " reported an error when entering protected mode. Backend won't be"
318 << " used. ErrorMsg: " << err;
319 continue;
320 }
321 // No errors so register the Custom Allocator with the BackendRegistry
322 BackendRegistryInstance().RegisterAllocator(id, customAllocatorMapIterator->second);
323 }
324 else
325 {
326 ARMNN_LOG(error) << "The CustomAllocator provided with the runtime options doesn't support "
327 "protected memory. Protected mode can't be activated. The backend "
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100328 << id
Jan Eilersc1c872f2021-07-22 13:17:04 +0100329 << " is not going to be used. MemorySource must be MemorySource::DmaBufProtected";
330 continue;
331 }
332 }
333 else
334 {
335 ARMNN_LOG(error) << "Protected mode can't be activated for backend: "
336 << id
337 << " no custom allocator was provided to the runtime options.";
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100338 continue;
339 }
340 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100341 else
342 {
343 // If a custom memory allocator is provided make the backend use that instead of the default
344 if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end())
345 {
346 std::string err;
347 if (!backend->UseCustomMemoryAllocator(customAllocatorMapIterator->second, err))
348 {
349 ARMNN_LOG(error) << "The backend "
350 << id
351 << " reported an error when trying to use the provided custom allocator."
352 " Backend won't be used."
353 << " ErrorMsg: " << err;
354 continue;
355 }
356 // No errors so register the Custom Allocator with the BackendRegistry
357 BackendRegistryInstance().RegisterAllocator(id, customAllocatorMapIterator->second);
358 }
359 }
David Beck1b61be52018-11-08 09:19:14 +0000360 auto context = backend->CreateBackendContext(options);
361
362 // backends are allowed to return nullptrs if they
363 // don't wish to create a backend specific context
364 if (context)
365 {
366 m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
367 }
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000368 supportedBackends.emplace(id);
Colm Donelan1aff3932020-02-05 17:48:59 +0000369
370 unique_ptr<armnn::profiling::IBackendProfiling> profilingIface =
371 std::make_unique<armnn::profiling::BackendProfiling>(armnn::profiling::BackendProfiling(
Sadik Armagan3184c902020-03-18 10:57:30 +0000372 options, m_ProfilingService, id));
Colm Donelan1aff3932020-02-05 17:48:59 +0000373
374 // Backends may also provide a profiling context. Ask for it now.
375 auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface);
376 // Backends that don't support profiling will return a null profiling context.
377 if (profilingContext)
378 {
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100379 // Pass the context onto the profiling service.
380 m_ProfilingService.AddBackendProfilingContext(id, profilingContext);
Colm Donelan1aff3932020-02-05 17:48:59 +0000381 }
David Beck1b61be52018-11-08 09:19:14 +0000382 }
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000383 catch (const BackendUnavailableException&)
384 {
385 // Ignore backends which are unavailable
386 }
David Beck1b61be52018-11-08 09:19:14 +0000387 }
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100388
Finn Williams45a73622020-05-15 18:41:05 +0100389 BackendRegistryInstance().SetProfilingService(m_ProfilingService);
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100390 // pass configuration info to the profiling service
391 m_ProfilingService.ConfigureProfilingService(options.m_ProfilingOptions);
Jim Flynn6398a982020-05-27 17:05:21 +0100392 if (options.m_ProfilingOptions.m_EnableProfiling)
393 {
394 // try to wait for the profiling service to initialise
395 m_ProfilingService.WaitForProfilingServiceActivation(3000);
396 }
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100397
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000398 m_DeviceSpec.AddSupportedBackends(supportedBackends);
alered01a7227ac2020-05-07 14:58:29 +0100399
400 ARMNN_LOG(info) << "Initialization time: " << std::setprecision(2)
401 << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
surmeh01bceff2f2018-03-29 16:29:27 +0100402}
403
Kevin Mayd92a6e42021-02-04 10:27:41 +0000404RuntimeImpl::~RuntimeImpl()
surmeh01bceff2f2018-03-29 16:29:27 +0100405{
alered01a7227ac2020-05-07 14:58:29 +0100406 const auto start_time = armnn::GetTimeNow();
surmeh01bceff2f2018-03-29 16:29:27 +0100407 std::vector<int> networkIDs;
surmeh013537c2c2018-05-18 16:31:43 +0100408 try
409 {
410 // Coverity fix: The following code may throw an exception of type std::length_error.
411 std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
412 std::back_inserter(networkIDs),
413 [](const auto &pair) { return pair.first; });
414 }
415 catch (const std::exception& e)
416 {
417 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
418 // exception of type std::length_error.
419 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
420 std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
421 << "\nSome of the loaded networks may not be unloaded" << std::endl;
422 }
423 // We then proceed to unload all the networks which IDs have been appended to the list
424 // up to the point the exception was thrown (if any).
surmeh01bceff2f2018-03-29 16:29:27 +0100425
426 for (auto networkID : networkIDs)
427 {
surmeh013537c2c2018-05-18 16:31:43 +0100428 try
429 {
430 // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
431 // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
432 UnloadNetwork(networkID);
433 }
434 catch (const std::exception& e)
435 {
436 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
437 // exception of type std::length_error.
438 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
439 std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
440 << std::endl;
441 }
telsoa014fcda012018-03-09 14:13:49 +0000442 }
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000443
444 // Clear all dynamic backends.
445 DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends());
446 m_DeviceSpec.ClearDynamicBackends();
Colm Donelan1aff3932020-02-05 17:48:59 +0000447 m_BackendContexts.clear();
Finn Williams45a73622020-05-15 18:41:05 +0100448
449 BackendRegistryInstance().SetProfilingService(armnn::EmptyOptional());
alered01a7227ac2020-05-07 14:58:29 +0100450 ARMNN_LOG(info) << "Shutdown time: " << std::setprecision(2)
451 << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
telsoa014fcda012018-03-09 14:13:49 +0000452}
453
Kevin Mayd92a6e42021-02-04 10:27:41 +0000454LoadedNetwork* RuntimeImpl::GetLoadedNetworkPtr(NetworkId networkId) const
surmeh013537c2c2018-05-18 16:31:43 +0100455{
456 std::lock_guard<std::mutex> lockGuard(m_Mutex);
457 return m_LoadedNetworks.at(networkId).get();
458}
459
Kevin Mayd92a6e42021-02-04 10:27:41 +0000460TensorInfo RuntimeImpl::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
telsoa014fcda012018-03-09 14:13:49 +0000461{
surmeh013537c2c2018-05-18 16:31:43 +0100462 return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000463}
464
Kevin Mayd92a6e42021-02-04 10:27:41 +0000465TensorInfo RuntimeImpl::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
telsoa014fcda012018-03-09 14:13:49 +0000466{
surmeh013537c2c2018-05-18 16:31:43 +0100467 return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000468}
469
Derek Lamberti03614f62018-10-02 15:52:46 +0100470
Kevin Mayd92a6e42021-02-04 10:27:41 +0000471Status RuntimeImpl::EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100472 const InputTensors& inputTensors,
473 const OutputTensors& outputTensors)
telsoa014fcda012018-03-09 14:13:49 +0000474{
surmeh013537c2c2018-05-18 16:31:43 +0100475 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100476
477 if (!loadedNetwork)
478 {
479 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
480 return Status::Failure;
481 }
482 if (loadedNetwork->IsAsyncEnabled())
483 {
484 ARMNN_LOG(error) << "Network " << networkId << " is async enabled.\n";
485 return Status::Failure;
486 }
Narumol Prangnawarat5b4d0d52020-06-23 11:45:56 +0100487 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
488
489 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "EnqueueWorkload");
Derek Lamberti03614f62018-10-02 15:52:46 +0100490
491 static thread_local NetworkId lastId = networkId;
492 if (lastId != networkId)
493 {
494 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
495 {
496 network->FreeWorkingMemory();
497 });
498 }
499 lastId=networkId;
500
surmeh013537c2c2018-05-18 16:31:43 +0100501 return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000502}
503
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100504Status RuntimeImpl::Execute(IWorkingMemHandle& iWorkingMemHandle,
505 const InputTensors& inputTensors,
506 const OutputTensors& outputTensors)
507{
508 NetworkId networkId = iWorkingMemHandle.GetNetworkId();
509 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
510
511 if (!loadedNetwork)
512 {
513 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
514 return Status::Failure;
515 }
516 if (!loadedNetwork->IsAsyncEnabled())
517 {
Keith Davise813d672021-04-22 10:10:34 +0100518 ARMNN_LOG(error) << "Attempting execute " << networkId << " when it is not async enabled.\n";
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100519 return Status::Failure;
520 }
521 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
522
523 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Execute");
524
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100525 return loadedNetwork->Execute(inputTensors, outputTensors, iWorkingMemHandle);
526}
527
528/// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
529/// overlapped Execution by calling this function from different threads.
530std::unique_ptr<IWorkingMemHandle> RuntimeImpl::CreateWorkingMemHandle(NetworkId networkId)
531{
532 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
533
534 if (!loadedNetwork)
535 {
536 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
537 return nullptr;
538 }
539 if (!loadedNetwork->IsAsyncEnabled())
540 {
541 ARMNN_LOG(error) << "Network " << networkId << " is not async enabled.\n";
542 return nullptr;
543 }
544 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
545
546 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "CreateWorkingMemHandle");
547
548 static thread_local NetworkId lastId = networkId;
549 if (lastId != networkId)
550 {
551 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
552 {
553 network->FreeWorkingMemory();
554 });
555 }
556 lastId=networkId;
557
558 return loadedNetwork->CreateWorkingMemHandle(networkId);
559}
560
Kevin Mayd92a6e42021-02-04 10:27:41 +0000561void RuntimeImpl::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000562{
563 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
564 loadedNetwork->RegisterDebugCallback(func);
565}
566
Kevin Mayd92a6e42021-02-04 10:27:41 +0000567void RuntimeImpl::LoadDynamicBackends(const std::string& overrideBackendPath)
Matteo Martincighe54aa062019-08-05 14:12:11 +0100568{
569 // Get the paths where to load the dynamic backends from
570 std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath);
571
572 // Get the shared objects to try to load as dynamic backends
573 std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths);
574
575 // Create a list of dynamic backends
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100576 m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects);
577
578 // Register the dynamic backends in the backend registry
Matteo Martincigh89533902019-08-15 12:08:06 +0100579 BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends);
580
581 // Add the registered dynamic backend ids to the list of supported backends
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000582 m_DeviceSpec.AddSupportedBackends(registeredBackendIds, true);
telsoa014fcda012018-03-09 14:13:49 +0000583}
Matteo Martincighe54aa062019-08-05 14:12:11 +0100584
585} // namespace armnn