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