blob: ef37cae6c5f654ec836692db3d86f88808bd88e2 [file] [log] [blame]
telsoa01ce3e84a2018-08-31 09:31:35 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa01ce3e84a2018-08-31 09:31:35 +01004//
5
Matteo Martincighe48bdff2018-09-03 13:50:50 +01006#define LOG_TAG "ArmnnDriver"
7
telsoa01ce3e84a2018-08-31 09:31:35 +01008#include "ArmnnDriverImpl.hpp"
telsoa01ce3e84a2018-08-31 09:31:35 +01009#include "ArmnnPreparedModel.hpp"
telsoa01ce3e84a2018-08-31 09:31:35 +010010
Kevin May42477c12020-03-26 13:34:14 +000011#if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) // Using ::android::hardware::neuralnetworks::V1_2
Mike Kellyb5fdf382019-06-11 16:35:25 +010012#include "ArmnnPreparedModel_1_2.hpp"
telsoa01ce3e84a2018-08-31 09:31:35 +010013#endif
14
Kevin May42477c12020-03-26 13:34:14 +000015#ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_2
16#include "ArmnnPreparedModel_1_3.hpp"
17#endif
18
19#include "Utils.hpp"
20
Mike Kellyb5fdf382019-06-11 16:35:25 +010021#include "ModelToINetworkConverter.hpp"
22#include "SystemPropertiesUtils.hpp"
Colm Donelan08d9a1c2020-09-09 17:56:55 +010023
Mike Kellyb5fdf382019-06-11 16:35:25 +010024#include <ValidateHal.h>
telsoa01ce3e84a2018-08-31 09:31:35 +010025#include <log/log.h>
26
27using namespace std;
28using namespace android;
29using namespace android::nn;
30using namespace android::hardware;
31
32namespace
33{
34
Matthew Bentham912b3622019-05-03 15:49:14 +010035void NotifyCallbackAndCheck(const sp<V1_0::IPreparedModelCallback>& callback,
Kevin Mayec1e5b82020-02-26 17:00:39 +000036 V1_0::ErrorStatus errorStatus,
Matthew Bentham912b3622019-05-03 15:49:14 +010037 const sp<V1_0::IPreparedModel>& preparedModelPtr)
telsoa01ce3e84a2018-08-31 09:31:35 +010038{
39 Return<void> returned = callback->notify(errorStatus, preparedModelPtr);
40 // This check is required, if the callback fails and it isn't checked it will bring down the service
41 if (!returned.isOk())
42 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +010043 ALOGE("ArmnnDriverImpl::prepareModel: hidl callback failed to return properly: %s ",
Mike Kellyb5fdf382019-06-11 16:35:25 +010044 returned.description().c_str());
telsoa01ce3e84a2018-08-31 09:31:35 +010045 }
46}
47
Kevin Mayec1e5b82020-02-26 17:00:39 +000048Return<V1_0::ErrorStatus> FailPrepareModel(V1_0::ErrorStatus error,
49 const string& message,
50 const sp<V1_0::IPreparedModelCallback>& callback)
telsoa01ce3e84a2018-08-31 09:31:35 +010051{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010052 ALOGW("ArmnnDriverImpl::prepareModel: %s", message.c_str());
telsoa01ce3e84a2018-08-31 09:31:35 +010053 NotifyCallbackAndCheck(callback, error, nullptr);
54 return error;
55}
56
57} // namespace
58
59namespace armnn_driver
60{
telsoa01ce3e84a2018-08-31 09:31:35 +010061
arovir01b0717b52018-09-05 17:03:25 +010062template<typename HalPolicy>
Kevin Mayec1e5b82020-02-26 17:00:39 +000063Return<V1_0::ErrorStatus> ArmnnDriverImpl<HalPolicy>::prepareModel(
telsoa01ce3e84a2018-08-31 09:31:35 +010064 const armnn::IRuntimePtr& runtime,
65 const armnn::IGpuAccTunedParametersPtr& clTunedParameters,
66 const DriverOptions& options,
Matteo Martincighe48bdff2018-09-03 13:50:50 +010067 const HalModel& model,
Matthew Bentham912b3622019-05-03 15:49:14 +010068 const sp<V1_0::IPreparedModelCallback>& cb,
Matteo Martincighe48bdff2018-09-03 13:50:50 +010069 bool float32ToFloat16)
telsoa01ce3e84a2018-08-31 09:31:35 +010070{
Matteo Martincighe48bdff2018-09-03 13:50:50 +010071 ALOGV("ArmnnDriverImpl::prepareModel()");
telsoa01ce3e84a2018-08-31 09:31:35 +010072
73 if (cb.get() == nullptr)
74 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +010075 ALOGW("ArmnnDriverImpl::prepareModel: Invalid callback passed to prepareModel");
Kevin Mayec1e5b82020-02-26 17:00:39 +000076 return V1_0::ErrorStatus::INVALID_ARGUMENT;
telsoa01ce3e84a2018-08-31 09:31:35 +010077 }
78
79 if (!runtime)
80 {
Kevin Mayec1e5b82020-02-26 17:00:39 +000081 return FailPrepareModel(V1_0::ErrorStatus::DEVICE_UNAVAILABLE, "Device unavailable", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +010082 }
83
84 if (!android::nn::validateModel(model))
85 {
Kevin Mayec1e5b82020-02-26 17:00:39 +000086 return FailPrepareModel(V1_0::ErrorStatus::INVALID_ARGUMENT, "Invalid model passed as input", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +010087 }
88
89 // Deliberately ignore any unsupported operations requested by the options -
90 // at this point we're being asked to prepare a model that we've already declared support for
91 // and the operation indices may be different to those in getSupportedOperations anyway.
92 set<unsigned int> unsupportedOperations;
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010093 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
94 model,
95 unsupportedOperations);
telsoa01ce3e84a2018-08-31 09:31:35 +010096
97 if (modelConverter.GetConversionResult() != ConversionResult::Success)
98 {
Kevin Mayec1e5b82020-02-26 17:00:39 +000099 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "ModelToINetworkConverter failed", cb);
100 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100101 }
102
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100103 // Optimize the network
telsoa01ce3e84a2018-08-31 09:31:35 +0100104 armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr);
105 armnn::OptimizerOptions OptOptions;
106 OptOptions.m_ReduceFp32ToFp16 = float32ToFloat16;
107
Mike Kelly7ed56dd2020-09-30 20:22:56 +0100108 armnn::BackendOptions gpuAcc("GpuAcc",
109 {
Sadik Armaganf36e10b2021-01-11 16:34:01 +0000110 { "FastMathEnabled", options.IsFastMathEnabled() },
111 { "SaveCachedNetwork", options.SaveCachedNetwork() },
112 { "CachedNetworkFilePath", options.GetCachedNetworkFilePath() }
Mike Kelly7ed56dd2020-09-30 20:22:56 +0100113 });
114 armnn::BackendOptions cpuAcc("CpuAcc",
115 {
116 { "FastMathEnabled", options.IsFastMathEnabled() }
117 });
118 OptOptions.m_ModelOptions.push_back(gpuAcc);
119 OptOptions.m_ModelOptions.push_back(cpuAcc);
120
jimfly0107dedda2018-10-09 12:29:41 +0100121 std::vector<std::string> errMessages;
telsoa01ce3e84a2018-08-31 09:31:35 +0100122 try
123 {
124 optNet = armnn::Optimize(*modelConverter.GetINetwork(),
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100125 options.GetBackends(),
telsoa01ce3e84a2018-08-31 09:31:35 +0100126 runtime->GetDeviceSpec(),
jimfly0107dedda2018-10-09 12:29:41 +0100127 OptOptions,
128 errMessages);
telsoa01ce3e84a2018-08-31 09:31:35 +0100129 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000130 catch (std::exception &e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100131 {
132 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000133 message << "Exception (" << e.what() << ") caught from optimize.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000134 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
135 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100136 }
137
138 // Check that the optimized network is valid.
139 if (!optNet)
140 {
jimfly0107dedda2018-10-09 12:29:41 +0100141 stringstream message;
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100142 message << "Invalid optimized network";
143 for (const string& msg : errMessages)
144 {
jimfly0107dedda2018-10-09 12:29:41 +0100145 message << "\n" << msg;
146 }
Kevin Mayec1e5b82020-02-26 17:00:39 +0000147 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
148 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100149 }
150
151 // Export the optimized network graph to a dot file if an output dump directory
152 // has been specified in the drivers' arguments.
Jim Flynn829ad302019-12-13 14:43:24 +0000153 std::string dotGraphFileName = ExportNetworkGraphToDotFile(*optNet, options.GetRequestInputsAndOutputsDumpDir());
telsoa01ce3e84a2018-08-31 09:31:35 +0100154
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100155 // Load it into the runtime.
telsoa01ce3e84a2018-08-31 09:31:35 +0100156 armnn::NetworkId netId = 0;
157 try
158 {
159 if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success)
160 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000161 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100162 }
163 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000164 catch (std::exception& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100165 {
166 stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000167 message << "Exception (" << e.what()<< ") caught from LoadNetwork.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000168 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
169 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100170 }
171
Jim Flynn829ad302019-12-13 14:43:24 +0000172 // Now that we have a networkId for the graph rename the dump file to use it
173 // so that we can associate the graph file and the input/output tensor dump files
174 RenameGraphDotFile(dotGraphFileName,
175 options.GetRequestInputsAndOutputsDumpDir(),
176 netId);
177
Kevin Mayd5e94652019-11-07 14:02:14 +0000178 sp<ArmnnPreparedModel<HalPolicy>> preparedModel(
Mike Kellyb5fdf382019-06-11 16:35:25 +0100179 new ArmnnPreparedModel<HalPolicy>(
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100180 netId,
181 runtime.get(),
182 model,
183 options.GetRequestInputsAndOutputsDumpDir(),
184 options.IsGpuProfilingEnabled()));
telsoa01ce3e84a2018-08-31 09:31:35 +0100185
186 // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if
187 // this is enabled) before the first 'real' inference which removes the overhead of the first inference.
Matthew Bentham16196e22019-04-01 17:17:58 +0100188 if (!preparedModel->ExecuteWithDummyInputs())
189 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000190 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb);
Matthew Bentham16196e22019-04-01 17:17:58 +0100191 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100192
193 if (clTunedParameters &&
194 options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters)
195 {
196 // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file.
197 try
198 {
199 clTunedParameters->Save(options.GetClTunedParametersFile().c_str());
200 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000201 catch (std::exception& error)
telsoa01ce3e84a2018-08-31 09:31:35 +0100202 {
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100203 ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s",
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100204 options.GetClTunedParametersFile().c_str(), error.what());
telsoa01ce3e84a2018-08-31 09:31:35 +0100205 }
206 }
207
Kevin Mayec1e5b82020-02-26 17:00:39 +0000208 NotifyCallbackAndCheck(cb, V1_0::ErrorStatus::NONE, preparedModel);
telsoa01ce3e84a2018-08-31 09:31:35 +0100209
Kevin Mayec1e5b82020-02-26 17:00:39 +0000210 return V1_0::ErrorStatus::NONE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100211}
212
arovir01b0717b52018-09-05 17:03:25 +0100213template<typename HalPolicy>
Mike Kellyb5fdf382019-06-11 16:35:25 +0100214Return<void> ArmnnDriverImpl<HalPolicy>::getSupportedOperations(const armnn::IRuntimePtr& runtime,
215 const DriverOptions& options,
216 const HalModel& model,
217 HalGetSupportedOperations_cb cb)
218{
Jim Flynn829ad302019-12-13 14:43:24 +0000219 std::stringstream ss;
220 ss << "ArmnnDriverImpl::getSupportedOperations()";
221 std::string fileName;
222 std::string timestamp;
223 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
224 {
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100225 ss << " : "
226 << options.GetRequestInputsAndOutputsDumpDir()
227 << "/"
228 << GetFileTimestamp()
229 << "_getSupportedOperations.txt";
Jim Flynn829ad302019-12-13 14:43:24 +0000230 }
231 ALOGV(ss.str().c_str());
232
233 if (!options.GetRequestInputsAndOutputsDumpDir().empty())
234 {
235 //dump the marker file
236 std::ofstream fileStream;
237 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
238 if (fileStream.good())
239 {
240 fileStream << timestamp << std::endl;
241 }
242 fileStream.close();
243 }
Mike Kellyb5fdf382019-06-11 16:35:25 +0100244
245 vector<bool> result;
246
247 if (!runtime)
248 {
Kevin May42477c12020-03-26 13:34:14 +0000249 cb(HalErrorStatus::DEVICE_UNAVAILABLE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100250 return Void();
251 }
252
253 // Run general model validation, if this doesn't pass we shouldn't analyse the model anyway.
254 if (!android::nn::validateModel(model))
255 {
Kevin May42477c12020-03-26 13:34:14 +0000256 cb(HalErrorStatus::INVALID_ARGUMENT, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100257 return Void();
258 }
259
260 // Attempt to convert the model to an ArmNN input network (INetwork).
261 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
262 model,
263 options.GetForcedUnsupportedOperations());
264
265 if (modelConverter.GetConversionResult() != ConversionResult::Success
266 && modelConverter.GetConversionResult() != ConversionResult::UnsupportedFeature)
267 {
Kevin May42477c12020-03-26 13:34:14 +0000268 cb(HalErrorStatus::GENERAL_FAILURE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100269 return Void();
270 }
271
272 // Check each operation if it was converted successfully and copy the flags
273 // into the result (vector<bool>) that we need to return to Android.
Kevin May42477c12020-03-26 13:34:14 +0000274 result.reserve(getMainModel(model).operations.size());
275 for (uint32_t operationIdx = 0;
276 operationIdx < getMainModel(model).operations.size();
277 ++operationIdx)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100278 {
279 bool operationSupported = modelConverter.IsOperationSupported(operationIdx);
280 result.push_back(operationSupported);
281 }
282
Kevin May42477c12020-03-26 13:34:14 +0000283 cb(HalErrorStatus::NONE, result);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100284 return Void();
285}
286
287template<typename HalPolicy>
arovir01b0717b52018-09-05 17:03:25 +0100288Return<DeviceStatus> ArmnnDriverImpl<HalPolicy>::getStatus()
telsoa01ce3e84a2018-08-31 09:31:35 +0100289{
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100290 ALOGV("ArmnnDriver::getStatus()");
telsoa01ce3e84a2018-08-31 09:31:35 +0100291
292 return DeviceStatus::AVAILABLE;
293}
294
arovir01b0717b52018-09-05 17:03:25 +0100295///
296/// Class template specializations
297///
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100298
arovir01b0717b52018-09-05 17:03:25 +0100299template class ArmnnDriverImpl<hal_1_0::HalPolicy>;
300
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100301#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100302template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100303#endif
304
Mike Kellyb5fdf382019-06-11 16:35:25 +0100305#ifdef ARMNN_ANDROID_NN_V1_2
306template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
307template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
308#endif
309
Kevin May42477c12020-03-26 13:34:14 +0000310#ifdef ARMNN_ANDROID_NN_V1_3
311template class ArmnnDriverImpl<hal_1_1::HalPolicy>;
312template class ArmnnDriverImpl<hal_1_2::HalPolicy>;
313template class ArmnnDriverImpl<hal_1_3::HalPolicy>;
314#endif
315
Matteo Martincigh8d50f8f2018-10-25 15:39:33 +0100316} // namespace armnn_driver