ArmNN
 22.05
IRuntime.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "BackendOptions.hpp"
8 #include "INetwork.hpp"
9 #include "IProfiler.hpp"
10 #include "IWorkingMemHandle.hpp"
12 #include "Tensor.hpp"
13 #include "Types.hpp"
14 #include "TypesUtils.hpp"
15 
18 
19 #include <client/include/ILocalPacketHandler.hpp>
20 
21 #include <memory>
22 #include <map>
23 
24 namespace armnn
25 {
26 
27 using NetworkId = int;
28 
30 
31 struct RuntimeImpl;
32 class IRuntime;
33 using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
34 
36 {
37  INetworkProperties(bool asyncEnabled,
38  MemorySource inputSource,
39  MemorySource outputSource,
40  bool profilingEnabled = false,
42  bool externalMemoryManagementEnabled = false)
43  : m_ImportEnabled(inputSource != MemorySource::Undefined),
44  m_ExportEnabled(outputSource != MemorySource::Undefined),
45  m_AsyncEnabled(asyncEnabled),
46  m_ProfilingEnabled(profilingEnabled),
47  m_OutputNetworkDetailsMethod(detailsMethod),
48  m_InputSource(inputSource),
49  m_OutputSource(outputSource),
50  m_ExternalMemoryManagementEnabled(externalMemoryManagementEnabled)
51  {}
52 
53  /// Deprecated and will be removed in future release.
54  const bool m_ImportEnabled;
55  /// Deprecated and will be removed in future release.
56  const bool m_ExportEnabled;
57 
58  const bool m_AsyncEnabled;
59 
60  const bool m_ProfilingEnabled;
61 
63 
66 
68 
69  virtual ~INetworkProperties() {}
70 };
71 
72 using namespace armnn::experimental;
73 
74 class IRuntime
75 {
76 public:
78  {
80  : m_GpuAccTunedParameters(nullptr)
81  , m_EnableGpuProfiling(false)
82  , m_DynamicBackendsPath("")
83  , m_ProtectedMode(false)
84  , m_CustomAllocatorMap()
85  , m_MemoryOptimizerStrategyMap()
86  {}
87 
88  /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
89  /// It will also be updated with new tuned parameters if it is configured to do so.
90  std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
91 
92  /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
94 
95  /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
96  /// Only a single path is allowed for the override
97  /// It defines the path to search for any [dynamic backend libraries](src/dynamic/README.md).
98  std::string m_DynamicBackendsPath;
99 
100  /// Setting this flag will allow the user to create the Runtime in protected mode.
101  /// It will run all the inferences on protected memory and will make sure that
102  /// INetworkProperties::m_ImportEnabled set to true with MemorySource::DmaBufProtected option
103  /// This requires that the backend supports Protected Memory and has an allocator capable of
104  /// allocating Protected Memory associated with it.
106 
107  /// @brief A map to define a custom memory allocator for specific backend Ids.
108  ///
109  /// @details A Custom Allocator is used for allocation of working memory in the backends.
110  /// Set this if you need to take control of how memory is allocated on a backend. Required for
111  /// Protected Mode in order to correctly allocate Protected Memory
112  ///
113  /// @note Only supported for GpuAcc
114  std::map<BackendId, std::shared_ptr<ICustomAllocator>> m_CustomAllocatorMap;
115 
116  /// @brief A map to define a custom memory optimizer strategy for specific backend Ids.
117  ///
118  /// @details A Memory Optimizer Strategy provides a solution to an abstract representation of
119  /// a network's memory requirements. This can also be used to return a pre-computed solution
120  /// for a specific network. Set this if you want to implement a Custom Memory Optimizer Strategy
121  /// for a given backend.
122  std::map<BackendId, std::shared_ptr<IMemoryOptimizerStrategy>> m_MemoryOptimizerStrategyMap;
123 
125  {
127  : m_EnableProfiling(false)
128  , m_TimelineEnabled(false)
129  , m_OutgoingCaptureFile("")
130  , m_IncomingCaptureFile("")
131  , m_FileOnly(false)
132  , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
133  , m_FileFormat("binary")
134  , m_LocalPacketHandlers()
135  {}
136 
137  /// Indicates whether external profiling is enabled or not.
139  /// Indicates whether external timeline profiling is enabled or not.
141  /// Path to a file in which outgoing timeline profiling messages will be stored.
143  /// Path to a file in which incoming timeline profiling messages will be stored.
145  /// Enable profiling output to file only.
147  /// The duration at which captured profiling messages will be flushed.
148  uint32_t m_CapturePeriod;
149  /// The format of the file used for outputting profiling data.
150  std::string m_FileFormat;
151  std::vector<arm::pipe::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
152  };
154 
155  /// Pass backend specific options.
156  ///
157  /// For example, to enable GpuAcc tuning add the following
158  /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
159  /// m_BackendOption.emplace_back(
160  /// BackendOptions{"GpuAcc",
161  /// {
162  /// {"TuningLevel", 2},
163  /// {"TuningFile", filename}
164  /// {"MemoryOptimizerStrategy", strategyname}
165  /// }
166  /// });
167  /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168  /// Execute representative workloads through the runtime to generate tuning data.
169  /// The tuning file is written once the runtime is destroyed
170 
171  /// To execute with the tuning data, start up with just the tuning file specified.
172  /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
173  /// m_BackendOption.emplace_back(
174  /// BackendOptions{"GpuAcc",
175  /// {
176  /// {"TuningFile", filename}
177  /// }
178  /// });
179  /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
180 
181  /// The following backend options are available:
182  /// AllBackends:
183  /// "MemoryOptimizerStrategy" : string [stategynameString]
184  /// (Existing Memory Optimizer Strategies: ConstantMemoryStrategy)
185  /// GpuAcc:
186  /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
187  /// "TuningFile" : string [filenameString]
188  /// "KernelProfilingEnabled" : bool [true | false]
189  std::vector<BackendOptions> m_BackendOptions;
190  };
191 
192  static IRuntime* CreateRaw(const CreationOptions& options);
193  static IRuntimePtr Create(const CreationOptions& options);
194  static void Destroy(IRuntime* runtime);
195 
196  /// Loads a complete network into the IRuntime.
197  /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
198  /// @param [in] network - Complete network to load into the IRuntime.
199  /// The runtime takes ownership of the network once passed in.
200  /// @return armnn::Status
201  Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
202 
203  /// Load a complete network into the IRuntime.
204  /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
205  /// @param [in] network Complete network to load into the IRuntime.
206  /// @param [out] errorMessage Error message if there were any errors.
207  /// The runtime takes ownership of the network once passed in.
208  /// @return armnn::Status
209  Status LoadNetwork(NetworkId& networkIdOut,
210  IOptimizedNetworkPtr network,
211  std::string& errorMessage);
212 
213  Status LoadNetwork(NetworkId& networkIdOut,
214  IOptimizedNetworkPtr network,
215  std::string& errorMessage,
216  const INetworkProperties& networkProperties);
217 
218  TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
219  TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
220 
221  /// ImportInputs separates the importing and mapping of InputTensors from network execution.
222  /// Allowing for a set of InputTensors to be imported and mapped once, but used in execution many times.
223  /// This function is not thread safe and must not be used while other threads are calling Execute().
224  /// Only compatible with AsyncEnabled networks and aligned memory import
225  std::vector<ImportedInputId> ImportInputs(NetworkId networkId, const InputTensors& inputTensors,
226  MemorySource forceImportMemorySource = MemorySource::Undefined);
227 
228  /// ImportOutputs separates the importing and mapping of OutputTensors from network execution.
229  /// Allowing for a set of OutputTensors to be imported and mapped once, but used in execution many times.
230  /// This function is not thread safe and must not be used while other threads are calling Execute().
231  /// Only compatible with AsyncEnabled networks and aligned memory import
232  std::vector<ImportedOutputId> ImportOutputs(NetworkId networkId, const OutputTensors& outputTensors,
233  MemorySource forceImportMemorySource = MemorySource::Undefined);
234 
235  /// Un-import and delete the imported InputTensor/s
236  /// This function is not thread safe and must not be used while other threads are calling Execute().
237  /// Only compatible with AsyncEnabled networks
238  void ClearImportedInputs(NetworkId networkId, const std::vector<ImportedInputId> inputIds);
239 
240  /// Un-import and delete the imported OutputTensor/s
241  /// This function is not thread safe and must not be used while other threads are calling Execute().
242  /// Only compatible with AsyncEnabled networks
243  void ClearImportedOutputs(NetworkId networkId, const std::vector<ImportedOutputId> outputIds);
244 
245  /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
246  Status EnqueueWorkload(NetworkId networkId,
247  const InputTensors& inputTensors,
248  const OutputTensors& outputTensors,
249  std::vector<ImportedInputId> preImportedInputIds = {},
250  std::vector<ImportedOutputId> preImportedOutputIds = {});
251 
252  /// This is an experimental function.
253  /// Evaluates a network using input in inputTensors and outputs filled into outputTensors.
254  /// This function performs a thread safe execution of the network. Returns once execution is complete.
255  /// Will block until this and any other thread using the same workingMem object completes.
256  Status Execute(IWorkingMemHandle& workingMemHandle,
257  const InputTensors& inputTensors,
258  const OutputTensors& outputTensors,
259  std::vector<ImportedInputId> preImportedInputs = {},
260  std::vector<ImportedOutputId> preImportedOutputs = {});
261 
262  /// Unloads a network from the IRuntime.
263  /// At the moment this only removes the network from the m_Impl->m_Network.
264  /// This might need more work in the future to be AndroidNN compliant.
265  /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
266  /// @return armnn::Status
267  Status UnloadNetwork(NetworkId networkId);
268 
269  const IDeviceSpec& GetDeviceSpec() const;
270 
271  /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
272  /// overlapped Execution by calling this function from different threads.
273  std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
274 
275  /// Gets the profiler corresponding to the given network id.
276  /// @param networkId The id of the network for which to get the profile.
277  /// @return A pointer to the requested profiler, or nullptr if not found.
278  const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
279 
280  /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
281  /// @param networkId The id of the network to register the callback.
282  /// @param func callback function to pass to the debug layer.
283  void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
284 
285 protected:
286  IRuntime();
287  IRuntime(const IRuntime::CreationOptions& options);
288  ~IRuntime();
289 
290  std::unique_ptr<RuntimeImpl> pRuntimeImpl;
291 };
292 
293 
294 /// The following API is replaced by the backend options API.
295 using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
296 
297 /// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
298 /// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
299 /// for all GPU workload execution.
300 ///
301 /// Can be created in two modes:
302 /// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
303 /// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
304 /// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
305 ///
306 /// The parameters can be loaded from and saved to a file so that you can first run a slow initial read-write
307 /// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
309 {
310 public:
311  enum class Mode
312  {
313  UseTunedParameters,
314  UpdateTunedParameters
315  };
316 
317  enum class TuningLevel
318  {
319  Rapid = 1,
320  Normal = 2,
321  Exhaustive = 3
322  };
323 
324  /// Creates an IClTunedParameters with the given mode.
325  /// @{
326  static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
327  static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
328  /// @}
329  static void Destroy(IGpuAccTunedParameters* params);
330 
331  /// Loads an existing set of tuned parameters from the given file.
332  /// If there is an error loading the file, an armnn::Exception is thrown.
333  virtual void Load(const char* filename) = 0;
334 
335  /// Saves the current set of tuned parameters to the given file.
336  /// If there is an error saving to the file, an armnn::Exception is thrown.
337  virtual void Save(const char* filename) const = 0;
338 
339 protected:
341 };
342 
343 } // namespace armnn
const MemorySource m_InputSource
Definition: IRuntime.hpp:64
const bool m_ImportEnabled
Deprecated and will be removed in future release.
Definition: IRuntime.hpp:54
std::string m_OutgoingCaptureFile
Path to a file in which outgoing timeline profiling messages will be stored.
Definition: IRuntime.hpp:142
std::vector< arm::pipe::ILocalPacketHandlerSharedPtr > m_LocalPacketHandlers
Definition: IRuntime.hpp:151
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:33
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:392
const ProfilingDetailsMethod m_OutputNetworkDetailsMethod
Definition: IRuntime.hpp:62
std::map< BackendId, std::shared_ptr< IMemoryOptimizerStrategy > > m_MemoryOptimizerStrategyMap
A map to define a custom memory optimizer strategy for specific backend Ids.
Definition: IRuntime.hpp:122
Copyright (c) 2021 ARM Limited and Contributors.
std::string m_IncomingCaptureFile
Path to a file in which incoming timeline profiling messages will be stored.
Definition: IRuntime.hpp:144
bool m_EnableProfiling
Indicates whether external profiling is enabled or not.
Definition: IRuntime.hpp:138
bool m_FileOnly
Enable profiling output to file only.
Definition: IRuntime.hpp:146
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:379
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:290
ProfilingDetailsMethod
Define the behaviour of the internal profiler when outputting network details.
Definition: Types.hpp:71
std::shared_ptr< IGpuAccTunedParameters > m_GpuAccTunedParameters
If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads...
Definition: IRuntime.hpp:90
std::vector< BackendOptions > m_BackendOptions
Pass backend specific options.
Definition: IRuntime.hpp:189
const bool m_ExternalMemoryManagementEnabled
Definition: IRuntime.hpp:67
int NetworkId
Definition: IRuntime.hpp:27
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:393
Status
enumeration
Definition: Types.hpp:42
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:242
INetworkProperties(bool asyncEnabled, MemorySource inputSource, MemorySource outputSource, bool profilingEnabled=false, ProfilingDetailsMethod detailsMethod=ProfilingDetailsMethod::Undefined, bool externalMemoryManagementEnabled=false)
Definition: IRuntime.hpp:37
const bool m_ExportEnabled
Deprecated and will be removed in future release.
Definition: IRuntime.hpp:56
std::map< BackendId, std::shared_ptr< ICustomAllocator > > m_CustomAllocatorMap
A map to define a custom memory allocator for specific backend Ids.
Definition: IRuntime.hpp:114
std::unique_ptr< RuntimeImpl > pRuntimeImpl
Definition: IRuntime.hpp:290
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:280
constexpr unsigned int LOWEST_CAPTURE_PERIOD
The lowest performance data capture interval we support is 10 miliseconds.
Definition: Types.hpp:34
std::shared_ptr< IGpuAccTunedParameters > IGpuAccTunedParametersPtr
The following API is replaced by the backend options API.
Definition: IRuntime.hpp:295
bool m_ProtectedMode
Setting this flag will allow the user to create the Runtime in protected mode.
Definition: IRuntime.hpp:105
std::string m_DynamicBackendsPath
Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive Only a...
Definition: IRuntime.hpp:98
bool m_EnableGpuProfiling
Setting this flag will allow the user to obtain GPU profiling information from the runtime...
Definition: IRuntime.hpp:93
Manages a set of GpuAcc parameters which have been tuned for maximum performance. ...
Definition: IRuntime.hpp:308
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:230
uint32_t m_CapturePeriod
The duration at which captured profiling messages will be flushed.
Definition: IRuntime.hpp:148
bool m_TimelineEnabled
Indicates whether external timeline profiling is enabled or not.
Definition: IRuntime.hpp:140
const MemorySource m_OutputSource
Definition: IRuntime.hpp:65
ExternalProfilingOptions m_ProfilingOptions
Definition: IRuntime.hpp:153
const bool m_ProfilingEnabled
Definition: IRuntime.hpp:60
std::string m_FileFormat
The format of the file used for outputting profiling data.
Definition: IRuntime.hpp:150
virtual ~INetworkProperties()
Definition: IRuntime.hpp:69