ArmNN
 21.02
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 "Tensor.hpp"
11 #include "Types.hpp"
12 #include "TypesUtils.hpp"
14 
15 #include <memory>
16 
17 namespace armnn
18 {
19 
20 using NetworkId = int;
21 
23 
24 struct RuntimeImpl;
25 class IRuntime;
26 using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
27 
29 {
30  INetworkProperties(bool importEnabled = false, bool exportEnabled = false)
31  : m_ImportEnabled(importEnabled),
32  m_ExportEnabled(exportEnabled) {}
33 
34  const bool m_ImportEnabled;
35  const bool m_ExportEnabled;
36 
37  virtual ~INetworkProperties() {}
38 };
39 
40 class IRuntime
41 {
42 public:
44  {
46  : m_GpuAccTunedParameters(nullptr)
47  , m_EnableGpuProfiling(false)
48  , m_DynamicBackendsPath("")
49  {}
50 
51  /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
52  /// It will also be updated with new tuned parameters if it is configured to do so.
53  std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
54 
55  /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
57 
58  /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
59  /// Only a single path is allowed for the override
60  std::string m_DynamicBackendsPath;
61 
63  {
65  : m_EnableProfiling(false)
66  , m_TimelineEnabled(false)
67  , m_OutgoingCaptureFile("")
68  , m_IncomingCaptureFile("")
69  , m_FileOnly(false)
70  , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
71  , m_FileFormat("binary")
72  , m_LocalPacketHandlers()
73  {}
74 
77  std::string m_OutgoingCaptureFile;
78  std::string m_IncomingCaptureFile;
79  bool m_FileOnly;
80  uint32_t m_CapturePeriod;
81  std::string m_FileFormat;
82  std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
83  };
85 
86  /// Pass backend specific options.
87  ///
88  /// For example, to enable GpuAcc tuning add the following
89  /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
90  /// m_BackendOption.emplace_back(
91  /// BackendOptions{"GpuAcc",
92  /// {
93  /// {"TuningLevel", 2},
94  /// {"TuningFile", filename}
95  /// }
96  /// });
97  /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98  /// Execute representative workloads through the runtime to generate tuning data.
99  /// The tuning file is written once the runtime is destroyed
100 
101  /// To execute with the tuning data, start up with just the tuning file specified.
102  /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
103  /// m_BackendOption.emplace_back(
104  /// BackendOptions{"GpuAcc",
105  /// {
106  /// {"TuningFile", filename}
107  /// }
108  /// });
109  /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110 
111  /// The following backend options are available:
112  /// GpuAcc:
113  /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
114  /// "TuningFile" : string [filenameString]
115  /// "KernelProfilingEnabled" : bool [true | false]
116  std::vector<BackendOptions> m_BackendOptions;
117  };
118 
119  static IRuntime* CreateRaw(const CreationOptions& options);
120  static IRuntimePtr Create(const CreationOptions& options);
121  static void Destroy(IRuntime* runtime);
122 
123  /// Loads a complete network into the IRuntime.
124  /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
125  /// @param [in] network - Complete network to load into the IRuntime.
126  /// The runtime takes ownership of the network once passed in.
127  /// @return armnn::Status
128  Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
129 
130  /// Load a complete network into the IRuntime.
131  /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
132  /// @param [in] network Complete network to load into the IRuntime.
133  /// @param [out] errorMessage Error message if there were any errors.
134  /// The runtime takes ownership of the network once passed in.
135  /// @return armnn::Status
136  Status LoadNetwork(NetworkId& networkIdOut,
137  IOptimizedNetworkPtr network,
138  std::string& errorMessage);
139 
140  Status LoadNetwork(NetworkId& networkIdOut,
141  IOptimizedNetworkPtr network,
142  std::string& errorMessage,
143  const INetworkProperties& networkProperties);
144 
145  TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
146  TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
147 
148  /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
149  Status EnqueueWorkload(NetworkId networkId,
150  const InputTensors& inputTensors,
151  const OutputTensors& outputTensors);
152 
153  /// Unloads a network from the IRuntime.
154  /// At the moment this only removes the network from the m_Impl->m_Network.
155  /// This might need more work in the future to be AndroidNN compliant.
156  /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
157  /// @return armnn::Status
158  Status UnloadNetwork(NetworkId networkId);
159 
160  const IDeviceSpec& GetDeviceSpec() const;
161 
162  /// Gets the profiler corresponding to the given network id.
163  /// @param networkId The id of the network for which to get the profile.
164  /// @return A pointer to the requested profiler, or nullptr if not found.
165  const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
166 
167  /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
168  /// @param networkId The id of the network to register the callback.
169  /// @param func callback function to pass to the debug layer.
170  void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
171 
172 protected:
173  IRuntime();
174  IRuntime(const IRuntime::CreationOptions& options);
175  ~IRuntime();
176 
177  std::unique_ptr<RuntimeImpl> pRuntimeImpl;
178 };
179 
180 
181 /// The following API is replaced by the backend options API.
182 using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
183 
184 /// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
185 /// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
186 /// for all GPU workload execution.
187 ///
188 /// Can be created in two modes:
189 /// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
190 /// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
191 /// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
192 ///
193 /// The parameters can be loaded from and saved to a file so that you can first run a slow initial read-write
194 /// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
196 {
197 public:
198  enum class Mode
199  {
200  UseTunedParameters,
201  UpdateTunedParameters
202  };
203 
204  enum class TuningLevel
205  {
206  Rapid = 0,
207  Normal = 1,
208  Exhaustive = 2
209  };
210 
211  /// Creates an IClTunedParameters with the given mode.
212  /// @{
213  static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
214  static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
215  /// @}
216  static void Destroy(IGpuAccTunedParameters* params);
217 
218  /// Loads an existing set of tuned parameters from the given file.
219  /// If there is an error loading the file, an armnn::Exception is thrown.
220  virtual void Load(const char* filename) = 0;
221 
222  /// Saves the current set of tuned parameters to the given file.
223  /// If there is an error saving to the file, an armnn::Exception is thrown.
224  virtual void Save(const char* filename) const = 0;
225 
226 protected:
228 };
229 
230 } // namespace armnn
const bool m_ImportEnabled
Definition: IRuntime.hpp:34
TensorInfo GetInputTensorInfo(const INetwork *network)
DataLayout::NCHW false
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:26
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:340
int NetworkId
Definition: IRuntime.hpp:20
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:283
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:210
std::shared_ptr< IGpuAccTunedParameters > m_GpuAccTunedParameters
If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads...
Definition: IRuntime.hpp:53
std::vector< BackendOptions > m_BackendOptions
Pass backend specific options.
Definition: IRuntime.hpp:116
std::vector< armnn::profiling::ILocalPacketHandlerSharedPtr > m_LocalPacketHandlers
Definition: IRuntime.hpp:82
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:341
Status
enumeration
Definition: Types.hpp:26
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:174
const bool m_ExportEnabled
Definition: IRuntime.hpp:35
std::unique_ptr< RuntimeImpl > pRuntimeImpl
Definition: IRuntime.hpp:177
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:200
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:182
INetworkProperties(bool importEnabled=false, bool exportEnabled=false)
Definition: IRuntime.hpp:30
std::string m_DynamicBackendsPath
Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive Only a...
Definition: IRuntime.hpp:60
bool m_EnableGpuProfiling
Setting this flag will allow the user to obtain GPU profiling information from the runtime...
Definition: IRuntime.hpp:56
Manages a set of GpuAcc parameters which have been tuned for maximum performance. ...
Definition: IRuntime.hpp:195
ExternalProfilingOptions m_ProfilingOptions
Definition: IRuntime.hpp:84
virtual ~INetworkProperties()
Definition: IRuntime.hpp:37