telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | 93e4898 | 2018-09-05 13:05:09 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 8 | #include "ArmnnDriver.hpp" |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 9 | #include "ArmnnDriverImpl.hpp" |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 10 | #include "RequestThread.hpp" |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 11 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 12 | #include <NeuralNetworks.h> |
| 13 | #include <armnn/ArmNN.hpp> |
| 14 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace armnn_driver |
| 19 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 20 | using armnnExecuteCallback_1_0 = std::function<void(V1_0::ErrorStatus status, std::string callingFunction)>; |
| 21 | |
| 22 | struct ArmnnCallback_1_0 |
| 23 | { |
| 24 | armnnExecuteCallback_1_0 callback; |
| 25 | }; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 26 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 27 | struct ExecutionContext_1_0 {}; |
| 28 | |
| 29 | using CallbackContext_1_0 = CallbackContext<armnnExecuteCallback_1_0, ExecutionContext_1_0>; |
| 30 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 31 | template <typename HalVersion> |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 32 | class ArmnnPreparedModel : public V1_0::IPreparedModel |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 33 | { |
| 34 | public: |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 35 | using HalModel = typename HalVersion::Model; |
| 36 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 37 | ArmnnPreparedModel(armnn::NetworkId networkId, |
| 38 | armnn::IRuntime* runtime, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 39 | const HalModel& model, |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 40 | const std::string& requestInputsAndOutputsDumpDir, |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 41 | const bool gpuProfilingEnabled, |
| 42 | const bool asyncModelExecutionEnabled = false); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 43 | |
| 44 | virtual ~ArmnnPreparedModel(); |
| 45 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 46 | virtual Return<V1_0::ErrorStatus> execute(const V1_0::Request& request, |
| 47 | const ::android::sp<V1_0::IExecutionCallback>& callback) override; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 48 | |
| 49 | /// execute the graph prepared from the request |
| 50 | void ExecuteGraph(std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 51 | armnn::InputTensors& inputTensors, |
| 52 | armnn::OutputTensors& outputTensors, |
| 53 | CallbackContext_1_0 callback); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 54 | |
| 55 | /// Executes this model with dummy inputs (e.g. all zeroes). |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 56 | /// \return false on failure, otherwise true |
| 57 | bool ExecuteWithDummyInputs(); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 58 | |
| 59 | private: |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 60 | |
| 61 | template<typename CallbackContext> |
| 62 | class ArmnnThreadPoolCallback : public armnn::IAsyncExecutionCallback |
| 63 | { |
| 64 | public: |
| 65 | ArmnnThreadPoolCallback(ArmnnPreparedModel<HalVersion>* model, |
| 66 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 67 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 68 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 69 | CallbackContext callbackContext) : |
| 70 | m_Model(model), |
| 71 | m_MemPools(pMemPools), |
| 72 | m_InputTensors(inputTensors), |
| 73 | m_OutputTensors(outputTensors), |
| 74 | m_CallbackContext(callbackContext) |
| 75 | {} |
| 76 | |
| 77 | void Notify(armnn::Status status, armnn::InferenceTimingPair timeTaken) override; |
| 78 | |
| 79 | // Retrieve the ArmNN Status from the AsyncExecutionCallback that has been notified |
| 80 | virtual armnn::Status GetStatus() const override |
| 81 | { |
| 82 | return armnn::Status::Success; |
| 83 | } |
| 84 | |
| 85 | // Block the calling thread until the AsyncExecutionCallback object allows it to proceed |
| 86 | virtual void Wait() const override |
| 87 | {} |
| 88 | |
| 89 | // Retrieve the start time before executing the inference |
| 90 | virtual armnn::HighResolutionClock GetStartTime() const override |
| 91 | { |
| 92 | return std::chrono::high_resolution_clock::now(); |
| 93 | } |
| 94 | |
| 95 | // Retrieve the time after executing the inference |
| 96 | virtual armnn::HighResolutionClock GetEndTime() const override |
| 97 | { |
| 98 | return std::chrono::high_resolution_clock::now(); |
| 99 | } |
| 100 | |
| 101 | ArmnnPreparedModel<HalVersion>* m_Model; |
| 102 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>> m_MemPools; |
| 103 | std::shared_ptr<armnn::InputTensors> m_InputTensors; |
| 104 | std::shared_ptr<armnn::OutputTensors> m_OutputTensors; |
| 105 | CallbackContext m_CallbackContext; |
| 106 | }; |
| 107 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 108 | template <typename TensorBindingCollection> |
| 109 | void DumpTensorsIfRequired(char const* tensorNamePrefix, const TensorBindingCollection& tensorBindings); |
| 110 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 111 | /// schedule the graph prepared from the request for execution |
| 112 | template<typename CallbackContext> |
| 113 | void ScheduleGraphForExecution( |
| 114 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 115 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 116 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 117 | CallbackContext m_CallbackContext); |
| 118 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 119 | armnn::NetworkId m_NetworkId; |
| 120 | armnn::IRuntime* m_Runtime; |
| 121 | HalModel m_Model; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 122 | // There must be a single RequestThread for all ArmnnPreparedModel objects to ensure serial execution of workloads |
| 123 | // 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] | 124 | static RequestThread<ArmnnPreparedModel, HalVersion, CallbackContext_1_0> m_RequestThread; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 125 | uint32_t m_RequestCount; |
| 126 | const std::string& m_RequestInputsAndOutputsDumpDir; |
| 127 | const bool m_GpuProfilingEnabled; |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 128 | |
| 129 | std::unique_ptr<armnn::IWorkingMemHandle> m_WorkingMemHandle; |
| 130 | const bool m_AsyncModelExecutionEnabled; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } |