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, |
Narumol Prangnawarat | d1a947f | 2022-02-07 13:12:24 +0000 | [diff] [blame] | 44 | const unsigned int numberOfThreads = 1, |
David Monahan | be9d99e | 2022-04-29 16:25:24 +0100 | [diff] [blame] | 45 | const bool importEnabled = false, |
| 46 | const bool exportEnabled = false); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 47 | |
| 48 | virtual ~ArmnnPreparedModel(); |
| 49 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 50 | virtual Return<V1_0::ErrorStatus> execute(const V1_0::Request& request, |
| 51 | const ::android::sp<V1_0::IExecutionCallback>& callback) override; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 52 | |
| 53 | /// execute the graph prepared from the request |
| 54 | void ExecuteGraph(std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 55 | armnn::InputTensors& inputTensors, |
| 56 | armnn::OutputTensors& outputTensors, |
| 57 | CallbackContext_1_0 callback); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 58 | |
| 59 | /// Executes this model with dummy inputs (e.g. all zeroes). |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 60 | /// \return false on failure, otherwise true |
| 61 | bool ExecuteWithDummyInputs(); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 62 | |
| 63 | private: |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 64 | |
| 65 | template<typename CallbackContext> |
| 66 | class ArmnnThreadPoolCallback : public armnn::IAsyncExecutionCallback |
| 67 | { |
| 68 | public: |
| 69 | ArmnnThreadPoolCallback(ArmnnPreparedModel<HalVersion>* model, |
| 70 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 71 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 72 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 73 | CallbackContext callbackContext) : |
| 74 | m_Model(model), |
| 75 | m_MemPools(pMemPools), |
| 76 | m_InputTensors(inputTensors), |
| 77 | m_OutputTensors(outputTensors), |
| 78 | m_CallbackContext(callbackContext) |
| 79 | {} |
| 80 | |
| 81 | void Notify(armnn::Status status, armnn::InferenceTimingPair timeTaken) override; |
| 82 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 83 | ArmnnPreparedModel<HalVersion>* m_Model; |
| 84 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>> m_MemPools; |
| 85 | std::shared_ptr<armnn::InputTensors> m_InputTensors; |
| 86 | std::shared_ptr<armnn::OutputTensors> m_OutputTensors; |
| 87 | CallbackContext m_CallbackContext; |
| 88 | }; |
| 89 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 90 | template <typename TensorBindingCollection> |
| 91 | void DumpTensorsIfRequired(char const* tensorNamePrefix, const TensorBindingCollection& tensorBindings); |
| 92 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 93 | /// schedule the graph prepared from the request for execution |
| 94 | template<typename CallbackContext> |
| 95 | void ScheduleGraphForExecution( |
| 96 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 97 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 98 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 99 | CallbackContext m_CallbackContext); |
| 100 | |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 101 | armnn::NetworkId m_NetworkId; |
| 102 | armnn::IRuntime* m_Runtime; |
| 103 | HalModel m_Model; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 104 | // There must be a single RequestThread for all ArmnnPreparedModel objects to ensure serial execution of workloads |
| 105 | // 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] | 106 | static RequestThread<ArmnnPreparedModel, |
| 107 | HalVersion, |
| 108 | CallbackContext_1_0> m_RequestThread; |
| 109 | uint32_t m_RequestCount; |
| 110 | const std::string& m_RequestInputsAndOutputsDumpDir; |
| 111 | const bool m_GpuProfilingEnabled; |
| 112 | // Static to allow sharing of threadpool between ArmnnPreparedModel instances |
| 113 | static std::unique_ptr<armnn::Threadpool> m_Threadpool; |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 114 | std::shared_ptr<armnn::IWorkingMemHandle> m_WorkingMemHandle; |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 115 | const bool m_AsyncModelExecutionEnabled; |
Narumol Prangnawarat | d1a947f | 2022-02-07 13:12:24 +0000 | [diff] [blame] | 116 | const bool m_EnableImport; |
| 117 | const bool m_EnableExport; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } |