blob: 34487d5b76b4d847ce790d0f7408481a265040f7 [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
26class IRuntime
27{
28public:
29 struct CreationOptions
30 {
telsoa01c577f2c2018-08-31 09:22:23 +010031 CreationOptions()
32 : m_GpuAccTunedParameters(nullptr)
33 , m_EnableGpuProfiling(false)
34 {}
telsoa014fcda012018-03-09 14:13:49 +000035
telsoa01c577f2c2018-08-31 09:22:23 +010036 /// 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;
telsoa014fcda012018-03-09 14:13:49 +000042 };
43
44 static IRuntime* CreateRaw(const CreationOptions& options);
45 static IRuntimePtr Create(const CreationOptions& options);
46 static void Destroy(IRuntime* runtime);
47
telsoa01c577f2c2018-08-31 09:22:23 +010048 /// 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.
telsoa014fcda012018-03-09 14:13:49 +000051 /// The runtime takes ownership of the network once passed in.
52 /// @return armnn::Status
53 virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) = 0;
54
telsoa01c577f2c2018-08-31 09:22:23 +010055 /// 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
telsoa014fcda012018-03-09 14:13:49 +000065 virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
66 virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const = 0;
67
telsoa01c577f2c2018-08-31 09:22:23 +010068 /// Evaluates a network using input in inputTensors and outputs filled into outputTensors
telsoa014fcda012018-03-09 14:13:49 +000069 virtual Status EnqueueWorkload(NetworkId networkId,
telsoa01c577f2c2018-08-31 09:22:23 +010070 const InputTensors& inputTensors,
71 const OutputTensors& outputTensors) = 0;
telsoa014fcda012018-03-09 14:13:49 +000072
telsoa01c577f2c2018-08-31 09:22:23 +010073 /// Unloads a network from the IRuntime.
telsoa014fcda012018-03-09 14:13:49 +000074 /// 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.
telsoa01c577f2c2018-08-31 09:22:23 +010076 /// @param [in] networkId - Unique identifier for the network to be unloaded. Generated in LoadNetwork().
telsoa014fcda012018-03-09 14:13:49 +000077 /// @return armnn::Status
78 virtual Status UnloadNetwork(NetworkId networkId) = 0;
79
telsoa01c577f2c2018-08-31 09:22:23 +010080 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;
telsoa014fcda012018-03-09 14:13:49 +000086
Nattapat Chaimanowong6e948202019-03-22 14:01:46 +000087 /// 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
telsoa014fcda012018-03-09 14:13:49 +000092protected:
93 ~IRuntime() {}
94};
95
telsoa01c577f2c2018-08-31 09:22:23 +010096using IGpuAccTunedParametersPtr = std::shared_ptr<IGpuAccTunedParameters>;
telsoa014fcda012018-03-09 14:13:49 +000097
telsoa01c577f2c2018-08-31 09:22:23 +010098/// 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.
telsoa014fcda012018-03-09 14:13:49 +0000101///
102/// Can be created in two modes:
telsoa01c577f2c2018-08-31 09:22:23 +0100103/// - 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
telsoa014fcda012018-03-09 14:13:49 +0000105/// optimum parameters will be found and stored in this object. WARNING - This tuning can be slow.
106///
telsoa01c577f2c2018-08-31 09:22:23 +0100107/// 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 +0000108/// execution, save the parameters for later and then run fast read-only executions using the optimised parameters.
telsoa01c577f2c2018-08-31 09:22:23 +0100109class IGpuAccTunedParameters
telsoa014fcda012018-03-09 14:13:49 +0000110{
111public:
112 enum class Mode
113 {
114 UseTunedParameters,
115 UpdateTunedParameters
116 };
117
118 /// Creates an IClTunedParameters with the given mode.
119 /// @{
telsoa01c577f2c2018-08-31 09:22:23 +0100120 static IGpuAccTunedParameters* CreateRaw(Mode mode);
121 static IGpuAccTunedParametersPtr Create(Mode mode);
telsoa014fcda012018-03-09 14:13:49 +0000122 /// @}
telsoa01c577f2c2018-08-31 09:22:23 +0100123 static void Destroy(IGpuAccTunedParameters* params);
telsoa014fcda012018-03-09 14:13:49 +0000124
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
133protected:
telsoa01c577f2c2018-08-31 09:22:23 +0100134 virtual ~IGpuAccTunedParameters() {};
telsoa014fcda012018-03-09 14:13:49 +0000135};
136
137}