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> |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 9 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 10 | #include <armnn/backends/IBackendContext.hpp> |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 11 | #include <backendsCommon/DynamicBackendUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 13 | #include <ProfilingService.hpp> |
Jim Flynn | c4728ad | 2019-10-07 15:15:12 +0100 | [diff] [blame] | 14 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 15 | #include <iostream> |
| 16 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 17 | #include <boost/polymorphic_cast.hpp> |
| 18 | |
| 19 | using namespace armnn; |
| 20 | using namespace std; |
| 21 | |
| 22 | namespace armnn |
| 23 | { |
| 24 | |
| 25 | IRuntime* IRuntime::CreateRaw(const CreationOptions& options) |
| 26 | { |
| 27 | return new Runtime(options); |
| 28 | } |
| 29 | |
| 30 | IRuntimePtr IRuntime::Create(const CreationOptions& options) |
| 31 | { |
| 32 | return IRuntimePtr(CreateRaw(options), &IRuntime::Destroy); |
| 33 | } |
| 34 | |
| 35 | void IRuntime::Destroy(IRuntime* runtime) |
| 36 | { |
| 37 | delete boost::polymorphic_downcast<Runtime*>(runtime); |
| 38 | } |
| 39 | |
| 40 | int Runtime::GenerateNetworkId() |
| 41 | { |
| 42 | return m_NetworkIdCounter++; |
| 43 | } |
| 44 | |
| 45 | Status Runtime::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr inNetwork) |
| 46 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 47 | std::string ignoredErrorMessage; |
| 48 | return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage); |
| 49 | } |
| 50 | |
| 51 | Status Runtime::LoadNetwork(NetworkId& networkIdOut, |
| 52 | IOptimizedNetworkPtr inNetwork, |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 53 | std::string& errorMessage) |
| 54 | { |
| 55 | INetworkProperties networkProperties; |
| 56 | return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties); |
| 57 | } |
| 58 | |
| 59 | Status Runtime::LoadNetwork(NetworkId& networkIdOut, |
| 60 | IOptimizedNetworkPtr inNetwork, |
| 61 | std::string& errorMessage, |
| 62 | const INetworkProperties& networkProperties) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 64 | IOptimizedNetwork* rawNetwork = inNetwork.release(); |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 65 | |
| 66 | networkIdOut = GenerateNetworkId(); |
| 67 | |
| 68 | for (auto&& context : m_BackendContexts) |
| 69 | { |
| 70 | context.second->BeforeLoadNetwork(networkIdOut); |
| 71 | } |
| 72 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork( |
| 74 | std::unique_ptr<OptimizedNetwork>(boost::polymorphic_downcast<OptimizedNetwork*>(rawNetwork)), |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 75 | errorMessage, |
| 76 | networkProperties); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | |
| 78 | if (!loadedNetwork) |
| 79 | { |
| 80 | return Status::Failure; |
| 81 | } |
| 82 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 83 | { |
| 84 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 85 | |
| 86 | // Stores the network |
| 87 | m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork); |
| 88 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 89 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 90 | for (auto&& context : m_BackendContexts) |
| 91 | { |
| 92 | context.second->AfterLoadNetwork(networkIdOut); |
| 93 | } |
| 94 | |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame^] | 95 | if (profiling::ProfilingService::Instance().IsProfilingEnabled()) |
| 96 | { |
| 97 | profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_LOADS); |
| 98 | } |
| 99 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 100 | return Status::Success; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | Status Runtime::UnloadNetwork(NetworkId networkId) |
| 104 | { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 105 | bool unloadOk = true; |
| 106 | for (auto&& context : m_BackendContexts) |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 107 | { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 108 | unloadOk &= context.second->BeforeUnloadNetwork(networkId); |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 109 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 110 | |
| 111 | if (!unloadOk) |
| 112 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 113 | ARMNN_LOG(warning) << "Runtime::UnloadNetwork(): failed to unload " |
| 114 | "network with ID:" << networkId << " because BeforeUnloadNetwork failed"; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 115 | return Status::Failure; |
| 116 | } |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 117 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 118 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 119 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 120 | |
| 121 | if (m_LoadedNetworks.erase(networkId) == 0) |
| 122 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 123 | ARMNN_LOG(warning) << "WARNING: Runtime::UnloadNetwork(): " << networkId << " not found!"; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 124 | return Status::Failure; |
| 125 | } |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame^] | 126 | if (profiling::ProfilingService::Instance().IsProfilingEnabled()) |
| 127 | { |
| 128 | profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS); |
| 129 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 130 | } |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 131 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 132 | for (auto&& context : m_BackendContexts) |
| 133 | { |
| 134 | context.second->AfterUnloadNetwork(networkId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 137 | ARMNN_LOG(debug) << "Runtime::UnloadNetwork(): Unloaded network with ID: " << networkId; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 138 | return Status::Success; |
| 139 | } |
| 140 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 141 | const 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 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 153 | Runtime::Runtime(const CreationOptions& options) |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 154 | : m_NetworkIdCounter(0) |
David Beck | 056be3c | 2018-10-22 13:16:00 +0100 | [diff] [blame] | 155 | , m_DeviceSpec{BackendRegistryInstance().GetBackendIds()} |
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 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 166 | for (const auto& id : BackendRegistryInstance().GetBackendIds()) |
| 167 | { |
| 168 | // Store backend contexts for the supported ones |
Matteo Martincigh | 3d8a9ed | 2019-08-08 10:49:03 +0100 | [diff] [blame] | 169 | const BackendIdSet& supportedBackends = m_DeviceSpec.GetSupportedBackends(); |
Matteo Martincigh | 8953390 | 2019-08-15 12:08:06 +0100 | [diff] [blame] | 170 | if (supportedBackends.find(id) != supportedBackends.end()) |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 171 | { |
| 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 | } |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | Runtime::~Runtime() |
| 189 | { |
| 190 | std::vector<int> networkIDs; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 191 | 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). |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 208 | |
| 209 | for (auto networkID : networkIDs) |
| 210 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 211 | 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 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 225 | } |
Narumol Prangnawarat | 60a20fb | 2019-12-09 17:24:41 +0000 | [diff] [blame] | 226 | |
| 227 | // Clear all dynamic backends. |
| 228 | DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends()); |
| 229 | m_DeviceSpec.ClearDynamicBackends(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 230 | } |
| 231 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 232 | LoadedNetwork* Runtime::GetLoadedNetworkPtr(NetworkId networkId) const |
| 233 | { |
| 234 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 235 | return m_LoadedNetworks.at(networkId).get(); |
| 236 | } |
| 237 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 238 | TensorInfo Runtime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const |
| 239 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 240 | return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | TensorInfo Runtime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const |
| 244 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 245 | return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 248 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 249 | Status Runtime::EnqueueWorkload(NetworkId networkId, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 250 | const InputTensors& inputTensors, |
| 251 | const OutputTensors& outputTensors) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 252 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 253 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 254 | |
| 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 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 265 | return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 268 | void Runtime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) |
| 269 | { |
| 270 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
| 271 | loadedNetwork->RegisterDebugCallback(func); |
| 272 | } |
| 273 | |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 274 | void 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 Martincigh | 0c2b289 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 283 | m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects); |
| 284 | |
| 285 | // Register the dynamic backends in the backend registry |
Matteo Martincigh | 8953390 | 2019-08-15 12:08:06 +0100 | [diff] [blame] | 286 | BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends); |
| 287 | |
| 288 | // Add the registered dynamic backend ids to the list of supported backends |
Narumol Prangnawarat | 60a20fb | 2019-12-09 17:24:41 +0000 | [diff] [blame] | 289 | m_DeviceSpec.AddSupportedBackends(registeredBackendIds, true); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 290 | } |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 291 | |
| 292 | } // namespace armnn |