blob: 908fe7692dea60931e57c7e3d4304c04d4dcea08 [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>
Jan Eilersc1c872f2021-07-22 13:17:04 +010019#include <map>
Matthew Bentham313e1c82019-03-25 17:37:47 +000020
telsoa014fcda012018-03-09 14:13:49 +000021namespace armnn
22{
23
24using NetworkId = int;
25
telsoa01c577f2c2018-08-31 09:22:23 +010026class IGpuAccTunedParameters;
telsoa014fcda012018-03-09 14:13:49 +000027
Kevin Mayd92a6e42021-02-04 10:27:41 +000028struct RuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +000029class IRuntime;
30using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
31
David Monahan4f1e8e42019-09-04 09:22:10 +010032struct INetworkProperties
33{
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010034 ARMNN_DEPRECATED_MSG("Please use INetworkProperties constructor with MemorySource argument")
35 INetworkProperties(bool importEnabled = false,
36 bool exportEnabled = false,
Keith Davis554fa092021-07-20 11:25:22 +010037 bool asyncEnabled = false,
38 bool profilingEnabled = false)
39 : m_ImportEnabled(importEnabled),
40 m_ExportEnabled(exportEnabled),
41 m_AsyncEnabled(asyncEnabled),
42 m_ProfilingEnabled(profilingEnabled),
Keith Davis4914d0c2021-08-18 17:14:05 +010043 m_OutputNetworkDetailsMethod(ProfilingDetailsMethod::Undefined),
Keith Davis554fa092021-07-20 11:25:22 +010044 m_InputSource(m_ImportEnabled ? MemorySource::Malloc : MemorySource::Undefined),
45 m_OutputSource(m_ExportEnabled ? MemorySource::Malloc : MemorySource::Undefined)
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +010046 {}
David Monahan4f1e8e42019-09-04 09:22:10 +010047
Finn Williamsf364d532021-06-09 17:07:33 +010048 ARMNN_DEPRECATED_MSG("Please use INetworkProperties constructor without numThreads argument")
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010049 INetworkProperties(bool asyncEnabled,
Keith Davisf4874862021-08-09 16:49:18 +010050 MemorySource inputSource,
51 MemorySource outputSource,
Keith Davis554fa092021-07-20 11:25:22 +010052 size_t numThreads,
53 bool profilingEnabled = false)
Keith Davisf4874862021-08-09 16:49:18 +010054 : m_ImportEnabled(inputSource != MemorySource::Undefined),
55 m_ExportEnabled(outputSource != MemorySource::Undefined),
Keith Davis554fa092021-07-20 11:25:22 +010056 m_AsyncEnabled(asyncEnabled),
57 m_ProfilingEnabled(profilingEnabled),
Keith Davis4914d0c2021-08-18 17:14:05 +010058 m_OutputNetworkDetailsMethod(ProfilingDetailsMethod::Undefined),
Keith Davisf4874862021-08-09 16:49:18 +010059 m_InputSource(inputSource),
60 m_OutputSource(outputSource)
Finn Williamsf364d532021-06-09 17:07:33 +010061 {
62 armnn::IgnoreUnused(numThreads);
63 }
64
65 INetworkProperties(bool asyncEnabled,
Keith Davisf4874862021-08-09 16:49:18 +010066 MemorySource inputSource,
67 MemorySource outputSource,
68 bool profilingEnabled = false,
Keith Davis4914d0c2021-08-18 17:14:05 +010069 ProfilingDetailsMethod detailsMethod = ProfilingDetailsMethod::Undefined)
Keith Davisf4874862021-08-09 16:49:18 +010070 : m_ImportEnabled(inputSource != MemorySource::Undefined),
71 m_ExportEnabled(outputSource != MemorySource::Undefined),
Keith Davis554fa092021-07-20 11:25:22 +010072 m_AsyncEnabled(asyncEnabled),
73 m_ProfilingEnabled(profilingEnabled),
Keith Davis4914d0c2021-08-18 17:14:05 +010074 m_OutputNetworkDetailsMethod(detailsMethod),
Keith Davisf4874862021-08-09 16:49:18 +010075 m_InputSource(inputSource),
76 m_OutputSource(outputSource)
Keith Davis554fa092021-07-20 11:25:22 +010077 {}
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010078
79 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010080 const bool m_ImportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010081 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010082 const bool m_ExportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010083
Keith Davis554fa092021-07-20 11:25:22 +010084 const bool m_AsyncEnabled;
85
86 const bool m_ProfilingEnabled;
Keith Davise813d672021-04-22 10:10:34 +010087
Keith Davis4914d0c2021-08-18 17:14:05 +010088 const ProfilingDetailsMethod m_OutputNetworkDetailsMethod;
Keith Davisf4874862021-08-09 16:49:18 +010089
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010090 const MemorySource m_InputSource;
91 const MemorySource m_OutputSource;
David Monahan4f1e8e42019-09-04 09:22:10 +010092
93 virtual ~INetworkProperties() {}
94};
95
Mike Kelly386ff1a2021-03-29 15:04:50 +010096using namespace armnn::experimental;
97
telsoa014fcda012018-03-09 14:13:49 +000098class IRuntime
99{
100public:
101 struct CreationOptions
102 {
telsoa01c577f2c2018-08-31 09:22:23 +0100103 CreationOptions()
104 : m_GpuAccTunedParameters(nullptr)
105 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +0100106 , m_DynamicBackendsPath("")
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100107 , m_ProtectedMode(false)
Jan Eilersc1c872f2021-07-22 13:17:04 +0100108 , m_CustomAllocatorMap()
telsoa01c577f2c2018-08-31 09:22:23 +0100109 {}
telsoa014fcda012018-03-09 14:13:49 +0000110
telsoa01c577f2c2018-08-31 09:22:23 +0100111 /// 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
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000115 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +0100116 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +0100117
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000118 /// 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
Matteo Martincighe7d44982019-08-05 12:16:47 +0100120 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100121
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100122 /// 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
Jan Eilersc1c872f2021-07-22 13:17:04 +0100125 /// This requires that the backend supports Protected Memory and has an allocator capable of
126 /// allocating Protected Memory associated with it.
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100127 bool m_ProtectedMode;
128
Jan Eilersc1c872f2021-07-22 13:17:04 +0100129 /// @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
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100138 struct ExternalProfilingOptions
139 {
140 ExternalProfilingOptions()
141 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +0100142 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100143 , m_OutgoingCaptureFile("")
144 , m_IncomingCaptureFile("")
145 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +0000146 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000147 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +0100148 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100149 {}
150
151 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +0100152 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100153 std::string m_OutgoingCaptureFile;
154 std::string m_IncomingCaptureFile;
155 bool m_FileOnly;
156 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000157 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +0100158 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100159 };
Jim Flynn4951b8c2019-10-03 10:04:30 -0700160 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +0000161
162 /// Pass backend specific options.
163 ///
164 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000165 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000166 /// m_BackendOption.emplace_back(
167 /// BackendOptions{"GpuAcc",
168 /// {
169 /// {"TuningLevel", 2},
170 /// {"TuningFile", filename}
171 /// }
172 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000173 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000174 /// 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.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000178 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000179 /// m_BackendOption.emplace_back(
180 /// BackendOptions{"GpuAcc",
181 /// {
182 /// {"TuningFile", filename}
183 /// }
184 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000185 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000186
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;
telsoa014fcda012018-03-09 14:13:49 +0000193 };
194
195 static IRuntime* CreateRaw(const CreationOptions& options);
196 static IRuntimePtr Create(const CreationOptions& options);
197 static void Destroy(IRuntime* runtime);
198
telsoa01c577f2c2018-08-31 09:22:23 +0100199 /// 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.
telsoa014fcda012018-03-09 14:13:49 +0000202 /// The runtime takes ownership of the network once passed in.
203 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000204 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000205
telsoa01c577f2c2018-08-31 09:22:23 +0100206 /// 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
Kevin Mayd92a6e42021-02-04 10:27:41 +0000212 Status LoadNetwork(NetworkId& networkIdOut,
213 IOptimizedNetworkPtr network,
214 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100215
Kevin Mayd92a6e42021-02-04 10:27:41 +0000216 Status LoadNetwork(NetworkId& networkIdOut,
217 IOptimizedNetworkPtr network,
218 std::string& errorMessage,
219 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100220
Kevin Mayd92a6e42021-02-04 10:27:41 +0000221 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
222 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000223
Finn Williamsf37b9702021-09-01 18:06:04 +0100224
225 /// ImportInputs separates the importing and mapping of InputTensors from network execution.
226 /// Allowing for a set of InputTensors to be imported and mapped once, but used in execution many times.
227 /// This function is not thread safe and must not be used while other threads are calling Execute().
228 /// Only compatible with AsyncEnabled networks
229 std::vector<ImportedInputId> ImportInputs(NetworkId networkId, const InputTensors& inputTensors);
230
231
telsoa01c577f2c2018-08-31 09:22:23 +0100232 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000233 Status EnqueueWorkload(NetworkId networkId,
234 const InputTensors& inputTensors,
235 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000236
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100237 /// This is an experimental function.
238 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors.
239 /// This function performs a thread safe execution of the network. Returns once execution is complete.
240 /// Will block until this and any other thread using the same workingMem object completes.
241 Status Execute(IWorkingMemHandle& workingMemHandle,
242 const InputTensors& inputTensors,
Finn Williamsf37b9702021-09-01 18:06:04 +0100243 const OutputTensors& outputTensors,
244 std::vector<ImportedInputId> preImportedInputs = {});
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100245
telsoa01c577f2c2018-08-31 09:22:23 +0100246 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000247 /// At the moment this only removes the network from the m_Impl->m_Network.
248 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100249 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000250 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000251 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000252
Kevin Mayd92a6e42021-02-04 10:27:41 +0000253 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100254
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100255 /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
256 /// overlapped Execution by calling this function from different threads.
257 std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
258
telsoa01c577f2c2018-08-31 09:22:23 +0100259 /// Gets the profiler corresponding to the given network id.
260 /// @param networkId The id of the network for which to get the profile.
261 /// @return A pointer to the requested profiler, or nullptr if not found.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000262 const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
telsoa014fcda012018-03-09 14:13:49 +0000263
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000264 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
265 /// @param networkId The id of the network to register the callback.
266 /// @param func callback function to pass to the debug layer.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000267 void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000268
telsoa014fcda012018-03-09 14:13:49 +0000269protected:
Kevin Mayd92a6e42021-02-04 10:27:41 +0000270 IRuntime();
271 IRuntime(const IRuntime::CreationOptions& options);
272 ~IRuntime();
273
274 std::unique_ptr<RuntimeImpl> pRuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +0000275};
276
Derek Lamberti836b27b2019-11-20 10:51:57 +0000277
278/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100279using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000280
telsoa01c577f2c2018-08-31 09:22:23 +0100281/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
282/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
283/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000284///
285/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100286/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
287/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000288/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
289///
telsoa01c577f2c2018-08-31 09:22:23 +0100290/// 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 +0000291/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100292class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000293{
294public:
295 enum class Mode
296 {
297 UseTunedParameters,
298 UpdateTunedParameters
299 };
300
Ruomei Yan49937f32019-04-25 14:24:05 +0100301 enum class TuningLevel
302 {
Inki Dae23dbe3d2021-03-16 16:24:09 +0900303 Rapid = 1,
304 Normal = 2,
305 Exhaustive = 3
Ruomei Yan49937f32019-04-25 14:24:05 +0100306 };
307
telsoa014fcda012018-03-09 14:13:49 +0000308 /// Creates an IClTunedParameters with the given mode.
309 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100310 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
311 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000312 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100313 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000314
315 /// Loads an existing set of tuned parameters from the given file.
316 /// If there is an error loading the file, an armnn::Exception is thrown.
317 virtual void Load(const char* filename) = 0;
318
319 /// Saves the current set of tuned parameters to the given file.
320 /// If there is an error saving to the file, an armnn::Exception is thrown.
321 virtual void Save(const char* filename) const = 0;
322
323protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100324 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000325};
326
David Monahan4f1e8e42019-09-04 09:22:10 +0100327} // namespace armnn