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