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