• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "BackendOptions.hpp"
8 #include "INetwork.hpp"
9 #include "IProfiler.hpp"
10 #include "Tensor.hpp"
11 #include "Types.hpp"
12 #include "TypesUtils.hpp"
13 #include "profiling/ILocalPacketHandler.hpp"
14 
15 #include <memory>
16 
17 namespace armnn
18 {
19 
20 using NetworkId = int;
21 
22 class IGpuAccTunedParameters;
23 
24 class IRuntime;
25 using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>;
26 
27 struct INetworkProperties
28 {
INetworkPropertiesarmnn::INetworkProperties29     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 
~INetworkPropertiesarmnn::INetworkProperties36     virtual ~INetworkProperties() {}
37 };
38 
39 class IRuntime
40 {
41 public:
42     struct CreationOptions
43     {
CreationOptionsarmnn::IRuntime::CreationOptions44         CreationOptions()
45             : m_GpuAccTunedParameters(nullptr)
46             , m_EnableGpuProfiling(false)
47             , m_DynamicBackendsPath("")
48         {}
49 
50         /// 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 
54         /// Setting this flag will allow the user to obtain GPU profiling information from the runtime.
55         bool m_EnableGpuProfiling;
56 
57         /// 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
59         std::string m_DynamicBackendsPath;
60 
61         struct ExternalProfilingOptions
62         {
ExternalProfilingOptionsarmnn::IRuntime::CreationOptions::ExternalProfilingOptions63             ExternalProfilingOptions()
64                 : m_EnableProfiling(false)
65                 , m_TimelineEnabled(false)
66                 , m_OutgoingCaptureFile("")
67                 , m_IncomingCaptureFile("")
68                 , m_FileOnly(false)
69                 , m_CapturePeriod(LOWEST_CAPTURE_PERIOD)
70                 , m_FileFormat("binary")
71                 , m_LocalPacketHandlers()
72             {}
73 
74             bool        m_EnableProfiling;
75             bool        m_TimelineEnabled;
76             std::string m_OutgoingCaptureFile;
77             std::string m_IncomingCaptureFile;
78             bool        m_FileOnly;
79             uint32_t    m_CapturePeriod;
80             std::string m_FileFormat;
81             std::vector<armnn::profiling::ILocalPacketHandlerSharedPtr> m_LocalPacketHandlers;
82         };
83         ExternalProfilingOptions m_ProfilingOptions;
84 
85         /// Pass backend specific options.
86         ///
87         /// For example, to enable GpuAcc tuning add the following
88         /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
89         /// m_BackendOption.emplace_back(
90         ///     BackendOptions{"GpuAcc",
91         ///       {
92         ///         {"TuningLevel", 2},
93         ///         {"TuningFile", filename}
94         ///       }
95         ///     });
96         /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97         /// 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.
101         /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.cpp
102         /// m_BackendOption.emplace_back(
103         ///     BackendOptions{"GpuAcc",
104         ///       {
105         ///         {"TuningFile", filename}
106         ///       }
107         ///     });
108         /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109 
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;
116     };
117 
118     static IRuntime* CreateRaw(const CreationOptions& options);
119     static IRuntimePtr Create(const CreationOptions& options);
120     static void Destroy(IRuntime* runtime);
121 
122     /// 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.
125     /// The runtime takes ownership of the network once passed in.
126     /// @return armnn::Status
127     virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) = 0;
128 
129     /// 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,
137                                std::string& errorMessage) = 0;
138 
139     virtual Status LoadNetwork(NetworkId& networkIdOut,
140                                IOptimizedNetworkPtr network,
141                                std::string& errorMessage,
142                                const INetworkProperties& networkProperties) = 0;
143 
144     virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
145     virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
146 
147     /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
148     virtual Status EnqueueWorkload(NetworkId networkId,
149                                    const InputTensors& inputTensors,
150                                    const OutputTensors& outputTensors) = 0;
151 
152     /// Unloads a network from the IRuntime.
153     /// 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.
155     /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
156     /// @return armnn::Status
157     virtual Status UnloadNetwork(NetworkId networkId) = 0;
158 
159     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;
165 
166     /// 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 
171 protected:
~IRuntime()172     ~IRuntime() {}
173 };
174 
175 
176 /// The following API is replaced by the backend options API.
177 using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
178 
179 /// 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.
182 ///
183 /// Can be created in two modes:
184 ///     - 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
186 ///       optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
187 ///
188 /// The parameters can be loaded from and saved to a file so that you can first run a slow initial read-write
189 /// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
190 class IGpuAccTunedParameters
191 {
192 public:
193     enum class Mode
194     {
195         UseTunedParameters,
196         UpdateTunedParameters
197     };
198 
199     enum class TuningLevel
200     {
201         Rapid = 0,
202         Normal = 1,
203         Exhaustive = 2
204     };
205 
206     /// Creates an IClTunedParameters with the given mode.
207     /// @{
208     static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode);
209     static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode);
210     /// @}
211     static void Destroy(IGpuAccTunedParameters* params);
212 
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 
221 protected:
~IGpuAccTunedParameters()222     virtual ~IGpuAccTunedParameters() {};
223 };
224 
225 } // namespace armnn
226