Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 7 | #include "BackendOptions.hpp" |
Mike Kelly | 386ff1a | 2021-03-29 15:04:50 +0100 | [diff] [blame^] | 8 | #include "IAsyncNetwork.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | #include "INetwork.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | #include "IProfiler.hpp" |
Matthew Bentham | 313e1c8 | 2019-03-25 17:37:47 +0000 | [diff] [blame] | 11 | #include "Tensor.hpp" |
| 12 | #include "Types.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | #include "TypesUtils.hpp" |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 14 | #include "profiling/ILocalPacketHandler.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | |
Matthew Bentham | 313e1c8 | 2019-03-25 17:37:47 +0000 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
| 20 | |
| 21 | using NetworkId = int; |
| 22 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | class IGpuAccTunedParameters; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 25 | struct RuntimeImpl; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 26 | class IRuntime; |
| 27 | using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>; |
| 28 | |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 29 | struct 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 Kelly | 386ff1a | 2021-03-29 15:04:50 +0100 | [diff] [blame^] | 41 | using namespace armnn::experimental; |
| 42 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 43 | class IRuntime |
| 44 | { |
| 45 | public: |
| 46 | struct CreationOptions |
| 47 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | CreationOptions() |
| 49 | : m_GpuAccTunedParameters(nullptr) |
| 50 | , m_EnableGpuProfiling(false) |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 51 | , m_DynamicBackendsPath("") |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 52 | {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 53 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | /// 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 OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 58 | /// Setting this flag will allow the user to obtain GPU profiling information from the runtime. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | bool m_EnableGpuProfiling; |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 60 | |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 61 | /// 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 Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame] | 63 | std::string m_DynamicBackendsPath; |
Aron Virginas-Tar | 1a0f691 | 2019-08-23 15:18:44 +0100 | [diff] [blame] | 64 | |
| 65 | struct ExternalProfilingOptions |
| 66 | { |
| 67 | ExternalProfilingOptions() |
| 68 | : m_EnableProfiling(false) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 69 | , m_TimelineEnabled(false) |
Aron Virginas-Tar | 1a0f691 | 2019-08-23 15:18:44 +0100 | [diff] [blame] | 70 | , m_OutgoingCaptureFile("") |
| 71 | , m_IncomingCaptureFile("") |
| 72 | , m_FileOnly(false) |
Colm Donelan | 0270524 | 2019-11-14 14:19:07 +0000 | [diff] [blame] | 73 | , m_CapturePeriod(LOWEST_CAPTURE_PERIOD) |
Isabella Gottardi | a0687ee | 2020-03-11 18:04:20 +0000 | [diff] [blame] | 74 | , m_FileFormat("binary") |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 75 | , m_LocalPacketHandlers() |
Aron Virginas-Tar | 1a0f691 | 2019-08-23 15:18:44 +0100 | [diff] [blame] | 76 | {} |
| 77 | |
| 78 | bool m_EnableProfiling; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 79 | bool m_TimelineEnabled; |
Aron Virginas-Tar | 1a0f691 | 2019-08-23 15:18:44 +0100 | [diff] [blame] | 80 | std::string m_OutgoingCaptureFile; |
| 81 | std::string m_IncomingCaptureFile; |
| 82 | bool m_FileOnly; |
| 83 | uint32_t m_CapturePeriod; |
Isabella Gottardi | a0687ee | 2020-03-11 18:04:20 +0000 | [diff] [blame] | 84 | std::string m_FileFormat; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 85 | std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers; |
Aron Virginas-Tar | 1a0f691 | 2019-08-23 15:18:44 +0100 | [diff] [blame] | 86 | }; |
Jim Flynn | 4951b8c | 2019-10-03 10:04:30 -0700 | [diff] [blame] | 87 | ExternalProfilingOptions m_ProfilingOptions; |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 88 | |
| 89 | /// Pass backend specific options. |
| 90 | /// |
| 91 | /// For example, to enable GpuAcc tuning add the following |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 92 | /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 93 | /// m_BackendOption.emplace_back( |
| 94 | /// BackendOptions{"GpuAcc", |
| 95 | /// { |
| 96 | /// {"TuningLevel", 2}, |
| 97 | /// {"TuningFile", filename} |
| 98 | /// } |
| 99 | /// }); |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 100 | /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 101 | /// 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 OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 105 | /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 106 | /// m_BackendOption.emplace_back( |
| 107 | /// BackendOptions{"GpuAcc", |
| 108 | /// { |
| 109 | /// {"TuningFile", filename} |
| 110 | /// } |
| 111 | /// }); |
Ryan OShea | 2bbfaa7 | 2020-02-12 16:15:27 +0000 | [diff] [blame] | 112 | /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 113 | |
| 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; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static IRuntime* CreateRaw(const CreationOptions& options); |
| 123 | static IRuntimePtr Create(const CreationOptions& options); |
| 124 | static void Destroy(IRuntime* runtime); |
| 125 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 126 | /// 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. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 129 | /// The runtime takes ownership of the network once passed in. |
| 130 | /// @return armnn::Status |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 131 | Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 132 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | /// 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 May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 139 | Status LoadNetwork(NetworkId& networkIdOut, |
| 140 | IOptimizedNetworkPtr network, |
| 141 | std::string& errorMessage); |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 142 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 143 | Status LoadNetwork(NetworkId& networkIdOut, |
| 144 | IOptimizedNetworkPtr network, |
| 145 | std::string& errorMessage, |
| 146 | const INetworkProperties& networkProperties); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 147 | |
Mike Kelly | 386ff1a | 2021-03-29 15:04:50 +0100 | [diff] [blame^] | 148 | /// 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 May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 162 | TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const; |
| 163 | TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 164 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 165 | /// Evaluates a network using input in inputTensors and outputs filled into outputTensors |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 166 | Status EnqueueWorkload(NetworkId networkId, |
| 167 | const InputTensors& inputTensors, |
| 168 | const OutputTensors& outputTensors); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 169 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 170 | /// Unloads a network from the IRuntime. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 171 | /// 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. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 173 | /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork(). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 174 | /// @return armnn::Status |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 175 | Status UnloadNetwork(NetworkId networkId); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 176 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 177 | const IDeviceSpec& GetDeviceSpec() const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 178 | |
| 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 May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 182 | const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 183 | |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 184 | /// 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 May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 187 | void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func); |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 188 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 189 | protected: |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 190 | IRuntime(); |
| 191 | IRuntime(const IRuntime::CreationOptions& options); |
| 192 | ~IRuntime(); |
| 193 | |
| 194 | std::unique_ptr<RuntimeImpl> pRuntimeImpl; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
Derek Lamberti | 836b27b | 2019-11-20 10:51:57 +0000 | [diff] [blame] | 197 | |
| 198 | /// The following API is replaced by the backend options API. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 199 | using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 200 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 201 | /// 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. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 204 | /// |
| 205 | /// Can be created in two modes: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 206 | /// - 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 |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 208 | /// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow. |
| 209 | /// |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 210 | /// The parameters can be loaded from and saved to a file so that you can first run a slow initial read-write |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 211 | /// execution, save the parameters for later and then run fast read-only executions using the optimised parameters. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 212 | class IGpuAccTunedParameters |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 213 | { |
| 214 | public: |
| 215 | enum class Mode |
| 216 | { |
| 217 | UseTunedParameters, |
| 218 | UpdateTunedParameters |
| 219 | }; |
| 220 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 221 | enum class TuningLevel |
| 222 | { |
Inki Dae | 23dbe3d | 2021-03-16 16:24:09 +0900 | [diff] [blame] | 223 | Rapid = 1, |
| 224 | Normal = 2, |
| 225 | Exhaustive = 3 |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 226 | }; |
| 227 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 228 | /// Creates an IClTunedParameters with the given mode. |
| 229 | /// @{ |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 230 | static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode); |
| 231 | static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 232 | /// @} |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 233 | static void Destroy(IGpuAccTunedParameters* params); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 234 | |
| 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 | |
| 243 | protected: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 244 | virtual ~IGpuAccTunedParameters() {}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
David Monahan | 4f1e8e4 | 2019-09-04 09:22:10 +0100 | [diff] [blame] | 247 | } // namespace armnn |