blob: d7f727f577cd243b83c2aea591380ebbae42346f [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>
14
surmeh01deb3bdb2018-07-05 12:06:04 +010015#if defined(ARMNN_ANDROID_P)
16// The headers of the ML framework have changed between Android O and Android P.
17// The validation functions have been moved into their own header, ValidateHal.h.
18#include <ValidateHal.h>
19#endif
20
telsoa015307bc12018-03-09 13:51:08 +000021#include <cassert>
22#include <cinttypes>
23
24using namespace android;
25
26namespace
27{
28using namespace armnn_driver;
29
30void NotifyCallbackAndCheck(const ::android::sp<IExecutionCallback>& callback, ErrorStatus errorStatus,
31 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
42bool ValidateRequestArgument(const RequestArgument& requestArg, const armnn::TensorInfo& tensorInfo)
43{
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 {
55 if (requestArg.dimensions[d] != tensorInfo.GetShape()[d])
56 {
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
67armnn::Tensor GetTensorForRequestArgument(const RequestArgument& requestArg,
68 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{
90
Matteo Martincighe48bdff2018-09-03 13:50:50 +010091template<typename HalVersion>
92RequestThread<HalVersion> ArmnnPreparedModel<HalVersion>::m_RequestThread;
telsoa015307bc12018-03-09 13:51:08 +000093
Matteo Martincighe48bdff2018-09-03 13:50:50 +010094template<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 {
101 const std::string requestName = boost::str(boost::format("%1%_%2%.dump") % m_NetworkId % m_RequestCount);
102 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,
117 const bool gpuProfilingEnabled)
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)
telsoa015307bc12018-03-09 13:51:08 +0000124{
telsoa01ce3e84a2018-08-31 09:31:35 +0100125 // Enable profiling if required.
126 m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled);
telsoa015307bc12018-03-09 13:51:08 +0000127}
128
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100129template<typename HalVersion>
130ArmnnPreparedModel<HalVersion>::~ArmnnPreparedModel()
telsoa015307bc12018-03-09 13:51:08 +0000131{
telsoa01ce3e84a2018-08-31 09:31:35 +0100132 // Get a hold of the profiler used by this model.
133 std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId);
134
135 // Unload the network associated with this model.
telsoa015307bc12018-03-09 13:51:08 +0000136 m_Runtime->UnloadNetwork(m_NetworkId);
telsoa01ce3e84a2018-08-31 09:31:35 +0100137
138 // Dump the profiling info to a file if required.
139 DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get());
telsoa015307bc12018-03-09 13:51:08 +0000140}
141
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100142template<typename HalVersion>
143Return<ErrorStatus> ArmnnPreparedModel<HalVersion>::execute(const Request& request,
144 const ::android::sp<IExecutionCallback>& callback)
telsoa015307bc12018-03-09 13:51:08 +0000145{
146 ALOGV("ArmnnPreparedModel::execute(): %s", GetModelSummary(m_Model).c_str());
147 m_RequestCount++;
148
149 if (callback.get() == nullptr) {
150 ALOGE("ArmnnPreparedModel::execute invalid callback passed");
151 return ErrorStatus::INVALID_ARGUMENT;
152 }
153
154 if (!android::nn::validateRequest(request, m_Model))
155 {
156 NotifyCallbackAndCheck(callback, ErrorStatus::INVALID_ARGUMENT, "ArmnnPreparedModel::execute");
157 return ErrorStatus::INVALID_ARGUMENT;
158 }
159
160 if (!m_RequestInputsAndOutputsDumpDir.empty())
161 {
162 ALOGD("Dumping inputs and outputs for request %" PRIuPTR, reinterpret_cast<std::uintptr_t>(callback.get()));
163 }
164
165 // allocate the tensors on the heap, as they are passed to the request thread
166 auto pInputTensors = std::make_shared<armnn::InputTensors>();
167 auto pOutputTensors = std::make_shared<armnn::OutputTensors>();
168
169 // map the memory pool into shared pointers
170 // use a shared memory pools vector on the heap, as it is passed to the request thread
171 auto pMemPools = std::make_shared<std::vector<android::nn::RunTimePoolInfo>>();
172 if (!setRunTimePoolInfosFromHidlMemories(pMemPools.get(), request.pools))
173 {
174 NotifyCallbackAndCheck(callback, ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
175 return ErrorStatus::GENERAL_FAILURE;
176 }
177
178 // add the inputs and outputs with their data
179 try
180 {
181 pInputTensors->reserve(request.inputs.size());
182 for (unsigned int i = 0; i < request.inputs.size(); i++)
183 {
184 const auto& inputArg = request.inputs[i];
185
186 const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
187 const armnn::Tensor inputTensor = GetTensorForRequestArgument(inputArg, inputTensorInfo, *pMemPools);
188 if (inputTensor.GetMemoryArea() == nullptr)
189 {
190 ALOGE("Cannot execute request. Error converting request input %u to tensor", i);
191 return ErrorStatus::GENERAL_FAILURE;
192 }
193
194 pInputTensors->emplace_back(i, inputTensor);
195 }
196
197 pOutputTensors->reserve(request.outputs.size());
198 for (unsigned int i = 0; i < request.outputs.size(); i++)
199 {
200 const auto& outputArg = request.outputs[i];
201
202 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
203 const armnn::Tensor outputTensor = GetTensorForRequestArgument(outputArg, outputTensorInfo, *pMemPools);
204 if (outputTensor.GetMemoryArea() == nullptr)
205 {
206 ALOGE("Cannot execute request. Error converting request output %u to tensor", i);
207 return ErrorStatus::GENERAL_FAILURE;
208 }
209
210 pOutputTensors->emplace_back(i, outputTensor);
211 }
212 }
213 catch (armnn::Exception& e)
214 {
215 ALOGW("armnn::Exception caught while preparing for EnqueueWorkload: %s", e.what());
216 NotifyCallbackAndCheck(callback, ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::execute");
217 return ErrorStatus::GENERAL_FAILURE;
218 }
219
220 ALOGV("ArmnnPreparedModel::execute(...) before PostMsg");
221 // post the request for asynchronous execution
222 m_RequestThread.PostMsg(this, pMemPools, pInputTensors, pOutputTensors, callback);
223 ALOGV("ArmnnPreparedModel::execute(...) after PostMsg");
224
225 return ErrorStatus::NONE; // successfully queued
226}
227
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100228template<typename HalVersion>
229void ArmnnPreparedModel<HalVersion>::ExecuteGraph(
230 std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& pMemPools,
231 std::shared_ptr<armnn::InputTensors>& pInputTensors,
232 std::shared_ptr<armnn::OutputTensors>& pOutputTensors,
233 const ::android::sp<IExecutionCallback>& callback)
telsoa015307bc12018-03-09 13:51:08 +0000234{
235 ALOGV("ArmnnPreparedModel::ExecuteGraph(...)");
236
237 DumpTensorsIfRequired("Input", *pInputTensors);
238
239 // run it
240 try
241 {
242 m_Runtime->EnqueueWorkload(m_NetworkId, *pInputTensors, *pOutputTensors);
243 }
244 catch (armnn::Exception& e)
245 {
246 ALOGW("armnn::Exception caught from EnqueueWorkload: %s", e.what());
247 NotifyCallbackAndCheck(callback, ErrorStatus::GENERAL_FAILURE, "ArmnnPreparedModel::ExecuteGraph");
248 return;
249 }
250
251 DumpTensorsIfRequired("Output", *pOutputTensors);
252
253 // Commit output buffers.
254 // Note that we update *all* pools, even if they aren't actually used as outputs -
255 // this is simpler and is what the CpuExecutor does.
256 for (android::nn::RunTimePoolInfo& pool : *pMemPools)
257 {
258 pool.update();
259 }
260
261 NotifyCallbackAndCheck(callback, ErrorStatus::NONE, "ExecuteGraph");
262}
263
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100264template<typename HalVersion>
265void ArmnnPreparedModel<HalVersion>::ExecuteWithDummyInputs()
telsoa015307bc12018-03-09 13:51:08 +0000266{
267 std::vector<std::vector<char>> storage;
268 armnn::InputTensors inputTensors;
269 for (unsigned int i = 0; i < m_Model.inputIndexes.size(); i++)
270 {
271 const armnn::TensorInfo inputTensorInfo = m_Runtime->GetInputTensorInfo(m_NetworkId, i);
272 storage.emplace_back(inputTensorInfo.GetNumBytes());
273 const armnn::ConstTensor inputTensor(inputTensorInfo, storage.back().data());
274
275 inputTensors.emplace_back(i, inputTensor);
276 }
277
278 armnn::OutputTensors outputTensors;
279 for (unsigned int i = 0; i < m_Model.outputIndexes.size(); i++)
280 {
281 const armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkId, i);
282 storage.emplace_back(outputTensorInfo.GetNumBytes());
283 const armnn::Tensor outputTensor(outputTensorInfo, storage.back().data());
284
285 outputTensors.emplace_back(i, outputTensor);
286 }
287
288 try
289 {
290 m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors);
291 }
292 catch (armnn::Exception& e)
293 {
294 ALOGW("ExecuteWithDummyInputs: armnn::Exception caught from EnqueueWorkload: %s", e.what());
295 }
296}
297
arovir01b0717b52018-09-05 17:03:25 +0100298///
299/// Class template specializations
300///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100301
arovir01b0717b52018-09-05 17:03:25 +0100302template class ArmnnPreparedModel<hal_1_0::HalPolicy>;
303
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100304#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100305template class ArmnnPreparedModel<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100306#endif
307
Nikhil Raj77605822018-09-03 11:25:56 +0100308} // namespace armnn_driver