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