blob: 48ad7c494ddcfb22739f6c7f25fd302e4eeea4b8 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// 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"
Matthew Bentham313e1c82019-03-25 17:37:47 +000010#include "Tensor.hpp"
11#include "Types.hpp"
telsoa014fcda012018-03-09 14:13:49 +000012#include "TypesUtils.hpp"
Jim Flynn4e755a52020-03-29 17:48:26 +010013#include "profiling/ILocalPacketHandler.hpp"
telsoa014fcda012018-03-09 14:13:49 +000014
Matthew Bentham313e1c82019-03-25 17:37:47 +000015#include <memory>
16
telsoa014fcda012018-03-09 14:13:49 +000017namespace armnn
18{
19
20using NetworkId = int;
21
telsoa01c577f2c2018-08-31 09:22:23 +010022class IGpuAccTunedParameters;
telsoa014fcda012018-03-09 14:13:49 +000023
24class IRuntime;
25using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
26
David Monahan4f1e8e42019-09-04 09:22:10 +010027struct INetworkProperties
28{
29 INetworkProperties(bool importEnabled = false, bool exportEnabled = false)
30 : m_ImportEnabled(importEnabled),
31 m_ExportEnabled(exportEnabled) {}
32
33 const bool m_ImportEnabled;
34 const bool m_ExportEnabled;
35
36 virtual ~INetworkProperties() {}
37};
38
telsoa014fcda012018-03-09 14:13:49 +000039class IRuntime
40{
41public:
42 struct CreationOptions
43 {
telsoa01c577f2c2018-08-31 09:22:23 +010044 CreationOptions()
45 : m_GpuAccTunedParameters(nullptr)
46 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010047 , m_DynamicBackendsPath("")
telsoa01c577f2c2018-08-31 09:22:23 +010048 {}
telsoa014fcda012018-03-09 14:13:49 +000049
telsoa01c577f2c2018-08-31 09:22:23 +010050 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
51 /// It will also be updated with new tuned parameters if it is configured to do so.
52 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
53
Ryan OShea2bbfaa72020-02-12 16:15:27 +000054 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +010055 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +010056
Ryan OShea2bbfaa72020-02-12 16:15:27 +000057 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
58 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +010059 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010060
61 struct ExternalProfilingOptions
62 {
63 ExternalProfilingOptions()
64 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +010065 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010066 , m_OutgoingCaptureFile("")
67 , m_IncomingCaptureFile("")
68 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +000069 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +000070 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +010071 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010072 {}
73
74 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +010075 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010076 std::string m_OutgoingCaptureFile;
77 std::string m_IncomingCaptureFile;
78 bool m_FileOnly;
79 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +000080 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +010081 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010082 };
Jim Flynn4951b8c2019-10-03 10:04:30 -070083 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +000084
85 /// Pass backend specific options.
86 ///
87 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +000088 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +000089 /// m_BackendOption.emplace_back(
90 /// BackendOptions{"GpuAcc",
91 /// {
92 /// {"TuningLevel", 2},
93 /// {"TuningFile", filename}
94 /// }
95 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +000096 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +000097 /// Execute representative workloads through the runtime to generate tuning data.
98 /// The tuning file is written once the runtime is destroyed
99
100 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000101 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000102 /// m_BackendOption.emplace_back(
103 /// BackendOptions{"GpuAcc",
104 /// {
105 /// {"TuningFile", filename}
106 /// }
107 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000108 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000109
110 /// The following backend options are available:
111 /// GpuAcc:
112 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
113 /// "TuningFile" : string [filenameString]
114 /// "KernelProfilingEnabled" : bool [true | false]
115 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000116 };
117
118 static IRuntime* CreateRaw(const CreationOptions& options);
119 static IRuntimePtr Create(const CreationOptions& options);
120 static void Destroy(IRuntime* runtime);
121
telsoa01c577f2c2018-08-31 09:22:23 +0100122 /// Loads a complete network into the IRuntime.
123 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
124 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000125 /// The runtime takes ownership of the network once passed in.
126 /// @return armnn::Status
127 virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) = 0;
128
telsoa01c577f2c2018-08-31 09:22:23 +0100129 /// Load a complete network into the IRuntime.
130 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
131 /// @param [in] network Complete network to load into the IRuntime.
132 /// @param [out] errorMessage Error message if there were any errors.
133 /// The runtime takes ownership of the network once passed in.
134 /// @return armnn::Status
135 virtual Status LoadNetwork(NetworkId& networkIdOut,
136 IOptimizedNetworkPtr network,
David Monahan4f1e8e42019-09-04 09:22:10 +0100137 std::string& errorMessage) = 0;
138
139 virtual Status LoadNetwork(NetworkId& networkIdOut,
140 IOptimizedNetworkPtr network,
141 std::string& errorMessage,
142 const INetworkProperties& networkProperties) = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100143
telsoa014fcda012018-03-09 14:13:49 +0000144 virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
145 virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
146
telsoa01c577f2c2018-08-31 09:22:23 +0100147 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
telsoa014fcda012018-03-09 14:13:49 +0000148 virtual Status EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100149 const InputTensors& inputTensors,
150 const OutputTensors& outputTensors) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000151
telsoa01c577f2c2018-08-31 09:22:23 +0100152 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000153 /// At the moment this only removes the network from the m_Impl->m_Network.
154 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100155 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000156 /// @return armnn::Status
157 virtual Status UnloadNetwork(NetworkId networkId) = 0;
158
telsoa01c577f2c2018-08-31 09:22:23 +0100159 virtual const IDeviceSpec& GetDeviceSpec() const = 0;
160
161 /// Gets the profiler corresponding to the given network id.
162 /// @param networkId The id of the network for which to get the profile.
163 /// @return A pointer to the requested profiler, or nullptr if not found.
164 virtual const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000165
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000166 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
167 /// @param networkId The id of the network to register the callback.
168 /// @param func callback function to pass to the debug layer.
169 virtual void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) = 0;
170
telsoa014fcda012018-03-09 14:13:49 +0000171protected:
172 ~IRuntime() {}
173};
174
Derek Lamberti836b27b2019-11-20 10:51:57 +0000175
176/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100177using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000178
telsoa01c577f2c2018-08-31 09:22:23 +0100179/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
180/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
181/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000182///
183/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100184/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
185/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000186/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
187///
telsoa01c577f2c2018-08-31 09:22:23 +0100188/// 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 +0000189/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100190class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000191{
192public:
193 enum class Mode
194 {
195 UseTunedParameters,
196 UpdateTunedParameters
197 };
198
Ruomei Yan49937f32019-04-25 14:24:05 +0100199 enum class TuningLevel
200 {
201 Rapid = 0,
202 Normal = 1,
203 Exhaustive = 2
204 };
205
telsoa014fcda012018-03-09 14:13:49 +0000206 /// Creates an IClTunedParameters with the given mode.
207 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100208 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
209 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000210 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100211 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000212
213 /// Loads an existing set of tuned parameters from the given file.
214 /// If there is an error loading the file, an armnn::Exception is thrown.
215 virtual void Load(const char* filename) = 0;
216
217 /// Saves the current set of tuned parameters to the given file.
218 /// If there is an error saving to the file, an armnn::Exception is thrown.
219 virtual void Save(const char* filename) const = 0;
220
221protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100222 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000223};
224
David Monahan4f1e8e42019-09-04 09:22:10 +0100225} // namespace armnn