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 | |
| 6 | #include "LoadedNetwork.hpp" |
| 7 | #include "Layer.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | #include "Graph.hpp" |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 9 | #include <Processes.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | #include "Profiling.hpp" |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 11 | #include "HeapProfiling.hpp" |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 12 | #include "WorkingMemHandle.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 14 | #include <armnn/BackendRegistry.hpp> |
Matthew Bentham | f48afc6 | 2020-01-15 17:55:08 +0000 | [diff] [blame] | 15 | #include <armnn/Logging.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 16 | #include <armnn/utility/Assert.hpp> |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 17 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 18 | #include <backendsCommon/TensorHandle.hpp> |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 19 | #include <armnn/backends/IMemoryManager.hpp> |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 20 | #include <backendsCommon/MemCopyWorkload.hpp> |
| 21 | #include <backendsCommon/MemSyncWorkload.hpp> |
Finn Williams | dbf5f31 | 2021-08-26 11:08:01 +0100 | [diff] [blame] | 22 | #include <armnn/BackendHelper.hpp> |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 23 | |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 24 | #include <fmt/format.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 25 | |
| 26 | namespace armnn |
| 27 | { |
| 28 | |
| 29 | using namespace std; |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 30 | using namespace armnn::profiling; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 31 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | namespace |
| 33 | { |
| 34 | |
| 35 | template <typename ExceptionType> |
| 36 | std::string ToErrorMessage(const char * prefix, const ExceptionType & error) |
| 37 | { |
| 38 | std::stringstream ss; |
| 39 | ss << prefix << " " << error.what(); |
| 40 | return ss.str(); |
| 41 | } |
| 42 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 43 | void AddLayerStructure(std::unique_ptr<TimelineUtilityMethods>& timelineUtils, |
| 44 | const Layer& layer, |
| 45 | ProfilingGuid networkGuid) |
| 46 | { |
| 47 | // Add layer to the post-optimisation network structure |
| 48 | std::string layerName = layer.GetNameStr().empty() ? "<Unnamed>" : layer.GetNameStr(); |
| 49 | timelineUtils->CreateNamedTypedChildEntity(layer.GetGuid(), |
| 50 | networkGuid, |
| 51 | layerName, |
| 52 | LabelsAndEventClasses::LAYER_GUID); |
| 53 | for (auto&& input : layer.GetInputSlots()) |
| 54 | { |
| 55 | const IOutputSlot* source = input.GetConnectedOutputSlot(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 56 | ARMNN_ASSERT(source != NULL); |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 57 | timelineUtils->CreateConnectionRelationship(ProfilingRelationshipType::RetentionLink, |
| 58 | source->GetOwningLayerGuid(), |
| 59 | layer.GetGuid()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void AddWorkloadStructure(std::unique_ptr<TimelineUtilityMethods>& timelineUtils, |
| 64 | std::unique_ptr<IWorkload>& workload, |
| 65 | const Layer& layer) |
| 66 | { |
| 67 | // Add workload to the post-optimisation network structure |
| 68 | timelineUtils->CreateTypedEntity(workload->GetGuid(), LabelsAndEventClasses::WORKLOAD_GUID); |
| 69 | timelineUtils->MarkEntityWithLabel(workload->GetGuid(), |
| 70 | layer.GetBackendId().Get(), |
| 71 | LabelsAndEventClasses::BACKENDID_GUID); |
| 72 | |
| 73 | // Link the workload to the layer |
| 74 | timelineUtils->CreateRelationship(ProfilingRelationshipType::RetentionLink, |
| 75 | layer.GetGuid(), |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 76 | workload->GetGuid(), |
| 77 | LabelsAndEventClasses::CHILD_GUID); |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 78 | } |
| 79 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | } // anonymous |
| 81 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 82 | std::unique_ptr<LoadedNetwork> LoadedNetwork::MakeLoadedNetwork(std::unique_ptr<IOptimizedNetwork> net, |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 83 | std::string& errorMessage, |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 84 | const INetworkProperties& networkProperties, |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 85 | profiling::ProfilingService& profilingService) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 86 | { |
| 87 | std::unique_ptr<LoadedNetwork> loadedNetwork; |
| 88 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 89 | auto Fail = [&](const std::exception& error) -> std::unique_ptr<LoadedNetwork> |
| 90 | { |
| 91 | errorMessage = ToErrorMessage("An error occurred when preparing the network workloads: ", error); |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 92 | ARMNN_LOG(error) << errorMessage; |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 93 | |
| 94 | return std::unique_ptr<LoadedNetwork>(); |
| 95 | }; |
| 96 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | try |
| 98 | { |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 99 | loadedNetwork.reset(new LoadedNetwork(std::move(net), networkProperties, profilingService)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 100 | } |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 101 | catch (const armnn::RuntimeException& error) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 102 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 103 | return Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 104 | } |
| 105 | catch (const armnn::Exception& error) |
| 106 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 107 | return Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 108 | } |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 109 | catch (const std::runtime_error& error) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 110 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 111 | return Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 112 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 113 | |
| 114 | return loadedNetwork; |
| 115 | } |
| 116 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 117 | LoadedNetwork::LoadedNetwork(std::unique_ptr<IOptimizedNetwork> net, |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 118 | const INetworkProperties& networkProperties, |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 119 | profiling::ProfilingService& profilingService) : |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 120 | m_OptimizedNetwork(std::move(net)), |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 121 | m_NetworkProperties(networkProperties), |
Narumol Prangnawarat | 549cb7a | 2020-07-10 17:50:53 +0100 | [diff] [blame] | 122 | m_TensorHandleFactoryRegistry(), |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 123 | m_ProfilingService(profilingService) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 124 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 125 | // Create a profiler and register it for the current thread. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 126 | m_Profiler = std::make_shared<IProfiler>(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 127 | ProfilerManager::GetInstance().RegisterProfiler(m_Profiler.get()); |
| 128 | |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 129 | m_Profiler->EnableProfiling(networkProperties.m_ProfilingEnabled); |
| 130 | |
Keith Davis | 4914d0c | 2021-08-18 17:14:05 +0100 | [diff] [blame] | 131 | m_Profiler->EnableNetworkDetailsToStdOut(networkProperties.m_OutputNetworkDetailsMethod); |
Keith Davis | f487486 | 2021-08-09 16:49:18 +0100 | [diff] [blame] | 132 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 133 | Graph& order = m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph().TopologicalSort(); |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 134 | //First create tensor handlers, backends and workload factories. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | //Handlers are created before workloads are. |
| 136 | //Because workload creation can modify some of the handlers, |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 137 | //(for example the splitter and concat layers). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 138 | for (auto&& layer : order) |
| 139 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 140 | auto const& backendId = layer->GetBackendId(); |
| 141 | if (m_Backends.count(backendId) == 0) |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 142 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 143 | auto createBackend = BackendRegistryInstance().GetFactory(backendId); |
| 144 | auto it = m_Backends.emplace(std::make_pair(backendId, createBackend())); |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 145 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 146 | IBackendInternal* backend = it.first->second.get(); |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 147 | |
Finn Williams | dbf5f31 | 2021-08-26 11:08:01 +0100 | [diff] [blame] | 148 | if (networkProperties.m_AsyncEnabled && |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 149 | !HasCapability(BackendOptions::BackendOption{"AsyncExecution", true}, backend->GetCapabilities())) |
Finn Williams | dbf5f31 | 2021-08-26 11:08:01 +0100 | [diff] [blame] | 150 | { |
| 151 | std::string er = backend->GetId(); |
| 152 | er += " does not support AsyncExecution"; |
| 153 | throw BackendCapabilityException(er); |
| 154 | } |
| 155 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 156 | if (backend->SupportsTensorAllocatorAPI()) |
| 157 | { |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 158 | auto workloadFactory = backend->CreateWorkloadFactory( |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 159 | m_TensorHandleFactoryRegistry, |
| 160 | m_OptimizedNetwork->pOptimizedNetworkImpl->GetModelOptions(), |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 161 | static_cast<MemorySourceFlags>(m_NetworkProperties.m_InputSource), |
| 162 | static_cast<MemorySourceFlags>(m_NetworkProperties.m_OutputSource)); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 163 | m_WorkloadFactories.emplace( |
| 164 | std::make_pair(backendId, std::make_pair(std::move(workloadFactory), nullptr))); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | IBackendInternal::IMemoryManagerSharedPtr memoryManager = backend->CreateMemoryManager(); |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 169 | auto workloadFactory = backend->CreateWorkloadFactory( |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 170 | memoryManager, m_OptimizedNetwork->pOptimizedNetworkImpl->GetModelOptions()); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 171 | |
| 172 | m_WorkloadFactories.emplace( |
| 173 | std::make_pair(backendId, std::make_pair(std::move(workloadFactory), memoryManager))); |
| 174 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 175 | } |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 176 | } |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 177 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 178 | if (!networkProperties.m_AsyncEnabled) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 179 | { |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 180 | for (auto&& layer : order) |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 181 | { |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 182 | auto& workloadFactory = GetWorkloadFactory(*layer); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 183 | |
| 184 | switch (layer->GetType()) |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 185 | { |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 186 | case LayerType::Input: |
| 187 | case LayerType::MemImport: |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 188 | { |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 189 | // If IsImportEnabled is true then we need to set IsMemoryManaged |
| 190 | // to false when creating TensorHandles |
Francis Murtagh | 73d3e2e | 2021-04-29 14:23:04 +0100 | [diff] [blame] | 191 | layer->CreateTensorHandles(m_TensorHandleFactoryRegistry, |
| 192 | workloadFactory, |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 193 | !m_NetworkProperties.m_ImportEnabled); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 194 | break; |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 195 | } |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 196 | default: |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 197 | { |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 198 | // Look for a layer with 1 OutputSlot which has 1 connection and that connection is an Output Layer |
| 199 | // If Export is enabled disable memory management so we can export, otherwise we do a copy |
| 200 | if ((layer->GetNumOutputSlots() == 1) && |
| 201 | (layer->GetOutputSlots()[0].GetNumConnections() == 1) && |
| 202 | (layer->GetOutputSlots()[0].GetConnection(0)->GetOwningLayer().GetType() == LayerType::Output)) |
| 203 | { |
Francis Murtagh | 73d3e2e | 2021-04-29 14:23:04 +0100 | [diff] [blame] | 204 | layer->CreateTensorHandles(m_TensorHandleFactoryRegistry, |
| 205 | workloadFactory, |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 206 | !m_NetworkProperties.m_ExportEnabled); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 207 | } |
| 208 | else |
| 209 | { |
| 210 | layer->CreateTensorHandles(m_TensorHandleFactoryRegistry, workloadFactory); |
| 211 | } |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 212 | } |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 213 | } |
| 214 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 217 | ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid(); |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 218 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 219 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 220 | if (timelineUtils) |
| 221 | { |
| 222 | timelineUtils->CreateTypedEntity(networkGuid, LabelsAndEventClasses::NETWORK_GUID); |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 223 | // Mark the network with a start of life event |
| 224 | timelineUtils->RecordEvent(networkGuid, LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS); |
| 225 | // and with the process ID |
| 226 | int processID = armnnUtils::Processes::GetCurrentId(); |
| 227 | std::stringstream ss; |
| 228 | ss << processID; |
| 229 | timelineUtils->MarkEntityWithLabel(networkGuid, ss.str(), LabelsAndEventClasses::PROCESS_ID_GUID); |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 230 | } |
| 231 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 232 | //Then create workloads. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 233 | for (auto&& layer : order) |
| 234 | { |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 235 | if (timelineUtils) |
| 236 | { |
| 237 | // Add layer to the post-optimisation network structure |
| 238 | AddLayerStructure(timelineUtils, *layer, networkGuid); |
| 239 | } |
| 240 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 241 | const IWorkloadFactory& workloadFactory = GetWorkloadFactory(*layer); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 242 | |
| 243 | switch (layer->GetType()) |
| 244 | { |
| 245 | case LayerType::Input: |
| 246 | case LayerType::Output: |
| 247 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 248 | // Inputs and outputs are treated in a special way - see EnqueueInput() and EnqueueOutput(). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | default: |
| 252 | { |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 253 | auto workload = layer->CreateWorkload(workloadFactory); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 254 | |
| 255 | if (!workload) |
| 256 | { |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 257 | const char* const layerName = |
| 258 | layer->GetNameStr().length() != 0 ? layer->GetName() : "<Unnamed>"; |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 259 | throw InvalidArgumentException( |
| 260 | fmt::format("No workload created for layer (name: '{0}' type: '{1}') (compute '{2}')", |
| 261 | layerName, static_cast<int>(layer->GetType()), layer->GetBackendId().Get() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 262 | )); |
| 263 | } |
| 264 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 265 | if (timelineUtils) |
| 266 | { |
| 267 | // Add workload to the post-optimisation network structure |
| 268 | AddWorkloadStructure(timelineUtils, workload, *layer); |
| 269 | } |
| 270 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 271 | // For async networks ConstantWorkloads are managed exclusively by LoadedNetwork |
| 272 | // and are separated out from the other workloads |
| 273 | if (networkProperties.m_AsyncEnabled && layer->GetType() == LayerType::Constant) |
| 274 | { |
| 275 | m_ConstantWorkloads[layer->GetGuid()] = std::move(workload); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | m_WorkloadQueue.push_back(move(workload)); |
| 280 | } |
| 281 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 282 | // release the constant data in the layer.. |
| 283 | layer->ReleaseConstantData(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Sadik Armagan | dea8fb6 | 2020-11-26 10:38:11 +0000 | [diff] [blame] | 289 | for (auto&& workloadFactory : m_WorkloadFactories) |
| 290 | { |
| 291 | workloadFactory.second.first->AfterWorkloadsCreated(); |
| 292 | } |
| 293 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 294 | if (timelineUtils) |
| 295 | { |
| 296 | // Commit to send the post-optimisation network structure |
| 297 | timelineUtils->Commit(); |
| 298 | } |
| 299 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 300 | if (!networkProperties.m_AsyncEnabled) |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 301 | { |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 302 | // Set up memory. |
| 303 | m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph().AllocateDynamicBuffers(); |
| 304 | |
| 305 | // Now that the intermediate tensor memory has been set-up, |
| 306 | // do any post allocation configuration for each workload. |
| 307 | for (auto &workload : m_WorkloadQueue) |
| 308 | { |
| 309 | workload->PostAllocationConfigure(); |
| 310 | } |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | AllocateAndExecuteConstantWorkloads(); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 315 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 318 | void LoadedNetwork::AllocateAndExecuteConstantWorkloads() |
| 319 | { |
| 320 | Graph& order = m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph(); |
| 321 | for (auto&& layer : order) |
| 322 | { |
| 323 | if (layer->GetType() == LayerType::Constant) |
| 324 | { |
| 325 | const auto& outSlot = layer->GetOutputSlots()[0]; |
| 326 | const auto factoryId = outSlot.GetTensorHandleFactoryId(); |
| 327 | ARMNN_ASSERT(factoryId != ITensorHandleFactory::LegacyFactoryId); |
| 328 | auto& workloadFactory = GetWorkloadFactory(*layer); |
| 329 | |
| 330 | layer->CreateTensorHandles(m_TensorHandleFactoryRegistry, workloadFactory); |
| 331 | ITensorHandle* tensorHandle = outSlot.GetOutputHandler().GetData(); |
| 332 | |
| 333 | m_ConstantTensorHandles[layer->GetGuid()] = tensorHandle; |
| 334 | tensorHandle->Allocate(); |
| 335 | |
| 336 | WorkingMemDescriptor memDesc; |
| 337 | memDesc.m_Outputs.push_back(tensorHandle); |
| 338 | m_ConstantWorkloads[layer->GetGuid()]->ExecuteAsync(memDesc); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 344 | void LoadedNetwork::SendNetworkStructure() |
| 345 | { |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 346 | Graph& order = m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph().TopologicalSort(); |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 347 | ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid(); |
| 348 | |
| 349 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 350 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
| 351 | |
| 352 | timelineUtils->CreateTypedEntity(networkGuid, LabelsAndEventClasses::NETWORK_GUID); |
| 353 | |
| 354 | for (auto&& layer : order) |
| 355 | { |
| 356 | // Add layer to the post-optimisation network structure |
| 357 | AddLayerStructure(timelineUtils, *layer, networkGuid); |
| 358 | switch (layer->GetType()) |
| 359 | { |
| 360 | case LayerType::Input: |
| 361 | case LayerType::Output: |
| 362 | { |
| 363 | // Inputs and outputs are treated in a special way - see EnqueueInput() and EnqueueOutput(). |
| 364 | break; |
| 365 | } |
| 366 | default: |
| 367 | { |
| 368 | for (auto& workload : m_WorkloadQueue) |
| 369 | { |
| 370 | // Add workload to the post-optimisation network structure |
| 371 | AddWorkloadStructure(timelineUtils, workload, *layer); |
| 372 | } |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | // Commit to send the post-optimisation network structure |
| 378 | timelineUtils->Commit(); |
| 379 | } |
| 380 | |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 381 | profiling::ProfilingGuid LoadedNetwork::GetNetworkGuid() |
| 382 | { |
| 383 | return m_OptimizedNetwork->GetGuid(); |
| 384 | } |
| 385 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 386 | TensorInfo LoadedNetwork::GetInputTensorInfo(LayerBindingId layerId) const |
| 387 | { |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 388 | for (auto&& inputLayer : m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph().GetInputLayers()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 389 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 390 | ARMNN_ASSERT_MSG(inputLayer->GetNumOutputSlots() == 1, "Input layer should have exactly 1 output slot"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 391 | if (inputLayer->GetBindingId() == layerId) |
| 392 | { |
| 393 | return inputLayer->GetOutputSlot(0).GetTensorInfo(); |
| 394 | } |
| 395 | } |
| 396 | |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 397 | throw InvalidArgumentException(fmt::format("No input layer is associated with id {}", layerId)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | TensorInfo LoadedNetwork::GetOutputTensorInfo(LayerBindingId layerId) const |
| 401 | { |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 402 | for (auto&& outputLayer : m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph().GetOutputLayers()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 403 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 404 | ARMNN_ASSERT_MSG(outputLayer->GetNumInputSlots() == 1, "Output layer should have exactly 1 input slot"); |
| 405 | ARMNN_ASSERT_MSG(outputLayer->GetInputSlot(0).GetConnection(), "Input slot on Output layer must be connected"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 406 | if (outputLayer->GetBindingId() == layerId) |
| 407 | { |
| 408 | return outputLayer->GetInputSlot(0).GetConnection()->GetTensorInfo(); |
| 409 | } |
| 410 | } |
| 411 | |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 412 | throw InvalidArgumentException(fmt::format("No output layer is associated with id {}", layerId)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 413 | } |
| 414 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 415 | const IWorkloadFactory& LoadedNetwork::GetWorkloadFactory(const Layer& layer) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 416 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 417 | const IWorkloadFactory* workloadFactory = nullptr; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 418 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 419 | auto it = m_WorkloadFactories.find(layer.GetBackendId()); |
| 420 | if (it == m_WorkloadFactories.end()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 421 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 422 | throw RuntimeException(fmt::format("No workload factory for {0} to be used for layer: {1}", |
| 423 | layer.GetBackendId().Get(), |
| 424 | layer.GetNameStr()), |
| 425 | CHECK_LOCATION()); |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 426 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 427 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 428 | workloadFactory = it->second.first.get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 429 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 430 | ARMNN_ASSERT_MSG(workloadFactory, "No workload factory"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 431 | |
| 432 | std::string reasonIfUnsupported; |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 433 | ARMNN_ASSERT_MSG(IWorkloadFactory::IsLayerSupported(layer, |
| 434 | {}, |
| 435 | reasonIfUnsupported, |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 436 | m_OptimizedNetwork->pOptimizedNetworkImpl->GetModelOptions()), |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 437 | "Factory does not support layer"); |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 438 | IgnoreUnused(reasonIfUnsupported); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 439 | return *workloadFactory; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | namespace { |
| 443 | |
| 444 | // Non-copyable class owning accelerator-specific tensor data. |
| 445 | class TensorPin |
| 446 | { |
| 447 | public: |
| 448 | TensorPin(std::unique_ptr<ITensorHandle> handle, const TensorInfo& info, LayerBindingId id) |
| 449 | : m_TensorHandle(std::move(handle)) |
| 450 | , m_TensorInfo(info) |
| 451 | , m_Id(id) |
| 452 | { |
| 453 | } |
| 454 | |
| 455 | ITensorHandle* GetTensorHandle() const { return m_TensorHandle.get(); } |
| 456 | const TensorInfo& GetTensorInfo() const { return m_TensorInfo; } |
| 457 | LayerBindingId GetBindingId() const { return m_Id; } |
| 458 | |
| 459 | private: |
| 460 | std::unique_ptr<ITensorHandle> m_TensorHandle; |
| 461 | TensorInfo m_TensorInfo; |
| 462 | LayerBindingId m_Id; |
| 463 | }; |
| 464 | |
| 465 | static const TensorPin& GetTensorPin(LayerBindingId id, |
| 466 | const std::vector<TensorPin>& pins, |
| 467 | char const* bindingPointDesc) |
| 468 | { |
| 469 | auto it = std::find_if(pins.begin(), pins.end(), |
| 470 | [id](const TensorPin& pin) |
| 471 | { |
| 472 | return pin.GetBindingId() == id; |
| 473 | }); |
| 474 | |
| 475 | if (it != pins.end()) |
| 476 | { |
| 477 | return *it; |
| 478 | } |
| 479 | else |
| 480 | { |
Colm Donelan | 5b5c222 | 2020-09-09 12:48:16 +0100 | [diff] [blame] | 481 | throw InvalidArgumentException(fmt::format("No tensor supplied for {0} {1}", bindingPointDesc, id)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | // Stores data that needs to be kept accessible for the entire execution of a workload. |
| 486 | class WorkloadData |
| 487 | { |
| 488 | public: |
| 489 | WorkloadData(const InputTensors& inputTensors, const OutputTensors& outputTensors) |
| 490 | { |
| 491 | m_InputTensorPins.reserve(inputTensors.size()); |
| 492 | m_OutputTensorPins.reserve(outputTensors.size()); |
| 493 | |
| 494 | for (auto inputTensorPair : inputTensors) |
| 495 | { |
| 496 | auto inputTensor = inputTensorPair.second; |
| 497 | |
| 498 | std::unique_ptr<ITensorHandle> tensorHandle = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 499 | std::make_unique<ConstPassthroughTensorHandle>(inputTensor.GetInfo(),inputTensor.GetMemoryArea()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 500 | LayerBindingId layerId = inputTensorPair.first; |
| 501 | |
| 502 | m_InputTensorPins.emplace_back(std::move(tensorHandle), inputTensor.GetInfo(), layerId); |
| 503 | } |
| 504 | |
| 505 | for (auto outputTensorPair : outputTensors) |
| 506 | { |
| 507 | auto outputTensor = outputTensorPair.second; |
| 508 | |
| 509 | std::unique_ptr<ITensorHandle> tensorHandle = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 510 | std::make_unique<PassthroughTensorHandle>(outputTensor.GetInfo(), outputTensor.GetMemoryArea()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 511 | LayerBindingId layerId = outputTensorPair.first; |
| 512 | |
| 513 | m_OutputTensorPins.emplace_back(std::move(tensorHandle), outputTensor.GetInfo(), layerId); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | const TensorPin& GetInputTensorPin(LayerBindingId id) const |
| 518 | { |
| 519 | return GetTensorPin(id, m_InputTensorPins, "input"); |
| 520 | } |
| 521 | |
| 522 | const TensorPin& GetOutputTensorPin(LayerBindingId id) const |
| 523 | { |
| 524 | return GetTensorPin(id, m_OutputTensorPins, "output"); |
| 525 | } |
| 526 | |
| 527 | private: |
| 528 | |
| 529 | std::vector<TensorPin> m_InputTensorPins; |
| 530 | std::vector<TensorPin> m_OutputTensorPins; |
| 531 | }; |
| 532 | |
| 533 | } |
| 534 | |
| 535 | Status LoadedNetwork::EnqueueWorkload(const InputTensors& inputTensors, |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 536 | const OutputTensors& outputTensors) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 537 | { |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 538 | const Graph& graph = m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 539 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 540 | // Walk graph to determine the order of execution. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 541 | if (graph.GetNumLayers() < 2) |
| 542 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 543 | ARMNN_LOG(warning) << "IRuntime::EnqueueWorkload()::Less than two nodes in graph"; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 544 | return Status::Failure; |
| 545 | } |
| 546 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 547 | // Data that must be kept alive for the entire execution of the workload. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 548 | WorkloadData workloadData(inputTensors, outputTensors); |
| 549 | |
| 550 | if (graph.GetNumInputs() != inputTensors.size()) |
| 551 | { |
| 552 | throw InvalidArgumentException("Number of inputs provided does not match network."); |
| 553 | } |
| 554 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 555 | // For each input to the network, call EnqueueInput with the data passed by the user. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 556 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 557 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "PrepareInputs"); |
| 558 | m_InputQueue.clear(); |
| 559 | m_InputQueue.reserve(graph.GetNumInputs()); |
| 560 | for (const BindableLayer* inputLayer : graph.GetInputLayers()) |
| 561 | { |
| 562 | const TensorPin& pin = workloadData.GetInputTensorPin(inputLayer->GetBindingId()); |
| 563 | EnqueueInput(*inputLayer, pin.GetTensorHandle(), pin.GetTensorInfo()); |
| 564 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 565 | } |
| 566 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 567 | // For each output to the network, call EnqueueOutput with the data passed by the user. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 568 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 569 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "PrepareOutputs"); |
| 570 | m_OutputQueue.clear(); |
| 571 | m_OutputQueue.reserve(graph.GetNumOutputs()); |
| 572 | for (const BindableLayer* outputLayer : graph.GetOutputLayers()) |
| 573 | { |
| 574 | const TensorPin& pin = workloadData.GetOutputTensorPin(outputLayer->GetBindingId()); |
| 575 | EnqueueOutput(*outputLayer, pin.GetTensorHandle(), pin.GetTensorInfo()); |
| 576 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 579 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 580 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
| 581 | ProfilingGuid inferenceGuid = m_ProfilingService.GetNextGuid(); |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 582 | if (timelineUtils) |
| 583 | { |
| 584 | // Add inference timeline trace if profiling is enabled. |
| 585 | ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid(); |
| 586 | timelineUtils->CreateTypedEntity(inferenceGuid, LabelsAndEventClasses::INFERENCE_GUID); |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 587 | timelineUtils->CreateRelationship(ProfilingRelationshipType::RetentionLink, |
| 588 | networkGuid, |
| 589 | inferenceGuid, |
| 590 | LabelsAndEventClasses::EXECUTION_OF_GUID); |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 591 | timelineUtils->RecordEvent(inferenceGuid, LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS); |
| 592 | } |
| 593 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 594 | bool executionSucceeded = true; |
| 595 | |
| 596 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 597 | if (m_ProfilingService.IsProfilingEnabled()) |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 598 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 599 | m_ProfilingService.IncrementCounterValue(armnn::profiling::INFERENCES_RUN); |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 600 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 601 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Execute"); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 602 | ARMNN_SCOPED_HEAP_PROFILING("Executing"); |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 603 | executionSucceeded = Execute(timelineUtils, inferenceGuid); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 604 | } |
| 605 | |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 606 | if (timelineUtils) |
| 607 | { |
| 608 | // Add end of life of the inference timeline if profiling is enabled. |
| 609 | timelineUtils->RecordEvent(inferenceGuid, LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS); |
| 610 | timelineUtils->Commit(); |
| 611 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 612 | return executionSucceeded ? Status::Success : Status::Failure; |
| 613 | } |
| 614 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 615 | void LoadedNetwork::EnqueueInput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 616 | { |
| 617 | if (layer.GetType() != LayerType::Input) |
| 618 | { |
| 619 | throw InvalidArgumentException("EnqueueInput: given layer not an InputLayer"); |
| 620 | } |
| 621 | |
| 622 | if (tensorHandle == nullptr) |
| 623 | { |
| 624 | throw InvalidArgumentException("EnqueueInput: tensorHandle must not be NULL"); |
| 625 | } |
| 626 | |
| 627 | InputQueueDescriptor inputQueueDescriptor; |
| 628 | WorkloadInfo info; |
| 629 | |
| 630 | inputQueueDescriptor.m_Inputs.push_back(tensorHandle); |
| 631 | info.m_InputTensorInfos.push_back(tensorInfo); |
| 632 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 633 | ARMNN_ASSERT_MSG(layer.GetNumOutputSlots() == 1, "Can only handle Input Layer with one output"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 634 | const OutputHandler& handler = layer.GetOutputHandler(); |
| 635 | const TensorInfo& outputTensorInfo = handler.GetTensorInfo(); |
| 636 | ITensorHandle* outputTensorHandle = handler.GetData(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 637 | ARMNN_ASSERT_MSG(outputTensorHandle != nullptr, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 638 | "Data should have been allocated."); |
| 639 | inputQueueDescriptor.m_Outputs.push_back(outputTensorHandle); |
| 640 | info.m_OutputTensorInfos.push_back(outputTensorInfo); |
| 641 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 642 | MemorySourceFlags importFlags = outputTensorHandle->GetImportFlags(); |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 643 | bool needMemCopy = true; |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 644 | if (m_NetworkProperties.m_ImportEnabled) // Try import the input tensor |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 645 | { |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 646 | if(CheckFlag(importFlags, m_NetworkProperties.m_InputSource)) |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 647 | { |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 648 | needMemCopy = false; |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 649 | // This assumes a CPU Tensor handle |
| 650 | void* mem = tensorHandle->Map(false); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 651 | if (outputTensorHandle->Import(mem, m_NetworkProperties.m_InputSource)) |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 652 | { |
| 653 | tensorHandle->Unmap(); |
| 654 | return; // No need for a workload since the import has been done. |
| 655 | } |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 656 | tensorHandle->Unmap(); |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 657 | throw MemoryImportException("EnqueueInput: Memory Import failed"); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 658 | } |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 659 | } |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 660 | if (needMemCopy) |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 661 | { |
| 662 | // Create a mem copy workload for input since we did not import |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 663 | std::unique_ptr<IWorkload> inputWorkload = std::make_unique<CopyMemGenericWorkload>(inputQueueDescriptor, info); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 664 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 665 | ARMNN_ASSERT_MSG(inputWorkload, "No input workload created"); |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 666 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 667 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 668 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 669 | if (timelineUtils) |
| 670 | { |
| 671 | // Add Input Workload to the post-optimisation network structure |
| 672 | AddWorkloadStructure(timelineUtils, inputWorkload, layer); |
| 673 | timelineUtils->Commit(); |
| 674 | } |
| 675 | |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 676 | m_InputQueue.push_back(move(inputWorkload)); |
| 677 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 678 | } |
| 679 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 680 | void LoadedNetwork::EnqueueOutput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 681 | { |
| 682 | if (layer.GetType() != LayerType::Output) |
| 683 | { |
| 684 | throw InvalidArgumentException("EnqueueOutput: given layer not an OutputLayer"); |
| 685 | } |
| 686 | |
| 687 | if (tensorHandle == nullptr) |
| 688 | { |
| 689 | throw InvalidArgumentException("EnqueueOutput: tensorHandle must not be NULL"); |
| 690 | } |
| 691 | |
| 692 | OutputQueueDescriptor outputQueueDescriptor; |
| 693 | WorkloadInfo info; |
| 694 | |
| 695 | outputQueueDescriptor.m_Outputs.push_back(tensorHandle); |
| 696 | info.m_OutputTensorInfos.push_back(tensorInfo); |
| 697 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 698 | ARMNN_ASSERT_MSG(layer.GetNumInputSlots() == 1, "Output Layer should have exactly one input."); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 699 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 700 | // Gets the output handler from the previous node. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 701 | const OutputHandler& outputHandler = layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetOutputHandler(); |
| 702 | |
| 703 | const TensorInfo& inputTensorInfo = outputHandler.GetTensorInfo(); |
| 704 | ITensorHandle* inputTensorHandle = outputHandler.GetData(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 705 | ARMNN_ASSERT_MSG(inputTensorHandle != nullptr, "Data should have been allocated."); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 706 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 707 | // Try import the output tensor. |
| 708 | // Note: We can only import the output pointer if all of the following hold true: |
| 709 | // a) The imported pointer is aligned sufficiently |
| 710 | // b) The tensor has zero padding |
| 711 | // c) There is only one connection to the OutputSlot and it is to an OutputLayer. |
| 712 | // d) The output pointer is allocated via malloc. (Other types will be supported in a later release) |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 713 | // e) m_IsExportEnabled must be set to true |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 714 | bool needMemCopy = true; |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 715 | if (m_NetworkProperties.m_ExportEnabled && |
| 716 | (layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetNumConnections() == 1)) |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 717 | { |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 718 | if(layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetOwningLayer().GetType() != LayerType::Input) |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 719 | { |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 720 | MemorySourceFlags importFlags = inputTensorHandle->GetImportFlags(); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 721 | if (CheckFlag(importFlags, m_NetworkProperties.m_OutputSource)) |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 722 | { |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 723 | needMemCopy = false; |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 724 | void *mem = tensorHandle->Map(false); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 725 | bool importOk = inputTensorHandle->Import(mem, m_NetworkProperties.m_OutputSource); |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 726 | tensorHandle->Unmap(); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 727 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 728 | if (importOk) |
| 729 | { |
| 730 | // Insert synchronization workload |
| 731 | MemSyncQueueDescriptor syncDesc; |
| 732 | syncDesc.m_Inputs.push_back(inputTensorHandle); |
| 733 | info.m_InputTensorInfos.push_back(inputTensorInfo); |
| 734 | auto syncWorkload = std::make_unique<SyncMemGenericWorkload>(syncDesc, info); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 735 | ARMNN_ASSERT_MSG(syncWorkload, "No sync workload created"); |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 736 | m_OutputQueue.push_back(move(syncWorkload)); |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 737 | } |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 738 | else |
| 739 | { |
| 740 | throw MemoryExportException("EnqueueOutput: Memory Export failed"); |
| 741 | } |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | } |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 745 | if (needMemCopy) |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 746 | { |
Sadik Armagan | 23969e8 | 2020-11-18 14:17:04 +0000 | [diff] [blame] | 747 | // If we got here then we didn't export the memory, so add an output workload which performs a memcopy. |
| 748 | outputQueueDescriptor.m_Inputs.push_back(inputTensorHandle); |
| 749 | info.m_InputTensorInfos.push_back(inputTensorInfo); |
| 750 | |
| 751 | std::unique_ptr<IWorkload> outputWorkload = |
| 752 | std::make_unique<CopyMemGenericWorkload>(outputQueueDescriptor, info); |
| 753 | ARMNN_ASSERT_MSG(outputWorkload, "No output workload created"); |
| 754 | |
| 755 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 756 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
| 757 | if (timelineUtils) |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 758 | { |
Sadik Armagan | 23969e8 | 2020-11-18 14:17:04 +0000 | [diff] [blame] | 759 | // Add Output Workload to the post-optimisation network structure |
| 760 | AddWorkloadStructure(timelineUtils, outputWorkload, layer); |
| 761 | timelineUtils->Commit(); |
Sadik Armagan | 890bf65 | 2020-09-29 15:12:36 +0100 | [diff] [blame] | 762 | } |
Sadik Armagan | 23969e8 | 2020-11-18 14:17:04 +0000 | [diff] [blame] | 763 | |
| 764 | m_OutputQueue.push_back(move(outputWorkload)); |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 765 | } |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 768 | void LoadedNetwork::AllocateWorkingMemory(std::lock_guard<std::mutex>& lock) |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 769 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 770 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Working Memory Allocation"); |
| 771 | |
| 772 | // this unused parameter makes sure we can only call this function with a valid lock |
| 773 | IgnoreUnused(lock); |
| 774 | |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 775 | if (m_IsWorkingMemAllocated) |
| 776 | { |
| 777 | return; |
| 778 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 779 | for (auto&& workloadFactory : m_WorkloadFactories) |
| 780 | { |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 781 | IBackendInternal::IMemoryManagerSharedPtr memoryManager = workloadFactory.second.second; |
| 782 | if (memoryManager) |
| 783 | { |
| 784 | memoryManager->Acquire(); |
| 785 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 786 | } |
Narumol Prangnawarat | 11bd261 | 2019-08-13 10:26:53 +0100 | [diff] [blame] | 787 | m_TensorHandleFactoryRegistry.AquireMemory(); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 788 | m_IsWorkingMemAllocated = true; |
| 789 | } |
| 790 | |
| 791 | void LoadedNetwork::FreeWorkingMemory() |
| 792 | { |
Matthew Bentham | 2a326b5 | 2019-03-19 10:11:01 +0000 | [diff] [blame] | 793 | std::lock_guard<std::mutex> lockGuard(m_WorkingMemMutex); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 794 | if (!m_IsWorkingMemAllocated) |
| 795 | { |
| 796 | return; |
| 797 | } |
| 798 | // Informs the memory managers to release memory in it's respective memory group |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 799 | for (auto&& workloadFactory : m_WorkloadFactories) |
| 800 | { |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 801 | IBackendInternal::IMemoryManagerSharedPtr memoryManager = workloadFactory.second.second; |
| 802 | if (memoryManager) |
| 803 | { |
| 804 | memoryManager->Release(); |
| 805 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 806 | } |
Narumol Prangnawarat | 11bd261 | 2019-08-13 10:26:53 +0100 | [diff] [blame] | 807 | m_TensorHandleFactoryRegistry.ReleaseMemory(); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 808 | m_IsWorkingMemAllocated = false; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 809 | } |
| 810 | |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 811 | bool LoadedNetwork::Execute(std::unique_ptr<TimelineUtilityMethods>& timelineUtils, |
| 812 | profiling::ProfilingGuid inferenceGuid) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 813 | { |
| 814 | bool success = true; |
| 815 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 816 | auto Fail = [&](const std::exception& error) |
| 817 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 818 | ARMNN_LOG(error) << "An error occurred attempting to execute a workload: " << error.what(); |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 819 | success = false; |
| 820 | }; |
| 821 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 822 | try |
| 823 | { |
Matthew Bentham | 2a326b5 | 2019-03-19 10:11:01 +0000 | [diff] [blame] | 824 | std::lock_guard<std::mutex> lockGuard(m_WorkingMemMutex); |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 825 | AllocateWorkingMemory(lockGuard); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 826 | |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 827 | ProfilingDynamicGuid workloadInferenceID(0); |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 828 | auto ExecuteQueue = [&timelineUtils, &workloadInferenceID, &inferenceGuid](WorkloadQueue& queue) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 829 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 830 | for (auto& workload : queue) |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 831 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 832 | if(timelineUtils) |
| 833 | { |
| 834 | workloadInferenceID = timelineUtils->RecordWorkloadInferenceAndStartOfLifeEvent(workload->GetGuid(), |
| 835 | inferenceGuid); |
| 836 | } |
| 837 | workload->Execute(); |
| 838 | if(timelineUtils) |
| 839 | { |
| 840 | timelineUtils->RecordEndOfLifeEvent(workloadInferenceID); |
| 841 | } |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 842 | } |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 843 | }; |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 844 | |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 845 | ExecuteQueue(m_InputQueue); |
| 846 | ExecuteQueue(m_WorkloadQueue); |
| 847 | ExecuteQueue(m_OutputQueue); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 848 | } |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 849 | catch (const RuntimeException& error) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 850 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 851 | Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 852 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 853 | catch (const std::runtime_error& error) |
| 854 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 855 | Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 856 | } |
| 857 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 858 | return success; |
| 859 | } |
| 860 | |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 861 | void LoadedNetwork::EnqueueInput(const ConstTensor& inputTensor, |
| 862 | ITensorHandle* inputTensorHandle) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 863 | { |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 864 | MemorySourceFlags importFlags = inputTensorHandle->GetImportFlags(); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 865 | if (m_NetworkProperties.m_ImportEnabled) // Try import the input tensor |
| 866 | { |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 867 | if (CheckFlag(importFlags, m_NetworkProperties.m_InputSource) ) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 868 | { |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 869 | std::unique_ptr<ITensorHandle> tensorHandle = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 870 | std::make_unique<ConstPassthroughTensorHandle>(inputTensor.GetInfo(), |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 871 | inputTensor.GetMemoryArea()); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 872 | void* mem = tensorHandle->Map(false); |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 873 | |
| 874 | if (inputTensorHandle->Import(mem, m_NetworkProperties.m_InputSource)) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 875 | { |
| 876 | tensorHandle->Unmap(); |
| 877 | return; |
| 878 | } |
| 879 | tensorHandle->Unmap(); |
| 880 | throw MemoryImportException("EnqueueInput: Memory Import failed"); |
| 881 | } |
| 882 | else |
| 883 | { |
| 884 | throw MemoryImportException("EnqueueInput: Memory Import failed, backend does not support Import"); |
| 885 | } |
| 886 | } |
| 887 | else |
| 888 | { |
| 889 | std::unique_ptr<ITensorHandle> tensorHandle = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 890 | std::make_unique<ConstPassthroughTensorHandle>(inputTensor.GetInfo(), inputTensor.GetMemoryArea()); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 891 | |
| 892 | auto copyFunc = [](void* dst, const void* src, size_t size) |
| 893 | { |
| 894 | memcpy(dst, src, size); |
| 895 | }; |
| 896 | |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 897 | CopyTensorContentsGeneric(tensorHandle.get(), inputTensorHandle, copyFunc); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
| 901 | void LoadedNetwork::EnqueueOutput(const BindableLayer& layer, const Tensor& outputTensor, WorkingMemHandle& handle) |
| 902 | { |
| 903 | if (layer.GetType() != LayerType::Output) |
| 904 | { |
| 905 | throw InvalidArgumentException("EnqueueOutput: given layer not an OutputLayer"); |
| 906 | } |
| 907 | ARMNN_ASSERT_MSG(layer.GetNumInputSlots() == 1, "Output Layer should have exactly one input."); |
| 908 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 909 | LayerGuid id = layer.GetGuid(); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 910 | WorkingMemDescriptor descriptor = handle.GetWorkingMemDescriptor(id); |
| 911 | |
| 912 | ITensorHandle* inputTensorHandle = descriptor.m_Inputs[0]; |
| 913 | ARMNN_ASSERT_MSG(inputTensorHandle != nullptr, "Data should have been allocated."); |
| 914 | |
| 915 | // Try import the output tensor. |
| 916 | // Note: We can only import the output pointer if all of the following hold true: |
| 917 | // a) The imported pointer is aligned sufficiently |
| 918 | // b) The tensor has zero padding |
| 919 | // c) There is only one connection to the OutputSlot and it is to an OutputLayer. |
| 920 | // d) The output pointer is allocated via malloc. (Other types will be supported in a later release) |
| 921 | // e) m_IsExportEnabled must be set to true |
| 922 | if (m_NetworkProperties.m_ExportEnabled && |
| 923 | (layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetNumConnections() == 1)) |
| 924 | { |
| 925 | if (layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetOwningLayer().GetType() != LayerType::Input) |
| 926 | { |
| 927 | MemorySourceFlags importFlags = inputTensorHandle->GetImportFlags(); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 928 | if (CheckFlag(importFlags, m_NetworkProperties.m_OutputSource)) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 929 | { |
| 930 | std::unique_ptr<ITensorHandle> tensorHandle = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 931 | std::make_unique<PassthroughTensorHandle>(outputTensor.GetInfo(), |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 932 | outputTensor.GetMemoryArea()); |
| 933 | |
| 934 | void* mem = tensorHandle->Map(false); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 935 | bool importOk = inputTensorHandle->Import(mem, m_NetworkProperties.m_OutputSource); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 936 | tensorHandle->Unmap(); |
| 937 | |
| 938 | if (importOk) |
| 939 | { |
| 940 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "SyncMemGeneric_Execute"); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 941 | inputTensorHandle->Map(true); |
| 942 | inputTensorHandle->Unmap(); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 943 | } |
| 944 | else |
| 945 | { |
| 946 | throw MemoryExportException("EnqueueOutput: Memory Export failed"); |
| 947 | } |
| 948 | } |
| 949 | else |
| 950 | { |
| 951 | throw MemoryExportException("EnqueueOutput: Memory Export failed, backend does not support Export"); |
| 952 | } |
| 953 | } |
| 954 | else |
| 955 | { |
| 956 | throw MemoryExportException("EnqueueOutput: Memory Export failed, attempting to export Input Layer"); |
| 957 | } |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | auto copyFunc = [](void* dst, const void* src, size_t size) |
| 962 | { |
| 963 | memcpy(dst, src, size); |
| 964 | }; |
| 965 | |
| 966 | std::unique_ptr<ITensorHandle> tensorHandle = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 967 | std::make_unique<PassthroughTensorHandle>(outputTensor.GetInfo(), |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 968 | outputTensor.GetMemoryArea()); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 969 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 970 | CopyTensorContentsGeneric(inputTensorHandle, tensorHandle.get(), copyFunc); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 974 | |
| 975 | const armnn::ConstTensor GetInputTensor(const LayerBindingId layerId, const InputTensors& inputTensors) |
| 976 | { |
| 977 | for (auto inputTensorPair : inputTensors) |
| 978 | { |
| 979 | LayerBindingId id = inputTensorPair.first; |
| 980 | if (id == layerId) |
| 981 | { |
| 982 | return inputTensorPair.second; |
| 983 | } |
| 984 | } |
| 985 | throw InvalidArgumentException("Input does not exist."); |
| 986 | } |
| 987 | |
| 988 | const armnn::Tensor GetOutputTensor(const LayerBindingId layerId, const OutputTensors& outputTensors) |
| 989 | { |
| 990 | for (auto outputTensorPair : outputTensors) |
| 991 | { |
| 992 | LayerBindingId id = outputTensorPair.first; |
| 993 | if (id == layerId) |
| 994 | { |
| 995 | return outputTensorPair.second; |
| 996 | } |
| 997 | } |
| 998 | throw InvalidArgumentException("Output does not exist."); |
| 999 | } |
| 1000 | |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1001 | std::vector<ImportedInputId> LoadedNetwork::ImportInputs(const InputTensors& inputTensors) |
| 1002 | { |
| 1003 | if (!m_NetworkProperties.m_ImportEnabled) // Try import the input tensor |
| 1004 | { |
| 1005 | throw MemoryImportException("ImportInputs: Memory Import failed, NetworkProperties.m_ImportEnabled"); |
| 1006 | } |
| 1007 | |
| 1008 | std::vector<ImportedInputId> importedInputs; |
| 1009 | Graph& graph = m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph().TopologicalSort(); |
| 1010 | |
| 1011 | for (auto inputTensor : inputTensors) |
| 1012 | { |
| 1013 | auto layerBindingId = inputTensor.first; |
| 1014 | auto it = std::find_if(graph.GetInputLayers().begin(), graph.GetInputLayers().end(), [=](auto* layer) |
| 1015 | { |
| 1016 | return layer->GetBindingId() == layerBindingId; |
| 1017 | }); |
| 1018 | |
| 1019 | if (it == graph.GetInputLayers().end()) |
| 1020 | { |
| 1021 | throw MemoryImportException("ImportInputs: Memory Import failed, backend does not support Import"); |
| 1022 | } |
| 1023 | |
| 1024 | const Layer* layer = *it; |
| 1025 | if (layer->GetType() != LayerType::Input) |
| 1026 | { |
| 1027 | throw InvalidArgumentException("ImportInputs: given layer not an InputLayer"); |
| 1028 | } |
| 1029 | |
| 1030 | const OutputSlot& outputSlot = layer->GetOutputSlots()[0]; |
| 1031 | |
| 1032 | ITensorHandleFactory::FactoryId factoryId = outputSlot.GetTensorHandleFactoryId(); |
| 1033 | const TensorInfo& tensorInfo = outputSlot.GetTensorInfo(); |
| 1034 | |
| 1035 | ITensorHandleFactory* handleFactory = m_TensorHandleFactoryRegistry.GetFactory(factoryId); |
| 1036 | ARMNN_ASSERT(handleFactory); |
| 1037 | |
| 1038 | m_PreImportedInputHandles.emplace_back(layerBindingId, |
| 1039 | handleFactory->CreateTensorHandle(tensorInfo, false)); |
| 1040 | |
| 1041 | ITensorHandle* tensorHandle = m_PreImportedInputHandles.back().m_TensorHandle.get(); |
| 1042 | |
| 1043 | if (!CheckFlag(tensorHandle->GetImportFlags(), m_NetworkProperties.m_InputSource)) |
| 1044 | { |
| 1045 | throw MemoryImportException( |
| 1046 | fmt::format("ImportInputs: Memory Import failed, backend: {} does not support importing from source {}" |
| 1047 | , factoryId, m_NetworkProperties.m_InputSource)); |
| 1048 | } |
| 1049 | |
| 1050 | std::unique_ptr<ITensorHandle> passThroughTensorHandle = |
| 1051 | std::make_unique<ConstPassthroughTensorHandle>(inputTensor.second.GetInfo(), |
| 1052 | inputTensor.second.GetMemoryArea()); |
| 1053 | |
| 1054 | if (tensorHandle->Import(passThroughTensorHandle->Map(), m_NetworkProperties.m_InputSource)) |
| 1055 | { |
| 1056 | importedInputs.push_back(m_CurImportedInputId++); |
| 1057 | passThroughTensorHandle->Unmap(); |
| 1058 | } |
| 1059 | else |
| 1060 | { |
| 1061 | passThroughTensorHandle->Unmap(); |
| 1062 | throw MemoryImportException("ImportInputs: Memory Import failed"); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | return importedInputs; |
| 1067 | } |
| 1068 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1069 | Status LoadedNetwork::Execute(const InputTensors& inputTensors, |
| 1070 | const OutputTensors& outputTensors, |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1071 | IWorkingMemHandle& iWorkingMemHandle, |
| 1072 | std::vector<ImportedInputId> preImportedInputs) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1073 | { |
| 1074 | const Graph& graph = m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph(); |
| 1075 | |
| 1076 | // Walk graph to determine the order of execution. |
| 1077 | if (graph.GetNumLayers() < 2) |
| 1078 | { |
| 1079 | ARMNN_LOG(warning) << "IRuntime::EnqueueWorkload()::Less than two nodes in graph"; |
| 1080 | return Status::Failure; |
| 1081 | } |
| 1082 | |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1083 | if (inputTensors.size() + preImportedInputs.size() != graph.GetNumInputs() ) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1084 | { |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1085 | if (preImportedInputs.empty()) |
| 1086 | { |
| 1087 | throw InvalidArgumentException("Number of inputs provided does not match network."); |
| 1088 | } |
| 1089 | else |
| 1090 | { |
| 1091 | throw InvalidArgumentException("Number of inputs + preImportedInputs provided does not match network."); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | WorkingMemHandle& workingMemHandle = dynamic_cast<WorkingMemHandle&>(iWorkingMemHandle); |
| 1096 | |
| 1097 | // This map is a quick way to check for duplicate or non-existing LayerBindingIds |
| 1098 | std::unordered_map<LayerBindingId, bool> validationMap = workingMemHandle.GetValidationMap(); |
| 1099 | for (auto pair : inputTensors) |
| 1100 | { |
| 1101 | const LayerBindingId layerBindingId = pair.first; |
| 1102 | |
| 1103 | try |
| 1104 | { |
| 1105 | bool& previouslyUsed = validationMap.at(pair.first); |
| 1106 | if (previouslyUsed) |
| 1107 | { |
| 1108 | throw InvalidArgumentException(fmt::format("Duplicate LayerbindingId: {} ", layerBindingId)); |
| 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | previouslyUsed = true; |
| 1113 | } |
| 1114 | } |
| 1115 | catch (std::out_of_range) |
| 1116 | { |
| 1117 | throw InvalidArgumentException(fmt::format("Unknown LayerBindingId id: {}", layerBindingId)); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | if (!preImportedInputs.empty()) |
| 1122 | { |
| 1123 | const unsigned int maxPreImportedId = *std::max_element(preImportedInputs.begin(), preImportedInputs.end()); |
| 1124 | if (maxPreImportedId > m_CurImportedInputId) |
| 1125 | { |
| 1126 | throw InvalidArgumentException(fmt::format("Invalid ImportedInputId: {}", maxPreImportedId)); |
| 1127 | } |
| 1128 | for (ImportedInputId id : preImportedInputs) |
| 1129 | { |
| 1130 | const LayerBindingId layerBindingId = m_PreImportedInputHandles[id].m_LayerBindingId; |
| 1131 | |
| 1132 | try |
| 1133 | { |
| 1134 | bool& previouslyUsed = validationMap.at(layerBindingId); |
| 1135 | if (previouslyUsed) |
| 1136 | { |
| 1137 | throw InvalidArgumentException(fmt::format("Duplicate LayerbindingId: {} ", layerBindingId)); |
| 1138 | } |
| 1139 | else |
| 1140 | { |
| 1141 | previouslyUsed = true; |
| 1142 | } |
| 1143 | } |
| 1144 | catch (std::out_of_range) |
| 1145 | { |
| 1146 | throw InvalidArgumentException(fmt::format("Unknown LayerBindingId id: {}", layerBindingId)); |
| 1147 | } |
| 1148 | } |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | std::unique_ptr<profiling::TimelineUtilityMethods> timelineUtils = |
| 1152 | profiling::TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
| 1153 | profiling::ProfilingGuid inferenceGuid = m_ProfilingService.GetNextGuid(); |
| 1154 | if (timelineUtils) |
| 1155 | { |
| 1156 | // Add inference timeline trace if profiling is enabled. |
| 1157 | profiling::ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid(); |
| 1158 | timelineUtils->CreateTypedEntity(inferenceGuid, profiling::LabelsAndEventClasses::INFERENCE_GUID); |
| 1159 | timelineUtils->CreateRelationship(profiling::ProfilingRelationshipType::RetentionLink, |
| 1160 | networkGuid, |
| 1161 | inferenceGuid, |
| 1162 | profiling::LabelsAndEventClasses::EXECUTION_OF_GUID); |
| 1163 | timelineUtils->RecordEvent(inferenceGuid, profiling::LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS); |
| 1164 | } |
| 1165 | |
| 1166 | bool executionSucceeded = true; |
| 1167 | |
| 1168 | if (timelineUtils) |
| 1169 | { |
| 1170 | // Add end of life of the inference timeline if profiling is enabled. |
| 1171 | timelineUtils->RecordEvent(inferenceGuid, profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS); |
| 1172 | timelineUtils->Commit(); |
| 1173 | } |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1174 | |
| 1175 | if (!workingMemHandle.IsAllocated()) |
| 1176 | { |
| 1177 | workingMemHandle.Allocate(); |
| 1178 | } |
| 1179 | |
| 1180 | { |
| 1181 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "PrepareInputs"); |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1182 | // Swap in the pre-imported inputs if any |
| 1183 | for (ImportedInputId id : preImportedInputs) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1184 | { |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1185 | const ImportedInputHandlePin& importedInputPin = m_PreImportedInputHandles[id]; |
| 1186 | |
| 1187 | const LayerBindingId layerBindingId = m_PreImportedInputHandles[id].m_LayerBindingId; |
| 1188 | ITensorHandle* preimportedHandle = importedInputPin.m_TensorHandle.get(); |
| 1189 | auto inputConnections = workingMemHandle.GetInputConnections(layerBindingId); |
| 1190 | for (auto it : inputConnections) |
| 1191 | { |
| 1192 | *it = preimportedHandle; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | for (auto pair : inputTensors) |
| 1197 | { |
| 1198 | EnqueueInput(pair.second, workingMemHandle.GetInputHandle(pair.first)); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | auto Fail = [&](const std::exception& error) |
| 1203 | { |
| 1204 | ARMNN_LOG(error) << "An error occurred attempting to execute a workload: " << error.what(); |
| 1205 | executionSucceeded = false; |
| 1206 | }; |
| 1207 | profiling::ProfilingDynamicGuid workloadInferenceID(0); |
| 1208 | |
| 1209 | try |
| 1210 | { |
| 1211 | for (unsigned int i = 0; i < m_WorkloadQueue.size(); ++i) |
| 1212 | { |
| 1213 | auto& workload = m_WorkloadQueue[i]; |
| 1214 | if (timelineUtils) |
| 1215 | { |
| 1216 | workloadInferenceID = timelineUtils->RecordWorkloadInferenceAndStartOfLifeEvent(workload->GetGuid(), |
| 1217 | inferenceGuid); |
| 1218 | } |
| 1219 | workload->ExecuteAsync(workingMemHandle.GetWorkingMemDescriptorAt(i)); |
| 1220 | |
| 1221 | if (timelineUtils) |
| 1222 | { |
| 1223 | timelineUtils->RecordEndOfLifeEvent(workloadInferenceID); |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | catch (const RuntimeException& error) |
| 1228 | { |
| 1229 | Fail(error); |
| 1230 | } |
| 1231 | catch (const std::runtime_error& error) |
| 1232 | { |
| 1233 | Fail(error); |
| 1234 | } |
| 1235 | // For each output to the network, call EnqueueOutput with the data passed by the user. |
| 1236 | { |
| 1237 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "PrepareOutputs"); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1238 | for (const BindableLayer *outputLayer : graph.GetOutputLayers()) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1239 | { |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1240 | EnqueueOutput(*outputLayer, GetOutputTensor(outputLayer->GetBindingId(), outputTensors), workingMemHandle); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1241 | } |
| 1242 | } |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 1243 | |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1244 | // Restore the workingMemHandle to its original state |
| 1245 | for (ImportedInputId id : preImportedInputs) |
| 1246 | { |
| 1247 | const LayerBindingId layerBindingId = m_PreImportedInputHandles[id].m_LayerBindingId; |
| 1248 | |
| 1249 | auto inputHandle = workingMemHandle.GetInputHandle(layerBindingId); |
| 1250 | auto inputConnections = workingMemHandle.GetInputConnections(layerBindingId); |
| 1251 | for (auto it : inputConnections) |
| 1252 | { |
| 1253 | *it = inputHandle; |
| 1254 | } |
| 1255 | } |
| 1256 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1257 | return executionSucceeded ? Status::Success : Status::Failure; |
| 1258 | } |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1259 | |
| 1260 | /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have |
| 1261 | /// overlapped Execution by calling this function from different threads. |
| 1262 | std::unique_ptr<IWorkingMemHandle> LoadedNetwork::CreateWorkingMemHandle(NetworkId networkId) |
| 1263 | { |
| 1264 | Graph& order = m_OptimizedNetwork->pOptimizedNetworkImpl->GetGraph(); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1265 | std::unordered_map<LayerGuid, std::vector<std::unique_ptr<ITensorHandle> > > tensorHandleMap; |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1266 | std::vector<WorkingMemDescriptor> workingMemDescriptors; |
| 1267 | std::unordered_map<LayerGuid, WorkingMemDescriptor> workingMemDescriptorMap; |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1268 | TensorHandleFactoryRegistry tensorHandleFactoryRegistry; |
| 1269 | WorkloadFactoryMap workloadFactoryMap; |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1270 | |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1271 | std::vector<std::shared_ptr<IMemoryManager>> memoryManagers; |
| 1272 | |
| 1273 | for (auto const& backend : m_Backends) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1274 | { |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1275 | if (backend.second->SupportsTensorAllocatorAPI()) |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1276 | { |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1277 | backend.second->RegisterTensorHandleFactories( |
| 1278 | tensorHandleFactoryRegistry, |
| 1279 | static_cast<MemorySourceFlags>(m_NetworkProperties.m_InputSource), |
| 1280 | static_cast<MemorySourceFlags>(m_NetworkProperties.m_OutputSource)); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1281 | memoryManagers.emplace_back(tensorHandleFactoryRegistry.GetMemoryManagers().back()); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1282 | } |
| 1283 | else |
| 1284 | { |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1285 | std::shared_ptr<IMemoryManager> memoryManager = backend.second->CreateMemoryManager(); |
| 1286 | auto workloadFactory = backend.second->CreateWorkloadFactory( |
| 1287 | memoryManager, m_OptimizedNetwork->pOptimizedNetworkImpl->GetModelOptions()); |
| 1288 | |
| 1289 | workloadFactoryMap.emplace( |
| 1290 | std::make_pair(backend.first, std::make_pair(std::move(workloadFactory), memoryManager))); |
| 1291 | memoryManagers.emplace_back(memoryManager); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | auto GetTensorHandle = [&](Layer* layer, const OutputSlot& outputSlot, bool isMemoryManaged) |
| 1296 | { |
| 1297 | ITensorHandleFactory::FactoryId factoryId = outputSlot.GetTensorHandleFactoryId(); |
| 1298 | const TensorInfo& tensorInfo = outputSlot.GetTensorInfo(); |
| 1299 | |
| 1300 | if (factoryId == ITensorHandleFactory::LegacyFactoryId) |
| 1301 | { |
| 1302 | BackendId id = layer->GetBackendId(); |
| 1303 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
| 1304 | return workloadFactoryMap.at(id).first->CreateTensorHandle(tensorInfo, isMemoryManaged); |
| 1305 | ARMNN_NO_DEPRECATE_WARN_END |
| 1306 | } |
| 1307 | else |
| 1308 | { |
| 1309 | ITensorHandleFactory* handleFactory = tensorHandleFactoryRegistry.GetFactory(factoryId); |
| 1310 | ARMNN_ASSERT(handleFactory); |
| 1311 | return handleFactory->CreateTensorHandle(tensorInfo, isMemoryManaged); |
| 1312 | } |
| 1313 | }; |
| 1314 | |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1315 | struct HandleInfo |
| 1316 | { |
| 1317 | unsigned int m_ReferenceCount = 0; |
| 1318 | bool isInputLayer = false; |
| 1319 | bool isOutputLayer = false; |
| 1320 | LayerBindingId m_LayerBindingId = -1; |
| 1321 | }; |
| 1322 | |
| 1323 | std::vector<WorkingMemHandle::InputConnectionInfo> inputConnections; |
| 1324 | std::vector<std::pair<LayerBindingId, LayerGuid>> inputIndexes; |
| 1325 | |
| 1326 | std::unordered_map<const ITensorHandle*, HandleInfo> handleReferenceCounts; |
| 1327 | |
| 1328 | unsigned int workingMemDescriptorIndex = 0; |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1329 | for (auto&& layer : order) |
| 1330 | { |
| 1331 | WorkingMemDescriptor workingMemDescriptor; |
| 1332 | |
| 1333 | // Constant layers execution and management is handled during loaded network construction |
| 1334 | if (layer->GetType() == LayerType::Constant) |
| 1335 | { |
| 1336 | continue; |
| 1337 | } |
| 1338 | bool isMemoryManaged = true; |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1339 | bool isInputLayer = false; |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1340 | // Look for the layer with 1 OutputSlot which has 1 connection and that connection is an Output Layer |
| 1341 | // If Export is enabled disable memory management so we can export, otherwise we do a copy |
| 1342 | if ((layer->GetNumOutputSlots() == 1) && |
| 1343 | (layer->GetOutputSlots()[0].GetNumConnections() == 1) && |
| 1344 | (layer->GetOutputSlots()[0].GetConnection(0)->GetOwningLayer().GetType() == LayerType::Output)) |
| 1345 | { |
| 1346 | isMemoryManaged = !m_NetworkProperties.m_ExportEnabled; |
| 1347 | } |
| 1348 | else if (layer->GetType() == LayerType::Input || layer->GetType() == LayerType::MemImport) |
| 1349 | { |
| 1350 | // Input layers/workloads will not be executed so the descriptor is not added to workingMemDescriptors |
| 1351 | // However we will still need to manage the tensorHandle |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1352 | isInputLayer = true; |
| 1353 | isMemoryManaged = !m_NetworkProperties.m_ImportEnabled; |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | // Create a tensor handle for each output slot of a layer |
| 1357 | // Once we create it, we start managing its lifetime |
| 1358 | for (auto& slot : layer->GetOutputSlots()) |
| 1359 | { |
| 1360 | tensorHandleMap[layer->GetGuid()].emplace_back(GetTensorHandle(layer, slot, isMemoryManaged)); |
| 1361 | ITensorHandle* tensorHandle = tensorHandleMap[layer->GetGuid()].back().get(); |
| 1362 | |
| 1363 | workingMemDescriptor.m_Outputs.push_back(tensorHandle); |
| 1364 | tensorHandle->Manage(); |
| 1365 | unsigned int numConnections = slot.GetNumConnections(); |
| 1366 | ARMNN_ASSERT(numConnections != 0); |
| 1367 | |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1368 | handleReferenceCounts[tensorHandle].m_ReferenceCount = numConnections; |
| 1369 | |
| 1370 | if (isInputLayer) |
| 1371 | { |
| 1372 | handleReferenceCounts[tensorHandle].isInputLayer = true; |
| 1373 | LayerBindingId bindingId = static_cast<BindableLayer*>(layer)->GetBindingId(); |
| 1374 | |
| 1375 | handleReferenceCounts[tensorHandle].m_LayerBindingId = bindingId; |
| 1376 | |
| 1377 | inputIndexes.emplace_back(std::make_pair(bindingId, layer->GetGuid())); |
| 1378 | } |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1379 | } |
| 1380 | // Loop through the input slots in the same layer and decrement the reference counter associated |
| 1381 | // to each tensor handle we encounter. |
| 1382 | // Once it reaches zero, the lifetime of the tensor handle has ended, and we mark it's memory as available |
| 1383 | // so that the next tensor handle with a non overlapping lifetime can share it's memory. |
| 1384 | for (auto& slot : layer->GetInputSlots()) |
| 1385 | { |
| 1386 | ARMNN_ASSERT(slot.GetConnection()); |
| 1387 | auto outputSlot = slot.GetConnectedOutputSlot(); |
| 1388 | auto key = outputSlot->GetOwningLayer().GetGuid(); |
| 1389 | |
| 1390 | // Constant layers execution and management is handled during loaded network construction |
| 1391 | auto found = m_ConstantTensorHandles.find(key); |
| 1392 | if (found != m_ConstantTensorHandles.end()) |
| 1393 | { |
| 1394 | workingMemDescriptor.m_Inputs.push_back(found->second); |
| 1395 | continue; |
| 1396 | } |
| 1397 | |
| 1398 | auto search = tensorHandleMap.find(key); |
| 1399 | unsigned int index = outputSlot->CalculateIndexOnOwner(); |
| 1400 | ITensorHandle* inputTensorHandle = search->second[index].get(); |
| 1401 | workingMemDescriptor.m_Inputs.push_back(inputTensorHandle); |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1402 | |
| 1403 | HandleInfo& handleInfo = handleReferenceCounts.at(inputTensorHandle); |
| 1404 | |
| 1405 | // Store the iterator to the |
| 1406 | if (handleInfo.isInputLayer) |
| 1407 | { |
| 1408 | inputConnections.emplace_back(WorkingMemHandle::InputConnectionInfo{ |
| 1409 | handleInfo.m_LayerBindingId, workingMemDescriptorIndex, slot.GetSlotIndex()}); |
| 1410 | } |
| 1411 | |
| 1412 | --handleInfo.m_ReferenceCount; |
| 1413 | if (handleInfo.m_ReferenceCount == 0u) |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1414 | { |
| 1415 | // Stop managing lifetime of tensor handle |
| 1416 | inputTensorHandle->Allocate(); |
| 1417 | handleReferenceCounts.erase(inputTensorHandle); |
| 1418 | } |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1419 | } |
| 1420 | workingMemDescriptorMap.insert({layer->GetGuid(), workingMemDescriptor}); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1421 | |
| 1422 | // Input layers/workloads will not be executed, so the descriptor is not added to workingMemDescriptors |
| 1423 | // However we will still need to manage the tensorHandle |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1424 | if (!isInputLayer) |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1425 | { |
| 1426 | workingMemDescriptors.push_back(workingMemDescriptor); |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1427 | workingMemDescriptorIndex++; |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1428 | } |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1429 | } |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1430 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1431 | return std::make_unique<WorkingMemHandle>(networkId, |
Finn Williams | f37b970 | 2021-09-01 18:06:04 +0100 | [diff] [blame^] | 1432 | inputIndexes, |
| 1433 | inputConnections, |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1434 | workingMemDescriptors, |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 1435 | workingMemDescriptorMap, |
| 1436 | memoryManagers, |
| 1437 | std::move(tensorHandleMap)); |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 1440 | void LoadedNetwork::RegisterDebugCallback(const DebugCallbackFunction& func) |
| 1441 | { |
| 1442 | for (auto&& workloadPtr: m_WorkloadQueue) |
| 1443 | { |
| 1444 | workloadPtr.get()->RegisterDebugCallback(func); |
| 1445 | } |
| 1446 | } |
| 1447 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1448 | } |