ArmNN
 25.11
Loading...
Searching...
No Matches
LoadedNetwork.hpp
Go to the documentation of this file.
1//
2// Copyright © 2017, 2024 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
7#include "Network.hpp"
8#include "LayerFwd.hpp"
9#include "Profiling.hpp"
10
11#include <armnn/Tensor.hpp>
12
17
22
23#include <client/include/IProfilingService.hpp>
24#include <client/include/TimelineUtilityMethods.hpp>
25
26#include <common/include/LabelsAndEventClasses.hpp>
27
28#include <mutex>
29#include <condition_variable>
30#include <unordered_map>
31
32namespace cl
33{
34class Context;
35class CommandQueue;
36class Device;
37}
38
39namespace armnn
40{
41
42class LoadedNetwork
43{
44public:
45 using WorkloadQueue = std::vector<std::unique_ptr<IWorkload>>;
46
51
54
55 std::vector<ImportedInputId> ImportInputs(const InputTensors& inputTensors,
56 MemorySource forceImportMemorySource = MemorySource::Undefined);
57 std::vector<ImportedOutputId> ImportOutputs(const OutputTensors& outputTensors,
58 MemorySource forceImportMemorySource = MemorySource::Undefined);
59
60 void ClearImportedInputs(const std::vector<ImportedInputId> inputIds);
61 void ClearImportedOutputs(const std::vector<ImportedOutputId> outputIds);
62
63 /// Single thread execution of the loaded network
64 Status EnqueueWorkload(const InputTensors& inputTensors, const OutputTensors& outputTensors,
65 std::vector<ImportedInputId> preImportedInputIds = {},
66 std::vector<ImportedOutputId> preImportedOutputIds = {});
67
68 static std::unique_ptr<LoadedNetwork> MakeLoadedNetwork(std::unique_ptr<IOptimizedNetwork> net,
69 std::string& errorMessage,
70 const INetworkProperties& networkProperties,
71 arm::pipe::IProfilingService* profilingService);
72
73 // NOTE we return by reference as the purpose of this method is only to provide
74 // access to the private m_Profiler and in theory we should not need to increment
75 // the shared_ptr's reference counter
76 const std::shared_ptr<IProfiler>& GetProfiler() const { return m_OptimizedNetwork->GetProfiler(); }
77
78 void FreeWorkingMemory();
79
81
82 void SendNetworkStructure(arm::pipe::IProfilingService& profilingService);
83
84 arm::pipe::ProfilingGuid GetNetworkGuid();
85
86private:
87
88
89 void AllocateWorkingMemory(
90#if !defined(ARMNN_DISABLE_THREADS)
91 std::lock_guard<std::mutex>& lock
92#endif
93 );
94 void AllocateAndExecuteConstantWorkloads();
95
96 std::unordered_map<LayerGuid, std::unique_ptr<IWorkload>> m_ConstantWorkloads;
97 std::unordered_map<LayerGuid, ITensorHandle*> m_ConstantTensorHandles;
98
99 std::unique_ptr<IMemoryOptimizerStrategy> m_ConstantStrategy = std::make_unique<SingleAxisPriorityList>();
100
101 LoadedNetwork(std::unique_ptr<IOptimizedNetwork> net,
102 const INetworkProperties& networkProperties,
103 arm::pipe::IProfilingService* profilingService);
104
105 void EnqueueInput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo);
106
107 void EnqueueOutput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo);
108
109 void EnqueueInput(const ConstTensor& inputTensor, ITensorHandle* inputTensorHandle);
110
111 void ImportOutputTensor(const Tensor& outputTensor, ITensorHandle* outputTensorHandle);
112
113 bool Execute(std::unique_ptr<arm::pipe::TimelineUtilityMethods>& timelineUtils,
114 arm::pipe::ProfilingGuid inferenceGuid);
115
116 const IWorkloadFactory& GetWorkloadFactory(const Layer& layer) const;
117
118 inline LayerBindingId ValidateImportedInputID(ImportedInputId id);
119 inline LayerBindingId ValidateImportedOutputID(ImportedOutputId id);
120
121 void CreateMemoryProfile();
122
123 std::unique_ptr<MemoryManager> CreateExternalMemoryManger(
124 std::vector<std::pair<std::shared_ptr<TensorMemory>, MemorySource>>& tensorMemory);
125
126 using BackendPtrMap = std::unordered_map<BackendId, IBackendInternalUniquePtr>;
127
128 BackendPtrMap m_Backends;
129 std::vector<IBackendInternal::IMemoryManagerSharedPtr> m_BackendMemoryMangers;
130
131 using WorkloadFactoryMap = std::unordered_map<BackendId, IBackendInternal::IWorkloadFactoryPtr>;
132 WorkloadFactoryMap m_WorkloadFactories;
133
134 std::unique_ptr<IOptimizedNetwork> m_OptimizedNetwork;
135
136 WorkloadQueue m_InputQueue;
137 WorkloadQueue m_WorkloadQueue;
138 WorkloadQueue m_OutputQueue;
139
140#if !defined(ARMNN_DISABLE_THREADS)
141 mutable std::mutex m_WorkingMemMutex;
142#endif
143
144 bool m_IsWorkingMemAllocated = false;
145
146 INetworkProperties m_NetworkProperties;
147
148 TensorHandleFactoryRegistry m_TensorHandleFactoryRegistry;
149
150 // NOTE: raw pointer because the profiling service is controlled by the Runtime
151 arm::pipe::IProfilingService* m_ProfilingService;
152
153 struct ImportedTensorHandlePin
154 {
155 ImportedTensorHandlePin()
156 {}
157
158 ImportedTensorHandlePin(LayerBindingId layerBindingId,
159 std::unique_ptr<ITensorHandle> tensorHandle)
160 : m_LayerBindingId(layerBindingId)
161 , m_TensorHandle(std::move(tensorHandle))
162 {}
163
164 ImportedTensorHandlePin(ImportedTensorHandlePin&&) = default;
165
166 ~ImportedTensorHandlePin()
167 {
168 if (m_TensorHandle)
169 {
170 m_TensorHandle->Unimport();
171 }
172 }
173
174 LayerBindingId m_LayerBindingId;
175 std::unique_ptr<ITensorHandle> m_TensorHandle;
176 };
177
178 std::vector<ImportedTensorHandlePin> m_PreImportedInputHandles;
179 std::vector<ImportedTensorHandlePin> m_PreImportedOutputHandles;
180
181 std::unordered_map<BackendId, std::vector<MemBlock>> m_MemBlockMap;
182 std::unordered_map<BackendId, std::vector<MemBin>> m_MemBinMap;
183
184 std::vector<ITensorHandle*> m_Tensorhandles;
185
186 std::vector<std::pair<std::shared_ptr<TensorMemory>, MemorySource>> m_TensorMemory;
187
188 std::unique_ptr<MemoryManager> m_ExternalMemoryManager;
189
190 std::unordered_map<BackendId, bool> m_SupportsExternallyManagedMemory;
191
192 // A set of vectors to record the workload queue indexes and their corresponding Input/Output Slot indexes
193 // which are connected to Inputs and Outputs for the network.
194 struct WorkloadIndices
195 {
196 unsigned int m_WorkloadIndex;
197 unsigned int m_SlotIndex;
198 };
199
200 struct OutputWorkloadIndices
201 {
202 WorkloadIndices m_OutputSlotIndices;
203 std::vector<WorkloadIndices> m_InputSlotIndices;
204 };
205 std::unordered_map<LayerBindingId, std::vector<WorkloadIndices>> m_InputWorkloadSlotPairs;
206 std::unordered_map<LayerBindingId, OutputWorkloadIndices> m_OutputWorkloadSlotPairs;
207 std::vector<bool> m_IsInputImported;
208 std::vector<bool> m_IsOutputImported;
209
210};
211
212}
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition Tensor.hpp:330
void RegisterDebugCallback(const DebugCallbackFunction &func)
TensorInfo GetOutputTensorInfo(LayerBindingId layerId) const
std::vector< std::unique_ptr< IWorkload > > WorkloadQueue
std::vector< ImportedInputId > ImportInputs(const InputTensors &inputTensors, MemorySource forceImportMemorySource=MemorySource::Undefined)
Status EnqueueWorkload(const InputTensors &inputTensors, const OutputTensors &outputTensors, std::vector< ImportedInputId > preImportedInputIds={}, std::vector< ImportedOutputId > preImportedOutputIds={})
Single thread execution of the loaded network.
const std::shared_ptr< IProfiler > & GetProfiler() const
void ClearImportedInputs(const std::vector< ImportedInputId > inputIds)
std::vector< ImportedOutputId > ImportOutputs(const OutputTensors &outputTensors, MemorySource forceImportMemorySource=MemorySource::Undefined)
arm::pipe::ProfilingGuid GetNetworkGuid()
void SendNetworkStructure(arm::pipe::IProfilingService &profilingService)
void ClearImportedOutputs(const std::vector< ImportedOutputId > outputIds)
TensorInfo GetInputTensorInfo(LayerBindingId layerId) const
static std::unique_ptr< LoadedNetwork > MakeLoadedNetwork(std::unique_ptr< IOptimizedNetwork > net, std::string &errorMessage, const INetworkProperties &networkProperties, arm::pipe::IProfilingService *profilingService)
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition Tensor.hpp:322
Copyright (c) 2021 ARM Limited and Contributors.
MemorySource
Define the Memory Source to reduce copies.
Definition Types.hpp:246
std::function< void(LayerGuid guid, unsigned int slotIndex, ITensorHandle *tensorHandle)> DebugCallbackFunction
Define the type of callback for the Debug layer to call.
Definition Types.hpp:400
std::vector< std::unique_ptr< IWorkload > > WorkloadQueue
unsigned int ImportedInputId
Definition Types.hpp:312
Status
enumeration
Definition Types.hpp:43
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition Tensor.hpp:394
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition Types.hpp:311
unsigned int ImportedOutputId
Definition Types.hpp:313
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition Tensor.hpp:395