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> |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 14 | #include <armnn/Threadpool.hpp> |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 15 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | namespace armnn_driver |
| 20 | { |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 21 | using armnnExecuteCallback_1_0 = std::function<void(V1_0::ErrorStatus status, std::string callingFunction)>; |
| 22 | |
| 23 | struct ArmnnCallback_1_0 |
| 24 | { |
| 25 | armnnExecuteCallback_1_0 callback; |
| 26 | }; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 27 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 28 | struct ExecutionContext_1_0 {}; |
| 29 | |
| 30 | using CallbackContext_1_0 = CallbackContext<armnnExecuteCallback_1_0, ExecutionContext_1_0>; |
| 31 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 32 | template <typename HalVersion> |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 33 | class ArmnnPreparedModel : public V1_0::IPreparedModel |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 34 | { |
| 35 | public: |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 36 | using HalModel = typename HalVersion::Model; |
| 37 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 38 | ArmnnPreparedModel(armnn::NetworkId networkId, |
| 39 | armnn::IRuntime* runtime, |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 40 | const HalModel& model, |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 41 | const std::string& requestInputsAndOutputsDumpDir, |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 42 | const bool gpuProfilingEnabled, |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 43 | const bool asyncModelExecutionEnabled = false, |
| 44 | const unsigned int numberOfThreads = 1); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 45 | |
| 46 | virtual ~ArmnnPreparedModel(); |
| 47 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 48 | virtual Return<V1_0::ErrorStatus> execute(const V1_0::Request& request, |
| 49 | const ::android::sp<V1_0::IExecutionCallback>& callback) override; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 50 | |
| 51 | /// execute the graph prepared from the request |
| 52 | void ExecuteGraph(std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 53 | armnn::InputTensors& inputTensors, |
| 54 | armnn::OutputTensors& outputTensors, |
| 55 | CallbackContext_1_0 callback); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 56 | |
| 57 | /// Executes this model with dummy inputs (e.g. all zeroes). |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 58 | /// \return false on failure, otherwise true |
| 59 | bool ExecuteWithDummyInputs(); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 60 | |
| 61 | private: |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 62 | |
| 63 | template<typename CallbackContext> |
| 64 | class ArmnnThreadPoolCallback : public armnn::IAsyncExecutionCallback |
| 65 | { |
| 66 | public: |
| 67 | ArmnnThreadPoolCallback(ArmnnPreparedModel<HalVersion>* model, |
| 68 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 69 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 70 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 71 | CallbackContext callbackContext) : |
| 72 | m_Model(model), |
| 73 | m_MemPools(pMemPools), |
| 74 | m_InputTensors(inputTensors), |
| 75 | m_OutputTensors(outputTensors), |
| 76 | m_CallbackContext(callbackContext) |
| 77 | {} |
| 78 | |
| 79 | void Notify(armnn::Status status, armnn::InferenceTimingPair timeTaken) override; |
| 80 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 81 | ArmnnPreparedModel<HalVersion>* m_Model; |
| 82 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>> m_MemPools; |
| 83 | std::shared_ptr<armnn::InputTensors> m_InputTensors; |
| 84 | std::shared_ptr<armnn::OutputTensors> m_OutputTensors; |
| 85 | CallbackContext m_CallbackContext; |
| 86 | }; |
| 87 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 88 | template <typename TensorBindingCollection> |
| 89 | void DumpTensorsIfRequired(char const* tensorNamePrefix, const TensorBindingCollection& tensorBindings); |
| 90 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 91 | /// schedule the graph prepared from the request for execution |
| 92 | template<typename CallbackContext> |
| 93 | void ScheduleGraphForExecution( |
| 94 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 95 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 96 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 97 | CallbackContext m_CallbackContext); |
| 98 | |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 99 | armnn::NetworkId m_NetworkId; |
| 100 | armnn::IRuntime* m_Runtime; |
| 101 | HalModel m_Model; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 102 | // There must be a single RequestThread for all ArmnnPreparedModel objects to ensure serial execution of workloads |
| 103 | // It is specific to this class, so it is declared as static here |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 104 | static RequestThread<ArmnnPreparedModel, |
| 105 | HalVersion, |
| 106 | CallbackContext_1_0> m_RequestThread; |
| 107 | uint32_t m_RequestCount; |
| 108 | const std::string& m_RequestInputsAndOutputsDumpDir; |
| 109 | const bool m_GpuProfilingEnabled; |
| 110 | // Static to allow sharing of threadpool between ArmnnPreparedModel instances |
| 111 | static std::unique_ptr<armnn::Threadpool> m_Threadpool; |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 112 | std::shared_ptr<armnn::IWorkingMemHandle> m_WorkingMemHandle; |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 113 | const bool m_AsyncModelExecutionEnabled; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } |