blob: 2d7269a09c8f6c1d828516af63c562d0d8a631f9 [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>
Matthew Benthamf48afc62020-01-15 17:55:08 +00009#include <armnn/Logging.hpp>
Matteo Martincighe54aa062019-08-05 14:12:11 +010010
Matteo Martincighe5b8eb92019-11-28 15:45:42 +000011#include <armnn/backends/IBackendContext.hpp>
Matteo Martincighe54aa062019-08-05 14:12:11 +010012#include <backendsCommon/DynamicBackendUtils.hpp>
telsoa014fcda012018-03-09 14:13:49 +000013
Narumol Prangnawarat85ad78c2019-11-18 15:34:23 +000014#include <ProfilingService.hpp>
Jim Flynnc4728ad2019-10-07 15:15:12 +010015
surmeh013537c2c2018-05-18 16:31:43 +010016#include <iostream>
17
telsoa014fcda012018-03-09 14:13:49 +000018#include <boost/polymorphic_cast.hpp>
19
20using namespace armnn;
21using namespace std;
22
23namespace armnn
24{
25
26IRuntime* IRuntime::CreateRaw(const CreationOptions& options)
27{
28 return new Runtime(options);
29}
30
31IRuntimePtr IRuntime::Create(const CreationOptions& options)
32{
33 return IRuntimePtr(CreateRaw(options), &IRuntime::Destroy);
34}
35
36void IRuntime::Destroy(IRuntime* runtime)
37{
38 delete boost::polymorphic_downcast<Runtime*>(runtime);
39}
40
41int Runtime::GenerateNetworkId()
42{
43 return m_NetworkIdCounter++;
44}
45
46Status Runtime::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr inNetwork)
47{
telsoa01c577f2c2018-08-31 09:22:23 +010048 std::string ignoredErrorMessage;
49 return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage);
50}
51
52Status Runtime::LoadNetwork(NetworkId& networkIdOut,
53 IOptimizedNetworkPtr inNetwork,
David Monahan4f1e8e42019-09-04 09:22:10 +010054 std::string& errorMessage)
55{
56 INetworkProperties networkProperties;
57 return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties);
58}
59
60Status Runtime::LoadNetwork(NetworkId& networkIdOut,
61 IOptimizedNetworkPtr inNetwork,
62 std::string& errorMessage,
63 const INetworkProperties& networkProperties)
telsoa01c577f2c2018-08-31 09:22:23 +010064{
telsoa014fcda012018-03-09 14:13:49 +000065 IOptimizedNetwork* rawNetwork = inNetwork.release();
David Beck1b61be52018-11-08 09:19:14 +000066
67 networkIdOut = GenerateNetworkId();
68
69 for (auto&& context : m_BackendContexts)
70 {
71 context.second->BeforeLoadNetwork(networkIdOut);
72 }
73
telsoa014fcda012018-03-09 14:13:49 +000074 unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
75 std::unique_ptr<OptimizedNetwork>(boost::polymorphic_downcast<OptimizedNetwork*>(rawNetwork)),
David Monahan4f1e8e42019-09-04 09:22:10 +010076 errorMessage,
77 networkProperties);
telsoa014fcda012018-03-09 14:13:49 +000078
79 if (!loadedNetwork)
80 {
81 return Status::Failure;
82 }
83
telsoa01c577f2c2018-08-31 09:22:23 +010084 {
85 std::lock_guard<std::mutex> lockGuard(m_Mutex);
86
87 // Stores the network
88 m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork);
89 }
telsoa014fcda012018-03-09 14:13:49 +000090
David Beck1b61be52018-11-08 09:19:14 +000091 for (auto&& context : m_BackendContexts)
92 {
93 context.second->AfterLoadNetwork(networkIdOut);
94 }
95
Keith Davise394bd92019-12-02 15:12:19 +000096 if (profiling::ProfilingService::Instance().IsProfilingEnabled())
97 {
98 profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_LOADS);
99 }
100
telsoa014fcda012018-03-09 14:13:49 +0000101 return Status::Success;
telsoa014fcda012018-03-09 14:13:49 +0000102}
103
104Status Runtime::UnloadNetwork(NetworkId networkId)
105{
David Beck1b61be52018-11-08 09:19:14 +0000106 bool unloadOk = true;
107 for (auto&& context : m_BackendContexts)
David Beck9efb57d2018-11-05 13:40:33 +0000108 {
David Beck1b61be52018-11-08 09:19:14 +0000109 unloadOk &= context.second->BeforeUnloadNetwork(networkId);
David Beck9efb57d2018-11-05 13:40:33 +0000110 }
David Beck1b61be52018-11-08 09:19:14 +0000111
112 if (!unloadOk)
113 {
Derek Lamberti08446972019-11-26 16:38:31 +0000114 ARMNN_LOG(warning) << "Runtime::UnloadNetwork(): failed to unload "
115 "network with ID:" << networkId << " because BeforeUnloadNetwork failed";
David Beck1b61be52018-11-08 09:19:14 +0000116 return Status::Failure;
117 }
David Beck9efb57d2018-11-05 13:40:33 +0000118
telsoa014fcda012018-03-09 14:13:49 +0000119 {
telsoa01c577f2c2018-08-31 09:22:23 +0100120 std::lock_guard<std::mutex> lockGuard(m_Mutex);
121
122 if (m_LoadedNetworks.erase(networkId) == 0)
123 {
Derek Lamberti08446972019-11-26 16:38:31 +0000124 ARMNN_LOG(warning) << "WARNING: Runtime::UnloadNetwork(): " << networkId << " not found!";
telsoa01c577f2c2018-08-31 09:22:23 +0100125 return Status::Failure;
126 }
Keith Davise394bd92019-12-02 15:12:19 +0000127 if (profiling::ProfilingService::Instance().IsProfilingEnabled())
128 {
129 profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS);
130 }
David Beck1b61be52018-11-08 09:19:14 +0000131 }
David Beck9efb57d2018-11-05 13:40:33 +0000132
David Beck1b61be52018-11-08 09:19:14 +0000133 for (auto&& context : m_BackendContexts)
134 {
135 context.second->AfterUnloadNetwork(networkId);
telsoa01c577f2c2018-08-31 09:22:23 +0100136 }
137
Derek Lamberti08446972019-11-26 16:38:31 +0000138 ARMNN_LOG(debug) << "Runtime::UnloadNetwork(): Unloaded network with ID: " << networkId;
telsoa014fcda012018-03-09 14:13:49 +0000139 return Status::Success;
140}
141
telsoa01c577f2c2018-08-31 09:22:23 +0100142const std::shared_ptr<IProfiler> Runtime::GetProfiler(NetworkId networkId) const
143{
144 auto it = m_LoadedNetworks.find(networkId);
145 if (it != m_LoadedNetworks.end())
146 {
147 auto& loadedNetwork = it->second;
148 return loadedNetwork->GetProfiler();
149 }
150
151 return nullptr;
152}
153
telsoa014fcda012018-03-09 14:13:49 +0000154Runtime::Runtime(const CreationOptions& options)
David Beck1b61be52018-11-08 09:19:14 +0000155 : m_NetworkIdCounter(0)
David Beck056be3c2018-10-22 13:16:00 +0100156 , m_DeviceSpec{BackendRegistryInstance().GetBackendIds()}
telsoa014fcda012018-03-09 14:13:49 +0000157{
Derek Lamberti08446972019-11-26 16:38:31 +0000158 ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n";
David Beck1b61be52018-11-08 09:19:14 +0000159
Jim Flynnc4728ad2019-10-07 15:15:12 +0100160 // pass configuration info to the profiling service
Jim Flynn672d06e2019-10-15 10:18:11 +0100161 armnn::profiling::ProfilingService::Instance().ConfigureProfilingService(options.m_ProfilingOptions);
Jim Flynnc4728ad2019-10-07 15:15:12 +0100162
Matteo Martincighe54aa062019-08-05 14:12:11 +0100163 // Load any available/compatible dynamic backend before the runtime
164 // goes through the backend registry
165 LoadDynamicBackends(options.m_DynamicBackendsPath);
166
David Beck1b61be52018-11-08 09:19:14 +0000167 for (const auto& id : BackendRegistryInstance().GetBackendIds())
168 {
169 // Store backend contexts for the supported ones
Matteo Martincigh3d8a9ed2019-08-08 10:49:03 +0100170 const BackendIdSet& supportedBackends = m_DeviceSpec.GetSupportedBackends();
Matteo Martincigh89533902019-08-15 12:08:06 +0100171 if (supportedBackends.find(id) != supportedBackends.end())
David Beck1b61be52018-11-08 09:19:14 +0000172 {
173 auto factoryFun = BackendRegistryInstance().GetFactory(id);
174 auto backend = factoryFun();
175 BOOST_ASSERT(backend.get() != nullptr);
176
177 auto context = backend->CreateBackendContext(options);
178
179 // backends are allowed to return nullptrs if they
180 // don't wish to create a backend specific context
181 if (context)
182 {
183 m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
184 }
185 }
186 }
surmeh01bceff2f2018-03-29 16:29:27 +0100187}
188
189Runtime::~Runtime()
190{
191 std::vector<int> networkIDs;
surmeh013537c2c2018-05-18 16:31:43 +0100192 try
193 {
194 // Coverity fix: The following code may throw an exception of type std::length_error.
195 std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
196 std::back_inserter(networkIDs),
197 [](const auto &pair) { return pair.first; });
198 }
199 catch (const std::exception& e)
200 {
201 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
202 // exception of type std::length_error.
203 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
204 std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
205 << "\nSome of the loaded networks may not be unloaded" << std::endl;
206 }
207 // We then proceed to unload all the networks which IDs have been appended to the list
208 // up to the point the exception was thrown (if any).
surmeh01bceff2f2018-03-29 16:29:27 +0100209
210 for (auto networkID : networkIDs)
211 {
surmeh013537c2c2018-05-18 16:31:43 +0100212 try
213 {
214 // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
215 // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
216 UnloadNetwork(networkID);
217 }
218 catch (const std::exception& e)
219 {
220 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
221 // exception of type std::length_error.
222 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
223 std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
224 << std::endl;
225 }
telsoa014fcda012018-03-09 14:13:49 +0000226 }
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000227
228 // Clear all dynamic backends.
229 DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends());
230 m_DeviceSpec.ClearDynamicBackends();
telsoa014fcda012018-03-09 14:13:49 +0000231}
232
surmeh013537c2c2018-05-18 16:31:43 +0100233LoadedNetwork* Runtime::GetLoadedNetworkPtr(NetworkId networkId) const
234{
235 std::lock_guard<std::mutex> lockGuard(m_Mutex);
236 return m_LoadedNetworks.at(networkId).get();
237}
238
telsoa014fcda012018-03-09 14:13:49 +0000239TensorInfo Runtime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
240{
surmeh013537c2c2018-05-18 16:31:43 +0100241 return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000242}
243
244TensorInfo Runtime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
245{
surmeh013537c2c2018-05-18 16:31:43 +0100246 return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000247}
248
Derek Lamberti03614f62018-10-02 15:52:46 +0100249
telsoa014fcda012018-03-09 14:13:49 +0000250Status Runtime::EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100251 const InputTensors& inputTensors,
252 const OutputTensors& outputTensors)
telsoa014fcda012018-03-09 14:13:49 +0000253{
surmeh013537c2c2018-05-18 16:31:43 +0100254 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
Derek Lamberti03614f62018-10-02 15:52:46 +0100255
256 static thread_local NetworkId lastId = networkId;
257 if (lastId != networkId)
258 {
259 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
260 {
261 network->FreeWorkingMemory();
262 });
263 }
264 lastId=networkId;
265
surmeh013537c2c2018-05-18 16:31:43 +0100266 return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000267}
268
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000269void Runtime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
270{
271 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
272 loadedNetwork->RegisterDebugCallback(func);
273}
274
Matteo Martincighe54aa062019-08-05 14:12:11 +0100275void Runtime::LoadDynamicBackends(const std::string& overrideBackendPath)
276{
277 // Get the paths where to load the dynamic backends from
278 std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath);
279
280 // Get the shared objects to try to load as dynamic backends
281 std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths);
282
283 // Create a list of dynamic backends
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100284 m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects);
285
286 // Register the dynamic backends in the backend registry
Matteo Martincigh89533902019-08-15 12:08:06 +0100287 BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends);
288
289 // Add the registered dynamic backend ids to the list of supported backends
Narumol Prangnawarat60a20fb2019-12-09 17:24:41 +0000290 m_DeviceSpec.AddSupportedBackends(registeredBackendIds, true);
telsoa014fcda012018-03-09 14:13:49 +0000291}
Matteo Martincighe54aa062019-08-05 14:12:11 +0100292
293} // namespace armnn