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