telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 7 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 8 | #include "INetwork.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | #include "IProfiler.hpp" |
Matthew Bentham | 313e1c8 | 2019-03-25 17:37:47 +0000 | [diff] [blame] | 10 | #include "Tensor.hpp" |
| 11 | #include "Types.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | #include "TypesUtils.hpp" |
| 13 | |
Matthew Bentham | 313e1c8 | 2019-03-25 17:37:47 +0000 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | namespace armnn |
| 17 | { |
| 18 | |
| 19 | using NetworkId = int; |
| 20 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 21 | class IGpuAccTunedParameters; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 22 | |
| 23 | class IRuntime; |
| 24 | using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>; |
| 25 | |
| 26 | class IRuntime |
| 27 | { |
| 28 | public: |
| 29 | struct CreationOptions |
| 30 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 31 | CreationOptions() |
| 32 | : m_GpuAccTunedParameters(nullptr) |
| 33 | , m_EnableGpuProfiling(false) |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame^] | 34 | , m_DynamicBackendsPath("") |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 36 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads. |
| 38 | /// It will also be updated with new tuned parameters if it is configured to do so. |
| 39 | std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters; |
| 40 | |
| 41 | // Setting this flag will allow the user to obtain GPU profiling information from the runtime. |
| 42 | bool m_EnableGpuProfiling; |
Matteo Martincigh | e7d4498 | 2019-08-05 12:16:47 +0100 | [diff] [blame^] | 43 | |
| 44 | // Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive |
| 45 | // Only a single path is allowed for the override |
| 46 | std::string m_DynamicBackendsPath; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static IRuntime* CreateRaw(const CreationOptions& options); |
| 50 | static IRuntimePtr Create(const CreationOptions& options); |
| 51 | static void Destroy(IRuntime* runtime); |
| 52 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 53 | /// Loads a complete network into the IRuntime. |
| 54 | /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference. |
| 55 | /// @param [in] network - Complete network to load into the IRuntime. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | /// The runtime takes ownership of the network once passed in. |
| 57 | /// @return armnn::Status |
| 58 | virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) = 0; |
| 59 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | /// Load a complete network into the IRuntime. |
| 61 | /// @param [out] networkIdOut Unique identifier for the network is returned in this reference. |
| 62 | /// @param [in] network Complete network to load into the IRuntime. |
| 63 | /// @param [out] errorMessage Error message if there were any errors. |
| 64 | /// The runtime takes ownership of the network once passed in. |
| 65 | /// @return armnn::Status |
| 66 | virtual Status LoadNetwork(NetworkId& networkIdOut, |
| 67 | IOptimizedNetworkPtr network, |
| 68 | std::string & errorMessage) = 0; |
| 69 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 70 | virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0; |
| 71 | virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0; |
| 72 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | /// Evaluates a network using input in inputTensors and outputs filled into outputTensors |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 74 | virtual Status EnqueueWorkload(NetworkId networkId, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 75 | const InputTensors& inputTensors, |
| 76 | const OutputTensors& outputTensors) = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 78 | /// Unloads a network from the IRuntime. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 79 | /// At the moment this only removes the network from the m_Impl->m_Network. |
| 80 | /// This might need more work in the future to be AndroidNN compliant. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork(). |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 82 | /// @return armnn::Status |
| 83 | virtual Status UnloadNetwork(NetworkId networkId) = 0; |
| 84 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 85 | virtual const IDeviceSpec& GetDeviceSpec() const = 0; |
| 86 | |
| 87 | /// Gets the profiler corresponding to the given network id. |
| 88 | /// @param networkId The id of the network for which to get the profile. |
| 89 | /// @return A pointer to the requested profiler, or nullptr if not found. |
| 90 | virtual const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 91 | |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 92 | /// Registers a callback function to debug layers performing custom computations on intermediate tensors. |
| 93 | /// @param networkId The id of the network to register the callback. |
| 94 | /// @param func callback function to pass to the debug layer. |
| 95 | virtual void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) = 0; |
| 96 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | protected: |
| 98 | ~IRuntime() {} |
| 99 | }; |
| 100 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 101 | using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 102 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 103 | /// Manages a set of GpuAcc parameters which have been tuned for maximum performance. |
| 104 | /// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it |
| 105 | /// for all GPU workload execution. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 106 | /// |
| 107 | /// Can be created in two modes: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | /// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads. |
| 109 | /// - In UpdateTunedParameters mode, additionally, whenever a GPU workload is executed for the first time, the |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 110 | /// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow. |
| 111 | /// |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 112 | /// The parameters can be loaded from and saved to a file so that you can first run a slow initial read-write |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 113 | /// execution, save the parameters for later and then run fast read-only executions using the optimised parameters. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 114 | class IGpuAccTunedParameters |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | { |
| 116 | public: |
| 117 | enum class Mode |
| 118 | { |
| 119 | UseTunedParameters, |
| 120 | UpdateTunedParameters |
| 121 | }; |
| 122 | |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 123 | enum class TuningLevel |
| 124 | { |
| 125 | Rapid = 0, |
| 126 | Normal = 1, |
| 127 | Exhaustive = 2 |
| 128 | }; |
| 129 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 130 | /// Creates an IClTunedParameters with the given mode. |
| 131 | /// @{ |
Ruomei Yan | 49937f3 | 2019-04-25 14:24:05 +0100 | [diff] [blame] | 132 | static IGpuAccTunedParameters* CreateRaw(Mode mode, TuningLevel tunerMode); |
| 133 | static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 134 | /// @} |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | static void Destroy(IGpuAccTunedParameters* params); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 136 | |
| 137 | /// Loads an existing set of tuned parameters from the given file. |
| 138 | /// If there is an error loading the file, an armnn::Exception is thrown. |
| 139 | virtual void Load(const char* filename) = 0; |
| 140 | |
| 141 | /// Saves the current set of tuned parameters to the given file. |
| 142 | /// If there is an error saving to the file, an armnn::Exception is thrown. |
| 143 | virtual void Save(const char* filename) const = 0; |
| 144 | |
| 145 | protected: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 146 | virtual ~IGpuAccTunedParameters() {}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } |