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