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