blob: 1e3e2320468fbbb05ea6a98927c3f861601beed9 [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 "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>
Matteo Martincighe54aa062019-08-05 14:12:11 +01009
Matteo Martincighe5b8eb92019-11-28 15:45:42 +000010#include <armnn/backends/IBackendContext.hpp>
Matteo Martincighe54aa062019-08-05 14:12:11 +010011#include <backendsCommon/DynamicBackendUtils.hpp>
telsoa014fcda012018-03-09 14:13:49 +000012
Narumol Prangnawarat85ad78c2019-11-18 15:34:23 +000013#include <ProfilingService.hpp>
Jim Flynnc4728ad2019-10-07 15:15:12 +010014
surmeh013537c2c2018-05-18 16:31:43 +010015#include <iostream>
16
telsoa014fcda012018-03-09 14:13:49 +000017#include <boost/polymorphic_cast.hpp>
18
19using namespace armnn;
20using namespace std;
21
22namespace armnn
23{
24
25IRuntime* IRuntime::CreateRaw(const CreationOptions& options)
26{
27 return new Runtime(options);
28}
29
30IRuntimePtr IRuntime::Create(const CreationOptions& options)
31{
32 return IRuntimePtr(CreateRaw(options), &IRuntime::Destroy);
33}
34
35void IRuntime::Destroy(IRuntime* runtime)
36{
37 delete boost::polymorphic_downcast<Runtime*>(runtime);
38}
39
40int Runtime::GenerateNetworkId()
41{
42 return m_NetworkIdCounter++;
43}
44
45Status Runtime::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr inNetwork)
46{
telsoa01c577f2c2018-08-31 09:22:23 +010047 std::string ignoredErrorMessage;
48 return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage);
49}
50
51Status Runtime::LoadNetwork(NetworkId& networkIdOut,
52 IOptimizedNetworkPtr inNetwork,
David Monahan4f1e8e42019-09-04 09:22:10 +010053 std::string& errorMessage)
54{
55 INetworkProperties networkProperties;
56 return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties);
57}
58
59Status Runtime::LoadNetwork(NetworkId& networkIdOut,
60 IOptimizedNetworkPtr inNetwork,
61 std::string& errorMessage,
62 const INetworkProperties& networkProperties)
telsoa01c577f2c2018-08-31 09:22:23 +010063{
telsoa014fcda012018-03-09 14:13:49 +000064 IOptimizedNetwork* rawNetwork = inNetwork.release();
David Beck1b61be52018-11-08 09:19:14 +000065
66 networkIdOut = GenerateNetworkId();
67
68 for (auto&& context : m_BackendContexts)
69 {
70 context.second->BeforeLoadNetwork(networkIdOut);
71 }
72
telsoa014fcda012018-03-09 14:13:49 +000073 unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
74 std::unique_ptr<OptimizedNetwork>(boost::polymorphic_downcast<OptimizedNetwork*>(rawNetwork)),
David Monahan4f1e8e42019-09-04 09:22:10 +010075 errorMessage,
76 networkProperties);
telsoa014fcda012018-03-09 14:13:49 +000077
78 if (!loadedNetwork)
79 {
80 return Status::Failure;
81 }
82
telsoa01c577f2c2018-08-31 09:22:23 +010083 {
84 std::lock_guard<std::mutex> lockGuard(m_Mutex);
85
86 // Stores the network
87 m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork);
88 }
telsoa014fcda012018-03-09 14:13:49 +000089
David Beck1b61be52018-11-08 09:19:14 +000090 for (auto&& context : m_BackendContexts)
91 {
92 context.second->AfterLoadNetwork(networkIdOut);
93 }
94
Keith Davise394bd92019-12-02 15:12:19 +000095 if (profiling::ProfilingService::Instance().IsProfilingEnabled())
96 {
97 profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_LOADS);
98 }
99
telsoa014fcda012018-03-09 14:13:49 +0000100 return Status::Success;
telsoa014fcda012018-03-09 14:13:49 +0000101}
102
103Status Runtime::UnloadNetwork(NetworkId networkId)
104{
David Beck1b61be52018-11-08 09:19:14 +0000105 bool unloadOk = true;
106 for (auto&& context : m_BackendContexts)
David Beck9efb57d2018-11-05 13:40:33 +0000107 {
David Beck1b61be52018-11-08 09:19:14 +0000108 unloadOk &= context.second->BeforeUnloadNetwork(networkId);
David Beck9efb57d2018-11-05 13:40:33 +0000109 }
David Beck1b61be52018-11-08 09:19:14 +0000110
111 if (!unloadOk)
112 {
Derek Lamberti08446972019-11-26 16:38:31 +0000113 ARMNN_LOG(warning) << "Runtime::UnloadNetwork(): failed to unload "
114 "network with ID:" << networkId << " because BeforeUnloadNetwork failed";
David Beck1b61be52018-11-08 09:19:14 +0000115 return Status::Failure;
116 }
David Beck9efb57d2018-11-05 13:40:33 +0000117
telsoa014fcda012018-03-09 14:13:49 +0000118 {
telsoa01c577f2c2018-08-31 09:22:23 +0100119 std::lock_guard<std::mutex> lockGuard(m_Mutex);
120
121 if (m_LoadedNetworks.erase(networkId) == 0)
122 {
Derek Lamberti08446972019-11-26 16:38:31 +0000123 ARMNN_LOG(warning) << "WARNING: Runtime::UnloadNetwork(): " << networkId << " not found!";
telsoa01c577f2c2018-08-31 09:22:23 +0100124 return Status::Failure;
125 }
Keith Davise394bd92019-12-02 15:12:19 +0000126 if (profiling::ProfilingService::Instance().IsProfilingEnabled())
127 {
128 profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS);
129 }
David Beck1b61be52018-11-08 09:19:14 +0000130 }
David Beck9efb57d2018-11-05 13:40:33 +0000131
David Beck1b61be52018-11-08 09:19:14 +0000132 for (auto&& context : m_BackendContexts)
133 {
134 context.second->AfterUnloadNetwork(networkId);
telsoa01c577f2c2018-08-31 09:22:23 +0100135 }
136
Derek Lamberti08446972019-11-26 16:38:31 +0000137 ARMNN_LOG(debug) << "Runtime::UnloadNetwork(): Unloaded network with ID: " << networkId;
telsoa014fcda012018-03-09 14:13:49 +0000138 return Status::Success;
139}
140
telsoa01c577f2c2018-08-31 09:22:23 +0100141const std::shared_ptr<IProfiler> Runtime::GetProfiler(NetworkId networkId) const
142{
143 auto it = m_LoadedNetworks.find(networkId);
144 if (it != m_LoadedNetworks.end())
145 {
146 auto& loadedNetwork = it->second;
147 return loadedNetwork->GetProfiler();
148 }
149
150 return nullptr;
151}
152
telsoa014fcda012018-03-09 14:13:49 +0000153Runtime::Runtime(const CreationOptions& options)
David Beck1b61be52018-11-08 09:19:14 +0000154 : m_NetworkIdCounter(0)
David Beck056be3c2018-10-22 13:16:00 +0100155 , m_DeviceSpec{BackendRegistryInstance().GetBackendIds()}
telsoa014fcda012018-03-09 14:13:49 +0000156{
Derek Lamberti08446972019-11-26 16:38:31 +0000157 ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n";
David Beck1b61be52018-11-08 09:19:14 +0000158
Jim Flynnc4728ad2019-10-07 15:15:12 +0100159 // pass configuration info to the profiling service
Jim Flynn672d06e2019-10-15 10:18:11 +0100160 armnn::profiling::ProfilingService::Instance().ConfigureProfilingService(options.m_ProfilingOptions);
Jim Flynnc4728ad2019-10-07 15:15:12 +0100161
Matteo Martincighe54aa062019-08-05 14:12:11 +0100162 // Load any available/compatible dynamic backend before the runtime
163 // goes through the backend registry
164 LoadDynamicBackends(options.m_DynamicBackendsPath);
165
David Beck1b61be52018-11-08 09:19:14 +0000166 for (const auto& id : BackendRegistryInstance().GetBackendIds())
167 {
168 // Store backend contexts for the supported ones
Matteo Martincigh3d8a9ed2019-08-08 10:49:03 +0100169 const BackendIdSet& supportedBackends = m_DeviceSpec.GetSupportedBackends();
Matteo Martincigh89533902019-08-15 12:08:06 +0100170 if (supportedBackends.find(id) != supportedBackends.end())
David Beck1b61be52018-11-08 09:19:14 +0000171 {
172 auto factoryFun = BackendRegistryInstance().GetFactory(id);
173 auto backend = factoryFun();
174 BOOST_ASSERT(backend.get() != nullptr);
175
176 auto context = backend->CreateBackendContext(options);
177
178 // backends are allowed to return nullptrs if they
179 // don't wish to create a backend specific context
180 if (context)
181 {
182 m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
183 }
184 }
185 }
surmeh01bceff2f2018-03-29 16:29:27 +0100186}
187
188Runtime::~Runtime()
189{
190 std::vector<int> networkIDs;
surmeh013537c2c2018-05-18 16:31:43 +0100191 try
192 {
193 // Coverity fix: The following code may throw an exception of type std::length_error.
194 std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
195 std::back_inserter(networkIDs),
196 [](const auto &pair) { return pair.first; });
197 }
198 catch (const std::exception& e)
199 {
200 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
201 // exception of type std::length_error.
202 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
203 std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
204 << "\nSome of the loaded networks may not be unloaded" << std::endl;
205 }
206 // We then proceed to unload all the networks which IDs have been appended to the list
207 // up to the point the exception was thrown (if any).
surmeh01bceff2f2018-03-29 16:29:27 +0100208
209 for (auto networkID : networkIDs)
210 {
surmeh013537c2c2018-05-18 16:31:43 +0100211 try
212 {
213 // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
214 // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
215 UnloadNetwork(networkID);
216 }
217 catch (const std::exception& e)
218 {
219 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
220 // exception of type std::length_error.
221 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
222 std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
223 << std::endl;
224 }
telsoa014fcda012018-03-09 14:13:49 +0000225 }
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000226
227 // Clear all dynamic backends.
228 DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends());
229 m_DeviceSpec.ClearDynamicBackends();
telsoa014fcda012018-03-09 14:13:49 +0000230}
231
surmeh013537c2c2018-05-18 16:31:43 +0100232LoadedNetwork* Runtime::GetLoadedNetworkPtr(NetworkId networkId) const
233{
234 std::lock_guard<std::mutex> lockGuard(m_Mutex);
235 return m_LoadedNetworks.at(networkId).get();
236}
237
telsoa014fcda012018-03-09 14:13:49 +0000238TensorInfo Runtime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
239{
surmeh013537c2c2018-05-18 16:31:43 +0100240 return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000241}
242
243TensorInfo Runtime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
244{
surmeh013537c2c2018-05-18 16:31:43 +0100245 return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000246}
247
Derek Lamberti03614f62018-10-02 15:52:46 +0100248
telsoa014fcda012018-03-09 14:13:49 +0000249Status Runtime::EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100250 const InputTensors& inputTensors,
251 const OutputTensors& outputTensors)
telsoa014fcda012018-03-09 14:13:49 +0000252{
surmeh013537c2c2018-05-18 16:31:43 +0100253 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
Derek Lamberti03614f62018-10-02 15:52:46 +0100254
255 static thread_local NetworkId lastId = networkId;
256 if (lastId != networkId)
257 {
258 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
259 {
260 network->FreeWorkingMemory();
261 });
262 }
263 lastId=networkId;
264
surmeh013537c2c2018-05-18 16:31:43 +0100265 return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000266}
267
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000268void Runtime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
269{
270 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
271 loadedNetwork->RegisterDebugCallback(func);
272}
273
Matteo Martincighe54aa062019-08-05 14:12:11 +0100274void Runtime::LoadDynamicBackends(const std::string& overrideBackendPath)
275{
276 // Get the paths where to load the dynamic backends from
277 std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath);
278
279 // Get the shared objects to try to load as dynamic backends
280 std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths);
281
282 // Create a list of dynamic backends
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100283 m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects);
284
285 // Register the dynamic backends in the backend registry
Matteo Martincigh89533902019-08-15 12:08:06 +0100286 BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends);
287
288 // Add the registered dynamic backend ids to the list of supported backends
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000289 m_DeviceSpec.AddSupportedBackends(registeredBackendIds, true);
telsoa014fcda012018-03-09 14:13:49 +0000290}
Matteo Martincighe54aa062019-08-05 14:12:11 +0100291
292} // namespace armnn