blob: 824a2b077c78bae5ca8a854dd42d547a9c536491 [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);
Francis Murtagh62573b62021-08-12 11:55:21 +0100289 if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end() &&
290 customAllocatorMapIterator->second == nullptr)
291 {
292 throw armnn::Exception("Allocator associated with id " + id.Get() + " is null");
293 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100294
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100295 // If the runtime is created in protected mode only add backends that support this mode
296 if (options.m_ProtectedMode)
297 {
298 // check if backend supports ProtectedMode
299 using BackendCapability = BackendOptions::BackendOption;
300 BackendCapability protectedContentCapability {"ProtectedContentAllocation", true};
301 if (!HasCapability(protectedContentCapability, id))
302 {
303 // Protected Content Allocation is not supported by the backend
304 // backend should not be registered
305 ARMNN_LOG(warning) << "Backend "
306 << id
307 << " is not registered as does not support protected content allocation \n";
308 continue;
309 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100310 // The user is responsible to provide a custom memory allocator which allows to allocate
311 // protected memory
312 if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end())
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100313 {
Jan Eilersc1c872f2021-07-22 13:17:04 +0100314 std::string err;
315 if (customAllocatorMapIterator->second->GetMemorySourceType()
316 == armnn::MemorySource::DmaBufProtected)
317 {
318 if (!backend->UseCustomMemoryAllocator(customAllocatorMapIterator->second, err))
319 {
320 ARMNN_LOG(error) << "The backend "
321 << id
322 << " reported an error when entering protected mode. Backend won't be"
323 << " used. ErrorMsg: " << err;
324 continue;
325 }
326 // No errors so register the Custom Allocator with the BackendRegistry
327 BackendRegistryInstance().RegisterAllocator(id, customAllocatorMapIterator->second);
328 }
329 else
330 {
331 ARMNN_LOG(error) << "The CustomAllocator provided with the runtime options doesn't support "
332 "protected memory. Protected mode can't be activated. The backend "
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100333 << id
Jan Eilersc1c872f2021-07-22 13:17:04 +0100334 << " is not going to be used. MemorySource must be MemorySource::DmaBufProtected";
335 continue;
336 }
337 }
338 else
339 {
340 ARMNN_LOG(error) << "Protected mode can't be activated for backend: "
341 << id
342 << " no custom allocator was provided to the runtime options.";
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100343 continue;
344 }
345 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100346 else
347 {
348 // If a custom memory allocator is provided make the backend use that instead of the default
349 if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end())
350 {
351 std::string err;
352 if (!backend->UseCustomMemoryAllocator(customAllocatorMapIterator->second, err))
353 {
354 ARMNN_LOG(error) << "The backend "
355 << id
356 << " reported an error when trying to use the provided custom allocator."
357 " Backend won't be used."
358 << " ErrorMsg: " << err;
359 continue;
360 }
361 // No errors so register the Custom Allocator with the BackendRegistry
362 BackendRegistryInstance().RegisterAllocator(id, customAllocatorMapIterator->second);
363 }
364 }
David Beck1b61be52018-11-08 09:19:14 +0000365 auto context = backend->CreateBackendContext(options);
366
367 // backends are allowed to return nullptrs if they
368 // don't wish to create a backend specific context
369 if (context)
370 {
371 m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
372 }
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000373 supportedBackends.emplace(id);
Colm Donelan1aff3932020-02-05 17:48:59 +0000374
375 unique_ptr<armnn::profiling::IBackendProfiling> profilingIface =
376 std::make_unique<armnn::profiling::BackendProfiling>(armnn::profiling::BackendProfiling(
Sadik Armagan3184c902020-03-18 10:57:30 +0000377 options, m_ProfilingService, id));
Colm Donelan1aff3932020-02-05 17:48:59 +0000378
379 // Backends may also provide a profiling context. Ask for it now.
380 auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface);
381 // Backends that don't support profiling will return a null profiling context.
382 if (profilingContext)
383 {
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100384 // Pass the context onto the profiling service.
385 m_ProfilingService.AddBackendProfilingContext(id, profilingContext);
Colm Donelan1aff3932020-02-05 17:48:59 +0000386 }
David Beck1b61be52018-11-08 09:19:14 +0000387 }
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000388 catch (const BackendUnavailableException&)
389 {
390 // Ignore backends which are unavailable
391 }
David Beck1b61be52018-11-08 09:19:14 +0000392 }
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100393
Finn Williams45a73622020-05-15 18:41:05 +0100394 BackendRegistryInstance().SetProfilingService(m_ProfilingService);
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100395 // pass configuration info to the profiling service
396 m_ProfilingService.ConfigureProfilingService(options.m_ProfilingOptions);
Jim Flynn6398a982020-05-27 17:05:21 +0100397 if (options.m_ProfilingOptions.m_EnableProfiling)
398 {
399 // try to wait for the profiling service to initialise
400 m_ProfilingService.WaitForProfilingServiceActivation(3000);
401 }
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100402
Matthew Bentham9a61fa62020-02-04 10:03:55 +0000403 m_DeviceSpec.AddSupportedBackends(supportedBackends);
alered01a7227ac2020-05-07 14:58:29 +0100404
405 ARMNN_LOG(info) << "Initialization time: " << std::setprecision(2)
406 << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
surmeh01bceff2f2018-03-29 16:29:27 +0100407}
408
Kevin Mayd92a6e42021-02-04 10:27:41 +0000409RuntimeImpl::~RuntimeImpl()
surmeh01bceff2f2018-03-29 16:29:27 +0100410{
alered01a7227ac2020-05-07 14:58:29 +0100411 const auto start_time = armnn::GetTimeNow();
surmeh01bceff2f2018-03-29 16:29:27 +0100412 std::vector<int> networkIDs;
surmeh013537c2c2018-05-18 16:31:43 +0100413 try
414 {
415 // Coverity fix: The following code may throw an exception of type std::length_error.
416 std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
417 std::back_inserter(networkIDs),
418 [](const auto &pair) { return pair.first; });
419 }
420 catch (const std::exception& e)
421 {
422 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
423 // exception of type std::length_error.
424 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
425 std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
426 << "\nSome of the loaded networks may not be unloaded" << std::endl;
427 }
428 // We then proceed to unload all the networks which IDs have been appended to the list
429 // up to the point the exception was thrown (if any).
surmeh01bceff2f2018-03-29 16:29:27 +0100430
431 for (auto networkID : networkIDs)
432 {
surmeh013537c2c2018-05-18 16:31:43 +0100433 try
434 {
435 // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
436 // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
437 UnloadNetwork(networkID);
438 }
439 catch (const std::exception& e)
440 {
441 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
442 // exception of type std::length_error.
443 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
444 std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
445 << std::endl;
446 }
telsoa014fcda012018-03-09 14:13:49 +0000447 }
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000448
449 // Clear all dynamic backends.
450 DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends());
451 m_DeviceSpec.ClearDynamicBackends();
Colm Donelan1aff3932020-02-05 17:48:59 +0000452 m_BackendContexts.clear();
Finn Williams45a73622020-05-15 18:41:05 +0100453
454 BackendRegistryInstance().SetProfilingService(armnn::EmptyOptional());
alered01a7227ac2020-05-07 14:58:29 +0100455 ARMNN_LOG(info) << "Shutdown time: " << std::setprecision(2)
456 << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
telsoa014fcda012018-03-09 14:13:49 +0000457}
458
Kevin Mayd92a6e42021-02-04 10:27:41 +0000459LoadedNetwork* RuntimeImpl::GetLoadedNetworkPtr(NetworkId networkId) const
surmeh013537c2c2018-05-18 16:31:43 +0100460{
461 std::lock_guard<std::mutex> lockGuard(m_Mutex);
462 return m_LoadedNetworks.at(networkId).get();
463}
464
Kevin Mayd92a6e42021-02-04 10:27:41 +0000465TensorInfo RuntimeImpl::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
telsoa014fcda012018-03-09 14:13:49 +0000466{
surmeh013537c2c2018-05-18 16:31:43 +0100467 return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000468}
469
Kevin Mayd92a6e42021-02-04 10:27:41 +0000470TensorInfo RuntimeImpl::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
telsoa014fcda012018-03-09 14:13:49 +0000471{
surmeh013537c2c2018-05-18 16:31:43 +0100472 return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000473}
474
Derek Lamberti03614f62018-10-02 15:52:46 +0100475
Kevin Mayd92a6e42021-02-04 10:27:41 +0000476Status RuntimeImpl::EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100477 const InputTensors& inputTensors,
478 const OutputTensors& outputTensors)
telsoa014fcda012018-03-09 14:13:49 +0000479{
surmeh013537c2c2018-05-18 16:31:43 +0100480 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100481
482 if (!loadedNetwork)
483 {
484 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
485 return Status::Failure;
486 }
487 if (loadedNetwork->IsAsyncEnabled())
488 {
489 ARMNN_LOG(error) << "Network " << networkId << " is async enabled.\n";
490 return Status::Failure;
491 }
Narumol Prangnawarat5b4d0d52020-06-23 11:45:56 +0100492 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
493
494 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "EnqueueWorkload");
Derek Lamberti03614f62018-10-02 15:52:46 +0100495
496 static thread_local NetworkId lastId = networkId;
497 if (lastId != networkId)
498 {
499 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
500 {
501 network->FreeWorkingMemory();
502 });
503 }
504 lastId=networkId;
505
surmeh013537c2c2018-05-18 16:31:43 +0100506 return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000507}
508
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100509Status RuntimeImpl::Execute(IWorkingMemHandle& iWorkingMemHandle,
510 const InputTensors& inputTensors,
511 const OutputTensors& outputTensors)
512{
513 NetworkId networkId = iWorkingMemHandle.GetNetworkId();
514 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
515
516 if (!loadedNetwork)
517 {
518 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
519 return Status::Failure;
520 }
521 if (!loadedNetwork->IsAsyncEnabled())
522 {
Keith Davise813d672021-04-22 10:10:34 +0100523 ARMNN_LOG(error) << "Attempting execute " << networkId << " when it is not async enabled.\n";
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100524 return Status::Failure;
525 }
526 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
527
528 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Execute");
529
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100530 return loadedNetwork->Execute(inputTensors, outputTensors, iWorkingMemHandle);
531}
532
533/// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
534/// overlapped Execution by calling this function from different threads.
535std::unique_ptr<IWorkingMemHandle> RuntimeImpl::CreateWorkingMemHandle(NetworkId networkId)
536{
537 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
538
539 if (!loadedNetwork)
540 {
541 ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
542 return nullptr;
543 }
544 if (!loadedNetwork->IsAsyncEnabled())
545 {
546 ARMNN_LOG(error) << "Network " << networkId << " is not async enabled.\n";
547 return nullptr;
548 }
549 ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get());
550
551 ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "CreateWorkingMemHandle");
552
553 static thread_local NetworkId lastId = networkId;
554 if (lastId != networkId)
555 {
556 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
557 {
558 network->FreeWorkingMemory();
559 });
560 }
561 lastId=networkId;
562
563 return loadedNetwork->CreateWorkingMemHandle(networkId);
564}
565
Kevin Mayd92a6e42021-02-04 10:27:41 +0000566void RuntimeImpl::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000567{
568 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
569 loadedNetwork->RegisterDebugCallback(func);
570}
571
Kevin Mayd92a6e42021-02-04 10:27:41 +0000572void RuntimeImpl::LoadDynamicBackends(const std::string& overrideBackendPath)
Matteo Martincighe54aa062019-08-05 14:12:11 +0100573{
574 // Get the paths where to load the dynamic backends from
575 std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath);
576
577 // Get the shared objects to try to load as dynamic backends
578 std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths);
579
580 // Create a list of dynamic backends
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100581 m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects);
582
583 // Register the dynamic backends in the backend registry
Matteo Martincigh89533902019-08-15 12:08:06 +0100584 BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends);
585
586 // Add the registered dynamic backend ids to the list of supported backends
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000587 m_DeviceSpec.AddSupportedBackends(registeredBackendIds, true);
telsoa014fcda012018-03-09 14:13:49 +0000588}
Matteo Martincighe54aa062019-08-05 14:12:11 +0100589
590} // namespace armnn