Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. 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> |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 10 | #include <armnn/utility/Timer.hpp> |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 11 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 12 | #include <armnn/backends/IBackendContext.hpp> |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 13 | #include <backendsCommon/DynamicBackendUtils.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 14 | #include <armnn/utility/PolymorphicDowncast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | |
Nikhil Raj | 77fe76b | 2021-06-09 14:55:32 +0100 | [diff] [blame^] | 16 | #include <common/include/LabelsAndEventClasses.hpp> |
| 17 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 18 | #include <iostream> |
| 19 | |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 20 | #include <backends/BackendProfiling.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace armnn; |
| 23 | using namespace std; |
| 24 | |
| 25 | namespace armnn |
| 26 | { |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 27 | IRuntime::IRuntime() : pRuntimeImpl( new RuntimeImpl(armnn::IRuntime::CreationOptions())) {} |
| 28 | |
| 29 | IRuntime::IRuntime(const IRuntime::CreationOptions& options) : pRuntimeImpl(new RuntimeImpl(options)) {} |
| 30 | |
| 31 | IRuntime::~IRuntime() = default; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | |
| 33 | IRuntime* IRuntime::CreateRaw(const CreationOptions& options) |
| 34 | { |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 35 | return new IRuntime(options); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | IRuntimePtr IRuntime::Create(const CreationOptions& options) |
| 39 | { |
| 40 | return IRuntimePtr(CreateRaw(options), &IRuntime::Destroy); |
| 41 | } |
| 42 | |
| 43 | void IRuntime::Destroy(IRuntime* runtime) |
| 44 | { |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 45 | delete runtime; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 48 | Status IRuntime::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) |
| 49 | { |
| 50 | return pRuntimeImpl->LoadNetwork(networkIdOut, std::move(network)); |
| 51 | } |
| 52 | |
| 53 | Status IRuntime::LoadNetwork(NetworkId& networkIdOut, |
| 54 | IOptimizedNetworkPtr network, |
| 55 | std::string& errorMessage) |
| 56 | { |
| 57 | return pRuntimeImpl->LoadNetwork(networkIdOut, std::move(network), errorMessage); |
| 58 | } |
| 59 | |
| 60 | Status IRuntime::LoadNetwork(NetworkId& networkIdOut, |
| 61 | IOptimizedNetworkPtr network, |
| 62 | std::string& errorMessage, |
| 63 | const INetworkProperties& networkProperties) |
| 64 | { |
| 65 | return pRuntimeImpl->LoadNetwork(networkIdOut, std::move(network), errorMessage, networkProperties); |
| 66 | } |
| 67 | |
| 68 | TensorInfo IRuntime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const |
| 69 | { |
| 70 | return pRuntimeImpl->GetInputTensorInfo(networkId, layerId); |
| 71 | } |
| 72 | |
| 73 | TensorInfo IRuntime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const |
| 74 | { |
| 75 | return pRuntimeImpl->GetOutputTensorInfo(networkId, layerId); |
| 76 | } |
| 77 | |
| 78 | Status IRuntime::EnqueueWorkload(NetworkId networkId, |
| 79 | const InputTensors& inputTensors, |
| 80 | const OutputTensors& outputTensors) |
| 81 | { |
| 82 | return pRuntimeImpl->EnqueueWorkload(networkId, inputTensors, outputTensors); |
| 83 | } |
| 84 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 85 | Status IRuntime::Execute(IWorkingMemHandle& workingMemHandle, |
| 86 | const InputTensors& inputTensors, |
| 87 | const OutputTensors& outputTensors) |
| 88 | { |
| 89 | return pRuntimeImpl->Execute(workingMemHandle, inputTensors, outputTensors); |
| 90 | } |
| 91 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 92 | void IRuntime::Schedule(NetworkId networkId, |
| 93 | const InputTensors& inputTensors, |
| 94 | const OutputTensors& outputTensors, |
| 95 | const QosExecPriority priority, |
| 96 | std::shared_ptr<IAsyncExecutionCallback> cb) |
| 97 | { |
| 98 | pRuntimeImpl->Schedule(networkId, inputTensors, outputTensors, priority, cb); |
| 99 | } |
| 100 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 101 | Status IRuntime::UnloadNetwork(NetworkId networkId) |
| 102 | { |
| 103 | return pRuntimeImpl->UnloadNetwork(networkId); |
| 104 | } |
| 105 | |
| 106 | const IDeviceSpec& IRuntime::GetDeviceSpec() const |
| 107 | { |
| 108 | return pRuntimeImpl->GetDeviceSpec(); |
| 109 | } |
| 110 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 111 | std::unique_ptr<IWorkingMemHandle> IRuntime::CreateWorkingMemHandle(NetworkId networkId) |
| 112 | { |
| 113 | return pRuntimeImpl->CreateWorkingMemHandle(networkId); |
| 114 | } |
| 115 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 116 | const std::shared_ptr<IProfiler> IRuntime::GetProfiler(NetworkId networkId) const |
| 117 | { |
| 118 | return pRuntimeImpl->GetProfiler(networkId); |
| 119 | } |
| 120 | |
| 121 | void IRuntime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) |
| 122 | { |
| 123 | return pRuntimeImpl->RegisterDebugCallback(networkId, func); |
| 124 | } |
| 125 | |
| 126 | int RuntimeImpl::GenerateNetworkId() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 127 | { |
| 128 | return m_NetworkIdCounter++; |
| 129 | } |
| 130 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 131 | Status RuntimeImpl::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr inNetwork) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 132 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | std::string ignoredErrorMessage; |
| 134 | return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage); |
| 135 | } |
| 136 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 137 | Status RuntimeImpl::LoadNetwork(NetworkId& networkIdOut, |
| 138 | IOptimizedNetworkPtr inNetwork, |
| 139 | std::string& errorMessage) |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 140 | { |
Francis Murtagh | 73d3e2e | 2021-04-29 14:23:04 +0100 | [diff] [blame] | 141 | INetworkProperties networkProperties(false, MemorySource::Undefined, MemorySource::Undefined); |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 142 | return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties); |
| 143 | } |
| 144 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 145 | Status RuntimeImpl::LoadNetwork(NetworkId& networkIdOut, |
| 146 | IOptimizedNetworkPtr inNetwork, |
| 147 | std::string& errorMessage, |
| 148 | const INetworkProperties& networkProperties) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 149 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 150 | IOptimizedNetwork* rawNetwork = inNetwork.release(); |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 151 | |
| 152 | networkIdOut = GenerateNetworkId(); |
| 153 | |
| 154 | for (auto&& context : m_BackendContexts) |
| 155 | { |
| 156 | context.second->BeforeLoadNetwork(networkIdOut); |
| 157 | } |
| 158 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 159 | unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork( |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 160 | std::unique_ptr<IOptimizedNetwork>(rawNetwork), |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 161 | errorMessage, |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 162 | networkProperties, |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 163 | m_ProfilingService, |
| 164 | networkIdOut); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 165 | |
| 166 | if (!loadedNetwork) |
| 167 | { |
| 168 | return Status::Failure; |
| 169 | } |
| 170 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 171 | { |
| 172 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 173 | |
| 174 | // Stores the network |
| 175 | m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork); |
| 176 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 177 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 178 | for (auto&& context : m_BackendContexts) |
| 179 | { |
| 180 | context.second->AfterLoadNetwork(networkIdOut); |
| 181 | } |
| 182 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 183 | if (m_ProfilingService.IsProfilingEnabled()) |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 184 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 185 | m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_LOADS); |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 186 | } |
| 187 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 188 | return Status::Success; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 191 | Status RuntimeImpl::UnloadNetwork(NetworkId networkId) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 192 | { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 193 | bool unloadOk = true; |
| 194 | for (auto&& context : m_BackendContexts) |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 195 | { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 196 | unloadOk &= context.second->BeforeUnloadNetwork(networkId); |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 197 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 198 | |
| 199 | if (!unloadOk) |
| 200 | { |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 201 | ARMNN_LOG(warning) << "RuntimeImpl::UnloadNetwork(): failed to unload " |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 202 | "network with ID:" << networkId << " because BeforeUnloadNetwork failed"; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 203 | return Status::Failure; |
| 204 | } |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 205 | |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 206 | std::unique_ptr<profiling::TimelineUtilityMethods> timelineUtils = |
| 207 | profiling::TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 208 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 209 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 210 | |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 211 | // If timeline recording is on mark the Network end of life |
| 212 | if (timelineUtils) |
| 213 | { |
| 214 | auto search = m_LoadedNetworks.find(networkId); |
| 215 | if (search != m_LoadedNetworks.end()) |
| 216 | { |
| 217 | profiling::ProfilingGuid networkGuid = search->second->GetNetworkGuid(); |
| 218 | timelineUtils->RecordEvent(networkGuid, |
| 219 | profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS); |
| 220 | } |
| 221 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 222 | if (m_LoadedNetworks.erase(networkId) == 0) |
| 223 | { |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 224 | ARMNN_LOG(warning) << "WARNING: RuntimeImpl::UnloadNetwork(): " << networkId << " not found!"; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 225 | return Status::Failure; |
| 226 | } |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 227 | |
| 228 | if (m_ProfilingService.IsProfilingEnabled()) |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 229 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 230 | m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS); |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 231 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 232 | } |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 233 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 234 | for (auto&& context : m_BackendContexts) |
| 235 | { |
| 236 | context.second->AfterUnloadNetwork(networkId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 239 | ARMNN_LOG(debug) << "RuntimeImpl::UnloadNetwork(): Unloaded network with ID: " << networkId; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 240 | return Status::Success; |
| 241 | } |
| 242 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 243 | const std::shared_ptr<IProfiler> RuntimeImpl::GetProfiler(NetworkId networkId) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 244 | { |
| 245 | auto it = m_LoadedNetworks.find(networkId); |
| 246 | if (it != m_LoadedNetworks.end()) |
| 247 | { |
| 248 | auto& loadedNetwork = it->second; |
| 249 | return loadedNetwork->GetProfiler(); |
| 250 | } |
| 251 | |
| 252 | return nullptr; |
| 253 | } |
| 254 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 255 | void RuntimeImpl::ReportStructure() // armnn::profiling::IProfilingService& profilingService as param |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 256 | { |
| 257 | // No-op for the time being, but this may be useful in future to have the profilingService available |
| 258 | // if (profilingService.IsProfilingEnabled()){} |
| 259 | |
| 260 | LoadedNetworks::iterator it = m_LoadedNetworks.begin(); |
| 261 | while (it != m_LoadedNetworks.end()) |
| 262 | { |
| 263 | auto& loadedNetwork = it->second; |
| 264 | loadedNetwork->SendNetworkStructure(); |
| 265 | // Increment the Iterator to point to next entry |
| 266 | it++; |
| 267 | } |
| 268 | } |
| 269 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 270 | RuntimeImpl::RuntimeImpl(const IRuntime::CreationOptions& options) |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 271 | : m_NetworkIdCounter(0), |
| 272 | m_ProfilingService(*this) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 273 | { |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 274 | const auto start_time = armnn::GetTimeNow(); |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 275 | ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n"; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 276 | |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 277 | if ( options.m_ProfilingOptions.m_TimelineEnabled && !options.m_ProfilingOptions.m_EnableProfiling ) |
| 278 | { |
| 279 | throw RuntimeException("It is not possible to enable timeline reporting without profiling being enabled"); |
| 280 | } |
| 281 | |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 282 | // Load any available/compatible dynamic backend before the runtime |
| 283 | // goes through the backend registry |
| 284 | LoadDynamicBackends(options.m_DynamicBackendsPath); |
| 285 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 286 | BackendIdSet supportedBackends; |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 287 | for (const auto& id : BackendRegistryInstance().GetBackendIds()) |
| 288 | { |
| 289 | // Store backend contexts for the supported ones |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 290 | try { |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 291 | auto factoryFun = BackendRegistryInstance().GetFactory(id); |
| 292 | auto backend = factoryFun(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 293 | ARMNN_ASSERT(backend.get() != nullptr); |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 294 | |
| 295 | auto context = backend->CreateBackendContext(options); |
| 296 | |
| 297 | // backends are allowed to return nullptrs if they |
| 298 | // don't wish to create a backend specific context |
| 299 | if (context) |
| 300 | { |
| 301 | m_BackendContexts.emplace(std::make_pair(id, std::move(context))); |
| 302 | } |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 303 | supportedBackends.emplace(id); |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 304 | |
| 305 | unique_ptr<armnn::profiling::IBackendProfiling> profilingIface = |
| 306 | std::make_unique<armnn::profiling::BackendProfiling>(armnn::profiling::BackendProfiling( |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 307 | options, m_ProfilingService, id)); |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 308 | |
| 309 | // Backends may also provide a profiling context. Ask for it now. |
| 310 | auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface); |
| 311 | // Backends that don't support profiling will return a null profiling context. |
| 312 | if (profilingContext) |
| 313 | { |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 314 | // Pass the context onto the profiling service. |
| 315 | m_ProfilingService.AddBackendProfilingContext(id, profilingContext); |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 316 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 317 | } |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 318 | catch (const BackendUnavailableException&) |
| 319 | { |
| 320 | // Ignore backends which are unavailable |
| 321 | } |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 322 | } |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 323 | |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 324 | BackendRegistryInstance().SetProfilingService(m_ProfilingService); |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 325 | // pass configuration info to the profiling service |
| 326 | m_ProfilingService.ConfigureProfilingService(options.m_ProfilingOptions); |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 327 | if (options.m_ProfilingOptions.m_EnableProfiling) |
| 328 | { |
| 329 | // try to wait for the profiling service to initialise |
| 330 | m_ProfilingService.WaitForProfilingServiceActivation(3000); |
| 331 | } |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 332 | |
Matthew Bentham | 9a61fa6 | 2020-02-04 10:03:55 +0000 | [diff] [blame] | 333 | m_DeviceSpec.AddSupportedBackends(supportedBackends); |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 334 | |
| 335 | ARMNN_LOG(info) << "Initialization time: " << std::setprecision(2) |
| 336 | << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n"; |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 339 | RuntimeImpl::~RuntimeImpl() |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 340 | { |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 341 | const auto start_time = armnn::GetTimeNow(); |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 342 | std::vector<int> networkIDs; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 343 | try |
| 344 | { |
| 345 | // Coverity fix: The following code may throw an exception of type std::length_error. |
| 346 | std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(), |
| 347 | std::back_inserter(networkIDs), |
| 348 | [](const auto &pair) { return pair.first; }); |
| 349 | } |
| 350 | catch (const std::exception& e) |
| 351 | { |
| 352 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 353 | // exception of type std::length_error. |
| 354 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 355 | std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what() |
| 356 | << "\nSome of the loaded networks may not be unloaded" << std::endl; |
| 357 | } |
| 358 | // We then proceed to unload all the networks which IDs have been appended to the list |
| 359 | // up to the point the exception was thrown (if any). |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 360 | |
| 361 | for (auto networkID : networkIDs) |
| 362 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 363 | try |
| 364 | { |
| 365 | // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error, |
| 366 | // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error |
| 367 | UnloadNetwork(networkID); |
| 368 | } |
| 369 | catch (const std::exception& e) |
| 370 | { |
| 371 | // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an |
| 372 | // exception of type std::length_error. |
| 373 | // Using stderr instead in this context as there is no point in nesting try-catch blocks here. |
| 374 | std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what() |
| 375 | << std::endl; |
| 376 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 377 | } |
Narumol Prangnawarat | 60a20fb | 2019-12-09 17:24:41 +0000 | [diff] [blame] | 378 | |
| 379 | // Clear all dynamic backends. |
| 380 | DynamicBackendUtils::DeregisterDynamicBackends(m_DeviceSpec.GetDynamicBackends()); |
| 381 | m_DeviceSpec.ClearDynamicBackends(); |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 382 | m_BackendContexts.clear(); |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 383 | |
| 384 | BackendRegistryInstance().SetProfilingService(armnn::EmptyOptional()); |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 385 | ARMNN_LOG(info) << "Shutdown time: " << std::setprecision(2) |
| 386 | << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n"; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 389 | LoadedNetwork* RuntimeImpl::GetLoadedNetworkPtr(NetworkId networkId) const |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 390 | { |
| 391 | std::lock_guard<std::mutex> lockGuard(m_Mutex); |
| 392 | return m_LoadedNetworks.at(networkId).get(); |
| 393 | } |
| 394 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 395 | TensorInfo RuntimeImpl::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 396 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 397 | return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 400 | TensorInfo RuntimeImpl::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 401 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 402 | return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 405 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 406 | Status RuntimeImpl::EnqueueWorkload(NetworkId networkId, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 407 | const InputTensors& inputTensors, |
| 408 | const OutputTensors& outputTensors) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 409 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 410 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 411 | |
| 412 | if (!loadedNetwork) |
| 413 | { |
| 414 | ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n"; |
| 415 | return Status::Failure; |
| 416 | } |
| 417 | if (loadedNetwork->IsAsyncEnabled()) |
| 418 | { |
| 419 | ARMNN_LOG(error) << "Network " << networkId << " is async enabled.\n"; |
| 420 | return Status::Failure; |
| 421 | } |
Narumol Prangnawarat | 5b4d0d5 | 2020-06-23 11:45:56 +0100 | [diff] [blame] | 422 | ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get()); |
| 423 | |
| 424 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "EnqueueWorkload"); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 425 | |
| 426 | static thread_local NetworkId lastId = networkId; |
| 427 | if (lastId != networkId) |
| 428 | { |
| 429 | LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network) |
| 430 | { |
| 431 | network->FreeWorkingMemory(); |
| 432 | }); |
| 433 | } |
| 434 | lastId=networkId; |
| 435 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 436 | return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 439 | Status RuntimeImpl::Execute(IWorkingMemHandle& iWorkingMemHandle, |
| 440 | const InputTensors& inputTensors, |
| 441 | const OutputTensors& outputTensors) |
| 442 | { |
| 443 | NetworkId networkId = iWorkingMemHandle.GetNetworkId(); |
| 444 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
| 445 | |
| 446 | if (!loadedNetwork) |
| 447 | { |
| 448 | ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n"; |
| 449 | return Status::Failure; |
| 450 | } |
| 451 | if (!loadedNetwork->IsAsyncEnabled()) |
| 452 | { |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 453 | ARMNN_LOG(error) << "Attempting execute " << networkId << " when it is not async enabled.\n"; |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 454 | return Status::Failure; |
| 455 | } |
| 456 | ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get()); |
| 457 | |
| 458 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Execute"); |
| 459 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 460 | return loadedNetwork->Execute(inputTensors, outputTensors, iWorkingMemHandle); |
| 461 | } |
| 462 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 463 | void RuntimeImpl::Schedule(NetworkId networkId, |
| 464 | const InputTensors& inputTensors, |
| 465 | const OutputTensors& outputTensors, |
| 466 | const QosExecPriority priority, |
| 467 | std::shared_ptr<IAsyncExecutionCallback> callback) |
| 468 | { |
| 469 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
| 470 | |
| 471 | if (!loadedNetwork) |
| 472 | { |
| 473 | throw armnn::Exception( |
| 474 | "Network with ID of " + std::to_string(networkId) + " does not exist \n" |
| 475 | ); |
| 476 | } |
| 477 | if (!loadedNetwork->IsAsyncEnabled()) |
| 478 | { |
| 479 | throw armnn::Exception( |
| 480 | "Attempting to schedule Network " + std::to_string(networkId) + " when it is not async enabled \n" |
| 481 | ); |
| 482 | } |
| 483 | |
| 484 | ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get()); |
| 485 | |
| 486 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Schedule"); |
| 487 | |
| 488 | loadedNetwork->Schedule(inputTensors, outputTensors, priority, callback); |
| 489 | } |
| 490 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 491 | /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have |
| 492 | /// overlapped Execution by calling this function from different threads. |
| 493 | std::unique_ptr<IWorkingMemHandle> RuntimeImpl::CreateWorkingMemHandle(NetworkId networkId) |
| 494 | { |
| 495 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
| 496 | |
| 497 | if (!loadedNetwork) |
| 498 | { |
| 499 | ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n"; |
| 500 | return nullptr; |
| 501 | } |
| 502 | if (!loadedNetwork->IsAsyncEnabled()) |
| 503 | { |
| 504 | ARMNN_LOG(error) << "Network " << networkId << " is not async enabled.\n"; |
| 505 | return nullptr; |
| 506 | } |
| 507 | ProfilerManager::GetInstance().RegisterProfiler(loadedNetwork->GetProfiler().get()); |
| 508 | |
| 509 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "CreateWorkingMemHandle"); |
| 510 | |
| 511 | static thread_local NetworkId lastId = networkId; |
| 512 | if (lastId != networkId) |
| 513 | { |
| 514 | LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network) |
| 515 | { |
| 516 | network->FreeWorkingMemory(); |
| 517 | }); |
| 518 | } |
| 519 | lastId=networkId; |
| 520 | |
| 521 | return loadedNetwork->CreateWorkingMemHandle(networkId); |
| 522 | } |
| 523 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 524 | void RuntimeImpl::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 525 | { |
| 526 | LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId); |
| 527 | loadedNetwork->RegisterDebugCallback(func); |
| 528 | } |
| 529 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 530 | void RuntimeImpl::LoadDynamicBackends(const std::string& overrideBackendPath) |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 531 | { |
| 532 | // Get the paths where to load the dynamic backends from |
| 533 | std::vector<std::string> backendPaths = DynamicBackendUtils::GetBackendPaths(overrideBackendPath); |
| 534 | |
| 535 | // Get the shared objects to try to load as dynamic backends |
| 536 | std::vector<std::string> sharedObjects = DynamicBackendUtils::GetSharedObjects(backendPaths); |
| 537 | |
| 538 | // Create a list of dynamic backends |
Matteo Martincigh | 0c2b289 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 539 | m_DynamicBackends = DynamicBackendUtils::CreateDynamicBackends(sharedObjects); |
| 540 | |
| 541 | // Register the dynamic backends in the backend registry |
Matteo Martincigh | 8953390 | 2019-08-15 12:08:06 +0100 | [diff] [blame] | 542 | BackendIdSet registeredBackendIds = DynamicBackendUtils::RegisterDynamicBackends(m_DynamicBackends); |
| 543 | |
| 544 | // Add the registered dynamic backend ids to the list of supported backends |
Narumol Prangnawarat | 60a20fb | 2019-12-09 17:24:41 +0000 | [diff] [blame] | 545 | m_DeviceSpec.AddSupportedBackends(registeredBackendIds, true); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 546 | } |
Matteo Martincigh | e54aa06 | 2019-08-05 14:12:11 +0100 | [diff] [blame] | 547 | |
| 548 | } // namespace armnn |