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 | #define LOG_TAG "ArmnnDriver" |
| 7 | |
| 8 | #include "ArmnnPreparedModel.hpp" |
| 9 | #include "Utils.hpp" |
| 10 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 11 | #include <log/log.h> |
| 12 | #include <OperationsUtils.h> |
surmeh01 | deb3bdb | 2018-07-05 12:06:04 +0100 | [diff] [blame] | 13 | #include <ValidateHal.h> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 14 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 15 | #include <cassert> |
| 16 | #include <cinttypes> |
| 17 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 18 | #ifdef ARMNN_ANDROID_S |
| 19 | #include <LegacyUtils.h> |
| 20 | #endif |
| 21 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 22 | using namespace android; |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | using namespace armnn_driver; |
| 27 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 28 | void NotifyCallbackAndCheck(const ::android::sp<V1_0::IExecutionCallback>& callback, V1_0::ErrorStatus errorStatus, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 29 | std::string callingFunction) |
| 30 | { |
| 31 | Return<void> returned = callback->notify(errorStatus); |
| 32 | // This check is required, if the callback fails and it isn't checked it will bring down the service |
| 33 | if (!returned.isOk()) |
| 34 | { |
| 35 | ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s", |
| 36 | callingFunction.c_str(), returned.description().c_str()); |
| 37 | } |
| 38 | } |
| 39 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 40 | bool ValidateRequestArgument(const V1_0::RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 41 | { |
| 42 | if (requestArg.dimensions.size() != 0) |
| 43 | { |
| 44 | if (requestArg.dimensions.size() != tensorInfo.GetNumDimensions()) |
| 45 | { |
| 46 | ALOGE("Mismatched dimensions (request argument: %zu, expected: %u)", |
| 47 | requestArg.dimensions.size(), tensorInfo.GetNumDimensions()); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | for (unsigned int d = 0; d < tensorInfo.GetNumDimensions(); ++d) |
| 52 | { |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 53 | if (requestArg.dimensions[d] != 0 && requestArg.dimensions[d] != tensorInfo.GetShape()[d]) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 54 | { |
| 55 | ALOGE("Mismatched size for dimension %d (request argument: %u, expected %u)", |
| 56 | d, requestArg.dimensions[d], tensorInfo.GetShape()[d]); |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 65 | armnn::Tensor GetTensorForRequestArgument(const V1_0::RequestArgument& requestArg, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 66 | const armnn::TensorInfo& tensorInfo, |
| 67 | const std::vector<::android::nn::RunTimePoolInfo>& requestPools) |
| 68 | { |
| 69 | if (!ValidateRequestArgument(requestArg, tensorInfo)) |
| 70 | { |
| 71 | return armnn::Tensor(); |
| 72 | } |
| 73 | |
| 74 | return armnn::Tensor(tensorInfo, GetMemoryFromPool(requestArg.location, requestPools)); |
| 75 | } |
| 76 | |
| 77 | inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index) |
| 78 | { |
| 79 | return tensorNamePrefix + std::to_string(index); |
| 80 | } |
| 81 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 82 | } // anonymous namespace |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 83 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 84 | using namespace android::hardware; |
| 85 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 86 | namespace armnn_driver |
| 87 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 88 | template<typename HalVersion> |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 89 | RequestThread<ArmnnPreparedModel, HalVersion, CallbackContext_1_0> |
| 90 | ArmnnPreparedModel<HalVersion>::m_RequestThread; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 91 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 92 | template<typename HalVersion> |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 93 | std::unique_ptr<armnn::Threadpool> ArmnnPreparedModel<HalVersion>::m_Threadpool(nullptr); |
| 94 | |
| 95 | template<typename HalVersion> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 96 | template <typename TensorBindingCollection> |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 97 | void ArmnnPreparedModel<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix, |
| 98 | const TensorBindingCollection& tensorBindings) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 99 | { |
| 100 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 101 | { |
Colm Donelan | 08d9a1c | 2020-09-09 17:56:55 +0100 | [diff] [blame] | 102 | const std::string requestName = std::to_string(m_NetworkId) + "_" + std::to_string(m_RequestCount) + ".dump"; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 103 | for (std::size_t i = 0u; i < tensorBindings.size(); ++i) |
| 104 | { |
| 105 | DumpTensor(m_RequestInputsAndOutputsDumpDir, |
| 106 | requestName, |
| 107 | BuildTensorName(tensorNamePrefix, i), |
| 108 | tensorBindings[i].second); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 113 | template<typename HalVersion> |
| 114 | ArmnnPreparedModel<HalVersion>::ArmnnPreparedModel(armnn::NetworkId networkId, |
| 115 | armnn::IRuntime* runtime, |
| 116 | const HalModel& model, |
| 117 | const std::string& requestInputsAndOutputsDumpDir, |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 118 | const bool gpuProfilingEnabled, |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 119 | const bool asyncModelExecutionEnabled, |
| 120 | const unsigned int numberOfThreads) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 121 | : m_NetworkId(networkId) |
| 122 | , m_Runtime(runtime) |
| 123 | , m_Model(model) |
| 124 | , m_RequestCount(0) |
| 125 | , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir) |
| 126 | , m_GpuProfilingEnabled(gpuProfilingEnabled) |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 127 | , m_AsyncModelExecutionEnabled(asyncModelExecutionEnabled) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 128 | { |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 129 | // Enable profiling if required. |
| 130 | m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled); |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 131 | |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 132 | if (m_AsyncModelExecutionEnabled) |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 133 | { |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 134 | std::vector<std::shared_ptr<armnn::IWorkingMemHandle>> memHandles; |
Finn Williams | d27c13b | 2021-06-25 10:06:09 +0100 | [diff] [blame] | 135 | for (unsigned int i=0; i < numberOfThreads; ++i) |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 136 | { |
| 137 | memHandles.emplace_back(m_Runtime->CreateWorkingMemHandle(networkId)); |
| 138 | } |
| 139 | |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 140 | if (!m_Threadpool) |
| 141 | { |
| 142 | m_Threadpool = std::make_unique<armnn::Threadpool>(numberOfThreads, runtime, memHandles); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | m_Threadpool->LoadMemHandles(memHandles); |
| 147 | } |
| 148 | |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 149 | m_WorkingMemHandle = memHandles.back(); |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 150 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 153 | template<typename HalVersion> |
| 154 | ArmnnPreparedModel<HalVersion>::~ArmnnPreparedModel() |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 155 | { |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 156 | // Get a hold of the profiler used by this model. |
| 157 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId); |
| 158 | |
| 159 | // Unload the network associated with this model. |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 160 | m_Runtime->UnloadNetwork(m_NetworkId); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 161 | |
Finn Williams | fdf2eae | 2021-07-08 13:07:19 +0100 | [diff] [blame] | 162 | // Unload the network memhandles from the threadpool |
| 163 | if (m_AsyncModelExecutionEnabled) |
| 164 | { |
| 165 | m_Threadpool->UnloadMemHandles(m_NetworkId); |
| 166 | } |
| 167 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 168 | // Dump the profiling info to a file if required. |
| 169 | DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get()); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 172 | template<typename HalVersion> |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 173 | Return<V1_0::ErrorStatus> ArmnnPreparedModel<HalVersion>::execute( |
| 174 | const V1_0::Request& request, |
| 175 | const ::android::sp<V1_0::IExecutionCallback>& callback) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 176 | { |
| 177 | ALOGV("ArmnnPreparedModel::execute(): %s", GetModelSummary(m_Model).c_str()); |
| 178 | m_RequestCount++; |
| 179 | |
| 180 | if (callback.get() == nullptr) { |
| 181 | ALOGE("ArmnnPreparedModel::execute invalid callback passed"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 182 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (!android::nn::validateRequest(request, m_Model)) |
| 186 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 187 | NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::INVALID_ARGUMENT, "ArmnnPreparedModel::execute"); |
| 188 | return V1_0::ErrorStatus::INVALID_ARGUMENT; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if (!m_RequestInputsAndOutputsDumpDir.empty()) |
| 192 | { |
| 193 | ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(callback.get())); |
| 194 | } |
| 195 | |
| 196 | // allocate the tensors on the heap, as they are passed to the request thread |
| 197 | auto pInputTensors = std::make_shared<armnn::InputTensors>(); |
| 198 | auto pOutputTensors = std::make_shared<armnn::OutputTensors>(); |
| 199 | |
| 200 | // map the memory pool into shared pointers |
| 201 | // use a shared memory pools vector on the heap, as it is passed to the request thread |
| 202 | auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>(); |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 203 | #if !defined(ARMNN_ANDROID_S) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 204 | if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools)) |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 205 | #else |
| 206 | if (!setRunTimePoolInfosFromCanonicalMemories(pMemPools.get(), uncheckedConvert(request.pools))) |
| 207 | #endif |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 208 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 209 | NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute"); |
| 210 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 211 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 212 | // add the inputs and outputs with their data |
| 213 | try |
| 214 | { |
| 215 | pInputTensors->reserve(request.inputs.size()); |
| 216 | for (unsigned int i = 0; i < request.inputs.size(); i++) |
| 217 | { |
| 218 | const auto& inputArg = request.inputs[i]; |
| 219 | |
| 220 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 221 | const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools); |
| 222 | if (inputTensor.GetMemoryArea() == nullptr) |
| 223 | { |
| 224 | ALOGE("Cannot execute request. Error converting request input %u to tensor", i); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 225 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | pInputTensors->emplace_back(i, inputTensor); |
| 229 | } |
| 230 | |
| 231 | pOutputTensors->reserve(request.outputs.size()); |
| 232 | for (unsigned int i = 0; i < request.outputs.size(); i++) |
| 233 | { |
| 234 | const auto& outputArg = request.outputs[i]; |
| 235 | |
| 236 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 237 | const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools); |
| 238 | if (outputTensor.GetMemoryArea() == nullptr) |
| 239 | { |
| 240 | ALOGE("Cannot execute request. Error converting request output %u to tensor", i); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 241 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | pOutputTensors->emplace_back(i, outputTensor); |
| 245 | } |
| 246 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 247 | catch (armnn::Exception& e) |
| 248 | { |
| 249 | ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what()); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 250 | NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute"); |
| 251 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 252 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 253 | catch (std::exception& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 254 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 255 | ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what()); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 256 | NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute"); |
| 257 | return V1_0::ErrorStatus::GENERAL_FAILURE; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 260 | auto cb = [callback](V1_0::ErrorStatus errorStatus, std::string callingFunction) |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 261 | { |
| 262 | NotifyCallbackAndCheck(callback, errorStatus, callingFunction); |
| 263 | }; |
| 264 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 265 | CallbackContext_1_0 armnnCb; |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 266 | armnnCb.callback = cb; |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 267 | |
| 268 | if (m_AsyncModelExecutionEnabled) |
| 269 | { |
| 270 | ALOGV("ArmnnPreparedModel::execute(...) before ScheduleGraphForExecution"); |
| 271 | ScheduleGraphForExecution(pMemPools, pInputTensors, pOutputTensors, armnnCb); |
| 272 | ALOGV("ArmnnPreparedModel::execute(...) after ScheduleGraphForExecution"); |
| 273 | return V1_0::ErrorStatus::NONE; |
| 274 | } |
| 275 | |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 276 | // post the request for asynchronous execution |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 277 | ALOGV("ArmnnPreparedModel::execute(...) before PostMsg"); |
Mike Kelly | 65c42dc | 2019-07-22 14:06:00 +0100 | [diff] [blame] | 278 | m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, armnnCb); |
| 279 | ALOGV("ArmnnPreparedModel::execute(...) after PostMsg"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 280 | return V1_0::ErrorStatus::NONE; // successfully queued |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 283 | template<typename HalVersion> |
| 284 | void ArmnnPreparedModel<HalVersion>::ExecuteGraph( |
| 285 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 286 | armnn::InputTensors& inputTensors, |
| 287 | armnn::OutputTensors& outputTensors, |
| 288 | CallbackContext_1_0 cb) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 289 | { |
| 290 | ALOGV("ArmnnPreparedModel::ExecuteGraph(...)"); |
| 291 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 292 | DumpTensorsIfRequired("Input", inputTensors); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 293 | |
| 294 | // run it |
| 295 | try |
| 296 | { |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 297 | armnn::Status status; |
| 298 | if (m_AsyncModelExecutionEnabled) |
| 299 | { |
| 300 | ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true"); |
| 301 | status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors); |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false"); |
| 306 | status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors); |
| 307 | } |
| 308 | |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 309 | if (status != armnn::Status::Success) |
| 310 | { |
| 311 | ALOGW("EnqueueWorkload failed"); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 312 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph"); |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 313 | return; |
| 314 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 315 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 316 | catch (armnn::Exception& e) |
| 317 | { |
| 318 | ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what()); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 319 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph"); |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 320 | return; |
| 321 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 322 | catch (std::exception& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 323 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 324 | ALOGE("std::exception caught from EnqueueWorkload: %s", e.what()); |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 325 | cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | |
Derek Lamberti | 4de83c5 | 2020-03-17 13:40:18 +0000 | [diff] [blame] | 329 | DumpTensorsIfRequired("Output", outputTensors); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 330 | |
| 331 | // Commit output buffers. |
| 332 | // Note that we update *all* pools, even if they aren't actually used as outputs - |
| 333 | // this is simpler and is what the CpuExecutor does. |
| 334 | for (android::nn::RunTimePoolInfo& pool : *pMemPools) |
| 335 | { |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 336 | // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where |
| 337 | // update() has been removed and flush() added. |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 338 | #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation. |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 339 | pool.flush(); |
| 340 | #else |
| 341 | pool.update(); |
| 342 | #endif |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 345 | cb.callback(V1_0::ErrorStatus::NONE, "ExecuteGraph"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 348 | template<typename HalVersion> |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 349 | bool ArmnnPreparedModel<HalVersion>::ExecuteWithDummyInputs() |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 350 | { |
| 351 | std::vector<std::vector<char>> storage; |
| 352 | armnn::InputTensors inputTensors; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 353 | for (unsigned int i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 354 | { |
| 355 | const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i); |
| 356 | storage.emplace_back(inputTensorInfo.GetNumBytes()); |
| 357 | const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data()); |
| 358 | |
| 359 | inputTensors.emplace_back(i, inputTensor); |
| 360 | } |
| 361 | |
| 362 | armnn::OutputTensors outputTensors; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 363 | for (unsigned int i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 364 | { |
| 365 | const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i); |
| 366 | storage.emplace_back(outputTensorInfo.GetNumBytes()); |
| 367 | const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data()); |
| 368 | |
| 369 | outputTensors.emplace_back(i, outputTensor); |
| 370 | } |
| 371 | |
| 372 | try |
| 373 | { |
Finn Williams | 8fde84b | 2021-05-31 14:57:15 +0100 | [diff] [blame] | 374 | armnn::Status status; |
| 375 | if (m_AsyncModelExecutionEnabled) |
| 376 | { |
| 377 | ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true"); |
| 378 | status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors); |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false"); |
| 383 | status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors); |
| 384 | } |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 385 | if (status != armnn::Status::Success) |
| 386 | { |
| 387 | ALOGW("ExecuteWithDummyInputs: EnqueueWorkload failed"); |
| 388 | return false; |
| 389 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 390 | } |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 391 | catch (armnn::Exception& e) |
| 392 | { |
| 393 | ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what()); |
| 394 | return false; |
| 395 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 396 | catch (std::exception& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 397 | { |
Kevin May | 7bdaac5 | 2020-02-10 12:10:07 +0000 | [diff] [blame] | 398 | ALOGE("ExecuteWithDummyInputs: std::exception caught from EnqueueWorkload: %s", e.what()); |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 399 | return false; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 400 | } |
Matthew Bentham | 16196e2 | 2019-04-01 17:17:58 +0100 | [diff] [blame] | 401 | return true; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 404 | /// Schedule the graph prepared from the request for execution |
| 405 | template<typename HalVersion> |
| 406 | template<typename CallbackContext> |
| 407 | void ArmnnPreparedModel<HalVersion>::ScheduleGraphForExecution( |
| 408 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 409 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 410 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 411 | CallbackContext callbackContext) |
| 412 | { |
| 413 | ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution(...)"); |
| 414 | |
| 415 | DumpTensorsIfRequired("Input", *inputTensors); |
| 416 | |
| 417 | |
| 418 | auto tpCb = std::make_shared< |
| 419 | ArmnnThreadPoolCallback<CallbackContext_1_0>>(this, |
| 420 | pMemPools, |
| 421 | inputTensors, |
| 422 | outputTensors, |
| 423 | callbackContext); |
| 424 | |
Finn Williams | ca3a3e0 | 2021-06-11 15:04:02 +0100 | [diff] [blame] | 425 | m_Threadpool->Schedule(m_NetworkId, |
| 426 | *tpCb->m_InputTensors, |
| 427 | *tpCb->m_OutputTensors, |
| 428 | armnn::QosExecPriority::Medium, |
| 429 | tpCb); |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 430 | ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution end"); |
| 431 | } |
| 432 | |
| 433 | template<typename HalVersion> |
| 434 | template <typename CallbackContext> |
| 435 | void ArmnnPreparedModel<HalVersion>::ArmnnThreadPoolCallback<CallbackContext>::Notify( |
| 436 | armnn::Status status, armnn::InferenceTimingPair timeTaken) |
| 437 | { |
| 438 | armnn::IgnoreUnused(status, timeTaken); |
| 439 | ALOGV("ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify"); |
| 440 | |
| 441 | m_Model->DumpTensorsIfRequired("Output", *m_OutputTensors); |
| 442 | |
| 443 | // Commit output buffers. |
| 444 | // Note that we update *all* pools, even if they aren't actually used as outputs - |
| 445 | // this is simpler and is what the CpuExecutor does. |
| 446 | for (android::nn::RunTimePoolInfo& pool : *m_MemPools) |
| 447 | { |
| 448 | // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where |
| 449 | // update() has been removed and flush() added. |
| 450 | #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation. |
| 451 | pool.flush(); |
| 452 | #else |
| 453 | pool.update(); |
| 454 | #endif |
| 455 | } |
| 456 | |
| 457 | m_CallbackContext.callback(V1_0::ErrorStatus::NONE, "ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify"); |
| 458 | return; |
| 459 | } |
| 460 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 461 | /// |
| 462 | /// Class template specializations |
| 463 | /// |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 464 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 465 | template class ArmnnPreparedModel<hal_1_0::HalPolicy>; |
Finn Williams | d8fb540 | 2021-05-19 20:52:00 +0100 | [diff] [blame] | 466 | template void ArmnnPreparedModel<hal_1_0::HalPolicy>::ScheduleGraphForExecution<CallbackContext_1_0>( |
| 467 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools, |
| 468 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 469 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 470 | CallbackContext_1_0 callbackContext); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 471 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 472 | #ifdef ARMNN_ANDROID_NN_V1_1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 473 | template class ArmnnPreparedModel<hal_1_1::HalPolicy>; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 474 | #endif |
| 475 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 476 | #ifdef ARMNN_ANDROID_NN_V1_2 |
| 477 | template class ArmnnPreparedModel<hal_1_1::HalPolicy>; |
| 478 | template class ArmnnPreparedModel<hal_1_2::HalPolicy>; |
| 479 | #endif |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 480 | |
| 481 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 482 | template class ArmnnPreparedModel<hal_1_1::HalPolicy>; |
| 483 | template class ArmnnPreparedModel<hal_1_2::HalPolicy>; |
| 484 | template class ArmnnPreparedModel<hal_1_3::HalPolicy>; |
| 485 | #endif |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 486 | } // namespace armnn_driver |