blob: 9e874848ecf5c2a43c4b80df37b5741f1a6a1739 [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
Matteo Martincigh3d8a9ed2019-08-08 10:49:03 +0100146 const BackendIdSet& supportedBackends = m_DeviceSpec.GetSupportedBackends();
Matteo Martincigh89533902019-08-15 12:08:06 +0100147 if (supportedBackends.find(id) != supportedBackends.end())
David Beck1b61be52018-11-08 09:19:14 +0000148 {
149 auto factoryFun = BackendRegistryInstance().GetFactory(id);
150 auto backend = factoryFun();
151 BOOST_ASSERT(backend.get() != nullptr);
152
153 auto context = backend->CreateBackendContext(options);
154
155 // backends are allowed to return nullptrs if they
156 // don't wish to create a backend specific context
157 if (context)
158 {
159 m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
160 }
161 }
162 }
surmeh01bceff2f2018-03-29 16:29:27 +0100163}
164
165Runtime::~Runtime()
166{
167 std::vector<int> networkIDs;
surmeh013537c2c2018-05-18 16:31:43 +0100168 try
169 {
170 // Coverity fix: The following code may throw an exception of type std::length_error.
171 std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
172 std::back_inserter(networkIDs),
173 [](const auto &pair) { return pair.first; });
174 }
175 catch (const std::exception& e)
176 {
177 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
178 // exception of type std::length_error.
179 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
180 std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
181 << "\nSome of the loaded networks may not be unloaded" << std::endl;
182 }
183 // We then proceed to unload all the networks which IDs have been appended to the list
184 // up to the point the exception was thrown (if any).
surmeh01bceff2f2018-03-29 16:29:27 +0100185
186 for (auto networkID : networkIDs)
187 {
surmeh013537c2c2018-05-18 16:31:43 +0100188 try
189 {
190 // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
191 // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
192 UnloadNetwork(networkID);
193 }
194 catch (const std::exception& e)
195 {
196 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
197 // exception of type std::length_error.
198 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
199 std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
200 << std::endl;
201 }
telsoa014fcda012018-03-09 14:13:49 +0000202 }
203}
204
surmeh013537c2c2018-05-18 16:31:43 +0100205LoadedNetwork* Runtime::GetLoadedNetworkPtr(NetworkId networkId) const
206{
207 std::lock_guard<std::mutex> lockGuard(m_Mutex);
208 return m_LoadedNetworks.at(networkId).get();
209}
210
telsoa014fcda012018-03-09 14:13:49 +0000211TensorInfo Runtime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
212{
surmeh013537c2c2018-05-18 16:31:43 +0100213 return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000214}
215
216TensorInfo Runtime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
217{
surmeh013537c2c2018-05-18 16:31:43 +0100218 return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
telsoa014fcda012018-03-09 14:13:49 +0000219}
220
Derek Lamberti03614f62018-10-02 15:52:46 +0100221
telsoa014fcda012018-03-09 14:13:49 +0000222Status Runtime::EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100223 const InputTensors& inputTensors,
224 const OutputTensors& outputTensors)
telsoa014fcda012018-03-09 14:13:49 +0000225{
surmeh013537c2c2018-05-18 16:31:43 +0100226 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
Derek Lamberti03614f62018-10-02 15:52:46 +0100227
228 static thread_local NetworkId lastId = networkId;
229 if (lastId != networkId)
230 {
231 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
232 {
233 network->FreeWorkingMemory();
234 });
235 }
236 lastId=networkId;
237
surmeh013537c2c2018-05-18 16:31:43 +0100238 return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000239}
240
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000241void Runtime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
242{
243 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
244 loadedNetwork->RegisterDebugCallback(func);
245}
246
Matteo Martincighe54aa062019-08-05 14:12:11 +0100247void Runtime::LoadDynamicBackends(const std::string& overrideBackendPath)
248{
249 // Get the paths where to load the dynamic backends from
250 std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath);
251
252 // Get the shared objects to try to load as dynamic backends
253 std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths);
254
255 // Create a list of dynamic backends
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100256 m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects);
257
258 // Register the dynamic backends in the backend registry
Matteo Martincigh89533902019-08-15 12:08:06 +0100259 BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends);
260
261 // Add the registered dynamic backend ids to the list of supported backends
262 m_DeviceSpec.AddSupportedBackends(registeredBackendIds);
telsoa014fcda012018-03-09 14:13:49 +0000263}
Matteo Martincighe54aa062019-08-05 14:12:11 +0100264
265} // namespace armnn