blob: ddfa6b4ffda35bdc1f2121b95c423ca3cd32ca3b [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 Martincighe54aa062019-08-05 14:12:11 +01008
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00009#include <backendsCommon/BackendRegistry.hpp>
David Beck1b61be52018-11-08 09:19:14 +000010#include <backendsCommon/IBackendContext.hpp>
Matteo Martincighe54aa062019-08-05 14:12:11 +010011#include <backendsCommon/DynamicBackendUtils.hpp>
telsoa014fcda012018-03-09 14:13:49 +000012
surmeh013537c2c2018-05-18 16:31:43 +010013#include <iostream>
14
telsoa014fcda012018-03-09 14:13:49 +000015#include <boost/log/trivial.hpp>
16#include <boost/polymorphic_cast.hpp>
17
18using namespace armnn;
19using namespace std;
20
21namespace armnn
22{
23
24IRuntime* IRuntime::CreateRaw(const CreationOptions& options)
25{
26 return new Runtime(options);
27}
28
29IRuntimePtr IRuntime::Create(const CreationOptions& options)
30{
31 return IRuntimePtr(CreateRaw(options), &IRuntime::Destroy);
32}
33
34void IRuntime::Destroy(IRuntime* runtime)
35{
36 delete boost::polymorphic_downcast<Runtime*>(runtime);
37}
38
39int Runtime::GenerateNetworkId()
40{
41 return m_NetworkIdCounter++;
42}
43
44Status Runtime::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr inNetwork)
45{
telsoa01c577f2c2018-08-31 09:22:23 +010046 std::string ignoredErrorMessage;
47 return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage);
48}
49
50Status Runtime::LoadNetwork(NetworkId& networkIdOut,
51 IOptimizedNetworkPtr inNetwork,
52 std::string & errorMessage)
53{
telsoa014fcda012018-03-09 14:13:49 +000054 IOptimizedNetwork* rawNetwork = inNetwork.release();
David Beck1b61be52018-11-08 09:19:14 +000055
56 networkIdOut = GenerateNetworkId();
57
58 for (auto&& context : m_BackendContexts)
59 {
60 context.second->BeforeLoadNetwork(networkIdOut);
61 }
62
telsoa014fcda012018-03-09 14:13:49 +000063 unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
64 std::unique_ptr<OptimizedNetwork>(boost::polymorphic_downcast<OptimizedNetwork*>(rawNetwork)),
telsoa01c577f2c2018-08-31 09:22:23 +010065 errorMessage);
telsoa014fcda012018-03-09 14:13:49 +000066
67 if (!loadedNetwork)
68 {
69 return Status::Failure;
70 }
71
telsoa01c577f2c2018-08-31 09:22:23 +010072 {
73 std::lock_guard<std::mutex> lockGuard(m_Mutex);
74
75 // Stores the network
76 m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork);
77 }
telsoa014fcda012018-03-09 14:13:49 +000078
David Beck1b61be52018-11-08 09:19:14 +000079 for (auto&& context : m_BackendContexts)
80 {
81 context.second->AfterLoadNetwork(networkIdOut);
82 }
83
telsoa014fcda012018-03-09 14:13:49 +000084 return Status::Success;
telsoa014fcda012018-03-09 14:13:49 +000085}
86
87Status Runtime::UnloadNetwork(NetworkId networkId)
88{
David Beck1b61be52018-11-08 09:19:14 +000089 bool unloadOk = true;
90 for (auto&& context : m_BackendContexts)
David Beck9efb57d2018-11-05 13:40:33 +000091 {
David Beck1b61be52018-11-08 09:19:14 +000092 unloadOk &= context.second->BeforeUnloadNetwork(networkId);
David Beck9efb57d2018-11-05 13:40:33 +000093 }
David Beck1b61be52018-11-08 09:19:14 +000094
95 if (!unloadOk)
96 {
97 BOOST_LOG_TRIVIAL(warning) << "Runtime::UnloadNetwork(): failed to unload "
98 "network with ID:" << networkId << " because BeforeUnloadNetwork failed";
99 return Status::Failure;
100 }
David Beck9efb57d2018-11-05 13:40:33 +0000101
telsoa014fcda012018-03-09 14:13:49 +0000102 {
telsoa01c577f2c2018-08-31 09:22:23 +0100103 std::lock_guard<std::mutex> lockGuard(m_Mutex);
104
105 if (m_LoadedNetworks.erase(networkId) == 0)
106 {
107 BOOST_LOG_TRIVIAL(warning) << "WARNING: Runtime::UnloadNetwork(): " << networkId << " not found!";
108 return Status::Failure;
109 }
David Beck1b61be52018-11-08 09:19:14 +0000110 }
David Beck9efb57d2018-11-05 13:40:33 +0000111
David Beck1b61be52018-11-08 09:19:14 +0000112 for (auto&& context : m_BackendContexts)
113 {
114 context.second->AfterUnloadNetwork(networkId);
telsoa01c577f2c2018-08-31 09:22:23 +0100115 }
116
telsoa014fcda012018-03-09 14:13:49 +0000117 BOOST_LOG_TRIVIAL(debug) << "Runtime::UnloadNetwork(): Unloaded network with ID: " << networkId;
118 return Status::Success;
119}
120
telsoa01c577f2c2018-08-31 09:22:23 +0100121const std::shared_ptr<IProfiler> Runtime::GetProfiler(NetworkId networkId) const
122{
123 auto it = m_LoadedNetworks.find(networkId);
124 if (it != m_LoadedNetworks.end())
125 {
126 auto& loadedNetwork = it->second;
127 return loadedNetwork->GetProfiler();
128 }
129
130 return nullptr;
131}
132
telsoa014fcda012018-03-09 14:13:49 +0000133Runtime::Runtime(const CreationOptions& options)
David Beck1b61be52018-11-08 09:19:14 +0000134 : m_NetworkIdCounter(0)
David Beck056be3c2018-10-22 13:16:00 +0100135 , m_DeviceSpec{BackendRegistryInstance().GetBackendIds()}
telsoa014fcda012018-03-09 14:13:49 +0000136{
137 BOOST_LOG_TRIVIAL(info) << "ArmNN v" << ARMNN_VERSION << "\n";
David Beck1b61be52018-11-08 09:19:14 +0000138
Matteo Martincighe54aa062019-08-05 14:12:11 +0100139 // Load any available/compatible dynamic backend before the runtime
140 // goes through the backend registry
141 LoadDynamicBackends(options.m_DynamicBackendsPath);
142
David Beck1b61be52018-11-08 09:19:14 +0000143 for (const auto& id : BackendRegistryInstance().GetBackendIds())
144 {
145 // Store backend contexts for the supported ones
146 if (m_DeviceSpec.GetSupportedBackends().count(id) > 0)
147 {
148 auto factoryFun = BackendRegistryInstance().GetFactory(id);
149 auto backend = factoryFun();
150 BOOST_ASSERT(backend.get() != nullptr);
151
152 auto context = backend->CreateBackendContext(options);
153
154 // backends are allowed to return nullptrs if they
155 // don't wish to create a backend specific context
156 if (context)
157 {
158 m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
159 }
160 }
161 }
surmeh01bceff2f2018-03-29 16:29:27 +0100162}
163
164Runtime::~Runtime()
165{
166 std::vector<int> networkIDs;
surmeh013537c2c2018-05-18 16:31:43 +0100167 try
168 {
169 // Coverity fix: The following code may throw an exception of type std::length_error.
170 std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
171 std::back_inserter(networkIDs),
172 [](const auto &pair) { return pair.first; });
173 }
174 catch (const std::exception& e)
175 {
176 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
177 // exception of type std::length_error.
178 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
179 std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
180 << "\nSome of the loaded networks may not be unloaded" << std::endl;
181 }
182 // We then proceed to unload all the networks which IDs have been appended to the list
183 // up to the point the exception was thrown (if any).
surmeh01bceff2f2018-03-29 16:29:27 +0100184
185 for (auto networkID : networkIDs)
186 {
surmeh013537c2c2018-05-18 16:31:43 +0100187 try
188 {
189 // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
190 // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
191 UnloadNetwork(networkID);
192 }
193 catch (const std::exception& e)
194 {
195 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
196 // exception of type std::length_error.
197 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
198 std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
199 << std::endl;
200 }
telsoa014fcda012018-03-09 14:13:49 +0000201 }
202}
203
surmeh013537c2c2018-05-18 16:31:43 +0100204LoadedNetwork* Runtime::GetLoadedNetworkPtr(NetworkId networkId) const
205{
206 std::lock_guard<std::mutex> lockGuard(m_Mutex);
207 return m_LoadedNetworks.at(networkId).get();
208}
209
telsoa014fcda012018-03-09 14:13:49 +0000210TensorInfo Runtime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
211{
surmeh013537c2c2018-05-18 16:31:43 +0100212 return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000213}
214
215TensorInfo Runtime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
216{
surmeh013537c2c2018-05-18 16:31:43 +0100217 return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000218}
219
Derek Lamberti03614f62018-10-02 15:52:46 +0100220
telsoa014fcda012018-03-09 14:13:49 +0000221Status Runtime::EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100222 const InputTensors& inputTensors,
223 const OutputTensors& outputTensors)
telsoa014fcda012018-03-09 14:13:49 +0000224{
surmeh013537c2c2018-05-18 16:31:43 +0100225 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
Derek Lamberti03614f62018-10-02 15:52:46 +0100226
227 static thread_local NetworkId lastId = networkId;
228 if (lastId != networkId)
229 {
230 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
231 {
232 network->FreeWorkingMemory();
233 });
234 }
235 lastId=networkId;
236
surmeh013537c2c2018-05-18 16:31:43 +0100237 return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000238}
239
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000240void Runtime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
241{
242 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
243 loadedNetwork->RegisterDebugCallback(func);
244}
245
Matteo Martincighe54aa062019-08-05 14:12:11 +0100246void Runtime::LoadDynamicBackends(const std::string& overrideBackendPath)
247{
248 // Get the paths where to load the dynamic backends from
249 std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath);
250
251 // Get the shared objects to try to load as dynamic backends
252 std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths);
253
254 // Create a list of dynamic backends
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100255 m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects);
256
257 // Register the dynamic backends in the backend registry
258 DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends);
telsoa014fcda012018-03-09 14:13:49 +0000259}
Matteo Martincighe54aa062019-08-05 14:12:11 +0100260
261} // namespace armnn