blob: bee61d21a960cf0f066b0d0fdd65de68a9cc3a4d [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,
Finn Williamsf364d532021-06-09 17:07:33 +010036 bool asyncEnabled = false)
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010037 : m_ImportEnabled(importEnabled)
38 , m_ExportEnabled(exportEnabled)
39 , m_AsyncEnabled(asyncEnabled)
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +010040 , m_InputSource(m_ImportEnabled ? MemorySource::Malloc : MemorySource::Undefined)
41 , m_OutputSource(m_ExportEnabled ? MemorySource::Malloc : MemorySource::Undefined)
42 {}
David Monahan4f1e8e42019-09-04 09:22:10 +010043
Finn Williamsf364d532021-06-09 17:07:33 +010044 ARMNN_DEPRECATED_MSG("Please use INetworkProperties constructor without numThreads argument")
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,
Finn Williamsf364d532021-06-09 17:07:33 +010048 size_t numThreads)
49 : m_ImportEnabled(m_InputSource != MemorySource::Undefined)
50 , m_ExportEnabled(m_OutputSource != MemorySource::Undefined)
51 , m_AsyncEnabled(asyncEnabled)
52 , m_InputSource(m_InputSource)
53 , m_OutputSource(m_OutputSource)
54 {
55 armnn::IgnoreUnused(numThreads);
56 }
57
58 INetworkProperties(bool asyncEnabled,
59 MemorySource m_InputSource,
60 MemorySource m_OutputSource)
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010061 : m_ImportEnabled(m_InputSource != MemorySource::Undefined)
62 , m_ExportEnabled(m_OutputSource != MemorySource::Undefined)
63 , m_AsyncEnabled(asyncEnabled)
64 , m_InputSource(m_InputSource)
65 , m_OutputSource(m_OutputSource)
66 {}
67
68 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010069 const bool m_ImportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010070 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010071 const bool m_ExportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010072
Keith Davise813d672021-04-22 10:10:34 +010073 const bool m_AsyncEnabled;
Keith Davise813d672021-04-22 10:10:34 +010074
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010075 const MemorySource m_InputSource;
76 const MemorySource m_OutputSource;
David Monahan4f1e8e42019-09-04 09:22:10 +010077
78 virtual ~INetworkProperties() {}
79};
80
Mike Kelly386ff1a2021-03-29 15:04:50 +010081using namespace armnn::experimental;
82
telsoa014fcda012018-03-09 14:13:49 +000083class IRuntime
84{
85public:
86 struct CreationOptions
87 {
telsoa01c577f2c2018-08-31 09:22:23 +010088 CreationOptions()
89 : m_GpuAccTunedParameters(nullptr)
90 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010091 , m_DynamicBackendsPath("")
David Monahan801e2d52021-07-19 17:06:30 +010092 , m_CustomAllocator(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +010093 {}
telsoa014fcda012018-03-09 14:13:49 +000094
telsoa01c577f2c2018-08-31 09:22:23 +010095 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
96 /// It will also be updated with new tuned parameters if it is configured to do so.
97 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
98
Ryan OShea2bbfaa72020-02-12 16:15:27 +000099 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +0100100 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +0100101
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000102 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
103 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +0100104 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100105
David Monahan801e2d52021-07-19 17:06:30 +0100106 /// A Custom Allocator used for allocation of working memory in the backends.
107 /// Set this for when you need to allocate Protected Working Memory, required for ProtectedMode
108 /// Only supported for GpuAcc
109 ICustomAllocator* m_CustomAllocator;
110
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100111 struct ExternalProfilingOptions
112 {
113 ExternalProfilingOptions()
114 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +0100115 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100116 , m_OutgoingCaptureFile("")
117 , m_IncomingCaptureFile("")
118 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +0000119 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000120 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +0100121 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100122 {}
123
124 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +0100125 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100126 std::string m_OutgoingCaptureFile;
127 std::string m_IncomingCaptureFile;
128 bool m_FileOnly;
129 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000130 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +0100131 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100132 };
Jim Flynn4951b8c2019-10-03 10:04:30 -0700133 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +0000134
135 /// Pass backend specific options.
136 ///
137 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000138 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000139 /// m_BackendOption.emplace_back(
140 /// BackendOptions{"GpuAcc",
141 /// {
142 /// {"TuningLevel", 2},
143 /// {"TuningFile", filename}
144 /// }
145 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000146 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000147 /// Execute representative workloads through the runtime to generate tuning data.
148 /// The tuning file is written once the runtime is destroyed
149
150 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000151 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000152 /// m_BackendOption.emplace_back(
153 /// BackendOptions{"GpuAcc",
154 /// {
155 /// {"TuningFile", filename}
156 /// }
157 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000158 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000159
160 /// The following backend options are available:
161 /// GpuAcc:
162 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
163 /// "TuningFile" : string [filenameString]
164 /// "KernelProfilingEnabled" : bool [true | false]
165 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000166 };
167
168 static IRuntime* CreateRaw(const CreationOptions& options);
169 static IRuntimePtr Create(const CreationOptions& options);
170 static void Destroy(IRuntime* runtime);
171
telsoa01c577f2c2018-08-31 09:22:23 +0100172 /// Loads a complete network into the IRuntime.
173 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
174 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000175 /// The runtime takes ownership of the network once passed in.
176 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000177 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000178
telsoa01c577f2c2018-08-31 09:22:23 +0100179 /// Load a complete network into the IRuntime.
180 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
181 /// @param [in] network Complete network to load into the IRuntime.
182 /// @param [out] errorMessage Error message if there were any errors.
183 /// The runtime takes ownership of the network once passed in.
184 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000185 Status LoadNetwork(NetworkId& networkIdOut,
186 IOptimizedNetworkPtr network,
187 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100188
Kevin Mayd92a6e42021-02-04 10:27:41 +0000189 Status LoadNetwork(NetworkId& networkIdOut,
190 IOptimizedNetworkPtr network,
191 std::string& errorMessage,
192 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100193
Kevin Mayd92a6e42021-02-04 10:27:41 +0000194 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
195 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000196
telsoa01c577f2c2018-08-31 09:22:23 +0100197 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000198 Status EnqueueWorkload(NetworkId networkId,
199 const InputTensors& inputTensors,
200 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000201
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100202 /// This is an experimental function.
203 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors.
204 /// This function performs a thread safe execution of the network. Returns once execution is complete.
205 /// Will block until this and any other thread using the same workingMem object completes.
206 Status Execute(IWorkingMemHandle& workingMemHandle,
207 const InputTensors& inputTensors,
208 const OutputTensors& outputTensors);
209
telsoa01c577f2c2018-08-31 09:22:23 +0100210 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000211 /// At the moment this only removes the network from the m_Impl->m_Network.
212 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100213 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000214 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000215 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000216
Kevin Mayd92a6e42021-02-04 10:27:41 +0000217 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100218
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100219 /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
220 /// overlapped Execution by calling this function from different threads.
221 std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
222
telsoa01c577f2c2018-08-31 09:22:23 +0100223 /// Gets the profiler corresponding to the given network id.
224 /// @param networkId The id of the network for which to get the profile.
225 /// @return A pointer to the requested profiler, or nullptr if not found.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000226 const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
telsoa014fcda012018-03-09 14:13:49 +0000227
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000228 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
229 /// @param networkId The id of the network to register the callback.
230 /// @param func callback function to pass to the debug layer.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000231 void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000232
telsoa014fcda012018-03-09 14:13:49 +0000233protected:
Kevin Mayd92a6e42021-02-04 10:27:41 +0000234 IRuntime();
235 IRuntime(const IRuntime::CreationOptions& options);
236 ~IRuntime();
237
238 std::unique_ptr<RuntimeImpl> pRuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +0000239};
240
Derek Lamberti836b27b2019-11-20 10:51:57 +0000241
242/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100243using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000244
telsoa01c577f2c2018-08-31 09:22:23 +0100245/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
246/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
247/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000248///
249/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100250/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
251/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000252/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
253///
telsoa01c577f2c2018-08-31 09:22:23 +0100254/// 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 +0000255/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100256class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000257{
258public:
259 enum class Mode
260 {
261 UseTunedParameters,
262 UpdateTunedParameters
263 };
264
Ruomei Yan49937f32019-04-25 14:24:05 +0100265 enum class TuningLevel
266 {
Inki Dae23dbe3d2021-03-16 16:24:09 +0900267 Rapid = 1,
268 Normal = 2,
269 Exhaustive = 3
Ruomei Yan49937f32019-04-25 14:24:05 +0100270 };
271
telsoa014fcda012018-03-09 14:13:49 +0000272 /// Creates an IClTunedParameters with the given mode.
273 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100274 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
275 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000276 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100277 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000278
279 /// Loads an existing set of tuned parameters from the given file.
280 /// If there is an error loading the file, an armnn::Exception is thrown.
281 virtual void Load(const char* filename) = 0;
282
283 /// Saves the current set of tuned parameters to the given file.
284 /// If there is an error saving to the file, an armnn::Exception is thrown.
285 virtual void Save(const char* filename) const = 0;
286
287protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100288 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000289};
290
David Monahan4f1e8e42019-09-04 09:22:10 +0100291} // namespace armnn