blob: 712355bb3aa920b6b63765af80db39c5329ec03c [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)
Aron Virginas-Tar1a0f6912019-08-23 15:18:44 +010068 {}
69
70 bool m_EnableProfiling;
71 std::string m_OutgoingCaptureFile;
72 std::string m_IncomingCaptureFile;
73 bool m_FileOnly;
74 uint32_t m_CapturePeriod;
75 };
Jim Flynn4951b8c2019-10-03 10:04:30 -070076 ExternalProfilingOptions m_ProfilingOptions;
Derek Lamberti836b27b2019-11-20 10:51:57 +000077
78 /// Pass backend specific options.
79 ///
80 /// For example, to enable GpuAcc tuning add the following
Ryan OShea2bbfaa72020-02-12 16:15:27 +000081 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +000082 /// m_BackendOption.emplace_back(
83 /// BackendOptions{"GpuAcc",
84 /// {
85 /// {"TuningLevel", 2},
86 /// {"TuningFile", filename}
87 /// }
88 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +000089 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +000090 /// Execute representative workloads through the runtime to generate tuning data.
91 /// The tuning file is written once the runtime is destroyed
92
93 /// To execute with the tuning data, start up with just the tuning file specified.
Ryan OShea2bbfaa72020-02-12 16:15:27 +000094 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
Derek Lamberti836b27b2019-11-20 10:51:57 +000095 /// m_BackendOption.emplace_back(
96 /// BackendOptions{"GpuAcc",
97 /// {
98 /// {"TuningFile", filename}
99 /// }
100 /// });
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000101 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Derek Lamberti836b27b2019-11-20 10:51:57 +0000102
103 /// The following backend options are available:
104 /// GpuAcc:
105 /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning)
106 /// "TuningFile" : string [filenameString]
107 /// "KernelProfilingEnabled" : bool [true | false]
108 std::vector<BackendOptions> m_BackendOptions;
telsoa014fcda012018-03-09 14:13:49 +0000109 };
110
111 static IRuntime* CreateRaw(const CreationOptions& options);
112 static IRuntimePtr Create(const CreationOptions& options);
113 static void Destroy(IRuntime* runtime);
114
telsoa01c577f2c2018-08-31 09:22:23 +0100115 /// Loads a complete network into the IRuntime.
116 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
117 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000118 /// The runtime takes ownership of the network once passed in.
119 /// @return armnn::Status
120 virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) = 0;
121
telsoa01c577f2c2018-08-31 09:22:23 +0100122 /// Load 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.
125 /// @param [out] errorMessage Error message if there were any errors.
126 /// The runtime takes ownership of the network once passed in.
127 /// @return armnn::Status
128 virtual Status LoadNetwork(NetworkId& networkIdOut,
129 IOptimizedNetworkPtr network,
David Monahan4f1e8e42019-09-04 09:22:10 +0100130 std::string& errorMessage) = 0;
131
132 virtual Status LoadNetwork(NetworkId& networkIdOut,
133 IOptimizedNetworkPtr network,
134 std::string& errorMessage,
135 const INetworkProperties& networkProperties) = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100136
telsoa014fcda012018-03-09 14:13:49 +0000137 virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
138 virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
139
telsoa01c577f2c2018-08-31 09:22:23 +0100140 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
telsoa014fcda012018-03-09 14:13:49 +0000141 virtual Status EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100142 const InputTensors& inputTensors,
143 const OutputTensors& outputTensors) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000144
telsoa01c577f2c2018-08-31 09:22:23 +0100145 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000146 /// At the moment this only removes the network from the m_Impl->m_Network.
147 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100148 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000149 /// @return armnn::Status
150 virtual Status UnloadNetwork(NetworkId networkId) = 0;
151
telsoa01c577f2c2018-08-31 09:22:23 +0100152 virtual const IDeviceSpec& GetDeviceSpec() const = 0;
153
154 /// Gets the profiler corresponding to the given network id.
155 /// @param networkId The id of the network for which to get the profile.
156 /// @return A pointer to the requested profiler, or nullptr if not found.
157 virtual const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000158
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000159 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
160 /// @param networkId The id of the network to register the callback.
161 /// @param func callback function to pass to the debug layer.
162 virtual void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) = 0;
163
telsoa014fcda012018-03-09 14:13:49 +0000164protected:
165 ~IRuntime() {}
166};
167
Derek Lamberti836b27b2019-11-20 10:51:57 +0000168
169/// The following API is replaced by the backend options API.
telsoa01c577f2c2018-08-31 09:22:23 +0100170using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000171
telsoa01c577f2c2018-08-31 09:22:23 +0100172/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
173/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
174/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000175///
176/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100177/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
178/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000179/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
180///
telsoa01c577f2c2018-08-31 09:22:23 +0100181/// 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 +0000182/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100183class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000184{
185public:
186 enum class Mode
187 {
188 UseTunedParameters,
189 UpdateTunedParameters
190 };
191
Ruomei Yan49937f32019-04-25 14:24:05 +0100192 enum class TuningLevel
193 {
194 Rapid = 0,
195 Normal = 1,
196 Exhaustive = 2
197 };
198
telsoa014fcda012018-03-09 14:13:49 +0000199 /// Creates an IClTunedParameters with the given mode.
200 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100201 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
202 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000203 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100204 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000205
206 /// Loads an existing set of tuned parameters from the given file.
207 /// If there is an error loading the file, an armnn::Exception is thrown.
208 virtual void Load(const char* filename) = 0;
209
210 /// Saves the current set of tuned parameters to the given file.
211 /// If there is an error saving to the file, an armnn::Exception is thrown.
212 virtual void Save(const char* filename) const = 0;
213
214protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100215 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000216};
217
David Monahan4f1e8e42019-09-04 09:22:10 +0100218} // namespace armnn