blob: d87f9f82e44cfb4f4d6e7db270e6a0b2701b7b53 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
Mike Kellyde547162023-03-08 10:08:20 +00002// Copyright © 2017-2023 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 Prangnawaratd1a947f2022-02-07 13:12:24 +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
Colm Donelan0fc16c62022-03-16 11:54:13 +000017#include <chrono>
telsoa015307bc12018-03-09 13:51:08 +000018#include <cinttypes>
19
Sadik Armagan188675f2021-02-12 17:16:42 +000020#ifdef ARMNN_ANDROID_S
21#include <LegacyUtils.h>
22#endif
23
telsoa015307bc12018-03-09 13:51:08 +000024using namespace android;
25
26namespace
27{
28using namespace armnn_driver;
29
Kevin Mayec1e5b82020-02-26 17:00:39 +000030void NotifyCallbackAndCheck(const ::android::sp<V1_0::IExecutionCallback>& callback, V1_0::ErrorStatus errorStatus,
telsoa015307bc12018-03-09 13:51:08 +000031 std::string callingFunction)
32{
33 Return<void> returned = callback->notify(errorStatus);
34 // This check is required, if the callback fails and it isn't checked it will bring down the service
35 if (!returned.isOk())
36 {
37 ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s",
38 callingFunction.c_str(), returned.description().c_str());
39 }
40}
41
Sadik Armagan188675f2021-02-12 17:16:42 +000042bool ValidateRequestArgument(const V1_0::RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo)
telsoa015307bc12018-03-09 13:51:08 +000043{
44 if (requestArg.dimensions.size() != 0)
45 {
46 if (requestArg.dimensions.size() != tensorInfo.GetNumDimensions())
47 {
48 ALOGE("Mismatched dimensions (request argument: %zu, expected: %u)",
49 requestArg.dimensions.size(), tensorInfo.GetNumDimensions());
50 return false;
51 }
52
53 for (unsigned int d = 0; d < tensorInfo.GetNumDimensions(); ++d)
54 {
Finn Williamsa4983ce2020-07-23 12:55:12 +010055 if (requestArg.dimensions[d] != 0 && requestArg.dimensions[d] != tensorInfo.GetShape()[d])
telsoa015307bc12018-03-09 13:51:08 +000056 {
57 ALOGE("Mismatched size for dimension %d (request argument: %u, expected %u)",
58 d, requestArg.dimensions[d], tensorInfo.GetShape()[d]);
59 return false;
60 }
61 }
62 }
63
64 return true;
65}
66
Sadik Armagan188675f2021-02-12 17:16:42 +000067armnn::Tensor GetTensorForRequestArgument(const V1_0::RequestArgument& requestArg,
telsoa015307bc12018-03-09 13:51:08 +000068 const armnn::TensorInfo& tensorInfo,
69 const std::vector<::android::nn::RunTimePoolInfo>& requestPools)
70{
71 if (!ValidateRequestArgument(requestArg, tensorInfo))
72 {
73 return armnn::Tensor();
74 }
75
76 return armnn::Tensor(tensorInfo, GetMemoryFromPool(requestArg.location, requestPools));
77}
78
79inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index)
80{
81 return tensorNamePrefix + std::to_string(index);
82}
83
Matteo Martincighe48bdff2018-09-03 13:50:50 +010084} // anonymous namespace
telsoa015307bc12018-03-09 13:51:08 +000085
telsoa01ce3e84a2018-08-31 09:31:35 +010086using namespace android::hardware;
87
telsoa015307bc12018-03-09 13:51:08 +000088namespace armnn_driver
89{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010090template<typename HalVersion>
Derek Lamberti4de83c52020-03-17 13:40:18 +000091RequestThread<ArmnnPreparedModel, HalVersion, CallbackContext_1_0>
92 ArmnnPreparedModel<HalVersion>::m_RequestThread;
telsoa015307bc12018-03-09 13:51:08 +000093
Matteo Martincighe48bdff2018-09-03 13:50:50 +010094template<typename HalVersion>
Finn Williamsfdf2eae2021-07-08 13:07:19 +010095std::unique_ptr<armnn::Threadpool> ArmnnPreparedModel<HalVersion>::m_Threadpool(nullptr);
96
97template<typename HalVersion>
telsoa015307bc12018-03-09 13:51:08 +000098template <typename TensorBindingCollection>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010099void ArmnnPreparedModel<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix,
100 const TensorBindingCollection& tensorBindings)
telsoa015307bc12018-03-09 13:51:08 +0000101{
102 if (!m_RequestInputsAndOutputsDumpDir.empty())
103 {
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100104 const std::string requestName = std::to_string(m_NetworkId) + "_" + std::to_string(m_RequestCount) + ".dump";
telsoa015307bc12018-03-09 13:51:08 +0000105 for (std::size_t i = 0u; i < tensorBindings.size(); ++i)
106 {
107 DumpTensor(m_RequestInputsAndOutputsDumpDir,
108 requestName,
109 BuildTensorName(tensorNamePrefix, i),
110 tensorBindings[i].second);
111 }
112 }
113}
114
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100115template<typename HalVersion>
116ArmnnPreparedModel<HalVersion>::ArmnnPreparedModel(armnn::NetworkId networkId,
117 armnn::IRuntime* runtime,
118 const HalModel& model,
119 const std::string& requestInputsAndOutputsDumpDir,
Finn Williamsd8fb5402021-05-19 20:52:00 +0100120 const bool gpuProfilingEnabled,
Finn Williamsca3a3e02021-06-11 15:04:02 +0100121 const bool asyncModelExecutionEnabled,
Narumol Prangnawaratd1a947f2022-02-07 13:12:24 +0000122 const unsigned int numberOfThreads,
123 const bool importEnabled,
124 const bool exportEnabled)
telsoa01ce3e84a2018-08-31 09:31:35 +0100125 : m_NetworkId(networkId)
126 , m_Runtime(runtime)
127 , m_Model(model)
128 , m_RequestCount(0)
129 , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir)
130 , m_GpuProfilingEnabled(gpuProfilingEnabled)
Finn Williamsd8fb5402021-05-19 20:52:00 +0100131 , m_AsyncModelExecutionEnabled(asyncModelExecutionEnabled)
Narumol Prangnawaratd1a947f2022-02-07 13:12:24 +0000132 , m_EnableImport(importEnabled)
133 , m_EnableExport(exportEnabled)
telsoa015307bc12018-03-09 13:51:08 +0000134{
telsoa01ce3e84a2018-08-31 09:31:35 +0100135 // Enable profiling if required.
136 m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100137
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100138 if (m_AsyncModelExecutionEnabled)
Finn Williamsd8fb5402021-05-19 20:52:00 +0100139 {
Finn Williamsca3a3e02021-06-11 15:04:02 +0100140 std::vector<std::shared_ptr<armnn::IWorkingMemHandle>> memHandles;
Finn Williamsd27c13b2021-06-25 10:06:09 +0100141 for (unsigned int i=0; i < numberOfThreads; ++i)
Finn Williamsca3a3e02021-06-11 15:04:02 +0100142 {
143 memHandles.emplace_back(m_Runtime->CreateWorkingMemHandle(networkId));
144 }
145
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100146 if (!m_Threadpool)
147 {
148 m_Threadpool = std::make_unique<armnn::Threadpool>(numberOfThreads, runtime, memHandles);
149 }
150 else
151 {
152 m_Threadpool->LoadMemHandles(memHandles);
153 }
154
Finn Williamsca3a3e02021-06-11 15:04:02 +0100155 m_WorkingMemHandle = memHandles.back();
Finn Williamsd8fb5402021-05-19 20:52:00 +0100156 }
telsoa015307bc12018-03-09 13:51:08 +0000157}
158
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100159template<typename HalVersion>
160ArmnnPreparedModel<HalVersion>::~ArmnnPreparedModel()
telsoa015307bc12018-03-09 13:51:08 +0000161{
telsoa01ce3e84a2018-08-31 09:31:35 +0100162 // Get a hold of the profiler used by this model.
163 std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId);
Colm Donelan2048b682022-02-15 14:59:08 +0000164 if (profiler && m_GpuProfilingEnabled)
165 {
166 // Dump the profiling info to a file if required.
167 DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId,
168 profiler.get());
169 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100170
171 // Unload the network associated with this model.
telsoa015307bc12018-03-09 13:51:08 +0000172 m_Runtime->UnloadNetwork(m_NetworkId);
telsoa01ce3e84a2018-08-31 09:31:35 +0100173
Finn Williamsfdf2eae2021-07-08 13:07:19 +0100174 // Unload the network memhandles from the threadpool
175 if (m_AsyncModelExecutionEnabled)
176 {
177 m_Threadpool->UnloadMemHandles(m_NetworkId);
178 }
telsoa015307bc12018-03-09 13:51:08 +0000179}
180
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100181template<typename HalVersion>
Kevin Mayec1e5b82020-02-26 17:00:39 +0000182Return<V1_0::ErrorStatus> ArmnnPreparedModel<HalVersion>::execute(
183 const V1_0::Request& request,
184 const ::android::sp<V1_0::IExecutionCallback>& callback)
telsoa015307bc12018-03-09 13:51:08 +0000185{
186 ALOGV("ArmnnPreparedModel::execute(): %s", GetModelSummary(m_Model).c_str());
187 m_RequestCount++;
188
189 if (callback.get() == nullptr) {
190 ALOGE("ArmnnPreparedModel::execute invalid callback passed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000191 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000192 }
193
194 if (!android::nn::validateRequest(request, m_Model))
195 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000196 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::INVALID_ARGUMENT, "ArmnnPreparedModel::execute");
197 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000198 }
199
200 if (!m_RequestInputsAndOutputsDumpDir.empty())
201 {
202 ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(callback.get()));
203 }
204
205 // allocate the tensors on the heap, as they are passed to the request thread
206 auto pInputTensors = std::make_shared<armnn::InputTensors>();
207 auto pOutputTensors = std::make_shared<armnn::OutputTensors>();
208
209 // map the memory pool into shared pointers
210 // use a shared memory pools vector on the heap, as it is passed to the request thread
211 auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>();
Sadik Armagan188675f2021-02-12 17:16:42 +0000212#if !defined(ARMNN_ANDROID_S)
telsoa015307bc12018-03-09 13:51:08 +0000213 if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools))
Sadik Armagan188675f2021-02-12 17:16:42 +0000214#else
215 if (!setRunTimePoolInfosFromCanonicalMemories(pMemPools.get(), uncheckedConvert(request.pools)))
216#endif
telsoa015307bc12018-03-09 13:51:08 +0000217 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000218 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
219 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000220 }
Mike Kellyde547162023-03-08 10:08:20 +0000221
telsoa015307bc12018-03-09 13:51:08 +0000222 // add the inputs and outputs with their data
223 try
224 {
225 pInputTensors->reserve(request.inputs.size());
226 for (unsigned int i = 0; i < request.inputs.size(); i++)
227 {
228 const auto& inputArg = request.inputs[i];
Cathal Corbette27d4e82021-10-28 12:28:35 +0100229 armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
230 // pInputTensors (of type InputTensors) is composed of a vector of ConstTensors.
231 // Therefore, set all TensorInfo isConstant parameters of input Tensors to true.
232 inputTensorInfo.SetConstant();
Mike Kellyde547162023-03-08 10:08:20 +0000233 auto result = ValidateRequestArgument<V1_0::ErrorStatus, V1_0::Request>(request,
234 inputTensorInfo,
235 inputArg,
236 "input");
237 if (result != V1_0::ErrorStatus::NONE)
238 {
239 return result;
240 }
241
telsoa015307bc12018-03-09 13:51:08 +0000242 const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools);
243 if (inputTensor.GetMemoryArea() == nullptr)
244 {
245 ALOGE("Cannot execute request. Error converting request input %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000246 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000247 }
248
249 pInputTensors->emplace_back(i, inputTensor);
250 }
251
252 pOutputTensors->reserve(request.outputs.size());
253 for (unsigned int i = 0; i < request.outputs.size(); i++)
254 {
255 const auto& outputArg = request.outputs[i];
telsoa015307bc12018-03-09 13:51:08 +0000256 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
Mike Kellyde547162023-03-08 10:08:20 +0000257 auto result = ValidateRequestArgument<V1_0::ErrorStatus, V1_0::Request>(request,
258 outputTensorInfo,
259 outputArg,
260 "output");
261
262 if (result != V1_0::ErrorStatus::NONE)
263 {
264 return result;
265 }
266
telsoa015307bc12018-03-09 13:51:08 +0000267 const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools);
268 if (outputTensor.GetMemoryArea() == nullptr)
269 {
270 ALOGE("Cannot execute request. Error converting request output %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000271 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000272 }
273
274 pOutputTensors->emplace_back(i, outputTensor);
275 }
276 }
Kevin May7bdaac52020-02-10 12:10:07 +0000277 catch (armnn::Exception& e)
278 {
279 ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000280 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
281 return V1_0::ErrorStatus::GENERAL_FAILURE;
Kevin May7bdaac52020-02-10 12:10:07 +0000282 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000283 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000284 {
Kevin May7bdaac52020-02-10 12:10:07 +0000285 ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000286 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
287 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000288 }
289
Kevin Mayec1e5b82020-02-26 17:00:39 +0000290 auto cb = [callback](V1_0::ErrorStatus errorStatus, std::string callingFunction)
Mike Kelly65c42dc2019-07-22 14:06:00 +0100291 {
292 NotifyCallbackAndCheck(callback, errorStatus, callingFunction);
293 };
294
Derek Lamberti4de83c52020-03-17 13:40:18 +0000295 CallbackContext_1_0 armnnCb;
Mike Kelly65c42dc2019-07-22 14:06:00 +0100296 armnnCb.callback = cb;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100297
298 if (m_AsyncModelExecutionEnabled)
299 {
300 ALOGV("ArmnnPreparedModel::execute(...) before ScheduleGraphForExecution");
301 ScheduleGraphForExecution(pMemPools, pInputTensors, pOutputTensors, armnnCb);
302 ALOGV("ArmnnPreparedModel::execute(...) after ScheduleGraphForExecution");
303 return V1_0::ErrorStatus::NONE;
304 }
305
Mike Kelly65c42dc2019-07-22 14:06:00 +0100306 // post the request for asynchronous execution
Finn Williamsd8fb5402021-05-19 20:52:00 +0100307 ALOGV("ArmnnPreparedModel::execute(...) before PostMsg");
Mike Kelly65c42dc2019-07-22 14:06:00 +0100308 m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, armnnCb);
309 ALOGV("ArmnnPreparedModel::execute(...) after PostMsg");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000310 return V1_0::ErrorStatus::NONE; // successfully queued
telsoa015307bc12018-03-09 13:51:08 +0000311}
312
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100313template<typename HalVersion>
314void ArmnnPreparedModel<HalVersion>::ExecuteGraph(
315 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
Derek Lamberti4de83c52020-03-17 13:40:18 +0000316 armnn::InputTensors& inputTensors,
317 armnn::OutputTensors& outputTensors,
318 CallbackContext_1_0 cb)
telsoa015307bc12018-03-09 13:51:08 +0000319{
320 ALOGV("ArmnnPreparedModel::ExecuteGraph(...)");
Colm Donelan0fc16c62022-03-16 11:54:13 +0000321 // Capture the graph execution start time.
322 std::chrono::time_point<std::chrono::system_clock> graphExecutionStart = std::chrono::system_clock::now();
telsoa015307bc12018-03-09 13:51:08 +0000323
Derek Lamberti4de83c52020-03-17 13:40:18 +0000324 DumpTensorsIfRequired("Input", inputTensors);
telsoa015307bc12018-03-09 13:51:08 +0000325
326 // run it
327 try
328 {
Finn Williamsd8fb5402021-05-19 20:52:00 +0100329 armnn::Status status;
330 if (m_AsyncModelExecutionEnabled)
331 {
332 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true");
333 status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors);
334 }
335 else
336 {
337 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false");
Narumol Prangnawaratd1a947f2022-02-07 13:12:24 +0000338 // Create a vector of Input and Output Ids which can be imported. An empty vector means all will be copied.
339 std::vector<armnn::ImportedInputId> importedInputIds;
340 if (m_EnableImport)
341 {
342 importedInputIds = m_Runtime->ImportInputs(m_NetworkId, inputTensors, armnn::MemorySource::Malloc);
343 }
344 std::vector<armnn::ImportedOutputId> importedOutputIds;
345 if (m_EnableExport)
346 {
347 importedOutputIds = m_Runtime->ImportOutputs(m_NetworkId, outputTensors, armnn::MemorySource::Malloc);
348 }
349 status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors,
350 importedInputIds, importedOutputIds);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100351 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100352 if (status != armnn::Status::Success)
353 {
354 ALOGW("EnqueueWorkload failed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000355 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Matthew Bentham16196e22019-04-01 17:17:58 +0100356 return;
357 }
telsoa015307bc12018-03-09 13:51:08 +0000358 }
Kevin May7bdaac52020-02-10 12:10:07 +0000359 catch (armnn::Exception& e)
360 {
361 ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000362 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Kevin May7bdaac52020-02-10 12:10:07 +0000363 return;
364 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000365 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000366 {
Kevin May7bdaac52020-02-10 12:10:07 +0000367 ALOGE("std::exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000368 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000369 return;
370 }
371
Derek Lamberti4de83c52020-03-17 13:40:18 +0000372 DumpTensorsIfRequired("Output", outputTensors);
telsoa015307bc12018-03-09 13:51:08 +0000373
374 // Commit output buffers.
375 // Note that we update *all* pools, even if they aren't actually used as outputs -
376 // this is simpler and is what the CpuExecutor does.
377 for (android::nn::RunTimePoolInfo& pool : *pMemPools)
378 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000379 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
380 // update() has been removed and flush() added.
Sadik Armagan188675f2021-02-12 17:16:42 +0000381 #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation.
Kevin Mayec1e5b82020-02-26 17:00:39 +0000382 pool.flush();
383 #else
384 pool.update();
385 #endif
telsoa015307bc12018-03-09 13:51:08 +0000386 }
387
Colm Donelan0fc16c62022-03-16 11:54:13 +0000388 // Log the total time in this call. This is a good number to compare to that printed out by
389 // RuntimeImpl::EnqueueWorkload. The difference should be the execution overhead of the driver.
390 ALOGI("ArmnnPreparedModel::ExecuteGraph Execution time = %lld µs",
391 std::chrono::duration_cast<std::chrono::microseconds>
392 (std::chrono::system_clock::now() - graphExecutionStart).count());
393
Kevin Mayec1e5b82020-02-26 17:00:39 +0000394 cb.callback(V1_0::ErrorStatus::NONE, "ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000395}
396
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100397template<typename HalVersion>
Matthew Bentham16196e22019-04-01 17:17:58 +0100398bool ArmnnPreparedModel<HalVersion>::ExecuteWithDummyInputs()
telsoa015307bc12018-03-09 13:51:08 +0000399{
400 std::vector<std::vector<char>> storage;
401 armnn::InputTensors inputTensors;
Kevin May42477c12020-03-26 13:34:14 +0000402 for (unsigned int i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000403 {
Cathal Corbette27d4e82021-10-28 12:28:35 +0100404 armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
405 // pInputTensors (of type InputTensors) is composed of a vector of ConstTensors.
406 // Therefore, set all TensorInfo isConstant parameters of input Tensors to true.
407 inputTensorInfo.SetConstant();
408
telsoa015307bc12018-03-09 13:51:08 +0000409 storage.emplace_back(inputTensorInfo.GetNumBytes());
410 const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data());
411
412 inputTensors.emplace_back(i, inputTensor);
413 }
414
415 armnn::OutputTensors outputTensors;
Kevin May42477c12020-03-26 13:34:14 +0000416 for (unsigned int i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000417 {
418 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
419 storage.emplace_back(outputTensorInfo.GetNumBytes());
420 const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data());
421
422 outputTensors.emplace_back(i, outputTensor);
423 }
424
425 try
426 {
Finn Williams8fde84b2021-05-31 14:57:15 +0100427 armnn::Status status;
428 if (m_AsyncModelExecutionEnabled)
429 {
430 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled true");
431 status = m_Runtime->Execute(*m_WorkingMemHandle, inputTensors, outputTensors);
432 }
433 else
434 {
435 ALOGW("ArmnnPreparedModel::ExecuteGraph m_AsyncModelExecutionEnabled false");
Narumol Prangnawaratd1a947f2022-02-07 13:12:24 +0000436 // Create a vector of Input and Output Ids which can be imported. An empty vector means all will be copied.
437 std::vector<armnn::ImportedInputId> importedInputIds;
438 if (m_EnableImport)
439 {
440 importedInputIds = m_Runtime->ImportInputs(m_NetworkId, inputTensors, armnn::MemorySource::Malloc);
441 }
442 std::vector<armnn::ImportedOutputId> importedOutputIds;
443 if (m_EnableExport)
444 {
445 importedOutputIds = m_Runtime->ImportOutputs(m_NetworkId, outputTensors, armnn::MemorySource::Malloc);
446 }
447 status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors,
448 importedInputIds, importedOutputIds);
Finn Williams8fde84b2021-05-31 14:57:15 +0100449 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100450 if (status != armnn::Status::Success)
451 {
452 ALOGW("ExecuteWithDummyInputs: EnqueueWorkload failed");
453 return false;
454 }
telsoa015307bc12018-03-09 13:51:08 +0000455 }
Kevin May7bdaac52020-02-10 12:10:07 +0000456 catch (armnn::Exception& e)
457 {
458 ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what());
459 return false;
460 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000461 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000462 {
Kevin May7bdaac52020-02-10 12:10:07 +0000463 ALOGE("ExecuteWithDummyInputs: std::exception caught from EnqueueWorkload: %s", e.what());
Matthew Bentham16196e22019-04-01 17:17:58 +0100464 return false;
telsoa015307bc12018-03-09 13:51:08 +0000465 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100466 return true;
telsoa015307bc12018-03-09 13:51:08 +0000467}
468
Finn Williamsd8fb5402021-05-19 20:52:00 +0100469/// Schedule the graph prepared from the request for execution
470template<typename HalVersion>
471template<typename CallbackContext>
472void ArmnnPreparedModel<HalVersion>::ScheduleGraphForExecution(
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 callbackContext)
477{
478 ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution(...)");
479
480 DumpTensorsIfRequired("Input", *inputTensors);
481
482
483 auto tpCb = std::make_shared<
484 ArmnnThreadPoolCallback<CallbackContext_1_0>>(this,
485 pMemPools,
486 inputTensors,
487 outputTensors,
488 callbackContext);
489
Finn Williamsca3a3e02021-06-11 15:04:02 +0100490 m_Threadpool->Schedule(m_NetworkId,
491 *tpCb->m_InputTensors,
492 *tpCb->m_OutputTensors,
493 armnn::QosExecPriority::Medium,
494 tpCb);
Finn Williamsd8fb5402021-05-19 20:52:00 +0100495 ALOGV("ArmnnPreparedModel::ScheduleGraphForExecution end");
496}
497
498template<typename HalVersion>
499template <typename CallbackContext>
500void ArmnnPreparedModel<HalVersion>::ArmnnThreadPoolCallback<CallbackContext>::Notify(
501 armnn::Status status, armnn::InferenceTimingPair timeTaken)
502{
503 armnn::IgnoreUnused(status, timeTaken);
504 ALOGV("ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify");
505
506 m_Model->DumpTensorsIfRequired("Output", *m_OutputTensors);
507
508 // Commit output buffers.
509 // Note that we update *all* pools, even if they aren't actually used as outputs -
510 // this is simpler and is what the CpuExecutor does.
511 for (android::nn::RunTimePoolInfo& pool : *m_MemPools)
512 {
513 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
514 // update() has been removed and flush() added.
515 #if defined(ARMNN_ANDROID_R) || defined(ARMNN_ANDROID_S) // Use the new Android implementation.
516 pool.flush();
517 #else
518 pool.update();
519 #endif
520 }
521
522 m_CallbackContext.callback(V1_0::ErrorStatus::NONE, "ArmnnPreparedModel::ArmnnThreadPoolCallback_1_2 Notify");
523 return;
524}
525
arovir01b0717b52018-09-05 17:03:25 +0100526///
527/// Class template specializations
528///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100529
arovir01b0717b52018-09-05 17:03:25 +0100530template class ArmnnPreparedModel<hal_1_0::HalPolicy>;
Finn Williamsd8fb5402021-05-19 20:52:00 +0100531template void ArmnnPreparedModel<hal_1_0::HalPolicy>::ScheduleGraphForExecution<CallbackContext_1_0>(
532 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
533 std::shared_ptr<armnn::InputTensors>& inputTensors,
534 std::shared_ptr<armnn::OutputTensors>& outputTensors,
535 CallbackContext_1_0 callbackContext);
arovir01b0717b52018-09-05 17:03:25 +0100536
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100537#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100538template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100539#endif
540
Mike Kellyb5fdf382019-06-11 16:35:25 +0100541#ifdef ARMNN_ANDROID_NN_V1_2
542template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
543template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
544#endif
Kevin May42477c12020-03-26 13:34:14 +0000545
546#ifdef ARMNN_ANDROID_NN_V1_3
547template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
548template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
549template class ArmnnPreparedModel<hal_1_3::HalPolicy>;
550#endif
Nikhil Raj77605822018-09-03 11:25:56 +0100551} // namespace armnn_driver