blob: 9f7032914f9705f7f83fc089207a6a099b7f2074 [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"
Mike Kelly386ff1a2021-03-29 15:04:50 +01008#include "IAsyncNetwork.hpp"
telsoa014fcda012018-03-09 14:13:49 +00009#include "INetwork.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010010#include "IProfiler.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{
31 INetworkProperties(bool importEnabled = false, bool exportEnabled = false)
32 : m_ImportEnabled(importEnabled),
33 m_ExportEnabled(exportEnabled) {}
34
35 const bool m_ImportEnabled;
36 const bool m_ExportEnabled;
37
38 virtual ~INetworkProperties() {}
39};
40
Mike Kelly386ff1a2021-03-29 15:04:50 +010041using namespace armnn::experimental;
42
telsoa014fcda012018-03-09 14:13:49 +000043class IRuntime
44{
45public:
46 struct CreationOptions
47 {
telsoa01c577f2c2018-08-31 09:22:23 +010048 CreationOptions()
49 : m_GpuAccTunedParameters(nullptr)
50 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010051 , m_DynamicBackendsPath("")
telsoa01c577f2c2018-08-31 09:22:23 +010052 {}
telsoa014fcda012018-03-09 14:13:49 +000053
telsoa01c577f2c2018-08-31 09:22:23 +010054 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
55 /// It will also be updated with new tuned parameters if it is configured to do so.
56 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
57
Ryan OShea2bbfaa72020-02-12 16:15:27 +000058 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +010059 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +010060
Ryan OShea2bbfaa72020-02-12 16:15:27 +000061 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
62 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +010063 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010064
65 struct ExternalProfilingOptions
66 {
67 ExternalProfilingOptions()
68 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +010069 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010070 , m_OutgoingCaptureFile("")
71 , m_IncomingCaptureFile("")
72 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +000073 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +000074 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +010075 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010076 {}
77
78 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +010079 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010080 std::string m_OutgoingCaptureFile;
81 std::string m_IncomingCaptureFile;
82 bool m_FileOnly;
83 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +000084 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +010085 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010086 };
Jim Flynn4951b8c2019-10-03 10:04:30 -070087 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +000088
89 /// Pass backend specific options.
90 ///
91 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +000092 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +000093 /// m_BackendOption.emplace_back(
94 /// BackendOptions{"GpuAcc",
95 /// {
96 /// {"TuningLevel", 2},
97 /// {"TuningFile", filename}
98 /// }
99 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000100 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000101 /// Execute representative workloads through the runtime to generate tuning data.
102 /// The tuning file is written once the runtime is destroyed
103
104 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000105 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000106 /// m_BackendOption.emplace_back(
107 /// BackendOptions{"GpuAcc",
108 /// {
109 /// {"TuningFile", filename}
110 /// }
111 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000112 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000113
114 /// The following backend options are available:
115 /// GpuAcc:
116 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
117 /// "TuningFile" : string [filenameString]
118 /// "KernelProfilingEnabled" : bool [true | false]
119 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000120 };
121
122 static IRuntime* CreateRaw(const CreationOptions& options);
123 static IRuntimePtr Create(const CreationOptions& options);
124 static void Destroy(IRuntime* runtime);
125
telsoa01c577f2c2018-08-31 09:22:23 +0100126 /// Loads a complete network into the IRuntime.
127 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
128 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000129 /// The runtime takes ownership of the network once passed in.
130 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000131 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000132
telsoa01c577f2c2018-08-31 09:22:23 +0100133 /// Load a complete network into the IRuntime.
134 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
135 /// @param [in] network Complete network to load into the IRuntime.
136 /// @param [out] errorMessage Error message if there were any errors.
137 /// The runtime takes ownership of the network once passed in.
138 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000139 Status LoadNetwork(NetworkId& networkIdOut,
140 IOptimizedNetworkPtr network,
141 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100142
Kevin Mayd92a6e42021-02-04 10:27:41 +0000143 Status LoadNetwork(NetworkId& networkIdOut,
144 IOptimizedNetworkPtr network,
145 std::string& errorMessage,
146 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100147
Mike Kelly386ff1a2021-03-29 15:04:50 +0100148 /// This is an experimental function.
149 /// Creates an executable network. This network is thread safe allowing for multiple networks to be
150 /// loaded simultaneously via different threads.
151 /// Note that the network is never registered with the runtime so does not need to be 'Unloaded'.
152 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
153 /// @param [in] network Complete network to load into the IRuntime.
154 /// @param [out] errorMessage Error message if there were any errors.
155 /// @param [out] networkProperties the INetworkProperties that govern how the network should operate.
156 /// @return The IAsyncNetwork
157 std::unique_ptr<IAsyncNetwork> CreateAsyncNetwork(NetworkId& networkIdOut,
158 IOptimizedNetworkPtr network,
159 std::string& errorMessage,
160 const INetworkProperties& networkProperties);
161
Kevin Mayd92a6e42021-02-04 10:27:41 +0000162 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
163 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000164
telsoa01c577f2c2018-08-31 09:22:23 +0100165 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000166 Status EnqueueWorkload(NetworkId networkId,
167 const InputTensors& inputTensors,
168 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000169
telsoa01c577f2c2018-08-31 09:22:23 +0100170 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000171 /// At the moment this only removes the network from the m_Impl->m_Network.
172 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100173 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000174 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000175 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000176
Kevin Mayd92a6e42021-02-04 10:27:41 +0000177 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100178
179 /// 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