Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "ArmnnDriver.hpp" |
| 9 | #include "ArmnnDriverImpl.hpp" |
| 10 | #include "RequestThread.hpp" |
| 11 | #include "ModelToINetworkConverter.hpp" |
| 12 | |
| 13 | #include <NeuralNetworks.h> |
| 14 | #include <armnn/ArmNN.hpp> |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 15 | #include <armnn/Threadpool.hpp> |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 16 | |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | namespace armnn_driver |
| 21 | { |
| 22 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 23 | using CallbackAsync_1_2 = std::function< |
| 24 | void(V1_0::ErrorStatus errorStatus, |
| 25 | std::vector<::android::hardware::neuralnetworks::V1_2::OutputShape> outputShapes, |
| 26 | const ::android::hardware::neuralnetworks::V1_2::Timing& timing, |
| 27 | std::string callingFunction)>; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 28 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 29 | struct ExecutionContext_1_2 |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 30 | { |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 31 | ::android::hardware::neuralnetworks::V1_2::MeasureTiming measureTimings = |
| 32 | ::android::hardware::neuralnetworks::V1_2::MeasureTiming::NO; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 33 | TimePoint driverStart; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 36 | using CallbackContext_1_2 = CallbackContext<CallbackAsync_1_2, ExecutionContext_1_2>; |
| 37 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 38 | template <typename HalVersion> |
| 39 | class ArmnnPreparedModel_1_2 : public V1_2::IPreparedModel |
| 40 | { |
| 41 | public: |
| 42 | using HalModel = typename V1_2::Model; |
| 43 | |
| 44 | ArmnnPreparedModel_1_2(armnn::NetworkId networkId, |
| 45 | armnn::IRuntime* runtime, |
| 46 | const HalModel& model, |
| 47 | const std::string& requestInputsAndOutputsDumpDir, |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 48 | const bool gpuProfilingEnabled, |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 49 | const bool asyncModelExecutionEnabled = false, |
Narumol Prangnawarat | 558a1d4 | 2022-02-07 13:12:24 +0000 | [diff] [blame^] | 50 | const unsigned int numberOfThreads = 1, |
| 51 | const bool importEnabled = false, |
| 52 | const bool exportEnabled = true); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 53 | |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 54 | ArmnnPreparedModel_1_2(armnn::NetworkId networkId, |
| 55 | armnn::IRuntime* runtime, |
| 56 | const std::string& requestInputsAndOutputsDumpDir, |
| 57 | const bool gpuProfilingEnabled, |
| 58 | const bool asyncModelExecutionEnabled = false, |
| 59 | const unsigned int numberOfThreads = 1, |
Narumol Prangnawarat | 558a1d4 | 2022-02-07 13:12:24 +0000 | [diff] [blame^] | 60 | const bool importEnabled = false, |
| 61 | const bool exportEnabled = true, |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 62 | const bool preparedFromCache = false); |
| 63 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 64 | virtual ~ArmnnPreparedModel_1_2(); |
| 65 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 66 | virtual Return<V1_0::ErrorStatus> execute(const V1_0::Request& request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 67 | const ::android::sp<V1_0::IExecutionCallback>& callback) override; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 68 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 69 | virtual Return<V1_0::ErrorStatus> execute_1_2(const V1_0::Request& request, V1_2::MeasureTiming measure, |
| 70 | const ::android::sp<V1_2::IExecutionCallback>& callback) override; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 71 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 72 | virtual Return<void> executeSynchronously(const V1_0::Request &request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 73 | V1_2::MeasureTiming measure, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 74 | V1_2::IPreparedModel::executeSynchronously_cb cb) override; |
| 75 | |
| 76 | virtual Return<void> configureExecutionBurst( |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 77 | const ::android::sp<V1_2::IBurstCallback>& callback, |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 78 | const android::hardware::MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel, |
| 79 | const android::hardware::MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, |
| 80 | configureExecutionBurst_cb cb) override; |
| 81 | |
| 82 | /// execute the graph prepared from the request |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 83 | template<typename CallbackContext> |
| 84 | bool ExecuteGraph(std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 85 | armnn::InputTensors& inputTensors, |
| 86 | armnn::OutputTensors& outputTensors, |
| 87 | CallbackContext callback); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 88 | |
| 89 | /// Executes this model with dummy inputs (e.g. all zeroes). |
| 90 | /// \return false on failure, otherwise true |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 91 | bool ExecuteWithDummyInputs(unsigned int numInputs, unsigned int numOutputs); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 92 | |
| 93 | private: |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 94 | |
| 95 | template<typename CallbackContext> |
| 96 | class ArmnnThreadPoolCallback_1_2 : public armnn::IAsyncExecutionCallback |
| 97 | { |
| 98 | public: |
| 99 | ArmnnThreadPoolCallback_1_2(ArmnnPreparedModel_1_2<HalVersion>* model, |
| 100 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 101 | std::vector<V1_2::OutputShape> outputShapes, |
| 102 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 103 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 104 | CallbackContext callbackContext) : |
| 105 | m_Model(model), |
| 106 | m_MemPools(pMemPools), |
| 107 | m_OutputShapes(outputShapes), |
| 108 | m_InputTensors(inputTensors), |
| 109 | m_OutputTensors(outputTensors), |
| 110 | m_CallbackContext(callbackContext) |
| 111 | {} |
| 112 | |
| 113 | void Notify(armnn::Status status, armnn::InferenceTimingPair timeTaken) override; |
| 114 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 115 | ArmnnPreparedModel_1_2<HalVersion>* m_Model; |
| 116 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>> m_MemPools; |
| 117 | std::vector<V1_2::OutputShape> m_OutputShapes; |
| 118 | std::shared_ptr<armnn::InputTensors> m_InputTensors; |
| 119 | std::shared_ptr<armnn::OutputTensors> m_OutputTensors; |
| 120 | CallbackContext m_CallbackContext; |
| 121 | }; |
| 122 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 123 | Return<V1_0::ErrorStatus> Execute(const V1_0::Request& request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 124 | V1_2::MeasureTiming measureTiming, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 125 | CallbackAsync_1_2 callback); |
| 126 | |
| 127 | Return<V1_0::ErrorStatus> PrepareMemoryForInputs( |
| 128 | armnn::InputTensors& inputs, |
| 129 | const V1_0::Request& request, |
| 130 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 131 | |
| 132 | Return<V1_0::ErrorStatus> PrepareMemoryForOutputs( |
| 133 | armnn::OutputTensors& outputs, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 134 | std::vector<V1_2::OutputShape> &outputShapes, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 135 | const V1_0::Request& request, |
| 136 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 137 | |
| 138 | Return <V1_0::ErrorStatus> PrepareMemoryForIO( |
| 139 | armnn::InputTensors& inputs, |
| 140 | armnn::OutputTensors& outputs, |
| 141 | std::vector<android::nn::RunTimePoolInfo>& memPools, |
| 142 | const V1_0::Request& request, |
| 143 | CallbackAsync_1_2 callback); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 144 | |
| 145 | template <typename TensorBindingCollection> |
| 146 | void DumpTensorsIfRequired(char const* tensorNamePrefix, const TensorBindingCollection& tensorBindings); |
| 147 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 148 | /// schedule the graph prepared from the request for execution |
| 149 | template<typename CallbackContext> |
| 150 | void ScheduleGraphForExecution( |
| 151 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 152 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 153 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 154 | CallbackContext m_CallbackContext); |
| 155 | |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 156 | armnn::NetworkId m_NetworkId; |
| 157 | armnn::IRuntime* m_Runtime; |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 158 | V1_2::Model m_Model; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 159 | // There must be a single RequestThread for all ArmnnPreparedModel objects to ensure serial execution of workloads |
| 160 | // It is specific to this class, so it is declared as static here |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 161 | static RequestThread<ArmnnPreparedModel_1_2, |
| 162 | HalVersion, |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 163 | CallbackContext_1_2> m_RequestThread; |
| 164 | uint32_t m_RequestCount; |
| 165 | const std::string& m_RequestInputsAndOutputsDumpDir; |
| 166 | const bool m_GpuProfilingEnabled; |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 167 | // Static to allow sharing of threadpool between ArmnnPreparedModel instances |
| 168 | static std::unique_ptr<armnn::Threadpool> m_Threadpool; |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 169 | std::shared_ptr<IWorkingMemHandle> m_WorkingMemHandle; |
| 170 | const bool m_AsyncModelExecutionEnabled; |
Narumol Prangnawarat | 558a1d4 | 2022-02-07 13:12:24 +0000 | [diff] [blame^] | 171 | const bool m_EnableImport; |
| 172 | const bool m_EnableExport; |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 173 | const bool m_PreparedFromCache; |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | } |