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