blob: 68965cffa5c7932e242264d2243988310ef45b45 [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
telsoa014fcda012018-03-09 14:13:49 +00007
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
53 // Setting this flag will allow the user to obtain GPU profiling information from the runtime.
54 bool m_EnableGpuProfiling;
Matteo Martincighe7d44982019-08-05 12:16:47 +010055
56 // 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
58 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)
67 , m_CapturePeriod(150u)
68 {}
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 };
telsoa014fcda012018-03-09 14:13:49 +000076 };
77
78 static IRuntime* CreateRaw(const CreationOptions& options);
79 static IRuntimePtr Create(const CreationOptions& options);
80 static void Destroy(IRuntime* runtime);
81
telsoa01c577f2c2018-08-31 09:22:23 +010082 /// Loads a complete network into the IRuntime.
83 /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
84 /// @param [in] network - Complete network to load into the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +000085 /// The runtime takes ownership of the network once passed in.
86 /// @return armnn::Status
87 virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) = 0;
88
telsoa01c577f2c2018-08-31 09:22:23 +010089 /// Load a complete network into the IRuntime.
90 /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
91 /// @param [in] network Complete network to load into the IRuntime.
92 /// @param [out] errorMessage Error message if there were any errors.
93 /// The runtime takes ownership of the network once passed in.
94 /// @return armnn::Status
95 virtual Status LoadNetwork(NetworkId& networkIdOut,
96 IOptimizedNetworkPtr network,
David Monahan4f1e8e42019-09-04 09:22:10 +010097 std::string& errorMessage) = 0;
98
99 virtual Status LoadNetwork(NetworkId& networkIdOut,
100 IOptimizedNetworkPtr network,
101 std::string& errorMessage,
102 const INetworkProperties& networkProperties) = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100103
telsoa014fcda012018-03-09 14:13:49 +0000104 virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
105 virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
106
telsoa01c577f2c2018-08-31 09:22:23 +0100107 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
telsoa014fcda012018-03-09 14:13:49 +0000108 virtual Status EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +0100109 const InputTensors& inputTensors,
110 const OutputTensors& outputTensors) = 0;
telsoa014fcda012018-03-09 14:13:49 +0000111
telsoa01c577f2c2018-08-31 09:22:23 +0100112 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +0000113 /// At the moment this only removes the network from the m_Impl->m_Network.
114 /// This might need more work in the future to be AndroidNN compliant.
telsoa01c577f2c2018-08-31 09:22:23 +0100115 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +0000116 /// @return armnn::Status
117 virtual Status UnloadNetwork(NetworkId networkId) = 0;
118
telsoa01c577f2c2018-08-31 09:22:23 +0100119 virtual const IDeviceSpec& GetDeviceSpec() const = 0;
120
121 /// Gets the profiler corresponding to the given network id.
122 /// @param networkId The id of the network for which to get the profile.
123 /// @return A pointer to the requested profiler, or nullptr if not found.
124 virtual const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const = 0;
telsoa014fcda012018-03-09 14:13:49 +0000125
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +0000126 /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
127 /// @param networkId The id of the network to register the callback.
128 /// @param func callback function to pass to the debug layer.
129 virtual void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) = 0;
130
telsoa014fcda012018-03-09 14:13:49 +0000131protected:
132 ~IRuntime() {}
133};
134
telsoa01c577f2c2018-08-31 09:22:23 +0100135using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +0000136
telsoa01c577f2c2018-08-31 09:22:23 +0100137/// Manages a set of GpuAcc parameters which have been tuned for maximum performance.
138/// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it
139/// for all GPU workload execution.
telsoa014fcda012018-03-09 14:13:49 +0000140///
141/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100142/// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads.
143/// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the
telsoa014fcda012018-03-09 14:13:49 +0000144/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
145///
telsoa01c577f2c2018-08-31 09:22:23 +0100146/// 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 +0000147/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100148class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000149{
150public:
151 enum class Mode
152 {
153 UseTunedParameters,
154 UpdateTunedParameters
155 };
156
Ruomei Yan49937f32019-04-25 14:24:05 +0100157 enum class TuningLevel
158 {
159 Rapid = 0,
160 Normal = 1,
161 Exhaustive = 2
162 };
163
telsoa014fcda012018-03-09 14:13:49 +0000164 /// Creates an IClTunedParameters with the given mode.
165 /// @{
Ruomei Yan49937f32019-04-25 14:24:05 +0100166 static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
167 static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
telsoa014fcda012018-03-09 14:13:49 +0000168 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100169 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000170
171 /// Loads an existing set of tuned parameters from the given file.
172 /// If there is an error loading the file, an armnn::Exception is thrown.
173 virtual void Load(const char* filename) = 0;
174
175 /// Saves the current set of tuned parameters to the given file.
176 /// If there is an error saving to the file, an armnn::Exception is thrown.
177 virtual void Save(const char* filename) const = 0;
178
179protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100180 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000181};
182
David Monahan4f1e8e42019-09-04 09:22:10 +0100183} // namespace armnn