blob: 563abd4490b03b4b0e42befbbda9fd05b101efc3 [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
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 <cinttypes>
16
Sadik Armagan188675f2021-02-12 17:16:42 +000017#ifdef ARMNN_ANDROID_S
18#include <LegacyUtils.h>
19#endif
20
telsoa015307bc12018-03-09 13:51:08 +000021using namespace android;
22
23namespace
24{
25using namespace armnn_driver;
26
Kevin Mayec1e5b82020-02-26 17:00:39 +000027void NotifyCallbackAndCheck(const ::android::sp<V1_0::IExecutionCallback>& callback, V1_0::ErrorStatus errorStatus,
telsoa015307bc12018-03-09 13:51:08 +000028 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 Armagan188675f2021-02-12 17:16:42 +000039bool ValidateRequestArgument(const V1_0::RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo)
telsoa015307bc12018-03-09 13:51:08 +000040{
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 Williamsa4983ce2020-07-23 12:55:12 +010052 if (requestArg.dimensions[d] != 0 && requestArg.dimensions[d] != tensorInfo.GetShape()[d])
telsoa015307bc12018-03-09 13:51:08 +000053 {
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 Armagan188675f2021-02-12 17:16:42 +000064armnn::Tensor GetTensorForRequestArgument(const V1_0::RequestArgument& requestArg,
telsoa015307bc12018-03-09 13:51:08 +000065 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
76inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index)
77{
78 return tensorNamePrefix + std::to_string(index);
79}
80
Matteo Martincighe48bdff2018-09-03 13:50:50 +010081} // anonymous namespace
telsoa015307bc12018-03-09 13:51:08 +000082
telsoa01ce3e84a2018-08-31 09:31:35 +010083using namespace android::hardware;
84
telsoa015307bc12018-03-09 13:51:08 +000085namespace armnn_driver
86{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010087template<typename HalVersion>
Derek Lamberti4de83c52020-03-17 13:40:18 +000088RequestThread<ArmnnPreparedModel, HalVersion, CallbackContext_1_0>
89 ArmnnPreparedModel<HalVersion>::m_RequestThread;
telsoa015307bc12018-03-09 13:51:08 +000090
Matteo Martincighe48bdff2018-09-03 13:50:50 +010091template<typename HalVersion>
Finn Williamsfdf2eae2021-07-08 13:07:19 +010092std::unique_ptr<armnn::Threadpool> ArmnnPreparedModel<HalVersion>::m_Threadpool(nullptr);
93
94template<typename HalVersion>
telsoa015307bc12018-03-09 13:51:08 +000095template <typename TensorBindingCollection>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010096void ArmnnPreparedModel<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix,
97 const TensorBindingCollection& tensorBindings)
telsoa015307bc12018-03-09 13:51:08 +000098{
99 if (!m_RequestInputsAndOutputsDumpDir.empty())
100 {
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100101 const std::string requestName = std::to_string(m_NetworkId) + "_" + std::to_string(m_RequestCount) + ".dump";
telsoa015307bc12018-03-09 13:51:08 +0000102 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 Martincighe48bdff2018-09-03 13:50:50 +0100112template<typename HalVersion>
113ArmnnPreparedModel<HalVersion>::ArmnnPreparedModel(armnn::NetworkId networkId,
114 armnn::IRuntime* runtime,
115 const HalModel& model,
116 const std::string& requestInputsAndOutputsDumpDir,
Finn Williamsd8fb5402021-05-19 20:52:00 +0100117 const bool gpuProfilingEnabled,
Finn Williamsca3a3e02021-06-11 15:04:02 +0100118 const bool asyncModelExecutionEnabled,
119 const unsigned int numberOfThreads)
telsoa01ce3e84a2018-08-31 09:31:35 +0100120 : m_NetworkId(networkId)
121 , m_Runtime(runtime)
122 , m_Model(model)
123 , m_RequestCount(0)
124 , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir)
125 , m_GpuProfilingEnabled(gpuProfilingEnabled)
Finn Williamsd8fb5402021-05-19 20:52:00 +0100126 , m_AsyncModelExecutionEnabled(asyncModelExecutionEnabled)
telsoa015307bc12018-03-09 13:51:08 +0000127{
telsoa01ce3e84a2018-08-31 09:31:35 +0100128 // Enable profiling if required.
129 m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100130
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100131 if (m_AsyncModelExecutionEnabled)
Finn Williamsd8fb5402021-05-19 20:52:00 +0100132 {
Finn Williamsca3a3e02021-06-11 15:04:02 +0100133 std::vector<std::shared_ptr<armnn::IWorkingMemHandle>> memHandles;
Finn Williamsd27c13b2021-06-25 10:06:09 +0100134 for (unsigned int i=0; i < numberOfThreads; ++i)
Finn Williamsca3a3e02021-06-11 15:04:02 +0100135 {
136 memHandles.emplace_back(m_Runtime->CreateWorkingMemHandle(networkId));
137 }
138
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100139 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 Williamsca3a3e02021-06-11 15:04:02 +0100148 m_WorkingMemHandle = memHandles.back();
Finn Williamsd8fb5402021-05-19 20:52:00 +0100149 }
telsoa015307bc12018-03-09 13:51:08 +0000150}
151
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100152template<typename HalVersion>
153ArmnnPreparedModel<HalVersion>::~ArmnnPreparedModel()
telsoa015307bc12018-03-09 13:51:08 +0000154{
telsoa01ce3e84a2018-08-31 09:31:35 +0100155 // Get a hold of the profiler used by this model.
156 std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId);
157
158 // Unload the network associated with this model.
telsoa015307bc12018-03-09 13:51:08 +0000159 m_Runtime->UnloadNetwork(m_NetworkId);
telsoa01ce3e84a2018-08-31 09:31:35 +0100160
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100161 // Unload the network memhandles from the threadpool
162 if (m_AsyncModelExecutionEnabled)
163 {
164 m_Threadpool->UnloadMemHandles(m_NetworkId);
165 }
166
telsoa01ce3e84a2018-08-31 09:31:35 +0100167 // Dump the profiling info to a file if required.
168 DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get());
telsoa015307bc12018-03-09 13:51:08 +0000169}
170
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100171template<typename HalVersion>
Kevin Mayec1e5b82020-02-26 17:00:39 +0000172Return<V1_0::ErrorStatus> ArmnnPreparedModel<HalVersion>::execute(
173 const V1_0::Request& request,
174 const ::android::sp<V1_0::IExecutionCallback>& callback)
telsoa015307bc12018-03-09 13:51:08 +0000175{
176 ALOGV("ArmnnPreparedModel::execute(): %s", GetModelSummary(m_Model).c_str());
177 m_RequestCount++;
178
179 if (callback.get() == nullptr) {
180 ALOGE("ArmnnPreparedModel::execute invalid callback passed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000181 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000182 }
183
184 if (!android::nn::validateRequest(request, m_Model))
185 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000186 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::INVALID_ARGUMENT, "ArmnnPreparedModel::execute");
187 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000188 }
189
190 if (!m_RequestInputsAndOutputsDumpDir.empty())
191 {
192 ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(callback.get()));
193 }
194
195 // allocate the tensors on the heap, as they are passed to the request thread
196 auto pInputTensors = std::make_shared<armnn::InputTensors>();
197 auto pOutputTensors = std::make_shared<armnn::OutputTensors>();
198
199 // map the memory pool into shared pointers
200 // use a shared memory pools vector on the heap, as it is passed to the request thread
201 auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>();
Sadik Armagan188675f2021-02-12 17:16:42 +0000202#if !defined(ARMNN_ANDROID_S)
telsoa015307bc12018-03-09 13:51:08 +0000203 if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools))
Sadik Armagan188675f2021-02-12 17:16:42 +0000204#else
205 if (!setRunTimePoolInfosFromCanonicalMemories(pMemPools.get(), uncheckedConvert(request.pools)))
206#endif
telsoa015307bc12018-03-09 13:51:08 +0000207 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000208 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
209 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000210 }
telsoa015307bc12018-03-09 13:51:08 +0000211 // add the inputs and outputs with their data
212 try
213 {
214 pInputTensors->reserve(request.inputs.size());
215 for (unsigned int i = 0; i < request.inputs.size(); i++)
216 {
217 const auto& inputArg = request.inputs[i];
218
Cathal Corbette27d4e82021-10-28 12:28:35 +0100219 armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
220 // pInputTensors (of type InputTensors) is composed of a vector of ConstTensors.
221 // Therefore, set all TensorInfo isConstant parameters of input Tensors to true.
222 inputTensorInfo.SetConstant();
telsoa015307bc12018-03-09 13:51:08 +0000223 const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools);
224 if (inputTensor.GetMemoryArea() == nullptr)
225 {
226 ALOGE("Cannot execute request. Error converting request input %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000227 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000228 }
229
230 pInputTensors->emplace_back(i, inputTensor);
231 }
232
233 pOutputTensors->reserve(request.outputs.size());
234 for (unsigned int i = 0; i < request.outputs.size(); i++)
235 {
236 const auto& outputArg = request.outputs[i];
237
238 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
239 const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools);
240 if (outputTensor.GetMemoryArea() == nullptr)
241 {
242 ALOGE("Cannot execute request. Error converting request output %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000243 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000244 }
245
246 pOutputTensors->emplace_back(i, outputTensor);
247 }
248 }
Kevin May7bdaac52020-02-10 12:10:07 +0000249 catch (armnn::Exception& e)
250 {
251 ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000252 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
253 return V1_0::ErrorStatus::GENERAL_FAILURE;
Kevin May7bdaac52020-02-10 12:10:07 +0000254 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000255 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000256 {
Kevin May7bdaac52020-02-10 12:10:07 +0000257 ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000258 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
259 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000260 }
261
Kevin Mayec1e5b82020-02-26 17:00:39 +0000262 auto cb = [callback](V1_0::ErrorStatus errorStatus, std::string callingFunction)
Mike Kelly65c42dc2019-07-22 14:06:00 +0100263 {
264 NotifyCallbackAndCheck(callback, errorStatus, callingFunction);
265 };
266
Derek Lamberti4de83c52020-03-17 13:40:18 +0000267 CallbackContext_1_0 armnnCb;
Mike Kelly65c42dc2019-07-22 14:06:00 +0100268 armnnCb.callback = cb;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100269
270 if (m_AsyncModelExecutionEnabled)
271 {
272 ALOGV("ArmnnPreparedModel::execute(...) before ScheduleGraphForExecution");
273 ScheduleGraphForExecution(pMemPools, pInputTensors, pOutputTensors, armnnCb);
274 ALOGV("ArmnnPreparedModel::execute(...) after ScheduleGraphForExecution");
275 return V1_0::ErrorStatus::NONE;
276 }
277
Mike Kelly65c42dc2019-07-22 14:06:00 +0100278 // post the request for asynchronous execution
Finn Williamsd8fb5402021-05-19 20:52:00 +0100279 ALOGV("ArmnnPreparedModel::execute(...) before PostMsg");
Mike Kelly65c42dc2019-07-22 14:06:00 +0100280 m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, armnnCb);
281 ALOGV("ArmnnPreparedModel::execute(...) after PostMsg");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000282 return V1_0::ErrorStatus::NONE; // successfully queued
telsoa015307bc12018-03-09 13:51:08 +0000283}
284
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100285template<typename HalVersion>
286void ArmnnPreparedModel<HalVersion>::ExecuteGraph(
287 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
Derek Lamberti4de83c52020-03-17 13:40:18 +0000288 armnn::InputTensors& inputTensors,
289 armnn::OutputTensors& outputTensors,
290 CallbackContext_1_0 cb)
telsoa015307bc12018-03-09 13:51:08 +0000291{
292 ALOGV("ArmnnPreparedModel::ExecuteGraph(...)");
293
Derek Lamberti4de83c52020-03-17 13:40:18 +0000294 DumpTensorsIfRequired("Input", inputTensors);
telsoa015307bc12018-03-09 13:51:08 +0000295
296 // run it
297 try
298 {
Finn Williamsd8fb5402021-05-19 20:52:00 +0100299 armnn::Status status;
300 if (m_AsyncModelExecutionEnabled)
301 {
302 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true");
303 status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors);
304 }
305 else
306 {
307 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false");
308 status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors);
309 }
310
Matthew Bentham16196e22019-04-01 17:17:58 +0100311 if (status != armnn::Status::Success)
312 {
313 ALOGW("EnqueueWorkload failed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000314 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Matthew Bentham16196e22019-04-01 17:17:58 +0100315 return;
316 }
telsoa015307bc12018-03-09 13:51:08 +0000317 }
Kevin May7bdaac52020-02-10 12:10:07 +0000318 catch (armnn::Exception& e)
319 {
320 ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000321 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Kevin May7bdaac52020-02-10 12:10:07 +0000322 return;
323 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000324 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000325 {
Kevin May7bdaac52020-02-10 12:10:07 +0000326 ALOGE("std::exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000327 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000328 return;
329 }
330
Derek Lamberti4de83c52020-03-17 13:40:18 +0000331 DumpTensorsIfRequired("Output", outputTensors);
telsoa015307bc12018-03-09 13:51:08 +0000332
333 // Commit output buffers.
334 // Note that we update *all* pools, even if they aren't actually used as outputs -
335 // this is simpler and is what the CpuExecutor does.
336 for (android::nn::RunTimePoolInfo& pool : *pMemPools)
337 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000338 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
339 // update() has been removed and flush() added.
Sadik Armagan188675f2021-02-12 17:16:42 +0000340 #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation.
Kevin Mayec1e5b82020-02-26 17:00:39 +0000341 pool.flush();
342 #else
343 pool.update();
344 #endif
telsoa015307bc12018-03-09 13:51:08 +0000345 }
346
Kevin Mayec1e5b82020-02-26 17:00:39 +0000347 cb.callback(V1_0::ErrorStatus::NONE, "ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000348}
349
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100350template<typename HalVersion>
Matthew Bentham16196e22019-04-01 17:17:58 +0100351bool ArmnnPreparedModel<HalVersion>::ExecuteWithDummyInputs()
telsoa015307bc12018-03-09 13:51:08 +0000352{
353 std::vector<std::vector<char>> storage;
354 armnn::InputTensors inputTensors;
Kevin May42477c12020-03-26 13:34:14 +0000355 for (unsigned int i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000356 {
Cathal Corbette27d4e82021-10-28 12:28:35 +0100357 armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
358 // pInputTensors (of type InputTensors) is composed of a vector of ConstTensors.
359 // Therefore, set all TensorInfo isConstant parameters of input Tensors to true.
360 inputTensorInfo.SetConstant();
361
telsoa015307bc12018-03-09 13:51:08 +0000362 storage.emplace_back(inputTensorInfo.GetNumBytes());
363 const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data());
364
365 inputTensors.emplace_back(i, inputTensor);
366 }
367
368 armnn::OutputTensors outputTensors;
Kevin May42477c12020-03-26 13:34:14 +0000369 for (unsigned int i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000370 {
371 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
372 storage.emplace_back(outputTensorInfo.GetNumBytes());
373 const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data());
374
375 outputTensors.emplace_back(i, outputTensor);
376 }
377
378 try
379 {
Finn Williams8fde84b2021-05-31 14:57:15 +0100380 armnn::Status status;
381 if (m_AsyncModelExecutionEnabled)
382 {
383 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true");
384 status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors);
385 }
386 else
387 {
388 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false");
389 status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors);
390 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100391 if (status != armnn::Status::Success)
392 {
393 ALOGW("ExecuteWithDummyInputs: EnqueueWorkload failed");
394 return false;
395 }
telsoa015307bc12018-03-09 13:51:08 +0000396 }
Kevin May7bdaac52020-02-10 12:10:07 +0000397 catch (armnn::Exception& e)
398 {
399 ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what());
400 return false;
401 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000402 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000403 {
Kevin May7bdaac52020-02-10 12:10:07 +0000404 ALOGE("ExecuteWithDummyInputs: std::exception caught from EnqueueWorkload: %s", e.what());
Matthew Bentham16196e22019-04-01 17:17:58 +0100405 return false;
telsoa015307bc12018-03-09 13:51:08 +0000406 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100407 return true;
telsoa015307bc12018-03-09 13:51:08 +0000408}
409
Finn Williamsd8fb5402021-05-19 20:52:00 +0100410/// Schedule the graph prepared from the request for execution
411template<typename HalVersion>
412template<typename CallbackContext>
413void ArmnnPreparedModel<HalVersion>::ScheduleGraphForExecution(
414 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
415 std::shared_ptr<armnn::InputTensors>& inputTensors,
416 std::shared_ptr<armnn::OutputTensors>& outputTensors,
417 CallbackContext callbackContext)
418{
419 ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution(...)");
420
421 DumpTensorsIfRequired("Input", *inputTensors);
422
423
424 auto tpCb = std::make_shared<
425 ArmnnThreadPoolCallback<CallbackContext_1_0>>(this,
426 pMemPools,
427 inputTensors,
428 outputTensors,
429 callbackContext);
430
Finn Williamsca3a3e02021-06-11 15:04:02 +0100431 m_Threadpool->Schedule(m_NetworkId,
432 *tpCb->m_InputTensors,
433 *tpCb->m_OutputTensors,
434 armnn::QosExecPriority::Medium,
435 tpCb);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100436 ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution end");
437}
438
439template<typename HalVersion>
440template <typename CallbackContext>
441void ArmnnPreparedModel<HalVersion>::ArmnnThreadPoolCallback<CallbackContext>::Notify(
442 armnn::Status status, armnn::InferenceTimingPair timeTaken)
443{
444 armnn::IgnoreUnused(status, timeTaken);
445 ALOGV("ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify");
446
447 m_Model->DumpTensorsIfRequired("Output", *m_OutputTensors);
448
449 // Commit output buffers.
450 // Note that we update *all* pools, even if they aren't actually used as outputs -
451 // this is simpler and is what the CpuExecutor does.
452 for (android::nn::RunTimePoolInfo& pool : *m_MemPools)
453 {
454 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
455 // update() has been removed and flush() added.
456 #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation.
457 pool.flush();
458 #else
459 pool.update();
460 #endif
461 }
462
463 m_CallbackContext.callback(V1_0::ErrorStatus::NONE, "ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify");
464 return;
465}
466
arovir01b0717b52018-09-05 17:03:25 +0100467///
468/// Class template specializations
469///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100470
arovir01b0717b52018-09-05 17:03:25 +0100471template class ArmnnPreparedModel<hal_1_0::HalPolicy>;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100472template void ArmnnPreparedModel<hal_1_0::HalPolicy>::ScheduleGraphForExecution<CallbackContext_1_0>(
473 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
474 std::shared_ptr<armnn::InputTensors>& inputTensors,
475 std::shared_ptr<armnn::OutputTensors>& outputTensors,
476 CallbackContext_1_0 callbackContext);
arovir01b0717b52018-09-05 17:03:25 +0100477
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100478#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100479template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100480#endif
481
Mike Kellyb5fdf382019-06-11 16:35:25 +0100482#ifdef ARMNN_ANDROID_NN_V1_2
483template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
484template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
485#endif
Kevin May42477c12020-03-26 13:34:14 +0000486
487#ifdef ARMNN_ANDROID_NN_V1_3
488template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
489template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
490template class ArmnnPreparedModel<hal_1_3::HalPolicy>;
491#endif
Nikhil Raj77605822018-09-03 11:25:56 +0100492} // namespace armnn_driver