ArmNN
 22.02
LoadedNetwork.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 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>
20 
21 
22 #include <ProfilingService.hpp>
24 
25 #include <common/include/LabelsAndEventClasses.hpp>
26 
27 #include <mutex>
28 #include <condition_variable>
29 #include <unordered_map>
30 
31 namespace cl
32 {
33 class Context;
34 class CommandQueue;
35 class Device;
36 }
37 
38 namespace armnn
39 {
40 
42 {
43 public:
44  using WorkloadQueue = std::vector<std::unique_ptr<IWorkload>>;
45 
47  {
48  FreeWorkingMemory();
49  }
50 
51  /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
52  /// overlapped Execution by calling this function from different threads.
53  std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
54 
55  TensorInfo GetInputTensorInfo(LayerBindingId layerId) const;
56  TensorInfo GetOutputTensorInfo(LayerBindingId layerId) const;
57 
58  std::vector<ImportedInputId> ImportInputs(const InputTensors& inputTensors,
59  MemorySource forceImportMemorySource = MemorySource::Undefined);
60  std::vector<ImportedOutputId> ImportOutputs(const OutputTensors& outputTensors,
61  MemorySource forceImportMemorySource = MemorySource::Undefined);
62 
63  void ClearImportedInputs(const std::vector<ImportedInputId> inputIds);
64  void ClearImportedOutputs(const std::vector<ImportedOutputId> outputIds);
65 
66  /// Single thread execution of the loaded network
67  Status EnqueueWorkload(const InputTensors& inputTensors, const OutputTensors& outputTensors,
68  std::vector<ImportedInputId> preImportedInputIds = {},
69  std::vector<ImportedOutputId> preImportedOutputIds = {});
70 
71  /// Thread safe execution of the loaded network
72  Status Execute(const InputTensors& inputTensors,
73  const OutputTensors& outputTensors,
74  IWorkingMemHandle& workingMemHandle,
75  std::vector<ImportedInputId> preImportedInputs = {},
76  std::vector<ImportedOutputId> preImportedOutputs = {});
77 
78  static std::unique_ptr<LoadedNetwork> MakeLoadedNetwork(std::unique_ptr<IOptimizedNetwork> net,
79  std::string& errorMessage,
80  const INetworkProperties& networkProperties,
81  profiling::ProfilingService& profilingService);
82 
83  // NOTE we return by reference as the purpose of this method is only to provide
84  // access to the private m_Profiler and in theory we should not need to increment
85  // the shared_ptr's reference counter
86  const std::shared_ptr<IProfiler>& GetProfiler() const { return m_OptimizedNetwork->GetProfiler(); }
87 
88  void FreeWorkingMemory();
89 
90  void RegisterDebugCallback(const DebugCallbackFunction& func);
91 
92  void SendNetworkStructure();
93 
95  {
96  return m_NetworkProperties.m_AsyncEnabled;
97  }
98 
99  profiling::ProfilingGuid GetNetworkGuid();
100 
101 private:
102 
103 
104  void AllocateWorkingMemory(std::lock_guard<std::mutex>& lock);
105  void AllocateAndExecuteConstantWorkloads();
106  void AllocateAndExecuteConstantWorkloadsAsync();
107 
108  std::unordered_map<LayerGuid, std::unique_ptr<IWorkload>> m_ConstantWorkloads;
109  std::unordered_map<LayerGuid, ITensorHandle*> m_ConstantTensorHandles;
110 
111  std::unique_ptr<IMemoryOptimizerStrategy> m_ConstantStrategy = std::make_unique<SingleAxisPriorityList>();
112 
113  LoadedNetwork(std::unique_ptr<IOptimizedNetwork> net,
114  const INetworkProperties& networkProperties,
115  profiling::ProfilingService& profilingService);
116 
117  void EnqueueInput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo);
118 
119  void EnqueueOutput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo);
120 
121  void EnqueueInput(const ConstTensor& inputTensor, ITensorHandle* inputTensorHandle);
122 
123  void ImportOutputTensor(const Tensor& outputTensor, ITensorHandle* outputTensorHandle);
124 
125  bool Execute(std::unique_ptr<profiling::TimelineUtilityMethods>& timelineUtils,
126  profiling::ProfilingGuid inferenceGuid);
127 
128  const IWorkloadFactory& GetWorkloadFactory(const Layer& layer) const;
129 
130  inline LayerBindingId ValidateImportedInputID(ImportedInputId id);
131  inline LayerBindingId ValidateImportedOutputID(ImportedOutputId id);
132 
133  void CreateMemoryProfile();
134  void CreateMemoryProfileAsync();
135 
136  std::unique_ptr<MemoryManager> CreateExternalMemoryManger(
137  std::vector<std::pair<std::shared_ptr<TensorMemory>, MemorySource>>& tensorMemory);
138 
139  using BackendPtrMap = std::unordered_map<BackendId, IBackendInternalUniquePtr>;
140 
141  BackendPtrMap m_Backends;
142  std::vector<IBackendInternal::IMemoryManagerSharedPtr> m_BackendMemoryMangers;
143 
144  using WorkloadFactoryMap = std::unordered_map<BackendId, IBackendInternal::IWorkloadFactoryPtr>;
145  WorkloadFactoryMap m_WorkloadFactories;
146 
147  std::unique_ptr<IOptimizedNetwork> m_OptimizedNetwork;
148 
149  WorkloadQueue m_InputQueue;
150  WorkloadQueue m_WorkloadQueue;
151  WorkloadQueue m_OutputQueue;
152 
153  mutable std::mutex m_WorkingMemMutex;
154 
155  bool m_IsWorkingMemAllocated = false;
156 
157  INetworkProperties m_NetworkProperties;
158 
159  TensorHandleFactoryRegistry m_TensorHandleFactoryRegistry;
160 
161  profiling::ProfilingService& m_ProfilingService;
162 
163  struct ImportedTensorHandlePin
164  {
165  ImportedTensorHandlePin()
166  {}
167 
168  ImportedTensorHandlePin(LayerBindingId layerBindingId,
169  std::unique_ptr<ITensorHandle> tensorHandle)
170  : m_LayerBindingId(layerBindingId)
171  , m_TensorHandle(std::move(tensorHandle))
172  {}
173 
174  ImportedTensorHandlePin(ImportedTensorHandlePin&&) = default;
175 
176  ~ImportedTensorHandlePin()
177  {
178  if (m_TensorHandle)
179  {
180  m_TensorHandle->Unimport();
181  }
182  }
183 
184  LayerBindingId m_LayerBindingId;
185  std::unique_ptr<ITensorHandle> m_TensorHandle;
186  };
187 
188  std::vector<ImportedTensorHandlePin> m_PreImportedInputHandles;
189  std::vector<ImportedTensorHandlePin> m_PreImportedOutputHandles;
190 
191  ImportedInputId m_CurImportedInputId = 0;
192  ImportedInputId m_CurImportedOutputId = 0;
193 
194  std::unordered_map<BackendId, std::vector<MemBlock>> m_MemBlockMap;
195  std::unordered_map<BackendId, std::vector<MemBin>> m_MemBinMap;
196 
197  std::vector<ITensorHandle*> m_Tensorhandles;
198 
199  std::vector<std::pair<std::shared_ptr<TensorMemory>, MemorySource>> m_TensorMemory;
200 
201  std::unique_ptr<MemoryManager> m_ExternalMemoryManager;
202 
203  std::unordered_map<BackendId, bool> m_SupportsExternallyManagedMemory;
204 
205  // A set of vectors to record the workload queue indexes and their corresponding Input/Output Slot indexes
206  // which are connected to Inputs and Outputs for the network.
207  struct WorkloadIndices
208  {
209  unsigned int m_WorkloadIndex;
210  unsigned int m_SlotIndex;
211  };
212 
213  struct OutputWorkloadIndices
214  {
215  WorkloadIndices m_OutputSlotIndices;
216  std::vector<WorkloadIndices> m_InputSlotIndices;
217  };
218  std::unordered_map<LayerBindingId, std::vector<WorkloadIndices>> m_InputWorkloadSlotPairs;
219  std::unordered_map<LayerBindingId, OutputWorkloadIndices> m_OutputWorkloadSlotPairs;
220  std::vector<bool> m_IsInputImported;
221  std::vector<bool> m_IsOutputImported;
222 
223 };
224 
225 }
unsigned int ImportedOutputId
Definition: Types.hpp:279
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:392
Copyright (c) 2021 ARM Limited and Contributors.
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:371
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:277
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:319
int NetworkId
Definition: IRuntime.hpp:25
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:393
Status
enumeration
Definition: Types.hpp:29
std::vector< std::unique_ptr< IWorkload > > WorkloadQueue
unsigned int ImportedInputId
Definition: Types.hpp:278
const std::shared_ptr< IProfiler > & GetProfiler() const
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:217