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