blob: fc203e67e45bdd6165e6961757ce4aa9aef038a4 [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{
Mike Kelly55a8ffd2021-04-07 20:10:49 +010031 INetworkProperties(bool importEnabled = false, bool exportEnabled = false, bool asyncEnabled = false)
David Monahan4f1e8e42019-09-04 09:22:10 +010032 : m_ImportEnabled(importEnabled),
Mike Kelly55a8ffd2021-04-07 20:10:49 +010033 m_ExportEnabled(exportEnabled),
34 m_AsyncEnabled(asyncEnabled) {}
David Monahan4f1e8e42019-09-04 09:22:10 +010035
36 const bool m_ImportEnabled;
37 const bool m_ExportEnabled;
Mike Kelly55a8ffd2021-04-07 20:10:49 +010038 const bool m_AsyncEnabled;
David Monahan4f1e8e42019-09-04 09:22:10 +010039
40 virtual ~INetworkProperties() {}
41};
42
Mike Kelly386ff1a2021-03-29 15:04:50 +010043using namespace armnn::experimental;
44
telsoa014fcda012018-03-09 14:13:49 +000045class IRuntime
46{
47public:
48 struct CreationOptions
49 {
telsoa01c577f2c2018-08-31 09:22:23 +010050 CreationOptions()
51 : m_GpuAccTunedParameters(nullptr)
52 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010053 , m_DynamicBackendsPath("")
telsoa01c577f2c2018-08-31 09:22:23 +010054 {}
telsoa014fcda012018-03-09 14:13:49 +000055
telsoa01c577f2c2018-08-31 09:22:23 +010056 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
57 /// It will also be updated with new tuned parameters if it is configured to do so.
58 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
59
Ryan OShea2bbfaa72020-02-12 16:15:27 +000060 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +010061 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +010062
Ryan OShea2bbfaa72020-02-12 16:15:27 +000063 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
64 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +010065 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010066
67 struct ExternalProfilingOptions
68 {
69 ExternalProfilingOptions()
70 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +010071 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010072 , m_OutgoingCaptureFile("")
73 , m_IncomingCaptureFile("")
74 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +000075 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +000076 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +010077 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010078 {}
79
80 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +010081 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010082 std::string m_OutgoingCaptureFile;
83 std::string m_IncomingCaptureFile;
84 bool m_FileOnly;
85 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +000086 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +010087 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010088 };
Jim Flynn4951b8c2019-10-03 10:04:30 -070089 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +000090
91 /// Pass backend specific options.
92 ///
93 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +000094 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +000095 /// m_BackendOption.emplace_back(
96 /// BackendOptions{"GpuAcc",
97 /// {
98 /// {"TuningLevel", 2},
99 /// {"TuningFile", filename}
100 /// }
101 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000102 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000103 /// Execute representative workloads through the runtime to generate tuning data.
104 /// The tuning file is written once the runtime is destroyed
105
106 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000107 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000108 /// m_BackendOption.emplace_back(
109 /// BackendOptions{"GpuAcc",
110 /// {
111 /// {"TuningFile", filename}
112 /// }
113 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000114 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000115
116 /// The following backend options are available:
117 /// GpuAcc:
118 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
119 /// "TuningFile" : string [filenameString]
120 /// "KernelProfilingEnabled" : bool [true | false]
121 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000122 };
123
124 static IRuntime* CreateRaw(const CreationOptions& options);
125 static IRuntimePtr Create(const CreationOptions& options);
126 static void Destroy(IRuntime* runtime);
127
telsoa01c577f2c2018-08-31 09:22:23 +0100128 /// Loads a complete network into the IRuntime.
129 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
130 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000131 /// The runtime takes ownership of the network once passed in.
132 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000133 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000134
telsoa01c577f2c2018-08-31 09:22:23 +0100135 /// Load a complete network into the IRuntime.
136 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
137 /// @param [in] network Complete network to load into the IRuntime.
138 /// @param [out] errorMessage Error message if there were any errors.
139 /// The runtime takes ownership of the network once passed in.
140 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000141 Status LoadNetwork(NetworkId& networkIdOut,
142 IOptimizedNetworkPtr network,
143 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100144
Kevin Mayd92a6e42021-02-04 10:27:41 +0000145 Status LoadNetwork(NetworkId& networkIdOut,
146 IOptimizedNetworkPtr network,
147 std::string& errorMessage,
148 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100149
Kevin Mayd92a6e42021-02-04 10:27:41 +0000150 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
151 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000152
telsoa01c577f2c2018-08-31 09:22:23 +0100153 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000154 Status EnqueueWorkload(NetworkId networkId,
155 const InputTensors& inputTensors,
156 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000157
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100158 /// This is an experimental function.
159 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors.
160 /// This function performs a thread safe execution of the network. Returns once execution is complete.
161 /// Will block until this and any other thread using the same workingMem object completes.
162 Status Execute(IWorkingMemHandle& workingMemHandle,
163 const InputTensors& inputTensors,
164 const OutputTensors& outputTensors);
165
telsoa01c577f2c2018-08-31 09:22:23 +0100166 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000167 /// At the moment this only removes the network from the m_Impl->m_Network.
168 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100169 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000170 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000171 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000172
Kevin Mayd92a6e42021-02-04 10:27:41 +0000173 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100174
Mike Kelly55a8ffd2021-04-07 20:10:49 +0100175 /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
176 /// overlapped Execution by calling this function from different threads.
177 std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
178
telsoa01c577f2c2018-08-31 09:22:23 +0100179 /// Gets the profiler corresponding to the given network id.
180 /// @param networkId The id of the network for which to get the profile.
181 /// @return A pointer to the requested profiler, or nullptr if not found.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000182 const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
telsoa014fcda012018-03-09 14:13:49 +0000183
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000184 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
185 /// @param networkId The id of the network to register the callback.
186 /// @param func callback function to pass to the debug layer.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000187 void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000188
telsoa014fcda012018-03-09 14:13:49 +0000189protected:
Kevin Mayd92a6e42021-02-04 10:27:41 +0000190 IRuntime();
191 IRuntime(const IRuntime::CreationOptions& options);
192 ~IRuntime();
193
194 std::unique_ptr<RuntimeImpl> pRuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +0000195};
196
Derek Lamberti836b27b2019-11-20 10:51:57 +0000197
198/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100199using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000200
telsoa01c577f2c2018-08-31 09:22:23 +0100201/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
202/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
203/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000204///
205/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100206/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
207/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000208/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
209///
telsoa01c577f2c2018-08-31 09:22:23 +0100210/// 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 +0000211/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100212class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000213{
214public:
215 enum class Mode
216 {
217 UseTunedParameters,
218 UpdateTunedParameters
219 };
220
Ruomei Yan49937f32019-04-25 14:24:05 +0100221 enum class TuningLevel
222 {
Inki Dae23dbe3d2021-03-16 16:24:09 +0900223 Rapid = 1,
224 Normal = 2,
225 Exhaustive = 3
Ruomei Yan49937f32019-04-25 14:24:05 +0100226 };
227
telsoa014fcda012018-03-09 14:13:49 +0000228 /// Creates an IClTunedParameters with the given mode.
229 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100230 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
231 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000232 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100233 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000234
235 /// Loads an existing set of tuned parameters from the given file.
236 /// If there is an error loading the file, an armnn::Exception is thrown.
237 virtual void Load(const char* filename) = 0;
238
239 /// Saves the current set of tuned parameters to the given file.
240 /// If there is an error saving to the file, an armnn::Exception is thrown.
241 virtual void Save(const char* filename) const = 0;
242
243protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100244 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000245};
246
David Monahan4f1e8e42019-09-04 09:22:10 +0100247} // namespace armnn