blob: 9122089b62be8ca3e68e4de44eb2e3e7d4b51da7 [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"
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
Kevin Mayd92a6e42021-02-04 10:27:41 +000024struct RuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +000025class IRuntime;
26using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
27
David Monahan4f1e8e42019-09-04 09:22:10 +010028struct INetworkProperties
29{
30 INetworkProperties(bool importEnabled = false, bool exportEnabled = false)
31 : m_ImportEnabled(importEnabled),
32 m_ExportEnabled(exportEnabled) {}
33
34 const bool m_ImportEnabled;
35 const bool m_ExportEnabled;
36
37 virtual ~INetworkProperties() {}
38};
39
telsoa014fcda012018-03-09 14:13:49 +000040class IRuntime
41{
42public:
43 struct CreationOptions
44 {
telsoa01c577f2c2018-08-31 09:22:23 +010045 CreationOptions()
46 : m_GpuAccTunedParameters(nullptr)
47 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010048 , m_DynamicBackendsPath("")
telsoa01c577f2c2018-08-31 09:22:23 +010049 {}
telsoa014fcda012018-03-09 14:13:49 +000050
telsoa01c577f2c2018-08-31 09:22:23 +010051 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
52 /// It will also be updated with new tuned parameters if it is configured to do so.
53 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
54
Ryan OShea2bbfaa72020-02-12 16:15:27 +000055 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +010056 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +010057
Ryan OShea2bbfaa72020-02-12 16:15:27 +000058 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
59 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +010060 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010061
62 struct ExternalProfilingOptions
63 {
64 ExternalProfilingOptions()
65 : m_EnableProfiling(false)
Jim Flynn4e755a52020-03-29 17:48:26 +010066 , m_TimelineEnabled(false)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010067 , m_OutgoingCaptureFile("")
68 , m_IncomingCaptureFile("")
69 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +000070 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +000071 , m_FileFormat("binary")
Jim Flynn4e755a52020-03-29 17:48:26 +010072 , m_LocalPacketHandlers()
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010073 {}
74
75 bool m_EnableProfiling;
Jim Flynn4e755a52020-03-29 17:48:26 +010076 bool m_TimelineEnabled;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010077 std::string m_OutgoingCaptureFile;
78 std::string m_IncomingCaptureFile;
79 bool m_FileOnly;
80 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +000081 std::string m_FileFormat;
Jim Flynn4e755a52020-03-29 17:48:26 +010082 std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010083 };
Jim Flynn4951b8c2019-10-03 10:04:30 -070084 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +000085
86 /// Pass backend specific options.
87 ///
88 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +000089 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +000090 /// m_BackendOption.emplace_back(
91 /// BackendOptions{"GpuAcc",
92 /// {
93 /// {"TuningLevel", 2},
94 /// {"TuningFile", filename}
95 /// }
96 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +000097 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +000098 /// Execute representative workloads through the runtime to generate tuning data.
99 /// The tuning file is written once the runtime is destroyed
100
101 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000102 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +0000103 /// m_BackendOption.emplace_back(
104 /// BackendOptions{"GpuAcc",
105 /// {
106 /// {"TuningFile", filename}
107 /// }
108 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000109 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000110
111 /// The following backend options are available:
112 /// GpuAcc:
113 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
114 /// "TuningFile" : string [filenameString]
115 /// "KernelProfilingEnabled" : bool [true | false]
116 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000117 };
118
119 static IRuntime* CreateRaw(const CreationOptions& options);
120 static IRuntimePtr Create(const CreationOptions& options);
121 static void Destroy(IRuntime* runtime);
122
telsoa01c577f2c2018-08-31 09:22:23 +0100123 /// Loads a complete network into the IRuntime.
124 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
125 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000126 /// The runtime takes ownership of the network once passed in.
127 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000128 Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
telsoa014fcda012018-03-09 14:13:49 +0000129
telsoa01c577f2c2018-08-31 09:22:23 +0100130 /// Load a complete network into the IRuntime.
131 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
132 /// @param [in] network Complete network to load into the IRuntime.
133 /// @param [out] errorMessage Error message if there were any errors.
134 /// The runtime takes ownership of the network once passed in.
135 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000136 Status LoadNetwork(NetworkId& networkIdOut,
137 IOptimizedNetworkPtr network,
138 std::string& errorMessage);
David Monahan4f1e8e42019-09-04 09:22:10 +0100139
Kevin Mayd92a6e42021-02-04 10:27:41 +0000140 Status LoadNetwork(NetworkId& networkIdOut,
141 IOptimizedNetworkPtr network,
142 std::string& errorMessage,
143 const INetworkProperties& networkProperties);
telsoa01c577f2c2018-08-31 09:22:23 +0100144
Kevin Mayd92a6e42021-02-04 10:27:41 +0000145 TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
146 TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
telsoa014fcda012018-03-09 14:13:49 +0000147
telsoa01c577f2c2018-08-31 09:22:23 +0100148 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
Kevin Mayd92a6e42021-02-04 10:27:41 +0000149 Status EnqueueWorkload(NetworkId networkId,
150 const InputTensors& inputTensors,
151 const OutputTensors& outputTensors);
telsoa014fcda012018-03-09 14:13:49 +0000152
telsoa01c577f2c2018-08-31 09:22:23 +0100153 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000154 /// At the moment this only removes the network from the m_Impl->m_Network.
155 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100156 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000157 /// @return armnn::Status
Kevin Mayd92a6e42021-02-04 10:27:41 +0000158 Status UnloadNetwork(NetworkId networkId);
telsoa014fcda012018-03-09 14:13:49 +0000159
Kevin Mayd92a6e42021-02-04 10:27:41 +0000160 const IDeviceSpec& GetDeviceSpec() const;
telsoa01c577f2c2018-08-31 09:22:23 +0100161
162 /// Gets the profiler corresponding to the given network id.
163 /// @param networkId The id of the network for which to get the profile.
164 /// @return A pointer to the requested profiler, or nullptr if not found.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000165 const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
telsoa014fcda012018-03-09 14:13:49 +0000166
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000167 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
168 /// @param networkId The id of the network to register the callback.
169 /// @param func callback function to pass to the debug layer.
Kevin Mayd92a6e42021-02-04 10:27:41 +0000170 void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000171
telsoa014fcda012018-03-09 14:13:49 +0000172protected:
Kevin Mayd92a6e42021-02-04 10:27:41 +0000173 IRuntime();
174 IRuntime(const IRuntime::CreationOptions& options);
175 ~IRuntime();
176
177 std::unique_ptr<RuntimeImpl> pRuntimeImpl;
telsoa014fcda012018-03-09 14:13:49 +0000178};
179
Derek Lamberti836b27b2019-11-20 10:51:57 +0000180
181/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100182using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000183
telsoa01c577f2c2018-08-31 09:22:23 +0100184/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
185/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
186/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000187///
188/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100189/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
190/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000191/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
192///
telsoa01c577f2c2018-08-31 09:22:23 +0100193/// 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 +0000194/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100195class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000196{
197public:
198 enum class Mode
199 {
200 UseTunedParameters,
201 UpdateTunedParameters
202 };
203
Ruomei Yan49937f32019-04-25 14:24:05 +0100204 enum class TuningLevel
205 {
Inki Dae23dbe3d2021-03-16 16:24:09 +0900206 Rapid = 1,
207 Normal = 2,
208 Exhaustive = 3
Ruomei Yan49937f32019-04-25 14:24:05 +0100209 };
210
telsoa014fcda012018-03-09 14:13:49 +0000211 /// Creates an IClTunedParameters with the given mode.
212 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100213 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
214 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000215 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100216 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000217
218 /// Loads an existing set of tuned parameters from the given file.
219 /// If there is an error loading the file, an armnn::Exception is thrown.
220 virtual void Load(const char* filename) = 0;
221
222 /// Saves the current set of tuned parameters to the given file.
223 /// If there is an error saving to the file, an armnn::Exception is thrown.
224 virtual void Save(const char* filename) const = 0;
225
226protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100227 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000228};
229
David Monahan4f1e8e42019-09-04 09:22:10 +0100230} // namespace armnn