telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // See LICENSE file in the project root for full license information. |
| 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "Types.hpp" |
| 10 | #include "Tensor.hpp" |
| 11 | #include "INetwork.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 12 | #include "IProfiler.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | #include "TypesUtils.hpp" |
| 14 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | |
| 18 | using NetworkId = int; |
| 19 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 20 | class IGpuAccTunedParameters; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 21 | |
| 22 | class IRuntime; |
| 23 | using IRuntimePtr = std::unique_ptr<IRuntime, void(*)(IRuntime* runtime)>; |
| 24 | |
| 25 | class IRuntime |
| 26 | { |
| 27 | public: |
| 28 | struct CreationOptions |
| 29 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 30 | CreationOptions() |
| 31 | : m_GpuAccTunedParameters(nullptr) |
| 32 | , m_EnableGpuProfiling(false) |
| 33 | {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 34 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 35 | /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads. |
| 36 | /// It will also be updated with new tuned parameters if it is configured to do so. |
| 37 | std::shared_ptr<IGpuAccTunedParameters> m_GpuAccTunedParameters; |
| 38 | |
| 39 | // Setting this flag will allow the user to obtain GPU profiling information from the runtime. |
| 40 | bool m_EnableGpuProfiling; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static IRuntime* CreateRaw(const CreationOptions& options); |
| 44 | static IRuntimePtr Create(const CreationOptions& options); |
| 45 | static void Destroy(IRuntime* runtime); |
| 46 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 47 | /// Loads a complete network into the IRuntime. |
| 48 | /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference. |
| 49 | /// @param [in] network - Complete network to load into the IRuntime. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 50 | /// The runtime takes ownership of the network once passed in. |
| 51 | /// @return armnn::Status |
| 52 | virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) = 0; |
| 53 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 54 | /// Load a complete network into the IRuntime. |
| 55 | /// @param [out] networkIdOut Unique identifier for the network is returned in this reference. |
| 56 | /// @param [in] network Complete network to load into the IRuntime. |
| 57 | /// @param [out] errorMessage Error message if there were any errors. |
| 58 | /// The runtime takes ownership of the network once passed in. |
| 59 | /// @return armnn::Status |
| 60 | virtual Status LoadNetwork(NetworkId& networkIdOut, |
| 61 | IOptimizedNetworkPtr network, |
| 62 | std::string & errorMessage) = 0; |
| 63 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 64 | virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0; |
| 65 | virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0; |
| 66 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 67 | /// Evaluates a network using input in inputTensors and outputs filled into outputTensors |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 68 | virtual Status EnqueueWorkload(NetworkId networkId, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 69 | const InputTensors& inputTensors, |
| 70 | const OutputTensors& outputTensors) = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 71 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 72 | /// Unloads a network from the IRuntime. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | /// At the moment this only removes the network from the m_Impl->m_Network. |
| 74 | /// This might need more work in the future to be AndroidNN compliant. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 75 | /// @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] | 76 | /// @return armnn::Status |
| 77 | virtual Status UnloadNetwork(NetworkId networkId) = 0; |
| 78 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 79 | virtual const IDeviceSpec& GetDeviceSpec() const = 0; |
| 80 | |
| 81 | /// Gets the profiler corresponding to the given network id. |
| 82 | /// @param networkId The id of the network for which to get the profile. |
| 83 | /// @return A pointer to the requested profiler, or nullptr if not found. |
| 84 | virtual const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 85 | |
| 86 | protected: |
| 87 | ~IRuntime() {} |
| 88 | }; |
| 89 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 90 | using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 91 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 92 | /// Manages a set of GpuAcc parameters which have been tuned for maximum performance. |
| 93 | /// Passes an instance of this object to the IRuntime::Create() method (via IRuntime::CreationOptions) to use it |
| 94 | /// for all GPU workload execution. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 95 | /// |
| 96 | /// Can be created in two modes: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 97 | /// - In UseTunedParameters mode, the parameters stored in this object are used to execute GPU workloads. |
| 98 | /// - 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] | 99 | /// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow. |
| 100 | /// |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 101 | /// 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] | 102 | /// 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^] | 103 | class IGpuAccTunedParameters |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 104 | { |
| 105 | public: |
| 106 | enum class Mode |
| 107 | { |
| 108 | UseTunedParameters, |
| 109 | UpdateTunedParameters |
| 110 | }; |
| 111 | |
| 112 | /// Creates an IClTunedParameters with the given mode. |
| 113 | /// @{ |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 114 | static IGpuAccTunedParameters* CreateRaw(Mode mode); |
| 115 | static IGpuAccTunedParametersPtr Create(Mode mode); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 116 | /// @} |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 117 | static void Destroy(IGpuAccTunedParameters* params); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 118 | |
| 119 | /// Loads an existing set of tuned parameters from the given file. |
| 120 | /// If there is an error loading the file, an armnn::Exception is thrown. |
| 121 | virtual void Load(const char* filename) = 0; |
| 122 | |
| 123 | /// Saves the current set of tuned parameters to the given file. |
| 124 | /// If there is an error saving to the file, an armnn::Exception is thrown. |
| 125 | virtual void Save(const char* filename) const = 0; |
| 126 | |
| 127 | protected: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame^] | 128 | virtual ~IGpuAccTunedParameters() {}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } |