blob: 55c57974dc606ec2c43e80519fdba838953863e0 [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"
Matthew Bentham313e1c82019-03-25 17:37:47 +000011#include "Tensor.hpp"
12#include "Types.hpp"
telsoa014fcda012018-03-09 14:13:49 +000013#include "TypesUtils.hpp"
Jim Flynn4e755a52020-03-29 17:48:26 +010014#include "profiling/ILocalPacketHandler.hpp"
telsoa014fcda012018-03-09 14:13:49 +000015
Matthew Bentham313e1c82019-03-25 17:37:47 +000016#include <memory>
17
telsoa014fcda012018-03-09 14:13:49 +000018namespace armnn
19{
20
21using NetworkId = int;
22
telsoa01c577f2c2018-08-31 09:22:23 +010023class IGpuAccTunedParameters;
telsoa014fcda012018-03-09 14:13:49 +000024
Kevin Mayd92a6e42021-02-04 10:27:41 +000025struct RuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +000026class IRuntime;
27using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
28
David Monahan4f1e8e42019-09-04 09:22:10 +010029struct INetworkProperties
30{
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010031 ARMNN_DEPRECATED_MSG("Please use INetworkProperties constructor with MemorySource argument")
32 INetworkProperties(bool importEnabled = false,
33 bool exportEnabled = false,
34 bool asyncEnabled = false)
35 : m_ImportEnabled(importEnabled)
36 , m_ExportEnabled(exportEnabled)
37 , m_AsyncEnabled(asyncEnabled)
38 , m_InputSource(MemorySource::Undefined)
39 , m_OutputSource(MemorySource::Undefined)
40 {}
David Monahan4f1e8e42019-09-04 09:22:10 +010041
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010042 INetworkProperties(bool asyncEnabled,
43 MemorySource m_InputSource,
44 MemorySource m_OutputSource)
45 : m_ImportEnabled(m_InputSource != MemorySource::Undefined)
46 , m_ExportEnabled(m_OutputSource != MemorySource::Undefined)
47 , m_AsyncEnabled(asyncEnabled)
48 , m_InputSource(m_InputSource)
49 , m_OutputSource(m_OutputSource)
50 {}
51
52 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010053 const bool m_ImportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010054 /// Deprecated and will be removed in future release.
David Monahan4f1e8e42019-09-04 09:22:10 +010055 const bool m_ExportEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010056
Mike Kelly55a8ffd2021-04-07 20:10:49 +010057 const bool m_AsyncEnabled;
Francis Murtagh73d3e2e2021-04-29 14:23:04 +010058 const MemorySource m_InputSource;
59 const MemorySource m_OutputSource;
David Monahan4f1e8e42019-09-04 09:22:10 +010060
61 virtual ~INetworkProperties() {}
62};
63
Mike Kelly386ff1a2021-03-29 15:04:50 +010064using namespace armnn::experimental;
65
telsoa014fcda012018-03-09 14:13:49 +000066class IRuntime
67{
68public:
69 struct CreationOptions
70 {
telsoa01c577f2c2018-08-31 09:22:23 +010071 CreationOptions()
72 : m_GpuAccTunedParameters(nullptr)
73 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010074 , m_DynamicBackendsPath("")
telsoa01c577f2c2018-08-31 09:22:23 +010075 {}
telsoa014fcda012018-03-09 14:13:49 +000076
telsoa01c577f2c2018-08-31 09:22:23 +010077 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
78 /// It will also be updated with new tuned parameters if it is configured to do so.
79 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
80
Ryan OShea2bbfaa72020-02-12 16:15:27 +000081 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +010082 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +010083
Ryan OShea2bbfaa72020-02-12 16:15:27 +000084 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
85 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +010086 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010087
88 struct ExternalProfilingOptions
89 {
90 ExternalProfilingOptions()
91 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +010092 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010093 , m_OutgoingCaptureFile("")
94 , m_IncomingCaptureFile("")
95 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +000096 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +000097 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +010098 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010099 {}
100
101 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +0100102 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100103 std::string m_OutgoingCaptureFile;
104 std::string m_IncomingCaptureFile;
105 bool m_FileOnly;
106 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +0000107 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +0100108 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +0100109 };
Jim Flynn4951b8c2019-10-03 10:04:30 -0700110 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +0000111
112 /// Pass backend specific options.
113 ///
114 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000115 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000116 /// m_BackendOption.emplace_back(
117 /// BackendOptions{"GpuAcc",
118 /// {
119 /// {"TuningLevel", 2},
120 /// {"TuningFile", filename}
121 /// }
122 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000123 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000124 /// Execute representative workloads through the runtime to generate tuning data.
125 /// The tuning file is written once the runtime is destroyed
126
127 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000128 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000129 /// m_BackendOption.emplace_back(
130 /// BackendOptions{"GpuAcc",
131 /// {
132 /// {"TuningFile", filename}
133 /// }
134 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000135 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000136
137 /// The following backend options are available:
138 /// GpuAcc:
139 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
140 /// "TuningFile" : string [filenameString]
141 /// "KernelProfilingEnabled" : bool [true | false]
142 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000143 };
144
145 static IRuntime* CreateRaw(const CreationOptions& options);
146 static IRuntimePtr Create(const CreationOptions& options);
147 static void Destroy(IRuntime* runtime);
148
telsoa01c577f2c2018-08-31 09:22:23 +0100149 /// Loads a complete network into the IRuntime.
150 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
151 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000152 /// The runtime takes ownership of the network once passed in.
153 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000154 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000155
telsoa01c577f2c2018-08-31 09:22:23 +0100156 /// Load 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.
159 /// @param [out] errorMessage Error message if there were any errors.
160 /// The runtime takes ownership of the network once passed in.
161 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000162 Status LoadNetwork(NetworkId& networkIdOut,
163 IOptimizedNetworkPtr network,
164 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100165
Kevin Mayd92a6e42021-02-04 10:27:41 +0000166 Status LoadNetwork(NetworkId& networkIdOut,
167 IOptimizedNetworkPtr network,
168 std::string& errorMessage,
169 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100170
Kevin Mayd92a6e42021-02-04 10:27:41 +0000171 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
172 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000173
telsoa01c577f2c2018-08-31 09:22:23 +0100174 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000175 Status EnqueueWorkload(NetworkId networkId,
176 const InputTensors& inputTensors,
177 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000178
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100179 /// This is an experimental function.
180 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors.
181 /// This function performs a thread safe execution of the network. Returns once execution is complete.
182 /// Will block until this and any other thread using the same workingMem object completes.
183 Status Execute(IWorkingMemHandle& workingMemHandle,
184 const InputTensors& inputTensors,
185 const OutputTensors& outputTensors);
186
telsoa01c577f2c2018-08-31 09:22:23 +0100187 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000188 /// At the moment this only removes the network from the m_Impl->m_Network.
189 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100190 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000191 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000192 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000193
Kevin Mayd92a6e42021-02-04 10:27:41 +0000194 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100195
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100196 /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
197 /// overlapped Execution by calling this function from different threads.
198 std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
199
telsoa01c577f2c2018-08-31 09:22:23 +0100200 /// Gets the profiler corresponding to the given network id.
201 /// @param networkId The id of the network for which to get the profile.
202 /// @return A pointer to the requested profiler, or nullptr if not found.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000203 const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
telsoa014fcda012018-03-09 14:13:49 +0000204
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000205 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
206 /// @param networkId The id of the network to register the callback.
207 /// @param func callback function to pass to the debug layer.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000208 void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000209
telsoa014fcda012018-03-09 14:13:49 +0000210protected:
Kevin Mayd92a6e42021-02-04 10:27:41 +0000211 IRuntime();
212 IRuntime(const IRuntime::CreationOptions& options);
213 ~IRuntime();
214
215 std::unique_ptr<RuntimeImpl> pRuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +0000216};
217
Derek Lamberti836b27b2019-11-20 10:51:57 +0000218
219/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100220using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000221
telsoa01c577f2c2018-08-31 09:22:23 +0100222/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
223/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
224/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000225///
226/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100227/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
228/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000229/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
230///
telsoa01c577f2c2018-08-31 09:22:23 +0100231/// 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 +0000232/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100233class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000234{
235public:
236 enum class Mode
237 {
238 UseTunedParameters,
239 UpdateTunedParameters
240 };
241
Ruomei Yan49937f32019-04-25 14:24:05 +0100242 enum class TuningLevel
243 {
Inki Dae23dbe3d2021-03-16 16:24:09 +0900244 Rapid = 1,
245 Normal = 2,
246 Exhaustive = 3
Ruomei Yan49937f32019-04-25 14:24:05 +0100247 };
248
telsoa014fcda012018-03-09 14:13:49 +0000249 /// Creates an IClTunedParameters with the given mode.
250 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100251 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
252 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000253 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100254 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000255
256 /// Loads an existing set of tuned parameters from the given file.
257 /// If there is an error loading the file, an armnn::Exception is thrown.
258 virtual void Load(const char* filename) = 0;
259
260 /// Saves the current set of tuned parameters to the given file.
261 /// If there is an error saving to the file, an armnn::Exception is thrown.
262 virtual void Save(const char* filename) const = 0;
263
264protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100265 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000266};
267
David Monahan4f1e8e42019-09-04 09:22:10 +0100268} // namespace armnn