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