blob: 8c269dee494783f4e04e7923fc0003bdbed18540 [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
David Monahan801e2d52021-07-19 17:06:30 +010017#include <armnn/backends/ICustomAllocator.hpp>
Matthew Bentham313e1c82019-03-25 17:37:47 +000018#include <memory>
19
telsoa014fcda012018-03-09 14:13:49 +000020namespace armnn
21{
22
23using NetworkId = int;
24
telsoa01c577f2c2018-08-31 09:22:23 +010025class IGpuAccTunedParameters;
telsoa014fcda012018-03-09 14:13:49 +000026
Kevin Mayd92a6e42021-02-04 10:27:41 +000027struct RuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +000028class IRuntime;
29using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
30
David Monahan4f1e8e42019-09-04 09:22:10 +010031struct INetworkProperties
32{
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010033 ARMNN_DEPRECATED_MSG("Please use INetworkProperties constructor with MemorySource argument")
34 INetworkProperties(bool importEnabled = false,
35 bool exportEnabled = false,
Keith Davis554fa092021-07-20 11:25:22 +010036 bool asyncEnabled = false,
37 bool profilingEnabled = false)
38 : m_ImportEnabled(importEnabled),
39 m_ExportEnabled(exportEnabled),
40 m_AsyncEnabled(asyncEnabled),
41 m_ProfilingEnabled(profilingEnabled),
Keith Davisf4874862021-08-09 16:49:18 +010042 m_OutputNetworkDetails(false),
Keith Davis554fa092021-07-20 11:25:22 +010043 m_InputSource(m_ImportEnabled ? MemorySource::Malloc : MemorySource::Undefined),
44 m_OutputSource(m_ExportEnabled ? MemorySource::Malloc : MemorySource::Undefined)
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +010045 {}
David Monahan4f1e8e42019-09-04 09:22:10 +010046
Finn Williamsf364d532021-06-09 17:07:33 +010047 ARMNN_DEPRECATED_MSG("Please use INetworkProperties constructor without numThreads argument")
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010048 INetworkProperties(bool asyncEnabled,
Keith Davisf4874862021-08-09 16:49:18 +010049 MemorySource inputSource,
50 MemorySource outputSource,
Keith Davis554fa092021-07-20 11:25:22 +010051 size_t numThreads,
52 bool profilingEnabled = false)
Keith Davisf4874862021-08-09 16:49:18 +010053 : m_ImportEnabled(inputSource != MemorySource::Undefined),
54 m_ExportEnabled(outputSource != MemorySource::Undefined),
Keith Davis554fa092021-07-20 11:25:22 +010055 m_AsyncEnabled(asyncEnabled),
56 m_ProfilingEnabled(profilingEnabled),
Keith Davisf4874862021-08-09 16:49:18 +010057 m_OutputNetworkDetails(false),
58 m_InputSource(inputSource),
59 m_OutputSource(outputSource)
Finn Williamsf364d532021-06-09 17:07:33 +010060 {
61 armnn::IgnoreUnused(numThreads);
62 }
63
64 INetworkProperties(bool asyncEnabled,
Keith Davisf4874862021-08-09 16:49:18 +010065 MemorySource inputSource,
66 MemorySource outputSource,
67 bool profilingEnabled = false,
68 bool outputDetails = false)
69 : m_ImportEnabled(inputSource != MemorySource::Undefined),
70 m_ExportEnabled(outputSource != MemorySource::Undefined),
Keith Davis554fa092021-07-20 11:25:22 +010071 m_AsyncEnabled(asyncEnabled),
72 m_ProfilingEnabled(profilingEnabled),
Keith Davisf4874862021-08-09 16:49:18 +010073 m_OutputNetworkDetails(outputDetails),
74 m_InputSource(inputSource),
75 m_OutputSource(outputSource)
Keith Davis554fa092021-07-20 11:25:22 +010076 {}
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010077
78 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010079 const bool m_ImportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010080 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010081 const bool m_ExportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010082
Keith Davis554fa092021-07-20 11:25:22 +010083 const bool m_AsyncEnabled;
84
85 const bool m_ProfilingEnabled;
Keith Davise813d672021-04-22 10:10:34 +010086
Keith Davisf4874862021-08-09 16:49:18 +010087 const bool m_OutputNetworkDetails;
88
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010089 const MemorySource m_InputSource;
90 const MemorySource m_OutputSource;
David Monahan4f1e8e42019-09-04 09:22:10 +010091
92 virtual ~INetworkProperties() {}
93};
94
Mike Kelly386ff1a2021-03-29 15:04:50 +010095using namespace armnn::experimental;
96
telsoa014fcda012018-03-09 14:13:49 +000097class IRuntime
98{
99public:
100 struct CreationOptions
101 {
telsoa01c577f2c2018-08-31 09:22:23 +0100102 CreationOptions()
103 : m_GpuAccTunedParameters(nullptr)
104 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +0100105 , m_DynamicBackendsPath("")
David Monahan801e2d52021-07-19 17:06:30 +0100106 , m_CustomAllocator(nullptr)
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100107 , m_ProtectedMode(false)
telsoa01c577f2c2018-08-31 09:22:23 +0100108 {}
telsoa014fcda012018-03-09 14:13:49 +0000109
telsoa01c577f2c2018-08-31 09:22:23 +0100110 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
111 /// It will also be updated with new tuned parameters if it is configured to do so.
112 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
113
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000114 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +0100115 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +0100116
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000117 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
118 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +0100119 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100120
David Monahan801e2d52021-07-19 17:06:30 +0100121 /// A Custom Allocator used for allocation of working memory in the backends.
122 /// Set this for when you need to allocate Protected Working Memory, required for ProtectedMode
123 /// Only supported for GpuAcc
124 ICustomAllocator* m_CustomAllocator;
125
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100126 /// Setting this flag will allow the user to create the Runtime in protected mode.
127 /// It will run all the inferences on protected memory and will make sure that
128 /// INetworkProperties::m_ImportEnabled set to true with MemorySource::DmaBufProtected option
129 /// This will use Protected Memory Allocator associated with the backend
130 bool m_ProtectedMode;
131
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100132 struct ExternalProfilingOptions
133 {
134 ExternalProfilingOptions()
135 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +0100136 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100137 , m_OutgoingCaptureFile("")
138 , m_IncomingCaptureFile("")
139 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +0000140 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000141 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +0100142 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100143 {}
144
145 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +0100146 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100147 std::string m_OutgoingCaptureFile;
148 std::string m_IncomingCaptureFile;
149 bool m_FileOnly;
150 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000151 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +0100152 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100153 };
Jim Flynn4951b8c2019-10-03 10:04:30 -0700154 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +0000155
156 /// Pass backend specific options.
157 ///
158 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000159 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000160 /// m_BackendOption.emplace_back(
161 /// BackendOptions{"GpuAcc",
162 /// {
163 /// {"TuningLevel", 2},
164 /// {"TuningFile", filename}
165 /// }
166 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000167 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000168 /// 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.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000172 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000173 /// m_BackendOption.emplace_back(
174 /// BackendOptions{"GpuAcc",
175 /// {
176 /// {"TuningFile", filename}
177 /// }
178 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000179 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000180
181 /// The following backend options are available:
182 /// GpuAcc:
183 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
184 /// "TuningFile" : string [filenameString]
185 /// "KernelProfilingEnabled" : bool [true | false]
186 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000187 };
188
189 static IRuntime* CreateRaw(const CreationOptions& options);
190 static IRuntimePtr Create(const CreationOptions& options);
191 static void Destroy(IRuntime* runtime);
192
telsoa01c577f2c2018-08-31 09:22:23 +0100193 /// Loads a complete network into the IRuntime.
194 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
195 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000196 /// The runtime takes ownership of the network once passed in.
197 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000198 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000199
telsoa01c577f2c2018-08-31 09:22:23 +0100200 /// Load a complete network into the IRuntime.
201 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
202 /// @param [in] network Complete network to load into the IRuntime.
203 /// @param [out] errorMessage Error message if there were any errors.
204 /// The runtime takes ownership of the network once passed in.
205 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000206 Status LoadNetwork(NetworkId& networkIdOut,
207 IOptimizedNetworkPtr network,
208 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100209
Kevin Mayd92a6e42021-02-04 10:27:41 +0000210 Status LoadNetwork(NetworkId& networkIdOut,
211 IOptimizedNetworkPtr network,
212 std::string& errorMessage,
213 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100214
Kevin Mayd92a6e42021-02-04 10:27:41 +0000215 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
216 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000217
telsoa01c577f2c2018-08-31 09:22:23 +0100218 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000219 Status EnqueueWorkload(NetworkId networkId,
220 const InputTensors& inputTensors,
221 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000222
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100223 /// This is an experimental function.
224 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors.
225 /// This function performs a thread safe execution of the network. Returns once execution is complete.
226 /// Will block until this and any other thread using the same workingMem object completes.
227 Status Execute(IWorkingMemHandle& workingMemHandle,
228 const InputTensors& inputTensors,
229 const OutputTensors& outputTensors);
230
telsoa01c577f2c2018-08-31 09:22:23 +0100231 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000232 /// At the moment this only removes the network from the m_Impl->m_Network.
233 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100234 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000235 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000236 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000237
Kevin Mayd92a6e42021-02-04 10:27:41 +0000238 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100239
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100240 /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
241 /// overlapped Execution by calling this function from different threads.
242 std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
243
telsoa01c577f2c2018-08-31 09:22:23 +0100244 /// Gets the profiler corresponding to the given network id.
245 /// @param networkId The id of the network for which to get the profile.
246 /// @return A pointer to the requested profiler, or nullptr if not found.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000247 const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
telsoa014fcda012018-03-09 14:13:49 +0000248
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000249 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
250 /// @param networkId The id of the network to register the callback.
251 /// @param func callback function to pass to the debug layer.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000252 void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000253
telsoa014fcda012018-03-09 14:13:49 +0000254protected:
Kevin Mayd92a6e42021-02-04 10:27:41 +0000255 IRuntime();
256 IRuntime(const IRuntime::CreationOptions& options);
257 ~IRuntime();
258
259 std::unique_ptr<RuntimeImpl> pRuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +0000260};
261
Derek Lamberti836b27b2019-11-20 10:51:57 +0000262
263/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100264using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000265
telsoa01c577f2c2018-08-31 09:22:23 +0100266/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
267/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
268/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000269///
270/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100271/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
272/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000273/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
274///
telsoa01c577f2c2018-08-31 09:22:23 +0100275/// 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 +0000276/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100277class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000278{
279public:
280 enum class Mode
281 {
282 UseTunedParameters,
283 UpdateTunedParameters
284 };
285
Ruomei Yan49937f32019-04-25 14:24:05 +0100286 enum class TuningLevel
287 {
Inki Dae23dbe3d2021-03-16 16:24:09 +0900288 Rapid = 1,
289 Normal = 2,
290 Exhaustive = 3
Ruomei Yan49937f32019-04-25 14:24:05 +0100291 };
292
telsoa014fcda012018-03-09 14:13:49 +0000293 /// Creates an IClTunedParameters with the given mode.
294 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100295 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
296 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000297 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100298 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000299
300 /// Loads an existing set of tuned parameters from the given file.
301 /// If there is an error loading the file, an armnn::Exception is thrown.
302 virtual void Load(const char* filename) = 0;
303
304 /// Saves the current set of tuned parameters to the given file.
305 /// If there is an error saving to the file, an armnn::Exception is thrown.
306 virtual void Save(const char* filename) const = 0;
307
308protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100309 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000310};
311
David Monahan4f1e8e42019-09-04 09:22:10 +0100312} // namespace armnn