blob: 2cd560d76d9592893d18a45ca1c27d444b7d7db8 [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
11#include <boost/format.hpp>
12#include <log/log.h>
13#include <OperationsUtils.h>
surmeh01deb3bdb2018-07-05 12:06:04 +010014#include <ValidateHal.h>
Kevin Mayec1e5b82020-02-26 17:00:39 +000015
surmeh01deb3bdb2018-07-05 12:06:04 +010016
telsoa015307bc12018-03-09 13:51:08 +000017#include <cassert>
18#include <cinttypes>
19
20using namespace android;
21
22namespace
23{
24using namespace armnn_driver;
25
Kevin Mayec1e5b82020-02-26 17:00:39 +000026void NotifyCallbackAndCheck(const ::android::sp<V1_0::IExecutionCallback>& callback, V1_0::ErrorStatus errorStatus,
telsoa015307bc12018-03-09 13:51:08 +000027 std::string callingFunction)
28{
29 Return<void> returned = callback->notify(errorStatus);
30 // This check is required, if the callback fails and it isn't checked it will bring down the service
31 if (!returned.isOk())
32 {
33 ALOGE("ArmnnDriver::%s: hidl callback failed to return properly: %s",
34 callingFunction.c_str(), returned.description().c_str());
35 }
36}
37
38bool ValidateRequestArgument(const RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo)
39{
40 if (requestArg.dimensions.size() != 0)
41 {
42 if (requestArg.dimensions.size() != tensorInfo.GetNumDimensions())
43 {
44 ALOGE("Mismatched dimensions (request argument: %zu, expected: %u)",
45 requestArg.dimensions.size(), tensorInfo.GetNumDimensions());
46 return false;
47 }
48
49 for (unsigned int d = 0; d < tensorInfo.GetNumDimensions(); ++d)
50 {
51 if (requestArg.dimensions[d] != tensorInfo.GetShape()[d])
52 {
53 ALOGE("Mismatched size for dimension %d (request argument: %u, expected %u)",
54 d, requestArg.dimensions[d], tensorInfo.GetShape()[d]);
55 return false;
56 }
57 }
58 }
59
60 return true;
61}
62
63armnn::Tensor GetTensorForRequestArgument(const RequestArgument& requestArg,
64 const armnn::TensorInfo& tensorInfo,
65 const std::vector<::android::nn::RunTimePoolInfo>& requestPools)
66{
67 if (!ValidateRequestArgument(requestArg, tensorInfo))
68 {
69 return armnn::Tensor();
70 }
71
72 return armnn::Tensor(tensorInfo, GetMemoryFromPool(requestArg.location, requestPools));
73}
74
75inline std::string BuildTensorName(const char* tensorNamePrefix, std::size_t index)
76{
77 return tensorNamePrefix + std::to_string(index);
78}
79
Matteo Martincighe48bdff2018-09-03 13:50:50 +010080} // anonymous namespace
telsoa015307bc12018-03-09 13:51:08 +000081
telsoa01ce3e84a2018-08-31 09:31:35 +010082using namespace android::hardware;
83
telsoa015307bc12018-03-09 13:51:08 +000084namespace armnn_driver
85{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010086template<typename HalVersion>
Mike Kelly65c42dc2019-07-22 14:06:00 +010087RequestThread<ArmnnPreparedModel, HalVersion, ArmnnCallback_1_0> ArmnnPreparedModel<HalVersion>::m_RequestThread;
telsoa015307bc12018-03-09 13:51:08 +000088
Matteo Martincighe48bdff2018-09-03 13:50:50 +010089template<typename HalVersion>
telsoa015307bc12018-03-09 13:51:08 +000090template <typename TensorBindingCollection>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010091void ArmnnPreparedModel<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix,
92 const TensorBindingCollection& tensorBindings)
telsoa015307bc12018-03-09 13:51:08 +000093{
94 if (!m_RequestInputsAndOutputsDumpDir.empty())
95 {
96 const std::string requestName = boost::str(boost::format("%1%_%2%.dump") % m_NetworkId % m_RequestCount);
97 for (std::size_t i = 0u; i < tensorBindings.size(); ++i)
98 {
99 DumpTensor(m_RequestInputsAndOutputsDumpDir,
100 requestName,
101 BuildTensorName(tensorNamePrefix, i),
102 tensorBindings[i].second);
103 }
104 }
105}
106
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100107template<typename HalVersion>
108ArmnnPreparedModel<HalVersion>::ArmnnPreparedModel(armnn::NetworkId networkId,
109 armnn::IRuntime* runtime,
110 const HalModel& model,
111 const std::string& requestInputsAndOutputsDumpDir,
112 const bool gpuProfilingEnabled)
telsoa01ce3e84a2018-08-31 09:31:35 +0100113 : m_NetworkId(networkId)
114 , m_Runtime(runtime)
115 , m_Model(model)
116 , m_RequestCount(0)
117 , m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir)
118 , m_GpuProfilingEnabled(gpuProfilingEnabled)
telsoa015307bc12018-03-09 13:51:08 +0000119{
telsoa01ce3e84a2018-08-31 09:31:35 +0100120 // Enable profiling if required.
121 m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled);
telsoa015307bc12018-03-09 13:51:08 +0000122}
123
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100124template<typename HalVersion>
125ArmnnPreparedModel<HalVersion>::~ArmnnPreparedModel()
telsoa015307bc12018-03-09 13:51:08 +0000126{
telsoa01ce3e84a2018-08-31 09:31:35 +0100127 // Get a hold of the profiler used by this model.
128 std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId);
129
130 // Unload the network associated with this model.
telsoa015307bc12018-03-09 13:51:08 +0000131 m_Runtime->UnloadNetwork(m_NetworkId);
telsoa01ce3e84a2018-08-31 09:31:35 +0100132
133 // Dump the profiling info to a file if required.
134 DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get());
telsoa015307bc12018-03-09 13:51:08 +0000135}
136
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100137template<typename HalVersion>
Kevin Mayec1e5b82020-02-26 17:00:39 +0000138Return<V1_0::ErrorStatus> ArmnnPreparedModel<HalVersion>::execute(
139 const V1_0::Request& request,
140 const ::android::sp<V1_0::IExecutionCallback>& callback)
telsoa015307bc12018-03-09 13:51:08 +0000141{
142 ALOGV("ArmnnPreparedModel::execute(): %s", GetModelSummary(m_Model).c_str());
143 m_RequestCount++;
144
145 if (callback.get() == nullptr) {
146 ALOGE("ArmnnPreparedModel::execute invalid callback passed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000147 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000148 }
149
150 if (!android::nn::validateRequest(request, m_Model))
151 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000152 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::INVALID_ARGUMENT, "ArmnnPreparedModel::execute");
153 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa015307bc12018-03-09 13:51:08 +0000154 }
155
156 if (!m_RequestInputsAndOutputsDumpDir.empty())
157 {
158 ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(callback.get()));
159 }
160
161 // allocate the tensors on the heap, as they are passed to the request thread
162 auto pInputTensors = std::make_shared<armnn::InputTensors>();
163 auto pOutputTensors = std::make_shared<armnn::OutputTensors>();
164
165 // map the memory pool into shared pointers
166 // use a shared memory pools vector on the heap, as it is passed to the request thread
167 auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>();
168 if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools))
169 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000170 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
171 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000172 }
173
174 // add the inputs and outputs with their data
175 try
176 {
177 pInputTensors->reserve(request.inputs.size());
178 for (unsigned int i = 0; i < request.inputs.size(); i++)
179 {
180 const auto& inputArg = request.inputs[i];
181
182 const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
183 const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools);
184 if (inputTensor.GetMemoryArea() == nullptr)
185 {
186 ALOGE("Cannot execute request. Error converting request input %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000187 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000188 }
189
190 pInputTensors->emplace_back(i, inputTensor);
191 }
192
193 pOutputTensors->reserve(request.outputs.size());
194 for (unsigned int i = 0; i < request.outputs.size(); i++)
195 {
196 const auto& outputArg = request.outputs[i];
197
198 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
199 const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools);
200 if (outputTensor.GetMemoryArea() == nullptr)
201 {
202 ALOGE("Cannot execute request. Error converting request output %u to tensor", i);
Kevin Mayec1e5b82020-02-26 17:00:39 +0000203 return V1_0::ErrorStatus::GENERAL_FAILURE;
telsoa015307bc12018-03-09 13:51:08 +0000204 }
205
206 pOutputTensors->emplace_back(i, outputTensor);
207 }
208 }
Kevin May7bdaac52020-02-10 12:10:07 +0000209 catch (armnn::Exception& e)
210 {
211 ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000212 NotifyCallbackAndCheck(callback, V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
213 return V1_0::ErrorStatus::GENERAL_FAILURE;
Kevin May7bdaac52020-02-10 12:10:07 +0000214 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000215 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000216 {
Kevin May7bdaac52020-02-10 12:10:07 +0000217 ALOGE("std::exception caught while preparing for EnqueueWorkload: %s", e.what());
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 }
221
222 ALOGV("ArmnnPreparedModel::execute(...) before PostMsg");
telsoa015307bc12018-03-09 13:51:08 +0000223
Kevin Mayec1e5b82020-02-26 17:00:39 +0000224 auto cb = [callback](V1_0::ErrorStatus errorStatus, std::string callingFunction)
Mike Kelly65c42dc2019-07-22 14:06:00 +0100225 {
226 NotifyCallbackAndCheck(callback, errorStatus, callingFunction);
227 };
228
229 ArmnnCallback_1_0 armnnCb;
230 armnnCb.callback = cb;
231 // post the request for asynchronous execution
232 m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, armnnCb);
233 ALOGV("ArmnnPreparedModel::execute(...) after PostMsg");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000234 return V1_0::ErrorStatus::NONE; // successfully queued
telsoa015307bc12018-03-09 13:51:08 +0000235}
236
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100237template<typename HalVersion>
238void ArmnnPreparedModel<HalVersion>::ExecuteGraph(
239 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
240 std::shared_ptr<armnn::InputTensors>& pInputTensors,
241 std::shared_ptr<armnn::OutputTensors>& pOutputTensors,
Mike Kelly65c42dc2019-07-22 14:06:00 +0100242 ArmnnCallback_1_0 cb)
telsoa015307bc12018-03-09 13:51:08 +0000243{
244 ALOGV("ArmnnPreparedModel::ExecuteGraph(...)");
245
246 DumpTensorsIfRequired("Input", *pInputTensors);
247
248 // run it
249 try
250 {
Matthew Bentham16196e22019-04-01 17:17:58 +0100251 armnn::Status status = m_Runtime->EnqueueWorkload(m_NetworkId, *pInputTensors, *pOutputTensors);
252 if (status != armnn::Status::Success)
253 {
254 ALOGW("EnqueueWorkload failed");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000255 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Matthew Bentham16196e22019-04-01 17:17:58 +0100256 return;
257 }
telsoa015307bc12018-03-09 13:51:08 +0000258 }
Kevin May7bdaac52020-02-10 12:10:07 +0000259 catch (armnn::Exception& e)
260 {
261 ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000262 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
Kevin May7bdaac52020-02-10 12:10:07 +0000263 return;
264 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000265 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000266 {
Kevin May7bdaac52020-02-10 12:10:07 +0000267 ALOGE("std::exception caught from EnqueueWorkload: %s", e.what());
Kevin Mayec1e5b82020-02-26 17:00:39 +0000268 cb.callback(V1_0::ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000269 return;
270 }
271
272 DumpTensorsIfRequired("Output", *pOutputTensors);
273
274 // Commit output buffers.
275 // Note that we update *all* pools, even if they aren't actually used as outputs -
276 // this is simpler and is what the CpuExecutor does.
277 for (android::nn::RunTimePoolInfo& pool : *pMemPools)
278 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000279 // Type android::nn::RunTimePoolInfo has changed between Android P & Q and Android R, where
280 // update() has been removed and flush() added.
281 #if defined(ARMNN_ANDROID_R) // Use the new Android implementation.
282 pool.flush();
283 #else
284 pool.update();
285 #endif
telsoa015307bc12018-03-09 13:51:08 +0000286 }
287
Kevin Mayec1e5b82020-02-26 17:00:39 +0000288 cb.callback(V1_0::ErrorStatus::NONE, "ExecuteGraph");
telsoa015307bc12018-03-09 13:51:08 +0000289}
290
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100291template<typename HalVersion>
Matthew Bentham16196e22019-04-01 17:17:58 +0100292bool ArmnnPreparedModel<HalVersion>::ExecuteWithDummyInputs()
telsoa015307bc12018-03-09 13:51:08 +0000293{
294 std::vector<std::vector<char>> storage;
295 armnn::InputTensors inputTensors;
296 for (unsigned int i = 0; i < m_Model.inputIndexes.size(); i++)
297 {
298 const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
299 storage.emplace_back(inputTensorInfo.GetNumBytes());
300 const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data());
301
302 inputTensors.emplace_back(i, inputTensor);
303 }
304
305 armnn::OutputTensors outputTensors;
306 for (unsigned int i = 0; i < m_Model.outputIndexes.size(); i++)
307 {
308 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
309 storage.emplace_back(outputTensorInfo.GetNumBytes());
310 const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data());
311
312 outputTensors.emplace_back(i, outputTensor);
313 }
314
315 try
316 {
Matthew Bentham16196e22019-04-01 17:17:58 +0100317 armnn::Status status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors);
318 if (status != armnn::Status::Success)
319 {
320 ALOGW("ExecuteWithDummyInputs: EnqueueWorkload failed");
321 return false;
322 }
telsoa015307bc12018-03-09 13:51:08 +0000323 }
Kevin May7bdaac52020-02-10 12:10:07 +0000324 catch (armnn::Exception& e)
325 {
326 ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what());
327 return false;
328 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000329 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +0000330 {
Kevin May7bdaac52020-02-10 12:10:07 +0000331 ALOGE("ExecuteWithDummyInputs: std::exception caught from EnqueueWorkload: %s", e.what());
Matthew Bentham16196e22019-04-01 17:17:58 +0100332 return false;
telsoa015307bc12018-03-09 13:51:08 +0000333 }
Matthew Bentham16196e22019-04-01 17:17:58 +0100334 return true;
telsoa015307bc12018-03-09 13:51:08 +0000335}
336
arovir01b0717b52018-09-05 17:03:25 +0100337///
338/// Class template specializations
339///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100340
arovir01b0717b52018-09-05 17:03:25 +0100341template class ArmnnPreparedModel<hal_1_0::HalPolicy>;
342
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100343#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100344template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100345#endif
346
Mike Kellyb5fdf382019-06-11 16:35:25 +0100347#ifdef ARMNN_ANDROID_NN_V1_2
348template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
349template class ArmnnPreparedModel<hal_1_2::HalPolicy>;
350#endif
Nikhil Raj77605822018-09-03 11:25:56 +0100351} // namespace armnn_driver