Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "ArmnnDriver.hpp" |
| 9 | #include "ArmnnDriverImpl.hpp" |
Narumol Prangnawarat | cad4e91 | 2020-06-02 12:07:43 +0100 | [diff] [blame] | 10 | #include "RequestThread_1_3.hpp" |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 11 | #include "ModelToINetworkConverter.hpp" |
| 12 | |
| 13 | #include <NeuralNetworks.h> |
| 14 | #include <armnn/ArmNN.hpp> |
| 15 | |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | namespace armnn_driver |
| 20 | { |
| 21 | using CallbackAsync_1_3 = std::function< |
| 22 | void(V1_3::ErrorStatus errorStatus, |
| 23 | std::vector<::android::hardware::neuralnetworks::V1_2::OutputShape> outputShapes, |
| 24 | const ::android::hardware::neuralnetworks::V1_2::Timing& timing, |
| 25 | std::string callingFunction)>; |
| 26 | |
| 27 | struct ExecutionContext_1_3 |
| 28 | { |
| 29 | ::android::hardware::neuralnetworks::V1_2::MeasureTiming measureTimings = |
| 30 | ::android::hardware::neuralnetworks::V1_2::MeasureTiming::NO; |
| 31 | TimePoint driverStart; |
Kevin May | 949a69e | 2020-04-24 10:21:40 +0100 | [diff] [blame] | 32 | TimePoint driverEnd; |
Sadik Armagan | d7be72e | 2020-04-23 12:56:05 +0100 | [diff] [blame] | 33 | TimePoint deviceStart; |
| 34 | TimePoint deviceEnd; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | using CallbackContext_1_3 = CallbackContext<CallbackAsync_1_3, ExecutionContext_1_3>; |
| 38 | |
| 39 | using executeFenced_cb = std::function<void(::android::hardware::neuralnetworks::V1_3::ErrorStatus status, |
| 40 | const ::android::hardware::hidl_handle& syncFence, |
| 41 | const ::android::sp<::android::hardware::neuralnetworks::V1_3::IFencedExecutionCallback>& callback)>; |
| 42 | |
| 43 | template <typename HalVersion> |
| 44 | class ArmnnPreparedModel_1_3 : public V1_3::IPreparedModel |
| 45 | { |
| 46 | public: |
| 47 | using HalModel = typename V1_3::Model; |
| 48 | |
| 49 | ArmnnPreparedModel_1_3(armnn::NetworkId networkId, |
| 50 | armnn::IRuntime* runtime, |
| 51 | const HalModel& model, |
| 52 | const std::string& requestInputsAndOutputsDumpDir, |
Narumol Prangnawarat | cad4e91 | 2020-06-02 12:07:43 +0100 | [diff] [blame] | 53 | const bool gpuProfilingEnabled, |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 54 | V1_3::Priority priority = V1_3::Priority::MEDIUM, |
| 55 | const bool asyncModelExecutionEnabled = false); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 56 | |
| 57 | virtual ~ArmnnPreparedModel_1_3(); |
| 58 | |
| 59 | Return<V1_0::ErrorStatus> execute(const V1_0::Request& request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 60 | const ::android::sp<V1_0::IExecutionCallback>& callback) override; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 61 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 62 | Return<V1_0::ErrorStatus> execute_1_2(const V1_0::Request& request, V1_2::MeasureTiming measure, |
| 63 | const ::android::sp<V1_2::IExecutionCallback>& callback) override; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 64 | |
| 65 | Return<V1_3::ErrorStatus> execute_1_3(const V1_3::Request& request, |
| 66 | V1_2::MeasureTiming measure, |
| 67 | const V1_3::OptionalTimePoint&, |
Kevin May | 352d838 | 2020-03-31 15:03:42 +0100 | [diff] [blame] | 68 | const V1_3::OptionalTimeoutDuration&, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 69 | const ::android::sp<V1_3::IExecutionCallback>& callback) override; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 70 | |
| 71 | Return<void> executeSynchronously(const V1_0::Request &request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 72 | V1_2::MeasureTiming measure, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 73 | V1_3::IPreparedModel::executeSynchronously_cb cb) override; |
| 74 | |
| 75 | Return<void> executeSynchronously_1_3(const V1_3::Request &request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 76 | V1_2::MeasureTiming measure, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 77 | const V1_3::OptionalTimePoint& deadline, |
Kevin May | 352d838 | 2020-03-31 15:03:42 +0100 | [diff] [blame] | 78 | const V1_3::OptionalTimeoutDuration& loopTimeoutDuration, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 79 | V1_3::IPreparedModel::executeSynchronously_1_3_cb cb) override; |
| 80 | |
| 81 | Return<void> executeFenced(const V1_3::Request& request, |
Sadik Armagan | d7be72e | 2020-04-23 12:56:05 +0100 | [diff] [blame] | 82 | const android::hardware::hidl_vec<android::hardware::hidl_handle>& fenceWaitFor, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 83 | V1_2::MeasureTiming measure, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 84 | const V1_3::OptionalTimePoint& deadline, |
Kevin May | 352d838 | 2020-03-31 15:03:42 +0100 | [diff] [blame] | 85 | const V1_3::OptionalTimeoutDuration& loopTimeoutDuration, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 86 | const V1_3::OptionalTimeoutDuration& duration, |
| 87 | executeFenced_cb callback) override; |
| 88 | |
| 89 | Return<void> configureExecutionBurst( |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 90 | const ::android::sp<V1_2::IBurstCallback>& callback, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 91 | const android::hardware::MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel, |
| 92 | const android::hardware::MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel, |
| 93 | configureExecutionBurst_cb cb) override; |
| 94 | |
| 95 | template<typename CallbackContext> |
| 96 | Return<void> ExecuteSynchronously(const V1_3::Request& request, CallbackContext cbCtx); |
| 97 | |
| 98 | /// execute the graph prepared from the request |
| 99 | template<typename CallbackContext> |
Sadik Armagan | d7be72e | 2020-04-23 12:56:05 +0100 | [diff] [blame] | 100 | Return <V1_3::ErrorStatus> ExecuteGraph( |
| 101 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 102 | armnn::InputTensors& inputTensors, |
| 103 | armnn::OutputTensors& outputTensors, |
| 104 | CallbackContext callback); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 105 | |
| 106 | /// Executes this model with dummy inputs (e.g. all zeroes). |
| 107 | /// \return false on failure, otherwise true |
| 108 | bool ExecuteWithDummyInputs(); |
| 109 | |
Narumol Prangnawarat | cad4e91 | 2020-06-02 12:07:43 +0100 | [diff] [blame] | 110 | V1_3::Priority GetModelPriority(); |
| 111 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 112 | private: |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 113 | |
| 114 | template<typename CallbackContext> |
| 115 | class ArmnnThreadPoolCallback_1_3 : public armnn::IAsyncExecutionCallback |
| 116 | { |
| 117 | public: |
| 118 | ArmnnThreadPoolCallback_1_3(ArmnnPreparedModel_1_3<HalVersion>* model, |
| 119 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 120 | std::vector<V1_2::OutputShape> outputShapes, |
| 121 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 122 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 123 | CallbackContext callbackContext) : |
| 124 | m_Model(model), |
| 125 | m_MemPools(pMemPools), |
| 126 | m_OutputShapes(outputShapes), |
| 127 | m_InputTensors(inputTensors), |
| 128 | m_OutputTensors(outputTensors), |
| 129 | m_CallbackContext(callbackContext) |
| 130 | {} |
| 131 | |
| 132 | void Notify(armnn::Status status, armnn::InferenceTimingPair timeTaken) override; |
| 133 | |
| 134 | // Retrieve the Arm NN Status from the AsyncExecutionCallback that has been notified |
| 135 | virtual armnn::Status GetStatus() const override |
| 136 | { |
| 137 | return armnn::Status::Success; |
| 138 | } |
| 139 | |
| 140 | // Block the calling thread until the AsyncExecutionCallback object allows it to proceed |
| 141 | virtual void Wait() const override |
| 142 | {} |
| 143 | |
| 144 | // Retrieve the start time before executing the inference |
| 145 | virtual armnn::HighResolutionClock GetStartTime() const override |
| 146 | { |
| 147 | return std::chrono::high_resolution_clock::now(); |
| 148 | } |
| 149 | |
| 150 | // Retrieve the time after executing the inference |
| 151 | virtual armnn::HighResolutionClock GetEndTime() const override |
| 152 | { |
| 153 | return std::chrono::high_resolution_clock::now(); |
| 154 | } |
| 155 | |
| 156 | ArmnnPreparedModel_1_3<HalVersion>* m_Model; |
| 157 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>> m_MemPools; |
| 158 | std::vector<V1_2::OutputShape> m_OutputShapes; |
| 159 | std::shared_ptr<armnn::InputTensors> m_InputTensors; |
| 160 | std::shared_ptr<armnn::OutputTensors> m_OutputTensors; |
| 161 | CallbackContext m_CallbackContext; |
| 162 | }; |
| 163 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 164 | Return <V1_3::ErrorStatus> Execute(const V1_3::Request& request, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 165 | V1_2::MeasureTiming measureTiming, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 166 | CallbackAsync_1_3 callback); |
| 167 | |
| 168 | Return<V1_3::ErrorStatus> PrepareMemoryForInputs( |
| 169 | armnn::InputTensors& inputs, |
| 170 | const V1_3::Request& request, |
| 171 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 172 | |
| 173 | Return<V1_3::ErrorStatus> PrepareMemoryForOutputs( |
| 174 | armnn::OutputTensors& outputs, |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 175 | std::vector<V1_2::OutputShape> &outputShapes, |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 176 | const V1_3::Request& request, |
| 177 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 178 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 179 | std::tuple<V1_3::ErrorStatus, hidl_vec<V1_2::OutputShape>, V1_2::Timing, std::string> PrepareMemoryForIO( |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 180 | armnn::InputTensors& inputs, |
| 181 | armnn::OutputTensors& outputs, |
| 182 | std::vector<android::nn::RunTimePoolInfo>& memPools, |
| 183 | const V1_3::Request& request); |
| 184 | |
| 185 | template <typename TensorBindingCollection> |
| 186 | void DumpTensorsIfRequired(char const* tensorNamePrefix, const TensorBindingCollection& tensorBindings); |
| 187 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 188 | /// schedule the graph prepared from the request for execution |
| 189 | template<typename CallbackContext> |
| 190 | void ScheduleGraphForExecution( |
| 191 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 192 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 193 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 194 | CallbackContext m_CallbackContext, |
| 195 | armnn::QosExecPriority priority); |
| 196 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 197 | armnn::NetworkId m_NetworkId; |
| 198 | armnn::IRuntime* m_Runtime; |
| 199 | V1_3::Model m_Model; |
| 200 | // There must be a single RequestThread for all ArmnnPreparedModel objects to ensure serial execution of workloads |
| 201 | // It is specific to this class, so it is declared as static here |
Narumol Prangnawarat | cad4e91 | 2020-06-02 12:07:43 +0100 | [diff] [blame] | 202 | static RequestThread_1_3<ArmnnPreparedModel_1_3, HalVersion, CallbackContext_1_3> m_RequestThread; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 203 | uint32_t m_RequestCount; |
| 204 | const std::string& m_RequestInputsAndOutputsDumpDir; |
| 205 | const bool m_GpuProfilingEnabled; |
Narumol Prangnawarat | cad4e91 | 2020-06-02 12:07:43 +0100 | [diff] [blame] | 206 | V1_3::Priority m_ModelPriority; |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 207 | |
| 208 | std::unique_ptr<IWorkingMemHandle> m_WorkingMemHandle; |
| 209 | const bool m_AsyncModelExecutionEnabled; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | } |