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