blob: f296a5f564f5bbfa827f3155cd9e1e0aaa439751 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa014fcda012018-03-09 14:13:49 +00002// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
Derek Lamberti836b27b2019-11-20 10:51:57 +00007#include "BackendOptions.hpp"
telsoa014fcda012018-03-09 14:13:49 +00008#include "INetwork.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01009#include "IProfiler.hpp"
Mike Kelly55a8ffd2021-04-07 20:10:49 +010010#include "IWorkingMemHandle.hpp"
Keith Davise813d672021-04-22 10:10:34 +010011#include "IAsyncExecutionCallback.hpp"
Matthew Bentham313e1c82019-03-25 17:37:47 +000012#include "Tensor.hpp"
13#include "Types.hpp"
telsoa014fcda012018-03-09 14:13:49 +000014#include "TypesUtils.hpp"
Jim Flynn4e755a52020-03-29 17:48:26 +010015#include "profiling/ILocalPacketHandler.hpp"
telsoa014fcda012018-03-09 14:13:49 +000016
Matthew Bentham313e1c82019-03-25 17:37:47 +000017#include <memory>
18
telsoa014fcda012018-03-09 14:13:49 +000019namespace armnn
20{
21
22using NetworkId = int;
23
telsoa01c577f2c2018-08-31 09:22:23 +010024class IGpuAccTunedParameters;
telsoa014fcda012018-03-09 14:13:49 +000025
Kevin Mayd92a6e42021-02-04 10:27:41 +000026struct RuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +000027class IRuntime;
28using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
29
David Monahan4f1e8e42019-09-04 09:22:10 +010030struct INetworkProperties
31{
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010032 ARMNN_DEPRECATED_MSG("Please use INetworkProperties constructor with MemorySource argument")
33 INetworkProperties(bool importEnabled = false,
34 bool exportEnabled = false,
Keith Davise813d672021-04-22 10:10:34 +010035 bool asyncEnabled = false,
36 size_t numThreads = 0)
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010037 : m_ImportEnabled(importEnabled)
38 , m_ExportEnabled(exportEnabled)
39 , m_AsyncEnabled(asyncEnabled)
Keith Davise813d672021-04-22 10:10:34 +010040 , m_NumThreads(numThreads)
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010041 , m_InputSource(MemorySource::Undefined)
42 , m_OutputSource(MemorySource::Undefined)
43 {}
David Monahan4f1e8e42019-09-04 09:22:10 +010044
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010045 INetworkProperties(bool asyncEnabled,
46 MemorySource m_InputSource,
Keith Davise813d672021-04-22 10:10:34 +010047 MemorySource m_OutputSource,
48 size_t numThreads = 0)
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010049 : m_ImportEnabled(m_InputSource != MemorySource::Undefined)
50 , m_ExportEnabled(m_OutputSource != MemorySource::Undefined)
51 , m_AsyncEnabled(asyncEnabled)
Keith Davise813d672021-04-22 10:10:34 +010052 , m_NumThreads(numThreads)
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010053 , m_InputSource(m_InputSource)
54 , m_OutputSource(m_OutputSource)
55 {}
56
57 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010058 const bool m_ImportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010059 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010060 const bool m_ExportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010061
Keith Davise813d672021-04-22 10:10:34 +010062 const bool m_AsyncEnabled;
63 const size_t m_NumThreads;
64
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010065 const MemorySource m_InputSource;
66 const MemorySource m_OutputSource;
David Monahan4f1e8e42019-09-04 09:22:10 +010067
68 virtual ~INetworkProperties() {}
69};
70
Mike Kelly386ff1a2021-03-29 15:04:50 +010071using namespace armnn::experimental;
72
telsoa014fcda012018-03-09 14:13:49 +000073class IRuntime
74{
75public:
76 struct CreationOptions
77 {
telsoa01c577f2c2018-08-31 09:22:23 +010078 CreationOptions()
79 : m_GpuAccTunedParameters(nullptr)
80 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010081 , m_DynamicBackendsPath("")
telsoa01c577f2c2018-08-31 09:22:23 +010082 {}
telsoa014fcda012018-03-09 14:13:49 +000083
telsoa01c577f2c2018-08-31 09:22:23 +010084 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
85 /// It will also be updated with new tuned parameters if it is configured to do so.
86 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
87
Ryan OShea2bbfaa72020-02-12 16:15:27 +000088 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +010089 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +010090
Ryan OShea2bbfaa72020-02-12 16:15:27 +000091 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
92 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +010093 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010094
95 struct ExternalProfilingOptions
96 {
97 ExternalProfilingOptions()
98 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +010099 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100100 , m_OutgoingCaptureFile("")
101 , m_IncomingCaptureFile("")
102 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +0000103 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000104 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +0100105 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100106 {}
107
108 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +0100109 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100110 std::string m_OutgoingCaptureFile;
111 std::string m_IncomingCaptureFile;
112 bool m_FileOnly;
113 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000114 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +0100115 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100116 };
Jim Flynn4951b8c2019-10-03 10:04:30 -0700117 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +0000118
119 /// Pass backend specific options.
120 ///
121 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000122 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000123 /// m_BackendOption.emplace_back(
124 /// BackendOptions{"GpuAcc",
125 /// {
126 /// {"TuningLevel", 2},
127 /// {"TuningFile", filename}
128 /// }
129 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000130 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000131 /// Execute representative workloads through the runtime to generate tuning data.
132 /// The tuning file is written once the runtime is destroyed
133
134 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000135 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000136 /// m_BackendOption.emplace_back(
137 /// BackendOptions{"GpuAcc",
138 /// {
139 /// {"TuningFile", filename}
140 /// }
141 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000142 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000143
144 /// The following backend options are available:
145 /// GpuAcc:
146 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
147 /// "TuningFile" : string [filenameString]
148 /// "KernelProfilingEnabled" : bool [true | false]
149 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000150 };
151
152 static IRuntime* CreateRaw(const CreationOptions& options);
153 static IRuntimePtr Create(const CreationOptions& options);
154 static void Destroy(IRuntime* runtime);
155
telsoa01c577f2c2018-08-31 09:22:23 +0100156 /// Loads a complete network into the IRuntime.
157 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
158 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000159 /// The runtime takes ownership of the network once passed in.
160 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000161 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000162
telsoa01c577f2c2018-08-31 09:22:23 +0100163 /// Load a complete network into the IRuntime.
164 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
165 /// @param [in] network Complete network to load into the IRuntime.
166 /// @param [out] errorMessage Error message if there were any errors.
167 /// The runtime takes ownership of the network once passed in.
168 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000169 Status LoadNetwork(NetworkId& networkIdOut,
170 IOptimizedNetworkPtr network,
171 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100172
Kevin Mayd92a6e42021-02-04 10:27:41 +0000173 Status LoadNetwork(NetworkId& networkIdOut,
174 IOptimizedNetworkPtr network,
175 std::string& errorMessage,
176 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100177
Kevin Mayd92a6e42021-02-04 10:27:41 +0000178 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
179 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000180
telsoa01c577f2c2018-08-31 09:22:23 +0100181 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000182 Status EnqueueWorkload(NetworkId networkId,
183 const InputTensors& inputTensors,
184 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000185
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100186 /// This is an experimental function.
187 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors.
188 /// This function performs a thread safe execution of the network. Returns once execution is complete.
189 /// Will block until this and any other thread using the same workingMem object completes.
190 Status Execute(IWorkingMemHandle& workingMemHandle,
191 const InputTensors& inputTensors,
192 const OutputTensors& outputTensors);
193
Keith Davise813d672021-04-22 10:10:34 +0100194 /// This is an experimental function
195 /// Schedule a thread safe execution by taking the input tensors and an execution priority for Quality of Service.
196 /// The output tensors will then be filled and the callback object will notify that the execution has either
197 /// succeeded or failed.
198 void Schedule(NetworkId networkId,
199 const InputTensors& inputTensors,
200 const OutputTensors& outputTensors,
201 const QosExecPriority priority,
202 std::shared_ptr<IAsyncExecutionCallback> callback);
203
telsoa01c577f2c2018-08-31 09:22:23 +0100204 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000205 /// At the moment this only removes the network from the m_Impl->m_Network.
206 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100207 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000208 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000209 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000210
Kevin Mayd92a6e42021-02-04 10:27:41 +0000211 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100212
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100213 /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
214 /// overlapped Execution by calling this function from different threads.
215 std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
216
telsoa01c577f2c2018-08-31 09:22:23 +0100217 /// Gets the profiler corresponding to the given network id.
218 /// @param networkId The id of the network for which to get the profile.
219 /// @return A pointer to the requested profiler, or nullptr if not found.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000220 const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
telsoa014fcda012018-03-09 14:13:49 +0000221
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000222 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
223 /// @param networkId The id of the network to register the callback.
224 /// @param func callback function to pass to the debug layer.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000225 void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000226
telsoa014fcda012018-03-09 14:13:49 +0000227protected:
Kevin Mayd92a6e42021-02-04 10:27:41 +0000228 IRuntime();
229 IRuntime(const IRuntime::CreationOptions& options);
230 ~IRuntime();
231
232 std::unique_ptr<RuntimeImpl> pRuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +0000233};
234
Derek Lamberti836b27b2019-11-20 10:51:57 +0000235
236/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100237using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000238
telsoa01c577f2c2018-08-31 09:22:23 +0100239/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
240/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
241/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000242///
243/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100244/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
245/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000246/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
247///
telsoa01c577f2c2018-08-31 09:22:23 +0100248/// The parameters can be loaded from and saved to a file so that you can first run a slow initial read-write
telsoa014fcda012018-03-09 14:13:49 +0000249/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100250class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000251{
252public:
253 enum class Mode
254 {
255 UseTunedParameters,
256 UpdateTunedParameters
257 };
258
Ruomei Yan49937f32019-04-25 14:24:05 +0100259 enum class TuningLevel
260 {
Inki Dae23dbe3d2021-03-16 16:24:09 +0900261 Rapid = 1,
262 Normal = 2,
263 Exhaustive = 3
Ruomei Yan49937f32019-04-25 14:24:05 +0100264 };
265
telsoa014fcda012018-03-09 14:13:49 +0000266 /// Creates an IClTunedParameters with the given mode.
267 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100268 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
269 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000270 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100271 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000272
273 /// Loads an existing set of tuned parameters from the given file.
274 /// If there is an error loading the file, an armnn::Exception is thrown.
275 virtual void Load(const char* filename) = 0;
276
277 /// Saves the current set of tuned parameters to the given file.
278 /// If there is an error saving to the file, an armnn::Exception is thrown.
279 virtual void Save(const char* filename) const = 0;
280
281protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100282 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000283};
284
David Monahan4f1e8e42019-09-04 09:22:10 +0100285} // namespace armnn