blob: 388a11162d446e24f755d180e1430426943389be [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. 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
telsoa015307bc12018-03-09 13:51:08 +000011#include <log/log.h>
12#include <OperationsUtils.h>
surmeh01deb3bdb2018-07-05 12:06:04 +010013#include <ValidateHal.h>
Kevin Mayec1e5b82020-02-26 17:00:39 +000014
telsoa015307bc12018-03-09 13:51:08 +000015#include <cassert>
16#include <cinttypes>
17
Sadik Armagan188675f2021-02-12 17:16:42 +000018#ifdef ARMNN_ANDROID_S
19#include <LegacyUtils.h>
20#endif
21
telsoa015307bc12018-03-09 13:51:08 +000022using namespace android;
23
24namespace
25{
26using namespace armnn_driver;
27
Kevin Mayec1e5b82020-02-26 17:00:39 +000028void NotifyCallbackAndCheck(const ::android::sp<V1_0::IExecutionCallback>& callback, V1_0::ErrorStatus errorStatus,
telsoa015307bc12018-03-09 13:51:08 +000029 std::string callingFunction)
30{
31 Return<void> returned = callback->notify(errorStatus);
32 // This check is required, if the callback fails and it isn't checked it will bring down the service
33 if (!returned.isOk())
34 {
35 ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s",
36 callingFunction.c_str(), returned.description().c_str());
37 }
38}
39
Sadik Armagan188675f2021-02-12 17:16:42 +000040bool ValidateRequestArgument(const V1_0::RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo)
telsoa015307bc12018-03-09 13:51:08 +000041{
42 if (requestArg.dimensions.size() != 0)
43 {
44 if (requestArg.dimensions.size() != tensorInfo.GetNumDimensions())
45 {
46 ALOGE("Mismatched dimensions (request argument: %zu, expected: %u)",
47 requestArg.dimensions.size(), tensorInfo.GetNumDimensions());
48 return false;
49 }
50
51 for (unsigned int d = 0; d < tensorInfo.GetNumDimensions(); ++d)
52 {
Finn Williamsa4983ce2020-07-23 12:55:12 +010053 if (requestArg.dimensions[d] != 0 && requestArg.dimensions[d] != tensorInfo.GetShape()[d])
telsoa015307bc12018-03-09 13:51:08 +000054 {
55 ALOGE("Mismatched size for dimension %d (request argument: %u, expected %u)",
56 d, requestArg.dimensions[d], tensorInfo.GetShape()[d]);
57 return false;
58 }
59 }
60 }
61
62 return true;
63}
64
Sadik Armagan188675f2021-02-12 17:16:42 +000065armnn::Tensor GetTensorForRequestArgument(const V1_0::RequestArgument& requestArg,
telsoa015307bc12018-03-09 13:51:08 +000066 const armnn::TensorInfo& tensorInfo,
67 const std::vector<::android::nn::RunTimePoolInfo>& requestPools)
68{
69 if (!ValidateRequestArgument(requestArg, tensorInfo))
70 {
71 return armnn::Tensor();
72 }
73
74 return armnn::Tensor(tensorInfo, GetMemoryFromPool(requestArg.location, requestPools));
75}
76
77inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index)
78{
79 return tensorNamePrefix + std::to_string(index);
80}
81
Matteo Martincighe48bdff2018-09-03 13:50:50 +010082} // anonymous namespace
telsoa015307bc12018-03-09 13:51:08 +000083
telsoa01ce3e84a2018-08-31 09:31:35 +010084using namespace android::hardware;
85
telsoa015307bc12018-03-09 13:51:08 +000086namespace armnn_driver
87{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010088template<typename HalVersion>
Derek Lamberti4de83c52020-03-17 13:40:18 +000089RequestThread<ArmnnPreparedModel, HalVersion, CallbackContext_1_0>
90 ArmnnPreparedModel<HalVersion>::m_RequestThread;
telsoa015307bc12018-03-09 13:51:08 +000091
Matteo Martincighe48bdff2018-09-03 13:50:50 +010092template<typename HalVersion>
telsoa015307bc12018-03-09 13:51:08 +000093template <typename TensorBindingCollection>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010094void ArmnnPreparedModel<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix,
95 const TensorBindingCollection& tensorBindings)
telsoa015307bc12018-03-09 13:51:08 +000096{
97 if (!m_RequestInputsAndOutputsDumpDir.empty())
98 {
Colm Donelan08d9a1c2020-09-09 17:56:55 +010099 const std::string requestName = std::to_string(m_NetworkId) + "_" + std::to_string(m_RequestCount) + ".dump";
telsoa015307bc12018-03-09 13:51:08 +0000100 for (std::size_t i = 0u; i < tensorBindings.size(); ++i)
101 {
102 DumpTensor(m_RequestInputsAndOutputsDumpDir,
103 requestName,
104 BuildTensorName(tensorNamePrefix, i),
105 tensorBindings[i].second);
106 }
107 }
108}
109
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100110template<typename HalVersion>
111ArmnnPreparedModel<HalVersion>::ArmnnPreparedModel(armnn::NetworkId networkId,
112 armnn::IRuntime* runtime,
113 const HalModel& model,
114 const std::string& requestInputsAndOutputsDumpDir,
Finn Williamsd8fb5402021-05-19 20:52:00 +0100115 const bool gpuProfilingEnabled,
Finn Williamsca3a3e02021-06-11 15:04:02 +0100116 const bool asyncModelExecutionEnabled,
117 const unsigned int numberOfThreads)
telsoa01ce3e84a2018-08-31 09:31:35 +0100118 : m_NetworkId(networkId)
119 , m_Runtime(runtime)
120 , m_Model(model)
121 , m_RequestCount(0)
122 , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir)
123 , m_GpuProfilingEnabled(gpuProfilingEnabled)
Finn Williamsd8fb5402021-05-19 20:52:00 +0100124 , m_AsyncModelExecutionEnabled(asyncModelExecutionEnabled)
telsoa015307bc12018-03-09 13:51:08 +0000125{
telsoa01ce3e84a2018-08-31 09:31:35 +0100126 // Enable profiling if required.
127 m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100128
129 if (asyncModelExecutionEnabled)
130 {
Finn Williamsca3a3e02021-06-11 15:04:02 +0100131 std::vector<std::shared_ptr<armnn::IWorkingMemHandle>> memHandles;
Finn Williamsd27c13b2021-06-25 10:06:09 +0100132 for (unsigned int i=0; i < numberOfThreads; ++i)
Finn Williamsca3a3e02021-06-11 15:04:02 +0100133 {
134 memHandles.emplace_back(m_Runtime->CreateWorkingMemHandle(networkId));
135 }
136
137 m_WorkingMemHandle = memHandles.back();
138 m_Threadpool = std::make_unique<armnn::Threadpool>(numberOfThreads, runtime, memHandles);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100139 }
telsoa015307bc12018-03-09 13:51:08 +0000140}
141
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100142template<typename HalVersion>
143ArmnnPreparedModel<HalVersion>::~ArmnnPreparedModel()
telsoa015307bc12018-03-09 13:51:08 +0000144{
telsoa01ce3e84a2018-08-31 09:31:35 +0100145 // Get a hold of the profiler used by this model.
146 std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId);
147
148 // Unload the network associated with this model.
telsoa015307bc12018-03-09 13:51:08 +0000149 m_Runtime->UnloadNetwork(m_NetworkId);
telsoa01ce3e84a2018-08-31 09:31:35 +0100150
151 // Dump the profiling info to a file if required.
152 DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get());
telsoa015307bc12018-03-09 13:51:08 +0000153}
154
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100155template<typename HalVersion>
Kevin Mayec1e5b82020-02-26 17:00:39 +0000156Return<V1_0::ErrorStatus> ArmnnPreparedModel<HalVersion>::execute(
157 const V1_0::Request& request,
158 const ::android::sp<V1_0::IExecutionCallback>& callback)
telsoa015307bc12018-03-09 13:51:08 +0000159{
160 ALOGV("ArmnnPreparedModel::execute(): %s", GetModelSummary(m_Model).c_str());
161 m_RequestCount++;
162
163 if (callback.get() == nullptr) {
164 ALOGE("ArmnnPreparedModel::execute invalid callback passed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000165 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000166 }
167
168 if (!android::nn::validateRequest(request, m_Model))
169 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000170 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::INVALID_ARGUMENT, "ArmnnPreparedModel::execute");
171 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000172 }
173
174 if (!m_RequestInputsAndOutputsDumpDir.empty())
175 {
176 ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(callback.get()));
177 }
178
179 // allocate the tensors on the heap, as they are passed to the request thread
180 auto pInputTensors = std::make_shared<armnn::InputTensors>();
181 auto pOutputTensors = std::make_shared<armnn::OutputTensors>();
182
183 // map the memory pool into shared pointers
184 // use a shared memory pools vector on the heap, as it is passed to the request thread
185 auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>();
Sadik Armagan188675f2021-02-12 17:16:42 +0000186#if !defined(ARMNN_ANDROID_S)
telsoa015307bc12018-03-09 13:51:08 +0000187 if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools))
Sadik Armagan188675f2021-02-12 17:16:42 +0000188#else
189 if (!setRunTimePoolInfosFromCanonicalMemories(pMemPools.get(), uncheckedConvert(request.pools)))
190#endif
telsoa015307bc12018-03-09 13:51:08 +0000191 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000192 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
193 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000194 }
telsoa015307bc12018-03-09 13:51:08 +0000195 // add the inputs and outputs with their data
196 try
197 {
198 pInputTensors->reserve(request.inputs.size());
199 for (unsigned int i = 0; i < request.inputs.size(); i++)
200 {
201 const auto& inputArg = request.inputs[i];
202
203 const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
204 const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools);
205 if (inputTensor.GetMemoryArea() == nullptr)
206 {
207 ALOGE("Cannot execute request. Error converting request input %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000208 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000209 }
210
211 pInputTensors->emplace_back(i, inputTensor);
212 }
213
214 pOutputTensors->reserve(request.outputs.size());
215 for (unsigned int i = 0; i < request.outputs.size(); i++)
216 {
217 const auto& outputArg = request.outputs[i];
218
219 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
220 const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools);
221 if (outputTensor.GetMemoryArea() == nullptr)
222 {
223 ALOGE("Cannot execute request. Error converting request output %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000224 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000225 }
226
227 pOutputTensors->emplace_back(i, outputTensor);
228 }
229 }
Kevin May7bdaac52020-02-10 12:10:07 +0000230 catch (armnn::Exception& e)
231 {
232 ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000233 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
234 return V1_0::ErrorStatus::GENERAL_FAILURE;
Kevin May7bdaac52020-02-10 12:10:07 +0000235 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000236 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000237 {
Kevin May7bdaac52020-02-10 12:10:07 +0000238 ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000239 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
240 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000241 }
242
Kevin Mayec1e5b82020-02-26 17:00:39 +0000243 auto cb = [callback](V1_0::ErrorStatus errorStatus, std::string callingFunction)
Mike Kelly65c42dc2019-07-22 14:06:00 +0100244 {
245 NotifyCallbackAndCheck(callback, errorStatus, callingFunction);
246 };
247
Derek Lamberti4de83c52020-03-17 13:40:18 +0000248 CallbackContext_1_0 armnnCb;
Mike Kelly65c42dc2019-07-22 14:06:00 +0100249 armnnCb.callback = cb;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100250
251 if (m_AsyncModelExecutionEnabled)
252 {
253 ALOGV("ArmnnPreparedModel::execute(...) before ScheduleGraphForExecution");
254 ScheduleGraphForExecution(pMemPools, pInputTensors, pOutputTensors, armnnCb);
255 ALOGV("ArmnnPreparedModel::execute(...) after ScheduleGraphForExecution");
256 return V1_0::ErrorStatus::NONE;
257 }
258
Mike Kelly65c42dc2019-07-22 14:06:00 +0100259 // post the request for asynchronous execution
Finn Williamsd8fb5402021-05-19 20:52:00 +0100260 ALOGV("ArmnnPreparedModel::execute(...) before PostMsg");
Mike Kelly65c42dc2019-07-22 14:06:00 +0100261 m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, armnnCb);
262 ALOGV("ArmnnPreparedModel::execute(...) after PostMsg");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000263 return V1_0::ErrorStatus::NONE; // successfully queued
telsoa015307bc12018-03-09 13:51:08 +0000264}
265
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100266template<typename HalVersion>
267void ArmnnPreparedModel<HalVersion>::ExecuteGraph(
268 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
Derek Lamberti4de83c52020-03-17 13:40:18 +0000269 armnn::InputTensors& inputTensors,
270 armnn::OutputTensors& outputTensors,
271 CallbackContext_1_0 cb)
telsoa015307bc12018-03-09 13:51:08 +0000272{
273 ALOGV("ArmnnPreparedModel::ExecuteGraph(...)");
274
Derek Lamberti4de83c52020-03-17 13:40:18 +0000275 DumpTensorsIfRequired("Input", inputTensors);
telsoa015307bc12018-03-09 13:51:08 +0000276
277 // run it
278 try
279 {
Finn Williamsd8fb5402021-05-19 20:52:00 +0100280 armnn::Status status;
281 if (m_AsyncModelExecutionEnabled)
282 {
283 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true");
284 status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors);
285 }
286 else
287 {
288 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false");
289 status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors);
290 }
291
Matthew Bentham16196e22019-04-01 17:17:58 +0100292 if (status != armnn::Status::Success)
293 {
294 ALOGW("EnqueueWorkload failed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000295 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Matthew Bentham16196e22019-04-01 17:17:58 +0100296 return;
297 }
telsoa015307bc12018-03-09 13:51:08 +0000298 }
Kevin May7bdaac52020-02-10 12:10:07 +0000299 catch (armnn::Exception& e)
300 {
301 ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000302 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Kevin May7bdaac52020-02-10 12:10:07 +0000303 return;
304 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000305 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000306 {
Kevin May7bdaac52020-02-10 12:10:07 +0000307 ALOGE("std::exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000308 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000309 return;
310 }
311
Derek Lamberti4de83c52020-03-17 13:40:18 +0000312 DumpTensorsIfRequired("Output", outputTensors);
telsoa015307bc12018-03-09 13:51:08 +0000313
314 // Commit output buffers.
315 // Note that we update *all* pools, even if they aren't actually used as outputs -
316 // this is simpler and is what the CpuExecutor does.
317 for (android::nn::RunTimePoolInfo& pool : *pMemPools)
318 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000319 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
320 // update() has been removed and flush() added.
Sadik Armagan188675f2021-02-12 17:16:42 +0000321 #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation.
Kevin Mayec1e5b82020-02-26 17:00:39 +0000322 pool.flush();
323 #else
324 pool.update();
325 #endif
telsoa015307bc12018-03-09 13:51:08 +0000326 }
327
Kevin Mayec1e5b82020-02-26 17:00:39 +0000328 cb.callback(V1_0::ErrorStatus::NONE, "ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000329}
330
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100331template<typename HalVersion>
Matthew Bentham16196e22019-04-01 17:17:58 +0100332bool ArmnnPreparedModel<HalVersion>::ExecuteWithDummyInputs()
telsoa015307bc12018-03-09 13:51:08 +0000333{
334 std::vector<std::vector<char>> storage;
335 armnn::InputTensors inputTensors;
Kevin May42477c12020-03-26 13:34:14 +0000336 for (unsigned int i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000337 {
338 const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
339 storage.emplace_back(inputTensorInfo.GetNumBytes());
340 const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data());
341
342 inputTensors.emplace_back(i, inputTensor);
343 }
344
345 armnn::OutputTensors outputTensors;
Kevin May42477c12020-03-26 13:34:14 +0000346 for (unsigned int i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000347 {
348 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
349 storage.emplace_back(outputTensorInfo.GetNumBytes());
350 const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data());
351
352 outputTensors.emplace_back(i, outputTensor);
353 }
354
355 try
356 {
Finn Williams8fde84b2021-05-31 14:57:15 +0100357 armnn::Status status;
358 if (m_AsyncModelExecutionEnabled)
359 {
360 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true");
361 status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors);
362 }
363 else
364 {
365 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false");
366 status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors);
367 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100368 if (status != armnn::Status::Success)
369 {
370 ALOGW("ExecuteWithDummyInputs: EnqueueWorkload failed");
371 return false;
372 }
telsoa015307bc12018-03-09 13:51:08 +0000373 }
Kevin May7bdaac52020-02-10 12:10:07 +0000374 catch (armnn::Exception& e)
375 {
376 ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what());
377 return false;
378 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000379 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000380 {
Kevin May7bdaac52020-02-10 12:10:07 +0000381 ALOGE("ExecuteWithDummyInputs: std::exception caught from EnqueueWorkload: %s", e.what());
Matthew Bentham16196e22019-04-01 17:17:58 +0100382 return false;
telsoa015307bc12018-03-09 13:51:08 +0000383 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100384 return true;
telsoa015307bc12018-03-09 13:51:08 +0000385}
386
Finn Williamsd8fb5402021-05-19 20:52:00 +0100387/// Schedule the graph prepared from the request for execution
388template<typename HalVersion>
389template<typename CallbackContext>
390void ArmnnPreparedModel<HalVersion>::ScheduleGraphForExecution(
391 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
392 std::shared_ptr<armnn::InputTensors>& inputTensors,
393 std::shared_ptr<armnn::OutputTensors>& outputTensors,
394 CallbackContext callbackContext)
395{
396 ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution(...)");
397
398 DumpTensorsIfRequired("Input", *inputTensors);
399
400
401 auto tpCb = std::make_shared<
402 ArmnnThreadPoolCallback<CallbackContext_1_0>>(this,
403 pMemPools,
404 inputTensors,
405 outputTensors,
406 callbackContext);
407
Finn Williamsca3a3e02021-06-11 15:04:02 +0100408 m_Threadpool->Schedule(m_NetworkId,
409 *tpCb->m_InputTensors,
410 *tpCb->m_OutputTensors,
411 armnn::QosExecPriority::Medium,
412 tpCb);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100413 ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution end");
414}
415
416template<typename HalVersion>
417template <typename CallbackContext>
418void ArmnnPreparedModel<HalVersion>::ArmnnThreadPoolCallback<CallbackContext>::Notify(
419 armnn::Status status, armnn::InferenceTimingPair timeTaken)
420{
421 armnn::IgnoreUnused(status, timeTaken);
422 ALOGV("ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify");
423
424 m_Model->DumpTensorsIfRequired("Output", *m_OutputTensors);
425
426 // Commit output buffers.
427 // Note that we update *all* pools, even if they aren't actually used as outputs -
428 // this is simpler and is what the CpuExecutor does.
429 for (android::nn::RunTimePoolInfo& pool : *m_MemPools)
430 {
431 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
432 // update() has been removed and flush() added.
433 #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation.
434 pool.flush();
435 #else
436 pool.update();
437 #endif
438 }
439
440 m_CallbackContext.callback(V1_0::ErrorStatus::NONE, "ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify");
441 return;
442}
443
arovir01b0717b52018-09-05 17:03:25 +0100444///
445/// Class template specializations
446///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100447
arovir01b0717b52018-09-05 17:03:25 +0100448template class ArmnnPreparedModel<hal_1_0::HalPolicy>;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100449template void ArmnnPreparedModel<hal_1_0::HalPolicy>::ScheduleGraphForExecution<CallbackContext_1_0>(
450 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
451 std::shared_ptr<armnn::InputTensors>& inputTensors,
452 std::shared_ptr<armnn::OutputTensors>& outputTensors,
453 CallbackContext_1_0 callbackContext);
arovir01b0717b52018-09-05 17:03:25 +0100454
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100455#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100456template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100457#endif
458
Mike Kellyb5fdf382019-06-11 16:35:25 +0100459#ifdef ARMNN_ANDROID_NN_V1_2
460template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
461template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
462#endif
Kevin May42477c12020-03-26 13:34:14 +0000463
464#ifdef ARMNN_ANDROID_NN_V1_3
465template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
466template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
467template class ArmnnPreparedModel<hal_1_3::HalPolicy>;
468#endif
Nikhil Raj77605822018-09-03 11:25:56 +0100469} // namespace armnn_driver