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" |
| 9 | #include "Network.hpp" |
| 10 | #include "Runtime.hpp" |
| 11 | #include "Profiling.hpp" |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 12 | #include "HeapProfiling.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 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 18 | #include <backendsCommon/CpuTensorHandle.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> |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 22 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 23 | #include <LabelsAndEventClasses.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 25 | #include <boost/format.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 26 | |
| 27 | namespace armnn |
| 28 | { |
| 29 | |
| 30 | using namespace std; |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 31 | using namespace armnn::profiling; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 33 | namespace |
| 34 | { |
| 35 | |
| 36 | template <typename ExceptionType> |
| 37 | std::string ToErrorMessage(const char * prefix, const ExceptionType & error) |
| 38 | { |
| 39 | std::stringstream ss; |
| 40 | ss << prefix << " " << error.what(); |
| 41 | return ss.str(); |
| 42 | } |
| 43 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 44 | void AddLayerStructure(std::unique_ptr<TimelineUtilityMethods>& timelineUtils, |
| 45 | const Layer& layer, |
| 46 | ProfilingGuid networkGuid) |
| 47 | { |
| 48 | // Add layer to the post-optimisation network structure |
| 49 | std::string layerName = layer.GetNameStr().empty() ? "<Unnamed>" : layer.GetNameStr(); |
| 50 | timelineUtils->CreateNamedTypedChildEntity(layer.GetGuid(), |
| 51 | networkGuid, |
| 52 | layerName, |
| 53 | LabelsAndEventClasses::LAYER_GUID); |
| 54 | for (auto&& input : layer.GetInputSlots()) |
| 55 | { |
| 56 | const IOutputSlot* source = input.GetConnectedOutputSlot(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 57 | ARMNN_ASSERT(source != NULL); |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 58 | timelineUtils->CreateConnectionRelationship(ProfilingRelationshipType::RetentionLink, |
| 59 | source->GetOwningLayerGuid(), |
| 60 | layer.GetGuid()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void AddWorkloadStructure(std::unique_ptr<TimelineUtilityMethods>& timelineUtils, |
| 65 | std::unique_ptr<IWorkload>& workload, |
| 66 | const Layer& layer) |
| 67 | { |
| 68 | // Add workload to the post-optimisation network structure |
| 69 | timelineUtils->CreateTypedEntity(workload->GetGuid(), LabelsAndEventClasses::WORKLOAD_GUID); |
| 70 | timelineUtils->MarkEntityWithLabel(workload->GetGuid(), |
| 71 | layer.GetBackendId().Get(), |
| 72 | LabelsAndEventClasses::BACKENDID_GUID); |
| 73 | |
| 74 | // Link the workload to the layer |
| 75 | timelineUtils->CreateRelationship(ProfilingRelationshipType::RetentionLink, |
| 76 | layer.GetGuid(), |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 77 | workload->GetGuid(), |
| 78 | LabelsAndEventClasses::CHILD_GUID); |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 79 | } |
| 80 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | } // anonymous |
| 82 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 83 | std::unique_ptr<LoadedNetwork> LoadedNetwork::MakeLoadedNetwork(std::unique_ptr<OptimizedNetwork> net, |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 84 | std::string& errorMessage, |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 85 | const INetworkProperties& networkProperties, |
| 86 | profiling::ProfilingService& profilingService) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 87 | { |
| 88 | std::unique_ptr<LoadedNetwork> loadedNetwork; |
| 89 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 90 | auto Fail = [&](const std::exception& error) -> std::unique_ptr<LoadedNetwork> |
| 91 | { |
| 92 | errorMessage = ToErrorMessage("An error occurred when preparing the network workloads: ", error); |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 93 | ARMNN_LOG(error) << errorMessage; |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 94 | |
| 95 | return std::unique_ptr<LoadedNetwork>(); |
| 96 | }; |
| 97 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 98 | try |
| 99 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 100 | loadedNetwork.reset(new LoadedNetwork(std::move(net), networkProperties, profilingService)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | } |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 102 | catch (const armnn::RuntimeException& error) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 103 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 104 | return Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 105 | } |
| 106 | catch (const armnn::Exception& error) |
| 107 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 108 | return Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | } |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 110 | catch (const std::runtime_error& error) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 112 | return Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 113 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 114 | |
| 115 | return loadedNetwork; |
| 116 | } |
| 117 | |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 118 | LoadedNetwork::LoadedNetwork(std::unique_ptr<OptimizedNetwork> net, |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 119 | const INetworkProperties& networkProperties, |
| 120 | profiling::ProfilingService& profilingService) : |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 121 | m_OptimizedNetwork(std::move(net)), |
| 122 | m_IsImportEnabled(networkProperties.m_ImportEnabled), |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 123 | m_IsExportEnabled(networkProperties.m_ExportEnabled), |
Narumol Prangnawarat | 549cb7a | 2020-07-10 17:50:53 +0100 | [diff] [blame] | 124 | m_TensorHandleFactoryRegistry(), |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 125 | m_ProfilingService(profilingService) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 126 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 127 | // Create a profiler and register it for the current thread. |
| 128 | m_Profiler = std::make_shared<Profiler>(); |
| 129 | ProfilerManager::GetInstance().RegisterProfiler(m_Profiler.get()); |
| 130 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 131 | Graph& order = m_OptimizedNetwork->GetGraph().TopologicalSort(); |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 132 | //First create tensor handlers, backends and workload factories. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | //Handlers are created before workloads are. |
| 134 | //Because workload creation can modify some of the handlers, |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 135 | //(for example the splitter and concat layers). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 136 | for (auto&& layer : order) |
| 137 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 138 | auto const& backendId = layer->GetBackendId(); |
| 139 | if (m_Backends.count(backendId) == 0) |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 140 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 141 | auto createBackend = BackendRegistryInstance().GetFactory(backendId); |
| 142 | auto it = m_Backends.emplace(std::make_pair(backendId, createBackend())); |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 143 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 144 | IBackendInternal* backend = it.first->second.get(); |
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 | if (backend->SupportsTensorAllocatorAPI()) |
| 147 | { |
Narumol Prangnawarat | 11bd261 | 2019-08-13 10:26:53 +0100 | [diff] [blame] | 148 | auto workloadFactory = backend->CreateWorkloadFactory(m_TensorHandleFactoryRegistry); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 149 | m_WorkloadFactories.emplace( |
| 150 | std::make_pair(backendId, std::make_pair(std::move(workloadFactory), nullptr))); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | IBackendInternal::IMemoryManagerSharedPtr memoryManager = backend->CreateMemoryManager(); |
| 155 | auto workloadFactory = backend->CreateWorkloadFactory(memoryManager); |
| 156 | |
| 157 | m_WorkloadFactories.emplace( |
| 158 | std::make_pair(backendId, std::make_pair(std::move(workloadFactory), memoryManager))); |
| 159 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 160 | } |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | for (auto&& layer : order) |
| 164 | { |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 165 | auto& workloadFactory = GetWorkloadFactory(*layer); |
| 166 | |
| 167 | switch (layer->GetType()) |
| 168 | { |
| 169 | case LayerType::Input: |
| 170 | { |
| 171 | // If IsImportEnabled is true then we need to set IsMemoryManaged to false when creating TensorHandles |
| 172 | layer->CreateTensorHandles(m_TensorHandleFactoryRegistry, workloadFactory, !m_IsImportEnabled); |
| 173 | break; |
| 174 | } |
| 175 | default: |
| 176 | { |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 177 | // Look for the layer with 1 OutputSlot which has 1 connection and that connection is an Output Layer |
| 178 | // If Export is enabled disable memory management so we can export, otherwise we do a copy |
| 179 | if((layer->GetNumOutputSlots() == 1) && |
| 180 | (layer->GetOutputSlots()[0].GetNumConnections() == 1) && |
| 181 | (layer->GetOutputSlots()[0].GetConnection(0)->GetOwningLayer().GetType() == LayerType::Output)) |
| 182 | { |
| 183 | layer->CreateTensorHandles(m_TensorHandleFactoryRegistry, workloadFactory, !m_IsExportEnabled); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | layer->CreateTensorHandles(m_TensorHandleFactoryRegistry, workloadFactory); |
| 188 | } |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 189 | } |
| 190 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 193 | ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid(); |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 194 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 195 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 196 | if (timelineUtils) |
| 197 | { |
| 198 | timelineUtils->CreateTypedEntity(networkGuid, LabelsAndEventClasses::NETWORK_GUID); |
| 199 | } |
| 200 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 201 | //Then create workloads. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 202 | for (auto&& layer : order) |
| 203 | { |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 204 | if (timelineUtils) |
| 205 | { |
| 206 | // Add layer to the post-optimisation network structure |
| 207 | AddLayerStructure(timelineUtils, *layer, networkGuid); |
| 208 | } |
| 209 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 210 | const IWorkloadFactory& workloadFactory = GetWorkloadFactory(*layer); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 211 | |
| 212 | switch (layer->GetType()) |
| 213 | { |
| 214 | case LayerType::Input: |
| 215 | case LayerType::Output: |
| 216 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 217 | // Inputs and outputs are treated in a special way - see EnqueueInput() and EnqueueOutput(). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | default: |
| 221 | { |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 222 | auto workload = layer->CreateWorkload(workloadFactory); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 223 | |
| 224 | if (!workload) |
| 225 | { |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 226 | const char* const layerName = |
| 227 | layer->GetNameStr().length() != 0 ? layer->GetName() : "<Unnamed>"; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 228 | throw InvalidArgumentException(boost::str( |
| 229 | boost::format("No workload created for layer (name: '%1%' type: '%2%') (compute '%3%')") |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 230 | % layerName % static_cast<int>(layer->GetType()) % layer->GetBackendId().Get() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 231 | )); |
| 232 | } |
| 233 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 234 | if (timelineUtils) |
| 235 | { |
| 236 | // Add workload to the post-optimisation network structure |
| 237 | AddWorkloadStructure(timelineUtils, workload, *layer); |
| 238 | } |
| 239 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 240 | m_WorkloadQueue.push_back(move(workload)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 241 | // release the constant data in the layer.. |
| 242 | layer->ReleaseConstantData(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Narumol Prangnawarat | df31cfe | 2019-11-22 11:26:06 +0000 | [diff] [blame] | 248 | if (timelineUtils) |
| 249 | { |
| 250 | // Commit to send the post-optimisation network structure |
| 251 | timelineUtils->Commit(); |
| 252 | } |
| 253 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 254 | // Set up memory. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 255 | m_OptimizedNetwork->GetGraph().AllocateDynamicBuffers(); |
Derek Lamberti | f30f7d3 | 2019-04-09 10:25:02 +0100 | [diff] [blame] | 256 | |
| 257 | // Now that the intermediate tensor memory has been set-up, do any post allocation configuration for each workload. |
| 258 | for (auto& workload : m_WorkloadQueue) |
| 259 | { |
| 260 | workload->PostAllocationConfigure(); |
| 261 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 264 | void LoadedNetwork::SendNetworkStructure() |
| 265 | { |
| 266 | Graph& order = m_OptimizedNetwork->GetGraph().TopologicalSort(); |
| 267 | ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid(); |
| 268 | |
| 269 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 270 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
| 271 | |
| 272 | timelineUtils->CreateTypedEntity(networkGuid, LabelsAndEventClasses::NETWORK_GUID); |
| 273 | |
| 274 | for (auto&& layer : order) |
| 275 | { |
| 276 | // Add layer to the post-optimisation network structure |
| 277 | AddLayerStructure(timelineUtils, *layer, networkGuid); |
| 278 | switch (layer->GetType()) |
| 279 | { |
| 280 | case LayerType::Input: |
| 281 | case LayerType::Output: |
| 282 | { |
| 283 | // Inputs and outputs are treated in a special way - see EnqueueInput() and EnqueueOutput(). |
| 284 | break; |
| 285 | } |
| 286 | default: |
| 287 | { |
| 288 | for (auto& workload : m_WorkloadQueue) |
| 289 | { |
| 290 | // Add workload to the post-optimisation network structure |
| 291 | AddWorkloadStructure(timelineUtils, workload, *layer); |
| 292 | } |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | // Commit to send the post-optimisation network structure |
| 298 | timelineUtils->Commit(); |
| 299 | } |
| 300 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 301 | TensorInfo LoadedNetwork::GetInputTensorInfo(LayerBindingId layerId) const |
| 302 | { |
| 303 | for (auto&& inputLayer : m_OptimizedNetwork->GetGraph().GetInputLayers()) |
| 304 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 305 | 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] | 306 | if (inputLayer->GetBindingId() == layerId) |
| 307 | { |
| 308 | return inputLayer->GetOutputSlot(0).GetTensorInfo(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | throw InvalidArgumentException(boost::str(boost::format("No input layer is associated with id %1%") % layerId)); |
| 313 | } |
| 314 | |
| 315 | TensorInfo LoadedNetwork::GetOutputTensorInfo(LayerBindingId layerId) const |
| 316 | { |
| 317 | for (auto&& outputLayer : m_OptimizedNetwork->GetGraph().GetOutputLayers()) |
| 318 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 319 | ARMNN_ASSERT_MSG(outputLayer->GetNumInputSlots() == 1, "Output layer should have exactly 1 input slot"); |
| 320 | 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] | 321 | if (outputLayer->GetBindingId() == layerId) |
| 322 | { |
| 323 | return outputLayer->GetInputSlot(0).GetConnection()->GetTensorInfo(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | throw InvalidArgumentException(boost::str(boost::format("No output layer is associated with id %1%") % layerId)); |
| 328 | } |
| 329 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 330 | const IWorkloadFactory& LoadedNetwork::GetWorkloadFactory(const Layer& layer) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 331 | { |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 332 | const IWorkloadFactory* workloadFactory = nullptr; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 333 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 334 | auto it = m_WorkloadFactories.find(layer.GetBackendId()); |
| 335 | if (it == m_WorkloadFactories.end()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 336 | { |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 337 | throw RuntimeException( |
| 338 | boost::str( |
| 339 | boost::format("No workload factory for %1% to be used for layer: %2%") |
| 340 | % layer.GetBackendId().Get() |
| 341 | % layer.GetNameStr()), |
| 342 | CHECK_LOCATION()); |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 343 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 344 | |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 345 | workloadFactory = it->second.first.get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 346 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 347 | ARMNN_ASSERT_MSG(workloadFactory, "No workload factory"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 348 | |
| 349 | std::string reasonIfUnsupported; |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 350 | ARMNN_ASSERT_MSG(IWorkloadFactory::IsLayerSupported(layer, {}, reasonIfUnsupported), |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 351 | "Factory does not support layer"); |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 352 | IgnoreUnused(reasonIfUnsupported); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 353 | return *workloadFactory; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | namespace { |
| 357 | |
| 358 | // Non-copyable class owning accelerator-specific tensor data. |
| 359 | class TensorPin |
| 360 | { |
| 361 | public: |
| 362 | TensorPin(std::unique_ptr<ITensorHandle> handle, const TensorInfo& info, LayerBindingId id) |
| 363 | : m_TensorHandle(std::move(handle)) |
| 364 | , m_TensorInfo(info) |
| 365 | , m_Id(id) |
| 366 | { |
| 367 | } |
| 368 | |
| 369 | ITensorHandle* GetTensorHandle() const { return m_TensorHandle.get(); } |
| 370 | const TensorInfo& GetTensorInfo() const { return m_TensorInfo; } |
| 371 | LayerBindingId GetBindingId() const { return m_Id; } |
| 372 | |
| 373 | private: |
| 374 | std::unique_ptr<ITensorHandle> m_TensorHandle; |
| 375 | TensorInfo m_TensorInfo; |
| 376 | LayerBindingId m_Id; |
| 377 | }; |
| 378 | |
| 379 | static const TensorPin& GetTensorPin(LayerBindingId id, |
| 380 | const std::vector<TensorPin>& pins, |
| 381 | char const* bindingPointDesc) |
| 382 | { |
| 383 | auto it = std::find_if(pins.begin(), pins.end(), |
| 384 | [id](const TensorPin& pin) |
| 385 | { |
| 386 | return pin.GetBindingId() == id; |
| 387 | }); |
| 388 | |
| 389 | if (it != pins.end()) |
| 390 | { |
| 391 | return *it; |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | throw InvalidArgumentException(boost::str( |
| 396 | boost::format("No tensor supplied for %1% %2%") % bindingPointDesc % id)); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Stores data that needs to be kept accessible for the entire execution of a workload. |
| 401 | class WorkloadData |
| 402 | { |
| 403 | public: |
| 404 | WorkloadData(const InputTensors& inputTensors, const OutputTensors& outputTensors) |
| 405 | { |
| 406 | m_InputTensorPins.reserve(inputTensors.size()); |
| 407 | m_OutputTensorPins.reserve(outputTensors.size()); |
| 408 | |
| 409 | for (auto inputTensorPair : inputTensors) |
| 410 | { |
| 411 | auto inputTensor = inputTensorPair.second; |
| 412 | |
| 413 | std::unique_ptr<ITensorHandle> tensorHandle = |
| 414 | std::make_unique<ConstPassthroughCpuTensorHandle>(inputTensor.GetInfo(),inputTensor.GetMemoryArea()); |
| 415 | LayerBindingId layerId = inputTensorPair.first; |
| 416 | |
| 417 | m_InputTensorPins.emplace_back(std::move(tensorHandle), inputTensor.GetInfo(), layerId); |
| 418 | } |
| 419 | |
| 420 | for (auto outputTensorPair : outputTensors) |
| 421 | { |
| 422 | auto outputTensor = outputTensorPair.second; |
| 423 | |
| 424 | std::unique_ptr<ITensorHandle> tensorHandle = |
| 425 | std::make_unique<PassthroughCpuTensorHandle>(outputTensor.GetInfo(), outputTensor.GetMemoryArea()); |
| 426 | LayerBindingId layerId = outputTensorPair.first; |
| 427 | |
| 428 | m_OutputTensorPins.emplace_back(std::move(tensorHandle), outputTensor.GetInfo(), layerId); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | const TensorPin& GetInputTensorPin(LayerBindingId id) const |
| 433 | { |
| 434 | return GetTensorPin(id, m_InputTensorPins, "input"); |
| 435 | } |
| 436 | |
| 437 | const TensorPin& GetOutputTensorPin(LayerBindingId id) const |
| 438 | { |
| 439 | return GetTensorPin(id, m_OutputTensorPins, "output"); |
| 440 | } |
| 441 | |
| 442 | private: |
| 443 | |
| 444 | std::vector<TensorPin> m_InputTensorPins; |
| 445 | std::vector<TensorPin> m_OutputTensorPins; |
| 446 | }; |
| 447 | |
| 448 | } |
| 449 | |
| 450 | Status LoadedNetwork::EnqueueWorkload(const InputTensors& inputTensors, |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 451 | const OutputTensors& outputTensors) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 452 | { |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 453 | const Graph& graph = m_OptimizedNetwork->GetGraph(); |
| 454 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 455 | // Walk graph to determine the order of execution. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 456 | if (graph.GetNumLayers() < 2) |
| 457 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 458 | ARMNN_LOG(warning) << "IRuntime::EnqueueWorkload()::Less than two nodes in graph"; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 459 | return Status::Failure; |
| 460 | } |
| 461 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 462 | // Data that must be kept alive for the entire execution of the workload. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 463 | WorkloadData workloadData(inputTensors, outputTensors); |
| 464 | |
| 465 | if (graph.GetNumInputs() != inputTensors.size()) |
| 466 | { |
| 467 | throw InvalidArgumentException("Number of inputs provided does not match network."); |
| 468 | } |
| 469 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 470 | // 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] | 471 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 472 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "PrepareInputs"); |
| 473 | m_InputQueue.clear(); |
| 474 | m_InputQueue.reserve(graph.GetNumInputs()); |
| 475 | for (const BindableLayer* inputLayer : graph.GetInputLayers()) |
| 476 | { |
| 477 | const TensorPin& pin = workloadData.GetInputTensorPin(inputLayer->GetBindingId()); |
| 478 | EnqueueInput(*inputLayer, pin.GetTensorHandle(), pin.GetTensorInfo()); |
| 479 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 480 | } |
| 481 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 482 | // 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] | 483 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 484 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "PrepareOutputs"); |
| 485 | m_OutputQueue.clear(); |
| 486 | m_OutputQueue.reserve(graph.GetNumOutputs()); |
| 487 | for (const BindableLayer* outputLayer : graph.GetOutputLayers()) |
| 488 | { |
| 489 | const TensorPin& pin = workloadData.GetOutputTensorPin(outputLayer->GetBindingId()); |
| 490 | EnqueueOutput(*outputLayer, pin.GetTensorHandle(), pin.GetTensorInfo()); |
| 491 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 494 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 495 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
| 496 | ProfilingGuid inferenceGuid = m_ProfilingService.GetNextGuid(); |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 497 | if (timelineUtils) |
| 498 | { |
| 499 | // Add inference timeline trace if profiling is enabled. |
| 500 | ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid(); |
| 501 | timelineUtils->CreateTypedEntity(inferenceGuid, LabelsAndEventClasses::INFERENCE_GUID); |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 502 | timelineUtils->CreateRelationship(ProfilingRelationshipType::RetentionLink, |
| 503 | networkGuid, |
| 504 | inferenceGuid, |
| 505 | LabelsAndEventClasses::EXECUTION_OF_GUID); |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 506 | timelineUtils->RecordEvent(inferenceGuid, LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS); |
| 507 | } |
| 508 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 509 | bool executionSucceeded = true; |
| 510 | |
| 511 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 512 | if (m_ProfilingService.IsProfilingEnabled()) |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 513 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 514 | m_ProfilingService.IncrementCounterValue(armnn::profiling::INFERENCES_RUN); |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 515 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 516 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Execute"); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 517 | ARMNN_SCOPED_HEAP_PROFILING("Executing"); |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 518 | executionSucceeded = Execute(timelineUtils, inferenceGuid); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 519 | } |
| 520 | |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 521 | if (timelineUtils) |
| 522 | { |
| 523 | // Add end of life of the inference timeline if profiling is enabled. |
| 524 | timelineUtils->RecordEvent(inferenceGuid, LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS); |
| 525 | timelineUtils->Commit(); |
| 526 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 527 | return executionSucceeded ? Status::Success : Status::Failure; |
| 528 | } |
| 529 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 530 | void LoadedNetwork::EnqueueInput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 531 | { |
| 532 | if (layer.GetType() != LayerType::Input) |
| 533 | { |
| 534 | throw InvalidArgumentException("EnqueueInput: given layer not an InputLayer"); |
| 535 | } |
| 536 | |
| 537 | if (tensorHandle == nullptr) |
| 538 | { |
| 539 | throw InvalidArgumentException("EnqueueInput: tensorHandle must not be NULL"); |
| 540 | } |
| 541 | |
| 542 | InputQueueDescriptor inputQueueDescriptor; |
| 543 | WorkloadInfo info; |
| 544 | |
| 545 | inputQueueDescriptor.m_Inputs.push_back(tensorHandle); |
| 546 | info.m_InputTensorInfos.push_back(tensorInfo); |
| 547 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 548 | 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] | 549 | const OutputHandler& handler = layer.GetOutputHandler(); |
| 550 | const TensorInfo& outputTensorInfo = handler.GetTensorInfo(); |
| 551 | ITensorHandle* outputTensorHandle = handler.GetData(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 552 | ARMNN_ASSERT_MSG(outputTensorHandle != nullptr, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 553 | "Data should have been allocated."); |
| 554 | inputQueueDescriptor.m_Outputs.push_back(outputTensorHandle); |
| 555 | info.m_OutputTensorInfos.push_back(outputTensorInfo); |
| 556 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 557 | MemorySourceFlags importFlags = outputTensorHandle->GetImportFlags(); |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 558 | if (m_IsImportEnabled) // Try import the input tensor |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 559 | { |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 560 | if(CheckFlag(importFlags, MemorySource::Malloc) ) |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 561 | { |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 562 | // This assumes a CPU Tensor handle |
| 563 | void* mem = tensorHandle->Map(false); |
| 564 | if (outputTensorHandle->Import(mem, MemorySource::Malloc)) |
| 565 | { |
| 566 | tensorHandle->Unmap(); |
| 567 | return; // No need for a workload since the import has been done. |
| 568 | } |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 569 | tensorHandle->Unmap(); |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 570 | throw MemoryImportException("EnqueueInput: Memory Import failed"); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 571 | } |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 572 | else |
| 573 | { |
| 574 | throw MemoryImportException("EnqueueInput: Memory Import failed, backend does not support Import"); |
| 575 | } |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 576 | } |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 577 | else |
| 578 | { |
| 579 | // Create a mem copy workload for input since we did not import |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 580 | std::unique_ptr<IWorkload> inputWorkload = std::make_unique<CopyMemGenericWorkload>(inputQueueDescriptor, info); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 581 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 582 | ARMNN_ASSERT_MSG(inputWorkload, "No input workload created"); |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 583 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 584 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 585 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 586 | if (timelineUtils) |
| 587 | { |
| 588 | // Add Input Workload to the post-optimisation network structure |
| 589 | AddWorkloadStructure(timelineUtils, inputWorkload, layer); |
| 590 | timelineUtils->Commit(); |
| 591 | } |
| 592 | |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 593 | m_InputQueue.push_back(move(inputWorkload)); |
| 594 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 595 | } |
| 596 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 597 | void LoadedNetwork::EnqueueOutput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 598 | { |
| 599 | if (layer.GetType() != LayerType::Output) |
| 600 | { |
| 601 | throw InvalidArgumentException("EnqueueOutput: given layer not an OutputLayer"); |
| 602 | } |
| 603 | |
| 604 | if (tensorHandle == nullptr) |
| 605 | { |
| 606 | throw InvalidArgumentException("EnqueueOutput: tensorHandle must not be NULL"); |
| 607 | } |
| 608 | |
| 609 | OutputQueueDescriptor outputQueueDescriptor; |
| 610 | WorkloadInfo info; |
| 611 | |
| 612 | outputQueueDescriptor.m_Outputs.push_back(tensorHandle); |
| 613 | info.m_OutputTensorInfos.push_back(tensorInfo); |
| 614 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 615 | ARMNN_ASSERT_MSG(layer.GetNumInputSlots() == 1, "Output Layer should have exactly one input."); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 616 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 617 | // Gets the output handler from the previous node. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 618 | const OutputHandler& outputHandler = layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetOutputHandler(); |
| 619 | |
| 620 | const TensorInfo& inputTensorInfo = outputHandler.GetTensorInfo(); |
| 621 | ITensorHandle* inputTensorHandle = outputHandler.GetData(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 622 | ARMNN_ASSERT_MSG(inputTensorHandle != nullptr, "Data should have been allocated."); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 623 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 624 | // Try import the output tensor. |
| 625 | // Note: We can only import the output pointer if all of the following hold true: |
| 626 | // a) The imported pointer is aligned sufficiently |
| 627 | // b) The tensor has zero padding |
| 628 | // c) There is only one connection to the OutputSlot and it is to an OutputLayer. |
| 629 | // 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] | 630 | // e) m_IsExportEnabled must be set to true |
| 631 | if (m_IsExportEnabled && (layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetNumConnections() == 1)) |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 632 | { |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 633 | if(layer.GetInputSlots()[0].GetConnectedOutputSlot()->GetOwningLayer().GetType() != LayerType::Input) |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 634 | { |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 635 | MemorySourceFlags importFlags = inputTensorHandle->GetImportFlags(); |
| 636 | if (CheckFlag(importFlags, MemorySource::Malloc)) |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 637 | { |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 638 | void *mem = tensorHandle->Map(false); |
| 639 | bool importOk = inputTensorHandle->Import(mem, MemorySource::Malloc); |
| 640 | tensorHandle->Unmap(); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 641 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 642 | if (importOk) |
| 643 | { |
| 644 | // Insert synchronization workload |
| 645 | MemSyncQueueDescriptor syncDesc; |
| 646 | syncDesc.m_Inputs.push_back(inputTensorHandle); |
| 647 | info.m_InputTensorInfos.push_back(inputTensorInfo); |
| 648 | auto syncWorkload = std::make_unique<SyncMemGenericWorkload>(syncDesc, info); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 649 | ARMNN_ASSERT_MSG(syncWorkload, "No sync workload created"); |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 650 | m_OutputQueue.push_back(move(syncWorkload)); |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 651 | } |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 652 | else |
| 653 | { |
| 654 | throw MemoryExportException("EnqueueOutput: Memory Export failed"); |
| 655 | } |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 656 | } |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 657 | else |
| 658 | { |
| 659 | throw MemoryExportException("EnqueueOutput: Memory Export failed, backend does not support Export"); |
| 660 | } |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | throw MemoryExportException("EnqueueOutput: Memory Export failed, attempting to export Input Layer"); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 665 | } |
| 666 | } |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 667 | else |
| 668 | { |
Ferran Balaguer | 83239f9 | 2019-09-19 11:49:25 +0100 | [diff] [blame] | 669 | // If we got here then we didn't export the memory, so add an output workload which performs a memcopy. |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 670 | outputQueueDescriptor.m_Inputs.push_back(inputTensorHandle); |
| 671 | info.m_InputTensorInfos.push_back(inputTensorInfo); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 672 | |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 673 | std::unique_ptr<IWorkload> outputWorkload = |
| 674 | std::make_unique<CopyMemGenericWorkload>(outputQueueDescriptor, info); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 675 | ARMNN_ASSERT_MSG(outputWorkload, "No output workload created"); |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 676 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 677 | std::unique_ptr<TimelineUtilityMethods> timelineUtils = |
| 678 | TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); |
Narumol Prangnawarat | aa68e01 | 2019-11-29 17:17:43 +0000 | [diff] [blame] | 679 | if (timelineUtils) |
| 680 | { |
| 681 | // Add Output Workload to the post-optimisation network structure |
| 682 | AddWorkloadStructure(timelineUtils, outputWorkload, layer); |
| 683 | timelineUtils->Commit(); |
| 684 | } |
| 685 | |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 686 | m_OutputQueue.push_back(move(outputWorkload)); |
| 687 | } |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 690 | void LoadedNetwork::AllocateWorkingMemory(std::lock_guard<std::mutex>& lock) |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 691 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 692 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Working Memory Allocation"); |
| 693 | |
| 694 | // this unused parameter makes sure we can only call this function with a valid lock |
| 695 | IgnoreUnused(lock); |
| 696 | |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 697 | if (m_IsWorkingMemAllocated) |
| 698 | { |
| 699 | return; |
| 700 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 701 | for (auto&& workloadFactory : m_WorkloadFactories) |
| 702 | { |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 703 | IBackendInternal::IMemoryManagerSharedPtr memoryManager = workloadFactory.second.second; |
| 704 | if (memoryManager) |
| 705 | { |
| 706 | memoryManager->Acquire(); |
| 707 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 708 | } |
Narumol Prangnawarat | 11bd261 | 2019-08-13 10:26:53 +0100 | [diff] [blame] | 709 | m_TensorHandleFactoryRegistry.AquireMemory(); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 710 | m_IsWorkingMemAllocated = true; |
| 711 | } |
| 712 | |
| 713 | void LoadedNetwork::FreeWorkingMemory() |
| 714 | { |
Matthew Bentham | 2a326b5 | 2019-03-19 10:11:01 +0000 | [diff] [blame] | 715 | std::lock_guard<std::mutex> lockGuard(m_WorkingMemMutex); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 716 | if (!m_IsWorkingMemAllocated) |
| 717 | { |
| 718 | return; |
| 719 | } |
| 720 | // 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] | 721 | for (auto&& workloadFactory : m_WorkloadFactories) |
| 722 | { |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 723 | IBackendInternal::IMemoryManagerSharedPtr memoryManager = workloadFactory.second.second; |
| 724 | if (memoryManager) |
| 725 | { |
| 726 | memoryManager->Release(); |
| 727 | } |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 728 | } |
Narumol Prangnawarat | 11bd261 | 2019-08-13 10:26:53 +0100 | [diff] [blame] | 729 | m_TensorHandleFactoryRegistry.ReleaseMemory(); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 730 | m_IsWorkingMemAllocated = false; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 731 | } |
| 732 | |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 733 | bool LoadedNetwork::Execute(std::unique_ptr<TimelineUtilityMethods>& timelineUtils, |
| 734 | profiling::ProfilingGuid inferenceGuid) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 735 | { |
| 736 | bool success = true; |
| 737 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 738 | auto Fail = [&](const std::exception& error) |
| 739 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 740 | 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] | 741 | success = false; |
| 742 | }; |
| 743 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 744 | try |
| 745 | { |
Matthew Bentham | 2a326b5 | 2019-03-19 10:11:01 +0000 | [diff] [blame] | 746 | std::lock_guard<std::mutex> lockGuard(m_WorkingMemMutex); |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 747 | AllocateWorkingMemory(lockGuard); |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 748 | |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 749 | ProfilingDynamicGuid workloadInferenceID(0); |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 750 | auto ExecuteQueue = [&timelineUtils, &workloadInferenceID, &inferenceGuid](WorkloadQueue& queue) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 751 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 752 | for (auto& workload : queue) |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 753 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 754 | if(timelineUtils) |
| 755 | { |
| 756 | workloadInferenceID = timelineUtils->RecordWorkloadInferenceAndStartOfLifeEvent(workload->GetGuid(), |
| 757 | inferenceGuid); |
| 758 | } |
| 759 | workload->Execute(); |
| 760 | if(timelineUtils) |
| 761 | { |
| 762 | timelineUtils->RecordEndOfLifeEvent(workloadInferenceID); |
| 763 | } |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 764 | } |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 765 | }; |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 766 | |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 767 | ExecuteQueue(m_InputQueue); |
| 768 | ExecuteQueue(m_WorkloadQueue); |
| 769 | ExecuteQueue(m_OutputQueue); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 770 | } |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 771 | catch (const RuntimeException& error) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 772 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 773 | Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 774 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 775 | catch (const std::runtime_error& error) |
| 776 | { |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 777 | Fail(error); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 778 | } |
| 779 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 780 | return success; |
| 781 | } |
| 782 | |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 783 | void LoadedNetwork::RegisterDebugCallback(const DebugCallbackFunction& func) |
| 784 | { |
| 785 | for (auto&& workloadPtr: m_WorkloadQueue) |
| 786 | { |
| 787 | workloadPtr.get()->RegisterDebugCallback(func); |
| 788 | } |
| 789 | } |
| 790 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 791 | } |