blob: ea48c0cc40fd9347ec8bce715d5d3f6400c63a1b [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
Mike Kellye2d611e2021-10-14 12:35:58 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa015307bc12018-03-09 13:51:08 +00004//
5
6#define LOG_TAG "ArmnnDriver"
7
8#include "ArmnnPreparedModel.hpp"
9#include "Utils.hpp"
10
Narumol Prangnawarat85094222022-01-28 23:26:07 +000011#include <armnn/Types.hpp>
12
telsoa015307bc12018-03-09 13:51:08 +000013#include <log/log.h>
14#include <OperationsUtils.h>
surmeh01deb3bdb2018-07-05 12:06:04 +010015#include <ValidateHal.h>
Kevin Mayec1e5b82020-02-26 17:00:39 +000016
telsoa015307bc12018-03-09 13:51:08 +000017#include <cinttypes>
18
Sadik Armagan188675f2021-02-12 17:16:42 +000019#ifdef ARMNN_ANDROID_S
20#include <LegacyUtils.h>
21#endif
22
telsoa015307bc12018-03-09 13:51:08 +000023using namespace android;
24
25namespace
26{
27using namespace armnn_driver;
28
Kevin Mayec1e5b82020-02-26 17:00:39 +000029void NotifyCallbackAndCheck(const ::android::sp<V1_0::IExecutionCallback>& callback, V1_0::ErrorStatus errorStatus,
telsoa015307bc12018-03-09 13:51:08 +000030 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 Armagan188675f2021-02-12 17:16:42 +000041bool ValidateRequestArgument(const V1_0::RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo)
telsoa015307bc12018-03-09 13:51:08 +000042{
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 Williamsa4983ce2020-07-23 12:55:12 +010054 if (requestArg.dimensions[d] != 0 && requestArg.dimensions[d] != tensorInfo.GetShape()[d])
telsoa015307bc12018-03-09 13:51:08 +000055 {
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 Armagan188675f2021-02-12 17:16:42 +000066armnn::Tensor GetTensorForRequestArgument(const V1_0::RequestArgument& requestArg,
telsoa015307bc12018-03-09 13:51:08 +000067 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
78inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index)
79{
80 return tensorNamePrefix + std::to_string(index);
81}
82
Matteo Martincighe48bdff2018-09-03 13:50:50 +010083} // anonymous namespace
telsoa015307bc12018-03-09 13:51:08 +000084
telsoa01ce3e84a2018-08-31 09:31:35 +010085using namespace android::hardware;
86
telsoa015307bc12018-03-09 13:51:08 +000087namespace armnn_driver
88{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010089template<typename HalVersion>
Derek Lamberti4de83c52020-03-17 13:40:18 +000090RequestThread<ArmnnPreparedModel, HalVersion, CallbackContext_1_0>
91 ArmnnPreparedModel<HalVersion>::m_RequestThread;
telsoa015307bc12018-03-09 13:51:08 +000092
Matteo Martincighe48bdff2018-09-03 13:50:50 +010093template<typename HalVersion>
Finn Williamsfdf2eae2021-07-08 13:07:19 +010094std::unique_ptr<armnn::Threadpool> ArmnnPreparedModel<HalVersion>::m_Threadpool(nullptr);
95
96template<typename HalVersion>
telsoa015307bc12018-03-09 13:51:08 +000097template <typename TensorBindingCollection>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010098void ArmnnPreparedModel<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix,
99 const TensorBindingCollection& tensorBindings)
telsoa015307bc12018-03-09 13:51:08 +0000100{
101 if (!m_RequestInputsAndOutputsDumpDir.empty())
102 {
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100103 const std::string requestName = std::to_string(m_NetworkId) + "_" + std::to_string(m_RequestCount) + ".dump";
telsoa015307bc12018-03-09 13:51:08 +0000104 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 Martincighe48bdff2018-09-03 13:50:50 +0100114template<typename HalVersion>
115ArmnnPreparedModel<HalVersion>::ArmnnPreparedModel(armnn::NetworkId networkId,
116 armnn::IRuntime* runtime,
117 const HalModel& model,
118 const std::string& requestInputsAndOutputsDumpDir,
Finn Williamsd8fb5402021-05-19 20:52:00 +0100119 const bool gpuProfilingEnabled,
Finn Williamsca3a3e02021-06-11 15:04:02 +0100120 const bool asyncModelExecutionEnabled,
121 const unsigned int numberOfThreads)
telsoa01ce3e84a2018-08-31 09:31:35 +0100122 : m_NetworkId(networkId)
123 , m_Runtime(runtime)
124 , m_Model(model)
125 , m_RequestCount(0)
126 , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir)
127 , m_GpuProfilingEnabled(gpuProfilingEnabled)
Finn Williamsd8fb5402021-05-19 20:52:00 +0100128 , m_AsyncModelExecutionEnabled(asyncModelExecutionEnabled)
telsoa015307bc12018-03-09 13:51:08 +0000129{
telsoa01ce3e84a2018-08-31 09:31:35 +0100130 // Enable profiling if required.
131 m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100132
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100133 if (m_AsyncModelExecutionEnabled)
Finn Williamsd8fb5402021-05-19 20:52:00 +0100134 {
Finn Williamsca3a3e02021-06-11 15:04:02 +0100135 std::vector<std::shared_ptr<armnn::IWorkingMemHandle>> memHandles;
Finn Williamsd27c13b2021-06-25 10:06:09 +0100136 for (unsigned int i=0; i < numberOfThreads; ++i)
Finn Williamsca3a3e02021-06-11 15:04:02 +0100137 {
138 memHandles.emplace_back(m_Runtime->CreateWorkingMemHandle(networkId));
139 }
140
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100141 if (!m_Threadpool)
142 {
143 m_Threadpool = std::make_unique<armnn::Threadpool>(numberOfThreads, runtime, memHandles);
144 }
145 else
146 {
147 m_Threadpool->LoadMemHandles(memHandles);
148 }
149
Finn Williamsca3a3e02021-06-11 15:04:02 +0100150 m_WorkingMemHandle = memHandles.back();
Finn Williamsd8fb5402021-05-19 20:52:00 +0100151 }
telsoa015307bc12018-03-09 13:51:08 +0000152}
153
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100154template<typename HalVersion>
155ArmnnPreparedModel<HalVersion>::~ArmnnPreparedModel()
telsoa015307bc12018-03-09 13:51:08 +0000156{
telsoa01ce3e84a2018-08-31 09:31:35 +0100157 // Get a hold of the profiler used by this model.
158 std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId);
159
160 // Unload the network associated with this model.
telsoa015307bc12018-03-09 13:51:08 +0000161 m_Runtime->UnloadNetwork(m_NetworkId);
telsoa01ce3e84a2018-08-31 09:31:35 +0100162
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100163 // Unload the network memhandles from the threadpool
164 if (m_AsyncModelExecutionEnabled)
165 {
166 m_Threadpool->UnloadMemHandles(m_NetworkId);
167 }
168
telsoa01ce3e84a2018-08-31 09:31:35 +0100169 // Dump the profiling info to a file if required.
170 DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get());
telsoa015307bc12018-03-09 13:51:08 +0000171}
172
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100173template<typename HalVersion>
Kevin Mayec1e5b82020-02-26 17:00:39 +0000174Return<V1_0::ErrorStatus> ArmnnPreparedModel<HalVersion>::execute(
175 const V1_0::Request& request,
176 const ::android::sp<V1_0::IExecutionCallback>& callback)
telsoa015307bc12018-03-09 13:51:08 +0000177{
178 ALOGV("ArmnnPreparedModel::execute(): %s", GetModelSummary(m_Model).c_str());
179 m_RequestCount++;
180
181 if (callback.get() == nullptr) {
182 ALOGE("ArmnnPreparedModel::execute invalid callback passed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000183 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000184 }
185
186 if (!android::nn::validateRequest(request, m_Model))
187 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000188 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::INVALID_ARGUMENT, "ArmnnPreparedModel::execute");
189 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000190 }
191
192 if (!m_RequestInputsAndOutputsDumpDir.empty())
193 {
194 ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(callback.get()));
195 }
196
197 // allocate the tensors on the heap, as they are passed to the request thread
198 auto pInputTensors = std::make_shared<armnn::InputTensors>();
199 auto pOutputTensors = std::make_shared<armnn::OutputTensors>();
200
201 // map the memory pool into shared pointers
202 // use a shared memory pools vector on the heap, as it is passed to the request thread
203 auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>();
Sadik Armagan188675f2021-02-12 17:16:42 +0000204#if !defined(ARMNN_ANDROID_S)
telsoa015307bc12018-03-09 13:51:08 +0000205 if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools))
Sadik Armagan188675f2021-02-12 17:16:42 +0000206#else
207 if (!setRunTimePoolInfosFromCanonicalMemories(pMemPools.get(), uncheckedConvert(request.pools)))
208#endif
telsoa015307bc12018-03-09 13:51:08 +0000209 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000210 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
211 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000212 }
telsoa015307bc12018-03-09 13:51:08 +0000213 // add the inputs and outputs with their data
214 try
215 {
216 pInputTensors->reserve(request.inputs.size());
217 for (unsigned int i = 0; i < request.inputs.size(); i++)
218 {
219 const auto& inputArg = request.inputs[i];
220
Cathal Corbette27d4e82021-10-28 12:28:35 +0100221 armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
222 // pInputTensors (of type InputTensors) is composed of a vector of ConstTensors.
223 // Therefore, set all TensorInfo isConstant parameters of input Tensors to true.
224 inputTensorInfo.SetConstant();
telsoa015307bc12018-03-09 13:51:08 +0000225 const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools);
226 if (inputTensor.GetMemoryArea() == nullptr)
227 {
228 ALOGE("Cannot execute request. Error converting request input %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000229 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000230 }
231
232 pInputTensors->emplace_back(i, inputTensor);
233 }
234
235 pOutputTensors->reserve(request.outputs.size());
236 for (unsigned int i = 0; i < request.outputs.size(); i++)
237 {
238 const auto& outputArg = request.outputs[i];
239
240 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
241 const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools);
242 if (outputTensor.GetMemoryArea() == nullptr)
243 {
244 ALOGE("Cannot execute request. Error converting request output %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000245 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000246 }
247
248 pOutputTensors->emplace_back(i, outputTensor);
249 }
250 }
Kevin May7bdaac52020-02-10 12:10:07 +0000251 catch (armnn::Exception& e)
252 {
253 ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000254 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
255 return V1_0::ErrorStatus::GENERAL_FAILURE;
Kevin May7bdaac52020-02-10 12:10:07 +0000256 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000257 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000258 {
Kevin May7bdaac52020-02-10 12:10:07 +0000259 ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000260 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
261 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000262 }
263
Kevin Mayec1e5b82020-02-26 17:00:39 +0000264 auto cb = [callback](V1_0::ErrorStatus errorStatus, std::string callingFunction)
Mike Kelly65c42dc2019-07-22 14:06:00 +0100265 {
266 NotifyCallbackAndCheck(callback, errorStatus, callingFunction);
267 };
268
Derek Lamberti4de83c52020-03-17 13:40:18 +0000269 CallbackContext_1_0 armnnCb;
Mike Kelly65c42dc2019-07-22 14:06:00 +0100270 armnnCb.callback = cb;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100271
272 if (m_AsyncModelExecutionEnabled)
273 {
274 ALOGV("ArmnnPreparedModel::execute(...) before ScheduleGraphForExecution");
275 ScheduleGraphForExecution(pMemPools, pInputTensors, pOutputTensors, armnnCb);
276 ALOGV("ArmnnPreparedModel::execute(...) after ScheduleGraphForExecution");
277 return V1_0::ErrorStatus::NONE;
278 }
279
Mike Kelly65c42dc2019-07-22 14:06:00 +0100280 // post the request for asynchronous execution
Finn Williamsd8fb5402021-05-19 20:52:00 +0100281 ALOGV("ArmnnPreparedModel::execute(...) before PostMsg");
Mike Kelly65c42dc2019-07-22 14:06:00 +0100282 m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, armnnCb);
283 ALOGV("ArmnnPreparedModel::execute(...) after PostMsg");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000284 return V1_0::ErrorStatus::NONE; // successfully queued
telsoa015307bc12018-03-09 13:51:08 +0000285}
286
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100287template<typename HalVersion>
288void ArmnnPreparedModel<HalVersion>::ExecuteGraph(
289 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
Derek Lamberti4de83c52020-03-17 13:40:18 +0000290 armnn::InputTensors& inputTensors,
291 armnn::OutputTensors& outputTensors,
292 CallbackContext_1_0 cb)
telsoa015307bc12018-03-09 13:51:08 +0000293{
294 ALOGV("ArmnnPreparedModel::ExecuteGraph(...)");
295
Derek Lamberti4de83c52020-03-17 13:40:18 +0000296 DumpTensorsIfRequired("Input", inputTensors);
telsoa015307bc12018-03-09 13:51:08 +0000297
298 // run it
299 try
300 {
Finn Williamsd8fb5402021-05-19 20:52:00 +0100301 armnn::Status status;
302 if (m_AsyncModelExecutionEnabled)
303 {
304 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true");
305 status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors);
306 }
307 else
308 {
309 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false");
Narumol Prangnawarat85094222022-01-28 23:26:07 +0000310 std::vector<armnn::ImportedInputId> importedInputIds =
311 m_Runtime->ImportInputs(m_NetworkId, inputTensors, armnn::MemorySource::Malloc);
312 std::vector<armnn::ImportedOutputId> importedOutputIds =
313 m_Runtime->ImportOutputs(m_NetworkId, outputTensors, armnn::MemorySource::Malloc);
314 status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors,
315 importedInputIds, importedOutputIds);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100316 }
317
Matthew Bentham16196e22019-04-01 17:17:58 +0100318 if (status != armnn::Status::Success)
319 {
320 ALOGW("EnqueueWorkload failed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000321 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Matthew Bentham16196e22019-04-01 17:17:58 +0100322 return;
323 }
telsoa015307bc12018-03-09 13:51:08 +0000324 }
Kevin May7bdaac52020-02-10 12:10:07 +0000325 catch (armnn::Exception& e)
326 {
327 ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000328 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Kevin May7bdaac52020-02-10 12:10:07 +0000329 return;
330 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000331 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000332 {
Kevin May7bdaac52020-02-10 12:10:07 +0000333 ALOGE("std::exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000334 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000335 return;
336 }
337
Derek Lamberti4de83c52020-03-17 13:40:18 +0000338 DumpTensorsIfRequired("Output", outputTensors);
telsoa015307bc12018-03-09 13:51:08 +0000339
340 // Commit output buffers.
341 // Note that we update *all* pools, even if they aren't actually used as outputs -
342 // this is simpler and is what the CpuExecutor does.
343 for (android::nn::RunTimePoolInfo& pool : *pMemPools)
344 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000345 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
346 // update() has been removed and flush() added.
Sadik Armagan188675f2021-02-12 17:16:42 +0000347 #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation.
Kevin Mayec1e5b82020-02-26 17:00:39 +0000348 pool.flush();
349 #else
350 pool.update();
351 #endif
telsoa015307bc12018-03-09 13:51:08 +0000352 }
353
Kevin Mayec1e5b82020-02-26 17:00:39 +0000354 cb.callback(V1_0::ErrorStatus::NONE, "ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000355}
356
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100357template<typename HalVersion>
Matthew Bentham16196e22019-04-01 17:17:58 +0100358bool ArmnnPreparedModel<HalVersion>::ExecuteWithDummyInputs()
telsoa015307bc12018-03-09 13:51:08 +0000359{
360 std::vector<std::vector<char>> storage;
361 armnn::InputTensors inputTensors;
Kevin May42477c12020-03-26 13:34:14 +0000362 for (unsigned int i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000363 {
Cathal Corbette27d4e82021-10-28 12:28:35 +0100364 armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
365 // pInputTensors (of type InputTensors) is composed of a vector of ConstTensors.
366 // Therefore, set all TensorInfo isConstant parameters of input Tensors to true.
367 inputTensorInfo.SetConstant();
368
telsoa015307bc12018-03-09 13:51:08 +0000369 storage.emplace_back(inputTensorInfo.GetNumBytes());
370 const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data());
371
372 inputTensors.emplace_back(i, inputTensor);
373 }
374
375 armnn::OutputTensors outputTensors;
Kevin May42477c12020-03-26 13:34:14 +0000376 for (unsigned int i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000377 {
378 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
379 storage.emplace_back(outputTensorInfo.GetNumBytes());
380 const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data());
381
382 outputTensors.emplace_back(i, outputTensor);
383 }
384
385 try
386 {
Finn Williams8fde84b2021-05-31 14:57:15 +0100387 armnn::Status status;
388 if (m_AsyncModelExecutionEnabled)
389 {
390 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true");
391 status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors);
392 }
393 else
394 {
395 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false");
Narumol Prangnawarat85094222022-01-28 23:26:07 +0000396 std::vector<armnn::ImportedInputId> importedInputIds =
397 m_Runtime->ImportInputs(m_NetworkId, inputTensors, armnn::MemorySource::Malloc);
398 std::vector<armnn::ImportedOutputId> importedOutputIds =
399 m_Runtime->ImportOutputs(m_NetworkId, outputTensors, armnn::MemorySource::Malloc);
400 status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors,
401 importedInputIds, importedOutputIds);
Finn Williams8fde84b2021-05-31 14:57:15 +0100402 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100403 if (status != armnn::Status::Success)
404 {
405 ALOGW("ExecuteWithDummyInputs: EnqueueWorkload failed");
406 return false;
407 }
telsoa015307bc12018-03-09 13:51:08 +0000408 }
Kevin May7bdaac52020-02-10 12:10:07 +0000409 catch (armnn::Exception& e)
410 {
411 ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what());
412 return false;
413 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000414 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000415 {
Kevin May7bdaac52020-02-10 12:10:07 +0000416 ALOGE("ExecuteWithDummyInputs: std::exception caught from EnqueueWorkload: %s", e.what());
Matthew Bentham16196e22019-04-01 17:17:58 +0100417 return false;
telsoa015307bc12018-03-09 13:51:08 +0000418 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100419 return true;
telsoa015307bc12018-03-09 13:51:08 +0000420}
421
Finn Williamsd8fb5402021-05-19 20:52:00 +0100422/// Schedule the graph prepared from the request for execution
423template<typename HalVersion>
424template<typename CallbackContext>
425void ArmnnPreparedModel<HalVersion>::ScheduleGraphForExecution(
426 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
427 std::shared_ptr<armnn::InputTensors>& inputTensors,
428 std::shared_ptr<armnn::OutputTensors>& outputTensors,
429 CallbackContext callbackContext)
430{
431 ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution(...)");
432
433 DumpTensorsIfRequired("Input", *inputTensors);
434
435
436 auto tpCb = std::make_shared<
437 ArmnnThreadPoolCallback<CallbackContext_1_0>>(this,
438 pMemPools,
439 inputTensors,
440 outputTensors,
441 callbackContext);
442
Finn Williamsca3a3e02021-06-11 15:04:02 +0100443 m_Threadpool->Schedule(m_NetworkId,
444 *tpCb->m_InputTensors,
445 *tpCb->m_OutputTensors,
446 armnn::QosExecPriority::Medium,
447 tpCb);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100448 ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution end");
449}
450
451template<typename HalVersion>
452template <typename CallbackContext>
453void ArmnnPreparedModel<HalVersion>::ArmnnThreadPoolCallback<CallbackContext>::Notify(
454 armnn::Status status, armnn::InferenceTimingPair timeTaken)
455{
456 armnn::IgnoreUnused(status, timeTaken);
457 ALOGV("ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify");
458
459 m_Model->DumpTensorsIfRequired("Output", *m_OutputTensors);
460
461 // Commit output buffers.
462 // Note that we update *all* pools, even if they aren't actually used as outputs -
463 // this is simpler and is what the CpuExecutor does.
464 for (android::nn::RunTimePoolInfo& pool : *m_MemPools)
465 {
466 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
467 // update() has been removed and flush() added.
468 #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation.
469 pool.flush();
470 #else
471 pool.update();
472 #endif
473 }
474
475 m_CallbackContext.callback(V1_0::ErrorStatus::NONE, "ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify");
476 return;
477}
478
arovir01b0717b52018-09-05 17:03:25 +0100479///
480/// Class template specializations
481///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100482
arovir01b0717b52018-09-05 17:03:25 +0100483template class ArmnnPreparedModel<hal_1_0::HalPolicy>;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100484template void ArmnnPreparedModel<hal_1_0::HalPolicy>::ScheduleGraphForExecution<CallbackContext_1_0>(
485 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
486 std::shared_ptr<armnn::InputTensors>& inputTensors,
487 std::shared_ptr<armnn::OutputTensors>& outputTensors,
488 CallbackContext_1_0 callbackContext);
arovir01b0717b52018-09-05 17:03:25 +0100489
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100490#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100491template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100492#endif
493
Mike Kellyb5fdf382019-06-11 16:35:25 +0100494#ifdef ARMNN_ANDROID_NN_V1_2
495template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
496template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
497#endif
Kevin May42477c12020-03-26 13:34:14 +0000498
499#ifdef ARMNN_ANDROID_NN_V1_3
500template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
501template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
502template class ArmnnPreparedModel<hal_1_3::HalPolicy>;
503#endif
Nikhil Raj77605822018-09-03 11:25:56 +0100504} // namespace armnn_driver