telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #include "Runtime.hpp" |
| 6 | |
David Beck | 056be3c | 2018-10-22 13:16:00 +0100 | [diff] [blame] | 7 | #include <armnn/Version.hpp> |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 8 | #include <armnn/BackendRegistry.hpp> |
Matthew Bentham | f48afc6 | 2020-01-15 17:55:08 +0000 | [diff] [blame] | 9 | #include <armnn/Logging.hpp> |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 10 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 11 | #include <armnn/backends/IBackendContext.hpp> |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 12 | #include <backendsCommon/DynamicBackendUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 14 | #include <ProfilingService.hpp> |
Jim Flynn | c4728ad | 2019-10-07 15:15:12 +0100 | [diff] [blame] | 15 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 16 | #include <iostream> |
| 17 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | #include <boost/polymorphic_cast.hpp> |
| 19 | |
| 20 | using namespace armnn; |
| 21 | using namespace std; |
| 22 | |
| 23 | namespace armnn |
| 24 | { |
| 25 | |
| 26 | IRuntime* IRuntime::CreateRaw(const CreationOptions& options) |
| 27 | { |
| 28 | return new Runtime(options); |
| 29 | } |
| 30 | |
| 31 | IRuntimePtr IRuntime::Create(const CreationOptions& options) |
| 32 | { |
| 33 | return IRuntimePtr(CreateRaw(options), &IRuntime::Destroy); |
| 34 | } |
| 35 | |
| 36 | void IRuntime::Destroy(IRuntime* runtime) |
| 37 | { |
| 38 | delete boost::polymorphic_downcast<Runtime*>(runtime); |
| 39 | } |
| 40 | |
| 41 | int Runtime::GenerateNetworkId() |
| 42 | { |
| 43 | return m_NetworkIdCounter++; |
| 44 | } |
| 45 | |
| 46 | Status Runtime::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr inNetwork) |
| 47 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | std::string ignoredErrorMessage; |
| 49 | return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage); |
| 50 | } |
| 51 | |
| 52 | Status Runtime::LoadNetwork(NetworkId& networkIdOut, |
| 53 | IOptimizedNetworkPtr inNetwork, |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 54 | std::string& errorMessage) |
| 55 | { |
| 56 | INetworkProperties networkProperties; |
| 57 | return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties); |
| 58 | } |
| 59 | |
| 60 | Status Runtime::LoadNetwork(NetworkId& networkIdOut, |
| 61 | IOptimizedNetworkPtr inNetwork, |
| 62 | std::string& errorMessage, |
| 63 | const INetworkProperties& networkProperties) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | IOptimizedNetwork* rawNetwork = inNetwork.release(); |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 66 | |
| 67 | networkIdOut = GenerateNetworkId(); |
| 68 | |
| 69 | for (auto&& context : m_BackendContexts) |
| 70 | { |
| 71 | context.second->BeforeLoadNetwork(networkIdOut); |
| 72 | } |
| 73 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 74 | unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork( |
| 75 | std::unique_ptr<OptimizedNetwork>(boost::polymorphic_downcast<OptimizedNetwork*>(rawNetwork)), |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 76 | errorMessage, |
| 77 | networkProperties); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 78 | |
| 79 | if (!loadedNetwork) |
| 80 | { |
| 81 | return Status::Failure; |
| 82 | } |
| 83 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 84 | { |
| 85 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 86 | |
| 87 | // Stores the network |
| 88 | m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork); |
| 89 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 91 | for (auto&& context : m_BackendContexts) |
| 92 | { |
| 93 | context.second->AfterLoadNetwork(networkIdOut); |
| 94 | } |
| 95 | |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 96 | if (profiling::ProfilingService::Instance().IsProfilingEnabled()) |
| 97 | { |
| 98 | profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_LOADS); |
| 99 | } |
| 100 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | return Status::Success; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | Status Runtime::UnloadNetwork(NetworkId networkId) |
| 105 | { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 106 | bool unloadOk = true; |
| 107 | for (auto&& context : m_BackendContexts) |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 108 | { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 109 | unloadOk &= context.second->BeforeUnloadNetwork(networkId); |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 110 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 111 | |
| 112 | if (!unloadOk) |
| 113 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 114 | ARMNN_LOG(warning) << "Runtime::UnloadNetwork(): failed to unload " |
| 115 | "network with ID:" << networkId << " because BeforeUnloadNetwork failed"; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 116 | return Status::Failure; |
| 117 | } |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 118 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 119 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 120 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 121 | |
| 122 | if (m_LoadedNetworks.erase(networkId) == 0) |
| 123 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 124 | ARMNN_LOG(warning) << "WARNING: Runtime::UnloadNetwork(): " << networkId << " not found!"; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 125 | return Status::Failure; |
| 126 | } |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 127 | if (profiling::ProfilingService::Instance().IsProfilingEnabled()) |
| 128 | { |
| 129 | profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS); |
| 130 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 131 | } |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 132 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 133 | for (auto&& context : m_BackendContexts) |
| 134 | { |
| 135 | context.second->AfterUnloadNetwork(networkId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 138 | ARMNN_LOG(debug) << "Runtime::UnloadNetwork(): Unloaded network with ID: " << networkId; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 139 | return Status::Success; |
| 140 | } |
| 141 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | const 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 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 154 | Runtime::Runtime(const CreationOptions& options) |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 155 | : m_NetworkIdCounter(0) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 156 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 157 | ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n"; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 158 | |
Jim Flynn | c4728ad | 2019-10-07 15:15:12 +0100 | [diff] [blame] | 159 | // pass configuration info to the profiling service |
Jim Flynn | 672d06e | 2019-10-15 10:18:11 +0100 | [diff] [blame] | 160 | armnn::profiling::ProfilingService::Instance().ConfigureProfilingService(options.m_ProfilingOptions); |
Jim Flynn | c4728ad | 2019-10-07 15:15:12 +0100 | [diff] [blame] | 161 | |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 162 | // Load any available/compatible dynamic backend before the runtime |
| 163 | // goes through the backend registry |
| 164 | LoadDynamicBackends(options.m_DynamicBackendsPath); |
| 165 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame^] | 166 | BackendIdSet supportedBackends; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 167 | for (const auto& id : BackendRegistryInstance().GetBackendIds()) |
| 168 | { |
| 169 | // Store backend contexts for the supported ones |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame^] | 170 | try { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 171 | auto factoryFun = BackendRegistryInstance().GetFactory(id); |
| 172 | auto backend = factoryFun(); |
| 173 | BOOST_ASSERT(backend.get() != nullptr); |
| 174 | |
| 175 | auto context = backend->CreateBackendContext(options); |
| 176 | |
| 177 | // backends are allowed to return nullptrs if they |
| 178 | // don't wish to create a backend specific context |
| 179 | if (context) |
| 180 | { |
| 181 | m_BackendContexts.emplace(std::make_pair(id, std::move(context))); |
| 182 | } |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame^] | 183 | supportedBackends.emplace(id); |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 184 | } |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame^] | 185 | catch (const BackendUnavailableException&) |
| 186 | { |
| 187 | // Ignore backends which are unavailable |
| 188 | } |
| 189 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 190 | } |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame^] | 191 | m_DeviceSpec.AddSupportedBackends(supportedBackends); |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | Runtime::~Runtime() |
| 195 | { |
| 196 | std::vector<int> networkIDs; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 197 | try |
| 198 | { |
| 199 | // Coverity fix: The following code may throw an exception of type std::length_error. |
| 200 | std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(), |
| 201 | std::back_inserter(networkIDs), |
| 202 | [](const auto &pair) { return pair.first; }); |
| 203 | } |
| 204 | catch (const std::exception& e) |
| 205 | { |
| 206 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 207 | // exception of type std::length_error. |
| 208 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 209 | std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what() |
| 210 | << "\nSome of the loaded networks may not be unloaded" << std::endl; |
| 211 | } |
| 212 | // We then proceed to unload all the networks which IDs have been appended to the list |
| 213 | // up to the point the exception was thrown (if any). |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 214 | |
| 215 | for (auto networkID : networkIDs) |
| 216 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 217 | try |
| 218 | { |
| 219 | // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error, |
| 220 | // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error |
| 221 | UnloadNetwork(networkID); |
| 222 | } |
| 223 | catch (const std::exception& e) |
| 224 | { |
| 225 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 226 | // exception of type std::length_error. |
| 227 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 228 | std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what() |
| 229 | << std::endl; |
| 230 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 231 | } |
Narumol Prangnawarat | 60a20fb | 2019-12-09 17:24:41 +0000 | [diff] [blame] | 232 | |
| 233 | // Clear all dynamic backends. |
| 234 | DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends()); |
| 235 | m_DeviceSpec.ClearDynamicBackends(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 236 | } |
| 237 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 238 | LoadedNetwork* Runtime::GetLoadedNetworkPtr(NetworkId networkId) const |
| 239 | { |
| 240 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 241 | return m_LoadedNetworks.at(networkId).get(); |
| 242 | } |
| 243 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 244 | TensorInfo Runtime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const |
| 245 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 246 | return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | TensorInfo Runtime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const |
| 250 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 251 | return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 254 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 255 | Status Runtime::EnqueueWorkload(NetworkId networkId, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 256 | const InputTensors& inputTensors, |
| 257 | const OutputTensors& outputTensors) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 258 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 259 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 260 | |
| 261 | static thread_local NetworkId lastId = networkId; |
| 262 | if (lastId != networkId) |
| 263 | { |
| 264 | LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network) |
| 265 | { |
| 266 | network->FreeWorkingMemory(); |
| 267 | }); |
| 268 | } |
| 269 | lastId=networkId; |
| 270 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 271 | return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 274 | void Runtime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) |
| 275 | { |
| 276 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
| 277 | loadedNetwork->RegisterDebugCallback(func); |
| 278 | } |
| 279 | |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 280 | void Runtime::LoadDynamicBackends(const std::string& overrideBackendPath) |
| 281 | { |
| 282 | // Get the paths where to load the dynamic backends from |
| 283 | std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath); |
| 284 | |
| 285 | // Get the shared objects to try to load as dynamic backends |
| 286 | std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths); |
| 287 | |
| 288 | // Create a list of dynamic backends |
Matteo Martincigh | 0c2b289 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 289 | m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects); |
| 290 | |
| 291 | // Register the dynamic backends in the backend registry |
Matteo Martincigh | 8953390 | 2019-08-15 12:08:06 +0100 | [diff] [blame] | 292 | BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends); |
| 293 | |
| 294 | // Add the registered dynamic backend ids to the list of supported backends |
Narumol Prangnawarat | 60a20fb | 2019-12-09 17:24:41 +0000 | [diff] [blame] | 295 | m_DeviceSpec.AddSupportedBackends(registeredBackendIds, true); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 296 | } |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 297 | |
| 298 | } // namespace armnn |