blob: 1bae94374ec3cbebba04fa183b780918d15d3ca1 [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),
42 m_InputSource(m_ImportEnabled ? MemorySource::Malloc : MemorySource::Undefined),
43 m_OutputSource(m_ExportEnabled ? MemorySource::Malloc : MemorySource::Undefined)
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +010044 {}
David Monahan4f1e8e42019-09-04 09:22:10 +010045
Finn Williamsf364d532021-06-09 17:07:33 +010046 ARMNN_DEPRECATED_MSG("Please use INetworkProperties constructor without numThreads argument")
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010047 INetworkProperties(bool asyncEnabled,
48 MemorySource m_InputSource,
Keith Davise813d672021-04-22 10:10:34 +010049 MemorySource m_OutputSource,
Keith Davis554fa092021-07-20 11:25:22 +010050 size_t numThreads,
51 bool profilingEnabled = false)
52 : m_ImportEnabled(m_InputSource != MemorySource::Undefined),
53 m_ExportEnabled(m_OutputSource != MemorySource::Undefined),
54 m_AsyncEnabled(asyncEnabled),
55 m_ProfilingEnabled(profilingEnabled),
56 m_InputSource(m_InputSource),
57 m_OutputSource(m_OutputSource)
Finn Williamsf364d532021-06-09 17:07:33 +010058 {
59 armnn::IgnoreUnused(numThreads);
60 }
61
62 INetworkProperties(bool asyncEnabled,
63 MemorySource m_InputSource,
Keith Davis554fa092021-07-20 11:25:22 +010064 MemorySource m_OutputSource,
65 bool profilingEnabled = false)
66 : m_ImportEnabled(m_InputSource != MemorySource::Undefined),
67 m_ExportEnabled(m_OutputSource != MemorySource::Undefined),
68 m_AsyncEnabled(asyncEnabled),
69 m_ProfilingEnabled(profilingEnabled),
70 m_InputSource(m_InputSource),
71 m_OutputSource(m_OutputSource)
72 {}
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010073
74 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010075 const bool m_ImportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010076 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010077 const bool m_ExportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010078
Keith Davis554fa092021-07-20 11:25:22 +010079 const bool m_AsyncEnabled;
80
81 const bool m_ProfilingEnabled;
Keith Davise813d672021-04-22 10:10:34 +010082
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010083 const MemorySource m_InputSource;
84 const MemorySource m_OutputSource;
David Monahan4f1e8e42019-09-04 09:22:10 +010085
86 virtual ~INetworkProperties() {}
87};
88
Mike Kelly386ff1a2021-03-29 15:04:50 +010089using namespace armnn::experimental;
90
telsoa014fcda012018-03-09 14:13:49 +000091class IRuntime
92{
93public:
94 struct CreationOptions
95 {
telsoa01c577f2c2018-08-31 09:22:23 +010096 CreationOptions()
97 : m_GpuAccTunedParameters(nullptr)
98 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010099 , m_DynamicBackendsPath("")
David Monahan801e2d52021-07-19 17:06:30 +0100100 , m_CustomAllocator(nullptr)
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100101 , m_ProtectedMode(false)
telsoa01c577f2c2018-08-31 09:22:23 +0100102 {}
telsoa014fcda012018-03-09 14:13:49 +0000103
telsoa01c577f2c2018-08-31 09:22:23 +0100104 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
105 /// It will also be updated with new tuned parameters if it is configured to do so.
106 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
107
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000108 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +0100109 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +0100110
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000111 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
112 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +0100113 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100114
David Monahan801e2d52021-07-19 17:06:30 +0100115 /// A Custom Allocator used for allocation of working memory in the backends.
116 /// Set this for when you need to allocate Protected Working Memory, required for ProtectedMode
117 /// Only supported for GpuAcc
118 ICustomAllocator* m_CustomAllocator;
119
Jan Eilers15fcc7e2021-07-14 13:50:15 +0100120 /// Setting this flag will allow the user to create the Runtime in protected mode.
121 /// It will run all the inferences on protected memory and will make sure that
122 /// INetworkProperties::m_ImportEnabled set to true with MemorySource::DmaBufProtected option
123 /// This will use Protected Memory Allocator associated with the backend
124 bool m_ProtectedMode;
125
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100126 struct ExternalProfilingOptions
127 {
128 ExternalProfilingOptions()
129 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +0100130 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100131 , m_OutgoingCaptureFile("")
132 , m_IncomingCaptureFile("")
133 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +0000134 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000135 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +0100136 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100137 {}
138
139 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +0100140 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100141 std::string m_OutgoingCaptureFile;
142 std::string m_IncomingCaptureFile;
143 bool m_FileOnly;
144 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000145 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +0100146 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100147 };
Jim Flynn4951b8c2019-10-03 10:04:30 -0700148 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +0000149
150 /// Pass backend specific options.
151 ///
152 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000153 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000154 /// m_BackendOption.emplace_back(
155 /// BackendOptions{"GpuAcc",
156 /// {
157 /// {"TuningLevel", 2},
158 /// {"TuningFile", filename}
159 /// }
160 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000161 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000162 /// Execute representative workloads through the runtime to generate tuning data.
163 /// The tuning file is written once the runtime is destroyed
164
165 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000166 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000167 /// m_BackendOption.emplace_back(
168 /// BackendOptions{"GpuAcc",
169 /// {
170 /// {"TuningFile", filename}
171 /// }
172 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000173 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000174
175 /// The following backend options are available:
176 /// GpuAcc:
177 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
178 /// "TuningFile" : string [filenameString]
179 /// "KernelProfilingEnabled" : bool [true | false]
180 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000181 };
182
183 static IRuntime* CreateRaw(const CreationOptions& options);
184 static IRuntimePtr Create(const CreationOptions& options);
185 static void Destroy(IRuntime* runtime);
186
telsoa01c577f2c2018-08-31 09:22:23 +0100187 /// Loads a complete network into the IRuntime.
188 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
189 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000190 /// The runtime takes ownership of the network once passed in.
191 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000192 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000193
telsoa01c577f2c2018-08-31 09:22:23 +0100194 /// Load a complete network into the IRuntime.
195 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
196 /// @param [in] network Complete network to load into the IRuntime.
197 /// @param [out] errorMessage Error message if there were any errors.
198 /// The runtime takes ownership of the network once passed in.
199 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000200 Status LoadNetwork(NetworkId& networkIdOut,
201 IOptimizedNetworkPtr network,
202 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100203
Kevin Mayd92a6e42021-02-04 10:27:41 +0000204 Status LoadNetwork(NetworkId& networkIdOut,
205 IOptimizedNetworkPtr network,
206 std::string& errorMessage,
207 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100208
Kevin Mayd92a6e42021-02-04 10:27:41 +0000209 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
210 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000211
telsoa01c577f2c2018-08-31 09:22:23 +0100212 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000213 Status EnqueueWorkload(NetworkId networkId,
214 const InputTensors& inputTensors,
215 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000216
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100217 /// This is an experimental function.
218 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors.
219 /// This function performs a thread safe execution of the network. Returns once execution is complete.
220 /// Will block until this and any other thread using the same workingMem object completes.
221 Status Execute(IWorkingMemHandle& workingMemHandle,
222 const InputTensors& inputTensors,
223 const OutputTensors& outputTensors);
224
telsoa01c577f2c2018-08-31 09:22:23 +0100225 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000226 /// At the moment this only removes the network from the m_Impl->m_Network.
227 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100228 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000229 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000230 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000231
Kevin Mayd92a6e42021-02-04 10:27:41 +0000232 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100233
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100234 /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
235 /// overlapped Execution by calling this function from different threads.
236 std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
237
telsoa01c577f2c2018-08-31 09:22:23 +0100238 /// Gets the profiler corresponding to the given network id.
239 /// @param networkId The id of the network for which to get the profile.
240 /// @return A pointer to the requested profiler, or nullptr if not found.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000241 const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
telsoa014fcda012018-03-09 14:13:49 +0000242
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000243 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
244 /// @param networkId The id of the network to register the callback.
245 /// @param func callback function to pass to the debug layer.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000246 void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000247
telsoa014fcda012018-03-09 14:13:49 +0000248protected:
Kevin Mayd92a6e42021-02-04 10:27:41 +0000249 IRuntime();
250 IRuntime(const IRuntime::CreationOptions& options);
251 ~IRuntime();
252
253 std::unique_ptr<RuntimeImpl> pRuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +0000254};
255
Derek Lamberti836b27b2019-11-20 10:51:57 +0000256
257/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100258using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000259
telsoa01c577f2c2018-08-31 09:22:23 +0100260/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
261/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
262/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000263///
264/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100265/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
266/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000267/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
268///
telsoa01c577f2c2018-08-31 09:22:23 +0100269/// 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 +0000270/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100271class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000272{
273public:
274 enum class Mode
275 {
276 UseTunedParameters,
277 UpdateTunedParameters
278 };
279
Ruomei Yan49937f32019-04-25 14:24:05 +0100280 enum class TuningLevel
281 {
Inki Dae23dbe3d2021-03-16 16:24:09 +0900282 Rapid = 1,
283 Normal = 2,
284 Exhaustive = 3
Ruomei Yan49937f32019-04-25 14:24:05 +0100285 };
286
telsoa014fcda012018-03-09 14:13:49 +0000287 /// Creates an IClTunedParameters with the given mode.
288 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100289 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
290 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000291 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100292 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000293
294 /// Loads an existing set of tuned parameters from the given file.
295 /// If there is an error loading the file, an armnn::Exception is thrown.
296 virtual void Load(const char* filename) = 0;
297
298 /// Saves the current set of tuned parameters to the given file.
299 /// If there is an error saving to the file, an armnn::Exception is thrown.
300 virtual void Save(const char* filename) const = 0;
301
302protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100303 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000304};
305
David Monahan4f1e8e42019-09-04 09:22:10 +0100306} // namespace armnn