blob: 8391ed3b15c156582aec5d69cc8eddff20afb628 [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"
13
Matthew Bentham313e1c82019-03-25 17:37:47 +000014#include <memory>
15
telsoa014fcda012018-03-09 14:13:49 +000016namespace armnn
17{
18
19using NetworkId = int;
20
telsoa01c577f2c2018-08-31 09:22:23 +010021class IGpuAccTunedParameters;
telsoa014fcda012018-03-09 14:13:49 +000022
23class IRuntime;
24using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
25
David Monahan4f1e8e42019-09-04 09:22:10 +010026struct INetworkProperties
27{
28 INetworkProperties(bool importEnabled = false, bool exportEnabled = false)
29 : m_ImportEnabled(importEnabled),
30 m_ExportEnabled(exportEnabled) {}
31
32 const bool m_ImportEnabled;
33 const bool m_ExportEnabled;
34
35 virtual ~INetworkProperties() {}
36};
37
telsoa014fcda012018-03-09 14:13:49 +000038class IRuntime
39{
40public:
41 struct CreationOptions
42 {
telsoa01c577f2c2018-08-31 09:22:23 +010043 CreationOptions()
44 : m_GpuAccTunedParameters(nullptr)
45 , m_EnableGpuProfiling(false)
Matteo Martincighe7d44982019-08-05 12:16:47 +010046 , m_DynamicBackendsPath("")
telsoa01c577f2c2018-08-31 09:22:23 +010047 {}
telsoa014fcda012018-03-09 14:13:49 +000048
telsoa01c577f2c2018-08-31 09:22:23 +010049 /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads.
50 /// It will also be updated with new tuned parameters if it is configured to do so.
51 std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters;
52
Ryan OShea2bbfaa72020-02-12 16:15:27 +000053 /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
telsoa01c577f2c2018-08-31 09:22:23 +010054 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +010055
Ryan OShea2bbfaa72020-02-12 16:15:27 +000056 /// Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive
57 /// Only a single path is allowed for the override
Matteo Martincighe7d44982019-08-05 12:16:47 +010058 std::string m_DynamicBackendsPath;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010059
60 struct ExternalProfilingOptions
61 {
62 ExternalProfilingOptions()
63 : m_EnableProfiling(false)
64 , m_OutgoingCaptureFile("")
65 , m_IncomingCaptureFile("")
66 , m_FileOnly(false)
Colm Donelan02705242019-11-14 14:19:07 +000067 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
Isabella Gottardia0687ee2020-03-11 18:04:20 +000068 , m_FileFormat("binary")
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010069 {}
70
71 bool m_EnableProfiling;
72 std::string m_OutgoingCaptureFile;
73 std::string m_IncomingCaptureFile;
74 bool m_FileOnly;
75 uint32_t m_CapturePeriod;
Isabella Gottardia0687ee2020-03-11 18:04:20 +000076 std::string m_FileFormat;
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010077 };
Jim Flynn4951b8c2019-10-03 10:04:30 -070078 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +000079
80 /// Pass backend specific options.
81 ///
82 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +000083 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +000084 /// m_BackendOption.emplace_back(
85 /// BackendOptions{"GpuAcc",
86 /// {
87 /// {"TuningLevel", 2},
88 /// {"TuningFile", filename}
89 /// }
90 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +000091 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +000092 /// Execute representative workloads through the runtime to generate tuning data.
93 /// The tuning file is written once the runtime is destroyed
94
95 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +000096 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +000097 /// m_BackendOption.emplace_back(
98 /// BackendOptions{"GpuAcc",
99 /// {
100 /// {"TuningFile", filename}
101 /// }
102 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000103 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000104
105 /// The following backend options are available:
106 /// GpuAcc:
107 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
108 /// "TuningFile" : string [filenameString]
109 /// "KernelProfilingEnabled" : bool [true | false]
110 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000111 };
112
113 static IRuntime* CreateRaw(const CreationOptions& options);
114 static IRuntimePtr Create(const CreationOptions& options);
115 static void Destroy(IRuntime* runtime);
116
telsoa01c577f2c2018-08-31 09:22:23 +0100117 /// Loads a complete network into the IRuntime.
118 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
119 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000120 /// The runtime takes ownership of the network once passed in.
121 /// @return armnn::Status
122 virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) = 0;
123
telsoa01c577f2c2018-08-31 09:22:23 +0100124 /// Load a complete network into the IRuntime.
125 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
126 /// @param [in] network Complete network to load into the IRuntime.
127 /// @param [out] errorMessage Error message if there were any errors.
128 /// The runtime takes ownership of the network once passed in.
129 /// @return armnn::Status
130 virtual Status LoadNetwork(NetworkId& networkIdOut,
131 IOptimizedNetworkPtr network,
David Monahan4f1e8e42019-09-04 09:22:10 +0100132 std::string& errorMessage) = 0;
133
134 virtual Status LoadNetwork(NetworkId& networkIdOut,
135 IOptimizedNetworkPtr network,
136 std::string& errorMessage,
137 const INetworkProperties& networkProperties) = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100138
telsoa014fcda012018-03-09 14:13:49 +0000139 virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
140 virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
141
telsoa01c577f2c2018-08-31 09:22:23 +0100142 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
telsoa014fcda012018-03-09 14:13:49 +0000143 virtual Status EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100144 const InputTensors& inputTensors,
145 const OutputTensors& outputTensors) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000146
telsoa01c577f2c2018-08-31 09:22:23 +0100147 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000148 /// At the moment this only removes the network from the m_Impl->m_Network.
149 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100150 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000151 /// @return armnn::Status
152 virtual Status UnloadNetwork(NetworkId networkId) = 0;
153
telsoa01c577f2c2018-08-31 09:22:23 +0100154 virtual const IDeviceSpec& GetDeviceSpec() const = 0;
155
156 /// Gets the profiler corresponding to the given network id.
157 /// @param networkId The id of the network for which to get the profile.
158 /// @return A pointer to the requested profiler, or nullptr if not found.
159 virtual const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000160
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000161 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
162 /// @param networkId The id of the network to register the callback.
163 /// @param func callback function to pass to the debug layer.
164 virtual void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) = 0;
165
telsoa014fcda012018-03-09 14:13:49 +0000166protected:
167 ~IRuntime() {}
168};
169
Derek Lamberti836b27b2019-11-20 10:51:57 +0000170
171/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100172using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000173
telsoa01c577f2c2018-08-31 09:22:23 +0100174/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
175/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
176/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000177///
178/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100179/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
180/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000181/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
182///
telsoa01c577f2c2018-08-31 09:22:23 +0100183/// 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 +0000184/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100185class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000186{
187public:
188 enum class Mode
189 {
190 UseTunedParameters,
191 UpdateTunedParameters
192 };
193
Ruomei Yan49937f32019-04-25 14:24:05 +0100194 enum class TuningLevel
195 {
196 Rapid = 0,
197 Normal = 1,
198 Exhaustive = 2
199 };
200
telsoa014fcda012018-03-09 14:13:49 +0000201 /// Creates an IClTunedParameters with the given mode.
202 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100203 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
204 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000205 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100206 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000207
208 /// Loads an existing set of tuned parameters from the given file.
209 /// If there is an error loading the file, an armnn::Exception is thrown.
210 virtual void Load(const char* filename) = 0;
211
212 /// Saves the current set of tuned parameters to the given file.
213 /// If there is an error saving to the file, an armnn::Exception is thrown.
214 virtual void Save(const char* filename) const = 0;
215
216protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100217 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000218};
219
David Monahan4f1e8e42019-09-04 09:22:10 +0100220} // namespace armnn